This commit is contained in:
Josh S 2023-08-18 18:17:33 -04:00 committed by GitHub
commit 092bb6fc0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 183 additions and 0 deletions

92
README.md Normal file
View file

@ -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`

33
scripts/critlowbatt.sh Normal file
View file

@ -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

33
scripts/lowbatt.sh Normal file
View file

@ -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

25
scripts/onbatt.sh Normal file
View file

@ -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