diff options
-rw-r--r-- | Changes | 3 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Functions.pm | 26 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Getopt.pm | 4 | ||||
-rw-r--r-- | t/Nagios-Plugin-Getopt-01.t | 3 | ||||
-rw-r--r-- | t/Nagios-Plugin-Getopt-02.t | 3 | ||||
-rw-r--r-- | t/Nagios-Plugin-Getopt-04.t | 3 | ||||
-rwxr-xr-x | t/check_stuff.t | 24 |
7 files changed, 38 insertions, 28 deletions
@@ -1,5 +1,8 @@ | |||
1 | Revision history for Perl module Nagios::Plugin. | 1 | Revision history for Perl module Nagios::Plugin. |
2 | 2 | ||
3 | 0.21 ?? | ||
4 | - Help, usage and version output now goes to stdout, not stderr | ||
5 | |||
3 | 0.20 5th September 2007 | 6 | 0.20 5th September 2007 |
4 | - Version bump because of CPAN permission problems | 7 | - Version bump because of CPAN permission problems |
5 | 8 | ||
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; | |||
8 | use strict; | 8 | use strict; |
9 | use warnings; | 9 | use warnings; |
10 | use File::Basename; | 10 | use File::Basename; |
11 | use Params::Validate qw(validate :types); | 11 | use Params::Validate qw(:types validate); |
12 | use Math::Calc::Units; | 12 | use Math::Calc::Units; |
13 | 13 | ||
14 | # Remember to update Nagios::Plugins as well | 14 | # Remember to update Nagios::Plugins as well |
@@ -119,14 +119,24 @@ sub nagios_exit { | |||
119 | return Nagios::Plugin::ExitResult->new($code, $output); | 119 | return Nagios::Plugin::ExitResult->new($code, $output); |
120 | } | 120 | } |
121 | 121 | ||
122 | # Print output and exit; die if called via nagios_die and flag set | 122 | _nagios_exit($code, $output); |
123 | if($_use_die && (caller(1))[3] =~ m/die/) { | 123 | } |
124 | $!=$code; | 124 | |
125 | die($output); | 125 | sub _nagios_exit { |
126 | } else { | 126 | my ($code, $output) = @_; |
127 | print $output; | 127 | # Print output and exit; die if flag set and called via a die in stack backtrace |
128 | exit $code; | 128 | if ($_use_die) { |
129 | for (my $i = 0;; $i++) { | ||
130 | @_ = caller($i); | ||
131 | last unless @_; | ||
132 | if ($_[3] =~ m/die/) { | ||
133 | $! = $code; | ||
134 | die($output); | ||
135 | } | ||
136 | } | ||
129 | } | 137 | } |
138 | print $output; | ||
139 | exit $code; | ||
130 | } | 140 | } |
131 | 141 | ||
132 | # nagios_die( $message, [ $code ]) OR nagios_die( $code, $message ) | 142 | # 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 | |||
60 | my $self = shift; | 60 | my $self = shift; |
61 | my ($msg) = @_; | 61 | my ($msg) = @_; |
62 | $msg .= "\n" unless substr($msg, -1) eq "\n"; | 62 | $msg .= "\n" unless substr($msg, -1) eq "\n"; |
63 | # Set errno to UNKNOWN for die return code | 63 | Nagios::Plugin::Functions::_nagios_exit(3, $msg); |
64 | local $! = 3; | ||
65 | die $msg; | ||
66 | } | 64 | } |
67 | 65 | ||
68 | # Return the given attribute, if set, including a final newline | 66 | # 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; | |||
5 | use Test::More tests => 76; | 5 | use Test::More tests => 76; |
6 | BEGIN { use_ok('Nagios::Plugin::Getopt') }; | 6 | BEGIN { use_ok('Nagios::Plugin::Getopt') }; |
7 | 7 | ||
8 | # Needed to get evals to work in testing | ||
9 | Nagios::Plugin::Functions::_use_die(1); | ||
10 | |||
8 | my %PARAM = ( | 11 | my %PARAM = ( |
9 | version => '0.01', | 12 | version => '0.01', |
10 | url => 'http://www.openfusion.com.au/labs/nagios/', | 13 | 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; | |||
5 | use Test::More tests => 14; | 5 | use Test::More tests => 14; |
6 | BEGIN { use_ok('Nagios::Plugin::Getopt') }; | 6 | BEGIN { use_ok('Nagios::Plugin::Getopt') }; |
7 | 7 | ||
8 | # Needed to get evals to work in testing | ||
9 | Nagios::Plugin::Functions::_use_die(1); | ||
10 | |||
8 | my %PARAM = ( | 11 | my %PARAM = ( |
9 | version => '0.01', | 12 | version => '0.01', |
10 | url => 'http://www.openfusion.com.au/labs/nagios/', | 13 | 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; | |||
5 | use Test::More tests => 11; | 5 | use Test::More tests => 11; |
6 | BEGIN { use_ok('Nagios::Plugin::Getopt') }; | 6 | BEGIN { use_ok('Nagios::Plugin::Getopt') }; |
7 | 7 | ||
8 | # Needed to get evals to work in testing | ||
9 | Nagios::Plugin::Functions::_use_die(1); | ||
10 | |||
8 | my %PARAM = ( | 11 | my %PARAM = ( |
9 | version => '0.01', | 12 | version => '0.01', |
10 | usage => "Don't use this plugin!", | 13 | 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 @@ | |||
2 | # | 2 | # |
3 | use strict; use warnings; | 3 | use strict; use warnings; |
4 | #use Test::More qw(no_plan); | 4 | #use Test::More qw(no_plan); |
5 | use Test::More tests => 16; | 5 | use Test::More tests => 14; |
6 | 6 | ||
7 | my ($r,$args); | 7 | my ($r,$args); |
8 | my $s = 't/check_stuff.pl'; | 8 | my $s = 't/check_stuff.pl'; |
@@ -22,23 +22,13 @@ $r = `$s`; | |||
22 | is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with no args"; | 22 | is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with no args"; |
23 | like $r, qr/^$n UNKNOWN/, "UNKNOWN with no args"; | 23 | like $r, qr/^$n UNKNOWN/, "UNKNOWN with no args"; |
24 | 24 | ||
25 | $r = `$s -V`; | ||
26 | is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with -V arg"; | ||
27 | like $r, qr/^[\w\.]+ \d+/i, "looks like there's a version"; | ||
25 | 28 | ||
26 | #TODO: | 29 | $r = `$s -h`; |
27 | SKIP: { | 30 | is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with -h arg"; |
28 | 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?~; | 31 | like $r, qr/usage/i, "looks like there's something helpful"; # broken |
29 | skip "too noisy, see TODO here", 6; | ||
30 | |||
31 | $r = `$s -V`; | ||
32 | is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with -V arg"; | ||
33 | like $r, qr/\d+\.\d/i, "looks like there's a version"; # broken | ||
34 | is $r, '', "prints nothing to STDOUT"; | ||
35 | |||
36 | $r = `$s -h`; | ||
37 | is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with -h arg"; | ||
38 | like $r, qr/usage/i, "looks like there's something helpful"; # broken | ||
39 | is $r, '', "prints nothing to STDOUT"; | ||
40 | } | ||
41 | |||
42 | 32 | ||
43 | $args = " -r 99 "; | 33 | $args = " -r 99 "; |
44 | diag "running `$s $args`" if $ENV{TEST_VERBOSE}; | 34 | diag "running `$s $args`" if $ENV{TEST_VERBOSE}; |