From d84da4e2ec8569b8ff15356b6881df307c7333ca Mon Sep 17 00:00:00 2001 From: Nathan Vonnahme Date: Fri, 10 Nov 2006 01:26:16 +0000 Subject: * exposed Getopt and Threshold functionality from top level Nagios::Plugin * exchanged Class::Struct for Class::Accessor * POD is not updated yet. git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1536 f882894a-f735-0410-b71e-b25c423dba1c --- t/check_stuff.pl | 83 ++++++++++++++++++-------------------------------------- 1 file changed, 27 insertions(+), 56 deletions(-) (limited to 't/check_stuff.pl') diff --git a/t/check_stuff.pl b/t/check_stuff.pl index 51f551f..889e484 100755 --- a/t/check_stuff.pl +++ b/t/check_stuff.pl @@ -18,32 +18,25 @@ use strict; use warnings; -use Nagios::Plugin qw(%ERRORS); - -use Nagios::Plugin::Getopt; - +use Nagios::Plugin ; use vars qw($VERSION $PROGNAME $verbose $warn $critical $timeout $result); '$Revision$' =~ /^.*(\d+.\d+) \$$/; # Use The Revision from RCS/CVS/Subversion $VERSION = $1; -$0 =~ m!^.*/([^/]+)$!; -$PROGNAME = $1; -# shortname is the identifier this script will give to Nagios. -# it's set here to the uppercase program name with file extension removed, -# e.g. check_stuff.pl -> CHECK_STUFF -my $short_name = uc $PROGNAME; -$short_name =~ s/\.\w+$//; +# get the base name of this script for use in the examples +use File::Basename; +$PROGNAME = basename($0); ############################################################################## # define and get the command line options. # see the command line option guidelines at -# +# http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOPTIONS -# Instantiate Nagios::Plugin::Getopt object (usage and version are mandatory) -my $nagopts = Nagios::Plugin::Getopt->new( +# Instantiate Nagios::Plugin object (the 'usage' parameter is mandatory) +my $p = Nagios::Plugin->new( usage => "Usage: %s [ -v|--verbose ] [-H ] [-t ] [ -c|--critical= ] [ -w|--warning= ] @@ -51,7 +44,7 @@ my $nagopts = Nagios::Plugin::Getopt->new( version => $VERSION, 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.', - extra => qq{ + extra => " THRESHOLDs for -w and -c are specified 'min:max' or 'min:' or ':max' (or 'max'). If specified '\@min:max', a warning status will be generated @@ -70,16 +63,14 @@ Examples: Returns a warning if the resulting number is less than 10, or a critical error if it is less than 4. - -} - +" ); # Define and document the valid command line options # usage, help, version, timeout and verbose are defined by default. -$nagopts->arg( +$p->arg( spec => 'warning|w=s', help => @@ -91,17 +82,15 @@ qq{-w, --warning=INTEGER:INTEGER # default => 10, ); -$nagopts->arg( +$p->arg( spec => 'critical|c=s', help => qq{-c, --critical=INTEGER:INTEGER Minimum and maximum number of the generated result, outside of - which a critical will be generated. If omitted, a critical is - generated if no processes are running.}, - + which a critical will be generated. }, ); -$nagopts->arg( +$p->arg( spec => 'result|r=f', help => qq{-r, --result=INTEGER @@ -110,59 +99,41 @@ qq{-r, --result=INTEGER ); # Parse arguments and process standard ones (e.g. usage, help, version) -$nagopts->getopts; - +$p->getopts; -my $p = Nagios::Plugin->new; -$p->shortname($short_name); - - -# sanity checking on command line options -if ( (defined $nagopts->result) && ($nagopts->result < 0 || $nagopts->result > 20) ) { - $p->die( - return_code => $ERRORS{UNKNOWN}, - message => 'invalid number supplied for the -r option' - ); +# perform sanity checking on command line options +if ( (defined $p->opts->result) && ($p->opts->result < 0 || $p->opts->result > 20) ) { + $p->nagios_die( "invalid number supplied for the -r option" ); } -unless ( defined $nagopts->warning || defined $nagopts->critical ) { - $p->die( - return_code => $ERRORS{UNKNOWN}, - message => "you didn't supply a threshold argument" - ); +unless ( defined $p->opts->warning || defined $p->opts->critical ) { + $p->nagios_die( "you didn't supply a threshold argument" ); } -############################################################################## -# define a Nagios::Threshold object based on the command line options -my $t = $p->set_thresholds( warning => $nagopts->warning, critical => $nagopts->critical ); ############################################################################## # check stuff. # THIS is where you'd do your actual checking to get a real value for $result -# don't forget to timeout after $nagopts->timeout seconds, if applicable. +# don't forget to timeout after $p->opts->timeout seconds, if applicable. my $result; -if (defined $nagopts->result) { - $result = $nagopts->result; - print "using supplied result $result from command line\n" if $nagopts->verbose; +if (defined $p->opts->result) { # you got a 'result' option from the command line options + $result = $p->opts->result; + print "using supplied result $result from command line\n" if $p->opts->verbose; } else { $result = int rand(20)+1; - print "generated random result $result\n" if $nagopts->verbose; + print "generated random result $result\n" if $p->opts->verbose; } -print "status of result ($result) is ", $t->get_status($result), "\n" - if $nagopts->verbose; - - - ############################################################################## +# check the result against the defined warning and critical thresholds, # output the result and exit -$p->die( - return_code => $t->get_status($result), +$p->nagios_exit( + return_code => $p->check_threshold($result), message => "sample result was $result" ); -- cgit v1.2.3-74-g34f1