add pwrstatd scripts for UPS
authorAndrew Lorimer <andrew@charles.cortex>
Tue, 21 Jan 2020 11:37:03 +0000 (22:37 +1100)
committerAndrew Lorimer <andrew@charles.cortex>
Tue, 21 Jan 2020 11:37:03 +0000 (22:37 +1100)
pwrstatd-lowbatt.sh [new file with mode: 0755]
pwrstatd-powerfail.sh [new file with mode: 0755]
diff --git a/pwrstatd-lowbatt.sh b/pwrstatd-lowbatt.sh
new file mode 100755 (executable)
index 0000000..4c59cfb
--- /dev/null
@@ -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 (executable)
index 0000000..65ea690
--- /dev/null
@@ -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