Detecting when a server is running on UPS battery

Solution 1:

There is an old but proven way: Use an old analog modem. Here is how

  • Connect the modem to the server via serial port (e.g. ttyS0)
  • Plug the modem's power supply not into the UPS, but directly in the mains
  • Do NOT plug anything into the modem's phone line

Now poll the modem every few seconds sending ATZ[Enter] and expect OK[Enter] - if the modem does not answer, you may asume it has no power, meaning the UPS is on battery.

Edit

The main point is, that if your Internet connection drops for some other reason, the server will not shut down, as it should be.

Edit 2

From the comments:

  • Ofcourse you should keep some state from one check to the next, only shutting down after N checks failed. I did not mention this, as it holds true for all possible checks, not only the modem
  • The difference between using the modem and using anything that has to do with the network is, that transient loss of network connectivity will happen: You don't want to be bothered with "Login disabled, server on USV", if you reboot a switch or reconfigure a VLAN. In my experience the serial line is as robust as it gets.

Solution 2:

A ups usually comes with an USB or COM1 connector. When you connect this to your server you can then start monitoring the UPS.

In a windows environment, without installing the software, the messages are already registered, and it will shut down the server for you. I don't know how this works on linux, but I bet there are some software packages available to interface with your UPS.

Solution 3:

If you're using Debian, try looking into using NUT (network UPS tools). I use NUT to do exactly what you describe on my Synology NAS (which runs busybox) and on a home webserver running Ubuntu 12.04 LTS. I have the NAS plugged in via USB to the UPS for the same reason as you, potential loss of all my files.

I believe the nut daemon queries the UPS every second to determine status. When the UPS is in battery mode, it can be configured to shutdown gracefully or wait for X minutes first, etc. You can also have multiple clients set up to be connected to the nut daemon you run on your server. So your server can tell multiple PCs on your network to shutdown gracefully as well. For the shutdown execution, you can supply your own script to shutdown services in a specific way, or you can use the built-in script which just calls shutdown -h, if I recall correctly.

NUT website

There were better sites to find info, but I can't remember them right now. Configuration can be a pain, but if you're talking about potentially losing your RAID, check it out! Depending on your needs, you might not need the entire NUT suite, you might be able to get by with just the UPSMON portion. I think you can get this in the Ubuntu package "nut-client".

There is also a NUT client for Windows, but on Windows you might be better off using something like PowerChute, or something similar.