This repository was archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_allnet4500.pl
More file actions
executable file
·422 lines (321 loc) · 9.51 KB
/
Copy pathcheck_allnet4500.pl
File metadata and controls
executable file
·422 lines (321 loc) · 9.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/perl -w
# $Id: 736afff95e253efdecd75ad35137c114421595f5 $
$PROGNAME = basename($0);
$VERSION = '1.0';
{
package TresholdFactory;
use strict;
use Carp qw(croak);
our %fn;
our $re_float = '^[+-]?\d*\.?\d+$';
our $re_infinity = '^~$';
our $re_range = '^@(.*)';
sub new {
my $class = shift;
bless({}, $class);
}
sub get_validator {
my $self = shift;
my $range = shift;
if($_ = $fn{$range}) {
return $_;
}
my ($start, $end) = split(/:/, $range);
my ($ll, $ul);
my $mode = 'd';
if ($start =~ m/$re_float/ && !defined $end) {
$ll = 0;
$ul = $start;
} elsif ($start =~ m/$re_float/ && (!$end || $end =~ m/$re_infinity/)) {
$ll = $start;
$ul = "+Inf";
} elsif ((!$start || $start =~ m/$re_infinity/) && $end =~ m/$re_float/) {
$ll = "-Inf";
$ul = $end;
} elsif ($start =~ m/$re_float/ && $end =~ m/$re_float/) {
$ll = $start;
$ul = $end;
} elsif ($start =~ m/$re_range/ && ($start = $1) && $start =~ m/$re_float/ && $end =~ m/$re_float/) {
$mode = 'r';
$ll = $start;
$ul = $end;
}
if(!defined $ll || !defined $ul || $ll > $ul) {
croak "Unexpected format.";
}
if($mode eq 'd') {
$fn{$range} = $_ = sub {
my($v, $if, $default) = @_;
if ($v < $ll || $v > $ul) {
return $if;
}
return $default;
};
return $_;
} else {
$fn{$range} = $_ = sub {
my($v, $if, $default) = @_;
if ($v >= $ll && $v <= $ul) {
return $if;
}
return $default;
};
return $_;
}
}
1;
}
{
package AuthAgent;
use strict;
require LWP::UserAgent;
our @ISA = qw(LWP::UserAgent);
sub new {
my($class, %cnf) = @_;
my $user = delete($cnf{user});
my $password = delete($cnf{password});
my $self = $class->SUPER::new(%cnf);
$self->{user} = $user;
$self->{password} = $password;
bless($self, $class);
}
sub get_basic_credentials {
my($self, $realm, $uri) = @_;
return ($self->{user}, $self->{password});
}
1;
}
use strict;
use Carp qw(croak);
use File::Basename;
use Getopt::Long;
require XML::Simple;
use Pod::Usage;
use subs qw(help);
use vars qw (
$PROGNAME
$VERSION
%states
%state_names
%function_map
$opt_protocol
$opt_host
$opt_port
$opt_user
$opt_password
$opt_path
$opt_agent
$opt_timeout
$opt_legend
$opt_warning
$opt_critical
$opt_help
$opt_man
$opt_verbose
$opt_version
$re_float
$re_infinity
$re_range
$url
$ua
$res
$xs
$ref
$tf
@sensors
$status
%sensors
);
%states = (
OK => 0,
WARNING => 1,
CRITICAL=> 2,
UNKNOWN => 3
);
%state_names = (
0 => 'OK',
1 => 'WARNING',
2 => 'CRITICAL',
3 => 'UNKNOWN'
);
# NOT COMPLETE!
%function_map = (
1 => {
type => "Temperature",
unit => "C"
},
2 => {
type => "Humidity",
unit => "%"
}
);
$opt_protocol = 'http';
$opt_port = 80;
$opt_path = '/xml/sensordata.xml';
$opt_agent = sprintf('%s/%s with LWP/%s', $PROGNAME, $VERSION, $LWP::VERSION);
$opt_timeout = 10;
$opt_legend = 'Sensors';
$status = $states{OK};
Getopt::Long::Configure('bundling');
GetOptions(
'h|help' => \$opt_help,
'man' => \$opt_man,
'H=s' => \$opt_host,
'p=i' => \$opt_port,
'path=s' => \$opt_path,
'U=s' => \$opt_user,
'P=s' => \$opt_password,
'a=s' => \$opt_agent,
't=i' => \$opt_timeout,
'l=s' => \$opt_legend,
'w=s' => \$opt_warning,
'c=s' => \$opt_critical,
'V|version' => \$opt_version
) || help(1, 'Please check your options!');
help( 1) if $opt_help;
help(99) if $opt_man;
help(-1) if $opt_version;
help( 1, 'Host not specified! Please check your options.') unless ($opt_host);
$url = sprintf('%s://%s:%u/%s', $opt_protocol, $opt_host, $opt_port, $opt_path);
$ua = AuthAgent->new(
agent => $opt_agent,
timeout => $opt_timeout,
user => $opt_user,
password=> $opt_password
);
$res = $ua->get($url);
if (!$res->is_success) {
croak $res->status_line;
}
$xs = XML::Simple->new();
$ref = $xs->XMLin($res->content);
$tf = TresholdFactory->new();
while ( my ($key, $struct) = each(%{$ref}) ) {
if ($key !~ m/sensor/) {
next;
}
my $v = $struct->{value_float};
my $warn = $opt_warning || $struct->{limit_low};
my $crit = $opt_critical || $struct->{limit_high};
my $status_ = $tf->get_validator($crit)->($v, 2, 0)
|| $tf->get_validator($warn)->($v, 1, 0)
|| $states{OK};
$status = $status_ if $status_ > $status;
push(@sensors, {
name => $struct->{name},
raw => $v,
warn => $warn,
crit => $crit,
min => $struct->{minimum},
max => $struct->{maximum},
status => $status_,
type => $function_map{$struct->{function}}{type} || 'Unsopperted (Plugin)',
uom => $function_map{$struct->{function}}{unit} || ''
});
}
foreach (@sensors) {
if (!$sensors{$_->{status}}) {
$sensors{$_->{status}} = ();
}
push(@{$sensors{$_->{status}}}, $_);
}
printf("%s %s - %i sensors checked: %s|%s\n%s", $opt_legend, $state_names{$status}, scalar(@sensors),
(sub {
my $out;
foreach(sort(keys(%sensors))) {
$out .= ', ' if $out;
$out .= sprintf('%i %s', scalar(@{$sensors{$_}}), $state_names{$_});
if ($_ ne $states{OK}) {
$out .= sprintf(' (%s)', join(', ', map(sprintf('%s=%s%s', $_->{name}, $_->{raw}, $_->{uom}), @{$sensors{$_}})));
}
}
return $out;
})->(),
join(' ', map(sprintf("'%s'=%s%s;%s;%s;%s;%s", $_->{name}, $_->{raw}, $_->{uom}, $_->{warn}, $_->{crit}, $_->{min}, $_->{max}), @sensors)),
join("\n", map(sprintf('%s %s: %s%s', $state_names{$_->{status}}, $_->{name}, $_->{raw}, $_->{uom}), @sensors))
);
exit $status;
sub help {
my ($level, $msg) = @_;
$level = 0 unless ($level);
if ($level == -1) {
print "$PROGNAME - Version: $VERSION\n";
exit $states{'UNKNOWN'};
}
pod2usage({
-message => $msg,
-verbose => $level
});
exit $states{'UNKNOWN'};
}
1;
__END__
=pod
=head1 COPYRIGHT
This software is Copyright (c) 2011 NETWAYS GmbH, Eric Lippmann
<support@netways.de>
(Except where explicitly superseded by other copyright notices)
=head1 LICENSE
This work is made available to you under the terms of Version 2 of
the GNU General Public License. A copy of that license should have
been provided with this software, but in any event can be snarfed
from http://www.fsf.org.
This work is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 or visit their web page on the internet at
http://www.fsf.org.
CONTRIBUTION SUBMISSION POLICY:
(The following paragraph is not intended to limit the rights granted
to you to modify and distribute this software under the terms of
the GNU General Public License and is only of importance to you if
you choose to contribute your changes and enhancements to the
community by submitting them to NETWAYS GmbH.)
By intentionally submitting any modifications, corrections or
derivatives to this work, or any other work intended for use with
this Software, to NETWAYS GmbH, you confirm that
you are the copyright holder for those contributions and you grant
NETWAYS GmbH a nonexclusive, worldwide, irrevocable,
royalty-free, perpetual, license to use, copy, create derivative
works based on those contributions, and sublicense and distribute
those contributions and any derivatives thereof.
Nagios and the Nagios logo are registered trademarks of Ethan Galstad.
=head1 NAME
check_allnet4500 - Icinga/Nagios plugin to query ALLNET ALL4500 sensors via HTTP/XML
=head1 SYNOPSIS
check_allnet4500.pl -h|--help
check_allnet4500.pl --man
check_allnet4500.pl -V|--version
check_allnet4500.pl -H hostname|hostaddress [-p port] [--path path]
[-a agent] [-t timeout]
[-U user] [-P password]
[-l legend]
[-w warning] [-c critical]
=head1 OPTIONS
=over 8
=item B<-H>
Hostname or hostaddress
=item B<-p>
Port, defaults to B<80>
=item B<--path>
Path, defaults to B<"/xml/sensordata.xml">
=item B<-a>
User agent
=item B<-t>
Timeout, defaults to B<10>
=item B<-U>
Username for basic or digest auth
=item B<-P>
Password for basic or digest auth
=item B<-l>
Legend a.k.a plugin/service name, defaults to B<"Sensors">
=item B<-w>
Warning treshold, defaults to the configured one
=item B<-c>
Critical treshold, defaults to the configured one
=back
=cut