APC UPS Monitoring

All kinds of 'OS' scripts
Kruu
Posts: 23
Joined: Tuesday 17 January 2017 21:27
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post by Kruu » Sunday 19 November 2017 20:51

I am getting communication lost often. Can I do something about it?

2017-11-14 17:35:16 +0100 Communications with UPS lost.

assenzuid
Posts: 166
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: APC UPS Monitoring

Post by assenzuid » Tuesday 24 April 2018 19:55

Rene wrote:
Tuesday 13 January 2015 3:56
I made one too, this one is in Python script for whoever wants it, now with auto shutdown when battery power is down to 10%

Code: Select all

#!/usr/bin/python 
#
#		Script to read APC battery backups settings and voltages.
#   	And publish these to domoticz software.
#
#		(C)reated by Rtm-soft Jan 2015
#	
#		This code is Free Software and comes with absolutely
#		no warranty and may freely be modified and distributed.


#Modules used in this program

import subprocess	#For OS calls
import time
import requests		#For URL calls

#Install APC driver with:
#
#Read https://help.ubuntu.com/community/apcupsd or follow below lines
#
#Quick Install for USB-based APC units
#    install the daemon: apt-get install apcupsd
#    edit /etc/apcupsd/apcupsd.conf and change these lines
#        UPSNAME myownups
#        UPSCABLE usb
#        UPSTYPE usb
#        comment out DEVICE (it contains a TTY link, which will prevent it from working) 
#    edit /etc/default/apcupsd
#        change ISCONFIGURED from no to yes 
#    /etc/init.d/apcupsd start
#    apcaccess 

#Shutdown access to user without password:
#
#sudo visudo -f /etc/sudoers.d/myOverrides 
#
#<myusername>    ALL = NOPASSWD: /sbin/shutdown


#Constants (Modify these to match your own settings)

DOMOTICZ_URL = "http://localhost:8080"	#http://<username:password@>domoticz-ip<:port>
APCACCESS	= "/sbin/apcaccess"			#Location of apcaccess program
LINEV		= "40"							#Idx for Line Voltage
BATTV		= "44"							#Idx for Battery Voltage
LOADPCT		= "45"							#Idx for Load Percentage
BCHARGE		= "46"							#Idx for Battery Charge
MINBATT		= 10							#Shut down when battery power is at 10%

#Variables

def apc_probe() :
	batt = 100	#Needs to be >MINBATT to not do a false processor stop
	dict = {'LINEV' : LINEV, 'BATTV' : BATTV, 'LOADPCT' : LOADPCT, 'BCHARGE' : BCHARGE } #Convert Keyword to Domoticz index.
	while True :						#Endless loop
		res = subprocess.check_output(APCACCESS) 
		for line in res.split('\n') :
			(key,spl,val) = line.partition(': ')
			key = key.rstrip()			#Strip spaces right of text
			val = val.strip()			#Remove outside spaces
			if key == 'STATUS' and 'ONBATT' in val and batt < MINBATT : #If there is less then 10 percent battery power left and we are offline then shutdown
				subprocess.call("sudo shutdown -h now", shell=True)	#Edit /etc/sudoers.d/myOverrides to make shutdown without password work
			val = val.split(' ',1)[0] 	#Split using space and only take first part
			if key in dict : 			#Are we interested in this parameter?
				if key == 'BCHARGE' :
					batt = int(float(val))		#Save battery level for Offline mode.
				url= DOMOTICZ_URL+"/json.htm?type=command&param=udevice&idx="+dict[key]+"&nvalue=0&svalue="+val #Publish value to Domoitcz by means of json.
				requests.get(url)		#Print url
		time.sleep(60) #Take a minute break

#Main Loop
print "APC Probe running..."
apc_probe()					
Is there a way to see in an log file if it shutdowns the PI is batt. status is 10 min?

ausonius
Posts: 1
Joined: Monday 08 October 2018 12:53
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post by ausonius » Monday 08 October 2018 13:01

