From d2e4bf2f35d216f46b9b2820fa248260545c5047 Mon Sep 17 00:00:00 2001 From: DIVISIONSolar Date: Fri, 18 Aug 2023 18:58:05 -0400 Subject: [PATCH] make it so the cron doesn't spam you --- README.md | 95 +---------------------- email/README.md | 94 ++++++++++++++++++++++ {scripts => email/scripts}/critlowbatt.sh | 23 +++++- {scripts => email/scripts}/lowbatt.sh | 25 +++++- {scripts => email/scripts}/onbatt.sh | 0 webhook/scripts/critlowbatt.sh | 65 ++++++++++++++++ webhook/scripts/lowbatt.sh | 65 ++++++++++++++++ webhook/scripts/onbatt.sh | 31 ++++++++ 8 files changed, 297 insertions(+), 101 deletions(-) create mode 100644 email/README.md rename {scripts => email/scripts}/critlowbatt.sh (57%) rename {scripts => email/scripts}/lowbatt.sh (56%) rename {scripts => email/scripts}/onbatt.sh (100%) create mode 100644 webhook/scripts/critlowbatt.sh create mode 100644 webhook/scripts/lowbatt.sh create mode 100644 webhook/scripts/onbatt.sh diff --git a/README.md b/README.md index 54a2561..341fb67 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file diff --git a/email/README.md b/email/README.md new file mode 100644 index 0000000..c281af7 --- /dev/null +++ b/email/README.md @@ -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` diff --git a/scripts/critlowbatt.sh b/email/scripts/critlowbatt.sh similarity index 57% rename from scripts/critlowbatt.sh rename to email/scripts/critlowbatt.sh index 230e3d8..f90507f 100644 --- a/scripts/critlowbatt.sh +++ b/email/scripts/critlowbatt.sh @@ -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 \ No newline at end of file + echo "UPS battery is above $threshold% or email was sent recently. No action needed." +fi diff --git a/scripts/lowbatt.sh b/email/scripts/lowbatt.sh similarity index 56% rename from scripts/lowbatt.sh rename to email/scripts/lowbatt.sh index d12d717..96663b2 100644 --- a/scripts/lowbatt.sh +++ b/email/scripts/lowbatt.sh @@ -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 \ No newline at end of file + echo "UPS battery is above $threshold% or email was sent recently. No action needed." +fi diff --git a/scripts/onbatt.sh b/email/scripts/onbatt.sh similarity index 100% rename from scripts/onbatt.sh rename to email/scripts/onbatt.sh diff --git a/webhook/scripts/critlowbatt.sh b/webhook/scripts/critlowbatt.sh new file mode 100644 index 0000000..4f60ad7 --- /dev/null +++ b/webhook/scripts/critlowbatt.sh @@ -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 diff --git a/webhook/scripts/lowbatt.sh b/webhook/scripts/lowbatt.sh new file mode 100644 index 0000000..099e751 --- /dev/null +++ b/webhook/scripts/lowbatt.sh @@ -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 diff --git a/webhook/scripts/onbatt.sh b/webhook/scripts/onbatt.sh new file mode 100644 index 0000000..d1f72d7 --- /dev/null +++ b/webhook/scripts/onbatt.sh @@ -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