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!
Perfect – thanks very much for this.