I'm using a UPS that includes a serial port, together with a driver that receives the signal from the UPS whenever it switches to battery power. It's configured to do a shutdown 5 minutes after the switch, unless power comes back up during that time. The delay allows for momentary power glitches without forcing shutdown, and the UPS is sturdy enough to keep the box going for at least 15 minutes so there's time for the shutdown.
I've tested it by pulling the plug on the UPS, and it works perfectly. Be sure, though, when you get your UPS to make certain it has the port to notify the computer! My first two did not have this capability. Since then I've learned to look for it (I only have six UPSes scattered around my LAN; only the one on the file server is set up to guarantee proper shutdown)...