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.
14 ##############################################################################
21 use vars
qw($VERSION $PROGNAME $verbose $warn $critical $timeout $result);
24 # get the base name of this script for use in the examples
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> ]",
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.',
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
53 . net / developer-guidelines
54 . html #THRESHOLDFORMAT
58 $PROGNAME -w 10 -c 18 Returns a warning
59 if the resulting number is greater than 10,
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,
72 # Define and document the valid command line options
73 # usage, help, version, timeout and verbose are defined by default.
76 spec => 'warning|w=s',
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.},
88 spec => 'critical|c=s',
90 qq{-c, --critical=INTEGER:INTEGER
91 Minimum and maximum number of the generated result, outside of
92 which a critical will be generated. },
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)
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 ##############################################################################
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.
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;
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
139 return_code => $p->check_threshold($result),
140 message => " sample result was $result"