Hello everyone

In relation to the script below I can not figure out where it is inserted? in the installation directory domoticz script? under what name?

excuse me for my english


assenzuid wrote:
Saturday 21 October 2017 22:46
Rene wrote:
Tuesday 13 January 2015 3:56
I made one too, this one is in Python script for whoever wants it, now with auto shutdown when battery power is down to 10%

Code: Select all

#!/usr/bin/python 
#
#		Script to read APC battery backups settings and voltages.
#   	And publish these to domoticz software.
#
#		(C)reated by Rtm-soft Jan 2015
#	
#		This code is Free Software and comes with absolutely
#		no warranty and may freely be modified and distributed.


#Modules used in this program

import subprocess	#For OS calls
import time
import requests		#For URL calls

#Install APC driver with:
#
#Read https://help.ubuntu.com/community/apcupsd or follow below lines
#
#Quick Install for USB-based APC units
#    install the daemon: apt-get install apcupsd
#    edit /etc/apcupsd/apcupsd.conf and change these lines
#        UPSNAME myownups
#        UPSCABLE usb
#        UPSTYPE usb
#        comment out DEVICE (it contains a TTY link, which will prevent it from working) 
#    edit /etc/default/apcupsd
#        change ISCONFIGURED from no to yes 
#    /etc/init.d/apcupsd start
#    apcaccess 

#Shutdown access to user without password:
#
#sudo visudo -f /etc/sudoers.d/myOverrides 
#
#<myusername>    ALL = NOPASSWD: /sbin/shutdown


#Constants (Modify these to match your own settings)

DOMOTICZ_URL = "http://localhost:8080"	#http://<username:password@>domoticz-ip<:port>
APCACCESS	= "/sbin/apcaccess"			#Location of apcaccess program
LINEV		= "40"							#Idx for Line Voltage
BATTV		= "44"							#Idx for Battery Voltage
LOADPCT		= "45"							#Idx for Load Percentage
BCHARGE		= "46"							#Idx for Battery Charge
MINBATT		= 10							#Shut down when battery power is at 10%

#Variables

def apc_probe() :
	batt = 100	#Needs to be >MINBATT to not do a false processor stop
	dict = {'LINEV' : LINEV, 'BATTV' : BATTV, 'LOADPCT' : LOADPCT, 'BCHARGE' : BCHARGE } #Convert Keyword to Domoticz index.
	while True :						#Endless loop
		res = subprocess.check_output(APCACCESS) 
		for line in res.split('\n') :
			(key,spl,val) = line.partition(': ')
			key = key.rstrip()			#Strip spaces right of text
			val = val.strip()			#Remove outside spaces
			if key == 'STATUS' and 'ONBATT' in val and batt < MINBATT : #If there is less then 10 percent battery power left and we are offline then shutdown
				subprocess.call("sudo shutdown -h now", shell=True)	#Edit /etc/sudoers.d/myOverrides to make shutdown without password work
			val = val.split(' ',1)[0] 	#Split using space and only take first part
			if key in dict : 			#Are we interested in this parameter?
				if key == 'BCHARGE' :
					batt = int(float(val))		#Save battery level for Offline mode.
				url= DOMOTICZ_URL+"/json.htm?type=command&param=udevice&idx="+dict[key]+"&nvalue=0&svalue="+val #Publish value to Domoitcz by means of json.
				requests.get(url)		#Print url
		time.sleep(60) #Take a minute break

#Main Loop
print "APC Probe running..."
apc_probe()					
I'm using this great script, but how to be sure that its running after a reboot op my PI?

NiCOOLaS
Posts: 1
Joined: Tuesday 09 October 2018 20:53
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post by NiCOOLaS » Tuesday 09 October 2018 21:37

Hello all.

I set up monitoring of remote APC UPS SMT3000 using SNMP on the server (windows) connected to the UPS.
Here is the instruction:

