SNMP-apcupsd

From Mike Neir's Wiki

Jump to: navigation, search

Premise

This script parses the output of the apcaccess command found in the apcupsd package, which provides an interface to serial or USB compatible APC UPSs. This script runs in concert with net-snmp to provide SNMP values for the following statistics (if they're provided by the UPS)...

  • Battery Charge Percentage
  • Battery Time Remaining
  • Load Percentage
  • Low Transfer Voltage
  • Line Voltage
  • High Transfer Voltage
  • Battery Voltage
  • Nominal Battery Voltage

Implementation

The following script does the heavy lifting...

#!/usr/bin/php -q
<?
$OUTPUT=`/usr/sbin/apcaccess`;

$regex = "/^(.+?)\s*:(.*?)$/m";

$matchcount = preg_match_all($regex, $OUTPUT, $matches);

for ($x = 0; $x < count($matches[1]); $x++)
{       
       $$matches[1][$x] = (float)trim($matches[2][$x]);
}

echo "$BCHARGE\n";
echo "$TIMELEFT\n";
echo "$LOADPCT\n";
echo "$LOTRANS\n";
echo "$LINEV\n";
echo "$HITRANS\n";
echo "$BATTV\n";
echo "$NOMBATTV\n";
?>

The following line should be placed in the snmpd.conf file to facilitate monitoring...

exec .1.3.6.1.4.1.2021.60 APCUPSD /root/bin/digestapcupsd

The MIB number can be changed somewhat, but I'm not actually sure how much. This is just what I'm using.

Results

If all works correctly, doing a snmpwalk to the specificed MIB should yield something similar to the following:

UCD-SNMP-MIB::ucdavis.60.1.1 = INTEGER: 1
UCD-SNMP-MIB::ucdavis.60.2.1 = STRING: "APCUPSD"
UCD-SNMP-MIB::ucdavis.60.3.1 = STRING: "/root/bin/digestapcupsd"
UCD-SNMP-MIB::ucdavis.60.100.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.60.101.1 = STRING: "100"
UCD-SNMP-MIB::ucdavis.60.101.2 = STRING: "41"
UCD-SNMP-MIB::ucdavis.60.101.3 = STRING: "22"
UCD-SNMP-MIB::ucdavis.60.101.4 = STRING: "97"
UCD-SNMP-MIB::ucdavis.60.101.5 = STRING: "119"
UCD-SNMP-MIB::ucdavis.60.101.6 = STRING: "132"
UCD-SNMP-MIB::ucdavis.60.101.7 = STRING: "26.9"
UCD-SNMP-MIB::ucdavis.60.101.8 = STRING: "24"
UCD-SNMP-MIB::ucdavis.60.102.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.60.103.1 = ""
Personal tools