My 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 server connected to the managed UPS 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. Moreover, some of the systems lack Perl, others lack a compiler, and it didn't want to have create a new receiver implementation for each machine. After giving it some thought, I realised that all the machines have an inetd/xinetd equivalent and if not Perl, at the very least, a shell. Inetd-style programs 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 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 text key embedded therein, which the receiver would use to accept the announcement, ignoring any others. And because inetd-style programs runs as root, the subsequently-called shell script does as well, hence a proper shutdown command can be executed as root from within the script, thus shutting down the system.