Fix nagios_die wrapper not properly passing last argument to nagios_exit
[Nagios-Plugin.git] / t / check_stuff.pl
blob112a18a29646bb238c68e6c54f76e909f428dbf1
1 #!/usr/local/bin/perl
3 ### check_stuff.pl
5 # an example Nagios plugin using the Nagios::Plugin modules.
7 # Originally by Nathan Vonnahme, n8v at users dot sourceforge
8 # dot net, July 19 2006
10 # Please modify to your heart's content and use as the basis for all
11 # the really cool Nagios monitoring scripts you're going to create.
12 # You rock.
14 ##############################################################################
15 # prologue
16 use strict;
17 use warnings;
19 use Nagios::Plugin ;
21 use vars qw($VERSION $PROGNAME $verbose $warn $critical $timeout $result);
22 $VERSION = '1.0';
24 # get the base name of this script for use in the examples
25 use File::Basename;
26 $PROGNAME = basename($0);
29 ##############################################################################
30 # define and get the command line options.
31 # see the command line option guidelines at
32 # http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOPTIONS
35 # Instantiate Nagios::Plugin object (the 'usage' parameter is mandatory)
36 my $p = Nagios::Plugin->new(
37 usage => "Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>]
38 [ -c|--critical=<critical threshold> ]
39 [ -w|--warning=<warning threshold> ]
40 [ -r|--result = <INTEGER> ]",
41 version => $VERSION,
42 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.',
44 extra => "
46 THRESHOLDs for -w and -c are specified 'min:max' or 'min:' or ':max'
47 (or 'max'). If specified '\@min:max', a warning status will be generated
48 if the count *is* inside the specified range.
50 See more threshold examples at http
51 : // nagiosplug
52 . sourceforge
53 . net / developer-guidelines
54 . html #THRESHOLDFORMAT
56 Examples:
58 $PROGNAME -w 10 -c 18 Returns a warning
59 if the resulting number is greater than 10,
60 or a critical error
61 if it is greater than 18.
63 $PROGNAME -w 10 : -c 4 : Returns a warning
64 if the resulting number is less than 10,
65 or a critical error
66 if it is less than 4.
72 # Define and document the valid command line options
73 # usage, help, version, timeout and verbose are defined by default.
75 $p->add_arg(
76 spec => 'warning|w=s',
78 help =>
79 qq{-w, --warning=INTEGER:INTEGER
80 Minimum and maximum number of allowable result, outside of which a
81 warning will be generated. If omitted, no warning is generated.},
83 # required => 1,
84 # default => 10,
87 $p->add_arg(
88 spec => 'critical|c=s',
89 help =>
90 qq{-c, --critical=INTEGER:INTEGER
91 Minimum and maximum number of the generated result, outside of
92 which a critical will be generated. },
95 $p->add_arg(
96 spec => 'result|r=f',
97 help =>
98 qq{-r, --result=INTEGER
99 Specify the result on the command line rather than generating a
100 random number. For testing.},
103 # Parse arguments and process standard ones (e.g. usage, help, version)
104 $p->getopts;
107 # perform sanity checking on command line options
108 if ( (defined $p->opts->result) && ($p->opts->result < 0 || $p->opts->result > 20) ) {
109 $p->nagios_die( " invalid number supplied for the -r option " );
112 unless ( defined $p->opts->warning || defined $p->opts->critical ) {
113 $p->nagios_die( " you didn't supply a threshold argument " );
118 ##############################################################################
119 # check stuff.
121 # THIS is where you'd do your actual checking to get a real value for $result
122 # don't forget to timeout after $p->opts->timeout seconds, if applicable.
123 my $result;
124 if (defined $p->opts->result) { # you got a 'result' option from the command line options
125 $result = $p->opts->result;
126 print " using supplied result $result from command line \n
127 " if $p->opts->verbose;
129 else {
130 $result = int rand(20)+1;
131 print " generated random result $result\n " if $p->opts->verbose;
135 ##############################################################################
136 # check the result against the defined warning and critical thresholds,
137 # output the result and exit
138 $p->nagios_exit(
139 return_code => $p->check_threshold($result),
140 message => " sample result was $result"