From e0c038d4c2a974f53c37d0b9fb3b22b7cd8d765b Mon Sep 17 00:00:00 2001 From: Nathan Vonnahme Date: Fri, 17 Nov 2006 21:48:22 +0000 Subject: * renamed N::P::arg to add_arg * some POD work git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1539 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index f1b1807..7187048 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm @@ -123,12 +123,10 @@ sub check_threshold { } ); } - # in order of preference, get warning and critical from # 1. explicit arguments to check_threshold # 2. previously explicitly set threshold object # 3. implicit options from Getopts object - if ( exists $args{warning} || exists $args{critical} ) { $self->set_thresholds( warning => $args{warning}, @@ -152,7 +150,7 @@ sub check_threshold { } # top level interface to my Nagios::Plugin::Getopt object -sub arg { +sub add_arg { my $self = shift; $self->opts->arg(@_) if $self->_check_for_opts; } @@ -258,8 +256,7 @@ plugins # Return code: 3; # output: PAGESIZE UNKNOWN - Could not retrieve page - # Threshold methods (NOT YET IMPLEMENTED - use - # Nagios::Plugin::Threshold for now) + # Threshold methods $code = $np->check_threshold( check => $value, warning => $warning_threshold, @@ -379,16 +376,50 @@ Alias for nagios_die(). Deprecated. =back - =head2 THRESHOLD METHODS -NOT YET IMPLEMENTED - use Nagios::Plugin::Threshold directly for now. +These provide a top level interface to the C +module; for more details, see its documentation. =over 4 =item check_threshold( $value ) + =item check_threshold( check => $value, warning => $warn, critical => $crit ) +Evaluates $value against the thresholds and returns OK, CRITICAL, or +WARNING constant. The thresholds may be: + +1. explicitly set by passing 'warning' and/or 'critical' parameters to + C, or, + +2. explicitly set by calling C before C, or, + +3. implicitly set by command-line parameters -w, -c, --critical or + --warning, if you have run C<$plugin->getopts()>. + +The return value is ready to pass to C , e . g ., + + $p->nagios_exit( + return_code => $p->check_threshold($result), + message => " sample result was $result" + ); + + +=item set_thresholds(warning => "10:25", critical => "~:25") + +Sets the acceptable ranges and creates the plugin's +Nagios::Plugins::Threshold object. See +http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT +for details and examples of the threshold format. + +=item threshold() + +Returns the object's C object, if it has +been defined by calling set_thresholds(). You can pass a new +Threshold object to it to replace the old one too, but you shouldn't +need to do that from a plugin script. + =back diff --git a/t/Nagios-Plugin-Range.t b/t/Nagios-Plugin-Range.t index df5dbd5..9b75a13 100644 --- a/t/Nagios-Plugin-Range.t +++ b/t/Nagios-Plugin-Range.t @@ -1,6 +1,7 @@ use strict; -use Test::More qw(no_plan); #tests => 123; +#use Test::More qw(no_plan); +use Test::More tests => 149; BEGIN { use_ok('Nagios::Plugin::Range'); diff --git a/t/check_stuff.pl b/t/check_stuff.pl index 889e484..8284169 100755 --- a/t/check_stuff.pl +++ b/t/check_stuff.pl @@ -50,27 +50,32 @@ 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 if the count *is* inside the specified range. -See more threshold examples at - http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT +See more threshold examples at http + : // nagiosplug + . sourceforge + . net / developer-guidelines + . html #THRESHOLDFORMAT -Examples: + Examples: - $PROGNAME -w 10 -c 18 - Returns a warning if the resulting number is greater than 10, or a - critical error if it is greater than 18. + $PROGNAME -w 10 -c 18 Returns a warning + if the resulting number is greater than 10, + or a critical error + if it is greater than 18. - $PROGNAME -w 10: -c 4: - Returns a warning if the resulting number is less than 10, or a - critical error if it is less than 4. + $PROGNAME -w 10 : -c 4 : 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. -$p->arg( +$p->add_arg( spec => 'warning|w=s', help => @@ -82,7 +87,7 @@ qq{-w, --warning=INTEGER:INTEGER # default => 10, ); -$p->arg( +$p->add_arg( spec => 'critical|c=s', help => qq{-c, --critical=INTEGER:INTEGER @@ -90,7 +95,7 @@ qq{-c, --critical=INTEGER:INTEGER which a critical will be generated. }, ); -$p->arg( +$p->add_arg( spec => 'result|r=f', help => qq{-r, --result=INTEGER @@ -104,11 +109,11 @@ $p->getopts; # 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" ); + $p->nagios_die( " invalid number supplied for the -r option " ); } unless ( defined $p->opts->warning || defined $p->opts->critical ) { - $p->nagios_die( "you didn't supply a threshold argument" ); + $p->nagios_die( " you didn't supply a threshold argument " ); } @@ -121,11 +126,12 @@ unless ( defined $p->opts->warning || defined $p->opts->critical ) { my $result; 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; + 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 $p->opts->verbose; + print " generated random result $result\n " if $p->opts->verbose; } @@ -134,6 +140,6 @@ else { # output the result and exit $p->nagios_exit( return_code => $p->check_threshold($result), - message => "sample result was $result" + message => " sample result was $result" ); -- cgit v0.10-9-g596f