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.

18 comments:

  1. Thanks, this helped my set up. Have you tried to use this with the business agent? Seems like the web front has more testing capabilities and such but I want to daemon as well.

    ReplyDelete
  2. Great source of information about setting up battery backups! If you are interested about different UPS, you can go check indicii suplimentare for further choices.

    ReplyDelete
  3. Thanks for sharing this information' this would really help. I can use this as resources. Keep posting.

    ReplyDelete
  4. Great Document! very concise with all the information needed to get started

    ReplyDelete
  5. Office UPS System UPS & batteries online sales & support. iElectrical is an online subsidiary of Computer & Telecom. Buy UPS Battery, Online PLC, Office UPS & Backup Power System

    ReplyDelete
  6. 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. camping batteries

    ReplyDelete
  7. Thanks, this was useful. Can I contribute a small additional improvement?

    As of PowerPanel version 1.3.2 (march 2016) another script gets installed: /etc/shutdown.sh. By default, this simply contains "shutdown -h now" to turn the computer off. However, this results in the computer not turning on again when power is eventually restored.

    In order for the computer to automatically turn back on when the power restored, two things are required:
    1) configure your BIOS setting "Restore on AC Power Loss" (or similar) and set it to "Last State". If you don't have this setting, then you can stop here - you wont be able to have the computer turn back on automatically.
    2) edit /etc/shutdown.sh and change the "-h" to "-H". This causes your computer to do all its normal shutdown activities, but stay powered on at the end. When the UPS turns off the power, the computer's "last state" was turned on, and the BIOS will restart the computer when the power is restored.


    ReplyDelete
  8. Online ups system - Find here online ups system manufacturers, suppliers in delhi, India. We provide online ups system manufacturers in delhi, India.
    VIEW MORE :-Online UPS System in India

    ReplyDelete
  9. Isolation Transformers- Find all the manufacturers of isolation transformers in delhi, India. Jetpowerdelhi are service provider of isolation transformers in delhi, India. VIEW MORE :- isolation transformers in india

    ReplyDelete
  10. Servo Stabilizers- Find here details of servo stabilizers in delhi India. We manufacturers and suppliers of servo stabilizers in Delhi India. VIEW MORE :- http://www.jetpowerdelhi.com/servo-stabilizers/

    ReplyDelete
  11. Industrial battery chargers- Find all the manufacturers of industrial battery chargers in delhi, India. Jet power are service provider of industrial battery in Delhi, India. VIEW MORE :-Industrial battery chargers in India

    ReplyDelete
  12. Sealed Maintenance Free Batteries - We provided sealed maintenance free batteries 12 volt, 200ah in delhi India. Various high quality of SMF ( Sealed Maintenance Free Batteries) in Delhi, India. VIEW MORE :-Sealed Maintenance Free Batteries in India

    ReplyDelete
  13. Thank you - worked for me. One thing, and I am sure it must be my setup, but I had to use /usr/sbin/update-rc.d pwrstatd defaults for one of the steps. I don't know whether it did anything or not, but the UPS is communicating. Thanks.

    ReplyDelete
  14. Thank you for this blog.Enertech group one of the popular online UPS manufacturers .

    ReplyDelete
  15. Helpful explanations thanks. I suppose there is a point of weakness here at the point the computer going into the halted -H state where it is still on and unable to recover unless the power goes out. i.e. if power comes back on here without the UPS going flat and the machine powering off then the machine will stay in this -H state indefinitely. Still ok for my home setup.

    ReplyDelete
  16. I swithced my UPS battery to UTL and I must say, the quality of battery which UTL solar is providing is unmatchable with any other brand.

    ReplyDelete
  17. Borgata Hotel Casino & Spa - MapyRO
    Borgata Hotel Casino & 강원도 출장마사지 Spa 광양 출장마사지 locations, rates, amenities: 대전광역 출장마사지 expert 파주 출장마사지 Borgata research, only at Hotel and Travel Index. Mapmyro.com. 경상남도 출장마사지

    ReplyDelete