NUT Email Notifications

Not too long ago, I posted the Raspberry Pi NUT server. This post is somewhat related to it. I am going to add email notification using msmtp. An assumption with this post: Gmail account that is configured with app password. It is probably a good idea to create a new account just for notification and not use your primary account.

To install msmtp on a RPi check this post and see the Debian section. Once msmtp has been installed, test the email make sure you’ll get email notified. If you received the test email then it means it is working.

echo "Subject: Test from the nut-server" | msmtp [email protected]

Now, to configure the NUT server to send email notifications, we need to edit the upsmon.conf and create a new file called notifycmd in the /etc/nut/. There are two pieces here to do with the upsmon.conf file. The type of notification needs to be uncommented and the path to get to notifycmd file.

Adding the +EXEC to the third column will execute the NOTIFYCMD.

sudo tee -a /etc/nut/upsmon.conf <<EOF

# Email script for NOTIFYCMD
NOTIFYCMD "/etc/nut/notifycmd.sh"

# Notification events
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

EOF

Create a bash script that will trigger the email when it is called by the NOTIFYCMD. The $NOTIFYTYPE and $UPSNAME are the environment variables. You can see them in the upsmon man page.

sudo tee -a /etc/nut/notifycmd.sh <<EOF
#!/bin/bash
EMAIL='[email protected]'
echo -e "Subject: UPS ALERT: $NOTIFYTYPE\n\nUPS: $UPSNAME\r\nAlert type: $NOTIFYTYPE\n\n\nups1: network\nups2: system\nups3: darktower" | msmtp $EMAIL
EOF

Change the group and add execution to the file notifycmd.sh. Once done, restart NUT.

# Change group to nut
sudo chown :nut /etc/nut/notifycmd.sh

# Add execution
sudo chmod 774 /etc/nut/notifycmd.sh

# Restart the NUT services
sudo systemctl restart nut-server.service
sudo systemctl restart nut-driver.service
sudo systemctl restart nut-monitor.service

That is it. Cheers!

Subscribe
Notify of
guest

6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dana
Dana
2 years ago

Perfect – thanks very much for this.

KevinConnolly
KevinConnolly
1 year ago

Just one quick question: Above you wrote…”\n\n\nups1: network\nups2: system\nups3: darktower” When I did a pull the plug test the email sent had the alert for ONBATT but had empty field for nups1, nups2, and nups3 “darktower” . What are these field for exactly?

Thanos Fonias
Thanos Fonias
8 months ago

Thank you for the guide but in my case the notif script does not execute and don’t know how to troubleshoot it..

Thanos
Thanos
8 months ago

Thank you for the guide but in my case the notif script does not execute and don’t know how to troubleshoot it..

Momo Sampler
Momo Sampler
Reply to  Thanos
4 months ago

Great article!!! Thanks!

Thanos!
Get rid of the quotes in:
NOTIFYCMD “/etc/nut/notifycmd.sh”

It worked for me that way!
I modified the sh for sending me telegram messages with a bot.

Good luck!

6
0
Would love your thoughts, please comment.x
()
x