line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nagios::Plugin::Range; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
50405
|
use 5.006; |
|
7
|
|
|
|
|
24
|
|
|
7
|
|
|
|
|
298
|
|
4
|
|
|
|
|
|
|
|
5
|
7
|
|
|
7
|
|
41
|
use strict; |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
237
|
|
6
|
7
|
|
|
7
|
|
37
|
use warnings; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
195
|
|
7
|
|
|
|
|
|
|
|
8
|
7
|
|
|
7
|
|
41
|
use Carp; |
|
7
|
|
|
|
|
11
|
|
|
7
|
|
|
|
|
721
|
|
9
|
7
|
|
|
7
|
|
38
|
use base qw(Class::Accessor::Fast); |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
2499
|
|
10
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors( |
11
|
|
|
|
|
|
|
qw(start end start_infinity end_infinity alert_on) |
12
|
|
|
|
|
|
|
); |
13
|
|
|
|
|
|
|
|
14
|
7
|
|
|
7
|
|
17315
|
use Nagios::Plugin::Functions qw(:DEFAULT $value_re); |
|
7
|
|
|
|
|
20
|
|
|
7
|
|
|
|
|
2723
|
|
15
|
|
|
|
|
|
|
our ($VERSION) = $Nagios::Plugin::Functions::VERSION; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use overload |
18
|
34
|
|
|
34
|
|
17175
|
'eq' => sub { shift->_stringify }, |
19
|
7
|
|
|
7
|
|
4061
|
'""' => sub { shift->_stringify }; |
|
7
|
|
|
17
|
|
2679
|
|
|
7
|
|
|
|
|
114
|
|
|
17
|
|
|
|
|
669
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# alert_on constants (undef == range not set) |
22
|
7
|
|
|
7
|
|
543
|
use constant OUTSIDE => 0; |
|
7
|
|
|
|
|
22
|
|
|
7
|
|
|
|
|
443
|
|
23
|
7
|
|
|
7
|
|
38
|
use constant INSIDE => 1; |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
6101
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _stringify { |
26
|
51
|
|
|
51
|
|
91
|
my $self = shift; |
27
|
51
|
100
|
|
|
|
294
|
return "" unless $self->is_set; |
28
|
49
|
100
|
|
|
|
1299
|
return (($self->alert_on) ? "@" : "") . |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
29
|
|
|
|
|
|
|
(($self->start_infinity == 1) ? "~:" : (($self->start == 0)?"":$self->start.":")) . |
30
|
|
|
|
|
|
|
(($self->end_infinity == 1) ? "" : $self->end); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub is_set { |
34
|
221
|
|
|
221
|
0
|
6523
|
my $self = shift; |
35
|
221
|
100
|
|
|
|
714
|
(! defined $self->alert_on) ? 0 : 1; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _set_range_start { |
39
|
40
|
|
|
40
|
|
69
|
my ($self, $value) = @_; |
40
|
40
|
|
|
|
|
185
|
$self->start($value+0); # Force scalar into number |
41
|
40
|
|
|
|
|
399
|
$self->start_infinity(0); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _set_range_end { |
45
|
189
|
|
|
189
|
|
356
|
my ($self, $value) = @_; |
46
|
189
|
|
|
|
|
636
|
$self->end($value+0); # Force scalar into number |
47
|
189
|
|
|
|
|
1708
|
$self->end_infinity(0); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Returns a N::P::Range object if the string is a conforms to a Nagios Plugin range string, otherwise null |
51
|
|
|
|
|
|
|
sub parse_range_string { |
52
|
204
|
|
|
204
|
0
|
12769
|
my ($class, $string) = @_; |
53
|
204
|
|
|
|
|
245
|
my $valid = 0; |
54
|
204
|
|
|
|
|
661
|
my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => OUTSIDE); |
55
|
|
|
|
|
|
|
|
56
|
204
|
|
|
|
|
2761
|
$string =~ s/\s//g; # strip out any whitespace |
57
|
|
|
|
|
|
|
# check for valid range definition |
58
|
204
|
100
|
100
|
|
|
3575
|
unless ( $string =~ /[\d~]/ && $string =~ m/^\@?($value_re|~)?(:($value_re)?)?$/ ) { |
59
|
7
|
|
|
|
|
1096
|
carp "invalid range definition '$string'"; |
60
|
7
|
|
|
|
|
57
|
return undef; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
197
|
100
|
|
|
|
554
|
if ($string =~ s/^\@//) { |
64
|
2
|
|
|
|
|
9
|
$range->alert_on(INSIDE); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
197
|
100
|
|
|
|
445
|
if ($string =~ s/^~//) { # '~:x' |
68
|
12
|
|
|
|
|
46
|
$range->start_infinity(1); |
69
|
|
|
|
|
|
|
} |
70
|
197
|
100
|
|
|
|
3112
|
if ( $string =~ m/^($value_re)?:/ ) { # '10:' |
71
|
53
|
|
|
|
|
345
|
my $start = $1; |
72
|
53
|
100
|
|
|
|
197
|
$range->_set_range_start($start) if defined $start; |
73
|
53
|
|
|
|
|
304
|
$range->end_infinity(1); # overridden below if there's an end specified |
74
|
53
|
|
|
|
|
1071
|
$string =~ s/^($value_re)?://; |
75
|
53
|
|
|
|
|
125
|
$valid++; |
76
|
|
|
|
|
|
|
} |
77
|
197
|
100
|
|
|
|
1745
|
if ($string =~ /^($value_re)$/) { # 'x:10' or '10' |
78
|
189
|
|
|
|
|
410
|
$range->_set_range_end($string); |
79
|
189
|
|
|
|
|
2428
|
$valid++; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
197
|
100
|
100
|
|
|
977
|
if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { |
|
|
|
33
|
|
|
|
|
83
|
196
|
|
|
|
|
4397
|
return $range; |
84
|
|
|
|
|
|
|
} |
85
|
1
|
|
|
|
|
24
|
return undef; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Returns 1 if an alert should be raised, otherwise 0 |
89
|
|
|
|
|
|
|
sub check_range { |
90
|
195
|
|
|
195
|
0
|
27502
|
my ($self, $value) = @_; |
91
|
195
|
|
|
|
|
953
|
my $false = 0; |
92
|
195
|
|
|
|
|
2444
|
my $true = 1; |
93
|
195
|
100
|
|
|
|
667
|
if ($self->alert_on == INSIDE) { |
94
|
16
|
|
|
|
|
80
|
$false = 1; |
95
|
16
|
|
|
|
|
20
|
$true = 0; |
96
|
|
|
|
|
|
|
} |
97
|
195
|
100
|
100
|
|
|
1535
|
if ($self->end_infinity == 0 && $self->start_infinity == 0) { |
|
|
100
|
66
|
|
|
|
|
|
|
50
|
33
|
|
|
|
|
98
|
101
|
100
|
100
|
|
|
2152
|
if ($self->start <= $value && $value <= $self->end) { |
99
|
52
|
|
|
|
|
1223
|
return $false; |
100
|
|
|
|
|
|
|
} else { |
101
|
49
|
|
|
|
|
721
|
return $true; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { |
104
|
29
|
100
|
|
|
|
555
|
if ( $value >= $self->start ) { |
105
|
14
|
|
|
|
|
108
|
return $false; |
106
|
|
|
|
|
|
|
} else { |
107
|
15
|
|
|
|
|
125
|
return $true; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} elsif ($self->start_infinity == 1 && $self->end_infinity == 0) { |
110
|
65
|
100
|
|
|
|
9474
|
if ($value <= $self->end) { |
111
|
40
|
|
|
|
|
922
|
return $false; |
112
|
|
|
|
|
|
|
} else { |
113
|
25
|
|
|
|
|
399
|
return $true; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} else { |
116
|
0
|
|
|
|
|
0
|
return $false; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Constructor - map args to hashref for SUPER |
121
|
|
|
|
|
|
|
sub new |
122
|
|
|
|
|
|
|
{ |
123
|
241
|
|
|
241
|
1
|
2121
|
shift->SUPER::new({ @_ }); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
__END__ |