Monitor an APC UPS Network Monitoring Card (e.g. AP9617)

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
AintBigAintClever
Posts: 5
Joined: Wednesday 01 November 2017 0:47
Target OS: Linux
Domoticz version:
Contact:

Monitor an APC UPS Network Monitoring Card (e.g. AP9617)

Post by AintBigAintClever »

UPDATED MAY 2018 - NOW SUPPORTS ENVIRONMENT MONITORING

This script monitors an APC Smart UPS using a Network Monitoring Card rather than USB. When a card is fitted the serial interface (and presumably USB as well) is intercepted so you're communicating with the card instead of the UPS itself, resulting in a completely different response; this script will allow access to the card over the LAN instead, using SNMP to retrieve the various statistics.

Prerequisites

In order to use this script you need have the following, which depending on your system may or may not be already installed:

awk - sudo apt-get install gawk
curl - sudo apt-get install curl
sed - sudo apt-get install sed
snmpget - sudo apt-get install snmp

Domoticz setup - needed hardware and devices

Under hardware create a new dummy device named UPS

On this hardware create virtual sensors of the following types:

UPS Battery Level - Percentage
UPS Internal Temperature - Temperature
UPS Battery Voltage - Voltage
UPS Input Voltage - Voltage
UPS Input Max Voltage - Voltage
UPS Input Min Voltage - Voltage
UPS Input Frequency - Custom (Hz)
UPS Output Status - Text
UPS Output Voltage - Voltage
UPS Output Frequency - Custom (Hz)
UPS Output Power - Usage (Electric)
Environment Monitoring Probe Temperature - Temperature
Environment Monitoring Probe Humidity - Percentage
Environment Monitoring Probe Temperature/Humidity - Temp+Humidity
Environment Monitoring Card Relay Status - Temperature

Go to the Devices list and note the IDX (not ID) of each of these sensors.

On your UPS Network Management Card's web interface go to Administration -> Network -> SNMPv1 -> access control and enter the details of your Domoticz server (it won't send SNMP data to a device it doesn't know about), give it "read" access and enable SNMPv1 on the access tab. Log off to save your changes (the card - but not the UPS - will reboot at this point).

The Script

Create a file in ~/domoticz/scripts/ called ap96xx.sh with the following content, but change the IP addresses, passwords and IDX numbers to suit your system. UPS_WATTAGE needs to match your UPS output power in Watts (not VA), if you're not sure what this should be look up your UPS part number on Google and check the Schneider Electric website. Mine for example is an SUA1500I rated at 1500VA, 980 Watts.

Code: Select all

#!/bin/bash

# =========================================================
# Domoticz APC/Schneider AP96xx UPS Management Card script
# Monitors an APC UPS using SNMP commands
# =========================================================
# AintBigAintClever, May 2018
# Derived from nas.sh
# Tested using an AP9617 card running AOS 3.7.3, SUMX 3.7.2
# Tested using an AP9619 card running AOS 3.7.3, SUMX 3.7.2
# Tested using an AP9619 card running AOS 3.9.2, SUMX 3.7.2
# =========================================================
# Ensure the Domoticz server IP is in the card's SNMP settings
# (a read-only community is fine) or the card will ignore the requests.
# Administration -> Network -> SNMPv1 -> access control
 
# Settings
 UPS_IP="xxx.xxx.xxx.xxx"		        # UPS IP Address
 PASSWORD="xxxxx"			# SNMP Password
 DOMO_IP="xxx.xxx.xxx.xxx"		# Domoticz IP Address
 DOMO_PORT="xxxx"			# Domoticz Port
 UPS_WATTAGE="980"			# Multiplier used to calculate output wattage from load percentage

