line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Functional interface to basic Nagios::Plugin constants, exports, |
2
|
|
|
|
|
|
|
# and functions |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Nagios::Plugin::Functions; |
5
|
|
|
|
|
|
|
|
6
|
15
|
|
|
15
|
|
112814
|
use 5.006; |
|
15
|
|
|
|
|
57
|
|
|
15
|
|
|
|
|
787
|
|
7
|
|
|
|
|
|
|
|
8
|
15
|
|
|
15
|
|
88
|
use strict; |
|
15
|
|
|
|
|
31
|
|
|
15
|
|
|
|
|
553
|
|
9
|
15
|
|
|
15
|
|
85
|
use warnings; |
|
15
|
|
|
|
|
113
|
|
|
15
|
|
|
|
|
582
|
|
10
|
15
|
|
|
15
|
|
105
|
use File::Basename; |
|
15
|
|
|
|
|
25
|
|
|
15
|
|
|
|
|
2000
|
|
11
|
15
|
|
|
15
|
|
11829
|
use Params::Validate qw(:types validate); |
|
15
|
|
|
|
|
212606
|
|
|
15
|
|
|
|
|
3594
|
|
12
|
15
|
|
|
15
|
|
14424
|
use Math::Calc::Units; |
|
15
|
|
|
|
|
1419066
|
|
|
15
|
|
|
|
|
2771
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Remember to update Nagios::Plugins as well |
15
|
|
|
|
|
|
|
our $VERSION = "0.36"; |
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(nagios_exit nagios_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(nagios_exit nagios_die check_messages max_state max_state_alt convert) ], |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
15
|
|
|
15
|
|
151
|
use constant OK => 0; |
|
15
|
|
|
|
|
40
|
|
|
15
|
|
|
|
|
1708
|
|
30
|
15
|
|
|
15
|
|
89
|
use constant WARNING => 1; |
|
15
|
|
|
|
|
34
|
|
|
15
|
|
|
|
|
787
|
|
31
|
15
|
|
|
15
|
|
82
|
use constant CRITICAL => 2; |
|
15
|
|
|
|
|
28
|
|
|
15
|
|
|
|
|
835
|
|
32
|
15
|
|
|
15
|
|
127
|
use constant UNKNOWN => 3; |
|
15
|
|
|
|
|
56
|
|
|
15
|
|
|
|
|
779
|
|
33
|
15
|
|
|
15
|
|
90
|
use constant DEPENDENT => 4; |
|
15
|
|
|
|
|
28
|
|
|
15
|
|
|
|
|
30831
|
|
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
|
|
590
|
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
|
|
121
|
sub _use_die { @_ ? $_use_die = shift : $_use_die }; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub get_shortname { |
57
|
69
|
|
|
69
|
1
|
2473
|
my $arg = shift; |
58
|
|
|
|
|
|
|
|
59
|
69
|
|
|
|
|
91
|
my $shortname = undef; |
60
|
|
|
|
|
|
|
|
61
|
69
|
100
|
|
|
|
209
|
return $arg->{shortname} if (defined($arg->{shortname})); |
62
|
65
|
100
|
|
|
|
154
|
$shortname = $arg->{plugin} if (defined( $arg->{plugin})); |
63
|
|
|
|
|
|
|
|
64
|
65
|
|
66
|
|
|
14692
|
$shortname = uc basename($shortname || $ENV{NAGIOS_PLUGIN} || $0); |
65
|
65
|
|
|
|
|
130
|
$shortname =~ s/^CHECK_(?:BY_)?//; # Remove any leading CHECK_[BY_] |
66
|
65
|
|
|
|
|
255
|
$shortname =~ s/\..*$//; # Remove any trailing suffix |
67
|
65
|
|
|
|
|
241
|
return $shortname; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub max_state { |
71
|
7
|
100
|
|
7
|
1
|
953
|
return CRITICAL if grep { $_ == CRITICAL } @_; |
|
16
|
|
|
|
|
45
|
|
72
|
6
|
100
|
|
|
|
10
|
return WARNING if grep { $_ == WARNING } @_; |
|
14
|
|
|
|
|
37
|
|
73
|
3
|
100
|
|
|
|
7
|
return OK if grep { $_ == OK } @_; |
|
4
|
|
|
|
|
18
|
|
74
|
1
|
50
|
|
|
|
5
|
return UNKNOWN if grep { $_ == UNKNOWN } @_; |
|
0
|
|
|
|
|
0
|
|
75
|
1
|
50
|
|
|
|
54
|
return DEPENDENT if grep { $_ == DEPENDENT } @_; |
|
0
|
|
|
|
|
0
|
|
76
|
1
|
|
|
|
|
7
|
return UNKNOWN; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub max_state_alt { |
80
|
0
|
0
|
|
0
|
1
|
0
|
return CRITICAL if grep { $_ == CRITICAL } @_; |
|
0
|
|
|
|
|
0
|
|
81
|
0
|
0
|
|
|
|
0
|
return WARNING if grep { $_ == WARNING } @_; |
|
0
|
|
|
|
|
0
|
|
82
|
0
|
0
|
|
|
|
0
|
return UNKNOWN if grep { $_ == UNKNOWN } @_; |
|
0
|
|
|
|
|
0
|
|
83
|
0
|
0
|
|
|
|
0
|
return DEPENDENT if grep { $_ == DEPENDENT } @_; |
|
0
|
|
|
|
|
0
|
|
84
|
0
|
0
|
|
|
|
0
|
return OK if grep { $_ == OK } @_; |
|
0
|
|
|
|
|
0
|
|
85
|
0
|
|
|
|
|
0
|
return UNKNOWN; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# nagios_exit( $code, $message ) |
89
|
|
|
|
|
|
|
sub nagios_exit { |
90
|
92
|
|
|
92
|
1
|
6432
|
my ($code, $message, $arg) = @_; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Handle named parameters |
93
|
92
|
100
|
66
|
|
|
658
|
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
|
|
|
42
|
if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) { |
96
|
6
|
|
|
|
|
10
|
$arg = pop @_; |
97
|
|
|
|
|
|
|
} else { |
98
|
0
|
|
|
|
|
0
|
undef $arg; |
99
|
|
|
|
|
|
|
} |
100
|
6
|
|
|
|
|
19
|
my %arg = @_; |
101
|
6
|
|
|
|
|
7
|
$code = $arg{return_code}; |
102
|
6
|
|
|
|
|
15
|
$message = $arg{message}; |
103
|
|
|
|
|
|
|
} |
104
|
92
|
|
100
|
|
|
317
|
$arg ||= {}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# Handle string codes |
107
|
92
|
100
|
100
|
|
|
551
|
$code = $ERRORS{$code} if defined $code && exists $ERRORS{$code}; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Set defaults |
110
|
92
|
100
|
100
|
|
|
425
|
$code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code}; |
111
|
92
|
100
|
|
|
|
208
|
$message = '' unless defined $message; |
112
|
92
|
50
|
33
|
|
|
275
|
if (ref $message && ref $message eq 'ARRAY') { |
113
|
0
|
|
|
|
|
0
|
$message = join(' ', map { chomp; $_ } @$message); |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
else { |
116
|
92
|
|
|
|
|
175
|
chomp $message; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Setup output |
120
|
92
|
|
|
|
|
491
|
my $output = "$STATUS_TEXT{$code}"; |
121
|
92
|
100
|
66
|
|
|
490
|
$output .= " - $message" if defined $message && $message ne ''; |
122
|
92
|
100
|
|
|
|
335
|
my $shortname = ($arg->{plugin} ? $arg->{plugin}->shortname : undef); |
123
|
92
|
|
66
|
|
|
483
|
$shortname ||= get_shortname(); # Should happen only if funnctions are called directly |
124
|
92
|
50
|
|
|
|
289
|
$output = "$shortname $output" if $shortname; |
125
|
92
|
100
|
|
|
|
223
|
if ($arg->{plugin}) { |
126
|
48
|
|
|
|
|
64
|
my $plugin = $arg->{plugin}; |
127
|
48
|
100
|
66
|
|
|
130
|
$output .= " | ". $plugin->all_perfoutput |
128
|
|
|
|
|
|
|
if $plugin->perfdata && $plugin->all_perfoutput; |
129
|
|
|
|
|
|
|
} |
130
|
92
|
|
|
|
|
442
|
$output .= "\n"; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# Don't actually exit if _fake_exit set |
133
|
92
|
100
|
|
|
|
195
|
if ($_fake_exit) { |
134
|
89
|
|
|
|
|
3082
|
require Nagios::Plugin::ExitResult; |
135
|
89
|
|
|
|
|
3718
|
return Nagios::Plugin::ExitResult->new($code, $output); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
3
|
|
|
|
|
11
|
_nagios_exit($code, $output); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub _nagios_exit { |
142
|
16
|
|
|
16
|
|
33
|
my ($code, $output) = @_; |
143
|
|
|
|
|
|
|
# Print output and exit; die if flag set and called via a die in stack backtrace |
144
|
16
|
100
|
|
|
|
48
|
if ($_use_die) { |
145
|
15
|
|
|
|
|
37
|
for (my $i = 0;; $i++) { |
146
|
32
|
|
|
|
|
267
|
@_ = caller($i); |
147
|
32
|
50
|
|
|
|
106
|
last unless @_; |
148
|
32
|
100
|
|
|
|
120
|
if ($_[3] =~ m/die/) { |
149
|
15
|
|
|
|
|
36
|
$! = $code; |
150
|
15
|
|
|
|
|
378
|
die($output); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
1
|
|
|
|
|
167
|
print $output; |
155
|
1
|
|
|
|
|
88
|
exit $code; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# nagios_die( $message, [ $code ]) OR nagios_die( $code, $message ) |
159
|
|
|
|
|
|
|
# Default $code: UNKNOWN |
160
|
|
|
|
|
|
|
sub nagios_die { |
161
|
55
|
|
|
55
|
1
|
12168
|
my ($arg1, $arg2, $rest) = @_; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# Named parameters |
164
|
55
|
100
|
66
|
|
|
870
|
if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) { |
|
|
100
|
66
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
165
|
6
|
|
|
|
|
15
|
return nagios_exit(@_); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# ($code, $message) |
169
|
|
|
|
|
|
|
elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) { |
170
|
20
|
|
|
|
|
56
|
return nagios_exit(@_); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# ($message, $code) |
174
|
|
|
|
|
|
|
elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) { |
175
|
21
|
|
|
|
|
59
|
return nagios_exit($arg2, $arg1, $rest); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Else just assume $arg1 is the message and hope for the best |
179
|
|
|
|
|
|
|
else { |
180
|
8
|
|
|
|
|
29
|
return nagios_exit( UNKNOWN, $arg1, $arg2 ); |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# For backwards compatibility |
185
|
0
|
|
|
0
|
0
|
0
|
sub die { nagios_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
|
18115
|
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
|
|
|
|
471
|
$arg{join} = ' ' unless defined $arg{join}; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# Decide $code |
217
|
50
|
|
|
|
|
59
|
my $code = OK; |
218
|
50
|
100
|
50
|
|
|
54
|
$code ||= CRITICAL if @{$arg{critical}}; |
|
50
|
|
|
|
|
180
|
|
219
|
50
|
100
|
100
|
|
|
54
|
$code ||= WARNING if @{$arg{warning}}; |
|
50
|
|
|
|
|
178
|
|
220
|
50
|
100
|
|
|
|
112
|
return $code unless wantarray; |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# Compose message |
223
|
44
|
|
|
|
|
57
|
my $message = ''; |
224
|
44
|
100
|
|
|
|
82
|
if ($arg{join_all}) { |
225
|
36
|
100
|
|
|
|
102
|
$message = join( $arg{join_all}, |
226
|
12
|
100
|
|
|
|
45
|
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
|
|
|
85
|
$message ||= join( $arg{'join'}, @{$arg{critical}} ) |
|
13
|
|
|
|
|
79
|
|
235
|
|
|
|
|
|
|
if $code == CRITICAL; |
236
|
32
|
100
|
33
|
|
|
79
|
$message ||= join( $arg{'join'}, @{$arg{warning}} ) |
|
10
|
|
|
|
|
31
|
|
237
|
|
|
|
|
|
|
if $code == WARNING; |
238
|
32
|
100
|
100
|
|
|
114
|
$message ||= ref $arg{ok} ? join( $arg{'join'}, @{$arg{ok}} ) : $arg{ok} |
|
8
|
100
|
|
|
|
35
|
|
239
|
|
|
|
|
|
|
if $arg{ok}; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
44
|
|
|
|
|
273
|
return ($code, $message); |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
1; |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
# vim:sw=4:sm:et |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
__END__ |