-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheck_usolved_disks.php
More file actions
496 lines (383 loc) · 15.2 KB
/
check_usolved_disks.php
File metadata and controls
496 lines (383 loc) · 15.2 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/php
<?php
/*
Automaticly checks all partitions of a Windows or Linux operating system.
You don't need special libraries. If you have PHP 5 or higher installed it should be working.
Copyright (c) 2015 www.usolved.net
Published under https://github.com/usolved/check_usolved_disks
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//------------------------------- Functions ---------------------------------
function show_help($help_for)
{
if(empty($help_for))
$help_for = "";
if($help_for == "ERROR_ARGUMENT_H")
{
echo "Unknown - argument -H (host address) is required but missing\n";
exit(3);
}
else if($help_for == "ERROR_ARGUMENT_C")
{
echo "Unknown - argument -C (snmp community string) is required but missing\n";
exit(3);
}
else if($help_for == "ERROR_ARGUMENT_c")
{
echo "Unknown - argument -c (scritical treshold) is required but missing\n";
exit(3);
}
else if($help_for == "ERROR_ARGUMENT_w")
{
echo "Unknown - argument -w (warning treshold) is required but missing\n";
exit(3);
}
else if($help_for == "SNMP")
{
echo "Unknown - Could't read SNMP information. Properly the host isn't configured correctly for SNMP or wrong SNMP version was given.\n";
exit(3);
}
else if($help_for == "SNMP_MODULE")
{
echo "Unknown - PHP SNMP mpdule isn't enabled. Please check if you have installed/enabled the snmp extension for PHP.\n";
exit(3);
}
else if($help_for == "ERROR_ARGUMENT_NUMERIC")
{
echo "Unknown - Warning and Critical have to be numeric.\n";
exit(3);
}
else if($help_for == "ERROR_NO_OS")
{
echo "Unknown - OS couldn't be determined. You can try to add the -O parameter if you know the os.\n";
exit(3);
}
else
{
echo "Usage:";
echo "
./".basename(__FILE__)." -H <host address> -C <snmp community> -w <warn> -c <crit> [-V <snmp version>] [-P <perfdata>] [-E '<exclude partitions>'] [-O '<operating system>']\n\n";
echo "Options:";
echo "
./".basename(__FILE__)."\n
-H <host address>
Give the host address with the IP address or FQDN
-C <snmp community>
Give the SNMP Community String
-w <warn>
Warning treshold in percent
-c <crit>
Critical treshold in percent
[-V <snmp version>]
Optional: SNMP version 1 or 2c are supported, if argument not given version 1 is used by default
[-P <perfdata>]
Optional: Give 'yes' as argument if you wish performace data output
[-E '<exclude partitions>']
Optional: Exclude partitions with a comma separated list on Windows like 'D:,E:' (with or without colon) or on Linux '/var,/tmp'
[-O '<operating system>']
Optional: The plugin detects automaticly the running OS. But in some cases (e.g. Barracuda firewalls) the running OS can't be determined. So use -O Linux or -O Windows to force the right OIDs
\n";
echo "Example:";
echo "
./".basename(__FILE__)." -H localhost -C public -V 2 -w 90 -c 95 -P yes -E 'D:,E:'\n\n";
exit(3);
}
}
function snmp_walk($snmp_host, $snmp_community, $snmp_oid, $snmp_version)
{
if(extension_loaded("snmp"))
{
if($snmp_version == "1")
{
if($snmp_return = @snmpwalk($snmp_host, $snmp_community, $snmp_oid))
return $snmp_return;
else
show_help("SNMP");
}
else if($snmp_version == "2c" || $snmp_version == "2")
{
if($snmp_return = @snmp2_walk($snmp_host, $snmp_community, $snmp_oid))
return $snmp_return;
else
show_help("SNMP");
}
else
show_help("SNMP");
}
else
show_help("SNMP_MODULE");
}
//---------------------------------------------------------------------------
//----------------------------- Check arguments -----------------------------
//get arguments from cli
$arguments = array();
$arguments = getopt("H:C:V:w:c:E:P:O:");
if(is_array($arguments) && count($arguments) < 4)
show_help("");
if((isset($arguments['w']) && !is_numeric($arguments['w'])) || (isset($arguments['c']) && !is_numeric($arguments['c'])))
show_help("ERROR_ARGUMENT_NUMERIC");
//---------------------------------------------------------------------------
//---------------- Get arguments and set variables --------------------------
//get and check host address argument
if(isset($arguments['H']))
$snmp_host = $arguments['H'];
else
show_help("ERROR_ARGUMENT_H");
//get and check snmp community string argument
if(isset($arguments['C']))
$snmp_community = $arguments['C'];
else
show_help("ERROR_ARGUMENT_C");
//get and check warning argument
if(isset($arguments['w']))
$snmp_warning = $arguments['w'];
else
show_help("ERROR_ARGUMENT_w");
//get and check critical argument
if(isset($arguments['c']))
$snmp_critical = $arguments['c'];
else
show_help("ERROR_ARGUMENT_c");
//get and check snmp version argument
if(isset($arguments['V']))
$snmp_version = $arguments['V'];
else
$snmp_version = 1;
//get and check perfdata argument
if(isset($arguments['P']))
$perfdata = $arguments['P'];
else
$perfdata = "";
$output_snmp_exclude = "";
$snmp_exclude = "";
if(!empty($arguments['E']))
{
$snmp_exclude = trim($arguments['E']);
$output_snmp_exclude = $snmp_exclude;
}
if(!empty($arguments['O']))
$snmp_os = trim($arguments['O']);
else
$snmp_os = "";
$output_perfdata = ""; //define string for performance data output
$exit_code = 0; //set default exit code to "ok"
$storage_info[][] = array(); //define array to store disk informations
//----------------------------------------------------------------------------
//------------------------------ Set OID paths -------------------------------
$snmp_oid_hrStorageDesc = ".1.3.6.1.2.1.25.2.3.1.3";
$snmp_oid_hrStorageUnit = ".1.3.6.1.2.1.25.2.3.1.4";
$snmp_oid_hrStorageSize = ".1.3.6.1.2.1.25.2.3.1.5";
$snmp_oid_hrStorageUsed = ".1.3.6.1.2.1.25.2.3.1.6";
$snmp_oid_hrStorageType = ".1.3.6.1.2.1.25.2.3.1.2";
$snmp_oid_sysDescr = ".1.3.6.1.2.1.1.1";
//---------------------------------------------------------------------------
//--------------- Walk through SNMP informations from server ----------------
$i = 0;
$hrStorageDesc = snmp_walk($snmp_host, $snmp_community, $snmp_oid_hrStorageDesc, $snmp_version);
foreach($hrStorageDesc as $hrStorageDesc_value)
{
$storage_info[$i]['hrStorageDesc'] = str_replace("STRING: ","",$hrStorageDesc_value);
$i++;
}
$i = 0;
$hrStorageSize = snmp_walk($snmp_host, $snmp_community, $snmp_oid_hrStorageSize, $snmp_version);
foreach($hrStorageSize as $hrStorageSize_value)
{
$storage_info[$i]['hrStorageSize'] = str_replace("INTEGER: ","",$hrStorageSize_value);
$i++;
}
$i = 0;
$hrStorageUsed = snmp_walk($snmp_host, $snmp_community, $snmp_oid_hrStorageUsed, $snmp_version);
foreach($hrStorageUsed as $hrStorageUsed_value)
{
$storage_info[$i]['hrStorageUsed'] = str_replace("INTEGER: ","",$hrStorageUsed_value);
$i++;
}
$i = 0;
$hrStorageUnit = snmp_walk($snmp_host, $snmp_community, $snmp_oid_hrStorageUnit, $snmp_version);
foreach($hrStorageUnit as $hrStorageUnit_value)
{
$hrStorageUnit_value = str_replace("INTEGER: ","",$hrStorageUnit_value);
$hrStorageUnit_value = str_replace(" Bytes","",$hrStorageUnit_value);
$storage_info[$i]['hrStorageUnit'] = $hrStorageUnit_value;
$i++;
}
$i = 0;
$hrStorageType = snmp_walk($snmp_host, $snmp_community, $snmp_oid_hrStorageType, $snmp_version);
foreach($hrStorageType as $hrStorageType_value)
{
$hrStorageType_value = str_replace("INTEGER: ","",$hrStorageType_value);
$hrStorageType_value = str_replace(" Bytes","",$hrStorageType_value);
$storage_info[$i]['hrStorageType'] = $hrStorageType_value;
$i++;
}
$sysDescr = snmp_walk($snmp_host, $snmp_community, $snmp_oid_sysDescr, $snmp_version);
//---------------------------------------------------------------------------
//--------------------- Loop throught all partitions ------------------------
$i = 0;
$disks_found = 0;
$output_string_head_error = "";
$output_string_head = "";
$output_string = "";
$os_determined = false;
if(!empty($snmp_exclude))
$snmp_exclude = explode(",",$snmp_exclude);
foreach($storage_info as $storage_info_value)
{
if(empty($storage_info_value['hrStorageDesc']))
{
echo "Error - No SNMP response\n";
exit(3);
}
$output_storage = true;
$hrStorageDesc_output = $storage_info_value['hrStorageDesc'];
$hrStorageType_output = $storage_info_value['hrStorageType'];
//if the disk size is bigger than the 32 bit snmp value could store it, then add value to correct the value
$hrStorageSize = $storage_info_value['hrStorageSize'] < 0 ? $storage_info_value['hrStorageSize'] + 4294967296 : $storage_info_value['hrStorageSize'];
$hrStorageUsed = $storage_info_value['hrStorageUsed'] < 0 ? $storage_info_value['hrStorageUsed'] + 4294967296 : $storage_info_value['hrStorageUsed'];
$hrStorageSize_output = round( (($hrStorageSize * $storage_info_value['hrStorageUnit']) / 1024 / 1024 / 1024), 2); //byte to gb
$hrStorageUsed_output = round( (($hrStorageUsed * $storage_info_value['hrStorageUnit']) / 1024 / 1024 / 1024), 2);
//---------------------------------------------------------------------------
//-------------------------- Check Linux Partitions -------------------------
if((!empty($snmp_os) && $snmp_os == "Linux") || strstr($sysDescr[0], "Linux"))
{
$os_determined = true;
//show only harddisks
if((!strstr($hrStorageType_output, "hrStorageFixedDisk") && !strstr($hrStorageType_output, "hrStorageNetworkDisk")) && (!strstr($hrStorageType_output, ".3.6.1.2.1.25.2.1.4") && !strstr($hrStorageType_output, ".3.6.1.2.1.25.2.1.10")))
$output_storage = false;
//don't show excluded partitions
if(is_array($snmp_exclude) && in_array($hrStorageDesc_output, $snmp_exclude))
$output_storage = false;
//if current disk meets the criteria
if($output_storage == true)
{
if($hrStorageSize_output > 0)
{
$percentage_used = round( (100 / $hrStorageSize_output) * $hrStorageUsed_output, 1);
$used_gb_warning = round( ($hrStorageSize_output / 100) * $snmp_warning, 0);
$used_gb_critical = round( ($hrStorageSize_output / 100) * $snmp_critical, 0);
if($percentage_used > $snmp_critical)
{
$output_string .= "Critical - ";
$exit_code = 2;
$output_string_head_error .= $hrStorageDesc_output." (".$hrStorageUsed_output." GB of ".$hrStorageSize_output." GB), ";
}
else if($percentage_used > $snmp_warning)
{
$output_string .= "Warning - ";
if($exit_code != 2) //if there was already a critical, don't overwrite with a warning
$exit_code = 1;
$output_string_head_error .= $hrStorageDesc_output." (".$hrStorageUsed_output." GB of ".$hrStorageSize_output." GB), ";
}
}
$output_string_head .= $hrStorageDesc_output." (".$percentage_used."%), ";
$output_string .= $hrStorageDesc_output." - ";
$output_string .= "Size: ".$hrStorageSize_output. " GB / ";
$output_string .= "Used: ".$hrStorageUsed_output. " GB (".$percentage_used."% used)";
$output_string .= "\n";
if($perfdata == "yes")
{
$output_perfdata .= "Disk-".$hrStorageDesc_output."=".$hrStorageUsed_output."GB;".$used_gb_warning.";".$used_gb_critical.";0;".$hrStorageSize_output." ";
}
$disks_found++;
}
}
//---------------------------------------------------------------------------
//------------------------- Check Windows Partitions ------------------------
else if((!empty($snmp_os) && $snmp_os == "Windows") || strstr($sysDescr[0], "Windows"))
{
$os_determined = true;
//show only harddisks
if(!strstr($hrStorageType_output, "hrStorageFixedDisk") && !strstr($hrStorageType_output, "hrStorageNetworkDisk") && !strstr($hrStorageType_output, ".3.6.1.2.1.25.2.1.4") && !strstr($hrStorageType_output, ".3.6.1.2.1.25.2.1.10"))
$output_storage = false;
//if not <name>:\, don't show it
if(!strstr($hrStorageDesc_output,":\\"))
$output_storage = false;
//don't show parition, if size is zero
if($hrStorageSize_output == 0)
$output_storage = false;
//$hrStorageDesc_output could be "C:\ Label: Serial Number a4d5d99d" so just use the first two chars
if(substr($hrStorageDesc_output, 0, 1) == "\"")
$hrStorageDesc_output = substr($hrStorageDesc_output, 1, 2);
else
$hrStorageDesc_output = substr($hrStorageDesc_output, 0, 2);
//don't show excluded partitions
if(is_array($snmp_exclude) && (in_array($hrStorageDesc_output, $snmp_exclude) || in_array(str_replace(":","",$hrStorageDesc_output), $snmp_exclude)))
$output_storage = false;
//if current disk meets the criteria
if($output_storage == true)
{
if($hrStorageSize_output > 0)
{
$percentage_used = round( (100 / $hrStorageSize_output) * $hrStorageUsed_output, 1);
$used_gb_warning = round( ($hrStorageSize_output / 100) * $snmp_warning, 0);
$used_gb_critical = round( ($hrStorageSize_output / 100) * $snmp_critical, 0);
if($percentage_used > $snmp_critical)
{
$output_string .= "Critical - ";
$exit_code = 2;
$output_string_head_error .= $hrStorageDesc_output." (".$hrStorageUsed_output." GB of ".$hrStorageSize_output." GB), ";
}
else if($percentage_used > $snmp_warning)
{
$output_string .= "Warning - ";
if($exit_code != 2) //if there was already a critical, don't overwrite with a warning
$exit_code = 1;
$output_string_head_error .= $hrStorageDesc_output." (".$hrStorageUsed_output." GB of ".$hrStorageSize_output." GB), ";
}
$output_string_head .= $hrStorageDesc_output." (".$percentage_used."%), ";
$output_string .= $hrStorageDesc_output." - ";
$output_string .= "Size: ".$hrStorageSize_output. " GB / ";
$output_string .= "Used: ".$hrStorageUsed_output. " GB (".$percentage_used."% used)";
$output_string .= "\n";
if($perfdata == "yes")
{
$output_perfdata .= "Disk-".$hrStorageDesc_output."=".$hrStorageUsed_output."GB;".$used_gb_warning.";".$used_gb_critical.";0;".$hrStorageSize_output." ";
}
$disks_found++;
}
}
}
$i++;
}
if($os_determined == false)
show_help("ERROR_NO_OS");
if($disks_found == 0)
{
$output_string_head .= "Could not get disk info, ";
$exit_code = 2;
}
//---------------------------------------------------------------------------
//----------------------------- Output to stdout ----------------------------
//Output status information
if(!empty($output_string_head_error))
$output_string_head_error = substr($output_string_head_error, 0, -2);
echo "Disks ";
if($exit_code == 0)
echo "OK";
else if($exit_code == 1)
echo "Warning {$output_string_head_error}";
else if($exit_code == 2)
echo "Critical {$output_string_head_error}";
echo " // ".substr($output_string_head, 0, -2);
//Output excluded disks if argument "E" was given
if(!empty($output_snmp_exclude))
echo " Excluded: ".$output_snmp_exclude;
//Output extended host information (for Nagios version 3 or higher)
echo "\n".$output_string;
//Output performance data if argument "P" was given
if($perfdata == "yes")
echo "|".$output_perfdata;
//exit with specific code for ok, warning or critical
exit($exit_code);
?>