You are on page 1of 8

Anites: Monitoring a UPS http://www.anites.com/2013/09/monitoring-ups.

html

Ce site utilise des cookies provenant de Google afin de fournir ses services, personnaliser les annonces et
analyser le trafic. Les informations relatives votre utilisation du site sont partages avec Google. En acceptant
ce site, vous acceptez l'utilisation des cookies.

EN SAVOIR PLUS OK !

All Things: Home Automation, Programming, Raspberry Pi, Arduino, Robotics and Linux

42 Anites ESXi Omnia :: What are Anites?

SATURDAY, SEPTEMBER 14, 2013 SEARCH

Monitoring a UPS
The original reason for my interest in the Raspberry Pi was for a mini computer to monitor a UPS
ABOUT ME
and alert us if there was a power loss. The following is how I setup the Raspberry Pi to monitor
the UPS and send an email to our cell phones in the event of a localized power loss.

Kenneth Burgener

View my complete profile

FOLLOW BY EMAIL

CyberPower LCD "Smart" UPS

ARCHIVE
Short Story of Education the Hard Way 2016 (1)
2015 (10)
(note to reader: If you are just interested just in the UPS monitoring details, skip ahead...)
2014 (5)
What are we monitoring with the UPS? Well, we have a small chest style deep freezer in our 2013 (22)
garage. We store steaks and other expensive meats in it. Anytime there is an excellent sale, my December (5)
wife stocks up on meats. A few months ago, something caused the GFCI to trip, and we didn't
November (4)
notice until a few days latter when the rancid smell reached us. We estimated over $500 worth of
meat lost in one fell swoop. So the wife tasked me with coming up with a solution to avoid a October (2)
repeat in the future. September (7)
Garage Door Part 1 - Open
So my solution was to use a "smart" UPS, one that I could actively read the metrics from, and Door (GPIO Output)
send a email notification in the event of a power failure. The second benefit, or so I had thought, is Monitoring a UPS
the UPS could keep the fridge powered for short term power loss. I already have a CyberPower
Driving a Relay using GPIO
UPS connected to my file server, and it works great for battery backed power outages, monitoring
battery reserves, and shutting down the server if the reserves deplete. Data Logging with the Intern
of Things - Xively

A bit of painful education. I wanted to make sure I purchased the "right" size of UPS, so I pulled Automated Sprinkler System
First Look
out my Kill A Watt power meter and connected it to the refrigerator. During the occasional freeze
cycle it appeared to pull about 460-490 Watts, and lasted less than a minute. At idle, it rated Remote Control Machines
closer to 10 Watts. I rationalized that the UPS should be able to handle a few of these cycles The MagPi
before failing out.
August (4)
When I looked online for consumer UPS, a majority measured in VA. So how does one get VA
from Watts? Well, I cheated. The Kill A Watt also has a VA meter, so I selected that, and it
showed around 560-580 VA. I then found a 600 VA CyberPower UPS. I admit that I realized 580
LABELS
to 600 is not a lot of wiggle room, but the next battery size up jumped quite a bit in price. I crossed achievement (2)
my fingers and ordered it. I should have done a bit more research. arduino (2)
automation (10)
The UPS arrived and I connected it to the freezer. The freezer's motor kicked on and the UPS

1 sur 8 06/07/2016 12:18


Anites: Monitoring a UPS http://www.anites.com/2013/09/monitoring-ups.html

Ce site utilise des cookies provenant de Google afin de fournir ses services, personnaliser les annonces et
analyser le trafic. Les informations relatives votre utilisation du site sont partages avec Google. En acceptant
ce site, vous acceptez l'utilisation des cookies.

EN SAVOIR PLUS OK !

