From 7c293cb653caea3dc429912c0a701b3b4f7627b2 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Thu, 15 Oct 2020 15:02:54 +0200 Subject: [PATCH] dlink add new modes (#2256) --- .../dlink/standard/snmp/mode/interfaces.pm | 253 ++++++++++++++++ .../network/dlink/standard/snmp/mode/stack.pm | 282 ++++++++++++++++++ .../network/dlink/standard/snmp/plugin.pm | 5 +- 3 files changed, 538 insertions(+), 2 deletions(-) create mode 100644 centreon-plugins/network/dlink/standard/snmp/mode/interfaces.pm create mode 100644 centreon-plugins/network/dlink/standard/snmp/mode/stack.pm diff --git a/centreon-plugins/network/dlink/standard/snmp/mode/interfaces.pm b/centreon-plugins/network/dlink/standard/snmp/mode/interfaces.pm new file mode 100644 index 000000000..e9ebed56c --- /dev/null +++ b/centreon-plugins/network/dlink/standard/snmp/mode/interfaces.pm @@ -0,0 +1,253 @@ +# +# Copyright 2020 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::dlink::standard::snmp::mode::interfaces; + +use base qw(snmp_standard::mode::interfaces); + +use strict; +use warnings; + +sub custom_status_output { + my ($self, %options) = @_; + my $msg = 'Status : ' . $self->{result_values}->{opstatus} . ' (admin: ' . $self->{result_values}->{admstatus} . ')'; + if (defined($self->{instance_mode}->{option_results}->{add_duplex_status})) { + $msg .= ' (duplex: ' . $self->{result_values}->{duplexstatus} . ')'; + } + if (defined($self->{instance_mode}->{option_results}->{add_err_disable})) { + $msg .= ' (error disable: ' . $self->{result_values}->{errdisable} . ')'; + } + + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->SUPER::custom_status_calc(%options); + $self->{result_values}->{errdisable} = $options{new_datas}->{$self->{instance} . '_errdisable'}; + return 0; +} + +sub set_key_values_status { + my ($self, %options) = @_; + + return [ { name => 'opstatus' }, { name => 'admstatus' }, { name => 'duplexstatus' }, { name => 'errdisable' }, { name => 'display' } ]; +} + +sub set_oids_status { + my ($self, %options) = @_; + + $self->SUPER::set_oids_status(%options); + $self->{oid_dErrDisIfStatusTimeLeft} = '.1.3.6.1.4.1.171.14.45.1.3.1.4'; + $self->{oid_esErrDisIfStatusTimeLeft} = '.1.3.6.1.4.1.171.17.45.1.3.1.4'; + $self->{oid_errDisIfStatusDisReason_mapping} = { + 1 => 'loopbackDetect', 2 => 'l2ptGuard', + 3 => 'psecureViolation', 4 => 'stormControl', 5 => 'bpduProtect', + 6 => 'arpRateLimit', 7 => 'dhcpRateLimit', 8 => 'ddm', + 9 => 'scheduledShutdown', 10 => 'scheduledHibernation', 11 => 'duld' + }; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'add-err-disable' => { name => 'add_err_disable' } + }); + + return $self; +} + +sub load_status { + my ($self, %options) = @_; + + $self->SUPER::load_status(%options); + if (defined($self->{option_results}->{add_err_disable})) { + $self->{snmp_errdisable_result}->{ $self->{oid_dErrDisIfStatusTimeLeft} } = $self->{snmp}->get_table(oid => $self->{oid_dErrDisIfStatusTimeLeft}); + $self->{snmp_errdisable_result}->{ $self->{oid_esErrDisIfStatusTimeLeft} } = $self->{snmp}->get_table(oid => $self->{oid_esErrDisIfStatusTimeLeft}); + } +} + +sub add_result_status { + my ($self, %options) = @_; + + $self->SUPER::add_result_status(%options); + + $self->{int}->{ $options{instance} }->{errdisable} = ''; + if (defined($self->{option_results}->{add_err_disable})) { + my $append = ''; + # ifIndex.vlanIndex (if physical interface, vlanIndex = 0) + foreach my $branch (($self->{oid_dErrDisIfStatusTimeLeft}, $self->{oid_esErrDisIfStatusTimeLeft})) { + foreach (keys %{$self->{snmp_errdisable_result}->{$branch}}) { + next if (! /^$branch\.$options{instance}\.(\d+)\.(\d+)/); + my ($vlan, $reason) = ($1, $2); + if ($vlan == 0) { + $self->{int}->{ $options{instance} }->{errdisable} = $self->{oid_errDisIfStatusDisReason_mapping}->{$reason}; + last; + } + + $self->{int}->{$options{instance}}->{errdisable} .= $append . 'vlan' . $vlan . ':' . $self->{oid_errDisIfStatusDisReason_mapping}->{$reason}; + $append = ','; + } + } + } + + $self->{int}->{$options{instance}}->{errdisable} = '-' + if ($self->{int}->{$options{instance}}->{errdisable} eq ''); +} + +1; + +__END__ + +=head1 MODE + +Check interfaces. + +=over 8 + +=item B<--add-global> + +Check global port statistics (By default if no --add-* option is set). + +=item B<--add-status> + +Check interface status. + +=item B<--add-duplex-status> + +Check duplex status (with --warning-status and --critical-status). + +=item B<--add-err-disable> + +Check error disable (with --warning-status and --critical-status). + +=item B<--add-traffic> + +Check interface traffic. + +=item B<--add-errors> + +Check interface errors. + +=item B<--add-cast> + +Check interface cast. + +=item B<--add-speed> + +Check interface speed. + +=item B<--add-volume> + +Check interface data volume between two checks (not supposed to be graphed, useful for BI reporting). + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{admstatus}, %{opstatus}, %{duplexstatus}, %{errdisable}, %{display} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{admstatus} eq "up" and %{opstatus} ne "up"'). +Can used special variables like: %{admstatus}, %{opstatus}, %{duplexstatus}, %{errdisable}, %{display} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'total-port', 'total-admin-up', 'total-admin-down', 'total-oper-up', 'total-oper-down', +'in-traffic', 'out-traffic', 'in-error', 'in-discard', 'out-error', 'out-discard', +'in-ucast' (%), 'in-bcast' (%), 'in-mcast' (%), 'out-ucast' (%), 'out-bcast' (%), 'out-mcast' (%), +'speed' (b/s). + +=item B<--units-traffic> + +Units of thresholds for the traffic (Default: '%') ('%', 'b/s'). + +=item B<--units-errors> + +Units of thresholds for errors/discards (Default: '%') ('%', 'absolute'). + +=item B<--nagvis-perfdata> + +Display traffic perfdata to be compatible with nagvis widget. + +=item B<--interface> + +Set the interface (number expected) ex: 1,2,... (empty means 'check all interface'). + +=item B<--name> + +Allows to use interface name with option --interface instead of interface oid index (Can be a regexp) + +=item B<--speed> + +Set interface speed for incoming/outgoing traffic (in Mb). + +=item B<--speed-in> + +Set interface speed for incoming traffic (in Mb). + +=item B<--speed-out> + +Set interface speed for outgoing traffic (in Mb). + +=item B<--no-skipped-counters> + +Don't skip counters when no change. + +=item B<--force-counters32> + +Force to use 32 bits counters (even in snmp v2c and v3). Should be used when 64 bits counters are buggy. + +=item B<--reload-cache-time> + +Time in minutes before reloading cache file (default: 180). + +=item B<--oid-filter> + +Choose OID used to filter interface (default: ifName) (values: ifDesc, ifAlias, ifName, IpAddr). + +=item B<--oid-display> + +Choose OID used to display interface (default: ifName) (values: ifDesc, ifAlias, ifName, IpAddr). + +=item B<--oid-extra-display> + +Add an OID to display. + +=item B<--display-transform-src> + +Regexp src to transform display value. + +=item B<--display-transform-dst> + +Regexp dst to transform display value. + +=item B<--show-cache> + +Display cache interface datas. + +=back + +=cut diff --git a/centreon-plugins/network/dlink/standard/snmp/mode/stack.pm b/centreon-plugins/network/dlink/standard/snmp/mode/stack.pm new file mode 100644 index 000000000..e79ba2905 --- /dev/null +++ b/centreon-plugins/network/dlink/standard/snmp/mode/stack.pm @@ -0,0 +1,282 @@ +# +# Copyright 2020 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::dlink::standard::snmp::mode::stack; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub custom_member_status_output { + my ($self, %options) = @_; + + return sprintf( + 'role: %s [status: %s]', + $self->{result_values}->{role}, + $self->{result_values}->{status} + ); +} + +sub custom_member_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{roleLast} = $options{old_datas}->{$self->{instance} . '_role'}; + $self->{result_values}->{role} = $options{new_datas}->{$self->{instance} . '_role'}; + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + if (!defined($options{old_datas}->{$self->{instance} . '_role'})) { + $self->{error_msg} = "buffer creation"; + return -2; + } + + return 0; +} + +sub custom_link_status_output { + my ($self, %options) = @_; + + return sprintf( + 'status: %s', + $self->{result_values}->{status} + ); +} + + +sub member_long_output { + my ($self, %options) = @_; + + return "checking stack member '" . $options{instance_value}->{display} . "'"; +} + +sub prefix_member_output { + my ($self, %options) = @_; + + return "Stack member '" . $options{instance_value}->{display} . "' "; +} + +sub prefix_link_output { + my ($self, %options) = @_; + + return "link '" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, skipped_code => { -10 => 1 } }, + { name => 'member', type => 3, cb_prefix_output => 'prefix_member_output', cb_long_output => 'member_long_output', indent_long_output => ' ', message_multiple => 'All stack members are ok', + group => [ + { name => 'member_global', type => 0, skipped_code => { -10 => 1 } }, + { name => 'link', display_long => 1, cb_prefix_output => 'prefix_link_output', message_multiple => 'All links are ok', type => 1, skipped_code => { -10 => 1 } }, + ] + } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'members-total', nlabel => 'stack.members.total.count', set => { + key_values => [ { name => 'members'} ], + output_template => 'Total members: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + } + ]; + + $self->{maps_counters}->{member_global} = [ + { + label => 'member-status', + type => 2, + unknown_default => '%{status} =~ /unknown/i', + warning_default => '%{status} =~ /codeUpdate/i', + critical_default => '%{role} ne %{roleLast} || %{status} =~ /unsupported|codeMismatch/i', + set => { + key_values => [ { name => 'role' }, { name => 'status' }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_member_status_calc'), + closure_custom_output => $self->can('custom_member_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; + + $self->{maps_counters}->{link} = [ + { label => 'link-status', type => 2, critical_default => '%{status} eq "down"', set => { + key_values => [ { name => 'status' }, { name => 'display' } ], + closure_custom_calc => \&catalog_status_calc, + closure_custom_output => $self->can('custom_link_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; +} + +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 => { + }); + + return $self; +} + +my $mapping_role = { + 1 => 'unknown', 3 => 'standAlone', + 4 => 'master', 5 => 'slave', 6 => 'backupmaster' +}; +my $mapping_unit_status = { + 0 => 'unknown', 1 => 'ok', 2 => 'unsupported', + 3 => 'codeMismatch', 4 => 'notPresent', 5 => 'codeUpdate' +}; +my $mapping_link_status = { + 1 => 'up', 2 => 'down' +}; + +my $mapping_industrial = { + role => { oid => '.1.3.6.1.4.1.171.14.9.1.1.8.1.3', map => $mapping_role }, # dStackInfoRole + macaddr => { oid => '.1.3.6.1.4.1.171.14.9.1.1.8.1.7' } # dStackInfoMacAddr +}; +my $mapping_common = { + role => { oid => '.1.3.6.1.4.1.171.17.9.1.1.13.1.3', map => $mapping_role }, # esStackInfoRole + macaddr => { oid => '.1.3.6.1.4.1.171.17.9.1.1.13.1.7' }, # esStackInfoMacAddr + status => { oid => '.1.3.6.1.4.1.171.17.9.1.1.13.1.14', map => $mapping_unit_status } # esStackInfoUnitStatus +}; +my $mapping_link = { + box_id => { oid => '.1.3.6.1.4.1.171.17.9.1.1.14.1.2' }, # esStackStackPortBoxId + tag => { oid => '.1.3.6.1.4.1.171.17.9.1.1.14.1.3' }, # esStackStackPortTag + status => { oid => '.1.3.6.1.4.1.171.17.9.1.1.14.1.4', map => $mapping_link_status } # esStackStackPortLinkStatus +}; +my $oid_dStackUnitInfoEntry = '.1.3.6.1.4.1.171.14.9.1.1.8.1'; +my $oid_esStackUnitInfoEntry = '.1.3.6.1.4.1.171.17.9.1.1.13.1'; +my $oid_esStackUnitStackPortEntry = '.1.3.6.1.4.1.171.17.9.1.1.14.1'; + +sub manage_stack { + my ($self, %options) = @_; + + foreach my $oid (keys %{$options{snmp_result}}) { + next if ($oid !~ /^$options{mapping}->{role}->{oid}\.(.*)$/); + my $box_id = $1; + my $result = $options{snmp}->map_instance(mapping => $options{mapping}, results => $options{snmp_result}, instance => $box_id); + + my $status = defined($result->{status}) ? $result->{status} : '-'; + $self->{global}->{members}++ if ($status ne 'notPresent'); + $self->{member}->{$box_id} = { + display => $box_id, + member_global => { + display => $box_id, + role => $result->{role}, + macaddr => $result->{macaddr}, + status => $status + }, + link => {} + }; + } +} + +sub manage_selection { + my ($self, %options) = @_; + + my $snmp_result = $options{snmp}->get_multiple_table( + oids => [ + { oid => $oid_dStackUnitInfoEntry, start => $mapping_industrial->{role}->{oid}, end => $mapping_industrial->{macaddr}->{oid} }, + { oid => $oid_esStackUnitInfoEntry, start => $mapping_common->{role}->{oid}, end => $mapping_common->{status}->{oid} }, + { oid => $oid_esStackUnitStackPortEntry, start => $mapping_link->{box_id}->{oid}, end => $mapping_link->{status}->{oid} } + ], + nothing_quit => 1 + ); + + $self->{global} = { members => 0 }; + $self->{members} = {}; + $self->manage_stack(snmp => $options{snmp}, snmp_result => $snmp_result->{$oid_dStackUnitInfoEntry}, mapping => $mapping_industrial); + $self->manage_stack(snmp => $options{snmp}, snmp_result => $snmp_result->{$oid_esStackUnitInfoEntry}, mapping => $mapping_common); + + foreach (keys %{$snmp_result->{$oid_esStackUnitStackPortEntry}}) { + next if (! /^$mapping_link->{box_id}->{oid}\.(.*)$/); + my $instance = $1; + my $result = $options{snmp}->map_instance(mapping => $mapping_link, results => $snmp_result->{$oid_esStackUnitStackPortEntry}, instance => $instance); + + next if (!defined($self->{member}->{ $result->{box_id} })); + + my $name = defined($result->{tag}) && $result->{tag} ne '' ? $result->{tag} : $instance; + $self->{member}->{ $result->{box_id} }->{link}->{$name} = { + display => $name, + status => $result->{status} + }; + } + + $self->{cache_name} = 'dlink_' . $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')); +} + +1; + +__END__ + +=head1 MODE + +Check stack. + +=over 8 + +=item B<--unknown-member-status> + +Set unknown threshold for status (Default: '%{status} =~ /unknown/i'). +Can used special variables like: %{role}, %{roleLast}, %{status}, %{display} + +=item B<--warning-member-status> + +Set warning threshold for status (Default: '%{status} =~ /codeUpdate/i'). +Can used special variables like: %{role}, %{roleLast}, %{status}, %{display} + +=item B<--critical-member-status> + +Set critical threshold for status (Default: '%{role} ne %{roleLast} || %{status} =~ /unsupported|codeMismatch/i'). +Can used special variables like: %{role}, %{roleLast}, %{status}, %{display} + +=item B<--unknown-link-status> + +Set unknown threshold for status. +Can used special variables like: %{status}, %{display} + +=item B<--warning-link-status> + +Set warning threshold for status. +Can used special variables like: %{status}, %{display} + +=item B<--critical-link-status> + +Set critical threshold for status (Default: '%{status} eq "down"'). +Can used special variables like: %{status}, %{display} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'members-total'. + +=back + +=cut diff --git a/centreon-plugins/network/dlink/standard/snmp/plugin.pm b/centreon-plugins/network/dlink/standard/snmp/plugin.pm index a5554e8f3..5e4d14e95 100644 --- a/centreon-plugins/network/dlink/standard/snmp/plugin.pm +++ b/centreon-plugins/network/dlink/standard/snmp/plugin.pm @@ -33,9 +33,10 @@ sub new { %{$self->{modes}} = ( 'cpu' => 'network::dlink::standard::snmp::mode::cpu', 'hardware' => 'network::dlink::standard::snmp::mode::hardware', - 'interfaces' => 'snmp_standard::mode::interfaces', + 'interfaces' => 'network::dlink::standard::snmp::mode::interfaces', 'list-interfaces' => 'snmp_standard::mode::listinterfaces', - 'memory' => 'network::dlink::standard::snmp::mode::memory' + 'memory' => 'network::dlink::standard::snmp::mode::memory', + 'stack' => 'network::dlink::standard::snmp::mode::stack' ); return $self;