AP96xx

From Domoticz
Revision as of 17:10, 3 March 2018 by AintBigAintClever (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Purpose

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)

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).

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.

#!/bin/bash

# =========================================================
# Domoticz APC/Schneider AP96xx UPS Management Card script
# Monitors an APC UPS using SNMP commands
# =========================================================
# AintBigAintClever, February 2018
# Derived from nas.sh
# Tested using an AP9617 card running AOS 3.7.3, 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="1001"				# Remaining Battery Capacity (percentage)
 BAT_TEMP="1002"				# Internal UPS Temperature (temperature)
 BAT_VOLTS="1003"				# Battery Voltage (voltage)
 UPS_IN_VOLTS="1004"			# Input Voltage (voltage)
 UPS_IN_MAX_VOLTS="1005"		# Input Max Voltage over previous minute (voltage)
 UPS_IN_MIN_VOLTS="1006"		# Input Min Voltage over previous minute (voltage)
 UPS_IN_FREQ="1007"			# Input Frequency (custom, Hz)
 UPS_OUT_STATUS="1008"			# Output Status (text)
 UPS_OUT_VOLTS="1009"			# Output Voltage (voltage)
 UPS_OUT_FREQ="1010"			# Output Frequency (custom, Hz)
 UPS_OUT_WATTS="1011"			# Output Power (Usage (Electric))

 
 # 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" ;;
       ("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"
 fi

Make sure it is executable

chmod +x ap96xx.sh

Let crontab execute it every minute:

crontab -e
# 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

AintBigAintClever (talk) 03:41, 28 February 2018 (CET)