CyberPower RMCARD205 SNMP UPS Integration Bash Script

All kinds of 'OS' scripts
Post Reply
ben53252642
Posts: 524
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

CyberPower RMCARD205 SNMP UPS Integration Bash Script

Post by ben53252642 » Monday 29 January 2018 22:36

This is a bash script to get data from a CyberPower RMCARD205 network card via SNMP and load it into Domoticz.

1) Login to the RMCARD205 web interface and enable SNMPv1: "System" > "Network Service" > "SNMPv1 Service".

2) Change settings as needed in the script below and install the requirements (mentioned in the script).

Code: Select all

#!/bin/bash
while true; do

# Requirements: apt-get install snmp bc

# UPS Configuration:
upsipaddress="192.168.0.117"

# Domoticz Configuration
domoticzserver="192.168.0.5"
domoticzport="443"
domoticzuser="USERNAME"
domoticzpass="PASSWORD"

# Get data from the UPS via SNMP
data=$(snmpwalk -v1 -c public ${upsipaddress} iso.3.6.1.4.1.3808)

upsmodel=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.1.1.1.0' | awk '{ print $4 }' | sed 's/["]*//g')
inputvoltage=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.3.2.2.0' | awk '{ print $4 }' | cut -c1-3)
outputvoltage=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.4.2.1.0' | awk '{ print $4 }' | cut -c1-3)
batterycapacity=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.2.2.1.0' | awk '{ print $4 }')
powerratingva=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.1.2.6.0' | awk '{ print $4 }')
powerratingwatts=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.1.2.7.0' | awk '{ print $4 }')
lastdiagnosticresult=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.7.2.3.0' | awk '{ print $4 }')
lastdiagnosticresultdate=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.7.2.4.0' | awk '{ print $4 }' | sed 's/["]*//g')
if [ "$lastdiagnosticresult" == "2" ]; then
lastdiagnosticresultmsg="Self test failed"
elif [ "$lastdiagnosticresult" == "3" ]; then
lastdiagnosticresultmsg="Self test failed"
elif [ "$lastdiagnosticresult" == "4" ]; then
lastdiagnosticresultmsg="Self test in progress"
else
lastdiagnosticresultmsg="OK"
fi
batteryreplacement=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.2.2.5.0' | awk '{ print $4 }')
if [ "$batteryreplacement" == "1" ]; then
batteryreplacementmsg="Not needed"
elif [ "$batteryreplacement" == "2" ]; then
batteryreplacementmsg="Needs replacement"
else
batteryreplacementmsg="Unknown"
fi
status=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.4.1.1.0' | awk '{ print $4 }')
if [ "$status" == "2" ]; then
status="Running on AC"
elif [ "$status" == "3" ]; then
status="Running on battery"
elif [ "$status" == "4" ]; then
status="Boosting utility power"
elif [ "$status" == "5" ]; then
status="Sleeping"
elif [ "$status" == "6" ]; then
status="Off"
elif [ "$status" == "7" ]; then
status="Rebooting"
else
status="Unknown"
fi
load=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.4.2.3.0' | awk '{ print $4 }')
estimatedloadwatts=$(echo $(("$powerratingwatts" / 100 * "$load")))
# Remaining minutes
remainingminutesmath1=$(echo "$data" | grep 'iso.3.6.1.4.1.3808.1.1.1.2.2.4.0' | awk '{ print $4 }' | sed 's/[^0-9]*//g')
remainingminutes=$(echo $(("$remainingminutesmath1" / 100 / 60)) | bc)

echo "UPS Model: $upsmodel"
echo "Power Rating: $powerratingva va or $powerratingwatts watts"
echo "Status: $status"
echo "Input Voltage:" "$inputvoltage"
echo "Output Voltage:" "$outputvoltage"
echo "Battery Capacity %:" "$batterycapacity"
echo "Remaining Runtime Minutes: $remainingminutes"
echo "Battery Replacement: $batteryreplacementmsg"
echo "Load %: $load"
echo "Estimated load in watts: $estimatedloadwatts"
echo "Last self test result: $lastdiagnosticresultmsg"
echo "Last self test date: $lastdiagnosticresultdate"

# Send data to Domoticz
curl --max-time 60 -k -s "https://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=1126&nvalue=0&svalue={$estimatedloadwatts}"
curl --max-time 60 -k -s "https://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=1128&nvalue=0&svalue={$inputvoltage}"
curl --max-time 60 -k -s "https://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=1129&nvalue=0&svalue={$remainingminutes}&battery={$batterycapacity}"

sleep 10
done
Example output from the script:

root@domoticz:~/scripts/ups# ./ups.sh
UPS Model: OR600ELCDRM1U
Power Rating: 600 va or 360 watts
Status: Running on AC
Input Voltage: 243
Output Voltage: 243
Battery Capacity %: 100
Remaining Runtime Minutes: 12
Battery Replacement: Not needed
Load %: 57
Estimated load in watts: 171
Last self test result: OK
Last self test date: 01/30/2018

ups.JPG
ups.JPG (90.78 KiB) Viewed 856 times

Notes:
1) Some status / diagnostic messages may be incorrect and thus should not be relied upon, the messages in the script are a combination of explanations I have found online for various status codes and personal testing. That said I am not currently aware of any incorrect messages, if you discover any problems please report them.
2) The curl commands in the script are set to allow unsigned SSL certificates
3) You will need to understand and customize the script for use in your own environment including understanding any potential security / operational implications.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest