From a5455c401bd64f3a1b564ce24dd042d3f6dd1290 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Tue, 26 Mar 2019 15:39:49 +0100 Subject: [PATCH] Fix #1442 --- centreon/plugins/output.pm | 155 +++++++++++++++++++++++++++++++++++-- 1 file changed, 149 insertions(+), 6 deletions(-) diff --git a/centreon/plugins/output.pm b/centreon/plugins/output.pm index ead500f66..8697032f9 100644 --- a/centreon/plugins/output.pm +++ b/centreon/plugins/output.pm @@ -39,6 +39,7 @@ sub new { "filter-perfdata:s" => { name => 'filter_perfdata' }, "change-perfdata:s@" => { name => 'change_perfdata' }, "extend-perfdata:s@" => { name => 'extend_perfdata' }, + "extend-perfdata-group:s@"=> { name => 'extend_perfdata_group' }, "change-short-output:s@" => { name => 'change_short_output' }, "filter-uom:s" => { name => 'filter_uom' }, "verbose" => { name => 'verbose' }, @@ -795,6 +796,43 @@ sub parse_pfdata_math { return (0, $args); } +sub parse_group_pfdata { + my ($self, %options) = @_; + + $options{args} =~ s/^\s+//; + $options{args} =~ s/\s+$//; + my $args = { pattern_pf => $options{args} }; + return $args; +} + +sub parse_pfdata_min { + my ($self, %options) = @_; + + my $args = $self->parse_group_pfdata(%options); + return (0, $args); +} + +sub parse_pfdata_max { + my ($self, %options) = @_; + + my $args = $self->parse_group_pfdata(%options); + return (0, $args); +} + +sub parse_pfdata_average { + my ($self, %options) = @_; + + my $args = $self->parse_group_pfdata(%options); + return (0, $args); +} + +sub parse_pfdata_sum { + my ($self, %options) = @_; + + my $args = $self->parse_group_pfdata(%options); + return (0, $args); +} + sub apply_pfdata_scale { my ($self, %options) = @_; @@ -919,13 +957,82 @@ sub apply_pfdata_math { ${$options{perf}}->{$threshold} = centreon::plugins::misc::get_threshold_litteral(%$result); } - ${$options{perf}}->{max} = 100; + ${$options{perf}}->{max} = 100; +} + +sub apply_pfdata_min { + my ($self, %options) = @_; + + my $pattern_pf; + eval "\$pattern_pf = \"$options{args}->{pattern_pf}\""; + my $min; + for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { + next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); + next if ($self->{perfdatas}->[$i]->{value} !~ /\d+/); + $min = $self->{perfdatas}->[$i]->{value} + if (!defined($min) || $min > $self->{perfdatas}->[$i]->{value}); + } + + ${$options{perf}}->{value} = $min + if (defined($min)); +} + +sub apply_pfdata_max { + my ($self, %options) = @_; + + my $pattern_pf; + eval "\$pattern_pf = \"$options{args}->{pattern_pf}\""; + my $max; + for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { + next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); + next if ($self->{perfdatas}->[$i]->{value} !~ /\d+/); + $max = $self->{perfdatas}->[$i]->{value} + if (!defined($max) || $max < $self->{perfdatas}->[$i]->{value}); + } + + ${$options{perf}}->{value} = $max + if (defined($max)); +} + +sub apply_pfdata_sum { + my ($self, %options) = @_; + + my $pattern_pf; + eval "\$pattern_pf = \"$options{args}->{pattern_pf}\""; + my $sum = 0; + for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { + next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); + next if ($self->{perfdatas}->[$i]->{value} !~ /\d+/); + $sum += $self->{perfdatas}->[$i]->{value}; + } + + ${$options{perf}}->{value} = $sum; +} + +sub apply_pfdata_average { + my ($self, %options) = @_; + + my $pattern_pf; + eval "\$pattern_pf = \"$options{args}->{pattern_pf}\""; + my ($sum, $num) = (0, 0); + for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { + next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); + next if ($self->{perfdatas}->[$i]->{value} !~ /\d+/); + $sum += $self->{perfdatas}->[$i]->{value}; + $num++; + } + + ${$options{perf}}->{value} = ($num > 0) ? sprintf("%.2f", ($sum / $num)) : 0; } sub load_perfdata_extend_args { my ($self, %options) = @_; - - foreach ([$self->{option_results}->{change_perfdata}, 1], [$self->{option_results}->{extend_perfdata}, 2]) { + + foreach ( + [$self->{option_results}->{change_perfdata}, 1], + [$self->{option_results}->{extend_perfdata}, 2], + [$self->{option_results}->{extend_perfdata_group}, 3], + ) { next if (!defined($_->[0])); foreach my $arg (@{$_->[0]}) { $self->parse_perfdata_extend_args(arg => $arg, type => $_->[1]); @@ -938,7 +1045,7 @@ sub parse_perfdata_extend_args { # --extend-perfdata=searchlabel,newlabel,method[,newuom] my ($pfdata_match, $pfdata_substitute, $method, $uom_substitute) = split /,/, $options{arg}; - return if (!defined($pfdata_match) || $pfdata_match eq ''); + return if ((!defined($pfdata_match) || $pfdata_match eq '') && $options{type} != 3); $self->{pfdata_extends} = [] if (!defined($self->{pfdata_extends})); my $pfdata_extends = { @@ -949,7 +1056,7 @@ sub parse_perfdata_extend_args { }; if (defined($method) && $method ne '') { - if ($method !~ /^\s*(invert|percent|scale|math)\s*\(\s*(.*?)\s*\)\s*$/) { + if ($method !~ /^\s*(invert|percent|scale|math|min|max|average|sum)\s*\(\s*(.*?)\s*\)\s*$/) { $self->output_add(long_msg => "method in argument '$options{arg}' is unknown", debug => 1); return ; } @@ -964,7 +1071,7 @@ sub parse_perfdata_extend_args { } } } - + push @{$self->{pfdata_extends}}, $pfdata_extends; } @@ -993,10 +1100,31 @@ sub apply_perfdata_extend { foreach my $extend (@{$self->{pfdata_extends}}) { my $new_pfdata = []; + # Manage special case when type group and pfdata_match empty + if ($extend->{type} == 3 && (!defined($extend->{pfdata_match}) || $extend->{pfdata_match} eq '')) { + next if (!defined($extend->{pfdata_substitute}) || $extend->{pfdata_substitute} eq ''); + my $new_perf = { + label => $extend->{pfdata_substitute}, value => '', + unit => defined($extend->{uom_substitute}) ? $extend->{uom_substitute} : '', + warning => '', critical => '', min => '', max => '' + }; + + if (defined($extend->{method_name})) { + my $func = $self->can('apply_pfdata_' . $extend->{method_name}); + $func->($self, perf => \$new_perf, args => $extend->{method_args}); + } + + push @{$self->{perfdatas}}, $new_perf; + next; + } + for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { next if ($self->{perfdatas}->[$i]->{label} !~ /$extend->{pfdata_match}/); my $new_perf = { %{$self->{perfdatas}->[$i]} }; + if ($extend->{type} == 3) { + $new_perf = { label => $self->{perfdatas}->[$i]->{label}, value => '', unit => '', warning => '', critical => '', min => '', max => '' }; + } if (defined($extend->{pfdata_substitute})) { eval "\$new_perf->{label} =~ s{$extend->{pfdata_match}}{$extend->{pfdata_substitute}}"; @@ -1083,6 +1211,21 @@ Change traffic values in percent: --change-perfdata=traffic_in,,percent() =back +=item B<--extend-perfdata-group> + +Extend perfdata from multiple perfdatas (methods in target are: min, max, average, sum) +Syntax: --extend-perfdata-group=searchlabel,newlabel,target[,newuom] + +Common examples: + +=over 4 + +Sum wrong packets from all interfaces (with interface need --units-errors=absolute): --extend-perfdata-group=',packets_wrong,sum(packet_(discard|error)_(in|out))' + +Sum traffic by interface: --extend-perfdata-group='traffic_in_(.*),traffic_$1,sum(traffic_(in|out)_$1)' + +=back + =item B<--change-short-output> Change short output display. --change-short-output=pattern~replace~modifier