From b421d0888f81b47a8c30e0dea267c5e83b339798 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Wed, 28 Sep 2022 15:07:38 +0200 Subject: [PATCH] (core) add alert_triggered() method for option --filter-perfdata-adv (#3935) --- centreon-plugins/centreon/plugins/misc.pm | 35 ++++++++++++++----- centreon-plugins/centreon/plugins/output.pm | 2 ++ centreon-plugins/centreon/plugins/perfdata.pm | 2 +- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/centreon-plugins/centreon/plugins/misc.pm b/centreon-plugins/centreon/plugins/misc.pm index 6152eff35..186e217ce 100644 --- a/centreon-plugins/centreon/plugins/misc.pm +++ b/centreon-plugins/centreon/plugins/misc.pm @@ -458,13 +458,32 @@ sub convert_fahrenheit { sub expand_exponential { my (%options) = @_; - + return $options{value} unless ($options{value} =~ /^(.*)e([-+]?)(.*)$/); my ($num, $sign, $exp) = ($1, $2, $3); my $sig = $sign eq '-' ? "." . ($exp - 1 + length $num) : ''; return sprintf("%${sig}f", $options{value}); } +sub alert_triggered { + my (%options) = @_; + + my ($rv_warn, $warning) = parse_threshold(threshold => $options{warning}); + my ($rv_crit, $critical) = parse_threshold(threshold => $options{critical}); + + foreach ([$rv_warn, $warning], [$rv_crit, $critical]) { + next if ($_->[0] == 0); + + if ($_->[1]->{arobase} == 0 && ($options{value} < $_->[1]->{start} || $options{value} > $_->[1]->{end})) { + return 1; + } elsif ($_->[1]->{arobase} == 1 && ($options{value} >= $_->[1]->{start} && $options{value} <= $_->[1]->{end})) { + return 1; + } + } + + return 0; +} + sub parse_threshold { my (%options) = @_; @@ -513,19 +532,19 @@ sub parse_threshold { sub get_threshold_litteral { my (%options) = @_; - + my $perf_output = ($options{arobase} == 1 ? '@' : '') . - (($options{infinite_neg} == 0) ? $options{start} : '~') . - ':' . - (($options{infinite_pos} == 0) ? $options{end} : ''); + (($options{infinite_neg} == 0) ? $options{start} : '~') . + ':' . + (($options{infinite_pos} == 0) ? $options{end} : ''); return $perf_output; } sub set_timezone { my (%options) = @_; - + return {} if (!defined($options{name}) || $options{name} eq ''); - + centreon::plugins::misc::mymodule_load( output => $options{output}, module => 'DateTime::TimeZone', error_msg => "Cannot load module 'DateTime::TimeZone'." @@ -533,7 +552,7 @@ sub set_timezone { if (DateTime::TimeZone->is_valid_name($options{name})) { return { time_zone => DateTime::TimeZone->new(name => $options{name}) }; } - + # try to manage syntax (:Pacific/Noumea for example) if ($options{name} =~ /^:(.*)$/ && DateTime::TimeZone->is_valid_name($1)) { return { time_zone => DateTime::TimeZone->new(name => $1) }; diff --git a/centreon-plugins/centreon/plugins/output.pm b/centreon-plugins/centreon/plugins/output.pm index ed50ff001..af625dab8 100644 --- a/centreon-plugins/centreon/plugins/output.pm +++ b/centreon-plugins/centreon/plugins/output.pm @@ -154,6 +154,7 @@ sub check_options { if (defined($self->{option_results}->{filter_perfdata_adv}) && $self->{option_results}->{filter_perfdata_adv} ne '') { $self->{option_results}->{filter_perfdata_adv} =~ s/%\{(.*?)\}/\$values->{$1}/g; $self->{option_results}->{filter_perfdata_adv} =~ s/%\((.*?)\)/\$values->{$1}/g; + $self->{option_results}->{filter_perfdata_adv} =~ s/alert_triggered\(\)/alert_triggered\(%\$values\)/g; } $self->load_perfdata_extend_args(); @@ -956,6 +957,7 @@ sub load_eval { $self->{safe} = Safe->new(); $self->{safe}->share('$values'); $self->{safe}->share('$assign_var'); + $self->{safe}->share_from('centreon::plugins::misc', ['alert_triggered']); } $self->{safe_test} = 1; diff --git a/centreon-plugins/centreon/plugins/perfdata.pm b/centreon-plugins/centreon/plugins/perfdata.pm index e180bfcc7..df0aa1e1b 100644 --- a/centreon-plugins/centreon/plugins/perfdata.pm +++ b/centreon-plugins/centreon/plugins/perfdata.pm @@ -110,7 +110,7 @@ sub threshold_check { 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 '');