UPS Status Email Alerts with Monit and apcupsd

  Linux admin, OPNSense

скопировано отсюда

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:

  • From shell, run “pkg install apcupsd”
  • Modify /usr/local/etc/apcupsd/apcupsd.conf as appropriate for your UPS model
  • Create the file /etc/rc.conf.d/apcupsd and put this line in it: pcupsd_enable=”YES”
  • From shell, run “service apcupsd start”
  • From shell, run “service apcupsd status”. This should result in saying that apcupsd is running
  • From shell, run “apcaccess”. If your apcupsd conf file is correct, you should get output regarding your UPS with the STATUS line saying “ONLINE”

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

UPSNAME APC1350S
UPSCABLE smart
UUPSTYPE usb
I left DEVICE blank
UPOLLTIME 60
ONBATTERYDELAY 6
BATTERYLEVEL 10
MINUTES 5

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).

#!/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:

  • Navigate to Services->Monit->Settings
  • Click on the General Settings tab
  • Enter your email server/account information
  • Select “Enable Monit”
  • Click Save

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

  • Click on the Alert Setting tab
  • Click the plus sign to add a new alert
  • Select Enable Alert
  • Place the email address you want to receive the alert at in the Recipient box
  • In the Events dropdown, select “Status failed”
  • In the “Mail format” box put “From: xxx” where xxx is the email address where the message will originate (the same email account you put in the General Settings tab
  • Click Save

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

  • Click on the Service Settings tab
  • Click the plus sign
  • Select “Enable service checks”
  • Set Name to something descriptive like UPSStatusCheck
  • Set Type to Custom
  • In the Path box put: /usr/local/opnsense/scripts/OPNsense/Monit/xxx where xxx is the name of your script file
  • Leave Start and Stop blank
  • For Test select NonZeroStatus
  • Leave Depends set to NothingSelected
  • Click Save

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.