-
Notifications
You must be signed in to change notification settings - Fork 13
/
check_port_errors.php
executable file
·473 lines (368 loc) · 12.1 KB
/
check_port_errors.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
#! /usr/bin/php
<?php
/**
* check_port_errors.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]>
* @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
];
// port interfaces to ignore.
//
// The array key is the hostname. The value is the shortened snmp name.
$ignoreports = [
// 'core-router02.example.com' => 'Gi0/19',
];
// parse the command line arguments
parseArguments();
// create a memcache key:
$MCKEY = 'NAGIOS_CHECK_PORT_ERRORS_' . md5( $cmdargs['host'] );
if( !class_exists( 'Memcache' ) )
die( "ERROR: php-memcache is required\n" );
$mc = new Memcache;
$mc->connect( 'localhost', 11211 ) or die( "ERROR: Could not connect to memcache on localhost:11211\n" );
_log( "Connected to Memcache with version: " . $mc->getVersion(), LOG__DEBUG );
require 'OSS_SNMP/OSS_SNMP/SNMP.php';
$snmp = new \OSS_SNMP\SNMP(
$cmdargs['host'],
$cmdargs['community'],
$cmdargs['snmpversion'],
$cmdargs['seclevel'],
$cmdargs['authprotocol'],
$cmdargs['authpassword'],
$cmdargs['privprotocol'],
$cmdargs['privpassword'],
);
$snmp->setSecName($cmdargs['username']);
// get interface types for later filtering
$types = $snmp->useIface()->types();
$names = filterForType( $snmp->useIface()->names(), $types, OSS_SNMP\MIBS\Iface::IF_TYPE_ETHERNETCSMACD );
_log( "Found " . count( $names ) . " physical ethernet ports", LOG__DEBUG );
// get current error counters
$ifInErrors = filterForType( $snmp->useIface()->inErrors(), $types, OSS_SNMP\MIBS\Iface::IF_TYPE_ETHERNETCSMACD );
$ifOutErrors = filterForType( $snmp->useIface()->outErrors(), $types, OSS_SNMP\MIBS\Iface::IF_TYPE_ETHERNETCSMACD );
_log( "Found " . count( $ifInErrors ) . " entries for in errors on physical ethernet ports", LOG__DEBUG );
_log( "Found " . count( $ifOutErrors ) . " entries for out errors on physical ethernet ports", LOG__DEBUG );
// delete unwanted entries
foreach ( array_keys( $ignoreports ) as $hostkey) {
if ($cmdargs[ 'host' ] == $hostkey) {
$portid = array_keys( $names, $ignoreports[ $hostkey ] )[0];
if( isset ($portid) ) {
unset( $ifInErrors[ $portid ] );
unset( $ifOutErrors[ $portid ] );
}
}
}
// if we don't have any entries already, set them and send an unknown
$cache = $mc->get( $MCKEY );
// save the new values to memcache
$newcache = [
'ifInErrors' => $ifInErrors,
'ifOutErrors' => $ifOutErrors,
'timestamp' => time()
];
if( !$mc->set( $MCKEY, $newcache, 0, 900 ) ) {
echo "UNKNOWN - could not update cache\n";
exit( STATUS_UNKNOWN );
}
if( $cache === false ) {
echo "UNKNOWN - no previous cached entries found\n";
exit( STATUS_UNKNOWN );
}
$msg = '';
foreach( [ 'IN' => 'ifInErrors', 'OUT' => 'ifOutErrors' ] as $dir => $name )
{
$result = subtractArray( $$name, $cache[ $name ] );
if( count( $result ) ) {
setStatus( STATUS_CRITICAL );
if( $criticals == '' )
$criticals = 'CRITICAL: In the last ' . ( time() - $cache['timestamp'] ) . ' seconds, port errors were found.';
$criticals .= " [DIRECTION: {$dir}] =>";
foreach( $result as $k => $v )
$criticals .= " " . $names[ $k ] . ": {$v} errors;";
$criticals .= ' ***';
}
else
$normals .= " No errors found for $dir packets on all interfaces.";
}
if( $status == STATUS_OK )
$msg = "OK -{$normals}\n";
else
$msg .= "{$criticals}{$warnings}{$unknowns}{$normals}\n";
echo $msg;
exit( $status );
function subtractArray( $new, $old )
{
$result = [];
foreach( $new as $k => $v )
if( $v - $old[$k] > 0 )
$result[$k] = $v - $old[$k];
return $result;
}
function filterForType( $ports, $types, $type )
{
foreach( $ports as $k => $v )
if( $types[$k] !== $type )
unset( $ports[ $k ] );
return $ports;
}
/**
* Parses (and checks some) command line arguments
*/
function parseArguments()
{
global $checkOptions, $cmdargs, $argc, $argv;
if( $argc == 1 )
{
printUsage( true );
exit( STATUS_UNKNOWN );
}
$i = 1;
$cmdargs['community'] = '';
$cmdargs['snmpversion'] = '2c';
$cmdargs['authprotocol'] = 'SHA';
$cmdargs['privprotocol'] = 'AES';
$cmdargs['seclevel'] = 'authPriv';
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 's':
$cmdargs['snmpversion'] = $argv[$i+1];
$i++;
break;
case 'u':
$cmdargs['username'] = $argv[$i+1];
$i++;
break;
case 'a':
$cmdargs['authprotocol'] = $argv[$i+1];
$i++;
break;
case 'A':
$cmdargs['authpassword'] = $argv[$i+1];
$i++;
break;
case 'e':
$cmdargs['privprotocol'] = $argv[$i+1];
$i++;
break;
case 'E':
$cmdargs['privpassword'] = $argv[$i+1];
$i++;
break;
case 'l':
$cmdargs['seclevel'] = $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> | -s 3] -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 for incrementing port errors'
Copyright (c) 2014 Open Source Solutions Ltd - http://www.opensolutions.ie/
{$progname} [-c <READCOMMUNITY> | -s 3] -p <PORT> -h <HOSTNAME> [-V] [-?] [--help]
WARNING: SCRIPT IS HARD CODED TO CONNECT TO A MEMCACHE INSTANCE ON:
localhost:11211
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
-s
SNMP Version (2c or 3)
-u
SNMPv3 username
-a
SNMPv3 authentication protocol
-A
SNMPv3 authentication password
-e
SNMPv3 encryption / privacy protocol
-E
SNMPv3 encryption / privacy password
-l
SNMPv3 security level (noAuthNoPriv|authNoPriv|authPriv)
END_USAGE;
}