Make the onbatt script show power % and time left
This commit is contained in:
parent
d2e4bf2f35
commit
c19a4b49dd
2 changed files with 65 additions and 22 deletions
|
|
@ -7,6 +7,9 @@ SENDER_ADDRESS=""
|
||||||
|
|
||||||
# Check the UPS status using the apcaccess command
|
# Check the UPS status using the apcaccess command
|
||||||
status=$(apcaccess status 2>/dev/null | grep STATUS | awk '{print $3}')
|
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
|
# Check if UPS is offline and send an email if necessary
|
||||||
if [ "$status" != "ONLINE" ]; then
|
if [ "$status" != "ONLINE" ]; then
|
||||||
|
|
@ -18,7 +21,8 @@ if [ "$status" != "ONLINE" ]; then
|
||||||
echo "From: $SENDER_ADDRESS"
|
echo "From: $SENDER_ADDRESS"
|
||||||
echo "Subject: $EMAIL_SUBJECT"
|
echo "Subject: $EMAIL_SUBJECT"
|
||||||
echo
|
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
|
} | ssmtp -vvv $EMAIL_TO
|
||||||
else
|
else
|
||||||
echo "APC UPS is online."
|
echo "APC UPS is online."
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,70 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Replace these variables with your actual values
|
DISCORD_WEBHOOK_URLS=(
|
||||||
DISCORD_WEBHOOK_URL=""
|
"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
|
# Grab the hostname
|
||||||
send_discord_webhook() {
|
ups_hostname=$(hostname)
|
||||||
local messages=("$@")
|
|
||||||
local message_data=""
|
|
||||||
|
|
||||||
for message in "${messages[@]}"; do
|
# Create the webhook and stuff
|
||||||
message_data+="[$message] \n"
|
create_discord_payload() {
|
||||||
done
|
local battery_percentage="$1"
|
||||||
|
local remaining_runtime="$2"
|
||||||
|
|
||||||
curl -X POST -H "Content-Type: application/json" -d "{\"embeds\":[{\"title\":\"APC UPS Alert\",\"description\":\"$message_data\",\"color\":16711680}]}" $DISCORD_WEBHOOK_URL
|
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}')
|
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
|
if [ "$status" != "ONLINE" ]; then
|
||||||
echo "APC UPS is offline. Sending Discord webhook notification."
|
echo "APC UPS is offline. Sending Discord webhook notifications."
|
||||||
|
create_discord_payload "$battery_percentage" "$remaining_runtime"
|
||||||
# Send a single webhook message with multiple lines
|
send_discord_webhook "$payload"
|
||||||
send_discord_webhook \
|
|
||||||
"Power Outage Detected." \
|
|
||||||
"NA-PA-01 UPS is offline. Please check the status."
|
|
||||||
else
|
else
|
||||||
echo "APC UPS is online."
|
echo "APC UPS is online."
|
||||||
fi
|
fi
|
||||||
Loading…
Add table
Reference in a new issue