From 96933fd2e1f53aff9c9ef26639fafe9a84ec754e Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Fri, 4 Aug 2006 20:22:31 +0000 Subject: Lots of extra tests and subsequent fixes (Nathan Vonnahme) git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1466 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/Changes b/Changes index 71208fb..8bf43f1 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl module Nagios::Plugin. +0.13 ??? + - Lots of extra tests and fixes from Nathan Vonnahme + 0.12 15th June 2006 - rrdlabel method available to get a performance label, converted to something rrd can use diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 2acc6ea..4e95e98 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm @@ -23,7 +23,7 @@ use Exporter; our @ISA = qw(Exporter Nagios::__::Plugin); our @EXPORT_OK = qw(%ERRORS); -our $VERSION = '0.12'; +our $VERSION = '0.13'; sub add_perfdata { my ($self, %args) = @_; @@ -59,7 +59,8 @@ Nagios::Plugin - Object oriented helper routines for your Nagios plugin use Nagios::Plugin qw(%ERRORS); $p = Nagios::Plugin->new( shortname => "PAGESIZE" ); - $threshold = $p->set_thresholds( warning => "10:25", critical => "25:" ); + $threshold = $p->set_thresholds( warning => "10:25", critical => "~:25" ); + # Critical if outside -INF to 25, ie > 25. Warn if outside 10-25, ie < 10 # ... collect current metric into $value if ($trouble_getting_metric) { @@ -120,7 +121,7 @@ Initializes a new Nagios::Plugin object. Can specify the shortname here. =head1 OBJECT METHODS -=item set_thresholds( warning => "10:25", critical => "25:" ) +=item set_thresholds( warning => "10:25", critical => "~:25" ) Sets the thresholds, based on the range specification at http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. @@ -148,6 +149,8 @@ http://nagiosplug.sourceforge.net Ton Voon, Eton.voon@altinity.comE +Thanks to Nathan Vonnahme for loads of extra tests and subsequent fixes. + =head1 COPYRIGHT AND LICENSE Copyright (C) 2006 by Nagios Plugin Development Team diff --git a/lib/Nagios/Plugin/Base.pm b/lib/Nagios/Plugin/Base.pm index f3a7f7c..d1678a5 100644 --- a/lib/Nagios/Plugin/Base.pm +++ b/lib/Nagios/Plugin/Base.pm @@ -5,6 +5,9 @@ package Nagios::Plugin::Base; use strict; use warnings; +use Nagios::Plugin; +our ($VERSION) = $Nagios::Plugin::VERSION; + use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(%ERRORS); @@ -13,28 +16,38 @@ our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); our %STATUS_TEXT = reverse %ERRORS; + my $exit_on_die = 1; sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; my $print_on_die = 1; sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; sub die { - my ($class, $args, $plugin) = @_; - my $return_code = $args->{return_code} || 3; - my $message = $args->{message} || "Internal error"; - my $output = join(" ", $STATUS_TEXT{$return_code}, $message); - if ($plugin) { - $output = $plugin->shortname." $output" if $plugin->shortname; - $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; - } - if ($print_on_die) { - print $output, $/; - } - if ($exit_on_die) { - exit $return_code; - } else { - return $output; - } + my ($class, $args, $plugin) = @_; + my $return_code; + + if ( exists $args->{return_code} + && exists $STATUS_TEXT{$args->{return_code}} + ) { + $return_code = $args->{return_code}; + } + else { + $return_code = $ERRORS{UNKNOWN}; + } + my $message = $args->{message} || "Internal error"; + my $output = join(" ", $STATUS_TEXT{$return_code}, $message); + if ($plugin) { + $output = $plugin->shortname." $output" if $plugin->shortname; + $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; + } + if ($print_on_die) { + print $output, $/; + } + if ($exit_on_die) { + exit $return_code; + } else { + return $output; + } } 1; diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index 45109ae..2af22bd 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm @@ -7,6 +7,9 @@ use warnings; use Carp; use Nagios::Plugin::Threshold; +use Nagios::Plugin; +our ($VERSION) = $Nagios::Plugin::VERSION; + use Class::Struct; struct "Nagios::Plugin::Performance" => { label => '$', diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm index c03001a..c0c47e4 100644 --- a/lib/Nagios/Plugin/Range.pm +++ b/lib/Nagios/Plugin/Range.pm @@ -4,6 +4,10 @@ use 5.008004; use strict; use warnings; +use Carp; + +use Nagios::Plugin; +our ($VERSION) = $Nagios::Plugin::VERSION; use overload '""' => sub { shift->stringify }; @@ -17,8 +21,8 @@ struct "Nagios::Plugin::Range" => { alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set }; -my $outside = 0; -my $inside = 1; +use constant OUTSIDE => 0; +use constant INSIDE => 1; sub stringify { my $self = shift; @@ -49,22 +53,32 @@ sub set_range_end { 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); + 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/^\@?(-?[\d.]+|~)?(:(-?[\d.]+)?)?$/ ) { + carp "invalid range definition '$string'"; + return undef; + } if ($string =~ s/^\@//) { - $range->alert_on($inside); + $range->alert_on(INSIDE); } - if ($string =~ s/^~//) { - $range->start_infinity(1); + + if ($string =~ s/^~//) { # '~:x' + $range->start_infinity(1); } - if (($_) = $string =~ /^([-\d\.]+)?:/) { - $range->set_range_start($_) if defined $_; - $string =~ s/^([-\d\.]+)?://; - $valid++ + if ( $string =~ m/^([\d\.-]+)?:/ ) { # '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/^([-\d\.]+)?://; + $valid++; } - if ($string =~ /^([-\d\.]+)$/) { - $range->set_range_end($string); - $valid++; + if ($string =~ /^([-\d\.]+)$/) { # 'x:10' or '10' + $range->set_range_end($string); + $valid++; } if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { @@ -78,7 +92,7 @@ sub check_range { my ($self, $value) = @_; my $false = 0; my $true = 1; - if ($self->alert_on == $inside) { + if ($self->alert_on == INSIDE) { $false = 1; $true = 0; } @@ -89,7 +103,7 @@ sub check_range { return $true; } } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { - if ($self->start <= $value) { + if ( $value >= $self->start ) { return $false; } else { return $true; diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index 1b332b9..494383f 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm @@ -7,6 +7,8 @@ use warnings; use Nagios::Plugin::Range; use Nagios::Plugin::Base; +use Nagios::Plugin; +our ($VERSION) = $Nagios::Plugin::VERSION; use Class::Struct; struct "Nagios::Plugin::Threshold" => { @@ -44,6 +46,7 @@ sub set_thresholds { sub get_status { my ($self, $value) = @_; + if ($self->critical->is_set) { if ($self->critical->check_range($value) == 1) { return $ERRORS{CRITICAL}; @@ -54,6 +57,7 @@ sub get_status { return $ERRORS{WARNING}; } } + return $ERRORS{OK}; } 1; diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t index b10a988..0f52de1 100644 --- a/t/Nagios-Plugin-Performance.t +++ b/t/Nagios-Plugin-Performance.t @@ -3,6 +3,8 @@ use strict; use Test::More tests => 49; BEGIN { use_ok('Nagios::Plugin::Performance') }; +diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n"; + use Nagios::Plugin::Base; Nagios::Plugin::Base->exit_on_die(0); @@ -27,13 +29,16 @@ cmp_ok( $p[1]->threshold->critical->end, "==", 9448, "crit okay"); ok( ! @p, "Errors correctly"); ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); + @p = Nagios::Plugin::Performance->parse_perfstring( "time=0.001229s;0.000000;0.000000;0.000000;10.000000"); cmp_ok( $p[0]->label, "eq", "time", "label okay"); cmp_ok( $p[0]->value, "==", 0.001229, "value okay"); cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); -cmp_ok( $p[0]->threshold->warning, "eq", "0", "warn okay"); -cmp_ok( $p[0]->threshold->critical, "eq", "0", "crit okay"); + ok( $p[0]->threshold->warning->is_set, "warn okay"); + ok( $p[0]->threshold->critical->is_set, "crit okay"); + + @p = Nagios::Plugin::Performance->parse_perfstring( "load1=0.000;5.000;9.000;0; load5=0.000;5.000;9.000;0; load15=0.000;5.000;9.000;0;"); diff --git a/t/Nagios-Plugin-Range.t b/t/Nagios-Plugin-Range.t index 13667de..f01518d 100644 --- a/t/Nagios-Plugin-Range.t +++ b/t/Nagios-Plugin-Range.t @@ -1,10 +1,32 @@ use strict; -use Test::More tests => 60; +use Test::More qw(no_plan); #tests => 123; + BEGIN { use_ok('Nagios::Plugin::Range') }; +diag "\nusing Nagios::Plugin::Range revision ". $Nagios::Plugin::Range::VERSION . "\n"; + +my $r; + +diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:"; + +foreach (qw( + : + 1:~ + foo + 1-10 + 10:~ + 1-10:2.4 + +), '1,10' # avoid warning about using , inside qw() +) { + $r =Nagios::Plugin::Range->parse_range_string($_); + is $r, undef, "'$_' should not be a valid range" ; +} + -my $r = Nagios::Plugin::Range->parse_range_string("6"); +diag "range: 0..6 inclusive" if $ENV{TEST_VERBOSE}; +$r = Nagios::Plugin::Range->parse_range_string("6"); isa_ok( $r, "Nagios::Plugin::Range"); ok( defined $r, "'6' is valid range"); cmp_ok( $r->start, '==', 0, "Start correct"); @@ -13,6 +35,27 @@ cmp_ok( $r->end, '==', 6, "End correct"); cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); cmp_ok( $r, 'eq', "6", "Stringification back to original"); +my $expected = { + -1 => 1, # 1 means it raises an alert because it's OUTSIDE the range + 0 => 0, # 0 means it's inside the range (no alert) + 4 => 0, + 6 => 0, + 6.1 => 1, + 79.999999 => 1, +}; + +sub test_expected { + my $r = shift; + my $expected = shift; + foreach (sort {$a<=>$b} keys %$expected) { + is $r->check_range($_), $expected->{$_}, + " $_ should " . ($expected->{$_} ? 'not ' : '') . "be in the range (line ".(caller)[2].")"; + } +} + +test_expected( $r, $expected ); + +diag "range : -7..23, inclusive" if $ENV{TEST_VERBOSE}; $r = Nagios::Plugin::Range->parse_range_string("-7:23"); ok( defined $r, "'-7:23' is valid range"); cmp_ok( $r->start, '==', -7, "Start correct"); @@ -21,6 +64,20 @@ cmp_ok( $r->end, '==', 23, "End correct"); cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); cmp_ok( $r, 'eq', "-7:23", "Stringification back to original"); +$expected = { + -23 => 1, + -7 => 0, + -1 => 0, + 0 => 0, + 4 => 0, + 23 => 0, + 23.1 => 1, + 79.999999 => 1, +}; +test_expected( $r, $expected ); + + +diag "range : 0..5.75, inclusive" if $ENV{TEST_VERBOSE}; $r = Nagios::Plugin::Range->parse_range_string(":5.75"); ok( defined $r, "':5.75' is valid range"); cmp_ok( $r->start, '==', 0, "Start correct"); @@ -28,21 +85,81 @@ cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); cmp_ok( $r->end, '==', 5.75, "End correct"); cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); cmp_ok( $r, 'eq', "5.75", "Stringification to simplification"); +$expected = { + -1 => 1, + 0 => 0, + 4 => 0, + 5.75 => 0, + 5.7501 => 1, + 6 => 1, + 6.1 => 1, + 79.999999 => 1, +}; +test_expected( $r, $expected ); + + +diag "range : negative infinity .. -95.99, inclusive" if $ENV{TEST_VERBOSE}; $r = Nagios::Plugin::Range->parse_range_string("~:-95.99"); ok( defined $r, "'~:-95.99' is valid range"); cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); cmp_ok( $r->end, '==', -95.99, "End correct"); cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); cmp_ok( $r, 'eq', "~:-95.99", "Stringification back to original"); +$expected = { + -1001341 => 0, + -96 => 0, + -95.999 => 0, + -95.99 => 0, + -95.989 => 1, + -95 => 1, + 0 => 1, + 5.7501 => 1, + 79.999999 => 1, +}; +test_expected( $r, $expected ); + +diag "range 10..infinity , inclusive" if $ENV{TEST_VERBOSE}; +test_expected( $r, $expected ); +$r = Nagios::Plugin::Range->parse_range_string("10:"); +ok( defined $r, "'10:' is valid range"); +cmp_ok( $r->start, '==', 10, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity"); +cmp_ok( $r, 'eq', "10:", "Stringification back to original"); +$expected = { + -95.999 => 1, + -1 => 1, + 0 => 1, + 9.91 => 1, + 10 => 0, + 11.1 => 0, + 123456789012346 => 0, +}; +test_expected( $r, $expected ); + + +diag "range 123456789012345..infinity , inclusive" if $ENV{TEST_VERBOSE}; +test_expected( $r, $expected ); $r = Nagios::Plugin::Range->parse_range_string("123456789012345:"); ok( defined $r, "'123456789012345:' is valid range"); cmp_ok( $r->start, '==', 123456789012345, "Start correct"); cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity"); cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original"); +$expected = { + -95.999 => 1, + -1 => 1, + 0 => 1, + 123456789012344.91 => 1, + 123456789012345 => 0, + 123456789012346 => 0, +}; +test_expected( $r, $expected ); + +diag "range: <= zero " if $ENV{TEST_VERBOSE}; $r = Nagios::Plugin::Range->parse_range_string("~:0"); ok( defined $r, "'~:0' is valid range"); cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); @@ -53,7 +170,17 @@ cmp_ok( $r, 'eq', "~:0", "Stringification back to original"); ok( $r->check_range(0.5) == 1, "0.5 - alert"); ok( $r->check_range(-10) == 0, "-10 - no alert"); ok( $r->check_range(0) == 0, "0 - no alert"); +$expected = { + -123456789012344.91 => 0, + -1 => 0, + 0 => 0, + .001 => 1, + 123456789012345 => 1, +}; +test_expected( $r, $expected ); + +diag "range: OUTSIDE 0..657.8210567" if $ENV{TEST_VERBOSE}; $r = Nagios::Plugin::Range->parse_range_string('@0:657.8210567'); ok( defined $r, '"@0:657.8210567" is a valid range'); cmp_ok( $r->start, '==', 0, "Start correct"); @@ -66,7 +193,19 @@ ok( $r->check_range(32.88) == 1, "32.88 - alert"); ok( $r->check_range(-2) == 0, "-2 - no alert"); ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert"); ok( $r->check_range(0) == 1, "0 - alert"); +$expected = { + -134151 => 0, + -1 => 0, + 0 => 1, + .001 => 1, + 657.8210567 => 1, + 657.9 => 0, + 123456789012345 => 0, +}; +test_expected( $r, $expected ); + +diag "range: 1..1 inclusive (equals one)" if $ENV{TEST_VERBOSE}; $r = Nagios::Plugin::Range->parse_range_string('1:1'); ok( defined $r, '"1:1" is a valid range'); cmp_ok( $r->start, '==', 1, "Start correct"); @@ -77,6 +216,16 @@ cmp_ok( $r, 'eq', "1:1", "Stringification to simplified version"); ok( $r->check_range(0.5) == 1, "0.5 - alert"); ok( $r->check_range(1) == 0, "1 - no alert"); ok( $r->check_range(5.2) == 1, "5.2 - alert"); +$expected = { + -1 => 1, + 0 => 1, + .5 => 1, + 1 => 0, + 1.001 => 1, + 5.2 => 1, +}; +test_expected( $r, $expected ); + $r = Nagios::Plugin::Range->parse_range_string('2:1'); ok( ! defined $r, '"2:1" is rejected'); diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t index 97d4fcc..bb8b578 100644 --- a/t/Nagios-Plugin-Threshold.t +++ b/t/Nagios-Plugin-Threshold.t @@ -1,32 +1,162 @@ use strict; -use Test::More tests => 18; +use Test::More tests => 71; +#use Test::Exception; # broken for now so we don't need this. BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Base') }; +diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n"; + Nagios::Plugin::Base->exit_on_die(0); Nagios::Plugin::Base->print_on_die(0); +my %STATUS_TEXT = reverse %ERRORS; +diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); ok( defined $t, "Threshold ('', '80') set"); ok( ! $t->warning->is_set, "Warning not set"); -cmp_ok( $t->critical->end, '==', 80, "Critical set correctly"); +cmp_ok( $t->critical->start, '==', 0, "Critical strat set correctly"); +cmp_ok( $t->critical->end, '==', 80, "Critical end set correctly"); +ok ! $t->critical->end_infinity, "not forever"; + +my $expected = { qw( + -1 CRITICAL + 4 OK + 79.999999 OK + 80 OK + 80.1 CRITICAL + 102321 CRITICAL +) }; + +sub test_expected_statuses { + my $t = shift; + my $expected = shift; + my $debug = shift; + foreach (sort {$a<=>$b} keys %$expected) { + is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}"; + if ($debug) { + diag "val = $_; critical check = ".$t->critical->check_range($_). + "; warning check = ".$t->warning->check_range($_); + } + } + use Data::Dumper; + diag "thresh dump: ". Dumper $t if $debug; +} +test_expected_statuses( $t, $expected ); + +diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => ""); ok( defined $t, "Threshold ('5:33', '') set"); cmp_ok( $t->warning->start, '==', 5, "Warning start set"); cmp_ok( $t->warning->end, '==', 33, "Warning end set"); ok( ! $t->critical->is_set, "Critical not set"); -$t = Nagios::Plugin::Threshold->set_thresholds(warning => "30", critical => "60"); -ok( defined $t, "Threshold ('30', '60') set"); +$expected = { qw( + -1 WARNING + 4 WARNING + 4.999999 WARNING + 5 OK + 14.21 OK + 33 OK + 33.01 WARNING + 10231 WARNING +) }; +test_expected_statuses( $t, $expected ); + +diag "threshold: warn if more than 30; critical if > 60" if $ENV{TEST_VERBOSE}; +$t = Nagios::Plugin::Threshold->set_thresholds(warning => "~:30", critical => "~:60"); +ok( defined $t, "Threshold ('~:30', '~:60') set"); cmp_ok( $t->warning->end, '==', 30, "Warning end set"); cmp_ok( $t->critical->end, '==',60, "Critical end set"); -cmp_ok( $t->get_status(15.3), '==', $ERRORS{OK}, "15.3 - ok"); -cmp_ok( $t->get_status(30.0001), '==', $ERRORS{WARNING}, "30.0001 - warning"); -cmp_ok( $t->get_status(69), '==', $ERRORS{CRITICAL}, "69 - critical"); +ok $t->critical->start_infinity, "Critical starts at negative infinity"; -$t = Nagios::Plugin::Threshold->set_thresholds(warning => "total", critical => "rubbish"); -ok( defined $t, "Threshold object created although ..."); +$expected = { qw( + -1 OK + 4 OK + 29.999999 OK + 30 OK + 30.1 WARNING + 50.90 WARNING + 59.9 WARNING + 60 WARNING + 60.00001 CRITICAL + 10231 CRITICAL +) }; +test_expected_statuses( $t, $expected ); + + +# "I'm going to die homeless, penniless, and 30 pounds overweight." +# "...and that's...okay." + +# TODO: figure out why this doesn't work and fix the test. +goto SKIP_DEATH; +diag "threshold: test pure crap for arguments - default to OK." if $ENV{TEST_VERBOSE}; +diag "you should see one invalid range definition warning and an UNKNOWN line here:\n"; +Nagios::Plugin::Base->print_on_die(1); +Nagios::Plugin::Base->exit_on_die(1); + +dies_ok( sub { + $t = Nagios::Plugin::Threshold->set_thresholds( + warning => "total", + critical => "rubbish" + ) + }, "bad thresholds cause death" +); +Nagios::Plugin::Base->print_on_die(0); +Nagios::Plugin::Base->exit_on_die(0); +SKIP_DEATH: + + +diag "threshold: critical if > 25 " if $ENV{TEST_VERBOSE}; +$t = Nagios::Plugin::Threshold->set_thresholds( critical => "~:25" ); +ok( defined $t, "Threshold ('', '~:25') set (".$t->critical->stringify().")" ); ok( ! $t->warning->is_set, "Warning not set"); -ok( ! $t->critical->is_set, "Critical not set"); +cmp_ok( $t->critical->end, '==',25, "Critical end set"); +ok $t->critical->start_infinity, "Critical starts at negative infinity"; + +$expected = { qw( + -1 OK + 4 OK + 10 OK + 14.21 OK + 25 OK + 25.01 CRITICAL + 31001 CRITICAL +) }; +test_expected_statuses( $t, $expected); + +diag "threshold: warn if OUTSIDE {10..25} , critical if > 25 " if $ENV{TEST_VERBOSE}; +$t = Nagios::Plugin::Threshold->set_thresholds(warning => "10:25", critical => "~:25"); +ok( defined $t, "Threshold ('10:25', '~:25') set"); +cmp_ok( $t->warning->start, '==', 10, "Warning start set"); +cmp_ok( $t->warning->end, '==', 25, "Warning end set"); +cmp_ok( $t->critical->end, '==', 25, "Critical end set"); + +$expected = { qw( + -1 WARNING + 4 WARNING + 9.999999 WARNING + 10 OK + 14.21 OK + 25 OK + 25.01 CRITICAL + 31001 CRITICAL +) }; +test_expected_statuses( $t, $expected ); + + +diag "warn if INSIDE {10..25} , critical if < 10 " if $ENV{TEST_VERBOSE}; +$t = Nagios::Plugin::Threshold->set_thresholds(warning => "\@10:25", critical => "10:"); +$expected = { qw( + -1 CRITICAL + 4 CRITICAL + 9.999999 CRITICAL + 10 WARNING + 14.21 WARNING + 25 WARNING + 25.01 OK + 31001 OK +) }; +test_expected_statuses( $t, $expected ); +ok 1, "sweet, made it to the end."; diff --git a/t/Nagios-Plugin.t b/t/Nagios-Plugin.t index 38e792d..ed25aca 100644 --- a/t/Nagios-Plugin.t +++ b/t/Nagios-Plugin.t @@ -1,18 +1,22 @@ use strict; -use Test::More tests => 5; +use Test::More tests => 9; + BEGIN { use_ok('Nagios::Plugin') }; use Nagios::Plugin::Base; Nagios::Plugin::Base->exit_on_die(0); Nagios::Plugin::Base->print_on_die(0); +diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"; + my $p = Nagios::Plugin->new; isa_ok( $p, "Nagios::Plugin"); $p->shortname("PAGESIZE"); -my $t = $p->set_thresholds( warning => "10:25", critical => "25:" ); +diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE}; +my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" ); $p->add_perfdata( label => "size", @@ -21,12 +25,20 @@ $p->add_perfdata( threshold => $t, ); -cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;25:", "Perfdata correct"); - -my $o = $p->die( return_code => $t->get_status(1), message => "page size at http://... was 1kB" ); -cmp_ok( $o, "eq", 'PAGESIZE CRITICAL page size at http://... was 1kB | size=1kB;10:25;25:', "Output okay"); - -cmp_ok( $p->die( return_code => $t->get_status(30), message => "page size at http://... was 30kB" ), - "eq", 'PAGESIZE WARNING page size at http://... was 30kB | size=1kB;10:25;25:', "Output okay"); - +cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct"); + +my $expected = {qw( + -1 WARNING + 1 WARNING + 20 OK + 25 OK + 26 CRITICAL + 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->{$_}" ; +} -- cgit v0.10-9-g596f