summaryrefslogtreecommitdiffstats
path: root/t/check_stuff.pl
diff options
context:
space:
mode:
Diffstat (limited to 't/check_stuff.pl')
-rwxr-xr-xt/check_stuff.pl83
1 files changed, 27 insertions, 56 deletions
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