# Apcupsd & email

**URL:** <https://community.ipfire.org/t/apcupsd-email/6320>\
**Category:** Monitoring\
**Created:** [24 September 2021 12:22 UTC](https://community.ipfire.org/t/apcupsd-email/6320 "2021-09-24T12:22:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pic18f2550](https://community.ipfire.org/user_avatar/community.ipfire.org/pic18f2550/32/16251_2.png) [@pic18f2550](https://community.ipfire.org/u/pic18f2550)\
**Post date:** [24 September 2021 12:22 UTC](https://community.ipfire.org/t/apcupsd-email/6320/1 "2021-09-24T12:22:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bbitsch](https://community.ipfire.org/user_avatar/community.ipfire.org/bbitsch/32/7420_2.png) [@bbitsch](https://community.ipfire.org/u/bbitsch)\
**Post date:** [24 September 2021 12:59 UTC](https://community.ipfire.org/t/apcupsd-email/6320/2 "2021-09-24T12:59:33Z")

</div>

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

[APC UPS Daemon manual](http://www.apcupsd.org/manual/manual.html)

and

> **[apcupsd - The IPFire Wiki](https://wiki.ipfire.org/addons/apcupsd)**

---

<div class="post-metadata">

**Author:** ![steph78630](https://community.ipfire.org/user_avatar/community.ipfire.org/steph78630/32/8251_2.png) [@steph78630](https://community.ipfire.org/u/steph78630)\
**Post date:** [24 September 2021 13:10 UTC](https://community.ipfire.org/t/apcupsd-email/6320/3 "2021-09-24T13:10:56Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![pic18f2550](https://community.ipfire.org/user_avatar/community.ipfire.org/pic18f2550/32/16251_2.png) [@pic18f2550](https://community.ipfire.org/u/pic18f2550)\
**Post date:** [24 September 2021 13:15 UTC](https://community.ipfire.org/t/apcupsd-email/6320/4 "2021-09-24T13:15:41Z")

</div>

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](mailto: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?

---

<div class="post-metadata">

**Author:** ![bonnietwin](https://community.ipfire.org/user_avatar/community.ipfire.org/bonnietwin/32/12350_2.png) [@bonnietwin](https://community.ipfire.org/u/bonnietwin)\
**Post date:** [24 September 2021 13:15 UTC](https://community.ipfire.org/t/apcupsd-email/6320/5 "2021-09-24T13:15:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bbitsch](https://community.ipfire.org/user_avatar/community.ipfire.org/bbitsch/32/7420_2.png) [@bbitsch](https://community.ipfire.org/u/bbitsch)\
**Post date:** [24 September 2021 13:37 UTC](https://community.ipfire.org/t/apcupsd-email/6320/6 "2021-09-24T13:37:34Z")

</div>

The cited topic is from the chapter " [The Windows Version of apcupsd](http://www.apcupsd.org/manual/manual.html#the-windows-version-of-apcupsd)"  
so it doesn’t help in a linux environment.

---

<div class="post-metadata">

**Author:** ![bonnietwin](https://community.ipfire.org/user_avatar/community.ipfire.org/bonnietwin/32/12350_2.png) [@bonnietwin](https://community.ipfire.org/u/bonnietwin)\
**Post date:** [24 September 2021 13:43 UTC](https://community.ipfire.org/t/apcupsd-email/6320/7 "2021-09-24T13:43:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pic18f2550](https://community.ipfire.org/user_avatar/community.ipfire.org/pic18f2550/32/16251_2.png) [@pic18f2550](https://community.ipfire.org/u/pic18f2550)\
**Post date:** [24 September 2021 14:19 UTC](https://community.ipfire.org/t/apcupsd-email/6320/8 "2021-09-24T14:19:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bonnietwin](https://community.ipfire.org/user_avatar/community.ipfire.org/bonnietwin/32/12350_2.png) [@bonnietwin](https://community.ipfire.org/u/bonnietwin)\
**Post date:** [24 September 2021 15:35 UTC](https://community.ipfire.org/t/apcupsd-email/6320/11 "2021-09-24T15:35:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mfischer](https://community.ipfire.org/user_avatar/community.ipfire.org/mfischer/32/16066_2.png) [@mfischer](https://community.ipfire.org/u/mfischer)\
**Post date:** [24 September 2021 15:56 UTC](https://community.ipfire.org/t/apcupsd-email/6320/12 "2021-09-24T15:56:04Z")

</div>

Alternatively, you can [use ‘monit’…](https://mmonit.com/wiki/Monit/ConfigurationExamples#apcupsd)

This requires the installation of [‘monit’](https://wiki.ipfire.org/addons/monit) as an additional addon.

Best,  
Matthias

---

<div class="post-metadata">

**Author:** ![mcjoshea](https://community.ipfire.org/user_avatar/community.ipfire.org/mcjoshea/32/9632_2.png) [@mcjoshea](https://community.ipfire.org/u/mcjoshea)\
**Post date:** [28 November 2021 02:24 UTC](https://community.ipfire.org/t/apcupsd-email/6320/13 "2021-11-28T02:24:22Z")

</div>

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:

```auto
#!/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

---

<div class="post-metadata">

**Author:** ![trish](https://community.ipfire.org/user_avatar/community.ipfire.org/trish/32/16001_2.png) [@trish](https://community.ipfire.org/u/trish)\
**Post date:** [2 February 2022 06:59 UTC](https://community.ipfire.org/t/apcupsd-email/6320/14 "2022-02-02T06:59:19Z")

</div>

> [@mcjoshea](#):
>
> `apcaccess status > /var/log/apcupsd.status`

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

---

<div class="post-metadata">

**Author:** ![hjkl](https://community.ipfire.org/user_avatar/community.ipfire.org/hjkl/32/8574_2.png) [@hjkl](https://community.ipfire.org/u/hjkl)\
**Post date:** [24 July 2022 14:45 UTC](https://community.ipfire.org/t/apcupsd-email/6320/15 "2022-07-24T14:45:54Z")

</div>

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

```auto
#!/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

```auto
#!/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)

```auto
#!/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

```auto
#!/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

```auto
#!/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
