summaryrefslogtreecommitdiffstats
path: root/web/attachments/112620-check_range.patch
blob: 2ddd8a397ed495dd7f682b7727a0a9ba4a5bb025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
? 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;