Apcupsd & email

Hello,
I would like to know how I can get the apcupsd to send me an email in case of network failure.

The webinterface consists only of one entry under Packfire and one entry under Status Information.

An extra window shows the current status of the UPS.

I did not find anything in the Wiki either.

Thank you.

Most configuration has to be done from the shell.
See:

APC UPS Daemon manual

and

Hello,

different from APCupsd but for event of a network failure (and not power failure), you can use WIO (Who Is Online) which can warn if a change of state of a network device…

Email Notification of Events

It is possible to receive email notification of apcupsd events using some simple Visual Basic scripts contributed by Ed Dondlinger edondlinger@thepylegroup.com. The scripts are automatically installed in the etc/apcupsd directory of your apcupsd installation but are disabled by default. To enable them, first open them in a text editor such as Notepad and edit the USER VARIABLES section to set your email preferences including address, server information, etc. Then rename the script files without the *.example suffix. Scripts are supplied for onbattery, offbattery, and commfailure events. You can copy the scripts to other filenames and modify the email body text to respond to other events as described in Customizing Event Handling.

???

“the USER VARIABLES section” in which file?

The standard message scripts are written on the basis of a sendmail system being present. IPFire uses dma which requires a different format for the sending of the email.

I have been working on this for a while and have recently got a working process but have not yet had the time to update the wiki. That is next on my list of things to do.

In /etc/apcupsd/ there are the various communication scripts.

  • changeme
  • commfailure
  • commok
  • offbattery
  • onbattery

The following section in all of these

#
(
   echo "Subject: $MSG"
   echo " "
   echo "$MSG"
   echo " "
   /sbin/apcaccess status
) | $APCUPSD_MAIL -s "$MSG" $SYSADMIN
exit 0

needs to be changed to

FROM="your from email address"
TO="your to email address"
#
(
echo From: $FROM
echo To: $TO
echo Subject: $MSG
echo " "
echo "$MSG"
echo " "
echo -ne $(echo "\r\n"; while read line; do echo "$line\r\n"; done < <(echo "$(/sbin/apcaccess status)"))
) | sudo -u nobody /usr/sbin/sendmail -t -f $FROM
exit 0

The “your from email address” and “your to email address” need to be changed to whatever email addresses you are using. In my case I used the same as I have entered into the Mail Service page of IPFire but I believe different ones should also work but I have not tried that out.

You can then test that it works by pulling the usb cable out of the UPS and after a certain time the commfailure script will be run. Then if after a while you plug the usb cable back in the commok message will be sent.

4 Likes

The cited topic is from the chapter " The Windows Version of apcupsd"
so it doesn’t help in a linux environment.

Something I forgot to mention.

The Mail Service menu page on the IPFIre WUI needs to be filled out, enabled and pressing the “Send test email” button needs to result in a successful receipt of an email before trying the changes to the apcups scripts.

2 Likes

“Send test email” is OK.

I have to play a bit with the times somehow the IPFire shuts down before it sends the mail in case of network failure.

I always have to send 2 emails but that is not a problem for me.

“UPS 1 The power supply has failed!”
Ok I get the message.

What I still need is “UPS 1 IPFire shutdown!” so I know that the IPFire is shutting down.
And “UPS 1 IPFire online” when the IPFire is up again.

I have selected “failing” and “loadlimit”.
But I will test this on Monday.

So scripts to do that are not part of the apcupsd package. So you will have to figure out how to write those scripts and how to link them to the apccontrol script.

I would recommend going in detail through the apcupsd manual which can be found on their website. Look in the “Customizing Event Handling” section.

1 Like

Alternatively, you can use ‘monit’…

This requires the installation of ‘monit’ as an additional addon.

Best,
Matthias

2 Likes

Hello,

Thanks for all the help on this thread. It has helped me tremendously!

I’ve now cobbled together a set of scripts that I’m using for this. Thought it would be good to share them here. Might help someone else.

All I did was go to /etc/apcupsd/ and replace each of the scripts there. They all have obvious sounding names related to when they are called. The only difference between my scrips are the subject fields which I have customised to something related to each scrip’s name.

The example below is from the onbattery script.

