diff options
author | Gavin Carr <gonzai@users.sourceforge.net> | 2006-09-11 01:57:26 +0000 |
---|---|---|
committer | Gavin Carr <gonzai@users.sourceforge.net> | 2006-09-11 01:57:26 +0000 |
commit | 1f8410dd1914449ce52ffa8fd47a308c5b372e52 (patch) | |
tree | dc14d10c159541daa211ef59f8d69e859e05fd13 | |
parent | bc239b3bd5023ed2da77ab03c581e56a4772f1d4 (diff) | |
download | monitoring-plugin-perl-1f8410dd1914449ce52ffa8fd47a308c5b372e52.tar.gz |
Add constants, nagios_exit, and nagios_die to Nagios::Plugin::Base.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1477 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r-- | MANIFEST | 4 | ||||
-rw-r--r-- | lib/Nagios/Plugin.pm | 3 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Base.pm | 131 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Getopt.pm | 7 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Threshold.pm | 20 | ||||
-rw-r--r-- | t/Nagios-Plugin-Base.t | 143 | ||||
-rw-r--r-- | t/Nagios-Plugin-Performance.t | 2 | ||||
-rw-r--r-- | t/Nagios-Plugin-Threshold.t | 16 | ||||
-rw-r--r-- | t/Nagios-Plugin.t | 3 |
9 files changed, 293 insertions, 36 deletions
@@ -5,6 +5,9 @@ README | |||
5 | t/check_stuff.pl | 5 | t/check_stuff.pl |
6 | t/check_stuff.t | 6 | t/check_stuff.t |
7 | t/Nagios-Plugin.t | 7 | t/Nagios-Plugin.t |
8 | t/Nagios-Plugin-Base.t | ||
9 | t/Nagios-Plugin-Getopt-01.t | ||
10 | t/Nagios-Plugin-Getopt-02.t | ||
8 | t/Nagios-Plugin-Performance.t | 11 | t/Nagios-Plugin-Performance.t |
9 | t/Nagios-Plugin-Range.t | 12 | t/Nagios-Plugin-Range.t |
10 | t/Nagios-Plugin-Threshold.t | 13 | t/Nagios-Plugin-Threshold.t |
@@ -14,4 +17,5 @@ lib/Nagios/Plugin/Range.pm | |||
14 | lib/Nagios/Plugin/Threshold.pm | 17 | lib/Nagios/Plugin/Threshold.pm |
15 | lib/Nagios/Plugin/Base.pm | 18 | lib/Nagios/Plugin/Base.pm |
16 | lib/Nagios/Plugin/Getopt.pm | 19 | lib/Nagios/Plugin/Getopt.pm |
20 | lib/Nagios/Plugin/ExitResult.pm | ||
17 | META.yml Module meta-data (added by MakeMaker) | 21 | META.yml Module meta-data (added by MakeMaker) |
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 0915571..88c15c5 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm | |||
@@ -43,8 +43,7 @@ sub shortname { shift; @_ ? $shortname = shift : $shortname } | |||
43 | 43 | ||
44 | sub die { | 44 | sub die { |
45 | my $self = shift; | 45 | my $self = shift; |
46 | my %args = @_; | 46 | Nagios::Plugin::Base::die(@_, { plugin => $self }); |
47 | Nagios::Plugin::Base->die(\%args, $self); | ||
48 | } | 47 | } |
49 | 48 | ||
50 | 1; | 49 | 1; |
diff --git a/lib/Nagios/Plugin/Base.pm b/lib/Nagios/Plugin/Base.pm index c2e9902..92651ed 100644 --- a/lib/Nagios/Plugin/Base.pm +++ b/lib/Nagios/Plugin/Base.pm | |||
@@ -4,24 +4,139 @@ package Nagios::Plugin::Base; | |||
4 | 4 | ||
5 | use strict; | 5 | use strict; |
6 | use warnings; | 6 | use warnings; |
7 | use File::Basename; | ||
7 | 8 | ||
8 | our $VERSION = "0.13"; | 9 | our $VERSION = "0.13"; |
9 | 10 | ||
10 | use Exporter; | 11 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); |
11 | our @ISA = qw(Exporter); | ||
12 | our @EXPORT = qw(%ERRORS); | ||
13 | 12 | ||
14 | our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); | 13 | require Exporter; |
14 | our @ISA = qw(Exporter); | ||
15 | our @EXPORT = (@STATUS_CODES, qw(nagios_exit %ERRORS)); | ||
16 | our @EXPORT_OK = qw(nagios_die %STATUS_TEXT); | ||
17 | our %EXPORT_TAGS = ( | ||
18 | all => [ @EXPORT, @EXPORT_OK ], | ||
19 | codes => [ @STATUS_CODES ], | ||
20 | functions => [ qw(nagios_exit nagios_die) ], | ||
21 | ); | ||
22 | |||
23 | use constant OK => 0; | ||
24 | use constant WARNING => 1; | ||
25 | use constant CRITICAL => 2; | ||
26 | use constant UNKNOWN => 3; | ||
27 | use constant DEPENDENT => 4; | ||
28 | |||
29 | our %ERRORS = ( | ||
30 | 'OK' => OK, | ||
31 | 'WARNING' => WARNING, | ||
32 | 'CRITICAL' => CRITICAL, | ||
33 | 'UNKNOWN' => UNKNOWN, | ||
34 | 'DEPENDENT' => DEPENDENT, | ||
35 | ); | ||
15 | 36 | ||
16 | our %STATUS_TEXT = reverse %ERRORS; | 37 | our %STATUS_TEXT = reverse %ERRORS; |
17 | 38 | ||
39 | # _fake_exit flag and accessor/mutator, for testing | ||
40 | my $_fake_exit = 0; | ||
41 | sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; | ||
42 | |||
43 | sub get_shortname { | ||
44 | my %arg = @_; | ||
45 | |||
46 | return $arg{plugin}->shortname if $arg{plugin}; | ||
47 | |||
48 | my $shortname = uc basename($ENV{NAGIOS_PLUGIN} || $0); | ||
49 | $shortname =~ s/^CHECK_//; | ||
50 | return $shortname; | ||
51 | } | ||
52 | |||
53 | # nagios_exit( $code, $message ) | ||
54 | sub nagios_exit { | ||
55 | my ($code, $message, $arg) = @_; | ||
56 | |||
57 | # Handle named parameters | ||
58 | if (defined $code && ($code eq 'return_code' || $code eq 'message')) { | ||
59 | # Remove last argument if odd no and last is ref | ||
60 | if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) { | ||
61 | $arg = pop @_; | ||
62 | } else { | ||
63 | undef $arg; | ||
64 | } | ||
65 | my %arg = @_; | ||
66 | $code = $arg{return_code}; | ||
67 | $message = $arg{message}; | ||
68 | } | ||
69 | $arg ||= {}; | ||
70 | |||
71 | # Handle string codes | ||
72 | $code = $ERRORS{$code} if defined $code && exists $ERRORS{$code}; | ||
73 | |||
74 | # Set defaults | ||
75 | $code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code}; | ||
76 | $message = '' unless defined $message; | ||
77 | $message = join(' ', @$message) if ref $message eq 'ARRAY'; | ||
78 | |||
79 | # Setup output | ||
80 | my $output = "$STATUS_TEXT{$code}"; | ||
81 | $output .= " - $message" if defined $message && $message ne ''; | ||
82 | my $shortname = get_shortname(plugin => $arg->{plugin}); | ||
83 | $output = "$shortname $output" if $shortname; | ||
84 | if ($arg->{plugin}) { | ||
85 | my $plugin = $arg->{plugin}; | ||
86 | $output .= " | ". $plugin->all_perfoutput if $plugin->perfdata; | ||
87 | } | ||
88 | $output .= "\n"; | ||
89 | |||
90 | # Don't actually exit if _fake_exit set | ||
91 | if ($_fake_exit) { | ||
92 | require Nagios::Plugin::ExitResult; | ||
93 | return Nagios::Plugin::ExitResult->new($code, $output); | ||
94 | } | ||
95 | |||
96 | # Print output and exit | ||
97 | print $output; | ||
98 | exit $code; | ||
99 | } | ||
100 | |||
101 | # nagios_die( $message, [ $code ]) OR nagios_die( $code, $message ) | ||
102 | # Default $code: UNKNOWN | ||
103 | sub nagios_die { | ||
104 | my ($arg1, $arg2, $rest) = @_; | ||
105 | |||
106 | # Named parameters | ||
107 | if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) { | ||
108 | return nagios_exit(@_); | ||
109 | } | ||
110 | |||
111 | # ($code, $message) | ||
112 | elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) { | ||
113 | return nagios_exit(@_); | ||
114 | } | ||
115 | |||
116 | # ($message, $code) | ||
117 | elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) { | ||
118 | return nagios_exit($arg2, $arg1, $rest); | ||
119 | } | ||
120 | |||
121 | # Else just assume $arg1 is the message and hope for the best | ||
122 | else { | ||
123 | return nagios_exit( UNKNOWN, $arg1, $rest ); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | # For backwards compatibility | ||
128 | sub die { nagios_die(@_); } | ||
129 | |||
130 | |||
131 | =pod old | ||
18 | 132 | ||
19 | my $exit_on_die = 1; | 133 | my $exit_on_die = 1; |
20 | sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; | 134 | sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; |
21 | my $print_on_die = 1; | 135 | my $print_on_die = 1; |
22 | sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; | 136 | sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; |
23 | 137 | ||
24 | sub die { | 138 | # Old version - TODO: remove |
139 | sub old_die { | ||
25 | my ($class, $args, $plugin) = @_; | 140 | my ($class, $args, $plugin) = @_; |
26 | my $return_code; | 141 | my $return_code; |
27 | 142 | ||
@@ -49,7 +164,12 @@ sub die { | |||
49 | } | 164 | } |
50 | } | 165 | } |
51 | 166 | ||
167 | =cut | ||
168 | |||
52 | 1; | 169 | 1; |
170 | |||
171 | # vim:sw=4:sm:et | ||
172 | |||
53 | __END__ | 173 | __END__ |
54 | 174 | ||
55 | =head1 NAME | 175 | =head1 NAME |
@@ -73,5 +193,4 @@ This library is free software; you can redistribute it and/or modify | |||
73 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | 193 | it under the same terms as Perl itself, either Perl version 5.8.4 or, |
74 | at your option, any later version of Perl 5 you may have available. | 194 | at your option, any later version of Perl 5 you may have available. |
75 | 195 | ||
76 | |||
77 | =cut | 196 | =cut |
diff --git a/lib/Nagios/Plugin/Getopt.pm b/lib/Nagios/Plugin/Getopt.pm index d38dced..7f32c3b 100644 --- a/lib/Nagios/Plugin/Getopt.pm +++ b/lib/Nagios/Plugin/Getopt.pm | |||
@@ -5,7 +5,6 @@ | |||
5 | 5 | ||
6 | package Nagios::Plugin::Getopt; | 6 | package Nagios::Plugin::Getopt; |
7 | 7 | ||
8 | use 5.005003; | ||
9 | use strict; | 8 | use strict; |
10 | use File::Basename; | 9 | use File::Basename; |
11 | use Getopt::Long qw(:config no_ignore_case bundling); | 10 | use Getopt::Long qw(:config no_ignore_case bundling); |
@@ -21,7 +20,7 @@ $VERSION = $Nagios::Plugin::Base::VERSION; | |||
21 | my %DEFAULT = ( | 20 | my %DEFAULT = ( |
22 | timeout => 15, | 21 | timeout => 15, |
23 | verbose => 0, | 22 | verbose => 0, |
24 | licence => | 23 | license => |
25 | "This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY. | 24 | "This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY. |
26 | It may be used, redistributed and/or modified under the terms of the GNU | 25 | It may be used, redistributed and/or modified under the terms of the GNU |
27 | General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt).", | 26 | General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt).", |
@@ -265,7 +264,7 @@ sub _init | |||
265 | plugin => { default => $plugin }, | 264 | plugin => { default => $plugin }, |
266 | blurb => 0, | 265 | blurb => 0, |
267 | extra => 0, | 266 | extra => 0, |
268 | license => { default => $DEFAULT{licence} }, | 267 | license => { default => $DEFAULT{license} }, |
269 | timeout => { default => $DEFAULT{timeout} }, | 268 | timeout => { default => $DEFAULT{timeout} }, |
270 | }); | 269 | }); |
271 | 270 | ||
@@ -400,7 +399,7 @@ Short plugin description, included in the longer --help output | |||
400 | 399 | ||
401 | License text, included in the longer --help output (see below for an | 400 | License text, included in the longer --help output (see below for an |
402 | example). By default, this is set to the standard nagios plugins | 401 | example). By default, this is set to the standard nagios plugins |
403 | GPL licence text: | 402 | GPL license text: |
404 | 403 | ||
405 | This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY. | 404 | This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY. |
406 | It may be used, redistributed and/or modified under the terms of the GNU | 405 | It may be used, redistributed and/or modified under the terms of the GNU |
diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index 9e7b938..d7a8177 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm | |||
@@ -6,7 +6,7 @@ use strict; | |||
6 | use warnings; | 6 | use warnings; |
7 | 7 | ||
8 | use Nagios::Plugin::Range; | 8 | use Nagios::Plugin::Range; |
9 | use Nagios::Plugin::Base; | 9 | use Nagios::Plugin::Base qw(:codes nagios_die); |
10 | our ($VERSION) = $Nagios::Plugin::Base::VERSION; | 10 | our ($VERSION) = $Nagios::Plugin::Base::VERSION; |
11 | 11 | ||
12 | use Class::Struct; | 12 | use Class::Struct; |
@@ -23,10 +23,7 @@ sub set_thresholds { | |||
23 | if (defined $r) { | 23 | if (defined $r) { |
24 | $t->warning($r); | 24 | $t->warning($r); |
25 | } else { | 25 | } else { |
26 | Nagios::Plugin::Base->die( { | 26 | nagios_die( "Warning range incorrect: '$args{warning}'" ); |
27 | return_code => $ERRORS{UNKNOWN}, | ||
28 | message => "Warning range incorrect: '$args{warning}'" | ||
29 | } ); | ||
30 | } | 27 | } |
31 | } | 28 | } |
32 | if (defined $args{critical}) { | 29 | if (defined $args{critical}) { |
@@ -34,10 +31,7 @@ sub set_thresholds { | |||
34 | if (defined $r) { | 31 | if (defined $r) { |
35 | $t->critical($r); | 32 | $t->critical($r); |
36 | } else { | 33 | } else { |
37 | Nagios::Plugin::Base->die( { | 34 | nagios_die( "Critical range incorrect: '$args{critical}'" ); |
38 | return_code => $ERRORS{UNKNOWN}, | ||
39 | message => "Critical range incorrect: '$args{critical}'" | ||
40 | } ); | ||
41 | } | 35 | } |
42 | } | 36 | } |
43 | return $t; | 37 | return $t; |
@@ -48,15 +42,15 @@ sub get_status { | |||
48 | 42 | ||
49 | if ($self->critical->is_set) { | 43 | if ($self->critical->is_set) { |
50 | if ($self->critical->check_range($value) == 1) { | 44 | if ($self->critical->check_range($value) == 1) { |
51 | return $ERRORS{CRITICAL}; | 45 | return CRITICAL; |
52 | } | 46 | } |
53 | } | 47 | } |
54 | if ($self->warning->is_set) { | 48 | if ($self->warning->is_set) { |
55 | if ($self->warning->check_range($value) == 1) { | 49 | if ($self->warning->check_range($value) == 1) { |
56 | return $ERRORS{WARNING}; | 50 | return WARNING; |
57 | } | 51 | } |
58 | } | 52 | } |
59 | return $ERRORS{OK}; | 53 | return OK; |
60 | } | 54 | } |
61 | 55 | ||
62 | 1; | 56 | 1; |
@@ -81,7 +75,7 @@ Returns the warning or critical range as a Nagios::Plugin::Range object. | |||
81 | 75 | ||
82 | =item get_status($value) | 76 | =item get_status($value) |
83 | 77 | ||
84 | Given a value, will see if the value breeches the critical or the warning range. Returns the status code. | 78 | Given a value, will see if the value breaches the critical or the warning range. Returns the status code. |
85 | 79 | ||
86 | =back | 80 | =back |
87 | 81 | ||
diff --git a/t/Nagios-Plugin-Base.t b/t/Nagios-Plugin-Base.t index 589f331..68a02fe 100644 --- a/t/Nagios-Plugin-Base.t +++ b/t/Nagios-Plugin-Base.t | |||
@@ -1,8 +1,10 @@ | |||
1 | 1 | ||
2 | use strict; | 2 | use strict; |
3 | use Test::More tests => 11; | 3 | use Test::More tests => 111; |
4 | |||
5 | BEGIN { use_ok("Nagios::Plugin::Base", ":all"); } | ||
6 | Nagios::Plugin::Base::_fake_exit(1); | ||
4 | 7 | ||
5 | use_ok("Nagios::Plugin::Base"); | ||
6 | my $this_version=$Nagios::Plugin::Base::VERSION; | 8 | my $this_version=$Nagios::Plugin::Base::VERSION; |
7 | foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { | 9 | foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { |
8 | my $mod = "Nagios::Plugin$m"; | 10 | my $mod = "Nagios::Plugin$m"; |
@@ -12,3 +14,140 @@ foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { | |||
12 | my $a = eval "\$$v"; | 14 | my $a = eval "\$$v"; |
13 | is($a, $this_version, "Version number for $mod the same as Base: $this_version"); | 15 | is($a, $this_version, "Version number for $mod the same as Base: $this_version"); |
14 | } | 16 | } |
17 | |||
18 | # Hardcoded checks of constants | ||
19 | ok(defined %ERRORS, '%ERRORS defined'); | ||
20 | is(OK, $ERRORS{OK}, "OK => $ERRORS{OK}"); | ||
21 | is(WARNING, $ERRORS{WARNING}, "WARNING => $ERRORS{WARNING}"); | ||
22 | is(CRITICAL, $ERRORS{CRITICAL}, "CRITICAL => $ERRORS{CRITICAL}"); | ||
23 | is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}"); | ||
24 | is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); | ||
25 | |||
26 | # Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg ) | ||
27 | my $r; | ||
28 | my @ok = ( | ||
29 | [ OK, "OK", 'test the first', ], | ||
30 | [ WARNING, "WARNING", 'test the second', ], | ||
31 | [ CRITICAL, "CRITICAL", 'test the third', ], | ||
32 | [ UNKNOWN, "UNKNOWN", 'test the fourth', ], | ||
33 | [ DEPENDENT, "DEPENDENT", 'test the fifth', ], | ||
34 | ); | ||
35 | for (@ok) { | ||
36 | # CONSTANT | ||
37 | $r = nagios_exit($_->[0], $_->[2]); | ||
38 | is($r->return_code, $_->[0], | ||
39 | sprintf('nagios_exit(%s, $msg) returned %s', $_->[1], $_->[0])); | ||
40 | like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, | ||
41 | sprintf('nagios_exit(%s, $msg) output matched "%s"', | ||
42 | $_->[1], $_->[1] . '.*' . $_->[2])); | ||
43 | |||
44 | # $string | ||
45 | $r = nagios_exit($_->[1], $_->[2]); | ||
46 | is($r->return_code, $_->[0], | ||
47 | sprintf('nagios_exit("%s", $msg) returned %s', $_->[1], $_->[0])); | ||
48 | like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, | ||
49 | sprintf('nagios_exit("%s", $msg) output matched "%s"', $_->[1], | ||
50 | $_->[1] . '.*' . $_->[2])); | ||
51 | like($r, qr/$_->[1]\b.*\b$_->[2]$/, | ||
52 | sprintf('nagios_exit("%s", $msg) stringified matched "%s"', $_->[1], | ||
53 | $_->[1] . '.*' . $_->[2])); | ||
54 | } | ||
55 | |||
56 | # nagios_exit code corner cases | ||
57 | my @ugly1 = ( | ||
58 | [ -1, 'testing code -1' ], | ||
59 | [ 7, 'testing code 7' ], | ||
60 | [ undef, 'testing code undef' ], | ||
61 | [ '', qq(testing code '') ], | ||
62 | [ 'string', qq(testing code 'string') ], | ||
63 | ); | ||
64 | for (@ugly1) { | ||
65 | $r = nagios_exit($_->[0], $_->[1]); | ||
66 | my $display = defined $_->[0] ? "'$_->[0]'" : 'undef'; | ||
67 | is($r->return_code, UNKNOWN, "nagios_exit($display, \$msg) returned ". UNKNOWN); | ||
68 | like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/, | ||
69 | sprintf('nagios_exit(%s, $msg) output matched "%s"', | ||
70 | $display, 'UNKNOWN.*' . $_->[1])); | ||
71 | } | ||
72 | |||
73 | # nagios_exit message corner cases | ||
74 | my @ugly2 = ( | ||
75 | [ '' ], | ||
76 | [ undef ], | ||
77 | [ UNKNOWN ], | ||
78 | ); | ||
79 | for (@ugly2) { | ||
80 | $r = nagios_exit(CRITICAL, $_->[0]); | ||
81 | my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; | ||
82 | my $display2 = defined $_->[0] ? $_->[0] : ''; | ||
83 | like($r->message, qr/CRITICAL\b.*\b$display2$/, | ||
84 | sprintf('nagios_exit(%s, $msg) output matched "%s"', | ||
85 | $display1, "CRITICAL.*$display2")); | ||
86 | } | ||
87 | |||
88 | # Test nagios_die( $msg ) | ||
89 | my @msg = ( | ||
90 | [ 'die you dog' ], | ||
91 | [ '' ], | ||
92 | [ undef ], | ||
93 | ); | ||
94 | for (@msg) { | ||
95 | $r = nagios_die($_->[0]); | ||
96 | my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; | ||
97 | my $display2 = defined $_->[0] ? $_->[0] : ''; | ||
98 | is($r->return_code, UNKNOWN, | ||
99 | sprintf('nagios_die(%s) returned UNKNOWN', $display1)); | ||
100 | like($r->message, qr/UNKNOWN\b.*\b$display2$/, | ||
101 | sprintf('nagios_die(%s) output matched "%s"', $display1, | ||
102 | "UNKNOWN.*$display2")); | ||
103 | } | ||
104 | |||
105 | # Test nagios_die( CONSTANT, $msg ), nagios_die( $msg, CONSTANT ), | ||
106 | # nagios_die( $string, $msg ), and nagios_die( $msg, $string ) | ||
107 | @ok = ( | ||
108 | [ OK, "OK", 'test the first', ], | ||
109 | [ WARNING, "WARNING", 'test the second', ], | ||
110 | [ CRITICAL, "CRITICAL", 'test the third', ], | ||
111 | [ UNKNOWN, "UNKNOWN", 'test the fourth', ], | ||
112 | [ DEPENDENT, "DEPENDENT", 'test the fifth', ], | ||
113 | ); | ||
114 | for (@ok) { | ||
115 | # CONSTANT, $msg | ||
116 | $r = nagios_die($_->[0], $_->[2]); | ||
117 | is($r->return_code, $_->[0], | ||
118 | sprintf('nagios_die(%s, $msg) returned %s', $_->[1], $_->[0])); | ||
119 | like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, | ||
120 | sprintf('nagios_die(%s, $msg) output matched "%s"', | ||
121 | $_->[1], $_->[1] . '.*' . $_->[2])); | ||
122 | |||
123 | # $msg, CONSTANT | ||
124 | $r = nagios_die($_->[2], $_->[0]); | ||
125 | is($r->return_code, $_->[0], | ||
126 | sprintf('nagios_die($msg, %s) returned %s', $_->[1], $_->[0])); | ||
127 | like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, | ||
128 | sprintf('nagios_die($msg, %s) output matched "%s"', | ||
129 | $_->[1], $_->[1] . '.*' . $_->[2])); | ||
130 | |||
131 | # $string, $msg | ||
132 | $r = nagios_die($_->[1], $_->[2]); | ||
133 | is($r->return_code, $_->[0], | ||
134 | sprintf('nagios_die("%s", $msg) returned %s', $_->[1], $_->[0])); | ||
135 | like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, | ||
136 | sprintf('nagios_die("%s", $msg) output matched "%s"', $_->[1], | ||
137 | $_->[1] . '.*' . $_->[2])); | ||
138 | like($r, qr/$_->[1]\b.*\b$_->[2]$/, | ||
139 | sprintf('nagios_die("%s", $msg) stringified matched "%s"', $_->[1], | ||
140 | $_->[1] . '.*' . $_->[2])); | ||
141 | |||
142 | # $string, $msg | ||
143 | $r = nagios_die($_->[2], $_->[1]); | ||
144 | is($r->return_code, $_->[0], | ||
145 | sprintf('nagios_die($msg, "%s") returned %s', $_->[1], $_->[0])); | ||
146 | like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, | ||
147 | sprintf('nagios_die($msg, "%s") output matched "%s"', $_->[1], | ||
148 | $_->[1] . '.*' . $_->[2])); | ||
149 | like($r, qr/$_->[1]\b.*\b$_->[2]$/, | ||
150 | sprintf('nagios_die($msg, "%s") stringified matched "%s"', $_->[1], | ||
151 | $_->[1] . '.*' . $_->[2])); | ||
152 | } | ||
153 | |||
diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t index 0f52de1..1da3191 100644 --- a/t/Nagios-Plugin-Performance.t +++ b/t/Nagios-Plugin-Performance.t | |||
@@ -6,7 +6,7 @@ BEGIN { use_ok('Nagios::Plugin::Performance') }; | |||
6 | diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n"; | 6 | diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n"; |
7 | 7 | ||
8 | use Nagios::Plugin::Base; | 8 | use Nagios::Plugin::Base; |
9 | Nagios::Plugin::Base->exit_on_die(0); | 9 | Nagios::Plugin::Base::_fake_exit(1); |
10 | 10 | ||
11 | my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); | 11 | my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); |
12 | cmp_ok( $p[0]->label, 'eq', "/", "label okay"); | 12 | cmp_ok( $p[0]->label, 'eq', "/", "label okay"); |
diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t index bb8b578..677c054 100644 --- a/t/Nagios-Plugin-Threshold.t +++ b/t/Nagios-Plugin-Threshold.t | |||
@@ -2,13 +2,17 @@ | |||
2 | use strict; | 2 | use strict; |
3 | use Test::More tests => 71; | 3 | use Test::More tests => 71; |
4 | #use Test::Exception; # broken for now so we don't need this. | 4 | #use Test::Exception; # broken for now so we don't need this. |
5 | BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Base') }; | 5 | BEGIN { |
6 | use_ok('Nagios::Plugin::Threshold'); | ||
7 | use_ok('Nagios::Plugin::Base', ':all' ); | ||
8 | # Silence warnings unless TEST_VERBOSE is set | ||
9 | $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} }; | ||
10 | } | ||
6 | 11 | ||
7 | diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n"; | 12 | diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n" |
13 | if $ENV{TEST_VERBOSE}; | ||
8 | 14 | ||
9 | Nagios::Plugin::Base->exit_on_die(0); | 15 | Nagios::Plugin::Base::_fake_exit(1); |
10 | Nagios::Plugin::Base->print_on_die(0); | ||
11 | my %STATUS_TEXT = reverse %ERRORS; | ||
12 | 16 | ||
13 | diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; | 17 | diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; |
14 | my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); | 18 | my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); |
@@ -45,7 +49,7 @@ sub test_expected_statuses { | |||
45 | test_expected_statuses( $t, $expected ); | 49 | test_expected_statuses( $t, $expected ); |
46 | 50 | ||
47 | diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; | 51 | diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; |
48 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => ""); | 52 | eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") }; |
49 | ok( defined $t, "Threshold ('5:33', '') set"); | 53 | ok( defined $t, "Threshold ('5:33', '') set"); |
50 | cmp_ok( $t->warning->start, '==', 5, "Warning start set"); | 54 | cmp_ok( $t->warning->start, '==', 5, "Warning start set"); |
51 | cmp_ok( $t->warning->end, '==', 33, "Warning end set"); | 55 | cmp_ok( $t->warning->end, '==', 33, "Warning end set"); |
diff --git a/t/Nagios-Plugin.t b/t/Nagios-Plugin.t index ed25aca..8921dc5 100644 --- a/t/Nagios-Plugin.t +++ b/t/Nagios-Plugin.t | |||
@@ -5,8 +5,7 @@ use Test::More tests => 9; | |||
5 | BEGIN { use_ok('Nagios::Plugin') }; | 5 | BEGIN { use_ok('Nagios::Plugin') }; |
6 | 6 | ||
7 | use Nagios::Plugin::Base; | 7 | use Nagios::Plugin::Base; |
8 | Nagios::Plugin::Base->exit_on_die(0); | 8 | Nagios::Plugin::Base::_fake_exit(1); |
9 | Nagios::Plugin::Base->print_on_die(0); | ||
10 | 9 | ||
11 | diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"; | 10 | diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"; |
12 | 11 | ||