line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Functional interface to basic Nagios::Monitoring::Plugin constants, exports,
|
2
|
|
|
|
|
|
|
# and functions
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Nagios::Monitoring::Plugin::Functions;
|
5
|
|
|
|
|
|
|
|
6
|
15
|
|
|
15
|
|
107914
|
use 5.006;
|
|
15
|
|
|
|
|
52
|
|
7
|
|
|
|
|
|
|
|
8
|
15
|
|
|
15
|
|
76
|
use strict;
|
|
15
|
|
|
|
|
28
|
|
|
15
|
|
|
|
|
327
|
|
9
|
15
|
|
|
15
|
|
68
|
use warnings;
|
|
15
|
|
|
|
|
27
|
|
|
15
|
|
|
|
|
1227
|
|
10
|
15
|
|
|
15
|
|
75
|
use File::Basename;
|
|
15
|
|
|
|
|
26
|
|
|
15
|
|
|
|
|
1634
|
|
11
|
15
|
|
|
15
|
|
9931
|
use Params::Validate qw(:types validate);
|
|
15
|
|
|
|
|
105897
|
|
|
15
|
|
|
|
|
3106
|
|
12
|
15
|
|
|
15
|
|
10936
|
use Math::Calc::Units;
|
|
15
|
|
|
|
|
493379
|
|
|
15
|
|
|
|
|
2554
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Remember to update Nagios::Monitoring::Plugins as well
|
15
|
|
|
|
|
|
|
our $VERSION = "0.51";
|
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
|
|
131
|
use constant OK => 0;
|
|
15
|
|
|
|
|
33
|
|
|
15
|
|
|
|
|
740
|
|
30
|
15
|
|
|
15
|
|
77
|
use constant WARNING => 1;
|
|
15
|
|
|
|
|
33
|
|
|
15
|
|
|
|
|
712
|
|
31
|
15
|
|
|
15
|
|
78
|
use constant CRITICAL => 2;
|
|
15
|
|
|
|
|
22
|
|
|
15
|
|
|
|
|
839
|
|
32
|
15
|
|
|
15
|
|
112
|
use constant UNKNOWN => 3;
|
|
15
|
|
|
|
|
34
|
|
|
15
|
|
|
|
|
659
|
|
33
|
15
|
|
|
15
|
|
103
|
use constant DEPENDENT => 4;
|
|
15
|
|
|
|
|
27
|
|
|
15
|
|
|
|
|
25687
|
|
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
|
|
332
|
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
|
|
107
|
sub _use_die { @_ ? $_use_die = shift : $_use_die };
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub get_shortname {
|
57
|
69
|
|
|
69
|
1
|
1453
|
my $arg = shift;
|
58
|
|
|
|
|
|
|
|
59
|
69
|
|
|
|
|
83
|
my $shortname = undef;
|
60
|
|
|
|
|
|
|
|
61
|
69
|
100
|
|
|
|
426
|
return $arg->{shortname} if (defined($arg->{shortname}));
|
62
|
65
|
100
|
|
|
|
138
|
$shortname = $arg->{plugin} if (defined( $arg->{plugin}));
|
63
|
|
|
|
|
|
|
|
64
|
65
|
|
33
|
|
|
2156
|
$shortname = uc basename($shortname || $ENV{NAGIOS_PLUGIN} || $0);
|
65
|
65
|
|
|
|
|
116
|
$shortname =~ s/^CHECK_(?:BY_)?//; # Remove any leading CHECK_[BY_]
|
66
|
65
|
|
|
|
|
216
|
$shortname =~ s/\..*$//; # Remove any trailing suffix
|
67
|
65
|
|
|
|
|
214
|
return $shortname;
|
68
|
|
|
|
|
|
|
}
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub max_state {
|
71
|
7
|
100
|
|
7
|
1
|
1190
|
return CRITICAL if grep { $_ == CRITICAL } @_;
|
|
16
|
|
|
|
|
41
|
|
72
|
6
|
100
|
|
|
|
10
|
return WARNING if grep { $_ == WARNING } @_;
|
|
14
|
|
|
|
|
35
|
|
73
|
3
|
100
|
|
|
|
6
|
return OK if grep { $_ == OK } @_;
|
|
4
|
|
|
|
|
17
|
|
74
|
1
|
50
|
|
|
|
4
|
return UNKNOWN if grep { $_ == UNKNOWN } @_;
|
|
0
|
|
|
|
|
0
|
|
75
|
1
|
50
|
|
|
|
22
|
return DEPENDENT if grep { $_ == DEPENDENT } @_;
|
|
0
|
|
|
|
|
0
|
|
76
|
1
|
|
|
|
|
6
|
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
|
3688
|
my ($code, $message, $arg) = @_;
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Handle named parameters
|
93
|
92
|
100
|
66
|
|
|
497
|
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
|
|
|
53
|
if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) {
|
96
|
6
|
|
|
|
|
13
|
$arg = pop @_;
|
97
|
|
|
|
|
|
|
} else {
|
98
|
0
|
|
|
|
|
0
|
undef $arg;
|
99
|
|
|
|
|
|
|
}
|
100
|
6
|
|
|
|
|
21
|
my %arg = @_;
|
101
|
6
|
|
|
|
|
11
|
$code = $arg{return_code};
|
102
|
6
|
|
|
|
|
14
|
$message = $arg{message};
|
103
|
|
|
|
|
|
|
}
|
104
|
92
|
|
100
|
|
|
233
|
$arg ||= {};
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# Handle string codes
|
107
|
92
|
100
|
66
|
|
|
343
|
$code = $ERRORS{$code} if defined $code && exists $ERRORS{$code};
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Set defaults
|
110
|
92
|
100
|
66
|
|
|
279
|
$code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code};
|
111
|
92
|
100
|
|
|
|
161
|
$message = '' unless defined $message;
|
112
|
92
|
50
|
33
|
|
|
247
|
if (ref $message && ref $message eq 'ARRAY') {
|
113
|
0
|
|
|
|
|
0
|
$message = join(' ', map { chomp; $_ } @$message);
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
114
|
|
|
|
|
|
|
}
|
115
|
|
|
|
|
|
|
else {
|
116
|
92
|
|
|
|
|
154
|
chomp $message;
|
117
|
|
|
|
|
|
|
}
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Setup output
|
120
|
92
|
|
|
|
|
155
|
my $output = "$STATUS_TEXT{$code}";
|
121
|
92
|
100
|
66
|
|
|
435
|
$output .= " - $message" if defined $message && $message ne '';
|
122
|
92
|
100
|
|
|
|
268
|
my $shortname = ($arg->{plugin} ? $arg->{plugin}->shortname : undef);
|
123
|
92
|
|
66
|
|
|
370
|
$shortname ||= get_shortname(); # Should happen only if funnctions are called directly
|
124
|
92
|
50
|
|
|
|
253
|
$output = "$shortname $output" if $shortname;
|
125
|
92
|
100
|
|
|
|
187
|
if ($arg->{plugin}) {
|
126
|
48
|
|
|
|
|
67
|
my $plugin = $arg->{plugin};
|
127
|
48
|
100
|
66
|
|
|
145
|
$output .= " | ". $plugin->all_perfoutput
|
128
|
|
|
|
|
|
|
if $plugin->perfdata && $plugin->all_perfoutput;
|
129
|
|
|
|
|
|
|
}
|
130
|
92
|
|
|
|
|
366
|
$output .= "\n";
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# Don't actually exit if _fake_exit set
|
133
|
92
|
100
|
|
|
|
175
|
if ($_fake_exit) {
|
134
|
89
|
|
|
|
|
3075
|
require Nagios::Monitoring::Plugin::ExitResult;
|
135
|
89
|
|
|
|
|
287
|
return Nagios::Monitoring::Plugin::ExitResult->new($code, $output);
|
136
|
|
|
|
|
|
|
}
|
137
|
|
|
|
|
|
|
|
138
|
3
|
|
|
|
|
8
|
_nagios_exit($code, $output);
|
139
|
|
|
|
|
|
|
}
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub _nagios_exit {
|
142
|
16
|
|
|
16
|
|
29
|
my ($code, $output) = @_;
|
143
|
|
|
|
|
|
|
# Print output and exit; die if flag set and called via a die in stack backtrace
|
144
|
16
|
100
|
|
|
|
58
|
if ($_use_die) {
|
145
|
15
|
|
|
|
|
31
|
for (my $i = 0;; $i++) {
|
146
|
32
|
|
|
|
|
258
|
@_ = caller($i);
|
147
|
32
|
50
|
|
|
|
101
|
last unless @_;
|
148
|
32
|
100
|
|
|
|
112
|
if ($_[3] =~ m/die/) {
|
149
|
15
|
|
|
|
|
35
|
$! = $code;
|
150
|
15
|
|
|
|
|
215
|
die($output);
|
151
|
|
|
|
|
|
|
}
|
152
|
|
|
|
|
|
|
}
|
153
|
|
|
|
|
|
|
}
|
154
|
1
|
|
|
|
|
33
|
print $output;
|
155
|
1
|
|
|
|
|
36
|
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
|
10388
|
my ($arg1, $arg2, $rest) = @_;
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# Named parameters
|
164
|
55
|
100
|
66
|
|
|
831
|
if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) {
|
|
|
100
|
66
|
|
|
|
|
|
|
100
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
165
|
6
|
|
|
|
|
21
|
return nagios_exit(@_);
|
166
|
|
|
|
|
|
|
}
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# ($code, $message)
|
169
|
|
|
|
|
|
|
elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) {
|
170
|
20
|
|
|
|
|
42
|
return nagios_exit(@_);
|
171
|
|
|
|
|
|
|
}
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# ($message, $code)
|
174
|
|
|
|
|
|
|
elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) {
|
175
|
21
|
|
|
|
|
43
|
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
|
|
|
|
|
20
|
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
|
13792
|
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
|
|
|
|
377
|
$arg{join} = ' ' unless defined $arg{join};
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# Decide $code
|
217
|
50
|
|
|
|
|
60
|
my $code = OK;
|
218
|
50
|
100
|
50
|
|
|
56
|
$code ||= CRITICAL if @{$arg{critical}};
|
|
50
|
|
|
|
|
670
|
|
219
|
50
|
100
|
100
|
|
|
58
|
$code ||= WARNING if @{$arg{warning}};
|
|
50
|
|
|
|
|
168
|
|
220
|
50
|
100
|
|
|
|
112
|
return $code unless wantarray;
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# Compose message
|
223
|
44
|
|
|
|
|
60
|
my $message = '';
|
224
|
44
|
100
|
|
|
|
81
|
if ($arg{join_all}) {
|
225
|
|
|
|
|
|
|
$message = join( $arg{join_all},
|
226
|
36
|
100
|
|
|
|
147
|
map { @$_ ? join( $arg{'join'}, @$_) : () }
|
227
|
|
|
|
|
|
|
$arg{critical},
|
228
|
|
|
|
|
|
|
$arg{warning},
|
229
|
12
|
100
|
|
|
|
50
|
$arg{ok} ? (ref $arg{ok} ? $arg{ok} : [ $arg{ok} ]) : []
|
|
|
100
|
|
|
|
|
|
230
|
|
|
|
|
|
|
);
|
231
|
|
|
|
|
|
|
}
|
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
else {
|
234
|
32
|
100
|
33
|
|
|
83
|
$message ||= join( $arg{'join'}, @{$arg{critical}} )
|
|
13
|
|
|
|
|
277
|
|
235
|
|
|
|
|
|
|
if $code == CRITICAL;
|
236
|
32
|
100
|
33
|
|
|
102
|
$message ||= join( $arg{'join'}, @{$arg{warning}} )
|
|
10
|
|
|
|
|
34
|
|
237
|
|
|
|
|
|
|
if $code == WARNING;
|
238
|
8
|
|
|
|
|
32
|
$message ||= ref $arg{ok} ? join( $arg{'join'}, @{$arg{ok}} ) : $arg{ok}
|
239
|
32
|
100
|
100
|
|
|
119
|
if $arg{ok};
|
|
|
100
|
|
|
|
|
|
240
|
|
|
|
|
|
|
}
|
241
|
|
|
|
|
|
|
|
242
|
44
|
|
|
|
|
466
|
return ($code, $message);
|
243
|
|
|
|
|
|
|
}
|
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
# ------------------------------------------------------------------------
|
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
1;
|
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
# vim:sw=4:sm:et
|
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
__END__
|