# IDX numbers for your various virtual sensors (check the IDX column in  your Devices list to
# find what these need to be, the required sensor type is in brackets)
 BAT_LEVEL="29"				# Remaining Battery Capacity (percentage)
 BAT_TEMP="27"				# Internal UPS Temperature (temperature)
 BAT_VOLTS="28"				# Battery Voltage (voltage)
 UPS_IN_VOLTS="30"			# Input Voltage (voltage)
 UPS_IN_MAX_VOLTS="1001"		# Input Max Voltage over previous minute (voltage)
 UPS_IN_MIN_VOLTS="1001"		# Input Min Voltage over previous minute (voltage)
 UPS_IN_FREQ="31"			# Input Frequency (custom, Hz)
 UPS_OUT_STATUS="35"			# Output Status (text)
 UPS_OUT_VOLTS="32"			# Output Voltage (voltage)
 UPS_OUT_FREQ="33"			# Output Frequency (custom, Hz)
 UPS_OUT_WATTS="34"			# Output Power (Usage (Electric))
 IEM_TEMP="1001"                    	# Environment Monitoring Probe Temperature (temperature)
 IEM_HUMID="1001"			# Environment Monitoring Probe Humidity (percentage)
 IEM_TEMP_HUMID="36"			# Environment Monitoring Probe Temperature/Humidity (humidity)
 IEM_RELAY="37"				# Environment Monitoring Card Relay Status (switch)
 
 # Check if UPS is contactable 
 
 PINGTIME=`ping -c 1 -q $UPS_IP | awk -F"/" '{print $5}' | xargs`
 
 echo $PINGTIME
 if expr "$PINGTIME" '>' 0
 then
   # Remaining battery capacity
   upsHighPrecBatteryCapacity=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.1.2.3.1.0 | sed 's/.\{1\}$/.&/'`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$BAT_LEVEL&nvalue=0&svalue=$upsHighPrecBatteryCapacity"

   # Battery Temperature
   upsHighPrecBatteryTemperature=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.1.2.3.2.0 | sed 's/.\{1\}$/.&/'`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$BAT_TEMP&nvalue=0&svalue=$upsHighPrecBatteryTemperature"
 
   # Battery Voltage
   upsHighPrecBatteryActualVoltage=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.1.2.3.4.0 | sed 's/.\{1\}$/.&/'`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$BAT_VOLTS&nvalue=0&svalue=$upsHighPrecBatteryActualVoltage"

   #Input Voltage
   upsHighPrecInputLineVoltage=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.1.3.3.1.0 | sed 's/.\{1\}$/.&/'`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UPS_IN_VOLTS&nvalue=0&svalue=$upsHighPrecInputLineVoltage"
 
   #Input Max Voltage (previous minute)
   upsHighPrecInputMaxLineVoltage=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.1.3.3.2.0 | sed 's/.\{1\}$/.&/'`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UPS_IN_MAX_VOLTS&nvalue=0&svalue=$upsHighPrecInputMaxLineVoltage"
 
   #Input Min Voltage (previous minute)
   upsHighPrecInputMinLineVoltage=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.1.3.3.3.0 | sed 's/.\{1\}$/.&/'`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UPS_IN_MIN_VOLTS&nvalue=0&svalue=$upsHighPrecInputMinLineVoltage"
 
   # Input Frequency
   upsHighPrecInputFrequency=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.1.3.3.4.0 | sed 's/.\{1\}$/.&/'`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UPS_IN_FREQ&nvalue=0&svalue=$upsHighPrecInputFrequency"
 
   # Output Status
   upsBasicOutputStatus=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.1.4.1.1.0`
   upsBasicOutputStatusText=$(
     case "$upsBasicOutputStatus" in
       ("10") echo "Hardware%20Failure%20Bypass" ;;
       ("11") echo "Sleeping%20Until%20AC%20Returns" ;;
       ("12") echo "On%20SmartTrim" ;;
       ("13") echo "Eco%20Mode" ;;
       ("14") echo "Hot%20Standby" ;;
       ("15") echo "On%20Battery%20Test" ;;
       ("16") echo "Emergency%20Static%20Bypass" ;;
       ("17") echo "Static%20Bypass%20Standby" ;;
       ("18") echo "Power%20Saving%20Mode" ;;
       ("19") echo "Spot%20Mode" ;;
       ("20") echo "eConversion" ;;
       ("21") echo "Charger%20Spot%20Mode" ;;
       ("22") echo "Inverter%20Spot%20Mode" ;;
       ("2") echo "On%20Line" ;;
       ("3") echo "On%20Battery" ;;
       ("4") echo "On%20SmartBoost" ;;
       ("5") echo "Timed%20Sleeping" ;;
       ("6") echo "Software%20Bypass" ;;
       ("7") echo "Off" ;;
       ("8") echo "Rebooting" ;;
       ("9") echo "Switched%20Bypass" ;;
       (*) echo "Unknown" ;;
     esac)
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UPS_OUT_STATUS&nvalue=0&svalue=$upsBasicOutputStatusText"

   # Output Voltage
   upsHighPrecOutputVoltage=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.1.4.3.1.0 | sed 's/.\{1\}$/.&/'`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UPS_OUT_VOLTS&nvalue=0&svalue=$upsHighPrecOutputVoltage"

   # Output Frequency
   upsHighPrecOutputFrequency=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.1.4.3.2.0 | sed 's/.\{1\}$/.&/'`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UPS_OUT_FREQ&nvalue=0&svalue=$upsHighPrecOutputFrequency"

   # Output Power
   upsHighPrecOutputLoad=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.1.4.3.3.0`
   OutWatts=`awk '{print $1*$2/1000}' <<<"$upsHighPrecOutputLoad $UPS_WATTAGE"`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UPS_OUT_WATTS&nvalue=0&svalue=$OutWatts"

   # Environment Monitoring Probe Temperature
   iemStatusProbeCurrentTemp=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.10.2.3.2.1.4.1`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$IEM_TEMP&nvalue=0&svalue=$iemStatusProbeCurrentTemp"

   # Environment Monitoring Probe Humidity
   iemStatusProbeCurrentHumid=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.10.2.3.2.1.6.1`
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$IEM_HUMID&nvalue=$iemStatusProbeCurrentHumid&svalue=0"

   # Environment Monitoring Probe Temperature/Humidity (uses and combines data gathered in the above two snmpget commands)
   curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$IEM_TEMP_HUMID&nvalue=0&svalue=$iemStatusProbeCurrentTemp;$iemStatusProbeCurrentHumid;0"

   # Environment Monitoring Card Relay Status
   iemStatusRelayStatus=`snmpget -v 1 -c $PASSWORD -O qv $UPS_IP 1.3.6.1.4.1.318.1.1.10.2.3.7.1.3.1`
   if [ "$iemStatusRelayStatus" = "2" ]; then
     curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$IEM_RELAY&switchcmd=Off"
   else
     curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$IEM_RELAY&switchcmd=On"
   fi
 fi
Make sure it is executable

Code: Select all

chmod +x ap96xx.sh
Let crontab execute it every minute:

Code: Select all

crontab -e

Code: Select all

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
* * * * * ~/domoticz/scripts/ap96xx.sh
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest