From cce4fccf608b3e4accf7cbf2f11da23b3d627f15 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Thu, 22 Dec 2011 11:56:08 -0500 Subject: check_threshold to check multiple values at once diff --git a/Changes b/Changes index 29b8fc3..2034b6c 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl module Nagios::Plugin. +0.36 22nd December 2011 + - Updated check_threshold to allow multiple check values to be checked at once + 0.35 3rd December 2010 - Fixed test failures with Test::More 0.96 (Slaven Rezic and Peter John Edwards - RT57709) diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index d2a5145..8950477 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm @@ -110,7 +110,7 @@ sub check_threshold { my %args; - if ( $#_ == 0 && ! ref $_[0]) { # one positional param + if ( $#_ == 0 && (! ref $_[0] || ref $_[0] eq "ARRAY" )) { # one positional param %args = (check => shift); } else { @@ -509,6 +509,9 @@ WARNING constant. The thresholds may be: 3. implicitly set by command-line parameters -w, -c, --critical or --warning, if you have run C<< $plugin->getopts() >>. +You can specify $value as an array of values and each will be checked against +the thresholds. + The return value is ready to pass to C , e . g ., $p->nagios_exit( diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index 73fce53..95a089b 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm @@ -16,11 +16,16 @@ sub get_status { my ($self, $value) = @_; - if ($self->critical->is_set) { - return CRITICAL if $self->critical->check_range($value); + $value = [ $value ] if (ref $value eq ""); + foreach my $v (@$value) { + if ($self->critical->is_set) { + return CRITICAL if $self->critical->check_range($v); + } } - if ($self->warning->is_set) { - return WARNING if $self->warning->check_range($value); + foreach my $v (@$value) { + if ($self->warning->is_set) { + return WARNING if $self->warning->check_range($v); + } } return OK; } diff --git a/t/Nagios-Plugin-04.t b/t/Nagios-Plugin-04.t index d88ad73..e5eb3ab 100644 --- a/t/Nagios-Plugin-04.t +++ b/t/Nagios-Plugin-04.t @@ -3,7 +3,7 @@ use strict; #use Test::More 'no_plan'; -use Test::More tests=>26; +use Test::More tests=>30; BEGIN { use_ok('Nagios::Plugin') }; use Nagios::Plugin::Functions; @@ -52,6 +52,12 @@ is $p->check_threshold(6), WARNING, "check_threshold WARNING"; is $p->check_threshold(11), CRITICAL, "check_threshold CRITICAL"; is $p->check_threshold(check=>11), CRITICAL, "check_threshold CRITICAL with hash param"; +# Check that arrays allowed +is $p->check_threshold([2,1]), OK, "check_threshold OK when called implicitly"; +is $p->check_threshold([6,2]), WARNING, "check_threshold WARNING"; +is $p->check_threshold([1,2,6,11]), CRITICAL, "check_threshold CRITICAL"; +is $p->check_threshold(check=>[1,2,6,11]), CRITICAL, "check_threshold CRITICAL with hash param"; + # thresholds set explicitly is $p->check_threshold( check => 2, -- cgit v0.10-9-g596f