Monday, July 15, 2013

Setup a UPS daemon for Cyberpower UPS Battery Backups

If you wish to use a Cyberpower UPS on a Linux system, then you'll want a monitoring daemon that alerts your PC when it's on battery power and does a proper shutdown before battery runs out. I have multiple cyberpower ups systems that I've verified this works on : CP1500AVR, CP1500AVRLCD, CP900AVR, PR1000LCDRTXL2Ua. It's likely it works on any cyberpower device. NOTE: this is for USB only, if you are using a rs232 serial connection - the below steps have not been verified.

You have a couple options to achieve interoperability, such as PowerPanel, upsmon, nut. The only one this article will explore at the moment is Cyberpower's Linux Power Panel Software. Although this article will most likely be updated with a NUT setup in the future.

Unless your distro comes with this package (Gentoo does not at the time of writing this), download the latest copy from their website : http://www.cyberpowersystems.com/products/management-software/ppl.html?selectedTabId=resources&imageI=#tab-box

Preferably the .tar package so you can build and customize as you see fit.



You can then verify connectivity with the following command :


 You will probably want to read through this config file before customizing your own:


 And the following helper scripts that come stock with this application:


The helper scripts are useful, but I started out by moving them to /usr/local/bin to maintain an orderly filesystem, then modified them as outlined below :


The following is my pwrstat daemon config with all notation removed.
Note: I don't have it setup to turn off my PC at the first power failure - as short power interruptions are common where I live - the below will only initiate a shutdown on low battery mode. Other notes are I have it setup to run until battery reaches 15% and then give 30s to launch script and another 60s to shutdown PC. Depending on the size of your UPS and power requirements, you might have to tweak those numbers to allot yourself enough time.
SIDE NOTE: I won't go into a huge recommendation and list of considerations, but generally speaking I measure my power usage for my PC and one monitor (other monitors, speakers, etc are all on the ups' surge protection only - not battery), and find a UPS wattage where I'd use roughly 60% of max UPS rated wattage. Most UPS' and power supplies have their peak efficiency around 50-60% utilization. Additionally I like to plan provide my desktop PC for around 20minutes of backup power time.



Afterwards you can restart the daemon and confirm the new settings have been applied :


Now we'll modify the helper scripts below...
First is pwrstatd-email.sh . If you wish to send an email when a power outtage has occured continue with this config, otherwise you may skip. A good thing to know is your cell phones SMS email address, which will send you a text message, very handy for instant PC alerts. If you don't know your carriers SMS address google it, for example ATT is : 10digitnumber@txt.att.net. You'll want to keep the subjects and message bodies short for txt alerts - in the below example I've added a secondary RECEIPT address and subject section for shortened text messaging purposes.

Note: this script depends on system email settings, using `mail`. I'll go through a basic setup to use gmail's smtp servers. I recommend having a gmail account used just for smtp email sending, so you don't have to keep your primary accounts password stored in a plain text file on your filesystem.


Modify the following with your login information...
Note: You do not need to include @gmail.com for AuthUser. Also for hostname you can comment out, or not include the domain if you are not a member of one.

You can test to see if the email works now : Replacing email@gmail.com with your TO: address.

Assuming that works, then next you can look over the pwrstatd-email.sh to confirm it will work with your setup:


We're going to use a centralized config file (pwrstatd-email.conf), as to not declare variables that may change inside a shell script:


Next we'll look at pwrstatd-powerfail.sh. This is the script that will run as soon as your UPS goes into on-battery mode (after whatever delay set in configuration).


Next we'll look at pwrstatd-lowbatt.sh. This is the script that will run as soon as your UPS goes into "low battery" mode, whatever battery percentage specified above, after the configured delay.

Note: This is where you might want to add any custom shutdown options for your PC (if it's setup to shutdown here). If you have a complicated shutdown procedure, IE certain daemons stopped, work saved, etc, it might be best to create a script that does it all (if you don't have one already) then simply call that script from the end of this one. Also to note if you have multiple PC's running off a UPS - like a rackmount server setup - you can setup ssh key auth and write a script that will remote into each machine to issue a shutdown command.

After all configuration is done, save all your work on your PC and start testing.

You can start by running the scripts directly to test email functionality:

If you get an error about command wall or beep not found - and you're using gentoo the following will resolve:
With other distro's you'll have to install the relevant packages or remove those commands from the scripts.

Next disconnect the power cord from the wall to your UPS. It should turn on the battery and start beeping. Depending on how you set it up above with a delay, email, and custom notification settings - you should see that go off. You can verify the status with the following command :



Finally, wait until power drains down below your set low-battery threshold - and you should see your PC do a normal/safe shutdown.