APC UPS run time remaining


It’s useful to plot (or alert on) the amount of battery left on your APC UPS. Here’s a script that uses SNMP and parses the string output to work out how many minutes remain on the battery.

This one is a performance monitor (since it sets the “context.SetValue”) but you should easily be able to tweak it as an active monitor.

' Contains run time statistics for APC UPS
runtimeTable =  "1.3.6.1.4.1.318.1.1.1.2.2.3.0"

'Initialize SNMP
set objSNMPReq = CreateObject("CoreAsp.SnmpRqst")
strDeviceID = Context.GetProperty("DeviceID")
set objSNMPInit = objSNMPReq.Initialize(strDeviceID)

if objSNMPInit.Failed then
              	Context.logmessage "Failed to initialize SNMP (" & objSNMPInit.GetErrorMsg & ")"
else
	strRunTimeRemaining=get_SNMPget(runtimeTable)
	'Result has the form "0days 00:43:00.00"
	floatRunTimeRemaining=getHoursfromTimeticks(strRunTimeRemaining)

	context.logmessage "Runtime = " & floatRunTimeRemaining
	Context.SetValue floatRunTimeRemaining
end if

function getHoursfromTimeticks(strtmpTimeticks)
	intReturn=0	
	intDays=left(strtmpTimeticks,instr(strtmpTimeticks,"day")-1)
	arrtmpTime=split(right(strtmpTimeTicks,(len(strtmpTimeTicks) - instr(strtmpTimeTicks,CHR(32)))),":")
	intHours=int(arrtmpTime(0))
	intMinutes=int(arrtmpTime(1))
	intSeconds=int(arrtmpTime(2))
	context.logmessage "Days=" & intDays & ",hours=" & intHours & ",mins=" & intMinutes & ",Secs=" & intSeconds

	floatRunTimeinMinutes=(intDays*24*60)+(intHours*60)+(intMinutes)
	floatReturn=floatRunTimeinMinutes
	getHoursfromTimeticks=floatReturn
end function

function get_SNMPget(strtmpOID)
	set objtmpSNMPResult = objSNMPReq.Get(strtmpOID)
	strtmpValue=objtmpSNMPResult.GetPayload
	context.logmessage strtmpOID & " returned value " & strtmpValue
	get_SNMPget=strtmpValue
end function
Advertisements
Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: