forked from barryo/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_chassis_brocade.php
executable file
·679 lines (534 loc) · 19.4 KB
/
check_chassis_brocade.php
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#! /usr/bin/php
<?php
/**
* check_chassis_brocade.php - Nagios plugin
*
*
* This file is part of "barryo / nagios-plugins" - a library of tools and
* utilities for Nagios developed by Barry O'Donovan
* (http://www.barryodonovan.com/) and his company, Open Solutions
* (http://www.opensolutions.ie/).
*
* Copyright (c) 2004 - 2013, Open Source Solutions Limited, Dublin, Ireland
* All rights reserved.
*
* Contact: Barry O'Donovan - info (at) opensolutions (dot) ie
* http://www.opensolutions.ie/
*
* LICENSE
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* * Neither the name of Open Solutions nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @package barryo / nagios-plugins
* @copyright Copyright (c) 2004 - 2013, Open Source Solutions Limited, Dublin, Ireland
* @license http://www.opensolutions.ie/licenses/new-bsd New BSD License
* @link http://www.opensolutions.ie/ Open Source Solutions Limited
* @author Barry O'Donovan <[email protected]>
* @author The Skilled Team of PHP Developers at Open Solutions <[email protected]>
*/
date_default_timezone_set('Europe/Dublin');
define( "VERSION", '1.0.0' );
ini_set( 'max_execution_time', '55' );
ini_set( 'display_errors', true );
ini_set( 'display_startup_errors', true );
define( "STATUS_OK", 0 );
define( "STATUS_WARNING", 1 );
define( "STATUS_CRITICAL", 2 );
define( "STATUS_UNKNOWN", 3 );
define( "LOG__NONE", 0 );
define( "LOG__ERROR", 1 );
define( "LOG__VERBOSE", 2 );
define( "LOG__DEBUG", 3 );
// initialise some variables
$status = STATUS_OK;
$log_level = LOG__NONE;
// possible output strings
$criticals = "";
$warnings = "";
$unknowns = "";
$normals = "";
// set default values for command line arguments
$cmdargs = [
'port' => '161',
'log_level' => LOG__NONE,
'memwarn' => 80,
'memcrit' => 90,
'reboot' => 3600,
'thres-cpu-1sec' => '95,98',
'thres-cpu-5sec' => '85,95',
'thres-cpu-1min' => '70,90'
];
// parse the command line arguments
parseArguments();
//print_r( $cmdargs ); die();
require 'OSS_SNMP/OSS_SNMP/SNMP.php';
$snmp = new \OSS_SNMP\SNMP( $cmdargs['host'], $cmdargs['community'] );
checkCPU();
checkReboot();
checkPower();
checkFans();
checkTemperature();
checkMemory();
checkOthers();
if( $status == STATUS_OK )
$msg = "OK -{$normals}\n";
else
$msg = "{$criticals}{$warnings}{$unknowns}\n";
echo $msg;
exit( $status );
/**
* Checks the chassis temperature
*
*/
function checkTemperature()
{
global $snmp, $cmdargs, $periods, $criticals, $warnings, $unknowns, $normals;
if( isset( $cmdargs['skip-temp'] ) && $cmdargs['skip-temp'] )
return;
_log( "========== Temperature check start ==========", LOG__DEBUG );
try
{
// remember - Foundry use units of 0.5 Celcius:
$temp = $snmp->useFoundry_Chassis()->actualTemperature() / 2.0;
$warn = $snmp->useFoundry_Chassis()->warningTemperature() / 2.0;
$shut = $snmp->useFoundry_Chassis()->shutdownTemperature() / 2.0;
if( !$warn ) $warn = 64.0;
}
catch( OSS_SNMP\Exception $e )
{
/* Temp not supported
setStatus( STATUS_UNKNOWN );
$unknowns .= "Temperature unknown - possibly not supported on platform? Use --skip-temp. ";
_log( "WARNING: Temperature unknown - possibly not supported on platform? Use --skip-temp.\n", LOG__ERROR );
*/
return;
}
_log( "Temp: $temp (Warn: $warn Shutdown: $shut)", LOG__VERBOSE );
$tempdata = sprintf( "Temp (A/W/C): %0.1f/%0.1f/%0.1f", $temp, $warn, $shut );
if( $temp >= $warn )
{
setStatus( STATUS_CRITICAL );
$criticals .= "Temperature approaching SHUTDOWN threshold: $temp/$shut";
}
if( isset( $cmdargs['tempcrit'] ) && $cmdargs['tempcrit'] && $temp >= $cmdargs['tempcrit'] )
{
setStatus( STATUS_CRITICAL );
$criticals .= "Temperature exceeds critical threshold: $temp/" . $cmdargs['tempcrit'];
}
else if( isset( $cmdargs['tempwarn'] ) && $cmdargs['tempwarn'] && $temp >= $cmdargs['tempwarn'] )
{
setStatus( STATUS_WARNING );
$warnings .= "Temperature exceeds warning threshold: $temp/" . $cmdargs['tempwarn'];
}
$normals .= " $tempdata.";
_log( "========== Temperature check end ==========\n", LOG__DEBUG );
}
function checkFans()
{
global $snmp, $cmdargs, $periods, $criticals, $warnings, $unknowns, $normals;
if( isset( $cmdargs['skip-fans'] ) && $cmdargs['skip-fans'] )
return;
_log( "========== Fan check start ==========", LOG__DEBUG );
try
{
$fanDescs = $snmp->useFoundry_Chassis()->fanDescriptions();
$fanStates = $snmp->useFoundry_Chassis()->fanStates();
}
catch( OSS_SNMP\Exception $e )
{
setStatus( STATUS_UNKNOWN );
$unknowns .= "Fan states unknown - possibly not supported on platform? Use --skip-fans. ";
_log( "WARNING: Fan states unknown - possibly not supported on platform? Use --skip-fans.\n", LOG__ERROR );
return;
}
$fandata = 'Fans:';
foreach( $fanStates as $i => $state )
{
_log( "Fan: {$fanDescs[$i]} - $state", LOG__VERBOSE );
$fandata .= $state == OSS_SNMP\MIBS\Foundry\Chassis::FAN_STATE_NORMAL ? ' OK' : " " . strtoupper( $state );
if( $state == OSS_SNMP\MIBS\Foundry\Chassis::FAN_STATE_FAILURE )
{
setStatus( STATUS_CRITICAL );
$criticals .= "Fan state for {$fanDescs[$i]}: $state";
}
else if( $state != OSS_SNMP\MIBS\Foundry\Chassis::FAN_STATE_NORMAL )
{
setStatus( STATUS_WARNING );
$criticals .= "Fan state for {$fanDescs[$i]}: $state";
}
}
$normals .= " $fandata.";
_log( "========== Fan check end ==========\n", LOG__DEBUG );
}
function checkPower()
{
global $snmp, $cmdargs, $periods, $criticals, $warnings, $unknowns, $normals;
if( isset( $cmdargs['skip-psu'] ) && $cmdargs['skip-psu'] )
return;
_log( "========== PSU check start ==========", LOG__DEBUG );
try
{
$psuDescs = $snmp->useFoundry_Chassis()->psuDescriptions();
$psuStates = $snmp->useFoundry_Chassis()->psuStates();
}
catch( OSS_SNMP\Exception $e )
{
setStatus( STATUS_UNKNOWN );
$unknowns .= "PSU states unknown - possibly not supported on platform? Use --skip-psu. ";
_log( "WARNING: PSU states unknown - possibly not supported on platform? Use --skip-psu.\n", LOG__ERROR );
return;
}
$psudata = 'PSUs:';
foreach( $psuStates as $i => $state )
{
_log( "PSU: {$psuDescs[$i]} - $state", LOG__VERBOSE );
$psudata .= $state == OSS_SNMP\MIBS\Foundry\Chassis::PSU_STATE_NORMAL ? ' OK' : " " . strtoupper( $state );
if( $state == OSS_SNMP\MIBS\Foundry\Chassis::PSU_STATE_FAILURE )
{
setStatus( STATUS_CRITICAL );
$criticals .= "PSU state for {$psuDescs[$i]}: $state";
}
else if( $state != OSS_SNMP\MIBS\Foundry\Chassis::PSU_STATE_NORMAL )
{
setStatus( STATUS_WARNING );
$criticals .= "PSU state for {$psuDescs[$i]}: $state";
}
}
$normals .= " $psudata.";
_log( "========== PSU check end ==========\n", LOG__DEBUG );
}
function checkMemory()
{
global $snmp, $cmdargs, $periods, $criticals, $warnings, $unknowns, $normals;
if( isset( $cmdargs['skip-mem'] ) && $cmdargs['skip-mem'] )
return;
_log( "========== Memory check start ==========", LOG__DEBUG );
$memUtil = $snmp->useFoundry_Chassis()->memoryUtilisation();
_log( "Memory used: {$memUtil}%", LOG__VERBOSE );
if( $memUtil > $cmdargs['memcrit'] )
{
setStatus( STATUS_CRITICAL );
$criticals .= "Memory usage at {$memUtil}%. ";
}
else if( $memUtil > $cmdargs['memwarn'] )
{
setStatus( STATUS_WARNING );
$warnings .= "Memory usage at {$memUtil}%. ";
}
else
$normals .= " Memory OK ({$memUtil}%).";
_log( "========== Memory check end ==========\n", LOG__DEBUG );
}
function checkReboot()
{
global $snmp, $cmdargs, $periods, $criticals, $warnings, $unknowns, $normals;
if( isset( $cmdargs['skip-reboot'] ) && $cmdargs['skip-reboot'] )
return;
_log( "========== Reboot check start ==========", LOG__DEBUG );
// uptime in seconds
$sysuptime = $snmp->useSystem()->uptime() / 100.0;
if( ( isset( $cmdargs['lastcheck'] ) && $cmdargs['lastcheck'] && $sysuptime <= $cmdargs['lastcheck'] )
|| ( $sysuptime < $cmdargs['reboot'] ) )
{
// Brocade use a 32bit integer for sysuptime (argh!) so it rolls over every 497.1 days
// If we find that the system has rebooted, we'll get a second opinion
$engineTime = $snmp->useSNMP_Engine()->time();
if( ( isset( $cmdargs['lastcheck'] ) && $cmdargs['lastcheck'] && $engineTime <= $cmdargs['lastcheck'] )
|| ( $engineTime < $cmdargs['reboot'] ) )
{
setStatus( STATUS_CRITICAL );
$criticals .= sprintf( "Device rebooted %0.1f minutes ago. ", $sysuptime / 60.0 );
}
}
_log( "========== Reboot check end ==========", LOG__DEBUG );
}
function checkCPU()
{
global $snmp, $cmdargs, $periods, $criticals, $warnings, $unknowns, $normals;
if( isset( $cmdargs['skip-cpu'] ) && $cmdargs['skip-cpu'] )
return;
_log( "========== CPU check start ==========", LOG__DEBUG );
$cpudata = 'CPU: ';
foreach( [ '1sec', '5sec', '1min' ] as $period )
{
if( isset( $cmdargs["skip-cpu-$period"] ) && $cmdargs["skip-cpu-$period"] )
continue;
$fn = "cpu{$period}Utilisation";
$util = $snmp->useFoundry_Chassis()->$fn();
_log( "CPU: $period - $util%", LOG__VERBOSE );
$cpudata .= " $period $util%";
list( $w, $c ) = explode( ',', $cmdargs["thres-cpu-$period"] );
if( $util >= $c )
{
setStatus( STATUS_CRITICAL );
$criticals .= "CPU $period usage at {$util}%. ";
}
else if( $util >= $w )
{
setStatus( STATUS_WARNING );
$warnings .= "CPU $period usage at {$util}%. ";
}
}
$normals .= " $cpudata.";
}
function checkOthers()
{
global $snmp, $cmdargs, $periods, $criticals, $warnings, $unknowns, $normals;
if( isset( $cmdargs['skip-others'] ) && $cmdargs['skip-others'] )
return;
_log( "========== Others check start ==========", LOG__DEBUG );
try
{
if( $snmp->useFoundry_Chassis()->isQueueOverflow() )
{
_log( "Error - queue overflow indicated", LOG__VERBOSE );
setStatus( STATUS_CRITICAL );
$criticals .= "Queue overflow indicated. ";
}
else
_log( "OK - queue overflow not indicated", LOG__VERBOSE );
}
catch( \OSS_SNMP\Exception $e )
{ /* not supported on this platform */ }
try
{
if( $snmp->useFoundry_Chassis()->isBufferShortage() )
{
_log( "Error - buffer shortage indicated", LOG__VERBOSE );
setStatus( STATUS_CRITICAL );
$criticals .= "Buffer shortage indicated. ";
}
else
_log( "OK - buffer shortage not indicated", LOG__VERBOSE );
}
catch( \OSS_SNMP\Exception $e )
{ /* not supported on this platform */ }
try
{
if( $snmp->useFoundry_Chassis()->isDMAFailure() )
{
_log( "Error - DMA failure indicated", LOG__VERBOSE );
setStatus( STATUS_CRITICAL );
$criticals .= "DMA failure indicated. ";
}
else
_log( "OK - DMA failure not indicated", LOG__VERBOSE );
}
catch( \OSS_SNMP\Exception $e )
{ /* not supported on this platform */ }
try
{
if( $snmp->useFoundry_Chassis()->isResourceLow() )
{
_log( "Error - low resources indicated", LOG__VERBOSE );
setStatus( STATUS_CRITICAL );
$criticals .= "Low resources indicated. ";
}
else
_log( "OK - low resources not indicated", LOG__VERBOSE );
}
catch( \OSS_SNMP\Exception $e )
{ /* not supported on this platform */ }
try
{
if( $snmp->useFoundry_Chassis()->isExcessiveError() )
{
_log( "Error - excessive errors indicated", LOG__VERBOSE );
setStatus( STATUS_CRITICAL );
$criticals .= "Excessive errors indicated. ";
}
else
_log( "OK - excessive errors not indicated", LOG__VERBOSE );
}
catch( \OSS_SNMP\Exception $e )
{ /* not supported on this platform */ }
_log( "========== Others check end ==========", LOG__DEBUG );
}
/**
* Parses (and checks some) command line arguments
*/
function parseArguments()
{
global $checkOptions, $cmdargs, $checksEnabled, $periods, $periodsEnabled, $argc, $argv;
if( $argc == 1 )
{
printUsage( true );
exit( STATUS_UNKNOWN );
}
$i = 1;
while( $i < $argc )
{
if( $argv[$i][0] != '-' )
{
$i++;
continue;
}
switch( $argv[$i][1] )
{
case 'V':
printVersion();
exit( STATUS_OK );
break;
case 'v':
$cmdargs['log_level'] = LOG__VERBOSE;
$i++;
break;
case 'd':
$cmdargs['log_level'] = LOG__DEBUG;
$i++;
break;
case 'c':
$cmdargs['community'] = $argv[$i+1];
$i++;
break;
case 'h':
$cmdargs['host'] = $argv[$i+1];
$i++;
break;
case 'p':
$cmdargs['port'] = $argv[$i+1];
$i++;
break;
case '?':
printHelp();
exit( STATUS_OK );
break;
default:
if( !isset( $argv[$i+1] ) || substr( $argv[$i+1], 0, 1 ) == '-' )
$cmdargs[ substr( $argv[$i], 2 ) ] = true;
else
$cmdargs[ substr( $argv[$i], 2 ) ] = $argv[$i+1];
$i++;
break;
}
}
}
/**
* Sets the planned exit status without overriding a previous error states.
*
* @param int $new_status New status to set.
* @return void
*/
function setStatus( $new_status )
{
global $status;
if( $new_status > $status )
$status = $new_status;
}
/**
* Prints a given message to stdout (or stderr as appropriate).
*
* @param string $log The log message.
* @param int $level The log level the user has requested.
* @return void
*/
function _log( $log, $level )
{
global $cmdargs;
if( $level == LOG__ERROR )
fwrite( STDERR, "$log\n" );
else if( $level <= $cmdargs['log_level'] )
print( $log . "\n" );
}
/**
* Print script usage instructions to the stadout
*/
function printUsage()
{
global $argv;
$progname = basename( $argv[0] );
echo <<<END_USAGE
{$progname} -c <READCOMMUNITY> -p <PORT> -h <HOSTNAME> [-V] [-h] [-?] [--help]
END_USAGE;
}
/**
* Print version information
*/
function printVersion()
{
global $argv;
printf( basename( $argv[0] ) . " (Nagios Plugin) %s\n", VERSION );
echo "Licensed under the New BSD License\n\n";
echo <<<LICENSE
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
LICENSE;
}
function printHelp()
{
global $argv;
$progname = basename( $argv[0] );
echo <<<END_USAGE
{$progname} - Nagios plugin to check the status of Brocade chassis'
Copyright (c) 2004 - 2013 Open Source Solutions Ltd - http://www.opensolutions.ie/
{$progname} -c <READCOMMUNITY> -p <PORT> -h <HOSTNAME> [-V] [-?] [--help]
Options:
-?,--help
Print detailed help screen.
-V
Print version information.
-c
SNMP Community (public)
-h
Device to poll
-p
SNMP port (default 161)
-v
Verbose output
-d
Debug output
--skip-mem Skip memory checks
--memwarn <integer> Percentage of memory usage for warning (using: 70)
--memcrit <integer> Percentage of memory usage for critical (using: 90)
--skip-temp Skip temperature checks
--tempwarn <integer> Degrees Celsius for warning (in addition to device's setting)
--tempcrit <integer> Degrees Celsius for critical (in addition to device's setting)
--skip-fans Skip fan checks
--skip-psu Skip PSU(s) checks
--ignore-psu-notpresent Ignore PSUs that are not installed
--skip-reboot Skip reboot check
--lastcheck Nagios $LASTSERVICECHECK$ macro. Used by reboot check such that if the
last reboot was within the last check, then an alert if generated. Overrides
--reboot to ensure reboots are caught
--skip-others Skip 'other' checks
--skip-cpu Skip all CPU utilisation checks
--skip-cpu-1sec Skip 1sec CPU utilisation check
--skip-cpu-5sec Skip 5sec CPU utilisation check
--skip-cpu-1min Skip 1min CPU utilisation check
--thres-cpu-1sec CPU warning,critical thresholds for 1sec checks (using 95,98)
--thres-cpu-5sec CPU warning,critical thresholds for 5sec checks (using 85,95)
--thres-cpu-1min CPU warning,critical thresholds for 1min checks (using 70,90)
--reboot <integer> How many minutes ago should we warn that the device has been rebooted (using: 60)
END_USAGE;
}