Post

Discord Alerts from NUT (network UPS tools)

Intro

I’ve been using NUT to manage and monitor my non-networked UPS and even one using SNMP for some time but never had any good alert method. Recently thought what if I could get discord alerts? I did some googling and found a script that pushes text to a webhook.

Setup

This is the script

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh
#
# Sends a message to your discord webhook
# Requires a string parameter to be passed in ($1)
#

# Supply your personal discord bot webhook
discordwebhookurl="YOUR_DISCORD_WEBHOOK_HERE"

curl -H "Content-Type: application/json" -X POST -d '{"content":"'"${1}"'"}' $discordwebhookurl
exit 0

Link to source thanks to @bp1313 on github

In the channel of your choice create a webhook under channel settings -> integrations. Copy the webhook URL and place it in the right place in the script.

Put the script somewhere on your NUT server and chmod it so its executable. I set the owner as root and group to nut and mode 0750.

I chose to put my script in /usr/local/bin

Make sure your nut server has curl installed (for debian sudo apt install curl)

Edit /etc/nut/upsmon.conf

In the section on notifycmd..

1
2
3
# Example:
# NOTIFYCMD /bin/notifyme
NOTIFYCMD /usr/local/bin/nut-notifydiscord

Then further down under the notifyflag section:

you’ll want to uncomment all the things you want alerts for and add +EXEC to them so it will call your script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# --------------------------------------------------------------------------
# NOTIFYFLAG - change behavior of upsmon when NOTIFY events occur
#
# By default, upsmon sends walls (global messages to all logged in users)
# and writes to the syslog when things happen.  You can change this.
#
# NOTIFYFLAG <notify type> <flag>[+<flag>][+<flag>] ...
#
NOTIFYFLAG ONLINE       SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT       SYSLOG+WALL+EXEC
NOTIFYFLAG LOWBATT      SYSLOG+WALL+EXEC
NOTIFYFLAG FSD  SYSLOG+WALL+EXEC
NOTIFYFLAG COMMOK       SYSLOG+WALL+EXEC
NOTIFYFLAG COMMBAD      SYSLOG+WALL+EXEC
NOTIFYFLAG SHUTDOWN     SYSLOG+WALL+EXEC
NOTIFYFLAG REPLBATT     SYSLOG+WALL+EXEC
NOTIFYFLAG NOCOMM       SYSLOG+WALL+EXEC
NOTIFYFLAG NOPARENT     SYSLOG+WALL+EXEC

save upsmon.conf

restart nut-monitor service

1
sudo service nut-monitor restart

I tested mine by pulling the usb cable out on my UPS and waiting a few seconds. I then got a ‘comms lost’ alert in my discord. After reconnecting it got the ‘comms restored’ message as well.

This post is licensed under CC BY 4.0 by the author.