[Nagiosplug-checkins] SF.net SVN: nagiosplug: [1772] Nagios-Plugin/trunk

tonvoon at users.sourceforge.net tonvoon at users.sourceforge.net
Fri Aug 31 15:21:11 CEST 2007


Revision: 1772
          http://nagiosplug.svn.sourceforge.net/nagiosplug/?rev=1772&view=rev
Author:   tonvoon
Date:     2007-08-31 06:21:10 -0700 (Fri, 31 Aug 2007)

Log Message:
-----------
Fixed bug where warn or crit = 0 will raise an error. Optional flag to
tell nagios_die to use die instead of exit so trappable by eval

Modified Paths:
--------------
    Nagios-Plugin/trunk/Changes
    Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm
    Nagios-Plugin/trunk/lib/Nagios/Plugin/Threshold.pm
    Nagios-Plugin/trunk/lib/Nagios/Plugin.pm
    Nagios-Plugin/trunk/t/Nagios-Plugin-Functions-01.t
    Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t

Modified: Nagios-Plugin/trunk/Changes
===================================================================
--- Nagios-Plugin/trunk/Changes	2007-08-28 03:19:45 UTC (rev 1771)
+++ Nagios-Plugin/trunk/Changes	2007-08-31 13:21:10 UTC (rev 1772)
@@ -1,5 +1,10 @@
 Revision history for Perl module Nagios::Plugin.
 
+0.18 ??
+  - Fix error when parsing performance data where warn or crit are 0
+  - Optional _use_die flag to force nagios_die to call die instead of exit, so
+    exceptions can be caught with an eval
+
 0.17  23rd March 2007
   - bump version number again due to cpan indexing stupidity (Gavin)
 

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm	2007-08-28 03:19:45 UTC (rev 1771)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm	2007-08-31 13:21:10 UTC (rev 1772)
@@ -46,6 +46,10 @@
 my $_fake_exit = 0;
 sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit };
 
+# _use_die flag and accessor/mutator, so exceptions can be raised correctly
+my $_use_die = 0;
+sub _use_die { @_ ? $_use_die = shift : $_use_die };
+
 sub get_shortname {
     my %arg = @_;
 
@@ -115,9 +119,14 @@
         return Nagios::Plugin::ExitResult->new($code, $output);
     }
 
-    # Print output and exit
-    print $output;
-    exit $code;
+    # Print output and exit; die if called via nagios_die and flag set
+    if($_use_die && (caller(1))[3] =~ m/die/) {
+        $!=$code;
+        die($output);
+    } else {
+        print $output;
+        exit $code;
+    }
 }
 
 # nagios_die( $message, [ $code ])   OR   nagios_die( $code, $message )
@@ -297,7 +306,8 @@
 =item nagios_die( $message, [CODE] )
 
 Same as nagios_exit(), except that CODE is optional, defaulting
-to UNKNOWN.
+to UNKNOWN.  NOTE: exceptions are not raised by default to calling code.
+Set C<$_use_die> flag if this functionality is required (see test code).
 
 =item check_messages( critical => \@crit, warning => \@warn )
 

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin/Threshold.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin/Threshold.pm	2007-08-28 03:19:45 UTC (rev 1771)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin/Threshold.pm	2007-08-31 13:21:10 UTC (rev 1772)
@@ -40,8 +40,8 @@
     }
 
     # Otherwise parse $value
-    my $range = Nagios::Plugin::Range->parse_range_string($value) 
-        or nagios_die("Cannot parse $key range: '$value'");
+    my $range = Nagios::Plugin::Range->parse_range_string($value);
+    nagios_die("Cannot parse $key range: '$value'") unless(defined($range));
     return $range;
 }
 

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin.pm	2007-08-28 03:19:45 UTC (rev 1771)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin.pm	2007-08-31 13:21:10 UTC (rev 1772)
@@ -476,7 +476,8 @@
 =item nagios_die( $message, [<CODE>] )
 
 Same as nagios_exit(), except that CODE is optional, defaulting
-to UNKNOWN.
+to UNKNOWN.  NOTE: exceptions are not raised by default to calling code.
+Set C<$_use_die> flag if this functionality is required (see test code).
 
 =item die( $message, [<CODE>] )
 

Modified: Nagios-Plugin/trunk/t/Nagios-Plugin-Functions-01.t
===================================================================
--- Nagios-Plugin/trunk/t/Nagios-Plugin-Functions-01.t	2007-08-28 03:19:45 UTC (rev 1771)
+++ Nagios-Plugin/trunk/t/Nagios-Plugin-Functions-01.t	2007-08-31 13:21:10 UTC (rev 1772)
@@ -1,6 +1,6 @@
 
 use strict;
-use Test::More tests => 112;
+use Test::More tests => 113;
 
 BEGIN { use_ok("Nagios::Plugin::Functions", ":all"); }
 Nagios::Plugin::Functions::_fake_exit(1);
@@ -154,3 +154,8 @@
             $_->[1] . '.*' . $_->[2]));
 }
 
+# Check that _use_die set to 1 will catch exceptions correctly
+Nagios::Plugin::Functions::_fake_exit(0);
+Nagios::Plugin::Functions::_use_die(1);
+eval { nagios_die("Using die") };
+is( $@, "NAGIOS-PLUGIN-FUNCTIONS-01 UNKNOWN - Using die\n", "Caught exception");

Modified: Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t
===================================================================
--- Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t	2007-08-28 03:19:45 UTC (rev 1771)
+++ Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t	2007-08-31 13:21:10 UTC (rev 1772)
@@ -1,6 +1,6 @@
 
 use strict;
-use Test::More tests => 77;
+use Test::More tests => 84;
 BEGIN { use_ok('Nagios::Plugin::Performance') };
 
 diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE};
@@ -120,5 +120,17 @@
 cmp_ok( $p[1]->rrdlabel, "eq", "shared_folder_big", "replacing bad characters");
 cmp_ok( $p[2]->rrdlabel, "eq", "1234567890123456789", "shortening rrd label");
 
+# turn off fake_exit and enable use_die so we pick up on errors via nagios_die
+Nagios::Plugin::Functions::_use_die(1);
+Nagios::Plugin::Functions::_fake_exit(0);
 
+ at p = Nagios::Plugin::Performance->parse_perfstring("time=0.002722s;0.000000;0.000000;0.000000;10.000000");
+cmp_ok( $p[0]->label, "eq", "time", "label okay");
+cmp_ok( $p[0]->value, "eq", "0.002722", "value okay");
+cmp_ok( $p[0]->uom, "eq", "s", "uom okay");
+    ok( defined $p[0]->threshold->warning->is_set, "Warning range has been set"); 
+    ok( defined $p[0]->threshold->critical->is_set, "Critical range has been set");
+cmp_ok( $p[0]->threshold->warning, 'eq', "0", "warn okay");
+cmp_ok( $p[0]->threshold->critical, 'eq', "0", "crit okay");
+
 # add_perfdata tests in t/Nagios-Plugin-01.t


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Commits mailing list