Here is the code from my script:

#!/bin/sh

subject="IPFire UPS now running on batteries"

FROM="your@emailaddress.com"
TO="your@emailaddress.com"

apcaccess status > /var/log/apcupsd.status
time=`grep -a TIMELEFT /var/log/apcupsd.status | cut -f2 -d:`
statusdate=`grep -a "END APC" /var/log/apcupsd.status | cut -f2,3,4 -d:`
battery=`grep -a BCHARGE /var/log/apcupsd.status | cut -f2 -d:`
load=`grep -a LOADPCT /var/log/apcupsd.status | cut -f2 -d:`
voltage=`grep -a LINEV /var/log/apcupsd.status | cut -f2 -d:`
timeonbattery=`grep -a TONBATT /var/log/apcupsd.status | cut -f2 -d:`
systemdate=`date "+%a, %e.%m.%Y %H:%M:%S"`

(
echo From: $FROM
echo To: $TO
echo Subject: $subject
echo $subject" - "$systemdate
echo " "
echo "Time left:            "$time
echo "Time on battery: "$timeonbattery
echo "Charge level:      "$battery
echo "Load:                   "$load
echo "Voltage:               "$voltage
echo "UPS report time: "$statusdate
echo " "
tail /var/log/apcupsd.events | tac
echo " "
echo -ne  ) | sudo -u nobody /usr/sbin/sendmail -t -f $FROM
exit 0

The result from that script is a nicely formatted email that looks like this:

IPFire UPS now running on batteries - Sun, 28.11.2021 01:26:40

Time left: 115.2 Minutes
Time on battery: 0 Seconds
Charge level: 100.0 Percent
Load: 7.0 Percent
Voltage: 246.0 Volts
UPS report time: 2021-11-28 01:26:40 +0000

2021-11-28 00:06:17 +0000 Power is back. UPS running on mains.
2021-11-28 00:06:17 +0000 Mains returned. No longer on UPS batteries.
2021-11-28 00:06:10 +0000 Running on UPS batteries.
2021-11-28 00:06:04 +0000 Power failure.
2021-11-27 22:43:25 +0000 Power is back. UPS running on mains.
2021-11-27 22:43:25 +0000 Mains returned. No longer on UPS batteries.
2021-11-27 22:43:13 +0000 Running on UPS batteries.
2021-11-27 22:43:07 +0000 Power failure.
2021-11-16 00:32:42 +0000 Power is back. UPS running on mains.
2021-11-16 00:32:40 +0000 Power failure.

To test each of my scrips all I do is rename each script and append .sh to the end. Then I run the script like this:

./onbattery.sh

When I am happy with each one I just remove the .sh and sit back and look forward to the next power cut.

One last thing, all my scripts do is send out an email. Nothing else. You will need to customise them further if you want an additional action to occur.

Hope this helps.

Cheers
Mark

3 Likes

Thank you Mark, the “onbattery” script looks great.
I will totally start using it.

On my IPFire, I don’t have apcupsd.status
instead I have
/var/log/apcupsd.events

Here are my APCUPSD Custom Scripts - all use the “Mail service” menu that writes inside config file
/var/ipfire/dma/mail.conf the SENDER and RECIPIENT of Mail Service

changeme script - used to signal Battery needs to be replaced

#!/bin/sh
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when apcupsd
# detects that the battery should be replaced.
# We send an email message to root to notify him.
#

mailfile="/var/ipfire/dma/mail.conf"

if [ -f $mailfile ]; then
        FROM=`grep -a SENDER $mailfile | cut -f2 -d=`
        TO=`grep -a RECIPIENT $mailfile | cut -f2 -d=`
        else
                exit
        fi
