line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Nagios::Monitoring::Plugin;
|
3
|
|
|
|
|
|
|
|
4
|
5
|
|
|
5
|
|
84238
|
use Nagios::Monitoring::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES);
|
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
1074
|
|
5
|
5
|
|
|
5
|
|
28
|
use Params::Validate qw(:all);
|
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
776
|
|
6
|
|
|
|
|
|
|
|
7
|
5
|
|
|
5
|
|
24
|
use strict;
|
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
94
|
|
8
|
5
|
|
|
5
|
|
25
|
use warnings;
|
|
5
|
|
|
|
|
27
|
|
|
5
|
|
|
|
|
159
|
|
9
|
|
|
|
|
|
|
|
10
|
5
|
|
|
5
|
|
23
|
use Carp;
|
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
317
|
|
11
|
5
|
|
|
5
|
|
32
|
use base qw(Class::Accessor::Fast);
|
|
5
|
|
|
|
|
17
|
|
|
5
|
|
|
|
|
3918
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Nagios::Monitoring::Plugin->mk_accessors(qw(
|
14
|
|
|
|
|
|
|
shortname
|
15
|
|
|
|
|
|
|
perfdata
|
16
|
|
|
|
|
|
|
messages
|
17
|
|
|
|
|
|
|
opts
|
18
|
|
|
|
|
|
|
threshold
|
19
|
|
|
|
|
|
|
));
|
20
|
|
|
|
|
|
|
|
21
|
5
|
|
|
5
|
|
16250
|
use Exporter;
|
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
5861
|
|
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 $Nagios::Monitoring::Plugin::Functions::VERSION too
|
29
|
|
|
|
|
|
|
our $VERSION = "0.51";
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub new {
|
32
|
24
|
|
|
24
|
0
|
34035
|
my $class = shift;
|
33
|
|
|
|
|
|
|
# my %args = @_;
|
34
|
|
|
|
|
|
|
|
35
|
24
|
|
|
|
|
772
|
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
|
|
|
|
|
188
|
my $shortname = Nagios::Monitoring::Plugin::Functions::get_shortname(\%args);
|
50
|
24
|
100
|
|
|
|
80
|
delete $args{shortname} if (exists $args{shortname});
|
51
|
24
|
|
|
|
|
155
|
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
|
|
|
|
|
58
|
bless $self, $class;
|
63
|
24
|
100
|
|
|
|
68
|
if (exists $args{usage}) {
|
64
|
1
|
|
|
|
|
884
|
require Nagios::Monitoring::Plugin::Getopt;
|
65
|
1
|
|
|
|
|
10
|
$self->opts( new Nagios::Monitoring::Plugin::Getopt(%args) );
|
66
|
|
|
|
|
|
|
}
|
67
|
24
|
|
|
|
|
103
|
return $self;
|
68
|
|
|
|
|
|
|
}
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub add_perfdata {
|
71
|
2
|
|
|
2
|
1
|
42
|
my ($self, %args) = @_;
|
72
|
2
|
|
|
|
|
925
|
require Nagios::Monitoring::Plugin::Performance;
|
73
|
2
|
|
|
|
|
14
|
my $perf = Nagios::Monitoring::Plugin::Performance->new(%args);
|
74
|
2
|
|
|
|
|
29
|
push @{$self->perfdata}, $perf;
|
|
2
|
|
|
|
|
9
|
|
75
|
|
|
|
|
|
|
}
|
76
|
|
|
|
|
|
|
sub all_perfoutput {
|
77
|
56
|
|
|
56
|
0
|
346
|
my $self = shift;
|
78
|
56
|
|
|
|
|
65
|
return join(" ", map {$_->perfoutput} (@{$self->perfdata}));
|
|
27
|
|
|
|
|
135
|
|
|
56
|
|
|
|
|
122
|
|
79
|
|
|
|
|
|
|
}
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub set_thresholds {
|
82
|
6
|
|
|
6
|
1
|
529
|
my $self = shift;
|
83
|
6
|
|
|
|
|
1479
|
require Nagios::Monitoring::Plugin::Threshold;
|
84
|
6
|
|
|
|
|
29
|
return $self->threshold( Nagios::Monitoring::Plugin::Threshold->set_thresholds(@_));
|
85
|
|
|
|
|
|
|
}
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# NP::Functions wrappers
|
88
|
|
|
|
|
|
|
sub nagios_exit {
|
89
|
19
|
|
|
19
|
1
|
4414
|
my $self = shift;
|
90
|
19
|
|
|
|
|
72
|
Nagios::Monitoring::Plugin::Functions::nagios_exit(@_, { plugin => $self });
|
91
|
|
|
|
|
|
|
}
|
92
|
|
|
|
|
|
|
sub nagios_die {
|
93
|
23
|
|
|
23
|
1
|
2387
|
my $self = shift;
|
94
|
23
|
|
|
|
|
92
|
Nagios::Monitoring::Plugin::Functions::nagios_die(@_, { plugin => $self });
|
95
|
|
|
|
|
|
|
}
|
96
|
|
|
|
|
|
|
sub die {
|
97
|
6
|
|
|
6
|
1
|
12
|
my $self = shift;
|
98
|
6
|
|
|
|
|
33
|
Nagios::Monitoring::Plugin::Functions::nagios_die(@_, { plugin => $self });
|
99
|
|
|
|
|
|
|
}
|
100
|
|
|
|
|
|
|
sub max_state {
|
101
|
0
|
|
|
0
|
1
|
0
|
Nagios::Monitoring::Plugin::Functions::max_state(@_);
|
102
|
|
|
|
|
|
|
}
|
103
|
|
|
|
|
|
|
sub max_state_alt {
|
104
|
0
|
|
|
0
|
1
|
0
|
Nagios::Monitoring::Plugin::Functions::max_state_alt(@_);
|
105
|
|
|
|
|
|
|
}
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# top level interface to Nagios::Monitoring::Plugin::Threshold
|
108
|
|
|
|
|
|
|
sub check_threshold {
|
109
|
20
|
|
|
20
|
1
|
2234
|
my $self = shift;
|
110
|
|
|
|
|
|
|
|
111
|
20
|
|
|
|
|
26
|
my %args;
|
112
|
|
|
|
|
|
|
|
113
|
20
|
100
|
66
|
|
|
105
|
if ( $#_ == 0 && (! ref $_[0] || ref $_[0] eq "ARRAY" )) { # one positional param
|
|
|
|
66
|
|
|
|
|
114
|
14
|
|
|
|
|
36
|
%args = (check => shift);
|
115
|
|
|
|
|
|
|
}
|
116
|
|
|
|
|
|
|
else {
|
117
|
6
|
|
|
|
|
319
|
%args = validate ( @_, { # named params
|
118
|
|
|
|
|
|
|
check => 1,
|
119
|
|
|
|
|
|
|
warning => 0,
|
120
|
|
|
|
|
|
|
critical => 0,
|
121
|
|
|
|
|
|
|
} );
|
122
|
|
|
|
|
|
|
}
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# in order of preference, get warning and critical from
|
125
|
|
|
|
|
|
|
# 1. explicit arguments to check_threshold
|
126
|
|
|
|
|
|
|
# 2. previously explicitly set threshold object
|
127
|
|
|
|
|
|
|
# 3. implicit options from Getopts object
|
128
|
19
|
100
|
66
|
|
|
107
|
if ( exists $args{warning} || exists $args{critical} ) {
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
129
|
|
|
|
|
|
|
$self->set_thresholds(
|
130
|
|
|
|
|
|
|
warning => $args{warning},
|
131
|
|
|
|
|
|
|
critical => $args{critical},
|
132
|
3
|
|
|
|
|
9
|
);
|
133
|
|
|
|
|
|
|
}
|
134
|
|
|
|
|
|
|
elsif ( defined $self->threshold ) {
|
135
|
|
|
|
|
|
|
# noop
|
136
|
|
|
|
|
|
|
}
|
137
|
|
|
|
|
|
|
elsif ( defined $self->opts ) {
|
138
|
1
|
|
|
|
|
16
|
$self->set_thresholds(
|
139
|
|
|
|
|
|
|
warning => $self->opts->warning,
|
140
|
|
|
|
|
|
|
critical => $self->opts->critical,
|
141
|
|
|
|
|
|
|
);
|
142
|
|
|
|
|
|
|
}
|
143
|
|
|
|
|
|
|
else {
|
144
|
4
|
|
|
|
|
47
|
return UNKNOWN;
|
145
|
|
|
|
|
|
|
}
|
146
|
|
|
|
|
|
|
|
147
|
15
|
|
|
|
|
180
|
return $self->threshold->get_status($args{check});
|
148
|
|
|
|
|
|
|
}
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# top level interface to my Nagios::Monitoring::Plugin::Getopt object
|
151
|
|
|
|
|
|
|
sub add_arg {
|
152
|
3
|
|
|
3
|
1
|
1332
|
my $self = shift;
|
153
|
3
|
50
|
|
|
|
10
|
$self->opts->arg(@_) if $self->_check_for_opts;
|
154
|
|
|
|
|
|
|
}
|
155
|
|
|
|
|
|
|
sub getopts {
|
156
|
2
|
|
|
2
|
1
|
609
|
my $self = shift;
|
157
|
2
|
50
|
|
|
|
5
|
$self->opts->getopts(@_) if $self->_check_for_opts;
|
158
|
|
|
|
|
|
|
}
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub _check_for_opts {
|
161
|
5
|
|
|
5
|
|
8
|
my $self = shift;
|
162
|
5
|
100
|
|
|
|
16
|
croak
|
163
|
|
|
|
|
|
|
"You have to supply a 'usage' param to Nagios::Monitoring::Plugin::new() if you want to use Getopts from your Nagios::Monitoring::Plugin object."
|
164
|
|
|
|
|
|
|
unless ref $self->opts() eq 'Nagios::Monitoring::Plugin::Getopt';
|
165
|
3
|
|
|
|
|
29
|
return $self;
|
166
|
|
|
|
|
|
|
}
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
171
|
|
|
|
|
|
|
# NP::Functions::check_messages helpers and wrappers
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub add_message {
|
174
|
18
|
|
|
18
|
1
|
1390
|
my $self = shift;
|
175
|
18
|
|
|
|
|
36
|
my ($code, @messages) = @_;
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
croak "Invalid error code '$code'"
|
178
|
18
|
100
|
100
|
|
|
372
|
unless defined($ERRORS{uc $code}) || defined($STATUS_TEXT{$code});
|
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# Store messages using strings rather than numeric codes
|
181
|
16
|
100
|
|
|
|
36
|
$code = $STATUS_TEXT{$code} if $STATUS_TEXT{$code};
|
182
|
16
|
|
|
|
|
20
|
$code = lc $code;
|
183
|
16
|
100
|
100
|
|
|
244
|
croak "Error code '$code' not supported by add_message"
|
184
|
|
|
|
|
|
|
if $code eq 'unknown' || $code eq 'dependent';
|
185
|
|
|
|
|
|
|
|
186
|
14
|
50
|
|
|
|
32
|
$self->messages($code, []) unless $self->messages->{$code};
|
187
|
14
|
|
|
|
|
106
|
push @{$self->messages->{$code}}, @messages;
|
|
14
|
|
|
|
|
34
|
|
188
|
|
|
|
|
|
|
}
|
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub check_messages {
|
191
|
30
|
|
|
30
|
1
|
27151
|
my $self = shift;
|
192
|
30
|
|
|
|
|
300
|
my %args = @_;
|
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
# Add object messages to any passed in as args
|
195
|
30
|
|
|
|
|
75
|
for my $code (qw(critical warning ok)) {
|
196
|
90
|
|
50
|
|
|
208
|
my $messages = $self->messages->{$code} || [];
|
197
|
90
|
100
|
|
|
|
735
|
if ($args{$code}) {
|
198
|
48
|
100
|
|
|
|
114
|
unless (ref $args{$code} eq 'ARRAY') {
|
199
|
4
|
50
|
|
|
|
12
|
if ($code eq 'ok') {
|
200
|
4
|
|
|
|
|
235
|
$args{$code} = [ $args{$code} ];
|
201
|
|
|
|
|
|
|
} else {
|
202
|
0
|
|
|
|
|
0
|
croak "Invalid argument '$code'"
|
203
|
|
|
|
|
|
|
}
|
204
|
|
|
|
|
|
|
}
|
205
|
48
|
|
|
|
|
54
|
push @{$args{$code}}, @$messages;
|
|
48
|
|
|
|
|
98
|
|
206
|
|
|
|
|
|
|
}
|
207
|
|
|
|
|
|
|
else {
|
208
|
42
|
|
|
|
|
91
|
$args{$code} = $messages;
|
209
|
|
|
|
|
|
|
}
|
210
|
|
|
|
|
|
|
}
|
211
|
|
|
|
|
|
|
|
212
|
30
|
|
|
|
|
108
|
Nagios::Monitoring::Plugin::Functions::check_messages(%args);
|
213
|
|
|
|
|
|
|
}
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
1;
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
#vim:et:sw=4
|
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
__END__
|