OPNsense Forum

English Forums => Tutorials and FAQs => Topic started by: gfeiner on May 14, 2021, 03:04:04 am

Title: HOWTO - UPS Status Email Alerts with Monit and apcupsd
Post by: gfeiner on May 14, 2021, 03:04:04 am
If you use the apcupsd package and you want to receive email alerts when the status of your UPS changes, then read on.  The information presented here will allow to you receive an email alert anytime the UPS status is anything other than "ONLINE".  This howto could be applied to any of the information presented by the apcupsd's apcaccess command.  The steps I describe below were performed on OPNsense v21.1.5

Install the apcupsd package:
My UPS is a APC BackUps Pro 1350, connected via USB cable to Opnsense. The portions of apcupsd.conf that I set are:


Next you need a shell script that queries the UPS using the apcaccess command and then passes that information to Monit. The script code below makes two calls to apcaccess. The first call is just to gather all output from the apcaccess command so it will be passed to Monit. The 2nd call to apcaccess parses the output for the value of the STATUS line. The rest of the code checks the value of the STATUS line from the apcaccess output and if it is anything other than ONLINE, the script will return non-zero status (failure) to Monit, otherwise it returns zero (OK).
Code: [Select]
#!/bin/sh

/usr/local/sbin/apcaccess
STATUS=`/usr/local/sbin/apcaccess -p STATUS`
OK='ONLINE'
if [ $STATUS != $OK ]; then
echo $STATUS
exit 1
else
exit 0
fi

Now we need to configure Monit. The first step is to configure your email account information:

Next we need to configure Monit to send a email notification anytime there is a failure status:

Finally we need to tie the apcaccess status script in to Monit:


Everything should be working at this point. Navigate to Services->Monit->Status. After the poll period on the General Settings tab has passed, you should see the output from the apcaccess command.  Please note that how quickly you are notified of UPS status changes depends on your Monit Polling Interval and the apcupsd polling interval.