Use Gotify with NUT Server
📆 2024-07-08 13:43
We setup Nut Server and Gotify in our home lab:
🔌 Installing and configuring NUT (Network UPS Tools) for your home lab
Gotify - Get notifications about everything on your phone
It's time to mix them together and get notified by Gotify whenever something happens with the UPS. This tutorial applies if you followed the tutorials linked above. If you have another NUT setup make sure to adapt to it.
Sending Gotify notifications from NUT server is a very simple process. All you have to do is to edit /bin/upssched-cmd.
Let's create the function to send notifications via Gotify. Add to /bin/upssched-cmd:
CURL="/usr/bin/curl --retry 3 --retry-delay 5 --retry-max-time 20"
GOTIFY_URL="https://your.gotify.url/message?token=<apptoken>"
HOSTNAME="$(hostname)"
send_gotify() {
if [ -n "$title" ] && [ -n "$message" ]; then
$CURL -fsS -F "title=$title" -F "message=$message" "$GOTIFY_URL"
fi
At the bottom of the file make sure to call the function.
What this does
If the title and message parameters are present the function will execute the curl command. If you now edit a case in /bin/upssched-cmd and add the title and message parameters like:
onbatt)
logger -t upssched-cmd "UPS running on battery. Battery is now discharging."
title="$HOSTNAME - UPS on Battery"
message="UPS is now running on battery. Battery is discharging."
;;
/bin/upssched-cmd will trigger the gotify notification if the UPS is on battery power.
Here's the full script from the connected PC to the UPS
#!/bin/sh
CURL="/usr/bin/curl --retry 3 --retry-delay 5 --retry-max-time 20"
GOTIFY_URL="https://your.gotify.url/message?token=<apptoken>"
HOSTNAME="$(hostname)"
send_gotify() {
if [ -n "$title" ] && [ -n "$message" ]; then
$CURL -fsS -F "title=$title" -F "message=$message" "$GOTIFY_URL"
fi
}
case $1 in
onbatt)
logger -t upssched-cmd "UPS running on battery. Battery is now discharging."
title="$HOSTNAME - UPS on Battery"
message="UPS is now running on battery. Battery is discharging."
;;
onpower)
logger -t upssched-cmd "UPS running on mains. Battery is now charging."
title="$HOSTNAME - Power Restored"
message="UPS is back on mains. Battery is charging."
;;
mute_beeper)
upscmd -u sava -p uliannina1986 apc@192.168.1.2 beeper.mute
logger -t upssched-cmd "1 minute limit exceeded, muting beeper"
title="$HOSTNAME - Beeper Muted"
message="Beeper muted after 1 minute on battery."
;;
commbad_timer)
logger -t upssched-cmd "UPS communication failure persists, initiating shutdown"
title="$HOSTNAME - UPS Communication Failure"
message="Lost connection to UPS. Shutting down in 60 seconds!"
/usr/sbin/upsmon -c fsd
;;
commbad_shutdown)
logger -t upssched-cmd "UPS communication failed! Shutting down!"
title="$HOSTNAME - UPS Communication Failure"
message="UPS communication failed for 60 seconds. Shutting down!"
/sbin/upsmon -c fsd
;;
critical_shutdown)
logger -t upssched-cmd "Battery critical. System shutting down now."
title="$HOSTNAME - Critical Shutdown"
message="Battery critical. System shutting down now."
/sbin/upsmon -c fsd
;;
powerdown)
logger -t upssched-cmd "Executing powerdown command"
title="$HOSTNAME - Powerdown"
message="System is executing powerdown now."
;;
*)
logger -t upssched-cmd "Unrecognized command: $1"
title="$HOSTNAME - Unknown Command"
message="Received unknown UPS command: $1"
;;
esac
send_gotify
You can also find the full config for NUT server and client on my GitHub repository:
Nut Config on GitHub/SavaAlexandruStefan
🚶 Back to my blog