#!/usr/bin/php * Licensed under the GPL. * * 2009-06-19 Initial revision. */ $p = @popen("/sbin/apcaccess 2>&1", "r"); if(!$p) { echo "UPS CRITICAL - Unable to execute the /sbin/apcaccess utility. Did you install and configure apcupsd?"; exit(2); } // It reads the status into a keyed array or it gets the hose again. $status = array(); while(!feof($p)) { $a = fgetcsv($p, 1024, ': '); if( trim($a) ) $status[ trim($a[0]) ] = trim($a[1]); } pclose($p); // I got these via `strings apcupsd'. I hope they match, as my power and UPS have never failed // so I don't *actually* know what apcupsd would output :-/ switch($status['STATUS']) { case 'ONLINE': case 'SLAVE': $ret = 0; break; case 'ONBATT': case 'COMMLOST': $ret = 1; break; case 'OVERLOAD': case 'LOWBATT': case 'REPLACEBATT': case 'NOBATT': case 'SLAVEDOWN': case 'SHUTTING DOWN': default: $ret = 2; break; } // Parse the run-time remaining. $time = split(' ', $status['TIMELEFT']); $time = floatval($time[0]); // If we have less than ten minutes of run-time on battery, also go critical. if($time < 10) { $ret = 2; } // Assemble a possibly useful status line. $info = sprintf("UPS %s - Line: %s; %s; Charge %s; Temp: %s\n", $status['STATUS'], $status['LINEV'], $status['LOADPCT'], $status['BCHARGE'], $status['ITEMP'] ); // If there was an execution issue.... re-join the first rety in $status // Bit of a kludge and it drops a colon, but it works well enough. // Oh, and go critical. if( count($status) == 1 ) { $ret = 2; foreach($status as $k => $v) $error .= sprintf("%s %s", $k, $v); $info = sprintf("UPS ERROR - %s\n", $error); } // Print status line and exit with correct return value. echo $info; exit($ret);