Last time, we looked at a useful
UPS-monitoring tool called NUT
. Part of the power of NUT is in how it can
be configured to handle UPS-related events. By default, NUT doesn’t do much of
anything; it’s up to you to customize it to respond to certain events.

The first step is to edit /etc/ups/upsmon.conf
(the location may vary depending on distribution) and set some NOTIFYFLAG
conditions. The NOTIFYFLAG command takes a flag and an action as parameters:

NOTIFYFLAG ONBATT SYSLOG+WALL

This tells upsmon to
write to the syslog and to send a
broadcast wall message when it detects that the system is on battery power. You
can customize this further by setting an EVENT action as well. A more complete
example could be:

NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC
NOTIFYFLAG COMMBAD SYSLOG+EXEC
NOTIFYFLAG COMMOK SYSLOG+EXEC
NOTIFYFLAG REPLBATT SYSLOG+WALL
NOTIFYFLAG FSD SYSLOG+WALL+EXEC

This will run the EXEC command on all events except the
REPLBATT event which indicates the UPS battery needs to be replaced. Syslog
actions are used for all events, and broadcast messages are sent on all events
except communications errors.

Tips in your inbox

Delivered each Tuesday, TechRepublic’s free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills.

Automatically sign up today!

The EXEC action needs to be defined earlier in the
configuration file with the NOTIFYCMD command. NUT comes with a handy tool
called upssched that allows for the
scheduling of events, so the best NOTIFYCMD to define is upssched:

NOTIFYCMD /usr/sbin/upssched

Once this is done, edit the /etc/ups/upssched.conf file. This configuration file controls how upssched handles events. You can tell it
to execute commands or start timers; e.g., if the UPS is still online after 30
seconds, send an e-mail. The first step is to define the CMDSCRIPT, or the
shell script (or program) that does the heavy lifting:

CMDSCRIPT /usr/sbin/ups_command

Then you define AT events:

AT ONBATT * START-TIMER onbattwarn 30
AT ONLINE * CANCEL-TIMER onbattwarn

This tells upssched
to start a timer that will execute CMDSCRIPT after 30 seconds of being on
batteries and will pass CMDSCRIPT the single argument onbattwarn. If the UPS comes back online before 30 seconds is up, upssched cancels the timer.

In this instance, CMDSCRIPT is /usr/sbin/ups_command, which could be a shellscript consisting of
nothing more than:

#!/bin/sh
 
case ${1} in
    onbattwarn)
        wall "The UPS is currently running on battery power!"
        ;;
esac
 
exit 0

Of course, you can configure it to handle multiple events and
do much more than just send a wall message. You could have it send an e-mail to
a cell phone or pager, trigger a series of other scripts, etc. This example,
however, illustrates how flexible the handling of UPS-related events can be.