diff options
| -rw-r--r-- | lib/Nagios/Plugin.pm | 45 | ||||
| -rw-r--r-- | t/Nagios-Plugin-Range.t | 3 | ||||
| -rwxr-xr-x | t/check_stuff.pl | 42 |
3 files changed, 64 insertions, 26 deletions
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 { | |||
| 123 | } ); | 123 | } ); |
| 124 | } | 124 | } |
| 125 | 125 | ||
| 126 | |||
| 127 | # in order of preference, get warning and critical from | 126 | # in order of preference, get warning and critical from |
| 128 | # 1. explicit arguments to check_threshold | 127 | # 1. explicit arguments to check_threshold |
| 129 | # 2. previously explicitly set threshold object | 128 | # 2. previously explicitly set threshold object |
| 130 | # 3. implicit options from Getopts object | 129 | # 3. implicit options from Getopts object |
| 131 | |||
| 132 | if ( exists $args{warning} || exists $args{critical} ) { | 130 | if ( exists $args{warning} || exists $args{critical} ) { |
| 133 | $self->set_thresholds( | 131 | $self->set_thresholds( |
| 134 | warning => $args{warning}, | 132 | warning => $args{warning}, |
| @@ -152,7 +150,7 @@ sub check_threshold { | |||
| 152 | } | 150 | } |
| 153 | 151 | ||
| 154 | # top level interface to my Nagios::Plugin::Getopt object | 152 | # top level interface to my Nagios::Plugin::Getopt object |
| 155 | sub arg { | 153 | sub add_arg { |
| 156 | my $self = shift; | 154 | my $self = shift; |
| 157 | $self->opts->arg(@_) if $self->_check_for_opts; | 155 | $self->opts->arg(@_) if $self->_check_for_opts; |
| 158 | } | 156 | } |
| @@ -258,8 +256,7 @@ plugins | |||
| 258 | # Return code: 3; | 256 | # Return code: 3; |
| 259 | # output: PAGESIZE UNKNOWN - Could not retrieve page | 257 | # output: PAGESIZE UNKNOWN - Could not retrieve page |
| 260 | 258 | ||
| 261 | # Threshold methods (NOT YET IMPLEMENTED - use | 259 | # Threshold methods |
| 262 | # Nagios::Plugin::Threshold for now) | ||
| 263 | $code = $np->check_threshold( | 260 | $code = $np->check_threshold( |
| 264 | check => $value, | 261 | check => $value, |
| 265 | warning => $warning_threshold, | 262 | warning => $warning_threshold, |
| @@ -379,16 +376,50 @@ Alias for nagios_die(). Deprecated. | |||
| 379 | 376 | ||
| 380 | =back | 377 | =back |
| 381 | 378 | ||
| 382 | |||
| 383 | =head2 THRESHOLD METHODS | 379 | =head2 THRESHOLD METHODS |
| 384 | 380 | ||
| 385 | NOT YET IMPLEMENTED - use Nagios::Plugin::Threshold directly for now. | 381 | These provide a top level interface to the C<Nagios::Plugins::Threshold> |
| 382 | module; for more details, see its documentation. | ||
| 386 | 383 | ||
| 387 | =over 4 | 384 | =over 4 |
| 388 | 385 | ||
| 389 | =item check_threshold( $value ) | 386 | =item check_threshold( $value ) |
| 387 | |||
| 390 | =item check_threshold( check => $value, warning => $warn, critical => $crit ) | 388 | =item check_threshold( check => $value, warning => $warn, critical => $crit ) |
| 391 | 389 | ||
| 390 | Evaluates $value against the thresholds and returns OK, CRITICAL, or | ||
| 391 | WARNING constant. The thresholds may be: | ||
| 392 | |||
| 393 | 1. explicitly set by passing 'warning' and/or 'critical' parameters to | ||
| 394 | C<check_threshold()>, or, | ||
| 395 | |||
| 396 | 2. explicitly set by calling C<set_thresholds()> before C<check_threshold()>, or, | ||
| 397 | |||
| 398 | 3. implicitly set by command-line parameters -w, -c, --critical or | ||
| 399 | --warning, if you have run C<$plugin->getopts()>. | ||
| 400 | |||
| 401 | The return value is ready to pass to C <nagios_exit>, e . g ., | ||
| 402 | |||
| 403 | $p->nagios_exit( | ||
| 404 | return_code => $p->check_threshold($result), | ||
| 405 | message => " sample result was $result" | ||
| 406 | ); | ||
| 407 | |||
| 408 | |||
| 409 | =item set_thresholds(warning => "10:25", critical => "~:25") | ||
| 410 | |||
| 411 | Sets the acceptable ranges and creates the plugin's | ||
| 412 | Nagios::Plugins::Threshold object. See | ||
| 413 | http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT | ||
| 414 | for details and examples of the threshold format. | ||
| 415 | |||
| 416 | =item threshold() | ||
| 417 | |||
| 418 | Returns the object's C<Nagios::Plugin::Threshold> object, if it has | ||
| 419 | been defined by calling set_thresholds(). You can pass a new | ||
| 420 | Threshold object to it to replace the old one too, but you shouldn't | ||
| 421 | need to do that from a plugin script. | ||
| 422 | |||
| 392 | =back | 423 | =back |
| 393 | 424 | ||
| 394 | 425 | ||
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 @@ | |||
| 1 | 1 | ||
| 2 | use strict; | 2 | use strict; |
| 3 | use Test::More qw(no_plan); #tests => 123; | 3 | #use Test::More qw(no_plan); |
| 4 | use Test::More tests => 149; | ||
| 4 | 5 | ||
| 5 | BEGIN { | 6 | BEGIN { |
| 6 | use_ok('Nagios::Plugin::Range'); | 7 | 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' | |||
| 50 | (or 'max'). If specified '\@min:max', a warning status will be generated | 50 | (or 'max'). If specified '\@min:max', a warning status will be generated |
| 51 | if the count *is* inside the specified range. | 51 | if the count *is* inside the specified range. |
| 52 | 52 | ||
| 53 | See more threshold examples at | 53 | See more threshold examples at http |
| 54 | http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT | 54 | : // nagiosplug |
| 55 | . sourceforge | ||
| 56 | . net / developer-guidelines | ||
| 57 | . html #THRESHOLDFORMAT | ||
| 55 | 58 | ||
| 56 | Examples: | 59 | Examples: |
| 57 | 60 | ||
| 58 | $PROGNAME -w 10 -c 18 | 61 | $PROGNAME -w 10 -c 18 Returns a warning |
| 59 | Returns a warning if the resulting number is greater than 10, or a | 62 | if the resulting number is greater than 10, |
| 60 | critical error if it is greater than 18. | 63 | or a critical error |
| 64 | if it is greater than 18. | ||
| 61 | 65 | ||
| 62 | $PROGNAME -w 10: -c 4: | 66 | $PROGNAME -w 10 : -c 4 : Returns a warning |
| 63 | Returns a warning if the resulting number is less than 10, or a | 67 | if the resulting number is less than 10, |
| 64 | critical error if it is less than 4. | 68 | or a critical error |
| 69 | if it is less than 4. | ||
| 65 | 70 | ||
| 66 | " | 71 | " |
| 67 | ); | 72 | ); |
| 68 | 73 | ||
| 69 | 74 | ||
| 70 | # Define and document the valid command line options | 75 | # Define and document the valid command line options |
| 71 | # usage, help, version, timeout and verbose are defined by default. | 76 | # usage, help, version, timeout and verbose are defined by default. |
| 72 | 77 | ||
| 73 | $p->arg( | 78 | $p->add_arg( |
| 74 | spec => 'warning|w=s', | 79 | spec => 'warning|w=s', |
| 75 | 80 | ||
| 76 | help => | 81 | help => |
| @@ -82,7 +87,7 @@ qq{-w, --warning=INTEGER:INTEGER | |||
| 82 | # default => 10, | 87 | # default => 10, |
| 83 | ); | 88 | ); |
| 84 | 89 | ||
| 85 | $p->arg( | 90 | $p->add_arg( |
| 86 | spec => 'critical|c=s', | 91 | spec => 'critical|c=s', |
| 87 | help => | 92 | help => |
| 88 | qq{-c, --critical=INTEGER:INTEGER | 93 | qq{-c, --critical=INTEGER:INTEGER |
| @@ -90,7 +95,7 @@ qq{-c, --critical=INTEGER:INTEGER | |||
| 90 | which a critical will be generated. }, | 95 | which a critical will be generated. }, |
| 91 | ); | 96 | ); |
| 92 | 97 | ||
| 93 | $p->arg( | 98 | $p->add_arg( |
| 94 | spec => 'result|r=f', | 99 | spec => 'result|r=f', |
| 95 | help => | 100 | help => |
| 96 | qq{-r, --result=INTEGER | 101 | qq{-r, --result=INTEGER |
| @@ -104,11 +109,11 @@ $p->getopts; | |||
| 104 | 109 | ||
| 105 | # perform sanity checking on command line options | 110 | # perform sanity checking on command line options |
| 106 | if ( (defined $p->opts->result) && ($p->opts->result < 0 || $p->opts->result > 20) ) { | 111 | if ( (defined $p->opts->result) && ($p->opts->result < 0 || $p->opts->result > 20) ) { |
| 107 | $p->nagios_die( "invalid number supplied for the -r option" ); | 112 | $p->nagios_die( " invalid number supplied for the -r option " ); |
| 108 | } | 113 | } |
| 109 | 114 | ||
| 110 | unless ( defined $p->opts->warning || defined $p->opts->critical ) { | 115 | unless ( defined $p->opts->warning || defined $p->opts->critical ) { |
| 111 | $p->nagios_die( "you didn't supply a threshold argument" ); | 116 | $p->nagios_die( " you didn't supply a threshold argument " ); |
| 112 | } | 117 | } |
| 113 | 118 | ||
| 114 | 119 | ||
| @@ -121,11 +126,12 @@ unless ( defined $p->opts->warning || defined $p->opts->critical ) { | |||
| 121 | my $result; | 126 | my $result; |
| 122 | if (defined $p->opts->result) { # you got a 'result' option from the command line options | 127 | if (defined $p->opts->result) { # you got a 'result' option from the command line options |
| 123 | $result = $p->opts->result; | 128 | $result = $p->opts->result; |
| 124 | print "using supplied result $result from command line\n" if $p->opts->verbose; | 129 | print " using supplied result $result from command line \n |
| 130 | " if $p->opts->verbose; | ||
| 125 | } | 131 | } |
| 126 | else { | 132 | else { |
| 127 | $result = int rand(20)+1; | 133 | $result = int rand(20)+1; |
| 128 | print "generated random result $result\n" if $p->opts->verbose; | 134 | print " generated random result $result\n " if $p->opts->verbose; |
| 129 | } | 135 | } |
| 130 | 136 | ||
| 131 | 137 | ||
| @@ -134,6 +140,6 @@ else { | |||
| 134 | # output the result and exit | 140 | # output the result and exit |
| 135 | $p->nagios_exit( | 141 | $p->nagios_exit( |
| 136 | return_code => $p->check_threshold($result), | 142 | return_code => $p->check_threshold($result), |
| 137 | message => "sample result was $result" | 143 | message => " sample result was $result" |
| 138 | ); | 144 | ); |
| 139 | 145 | ||
