PHP Script for controlling APC PDU via SNMP

This one is neat – you can control those APC PDU Power Poles over the network via SNMP. This works for the APC Switched Rack Unit PDU, but it may work for others.

I use this on a PDU with Network Management Card AOS version v2.6.4 and Rack PDU APP v2.6.5, so of course this is not guaranteed for any other versions.

First, set up the PDU on the network. Then, you need to configure the SNMP write string. You do this by navigating the menu (default username password is apc/apc):
Control Console->Network->SNMP

Change one of the strings to Write+ and preferable change it from public/private to your own string. You can also limit access from certain hosts for extra security. I always keep the APC PDUs on a private network since the APC telnet/web interface is so insecure.

Once you have that set up, your PDU is read to accept SNMP commands from your script of choice. I use PHP, so my examples use PHP code.

The APC MIB which controls the status of the power ports is: ., where is the power port you wish to control.

I created a function that controls the port, it is as follows:

In config.php:

// translates APC IDs into their IP addresses
$apcids = array(
"APC01" => "",
"APC02" => "",
"APC03" => "",
"APC04" => "",
"APC05" => "",
"APC06" => "",

// This APC MIB is incomplete - must add the port number of the PDU at the end
$apcportcontrolmib = ".";

// This is your SNMP Write+ String
$apcsnmp = "rwstring";

In functions.php:
// string function displayPowerStatus
// $apcid = ID of the APC unit as defined in config.php
// $apcport = Port of the outlet on the APC Switch
// returns STRING On, Off, Rebooting, or Error
function displayPowerStatus($apcid, $apcport) {
$ip = $apcids[$apcid];
$mib = $apcportcontrolmib.$apcport;
$a = trim(snmpget($ip, $apcsnmp, $mib, 20000), "INTEGER: ");
switch ($a) {
case 1:
return "On";
case 2:
return "Off";
case 3:
return "Rebooting";
return "Error";

// bool function manageAPCPort
// $apcid = ID of the APC unit as defined in config.php
// $apcport = Port of the outlet on the APC Switch
// $apcpass = APC Pass (SNMP Community String)
// $action {
// immediateOn (1),
// immediateOff (2),
// immediateReboot (3),
// delayedOn (4),
// delayedOff (5),
// delayedReboot (6),
// cancelPendingCommand (7)
// }
// returns TRUE or FALSE
function manageAPCPort($apcid, $apcport, $apcpass, $action) {
$ip = $apcids[$apcid];
$mib = $apcportcontrolmib.$apcport;
$VERIFY = snmpset($ip, $apcpass, $mib, i, $action);
return $VERIFY;

Note that this code required the php snmpset function, which is not compiled into php by default. If you receive some error about snmpset or snmpget function not found, this is why.

Dave Drager

Dave Drager


Sign in or become a free systemBash member to read and leave comments.
Just enter your email below to get an easy log in link.