Make the onbatt script show power % and time left

This commit is contained in:
DIVISIONSolar 2023-08-19 15:20:27 -04:00
parent d2e4bf2f35
commit c19a4b49dd
2 changed files with 65 additions and 22 deletions

View file

@ -7,6 +7,9 @@ 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 offline and send an email if necessary
if [ "$status" != "ONLINE" ]; then
@ -18,7 +21,8 @@ if [ "$status" != "ONLINE" ]; then
echo "From: $SENDER_ADDRESS"
echo "Subject: $EMAIL_SUBJECT"
echo
echo "NA-PA-01 UPS is offline. Please check the status."
echo "$ups_hostname is offline. Please check the status."
echo "Power: $battery_percentage % && Remaining Time: $remaining_runtime Minutes"
} | ssmtp -vvv $EMAIL_TO
else
echo "APC UPS is online."

View file

@ -1,31 +1,70 @@
#!/bin/bash
# Replace these variables with your actual values
DISCORD_WEBHOOK_URL=""
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
#
####
)
# 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
# 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 Outage Detected.",
"description": "'"$ups_hostname"' UPS is offline. Please check the status.",
"color": 16711680,
"fields": [
{
"name": "Current Level",
"value": "'"$battery_percentage"'%",
"inline": true
},
{
"name": "Estimated Time Remaining",
"value": "'"$remaining_runtime"' Minutes",
"inline": true
}
]
}
]
}'
}
# Check the UPS status using the apcaccess command
# 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 offline and send a single Discord webhook message if necessary
# Check if UPS is offline and send webhook messages if its offline
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."
echo "APC UPS is offline. Sending Discord webhook notifications."
create_discord_payload "$battery_percentage" "$remaining_runtime"
send_discord_webhook "$payload"
else
echo "APC UPS is online."
fi
fi