From e0b5e147dbc562a3d50126e38dd0f52003f1a05a Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Tue, 4 Jun 2019 12:40:01 +0200 Subject: [PATCH] refactoring cpu cisco standard --- .../common/cisco/standard/snmp/mode/cpu.pm | 255 ++++++++++-------- 1 file changed, 140 insertions(+), 115 deletions(-) diff --git a/centreon-plugins/centreon/common/cisco/standard/snmp/mode/cpu.pm b/centreon-plugins/centreon/common/cisco/standard/snmp/mode/cpu.pm index 676481cae..13fff40ba 100644 --- a/centreon-plugins/centreon/common/cisco/standard/snmp/mode/cpu.pm +++ b/centreon-plugins/centreon/common/cisco/standard/snmp/mode/cpu.pm @@ -20,76 +20,117 @@ package centreon::common::cisco::standard::snmp::mode::cpu; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'cpu_avg', type => 0, cb_prefix_output => 'prefix_cpu_avg_output', message_separator => ' ', skipped_code => { -10 => 1 } }, + { name => 'cpu_core', type => 1, cb_prefix_output => 'prefix_cpu_core_output', message_separator => ' ', message_multiple => 'All core cpu are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{cpu_avg} = [ + { label => 'average-5s', nlabel => 'cpu.utilization.5s.percentage', set => { + key_values => [ { name => 'average_5s' } ], + output_template => '%.2f %% (5s)', + perfdatas => [ + { label => 'total_cpu_5s_avg', value => 'average_5s_absolute', template => '%.2f', + min => 0, max => 100, unit => '%' }, + ], + } + }, + { label => 'average-1m', nlabel => 'cpu.utilization.1m.percentage', set => { + key_values => [ { name => 'average_1m' } ], + output_template => '%.2f %% (1m)', + perfdatas => [ + { label => 'total_cpu_1m_avg', value => 'average_1m_absolute', template => '%.2f', + min => 0, max => 100, unit => '%' }, + ], + } + }, + { label => 'average-5m', nlabel => 'cpu.utilization.5m.percentage', set => { + key_values => [ { name => 'average_5m' } ], + output_template => '%.2f %% (5m)', + perfdatas => [ + { label => 'total_cpu_5m_avg', value => 'average_5m_absolute', template => '%.2f', + min => 0, max => 100, unit => '%' }, + ], + } + }, + ]; + + $self->{maps_counters}->{cpu_core} = [ + { label => 'core-5s', nlabel => 'core.cpu.utilization.5s.percentage', set => { + key_values => [ { name => 'cpu_5s' }, { name => 'display' } ], + output_template => '%.2f %% (5s)', + perfdatas => [ + { label => 'cpu_5s', value => 'cpu_5s_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'core-1m', nlabel => 'core.cpu.utilization.1m.percentage', set => { + key_values => [ { name => 'cpu_1m' }, { name => 'display' } ], + output_template => '%.2f %% (1m)', + perfdatas => [ + { label => 'cpu_1m', value => 'cpu_1m_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'core-5m', nlabel => 'core.cpu.utilization.5m.percentage', set => { + key_values => [ { name => 'cpu_5m' }, { name => 'display' } ], + output_template => '%.2f %% (5m)', + perfdatas => [ + { label => 'cpu_5m', value => 'cpu_5m_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + ]; +} + +sub prefix_cpu_avg_output { + my ($self, %options) = @_; + + return $self->{cpu_avg}->{count} . " CPU(s) average usage is "; +} + +sub prefix_cpu_core_output { + my ($self, %options) = @_; + + return "CPU '" . $options{instance_value}->{display} . "' usage "; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', default => '' }, - "critical:s" => { name => 'critical', default => '' }, - }); + $options{options}->add_options(arguments => { + }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - ($self->{warn5s}, $self->{warn1m}, $self->{warn5m}) = split /,/, $self->{option_results}->{warning}; - ($self->{crit5s}, $self->{crit1m}, $self->{crit5m}) = split /,/, $self->{option_results}->{critical}; - - if (($self->{perfdata}->threshold_validate(label => 'warn5s', value => $self->{warn5s})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning (5sec) threshold '" . $self->{warn5s} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'warn1m', value => $self->{warn1m})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning (1min) threshold '" . $self->{warn1m} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'warn5m', value => $self->{warn5m})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning (5min) threshold '" . $self->{warn5m} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'crit5s', value => $self->{crit5s})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical (5sec) threshold '" . $self->{crit5s} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'crit1m', value => $self->{crit1m})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical (1min) threshold '" . $self->{crit1m} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'crit5m', value => $self->{crit5m})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical (5min) threshold '" . $self->{crit5m} . "'."); - $self->{output}->option_exit(); - } -} - sub check_nexus_cpu { my ($self, %options) = @_; if (!defined($self->{results}->{$options{oid}}->{$options{oid} . '.0'})) { return 0; } - - my $cpu = $self->{results}->{$options{oid}}->{$options{oid} . '.0'}; - my $exit = $self->{perfdata}->threshold_check(value => $cpu, - threshold => [ { label => 'crit5m', exit_litteral => 'critical' }, { label => 'warn5m', exit_litteral => 'warning' } ]); - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("CPU Usage : %s %%", $cpu)); - $self->{output}->perfdata_add(label => "cpu", unit => '%', - value => $cpu, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn5m'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit5m'), - min => 0, max => 100); + + my $instance = 0; + $self->{cpu_core}->{$instance} = { + display => $instance, + cpu_5m => $self->{results}->{$options{oid}}->{$options{oid} . '.0'}, + }; + return 1; } @@ -103,64 +144,53 @@ sub check_table_cpu { next if (defined($instances->{$1})); $instances->{$1} = 1; my $instance = $1; - my $cpu5sec = defined($self->{results}->{$options{entry}}->{$options{sec5} . '.' . $instance}) ? sprintf("%.2f", $self->{results}->{$options{entry}}->{$options{sec5} . '.' . $instance}) : undef; - my $cpu1min = defined($self->{results}->{$options{entry}}->{$options{min1} . '.' . $instance}) ? sprintf("%.2f", $self->{results}->{$options{entry}}->{$options{min1} . '.' . $instance}) : undef; - my $cpu5min = defined($self->{results}->{$options{entry}}->{$options{min5} . '.' . $instance}) ? sprintf("%.2f", $self->{results}->{$options{entry}}->{$options{min5} . '.' . $instance}) : undef; + + my $cpu5sec = defined($self->{results}->{$options{entry}}->{$options{sec5} . '.' . $instance}) ? $self->{results}->{$options{entry}}->{$options{sec5} . '.' . $instance} : undef; + my $cpu1min = defined($self->{results}->{$options{entry}}->{$options{min1} . '.' . $instance}) ? $self->{results}->{$options{entry}}->{$options{min1} . '.' . $instance} : undef; + my $cpu5min = defined($self->{results}->{$options{entry}}->{$options{min5} . '.' . $instance}) ? $self->{results}->{$options{entry}}->{$options{min5} . '.' . $instance} : undef; # Case that it's maybe other CPU oid in table for datas. next if (!defined($cpu5sec) && !defined($cpu1min) && !defined($cpu5min)); - + $checked = 1; - my @exits; - push @exits, $self->{perfdata}->threshold_check(value => $cpu5sec, - threshold => [ { label => 'crit5s', exit_litteral => 'critical' }, { label => 'warn5s', exit_litteral => 'warning' } ]) if (defined($cpu5sec)); - push @exits, $self->{perfdata}->threshold_check(value => $cpu1min, - threshold => [ { label => 'crit1m', exit_litteral => 'critical' }, { label => 'warn1m', exit_litteral => 'warning' } ]) if (defined($cpu1min)); - push @exits, $self->{perfdata}->threshold_check(value => $cpu5min, - threshold => [ { label => 'crit5m', exit_litteral => 'critical' }, { label => 'warn5m', exit_litteral => 'warning' } ]) if (defined($cpu5min)); - my $exit = $self->{output}->get_most_critical(status => \@exits); - - $self->{output}->output_add(long_msg => sprintf("CPU '%s': %s (5sec), %s (1min), %s (5min)", $instance, - defined($cpu5sec) ? $cpu5sec . '%' : 'not defined', - defined($cpu1min) ? $cpu1min . '%' : 'not defined', - defined($cpu5min) ? $cpu5min . '%' : 'not defined')); - if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("CPU '%s': %s (5sec), %s (1min), %s (5min)", $instance, - defined($cpu5sec) ? $cpu5sec . '%' : 'not defined', - defined($cpu1min) ? $cpu1min . '%' : 'not defined', - defined($cpu5min) ? $cpu5min . '%' : 'not defined')); - } - - if (defined($cpu5sec)) { - $self->{output}->perfdata_add(label => "cpu_" . $instance . "_5s", unit => '%', - value => $cpu5sec, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn5s'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit5s'), - min => 0, max => 100); - } - if (defined($cpu1min)) { - $self->{output}->perfdata_add(label => "cpu_" . $instance . "_1m", unit => '%', - value => $cpu1min, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn1m'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit1m'), - min => 0, max => 100); - } - if (defined($cpu5min)) { - $self->{output}->perfdata_add(label => "cpu_" . $instance . "_5m", unit => '%', - value => $cpu5min, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn5m'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit5m'), - min => 0, max => 100); - } + $self->{cpu_core}->{$instance} = { + display => $instance, + cpu_5s => $cpu5sec, + cpu_1m => $cpu1min, + cpu_5m => $cpu5min, + }; } return $checked; } -sub run { +sub check_cpu_average { my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; + + my $count = scalar(keys %{$self->{cpu_core}}); + my ($avg_5s, $avg_1m, $avg_5m); + foreach (values %{$self->{cpu_core}}) { + $avg_5s = defined($avg_5s) ? $avg_5s + $_->{cpu_5s} : $_->{cpu_5s} + if (defined($_->{cpu_5s})); + $avg_1m = defined($avg_1m) ? $avg_1m + $_->{cpu_1m} : $_->{cpu_1m} + if (defined($_->{cpu_1m})); + $avg_5m = defined($avg_5m) ? $avg_5m + $_->{cpu_5m} : $_->{cpu_5m} + if (defined($_->{cpu_5m})); + } + + $self->{cpu_avg} = { + average_5s => defined($avg_5s) ? $avg_5s / $count : undef, + average_1m => defined($avg_1m) ? $avg_1m / $count : undef, + average_5m => defined($avg_5m) ? $avg_5m / $count : undef, + count => $count, + }; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{cpu_avg} = {}; + $self->{cpu_core} = {}; # Cisco IOS Software releases later to 12.0(3)T and prior to 12.2(3.5) my $oid_cpmCPUTotalEntry = '.1.3.6.1.4.1.9.9.109.1.1.1.1'; @@ -179,31 +209,26 @@ sub run { # Cisco Nexus my $oid_cseSysCPUUtilization = '.1.3.6.1.4.1.9.9.305.1.1.1'; # .0 in reality - $self->{results} = $self->{snmp}->get_multiple_table(oids => [ - { oid => $oid_cpmCPUTotalEntry, - start => $oid_cpmCPUTotal5sec, end => $oid_cpmCPUTotal5minRev - }, - { oid => $oid_lcpu, - start => $oid_busyPer, end => $oid_avgBusy5 }, - { oid => $oid_cseSysCPUUtilization }, - ], - nothing_quit => 1); - - $self->{output}->output_add(severity => 'OK', - short_msg => 'All CPUs are ok.'); + $self->{results} = $options{snmp}->get_multiple_table( + oids => [ + { oid => $oid_cpmCPUTotalEntry, start => $oid_cpmCPUTotal5sec, end => $oid_cpmCPUTotal5minRev }, + { oid => $oid_lcpu, start => $oid_busyPer, end => $oid_avgBusy5 }, + { oid => $oid_cseSysCPUUtilization }, + ], + nothing_quit => 1 + ); if (!$self->check_table_cpu(entry => $oid_cpmCPUTotalEntry, sec5 => $oid_cpmCPUTotal5secRev, min1 => $oid_cpmCPUTotal1minRev, min5 => $oid_cpmCPUTotal5minRev) && !$self->check_table_cpu(entry => $oid_cpmCPUTotalEntry, sec5 => $oid_cpmCPUTotal5sec, min1 => $oid_cpmCPUTotal1min, min5 => $oid_cpmCPUTotal5min) && !$self->check_table_cpu(entry => $oid_lcpu, sec5 => $oid_busyPer, min1 => $oid_avgBusy1, min5 => $oid_avgBusy5) ) { if (!$self->check_nexus_cpu(oid => $oid_cseSysCPUUtilization)) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("Cannot find CPU informations.")); + $self->{output}->add_option_msg(short_msg => "Cannot find CPU informations"); + $self->{output}->option_exit(); } } - - $self->{output}->display(); - $self->{output}->exit(); + + $self->check_cpu_average(); } 1;