diff options
-rw-r--r-- | Changes | 3 | ||||
-rw-r--r-- | lib/Nagios/Plugin.pm | 9 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Base.pm | 45 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Performance.pm | 3 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Range.pm | 44 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Threshold.pm | 4 | ||||
-rw-r--r-- | t/Nagios-Plugin-Performance.t | 9 | ||||
-rw-r--r-- | t/Nagios-Plugin-Range.t | 153 | ||||
-rw-r--r-- | t/Nagios-Plugin-Threshold.t | 150 | ||||
-rw-r--r-- | t/Nagios-Plugin.t | 32 |
10 files changed, 394 insertions, 58 deletions
@@ -1,5 +1,8 @@ | |||
1 | Revision history for Perl module Nagios::Plugin. | 1 | Revision history for Perl module Nagios::Plugin. |
2 | 2 | ||
3 | 0.13 ??? | ||
4 | - Lots of extra tests and fixes from Nathan Vonnahme | ||
5 | |||
3 | 0.12 15th June 2006 | 6 | 0.12 15th June 2006 |
4 | - rrdlabel method available to get a performance label, | 7 | - rrdlabel method available to get a performance label, |
5 | converted to something rrd can use | 8 | 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; | |||
23 | our @ISA = qw(Exporter Nagios::__::Plugin); | 23 | our @ISA = qw(Exporter Nagios::__::Plugin); |
24 | our @EXPORT_OK = qw(%ERRORS); | 24 | our @EXPORT_OK = qw(%ERRORS); |
25 | 25 | ||
26 | our $VERSION = '0.12'; | 26 | our $VERSION = '0.13'; |
27 | 27 | ||
28 | sub add_perfdata { | 28 | sub add_perfdata { |
29 | my ($self, %args) = @_; | 29 | my ($self, %args) = @_; |
@@ -59,7 +59,8 @@ Nagios::Plugin - Object oriented helper routines for your Nagios plugin | |||
59 | use Nagios::Plugin qw(%ERRORS); | 59 | use Nagios::Plugin qw(%ERRORS); |
60 | $p = Nagios::Plugin->new( shortname => "PAGESIZE" ); | 60 | $p = Nagios::Plugin->new( shortname => "PAGESIZE" ); |
61 | 61 | ||
62 | $threshold = $p->set_thresholds( warning => "10:25", critical => "25:" ); | 62 | $threshold = $p->set_thresholds( warning => "10:25", critical => "~:25" ); |
63 | # Critical if outside -INF to 25, ie > 25. Warn if outside 10-25, ie < 10 | ||
63 | 64 | ||
64 | # ... collect current metric into $value | 65 | # ... collect current metric into $value |
65 | if ($trouble_getting_metric) { | 66 | if ($trouble_getting_metric) { |
@@ -120,7 +121,7 @@ Initializes a new Nagios::Plugin object. Can specify the shortname here. | |||
120 | 121 | ||
121 | =head1 OBJECT METHODS | 122 | =head1 OBJECT METHODS |
122 | 123 | ||
123 | =item set_thresholds( warning => "10:25", critical => "25:" ) | 124 | =item set_thresholds( warning => "10:25", critical => "~:25" ) |
124 | 125 | ||
125 | Sets the thresholds, based on the range specification at | 126 | Sets the thresholds, based on the range specification at |
126 | http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. | 127 | http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. |
@@ -148,6 +149,8 @@ http://nagiosplug.sourceforge.net | |||
148 | 149 | ||
149 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | 150 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> |
150 | 151 | ||
152 | Thanks to Nathan Vonnahme for loads of extra tests and subsequent fixes. | ||
153 | |||
151 | =head1 COPYRIGHT AND LICENSE | 154 | =head1 COPYRIGHT AND LICENSE |
152 | 155 | ||
153 | Copyright (C) 2006 by Nagios Plugin Development Team | 156 | 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; | |||
5 | use strict; | 5 | use strict; |
6 | use warnings; | 6 | use warnings; |
7 | 7 | ||
8 | use Nagios::Plugin; | ||
9 | our ($VERSION) = $Nagios::Plugin::VERSION; | ||
10 | |||
8 | use Exporter; | 11 | use Exporter; |
9 | our @ISA = qw(Exporter); | 12 | our @ISA = qw(Exporter); |
10 | our @EXPORT = qw(%ERRORS); | 13 | our @EXPORT = qw(%ERRORS); |
@@ -13,28 +16,38 @@ our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); | |||
13 | 16 | ||
14 | our %STATUS_TEXT = reverse %ERRORS; | 17 | our %STATUS_TEXT = reverse %ERRORS; |
15 | 18 | ||
19 | |||
16 | my $exit_on_die = 1; | 20 | my $exit_on_die = 1; |
17 | sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; | 21 | sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; |
18 | my $print_on_die = 1; | 22 | my $print_on_die = 1; |
19 | sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; | 23 | sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; |
20 | 24 | ||
21 | sub die { | 25 | sub die { |
22 | my ($class, $args, $plugin) = @_; | 26 | my ($class, $args, $plugin) = @_; |
23 | my $return_code = $args->{return_code} || 3; | 27 | my $return_code; |
24 | my $message = $args->{message} || "Internal error"; | 28 | |
25 | my $output = join(" ", $STATUS_TEXT{$return_code}, $message); | 29 | if ( exists $args->{return_code} |
26 | if ($plugin) { | 30 | && exists $STATUS_TEXT{$args->{return_code}} |
27 | $output = $plugin->shortname." $output" if $plugin->shortname; | 31 | ) { |
28 | $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; | 32 | $return_code = $args->{return_code}; |
29 | } | 33 | } |
30 | if ($print_on_die) { | 34 | else { |
31 | print $output, $/; | 35 | $return_code = $ERRORS{UNKNOWN}; |
32 | } | 36 | } |
33 | if ($exit_on_die) { | 37 | my $message = $args->{message} || "Internal error"; |
34 | exit $return_code; | 38 | my $output = join(" ", $STATUS_TEXT{$return_code}, $message); |
35 | } else { | 39 | if ($plugin) { |
36 | return $output; | 40 | $output = $plugin->shortname." $output" if $plugin->shortname; |
37 | } | 41 | $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; |
42 | } | ||
43 | if ($print_on_die) { | ||
44 | print $output, $/; | ||
45 | } | ||
46 | if ($exit_on_die) { | ||
47 | exit $return_code; | ||
48 | } else { | ||
49 | return $output; | ||
50 | } | ||
38 | } | 51 | } |
39 | 52 | ||
40 | 1; | 53 | 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; | |||
7 | 7 | ||
8 | use Carp; | 8 | use Carp; |
9 | use Nagios::Plugin::Threshold; | 9 | use Nagios::Plugin::Threshold; |
10 | use Nagios::Plugin; | ||
11 | our ($VERSION) = $Nagios::Plugin::VERSION; | ||
12 | |||
10 | use Class::Struct; | 13 | use Class::Struct; |
11 | struct "Nagios::Plugin::Performance" => { | 14 | struct "Nagios::Plugin::Performance" => { |
12 | label => '$', | 15 | 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; | |||
4 | 4 | ||
5 | use strict; | 5 | use strict; |
6 | use warnings; | 6 | use warnings; |
7 | use Carp; | ||
8 | |||
9 | use Nagios::Plugin; | ||
10 | our ($VERSION) = $Nagios::Plugin::VERSION; | ||
7 | 11 | ||
8 | use overload | 12 | use overload |
9 | '""' => sub { shift->stringify }; | 13 | '""' => sub { shift->stringify }; |
@@ -17,8 +21,8 @@ struct "Nagios::Plugin::Range" => { | |||
17 | alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set | 21 | alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set |
18 | }; | 22 | }; |
19 | 23 | ||
20 | my $outside = 0; | 24 | use constant OUTSIDE => 0; |
21 | my $inside = 1; | 25 | use constant INSIDE => 1; |
22 | 26 | ||
23 | sub stringify { | 27 | sub stringify { |
24 | my $self = shift; | 28 | my $self = shift; |
@@ -49,22 +53,32 @@ sub set_range_end { | |||
49 | sub parse_range_string { | 53 | sub parse_range_string { |
50 | my ($class, $string) = @_; | 54 | my ($class, $string) = @_; |
51 | my $valid = 0; | 55 | my $valid = 0; |
52 | my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => $outside); | 56 | my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => OUTSIDE); |
57 | |||
58 | $string =~ s/\s//g; # strip out any whitespace | ||
59 | # check for valid range definition | ||
60 | unless ( $string =~ /[\d~]/ && $string =~ m/^\@?(-?[\d.]+|~)?(:(-?[\d.]+)?)?$/ ) { | ||
61 | carp "invalid range definition '$string'"; | ||
62 | return undef; | ||
63 | } | ||
53 | 64 | ||
54 | if ($string =~ s/^\@//) { | 65 | if ($string =~ s/^\@//) { |
55 | $range->alert_on($inside); | 66 | $range->alert_on(INSIDE); |
56 | } | 67 | } |
57 | if ($string =~ s/^~//) { | 68 | |
58 | $range->start_infinity(1); | 69 | if ($string =~ s/^~//) { # '~:x' |
70 | $range->start_infinity(1); | ||
59 | } | 71 | } |
60 | if (($_) = $string =~ /^([-\d\.]+)?:/) { | 72 | if ( $string =~ m/^([\d\.-]+)?:/ ) { # '10:' |
61 | $range->set_range_start($_) if defined $_; | 73 | my $start = $1; |
62 | $string =~ s/^([-\d\.]+)?://; | 74 | $range->set_range_start($start) if defined $start; |
63 | $valid++ | 75 | $range->end_infinity(1); # overridden below if there's an end specified |
76 | $string =~ s/^([-\d\.]+)?://; | ||
77 | $valid++; | ||
64 | } | 78 | } |
65 | if ($string =~ /^([-\d\.]+)$/) { | 79 | if ($string =~ /^([-\d\.]+)$/) { # 'x:10' or '10' |
66 | $range->set_range_end($string); | 80 | $range->set_range_end($string); |
67 | $valid++; | 81 | $valid++; |
68 | } | 82 | } |
69 | 83 | ||
70 | if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { | 84 | if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { |
@@ -78,7 +92,7 @@ sub check_range { | |||
78 | my ($self, $value) = @_; | 92 | my ($self, $value) = @_; |
79 | my $false = 0; | 93 | my $false = 0; |
80 | my $true = 1; | 94 | my $true = 1; |
81 | if ($self->alert_on == $inside) { | 95 | if ($self->alert_on == INSIDE) { |
82 | $false = 1; | 96 | $false = 1; |
83 | $true = 0; | 97 | $true = 0; |
84 | } | 98 | } |
@@ -89,7 +103,7 @@ sub check_range { | |||
89 | return $true; | 103 | return $true; |
90 | } | 104 | } |
91 | } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { | 105 | } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { |
92 | if ($self->start <= $value) { | 106 | if ( $value >= $self->start ) { |
93 | return $false; | 107 | return $false; |
94 | } else { | 108 | } else { |
95 | return $true; | 109 | 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; | |||
7 | 7 | ||
8 | use Nagios::Plugin::Range; | 8 | use Nagios::Plugin::Range; |
9 | use Nagios::Plugin::Base; | 9 | use Nagios::Plugin::Base; |
10 | use Nagios::Plugin; | ||
11 | our ($VERSION) = $Nagios::Plugin::VERSION; | ||
10 | 12 | ||
11 | use Class::Struct; | 13 | use Class::Struct; |
12 | struct "Nagios::Plugin::Threshold" => { | 14 | struct "Nagios::Plugin::Threshold" => { |
@@ -44,6 +46,7 @@ sub set_thresholds { | |||
44 | 46 | ||
45 | sub get_status { | 47 | sub get_status { |
46 | my ($self, $value) = @_; | 48 | my ($self, $value) = @_; |
49 | |||
47 | if ($self->critical->is_set) { | 50 | if ($self->critical->is_set) { |
48 | if ($self->critical->check_range($value) == 1) { | 51 | if ($self->critical->check_range($value) == 1) { |
49 | return $ERRORS{CRITICAL}; | 52 | return $ERRORS{CRITICAL}; |
@@ -54,6 +57,7 @@ sub get_status { | |||
54 | return $ERRORS{WARNING}; | 57 | return $ERRORS{WARNING}; |
55 | } | 58 | } |
56 | } | 59 | } |
60 | return $ERRORS{OK}; | ||
57 | } | 61 | } |
58 | 62 | ||
59 | 1; | 63 | 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; | |||
3 | use Test::More tests => 49; | 3 | use Test::More tests => 49; |
4 | BEGIN { use_ok('Nagios::Plugin::Performance') }; | 4 | BEGIN { use_ok('Nagios::Plugin::Performance') }; |
5 | 5 | ||
6 | diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n"; | ||
7 | |||
6 | use Nagios::Plugin::Base; | 8 | use Nagios::Plugin::Base; |
7 | Nagios::Plugin::Base->exit_on_die(0); | 9 | Nagios::Plugin::Base->exit_on_die(0); |
8 | 10 | ||
@@ -27,13 +29,16 @@ cmp_ok( $p[1]->threshold->critical->end, "==", 9448, "crit okay"); | |||
27 | ok( ! @p, "Errors correctly"); | 29 | ok( ! @p, "Errors correctly"); |
28 | ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); | 30 | ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); |
29 | 31 | ||
32 | |||
30 | @p = Nagios::Plugin::Performance->parse_perfstring( | 33 | @p = Nagios::Plugin::Performance->parse_perfstring( |
31 | "time=0.001229s;0.000000;0.000000;0.000000;10.000000"); | 34 | "time=0.001229s;0.000000;0.000000;0.000000;10.000000"); |
32 | cmp_ok( $p[0]->label, "eq", "time", "label okay"); | 35 | cmp_ok( $p[0]->label, "eq", "time", "label okay"); |
33 | cmp_ok( $p[0]->value, "==", 0.001229, "value okay"); | 36 | cmp_ok( $p[0]->value, "==", 0.001229, "value okay"); |
34 | cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); | 37 | cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); |
35 | cmp_ok( $p[0]->threshold->warning, "eq", "0", "warn okay"); | 38 | ok( $p[0]->threshold->warning->is_set, "warn okay"); |
36 | cmp_ok( $p[0]->threshold->critical, "eq", "0", "crit okay"); | 39 | ok( $p[0]->threshold->critical->is_set, "crit okay"); |
40 | |||
41 | |||
37 | 42 | ||
38 | @p = Nagios::Plugin::Performance->parse_perfstring( | 43 | @p = Nagios::Plugin::Performance->parse_perfstring( |
39 | "load1=0.000;5.000;9.000;0; load5=0.000;5.000;9.000;0; load15=0.000;5.000;9.000;0;"); | 44 | "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 @@ | |||
1 | 1 | ||
2 | use strict; | 2 | use strict; |
3 | use Test::More tests => 60; | 3 | use Test::More qw(no_plan); #tests => 123; |
4 | |||
4 | BEGIN { use_ok('Nagios::Plugin::Range') }; | 5 | BEGIN { use_ok('Nagios::Plugin::Range') }; |
5 | 6 | ||
7 | diag "\nusing Nagios::Plugin::Range revision ". $Nagios::Plugin::Range::VERSION . "\n"; | ||
8 | |||
9 | my $r; | ||
10 | |||
11 | diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:"; | ||
12 | |||
13 | foreach (qw( | ||
14 | : | ||
15 | 1:~ | ||
16 | foo | ||
17 | 1-10 | ||
18 | 10:~ | ||
19 | 1-10:2.4 | ||
20 | |||
21 | ), '1,10' # avoid warning about using , inside qw() | ||
22 | ) { | ||
23 | $r =Nagios::Plugin::Range->parse_range_string($_); | ||
24 | is $r, undef, "'$_' should not be a valid range" ; | ||
25 | } | ||
26 | |||
6 | 27 | ||
7 | my $r = Nagios::Plugin::Range->parse_range_string("6"); | 28 | diag "range: 0..6 inclusive" if $ENV{TEST_VERBOSE}; |
29 | $r = Nagios::Plugin::Range->parse_range_string("6"); | ||
8 | isa_ok( $r, "Nagios::Plugin::Range"); | 30 | isa_ok( $r, "Nagios::Plugin::Range"); |
9 | ok( defined $r, "'6' is valid range"); | 31 | ok( defined $r, "'6' is valid range"); |
10 | cmp_ok( $r->start, '==', 0, "Start correct"); | 32 | cmp_ok( $r->start, '==', 0, "Start correct"); |
@@ -13,6 +35,27 @@ cmp_ok( $r->end, '==', 6, "End correct"); | |||
13 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | 35 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); |
14 | cmp_ok( $r, 'eq', "6", "Stringification back to original"); | 36 | cmp_ok( $r, 'eq', "6", "Stringification back to original"); |
15 | 37 | ||
38 | my $expected = { | ||
39 | -1 => 1, # 1 means it raises an alert because it's OUTSIDE the range | ||
40 | 0 => 0, # 0 means it's inside the range (no alert) | ||
41 | 4 => 0, | ||
42 | 6 => 0, | ||
43 | 6.1 => 1, | ||
44 | 79.999999 => 1, | ||
45 | }; | ||
46 | |||
47 | sub test_expected { | ||
48 | my $r = shift; | ||
49 | my $expected = shift; | ||
50 | foreach (sort {$a<=>$b} keys %$expected) { | ||
51 | is $r->check_range($_), $expected->{$_}, | ||
52 | " $_ should " . ($expected->{$_} ? 'not ' : '') . "be in the range (line ".(caller)[2].")"; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | test_expected( $r, $expected ); | ||
57 | |||
58 | diag "range : -7..23, inclusive" if $ENV{TEST_VERBOSE}; | ||
16 | $r = Nagios::Plugin::Range->parse_range_string("-7:23"); | 59 | $r = Nagios::Plugin::Range->parse_range_string("-7:23"); |
17 | ok( defined $r, "'-7:23' is valid range"); | 60 | ok( defined $r, "'-7:23' is valid range"); |
18 | cmp_ok( $r->start, '==', -7, "Start correct"); | 61 | cmp_ok( $r->start, '==', -7, "Start correct"); |
@@ -21,6 +64,20 @@ cmp_ok( $r->end, '==', 23, "End correct"); | |||
21 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | 64 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); |
22 | cmp_ok( $r, 'eq', "-7:23", "Stringification back to original"); | 65 | cmp_ok( $r, 'eq', "-7:23", "Stringification back to original"); |
23 | 66 | ||
67 | $expected = { | ||
68 | -23 => 1, | ||
69 | -7 => 0, | ||
70 | -1 => 0, | ||
71 | 0 => 0, | ||
72 | 4 => 0, | ||
73 | 23 => 0, | ||
74 | 23.1 => 1, | ||
75 | 79.999999 => 1, | ||
76 | }; | ||
77 | test_expected( $r, $expected ); | ||
78 | |||
79 | |||
80 | diag "range : 0..5.75, inclusive" if $ENV{TEST_VERBOSE}; | ||
24 | $r = Nagios::Plugin::Range->parse_range_string(":5.75"); | 81 | $r = Nagios::Plugin::Range->parse_range_string(":5.75"); |
25 | ok( defined $r, "':5.75' is valid range"); | 82 | ok( defined $r, "':5.75' is valid range"); |
26 | cmp_ok( $r->start, '==', 0, "Start correct"); | 83 | cmp_ok( $r->start, '==', 0, "Start correct"); |
@@ -28,21 +85,81 @@ cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | |||
28 | cmp_ok( $r->end, '==', 5.75, "End correct"); | 85 | cmp_ok( $r->end, '==', 5.75, "End correct"); |
29 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | 86 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); |
30 | cmp_ok( $r, 'eq', "5.75", "Stringification to simplification"); | 87 | cmp_ok( $r, 'eq', "5.75", "Stringification to simplification"); |
88 | $expected = { | ||
89 | -1 => 1, | ||
90 | 0 => 0, | ||
91 | 4 => 0, | ||
92 | 5.75 => 0, | ||
93 | 5.7501 => 1, | ||
94 | 6 => 1, | ||
95 | 6.1 => 1, | ||
96 | 79.999999 => 1, | ||
97 | }; | ||
98 | test_expected( $r, $expected ); | ||
31 | 99 | ||
100 | |||
101 | |||
102 | diag "range : negative infinity .. -95.99, inclusive" if $ENV{TEST_VERBOSE}; | ||
32 | $r = Nagios::Plugin::Range->parse_range_string("~:-95.99"); | 103 | $r = Nagios::Plugin::Range->parse_range_string("~:-95.99"); |
33 | ok( defined $r, "'~:-95.99' is valid range"); | 104 | ok( defined $r, "'~:-95.99' is valid range"); |
34 | cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); | 105 | cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); |
35 | cmp_ok( $r->end, '==', -95.99, "End correct"); | 106 | cmp_ok( $r->end, '==', -95.99, "End correct"); |
36 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | 107 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); |
37 | cmp_ok( $r, 'eq', "~:-95.99", "Stringification back to original"); | 108 | cmp_ok( $r, 'eq', "~:-95.99", "Stringification back to original"); |
109 | $expected = { | ||
110 | -1001341 => 0, | ||
111 | -96 => 0, | ||
112 | -95.999 => 0, | ||
113 | -95.99 => 0, | ||
114 | -95.989 => 1, | ||
115 | -95 => 1, | ||
116 | 0 => 1, | ||
117 | 5.7501 => 1, | ||
118 | 79.999999 => 1, | ||
119 | }; | ||
120 | test_expected( $r, $expected ); | ||
121 | |||
122 | diag "range 10..infinity , inclusive" if $ENV{TEST_VERBOSE}; | ||
123 | test_expected( $r, $expected ); | ||
124 | $r = Nagios::Plugin::Range->parse_range_string("10:"); | ||
125 | ok( defined $r, "'10:' is valid range"); | ||
126 | cmp_ok( $r->start, '==', 10, "Start correct"); | ||
127 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | ||
128 | cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity"); | ||
129 | cmp_ok( $r, 'eq', "10:", "Stringification back to original"); | ||
130 | $expected = { | ||
131 | -95.999 => 1, | ||
132 | -1 => 1, | ||
133 | 0 => 1, | ||
134 | 9.91 => 1, | ||
135 | 10 => 0, | ||
136 | 11.1 => 0, | ||
137 | 123456789012346 => 0, | ||
138 | }; | ||
139 | test_expected( $r, $expected ); | ||
140 | |||
38 | 141 | ||
142 | |||
143 | diag "range 123456789012345..infinity , inclusive" if $ENV{TEST_VERBOSE}; | ||
144 | test_expected( $r, $expected ); | ||
39 | $r = Nagios::Plugin::Range->parse_range_string("123456789012345:"); | 145 | $r = Nagios::Plugin::Range->parse_range_string("123456789012345:"); |
40 | ok( defined $r, "'123456789012345:' is valid range"); | 146 | ok( defined $r, "'123456789012345:' is valid range"); |
41 | cmp_ok( $r->start, '==', 123456789012345, "Start correct"); | 147 | cmp_ok( $r->start, '==', 123456789012345, "Start correct"); |
42 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | 148 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); |
43 | cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity"); | 149 | cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity"); |
44 | cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original"); | 150 | cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original"); |
151 | $expected = { | ||
152 | -95.999 => 1, | ||
153 | -1 => 1, | ||
154 | 0 => 1, | ||
155 | 123456789012344.91 => 1, | ||
156 | 123456789012345 => 0, | ||
157 | 123456789012346 => 0, | ||
158 | }; | ||
159 | test_expected( $r, $expected ); | ||
160 | |||
45 | 161 | ||
162 | diag "range: <= zero " if $ENV{TEST_VERBOSE}; | ||
46 | $r = Nagios::Plugin::Range->parse_range_string("~:0"); | 163 | $r = Nagios::Plugin::Range->parse_range_string("~:0"); |
47 | ok( defined $r, "'~:0' is valid range"); | 164 | ok( defined $r, "'~:0' is valid range"); |
48 | cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); | 165 | cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); |
@@ -53,7 +170,17 @@ cmp_ok( $r, 'eq', "~:0", "Stringification back to original"); | |||
53 | ok( $r->check_range(0.5) == 1, "0.5 - alert"); | 170 | ok( $r->check_range(0.5) == 1, "0.5 - alert"); |
54 | ok( $r->check_range(-10) == 0, "-10 - no alert"); | 171 | ok( $r->check_range(-10) == 0, "-10 - no alert"); |
55 | ok( $r->check_range(0) == 0, "0 - no alert"); | 172 | ok( $r->check_range(0) == 0, "0 - no alert"); |
173 | $expected = { | ||
174 | -123456789012344.91 => 0, | ||
175 | -1 => 0, | ||
176 | 0 => 0, | ||
177 | .001 => 1, | ||
178 | 123456789012345 => 1, | ||
179 | }; | ||
180 | test_expected( $r, $expected ); | ||
181 | |||
56 | 182 | ||
183 | diag "range: OUTSIDE 0..657.8210567" if $ENV{TEST_VERBOSE}; | ||
57 | $r = Nagios::Plugin::Range->parse_range_string('@0:657.8210567'); | 184 | $r = Nagios::Plugin::Range->parse_range_string('@0:657.8210567'); |
58 | ok( defined $r, '"@0:657.8210567" is a valid range'); | 185 | ok( defined $r, '"@0:657.8210567" is a valid range'); |
59 | cmp_ok( $r->start, '==', 0, "Start correct"); | 186 | cmp_ok( $r->start, '==', 0, "Start correct"); |
@@ -66,7 +193,19 @@ ok( $r->check_range(32.88) == 1, "32.88 - alert"); | |||
66 | ok( $r->check_range(-2) == 0, "-2 - no alert"); | 193 | ok( $r->check_range(-2) == 0, "-2 - no alert"); |
67 | ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert"); | 194 | ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert"); |
68 | ok( $r->check_range(0) == 1, "0 - alert"); | 195 | ok( $r->check_range(0) == 1, "0 - alert"); |
196 | $expected = { | ||
197 | -134151 => 0, | ||
198 | -1 => 0, | ||
199 | 0 => 1, | ||
200 | .001 => 1, | ||
201 | 657.8210567 => 1, | ||
202 | 657.9 => 0, | ||
203 | 123456789012345 => 0, | ||
204 | }; | ||
205 | test_expected( $r, $expected ); | ||
69 | 206 | ||
207 | |||
208 | diag "range: 1..1 inclusive (equals one)" if $ENV{TEST_VERBOSE}; | ||
70 | $r = Nagios::Plugin::Range->parse_range_string('1:1'); | 209 | $r = Nagios::Plugin::Range->parse_range_string('1:1'); |
71 | ok( defined $r, '"1:1" is a valid range'); | 210 | ok( defined $r, '"1:1" is a valid range'); |
72 | cmp_ok( $r->start, '==', 1, "Start correct"); | 211 | cmp_ok( $r->start, '==', 1, "Start correct"); |
@@ -77,6 +216,16 @@ cmp_ok( $r, 'eq', "1:1", "Stringification to simplified version"); | |||
77 | ok( $r->check_range(0.5) == 1, "0.5 - alert"); | 216 | ok( $r->check_range(0.5) == 1, "0.5 - alert"); |
78 | ok( $r->check_range(1) == 0, "1 - no alert"); | 217 | ok( $r->check_range(1) == 0, "1 - no alert"); |
79 | ok( $r->check_range(5.2) == 1, "5.2 - alert"); | 218 | ok( $r->check_range(5.2) == 1, "5.2 - alert"); |
219 | $expected = { | ||
220 | -1 => 1, | ||
221 | 0 => 1, | ||
222 | .5 => 1, | ||
223 | 1 => 0, | ||
224 | 1.001 => 1, | ||
225 | 5.2 => 1, | ||
226 | }; | ||
227 | test_expected( $r, $expected ); | ||
228 | |||
80 | 229 | ||
81 | $r = Nagios::Plugin::Range->parse_range_string('2:1'); | 230 | $r = Nagios::Plugin::Range->parse_range_string('2:1'); |
82 | ok( ! defined $r, '"2:1" is rejected'); | 231 | 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 @@ | |||
1 | 1 | ||
2 | use strict; | 2 | use strict; |
3 | use Test::More tests => 18; | 3 | use Test::More tests => 71; |
4 | #use Test::Exception; # broken for now so we don't need this. | ||
4 | BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Base') }; | 5 | BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Base') }; |
5 | 6 | ||
7 | diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n"; | ||
8 | |||
6 | Nagios::Plugin::Base->exit_on_die(0); | 9 | Nagios::Plugin::Base->exit_on_die(0); |
7 | Nagios::Plugin::Base->print_on_die(0); | 10 | Nagios::Plugin::Base->print_on_die(0); |
11 | my %STATUS_TEXT = reverse %ERRORS; | ||
8 | 12 | ||
13 | diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; | ||
9 | my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); | 14 | my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); |
10 | ok( defined $t, "Threshold ('', '80') set"); | 15 | ok( defined $t, "Threshold ('', '80') set"); |
11 | ok( ! $t->warning->is_set, "Warning not set"); | 16 | ok( ! $t->warning->is_set, "Warning not set"); |
12 | cmp_ok( $t->critical->end, '==', 80, "Critical set correctly"); | 17 | cmp_ok( $t->critical->start, '==', 0, "Critical strat set correctly"); |
18 | cmp_ok( $t->critical->end, '==', 80, "Critical end set correctly"); | ||
19 | ok ! $t->critical->end_infinity, "not forever"; | ||
20 | |||
21 | my $expected = { qw( | ||
22 | -1 CRITICAL | ||
23 | 4 OK | ||
24 | 79.999999 OK | ||
25 | 80 OK | ||
26 | 80.1 CRITICAL | ||
27 | 102321 CRITICAL | ||
28 | ) }; | ||
29 | |||
30 | sub test_expected_statuses { | ||
31 | my $t = shift; | ||
32 | my $expected = shift; | ||
33 | my $debug = shift; | ||
13 | 34 | ||
35 | foreach (sort {$a<=>$b} keys %$expected) { | ||
36 | is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}"; | ||
37 | if ($debug) { | ||
38 | diag "val = $_; critical check = ".$t->critical->check_range($_). | ||
39 | "; warning check = ".$t->warning->check_range($_); | ||
40 | } | ||
41 | } | ||
42 | use Data::Dumper; | ||
43 | diag "thresh dump: ". Dumper $t if $debug; | ||
44 | } | ||
45 | test_expected_statuses( $t, $expected ); | ||
46 | |||
47 | diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; | ||
14 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => ""); | 48 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => ""); |
15 | ok( defined $t, "Threshold ('5:33', '') set"); | 49 | ok( defined $t, "Threshold ('5:33', '') set"); |
16 | cmp_ok( $t->warning->start, '==', 5, "Warning start set"); | 50 | cmp_ok( $t->warning->start, '==', 5, "Warning start set"); |
17 | cmp_ok( $t->warning->end, '==', 33, "Warning end set"); | 51 | cmp_ok( $t->warning->end, '==', 33, "Warning end set"); |
18 | ok( ! $t->critical->is_set, "Critical not set"); | 52 | ok( ! $t->critical->is_set, "Critical not set"); |
19 | 53 | ||
20 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "30", critical => "60"); | 54 | $expected = { qw( |
21 | ok( defined $t, "Threshold ('30', '60') set"); | 55 | -1 WARNING |
56 | 4 WARNING | ||
57 | 4.999999 WARNING | ||
58 | 5 OK | ||
59 | 14.21 OK | ||
60 | 33 OK | ||
61 | 33.01 WARNING | ||
62 | 10231 WARNING | ||
63 | ) }; | ||
64 | test_expected_statuses( $t, $expected ); | ||
65 | |||
66 | diag "threshold: warn if more than 30; critical if > 60" if $ENV{TEST_VERBOSE}; | ||
67 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "~:30", critical => "~:60"); | ||
68 | ok( defined $t, "Threshold ('~:30', '~:60') set"); | ||
22 | cmp_ok( $t->warning->end, '==', 30, "Warning end set"); | 69 | cmp_ok( $t->warning->end, '==', 30, "Warning end set"); |
23 | cmp_ok( $t->critical->end, '==',60, "Critical end set"); | 70 | cmp_ok( $t->critical->end, '==',60, "Critical end set"); |
24 | cmp_ok( $t->get_status(15.3), '==', $ERRORS{OK}, "15.3 - ok"); | 71 | ok $t->critical->start_infinity, "Critical starts at negative infinity"; |
25 | cmp_ok( $t->get_status(30.0001), '==', $ERRORS{WARNING}, "30.0001 - warning"); | ||
26 | cmp_ok( $t->get_status(69), '==', $ERRORS{CRITICAL}, "69 - critical"); | ||
27 | 72 | ||
28 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "total", critical => "rubbish"); | 73 | $expected = { qw( |
29 | ok( defined $t, "Threshold object created although ..."); | 74 | -1 OK |
75 | 4 OK | ||
76 | 29.999999 OK | ||
77 | 30 OK | ||
78 | 30.1 WARNING | ||
79 | 50.90 WARNING | ||
80 | 59.9 WARNING | ||
81 | 60 WARNING | ||
82 | 60.00001 CRITICAL | ||
83 | 10231 CRITICAL | ||
84 | ) }; | ||
85 | test_expected_statuses( $t, $expected ); | ||
86 | |||
87 | |||
88 | # "I'm going to die homeless, penniless, and 30 pounds overweight." | ||
89 | # "...and that's...okay." | ||
90 | |||
91 | # TODO: figure out why this doesn't work and fix the test. | ||
92 | goto SKIP_DEATH; | ||
93 | diag "threshold: test pure crap for arguments - default to OK." if $ENV{TEST_VERBOSE}; | ||
94 | diag "you should see one invalid range definition warning and an UNKNOWN line here:\n"; | ||
95 | Nagios::Plugin::Base->print_on_die(1); | ||
96 | Nagios::Plugin::Base->exit_on_die(1); | ||
97 | |||
98 | dies_ok( sub { | ||
99 | $t = Nagios::Plugin::Threshold->set_thresholds( | ||
100 | warning => "total", | ||
101 | critical => "rubbish" | ||
102 | ) | ||
103 | }, "bad thresholds cause death" | ||
104 | ); | ||
105 | Nagios::Plugin::Base->print_on_die(0); | ||
106 | Nagios::Plugin::Base->exit_on_die(0); | ||
107 | SKIP_DEATH: | ||
108 | |||
109 | |||
110 | diag "threshold: critical if > 25 " if $ENV{TEST_VERBOSE}; | ||
111 | $t = Nagios::Plugin::Threshold->set_thresholds( critical => "~:25" ); | ||
112 | ok( defined $t, "Threshold ('', '~:25') set (".$t->critical->stringify().")" ); | ||
30 | ok( ! $t->warning->is_set, "Warning not set"); | 113 | ok( ! $t->warning->is_set, "Warning not set"); |
31 | ok( ! $t->critical->is_set, "Critical not set"); | 114 | cmp_ok( $t->critical->end, '==',25, "Critical end set"); |
115 | ok $t->critical->start_infinity, "Critical starts at negative infinity"; | ||
116 | |||
117 | $expected = { qw( | ||
118 | -1 OK | ||
119 | 4 OK | ||
120 | 10 OK | ||
121 | 14.21 OK | ||
122 | 25 OK | ||
123 | 25.01 CRITICAL | ||
124 | 31001 CRITICAL | ||
125 | ) }; | ||
126 | test_expected_statuses( $t, $expected); | ||
127 | |||
128 | diag "threshold: warn if OUTSIDE {10..25} , critical if > 25 " if $ENV{TEST_VERBOSE}; | ||
129 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "10:25", critical => "~:25"); | ||
130 | ok( defined $t, "Threshold ('10:25', '~:25') set"); | ||
131 | cmp_ok( $t->warning->start, '==', 10, "Warning start set"); | ||
132 | cmp_ok( $t->warning->end, '==', 25, "Warning end set"); | ||
133 | cmp_ok( $t->critical->end, '==', 25, "Critical end set"); | ||
134 | |||
135 | $expected = { qw( | ||
136 | -1 WARNING | ||
137 | 4 WARNING | ||
138 | 9.999999 WARNING | ||
139 | 10 OK | ||
140 | 14.21 OK | ||
141 | 25 OK | ||
142 | 25.01 CRITICAL | ||
143 | 31001 CRITICAL | ||
144 | ) }; | ||
145 | test_expected_statuses( $t, $expected ); | ||
146 | |||
147 | |||
148 | diag "warn if INSIDE {10..25} , critical if < 10 " if $ENV{TEST_VERBOSE}; | ||
149 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "\@10:25", critical => "10:"); | ||
150 | $expected = { qw( | ||
151 | -1 CRITICAL | ||
152 | 4 CRITICAL | ||
153 | 9.999999 CRITICAL | ||
154 | 10 WARNING | ||
155 | 14.21 WARNING | ||
156 | 25 WARNING | ||
157 | 25.01 OK | ||
158 | 31001 OK | ||
159 | ) }; | ||
160 | test_expected_statuses( $t, $expected ); | ||
32 | 161 | ||
162 | 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 @@ | |||
1 | 1 | ||
2 | use strict; | 2 | use strict; |
3 | use Test::More tests => 5; | 3 | use Test::More tests => 9; |
4 | |||
4 | BEGIN { use_ok('Nagios::Plugin') }; | 5 | BEGIN { use_ok('Nagios::Plugin') }; |
5 | 6 | ||
6 | use Nagios::Plugin::Base; | 7 | use Nagios::Plugin::Base; |
7 | Nagios::Plugin::Base->exit_on_die(0); | 8 | Nagios::Plugin::Base->exit_on_die(0); |
8 | Nagios::Plugin::Base->print_on_die(0); | 9 | Nagios::Plugin::Base->print_on_die(0); |
9 | 10 | ||
11 | diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"; | ||
12 | |||
10 | my $p = Nagios::Plugin->new; | 13 | my $p = Nagios::Plugin->new; |
11 | isa_ok( $p, "Nagios::Plugin"); | 14 | isa_ok( $p, "Nagios::Plugin"); |
12 | 15 | ||
13 | $p->shortname("PAGESIZE"); | 16 | $p->shortname("PAGESIZE"); |
14 | 17 | ||
15 | my $t = $p->set_thresholds( warning => "10:25", critical => "25:" ); | 18 | diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE}; |
19 | my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" ); | ||
16 | 20 | ||
17 | $p->add_perfdata( | 21 | $p->add_perfdata( |
18 | label => "size", | 22 | label => "size", |
@@ -21,12 +25,20 @@ $p->add_perfdata( | |||
21 | threshold => $t, | 25 | threshold => $t, |
22 | ); | 26 | ); |
23 | 27 | ||
24 | cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;25:", "Perfdata correct"); | 28 | cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct"); |
25 | 29 | ||
26 | my $o = $p->die( return_code => $t->get_status(1), message => "page size at http://... was 1kB" ); | 30 | my $expected = {qw( |
27 | cmp_ok( $o, "eq", 'PAGESIZE CRITICAL page size at http://... was 1kB | size=1kB;10:25;25:', "Output okay"); | 31 | -1 WARNING |
28 | 32 | 1 WARNING | |
29 | cmp_ok( $p->die( return_code => $t->get_status(30), message => "page size at http://... was 30kB" ), | 33 | 20 OK |
30 | "eq", 'PAGESIZE WARNING page size at http://... was 30kB | size=1kB;10:25;25:', "Output okay"); | 34 | 25 OK |
31 | 35 | 26 CRITICAL | |
36 | 30 CRITICAL | ||
37 | )}; | ||
38 | |||
39 | foreach (sort {$a<=>$b} keys %$expected) { | ||
40 | like $p->die( return_code => $t->get_status($_), message => "page size at http://... was ${_}kB" ), | ||
41 | qr/$expected->{$_}/, | ||
42 | "Output okay. $_ = $expected->{$_}" ; | ||
43 | } | ||
32 | 44 | ||