From 29e8bce989254b66b50021914bfb70c3ae3f08bf Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Sun, 6 Jan 2019 09:50:24 +0100 Subject: [PATCH] + WIP perfdata extend system --- centreon-plugins/centreon/plugins/misc.pm | 37 ++++++++ centreon-plugins/centreon/plugins/output.pm | 95 +++++++++++-------- centreon-plugins/centreon/plugins/perfdata.pm | 41 +------- 3 files changed, 96 insertions(+), 77 deletions(-) diff --git a/centreon-plugins/centreon/plugins/misc.pm b/centreon-plugins/centreon/plugins/misc.pm index b535829db..d8cb36684 100644 --- a/centreon-plugins/centreon/plugins/misc.pm +++ b/centreon-plugins/centreon/plugins/misc.pm @@ -402,6 +402,43 @@ sub convert_bytes { return $value; } +sub parse_threshold { + my ($perf) = @_; + + $perf = trim($perf); + + my $arobase = 0; + my $infinite_neg = 0; + my $infinite_pos = 0; + my $value_start = ""; + my $value_end = ""; + my $global_status = 1; + + if ($perf =~ /^(\@?)((?:~|(?:\+|-)?\d+(?:[\.,]\d+)?|):)?((?:\+|-)?\d+(?:[\.,]\d+)?)?$/) { + $value_start = $2 if (defined($2)); + $value_end = $3 if (defined($3)); + $arobase = 1 if (defined($1) && $1 eq '@'); + $value_start =~ s/[\+:]//g; + $value_end =~ s/\+//; + if ($value_end eq '') { + $value_end = 1e500; + $infinite_pos = 1; + } + $value_start = 0 if ($value_start eq ''); + $value_start =~ s/,/\./; + $value_end =~ s/,/\./; + + if ($value_start eq '~') { + $value_start = -1e500; + $infinite_neg = 1; + } + } else { + $global_status = 0; + } + + return ($global_status, $value_start, $value_end, $arobase, $infinite_neg, $infinite_pos); +} + 1; __END__ diff --git a/centreon-plugins/centreon/plugins/output.pm b/centreon-plugins/centreon/plugins/output.pm index e7fe92608..84e97677c 100644 --- a/centreon-plugins/centreon/plugins/output.pm +++ b/centreon-plugins/centreon/plugins/output.pm @@ -123,15 +123,7 @@ sub check_options { } } - if (defined($self->{option_results}->{change_perfdata})) { - foreach (@{$self->{option_results}->{change_perfdata}}) { - if (! /^(.+?),(.+)$/) { - $self->add_option_msg(short_msg => "Wrong change-perfdata option '" . $_ . "' (syntax: match,substitute)"); - $self->option_exit(); - } - $self->{change_perfdata}->{$1} = $2; - } - } + $self->load_perfdata_extend_args(); } sub add_option_msg { @@ -194,36 +186,6 @@ sub perfdata_add { push @{$self->{perfdatas}}, $perfdata; } -sub change_perfdatas { - my ($self, %options) = @_; - - if ($self->{option_results}->{change_perfdata}) { - foreach (@{$self->{perfdatas}}) { - foreach my $filter (keys %{$self->{change_perfdata}}) { - if ($_->{label} =~ /$filter/) { - eval "\$_->{label} =~ s{$filter}{$self->{change_perfdata}->{$filter}}"; - last; - } - } - } - } - - return if ($self->{explode_perfdata_total} == 0); - foreach (@{$self->{perfdatas}}) { - next if ($_->{max} eq ''); - if ($self->{explode_perfdata_total} == 2) { - $self->perfdata_add(label => $_->{label} . '_max', value => $_->{max}); - next; - } - foreach my $regexp (keys %{$self->{explode_perfdatas}}) { - if ($_->{label} =~ /$regexp/) { - $self->perfdata_add(label => $self->{explode_perfdatas}->{$regexp}, value => $_->{max}); - last; - } - } - } -} - sub range_perfdata { my ($self, %options) = @_; @@ -746,6 +708,61 @@ sub is_disco_show { return 0; } +# --add-perfdata=used,,scale(auto) +# --add-perfdata=used,,scale(MB) +# --add-perfdata=traffic_in,,scale(Mbps),mbps +# --add-perfdata=used,,percent() +# --add-perfdata=perfx,,math(perfx+10-100) +# --add-perfdata=free,used,used() +# --add-perfdata=used,free,free() +# ^([KMGTP])?(B|b|bps|\/s|auto)$ + +# parse_threshold est dans misc maintenant. + +sub load_perfdata_extend_args { + my ($self, %options) = @_; + + if (defined($self->{option_results}->{change_perfdata})) { + foreach (@{$self->{option_results}->{change_perfdata}}) { + if (! /^(.+?),(.+)$/) { + $self->add_option_msg(short_msg => "Wrong change-perfdata option '" . $_ . "' (syntax: match,substitute)"); + $self->option_exit(); + } + $self->{change_perfdata}->{$1} = $2; + } + } +} + +sub change_perfdatas { + my ($self, %options) = @_; + + if ($self->{option_results}->{change_perfdata}) { + foreach (@{$self->{perfdatas}}) { + foreach my $filter (keys %{$self->{change_perfdata}}) { + if ($_->{label} =~ /$filter/) { + eval "\$_->{label} =~ s{$filter}{$self->{change_perfdata}->{$filter}}"; + last; + } + } + } + } + + return if ($self->{explode_perfdata_total} == 0); + foreach (@{$self->{perfdatas}}) { + next if ($_->{max} eq ''); + if ($self->{explode_perfdata_total} == 2) { + $self->perfdata_add(label => $_->{label} . '_max', value => $_->{max}); + next; + } + foreach my $regexp (keys %{$self->{explode_perfdatas}}) { + if ($_->{label} =~ /$regexp/) { + $self->perfdata_add(label => $self->{explode_perfdatas}->{$regexp}, value => $_->{max}); + last; + } + } + } +} + 1; __END__ diff --git a/centreon-plugins/centreon/plugins/perfdata.pm b/centreon-plugins/centreon/plugins/perfdata.pm index 8c415ca0e..d3e7b1fca 100644 --- a/centreon-plugins/centreon/plugins/perfdata.pm +++ b/centreon-plugins/centreon/plugins/perfdata.pm @@ -22,6 +22,7 @@ package centreon::plugins::perfdata; use strict; use warnings; +use centreon::plugins::misc; sub new { my ($class, %options) = @_; @@ -82,7 +83,8 @@ sub threshold_validate { return $status; } - ($status, $self->{threshold_label}->{$options{label}}->{start}, $self->{threshold_label}->{$options{label}}->{end}, $self->{threshold_label}->{$options{label}}->{arobase}, $self->{threshold_label}->{$options{label}}->{infinite_neg}, $self->{threshold_label}->{$options{label}}->{infinite_pos}) = $self->parse_threshold($options{value}); + ($status, $self->{threshold_label}->{$options{label}}->{start}, $self->{threshold_label}->{$options{label}}->{end}, $self->{threshold_label}->{$options{label}}->{arobase}, $self->{threshold_label}->{$options{label}}->{infinite_neg}, $self->{threshold_label}->{$options{label}}->{infinite_pos}) = + centreon::plugins::misc::parse_threshold($options{value}); return $status; } @@ -113,43 +115,6 @@ sub trim { return $value; } -sub parse_threshold { - my ($self, $perf) = @_; - - $perf = $self->trim($perf); - - my $arobase = 0; - my $infinite_neg = 0; - my $infinite_pos = 0; - my $value_start = ""; - my $value_end = ""; - my $global_status = 1; - - if ($perf =~ /^(\@?)((?:~|(?:\+|-)?\d+(?:[\.,]\d+)?|):)?((?:\+|-)?\d+(?:[\.,]\d+)?)?$/) { - $value_start = $2 if (defined($2)); - $value_end = $3 if (defined($3)); - $arobase = 1 if (defined($1) && $1 eq '@'); - $value_start =~ s/[\+:]//g; - $value_end =~ s/\+//; - if ($value_end eq '') { - $value_end = 1e500; - $infinite_pos = 1; - } - $value_start = 0 if ($value_start eq ''); - $value_start =~ s/,/\./; - $value_end =~ s/,/\./; - - if ($value_start eq '~') { - $value_start = -1e500; - $infinite_neg = 1; - } - } else { - $global_status = 0; - } - - return ($global_status, $value_start, $value_end, $arobase, $infinite_neg, $infinite_pos); -} - sub change_bytes { my ($self, %options) = @_; my $divide = defined($options{network}) ? 1000 : 1024;