line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Monitoring::Plugin::Functions; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Functional interface to basic Monitoring::Plugin constants, exports, |
4
|
|
|
|
|
|
|
# and functions |
5
|
|
|
|
|
|
|
|
6
|
17
|
|
|
17
|
|
87590
|
use 5.006; |
|
17
|
|
|
|
|
53
|
|
|
17
|
|
|
|
|
732
|
|
7
|
17
|
|
|
17
|
|
81
|
use strict; |
|
17
|
|
|
|
|
22
|
|
|
17
|
|
|
|
|
647
|
|
8
|
17
|
|
|
17
|
|
73
|
use warnings; |
|
17
|
|
|
|
|
26
|
|
|
17
|
|
|
|
|
500
|
|
9
|
|
|
|
|
|
|
|
10
|
17
|
|
|
17
|
|
71
|
use File::Basename; |
|
17
|
|
|
|
|
20
|
|
|
17
|
|
|
|
|
1477
|
|
11
|
17
|
|
|
17
|
|
8023
|
use Params::Validate qw(:types validate); |
|
17
|
|
|
|
|
130550
|
|
|
17
|
|
|
|
|
3445
|
|
12
|
17
|
|
|
17
|
|
8615
|
use Math::Calc::Units; |
|
17
|
|
|
|
|
427236
|
|
|
17
|
|
|
|
|
2592
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Remember to update Monitoring::Plugins as well |
15
|
|
|
|
|
|
|
our $VERSION = "0.38"; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
require Exporter; |
20
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
21
|
|
|
|
|
|
|
our @EXPORT = (@STATUS_CODES, qw(plugin_exit plugin_die check_messages)); |
22
|
|
|
|
|
|
|
our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state max_state_alt convert $value_re); |
23
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
24
|
|
|
|
|
|
|
all => [ @EXPORT, @EXPORT_OK ], |
25
|
|
|
|
|
|
|
codes => [ @STATUS_CODES ], |
26
|
|
|
|
|
|
|
functions => [ qw(plugin_exit plugin_die check_messages max_state max_state_alt convert) ], |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
17
|
|
|
17
|
|
147
|
use constant OK => 0; |
|
17
|
|
|
|
|
33
|
|
|
17
|
|
|
|
|
972
|
|
30
|
17
|
|
|
17
|
|
90
|
use constant WARNING => 1; |
|
17
|
|
|
|
|
27
|
|
|
17
|
|
|
|
|
705
|
|
31
|
17
|
|
|
17
|
|
74
|
use constant CRITICAL => 2; |
|
17
|
|
|
|
|
26
|
|
|
17
|
|
|
|
|
775
|
|
32
|
17
|
|
|
17
|
|
137
|
use constant UNKNOWN => 3; |
|
17
|
|
|
|
|
29
|
|
|
17
|
|
|
|
|
795
|
|
33
|
17
|
|
|
17
|
|
76
|
use constant DEPENDENT => 4; |
|
17
|
|
|
|
|
21
|
|
|
17
|
|
|
|
|
26295
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our %ERRORS = ( |
36
|
|
|
|
|
|
|
'OK' => OK, |
37
|
|
|
|
|
|
|
'WARNING' => WARNING, |
38
|
|
|
|
|
|
|
'CRITICAL' => CRITICAL, |
39
|
|
|
|
|
|
|
'UNKNOWN' => UNKNOWN, |
40
|
|
|
|
|
|
|
'DEPENDENT' => DEPENDENT, |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
our %STATUS_TEXT = reverse %ERRORS; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $value = qr/[-+]?[\d\.]+/; |
46
|
|
|
|
|
|
|
our $value_re = qr/$value(?:e$value)?/; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# _fake_exit flag and accessor/mutator, for testing |
49
|
|
|
|
|
|
|
my $_fake_exit = 0; |
50
|
9
|
50
|
|
9
|
|
408
|
sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# _use_die flag and accessor/mutator, so exceptions can be raised correctly |
53
|
|
|
|
|
|
|
my $_use_die = 0; |
54
|
10
|
50
|
|
10
|
|
110
|
sub _use_die { @_ ? $_use_die = shift : $_use_die }; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub get_shortname { |
57
|
69
|
|
|
69
|
1
|
1397
|
my $arg = shift; |
58
|
|
|
|
|
|
|
|
59
|
69
|
|
|
|
|
76
|
my $shortname = undef; |
60
|
|
|
|
|
|
|
|
61
|
69
|
100
|
|
|
|
471
|
return $arg->{shortname} if (defined($arg->{shortname})); |
62
|
65
|
100
|
|
|
|
135
|
$shortname = $arg->{plugin} if (defined( $arg->{plugin})); |
63
|
|
|
|
|
|
|
|
64
|
65
|
|
33
|
|
|
2658
|
$shortname = uc basename($shortname || $ENV{PLUGIN_NAME} || $ENV{NAGIOS_PLUGIN} || $0); |
65
|
65
|
|
|
|
|
121
|
$shortname =~ s/^CHECK_(?:BY_)?//; # Remove any leading CHECK_[BY_] |
66
|
65
|
|
|
|
|
205
|
$shortname =~ s/\..*$//; # Remove any trailing suffix |
67
|
65
|
|
|
|
|
191
|
return $shortname; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub max_state { |
71
|
7
|
100
|
|
7
|
1
|
1129
|
return CRITICAL if grep { $_ == CRITICAL } @_; |
|
16
|
|
|
|
|
31
|
|
72
|
6
|
100
|
|
|
|
7
|
return WARNING if grep { $_ == WARNING } @_; |
|
14
|
|
|
|
|
24
|
|
73
|
3
|
100
|
|
|
|
5
|
return OK if grep { $_ == OK } @_; |
|
4
|
|
|
|
|
13
|
|
74
|
1
|
50
|
|
|
|
4
|
return UNKNOWN if grep { $_ == UNKNOWN } @_; |
|
0
|
|
|
|
|
0
|
|
75
|
1
|
50
|
|
|
|
18
|
return DEPENDENT if grep { $_ == DEPENDENT } @_; |
|
0
|
|
|
|
|
0
|
|
76
|
1
|
|
|
|
|
4
|
return UNKNOWN; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub max_state_alt { |
80
|
7
|
100
|
|
7
|
1
|
697
|
return CRITICAL if grep { $_ == CRITICAL } @_; |
|
16
|
|
|
|
|
30
|
|
81
|
6
|
100
|
|
|
|
9
|
return WARNING if grep { $_ == WARNING } @_; |
|
14
|
|
|
|
|
28
|
|
82
|
3
|
100
|
|
|
|
4
|
return UNKNOWN if grep { $_ == UNKNOWN } @_; |
|
4
|
|
|
|
|
10
|
|
83
|
2
|
50
|
|
|
|
5
|
return DEPENDENT if grep { $_ == DEPENDENT } @_; |
|
2
|
|
|
|
|
4
|
|
84
|
2
|
100
|
|
|
|
2
|
return OK if grep { $_ == OK } @_; |
|
2
|
|
|
|
|
6
|
|
85
|
1
|
|
|
|
|
5
|
return UNKNOWN; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# plugin_exit( $code, $message ) |
89
|
|
|
|
|
|
|
sub plugin_exit { |
90
|
92
|
|
|
92
|
1
|
3387
|
my ($code, $message, $arg) = @_; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Handle named parameters |
93
|
92
|
100
|
66
|
|
|
681
|
if (defined $code && ($code eq 'return_code' || $code eq 'message')) { |
|
|
|
66
|
|
|
|
|
94
|
|
|
|
|
|
|
# Remove last argument if odd no and last is ref |
95
|
6
|
50
|
33
|
|
|
36
|
if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) { |
96
|
6
|
|
|
|
|
9
|
$arg = pop @_; |
97
|
|
|
|
|
|
|
} else { |
98
|
0
|
|
|
|
|
0
|
undef $arg; |
99
|
|
|
|
|
|
|
} |
100
|
6
|
|
|
|
|
13
|
my %arg = @_; |
101
|
6
|
|
|
|
|
7
|
$code = $arg{return_code}; |
102
|
6
|
|
|
|
|
7
|
$message = $arg{message}; |
103
|
|
|
|
|
|
|
} |
104
|
92
|
|
100
|
|
|
283
|
$arg ||= {}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# Handle string codes |
107
|
92
|
100
|
100
|
|
|
471
|
$code = $ERRORS{$code} if defined $code && exists $ERRORS{$code}; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Set defaults |
110
|
92
|
100
|
100
|
|
|
545
|
$code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code}; |
111
|
92
|
100
|
|
|
|
1545
|
$message = '' unless defined $message; |
112
|
92
|
50
|
33
|
|
|
390
|
if (ref $message && ref $message eq 'ARRAY') { |
113
|
0
|
|
|
|
|
0
|
$message = join(' ', map { chomp; $_ } @$message); |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
else { |
116
|
92
|
|
|
|
|
202
|
chomp $message; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Setup output |
120
|
92
|
|
|
|
|
288
|
my $output = "$STATUS_TEXT{$code}"; |
121
|
92
|
100
|
66
|
|
|
928
|
$output .= " - $message" if defined $message && $message ne ''; |
122
|
92
|
100
|
|
|
|
537
|
my $shortname = ($arg->{plugin} ? $arg->{plugin}->shortname : undef); |
123
|
92
|
|
66
|
|
|
515
|
$shortname ||= get_shortname(); # Should happen only if funnctions are called directly |
124
|
92
|
50
|
|
|
|
438
|
$output = "$shortname $output" if $shortname; |
125
|
92
|
100
|
|
|
|
257
|
if ($arg->{plugin}) { |
126
|
48
|
|
|
|
|
90
|
my $plugin = $arg->{plugin}; |
127
|
48
|
100
|
66
|
|
|
217
|
$output .= " | ". $plugin->all_perfoutput |
128
|
|
|
|
|
|
|
if $plugin->perfdata && $plugin->all_perfoutput; |
129
|
|
|
|
|
|
|
} |
130
|
92
|
|
|
|
|
1261
|
$output .= "\n"; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# Don't actually exit if _fake_exit set |
133
|
92
|
100
|
|
|
|
346
|
if ($_fake_exit) { |
134
|
89
|
|
|
|
|
3011
|
require Monitoring::Plugin::ExitResult; |
135
|
89
|
|
|
|
|
436
|
return Monitoring::Plugin::ExitResult->new($code, $output); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
3
|
|
|
|
|
7
|
_plugin_exit($code, $output); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub _plugin_exit { |
142
|
17
|
|
|
17
|
|
25
|
my ($code, $output) = @_; |
143
|
|
|
|
|
|
|
# Print output and exit; die if flag set and called via a die in stack backtrace |
144
|
17
|
100
|
|
|
|
38
|
if ($_use_die) { |
145
|
16
|
|
|
|
|
24
|
for (my $i = 0;; $i++) { |
146
|
34
|
|
|
|
|
231
|
@_ = caller($i); |
147
|
34
|
50
|
|
|
|
81
|
last unless @_; |
148
|
34
|
100
|
|
|
|
90
|
if ($_[3] =~ m/die/) { |
149
|
16
|
|
|
|
|
32
|
$! = $code; |
150
|
16
|
|
|
|
|
175
|
die($output); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
1
|
|
|
|
|
153
|
print $output; |
155
|
1
|
|
|
|
|
30
|
exit $code; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# plugin_die( $message, [ $code ]) OR plugin_die( $code, $message ) |
159
|
|
|
|
|
|
|
# Default $code: UNKNOWN |
160
|
|
|
|
|
|
|
sub plugin_die { |
161
|
55
|
|
|
55
|
1
|
3323
|
my ($arg1, $arg2, $rest) = @_; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# Named parameters |
164
|
55
|
100
|
66
|
|
|
1028
|
if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) { |
|
|
100
|
66
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
165
|
6
|
|
|
|
|
10
|
return plugin_exit(@_); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# ($code, $message) |
169
|
|
|
|
|
|
|
elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) { |
170
|
20
|
|
|
|
|
68
|
return plugin_exit(@_); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# ($message, $code) |
174
|
|
|
|
|
|
|
elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) { |
175
|
21
|
|
|
|
|
66
|
return plugin_exit($arg2, $arg1, $rest); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Else just assume $arg1 is the message and hope for the best |
179
|
|
|
|
|
|
|
else { |
180
|
8
|
|
|
|
|
27
|
return plugin_exit( UNKNOWN, $arg1, $arg2 ); |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# For backwards compatibility |
185
|
0
|
|
|
0
|
0
|
0
|
sub die { plugin_die(@_); } |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
189
|
|
|
|
|
|
|
# Utility functions |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# Simple wrapper around Math::Calc::Units::convert |
192
|
|
|
|
|
|
|
sub convert |
193
|
|
|
|
|
|
|
{ |
194
|
0
|
|
|
0
|
0
|
0
|
my ($value, $from, $to) = @_; |
195
|
0
|
|
|
|
|
0
|
my ($newval) = Math::Calc::Units::convert("$value $from", $to, 'exact'); |
196
|
0
|
|
|
|
|
0
|
return $newval; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
200
|
|
|
|
|
|
|
# check_messages - return a status and/or message based on a set of |
201
|
|
|
|
|
|
|
# message arrays. |
202
|
|
|
|
|
|
|
# Returns a nagios status code in scalar context. |
203
|
|
|
|
|
|
|
# Returns a code and a message in list context. |
204
|
|
|
|
|
|
|
# The message is join($join, @array) for the relevant array for the code, |
205
|
|
|
|
|
|
|
# or join($join_all, $message) for all arrays if $join_all is set. |
206
|
|
|
|
|
|
|
sub check_messages { |
207
|
53
|
|
|
53
|
1
|
16168
|
my %arg = validate( @_, { |
208
|
|
|
|
|
|
|
critical => { type => ARRAYREF }, |
209
|
|
|
|
|
|
|
warning => { type => ARRAYREF }, |
210
|
|
|
|
|
|
|
ok => { type => ARRAYREF | SCALAR, optional => 1 }, |
211
|
|
|
|
|
|
|
'join' => { default => ' ' }, |
212
|
|
|
|
|
|
|
join_all => 0, |
213
|
|
|
|
|
|
|
}); |
214
|
50
|
100
|
|
|
|
399
|
$arg{join} = ' ' unless defined $arg{join}; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# Decide $code |
217
|
50
|
|
|
|
|
63
|
my $code = OK; |
218
|
50
|
100
|
50
|
|
|
56
|
$code ||= CRITICAL if @{$arg{critical}}; |
|
50
|
|
|
|
|
191
|
|
219
|
50
|
100
|
100
|
|
|
57
|
$code ||= WARNING if @{$arg{warning}}; |
|
50
|
|
|
|
|
178
|
|
220
|
50
|
100
|
|
|
|
123
|
return $code unless wantarray; |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# Compose message |
223
|
44
|
|
|
|
|
62
|
my $message = ''; |
224
|
44
|
100
|
|
|
|
90
|
if ($arg{join_all}) { |
225
|
36
|
100
|
|
|
|
120
|
$message = join( $arg{join_all}, |
226
|
12
|
100
|
|
|
|
64
|
map { @$_ ? join( $arg{'join'}, @$_) : () } |
|
|
100
|
|
|
|
|
|
227
|
|
|
|
|
|
|
$arg{critical}, |
228
|
|
|
|
|
|
|
$arg{warning}, |
229
|
|
|
|
|
|
|
$arg{ok} ? (ref $arg{ok} ? $arg{ok} : [ $arg{ok} ]) : [] |
230
|
|
|
|
|
|
|
); |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
else { |
234
|
32
|
100
|
33
|
|
|
95
|
$message ||= join( $arg{'join'}, @{$arg{critical}} ) |
|
13
|
|
|
|
|
64
|
|
235
|
|
|
|
|
|
|
if $code == CRITICAL; |
236
|
32
|
100
|
33
|
|
|
85
|
$message ||= join( $arg{'join'}, @{$arg{warning}} ) |
|
10
|
|
|
|
|
40
|
|
237
|
|
|
|
|
|
|
if $code == WARNING; |
238
|
32
|
100
|
100
|
|
|
135
|
$message ||= ref $arg{ok} ? join( $arg{'join'}, @{$arg{ok}} ) : $arg{ok} |
|
8
|
100
|
|
|
|
40
|
|
239
|
|
|
|
|
|
|
if $arg{ok}; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
44
|
|
|
|
|
288
|
return ($code, $message); |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
1; |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
# vim:sw=4:sm:et |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
__END__ |