From 092bb6fc0daeec99d940b93eeeb7f3eda3d445ff Mon Sep 17 00:00:00 2001 From: Josh S Date: Fri, 18 Aug 2023 18:17:33 -0400 Subject: [PATCH] yes --- README.md | 92 ++++++++++++++++++++++++++++++++++++++++++ scripts/critlowbatt.sh | 33 +++++++++++++++ scripts/lowbatt.sh | 33 +++++++++++++++ scripts/onbatt.sh | 25 ++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 README.md create mode 100644 scripts/critlowbatt.sh create mode 100644 scripts/lowbatt.sh create mode 100644 scripts/onbatt.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..02817be --- /dev/null +++ b/README.md @@ -0,0 +1,92 @@ +# These are automated notification scripts for the APC UPS + +### Steps to setup are here: https://www.pontikis.net/blog/apc-ups-on-ubuntu-workstation + + + +# How to setup + +1. Install `sudo apt-get install ssmtp` +2. Enter your SMTP creds: `nano /etc/ssmtp/ssmtp.conf` + +``` +root=changeme@example.com +mailhub=smtp.example.com:587 +AuthUser=your_username +AuthPass=your_password +UseTLS=YES # OR UseSTARTTLS=YES + +``` +3. Go to `/etc/apcupsd/` and edit the onbattery file. `nano /etc/apcupsd/onbattery` + +it should look something like this: + +``` +#!/bin/sh +# +# This shell script if placed in /etc/apcupsd +# will be called by /etc/apcupsd/apccontrol when the UPS +# goes on batteries. +# We send an email message to root to notify him. +# + +HOSTNAME=`hostname` +MSG="$HOSTNAME UPS $1 Power Failure !!!" +# +( + echo "$MSG" + echo " " + /sbin/apcaccess status +) | $APCUPSD_MAIL -s "$MSG" $SYSADMIN +exit 0 + +``` + +you'll want yours to look like this: + +``` +#!/bin/sh +# +# This shell script if placed in /etc/apcupsd +# will be called by /etc/apcupsd/apccontrol when the UPS +# goes on batteries. +# We send an email message to root to notify him. +# + +HOSTNAME=`hostname` +MSG="$HOSTNAME UPS $1 Power Failure !!!" +# +( + echo "$MSG" + echo " " + /sbin/apcaccess status +) | $APCUPSD_MAIL -s "$MSG" $SYSADMIN +./etc/apcupsd/scripts/onbatt.sh +exit 0 + +``` + +4. Run these commands: `cd /etc/apcupsd/ && mkdir scripts && nano onbatt.sh` + +then fill the `onbatt.sh` file with the contents from this github repo + +5. Run these commands to grant permissions to the script and run it: `chmod +x ./onbatt.sh && ./onbatt.sh` and it should run some commands and send you the email! + +# Setting up the battery alerts + +1. Run this: `crontab -e` and scroll all the way to the bottom and paste these lines: + +``` +*/1 * * * * /bin/bash /etc/apcupsd/scripts/lowbatt.sh +*/1 * * * * /bin/bash /etc/apcupsd/scripts/critlowbatt.sh + +``` +and then `systemctl restart cron` + +this will save the crontab file and start running the crons. + +Want to change how often they run? + +Go to: https://crontab.cronhub.io/ + +## If you need any help feel free to email me: `me@joshsevero.dev` \ No newline at end of file diff --git a/scripts/critlowbatt.sh b/scripts/critlowbatt.sh new file mode 100644 index 0000000..230e3d8 --- /dev/null +++ b/scripts/critlowbatt.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Replace these variables with your actual values +EMAIL_TO="" +EMAIL_SUBJECT="UPS Battery Critically Low" +SENDER_ADDRESS="" + +# Check the UPS battery percentage using the apcaccess command +battery_percentage=$(apcaccess status 2>/dev/null | awk '/BCHARGE/ {print $3}') + +# Define the threshold as a floating-point number +threshold=30.0 + +# Compare battery percentage with the threshold using bc +if (( $(echo "$battery_percentage <= $threshold" | bc -l) )); then + echo "UPS battery is at or below $threshold%. Sending email alert." + + # Get estimated remaining runtime + remaining_runtime=$(apcaccess status 2>/dev/null | awk '/TIMELEFT/ {print $3}') + + # Send email using ssmtp + { + echo "To: $EMAIL_TO" + echo "From: $SENDER_ADDRESS" + echo "Subject: $EMAIL_SUBJECT" + echo + echo "UPS battery is at or below $threshold%." + echo "Current Level: $battery_percentage%" + echo "Estimated time remaining: $remaining_runtime" + } | ssmtp -vvv $EMAIL_TO +else + echo "UPS battery is above $threshold%. No action needed." +fi \ No newline at end of file diff --git a/scripts/lowbatt.sh b/scripts/lowbatt.sh new file mode 100644 index 0000000..d12d717 --- /dev/null +++ b/scripts/lowbatt.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Replace these variables with your actual values +EMAIL_TO="" +EMAIL_SUBJECT="UPS Battery Low" +SENDER_ADDRESS="" + +# Check the UPS battery percentage using the apcaccess command +battery_percentage=$(apcaccess status 2>/dev/null | awk '/BCHARGE/ {print $3}') + +# Define the threshold as a floating-point number +threshold=75.0 + +# Compare battery percentage with the threshold using bc +if (( $(echo "$battery_percentage <= $threshold" | bc -l) )); then + echo "UPS battery is at or below $threshold%. Sending email alert." + + # Get estimated remaining runtime + remaining_runtime=$(apcaccess status 2>/dev/null | awk '/TIMELEFT/ {print $3}') + + # Send email using ssmtp + { + echo "To: $EMAIL_TO" + echo "From: $SENDER_ADDRESS" + echo "Subject: $EMAIL_SUBJECT" + echo + echo "UPS battery is at or below $threshold%." + echo "Current Level: $battery_percentage%" + echo "Estimated time remaining: $remaining_runtime" + } | ssmtp -vvv $EMAIL_TO +else + echo "UPS battery is above $threshold%. No action needed." +fi \ No newline at end of file diff --git a/scripts/onbatt.sh b/scripts/onbatt.sh new file mode 100644 index 0000000..1510560 --- /dev/null +++ b/scripts/onbatt.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Replace these variables with your actual values +EMAIL_TO="" +EMAIL_SUBJECT="Power Outage Detected." +SENDER_ADDRESS="" + +# Check the UPS status using the apcaccess command +status=$(apcaccess status 2>/dev/null | grep STATUS | awk '{print $3}') + +# Check if UPS is offline and send an email if necessary +if [ "$status" != "ONLINE" ]; then + echo "APC UPS is offline. Sending email notification." + + # Send email using ssmtp + { + echo "To: $EMAIL_TO" + echo "From: $SENDER_ADDRESS" + echo "Subject: $EMAIL_SUBJECT" + echo + echo "NA-PA-01 UPS is offline. Please check the status." + } | ssmtp -vvv $EMAIL_TO +else + echo "APC UPS is online." +fi \ No newline at end of file