line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Monitoring::Plugin; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
255152
|
use Monitoring::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES); |
|
6
|
|
|
|
|
17
|
|
|
6
|
|
|
|
|
959
|
|
4
|
6
|
|
|
6
|
|
33
|
use Params::Validate qw(:all); |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
650
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
81
|
use 5.006; |
|
6
|
|
|
|
|
15
|
|
7
|
6
|
|
|
6
|
|
46
|
use strict; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
116
|
|
8
|
6
|
|
|
6
|
|
26
|
use warnings; |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
160
|
|
9
|
|
|
|
|
|
|
|
10
|
6
|
|
|
6
|
|
27
|
use Carp; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
338
|
|
11
|
6
|
|
|
6
|
|
69
|
use base qw(Class::Accessor::Fast); |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
2391
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Monitoring::Plugin->mk_accessors(qw( |
14
|
|
|
|
|
|
|
shortname |
15
|
|
|
|
|
|
|
perfdata |
16
|
|
|
|
|
|
|
messages |
17
|
|
|
|
|
|
|
opts |
18
|
|
|
|
|
|
|
threshold |
19
|
|
|
|
|
|
|
)); |
20
|
|
|
|
|
|
|
|
21
|
6
|
|
|
6
|
|
12835
|
use Exporter; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
6080
|
|
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.40"; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub new { |
32
|
24
|
|
|
24
|
0
|
15601
|
my $class = shift; |
33
|
|
|
|
|
|
|
# my %args = @_; |
34
|
|
|
|
|
|
|
|
35
|
24
|
|
|
|
|
424
|
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
|
|
|
|
|
118
|
my $shortname = Monitoring::Plugin::Functions::get_shortname(\%args); |
50
|
24
|
100
|
|
|
|
58
|
delete $args{shortname} if (exists $args{shortname}); |
51
|
24
|
|
|
|
|
103
|
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
|
|
|
|
|
40
|
bless $self, $class; |
63
|
24
|
100
|
|
|
|
51
|
if (exists $args{usage}) { |
64
|
1
|
|
|
|
|
450
|
require Monitoring::Plugin::Getopt; |
65
|
1
|
|
|
|
|
14
|
$self->opts( new Monitoring::Plugin::Getopt(%args) ); |
66
|
|
|
|
|
|
|
} |
67
|
24
|
|
|
|
|
106
|
return $self; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub add_perfdata { |
71
|
2
|
|
|
2
|
1
|
37
|
my ($self, %args) = @_; |
72
|
2
|
|
|
|
|
389
|
require Monitoring::Plugin::Performance; |
73
|
2
|
|
|
|
|
9
|
my $perf = Monitoring::Plugin::Performance->new(%args); |
74
|
2
|
|
|
|
|
18
|
push @{$self->perfdata}, $perf; |
|
2
|
|
|
|
|
28
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
sub all_perfoutput { |
77
|
56
|
|
|
56
|
0
|
267
|
my $self = shift; |
78
|
56
|
|
|
|
|
59
|
return join(" ", map {$_->perfoutput} (@{$self->perfdata})); |
|
27
|
|
|
|
|
79
|
|
|
56
|
|
|
|
|
667
|
|
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub set_thresholds { |
82
|
6
|
|
|
6
|
1
|
543
|
my $self = shift; |
83
|
6
|
|
|
|
|
726
|
require Monitoring::Plugin::Threshold; |
84
|
6
|
|
|
|
|
21
|
return $self->threshold( Monitoring::Plugin::Threshold->set_thresholds(@_)); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# MP::Functions wrappers |
88
|
|
|
|
|
|
|
sub plugin_exit { |
89
|
19
|
|
|
19
|
1
|
5255
|
my $self = shift; |
90
|
19
|
|
|
|
|
87
|
Monitoring::Plugin::Functions::plugin_exit(@_, { plugin => $self }); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
sub plugin_die { |
93
|
23
|
|
|
23
|
1
|
2795
|
my $self = shift; |
94
|
23
|
|
|
|
|
100
|
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
|
9
|
my $self = shift; |
106
|
6
|
|
|
|
|
22
|
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
|
1613
|
my $self = shift; |
118
|
|
|
|
|
|
|
|
119
|
20
|
|
|
|
|
22
|
my %args; |
120
|
|
|
|
|
|
|
|
121
|
20
|
100
|
66
|
|
|
75
|
if ( $#_ == 0 && (! ref $_[0] || ref $_[0] eq "ARRAY" )) { # one positional param |
|
|
|
66
|
|
|
|
|
122
|
14
|
|
|
|
|
21
|
%args = (check => shift); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
else { |
125
|
6
|
|
|
|
|
219
|
%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
|
|
|
325
|
if ( exists $args{warning} || exists $args{critical} ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
137
|
|
|
|
|
|
|
$self->set_thresholds( |
138
|
|
|
|
|
|
|
warning => $args{warning}, |
139
|
|
|
|
|
|
|
critical => $args{critical}, |
140
|
3
|
|
|
|
|
7
|
); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
elsif ( defined $self->threshold ) { |
143
|
|
|
|
|
|
|
# noop |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
elsif ( defined $self->opts ) { |
146
|
1
|
|
|
|
|
32
|
$self->set_thresholds( |
147
|
|
|
|
|
|
|
warning => $self->opts->warning, |
148
|
|
|
|
|
|
|
critical => $self->opts->critical, |
149
|
|
|
|
|
|
|
); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
else { |
152
|
4
|
|
|
|
|
79
|
return UNKNOWN; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
15
|
|
|
|
|
309
|
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
|
639
|
my $self = shift; |
161
|
3
|
50
|
|
|
|
6
|
$self->opts->arg(@_) if $self->_check_for_opts; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
sub getopts { |
164
|
2
|
|
|
2
|
1
|
413
|
my $self = shift; |
165
|
2
|
50
|
|
|
|
5
|
$self->opts->getopts(@_) if $self->_check_for_opts; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub _check_for_opts { |
169
|
5
|
|
|
5
|
|
6
|
my $self = shift; |
170
|
5
|
100
|
|
|
|
90
|
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
|
|
|
|
|
54
|
return $self; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# ------------------------------------------------------------------------- |
179
|
|
|
|
|
|
|
# MP::Functions::check_messages helpers and wrappers |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub add_message { |
182
|
18
|
|
|
18
|
1
|
780
|
my $self = shift; |
183
|
18
|
|
|
|
|
29
|
my ($code, @messages) = @_; |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
croak "Invalid error code '$code'" |
186
|
18
|
100
|
100
|
|
|
268
|
unless defined($ERRORS{uc $code}) || defined($STATUS_TEXT{$code}); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# Store messages using strings rather than numeric codes |
189
|
16
|
100
|
|
|
|
29
|
$code = $STATUS_TEXT{$code} if $STATUS_TEXT{$code}; |
190
|
16
|
|
|
|
|
21
|
$code = lc $code; |
191
|
16
|
100
|
100
|
|
|
157
|
croak "Error code '$code' not supported by add_message" |
192
|
|
|
|
|
|
|
if $code eq 'unknown' || $code eq 'dependent'; |
193
|
|
|
|
|
|
|
|
194
|
14
|
50
|
|
|
|
226
|
$self->messages($code, []) unless $self->messages->{$code}; |
195
|
14
|
|
|
|
|
61
|
push @{$self->messages->{$code}}, @messages; |
|
14
|
|
|
|
|
184
|
|
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub check_messages { |
199
|
30
|
|
|
30
|
1
|
12222
|
my $self = shift; |
200
|
30
|
|
|
|
|
60
|
my %args = @_; |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# Add object messages to any passed in as args |
203
|
30
|
|
|
|
|
45
|
for my $code (qw(critical warning ok)) { |
204
|
90
|
|
50
|
|
|
1162
|
my $messages = $self->messages->{$code} || []; |
205
|
90
|
100
|
|
|
|
361
|
if ($args{$code}) { |
206
|
48
|
100
|
|
|
|
103
|
unless (ref $args{$code} eq 'ARRAY') { |
207
|
4
|
50
|
|
|
|
7
|
if ($code eq 'ok') { |
208
|
4
|
|
|
|
|
7
|
$args{$code} = [ $args{$code} ]; |
209
|
|
|
|
|
|
|
} else { |
210
|
0
|
|
|
|
|
0
|
croak "Invalid argument '$code'" |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
} |
213
|
48
|
|
|
|
|
47
|
push @{$args{$code}}, @$messages; |
|
48
|
|
|
|
|
83
|
|
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
else { |
216
|
42
|
|
|
|
|
59
|
$args{$code} = $messages; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
30
|
|
|
|
|
85
|
Monitoring::Plugin::Functions::check_messages(%args); |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
# ------------------------------------------------------------------------- |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
1; |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
#vim:et:sw=4 |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
__END__ |