From 6625e6a0d2a4cec99339b8b526035592dcd4418c Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Mon, 24 Sep 2007 18:45:59 +0000 Subject: Help, usage and version output go to stdout now, rather than stderr git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1795 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/Changes b/Changes index cc6d5f5..c06932f 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl module Nagios::Plugin. +0.21 ?? + - Help, usage and version output now goes to stdout, not stderr + 0.20 5th September 2007 - Version bump because of CPAN permission problems diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm index e6baca9..87f0ccd 100644 --- a/lib/Nagios/Plugin/Functions.pm +++ b/lib/Nagios/Plugin/Functions.pm @@ -8,7 +8,7 @@ use 5.006; use strict; use warnings; use File::Basename; -use Params::Validate qw(validate :types); +use Params::Validate qw(:types validate); use Math::Calc::Units; # Remember to update Nagios::Plugins as well @@ -119,14 +119,24 @@ sub nagios_exit { return Nagios::Plugin::ExitResult->new($code, $output); } - # 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_exit($code, $output); +} + +sub _nagios_exit { + my ($code, $output) = @_; + # Print output and exit; die if flag set and called via a die in stack backtrace + if ($_use_die) { + for (my $i = 0;; $i++) { + @_ = caller($i); + last unless @_; + if ($_[3] =~ m/die/) { + $! = $code; + die($output); + } + } } + print $output; + exit $code; } # nagios_die( $message, [ $code ]) OR nagios_die( $code, $message ) diff --git a/lib/Nagios/Plugin/Getopt.pm b/lib/Nagios/Plugin/Getopt.pm index 743bf7e..b21dc6d 100644 --- a/lib/Nagios/Plugin/Getopt.pm +++ b/lib/Nagios/Plugin/Getopt.pm @@ -60,9 +60,7 @@ sub _die my $self = shift; my ($msg) = @_; $msg .= "\n" unless substr($msg, -1) eq "\n"; - # Set errno to UNKNOWN for die return code - local $! = 3; - die $msg; + Nagios::Plugin::Functions::_nagios_exit(3, $msg); } # Return the given attribute, if set, including a final newline diff --git a/t/Nagios-Plugin-Getopt-01.t b/t/Nagios-Plugin-Getopt-01.t index bad1367..7076d75 100644 --- a/t/Nagios-Plugin-Getopt-01.t +++ b/t/Nagios-Plugin-Getopt-01.t @@ -5,6 +5,9 @@ use strict; use Test::More tests => 76; BEGIN { use_ok('Nagios::Plugin::Getopt') }; +# Needed to get evals to work in testing +Nagios::Plugin::Functions::_use_die(1); + my %PARAM = ( version => '0.01', url => 'http://www.openfusion.com.au/labs/nagios/', diff --git a/t/Nagios-Plugin-Getopt-02.t b/t/Nagios-Plugin-Getopt-02.t index 26e0293..f83b180 100644 --- a/t/Nagios-Plugin-Getopt-02.t +++ b/t/Nagios-Plugin-Getopt-02.t @@ -5,6 +5,9 @@ use strict; use Test::More tests => 14; BEGIN { use_ok('Nagios::Plugin::Getopt') }; +# Needed to get evals to work in testing +Nagios::Plugin::Functions::_use_die(1); + my %PARAM = ( version => '0.01', url => 'http://www.openfusion.com.au/labs/nagios/', diff --git a/t/Nagios-Plugin-Getopt-04.t b/t/Nagios-Plugin-Getopt-04.t index 9092636..115f2a2 100644 --- a/t/Nagios-Plugin-Getopt-04.t +++ b/t/Nagios-Plugin-Getopt-04.t @@ -5,6 +5,9 @@ use strict; use Test::More tests => 11; BEGIN { use_ok('Nagios::Plugin::Getopt') }; +# Needed to get evals to work in testing +Nagios::Plugin::Functions::_use_die(1); + my %PARAM = ( version => '0.01', usage => "Don't use this plugin!", diff --git a/t/check_stuff.t b/t/check_stuff.t index 2d2ce38..64f95ae 100755 --- a/t/check_stuff.t +++ b/t/check_stuff.t @@ -2,7 +2,7 @@ # use strict; use warnings; #use Test::More qw(no_plan); -use Test::More tests => 16; +use Test::More tests => 14; my ($r,$args); my $s = 't/check_stuff.pl'; @@ -22,23 +22,13 @@ $r = `$s`; is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with no args"; like $r, qr/^$n UNKNOWN/, "UNKNOWN with no args"; +$r = `$s -V`; +is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with -V arg"; +like $r, qr/^[\w\.]+ \d+/i, "looks like there's a version"; -#TODO: -SKIP: { - local $TODO = q~d'oh! we'll have to redirect STDERR and check it with like() here instead of checking `` which only gets STDIN. Maybe use IPC::Open3?~; - skip "too noisy, see TODO here", 6; - - $r = `$s -V`; - is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with -V arg"; - like $r, qr/\d+\.\d/i, "looks like there's a version"; # broken - is $r, '', "prints nothing to STDOUT"; - - $r = `$s -h`; - is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with -h arg"; - like $r, qr/usage/i, "looks like there's something helpful"; # broken - is $r, '', "prints nothing to STDOUT"; -} - +$r = `$s -h`; +is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with -h arg"; +like $r, qr/usage/i, "looks like there's something helpful"; # broken $args = " -r 99 "; diag "running `$s $args`" if $ENV{TEST_VERBOSE}; -- cgit v0.10-9-g596f