From 60eecd413c1b313e06fa36e172bb9e32f92369bc Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 21 Aug 2024 09:16:37 -0400 Subject: [PATCH 1/2] add option --change-output-adv --- src/centreon/plugins/output.pm | 48 ++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/centreon/plugins/output.pm b/src/centreon/plugins/output.pm index e2f9e540fd..4ebcc90164 100644 --- a/src/centreon/plugins/output.pm +++ b/src/centreon/plugins/output.pm @@ -45,6 +45,7 @@ sub new { 'change-exit:s@' => { name => 'change_exit' }, 'change-short-output:s@' => { name => 'change_short_output' }, 'change-long-output:s@' => { name => 'change_long_output' }, + 'change-output-adv:s@' => { name => 'change_output_adv' }, 'use-new-perfdata' => { name => 'use_new_perfdata' }, 'filter-uom:s' => { name => 'filter_uom' }, 'verbose' => { name => 'verbose' }, @@ -125,6 +126,23 @@ sub check_options { } } + if (defined($self->{option_results}->{change_output_adv})) { + foreach (@{$self->{option_results}->{change_output_adv}}) { + my ($expr, $short_output, $exit_code) = split /,/; + next if (!defined($expr) || $expr eq ''); + + $expr =~ s/%\{(.*?)\}/\$values->{$1}/g; + $expr =~ s/%\((.*?)\)/\$values->{$1}/g; + + $self->{change_output_adv} = [] if (!defined($self->{change_output_adv})); + push @{$self->{change_output_adv}}, { + expr => $expr, + short_output => $short_output, + exit_code => defined($exit_code) && $exit_code ne '' && $self->{errors}->{uc($exit_code)} ? uc($exit_code) : undef + }; + } + } + if (defined($self->{option_results}->{change_exit})) { $self->{change_exit} = {}; foreach (@{$self->{option_results}->{change_exit}}) { @@ -212,6 +230,7 @@ sub output_add { push @{$self->{global_short_outputs}->{uc($options->{severity})}}, $options->{short_msg}; $self->set_status(exit_litteral => $options->{severity}); } + if (defined($options->{long_msg})) { chomp $options->{long_msg}; @@ -484,7 +503,8 @@ sub output_txt_short_display { sub output_txt_short { my ($self, %options) = @_; - if (!defined($self->{option_results}->{change_short_output})) { + if (!defined($self->{option_results}->{change_short_output}) && + !defined($self->{change_output_adv})) { $self->output_txt_short_display(%options); return ; } @@ -504,6 +524,18 @@ sub output_txt_short { eval "\$stdout =~ s{$pattern}{$replace}$modifier"; } + my $exit = defined($options{exit_litteral}) ? $options{exit_litteral} : $self->{myerrors}->{ $self->{global_status} }; + foreach (@{$self->{change_output_adv}}) { + if ($self->test_eval(test => $_->{expr}, values => { short_output => $stdout, exit_code => $self->{errors}->{$exit} })) { + if (defined($_->{short_output}) && $_->{short_output} ne '') { + $stdout = $_->{short_output}; + } + if (defined($_->{exit_code}) && $_->{exit_code} ne '') { + $self->{coa_save_exit_code} = $_->{exit_code}; + } + } + } + print $stdout; } @@ -513,6 +545,7 @@ sub output_txt { my $force_long_output = (defined($options{force_long_output}) && $options{force_long_output} == 1) ? 1 : 0; return if ($self->{nodisplay} == 1); + if (defined($self->{global_short_concat_outputs}->{UNQUALIFIED_YET})) { $self->output_add(severity => uc($options{exit_litteral}), short_msg => $self->{global_short_concat_outputs}->{UNQUALIFIED_YET}); } @@ -690,6 +723,10 @@ sub exit { } else { $exit = $self->{myerrors}->{ $self->{global_status} }; } + + if (defined($self->{coa_save_exit_code})) { + $exit = $self->{coa_save_exit_code}; + } if (defined($self->{change_exit}) && defined($self->{change_exit}->{$exit})) { $exit = $self->{change_exit}->{$exit}; } @@ -1543,7 +1580,6 @@ metric will be named identically with a '_max' suffix). Example: it will split 'used_prct'=26.93%;0:80;0:90;0;100 into 'used_prct'=26.93%;0:80;0:90;0;100 'used_prct_max'=100%;;;; - =item B<--change-perfdata> B<--extend-perfdata> Change or extend perfdata. @@ -1599,6 +1635,14 @@ Replace an exit code with one of your choice. Example: adding --change-exit=unknown=critical will result in a CRITICAL state instead of an UNKNOWN state. +=item B<--change-output-adv> + +Replace short output and exit code based on a "if" condition using the following variables: +short_output, exit_code. +Variables must be written either %{variable} or %(variable). +Example: adding --change-output-adv='%(short_ouput) =~ /UNKNOWN: No daemon/,OK: No daemon,OK' will +change the following specific UNKNOWN result to an OK result. + =item B<--range-perfdata> Rewrite the ranges displayed in the perfdata. Accepted values: From 8f460d5b4fb83b76a38afde762287992e17f69ae Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Fri, 23 Aug 2024 09:19:55 -0400 Subject: [PATCH 2/2] fix litteral exit --- src/centreon/plugins/output.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/centreon/plugins/output.pm b/src/centreon/plugins/output.pm index 4ebcc90164..97b63fcef1 100644 --- a/src/centreon/plugins/output.pm +++ b/src/centreon/plugins/output.pm @@ -524,7 +524,7 @@ sub output_txt_short { eval "\$stdout =~ s{$pattern}{$replace}$modifier"; } - my $exit = defined($options{exit_litteral}) ? $options{exit_litteral} : $self->{myerrors}->{ $self->{global_status} }; + my $exit = defined($options{exit_litteral}) ? uc($options{exit_litteral}) : uc($self->{myerrors}->{ $self->{global_status} }); foreach (@{$self->{change_output_adv}}) { if ($self->test_eval(test => $_->{expr}, values => { short_output => $stdout, exit_code => $self->{errors}->{$exit} })) { if (defined($_->{short_output}) && $_->{short_output} ne '') {