? plugins-scripts/t/utils.pm.t Index: test.pl.in =================================================================== RCS file: /cvsroot/nagiosplug/nagiosplug/test.pl.in,v retrieving revision 1.2 diff -u -w -u -r1.2 test.pl.in --- test.pl.in 25 Nov 2004 05:06:24 -0000 1.2 +++ test.pl.in 15 Dec 2004 23:59:46 -0000 @@ -69,7 +69,7 @@ $tstdir = './t' unless ($tstdir); opendir(DIR, $tstdir) || die "can't opendir $tstdir: $!"; while ($file = readdir(DIR)) { - push @dots, "$tstdir/$file" if ($file =~ m/^[^\.]+\.t$/); + push @dots, "$tstdir/$file" if ($file =~ m/^[^\.]+.*\.t$/); } closedir DIR; } Index: plugins-scripts/utils.pm.in =================================================================== RCS file: /cvsroot/nagiosplug/nagiosplug/plugins-scripts/utils.pm.in,v retrieving revision 1.7 diff -u -w -u -r1.7 utils.pm.in --- plugins-scripts/utils.pm.in 13 Apr 2003 04:25:36 -0000 1.7 +++ plugins-scripts/utils.pm.in 15 Dec 2004 23:59:46 -0000 @@ -25,13 +25,14 @@ require Exporter; @ISA = qw(Exporter); -@EXPORT_OK = qw($TIMEOUT %ERRORS &print_revision &support &usage); +@EXPORT_OK = qw($TIMEOUT %ERRORS &print_revision &support &usage &check_range); #use strict; #use vars($TIMEOUT %ERRORS); sub print_revision ($$); sub usage; sub support(); +sub check_range($$); sub is_hostname; ## updated by autoconf @@ -80,4 +81,75 @@ } } +sub check_range ($$) +{ + # Takes two parameters. The first is a metric. The second is + # a range using Nagios plugin range syntax. + # No explicit checking is done that the metric or the range consists + # of non-numeric values - they will be treated as zero. + + # Accepts ranges of the following form: + # + # min:max (between min and max inclusive) + # min: (equivalent to min:infinity) + # :max (equivalent to 0:max) + # max (equivalent :max which is equivalent to 0:max) + # 0 (equivalent to 0:infinity) + # + # The symbol ~ may be specified as the minimum portion of a range + # to represent -infinity. The range may be prefixed by @ to negate + # the result (so the metric is critical if within the range). + # Note that ~:~ is treated as an invalid range + + my($metric) = shift(@_); + my($range) = shift(@_); + + # Take third and any subsequent parameters and concatenate with spaces + # to form the name of the range for use in error messages. + my($range_name) = join(' ', @_); + $range_name = " for $range_name." if ($range_name); + + # Use strict doesn't let us do string/numeric conversions, so turn + # off warnings to get around that problem and the problem of undefined + # variables from the regex below. + local($^W) = 0; + + my($range_ok) = 0; + + # Split the range into the optional 'invert range' prefix (@), minimum + # bound and upper bound, allowing all the variations. + my($invert_range, $lower, $upper) = $range =~ /^(\@)?(?:([^:])*:)?(.*)?$/; + + # The above regex has problems with 0 (which means 0:infinity), so + # we have to correct for that (but not for :0, which means 0:0 and is + # handled correctly by the regex). + ($lower, $upper) = ($upper, $lower) if + (($upper eq '0' and not $lower) and $range !~ /:/); + + # Check that upper >= lower, including the case when lower or upper is + # ~ (negative infinity). + if ($upper eq '~' or ($lower ne '~' and $upper ne '' and $lower > $upper )) + { + return -1; + } + + # If the lower bound is not ~ (negative infinity) then check that the + # metric is greater than or equal to the lower bound. If the lower bound + # is ~ then the metric has to be OK because any metric has to be higher + # than negative infinity. + if ($lower ne '~') + { + $range_ok++ if ($metric >= $lower + 0); + } + else + { + $range_ok++; + } + + $range_ok = 0 if ($upper ne '' and $metric > $upper + 0); + $range_ok ^= 1 if ($invert_range); + + return($range_ok); +} + 1;