APC PDU Power control via SNMP

Below are a couple of simple examples on how you can use PHP/SNMP to control the power outlets on APC PDU devices , AP7951 or the lower priced model here

Note : in the example below you need to replace "readcommunity" and "writecommunity" with your SNMP community strings that you set on the APC

function apcGetStatus($APCip, $APCport) {  
  $APCresponse = snmpget($APCip, "readcommunity", ".1.3.6.1.4.1.318.1.1.4.4.2.1.3.$APCport");
  $APCresponse = str_replace('INTEGER: ', '', $APCresponse);
  switch ($APCresponse) {
    case '1':
      $returnVal = 'On';
      break;
    case '2':
      $returnVal = 'Off';
      break;
    case '3':
      $returnVal = 'Reboot';
      break;
    case '4':
      $returnVal = 'Unknown';
      break;
    case '5':
      $returnVal = 'On with delay';
      break;
    case '6':
      $returnVal = 'Off with delay';
      break;
    case '7':
      $returnVal = 'Reboot with delay';
      break;
    default:
      $returnVal = $APCresponse;
  }
  return $returnVal;
}
function apcSetStatus($APCip, $APCport, $rebootOption) {

  switch ($rebootOption) {
    case '1':
      $actionTo = 'On';
      break;
    case '2':
      $actionTo = 'Off';
      break;
    case '3':
      $actionTo = 'Reboot';
      break;
    case '5':
      $actionTo = 'On with delay';
      break;
    case '6':
      $actionTo = 'Off with delay';
      break;
    case '7':
      $actionTo = 'Reboot with delay';
      break;
    default:
      return 'Invalid reboot option passed';
  }
  
  $actionFrom = apcGetStatus($APCip, $APCport);

  if ( snmpset ( $APCip, "writecommunity", ".1.3.6.1.4.1.318.1.1.4.4.2.1.3.$APCport", 'i', $rebootOption) )
  {
    $returnVal = "Actioned:$actionFrom to $actionTo";
  }
  else
  {
    $returnVal = "Failed to Action:$actionFrom to $actionTo";
  }
  return $returnVal;
}

Post a comment