line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Monitoring::Plugin; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
65526
|
use Monitoring::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES); |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
919
|
|
4
|
6
|
|
|
6
|
|
24
|
use Params::Validate qw(:all); |
|
6
|
|
|
|
|
6
|
|
|
6
|
|
|
|
|
656
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
90
|
use 5.006; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
120
|
|
7
|
6
|
|
|
6
|
|
16
|
use strict; |
|
6
|
|
|
|
|
19
|
|
|
6
|
|
|
|
|
115
|
|
8
|
6
|
|
|
6
|
|
19
|
use warnings; |
|
6
|
|
|
|
|
5
|
|
|
6
|
|
|
|
|
144
|
|
9
|
|
|
|
|
|
|
|
10
|
6
|
|
|
6
|
|
17
|
use Carp; |
|
6
|
|
|
|
|
6
|
|
|
6
|
|
|
|
|
249
|
|
11
|
6
|
|
|
6
|
|
22
|
use base qw(Class::Accessor::Fast); |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
2537
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Monitoring::Plugin->mk_accessors(qw( |
14
|
|
|
|
|
|
|
shortname |
15
|
|
|
|
|
|
|
perfdata |
16
|
|
|
|
|
|
|
messages |
17
|
|
|
|
|
|
|
opts |
18
|
|
|
|
|
|
|
threshold |
19
|
|
|
|
|
|
|
)); |
20
|
|
|
|
|
|
|
|
21
|
6
|
|
|
6
|
|
10932
|
use Exporter; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
4317
|
|
22
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
23
|
|
|
|
|
|
|
our @EXPORT = (@STATUS_CODES); |
24
|
|
|
|
|
|
|
our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# CPAN stupidly won't index this module without a literal $VERSION here, |
27
|
|
|
|
|
|
|
# so we're forced to duplicate it explicitly |
28
|
|
|
|
|
|
|
# Make sure you update $Monitoring::Plugin::Functions::VERSION too |
29
|
|
|
|
|
|
|
our $VERSION = "0.39"; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub new { |
32
|
24
|
|
|
24
|
0
|
13477
|
my $class = shift; |
33
|
|
|
|
|
|
|
# my %args = @_; |
34
|
|
|
|
|
|
|
|
35
|
24
|
|
|
|
|
463
|
my %args = validate( @_, |
36
|
|
|
|
|
|
|
{ |
37
|
|
|
|
|
|
|
shortname => 0, |
38
|
|
|
|
|
|
|
usage => 0, |
39
|
|
|
|
|
|
|
version => 0, |
40
|
|
|
|
|
|
|
url => 0, |
41
|
|
|
|
|
|
|
plugin => 0, |
42
|
|
|
|
|
|
|
blurb => 0, |
43
|
|
|
|
|
|
|
extra => 0, |
44
|
|
|
|
|
|
|
license => 0, |
45
|
|
|
|
|
|
|
timeout => 0 |
46
|
|
|
|
|
|
|
}, |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
24
|
|
|
|
|
117
|
my $shortname = Monitoring::Plugin::Functions::get_shortname(\%args); |
50
|
24
|
100
|
|
|
|
57
|
delete $args{shortname} if (exists $args{shortname}); |
51
|
24
|
|
|
|
|
116
|
my $self = { |
52
|
|
|
|
|
|
|
shortname => $shortname, |
53
|
|
|
|
|
|
|
perfdata => [], # to be added later |
54
|
|
|
|
|
|
|
messages => { |
55
|
|
|
|
|
|
|
warning => [], |
56
|
|
|
|
|
|
|
critical => [], |
57
|
|
|
|
|
|
|
ok => [] |
58
|
|
|
|
|
|
|
}, |
59
|
|
|
|
|
|
|
opts => undef, # see below |
60
|
|
|
|
|
|
|
threshold => undef, # defined later |
61
|
|
|
|
|
|
|
}; |
62
|
24
|
|
|
|
|
43
|
bless $self, $class; |
63
|
24
|
100
|
|
|
|
47
|
if (exists $args{usage}) { |
64
|
1
|
|
|
|
|
410
|
require Monitoring::Plugin::Getopt; |
65
|
1
|
|
|
|
|
8
|
$self->opts( new Monitoring::Plugin::Getopt(%args) ); |
66
|
|
|
|
|
|
|
} |
67
|
24
|
|
|
|
|
71
|
return $self; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub add_perfdata { |
71
|
2
|
|
|
2
|
1
|
47
|
my ($self, %args) = @_; |
72
|
2
|
|
|
|
|
374
|
require Monitoring::Plugin::Performance; |
73
|
2
|
|
|
|
|
9
|
my $perf = Monitoring::Plugin::Performance->new(%args); |
74
|
2
|
|
|
|
|
18
|
push @{$self->perfdata}, $perf; |
|
2
|
|
|
|
|
6
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
sub all_perfoutput { |
77
|
56
|
|
|
56
|
0
|
206
|
my $self = shift; |
78
|
56
|
|
|
|
|
43
|
return join(" ", map {$_->perfoutput} (@{$self->perfdata})); |
|
27
|
|
|
|
|
68
|
|
|
56
|
|
|
|
|
64
|
|
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub set_thresholds { |
82
|
6
|
|
|
6
|
1
|
353
|
my $self = shift; |
83
|
6
|
|
|
|
|
852
|
require Monitoring::Plugin::Threshold; |
84
|
6
|
|
|
|
|
16
|
return $self->threshold( Monitoring::Plugin::Threshold->set_thresholds(@_)); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# MP::Functions wrappers |
88
|
|
|
|
|
|
|
sub plugin_exit { |
89
|
19
|
|
|
19
|
1
|
4673
|
my $self = shift; |
90
|
19
|
|
|
|
|
67
|
Monitoring::Plugin::Functions::plugin_exit(@_, { plugin => $self }); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
sub plugin_die { |
93
|
23
|
|
|
23
|
1
|
2560
|
my $self = shift; |
94
|
23
|
|
|
|
|
79
|
Monitoring::Plugin::Functions::plugin_die(@_, { plugin => $self }); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
sub nagios_exit { |
97
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
98
|
0
|
|
|
|
|
0
|
Monitoring::Plugin::Functions::plugin_exit(@_, { plugin => $self }); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
sub nagios_die { |
101
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
102
|
0
|
|
|
|
|
0
|
Monitoring::Plugin::Functions::plugin_die(@_, { plugin => $self }); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
sub die { |
105
|
6
|
|
|
6
|
1
|
4
|
my $self = shift; |
106
|
6
|
|
|
|
|
20
|
Monitoring::Plugin::Functions::plugin_die(@_, { plugin => $self }); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
sub max_state { |
109
|
0
|
|
|
0
|
1
|
0
|
Monitoring::Plugin::Functions::max_state(@_); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
sub max_state_alt { |
112
|
0
|
|
|
0
|
1
|
0
|
Monitoring::Plugin::Functions::max_state_alt(@_); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# top level interface to Monitoring::Plugin::Threshold |
116
|
|
|
|
|
|
|
sub check_threshold { |
117
|
20
|
|
|
20
|
1
|
1555
|
my $self = shift; |
118
|
|
|
|
|
|
|
|
119
|
20
|
|
|
|
|
18
|
my %args; |
120
|
|
|
|
|
|
|
|
121
|
20
|
100
|
66
|
|
|
76
|
if ( $#_ == 0 && (! ref $_[0] || ref $_[0] eq "ARRAY" )) { # one positional param |
|
|
|
66
|
|
|
|
|
122
|
14
|
|
|
|
|
21
|
%args = (check => shift); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
else { |
125
|
6
|
|
|
|
|
232
|
%args = validate ( @_, { # named params |
126
|
|
|
|
|
|
|
check => 1, |
127
|
|
|
|
|
|
|
warning => 0, |
128
|
|
|
|
|
|
|
critical => 0, |
129
|
|
|
|
|
|
|
} ); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# in order of preference, get warning and critical from |
133
|
|
|
|
|
|
|
# 1. explicit arguments to check_threshold |
134
|
|
|
|
|
|
|
# 2. previously explicitly set threshold object |
135
|
|
|
|
|
|
|
# 3. implicit options from Getopts object |
136
|
19
|
100
|
66
|
|
|
85
|
if ( exists $args{warning} || exists $args{critical} ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
137
|
3
|
|
|
|
|
12
|
$self->set_thresholds( |
138
|
|
|
|
|
|
|
warning => $args{warning}, |
139
|
|
|
|
|
|
|
critical => $args{critical}, |
140
|
|
|
|
|
|
|
); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
elsif ( defined $self->threshold ) { |
143
|
|
|
|
|
|
|
# noop |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
elsif ( defined $self->opts ) { |
146
|
1
|
|
|
|
|
11
|
$self->set_thresholds( |
147
|
|
|
|
|
|
|
warning => $self->opts->warning, |
148
|
|
|
|
|
|
|
critical => $self->opts->critical, |
149
|
|
|
|
|
|
|
); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
else { |
152
|
4
|
|
|
|
|
32
|
return UNKNOWN; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
15
|
|
|
|
|
99
|
return $self->threshold->get_status($args{check}); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# top level interface to my Monitoring::Plugin::Getopt object |
159
|
|
|
|
|
|
|
sub add_arg { |
160
|
3
|
|
|
3
|
1
|
833
|
my $self = shift; |
161
|
3
|
50
|
|
|
|
7
|
$self->opts->arg(@_) if $self->_check_for_opts; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
sub getopts { |
164
|
2
|
|
|
2
|
1
|
456
|
my $self = shift; |
165
|
2
|
50
|
|
|
|
4
|
$self->opts->getopts(@_) if $self->_check_for_opts; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub _check_for_opts { |
169
|
5
|
|
|
5
|
|
3
|
my $self = shift; |
170
|
5
|
100
|
|
|
|
11
|
croak |
171
|
|
|
|
|
|
|
"You have to supply a 'usage' param to Monitoring::Plugin::new() if you want to use Getopts from your Monitoring::Plugin object." |
172
|
|
|
|
|
|
|
unless ref $self->opts() eq 'Monitoring::Plugin::Getopt'; |
173
|
3
|
|
|
|
|
16
|
return $self; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# ------------------------------------------------------------------------- |
179
|
|
|
|
|
|
|
# MP::Functions::check_messages helpers and wrappers |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub add_message { |
182
|
18
|
|
|
18
|
1
|
986
|
my $self = shift; |
183
|
18
|
|
|
|
|
23
|
my ($code, @messages) = @_; |
184
|
|
|
|
|
|
|
|
185
|
18
|
100
|
100
|
|
|
260
|
croak "Invalid error code '$code'" |
186
|
|
|
|
|
|
|
unless defined($ERRORS{uc $code}) || defined($STATUS_TEXT{$code}); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# Store messages using strings rather than numeric codes |
189
|
16
|
100
|
|
|
|
22
|
$code = $STATUS_TEXT{$code} if $STATUS_TEXT{$code}; |
190
|
16
|
|
|
|
|
16
|
$code = lc $code; |
191
|
16
|
100
|
100
|
|
|
181
|
croak "Error code '$code' not supported by add_message" |
192
|
|
|
|
|
|
|
if $code eq 'unknown' || $code eq 'dependent'; |
193
|
|
|
|
|
|
|
|
194
|
14
|
50
|
|
|
|
24
|
$self->messages($code, []) unless $self->messages->{$code}; |
195
|
14
|
|
|
|
|
48
|
push @{$self->messages->{$code}}, @messages; |
|
14
|
|
|
|
|
22
|
|
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub check_messages { |
199
|
30
|
|
|
30
|
1
|
10689
|
my $self = shift; |
200
|
30
|
|
|
|
|
57
|
my %args = @_; |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# Add object messages to any passed in as args |
203
|
30
|
|
|
|
|
41
|
for my $code (qw(critical warning ok)) { |
204
|
90
|
|
50
|
|
|
121
|
my $messages = $self->messages->{$code} || []; |
205
|
90
|
100
|
|
|
|
303
|
if ($args{$code}) { |
206
|
48
|
100
|
|
|
|
74
|
unless (ref $args{$code} eq 'ARRAY') { |
207
|
4
|
50
|
|
|
|
5
|
if ($code eq 'ok') { |
208
|
4
|
|
|
|
|
9
|
$args{$code} = [ $args{$code} ]; |
209
|
|
|
|
|
|
|
} else { |
210
|
0
|
|
|
|
|
0
|
croak "Invalid argument '$code'" |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
} |
213
|
48
|
|
|
|
|
30
|
push @{$args{$code}}, @$messages; |
|
48
|
|
|
|
|
66
|
|
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
else { |
216
|
42
|
|
|
|
|
61
|
$args{$code} = $messages; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
30
|
|
|
|
|
82
|
Monitoring::Plugin::Functions::check_messages(%args); |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
# ------------------------------------------------------------------------- |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
1; |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
#vim:et:sw=4 |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
__END__ |