line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nagios::Plugin::Simple; |
2
|
2
|
|
|
2
|
|
45470
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
74
|
|
3
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1228
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION='0.06'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Nagios::Plugin::Simple - Simple and Minimalistic Nagios Plugin Package |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 SYNOPSIS |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Nagios::Plugin::Simple; |
14
|
|
|
|
|
|
|
my $nps=Nagios::Plugin::Simple->new; |
15
|
|
|
|
|
|
|
$nps->ok("I'm OK") if &ok; |
16
|
|
|
|
|
|
|
$nps->warning("I'm a bit sickly") if &sick; |
17
|
|
|
|
|
|
|
$nps->critical("Barf..."); |
18
|
|
|
|
|
|
|
$nps->unknown("Huh?"); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
In the true spirit of Perl you can even do a one-liner. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
perl -MNagios::Plugin::Simple -e 'Nagios::Plugin::Simple->new->ok("")';echo $? |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 DESCRIPTION |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
This is the package that I use mostly because I feel the L is too encompassing. I feel that it is the scripts responsibility to handle arguments and thus this package does not do that nor will do that. If you want argument handling use one of the GetOpt packages. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 USAGE |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use Nagios::Plugin::Simple; |
32
|
|
|
|
|
|
|
my $nps=Nagios::Plugin::Simple->new; |
33
|
|
|
|
|
|
|
if (&ok) {$nps->ok("good!")} else {$nps->critical("bad!")}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head2 new |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $nps=Nagios::Plugin::Simple->new(); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub new { |
45
|
2
|
|
|
2
|
1
|
18
|
my $this = shift(); |
46
|
2
|
|
33
|
|
|
12
|
my $class = ref($this) || $this; |
47
|
2
|
|
|
|
|
5
|
my $self = {}; |
48
|
2
|
|
|
|
|
5
|
bless $self, $class; |
49
|
2
|
|
|
|
|
9
|
$self->initialize(@_); |
50
|
2
|
|
|
|
|
6
|
return $self; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 METHODS |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head2 initialize |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub initialize { |
60
|
2
|
|
|
2
|
1
|
4
|
my $self=shift; |
61
|
2
|
|
|
|
|
10
|
%$self=@_; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 ok |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Exits script with ok status code. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
$nps->ok("I'm OK"); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Prints "OK: %s" and exits with a code 0. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
STDOUT => "OK: I'm OK\n", EXIT=>0 |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub ok { |
77
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
78
|
0
|
|
|
|
|
|
my $string=shift; |
79
|
0
|
|
|
|
|
|
$self->status(OK=>$string); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 warning |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Exits script with warning status code. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
$nps->warning("I'm a bit sickly"); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Prints "Warning: %s" and exits with a code 1. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
STDOUT => "Warning: I'm a bit sickly\n", EXIT=>1 |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub warning { |
95
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
96
|
0
|
|
|
|
|
|
my $string=shift; |
97
|
0
|
|
|
|
|
|
$self->status(Warning=>$string); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 critical |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Exits script with critical status code. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
$nps->critical("Barf..."); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Prints "Critical: %s" and exits with a code 2. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
STDOUT => "Critical: Barf...\n", EXIT=>2 |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub critical { |
113
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
114
|
0
|
|
|
|
|
|
my $string=shift; |
115
|
0
|
|
|
|
|
|
$self->status(Critical=>$string); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 unknown |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Exits script with unknown status code. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
$nps->unknown("Huh?") |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Prints "Unknown: %s" and exits with a code 3. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
STDOUT => "Unknown: Huh?\n", EXIT=>3 |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub unknown { |
131
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
132
|
0
|
|
|
|
|
|
my $string=shift; |
133
|
0
|
|
|
|
|
|
$self->status(Unknown=>$string); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 code |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Exits script by status code. This works best if your status is actually stored as a code 0, 1, 2, or 3 in a variable. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
$nps->code($code => $string); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Examples: |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
$nps->code(0 => "I'm OK!"); |
145
|
|
|
|
|
|
|
$nps->code(1 => "I'm a bit sickly"); |
146
|
|
|
|
|
|
|
$nps->code(2 => "Barf..."); |
147
|
|
|
|
|
|
|
$nps->code(3 => "Huh?") |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Prints ``$status: %s'' and exits with $code. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub code { |
154
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
155
|
0
|
|
|
|
|
|
my $code=shift; |
156
|
0
|
|
|
|
|
|
my $string=shift; |
157
|
0
|
|
|
|
|
|
my %status=reverse $self->codes; |
158
|
0
|
|
|
|
|
|
my $status=$status{$code}; |
159
|
0
|
|
|
|
|
|
$self->status($status, $string); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 status |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Exits script by status string. This works best if your string is actually stored as "OK", "Warning", etc in a variable |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
$nps->status($status => $string); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Examples: |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
$nps->status("OK" => "I'm OK!"); |
171
|
|
|
|
|
|
|
$nps->status("Warning" => "I'm a bit sickly"); |
172
|
|
|
|
|
|
|
$nps->status("Critical" => "Barf..."); |
173
|
|
|
|
|
|
|
$nps->status("Unknown" => "Huh?") |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Prints ``$status: %s'' and exits with correct code. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=cut |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub status { |
180
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
181
|
0
|
|
|
|
|
|
my $status=shift; |
182
|
0
|
|
|
|
|
|
my $string=shift; |
183
|
0
|
0
|
|
|
|
|
$string='' unless defined($string); |
184
|
0
|
|
|
|
|
|
my %codes=map {uc($_)} $self->codes; |
|
0
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
#use Data::Dumper; |
186
|
|
|
|
|
|
|
#print Dumper([\%codes]); |
187
|
0
|
|
|
|
|
|
my $code=$codes{uc($status)}; |
188
|
0
|
0
|
|
|
|
|
die(qq{Error: Exit code not defined for "$status"}) unless defined($code); |
189
|
0
|
|
|
|
|
|
printf "%s: %s\n", $status, $string; |
190
|
0
|
|
|
|
|
|
exit $code; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 codes |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Returns a hash of the Nagios status codes. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
my %codes=$nps->codes; #(OK=>0, Warning=>1, Critical=>2, Unknown=>3) |
198
|
|
|
|
|
|
|
my $codes=$nps->codes; #{OK=>0, Warning=>1, Critical=>2, Unknown=>3} |
199
|
|
|
|
|
|
|
my %status=reverse $self->codes; #(0=>"OK", 1=>"Warning", ...) |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub codes { |
204
|
|
|
|
|
|
|
#my $self=shift; |
205
|
0
|
|
|
0
|
1
|
|
my @data=(OK=>0, Warning=>1, Critical=>2, Unknown=>3); |
206
|
0
|
0
|
|
|
|
|
return wantarray ? @data : {@data}; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head1 BUGS |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 SUPPORT |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head1 AUTHOR |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Michael R. Davis |
216
|
|
|
|
|
|
|
CPAN ID: MRDVT |
217
|
|
|
|
|
|
|
STOP, LLC |
218
|
|
|
|
|
|
|
account=>perl,tld=>com,domain=>michaelrdavis |
219
|
|
|
|
|
|
|
http://www.stopllc.com/ |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 COPYRIGHT |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
This program is free software licensed under the... |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
The BSD License |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
The full text of the license can be found in the |
228
|
|
|
|
|
|
|
LICENSE file included with this module. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 SEE ALSO |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
L, L, L |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=cut |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
1; |