update changelog
[monitoring-plugin-perl.git] / t / check_stuff.pl
blobdca4e58cd2e91e31c6a8cbae1e985f5d59138c4f
1 #!/usr/local/bin/perl
3 ### check_stuff.pl
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.
12 # You rock.
14 ##############################################################################
15 # prologue
16 use strict;
17 use warnings;
19 use Monitoring::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 # 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> ]",
41 version => $VERSION,
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.',
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 https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT
52 Examples:
54 $PROGNAME -w 10 -c 18 Returns a warning
55 if the resulting number is greater than 10,
56 or a critical error
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,
61 or a critical error
62 if it is less than 4.
68 # Define and document the valid command line options
69 # usage, help, version, timeout and verbose are defined by default.
71 $p->add_arg(
72 spec => 'warning|w=s',
74 help =>
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.},
79 # required => 1,
80 # default => 10,
83 $p->add_arg(
84 spec => 'critical|c=s',
85 help =>
86 qq{-c, --critical=INTEGER:INTEGER
87 Minimum and maximum number of the generated result, outside of
88 which a critical will be generated. },
91 $p->add_arg(
92 spec => 'result|r=f',
93 help =>
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)
100 $p->getopts;
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 ##############################################################################
115 # check stuff.
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.
119 my $result;
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;
125 else {
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
134 $p->plugin_exit(
135 return_code => $p->check_threshold($result),
136 message => " sample result was $result"