From 7c4316088419f0a2a28faff56ba952a65c929847 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Thu, 23 Apr 2020 11:23:52 +0200 Subject: [PATCH] Fix --- snmp_standard/mode/listspanningtrees.pm | 97 ++++++++++++++----------- snmp_standard/mode/spanningtree.pm | 73 +++++++++++-------- 2 files changed, 95 insertions(+), 75 deletions(-) diff --git a/snmp_standard/mode/listspanningtrees.pm b/snmp_standard/mode/listspanningtrees.pm index e8d3da959..0f3deff3f 100644 --- a/snmp_standard/mode/listspanningtrees.pm +++ b/snmp_standard/mode/listspanningtrees.pm @@ -29,12 +29,10 @@ sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - - $options{options}->add_options(arguments => - { - "filter-port:s" => { name => 'filter_port' }, - }); - $self->{spanningtrees} = {}; + + $options{options}->add_options(arguments => { + 'filter-port:s' => { name => 'filter_port' }, + }); return $self; } @@ -44,32 +42,33 @@ sub check_options { $self->SUPER::init(%options); } -my %mapping_state = ( +my $mapping_state = { 1 => 'disabled', 2 => 'blocking', 3 => 'listening', 4 => 'learning', 5 => 'forwarding', 6 => 'broken', - 10 => 'not defined', -); -my %mapping_status = ( + 10 => 'not defined' +}; +my $mapping_status = { 1 => 'enabled', - 2 => 'disabled', -); + 2 => 'disabled' +}; my $mapping = { - dot1dStpPortState => { oid => '.1.3.6.1.2.1.17.2.15.1.3', map => \%mapping_state }, - dot1dStpPortEnable => { oid => '.1.3.6.1.2.1.17.2.15.1.4', map => \%mapping_status }, + dot1dStpPortState => { oid => '.1.3.6.1.2.1.17.2.15.1.3', map => $mapping_state }, + dot1dStpPortEnable => { oid => '.1.3.6.1.2.1.17.2.15.1.4', map => $mapping_status } }; my $oid_dot1dStpPortEntry = '.1.3.6.1.2.1.17.2.15.1'; my $oid_dot1dBasePortIfIndex = '.1.3.6.1.2.1.17.1.4.1.2'; -my %mapping_if_status = ( +my $mapping_if_status = { 1 => 'up', 2 => 'down', 3 => 'testing', 4 => 'unknown', 5 => 'dormant', 6 => 'notPresent', 7 => 'lowerLayerDown', -); + 100 => 'notfound' +}; my $oid_ifDesc = '.1.3.6.1.2.1.2.2.1.2'; my $oid_ifAdminStatus = '.1.3.6.1.2.1.2.2.1.7'; my $oid_ifOpStatus = '.1.3.6.1.2.1.2.2.1.8'; @@ -77,16 +76,17 @@ my $oid_ifOpStatus = '.1.3.6.1.2.1.2.2.1.8'; sub manage_selection { my ($self, %options) = @_; - my $results = $options{snmp}->get_table(oid => $oid_dot1dStpPortEntry, start => $mapping->{dot1dStpPortState}->{oid}, end => $mapping->{dot1dStpPortEnable}->{oid}, nothing_quit => 1); + my $results = {}; + my $snmp_results = $options{snmp}->get_table(oid => $oid_dot1dStpPortEntry, start => $mapping->{dot1dStpPortState}->{oid}, end => $mapping->{dot1dStpPortEnable}->{oid}, nothing_quit => 1); my @instances = (); - foreach my $oid (keys %{$results}) { + foreach my $oid (keys %$snmp_results) { next if ($oid !~ /^$mapping->{dot1dStpPortState}->{oid}\.(.*)/); my $instance = $1; - my $map_result = $options{snmp}->map_instance(mapping => $mapping, results => $results, instance => $instance); + my $map_result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_results, instance => $instance); if ($map_result->{dot1dStpPortEnable} =~ /disabled/) { - $self->{output}->output_add(long_msg => sprintf("Skipping interface '%d': Stp port disabled", $instance), debug => 1); + $self->{output}->output_add(long_msg => sprintf("skipping interface '%d': Stp port disabled", $instance), debug => 1); next; } push @instances, $instance; @@ -102,44 +102,55 @@ sub manage_selection { my $result_if = $options{snmp}->get_leef(); foreach my $instance (@instances) { - my $map_result = $options{snmp}->map_instance(mapping => $mapping, results => $results, instance => $instance); + my $map_result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_results, instance => $instance); my $state = (defined($map_result->{dot1dStpPortState})) ? $map_result->{dot1dStpPortState} : 'not defined'; my $description = (defined($result->{$oid_dot1dBasePortIfIndex . '.' . $instance}) && defined($result_if->{$oid_ifDesc . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}})) ? - $result_if->{$oid_ifDesc . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : 'unknown'; + $result_if->{$oid_ifDesc . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : $instance . '.unknown'; my $admin_status = (defined($result->{$oid_dot1dBasePortIfIndex . '.' . $instance}) && defined($result_if->{$oid_ifAdminStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}})) ? - $result_if->{$oid_ifAdminStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : 'unknown'; + $result_if->{$oid_ifAdminStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : 100; my $op_status = (defined($result->{$oid_dot1dBasePortIfIndex . '.' . $instance}) && defined($result_if->{$oid_ifOpStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}})) ? - $result_if->{$oid_ifOpStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : 'unknown'; + $result_if->{$oid_ifOpStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : 100; if (defined($self->{option_results}->{filter_port}) && $self->{option_results}->{filter_port} ne '' && $description !~ /$self->{option_results}->{filter_port}/) { - $self->{output}->output_add(long_msg => sprintf("Skipping interface '%s': filtered with options", $description), debug => 1); + $self->{output}->output_add(long_msg => sprintf("skipping interface '%s': filtered with options", $description), debug => 1); next; } - $self->{spanningtrees}->{$result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} = { + $results->{$instance} = { state => $state, - admin_status => $mapping_if_status{$admin_status}, - op_status => $mapping_if_status{$op_status}, - index => $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}, + admin_status => $mapping_if_status->{$admin_status}, + op_status => $mapping_if_status->{$op_status}, + index => defined($result->{$oid_dot1dBasePortIfIndex . '.' . $instance}) ? $result->{$oid_dot1dBasePortIfIndex . '.' . $instance} : 'notfound', description => $description }; } + + return $results; } sub run { my ($self, %options) = @_; - $self->manage_selection(%options); - foreach my $instance (sort keys %{$self->{spanningtrees}}) { - $self->{output}->output_add(long_msg => sprintf("[port = %s] [state = %s] [op_status = %s] [admin_status = %s] [index = %s]", - $self->{spanningtrees}->{$instance}->{description}, $self->{spanningtrees}->{$instance}->{state}, $self->{spanningtrees}->{$instance}->{op_status}, - $self->{spanningtrees}->{$instance}->{admin_status}, $self->{spanningtrees}->{$instance}->{index})); + my $results = $self->manage_selection(%options); + foreach my $instance (sort keys %$results) { + $self->{output}->output_add( + long_msg => sprintf( + "[port = %s] [state = %s] [op_status = %s] [admin_status = %s] [index = %s]", + $results->{$instance}->{description}, + $results->{$instance}->{state}, + $results->{$instance}->{op_status}, + $results->{$instance}->{admin_status}, + $results->{$instance}->{index} + ) + ); } - $self->{output}->output_add(severity => 'OK', - short_msg => 'List ports with Spanning Tree Protocol:'); + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List ports with spanning tree protocol:' + ); $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); $self->{output}->exit(); } @@ -153,14 +164,14 @@ sub disco_format { sub disco_show { my ($self, %options) = @_; - $self->manage_selection(%options); - foreach my $instance (sort keys %{$self->{spanningtrees}}) { + my $results = $self->manage_selection(%options); + foreach my $instance (sort keys %$results) { $self->{output}->add_disco_entry( - port => $self->{spanningtrees}->{$instance}->{description}, - state => $self->{spanningtrees}->{$instance}->{state}, - op_status => $self->{spanningtrees}->{$instance}->{op_status}, - admin_status => $self->{spanningtrees}->{$instance}->{admin_status}, - index => $self->{spanningtrees}->{$instance}->{index} + port => $results->{$instance}->{description}, + state => $results->{$instance}->{state}, + op_status => $results->{$instance}->{op_status}, + admin_status => $results->{$instance}->{admin_status}, + index => $results->{$instance}->{index} ); } } diff --git a/snmp_standard/mode/spanningtree.pm b/snmp_standard/mode/spanningtree.pm index ea9cc88b4..ad31e156a 100644 --- a/snmp_standard/mode/spanningtree.pm +++ b/snmp_standard/mode/spanningtree.pm @@ -29,10 +29,11 @@ use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold) sub custom_status_output { my ($self, %options) = @_; - my $msg = sprintf("spanning tree state is '%s' [op status: '%s'] [admin status: '%s'] [index: '%s']", + return sprintf( + "spanning tree state is '%s' [op status: '%s'] [admin status: '%s'] [index: '%s']", $self->{result_values}->{state}, $self->{result_values}->{op_status}, - $self->{result_values}->{admin_status}, $self->{result_values}->{index}); - return $msg; + $self->{result_values}->{admin_status}, $self->{result_values}->{index} + ) } sub custom_status_calc { @@ -52,10 +53,13 @@ sub set_counters { $self->{maps_counters_type} = [ { name => 'spanningtrees', type => 1, cb_prefix_output => 'prefix_peers_output', message_multiple => 'All spanning trees are ok' }, ]; + $self->{maps_counters}->{spanningtrees} = [ { label => 'status', threshold => 0, set => { - key_values => [ { name => 'state' }, { name => 'admin_status' }, { name => 'op_status' }, - { name => 'index' }, { name => 'description' } ], + key_values => [ + { name => 'state' }, { name => 'admin_status' }, { name => 'op_status' }, + { name => 'index' }, { name => 'description' } + ], closure_custom_calc => $self->can('custom_status_calc'), closure_custom_output => $self->can('custom_status_output'), closure_custom_perfdata => sub { return 0; }, @@ -76,12 +80,11 @@ sub new { my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - $options{options}->add_options(arguments => - { - "filter-port:s" => { name => 'filter_port' }, - "warning-status:s" => { name => 'warning_status', default => '' }, - "critical-status:s" => { name => 'critical_status', default => '%{op_status} =~ /up/ && %{state} =~ /blocking|broken/' }, - }); + $options{options}->add_options(arguments => { + 'filter-port:s' => { name => 'filter_port' }, + 'warning-status:s' => { name => 'warning_status', default => '' }, + 'critical-status:s' => { name => 'critical_status', default => '%{op_status} =~ /up/ && %{state} =~ /blocking|broken/' }, + }); return $self; } @@ -93,50 +96,51 @@ sub check_options { $self->change_macros(macros => ['warning_status', 'critical_status']); } -my %mapping_state = ( +my $mapping_state = { 1 => 'disabled', 2 => 'blocking', 3 => 'listening', 4 => 'learning', 5 => 'forwarding', 6 => 'broken', - 10 => 'not defined', -); -my %mapping_status = ( + 10 => 'not defined' +}; +my $mapping_status = { 1 => 'enabled', - 2 => 'disabled', -); + 2 => 'disabled' +}; my $mapping = { - dot1dStpPortState => { oid => '.1.3.6.1.2.1.17.2.15.1.3', map => \%mapping_state }, - dot1dStpPortEnable => { oid => '.1.3.6.1.2.1.17.2.15.1.4', map => \%mapping_status }, + dot1dStpPortState => { oid => '.1.3.6.1.2.1.17.2.15.1.3', map => $mapping_state }, + dot1dStpPortEnable => { oid => '.1.3.6.1.2.1.17.2.15.1.4', map => $mapping_status } }; my $oid_dot1dStpPortEntry = '.1.3.6.1.2.1.17.2.15.1'; my $oid_dot1dBasePortIfIndex = '.1.3.6.1.2.1.17.1.4.1.2'; -my %mapping_if_status = ( +my $mapping_if_status = { 1 => 'up', 2 => 'down', 3 => 'testing', 4 => 'unknown', 5 => 'dormant', 6 => 'notPresent', 7 => 'lowerLayerDown', -); + 100 => 'notfound' +}; my $oid_ifDesc = '.1.3.6.1.2.1.2.2.1.2'; my $oid_ifAdminStatus = '.1.3.6.1.2.1.2.2.1.7'; my $oid_ifOpStatus = '.1.3.6.1.2.1.2.2.1.8'; sub manage_selection { my ($self, %options) = @_; - + $self->{spanningtrees} = {}; my $results = $options{snmp}->get_table(oid => $oid_dot1dStpPortEntry, start => $mapping->{dot1dStpPortState}->{oid}, end => $mapping->{dot1dStpPortEnable}->{oid}, nothing_quit => 1); my @instances = (); - foreach my $oid (keys %{$results}) { + foreach my $oid (keys %$results) { next if ($oid !~ /^$mapping->{dot1dStpPortState}->{oid}\.(.*)/); my $instance = $1; my $map_result = $options{snmp}->map_instance(mapping => $mapping, results => $results, instance => $instance); if ($map_result->{dot1dStpPortEnable} =~ /disabled/) { - $self->{output}->output_add(long_msg => sprintf("Skipping interface '%d': Stp port disabled", $instance), debug => 1); + $self->{output}->output_add(long_msg => sprintf("skipping interface '%d': Stp port disabled", $instance), debug => 1); next; } push @instances, $instance; @@ -145,7 +149,7 @@ sub manage_selection { $options{snmp}->load(oids => [ $oid_dot1dBasePortIfIndex ], instances => [ @instances ]); my $result = $options{snmp}->get_leef(nothing_quit => 1); - foreach my $oid (keys %{$result}) { + foreach my $oid (keys %$result) { next if ($oid !~ /^$oid_dot1dBasePortIfIndex\./ || !defined($result->{$oid})); $options{snmp}->load(oids => [ $oid_ifDesc . "." . $result->{$oid}, $oid_ifAdminStatus . "." . $result->{$oid}, $oid_ifOpStatus . "." . $result->{$oid} ]); } @@ -156,22 +160,27 @@ sub manage_selection { my $state = (defined($map_result->{dot1dStpPortState})) ? $map_result->{dot1dStpPortState} : 'not defined'; my $description = (defined($result->{$oid_dot1dBasePortIfIndex . '.' . $instance}) && defined($result_if->{$oid_ifDesc . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}})) ? - $result_if->{$oid_ifDesc . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : 'unknown'; + $result_if->{$oid_ifDesc . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : $instance . '.unknown'; my $admin_status = (defined($result->{$oid_dot1dBasePortIfIndex . '.' . $instance}) && defined($result_if->{$oid_ifAdminStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}})) ? - $result_if->{$oid_ifAdminStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : 'unknown'; + $result_if->{$oid_ifAdminStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : 100; my $op_status = (defined($result->{$oid_dot1dBasePortIfIndex . '.' . $instance}) && defined($result_if->{$oid_ifOpStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}})) ? - $result_if->{$oid_ifOpStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : 'unknown'; + $result_if->{$oid_ifOpStatus . '.' . $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} : 100; if (defined($self->{option_results}->{filter_port}) && $self->{option_results}->{filter_port} ne '' && $description !~ /$self->{option_results}->{filter_port}/) { - $self->{output}->output_add(long_msg => sprintf("Skipping interface '%s': filtered with options", $description), debug => 1); + $self->{output}->output_add(long_msg => sprintf("skipping interface '%s': filtered with options", $description), debug => 1); next; } - $self->{spanningtrees}->{$result->{$oid_dot1dBasePortIfIndex . '.' . $instance}} = { + if (!defined($result->{$oid_dot1dBasePortIfIndex . '.' . $instance})) { + $self->{output}->output_add(long_msg => sprintf("skipping spanning '%s': filtered with options", $instance), debug => 1); + next; + } + + $self->{spanningtrees}->{$instance} = { state => $state, - admin_status => $mapping_if_status{$admin_status}, - op_status => $mapping_if_status{$op_status}, + admin_status => $mapping_if_status->{$admin_status}, + op_status => $mapping_if_status->{$op_status}, index => $result->{$oid_dot1dBasePortIfIndex . '.' . $instance}, description => $description };