summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
Diffstat (limited to 't')
-rw-r--r--t/Nagios-Plugin-01.t14
-rw-r--r--t/Nagios-Plugin-02.t4
-rw-r--r--t/Nagios-Plugin-03.t26
-rwxr-xr-xt/check_stuff.pl83
-rwxr-xr-xt/check_stuff.t2
5 files changed, 53 insertions, 76 deletions
diff --git a/t/Nagios-Plugin-01.t b/t/Nagios-Plugin-01.t
index 9de5009..a73fce4 100644
--- a/t/Nagios-Plugin-01.t
+++ b/t/Nagios-Plugin-01.t
@@ -11,21 +11,26 @@ Nagios::Plugin::Functions::_fake_exit(1);
11diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n" 11diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"
12 if $ENV{TEST_VERBOSE}; 12 if $ENV{TEST_VERBOSE};
13 13
14my $p = Nagios::Plugin->new; 14my $p = Nagios::Plugin->new (usage => "dummy usage");
15isa_ok( $p, "Nagios::Plugin"); 15isa_ok( $p, "Nagios::Plugin");
16 16
17$p->shortname("PAGESIZE"); 17$p->shortname("PAGESIZE");
18is($p->shortname, "PAGESIZE", "shortname set correctly"); 18is($p->shortname, "PAGESIZE", "shortname explicitly set correctly");
19 19
20$p = Nagios::Plugin->new; 20$p = Nagios::Plugin->new (usage => "dummy usage");
21is($p->shortname, "NAGIOS-PLUGIN-01", "shortname should default on new"); 21is($p->shortname, "NAGIOS-PLUGIN-01", "shortname should default on new");
22 22
23$p = Nagios::Plugin->new( shortname => "SIZE" ); 23$p = Nagios::Plugin->new( shortname => "SIZE", usage => "dummy usage" );
24is($p->shortname, "SIZE", "shortname set correctly on new"); 24is($p->shortname, "SIZE", "shortname set correctly on new");
25 25
26diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE}; 26diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE};
27my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" ); 27my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" );
28 28
29use Data::Dumper;
30#diag "dumping p: ". Dumper $p;
31#diag "dumping perfdata: ". Dumper $p->perfdata;
32
33
29$p->add_perfdata( 34$p->add_perfdata(
30 label => "size", 35 label => "size",
31 value => 1, 36 value => 1,
@@ -34,6 +39,7 @@ $p->add_perfdata(
34 ); 39 );
35 40
36cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct"); 41cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct");
42#diag "dumping perfdata: ". Dumper ($p->perfdata);
37 43
38my $expected = {qw( 44my $expected = {qw(
39 -1 WARNING 45 -1 WARNING
diff --git a/t/Nagios-Plugin-02.t b/t/Nagios-Plugin-02.t
index 360e180..15ae3d6 100644
--- a/t/Nagios-Plugin-02.t
+++ b/t/Nagios-Plugin-02.t
@@ -16,7 +16,7 @@ is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}");
16is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); 16is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}");
17 17
18my $plugin = 'TEST_PLUGIN'; 18my $plugin = 'TEST_PLUGIN';
19my $np = Nagios::Plugin->new( shortname => $plugin ); 19my $np = Nagios::Plugin->new( shortname => $plugin, usage => "dummy usage" );
20is($np->shortname, $plugin, "shortname() is $plugin"); 20is($np->shortname, $plugin, "shortname() is $plugin");
21 21
22# Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg ) 22# Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg )
@@ -151,7 +151,7 @@ for (@ok) {
151# shortname testing 151# shortname testing
152SKIP: { 152SKIP: {
153 skip "requires File::Basename", 2 unless eval { require File::Basename }; 153 skip "requires File::Basename", 2 unless eval { require File::Basename };
154 $np = Nagios::Plugin->new; 154 $np = Nagios::Plugin->new (usage => "dummy usage", version => "1");
155 $plugin = uc File::Basename::basename($0); 155 $plugin = uc File::Basename::basename($0);
156 $plugin =~ s/\..*$//; 156 $plugin =~ s/\..*$//;
157 is($np->shortname, $plugin, "shortname() is '$plugin'"); 157 is($np->shortname, $plugin, "shortname() is '$plugin'");
diff --git a/t/Nagios-Plugin-03.t b/t/Nagios-Plugin-03.t
index 0366156..0b7b8af 100644
--- a/t/Nagios-Plugin-03.t
+++ b/t/Nagios-Plugin-03.t
@@ -10,7 +10,7 @@ BEGIN {
10Nagios::Plugin::Functions::_fake_exit(1); 10Nagios::Plugin::Functions::_fake_exit(1);
11 11
12my $plugin = 'NP_CHECK_MESSAGES_03'; 12my $plugin = 'NP_CHECK_MESSAGES_03';
13my $np = Nagios::Plugin->new( shortname => $plugin ); 13my $np = Nagios::Plugin->new( shortname => $plugin, usage => "dummy usage" );
14is($np->shortname, $plugin, "shortname() is $plugin"); 14is($np->shortname, $plugin, "shortname() is $plugin");
15 15
16my ($code, $message); 16my ($code, $message);
@@ -172,33 +172,33 @@ is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $mess
172# add_messages 172# add_messages
173 173
174# Constant codes 174# Constant codes
175$np = Nagios::Plugin->new; 175$np = Nagios::Plugin->new (usage => "dummy usage");
176$np->add_message( CRITICAL, "A B C" ); 176$np->add_message( CRITICAL, "A B C" );
177$np->add_message( WARNING, "D E F" ); 177$np->add_message( WARNING, "D E F" );
178($code, $message) = $np->check_messages(); 178($code, $message) = $np->check_messages();
179is($code, CRITICAL, "(CRITICAL, WARNING) code is $STATUS_TEXT{$code}"); 179is($code, CRITICAL, "(CRITICAL, WARNING) code is $STATUS_TEXT{$code}");
180is($message, $messages{critical}, "(CRITICAL, WARNING) message is $message"); 180is($message, $messages{critical}, "(CRITICAL, WARNING) message is $message");
181 181
182$np = Nagios::Plugin->new; 182$np = Nagios::Plugin->new (usage => "dummy usage");
183$np->add_message( CRITICAL, "A B C" ); 183$np->add_message( CRITICAL, "A B C" );
184($code, $message) = $np->check_messages(); 184($code, $message) = $np->check_messages();
185is($code, CRITICAL, "(CRITICAL) code is $STATUS_TEXT{$code}"); 185is($code, CRITICAL, "(CRITICAL) code is $STATUS_TEXT{$code}");
186is($message, $messages{critical}, "(CRITICAL) message is $message"); 186is($message, $messages{critical}, "(CRITICAL) message is $message");
187 187
188$np = Nagios::Plugin->new; 188$np = Nagios::Plugin->new (usage => "dummy usage");
189$np->add_message( WARNING, "D E F" ); 189$np->add_message( WARNING, "D E F" );
190($code, $message) = $np->check_messages(); 190($code, $message) = $np->check_messages();
191is($code, WARNING, "(WARNING) code is $STATUS_TEXT{$code}"); 191is($code, WARNING, "(WARNING) code is $STATUS_TEXT{$code}");
192is($message, $messages{warning}, "(WARNING) message is $message"); 192is($message, $messages{warning}, "(WARNING) message is $message");
193 193
194$np = Nagios::Plugin->new; 194$np = Nagios::Plugin->new (usage => "dummy usage");
195$np->add_message( WARNING, "D E F" ); 195$np->add_message( WARNING, "D E F" );
196$np->add_message( OK, "G H I" ); 196$np->add_message( OK, "G H I" );
197($code, $message) = $np->check_messages(); 197($code, $message) = $np->check_messages();
198is($code, WARNING, "(WARNING, OK) code is $STATUS_TEXT{$code}"); 198is($code, WARNING, "(WARNING, OK) code is $STATUS_TEXT{$code}");
199is($message, $messages{warning}, "(WARNING, OK) message is $message"); 199is($message, $messages{warning}, "(WARNING, OK) message is $message");
200 200
201$np = Nagios::Plugin->new; 201$np = Nagios::Plugin->new (usage => "dummy usage");
202$np->add_message( OK, "G H I" ); 202$np->add_message( OK, "G H I" );
203($code, $message) = $np->check_messages(); 203($code, $message) = $np->check_messages();
204is($code, OK, "(OK) code is $STATUS_TEXT{$code}"); 204is($code, OK, "(OK) code is $STATUS_TEXT{$code}");
@@ -206,33 +206,33 @@ is($message, $messages{ok}, "(OK) message is $message");
206 206
207 207
208# String codes 208# String codes
209$np = Nagios::Plugin->new; 209$np = Nagios::Plugin->new (usage => "dummy usage");
210$np->add_message( critical => "A B C" ); 210$np->add_message( critical => "A B C" );
211$np->add_message( warning => "D E F" ); 211$np->add_message( warning => "D E F" );
212($code, $message) = $np->check_messages(); 212($code, $message) = $np->check_messages();
213is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); 213is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
214is($message, $messages{critical}, "(critical, warning) message is $message"); 214is($message, $messages{critical}, "(critical, warning) message is $message");
215 215
216$np = Nagios::Plugin->new; 216$np = Nagios::Plugin->new (usage => "dummy usage");
217$np->add_message( critical => "A B C" ); 217$np->add_message( critical => "A B C" );
218($code, $message) = $np->check_messages(); 218($code, $message) = $np->check_messages();
219is($code, CRITICAL, "(critical) code is $STATUS_TEXT{$code}"); 219is($code, CRITICAL, "(critical) code is $STATUS_TEXT{$code}");
220is($message, $messages{critical}, "(critical) message is $message"); 220is($message, $messages{critical}, "(critical) message is $message");
221 221
222$np = Nagios::Plugin->new; 222$np = Nagios::Plugin->new (usage => "dummy usage");
223$np->add_message( warning => "D E F" ); 223$np->add_message( warning => "D E F" );
224($code, $message) = $np->check_messages(); 224($code, $message) = $np->check_messages();
225is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); 225is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
226is($message, $messages{warning}, "(warning) message is $message"); 226is($message, $messages{warning}, "(warning) message is $message");
227 227
228$np = Nagios::Plugin->new; 228$np = Nagios::Plugin->new (usage => "dummy usage");
229$np->add_message( warning => "D E F" ); 229$np->add_message( warning => "D E F" );
230$np->add_message( ok => "G H I" ); 230$np->add_message( ok => "G H I" );
231($code, $message) = $np->check_messages(); 231($code, $message) = $np->check_messages();
232is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); 232is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
233is($message, $messages{warning}, "(warning, ok) message is $message"); 233is($message, $messages{warning}, "(warning, ok) message is $message");
234 234
235$np = Nagios::Plugin->new; 235$np = Nagios::Plugin->new (usage => "dummy usage");
236$np->add_message( ok => "G H I" ); 236$np->add_message( ok => "G H I" );
237($code, $message) = $np->check_messages(); 237($code, $message) = $np->check_messages();
238is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); 238is($code, OK, "(ok) code is $STATUS_TEXT{$code}");
@@ -240,7 +240,7 @@ is($message, $messages{ok}, "(ok) message is $message");
240 240
241 241
242# No add_message 242# No add_message
243$np = Nagios::Plugin->new; 243$np = Nagios::Plugin->new (usage => "dummy usage");
244($code, $message) = $np->check_messages(); 244($code, $message) = $np->check_messages();
245is($code, OK, "() code is $STATUS_TEXT{$code}"); 245is($code, OK, "() code is $STATUS_TEXT{$code}");
246is($message, '', "() message is ''"); 246is($message, '', "() message is ''");
@@ -250,7 +250,7 @@ is($message, '', "() message is ''");
250# Error conditions 250# Error conditions
251 251
252# add_message errors 252# add_message errors
253$np = Nagios::Plugin->new; 253$np = Nagios::Plugin->new (usage => "dummy usage");
254ok(! defined eval { $np->add_message( foobar => 'hi mum' ) }, 254ok(! defined eval { $np->add_message( foobar => 'hi mum' ) },
255 'add_message dies on invalid code'); 255 'add_message dies on invalid code');
256ok(! defined eval { $np->add_message( OKAY => 'hi mum' ) }, 256ok(! defined eval { $np->add_message( OKAY => 'hi mum' ) },
diff --git a/t/check_stuff.pl b/t/check_stuff.pl
index 51f551f..889e484 100755
--- a/t/check_stuff.pl
+++ b/t/check_stuff.pl
@@ -18,32 +18,25 @@
18use strict; 18use strict;
19use warnings; 19use warnings;
20 20
21use Nagios::Plugin qw(%ERRORS); 21use Nagios::Plugin ;
22
23use Nagios::Plugin::Getopt;
24
25 22
26use vars qw($VERSION $PROGNAME $verbose $warn $critical $timeout $result); 23use vars qw($VERSION $PROGNAME $verbose $warn $critical $timeout $result);
27'$Revision$' =~ /^.*(\d+.\d+) \$$/; # Use The Revision from RCS/CVS/Subversion 24'$Revision$' =~ /^.*(\d+.\d+) \$$/; # Use The Revision from RCS/CVS/Subversion
28$VERSION = $1; 25$VERSION = $1;
29$0 =~ m!^.*/([^/]+)$!;
30$PROGNAME = $1;
31 26
32# shortname is the identifier this script will give to Nagios. 27# get the base name of this script for use in the examples
33# it's set here to the uppercase program name with file extension removed, 28use File::Basename;
34# e.g. check_stuff.pl -> CHECK_STUFF 29$PROGNAME = basename($0);
35my $short_name = uc $PROGNAME;
36$short_name =~ s/\.\w+$//;
37 30
38 31
39############################################################################## 32##############################################################################
40# define and get the command line options. 33# define and get the command line options.
41# see the command line option guidelines at 34# see the command line option guidelines at
42# 35# http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOPTIONS
43 36
44 37
45# Instantiate Nagios::Plugin::Getopt object (usage and version are mandatory) 38# Instantiate Nagios::Plugin object (the 'usage' parameter is mandatory)
46my $nagopts = Nagios::Plugin::Getopt->new( 39my $p = Nagios::Plugin->new(
47 usage => "Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>] 40 usage => "Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>]
48 [ -c|--critical=<critical threshold> ] 41 [ -c|--critical=<critical threshold> ]
49 [ -w|--warning=<warning threshold> ] 42 [ -w|--warning=<warning threshold> ]
@@ -51,7 +44,7 @@ my $nagopts = Nagios::Plugin::Getopt->new(
51 version => $VERSION, 44 version => $VERSION,
52 blurb => 'This plugin is an example of a Nagios plugin written in Perl using the Nagios::Plugin modules. It will generate a random integer between 1 and 20 (though you can specify the number with the -n option for testing), and will output OK, WARNING or CRITICAL if the resulting number is outside the specified thresholds.', 45 blurb => 'This plugin is an example of a Nagios plugin written in Perl using the Nagios::Plugin modules. It will generate a random integer between 1 and 20 (though you can specify the number with the -n option for testing), and will output OK, WARNING or CRITICAL if the resulting number is outside the specified thresholds.',
53 46
54 extra => qq{ 47 extra => "
55 48
56THRESHOLDs for -w and -c are specified 'min:max' or 'min:' or ':max' 49THRESHOLDs for -w and -c are specified 'min:max' or 'min:' or ':max'
57(or 'max'). If specified '\@min:max', a warning status will be generated 50(or 'max'). If specified '\@min:max', a warning status will be generated
@@ -70,16 +63,14 @@ Examples:
70 Returns a warning if the resulting number is less than 10, or a 63 Returns a warning if the resulting number is less than 10, or a
71 critical error if it is less than 4. 64 critical error if it is less than 4.
72 65
73 66"
74}
75
76); 67);
77 68
78 69
79# Define and document the valid command line options 70# Define and document the valid command line options
80# usage, help, version, timeout and verbose are defined by default. 71# usage, help, version, timeout and verbose are defined by default.
81 72
82$nagopts->arg( 73$p->arg(
83 spec => 'warning|w=s', 74 spec => 'warning|w=s',
84 75
85 help => 76 help =>
@@ -91,17 +82,15 @@ qq{-w, --warning=INTEGER:INTEGER
91# default => 10, 82# default => 10,
92); 83);
93 84
94$nagopts->arg( 85$p->arg(
95 spec => 'critical|c=s', 86 spec => 'critical|c=s',
96 help => 87 help =>
97qq{-c, --critical=INTEGER:INTEGER 88qq{-c, --critical=INTEGER:INTEGER
98 Minimum and maximum number of the generated result, outside of 89 Minimum and maximum number of the generated result, outside of
99 which a critical will be generated. If omitted, a critical is 90 which a critical will be generated. },
100 generated if no processes are running.},
101
102); 91);
103 92
104$nagopts->arg( 93$p->arg(
105 spec => 'result|r=f', 94 spec => 'result|r=f',
106 help => 95 help =>
107qq{-r, --result=INTEGER 96qq{-r, --result=INTEGER
@@ -110,59 +99,41 @@ qq{-r, --result=INTEGER
110); 99);
111 100
112# Parse arguments and process standard ones (e.g. usage, help, version) 101# Parse arguments and process standard ones (e.g. usage, help, version)
113$nagopts->getopts; 102$p->getopts;
114
115 103
116my $p = Nagios::Plugin->new;
117 104
118$p->shortname($short_name); 105# perform sanity checking on command line options
119 106if ( (defined $p->opts->result) && ($p->opts->result < 0 || $p->opts->result > 20) ) {
120 107 $p->nagios_die( "invalid number supplied for the -r option" );
121# sanity checking on command line options
122if ( (defined $nagopts->result) && ($nagopts->result < 0 || $nagopts->result > 20) ) {
123 $p->die(
124 return_code => $ERRORS{UNKNOWN},
125 message => 'invalid number supplied for the -r option'
126 );
127} 108}
128 109
129unless ( defined $nagopts->warning || defined $nagopts->critical ) { 110unless ( defined $p->opts->warning || defined $p->opts->critical ) {
130 $p->die( 111 $p->nagios_die( "you didn't supply a threshold argument" );
131 return_code => $ERRORS{UNKNOWN},
132 message => "you didn't supply a threshold argument"
133 );
134} 112}
135 113
136##############################################################################
137# define a Nagios::Threshold object based on the command line options
138my $t = $p->set_thresholds( warning => $nagopts->warning, critical => $nagopts->critical );
139 114
140 115
141############################################################################## 116##############################################################################
142# check stuff. 117# check stuff.
143 118
144# THIS is where you'd do your actual checking to get a real value for $result 119# THIS is where you'd do your actual checking to get a real value for $result
145# don't forget to timeout after $nagopts->timeout seconds, if applicable. 120# don't forget to timeout after $p->opts->timeout seconds, if applicable.
146my $result; 121my $result;
147if (defined $nagopts->result) { 122if (defined $p->opts->result) { # you got a 'result' option from the command line options
148 $result = $nagopts->result; 123 $result = $p->opts->result;
149 print "using supplied result $result from command line\n" if $nagopts->verbose; 124 print "using supplied result $result from command line\n" if $p->opts->verbose;
150} 125}
151else { 126else {
152 $result = int rand(20)+1; 127 $result = int rand(20)+1;
153 print "generated random result $result\n" if $nagopts->verbose; 128 print "generated random result $result\n" if $p->opts->verbose;
154} 129}
155 130
156print "status of result ($result) is ", $t->get_status($result), "\n"
157 if $nagopts->verbose;
158
159
160
161 131
162############################################################################## 132##############################################################################
133# check the result against the defined warning and critical thresholds,
163# output the result and exit 134# output the result and exit
164$p->die( 135$p->nagios_exit(
165 return_code => $t->get_status($result), 136 return_code => $p->check_threshold($result),
166 message => "sample result was $result" 137 message => "sample result was $result"
167); 138);
168 139
diff --git a/t/check_stuff.t b/t/check_stuff.t
index a748605..2d2ce38 100755
--- a/t/check_stuff.t
+++ b/t/check_stuff.t
@@ -8,7 +8,7 @@ my ($r,$args);
8my $s = 't/check_stuff.pl'; 8my $s = 't/check_stuff.pl';
9$s = 'perl -Ilib '.$s; 9$s = 'perl -Ilib '.$s;
10 10
11my $n = 'CHECK_STUFF'; 11my $n = 'STUFF';
12 12
13# Nagios status strings and exit codes 13# Nagios status strings and exit codes
14my %e = qw( 14my %e = qw(