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

Threshold specification fixes #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
160 changes: 98 additions & 62 deletions lib/Monitoring/Plugin/Range.pm
Original file line number Diff line number Diff line change
Expand Up @@ -48,74 +48,110 @@ sub _set_range_end {

# Returns a N::P::Range object if the string is a conforms to a Monitoring Plugin range string, otherwise null
sub parse_range_string {
my ($class, $string) = @_;
my $valid = 0;
my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => OUTSIDE);

$string =~ s/\s//g; # strip out any whitespace
# check for valid range definition
unless ( $string =~ /[\d~]/ && $string =~ m/^\@?($value_re|~)?(:($value_re)?)?$/ ) {
carp "invalid range definition '$string'";
return undef;
}

if ($string =~ s/^\@//) {
$range->alert_on(INSIDE);
}

if ($string =~ s/^~//) { # '~:x'
$range->start_infinity(1);
}
if ( $string =~ m/^($value_re)?:/ ) { # '10:'
my $start = $1;
$range->_set_range_start($start) if defined $start;
$range->end_infinity(1); # overridden below if there's an end specified
$string =~ s/^($value_re)?://;
$valid++;
}
if ($string =~ /^($value_re)$/) { # 'x:10' or '10'
$range->_set_range_end($string);
$valid++;
}

if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) {
return $range;
}
return undef;
my ( $class, $string ) = @_;
my $valid = 0;
my $range = $class->new(
start => 0, start_infinity => 0, end => 0,
end_infinity => 1, alert_on => OUTSIDE
);

$string =~ s/\s//g; # strip out any whitespace
# check for valid range definition
unless ( $string =~ /[\d~]/
&& $string =~ m/^\@?($value_re|~)?(:($value_re)?)?$/ ) {
carp "invalid range definition '$string'";
return undef;
}

if ( $string =~ s/^\@// ) {
$range->alert_on(INSIDE);
}

if ( $string =~ s/^~// ) { # '~:x'
$range->start_infinity(1);
}
if ( $string =~ m/^($value_re)?:/ ) { # '10:'
my $start = $1;
$range->_set_range_start($start) if defined $start;
$range->end_infinity(1); # overridden below if there's an end specified
$string =~ s/^($value_re)?://;
$valid++;
}
if ( $string =~ /^($value_re)$/ ) { # 'x:10' or '10'
$range->_set_range_end($string);
$range->end_infinity(0);
$valid++;
}
if (
$valid
&& ( $range->start_infinity == 1
|| $range->end_infinity == 1
|| $range->start <= $range->end )
) {
return $range;
}
return undef;
}


# Returns 1 if an alert should be raised, otherwise 0
sub check_range {
my ($self, $value) = @_;
my $false = 0;
my $true = 1;
if ($self->alert_on == INSIDE) {
$false = 1;
$true = 0;
}
if ($self->end_infinity == 0 && $self->start_infinity == 0) {
if ($self->start <= $value && $value <= $self->end) {
return $false;
} else {
return $true;
}
} elsif ($self->start_infinity == 0 && $self->end_infinity == 1) {
if ( $value >= $self->start ) {
return $false;
} else {
return $true;
}
} elsif ($self->start_infinity == 1 && $self->end_infinity == 0) {
if ($value <= $self->end) {
return $false;
} else {
return $true;
}
} else {
return $false;
}
my ( $self, $value ) = @_;
my ( $eval_string, $op );

# untaint
if ($value =~ /($value_re)/) {
$value = $1;
}
else {
carp "invalid value '$value'";
return undef;
}

# this could be a from-to or equality check
if ( $self->end_infinity == 0 && $self->start_infinity == 0 ) {

# this could be a test for exclusion (outside the range)
# or inclusion (inside the range)
my $left_op = $self->alert_on == INSIDE ? '>=' : '<';
my $right_op = $self->alert_on == INSIDE ? '<=' : '>';
my $cmp = $self->alert_on == INSIDE ? '&&' : '||';
$eval_string = sprintf(
"%s %s %s %s %s %s %s",
$value, $left_op, $self->start, $cmp,
$value, $right_op, $self->end
);
}
else {
my $check;

# there is a start value, testing to infinity
if ( $self->start_infinity == 0 && $self->end_infinity == 1 ) {

# if we are checking for a value inside the range
# it will be >= the start value
$op = $self->alert_on == INSIDE ? '>=' : '<';
$check = $self->start;
}

# there is an end value, testing from negative infinity
elsif ( $self->start_infinity == 1 && $self->end_infinity == 0 ) {

# if we are checking for a value inside the range
# it will be <= the start value
$op = $self->alert_on == INSIDE ? '<=' : '>';
$check = $self->end;
}
else {
return 0;
}
$eval_string = "$value $op $check";
}

return int( eval $eval_string );
}


# Constructor - map args to hashref for SUPER
sub new
{
Expand Down
19 changes: 12 additions & 7 deletions t/Monitoring-Plugin-01.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $p = Monitoring::Plugin->new( shortname => "SIZE", plugin => "check_stuff", ()
is($p->shortname, "SIZE", "shortname is not overriden by default");

diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE};
my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" );
my $t = $p->set_thresholds( warning => "10:", critical => "~:25" );

use Data::Dumper;
#diag "dumping p: ". Dumper $p;
Expand All @@ -44,7 +44,7 @@ $p->add_perfdata(
threshold => $t,
);

cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct");
cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:;~:25", "Perfdata correct");
#diag "dumping perfdata: ". Dumper ($p->perfdata);

$p->add_perfdata(
Expand All @@ -53,7 +53,7 @@ $p->add_perfdata(
threshold => $t,
);

is( $p->all_perfoutput, "size=1kB;10:25;~:25 time=3.52;10:25;~:25", "Perfdata correct when no uom specified");
is( $p->all_perfoutput, "size=1kB;10:;~:25 time=3.52;10:;~:25", "Perfdata correct when no uom specified");

my $expected = {qw(
-1 WARNING
Expand All @@ -64,8 +64,13 @@ my $expected = {qw(
30 CRITICAL
)};

foreach (sort {$a<=>$b} keys %$expected) {
like $p->die( return_code => $t->get_status($_), message => "page size at http://... was ${_}kB" ),
qr/$expected->{$_}/,
"Output okay. $_ = $expected->{$_}" ;
foreach ( sort { $a <=> $b } keys %$expected ) {
like(
$p->die(
return_code => $t->get_status($_),
message => "page size at http://... was ${_}kB"
),
qr/$expected->{$_}/,
"Output okay. $_ = $expected->{$_}"
);
}
Loading