line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nagios::Monitoring::Plugin::Range;
|
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
23974
|
use 5.006;
|
|
7
|
|
|
|
|
23
|
|
4
|
|
|
|
|
|
|
|
5
|
7
|
|
|
7
|
|
35
|
use strict;
|
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
163
|
|
6
|
7
|
|
|
7
|
|
35
|
use warnings;
|
|
7
|
|
|
|
|
10
|
|
|
7
|
|
|
|
|
508
|
|
7
|
|
|
|
|
|
|
|
8
|
7
|
|
|
7
|
|
35
|
use Carp;
|
|
7
|
|
|
|
|
11
|
|
|
7
|
|
|
|
|
589
|
|
9
|
7
|
|
|
7
|
|
36
|
use base qw(Class::Accessor::Fast);
|
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
1695
|
|
10
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(
|
11
|
|
|
|
|
|
|
qw(start end start_infinity end_infinity alert_on)
|
12
|
|
|
|
|
|
|
);
|
13
|
|
|
|
|
|
|
|
14
|
7
|
|
|
7
|
|
5625
|
use Nagios::Monitoring::Plugin::Functions qw(:DEFAULT $value_re);
|
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
2078
|
|
15
|
|
|
|
|
|
|
our ($VERSION) = $Nagios::Monitoring::Plugin::Functions::VERSION;
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use overload
|
18
|
34
|
|
|
34
|
|
33807
|
'eq' => sub { shift->_stringify },
|
19
|
7
|
|
|
7
|
|
4059
|
'""' => sub { shift->_stringify };
|
|
7
|
|
|
17
|
|
2649
|
|
|
7
|
|
|
|
|
95
|
|
|
17
|
|
|
|
|
157
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# alert_on constants (undef == range not set)
|
22
|
7
|
|
|
7
|
|
536
|
use constant OUTSIDE => 0;
|
|
7
|
|
|
|
|
21
|
|
|
7
|
|
|
|
|
497
|
|
23
|
7
|
|
|
7
|
|
38
|
use constant INSIDE => 1;
|
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
5884
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _stringify {
|
26
|
51
|
|
|
51
|
|
82
|
my $self = shift;
|
27
|
51
|
100
|
|
|
|
127
|
return "" unless $self->is_set;
|
28
|
49
|
100
|
|
|
|
458
|
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
|
3712
|
my $self = shift;
|
35
|
221
|
100
|
|
|
|
552
|
(! defined $self->alert_on) ? 0 : 1;
|
36
|
|
|
|
|
|
|
}
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _set_range_start {
|
39
|
40
|
|
|
40
|
|
71
|
my ($self, $value) = @_;
|
40
|
40
|
|
|
|
|
158
|
$self->start($value+0); # Force scalar into number
|
41
|
40
|
|
|
|
|
339
|
$self->start_infinity(0);
|
42
|
|
|
|
|
|
|
}
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _set_range_end {
|
45
|
189
|
|
|
189
|
|
257
|
my ($self, $value) = @_;
|
46
|
189
|
|
|
|
|
666
|
$self->end($value+0); # Force scalar into number
|
47
|
189
|
|
|
|
|
1352
|
$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
|
11328
|
my ($class, $string) = @_;
|
53
|
204
|
|
|
|
|
248
|
my $valid = 0;
|
54
|
204
|
|
|
|
|
496
|
my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => OUTSIDE);
|
55
|
|
|
|
|
|
|
|
56
|
204
|
|
|
|
|
2187
|
$string =~ s/\s//g; # strip out any whitespace
|
57
|
|
|
|
|
|
|
# check for valid range definition
|
58
|
204
|
100
|
100
|
|
|
2944
|
unless ( $string =~ /[\d~]/ && $string =~ m/^\@?($value_re|~)?(:($value_re)?)?$/ ) {
|
59
|
7
|
|
|
|
|
1126
|
carp "invalid range definition '$string'";
|
60
|
7
|
|
|
|
|
62
|
return undef;
|
61
|
|
|
|
|
|
|
}
|
62
|
|
|
|
|
|
|
|
63
|
197
|
100
|
|
|
|
481
|
if ($string =~ s/^\@//) {
|
64
|
2
|
|
|
|
|
11
|
$range->alert_on(INSIDE);
|
65
|
|
|
|
|
|
|
}
|
66
|
|
|
|
|
|
|
|
67
|
197
|
100
|
|
|
|
449
|
if ($string =~ s/^~//) { # '~:x'
|
68
|
12
|
|
|
|
|
48
|
$range->start_infinity(1);
|
69
|
|
|
|
|
|
|
}
|
70
|
197
|
100
|
|
|
|
1187
|
if ( $string =~ m/^($value_re)?:/ ) { # '10:'
|
71
|
53
|
|
|
|
|
156
|
my $start = $1;
|
72
|
53
|
100
|
|
|
|
185
|
$range->_set_range_start($start) if defined $start;
|
73
|
53
|
|
|
|
|
286
|
$range->end_infinity(1); # overridden below if there's an end specified
|
74
|
53
|
|
|
|
|
915
|
$string =~ s/^($value_re)?://;
|
75
|
53
|
|
|
|
|
114
|
$valid++;
|
76
|
|
|
|
|
|
|
}
|
77
|
197
|
100
|
|
|
|
1106
|
if ($string =~ /^($value_re)$/) { # 'x:10' or '10'
|
78
|
189
|
|
|
|
|
398
|
$range->_set_range_end($string);
|
79
|
189
|
|
|
|
|
887
|
$valid++;
|
80
|
|
|
|
|
|
|
}
|
81
|
|
|
|
|
|
|
|
82
|
197
|
100
|
100
|
|
|
725
|
if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) {
|
|
|
|
33
|
|
|
|
|
83
|
196
|
|
|
|
|
3570
|
return $range;
|
84
|
|
|
|
|
|
|
}
|
85
|
1
|
|
|
|
|
26
|
return undef;
|
86
|
|
|
|
|
|
|
}
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Returns 1 if an alert should be raised, otherwise 0
|
89
|
|
|
|
|
|
|
sub check_range {
|
90
|
195
|
|
|
195
|
0
|
84496
|
my ($self, $value) = @_;
|
91
|
195
|
|
|
|
|
263
|
my $false = 0;
|
92
|
195
|
|
|
|
|
388
|
my $true = 1;
|
93
|
195
|
100
|
|
|
|
570
|
if ($self->alert_on == INSIDE) {
|
94
|
16
|
|
|
|
|
104
|
$false = 1;
|
95
|
16
|
|
|
|
|
27
|
$true = 0;
|
96
|
|
|
|
|
|
|
}
|
97
|
195
|
100
|
100
|
|
|
1306
|
if ($self->end_infinity == 0 && $self->start_infinity == 0) {
|
|
|
100
|
66
|
|
|
|
|
|
|
50
|
33
|
|
|
|
|
98
|
101
|
100
|
100
|
|
|
1346
|
if ($self->start <= $value && $value <= $self->end) {
|
99
|
52
|
|
|
|
|
758
|
return $false;
|
100
|
|
|
|
|
|
|
} else {
|
101
|
49
|
|
|
|
|
699
|
return $true;
|
102
|
|
|
|
|
|
|
}
|
103
|
|
|
|
|
|
|
} elsif ($self->start_infinity == 0 && $self->end_infinity == 1) {
|
104
|
29
|
100
|
|
|
|
613
|
if ( $value >= $self->start ) {
|
105
|
14
|
|
|
|
|
120
|
return $false;
|
106
|
|
|
|
|
|
|
} else {
|
107
|
15
|
|
|
|
|
145
|
return $true;
|
108
|
|
|
|
|
|
|
}
|
109
|
|
|
|
|
|
|
} elsif ($self->start_infinity == 1 && $self->end_infinity == 0) {
|
110
|
65
|
100
|
|
|
|
1999
|
if ($value <= $self->end) {
|
111
|
40
|
|
|
|
|
328
|
return $false;
|
112
|
|
|
|
|
|
|
} else {
|
113
|
25
|
|
|
|
|
265
|
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
|
1220
|
shift->SUPER::new({ @_ });
|
124
|
|
|
|
|
|
|
}
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1;
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
__END__
|