From 1ee42cbb4577d89dea9c028c3d2e9925b85b72aa Mon Sep 17 00:00:00 2001 From: DIVISIONSolar Date: Sun, 20 Aug 2023 21:47:16 -0400 Subject: [PATCH] add online alert --- email/scripts/onpower.sh | 30 ++++++++++++++++ webhook/scripts/onpower.sh | 70 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 email/scripts/onpower.sh create mode 100644 webhook/scripts/onpower.sh diff --git a/email/scripts/onpower.sh b/email/scripts/onpower.sh new file mode 100644 index 0000000..5845b2a --- /dev/null +++ b/email/scripts/onpower.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# Replace these variables with your actual values +EMAIL_TO="" +EMAIL_SUBJECT="Power Restored." +SENDER_ADDRESS="" + +# Check the UPS status using the apcaccess command +status=$(apcaccess status 2>/dev/null | grep STATUS | awk '{print $3}') +battery_percentage=$(apcaccess status 2>/dev/null | awk '/BCHARGE/ {print $3}') +remaining_runtime=$(apcaccess status 2>/dev/null | awk '/TIMELEFT/ {print $3}') +ups_hostname=$(hostname) + +# Check if UPS is online and send an email if necessary +if [ "$status" != "ONLINE" ]; then + echo "APC UPS is online. Sending email notification." + + # Send email using ssmtp + { + echo "To: $EMAIL_TO" + echo "From: $SENDER_ADDRESS" + echo "Subject: $EMAIL_SUBJECT" + echo + echo "$ups_hostname is back online." + echo "Current Level: $battery_percentage%" + echo "Estimated time remaining: $remaining_runtime" + } | ssmtp -vvv $EMAIL_TO +else + echo "APC UPS is online." +fi \ No newline at end of file diff --git a/webhook/scripts/onpower.sh b/webhook/scripts/onpower.sh new file mode 100644 index 0000000..54e9d4f --- /dev/null +++ b/webhook/scripts/onpower.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +DISCORD_WEBHOOK_URLS=( + "https://discord.com/api/webhooks/" + "https://discord.com/api/webhooks/" + #### + # + # TO ADD MORE LINKS JUST DO THIS: + # "link #1" + # "link #2" + # "link #3" + # + # AND CHANGE TO THE REAL VALUES + # + #### +) + +# Grab the hostname +ups_hostname=$(hostname) + +# Create the webhook and stuff +create_discord_payload() { + local battery_percentage="$1" + local remaining_runtime="$2" + + payload='{ + "embeds": [ + { + "title": "Power Restored.", + "description": "'"$ups_hostname"' UPS is back online.", + "color": 16711680, + "fields": [ + { + "name": "Current Level", + "value": "'"$battery_percentage"'%", + "inline": true + }, + { + "name": "Estimated Time Remaining", + "value": "'"$remaining_runtime"' Minutes", + "inline": true + } + ] + } + ] + }' +} + +# This sends the webhook +send_discord_webhook() { + local payload="$1" + + for webhook_url in "${DISCORD_WEBHOOK_URLS[@]}"; do + curl -H "Content-Type: application/json" -d "$payload" "$webhook_url" + done +} + +# Check the UPS status [Don't change this! >:( ] +status=$(apcaccess status 2>/dev/null | grep STATUS | awk '{print $3}') +battery_percentage=$(apcaccess status 2>/dev/null | awk '/BCHARGE/ {print $3}') +remaining_runtime=$(apcaccess status 2>/dev/null | awk '/TIMELEFT/ {print $3}') + +# Check if UPS is online and send webhook messages if its online +if [ "$status" != "OFFLINE" ]; then + echo "APC UPS is back online. Sending Discord webhook notifications." + create_discord_payload "$battery_percentage" "$remaining_runtime" + send_discord_webhook "$payload" +else + echo "APC UPS is online." +fi \ No newline at end of file