forked from ofalk/Nagios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_snmp_dskusg
More file actions
executable file
·338 lines (302 loc) · 8.33 KB
/
Copy pathcheck_snmp_dskusg
File metadata and controls
executable file
·338 lines (302 loc) · 8.33 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
#!/usr/bin/perl
# Copyright (c) by Oliver Falk <oliver@linux-kernel.at>, 2012-2014
use strict;
use warnings;
use Net::SNMP;
use Carp qw/croak/;
# Nagios parsable return codes
our $OK = 0;
our $WARNING = 1;
our $CRITICAL = 2;
our $UNKNOWN = 3;
our $storbase= '.1.3.6.1.2.1.25.2.3';
our $descbase= $storbase . '.1.3';
package Net::SNMP::DskUsg::Item;
sub new {
my $class = shift;
my $args = shift;
$class = ref($class) || $class;
return bless { session => $args->{session}, idx => $args->{idx}}, $class;
}
sub _get_single {
my $self = shift;
my $oid = shift;
$self->{session}->get_request(-varbindlist => [ $oid ])->{$oid};
}
sub get_single {
my $self = shift;
my $oid = shift;
my $name = '_' . ((caller(1))[3]);
$self->{count_count} = 0 unless exists $self->{count_count};
$self->{count_count}++;
if($self->{$name}) {
return $self->{$name};
} else {
$self->{$name} = $self->_get_single($oid);
return $self->{$name};
}
}
sub index { my $self = shift; $self->{idx}; };
sub description { my $self = shift; $self->get_single($storbase . '.1.3.' . $self->{idx}); };
sub total { my $self = shift; $self->get_single($storbase . '.1.5.' . $self->{idx}); };
sub used { my $self = shift; $self->get_single($storbase . '.1.6.' . $self->{idx}); };
sub failures { my $self = shift; $self->get_single($storbase . '.1.7.' . $self->{idx}); };
sub sau { my $self = shift; $self->get_single($storbase . '.1.4.' . $self->{idx}); };
sub type { my $self = shift; $self->get_single($storbase . '.1.2.' . $self->{idx}); };
sub total_1k { my $self = shift; eval { $self->total * $self->sau / 1024; }; };
sub used_1k { my $self = shift; eval { $self->used * $self->sau / 1024; }; };
sub total_human { my $self = shift; eval { _convert($self->total * $self->sau); } };;
sub used_human { my $self = shift; eval { _convert($self->used * $self->sau); } };;
sub used_percent { my $self = shift; eval { sprintf("%.2f", $self->used / ($self->total / 100)); } };;
sub _convert {
defined (my $size = shift) || return undef;
my $block = 1024;
my @args = qw/B K M G/;
while (@args && $size > $block) {
shift @args;
$size /= $block;
}
$size = sprintf("%.2f",$size);
"$size$args[0]";
}
package Net::SNMP::DskUsg;
use Carp qw/carp/;
sub new {
my $class = shift;
my $args = shift;
$class = ref($class) || $class;
return bless {}, $class;
}
sub DESTROY {
my $self = shift;
eval {
$self->{session}->close;
};
}
sub get {
my $self = shift;
my $host = shift;
my $community = shift;
my $table;
($self->{session}, $self->{error}) = Net::SNMP->session(
-hostname => $host,
-version => '1',
-retries => 3,
-timeout => 10,
-community => $community,
);
if ($self->{error}) {
carp "UNKNOWN - cannot establish connection, $self->{error}\n";
exit $UNKNOWN;
}
$self->{result} = $self->{session}->get_table(
-baseoid => $descbase,
);
if ($self->{session}->error_status()) {
carp "UNKNOWN - cannot fetch data, " . $self->{session}->error() . "\n";
exit $UNKNOWN;
}
foreach (sort keys %{$self->{result}}) {
my $desc = $self->{result}->{$_};
$_ =~ m/\.([0-9]+?)\.([0-9]+?)$/;
$table->{$desc} = new Net::SNMP::DskUsg::Item({ session => $self->{session}, idx => $2 });
}
if (!$table) {
carp "UNKNOWN - no data for found\n";
exit $UNKNOWN;
}
return $table;
}
package Net::SNMP::DskUsg::Cached;
use base 'Net::SNMP::DskUsg';
use Storable qw/lock_store lock_retrieve/;
use Carp qw/croak/;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my %parameters = ();
if(ref($_[0]) eq 'HASH') {
%parameters = %{$_[0]};
} else {
%parameters = @_;
}
my $self = $class->SUPER::new(\%parameters);
if($parameters{cache}) {
$self->{PATH} = $parameters{cache};
} else {
$self->{PATH} = '/tmp';
}
return $self;
}
sub get {
my $self = shift;
my $host = shift;
my $community = shift;
my $cache = {};
eval { $cache = lock_retrieve( $self->{PATH} . "/$host.dat" ); };
$cache = {} if ($@ or !$cache);
if($cache->{$host} && !$self->_older_than(5, $cache->{$host}->{cached})) {
use Data::Dumper;
die Dumper($cache);
return $cache->{$host}->{dta};
}
my $dta = $self->SUPER::get($host, $community);
$cache->{$host} = {
dta => $dta,
cached => time(),
};
unless(lock_store($cache, $self->{PATH} . "/$host.dat")) {
croak("ERROR I/O problem while storing cachefile!");
}
return $cache->{$host}->{dta};
}
sub _older_than {
my $self = shift;
my $timeframe = shift;
my $cached = shift;
my $now = time();
if($cached < ($now - $timeframe * 60)) {
return 1;
} else {
return 0;
}
}
package main;
use Getopt::Long;
use Data::Dumper;
Getopt::Long::Configure('bundling');
MAIN: {
my $community = 'public';
my ($host,$mountpoint);
my @exclude = qw//;
my @regexclude = qw//;
my @excludetype = qw//;
my @includeonly = qw//;
my $warnlvl = 80;
my $critlvl = 90;
my ($error, $session, $result);
my $table;
my $debug = 0;
my $cache = 1; # THIS DOESN'T WORK AT THE MOMENT!!!!!
$result = GetOptions(
"host|hostname|H=s" => \$host,
"community|C=s" => \$community,
"mountpoint|m=s" => \$mountpoint,
"exclude|e=s" => \@exclude,
"includeonly|i=s" => \@includeonly,
"regexclude|r=s" => \@regexclude,
"excludetype=s" => \@excludetype,
"warning|w=f" => \$warnlvl,
"critical|c=f" => \$critlvl,
"debug" => \$debug,
# "cache" => \$cache,
);
if (!$result) {
print "UNKNOWN - no parameters specified\n";
exit $UNKNOWN;
}
if (!$host) {
print "UNKNOWN - hostname is missing\n";
exit $UNKNOWN;
}
if (!$mountpoint && !@exclude && !@regexclude && !@includeonly) {
print "UNKNOWN - mountpoint, exclude(s) or include(s) is missing\n";
exit $UNKNOWN;
}
my $grabber;
unless($cache) {
$grabber = new Net::SNMP::DskUsg::Cached;
} else {
$grabber = new Net::SNMP::DskUsg;
}
$table = $grabber->get($host, $community);
if ($debug == 1) {
warn Dumper($table);
}
if(!@exclude && !@regexclude && !@excludetype && !@includeonly) {
if($table->{$mountpoint}) {
$table = $table->{$mountpoint};
} else {
print "OK - $mountpoint doesn't exist\n";
exit $OK;
}
my ($abswarn,$abscrit);
my $ex;
eval {
$abswarn = $table->total_1k/100*$warnlvl;
$abscrit = $table->total_1k/100*$critlvl;
if ($table->used_percent > $critlvl) {
print "SNMP CRITICAL - " . $table->used_percent . " | usg=" . $table->used_percent . ";$warnlvl;$critlvl;0; ";
print "usgABS=" . $table->used_1k . ";$abswarn;$abscrit;0;\n";
$ex = $CRITICAL;
} elsif ($table->used_percent > $warnlvl) {
print "SNMP WARNING - ".$table->used_percent . " | usg=" . $table->used_percent . ";$warnlvl;$critlvl;0; ";
print "usgABS=" . $table->used_1k . ";$abswarn;$abscrit;0;\n";
$ex = $WARNING;
} else {
print "SNMP OK - ".$table->used_percent . " | usg=" . $table->used_percent . ";$warnlvl;$critlvl;0; ";
print "usgABS=" . $table->used_1k . ";$abswarn;$abscrit;0;\n";
$ex = $OK;
}
};
if($@) {
print "UNKNOWN - $@";
exit $UNKNOWN;
} else {
exit $ex;
}
} else {
my $status = $OK;
my $excl;
$excl->{$_} = 1 foreach(@exclude);
my $perfdata = "";
my $message = "";
my $crit = "";
my $warn = "";
foreach(keys %{$table}) {
unless(scalar @includeonly) {
next if $excl->{$_};
my $skip = 0;
foreach my $regexcl (@regexclude) {
$skip = 1 if $_ =~ $regexcl;
}
foreach my $excltype (@excludetype) {
$skip = 1 if $table->{$_}->type eq $excltype;
}
next if $skip;
} else {
my $skip = 1;
foreach my $reginclo (@includeonly) {
$skip = 0 if $_ =~ /$reginclo/i;
}
next if $skip;
}
if (($table->{$_}->used_percent||0) > $critlvl) {
$crit .= "'$_' = " . ($table->{$_}->used_percent||0) . " > $critlvl ";
$status = $CRITICAL;
} elsif (($table->{$_}->used_percent||0) > $warnlvl) {
$warn .= "'$_' = " . ($table->{$_}->used_percent||0) . " > $warnlvl ";
$status = $WARNING if $status != $CRITICAL;
} else {
$message .= "'$_' = " . ($table->{$_}->used_percent||0) . ' ';
}
$perfdata .= "'$_'=" . ($table->{$_}->used_percent||0) . ";$warnlvl;$critlvl;0 ";
}
if($status == $OK) {
print "SNMP OK - $message | $perfdata";
exit $OK;
} elsif($status == $WARNING) {
print "SNMP WARNING - $warn | $perfdata";
exit $status;
} elsif ($status == $CRITICAL) {
print "SNMP CRITICAL - $crit ";
print "(WARNING: $warn)" if $warn;
print " | $perfdata";
exit $status;
} else {
# Duh!?
exit -100;
}
}
}
# vim: tabstop=4: