make it so the cron doesn't spam you

This commit is contained in:
DIVISIONSolar 2023-08-18 18:58:05 -04:00
parent dad1dcdc73
commit d2e4bf2f35
8 changed files with 297 additions and 101 deletions

View file

@ -1,94 +1 @@
# 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! (it'll only send the email if the UPS has no power!)
# Setting up the battery alerts
1. Run these commands `nano /etc/apcupsd/scripts/lowbatt.sh` and fill it with the desired contents (do the same with the /etc/apcupsd/scripts/critlowbatt.sh).
2. Make sure to grant the right perms for the scripts: `chmod +x /etc/apcupsd/scripts/lowbatt.sh && chmod +x /etc/apcupsd/scripts/critlowbatt.sh`
3. 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`
# For info on how to setup and such click on the method you want. (email or discord webhook)

94
email/README.md Normal file
View file

@ -0,0 +1,94 @@
# 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! (it'll only send the email if the UPS has no power!)
# Setting up the battery alerts
1. Run these commands `nano /etc/apcupsd/scripts/lowbatt.sh` and fill it with the desired contents (do the same with the /etc/apcupsd/scripts/critlowbatt.sh).
2. Make sure to grant the right perms for the scripts: `chmod +x /etc/apcupsd/scripts/lowbatt.sh && chmod +x /etc/apcupsd/scripts/critlowbatt.sh`
3. 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`

View file

@ -4,6 +4,7 @@
EMAIL_TO=""
EMAIL_SUBJECT="UPS Battery Critically Low"
SENDER_ADDRESS=""
TIMESTAMP_FILE="/tmp/last_email_sent_timestamp_critically_low"
# Check the UPS battery percentage using the apcaccess command
battery_percentage=$(apcaccess status 2>/dev/null | awk '/BCHARGE/ {print $3}')
@ -11,8 +12,21 @@ battery_percentage=$(apcaccess status 2>/dev/null | awk '/BCHARGE/ {print $3}')
# Define the threshold as a floating-point number
threshold=30.0
# Get the current timestamp
current_timestamp=$(date +%s)
# Read the last email sent timestamp from the file (if it exists)
if [ -f "$TIMESTAMP_FILE" ]; then
last_email_timestamp=$(cat "$TIMESTAMP_FILE")
else
last_email_timestamp=0
fi
# Calculate the time elapsed since the last email
time_elapsed=$((current_timestamp - last_email_timestamp))
# Compare battery percentage with the threshold using bc
if (( $(echo "$battery_percentage <= $threshold" | bc -l) )); then
if (( $(echo "$battery_percentage <= $threshold" | bc -l) )) && [ "$time_elapsed" -ge 1800 ]; then
echo "UPS battery is at or below $threshold%. Sending email alert."
# Get estimated remaining runtime
@ -28,6 +42,9 @@ if (( $(echo "$battery_percentage <= $threshold" | bc -l) )); then
echo "Current Level: $battery_percentage%"
echo "Estimated time remaining: $remaining_runtime"
} | ssmtp -vvv $EMAIL_TO
# Update the last email sent timestamp
echo "$current_timestamp" > "$TIMESTAMP_FILE"
else
echo "UPS battery is above $threshold%. No action needed."
fi
echo "UPS battery is above $threshold% or email was sent recently. No action needed."
fi

View file

@ -3,7 +3,8 @@
# Replace these variables with your actual values
EMAIL_TO=""
EMAIL_SUBJECT="UPS Battery Low"
SENDER_ADDRESS=""
SENDER_ADDRESS=""
TIMESTAMP_FILE="/tmp/last_email_sent_timestamp"
# Check the UPS battery percentage using the apcaccess command
battery_percentage=$(apcaccess status 2>/dev/null | awk '/BCHARGE/ {print $3}')
@ -11,8 +12,21 @@ battery_percentage=$(apcaccess status 2>/dev/null | awk '/BCHARGE/ {print $3}')
# Define the threshold as a floating-point number
threshold=75.0
# Get the current timestamp
current_timestamp=$(date +%s)
# Read the last email sent timestamp from the file (if it exists)
if [ -f "$TIMESTAMP_FILE" ]; then
last_email_timestamp=$(cat "$TIMESTAMP_FILE")
else
last_email_timestamp=0
fi
# Calculate the time elapsed since the last email
time_elapsed=$((current_timestamp - last_email_timestamp))
# Compare battery percentage with the threshold using bc
if (( $(echo "$battery_percentage <= $threshold" | bc -l) )); then
if (( $(echo "$battery_percentage <= $threshold" | bc -l) )) && [ "$time_elapsed" -ge 1800 ]; then
echo "UPS battery is at or below $threshold%. Sending email alert."
# Get estimated remaining runtime
@ -28,6 +42,9 @@ if (( $(echo "$battery_percentage <= $threshold" | bc -l) )); then
echo "Current Level: $battery_percentage%"
echo "Estimated time remaining: $remaining_runtime"
} | ssmtp -vvv $EMAIL_TO
# Update the last email sent timestamp
echo "$current_timestamp" > "$TIMESTAMP_FILE"
else
echo "UPS battery is above $threshold%. No action needed."
fi
echo "UPS battery is above $threshold% or email was sent recently. No action needed."
fi

