summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Changes5
-rw-r--r--lib/Nagios/Plugin.pm3
-rw-r--r--lib/Nagios/Plugin/Functions.pm18
-rw-r--r--lib/Nagios/Plugin/Threshold.pm4
-rw-r--r--t/Nagios-Plugin-Functions-01.t7
-rw-r--r--t/Nagios-Plugin-Performance.t14
6 files changed, 42 insertions, 9 deletions
diff --git a/Changes b/Changes
index 67e36fe..2b67941 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,10 @@
1Revision history for Perl module Nagios::Plugin. 1Revision history for Perl module Nagios::Plugin.
2 2
30.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
30.17 23rd March 2007 80.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
478Same as nagios_exit(), except that CODE is optional, defaulting 478Same as nagios_exit(), except that CODE is optional, defaulting
479to UNKNOWN. 479to UNKNOWN. NOTE: exceptions are not raised by default to calling code.
480Set 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;
46my $_fake_exit = 0; 46my $_fake_exit = 0;
47sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; 47sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit };
48 48
49# _use_die flag and accessor/mutator, so exceptions can be raised correctly
50my $_use_die = 0;
51sub _use_die { @_ ? $_use_die = shift : $_use_die };
52
49sub get_shortname { 53sub 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
299Same as nagios_exit(), except that CODE is optional, defaulting 308Same as nagios_exit(), except that CODE is optional, defaulting
300to UNKNOWN. 309to UNKNOWN. NOTE: exceptions are not raised by default to calling code.
310Set 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
2use strict; 2use strict;
3use Test::More tests => 112; 3use Test::More tests => 113;
4 4
5BEGIN { use_ok("Nagios::Plugin::Functions", ":all"); } 5BEGIN { use_ok("Nagios::Plugin::Functions", ":all"); }
6Nagios::Plugin::Functions::_fake_exit(1); 6Nagios::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
158Nagios::Plugin::Functions::_fake_exit(0);
159Nagios::Plugin::Functions::_use_die(1);
160eval { nagios_die("Using die") };
161is( $@, "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
2use strict; 2use strict;
3use Test::More tests => 77; 3use Test::More tests => 84;
4BEGIN { use_ok('Nagios::Plugin::Performance') }; 4BEGIN { use_ok('Nagios::Plugin::Performance') };
5 5
6diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; 6diag "\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 _");
120cmp_ok( $p[1]->rrdlabel, "eq", "shared_folder_big", "replacing bad characters"); 120cmp_ok( $p[1]->rrdlabel, "eq", "shared_folder_big", "replacing bad characters");
121cmp_ok( $p[2]->rrdlabel, "eq", "1234567890123456789", "shortening rrd label"); 121cmp_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
124Nagios::Plugin::Functions::_use_die(1);
125Nagios::Plugin::Functions::_fake_exit(0);
126
127@p = Nagios::Plugin::Performance->parse_perfstring("time=0.002722s;0.000000;0.000000;0.000000;10.000000");
128cmp_ok( $p[0]->label, "eq", "time", "label okay");
129cmp_ok( $p[0]->value, "eq", "0.002722", "value okay");
130cmp_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");
133cmp_ok( $p[0]->threshold->warning, 'eq', "0", "warn okay");
134cmp_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