5 # an example plugin using the Monitoring::Plugin module.
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 monitoring scripts you're going to create.
14 ##############################################################################
19 use Monitoring
::Plugin
;
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 # https://www.monitoring-plugins.org/doc/guidelines.html#PLUGOPTIONS
35 # Instantiate Monitoring::Plugin object (the 'usage' parameter is mandatory)
36 my $p = Monitoring::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 monitoring plugin written in Perl using the Monitoring::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 https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT
54 $PROGNAME -w 10 -c 18 Returns a warning
55 if the resulting number is greater than 10,
57 if it is greater than 18.
59 $PROGNAME -w 10 : -c 4 : Returns a warning
60 if the resulting number is less than 10,
68 # Define and document the valid command line options
69 # usage, help, version, timeout and verbose are defined by default.
72 spec => 'warning|w=s',
75 qq{-w, --warning=INTEGER:INTEGER
76 Minimum and maximum number of allowable result, outside of which a
77 warning will be generated. If omitted, no warning is generated.},
84 spec => 'critical|c=s',
86 qq{-c, --critical=INTEGER:INTEGER
87 Minimum and maximum number of the generated result, outside of
88 which a critical will be generated. },
94 qq{-r, --result=INTEGER
95 Specify the result on the command line rather than generating a
96 random number. For testing.},
99 # Parse arguments and process standard ones (e.g. usage, help, version)
103 # perform sanity checking on command line options
104 if ( (defined $p->opts->result) && ($p->opts->result < 0 || $p->opts->result > 20) ) {
105 $p->plugin_die( " invalid number supplied for the -r option " );
108 unless ( defined $p->opts->warning || defined $p->opts->critical ) {
109 $p->plugin_die( " you didn't supply a threshold argument " );
114 ##############################################################################
117 # THIS is where you'd do your actual checking to get a real value for $result
118 # don't forget to timeout after $p->opts->timeout seconds, if applicable.
120 if (defined $p->opts->result) { # you got a 'result' option from the command line options
121 $result = $p->opts->result;
122 print " using supplied result $result from command line \n
123 " if $p->opts->verbose;
126 $result = int rand(20)+1;
127 print " generated random result $result\n " if $p->opts->verbose;
131 ##############################################################################
132 # check the result against the defined warning and critical thresholds,
133 # output the result and exit
135 return_code => $p->check_threshold($result),
136 message => " sample result was $result"