line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nagios::Monitoring::Plugin::Range; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
19553
|
use 5.006; |
|
6
|
|
|
|
|
21
|
|
4
|
|
|
|
|
|
|
|
5
|
6
|
|
|
6
|
|
31
|
use strict; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
129
|
|
6
|
6
|
|
|
6
|
|
28
|
use warnings; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
161
|
|
7
|
|
|
|
|
|
|
|
8
|
6
|
|
|
6
|
|
29
|
use Carp; |
|
6
|
|
|
|
|
16
|
|
|
6
|
|
|
|
|
501
|
|
9
|
6
|
|
|
6
|
|
30
|
use base qw(Class::Accessor::Fast); |
|
6
|
|
|
|
|
20
|
|
|
6
|
|
|
|
|
1303
|
|
10
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors( |
11
|
|
|
|
|
|
|
qw(start end start_infinity end_infinity alert_on) |
12
|
|
|
|
|
|
|
); |
13
|
|
|
|
|
|
|
|
14
|
6
|
|
|
6
|
|
4203
|
use Nagios::Monitoring::Plugin::Functions qw(:DEFAULT $value_re); |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
1640
|
|
15
|
|
|
|
|
|
|
our ($VERSION) = $Nagios::Monitoring::Plugin::Functions::VERSION; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use overload |
18
|
34
|
|
|
34
|
|
21967
|
'eq' => sub { shift->_stringify }, |
19
|
6
|
|
|
6
|
|
2992
|
'""' => sub { shift->_stringify }; |
|
6
|
|
|
17
|
|
2066
|
|
|
6
|
|
|
|
|
69
|
|
|
17
|
|
|
|
|
210
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# alert_on constants (undef == range not set) |
22
|
6
|
|
|
6
|
|
431
|
use constant OUTSIDE => 0; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
364
|
|
23
|
6
|
|
|
6
|
|
27
|
use constant INSIDE => 1; |
|
6
|
|
|
|
|
17
|
|
|
6
|
|
|
|
|
16831
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _stringify { |
26
|
51
|
|
|
51
|
|
69
|
my $self = shift; |
27
|
51
|
100
|
|
|
|
124
|
return "" unless $self->is_set; |
28
|
49
|
100
|
|
|
|
466
|
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
|
4384
|
my $self = shift; |
35
|
221
|
100
|
|
|
|
514
|
(! defined $self->alert_on) ? 0 : 1; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _set_range_start { |
39
|
40
|
|
|
40
|
|
87
|
my ($self, $value) = @_; |
40
|
40
|
|
|
|
|
138
|
$self->start($value+0); # Force scalar into number |
41
|
40
|
|
|
|
|
315
|
$self->start_infinity(0); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _set_range_end { |
45
|
189
|
|
|
189
|
|
306
|
my ($self, $value) = @_; |
46
|
189
|
|
|
|
|
582
|
$self->end($value+0); # Force scalar into number |
47
|
189
|
|
|
|
|
1422
|
$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
|
9755
|
my ($class, $string) = @_; |
53
|
204
|
|
|
|
|
320
|
my $valid = 0; |
54
|
204
|
|
|
|
|
467
|
my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => OUTSIDE); |
55
|
|
|
|
|
|
|
|
56
|
204
|
|
|
|
|
2316
|
$string =~ s/\s//g; # strip out any whitespace |
57
|
|
|
|
|
|
|
# check for valid range definition |
58
|
204
|
100
|
100
|
|
|
2911
|
unless ( $string =~ /[\d~]/ && $string =~ m/^\@?($value_re|~)?(:($value_re)?)?$/ ) { |
59
|
7
|
|
|
|
|
1107
|
carp "invalid range definition '$string'"; |
60
|
7
|
|
|
|
|
56
|
return undef; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
197
|
100
|
|
|
|
442
|
if ($string =~ s/^\@//) { |
64
|
2
|
|
|
|
|
8
|
$range->alert_on(INSIDE); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
197
|
100
|
|
|
|
393
|
if ($string =~ s/^~//) { # '~:x' |
68
|
12
|
|
|
|
|
40
|
$range->start_infinity(1); |
69
|
|
|
|
|
|
|
} |
70
|
197
|
100
|
|
|
|
1152
|
if ( $string =~ m/^($value_re)?:/ ) { # '10:' |
71
|
53
|
|
|
|
|
105
|
my $start = $1; |
72
|
53
|
100
|
|
|
|
165
|
$range->_set_range_start($start) if defined $start; |
73
|
53
|
|
|
|
|
259
|
$range->end_infinity(1); # overridden below if there's an end specified |
74
|
53
|
|
|
|
|
764
|
$string =~ s/^($value_re)?://; |
75
|
53
|
|
|
|
|
106
|
$valid++; |
76
|
|
|
|
|
|
|
} |
77
|
197
|
100
|
|
|
|
1096
|
if ($string =~ /^($value_re)$/) { # 'x:10' or '10' |
78
|
189
|
|
|
|
|
466
|
$range->_set_range_end($string); |
79
|
189
|
|
|
|
|
944
|
$valid++; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
197
|
100
|
100
|
|
|
1294
|
if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { |
|
|
|
33
|
|
|
|
|
83
|
196
|
|
|
|
|
3783
|
return $range; |
84
|
|
|
|
|
|
|
} |
85
|
1
|
|
|
|
|
20
|
return undef; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Returns 1 if an alert should be raised, otherwise 0 |
89
|
|
|
|
|
|
|
sub check_range { |
90
|
195
|
|
|
195
|
0
|
35990
|
my ($self, $value) = @_; |
91
|
195
|
|
|
|
|
221
|
my $false = 0; |
92
|
195
|
|
|
|
|
208
|
my $true = 1; |
93
|
195
|
100
|
|
|
|
477
|
if ($self->alert_on == INSIDE) { |
94
|
16
|
|
|
|
|
77
|
$false = 1; |
95
|
16
|
|
|
|
|
21
|
$true = 0; |
96
|
|
|
|
|
|
|
} |
97
|
195
|
100
|
100
|
|
|
1072
|
if ($self->end_infinity == 0 && $self->start_infinity == 0) { |
|
|
100
|
66
|
|
|
|
|
|
|
50
|
33
|
|
|
|
|
98
|
101
|
100
|
100
|
|
|
1056
|
if ($self->start <= $value && $value <= $self->end) { |
99
|
52
|
|
|
|
|
670
|
return $false; |
100
|
|
|
|
|
|
|
} else { |
101
|
49
|
|
|
|
|
604
|
return $true; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { |
104
|
29
|
100
|
|
|
|
484
|
if ( $value >= $self->start ) { |
105
|
14
|
|
|
|
|
95
|
return $false; |
106
|
|
|
|
|
|
|
} else { |
107
|
15
|
|
|
|
|
108
|
return $true; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} elsif ($self->start_infinity == 1 && $self->end_infinity == 0) { |
110
|
65
|
100
|
|
|
|
1563
|
if ($value <= $self->end) { |
111
|
40
|
|
|
|
|
292
|
return $false; |
112
|
|
|
|
|
|
|
} else { |
113
|
25
|
|
|
|
|
212
|
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
|
1194
|
shift->SUPER::new({ @_ }); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
__END__ |