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
|
|
66545
|
use 5.006; |
|
17
|
|
|
|
|
59
|
|
|
17
|
|
|
|
|
561
|
|
7
|
17
|
|
|
17
|
|
62
|
use strict; |
|
17
|
|
|
|
|
18
|
|
|
17
|
|
|
|
|
437
|
|
8
|
17
|
|
|
17
|
|
61
|
use warnings; |
|
17
|
|
|
|
|
16
|
|
|
17
|
|
|
|
|
417
|
|
9
|
|
|
|
|
|
|
|
10
|
17
|
|
|
17
|
|
60
|
use File::Basename; |
|
17
|
|
|
|
|
18
|
|
|
17
|
|
|
|
|
1222
|
|
11
|
17
|
|
|
17
|
|
5532
|
use Params::Validate qw(:types validate); |
|
17
|
|
|
|
|
76925
|
|
|
17
|
|
|
|
|
2454
|
|
12
|
17
|
|
|
17
|
|
6829
|
use Math::Calc::Units; |
|
17
|
|
|
|
|
304197
|
|
|
17
|
|
|
|
|
1912
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Remember to update Monitoring::Plugins as well |
15
|
|
|
|
|
|
|
our $VERSION = "0.39"; |
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
|
|
112
|
use constant OK => 0; |
|
17
|
|
|
|
|
28
|
|
|
17
|
|
|
|
|
634
|
|
30
|
17
|
|
|
17
|
|
63
|
use constant WARNING => 1; |
|
17
|
|
|
|
|
23
|
|
|
17
|
|
|
|
|
551
|
|
31
|
17
|
|
|
17
|
|
57
|
use constant CRITICAL => 2; |
|
17
|
|
|
|
|
22
|
|
|
17
|
|
|
|
|
583
|
|
32
|
17
|
|
|
17
|
|
102
|
use constant UNKNOWN => 3; |
|
17
|
|
|
|
|
16
|
|
|
17
|
|
|
|
|
589
|
|
33
|
17
|
|
|
17
|
|
54
|
use constant DEPENDENT => 4; |
|
17
|
|
|
|
|
18
|
|
|
17
|
|
|
|
|
19422
|
|
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
|
|
240
|
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
|
|
89
|
sub _use_die { @_ ? $_use_die = shift : $_use_die }; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub get_shortname { |
57
|
69
|
|
|
69
|
1
|
1082
|
my $arg = shift; |
58
|
|
|
|
|
|
|
|
59
|
69
|
|
|
|
|
53
|
my $shortname = undef; |
60
|
|
|
|
|
|
|
|
61
|
69
|
100
|
|
|
|
134
|
return $arg->{shortname} if (defined($arg->{shortname})); |
62
|
65
|
100
|
|
|
|
90
|
$shortname = $arg->{plugin} if (defined( $arg->{plugin})); |
63
|
|
|
|
|
|
|
|
64
|
65
|
|
33
|
|
|
1886
|
$shortname = uc basename($shortname || $ENV{PLUGIN_NAME} || $ENV{NAGIOS_PLUGIN} || $0); |
65
|
65
|
|
|
|
|
74
|
$shortname =~ s/^CHECK_(?:BY_)?//; # Remove any leading CHECK_[BY_] |
66
|
65
|
|
|
|
|
163
|
$shortname =~ s/\..*$//; # Remove any trailing suffix |
67
|
65
|
|
|
|
|
138
|
return $shortname; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub max_state { |
71
|
7
|
100
|
|
7
|
1
|
625
|
return CRITICAL if grep { $_ == CRITICAL } @_; |
|
16
|
|
|
|
|
29
|
|
72
|
6
|
100
|
|
|
|
7
|
return WARNING if grep { $_ == WARNING } @_; |
|
14
|
|
|
|
|
22
|
|
73
|
3
|
100
|
|
|
|
3
|
return OK if grep { $_ == OK } @_; |
|
4
|
|
|
|
|
11
|
|
74
|
1
|
50
|
|
|
|
3
|
return UNKNOWN if grep { $_ == UNKNOWN } @_; |
|
0
|
|
|
|
|
0
|
|
75
|
1
|
50
|
|
|
|
32
|
return DEPENDENT if grep { $_ == DEPENDENT } @_; |
|
0
|
|
|
|
|
0
|
|
76
|
1
|
|
|
|
|
5
|
return UNKNOWN; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub max_state_alt { |
80
|
7
|
100
|
|
7
|
1
|
711
|
return CRITICAL if grep { $_ == CRITICAL } @_; |
|
16
|
|
|
|
|
25
|
|
81
|
6
|
100
|
|
|
|
7
|
return WARNING if grep { $_ == WARNING } @_; |
|
14
|
|
|
|
|
22
|
|
82
|
3
|
100
|
|
|
|
5
|
return UNKNOWN if grep { $_ == UNKNOWN } @_; |
|
4
|
|
|
|
|
10
|
|
83
|
2
|
50
|
|
|
|
3
|
return DEPENDENT if grep { $_ == DEPENDENT } @_; |
|
2
|
|
|
|
|
4
|
|
84
|
2
|
100
|
|
|
|
4
|
return OK if grep { $_ == OK } @_; |
|
2
|
|
|
|
|
5
|
|
85
|
1
|
|
|
|
|
4
|
return UNKNOWN; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# plugin_exit( $code, $message ) |
89
|
|
|
|
|
|
|
sub plugin_exit { |
90
|
92
|
|
|
92
|
1
|
2632
|
my ($code, $message, $arg) = @_; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Handle named parameters |
93
|
92
|
100
|
66
|
|
|
385
|
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
|
|
|
34
|
if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) { |
96
|
6
|
|
|
|
|
8
|
$arg = pop @_; |
97
|
|
|
|
|
|
|
} else { |
98
|
0
|
|
|
|
|
0
|
undef $arg; |
99
|
|
|
|
|
|
|
} |
100
|
6
|
|
|
|
|
13
|
my %arg = @_; |
101
|
6
|
|
|
|
|
5
|
$code = $arg{return_code}; |
102
|
6
|
|
|
|
|
10
|
$message = $arg{message}; |
103
|
|
|
|
|
|
|
} |
104
|
92
|
|
100
|
|
|
171
|
$arg ||= {}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# Handle string codes |
107
|
92
|
100
|
100
|
|
|
313
|
$code = $ERRORS{$code} if defined $code && exists $ERRORS{$code}; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Set defaults |
110
|
92
|
100
|
100
|
|
|
260
|
$code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code}; |
111
|
92
|
100
|
|
|
|
109
|
$message = '' unless defined $message; |
112
|
92
|
50
|
33
|
|
|
163
|
if (ref $message && ref $message eq 'ARRAY') { |
113
|
0
|
|
|
|
|
0
|
$message = join(' ', map { chomp; $_ } @$message); |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
else { |
116
|
92
|
|
|
|
|
107
|
chomp $message; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Setup output |
120
|
92
|
|
|
|
|
119
|
my $output = "$STATUS_TEXT{$code}"; |
121
|
92
|
100
|
66
|
|
|
285
|
$output .= " - $message" if defined $message && $message ne ''; |
122
|
92
|
100
|
|
|
|
193
|
my $shortname = ($arg->{plugin} ? $arg->{plugin}->shortname : undef); |
123
|
92
|
|
66
|
|
|
251
|
$shortname ||= get_shortname(); # Should happen only if funnctions are called directly |
124
|
92
|
50
|
|
|
|
177
|
$output = "$shortname $output" if $shortname; |
125
|
92
|
100
|
|
|
|
124
|
if ($arg->{plugin}) { |
126
|
48
|
|
|
|
|
33
|
my $plugin = $arg->{plugin}; |
127
|
48
|
100
|
66
|
|
|
77
|
$output .= " | ". $plugin->all_perfoutput |
128
|
|
|
|
|
|
|
if $plugin->perfdata && $plugin->all_perfoutput; |
129
|
|
|
|
|
|
|
} |
130
|
92
|
|
|
|
|
236
|
$output .= "\n"; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# Don't actually exit if _fake_exit set |
133
|
92
|
100
|
|
|
|
118
|
if ($_fake_exit) { |
134
|
89
|
|
|
|
|
1783
|
require Monitoring::Plugin::ExitResult; |
135
|
89
|
|
|
|
|
208
|
return Monitoring::Plugin::ExitResult->new($code, $output); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
3
|
|
|
|
|
6
|
_plugin_exit($code, $output); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub _plugin_exit { |
142
|
17
|
|
|
17
|
|
29
|
my ($code, $output) = @_; |
143
|
|
|
|
|
|
|
# Print output and exit; die if flag set and called via a die in stack backtrace |
144
|
17
|
100
|
|
|
|
41
|
if ($_use_die) { |
145
|
16
|
|
|
|
|
23
|
for (my $i = 0;; $i++) { |
146
|
34
|
|
|
|
|
281
|
@_ = caller($i); |
147
|
34
|
50
|
|
|
|
86
|
last unless @_; |
148
|
34
|
100
|
|
|
|
104
|
if ($_[3] =~ m/die/) { |
149
|
16
|
|
|
|
|
30
|
$! = $code; |
150
|
16
|
|
|
|
|
192
|
die($output); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
1
|
|
|
|
|
10
|
print $output; |
155
|
1
|
|
|
|
|
21
|
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
|
2238
|
my ($arg1, $arg2, $rest) = @_; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# Named parameters |
164
|
55
|
100
|
66
|
|
|
477
|
if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) { |
|
|
100
|
66
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
165
|
6
|
|
|
|
|
12
|
return plugin_exit(@_); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# ($code, $message) |
169
|
|
|
|
|
|
|
elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) { |
170
|
20
|
|
|
|
|
28
|
return plugin_exit(@_); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# ($message, $code) |
174
|
|
|
|
|
|
|
elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) { |
175
|
21
|
|
|
|
|
26
|
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
|
|
|
|
|
13
|
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
|
10201
|
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
|
|
|
|
257
|
$arg{join} = ' ' unless defined $arg{join}; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# Decide $code |
217
|
50
|
|
|
|
|
44
|
my $code = OK; |
218
|
50
|
100
|
50
|
|
|
38
|
$code ||= CRITICAL if @{$arg{critical}}; |
|
50
|
|
|
|
|
123
|
|
219
|
50
|
100
|
100
|
|
|
37
|
$code ||= WARNING if @{$arg{warning}}; |
|
50
|
|
|
|
|
123
|
|
220
|
50
|
100
|
|
|
|
77
|
return $code unless wantarray; |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# Compose message |
223
|
44
|
|
|
|
|
42
|
my $message = ''; |
224
|
44
|
100
|
|
|
|
55
|
if ($arg{join_all}) { |
225
|
36
|
100
|
|
|
|
68
|
$message = join( $arg{join_all}, |
226
|
12
|
100
|
|
|
|
36
|
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
|
|
|
56
|
$message ||= join( $arg{'join'}, @{$arg{critical}} ) |
|
13
|
|
|
|
|
39
|
|
235
|
|
|
|
|
|
|
if $code == CRITICAL; |
236
|
32
|
100
|
33
|
|
|
52
|
$message ||= join( $arg{'join'}, @{$arg{warning}} ) |
|
10
|
|
|
|
|
23
|
|
237
|
|
|
|
|
|
|
if $code == WARNING; |
238
|
32
|
100
|
100
|
|
|
79
|
$message ||= ref $arg{ok} ? join( $arg{'join'}, @{$arg{ok}} ) : $arg{ok} |
|
8
|
100
|
|
|
|
21
|
|
239
|
|
|
|
|
|
|
if $arg{ok}; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
44
|
|
|
|
|
171
|
return ($code, $message); |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
1; |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
# vim:sw=4:sm:et |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
__END__ |