summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Changes3
-rw-r--r--lib/Nagios/Plugin/Functions.pm26
-rw-r--r--lib/Nagios/Plugin/Getopt.pm4
-rw-r--r--t/Nagios-Plugin-Getopt-01.t3
-rw-r--r--t/Nagios-Plugin-Getopt-02.t3
-rw-r--r--t/Nagios-Plugin-Getopt-04.t3
-rwxr-xr-xt/check_stuff.t24
7 files changed, 38 insertions, 28 deletions
diff --git a/Changes b/Changes
index cc6d5f5..c06932f 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
1Revision history for Perl module Nagios::Plugin. 1Revision history for Perl module Nagios::Plugin.
2 2
30.21 ??
4 - Help, usage and version output now goes to stdout, not stderr
5
30.20 5th September 2007 60.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;
8use strict; 8use strict;
9use warnings; 9use warnings;
10use File::Basename; 10use File::Basename;
11use Params::Validate qw(validate :types); 11use Params::Validate qw(:types validate);
12use Math::Calc::Units; 12use 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); 125sub _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;
5use Test::More tests => 76; 5use Test::More tests => 76;
6BEGIN { use_ok('Nagios::Plugin::Getopt') }; 6BEGIN { use_ok('Nagios::Plugin::Getopt') };
7 7
8# Needed to get evals to work in testing
9Nagios::Plugin::Functions::_use_die(1);
10
8my %PARAM = ( 11my %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;
5use Test::More tests => 14; 5use Test::More tests => 14;
6BEGIN { use_ok('Nagios::Plugin::Getopt') }; 6BEGIN { use_ok('Nagios::Plugin::Getopt') };
7 7
8# Needed to get evals to work in testing
9Nagios::Plugin::Functions::_use_die(1);
10
8my %PARAM = ( 11my %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;
5use Test::More tests => 11; 5use Test::More tests => 11;
6BEGIN { use_ok('Nagios::Plugin::Getopt') }; 6BEGIN { use_ok('Nagios::Plugin::Getopt') };
7 7
8# Needed to get evals to work in testing
9Nagios::Plugin::Functions::_use_die(1);
10
8my %PARAM = ( 11my %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#
3use strict; use warnings; 3use strict; use warnings;
4#use Test::More qw(no_plan); 4#use Test::More qw(no_plan);
5use Test::More tests => 16; 5use Test::More tests => 14;
6 6
7my ($r,$args); 7my ($r,$args);
8my $s = 't/check_stuff.pl'; 8my $s = 't/check_stuff.pl';
@@ -22,23 +22,13 @@ $r = `$s`;
22is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with no args"; 22is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with no args";
23like $r, qr/^$n UNKNOWN/, "UNKNOWN with no args"; 23like $r, qr/^$n UNKNOWN/, "UNKNOWN with no args";
24 24
25$r = `$s -V`;
26is $?>>8 , $e{UNKNOWN}, "exits($e{UNKNOWN}) with -V arg";
27like $r, qr/^[\w\.]+ \d+/i, "looks like there's a version";
25 28
26#TODO: 29$r = `$s -h`;
27SKIP: { 30is $?>>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?~; 31like $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 ";
44diag "running `$s $args`" if $ENV{TEST_VERBOSE}; 34diag "running `$s $args`" if $ENV{TEST_VERBOSE};