From 4c643145e12e5524f7d791c2ed3eee21557212c4 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Fri, 7 May 2021 14:30:07 +0200 Subject: [PATCH] add(plugin): ubiquiti unifi snmp (#2776) --- .../snmp/mode/listvirtualaccesspoints.pm | 130 +++++++++ .../unifi/snmp/mode/virtualaccesspoints.pm | 250 ++++++++++++++++++ network/ubiquiti/unifi/snmp/plugin.pm | 57 ++++ 3 files changed, 437 insertions(+) create mode 100644 network/ubiquiti/unifi/snmp/mode/listvirtualaccesspoints.pm create mode 100644 network/ubiquiti/unifi/snmp/mode/virtualaccesspoints.pm create mode 100644 network/ubiquiti/unifi/snmp/plugin.pm diff --git a/network/ubiquiti/unifi/snmp/mode/listvirtualaccesspoints.pm b/network/ubiquiti/unifi/snmp/mode/listvirtualaccesspoints.pm new file mode 100644 index 000000000..52409485a --- /dev/null +++ b/network/ubiquiti/unifi/snmp/mode/listvirtualaccesspoints.pm @@ -0,0 +1,130 @@ +# +# Copyright 2021 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package network::ubiquiti::unifi::snmp::mode::listvirtualaccesspoints; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => {}); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +my $map_status = { 1 => 'up', 2 => 'down' }; + +my $mapping = { + ssid => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.6' }, # unifiVapEssId + name => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.7' } # unifiVapName +}; +my $mapping2 = { + status => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.22', map => $map_status } # unifiVapUp +}; + +sub manage_selection { + my ($self, %options) = @_; + + my $oid_vap_table = '.1.3.6.1.4.1.41112.1.6.1.2'; # unifiVapTable + my $snmp_result = $options{snmp}->get_table( + oid => $oid_vap_table, + start => $mapping->{ssid}->{oid}, + end => $mapping->{name}->{oid} + ); + my $results = {}; + foreach (keys %$snmp_result) { + next if (! /^$mapping->{name}->{oid}\.(.*)$/); + + $results->{$1} = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $1); + } + + $options{snmp}->load( + oids => [ map($_->{oid}, values(%$mapping2)) ], + instances => [ map($_, keys(%$results)) ], + instance_regexp => '^(.*)$' + ); + $snmp_result = $options{snmp}->get_leef(); + foreach (keys %$results) { + my $result = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result, instance => $_); + + $results->{$_} = { %$result, %{$results->{$_}} }; + } + + return $results; +} + +sub run { + my ($self, %options) = @_; + + my $results = $self->manage_selection(snmp => $options{snmp}); + foreach my $instance (sort keys %$results) { + $self->{output}->output_add( + long_msg => join('', map("[$_ = " . $results->{$instance}->{$_} . ']', (keys(%$mapping), keys(%$mapping2)))) + ); + } + + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List virtaul access points:' + ); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => [keys(%$mapping), keys(%$mapping2)]); +} + +sub disco_show { + my ($self, %options) = @_; + + my $results = $self->manage_selection(snmp => $options{snmp}); + foreach (sort keys %$results) { + $self->{output}->add_disco_entry( + %{$results->{$_}} + ); + } +} +1; + +__END__ + +=head1 MODE + +List virtual access points. + +=over 8 + +=back + +=cut diff --git a/network/ubiquiti/unifi/snmp/mode/virtualaccesspoints.pm b/network/ubiquiti/unifi/snmp/mode/virtualaccesspoints.pm new file mode 100644 index 000000000..3fbe69616 --- /dev/null +++ b/network/ubiquiti/unifi/snmp/mode/virtualaccesspoints.pm @@ -0,0 +1,250 @@ +# +# Copyright 2021 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package network::ubiquiti::unifi::snmp::mode::virtualaccesspoints; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); +use Digest::MD5 qw(md5_hex); + +sub custom_status_output { + my ($self, %options) = @_; + + return sprintf( + 'status: %s', + $self->{result_values}->{status} + ); +} + +sub prefix_vap_output { + my ($self, %options) = @_; + + return sprintf( + "Virtual access point '%s' [ssid: %s] ", + $options{instance_value}->{name}, + $options{instance_value}->{ssid} + ); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, + { name => 'vaps', type => 1, cb_prefix_output => 'prefix_vap_output', message_multiple => 'All virtual access points are ok' } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'total', nlabel => 'virtual_access_points.total.count', display_ok => 0, set => { + key_values => [ { name => 'total' } ], + output_template => 'total: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + }, + { label => 'total-clients-connected', nlabel => 'virtual_access_points.clients.connected.count', display_ok => 0, set => { + key_values => [ { name => 'clients_connected' } ], + output_template => 'clients connected: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + } + ]; + + $self->{maps_counters}->{vaps} = [ + { + label => 'status', type => 2, critical_default => '%{status} eq "down"', + set => { + key_values => [ + { name => 'name' }, { name => 'ssid' }, + { name => 'status' } + ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + }, + { label => 'clients-connected', nlabel => 'virtual_access_point.clients.connected.count', set => { + key_values => [ { name => 'clients' }, { name => 'name' } ], + output_template => 'clients connected: %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1, instance_use => 'name' } + ] + } + }, + { label => 'traffic-in', nlabel => 'virtual_access_point.traffic.in.bitspersecond', set => { + key_values => [ { name => 'traffic_in', per_second => 1 }, { name => 'name' } ], + output_template => 'traffic in: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { template => '%s', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'name' } + ] + } + }, + { label => 'traffic-out', nlabel => 'virtual_access_point.traffic.out.bitspersecond', set => { + key_values => [ { name => 'traffic_out', per_second => 1 }, { name => 'name' } ], + output_template => 'traffic out: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { template => '%s', min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'name' } + ] + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'filter-name:s' => { name => 'filter_name' }, + 'filter-ssid:s' => { name => 'filter_ssid' } + }); + + return $self; +} + +my $map_status = { 1 => 'up', 2 => 'down' }; + +my $mapping = { + filters => { + ssid => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.6' }, # unifiVapEssId + name => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.7' } # unifiVapName + }, + metrics => { + clients => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.8' }, # unifiVapNumStations + traffic_in => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.10' }, # unifiVapRxBytes + traffic_out => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.16' }, # unifiVapTxBytes + status => { oid => '.1.3.6.1.4.1.41112.1.6.1.2.1.22', map => $map_status } # unifiVapUp + } +}; + +sub manage_selection { + my ($self, %options) = @_; + + my $oid_vap_table = '.1.3.6.1.4.1.41112.1.6.1.2'; # unifiVapTable + my $snmp_result = $options{snmp}->get_table( + oid => $oid_vap_table, + start => $mapping->{filters}->{ssid}->{oid}, + end => $mapping->{filters}->{name}->{oid}, + nothing_quit => 1 + ); + + $self->{vaps} = {}; + foreach (keys %$snmp_result) { + next if (! /^$mapping->{filters}->{name}->{oid}\.(.*)$/); + my $instance = $1; + + my $result = $options{snmp}->map_instance(mapping => $mapping->{filters}, results => $snmp_result, instance => $instance); + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $result->{name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $result->{name} . "': no matching filter.", debug => 1); + next; + } + if (defined($self->{option_results}->{filter_ssid}) && $self->{option_results}->{filter_ssid} ne '' && + $result->{ssid} !~ /$self->{option_results}->{filter_ssid}/) { + $self->{output}->output_add(long_msg => "skipping '" . $result->{name} . "': no matching filter.", debug => 1); + next; + } + + $self->{vaps}->{$instance} = $result; + } + + $self->{global} = { total => scalar(keys %{$self->{vaps}}), clients_connected => 0 }; + + return if (scalar(keys %{$self->{vaps}}) <= 0); + + $options{snmp}->load( + oids => [ + map($_->{oid}, values(%{$mapping->{metrics}})) + ], + instances => [keys %{$self->{vaps}}], + instance_regexp => '^(.*)$' + ); + $snmp_result = $options{snmp}->get_leef(nothing_quit => 1); + + foreach (keys %{$self->{vaps}}) { + my $result = $options{snmp}->map_instance(mapping => $mapping->{metrics}, results => $snmp_result, instance => $_); + + $result->{traffic_in} *= 8; + $result->{traffic_out} *= 8; + $self->{vaps}->{$_} = { %{$self->{vaps}->{$_}}, %$result }; + $self->{global}->{clients_connected} += $result->{clients}; + } + + $self->{cache_name} = 'ubiquiti_unifi_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_ssid}) ? md5_hex($self->{option_results}->{filter_ssid}) : md5_hex('all')); +} + +1; + +__END__ + +=head1 MODE + +Check virtual access points. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='status' + +=item B<--filter-name> + +Filter virtual access points by name (can be a regexp). + +=item B<--filter-ssid> + +Filter virtual access points by SSID (can be a regexp). + +=item B<--unknown-status> + +Set unknown threshold for status. +Can used special variables like: %{name}, %{ssid}, %{status} + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{name}, %{ssid}, %{status} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} eq "down"'). +Can used special variables like: %{name}, %{ssid}, %{status} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'total', 'total-clients-connected', 'clients-connected', 'traffic-in', 'traffic-out'. + +=back + +=cut diff --git a/network/ubiquiti/unifi/snmp/plugin.pm b/network/ubiquiti/unifi/snmp/plugin.pm new file mode 100644 index 000000000..0a449b1ec --- /dev/null +++ b/network/ubiquiti/unifi/snmp/plugin.pm @@ -0,0 +1,57 @@ +# +# Copyright 2021 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package network::ubiquiti::unifi::snmp::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_snmp); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $self->{modes} = { + 'cpu' => 'snmp_standard::mode::cpudetailed', + 'disks' => 'snmp_standard::mode::diskusage', + 'load' => 'snmp_standard::mode::loadaverage', + 'interfaces' => 'snmp_standard::mode::interfaces', + 'list-virtual-access-points' => 'network::ubiquiti::unifi::snmp::mode::listvirtualaccesspoints', + 'list-interfaces' => 'snmp_standard::mode::listinterfaces', + 'memory' => 'snmp_standard::mode::memory', + 'swap' => 'snmp_standard::mode::swap', + 'uptime' => 'snmp_standard::mode::uptime', + 'virtual-access-points' => 'network::ubiquiti::unifi::snmp::mode::virtualaccesspoints' + }; + + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Ubiquiti UniFi in SNMP. + +=cut