#!/usr/bin/php = 60) { $hours = floor($seconds / 3600); if ($hours >= 24) { $days = floor($hours / 24); $hours = $hours % 24; } $minutes = ($minutes % 60) % 60; $formatedUptime = $hours . "h " . $minutes . "min " . $secs . "sec"; if ($days) { $formatedUptime = $days ."d ".$formatedUptime; } } else if ($minutes >= 1) { $formatedUptime = $minutes . "min " . $secs . "sec"; } else { $formatedUptime = $secs . "sec"; } return $formatedUptime; } function help () { echo "This Plugin was developed by Oliver Skibbe - oliskibbe (at) gmail.com !required packages: php-cli, php5-snmp! Usage: ".$_SERVER['argv'][0]." ip community command ip - ip of beronet media gateway community - snmp community of beronet media gateway command: errors - checks if there are physical layer error - sends perfdata portstate - checks portstate of modules (UP, DOWN) calls - displays total calls and average connection - sends perfdata current - displays current calls - sends perfdata load - displays load (cpu,memory): crit if over 80% - sends perfdata info - always ok. prints version (FPGA,Firmware), revision and serial of beronet media gateway uptime - always ok. prints uptime of beronet media gateway "; } if(!extension_loaded("snmp")) { if (!dl("snmp.so") { echo "PHP SNMP extension could not be loaded...please check if it's installed!"; help(); exit ( 3 ); } } snmp_set_quick_print ( true ); snmp_set_valueretrieval(SNMP_VALUE_PLAIN); if ( $_SERVER['argc'] < 4 ) { help(); exit ( 3 ); } $ip = $_SERVER['argv'][1]; $community = $_SERVER['argv'][2]; $command = $_SERVER['argv'][3]; switch ( $command ) { case "errors": $warn = $_SERVER['argv'][4]; $crit = $_SERVER['argv'][5]; $i = 0; $j = 0; $retMsg = ""; $perfMsg = ""; $port = array(); $l1error = @snmp2_walk ( $ip, $community, '.enterprises.29886.0.10.2.1.1.4' ); $l2error = @snmp2_walk ( $ip, $community, '.enterprises.29886.0.10.3.1.1.2' ); if ( $l1error === FALSE || $l2error === FALSE ) { echo "Could not read data!\n"; exit(2); } foreach ($l1error as $l1 ) { $port[$i] = array($l1error[$i], $l2error[$i]); $i++; } foreach ($port as $portErrors) { $perfMsg .= " l1port{$j}={$portErrors[0]} l2port{$j}={$portErrors[1]}"; if ( $portErrors[0] > $crit || $portErrors[1] > $crit ) { $retMsg .= "CRIT Port {$j} ({$portErrors[0]}/{$portErrors[1]}) "; $crit = 1; } else if ( $portErrors[0] > $warn || $portErrors[1] > $warn ) { $retMsg .= "WARN Port {$j} ({$portErrors[0]}/{$portErrors[1]}) "; $warn = 1; } else { $retMsg .= "OK Port {$j} ({$portErrors[0]}/{$portErrors[1]}) "; } $j++; } if ($warn == 1) { $ret = 1; } else if ($crit == 1) { $ret = 2; } else { $ret = 0; } echo $retMsg."|".$perfMsg."\n"; exit ( $ret ); break; case "portstate": $i = 0; $j = 0; $retMsg = ""; $perfMsg = ""; $port = array(); $portState = @snmp2_walk ( $ip, $community, '.enterprises.29886.0.10.4.1.1.4' ); $portProtocol = @snmp2_walk ( $ip, $community, '.enterprises.29886.0.10.4.1.1.2' ); $portTerm = @snmp2_walk ( $ip, $community, '.enterprises.29886.0.10.4.1.1.2' ); if ( $portState === FALSE ) { echo "Could not read data!\n"; exit(2); } foreach ($portState as $porttmp ) { $port[$i] = array($portState[$i],$portProtocol[$i],$portTerm[$i]); $i++; } foreach ($port as $portState) { if ( $portState[0] != "UP" && ( $portState[1] == "PTP" || ( $portState[1] == "PMP" && $portState[2] == "NT" ) ) ) { $retMsg .= "CRIT Port {$j} ({$portState[1]}/{$portState[2]} IS DOWN "; $crit = 1; } $j++; } if ($crit == 1) { $ret = 2; } else { $retMsg .= "OK All port(s) ({$j}) are UP "; $ret = 0; } echo $retMsg."\n"; exit ( $ret ); break; case "calls": $callsCompleted = @snmp2_walk ( $ip, $community, '.enterprises.29886.0.10.1.1.1.3' ); $averageCallTime = @snmp2_walk ( $ip, $community, '.enterprises.29886.0.10.1.1.1.4' ); if ( $callsCompleted === FALSE || $averageCallTime === FALSE ) { echo "Could not read data!\n"; exit(2); } $totalCalls = 0; $averageTime = array(0,0,0); foreach ( $callsCompleted as $calls ) { $totalCalls += $calls; } $i = 0; foreach ( $averageCallTime as $avg ) { $avg = split(":",$avg); $averageTime[0] += $avg[0]; $averageTime[1] += $avg[1]; $averageTime[2] += $avg[2]; $i++; } $hours = $averageTime[0] / $i; $minutes = ($averageTime[1] / $i) % 60; $seconds = ($averageTime[2] / $i) % 60; $retMsg = "There are {$totalCalls} completed calls with an average connection time of"; if ($hours > 0 ) { $retMsg .= " {$hours} h"; } if ($minutes > 0 ) { $retMsg .= " {$minutes} min"; } if ($seconds > 0 ) { $retMsg .= " {$seconds} secs"; } echo $retMsg."| totalcalls={$totalCalls}\n"; exit ( 0 ); break; case "current": $currentCalls = @snmp2_walk ( $ip, $community, '.enterprises.29886.0.10.1.1.1.2' ); if ( $currentCalls === FALSE ) { echo "Could not read data!\n"; exit(2); } $totalCurrentCalls = 0; foreach ( $currentCalls as $calls ) { $totalCurrentCalls += $calls; } $retMsg = "There are currently {$totalCurrentCalls} calls | currentcalls={$totalCurrentCalls}\n"; echo $retMsg; exit (0); break; case "uptime": $seconds = @snmp2_getnext ( $ip, $community, '.enterprises.29886.0.9.1' ); if ( $seconds === FALSE ) { echo "Could not read data!\n"; exit(2); } $formatedUptime = formattime($seconds); echo "Uptime is: {$formatedUptime}\n"; exit (0); break; case "load": // TODO is load really a percentage value? have to ask beronet $cpu = @snmp2_getnext ( $ip, $community, '.enterprises.29886.0.11.1.1.3'); $memTotal = @snmp2_getnext ( $ip, $community, '.enterprises.29886.0.12.1.1.1'); $memFree = @snmp2_getnext ( $ip, $community, '.enterprises.29886.0.12.1.1.2'); if ( $cpu === FALSE || $memTotal === FALSE || $memFree === FALSE ) { echo "Could not read data!\n"; exit(2); } $memUsage = floor(( $memFree * 100 ) / $memTotal); if ( $memUsage > 80 || $cpu > 80 ) { $retString = "CRITICAL"; $ret = 2; } else { $retString = "OK"; $ret = 0; } // output in megabytes $memFree = floor($memFree / 1024); $memTotal = floor($memTotal / 1024); $perfMsg = "| freemem={$memFree} totalmem={$memTotal} cpu={$cpu}"; $retMsg = $retString . " CPU: {$cpu}% Memory: {$memUsage}% ({$memFree}/{$memTotal})"; echo $retMsg . $perfMsg . "\n"; exit($ret); break; case "version": $serial = @snmp2_getnext ( $ip, $community, '.enterprises.29886.0.2'); $revision = @snmp2_getnext ( $ip, $community, '.enterprises.29886.0.3'); $appfsVersion = @snmp2_getnext ( $ip, $community, '.enterprises.29886.0.5'); $fpgaVersion = @snmp2_getnext ( $ip, $community, '.enterprises.29886.0.6'); if ( $appfsVersion === FALSE || $serial === FALSE || $revision === FALSE || $fpgaVersion === FALSE ) { echo "Could not read data!\n"; exit(2); } echo "Firmware: {$appfsVersion} FPGA: {$fpgaVersion} Revision: {$revision} Serial: {$serial}\n"; exit(0); break; } ?>