Pwrstat



I have a CyberPower BRG1350AVRLCD at home and I’ve just connected it to a new device. However, the pwrstat command doesn’t retrieve any useful data on the new system:

Bsp infoshow pwrstat Usage Guidelines After you run this command, the information about low power consumption is displayed, including the sleep time, scheduled power-off time, triggered event, and current lowe power consumption status.

  1. One in particular is called “powerstat”, and it is a pretty excellent little tool that calculates the power consumption of your Laptop/Notebook etc when running on your battery.
  2. The plug between the power supply and the mainboard follows the ATX specification, which regulates which signals and which voltages shall be present. Com is common, or ground. The rest of the pins are used for voltages, or simple binary signals, like PSON and PWROK. There is no bus data transfer pins.
  3. PWRSTAT Variable Transformer, 136 kVa, Voltage: 480V - 0 - 560VACR-0804B Rental Variable Transformer stocked by AContact Electric Rentals, LP.

I disconnected the USB cable and ran pwrstat again. Same output. I disconnected power from the UPS itself and ran pwrstat again. Same output. This can’t be right.

Checking the basics

A quick look at dmesg output shows that the UPS is connected and the kernel recognizes it:

I checked the /var/log/pwrstatd.log file to see if there were any errors:

The pwrstatd daemon can see the device and communicate with it. This is unusual.

Digging into the daemon

If the daemon can truly see the UPS, then what is it talking to? I used lsof to examine what the pwrstatd daemon is doing:

Wait a minute. The last line of the lsof output shows that pwrstatd is talking to /dev/ttyS1, but the device is supposed to be a hiddev device over USB. If you remember, we had this line in dmesg when the UPS was plugged in:

Things are beginning to make more sense now. I have a USB-to-serial device that allows my server to talk to the console port on my Cisco switch:

It appears that pwrstatd is trying to talk to my Cisco switch (through the USB-to-serial adapter) rather than my UPS! I’m sure they could have a great conversation together, but it’s hardly productive.

Fixing it

The /etc/pwrstatd.conf has a relevant section:

We need to explicitly tell pwrstatd to talk to the UPS on /dev/hid/hiddev0:

Let’s restart the pwrstatd daemon and see what we get:

Success!

Photo credit: Wikipedia

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

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:
Pwrstat daemon not found
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.

Pwrstat Test


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 (Pwrstat daemon not foundpwrstatd-email.conf

Pwrstat

), 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.
Pwrstat daemon not foundYou can start by running the scripts directly to test email functionality:
Pwrstat

Pwrstation Company Profile


If you get an error about command wall or beep not found - and you're using gentoo the following will resolve:

Pwrstation


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.