From c400894c98738ed1c5000d1f88400bda9ff00ab9 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Thu, 10 Oct 2019 15:50:06 +0200 Subject: [PATCH] enhance stack network --- network/extreme/snmp/mode/cpu.pm | 28 +- network/extreme/snmp/mode/hardware.pm | 11 +- network/extreme/snmp/mode/memory.pm | 26 +- network/extreme/snmp/mode/stack.pm | 311 +++++++++++++-------- network/juniper/common/junos/mode/stack.pm | 2 +- 5 files changed, 237 insertions(+), 141 deletions(-) diff --git a/network/extreme/snmp/mode/cpu.pm b/network/extreme/snmp/mode/cpu.pm index f2cba487f..9a8eea922 100644 --- a/network/extreme/snmp/mode/cpu.pm +++ b/network/extreme/snmp/mode/cpu.pm @@ -108,11 +108,10 @@ sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - - $options{options}->add_options(arguments => - { - }); - + + $options{options}->add_options(arguments => { + }); + return $self; } @@ -129,16 +128,19 @@ sub manage_selection { my $oid_extremeCpuMonitorSystemEntry = '.1.3.6.1.4.1.1916.1.32.1.4.1'; my $oid_extremeCpuMonitorTotalUtilization = '.1.3.6.1.4.1.1916.1.32.1.2'; # without .0 - $self->{results} = $options{snmp}->get_multiple_table(oids => [ - { oid => $oid_extremeCpuMonitorTotalUtilization }, - { oid => $oid_extremeCpuMonitorSystemEntry }, - ], nothing_quit => 1); - + my $snmp_result = $options{snmp}->get_multiple_table( + oids => [ + { oid => $oid_extremeCpuMonitorTotalUtilization }, + { oid => $oid_extremeCpuMonitorSystemEntry, start => $mapping->{extremeCpuMonitorSystemUtilization5secs}->{oid}, end => $mapping->{extremeCpuMonitorSystemUtilization5mins}->{oid} }, + ], + nothing_quit => 1 + ); + $self->{cpu} = {}; - foreach my $oid (keys %{$self->{results}->{$oid_extremeCpuMonitorSystemEntry}}) { + foreach my $oid (keys %{$snmp_result->{$oid_extremeCpuMonitorSystemEntry}}) { next if ($oid !~ /^$mapping->{extremeCpuMonitorSystemUtilization1min}->{oid}\.(.*)$/); my $instance = $1; - my $result = $options{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{$oid_extremeCpuMonitorSystemEntry}, instance => $instance); + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result->{$oid_extremeCpuMonitorSystemEntry}, instance => $instance); foreach (keys %{$mapping}) { $result->{$_} = undef if (defined($result->{$_}) && $result->{$_} =~ /n\/a/i); @@ -147,7 +149,7 @@ sub manage_selection { $self->{cpu}->{$instance} = { num => $instance, %$result }; } - $self->{global} = { total => $self->{results}->{$oid_extremeCpuMonitorTotalUtilization}->{$oid_extremeCpuMonitorTotalUtilization . '.0'} }; + $self->{global} = { total => $snmp_result->{$oid_extremeCpuMonitorTotalUtilization}->{$oid_extremeCpuMonitorTotalUtilization . '.0'} }; } 1; diff --git a/network/extreme/snmp/mode/hardware.pm b/network/extreme/snmp/mode/hardware.pm index a9d471fdb..681fcfdc5 100644 --- a/network/extreme/snmp/mode/hardware.pm +++ b/network/extreme/snmp/mode/hardware.pm @@ -90,11 +90,10 @@ sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - - $options{options}->add_options(arguments => - { - }); - + + $options{options}->add_options(arguments => { + }); + return $self; } @@ -146,4 +145,4 @@ Example: --critical='psu,.*,200' =back -=cut \ No newline at end of file +=cut diff --git a/network/extreme/snmp/mode/memory.pm b/network/extreme/snmp/mode/memory.pm index e8b195f93..9dba0458a 100644 --- a/network/extreme/snmp/mode/memory.pm +++ b/network/extreme/snmp/mode/memory.pm @@ -100,11 +100,10 @@ sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - - $options{options}->add_options(arguments => - { - }); - + + $options{options}->add_options(arguments => { + }); + return $self; } @@ -118,15 +117,20 @@ sub manage_selection { $self->{memory} = {}; my $oid_extremeMemoryMonitorSystemEntry = '.1.3.6.1.4.1.1916.1.32.2.2.1'; - $self->{results} = $options{snmp}->get_table(oid => $oid_extremeMemoryMonitorSystemEntry, - nothing_quit => 1); - foreach my $oid (keys %{$self->{results}}) { + my $snmp_result = $options{snmp}->get_table( + oid => $oid_extremeMemoryMonitorSystemEntry, + nothing_quit => 1 + ); + foreach my $oid (keys %$snmp_result) { next if ($oid !~ /^$mapping->{extremeMemoryMonitorSystemFree}->{oid}\.(\d+)/); my $instance = $1; - my $result = $options{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance); - $self->{memory}->{$instance} = { display => $instance, - free => $result->{extremeMemoryMonitorSystemFree} * 1024, total => $result->{extremeMemoryMonitorSystemTotal} * 1024}; + $self->{memory}->{$instance} = { + display => $instance, + free => $result->{extremeMemoryMonitorSystemFree} * 1024, + total => $result->{extremeMemoryMonitorSystemTotal} * 1024 + }; } } diff --git a/network/extreme/snmp/mode/stack.pm b/network/extreme/snmp/mode/stack.pm index c7832fc24..452cfc469 100644 --- a/network/extreme/snmp/mode/stack.pm +++ b/network/extreme/snmp/mode/stack.pm @@ -20,150 +20,217 @@ package network::extreme::snmp::mode::stack; -use base qw(centreon::plugins::mode); +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 catalog_status_calc); -# Extreme put 0 when it's disabled. -my %mapping_truth = ( - 0 => 'disabled', - 1 => 'enable', - 2 => 'disable', -); -my %mapping_stack_status = ( - 1 => 'up', - 2 => 'down', - 3 => 'mismatch', -); -my %mapping_stack_role = ( - 1 => 'master', - 2 => 'slave', - 3 => 'backup', -); -my $thresholds = { - stack => [ - ['up', 'OK'], - ['down', 'CRITICAL'], - ['mismatch', 'WARNING'], - ], -}; +sub custom_member_status_output { + my ($self, %options) = @_; + + my $msg = sprintf( + 'status: %s [role: %s]', + $self->{result_values}->{status}, + $self->{result_values}->{role} + ); + return $msg; +} + +sub custom_member_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; + $self->{result_values}->{roleLast} = $options{old_datas}->{$self->{instance} . '_role'}; + $self->{result_values}->{role} = $options{new_datas}->{$self->{instance} . '_role'}; + if (!defined($options{old_datas}->{$self->{instance} . '_role'})) { + $self->{error_msg} = "buffer creation"; + return -2; + } + + return 0; +} + +sub custom_port_status_output { + my ($self, %options) = @_; + + my $msg = sprintf( + 'operational status: %s', + $self->{result_values}->{link_status} + ); + return $msg; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { 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 => 'global', type => 0, skipped_code => { -10 => 1 } }, + { name => 'port', display_long => 1, cb_prefix_output => 'prefix_port_output', message_multiple => 'All ports are ok', type => 1, skipped_code => { -10 => 1 } }, + ] + } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'member-status', threshold => 0, 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, + } + }, + ]; + + $self->{maps_counters}->{port} = [ + { label => 'port-status', threshold => 0, set => { + key_values => [ { name => 'link_status' }, { name => 'display' } ], + closure_custom_calc => \&catalog_status_calc, + closure_custom_output => $self->can('custom_port_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold, + } + }, + ]; +} + +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_port_output { + my ($self, %options) = @_; + + return "port '" . $options{instance_value}->{display} . "' "; +} sub new { my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1); bless $self, $class; - - $options{options}->add_options(arguments => - { - "threshold-overload:s@" => { name => 'threshold_overload' }, - }); + + $options{options}->add_options(arguments => { + 'unknown-member-status:s' => { name => 'unknown_member_status', default => '' }, + 'warning-member-status:s' => { name => 'warning_member_status', default => '%{status} eq "mismatch"' }, + 'critical-member-status:s' => { name => 'critical_member_status', default => '%{role} ne %{roleLast} || %{status} eq "down"' }, + 'unknown-port-status:s' => { name => 'unknown_port_status', default => '' }, + 'warning-port-status:s' => { name => 'warning_port_status', default => '' }, + 'critical-port-status:s' => { name => 'critical_port_status', default => '%{link_status} ne "up"' }, + }); return $self; } sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); - - $self->{overload_th} = {}; - foreach my $val (@{$self->{option_results}->{threshold_overload}}) { - if ($val !~ /^(.*?),(.*?),(.*)$/) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); - $self->{output}->option_exit(); - } - my ($section, $status, $filter) = ($1, $2, $3); - if ($self->{output}->is_litteral_status(status => $status) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); - $self->{output}->option_exit(); - } - $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); - push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; - } + $self->SUPER::check_options(%options); + + $self->change_macros( + macros => [ + 'unknown_member_status', 'warning_member_status', 'critical_member_status', + 'unknown_port_status', 'warning_port_status', 'critical_port_status' + ] + ); } +my $mapping_truth = { + 0 => 'disabled', 1 => 'enable', 2 => 'disable', +}; +my $mapping_stack_status = { + 1 => 'up', 2 => 'down', 3 => 'mismatch', +}; +my $mapping_stack_role = { + 1 => 'master', 2 => 'slave', 3 => 'backup', +}; +my $mapping_link_status = { + 1 => 'up', 2 => 'down', +}; + my $mapping = { - extremeStackDetection => { oid => '.1.3.6.1.4.1.1916.1.33.1', map => \%mapping_truth }, + extremeStackDetection => { oid => '.1.3.6.1.4.1.1916.1.33.1', map => $mapping_truth }, }; my $mapping2 = { - extremeStackMemberOperStatus => { oid => '.1.3.6.1.4.1.1916.1.33.2.1.3', map => \%mapping_stack_status }, + extremeStackMemberOperStatus => { oid => '.1.3.6.1.4.1.1916.1.33.2.1.3', map => $mapping_stack_status }, + extremeStackMemberRole => { oid => '.1.3.6.1.4.1.1916.1.33.2.1.4', map => $mapping_stack_role }, + extremeStackMemberMACAddress => { oid => '.1.3.6.1.4.1.1916.1.33.2.1.6' }, }; my $mapping3 = { - extremeStackMemberRole => { oid => '.1.3.6.1.4.1.1916.1.33.2.1.4', map => \%mapping_stack_role }, -}; -my $mapping4 = { - extremeStackMemberMACAddress => { oid => '.1.3.6.1.4.1.1916.1.33.2.1.6' }, + extremeStackingPortRemoteMac => { oid => '.1.3.6.1.4.1.1916.1.33.3.1.2' }, + extremeStackingPortLinkStatus => { oid => '.1.3.6.1.4.1.1916.1.33.3.1.4', map => $mapping_link_status }, }; +my $oid_extremeStackMemberEntry = '.1.3.6.1.4.1.1916.1.33.2.1'; +my $oid_extremeStackingPortEntry = '.1.3.6.1.4.1.1916.1.33.3.1'; -sub run { +sub manage_selection { my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; - my $results = $self->{snmp}->get_multiple_table(oids => [ - { oid => $mapping->{extremeStackDetection}->{oid} }, - { oid => $mapping2->{extremeStackMemberOperStatus}->{oid} }, - { oid => $mapping3->{extremeStackMemberRole}->{oid} }, - { oid => $mapping4->{extremeStackMemberMACAddress}->{oid} }, - ], nothing_quit => 1); + my $snmp_result = $options{snmp}->get_multiple_table( + oids => [ + { oid => $mapping->{extremeStackDetection}->{oid} }, + { oid => $oid_extremeStackMemberEntry, start => $mapping2->{extremeStackMemberOperStatus}->{oid}, end => $mapping2->{extremeStackMemberMACAddress}->{oid} }, + { oid => $oid_extremeStackingPortEntry, start => $mapping3->{extremeStackingPortRemoteMac}->{oid} }, + ], + nothing_quit => 1 + ); - my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $results->{$mapping->{extremeStackDetection}->{oid}}, instance => '0'); + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result->{$mapping->{extremeStackDetection}->{oid}}, instance => '0'); # disable is voluntary if ($result->{extremeStackDetection} eq 'disable') { - $self->{output}->output_add(severity => 'OK', - short_msg => 'Stacking is disable'); + $self->{output}->output_add( + severity => 'OK', + short_msg => 'Stacking is disable' + ); $self->{output}->display(); $self->{output}->exit(); } - - $self->{output}->output_add(severity => 'OK', - short_msg => 'Stack ring is redundant'); - foreach my $oid (keys %{$results->{$mapping2->{extremeStackMemberOperStatus}->{oid}}}) { - $oid =~ /\.([0-9]+)$/; + + foreach my $oid (keys %{$snmp_result->{$oid_extremeStackMemberEntry}}) { + next if ($oid !~ /^$mapping2->{extremeStackMemberOperStatus}->{oid}\.(.*)$/); my $instance = $1; + my $result = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result->{$oid_extremeStackMemberEntry}, instance => $instance); - my $result2 = $self->{snmp}->map_instance(mapping => $mapping2, results => $results->{$mapping2->{extremeStackMemberOperStatus}->{oid}}, instance => $instance); - my $result3 = $self->{snmp}->map_instance(mapping => $mapping3, results => $results->{$mapping3->{extremeStackMemberRole}->{oid}}, instance => $instance); - my $result4 = $self->{snmp}->map_instance(mapping => $mapping4, results => $results->{$mapping4->{extremeStackMemberMACAddress}->{oid}}, instance => $instance); - - $self->{output}->output_add(long_msg => sprintf("Member '%s' state is %s [Role is '%s'] [Mac: %s]", - $instance, $result2->{extremeStackMemberOperStatus}, - $result3->{extremeStackMemberRole}, - defined($result4->{extremeStackMemberMACAddress}) ? unpack('H*', $result4->{extremeStackMemberMACAddress}) : '-')); - my $exit = $self->get_severity(section => 'stack', value => $result2->{extremeStackMemberOperStatus}); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Member '%s' state is %s", - $instance, $result2->{extremeStackMemberOperStatus})); + my $member_name = defined($result->{extremeStackMemberMACAddress}) && $result->{extremeStackMemberMACAddress} ne '' ? + join(":", unpack("(H2)*", $result->{extremeStackMemberMACAddress})) : + $instance; + $self->{member}->{$member_name} = { + display => $member_name, + global => { + display => $member_name, + role => $result->{extremeStackMemberRole}, + status => $result->{extremeStackMemberOperStatus}, + }, + port => {}, + }; + + foreach (keys %{$snmp_result->{$oid_extremeStackingPortEntry}}) { + next if (!/^$mapping3->{extremeStackingPortRemoteMac}->{oid}\.(.*)$/); + $instance = $1; + $result = $options{snmp}->map_instance(mapping => $mapping3, results => $snmp_result->{$oid_extremeStackingPortEntry}, instance => $instance); + my $member_mac = join(":", unpack("(H2)*", $result->{extremeStackingPortRemoteMac})); + next if (!defined($self->{member}->{$member_mac})); + + $self->{member}->{$member_mac}->{port}->{$instance} = { + display => $instance, + link_status => $result->{extremeStackingPortLinkStatus}, + }; } } - $self->{output}->display(); - $self->{output}->exit(); + $self->{cache_name} = 'extreme_' . $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')); } -sub get_severity { - my ($self, %options) = @_; - my $status = 'UNKNOWN'; # default - - if (defined($self->{overload_th}->{$options{section}})) { - foreach (@{$self->{overload_th}->{$options{section}}}) { - if ($options{value} =~ /$_->{filter}/i) { - $status = $_->{status}; - return $status; - } - } - } - foreach (@{$thresholds->{$options{section}}}) { - if ($options{value} =~ /$$_[0]/i) { - $status = $$_[1]; - return $status; - } - } - - return $status; -} - - 1; __END__ @@ -174,11 +241,35 @@ Check stack status. =over 8 -=item B<--threshold-overload> +=item B<--unknown-member-status> -Set to overload default threshold values (syntax: section,status,regexp) -It used before default thresholds (order stays). -Example: --threshold-overload='stack,WARNING,mismatch' +Set warning threshold for status (Default: ''). +Can used special variables like: %{role}, %{roleLast} + +=item B<--warning-member-status> + +Set warning threshold for status (Default: '%{status} eq "mismatch"'). +Can used special variables like: %{role}, %{roleLast} + +=item B<--critical-member-status> + +Set critical threshold for status (Default: '%{role} ne %{roleLast} || %{status} eq "down"'). +Can used special variables like: %{role}, %{roleLast} + +=item B<--unknown-port-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{link_status}, %{display} + +=item B<--warning-port-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{link_status}, %{display} + +=item B<--critical-port-status> + +Set critical threshold for status (Default: '%{link_status} ne "up"'). +Can used special variables like: %{link_status}, %{display} =back diff --git a/network/juniper/common/junos/mode/stack.pm b/network/juniper/common/junos/mode/stack.pm index db2e4d3c1..b82224cac 100644 --- a/network/juniper/common/junos/mode/stack.pm +++ b/network/juniper/common/junos/mode/stack.pm @@ -73,7 +73,7 @@ sub set_counters { } ]; - $self->{maps_counters}->{member} = [ + $self->{maps_counters}->{global} = [ { label => 'member-status', threshold => 0, set => { key_values => [ { name => 'role' }, { name => 'display' } ], closure_custom_calc => $self->can('custom_member_status_calc'),