-
Notifications
You must be signed in to change notification settings - Fork 13
/
check_chassis_extreme.php
executable file
·606 lines (465 loc) · 17.8 KB
/
check_chassis_extreme.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
#! /usr/bin/php
<?php
/**
* check_chassis_extreme.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 - 2014, 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 - 2014, 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]>
*/
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,
'tempwarn' => 55,
'tempcrit' => 65,
'reboot' => 3600,
'thres-cpu' => '85,95',
];
// 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();
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
{
$temp = $snmp->useExtreme_System_Common()->currentTemperature();
$over = $snmp->useExtreme_System_Common()->overTemperatureAlarm();
} catch( OSS_SNMP\Exception $e ) {
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}'C", LOG__VERBOSE );
_log( "Over temp alert: " . ( $over ? 'YES' : 'NO' ), LOG__VERBOSE );
$tempdata = sprintf( "Temp: %d'C", $temp );
if( $over ) {
setStatus( STATUS_CRITICAL );
$criticals .= "Temperature alarm set: {$temp}'C. ";
}
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
{
$fans = $snmp->useExtreme_System_Common()->fanOperational();
$speeds = $snmp->useExtreme_System_Common()->fanSpeed();
} 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( $fans as $i => $operational )
{
$ok = $operational && $speeds[$i] >= 2000;
_log( "Fan: {$i} - " . ( $ok ? 'OK' : 'NOT OK' ) . " ({$speeds[$i]} RPM)", LOG__VERBOSE );
$fandata .= " [{$i} - " . ( $ok ? 'OK' : 'NOT OK' ) . " ({$speeds[$i]} RPM)];";
if( !$ok ) {
setStatus( STATUS_CRITICAL );
if( $operational && $speeds[$i] < 2000 )
$criticals .= "Fan {$i} is operational but speed is outside normal operating range (<2000). ";
else if( !$operational )
$criticals .= "Fan {$i} is not operational. ";
else
$criticals .= "Fan {$i} is not operational (reason unknown). ";
}
}
$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
{
$psuStates = $snmp->useExtreme_System_Common()->powerSupplyStatus();
$psuSerials = $snmp->useExtreme_System_Common()->powerSupplySerialNumbers();
$psuSources = $snmp->useExtreme_System_Common()->powerSupplySource();
$sources = OSS_SNMP\MIBS\Extreme\System\Common::$POWER_SUPPLY_SOURCES;
$states = OSS_SNMP\MIBS\Extreme\System\Common::$POWER_SUPPLY_STATES;
$systemPowerState = $snmp->useExtreme_System_Common()->systemPowerState();
$systemState = OSS_SNMP\MIBS\Extreme\System\Common::$POWER_STATES;
}
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: {$i} - {$states[$state]} (Serial: {$psuSerials[$i]}; Source: {$sources[ $psuSources[$i] ]})", LOG__VERBOSE );
$psudata .= " {$i} - {$states[$state]};";
if( $state == OSS_SNMP\MIBS\Extreme\System\Common::POWER_SUPPLY_STATUS_NOT_PRESENT
&& !( isset( $cmdargs['ignore-psu-notpresent'] ) && $cmdargs['ignore-psu-notpresent'] ) )
{
setStatus( STATUS_WARNING );
$warnings .= "PSU $i is not present";
}
else if( $state != OSS_SNMP\MIBS\Extreme\System\Common::POWER_SUPPLY_STATUS_PRESENT_OK )
{
setStatus( STATUS_CRITICAL );
$criticals .= "PSU state for {$i}: {$states[$state]}";
}
}
$psudata .= ". ";
if( $systemPowerState == OSS_SNMP\MIBS\Extreme\System\Common::SYSTEM_POWER_STATE_REDUNDANT_POWER_AVAILABLE )
{
$psudata .= "Overall system power state: redundant power available";
}
else if( $systemPowerState == OSS_SNMP\MIBS\Extreme\System\Common::SYSTEM_POWER_STATE_SUFFICIENT_BUT_NOT_REDUNDANT_POWER )
{
setStatus( STATUS_WARNING );
$warnings .= "Overall system power state: sufficient but not redundant power available";
}
else if( $systemPowerState == OSS_SNMP\MIBS\Extreme\System\Common::SYSTEM_POWER_STATE_INSUFFICIENT_POWER )
{
setStatus( STATUS_CRITICAL );
$warnings .= "Overall system power state: insufficent power available";
}
$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->useExtreme_SwMonitor_Memory()->percentUsage();
$memData = " Memory (slot:usage%):";
foreach( $memUtil as $slotId => $usage ) {
_log( "Memory used in slot {$slotId}: {$usage}%", LOG__VERBOSE );
if( $usage > $cmdargs['memcrit'] )
{
setStatus( STATUS_CRITICAL );
$criticals .= "Memory usage in slot {$slotId} at {$usage}%. ";
}
else if( $usage > $cmdargs['memwarn'] )
{
setStatus( STATUS_WARNING );
$warningss .= "Memory usage in slot {$slotId} at {$usage}%. ";
}
else
$memData .= " {$slotId}:{$usage}%";
}
$normals .= $memData . '. ';
_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 );
$bootTime = $snmp->useExtreme_System_Common()->bootTime();
if( ( isset( $cmdargs['lastcheck'] ) && $cmdargs['lastcheck'] && $bootTime <= $cmdargs['lastcheck'] )
|| ( time() - $bootTime <= $cmdargs['reboot'] ) )
{
setStatus( STATUS_CRITICAL );
$criticals .= sprintf( "Device rebooted %0.1f minutes ago. ", ( time() - $bootTime ) / 60.0 );
}
$up = ( time() - $bootTime ) / 60.0 / 60.0;
if( $up < 24 )
$up = sprintf( "Uptime: %0.1f hours. ", $up );
else
$up = sprintf( "Uptime: %0.1f days. ", $up / 24 );
_log( "Last reboot: " . ( ( time() - $bootTime ) / 60.0 ) . " minutes ago", LOG__VERBOSE );
_log( $up, LOG__VERBOSE );
$normals .= " {$up}";
_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 );
$util = $snmp->useExtreme_SwMonitor_Cpu()->totalUtilization();
_log( "CPU: 5sec - $util%", LOG__VERBOSE );
$cpudata = "CPU: 5sec - {$util}%";
list( $w, $c ) = explode( ',', $cmdargs["thres-cpu"] );
if( $util >= $c )
{
setStatus( STATUS_CRITICAL );
$criticals .= "CPU 5sec usage at {$util}%. ";
}
else if( $util >= $w )
{
setStatus( STATUS_WARNING );
$warnings .= "CPU 5sec usage at {$util}%. ";
}
$normals .= " $cpudata.";
}
/**
* 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':
if( !isset( $argv[$i+1] ) ) { printUsage( true ); exit( STATUS_UNKNOWN ); }
$cmdargs['community'] = $argv[$i+1];
$i++;
break;
case 'h':
if( !isset( $argv[$i+1] ) ) { printUsage( true ); exit( STATUS_UNKNOWN ); }
$cmdargs['host'] = $argv[$i+1];
$i++;
break;
case 'p':
if( !isset( $argv[$i+1] ) ) { printUsage( true ); exit( STATUS_UNKNOWN ); }
$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, $cmdargs;
$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: {$cmdargs['memwarn']})
--memcrit <integer> Percentage of memory usage for critical (using: {$cmdargs['memcrit']})
--skip-temp Skip temperature checks
--tempwarn <integer> Degrees Celsius for warning (using: {$cmdargs['tempwarn']})
--tempcrit <integer> Degrees Celsius for critical (using: {$cmdargs['tempwarn']})
--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 is generated. Overrides
--reboot to ensure reboots are caught
--reboot <integer> How many seconds ago should we warn that the device has been rebooted (using: {$cmdargs['reboot']})
--skip-cpu Skip all CPU utilisation checks
--thres-cpu CPU warning,critical thresholds for 5sec checks (using {$cmdargs['thres-cpu']})
END_USAGE;
}