diff options
-rw-r--r-- | Changes | 5 | ||||
-rw-r--r-- | lib/Nagios/Plugin.pm | 3 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Functions.pm | 18 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Threshold.pm | 4 | ||||
-rw-r--r-- | t/Nagios-Plugin-Functions-01.t | 7 | ||||
-rw-r--r-- | t/Nagios-Plugin-Performance.t | 14 |
6 files changed, 42 insertions, 9 deletions
@@ -1,5 +1,10 @@ | |||
1 | Revision history for Perl module Nagios::Plugin. | 1 | Revision history for Perl module Nagios::Plugin. |
2 | 2 | ||
3 | 0.18 ?? | ||
4 | - Fix error when parsing performance data where warn or crit are 0 | ||
5 | - Optional _use_die flag to force nagios_die to call die instead of exit, so | ||
6 | exceptions can be caught with an eval | ||
7 | |||
3 | 0.17 23rd March 2007 | 8 | 0.17 23rd March 2007 |
4 | - bump version number again due to cpan indexing stupidity (Gavin) | 9 | - bump version number again due to cpan indexing stupidity (Gavin) |
5 | 10 | ||
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 459e6bb..fe83575 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm | |||
@@ -476,7 +476,8 @@ form "SHORTNAME CODE - $message". | |||
476 | =item nagios_die( $message, [<CODE>] ) | 476 | =item nagios_die( $message, [<CODE>] ) |
477 | 477 | ||
478 | Same as nagios_exit(), except that CODE is optional, defaulting | 478 | Same as nagios_exit(), except that CODE is optional, defaulting |
479 | to UNKNOWN. | 479 | to UNKNOWN. NOTE: exceptions are not raised by default to calling code. |
480 | Set C<$_use_die> flag if this functionality is required (see test code). | ||
480 | 481 | ||
481 | =item die( $message, [<CODE>] ) | 482 | =item die( $message, [<CODE>] ) |
482 | 483 | ||
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm index f750968..e8b292e 100644 --- a/lib/Nagios/Plugin/Functions.pm +++ b/lib/Nagios/Plugin/Functions.pm | |||
@@ -46,6 +46,10 @@ our %STATUS_TEXT = reverse %ERRORS; | |||
46 | my $_fake_exit = 0; | 46 | my $_fake_exit = 0; |
47 | sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; | 47 | sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; |
48 | 48 | ||
49 | # _use_die flag and accessor/mutator, so exceptions can be raised correctly | ||
50 | my $_use_die = 0; | ||
51 | sub _use_die { @_ ? $_use_die = shift : $_use_die }; | ||
52 | |||
49 | sub get_shortname { | 53 | sub get_shortname { |
50 | my %arg = @_; | 54 | my %arg = @_; |
51 | 55 | ||
@@ -115,9 +119,14 @@ sub nagios_exit { | |||
115 | return Nagios::Plugin::ExitResult->new($code, $output); | 119 | return Nagios::Plugin::ExitResult->new($code, $output); |
116 | } | 120 | } |
117 | 121 | ||
118 | # Print output and exit | 122 | # Print output and exit; die if called via nagios_die and flag set |
119 | print $output; | 123 | if($_use_die && (caller(1))[3] =~ m/die/) { |
120 | exit $code; | 124 | $!=$code; |
125 | die($output); | ||
126 | } else { | ||
127 | print $output; | ||
128 | exit $code; | ||
129 | } | ||
121 | } | 130 | } |
122 | 131 | ||
123 | # nagios_die( $message, [ $code ]) OR nagios_die( $code, $message ) | 132 | # nagios_die( $message, [ $code ]) OR nagios_die( $code, $message ) |
@@ -297,7 +306,8 @@ form "PLUGIN CODE - $message". | |||
297 | =item nagios_die( $message, [CODE] ) | 306 | =item nagios_die( $message, [CODE] ) |
298 | 307 | ||
299 | Same as nagios_exit(), except that CODE is optional, defaulting | 308 | Same as nagios_exit(), except that CODE is optional, defaulting |
300 | to UNKNOWN. | 309 | to UNKNOWN. NOTE: exceptions are not raised by default to calling code. |
310 | Set C<$_use_die> flag if this functionality is required (see test code). | ||
301 | 311 | ||
302 | =item check_messages( critical => \@crit, warning => \@warn ) | 312 | =item check_messages( critical => \@crit, warning => \@warn ) |
303 | 313 | ||
diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index ad58134..145b89f 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm | |||
@@ -40,8 +40,8 @@ sub _inflate | |||
40 | } | 40 | } |
41 | 41 | ||
42 | # Otherwise parse $value | 42 | # Otherwise parse $value |
43 | my $range = Nagios::Plugin::Range->parse_range_string($value) | 43 | my $range = Nagios::Plugin::Range->parse_range_string($value); |
44 | or nagios_die("Cannot parse $key range: '$value'"); | 44 | nagios_die("Cannot parse $key range: '$value'") unless(defined($range)); |
45 | return $range; | 45 | return $range; |
46 | } | 46 | } |
47 | 47 | ||
diff --git a/t/Nagios-Plugin-Functions-01.t b/t/Nagios-Plugin-Functions-01.t index 70db221..5268255 100644 --- a/t/Nagios-Plugin-Functions-01.t +++ b/t/Nagios-Plugin-Functions-01.t | |||
@@ -1,6 +1,6 @@ | |||
1 | 1 | ||
2 | use strict; | 2 | use strict; |
3 | use Test::More tests => 112; | 3 | use Test::More tests => 113; |
4 | 4 | ||
5 | BEGIN { use_ok("Nagios::Plugin::Functions", ":all"); } | 5 | BEGIN { use_ok("Nagios::Plugin::Functions", ":all"); } |
6 | Nagios::Plugin::Functions::_fake_exit(1); | 6 | Nagios::Plugin::Functions::_fake_exit(1); |
@@ -154,3 +154,8 @@ for (@ok) { | |||
154 | $_->[1] . '.*' . $_->[2])); | 154 | $_->[1] . '.*' . $_->[2])); |
155 | } | 155 | } |
156 | 156 | ||
157 | # Check that _use_die set to 1 will catch exceptions correctly | ||
158 | Nagios::Plugin::Functions::_fake_exit(0); | ||
159 | Nagios::Plugin::Functions::_use_die(1); | ||
160 | eval { nagios_die("Using die") }; | ||
161 | is( $@, "NAGIOS-PLUGIN-FUNCTIONS-01 UNKNOWN - Using die\n", "Caught exception"); | ||
diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t index 0dcb800..c4d518c 100644 --- a/t/Nagios-Plugin-Performance.t +++ b/t/Nagios-Plugin-Performance.t | |||
@@ -1,6 +1,6 @@ | |||
1 | 1 | ||
2 | use strict; | 2 | use strict; |
3 | use Test::More tests => 77; | 3 | use Test::More tests => 84; |
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" if $ENV{TEST_VERBOSE}; | 6 | diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; |
@@ -120,5 +120,17 @@ cmp_ok( $p[0]->rrdlabel, "eq", "home_a_m", "changing / to _"); | |||
120 | cmp_ok( $p[1]->rrdlabel, "eq", "shared_folder_big", "replacing bad characters"); | 120 | cmp_ok( $p[1]->rrdlabel, "eq", "shared_folder_big", "replacing bad characters"); |
121 | cmp_ok( $p[2]->rrdlabel, "eq", "1234567890123456789", "shortening rrd label"); | 121 | cmp_ok( $p[2]->rrdlabel, "eq", "1234567890123456789", "shortening rrd label"); |
122 | 122 | ||
123 | # turn off fake_exit and enable use_die so we pick up on errors via nagios_die | ||
124 | Nagios::Plugin::Functions::_use_die(1); | ||
125 | Nagios::Plugin::Functions::_fake_exit(0); | ||
126 | |||
127 | @p = Nagios::Plugin::Performance->parse_perfstring("time=0.002722s;0.000000;0.000000;0.000000;10.000000"); | ||
128 | cmp_ok( $p[0]->label, "eq", "time", "label okay"); | ||
129 | cmp_ok( $p[0]->value, "eq", "0.002722", "value okay"); | ||
130 | cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); | ||
131 | ok( defined $p[0]->threshold->warning->is_set, "Warning range has been set"); | ||
132 | ok( defined $p[0]->threshold->critical->is_set, "Critical range has been set"); | ||
133 | cmp_ok( $p[0]->threshold->warning, 'eq', "0", "warn okay"); | ||
134 | cmp_ok( $p[0]->threshold->critical, 'eq', "0", "crit okay"); | ||
123 | 135 | ||
124 | # add_perfdata tests in t/Nagios-Plugin-01.t | 136 | # add_perfdata tests in t/Nagios-Plugin-01.t |