Check-power-json.sh
Jump to navigation
Jump to search
check-power-json.sh is a bash script that interogates the APC HS500 UPS and returns it's status as a JSON object. This is primarily used for the Homebridge APC Back-UPS HS500 Plugin and also called by apc500.sh command line configuration utility to confirm status.
Example Use
$ ./check-power-json.sh 192.168.1.10
{
"upsstatus":"On Line",
"load":74,
"batterylevel":100,
"batterystatus":"Charged",
"runtime":26,
"lasttest":"Passed",
"lasttransfer":"Blackout"
}
Code
#!/bin/bash
# This script uses apc.sh to check the status of BackUPS HS-500
# And outputs the status as an json record
STATUS="/tmp/apc-500-status.tmp"
UPS=$1
# get status values from the primitive web UI
# logger "Getting UPS Status from $UPS"
curl -sl "http://$UPS/status.cgi" | tr -dc '[:print:]\n' > $STATUS
# Extract the unit operating status fields - battery level etc
LOAD="$(cat $STATUS | grep -o '[0-9]* Watts' | grep -o '[0-9]*')"
BATTERYLEVEL="$(cat $STATUS | grep -o '[0-9]* %' | grep -o '[0-9]*')"
RUNTIME="$(cat $STATUS | grep -o '[0-9]* minutes' | grep -o '[0-9]*')"
BATTERYSTATUS="$(cat $STATUS | egrep -o 'Charged|Charging|Discharged|Discharging')"
UPSSTATUS="$(cat $STATUS | egrep -o 'On Line|On Battery' | sed 's/ / /g')"
LASTTEST="$(cat $STATUS | egrep -o 'Result of last self-test is:.*(Passed|Failed)</font>' | egrep -o '(Passed|Failed)')"
LASTTRANSFER="$(cat $STATUS | egrep -o 'No Transfer|Blackout' | sed 's/ / /g')"
# log process
# logger "UPS Status $UPSSTATUS, $RUNTIME minutes remaining (load: $LOAD Watts)"
# Generate json
echo '{'
echo ' "upsstatus":"'$UPSSTATUS'",'
echo ' "load":'$LOAD','
echo ' "batterylevel":'$BATTERYLEVEL','
echo ' "batterystatus":"'$BATTERYSTATUS'",'
echo ' "runtime":'$RUNTIME','
echo ' "lasttest":"'$LASTTEST'",'
echo ' "lasttransfer":"'$LASTTRANSFER'"'
echo '}'
# garbage collector
rm -f $STATUS