Lesson learned: You would probably need an industrial sized UPS to battery back a simple games (1)
freezer. google (1)
hardware (1)
Well, the battery protection was a bust, but at least I could still use the UPS to monitor for a power howto (1)
failure. Unfortunately, had I known this, I could have saved a few bucks, and just purchased the ideas (2)
cheaper smaller "smart" UPS. The Raspberry Pi could run for days off even the smallest one.
installation (2)
keyboard (1)
So onto the technical stuff...
lala-pi (4)
linux (2)
How to monitor a UPS with a Raspberry Pi magazine (1)
magpi (1)
The setup. The Raspberry Pi lives out in the garage, on top of the deep freezer. I have a USB news (1)
powered hub connected to the UPS, in one of the battery backed up slots. The USB hub then power (1)
powers the Raspberry Pi (to avoid having to waste two power adapters). To talk to the USB hub I programming (5)
have a second USB cable connected to the data side of the Pi. To connect to the network, I have
pytemper (1)
a WiFi dongle connected to the USB powered hub. To try and keep it as clean as possible, I have
python (6)
Raspberry Pi inside a plastic container, with holes for air flow. Finally, to get as much use of the
Raspberry Pi, it also serves double duty and controls/monitors the garage door. raspberrypi (9)
raspbian (6)
There are numerous "UPS" monitoring packages available, but the one I have had the most raspbmc (1)
success with, is the one from APC called "apcupsd - APC UPS Power Management (daemon)". robots (1)
Although it was designed for APC branded UPS, it works quite well with numerous other UPS. security (2)
sprinkler (1)
I tend to purchase the CyperPower UPS line with the LCD display. I am a sucker for the LCD that
temperature (1)
provides quick visual metrics, of the UPS. The CyperPower UPS connects to a PC through USB
and works well with the apcupsd daemon. wifi (1)
x10 (1)
Luckily the package is already included in the Raspbian repositories, so installation is this simple: xively (1)
zeromq (1)

# apcupsd installation
apt-get update CONTACT
apt-get install apcupsd Name

The next step is to modify the configuration scripts, so apcupsd can find the UPS. Use your editor Email *
and modify /etc/apcupsd/apcupsd.conf with the following changes.

Message *
# vim /etc/apcupsd/apcupsd.conf

#UPSCABLE smart
UPSCABLE usb

#UPSTYPE apcsmart Send

UPSTYPE usb

#DEVICE /dev/ttyS0
DEVICE

These changes tell apcupsd that the UPS is connected via USB.

Next we want to test our connection to the UPS. This can be achieved with the 'acptest' program.

2 sur 8 06/07/2016 12:18


Anites: Monitoring a UPS http://www.anites.com/2013/09/monitoring-ups.html

Ce site utilise des cookies provenant de Google afin de fournir ses services, personnaliser les annonces et
analyser le trafic. Les informations relatives votre utilisation du site sont partages avec Google. En acceptant
ce site, vous acceptez l'utilisation des cookies.

EN SAVOIR PLUS OK !
Checking configuration ...
Attached to driver: usb
sharenet.type = Network & ShareUPS Disabled
cable.type = USB Cable
mode.type = USB UPS Driver
Setting up the port ...
Doing prep_device() ...

You are using a USB cable type, so I'm entering USB test mode
Hello, this is the apcupsd Cable Test program.
This part of apctest is for testing USB UPSes.

Getting UPS capabilities...SUCCESS

If you see the "SUCCESS", everything is great. There isn't much here that you will want to
change, so let's quit this program and start the apcupsd daemon.

First, enable the service:

# vim /etc/default/apcupsd

ISCONFIGURED=yes

Next, restart the service:

# service apcupsd restart

Starting UPS power management: apcupsd.

To have apcupsd auto start at boot:

# update-rc.d apcupsd defaults

Next, we can query the UPS to see the various metrics available to us using the 'apcaccess'
application. The properties that apcupsd supports is quite large, and each UPS uses different
properties, so your results may vary.

# apcaccess

APC : 001,032,0781
DATE : 2013-09-14 14:54:31 -0600
HOSTNAME : pi-ups
VERSION : 3.14.10 (13 September 2011) debian
UPSNAME : pi-ups
CABLE : USB Cable
DRIVER : USB UPS Driver
UPSMODE : Stand Alone

3 sur 8 06/07/2016 12:18


Anites: Monitoring a UPS http://www.anites.com/2013/09/monitoring-ups.html

Ce site utilise des cookies provenant de Google afin de fournir ses services, personnaliser les annonces et
analyser le trafic. Les informations relatives votre utilisation du site sont partages avec Google. En acceptant
ce site, vous acceptez l'utilisation des cookies.

EN SAVOIR PLUS OK !
TIMELEFT : 82.0 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME : 0 Seconds
OUTPUTV : 120.0 Volts
DWAKE : 000 Seconds
LOTRANS : 100.0 Volts
HITRANS : 140.0 Volts
ALARMDEL : 30 seconds
NUMXFERS : 0
TONBATT : 0 seconds
CUMONBATT: 0 seconds
XOFFBATT : N/A
SELFTEST : OK
STATFLAG : 0x07000008 Status Flag
SERIALNO : AEC7101.251
NOMINV : 120 Volts
NOMPOWER : 340 Watts
END APC : 2013-09-14 14:55:02 -0600

