From 7ebedd5ffad78ea39b85ea2996eb2d7a5b7dfdf2 Mon Sep 17 00:00:00 2001 From: Andrew Lorimer Date: Tue, 21 Jan 2020 22:37:03 +1100 Subject: [PATCH] add pwrstatd scripts for UPS --- pwrstatd-lowbatt.sh | 18 ++++++++++++++++++ pwrstatd-powerfail.sh | 13 +++++++++++++ 2 files changed, 31 insertions(+) create mode 100755 pwrstatd-lowbatt.sh create mode 100755 pwrstatd-powerfail.sh diff --git a/pwrstatd-lowbatt.sh b/pwrstatd-lowbatt.sh new file mode 100755 index 0000000..4c59cfb --- /dev/null +++ b/pwrstatd-lowbatt.sh @@ -0,0 +1,18 @@ +#! /bin/bash + +# Send warning email and shut down system when UPS battery is low. +# Andrew Lorimer, January 2020 + +recipient="andrew" + +ctime=`date` +state=`pwrstat -status` +model=`echo $state | grep -oP 'Model Name\.+ \K\w+'` +runtime=`echo $state | grep -oP 'Remaining Runtime\.+ \K\d+\s?\w+(?=\.)?'` +level=`echo $state | grep -oP 'Battery Capacity\.+ \K\d+'`"%" + +echo -e "Battery level is $level (expected runtime $runtime) as of $ctime. Shutting down system in one minute.\n$state" | mail -a "Importance: High" -a "Content-Type: text/plain; charset=UTF-8" -s "⚠️🔋 Battery level $level on $model" $recipient +sleep 60 + +echo "Starting system shutdown due to low battery" | systemd-cat -t `basename "$0"` +shutdown now diff --git a/pwrstatd-powerfail.sh b/pwrstatd-powerfail.sh new file mode 100755 index 0000000..65ea690 --- /dev/null +++ b/pwrstatd-powerfail.sh @@ -0,0 +1,13 @@ +#! /bin/bash + +# Send warning email on power loss. Triggered by pwrstatd. +# Andrew Lorimer, January 202t + +recipient="andrew" + +ctime=`date` +state=`pwrstat -status` +model=`echo $state | grep -oP 'Model Name\.+ \K\w+'` +runtime=`echo $state | grep -oP 'Remaining Runtime\.+ \K\d+\s?\w+(?=\.)?'` + +echo -e "Mains power was lost at $ctime. Expected runtime is $runtime.\n$state" | mail -a "Importance: High" -a "Content-Type: text/plain; charset=UTF-8" -s "⚠️🔌 Power failure on $model" $recipient -- 2.43.2