class ApcSnmp

Constants

MIBDIR

Public Class Methods

new(host, community='public', write_community='private') click to toggle source
# File lib/apc.rb, line 14
def initialize (host, community='public', write_community='private')
  @mib = SNMP::MIB.new
  @mib.load_module("PowerNet-MIB", MIBDIR)
  @mib.load_module("RFC1213-MIB", MIBDIR)
  @manager = SNMP::Manager.new( :host => host,
                                :version => :SNMPv1,
                                :community => community,
                                :write_community => write_community,
                                :mib_dir => MIBDIR,
                                :mib_modules => [ "PowerNet-MIB", "RFC1213-MIB" ])
end

Public Instance Methods

getFirmwareVersion() click to toggle source

Get the firmware revision of the PDU

# File lib/apc.rb, line 116
def getFirmwareVersion ()
  return readValue("sPDUIdentFirmwareRev.0")
end
getLoadAmps() click to toggle source

Get total Amp load on PDU

# File lib/apc.rb, line 121
def getLoadAmps ()
  return readValue("rPDULoadStatusLoad.1")
end
getLoadDetail(phase) click to toggle source

Get the load on a phase return load, max load and warning load

# File lib/apc.rb, line 136
def getLoadDetail(phase)
  load = Hash.new
  loaddata = readValue(["rPDULoadStatusLoad.#{phase}",
                        "rPDULoadPhaseConfigOverloadThreshold.#{phase}",
                        "rPDULoadPhaseConfigNearOverloadThreshold.#{phase}"])
  load['used'] = loaddata[0].to_i/10 # Is reported as 2.9amps being 29
  load['max'] = loaddata[1]
  load['warn'] = loaddata[2]
  return load
end
getLocation() click to toggle source

 Get the location of the PDU

# File lib/apc.rb, line 131
def getLocation ()
  return readValue("RFC1213-MIB::sysLocation.0")
end
getModelNumber() click to toggle source

Get the model number of a PDU

# File lib/apc.rb, line 111
def getModelNumber ()
  return readValue("rPDUIdentModelNumber.0")
end
getNumPhases() click to toggle source

Get number of phases on this PDU

# File lib/apc.rb, line 126
def getNumPhases ()
  return readValue("rPDULoadDevNumPhases.0")
end
getOutletName(outlet=0) click to toggle source

Get the name of a particular outlet or of all outlets if not specified

# File lib/apc.rb, line 73
def getOutletName (outlet=0)
  if outlet == 0
    outletList = Array.new
    outlets = numOutlets()
    for i in 1..outlets do
      outletList.push("sPDUOutletName.#{i}")
    end
    return readValue(outletList)
  else
    begin
      return readValue("sPDUOutletName.#{outlet}")
    rescue NullValueException
      raise NonExistentPortException
    end
  end
end
getOutletStatus(outlet=0) click to toggle source

 Get the status of a particular outlet or of all outlets if not specified

# File lib/apc.rb, line 55
def getOutletStatus (outlet=0)
  if outlet == 0
    outletList = Array.new
    outlets = numOutlets()
    for i in 1..outlets do
      outletList.push("sPDUOutletCtl.#{i}")
    end
    return readValue(outletList)
  else
    begin
      return readValue("sPDUOutletCtl.#{outlet}")
    rescue NullValueException
      raise NonExistentPortException
    end
  end
end
getPDUName() click to toggle source

Get the name of the PDU

# File lib/apc.rb, line 96
def getPDUName ()
  return readValue("rPDUIdentName.0")
end
getSerialNumber() click to toggle source

Get the serial number of a PDU

# File lib/apc.rb, line 106
def getSerialNumber ()
  return readValue("rPDUIdentSerialNumber.0")
end
numOutlets() click to toggle source

 Get the number of outlets from rPDUOutletDevNumCntrlOutlets

# File lib/apc.rb, line 50
def numOutlets ()
  return readValue("rPDUOutletDevNumCntrlOutlets.0").to_i
end
outletOff(outlet) click to toggle source

 Turn an outlet off

# File lib/apc.rb, line 148
def outletOff (outlet)
  return writeValue("sPDUOutletCtl.#{outlet}", SNMP::Integer.new(2))
end
outletOn(outlet) click to toggle source

Turn an outlet on

# File lib/apc.rb, line 153
def outletOn (outlet)
  puts "Sending 'On' to sPDUOutletCtl.#{outlet}"
  return writeValue("sPDUOutletCtl.#{outlet}", SNMP::Integer.new(1))
end
readValue(oid) click to toggle source

Read a single OID or an array of OIDs over SNMP

# File lib/apc.rb, line 27
def readValue (oid)
  response = @manager.get(oid)
  data = Array.new
  response.each_varbind do |varbind|
    data.push(varbind.value.to_s)
  end
  if data.length == 1
    raise NullValueException if data[0] == 'Null'      
    return data[0]
  else
    return data
  end
end
setOutletName(outlet, name) click to toggle source

 Set the name of an outlet

# File lib/apc.rb, line 91
def setOutletName (outlet, name)
  writeValue("sPDUOutletName.#{outlet}", SNMP::OctetString.new(name))
end
setPDUName(name) click to toggle source

 Change the name of a PDU

# File lib/apc.rb, line 101
def setPDUName (name)
  return writeValue("rPDUIdentName.0", SNMP::OctetString.new(name))
end
writeValue(name, value) click to toggle source

Write a single OID with a new value

# File lib/apc.rb, line 42
def writeValue (name, value)
  oid = @mib.oid(name)
  varbind = SNMP::VarBind.new(oid, value)
  puts varbind
  return @manager.set(varbind)
end