From 4c14f9778050494524bf195da7d2c34c94f9e7d7 Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Wed, 21 May 2014 14:56:21 +0200 Subject: [PATCH] Refs #5517 + better management of errors for some modes --- centreon/plugins/perfdata.pm | 34 ++++++++++++++++++++++-------- os/linux/local/mode/loadaverage.pm | 6 ++++++ snmp_standard/mode/loadaverage.pm | 6 ++++++ snmp_standard/mode/packeterrors.pm | 21 +++++++++++------- snmp_standard/mode/traffic.pm | 31 +++++++++++++++++++++------ 5 files changed, 74 insertions(+), 24 deletions(-) diff --git a/centreon/plugins/perfdata.pm b/centreon/plugins/perfdata.pm index c689e6201..52eac55d0 100644 --- a/centreon/plugins/perfdata.pm +++ b/centreon/plugins/perfdata.pm @@ -55,18 +55,34 @@ sub get_perfdata_for_output { # $options{label} : threshold label # $options{total} : percent threshold to transform in global # $options{cast_int} : cast absolute to int + # $options{op} : operator to apply to start/end value (uses with 'value'}) + # $options{value} : value to apply with 'op' option - my $perf_output = $self->{threshold_label}->{$options{label}}->{value}; - if (defined($perf_output) && $perf_output ne '' && defined($options{total})) { - $perf_output = ($self->{threshold_label}->{$options{label}}->{arobase} == 1 ? "@" : "") . - (($self->{threshold_label}->{$options{label}}->{infinite_neg} == 0) ? (defined($options{cast_int}) ? sprintf("%d", ($self->{threshold_label}->{$options{label}}->{start} * $options{total} / 100)) : sprintf("%.2f", ($self->{threshold_label}->{$options{label}}->{start} * $options{total} / 100))) : "") . - ":" . - (($self->{threshold_label}->{$options{label}}->{infinite_pos} == 0) ? (defined($options{cast_int}) ? sprintf("%d", ($self->{threshold_label}->{$options{label}}->{end} * $options{total} / 100)) : sprintf("%.2f", ($self->{threshold_label}->{$options{label}}->{end} * $options{total} / 100))) : ""); + if (!defined($self->{threshold_label}->{$options{label}}->{value}) || $self->{threshold_label}->{$options{label}}->{value} eq '') { + return ''; } + + my %perf_value = %{$self->{threshold_label}->{$options{label}}}; + + if (defined($options{op}) && defined($options{value})) { + eval "\$perf_value{start} = \$perf_value{start} $options{op} \$options{value}" if ($perf_value{infinite_neg} == 0); + eval "\$perf_value{end} = \$perf_value{end} $options{op} \$options{value}" if ($perf_value{infinite_pos} == 0); + } + if (defined($options{total})) { + $perf_value{start} = $perf_value{start} * $options{total} / 100 if ($perf_value{infinite_neg} == 0); + $perf_value{end} = $perf_value{end} * $options{total} / 100 if ($perf_value{infinite_pos} == 0); + $perf_value{start} = sprintf("%.2f", $perf_value{start}) if ($perf_value{infinite_neg} == 0 && !defined($options{cast_int})); + $perf_value{end} = sprintf("%.2f", $perf_value{end}) if ($perf_value{infinite_pos} == 0 && !defined($options{cast_int})); + } + + $perf_value{start} = sprintf("%d", $perf_value{start}) if ($perf_value{infinite_neg} == 0 && defined($options{cast_int})); + $perf_value{end} = sprintf("%d", $perf_value{end}) if ($perf_value{infinite_pos} == 0 && defined($options{cast_int})); + + my $perf_output = ($perf_value{arobase} == 1 ? "@" : "") . + (($perf_value{infinite_neg} == 0) ? $perf_value{start} : "~") . + ":" . + (($perf_value{infinite_pos} == 0) ? $perf_value{end} : ""); - if (!defined($perf_output)) { - $perf_output = ''; - } return $perf_output; } diff --git a/os/linux/local/mode/loadaverage.pm b/os/linux/local/mode/loadaverage.pm index a8b644b8b..9297c090d 100644 --- a/os/linux/local/mode/loadaverage.pm +++ b/os/linux/local/mode/loadaverage.pm @@ -156,12 +156,18 @@ sub run { min => 0); $self->{output}->perfdata_add(label => 'load1', value => $load1m, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn1', op => '*', value => $countCpu), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit1', op => '*', value => $countCpu), min => 0); $self->{output}->perfdata_add(label => 'load5', value => $load5m, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn5', op => '*', value => $countCpu), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit5', op => '*', value => $countCpu), min => 0); $self->{output}->perfdata_add(label => 'load15', value => $load15m, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn15', op => '*', value => $countCpu), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit15', op => '*', value => $countCpu), min => 0); } else { $cpu_load1 = $load1m; diff --git a/snmp_standard/mode/loadaverage.pm b/snmp_standard/mode/loadaverage.pm index 69a632a12..aef74c005 100644 --- a/snmp_standard/mode/loadaverage.pm +++ b/snmp_standard/mode/loadaverage.pm @@ -137,12 +137,18 @@ sub run { min => 0); $self->{output}->perfdata_add(label => 'load1', value => $result->{$oid_CpuLoad1m}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn1', op => '*', value => $countCpu), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit1', op => '*', value => $countCpu), min => 0); $self->{output}->perfdata_add(label => 'load5', value => $result->{$oid_CpuLoad5m}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn5', op => '*', value => $countCpu), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit5', op => '*', value => $countCpu), min => 0); $self->{output}->perfdata_add(label => 'load15', value => $result->{$oid_CpuLoad15m}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn15', op => '*', value => $countCpu), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit15', op => '*', value => $countCpu), min => 0); } else { $cpu_load1 = $result->{$oid_CpuLoad1m}; diff --git a/snmp_standard/mode/packeterrors.pm b/snmp_standard/mode/packeterrors.pm index 69b6f54ea..dd4f77fda 100644 --- a/snmp_standard/mode/packeterrors.pm +++ b/snmp_standard/mode/packeterrors.pm @@ -191,7 +191,10 @@ sub run { my $result = $self->{snmp}->get_leef(); $new_datas->{last_timestamp} = time(); - my $old_timestamp; + my $buffer_creation = 0; + my $old_timestamp = $self->{statefile_value}->get(name => 'last_timestamp'); + + if (!defined($self->{option_results}->{interface}) || defined($self->{option_results}->{use_regexp})) { $self->{output}->output_add(severity => 'OK', short_msg => 'All interfaces are ok.'); @@ -205,6 +208,11 @@ sub run { $self->{output}->output_add(severity => 'CRITICAL', short_msg => "Interface '" . $display_value . "' is not ready: " . $operstatus[$result->{$oid_operstatus . "." . $_} - 1]); } else { + # Avoid empty message + if (defined($self->{option_results}->{interface}) && !defined($self->{option_results}->{use_regexp})) { + $self->{output}->output_add(severity => 'OK', + short_msg => "Interface '" . $display_value . "' is not up (normal state)"); + } $self->{output}->output_add(long_msg => "Skip interface '" . $display_value . "'."); } next; @@ -238,7 +246,8 @@ sub run { } # We change mode. need to recreate a buffer - if (!defined($old_mode) || $new_datas->{'mode_' . $_} ne $old_mode) { + if (!defined($old_timestamp) || !defined($old_mode) || $new_datas->{'mode_' . $_} ne $old_mode) { + $buffer_creation = 1; next; } @@ -248,7 +257,6 @@ sub run { my @getting = ('in_ucast', 'in_bcast', 'in_mcast', 'out_ucast', 'out_bcast', 'out_mcast', 'in_discard', 'in_error', 'out_discard', 'out_error'); my $old_datas = {}; - $old_timestamp = $self->{statefile_value}->get(name => 'last_timestamp'); foreach my $key (@getting) { $old_datas->{$key} = $self->{statefile_value}->get(name => $key . '_' . $_); if (!defined($old_datas->{$key}) || $new_datas->{$key . '_' . $_} < $old_datas->{$key}) { @@ -256,10 +264,7 @@ sub run { $old_datas->{$key} = 0; } } - - if (!defined($old_timestamp)) { - next; - } + my $time_delta = $new_datas->{last_timestamp} - $old_timestamp; if ($time_delta <= 0) { # At least one second. two fast calls ;) @@ -330,7 +335,7 @@ sub run { } $self->{statefile_value}->write(data => $new_datas); - if (!defined($old_timestamp)) { + if ($buffer_creation == 1) { $self->{output}->output_add(severity => 'OK', short_msg => "Buffer creation..."); } diff --git a/snmp_standard/mode/traffic.pm b/snmp_standard/mode/traffic.pm index ec4d8920c..139bc31f1 100644 --- a/snmp_standard/mode/traffic.pm +++ b/snmp_standard/mode/traffic.pm @@ -155,6 +155,7 @@ sub run { my $result = $self->{snmp}->get_leef(); $new_datas->{last_timestamp} = time(); my $old_timestamp = $self->{statefile_value}->get(name => 'last_timestamp'); + my $buffer_creation = 0; if (!defined($self->{option_results}->{interface}) || defined($self->{option_results}->{use_regexp})) { $self->{output}->output_add(severity => 'OK', short_msg => 'All traffic are ok'); @@ -190,7 +191,7 @@ sub run { next; } $interface_speed = (defined($result->{$oid_speed64 . "." . $_}) && $result->{$oid_speed64 . "." . $_} ne '' ? ($result->{$oid_speed64 . "." . $_} * 1000000) : ($result->{$oid_speed32 . "." . $_})); - if ($interface_speed == 0) { + if (!defined($interface_speed) || $interface_speed == 0) { if (!defined($self->{option_results}->{skip_speed0})) { $self->{output}->output_add(severity => 'UNKNOWN', short_msg => "Interface '" . $display_value . "' Speed is 0. You should force the value with --speed option"); @@ -204,27 +205,43 @@ sub run { my $old_mode = $self->{statefile_value}->get(name => 'mode_' . $_); $new_datas->{'mode_' . $_} = '32'; - $new_datas->{'in_' . $_} = $result->{$oid_in32 . "." . $_} * 8; + $new_datas->{'in_' . $_} = $result->{$oid_in32 . "." . $_}; if (defined($result->{$oid_in64 . "." . $_}) && $result->{$oid_in64 . "." . $_} ne '' && $result->{$oid_in64 . "." . $_} != 0) { - $new_datas->{'in_' . $_} = $result->{$oid_in64 . "." . $_} * 8; + $new_datas->{'in_' . $_} = $result->{$oid_in64 . "." . $_}; $new_datas->{'mode_' . $_} = '64'; } - $new_datas->{'out_' . $_} = $result->{$oid_out32 . "." . $_} * 8; + $new_datas->{'out_' . $_} = $result->{$oid_out32 . "." . $_}; if (defined($result->{$oid_out64 . "." . $_}) && $result->{$oid_out64 . "." . $_} ne '' && $result->{$oid_out64 . "." . $_} != 0) { - $new_datas->{'out_' . $_} = $result->{$oid_out64 . "." . $_} * 8; + $new_datas->{'out_' . $_} = $result->{$oid_out64 . "." . $_}; $new_datas->{'mode_' . $_} = '64'; } + # Check if there is no values. Can happen :) + if (!defined($new_datas->{'out_' . $_}) || !defined($new_datas->{'in_' . $_})) { + # Avoid empty message + if (defined($self->{option_results}->{interface}) && !defined($self->{option_results}->{use_regexp})) { + $self->{output}->output_add(severity => 'OK', + short_msg => "Interface '" . $display_value . "' is up"); + } + $self->{output}->output_add(long_msg => "Skip interface '" . $display_value . "': bytes values are missing."); + next; + } + $new_datas->{'out_' . $_} *= 8; + $new_datas->{'in_' . $_} *= 8; + # We change mode. need to recreate a buffer if (!defined($old_mode) || $new_datas->{'mode_' . $_} ne $old_mode) { + $buffer_creation = 1; next; } my $old_in = $self->{statefile_value}->get(name => 'in_' . $_); my $old_out = $self->{statefile_value}->get(name => 'out_' . $_); - if (!defined($old_timestamp) || !defined($old_in) || !defined($old_out)) { + if (!defined($old_in) || !defined($old_out)) { + $buffer_creation = 1; next; } + if ($new_datas->{'in_' . $_} < $old_in) { # We set 0. Has reboot. $old_in = 0; @@ -278,7 +295,7 @@ sub run { } $self->{statefile_value}->write(data => $new_datas); - if (!defined($old_timestamp)) { + if ($buffer_creation == 1) { $self->{output}->output_add(severity => 'OK', short_msg => "Buffer creation..."); }