add online alert
This commit is contained in:
parent
6aa724b8b0
commit
1ee42cbb45
2 changed files with 100 additions and 0 deletions
30
email/scripts/onpower.sh
Normal file
30
email/scripts/onpower.sh
Normal file
|
|
@ -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
|
||||
70
webhook/scripts/onpower.sh
Normal file
70
webhook/scripts/onpower.sh
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue