Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add common option --change-output-adv #5155

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions src/centreon/plugins/output.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down Expand Up @@ -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}}) {
Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -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 ;
}
Expand All @@ -504,6 +524,18 @@ sub output_txt_short {
eval "\$stdout =~ s{$pattern}{$replace}$modifier";
}

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 '') {
$stdout = $_->{short_output};
}
if (defined($_->{exit_code}) && $_->{exit_code} ne '') {
$self->{coa_save_exit_code} = $_->{exit_code};
}
}
}

print $stdout;
}

Expand All @@ -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});
}
Expand Down Expand Up @@ -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};
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down