Overview: “pwrstat” is a utility in PowerPanel for GNU/Linux which allows users to configure and monitor the status of CyberPower’s uninterruptible power supply (UPS). Documentation and software is available here


Configuration


Depending on the model, one must first connect their node to the UPS directly via Ethernet (RJ 45) or USB cable. Also, the battery in the UPS should charge fully before the initial deployment. In addition, the UPS LCD panel has a few configurable options of which the defaults are safe. 


On the software side, one needs to install the .deb file from CyberPower that matches their node’s architecture, e.g.: 


sudo dpkg -i 'CyberPower_PPL_Linux+64bit+(deb)_v1.4.1.deb'

 

One can then check “pwrstat” status: 


sudo pwrstat -status 


as well as the current "pwrstat" configuration: 


          sudo pwrstat -config


Sample (default) configuration:


—-------


Daemon Configuration:


Alarm .............................................. On

Hibernate .......................................... Off

Cloud .............................................. Off


Action for Power Failure:


    Delay time since Power failure ............. 60 sec.

    Run script command ......................... On

    Path of script command ..................... /etc/pwrstatd-powerfail.sh

    Duration of command running ................ 0 sec.

    Enable shutdown system ..................... On


Action for Battery Low:


    Remaining runtime threshold ................ 300 sec.

    Battery capacity threshold ................. 35 %.

    Run script command ......................... On

    Path of command ............................ /etc/pwrstatd-lowbatt.sh

    Duration of command running ................ 0 sec.

    Enable shutdown system ..................... On


 —------


In /etc, there’s one (1) configuration file [pwrstatd.conf] and three (3) shell scripts [ email.sh, lowbatt.sh, powerfail.sh] that manage interactions between a node and the UPS, as well as emit emails. Be sure to create a backup of the configuration files BEFORE you edit the live ones. Also, make sure you’ve first followed these instructions for getting your node to emit email. 


For pwrstatd.conf, the default settings are suitable for most use cases, but may be modified by editing the file, and then restarting the powerstat daemon with:


    sudo /etc/init.d/pwrstatd restart


For the other three (3) files, first configure pwrstatd-email.sh. Here’s how one’s set up to work at Meyer Hall (6 Washington Place) : 


—----------------


#!/bin/bash


#

# This is a script of the send mail for pwrstatd daemon using.

# 12.14.2023: Script modified so that it can emit useful messages to the CNS ticketing system.

# By: Foo Fighter <foo fighter [at] nyu [dot] edu>

# If you want to change SMTP server, edit following parameters into /etc/mail.rc file.

# set smtp=smtp server address

# set smtp-auth-user=user name

# set smtp-auth-password=user password


EVENT=$(pwrstat -status |awk '/Power Event/ {print $4, $5, $6, $7}')

SENDER_ADDRESS="@nyu.edu"

RECEIPT_NAME="Foo Fighter"

RECEIPT_ADDRESS="@nyu.edu"


SUBJECT="PowerPanel Notification - [$EVENT]"

FROM="PowerPanel Daemon <$SENDER_ADDRESS>"

TO="$RECEIPT_NAME <$RECEIPT_ADDRESS>"

MESSAGE="Warning: The $EVENT event has occurred for a while, system will be shutdown immediately!"


REMAINING_RUNTIME=$(pwrstat -status |awk '/Remaining Runtime/ {print $3}')

MODEL_NAME=$(pwrstat -status |awk '/Model Name/ {print $3}')

BATTERY_CAPACITY=$(pwrstat -status |awk '/Battery Capacity/ {print $3}')


DATE=`date +'%Y/%m/%d %p %H:%M'`

test ${#DATE}


RUNTIME=""


if [ ! -z "$REMAINING_RUNTIME" ]; then

    RUNTIME="Remaining Runtime: $REMAINING_RUNTIME Seconds"

fi


DATA=(

"========================================================"

"   $SUBJECT"

"========================================================"

""

""

"$MESSAGE"

"Time: $DATE"

""

""

"UPS Model Name: $MODEL_NAME"

"Battery Capacity: $BATTERY_CAPACITY %"

"$RUNTIME"

)


IFS=$'\n'

echo "${DATA[*]}" | mail \

-r   "$FROM" \

-s   "$SUBJECT" \

     "$TO"


exit 0


—----------------


Second configure: pwrstatd-lowbatt.sh with the following: 


—----------------


#!/bin/sh

echo "Warning: The UPS's battery power is not enough, system will be shutdown soon!" | wall


export RECEIPT_NAME

export RECEIPT_ADDRESS

export SENDER_ADDRESS


#

# If you want to receive event notification by e-mail, you must change 'ENABLE_EMAIL' item to 'yes'.

# Note: After change 'ENABLE_EMAIL' item, you must asign 'RECEIPT_NAME', 'RECEIPT_ADDRESS', and

# 'SENDER_ADDRESS' three items as below for the correct information.

# 12.14.2023 modified by Foo Fighter <foo fighter [at] nyu [dot] edu>


# Event code

EVENT="Low Battery"


# Enable to send e-mail

ENABLE_EMAIL=yes


# Change your name at this itme.

RECEIPT_NAME="Foo Fighter"


# Change mail receiver address at this itme.

RECEIPT_ADDRESS=@nyu.edu


# Change mail sender address at this itme.

SENDER_ADDRESS=@nyu.edu


# Execute the 'pwrstatd-email.sh' shell script

if [ $ENABLE_EMAIL = yes ]; then

   /etc/pwrstatd-email.sh

fi


—----------------


Third, configure: pwrstatd-powerfail.sh with the following: 


—----------------


#!/bin/sh

echo "Warning: Utility power failure has occurred for a while, system will be shutdown soon!" | wall


export RECEIPT_NAME

export RECEIPT_ADDRESS

export SENDER_ADDRESS


#

# If you want to receive event notification by e-mail, you must change 'ENABLE_EMAIL' item to 'yes'.

# Note: After change 'ENABLE_EMAIL' item, you must asign 'RECEIPT_NAME', 'RECEIPT_ADDRESS', and

# 'SENDER_ADDRESS' three items as below for the correct information.

# 12.14.2023 modified by Foo Fighter <foo fighter [at] nyu [dot] edu>


# Event code

EVENT="Power Failure"


# Enable to send e-mail

ENABLE_EMAIL=yes


# Change your name at this itme.

RECEIPT_NAME="root"


# Change mail receiver address at this itme.

RECEIPT_ADDRESS=@nyu.edu


# Change mail sender address at this itme.

SENDER_ADDRESS=@nyu.edu


# Execute the 'pwrstatd-email.sh' shell script

if [ $ENABLE_EMAIL = yes ]; then

   /etc/pwrstatd-email.sh

fi


—----------------


Confirm that things work as expected by testing various power disruption scenarios.