Skip to content

Commit

Permalink
feat(option): added --change-output-adv option to alter output and ex…
Browse files Browse the repository at this point in the history
…it code (#5155, #5379)

Co-authored-by: garnier-quentin <garnier.quentin@gmail.com>
Refs: CTOR-820
  • Loading branch information
omercier and garnier-quentin authored Jan 13, 2025
1 parent accf292 commit 56e8d8c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
54 changes: 52 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 @@ -124,6 +125,29 @@ 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;

if ( defined($exit_code) && $exit_code ne '' && defined( $self->{errors}->{uc($exit_code)} ) ) {
$exit_code = uc($exit_code);
} else {
$exit_code = undef;
}

$self->{change_output_adv} = [] if (!defined($self->{change_output_adv}));
push @{$self->{change_output_adv}}, {
expr => $expr,
short_output => $short_output,
exit_code => $exit_code
};
}
}

if (defined($self->{option_results}->{change_exit})) {
$self->{change_exit} = {};
foreach (@{$self->{option_results}->{change_exit}}) {
Expand Down Expand Up @@ -211,6 +235,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 @@ -483,7 +508,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 @@ -503,6 +529,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 @@ -512,6 +550,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 @@ -689,6 +728,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 @@ -1542,7 +1585,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 @@ -1598,6 +1640,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
12 changes: 8 additions & 4 deletions tests/os/linux/snmp/load.robot
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Documentation Check load table
Resource ${CURDIR}${/}..${/}..${/}..${/}resources/import.resource

Test Timeout 120s

Test Setup Ctn Generic Suite Setup

*** Variables ***
${CMD} ${CENTREON_PLUGINS} --plugin=os::linux::snmp::plugin


*** Test Cases ***
load ${tc}
[Tags] os linux
[Tags] os linux snmp
${command} Catenate
... ${CMD}
... --mode=load
Expand All @@ -23,11 +23,15 @@ load ${tc}
... --snmp-timeout=1
... --critical=${critical}
... --warning=${warning}
... --average=${average}
... --average
... ${extra_options}

Ctn Run Command And Check Result As Strings ${command} ${expected_result}

Examples: tc critical warning average expected_result --
Examples: tc critical warning extra_options expected_result --
... 1 '6,5,4' '4,3,2' ${EMPTY} OK: Load average: 0.43 [0.87/2 CPUs], 0.32 [0.63/2 CPUs], 0.23 [0.47/2 CPUs] | 'avg_load1'=0.43;0:4;0:6;0; 'avg_load5'=0.32;0:3;0:5;0; 'avg_load15'=0.23;0:2;0:4;0; 'load1'=0.87;0:8;0:12;0; 'load5'=0.63;0:6;0:10;0; 'load15'=0.47;0:4;0:8;0;
... 2 '0,0,0' '4,3,2' ${EMPTY} CRITICAL: Load average: 0.43 [0.87/2 CPUs], 0.32 [0.63/2 CPUs], 0.23 [0.47/2 CPUs] | 'avg_load1'=0.43;0:4;0:0;0; 'avg_load5'=0.32;0:3;0:0;0; 'avg_load15'=0.23;0:2;0:0;0; 'load1'=0.87;0:8;0:0;0; 'load5'=0.63;0:6;0:0;0; 'load15'=0.47;0:4;0:0;0;
... 3 '600,500,100' '0,0,0' ${EMPTY} WARNING: Load average: 0.43 [0.87/2 CPUs], 0.32 [0.63/2 CPUs], 0.23 [0.47/2 CPUs] | 'avg_load1'=0.43;0:0;0:600;0; 'avg_load5'=0.32;0:0;0:500;0; 'avg_load15'=0.23;0:0;0:100;0; 'load1'=0.87;0:0;0:1200;0; 'load5'=0.63;0:0;0:1000;0; 'load15'=0.47;0:0;0:200;0;
... 4 '6,5,4' '4,3,2' --change-output-adv='%(short_output) =~ /OK:/ and %(exit_code) == 0,Forced from OK to WARNING,WARNING' Forced from OK to WARNING| 'avg_load1'=0.43;0:4;0:6;0; 'avg_load5'=0.32;0:3;0:5;0; 'avg_load15'=0.23;0:2;0:4;0; 'load1'=0.87;0:8;0:12;0; 'load5'=0.63;0:6;0:10;0; 'load15'=0.47;0:4;0:8;0;
... 5 '0,0,0' '4,3,2' --change-output-adv='%(short_output) =~ /CRITICAL:/ and %(exit_code) == 2,Forced from CRITICAL to OK,OK' Forced from CRITICAL to OK| 'avg_load1'=0.43;0:4;0:0;0; 'avg_load5'=0.32;0:3;0:0;0; 'avg_load15'=0.23;0:2;0:0;0; 'load1'=0.87;0:8;0:0;0; 'load5'=0.63;0:6;0:0;0; 'load15'=0.47;0:4;0:0;0;
... 6 '600,500,100' '0,0,0' --change-output-adv='%(short_output) =~ /WARNING:/ and %(exit_code) == 1,Forced from WARNING to CRITICAL,CRITICAL' Forced from WARNING to CRITICAL| 'avg_load1'=0.43;0:0;0:600;0; 'avg_load5'=0.32;0:0;0:500;0; 'avg_load15'=0.23;0:0;0:100;0; 'load1'=0.87;0:0;0:1200;0; 'load5'=0.63;0:0;0:1000;0; 'load15'=0.47;0:0;0:200;0;

0 comments on commit 56e8d8c

Please sign in to comment.