forked from mludvig/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_snmp_disk.pl
executable file
·365 lines (311 loc) · 9.52 KB
/
check_snmp_disk.pl
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
#!/usr/bin/perl -w
# check_snmp_disk.pl - source unknown
# Updated for hrStorageTable by Michal Ludvig
# http://logix.cz/michal/devel/nagios
use strict;
use lib qw( /usr/local/nagios/libexec );
use utils qw( %ERRORS $TIMEOUT &print_revision &support &usage );
use Net::SNMP;
use Getopt::Long;
use Data::Dumper;
# globals
use vars qw(
$PROGNAME $VERSION %disks $snmp $errstr @built_in_excludes
$dskTable $dskIndex $dskPath
$dskTotal $dskAvail $dskUsed $dskPercent $dskPercentNode $dskAllocUnits
$exit @criticals @warnings @oks
$opt_version $opt_help $opt_timeout $opt_verbose $opt_host $opt_community
$opt_snmpver $opt_warn $opt_crit @opt_include @opt_exclude
$opt_use_dskTable $opt_warn_inodes $opt_crit_inodes
);
# config
$PROGNAME = 'check_snmp_disk.pl';
$VERSION = '0.3';
@built_in_excludes = qw(usbfs usbdevfs sysfs /proc /dev/pts);
# initialize
%disks = ();
$snmp = undef;
$errstr = undef;
# init options
$opt_version = undef;
$opt_help = undef;
$opt_timeout = $TIMEOUT;
$opt_verbose = undef;
$opt_host = undef;
$opt_community = 'public';
$opt_snmpver = "2c";
$opt_warn = 20;
$opt_crit = 10;
$opt_warn_inodes = 20;
$opt_crit_inodes = 10;
@opt_include = ();
@opt_exclude = ();
$opt_use_dskTable = 1;
# get options
Getopt::Long::Configure('bundling');
GetOptions(
'V|version' => \$opt_version,
'h|help' => \$opt_help,
't|timeout=i' => \$opt_timeout,
'v|verbose+' => \$opt_verbose,
'H|host=s' => \$opt_host,
'C|community=s' => \$opt_community,
'S|snmpver=s' => \$opt_snmpver,
'w|warning=s' => \$opt_warn,
'c|critical=s' => \$opt_crit,
'warning_inodes=s' => \$opt_warn_inodes,
'critical_inodes=s' => \$opt_crit_inodes,
'i|include=s' => \@opt_include,
'x|exclude=s' => \@opt_exclude,
'dskTable' => sub { $opt_use_dskTable = 1; },
'hrStorageTable' => sub { $opt_use_dskTable = 0; },
) or do {
print_usage();
exit($ERRORS{'UNKNOWN'});
};
if($opt_version) {
print_version();
exit($ERRORS{'UNKNOWN'});
}
if($opt_help) {
print_help();
exit($ERRORS{'UNKNOWN'});
}
if(!$opt_host) {
print "Host option not given\n";
print_usage();
exit($ERRORS{'UNKNOWN'});
}
if(!$opt_warn) {
print "No warning level given\n";
print_usage();
exit($ERRORS{'UNKNOWN'});
}
if(!$opt_crit) {
print "No critical level given\n";
print_usage();
exit($ERRORS{'UNKNOWN'});
}
if($opt_warn =~ /^(\d+)([kmgt])b?$/i) {
$opt_warn = convert_units($1, $2);
}
if($opt_crit =~ /^(\d+)([kmgt])b?$/i) {
$opt_crit = convert_units($1, $2);
}
push(@opt_exclude, @built_in_excludes);
if ($opt_use_dskTable) {
$dskTable = '.1.3.6.1.4.1.2021.9';
$dskIndex = '.1.3.6.1.4.1.2021.9.1.1';
$dskPath = '.1.3.6.1.4.1.2021.9.1.2';
$dskTotal = '.1.3.6.1.4.1.2021.9.1.6';
$dskAvail = '.1.3.6.1.4.1.2021.9.1.7';
$dskUsed = '.1.3.6.1.4.1.2021.9.1.8';
$dskPercentNode = '.1.3.6.1.4.1.2021.9.1.10';
} else {
$dskTable = '.1.3.6.1.2.1.25.2.3'; # hrStorageTable
$dskIndex = '.1.3.6.1.2.1.25.2.3.1.1'; # hrStorageIndex
$dskPath = '.1.3.6.1.2.1.25.2.3.1.3'; # hrStorageDescr
$dskAllocUnits = '.1.3.6.1.2.1.25.2.3.1.4'; # hrStorageAllocationUnits
$dskTotal = '.1.3.6.1.2.1.25.2.3.1.5'; # hrStorageSize
$dskUsed = '.1.3.6.1.2.1.25.2.3.1.6'; # hrStorageUsed
$dskPercentNode = undef; # hrStorageTable doesn't report on i-nodes
}
# set alarm in case we hang
$SIG{ALRM} = sub {
print "DISK UNKNOWN - Timeout after $opt_timeout seconds\n";
exit($ERRORS{'UNKNOWN'});
};
alarm($opt_timeout);
# connect to the snmp server
my ($snmp, $errstr);
foreach my $domain ("udp4", "udp6", "tcp4", "tcp6") {
($snmp, $errstr) = Net::SNMP->session(
-hostname => $opt_host,
-version => $opt_snmpver,
-community => $opt_community,
-timeout => $opt_timeout,
-domain => $domain,
);
last if (defined($snmp));
}
unless($snmp) {
print("DISK UNKNOWN - Could not create SNMP session: $errstr\n");
exit($ERRORS{'UNKNOWN'});
}
# grab the table
my $result = $snmp->get_table(-baseoid => $dskTable);
# parse into the disks hash
foreach my $key (keys(%$result)) {
my($base, $index) = ($key =~ /($dskTable\.1\.\d+)\.(\d+)/);
#print "base: [$base] index: [$index]\n";
if($base eq $dskPath) { $disks{$index}{path} = $result->{$key}; }
if($base eq $dskTotal) { $disks{$index}{total} = $result->{$key}; }
if($base eq $dskUsed) { $disks{$index}{used} = $result->{$key}; }
if($opt_use_dskTable) {
if($base eq $dskAvail) { $disks{$index}{avail} = $result->{$key}; }
if($base eq $dskPercentNode) { $disks{$index}{inodes} = $result->{$key}; }
} else {
if($base eq $dskAllocUnits) { $disks{$index}{alloc_unit} = $result->{$key}; }
}
}
# modify the disks hash to only include those paths to check
# based on the include and exlude options
foreach my $key (keys(%disks)) {
my $path = $disks{$key}{path};
if(@opt_include) {
my $is_included = 0;
foreach my $include (@opt_include) {
if($include eq $path) {
#print "including $key [$path]: in includes ($include)\n";
$is_included = 1;
last;
}
}
if(!$is_included) {
#print "excluding $key [$path]: not in includes\n";
delete($disks{$key});
}
}
if(@opt_exclude) {
foreach my $exclude (@opt_exclude) {
if($exclude eq $path) {
#print "excluding $key [$path]: in excludes ($exclude)\n";
delete($disks{$key});
}
}
}
}
# Check the devices if their type is in the %types hash
@criticals = ();
@warnings = ();
@oks = ();
foreach my $key (keys(%disks)) {
my($path, $total, $used) = ( $disks{$key}{path}, $disks{$key}{total}, $disks{$key}{used} );
# Skip non-real devices
next if $total <= 0;
if (not $opt_use_dskTable) {
my $unit = $disks{$key}{alloc_unit};
$total = int($total*$unit/1024);
$used = int($used*$unit/1024);
}
my $avail = defined($disks{$key}{avail}) ? $disks{$key}{avail} : $total - $used;
my $pcntavail = int(100.0*$avail/$total);
my $pcntinodes = $disks{$key}{inodes};
#print "path [$path] total [$total] used [$used] pcntavail: [$pcntavail]\n";
my $output = "$avail kB ($pcntavail%) free on $path";
if(!check_disk($opt_crit, $pcntavail, $avail)) {
push(@criticals, $output);
} elsif(!check_disk($opt_warn, $pcntavail, $avail)) {
push(@warnings, $output);
} else {
push(@oks, $output);
}
if(defined($pcntinodes)) {
$output = "$pcntinodes% inodes used on $path";
if($pcntinodes > (100 - $opt_crit_inodes)) {
push(@criticals, $output);
} elsif($pcntinodes > (100 - $opt_warn_inodes)) {
push(@warnings, $output);
} else {
push(@oks, $output);
}
}
}
if(!keys(%disks)) {
push(@criticals, "No disks found!");
}
if(@criticals) {
print "DISK CRITICAL - " . join(', ', @criticals) . "\n";
exit($ERRORS{'CRITICAL'});
} elsif(@warnings) {
print "DISK WARNING - " . join(', ', @warnings) . "\n";
exit($ERRORS{'WARNING'});
} elsif(@oks) {
print "DISK OK - " . join(', ', @oks) . "\n";
exit($ERRORS{'OK'});
}
sub check_disk {
my($limit, $pcntavail, $avail) = @_;
#print "check_disk($limit, $pcntavail, $avail)\n";
if($limit =~ /^(\d+)%$/) {
$limit = $1;
if($pcntavail <= 0) {
return(0);
} elsif($pcntavail <= $limit) {
return(0);
} else {
return(1);
}
} elsif($limit =~ /^\d+$/) {
if($avail <= $limit) {
return(0);
} else {
return(1);
}
} else {
die("Invalid limit: $limit\n");
}
}
sub convert_units {
# Convert $limit in the form on 20MB, 30g, 60T, ... to kB
my ($limit, $unit) = @_;
$unit =~ tr/A-Z/a-z/; # Lowercase it for ease of use
return $limit if ($unit eq "k");
$limit *= 1024;
return $limit if ($unit eq "m");
$limit *= 1024;
return $limit if ($unit eq "g");
$limit *= 1024;
return $limit if ($unit eq "t");
return 0; # Unknown unit
}
sub print_usage {
my $tab = ' ' x length($PROGNAME);
print <<EOB
Usage:
$PROGNAME -H host -w limit -c limit [-i include] [-x exclude]
$tab [-C snmp_community] [-S snmp_version] [-t timeout]
$PROGNAME --version
$PROGNAME --help
EOB
}
sub print_version {
print_revision($PROGNAME, $VERSION);
}
sub print_help {
print_version();
print <<EOB;
Check disk space on the remote machine through SNMP. You must configure the
SNMP daemon on the target machine to report the disk space using the option
"disk /" or "disk /home", etc.
EOB
print_usage();
print <<EOB;
Required Arguments:
-H, --host=HOST
The name or address of the host running SNMP.
-w, --warning=INTEGER[kMGT]
Exit with WARNING status if less than INTEGER kB / MB / GB / TB of disk are free
-w, --warning=PERCENT%
Exit with WARNING status if less than PERCENT of disk space is free
-c, --critical=INTEGER[kMGT]
Exit with CRITICAL status if less than INTEGER kB / MB / GB / TB of disk are free
-c, --critical=PERCENT%
Exit with CRITICAL status if less than PERCENT of disk space is free
-i, --include=PATH or DEVICE
Check only the included paths
-e, --exclude=PATH or DEVICE
Do not check the given paths
--dskTable
Use dskTable MIB (default, used for instance on Linux)
--hrStorageTable
Use hrStorageTable MIB (for instance on Solaris)
Optional Arguments:
-C, --community=STRING
The community string of the SNMP agent. Default: public
-S, --snmpver=STRING
The version of snmp to use. 1 and 2 are supported. Default: 1
-t, --timeout=INTEGER
Number of seconds to wait for a response.
EOB
}