On a server running an OS, for example Windows, to which UPS APC is connected, you must install / start the SNMP service. It will collect statistics on the operation of the UPS and provide information to customers.
The Domoticz management server (SNMP service client) will request information from the / UPS server for further processing.
Install / configure the SNMP service in Windows

Install/configure the SNMP service in Windows
Programs -> Programs and Components -> Windows Components (choose SNMP)
Start the service control and select the SNMP service
On the “Security” tab, add the community name “Public” with “read only” rights and select the “Receive SNMP packets from any node” option (it is possible to specify only specific addresses of clients).
On the “SNMP Agent” tab, fill in the data as you wish:
You also need to add a “trap” as in the picture, the ip address is Domoticz

image5.png

2. Install APC PowerChute
On the manufacturer's website, download the APC PowerChute suitable for your UPS
https://www.apc.com/shop/ru/ru/categori ... /N-13kn0i4
I used the version - PowerChute Business Edition v9.2, it does not support SNMP v3 and settings are easier.

During installation, the system will automatically query the ports and configure the connection to the UPS(USB), and also request a password for the UPS Web Management Interface.
After installation, you must go to the Web interface at: https://localhost:6547 . Most likely, the browser will block the transition and write that we’ll ignore the “connection is not protected” and proceed.
On the “PowerChute” tab, select “SNMP Settings” and activate “SNMPv1”

image3.png

Reboot.
The server is ready to transfer the UPS parameters, now we will configure Domoticz to read them.

Install SNMP on Raspberry Pi

Code: Select all

sudo apt-get install snmpd
sudo apt-get install snmp
Reboot the Pi

Code: Select all

sudo reboot
Run to check:

Code: Select all

snmpget -v 1 -c public -O qv IPserver 1.3.6.1.4.1.318.1.1.1.1.1.1.0
Where is the “public” community that you specified when configuring SNMP,, “IPserver ” - is the IP address of the server with UPS
We will get a response with the name:

Code: Select all

"Smart-UPS 3000"

Now creating in Domoticz 1 virtual switch and several sensors (3 for voltage, 2 for %, 1 text and 1 temperature)
The script itself is created here:
UPS_APC.sh in the /domoticz/scripts/ folder
script

#!/bin/bash

Code: Select all

# Settings
UPSIP="192.x.x.x"                   # UPS (SNMP server) IP Address
PASSWORD="public"                   # SNMP Password
DOMO_IP="192.x.x.x"                # Domoticz IP Address
DOMO_PORT="8080"                    # Domoticz Port

# Monitoring parameters
UPS_IDX="53"                                   # UPS Switch IDX (ON/OFF State)
upsAdvInputLineVoltage_IDX="50"                # APC UPS Input Voltage (V AC)
upsAdvBatteryTemperature_IDX="52"              # APC UPS Internal Temperature (C)
upsAdvBatteryCapacity_IDX="54"                 # APC UPS Battery Capacity (%)
upsAdvBatteryRunTimeRemaining_IDX="55"         # APC UPS Battery Runtime Remaining (TimeTicks)
upsAdvBatteryActualVoltage_IDX="56"            # APC UPS Battery Voltage (V DC)
upsAdvOutputLoad_IDX="57"                      # APC UPS Output Load (%) VA
upsAdvOutputVoltage_IDX="58"                   # APC UPS Output Voltage (V AC)
 
