From 65f2a356f8c9741803b823f5825fb29f5c9fa8a0 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 18 Mar 2019 16:26:11 +0100 Subject: [PATCH] core: add float-precision to compare float numbers --- .../standard/snmp/mode/components/sensor.pm | 15 ++++--- centreon-plugins/centreon/plugins/misc.pm | 2 +- centreon-plugins/centreon/plugins/mode.pm | 1 + centreon-plugins/centreon/plugins/output.pm | 41 +++++++++++-------- centreon-plugins/centreon/plugins/perfdata.pm | 21 +++++++++- 5 files changed, 54 insertions(+), 26 deletions(-) diff --git a/centreon-plugins/centreon/common/cisco/standard/snmp/mode/components/sensor.pm b/centreon-plugins/centreon/common/cisco/standard/snmp/mode/components/sensor.pm index 9182b48a2..8c72b4a64 100644 --- a/centreon-plugins/centreon/common/cisco/standard/snmp/mode/components/sensor.pm +++ b/centreon-plugins/centreon/common/cisco/standard/snmp/mode/components/sensor.pm @@ -141,13 +141,13 @@ sub get_default_warning_threshold { } # when it's the same value. Means no threshold. - if (defined($low_th) && defined($high_th) && $high_th < $low_th) { + if (defined($low_th) && defined($high_th) && $high_th <= $low_th) { return ''; } my $th = ''; $th = centreon::plugins::misc::expand_exponential(value => $low_th) . ':' if (defined($low_th)); $th .= centreon::plugins::misc::expand_exponential(value => $high_th) if (defined($high_th)); - + return $th; } @@ -174,7 +174,7 @@ sub get_default_critical_threshold { } # when it's the same value. Means no threshold. - if (defined($low_th) && defined($high_th) && $high_th < $low_th) { + if (defined($low_th) && defined($high_th) && $high_th <= $low_th) { return ''; } my $th = ''; @@ -204,6 +204,7 @@ sub check { $result->{entSensorValue} = defined($result->{entSensorValue}) ? $result->{entSensorValue} * (10 ** ($result->{entSensorScale}) * (10 ** -($result->{entSensorPrecision}))) : undef; + $result->{entSensorValue} = sprintf("%.2f", $result->{entSensorValue}); $self->{output}->output_add(long_msg => sprintf("Sensor '%s' status is '%s' [instance: %s] [value: %s %s]", $sensor_descr, $result->{entSensorStatus}, @@ -228,8 +229,12 @@ sub check { $self->{perfdata}->threshold_validate(label => 'critical-' . $component . '-instance-' . $instance, value => $crit_th); $warn = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $component . '-instance-' . $instance); $crit = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $component . '-instance-' . $instance); - $exit2 = $self->{perfdata}->threshold_check(value => $result->{entSensorValue}, threshold => [ { label => 'critical-' . $component . '-instance-' . $instance, exit_litteral => 'critical' }, - { label => 'warning-' . $component . '-instance-' . $instance, exit_litteral => 'warning' } ]); + $exit2 = $self->{perfdata}->threshold_check( + value => $result->{entSensorValue}, + threshold => [ { label => 'critical-' . $component . '-instance-' . $instance, exit_litteral => 'critical' }, + { label => 'warning-' . $component . '-instance-' . $instance, exit_litteral => 'warning' } + ] + ); } if (!$self->{output}->is_status(value => $exit2, compare => 'ok', litteral => 1)) { $self->{output}->output_add(severity => $exit2, diff --git a/centreon-plugins/centreon/plugins/misc.pm b/centreon-plugins/centreon/plugins/misc.pm index f31b8c0c8..9a1b74218 100644 --- a/centreon-plugins/centreon/plugins/misc.pm +++ b/centreon-plugins/centreon/plugins/misc.pm @@ -439,7 +439,7 @@ sub parse_threshold { my (%options) = @_; my $perf = trim($options{threshold}); - my $perf_result = { arobase => 0, infinite_neg => 0, infinite_pos => 0, start => "", end => "" }; + my $perf_result = { arobase => 0, infinite_neg => 0, infinite_pos => 0, start => '', end => '' }; my $global_status = 1; if ($perf =~ /^(\@?)((?:~|(?:\+|-)?\d+(?:[\.,]\d+)?|):)?((?:\+|-)?\d+(?:[\.,]\d+)?)?$/) { diff --git a/centreon-plugins/centreon/plugins/mode.pm b/centreon-plugins/centreon/plugins/mode.pm index e95bd6556..13507acd4 100644 --- a/centreon-plugins/centreon/plugins/mode.pm +++ b/centreon-plugins/centreon/plugins/mode.pm @@ -30,6 +30,7 @@ sub new { bless $self, $class; $self->{perfdata} = centreon::plugins::perfdata->new(output => $options{output}); + %{$self->{option_results}} = (); $self->{output} = $options{output}; $self->{mode} = $options{mode}; diff --git a/centreon-plugins/centreon/plugins/output.pm b/centreon-plugins/centreon/plugins/output.pm index f2fbe319b..ead500f66 100644 --- a/centreon-plugins/centreon/plugins/output.pm +++ b/centreon-plugins/centreon/plugins/output.pm @@ -33,24 +33,25 @@ sub new { exit 3; } - $options{options}->add_options(arguments => - { - "explode-perfdata-max:s@" => { name => 'explode_perfdata_max' }, - "range-perfdata:s" => { name => 'range_perfdata' }, - "filter-perfdata:s" => { name => 'filter_perfdata' }, - "change-perfdata:s@" => { name => 'change_perfdata' }, - "extend-perfdata:s@" => { name => 'extend_perfdata' }, - "change-short-output:s@" => { name => 'change_short_output' }, - "filter-uom:s" => { name => 'filter_uom' }, - "verbose" => { name => 'verbose' }, - "debug" => { name => 'debug' }, - "opt-exit:s" => { name => 'opt_exit', default => 'unknown' }, - "output-xml" => { name => 'output_xml' }, - "output-json" => { name => 'output_json' }, - "output-file:s" => { name => 'output_file' }, - "disco-format" => { name => 'disco_format' }, - "disco-show" => { name => 'disco_show' }, - }); + $options{options}->add_options(arguments => { + "explode-perfdata-max:s@" => { name => 'explode_perfdata_max' }, + "range-perfdata:s" => { name => 'range_perfdata' }, + "filter-perfdata:s" => { name => 'filter_perfdata' }, + "change-perfdata:s@" => { name => 'change_perfdata' }, + "extend-perfdata:s@" => { name => 'extend_perfdata' }, + "change-short-output:s@" => { name => 'change_short_output' }, + "filter-uom:s" => { name => 'filter_uom' }, + "verbose" => { name => 'verbose' }, + "debug" => { name => 'debug' }, + "opt-exit:s" => { name => 'opt_exit', default => 'unknown' }, + "output-xml" => { name => 'output_xml' }, + "output-json" => { name => 'output_json' }, + "output-file:s" => { name => 'output_file' }, + "disco-format" => { name => 'disco_format' }, + "disco-show" => { name => 'disco_show' }, + "float-precision:s" => { name => 'float_precision', default => 8 }, + }); + %{$self->{option_results}} = (); $self->{option_msg} = []; @@ -1121,6 +1122,10 @@ Display discovery arguments (if the mode manages it). Display discovery values (if the mode manages it). +=item B<--float-precision> + +Set the float precision for thresholds (Default: 8). + =head1 DESCRIPTION B. diff --git a/centreon-plugins/centreon/plugins/perfdata.pm b/centreon-plugins/centreon/plugins/perfdata.pm index 23475a597..7368b4844 100644 --- a/centreon-plugins/centreon/plugins/perfdata.pm +++ b/centreon-plugins/centreon/plugins/perfdata.pm @@ -32,6 +32,8 @@ sub new { $self->{output} = $options{output}; # Typical Nagios Perfdata 'with ~ @ ..' $self->{threshold_label} = {}; + $self->{float_precision} = defined($self->{output}->{option_results}->{float_precision}) && $self->{output}->{option_results}->{float_precision} =~ /\d+/ ? + int($self->{output}->{option_results}->{float_precision}) : 8; return $self; } @@ -86,6 +88,17 @@ sub threshold_validate { ($status, my $result_perf) = centreon::plugins::misc::parse_threshold(threshold => $options{value}); $self->{threshold_label}->{$options{label}} = { %{$self->{threshold_label}->{$options{label}}}, %$result_perf }; + + $self->{threshold_label}->{$options{label}}->{start_precision} = $self->{threshold_label}->{$options{label}}->{start}; + if ($self->{threshold_label}->{$options{label}}->{start} =~ /[.,]/) { + $self->{threshold_label}->{$options{label}}->{start_precision} = sprintf("%.$self->{output}->{option_results}->{float_precision}f", $self->{threshold_label}->{$options{label}}->{start}); + } + + $self->{threshold_label}->{$options{label}}->{end_precision} = $self->{threshold_label}->{$options{label}}->{end}; + if ($self->{threshold_label}->{$options{label}}->{end} =~ /[.,]/) { + $self->{threshold_label}->{$options{label}}->{end_precision} = sprintf("%.$self->{output}->{option_results}->{float_precision}f", $self->{threshold_label}->{$options{label}}->{end}); + } + return $status; } @@ -94,12 +107,16 @@ sub threshold_check { # Can check multiple threshold. First match: out. Order is important # options{value}: value to compare # options{threshold}: ref to an array (example: [ {label => 'warning', exit_litteral => 'warning' }, {label => 'critical', exit_litteral => 'critical'} ] + if ($options{value} =~ /[.,]/) { + $options{value} = sprintf("%.$self->{output}->{option_results}->{float_precision}f", $options{value}); + } + foreach (@{$options{threshold}}) { next if (!defined($self->{threshold_label}->{$_->{label}})); next if (!defined($self->{threshold_label}->{$_->{label}}->{value}) || $self->{threshold_label}->{$_->{label}}->{value} eq ''); - if ($self->{threshold_label}->{$_->{label}}->{arobase} == 0 && ($options{value} < $self->{threshold_label}->{$_->{label}}->{start} || $options{value} > $self->{threshold_label}->{$_->{label}}->{end})) { + if ($self->{threshold_label}->{$_->{label}}->{arobase} == 0 && ($options{value} < $self->{threshold_label}->{$_->{label}}->{start_precision} || $options{value} > $self->{threshold_label}->{$_->{label}}->{end_precision})) { return $_->{exit_litteral}; - } elsif ($self->{threshold_label}->{$_->{label}}->{arobase} == 1 && ($options{value} >= $self->{threshold_label}->{$_->{label}}->{start} && $options{value} <= $self->{threshold_label}->{$_->{label}}->{end})) { + } elsif ($self->{threshold_label}->{$_->{label}}->{arobase} == 1 && ($options{value} >= $self->{threshold_label}->{$_->{label}}->{start_precision} && $options{value} <= $self->{threshold_label}->{$_->{label}}->{end_precision})) { return $_->{exit_litteral}; } }