View file

@ -0,0 +1,65 @@
#!/bin/bash
# Replace this with your actual Discord webhook URL
DISCORD_WEBHOOK_URL="YOUR_DISCORD_WEBHOOK_URL"
# Replace these variables with your actual values
TIMESTAMP_FILE="/tmp/last_message_sent_timestamp_critically_low"
# 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
# Get the current timestamp
current_timestamp=$(date +%s)
# Read the last message sent timestamp from the file (if it exists)
if [ -f "$TIMESTAMP_FILE" ]; then
last_message_timestamp=$(cat "$TIMESTAMP_FILE")
else
last_message_timestamp=0
fi
# Calculate the time elapsed since the last message
time_elapsed=$((current_timestamp - last_message_timestamp))
# Compare battery percentage with the threshold using bc
if (( $(echo "$battery_percentage <= $threshold" | bc -l) )) && [ "$time_elapsed" -ge 1800 ]; then
echo "UPS battery is at or below $threshold%. Sending message to Discord."
# Get estimated remaining runtime
remaining_runtime=$(apcaccess status 2>/dev/null | awk '/TIMELEFT/ {print $3}')
# Prepare the JSON payload for the embedded message in Discord
discord_payload='{
"embeds": [
{
"title": "Critical UPS Battery Alert",
"description": "UPS battery is critically low at or below '"$threshold"'%",
"color": 16711680,
"fields": [
{
"name": "Current Level",
"value": "'"$battery_percentage"'%",
"inline": true
},
{
"name": "Estimated Time Remaining",
"value": "'"$remaining_runtime"'",
"inline": true
}
]
}
]
}'
# Send embedded message to Discord using curl
curl -H "Content-Type: application/json" -d "$discord_payload" "$DISCORD_WEBHOOK_URL"
# Update the last message sent timestamp
echo "$current_timestamp" > "$TIMESTAMP_FILE"
else
echo "UPS battery is above $threshold% or message was sent recently. No action needed."
fi

View file

@ -0,0 +1,65 @@
#!/bin/bash
# Replace this with your actual Discord webhook URL
DISCORD_WEBHOOK_URL="YOUR_DISCORD_WEBHOOK_URL"
# Replace these variables with your actual values
TIMESTAMP_FILE="/tmp/last_message_sent_timestamp"
# 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
# Get the current timestamp
current_timestamp=$(date +%s)
# Read the last message sent timestamp from the file (if it exists)
if [ -f "$TIMESTAMP_FILE" ]; then
last_message_timestamp=$(cat "$TIMESTAMP_FILE")
else
last_message_timestamp=0
fi
# Calculate the time elapsed since the last message
time_elapsed=$((current_timestamp - last_message_timestamp))
# Compare battery percentage with the threshold using bc
if (( $(echo "$battery_percentage <= $threshold" | bc -l) )) && [ "$time_elapsed" -ge 1800 ]; then
echo "UPS battery is at or below $threshold%. Sending message to Discord."
# Get estimated remaining runtime
remaining_runtime=$(apcaccess status 2>/dev/null | awk '/TIMELEFT/ {print $3}')
# Prepare the JSON payload for the embedded message in Discord
discord_payload='{
"embeds": [
{
"title": "UPS Battery Alert",
"description": "UPS battery is at or below '"$threshold"'%",
"color": 16711680,
"fields": [
{
"name": "Current Level",
"value": "'"$battery_percentage"'%",
"inline": true
},
{
"name": "Estimated Time Remaining",
"value": "'"$remaining_runtime"'",
"inline": true
}
]
}
]
}'
# Send embedded message to Discord using curl
curl -H "Content-Type: application/json" -d "$discord_payload" "$DISCORD_WEBHOOK_URL"
# Update the last message sent timestamp
echo "$current_timestamp" > "$TIMESTAMP_FILE"
else
echo "UPS battery is above $threshold% or message was sent recently. No action needed."
fi

31
webhook/scripts/onbatt.sh Normal file
View file

@ -0,0 +1,31 @@
#!/bin/bash
# Replace these variables with your actual values
DISCORD_WEBHOOK_URL=""
# Function to send a Discord webhook message
send_discord_webhook() {
local messages=("$@")
local message_data=""
for message in "${messages[@]}"; do
message_data+="[$message] \n"
done
curl -X POST -H "Content-Type: application/json" -d "{\"embeds\":[{\"title\":\"APC UPS Alert\",\"description\":\"$message_data\",\"color\":16711680}]}" $DISCORD_WEBHOOK_URL
}
# 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 a single Discord webhook message if necessary
if [ "$status" != "ONLINE" ]; then
echo "APC UPS is offline. Sending Discord webhook notification."
# Send a single webhook message with multiple lines
send_discord_webhook \
"Power Outage Detected." \
"NA-PA-01 UPS is offline. Please check the status."
else
echo "APC UPS is online."
fi