UPS Network Notification System
I monitor the UPS that services my LAN infrastructure devices, using the
APC UPS Daemon. This software has
hooks for calling custom programs when it becomes aware that the line power
is down and the UPS is running on battery. I took advantage of this to
create a simple network notification system. My goal was to enable
all my workstation/lab boxes (using several diverse flavours of *nix, with
various levels of toolsets available on each), which are connected to
several disparate and unmanaged UPSes, to know when to shut down gracefully
in the event of a power outage.
I decided to implement the notification system using UDP, as I didn't want
the mail/web server to hang around waiting for a bunch of machines to
respond to a "power-out" announcement, especially because at any given
moment some boxes could be up and others powered off. So I wrote a small
Perl program to spray out a UDP notification message to the entire range of
IP addresses for each Class C network listed therein.
Implementing the receiver program presented a little more difficulty,
however, because I didn't want to go through the trouble of writing a
full-blown server daemon that listened for UDP messages. After giving
it some thought, I realised that all the machines have inetd/xinetd and
Perl (or at the very least, a shell). The inetd/xinetd program can be
configured to listen for either a TCP or a UDP connection on a specified
port, and when one is established, to spawn a given process and hand off
the connection to it, which is then piped to the standard I/O of the
process. This means, in effect, that I could write a very simple Perl
program (or even a shell script) to parse the input for a pre-determined
"power out" message. To prevent false alarms, I decided that the notifier
would send out the notification with a predetermined key text embedded
therein, which the receiver would use to accept the announcement, ignoring
any others. And because inetd/xinetd runs as root, the subsequent Perl or
shell script does as well, which means that a proper shutdown command can
be executed from within the script, also as root.