Now that we can query the UPS, let's work with the apcupsd "event" scripts. This is how we will
get an email sent when a power failure occurs. The event scripts can all be found in the
/etc/apcupsd/ folder. All events are controlled by the /etc/apcupsd/apccontrol master script, so
take a quick peak there if you want to know what else can be modified.

The two scripts we are interested in are the "onbattery" and "offbattery" scripts. The "onbattery" is
triggered during a power failure, and "offbattery" is triggered when power returns.

By default these scripts are just simple bash scripts that send a message to the PC's console and
send a local email. Instead of using local mail, we will use Gmail to send our notification, and write
the script in Python. One other side benefit of having these scripts written in Python, is I can also
integrate Xively and then update Xively with the various data points of my UPS.

Let's backup the current scripts, before modifying them:

cp /etc/apcupsd/onbattery /etc/apcupsd/onbattery.original
cp /etc/apcupsd/offbattery /etc/apcupsd/offbattery.original

This is the powery failure script I use:

onbattery:

#!/usr/bin/env python

import smtplib
import email.mime.text
import syslog

syslog.openlog('[UPS]')
def log(msg):
syslog.syslog(str(msg))

4 sur 8 06/07/2016 12:18


Anites: Monitoring a UPS http://www.anites.com/2013/09/monitoring-ups.html

Ce site utilise des cookies provenant de Google afin de fournir ses services, personnaliser les annonces et
analyser le trafic. Les informations relatives votre utilisation du site sont partages avec Google. En acceptant
ce site, vous acceptez l'utilisation des cookies.

EN SAVOIR PLUS OK !
msg_subject = "ALERT: UPS Power Failure"
msg_text = "Auto Notification"

log(msg_subject)

msg = email.mime.text.MIMEText(msg_text)
msg['Subject'] = msg_subject
msg['From'] = from_email
msg['To'] = ", ".join(to_emails)
s = smtplib.SMTP_SSL('smtp.gmail.com', '465')
s.login(GMAIL_ADDRESS, GMAIL_PASSWORD)
s.sendmail(from_email, to_emails, msg.as_string())
s.quit()

And for when power returns:

offbattery:

#!/usr/bin/env python

import smtplib
import email.mime.text
import syslog

syslog.openlog('[UPS]')
def log(msg):
syslog.syslog(str(msg))

GMAIL_ADDRESS = 'xxx@gmail.com'
GMAIL_PASSWORD = 'xxx'

from_email = GMAIL_ADDRESS
to_emails = ["xxxxxxxxxx@tmomail.net"] # cell phone address

msg_subject = "OK: UPS Power Recovered"


msg_text = "Auto Notification"

log(msg_subject)

msg = email.mime.text.MIMEText(msg_text)
msg['Subject'] = msg_subject
msg['From'] = from_email
msg['To'] = ", ".join(to_emails)
s = smtplib.SMTP_SSL('smtp.gmail.com', '465')
s.login(GMAIL_ADDRESS, GMAIL_PASSWORD)
s.sendmail(from_email, to_emails, msg.as_string())
s.quit()

Finally, we test the scripts locally to make sure the email notification is sent. If everything looks
good, test a power failure by disconnecting the UPS from the wall. Within a few seconds, the
apcupsd daemon should trigger the onbattery event and our email should be sent. Now we will
immediately know the next time the UPS looses wall power.

5 sur 8 06/07/2016 12:18


Anites: Monitoring a UPS http://www.anites.com/2013/09/monitoring-ups.html

Ce site utilise des cookies provenant de Google afin de fournir ses services, personnaliser les annonces et
analyser le trafic. Les informations relatives votre utilisation du site sont partages avec Google. En acceptant
ce site, vous acceptez l'utilisation des cookies.

EN SAVOIR PLUS OK !
Posted by Kenneth Burgener at 3:25 PM

Labels: automation

14 comments:
AndyG said...
Everything works dandy, did the install, tested the scripts with python, unplugged the UPS to test
it and I'm good with everything except the email functionality. When I unplug the UPS from the
wall, I get wall messages indicating the UPS is on battery & it will shutdown my Pi then reboot it
when the power comes back, but it's not actually sending the email. Any thoughts on what I
might be missing? I've checked the man pages, and it appears that APCUPSD is working fine
(apaccess and apctest work) so I know the UPS its talking fine to the Pi, it just seems like
somewhere in the apcontrol script. When I look at /var/log/messages I get:

