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: .1.3.6.1.4.1.318.1.1.12.3.3.1.1.4., where is the power port you wish to control.

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

In config.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// translates APC IDs into their IP addresses
$apcids = array(
  "APC01" => "10.0.1.2",
  "APC02" => "10.0.1.6",
  "APC03" => "10.0.1.10",
  "APC04" => "10.0.1.14",
  "APC05" => "10.0.1.18",
  "APC06" => "10.0.1.22",
);

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

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

In functions.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// 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) {
  include('config.php');
  $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";
  default:
    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) {
  include('config.php');
  $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.

  • Thanks for the info! I managed to implement the same thing in ruby, its much faster and reliable than the telnet implementation I had.
    For the other readers:
    http://support.ipmonitor.com/mibs/POWERNET-MIB/oids.aspx
    here’s the full APC mib including the rPDU branch for controlling the family 70 Rack PDUs.

  • Thanks for the info! I managed to implement the same thing in ruby, its much faster and reliable than the telnet implementation I had.
    For the other readers:
    http://support.ipmonitor.com/mibs/POWERNET-MIB/oids.aspx
    here’s the full APC mib including the rPDU branch for controlling the family 70 Rack PDUs.

  • Graham

    Wow, beautiful. I was trying to write a system utilizing PHP w/ the SSH2 PECL package, but the APC SSH system was just so incredibly slow, and the fact that I could not for the life of me get the SSH2 functions to work with it that I had to look for a SNMP solution.

    The code examples you have above allowed me to write a web interface which unifies fifteen individual PDU’s into one system.

    Thank you!

  • Graham

    Wow, beautiful. I was trying to write a system utilizing PHP w/ the SSH2 PECL package, but the APC SSH system was just so incredibly slow, and the fact that I could not for the life of me get the SSH2 functions to work with it that I had to look for a SNMP solution.

    The code examples you have above allowed me to write a web interface which unifies fifteen individual PDU’s into one system.

    Thank you!