Debian Codegraph @

src/network/ubiquiti/unifi/snmp/mode/virtualaccesspoints.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package network::ubiquiti::unifi::snmp::mode::virtualaccesspoints;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
28 use Digest::MD5 qw(md5_hex);
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 return sprintf(
34 'status: %s',
35 $self->{result_values}->{status}
36 );
37 }
38
39 sub prefix_vap_output {
40 my ($self, %options) = @_;
41
42 return sprintf(
43 "Virtual access point '%s' [ssid: %s] ",
44 $options{instance_value}->{name},
45 $options{instance_value}->{ssid}
46 );
47 }
48
49 sub set_counters {
50 my ($self, %options) = @_;
51
52 $self->{maps_counters_type} = [
53 { name => 'global', type => 0 },
54 { name => 'vaps', type => 1, cb_prefix_output => 'prefix_vap_output', message_multiple => 'All virtual access points are ok' }
55 ];
56
57 $self->{maps_counters}->{global} = [
58 { label => 'total', nlabel => 'virtual_access_points.total.count', display_ok => 0, set => {
59 key_values => [ { name => 'total' } ],
60 output_template => 'total: %s',
61 perfdatas => [
62 { template => '%s', min => 0 }
63 ]
64 }
65 },
66 { label => 'total-clients-connected', nlabel => 'virtual_access_points.clients.connected.count', display_ok => 0, set => {
67 key_values => [ { name => 'clients_connected' } ],
68 output_template => 'clients connected: %s',
69 perfdatas => [
70 { template => '%s', min => 0 }
71 ]
72 }
73 }
74 ];
75
76 $self->{maps_counters}->{vaps} = [
77 {
78 label => 'status', type => 2, critical_default => '%{status} eq "down"',
79 set => {
80 key_values => [
81 { name => 'name' }, { name => 'ssid' },
82 { name => 'status' }
83 ],
84 closure_custom_output => $self->can('custom_status_output'),
85 closure_custom_perfdata => sub { return 0; },
86 closure_custom_threshold_check => \&catalog_status_threshold_ng
87 }
88 },
89 { label => 'clients-connected', nlabel => 'virtual_access_point.clients.connected.count', set => {
90 key_values => [ { name => 'clients' }, { name => 'name' } ],
91 output_template => 'clients connected: %s',
92 perfdatas => [
93 { template => '%s', min => 0, label_extra_instance => 1, instance_use => 'name' }
94 ]
95 }
96 },
97 { label => 'traffic-in', nlabel => 'virtual_access_point.traffic.in.bitspersecond', set => {
98 key_values => [ { name => 'traffic_in', per_second => 1 }, { name => 'name' } ],
99 output_template => 'traffic in: %s %s/s',
100 output_change_bytes => 2,
101 perfdatas => [
102 { template => '%s', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'name' }
103 ]
104 }
105 },
106 { label => 'traffic-out', nlabel => 'virtual_access_point.traffic.out.bitspersecond', set => {
107 key_values => [ { name => 'traffic_out', per_second => 1 }, { name => 'name' } ],
108 output_template => 'traffic out: %s %s/s',
109 output_change_bytes => 2,
110 perfdatas => [
111 { template => '%s', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'name' }
112 ]
113 }
114 }
115 ];
116 }
117
118 sub new {
119 my ($class, %options) = @_;
120 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
121 bless $self, $class;
122
123 $options{options}->add_options(arguments => {
124 'filter-name:s' => { name => 'filter_name' },
125 'filter-ssid:s' => { name => 'filter_ssid' }
126 });
127
128 return $self;
129 }
130
131 my $map_status = { 1 => 'up', 2 => 'down' };
132
133 my $mapping = {
134 filters => {
135 ssid => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.6' }, # unifiVapEssId
136 name => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.7' } # unifiVapName
137 },
138 metrics => {
139 clients => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.8' }, # unifiVapNumStations
140 traffic_in => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.10' }, # unifiVapRxBytes
141 traffic_out => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.16' }, # unifiVapTxBytes
142 status => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.22', map => $map_status } # unifiVapUp
143 }
144 };
145
146 sub manage_selection {
147 my ($self, %options) = @_;
148
149 my $oid_vap_table = '.1.3.6.1.4.1.41112.1.6.1.2'; # unifiVapTable
150 my $snmp_result = $options{snmp}->get_table(
151 oid => $oid_vap_table,
152 start => $mapping->{filters}->{ssid}->{oid},
153 end => $mapping->{filters}->{name}->{oid},
154 nothing_quit => 1
155 );
156
157 $self->{vaps} = {};
158 foreach (keys %$snmp_result) {
159 next if (! /^$mapping->{filters}->{name}->{oid}\.(.*)$/);
160 my $instance = $1;
161
162 my $result = $options{snmp}->map_instance(mapping => $mapping->{filters}, results => $snmp_result, instance => $instance);
163 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
164 $result->{name} !~ /$self->{option_results}->{filter_name}/) {
165 $self->{output}->output_add(long_msg => "skipping '" . $result->{name} . "': no matching filter.", debug => 1);
166 next;
167 }
168 if (defined($self->{option_results}->{filter_ssid}) && $self->{option_results}->{filter_ssid} ne '' &&
169 $result->{ssid} !~ /$self->{option_results}->{filter_ssid}/) {
170 $self->{output}->output_add(long_msg => "skipping '" . $result->{name} . "': no matching filter.", debug => 1);
171 next;
172 }
173
174 $self->{vaps}->{$instance} = $result;
175 }
176
177 $self->{global} = { total => scalar(keys %{$self->{vaps}}), clients_connected => 0 };
178
179 return if (scalar(keys %{$self->{vaps}}) <= 0);
180
181 $options{snmp}->load(
182 oids => [
183 map($_->{oid}, values(%{$mapping->{metrics}}))
184 ],
185 instances => [keys %{$self->{vaps}}],
186 instance_regexp => '^(.*)$'
187 );
188 $snmp_result = $options{snmp}->get_leef(nothing_quit => 1);
189
190 foreach (keys %{$self->{vaps}}) {
191 my $result = $options{snmp}->map_instance(mapping => $mapping->{metrics}, results => $snmp_result, instance => $_);
192
193 $result->{traffic_in} *= 8;
194 $result->{traffic_out} *= 8;
195 $self->{vaps}->{$_} = { %{$self->{vaps}->{$_}}, %$result };
196 $self->{global}->{clients_connected} += $result->{clients};
197 }
198
199 $self->{cache_name} = 'ubiquiti_unifi_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
200 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
201 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' .
202 (defined($self->{option_results}->{filter_ssid}) ? md5_hex($self->{option_results}->{filter_ssid}) : md5_hex('all'));
203 }
204
205 1;
206
207 __END__
208
209 =head1 MODE
210
211 Check virtual access points.
212
213 =over 8
214
215 =item B<--filter-counters>
216
217 Only display some counters (regexp can be used).
218 Example: --filter-counters='status'
219
220 =item B<--filter-name>
221
222 Filter virtual access points by name (can be a regexp).
223
224 =item B<--filter-ssid>
225
226 Filter virtual access points by SSID (can be a regexp).
227
228 =item B<--unknown-status>
229
230 Define the conditions to match for the status to be UNKNOWN.
231 You can use the following variables: %{name}, %{ssid}, %{status}
232
233 =item B<--warning-status>
234
235 Define the conditions to match for the status to be WARNING.
236 You can use the following variables: %{name}, %{ssid}, %{status}
237
238 =item B<--critical-status>
239
240 Define the conditions to match for the status to be CRITICAL (default: '%{status} eq "down"').
241 You can use the following variables: %{name}, %{ssid}, %{status}
242
243 =item B<--warning-*> B<--critical-*>
244
245 Thresholds.
246 Can be: 'total', 'total-clients-connected', 'clients-connected', 'traffic-in', 'traffic-out'.
247
248 =back
249
250 =cut