# Check if UPS in online 
PINGTIME=`ping -c 1 -q $UPSIP | awk -F"/" '{print $5}' | xargs`
echo $PINGTIME
if expr "$PINGTIME" '>' 0
then

    curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=devices&rid=$UPS_IDX" | grep "Status" | grep "On" > /dev/null
    if [ $? -eq 0 ] ; then
        echo "UPS already ON"
    else
        echo "UPS ON"
        # Send data
        curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$UPS_IDX&switchcmd=On"
    fi
    
    # UPS Input Voltage (V AC)
    UPSInVolt=`snmpget -v 1 -c $PASSWORD -O qv $UPSIP 1.3.6.1.4.1.318.1.1.1.3.2.1.0`
    # Send data
    curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$upsAdvInputLineVoltage_IDX&nvalue=0&svalue=$UPSInVolt"

    # UPS Battery Temperature 
    UPSBattemp=`snmpget -v 1 -c $PASSWORD -O qv $UPSIP 1.3.6.1.4.1.318.1.1.1.2.2.2.0`
    # Send data
    curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$upsAdvBatteryTemperature_IDX&nvalue=0&svalue=$UPSBattemp"

    # UPS Battery Capacity (%)
    UPSBatCapst=`snmpget -v 1 -c $PASSWORD -O qv $UPSIP 1.3.6.1.4.1.318.1.1.1.2.2.1.0`
    # Send data
    curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$upsAdvBatteryCapacity_IDX&nvalue=0&svalue=$UPSBatCapst"

    # UPS Battery Runtime Remaining (TimeTicks)
    UPSTimeRem=`snmpget -v 1 -c $PASSWORD -O qv $UPSIP 1.3.6.1.4.1.318.1.1.1.2.2.3.0`
    # Send data
    curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$upsAdvBatteryRunTimeRemaining_IDX&nvalue=0&svalue=$UPSTimeRem"

    # UPS Battery Voltage (V DC)
    UPSBatVolt=`snmpget -v 1 -c $PASSWORD -O qv $UPSIP 1.3.6.1.4.1.318.1.1.1.2.2.8.0`
    # Send data
    curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$upsAdvBatteryActualVoltage_IDX&nvalue=0&svalue=$UPSBatVolt"

    # UPS Output Load (%) VA
    UPSOutLoad=`snmpget -v 1 -c $PASSWORD -O qv $UPSIP 1.3.6.1.4.1.318.1.1.1.4.2.3.0`
    # Send data
    curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$upsAdvOutputLoad_IDX&nvalue=0&svalue=$UPSOutLoad"

    # UPS Output Voltage (V AC)
    UPSOutVolt=`snmpget -v 1 -c $PASSWORD -O qv $UPSIP 1.3.6.1.4.1.318.1.1.1.4.2.1.0`
    # Send data
    curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$upsAdvOutputVoltage_IDX&nvalue=0&svalue=$UPSOutVolt"


else
    
    curl -s "http://$DOMO_IP:$DOMO_PORT/json.htm?type=devices&rid=$UPS_IDX" | grep "Status" | grep "Off" > /dev/null
    if [ $? -eq 0 ] ; then
        echo "UPS already OFF"
    else
        echo "UPS OFF"
        # Send data
        curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$UPS_IDX&switchcmd=Off"
    fi
fi

In the script IP addresses and identifiers (№ IDX) replace with your own.
If Domoticz is password protected, you need to add 2 variables:

Code: Select all

DOMO_LOGIN="yourusername"
DOMO_PASS="yourpassword"
And change the link in the request to this view:

Code: Select all

http://$DOMO_LOGIN:$DOMO_PASS@$DOMO_IP:$DOMO_PORT 
Instead:

Code: Select all

 http://$DOMO_IP:$DOMO_PORT

Making the script executable:
In the folder with the script type:

Code: Select all

 sudo chmod +x UPS_APC.sh
Run to check:

Code: Select all

 ./UPS_APC.sh
Configure the scheduler

Code: Select all

 crontab -e
Updating once a minute:

Code: Select all

 */1 * * * * /home/pi/domoticz/scripts/UPS_APC.sh
Done!
image3.png

p.s. sorry for my english :roll:
Attachments
image2.png
image2.png (15.47 KiB) Viewed 74 times
image3.png
image3.png (25.78 KiB) Viewed 74 times
image3.png
image3.png (25.78 KiB) Viewed 75 times
image5.png
image5.png (68.31 KiB) Viewed 75 times

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests