From 95dba9c4213c2e7e8f4b572efd873c1979406221 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Fri, 31 Aug 2007 13:21:10 +0000 Subject: 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 git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1772 f882894a-f735-0410-b71e-b25c423dba1c --- lib/Nagios/Plugin/Functions.pm | 18 ++++++++++++++---- lib/Nagios/Plugin/Threshold.pm | 4 ++-- 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'lib/Nagios/Plugin') 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; 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 @@ sub nagios_exit { 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 @@ form "PLUGIN CODE - $message". =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 ) 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 } # 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; } -- cgit v1.2.3-74-g34f1