diff options
Diffstat (limited to 't/check_stuff.pl')
| -rwxr-xr-x | t/check_stuff.pl | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/t/check_stuff.pl b/t/check_stuff.pl new file mode 100755 index 0000000..51f551f --- /dev/null +++ b/t/check_stuff.pl | |||
| @@ -0,0 +1,168 @@ | |||
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | ### check_stuff.pl | ||
| 4 | |||
| 5 | # an example Nagios plugin using the Nagios::Plugin modules. | ||
| 6 | |||
| 7 | # Originally by Nathan Vonnahme, n8v at users dot sourceforge | ||
| 8 | # dot net, July 19 2006 | ||
| 9 | |||
| 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. | ||
| 13 | |||
| 14 | # $Id$ | ||
| 15 | |||
| 16 | ############################################################################## | ||
| 17 | # prologue | ||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | use Nagios::Plugin qw(%ERRORS); | ||
| 22 | |||
| 23 | use Nagios::Plugin::Getopt; | ||
| 24 | |||
| 25 | |||
| 26 | use vars qw($VERSION $PROGNAME $verbose $warn $critical $timeout $result); | ||
| 27 | '$Revision$' =~ /^.*(\d+.\d+) \$$/; # Use The Revision from RCS/CVS/Subversion | ||
| 28 | $VERSION = $1; | ||
| 29 | $0 =~ m!^.*/([^/]+)$!; | ||
| 30 | $PROGNAME = $1; | ||
| 31 | |||
| 32 | # shortname is the identifier this script will give to Nagios. | ||
| 33 | # it's set here to the uppercase program name with file extension removed, | ||
| 34 | # e.g. check_stuff.pl -> CHECK_STUFF | ||
| 35 | my $short_name = uc $PROGNAME; | ||
| 36 | $short_name =~ s/\.\w+$//; | ||
| 37 | |||
| 38 | |||
| 39 | ############################################################################## | ||
| 40 | # define and get the command line options. | ||
| 41 | # see the command line option guidelines at | ||
| 42 | # | ||
| 43 | |||
| 44 | |||
| 45 | # Instantiate Nagios::Plugin::Getopt object (usage and version are mandatory) | ||
| 46 | my $nagopts = Nagios::Plugin::Getopt->new( | ||
| 47 | usage => "Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>] | ||
| 48 | [ -c|--critical=<critical threshold> ] | ||
| 49 | [ -w|--warning=<warning threshold> ] | ||
| 50 | [ -r|--result = <INTEGER> ]", | ||
| 51 | 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.', | ||
| 53 | |||
| 54 | extra => qq{ | ||
| 55 | |||
| 56 | THRESHOLDs 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 | ||
| 58 | if the count *is* inside the specified range. | ||
| 59 | |||
| 60 | See more threshold examples at | ||
| 61 | http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT | ||
| 62 | |||
| 63 | Examples: | ||
| 64 | |||
| 65 | $PROGNAME -w 10 -c 18 | ||
| 66 | Returns a warning if the resulting number is greater than 10, or a | ||
| 67 | critical error if it is greater than 18. | ||
| 68 | |||
| 69 | $PROGNAME -w 10: -c 4: | ||
| 70 | Returns a warning if the resulting number is less than 10, or a | ||
| 71 | critical error if it is less than 4. | ||
| 72 | |||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | ); | ||
| 77 | |||
| 78 | |||
| 79 | # Define and document the valid command line options | ||
| 80 | # usage, help, version, timeout and verbose are defined by default. | ||
| 81 | |||
| 82 | $nagopts->arg( | ||
| 83 | spec => 'warning|w=s', | ||
| 84 | |||
| 85 | help => | ||
| 86 | qq{-w, --warning=INTEGER:INTEGER | ||
| 87 | Minimum and maximum number of allowable result, outside of which a | ||
| 88 | warning will be generated. If omitted, no warning is generated.}, | ||
| 89 | |||
| 90 | # required => 1, | ||
| 91 | # default => 10, | ||
| 92 | ); | ||
| 93 | |||
| 94 | $nagopts->arg( | ||
| 95 | spec => 'critical|c=s', | ||
| 96 | help => | ||
| 97 | qq{-c, --critical=INTEGER:INTEGER | ||
| 98 | Minimum and maximum number of the generated result, outside of | ||
| 99 | which a critical will be generated. If omitted, a critical is | ||
| 100 | generated if no processes are running.}, | ||
| 101 | |||
| 102 | ); | ||
| 103 | |||
| 104 | $nagopts->arg( | ||
| 105 | spec => 'result|r=f', | ||
| 106 | help => | ||
| 107 | qq{-r, --result=INTEGER | ||
| 108 | Specify the result on the command line rather than generating a | ||
| 109 | random number. For testing.}, | ||
| 110 | ); | ||
| 111 | |||
| 112 | # Parse arguments and process standard ones (e.g. usage, help, version) | ||
| 113 | $nagopts->getopts; | ||
| 114 | |||
| 115 | |||
| 116 | my $p = Nagios::Plugin->new; | ||
| 117 | |||
| 118 | $p->shortname($short_name); | ||
| 119 | |||
| 120 | |||
| 121 | # sanity checking on command line options | ||
| 122 | if ( (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 | } | ||
| 128 | |||
| 129 | unless ( defined $nagopts->warning || defined $nagopts->critical ) { | ||
| 130 | $p->die( | ||
| 131 | return_code => $ERRORS{UNKNOWN}, | ||
| 132 | message => "you didn't supply a threshold argument" | ||
| 133 | ); | ||
| 134 | } | ||
| 135 | |||
| 136 | ############################################################################## | ||
| 137 | # define a Nagios::Threshold object based on the command line options | ||
| 138 | my $t = $p->set_thresholds( warning => $nagopts->warning, critical => $nagopts->critical ); | ||
| 139 | |||
| 140 | |||
| 141 | ############################################################################## | ||
| 142 | # check stuff. | ||
| 143 | |||
| 144 | # 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. | ||
| 146 | my $result; | ||
| 147 | if (defined $nagopts->result) { | ||
| 148 | $result = $nagopts->result; | ||
| 149 | print "using supplied result $result from command line\n" if $nagopts->verbose; | ||
| 150 | } | ||
| 151 | else { | ||
| 152 | $result = int rand(20)+1; | ||
| 153 | print "generated random result $result\n" if $nagopts->verbose; | ||
| 154 | } | ||
| 155 | |||
| 156 | print "status of result ($result) is ", $t->get_status($result), "\n" | ||
| 157 | if $nagopts->verbose; | ||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | ############################################################################## | ||
| 163 | # output the result and exit | ||
| 164 | $p->die( | ||
| 165 | return_code => $t->get_status($result), | ||
| 166 | message => "sample result was $result" | ||
| 167 | ); | ||
| 168 | |||