MSG="$HOSTNAME UPS $1 battery needs changing NOW!!!"
#
apcaccess status > /var/log/apcupsd.status
BATTDATE=`grep -a BATTDATE /var/log/apcupsd.status | cut -f2 -d:`
time=`grep -a TIMELEFT /var/log/apcupsd.status | cut -f2 -d:`
statusdate=`grep -a "END APC" /var/log/apcupsd.status | cut -f2,3,4 -d:`
battery=`grep -a BCHARGE /var/log/apcupsd.status | cut -f2 -d:`
load=`grep -a LOADPCT /var/log/apcupsd.status | cut -f2 -d:`
voltage=`grep -a LINEV /var/log/apcupsd.status | cut -f2 -d:`
timeonbattery=`grep -a CUMONBATT /var/log/apcupsd.status | cut -f2 -d:`
systemdate=`date "+%a, %e.%m.%Y %H:%M:%S"`
BATTV=`grep -a BATTV /var/log/apcupsd.status | cut -f2 -d:`

(
echo From: $FROM
echo To: $TO
echo Subject: "$MSG"
echo ""
echo $MSG
echo ""
echo "Battery Install Date:            "$BATTDATE
echo "Battery Voltage:            "$BATTV
echo "Time left:            "$time
echo "Cummulated time on battery: "$timeonbattery
echo "Battery Charge level and Nominal Voltage:      "$battery
echo "Load:                   "$load
echo "Voltage:               "$voltage
echo "UPS report time: "$statusdate
echo ""

echo "Full UPS Status follows:"
apcaccess status
)| sudo -u nobody /usr/sbin/sendmail -t -f $FROM
exit 0

onbattery used to signal when UPS on Battery

#!/bin/sh
#########################################################
# This shell script if placed in /etc/apcupsd           #
# will be called by /etc/apcupsd/apccontrol when the UPS#
# goes on batteries.                                    #
# We send an email message to root to notify him.       #
#########################################################

mailfile="/var/ipfire/dma/mail.conf"

if [ -f $mailfile ]; then
        FROM=`grep -a SENDER $mailfile | cut -f2 -d=`
        TO=`grep -a RECIPIENT $mailfile | cut -f2 -d=`
        else
                exit
        fi
MSG="$HOSTNAME UPS $1 Power Failure!!!";
#Ready to start

apcaccess status > /var/log/apcupsd.status
time=`grep -a TIMELEFT /var/log/apcupsd.status | cut -f2 -d:`
statusdate=`grep -a "END APC" /var/log/apcupsd.status | cut -f2,3,4 -d:`
battery=`grep -a BCHARGE /var/log/apcupsd.status | cut -f2 -d:`
load=`grep -a LOADPCT /var/log/apcupsd.status | cut -f2 -d:`
voltage=`grep -a LINEV /var/log/apcupsd.status | cut -f2 -d:`
timeonbattery=`grep -a TONBATT /var/log/apcupsd.status | cut -f2 -d:`
systemdate=`date "+%a, %e.%m.%Y %H:%M:%S"`

(
echo From: $FROM
echo To: $TO
echo Subject: "$MSG"
echo ""
echo $MSG
echo ""
echo "Time left:            "$time
echo "Time on battery: "$timeonbattery
echo "Charge level:      "$battery
echo "Load:                   "$load
echo "Voltage:               "$voltage
echo "UPS report time: "$statusdate
echo ""
echo "Last UPS Events:"
echo ""
tail /var/log/apcupsd.events | tac
echo ""
echo "Full UPS Status follows:"
apcaccess status
)| sudo -u nobody /usr/sbin/sendmail -t -f $FROM
exit 0

offbattery used to signal when UPS no longer on battery (OFF Battery)

#!/bin/sh
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when the
# UPS goes back on to the mains after a power failure.
# We send an email message to root to notify him.
#

mailfile="/var/ipfire/dma/mail.conf"

if [ -f $mailfile ]; then
        FROM=`grep -a SENDER $mailfile | cut -f2 -d=`
        TO=`grep -a RECIPIENT $mailfile | cut -f2 -d=`
        else
                exit
        fi
MSG="$HOSTNAME UPS $1 Power has returned"
#

(
echo From: $FROM
echo To: $TO
echo Subject: "$MSG"
echo ""
echo $MSG
echo ""
echo "Last UPS Events:"
echo ""
tail /var/log/apcupsd.events | tac
echo ""
echo "Full UPS Status follows:"
apcaccess status
)| sudo -u nobody /usr/sbin/sendmail -t -f $FROM
exit 0

commfailure - used when APCUPSD lost communication with UPS

