In case you did not know, when power goes down and the UPS switches to battery power, the apcupsd service automatically triggers a shell script under /etc/apcupsd/onbattery . We will need to modify it to use our mail service and email the right email address.
nano /etc/apcupsd/onbattery #!/bin/bash # Variables MAIL_TO="[email protected]" LOG_FILE="/var/log/ups_manager.log" MAIL_BODY="/tmp/power_lost.html" SUBJ="Power LOST for `hostname`" # Create HTML email body cat > $MAIL_BODY << EOF <html> <body> <h2>Power was lost!</h2> <p><strong>Current APC status:</strong> <pre>`/usr/sbin/apcaccess status`</pre> <p><strong>Recent log file output:</strong> <pre>`tail -n 20 /var/log/ups_manager.log`</pre> <p>Your RPi script :)</p> </body> </html> EOF mutt -e 'set content_type="text/html"' \\ -s "$SUBJ" \\ "$MAIL_TO" \\ -a "$LOG_FILE" < "$MAIL_BODY" rm -f "$MAIL_BODY" # Remove our temporary file exit 0
- Once power is back on, another script under
/etc/apcupsd/offbatteryis executed. Located it and at the end of it, please add the following lines to clear the flag for our script. - Kudos to https://pieterbakker.com/using-mutt-to-send-html-emails-with-attachments/ for the suggestions on how to use HTML format with mutt.
nano /etc/apcupsd/offbattery #!/bin/bash # Variables MAIL_TO="[email protected]" LOG_FILE="/var/log/ups_manager.log" MAIL_BODY="/tmp/power_restored.html" SUBJ="Power restored for `hostname`" # Clear the flags for future cases rm -f /tmp/ups_shutdown_flags/*.flag # Create HTML email body cat > $MAIL_BODY << EOF <html> <body> <h2>Good news, power was restored!</h2> <p><strong>APC status just after restoration:</strong> <pre>`/usr/sbin/apcaccess status`</pre> <p><strong>Recent log file output:</strong> <pre>`tail -n 20 /var/log/ups_manager.log`</pre> <p>Your RPi script :)</p> </body> </html> EOF mutt -e 'set content_type="text/html"' \\ -s "$SUBJ" \\ "$MAIL_TO" \\ -a "$LOG_FILE" < "$MAIL_BODY" rm -f "$MAIL_BODY" # Remove our temporary file exit 0
- Make the scripts executable and test them:
sudo chmod +x /usr/local/sbin/ups_manager.sh sudo chmod +x /etc/apcupsd/onbattery sudo chmod +x /etc/apcupsd/offbattery # Calling these two should result in an email being sent. bash /etc/apcupsd/onbattery bash /etc/apcupsd/offbattery # Should show you that the UPS is connected to power and no further action is needed. bash /usr/local/sbin/ups_manager.sh
- Schedule it to run every two minutes:
sudo crontab -e # Add this line: */2 * * * * /usr/local/sbin/ups_manager.sh
- Note that logs will be saved under
/var/log/ups_manager.login addition to the email alerts. At this point, if the power goes down, you will receive an email and the script will trigger further to eventually start gracefully shutting down your Proxmox hosts. - One more thing, we need to ensure that the log gets rotated, as it will be getting updates every two minutes and will clog up quickly. Let’s use
logrotatefor that.
sudo nano /etc/logrotate.d/ups_manager
/var/log/ups_manager.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 644 root root
}
# Test it
sudo logrotate -d /etc/logrotate.conf
Are we finally ready for a test? Yes! But how about we configure a script also for our existing monitoring system, such as Uptime Kuma, first?

