+ WIP perfdata extend system

This commit is contained in:
garnier-quentin 2019-01-06 09:50:24 +01:00
parent 8b8ed15ec3
commit 29e8bce989
3 changed files with 96 additions and 77 deletions

View File

@ -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__

View File

@ -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__

View File

@ -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;