From 1034f3561a54fb5809af9a0ae7f18992008744c8 Mon Sep 17 00:00:00 2001 From: Nicolas Damiens Date: Wed, 16 Oct 2013 11:02:00 +0200 Subject: [PATCH 1/7] add dreprecated retain alias : interval --- check_rsnapshot.php | 1 + 1 file changed, 1 insertion(+) diff --git a/check_rsnapshot.php b/check_rsnapshot.php index 8ff269e..ffe2220 100755 --- a/check_rsnapshot.php +++ b/check_rsnapshot.php @@ -735,6 +735,7 @@ function parseConfig( $confFile, $primary = true ) break; case "retain": + case "interval": _log( sprintf( "PARSING CONF: found retention period: %s => %s", $tokens[1], $tokens[2] ), LOG__DEBUG ); $cmdargs["retain"][ $tokens[1] ] = $tokens[2]; break; From 82e9994247b52f89a7056f8bfc568c9743949c5c Mon Sep 17 00:00:00 2001 From: Barry O'Donovan Date: Thu, 27 Mar 2014 12:17:56 +0000 Subject: [PATCH 2/7] Update check_rsnapshot.php --- check_rsnapshot.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/check_rsnapshot.php b/check_rsnapshot.php index ffe2220..75edc41 100755 --- a/check_rsnapshot.php +++ b/check_rsnapshot.php @@ -920,6 +920,8 @@ function parseArguments() _log( "Bad period provided. Use help for more details.", LOG__ERROR ); exit( STATUS_UNKNOWN ); } + else + break; } } $i++; From ccfbcb50a5432982fd40a5d90f99934c27831b9f Mon Sep 17 00:00:00 2001 From: Stanislav German-Evtushenko Date: Thu, 5 Jun 2014 11:53:57 +0400 Subject: [PATCH 3/7] check_chassis_brocade.pl: add new options 1) make --ignore-psu-notpresent option work 2) add --timeout option --- check_chassis_brocade.pl | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/check_chassis_brocade.pl b/check_chassis_brocade.pl index 4098d7b..3dae94b 100755 --- a/check_chassis_brocade.pl +++ b/check_chassis_brocade.pl @@ -46,7 +46,7 @@ my $status; -my $TIMEOUT = 20; +my $timeout = 20; my $state = "OK"; my $answer = ""; my $int; @@ -106,15 +106,6 @@ my $skipothers = 0; - -# Just in case of problems, let's not hang Nagios -$SIG{'ALRM'} = sub { - print( "ERROR: No snmp response from $hostname\n" ); - exit $ERRORS{"UNKNOWN"}; -}; -alarm( $TIMEOUT ); - - $status = GetOptions( "hostname=s", \$hostname, "community=s", \$community, @@ -138,11 +129,19 @@ "thres-cpu-5sec=s", \$threscpuarg{'5sec'}, "thres-cpu-1min=s", \$threscpuarg{'1min'}, "ignore-psu-notpresent", \$ignorepsunotpresent, + "timeout=i", \$timeout, "help|?", \$help, "verbose", \$verbose, "reboot=i", \$rebootWindow ); +# Just in case of problems, let's not hang Nagios +$SIG{'ALRM'} = sub { + print( "ERROR: No snmp response from $hostname\n" ); + exit $ERRORS{"UNKNOWN"}; +}; +alarm( $timeout); + if( !$status || $help ) { usage(); } @@ -350,6 +349,7 @@ sub checkPower my $t_desc = $response->{$snmpPowerDesc . '.' . $t_index}; my $t_state = $PSU_STATES{$response->{$snmpPowerState . '.' . $t_index}}; + if ( $ignorepsunotpresent && ($t_desc =~ /not present/) ) { $t_state = "N/A"; } print( "PSU: $t_desc - $t_state\n" ) if $verbose; $psudata = "PSUs:" if( !defined( $psudata ) ); @@ -357,7 +357,7 @@ sub checkPower if( $t_state =~ m/FAILURE/i ) { &setstate( 'CRITICAL', "PSU state for $t_desc: $t_state" ); - } elsif( $t_state !~ m/^NORMAL$/i ) { + } elsif( $t_state !~ m/^NORMAL|N\/A$/i ) { &setstate( 'WARNING', "PSU state for $t_desc: $t_state" ); } } @@ -516,6 +516,7 @@ sub usage { printf " --hostname The hostname to check\n"; printf " --port The port to query SNMP on (using: $port)\n"; printf " --community The SNMP access community (using: $community)\n\n"; + printf " --timeout Execution timeout in seconds (using: " . $timeout. ")\n\n"; printf " --skip-mem Skip memory checks\n"; printf " --memwarn Percentage of memory usage for warning (using: " . $memwarn . ")\n"; printf " --memcrit Percentage of memory usage for critical (using: " . $memcrit . ")\n\n"; From f79c79935ba71a4f4434ea38e1a6ac5df0410895 Mon Sep 17 00:00:00 2001 From: Stanislav German-Evtushenko Date: Thu, 5 Jun 2014 15:56:44 +0400 Subject: [PATCH 4/7] check_chassis_brocade.pl: add --perf option Add --perf option to produce performance data output Output example: OK - CPU: 1min 1% 5sec 1% 1sec 1%. Memory OK (6%). Temp (A/W/C): 48.5/85.0/90.0; Fans: OK OK. PSUs: OK N/A. Up 25.91 days|CPU_1min=1%;70;90 CPU_5sec=1%;85;95 CPU_1sec=1%;95;98 Memory=6%;70;90 Temp=48.5deg;85.0;90.0 --- check_chassis_brocade.pl | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/check_chassis_brocade.pl b/check_chassis_brocade.pl index 3dae94b..5f03fef 100755 --- a/check_chassis_brocade.pl +++ b/check_chassis_brocade.pl @@ -54,6 +54,7 @@ my $key; my $community = "public"; my $port = 161; +my $perf = 0; my $hostname = undef; my $session; @@ -102,6 +103,10 @@ my $cpudata = undef; my $memdata = undef; +my $tempdataperf = undef; +my $cpudataperf = undef; +my $memdataperf = undef; + my $lastcheck = undef; my $skipothers = 0; @@ -130,6 +135,7 @@ "thres-cpu-1min=s", \$threscpuarg{'1min'}, "ignore-psu-notpresent", \$ignorepsunotpresent, "timeout=i", \$timeout, + "perf", \$perf, "help|?", \$help, "verbose", \$verbose, "reboot=i", \$rebootWindow @@ -206,6 +212,12 @@ print $fandata if( defined( $fandata ) && !$skipfans ); print $psudata if( defined( $psudata ) && !$skippsu ); printf( "Up %0.2f days", $uptime ) if( !$skipreboot ); + if ($perf) { + print "|"; + print $cpudataperf if( !$skipcpuall && defined( $cpudataperf ) ); + print $memdataperf if( !$skipmem && defined( $memdataperf ) ); + print $tempdataperf if( !$skiptemp && defined( $tempdataperf ) ); + } print( "\n" ); } else @@ -251,6 +263,7 @@ sub checkTemperature print( "Temp #$t_index: $t_actual (Warn: $t_warning Shutdown: $t_shutdown)\n" ) if $verbose; $tempdata = "Temp (A/W/C): " if( !defined( $tempdata ) ); $tempdata .= sprintf( "%0.1f/%0.1f/%0.1f; ", $t_actual, $t_warning, $t_shutdown ); + $tempdataperf .= sprintf( "Temp=%0.1fdeg;%0.1f;%0.1f ", $t_actual, $t_warning, $t_shutdown ); if( $t_actual >= $t_warning ) { &setstate( 'CRITICAL', "Temperature approaching SHUTDOWN threshold: $t_actual/$t_shutdown" ); @@ -382,6 +395,8 @@ sub checkMemory printf( "Memory: %d%% used\n", $memused ) if $verbose; + $memdataperf = sprintf( "Memory=%d%%;%d;%d ", $memused, $memwarn, $memcrit ); + if( $memused >= $memcrit ) { &setstate( 'CRITICAL', sprintf( "Memory Usage at %d%%", $memused ) ); } elsif( $memused >= $memwarn ) { @@ -495,6 +510,7 @@ sub checkCPU exit $ERRORS{"UNKNOWN"}; } } + $cpudataperf .= "CPU_$t_time=$util%" . ";" . $threscpu{$t_time.'w'}. ";" . $threscpu{$t_time.'c'} . " "; if( $util >= $threscpu{$t_time . 'c'} ) { &setstate( 'CRITICAL', "$t_time CPU Usage $util%" ); @@ -516,7 +532,8 @@ sub usage { printf " --hostname The hostname to check\n"; printf " --port The port to query SNMP on (using: $port)\n"; printf " --community The SNMP access community (using: $community)\n\n"; - printf " --timeout Execution timeout in seconds (using: " . $timeout. ")\n\n"; + printf " --timeout Execution timeout in seconds (using: " . $timeout. ")\n"; + printf " --perf Produce performance data output\n\n"; printf " --skip-mem Skip memory checks\n"; printf " --memwarn Percentage of memory usage for warning (using: " . $memwarn . ")\n"; printf " --memcrit Percentage of memory usage for critical (using: " . $memcrit . ")\n\n"; From e911abd50b29bd8e9e98cc1042e79ba72f24dec8 Mon Sep 17 00:00:00 2001 From: Stanislav German-Evtushenko Date: Thu, 5 Jun 2014 16:11:35 +0400 Subject: [PATCH 5/7] check_chassis_brocade.pl: small perf fix Produce performance data for all statuses and not only for OK --- check_chassis_brocade.pl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/check_chassis_brocade.pl b/check_chassis_brocade.pl index 5f03fef..61b61e6 100755 --- a/check_chassis_brocade.pl +++ b/check_chassis_brocade.pl @@ -212,19 +212,21 @@ print $fandata if( defined( $fandata ) && !$skipfans ); print $psudata if( defined( $psudata ) && !$skippsu ); printf( "Up %0.2f days", $uptime ) if( !$skipreboot ); - if ($perf) { - print "|"; - print $cpudataperf if( !$skipcpuall && defined( $cpudataperf ) ); - print $memdataperf if( !$skipmem && defined( $memdataperf ) ); - print $tempdataperf if( !$skiptemp && defined( $tempdataperf ) ); - } - print( "\n" ); } else { - print "$answer\n"; + print "$answer"; +} + +if ($perf) { + print "|"; + print $cpudataperf if( !$skipcpuall && defined( $cpudataperf ) ); + print $memdataperf if( !$skipmem && defined( $memdataperf ) ); + print $tempdataperf if( !$skiptemp && defined( $tempdataperf ) ); } +print( "\n" ); + exit $ERRORS{$state}; From 20e4859401ff2662542ed2721554cf18b27286a3 Mon Sep 17 00:00:00 2001 From: Barry O'Donovan Date: Sat, 12 Jul 2014 17:40:59 +0100 Subject: [PATCH 6/7] Updated to latest version --- OSS_SNMP | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OSS_SNMP b/OSS_SNMP index 5579335..1b60b73 160000 --- a/OSS_SNMP +++ b/OSS_SNMP @@ -1 +1 @@ -Subproject commit 5579335780a4606edb8045e4506697f32190eb01 +Subproject commit 1b60b73d11ddbef77f64256b73aab480799e0585 From 366e6cd47100fed48db7524ac4595bac455481f8 Mon Sep 17 00:00:00 2001 From: Barry O'Donovan Date: Sat, 12 Jul 2014 17:41:37 +0100 Subject: [PATCH 7/7] Plugin to check for incrementing error counters on ports Requires (and is hardcoded to use) memcached on localhost:11211 as a cache. --- check_port_errors.php | 387 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 387 insertions(+) create mode 100755 check_port_errors.php diff --git a/check_port_errors.php b/check_port_errors.php new file mode 100755 index 0000000..61e6a8d --- /dev/null +++ b/check_port_errors.php @@ -0,0 +1,387 @@ +#! /usr/bin/php + + * @author The Skilled Team of PHP Developers at Open Solutions + */ + +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 +]; + + +// parse the command line arguments +parseArguments(); + + +// create a memcache key: +$MCKEY = 'NAGIOS_CHECK_PORT_ERRORS_' . md5( $cmdargs['host'] ); + +if( !class_exists( 'Memcache' ) ) + die( "ERROR: php5-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'] ); + +// 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 ); + + +// 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; + + + 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 << -p -h [-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 << -p -h [-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 + + +END_USAGE; + +}