#!/bin/sh
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when apcupsd
# loses contact with the UPS (i.e. the serial connection is not responding).
# We send an email message to root to notify him.
#

mailfile="/var/ipfire/dma/mail.conf"

if [ -f $mailfile ]; then
        FROM=`grep -a SENDER $mailfile | cut -f2 -d=`
        TO=`grep -a RECIPIENT $mailfile | cut -f2 -d=`
        else
                exit
        fi
MSG="$HOSTNAME Communications with UPS $1 lost"
#
(
echo From: $FROM
echo To: $TO
echo Subject: "$MSG"
echo ""
echo $MSG
echo ""
echo "Last UPS Events:"
echo ""
tail /var/log/apcupsd.events | tac
echo ""
echo "Full UPS Status follows:"
apcaccess status
)| sudo -u nobody /usr/sbin/sendmail -t -f $FROM
exit 0

commok - used when APCUPSD restore communication with UPS

#!/bin/sh
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when apcupsd
# restores contact with the UPS (i.e. the serial connection is restored).
# We send an email message to root to notify him.
#

mailfile="/var/ipfire/dma/mail.conf"

if [ -f $mailfile ]; then
        FROM=`grep -a SENDER $mailfile | cut -f2 -d=`
        TO=`grep -a RECIPIENT $mailfile | cut -f2 -d=`
        else
                exit
        fi
MSG="$HOSTNAME Communications with UPS $1 restored"
#
(
echo From: $FROM
echo To: $TO
echo Subject: "$MSG"
echo ""
echo $MSG
echo ""
echo "Last UPS Events:"
echo ""
tail /var/log/apcupsd.events | tac
echo ""
echo "Full UPS Status follows:"
apcaccess status
)| sudo -u nobody /usr/sbin/sendmail -t -f $FROM
exit 0

How message looks like

ipfire UPS Back-UPS_CS_500 battery needs changing NOW!!!

Battery Install Date: 2021-01-20
Battery Voltage: 13.5 Volts 12.0 Volts
Time left: 33.5 Minutes
Cummulated time on battery: 104 Seconds
Battery Charge level and Nominal Voltage: 75.0 Percent
Load: 8.0 Percent
Voltage: 230.0 Volts
UPS report time: 2022-07-24 17:38:08 +0300

Full UPS Status follows:
APC : 001,046,1130
DATE : 2022-07-24 17:37:38 +0300
HOSTNAME : ipfire
VERSION : 3.14.14 (31 May 2016) unknown
UPSNAME : Back-UPS_CS_500
CABLE : USB Cable
DRIVER : USB UPS Driver
UPSMODE : Stand Alone
STARTTIME: 2022-07-22 16:43:04 +0300
MODEL : Back-UPS CS 500
STATUS : ONLINE
LINEV : 230.0 Volts
LOADPCT : 8.0 Percent
BCHARGE : 75.0 Percent
TIMELEFT : 33.5 Minutes
MBATTCHG : 25 Percent
MINTIMEL : 20 Minutes
MAXTIME : 0 Seconds
OUTPUTV : 230.0 Volts
SENSE : High
DWAKE : 0 Seconds
DSHUTD : 0 Seconds
LOTRANS : 196.0 Volts
HITRANS : 256.0 Volts
RETPCT : 0.0 Percent
ITEMP : 29.2 C
ALARMDEL : No alarm
BATTV : 13.5 Volts
LINEFREQ : 50.0 Hz
LASTXFER : Low line voltage
NUMXFERS : 3
XONBATT : 2022-07-24 16:41:01 +0300
TONBATT : 0 Seconds
CUMONBATT: 104 Seconds
XOFFBATT : 2022-07-24 16:42:02 +0300
SELFTEST : NO
STESTI : None
STATFLAG : 0x05000008
MANDATE : 2005-10-09
SERIALNO : BB0541072054
BATTDATE : 2021-01-20
NOMOUTV : 230 Volts
NOMINV : 230 Volts
NOMBATTV : 12.0 Volts
NOMPOWER : 300 Watts
FIRMWARE : 808.q7.I USB FW:q7
END APC : 2022-07-24 17:38:08 +0300

2 Likes