summaryrefslogtreecommitdiffstats
path: root/lib/Nagios/Plugin/Range.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Nagios/Plugin/Range.pm')
-rw-r--r--lib/Nagios/Plugin/Range.pm44
1 files changed, 29 insertions, 15 deletions
diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm
index c03001a..c0c47e4 100644
--- a/lib/Nagios/Plugin/Range.pm
+++ b/lib/Nagios/Plugin/Range.pm
@@ -4,6 +4,10 @@ use 5.008004;
4 4
5use strict; 5use strict;
6use warnings; 6use warnings;
7use Carp;
8
9use Nagios::Plugin;
10our ($VERSION) = $Nagios::Plugin::VERSION;
7 11
8use overload 12use overload
9 '""' => sub { shift->stringify }; 13 '""' => sub { shift->stringify };
@@ -17,8 +21,8 @@ struct "Nagios::Plugin::Range" => {
17 alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set 21 alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set
18 }; 22 };
19 23
20my $outside = 0; 24use constant OUTSIDE => 0;
21my $inside = 1; 25use constant INSIDE => 1;
22 26
23sub stringify { 27sub stringify {
24 my $self = shift; 28 my $self = shift;
@@ -49,22 +53,32 @@ sub set_range_end {
49sub parse_range_string { 53sub parse_range_string {
50 my ($class, $string) = @_; 54 my ($class, $string) = @_;
51 my $valid = 0; 55 my $valid = 0;
52 my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => $outside); 56 my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => OUTSIDE);
57
58 $string =~ s/\s//g; # strip out any whitespace
59 # check for valid range definition
60 unless ( $string =~ /[\d~]/ && $string =~ m/^\@?(-?[\d.]+|~)?(:(-?[\d.]+)?)?$/ ) {
61 carp "invalid range definition '$string'";
62 return undef;
63 }
53 64
54 if ($string =~ s/^\@//) { 65 if ($string =~ s/^\@//) {
55 $range->alert_on($inside); 66 $range->alert_on(INSIDE);
56 } 67 }
57 if ($string =~ s/^~//) { 68
58 $range->start_infinity(1); 69 if ($string =~ s/^~//) { # '~:x'
70 $range->start_infinity(1);
59 } 71 }
60 if (($_) = $string =~ /^([-\d\.]+)?:/) { 72 if ( $string =~ m/^([\d\.-]+)?:/ ) { # '10:'
61 $range->set_range_start($_) if defined $_; 73 my $start = $1;
62 $string =~ s/^([-\d\.]+)?://; 74 $range->set_range_start($start) if defined $start;
63 $valid++ 75 $range->end_infinity(1); # overridden below if there's an end specified
76 $string =~ s/^([-\d\.]+)?://;
77 $valid++;
64 } 78 }
65 if ($string =~ /^([-\d\.]+)$/) { 79 if ($string =~ /^([-\d\.]+)$/) { # 'x:10' or '10'
66 $range->set_range_end($string); 80 $range->set_range_end($string);
67 $valid++; 81 $valid++;
68 } 82 }
69 83
70 if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { 84 if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) {
@@ -78,7 +92,7 @@ sub check_range {
78 my ($self, $value) = @_; 92 my ($self, $value) = @_;
79 my $false = 0; 93 my $false = 0;
80 my $true = 1; 94 my $true = 1;
81 if ($self->alert_on == $inside) { 95 if ($self->alert_on == INSIDE) {
82 $false = 1; 96 $false = 1;
83 $true = 0; 97 $true = 0;
84 } 98 }
@@ -89,7 +103,7 @@ sub check_range {
89 return $true; 103 return $true;
90 } 104 }
91 } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { 105 } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) {
92 if ($self->start <= $value) { 106 if ( $value >= $self->start ) {
93 return $false; 107 return $false;
94 } else { 108 } else {
95 return $true; 109 return $true;