19-Sep 1:12:46 raspberrypi kernel: [ 2584.736661] usb 1-1.2: USB disconnect, device number 6
19-Sep 1:12:46 raspberrypi kernel: [ 2584.736698] usb 1-1.2.1: USB disconnect, device number
7
19-Sep 1:12:46 raspberrypi kernel: [ 2584.958567] usb 1-1.2.2: USB disconnect, device number
8
19-Sep 1:15:54 raspberrypi shutdown[3604]: shutting down for system halt
19-Sep 1:16:14 raspberrypi kernel: imklog 5.8.11, log source = /proc/kmsg started.
19-Sep 1:16:14 raspberrypi rsyslogd: [origin software="rsyslogd" swVersion="5.8.11"
x-pid="1856" x-info="http://www.rsyslog.com"] start
19-Sep 1:16:14 raspberrypi kernel: [ 0.000000] Booting Linux on physical CPU 0x0

This is the apcupsd.events file:


2014-09-19 01:12:46 +0000 Power failure.
2014-09-19 01:12:52 +0000 Running on UPS batteries.
2014-09-19 01:15:53 +0000 Reached run time limit on batteries.
2014-09-19 01:15:53 +0000 Initiating system shutdown!
2014-09-19 01:15:53 +0000 User logins prohibited
2014-09-19 01:15:55 +0000 apcupsd exiting, signal 15
2014-09-19 01:15:55 +0000 apcupsd shutdown succeeded
2014-09-19 01:16:17 +0000 apcupsd 3.14.10 (13 September 2011) debian startup succeeded

Any suggestions on what I might be missing or where to try next?


September 18, 2014 at 8:36 PM

Anonymousy said...
Love the detailed description. But, remember that when the power fails you may not have a
working network to send the mail out.
October 11, 2014 at 5:25 AM

AndyG said...
*UPDATE*

I had a DOH! moment. I hit up a friend for some help and he noticed the APCCONTROL script
by default does not actually have a call to either onbattery or offbattery, thus the reason my script
didn't work. He suggested adding the following if statement (change offbattery to onbattery for
that section):

6 sur 8 06/07/2016 12:18


Anites: Monitoring a UPS http://www.anites.com/2013/09/monitoring-ups.html

Ce site utilise des cookies provenant de Google afin de fournir ses services, personnaliser les annonces et
analyser le trafic. Les informations relatives votre utilisation du site sont partages avec Google. En acceptant
ce site, vous acceptez l'utilisation des cookies.

EN SAVOIR PLUS OK !

As for as the power fail not sending email, in my case, I have the router and modem on the UPS
as well so I should get a last gasp message, and if not, there are always the log files...
October 13, 2014 at 6:33 AM

Thomas said...
Thanks, this was very helpful. Exact what i was looking for.

Greetings from Germany,


Thomas
November 23, 2014 at 11:00 AM

Danny Swan said...


I' "borrowed" your coding and setup to monitor our power at work. It works great. Now i just need
to add one more thing, when the script is activated for power failure, i need it to switch a relay
high. This in turn will be connected to our security system and will send signal to the monitoring
company and they will make a phone call. not exactly sure how to do this, can it be added into
the existing scripts or should it run seperately?
February 18, 2015 at 5:11 PM

Joe T. said...
Very nice setup....I did this on the cyberpower ups i have protecting my fileservers. Can I call a
bash script from this onbattery script?
August 2, 2015 at 6:24 PM

Joe T. said...
Or...Can I call more than just the onbattery script?
August 2, 2015 at 6:40 PM

Joe T. said...
Ok...I figured it out. I have is wait 2 min then if it's still down, email me and shutdown my
fileservers.
August 8, 2015 at 2:49 PM

Joachim said...
Thank you for this posting. Worked fine for me.

When configured, do not forget to edit the following file:


/etc/default/apcupsd

and set ISCONFIGURED=yes

Greetiings vrom Germany


Joachim
December 27, 2015 at 2:55 AM

Kenneth Burgener said...


Thank you Joachim, updated the post.

7 sur 8 06/07/2016 12:18


Anites: Monitoring a UPS http://www.anites.com/2013/09/monitoring-ups.html

Ce site utilise des cookies provenant de Google afin de fournir ses services, personnaliser les annonces et
analyser le trafic. Les informations relatives votre utilisation du site sont partages avec Google. En acceptant
ce site, vous acceptez l'utilisation des cookies.

EN SAVOIR PLUS OK !

8 sur 8 06/07/2016 12:18

You might also like