Detecting when a server is running on UPS battery

24

3

I have a home server (HP Proliant ML310 G4) that I bought from my employer a month or so ago. The server runs Debian Linux and I use it for just about everything. I can get away with doing this because my needs aren't that demanding so the server is rarely under heavy loads at any given time. This server has a 2 TB RAID1 array (with one hot spare) managed by mdadm that I use for file sharing over my network via Samba. I'm running the OS off a smaller hard drive that is not part of the array.

I have the server powered through a small 255-watt UPS because I need it to withstand minor power fluctuations without coming down. This is very important because a sudden unclean shutdown (caused by a power drop or blackout) can screw up the RAID array, which has already happened on one occasion.

In the event of a power failure, I'm estimating the UPS has only 10-15 minutes of power in it. There may be more but I don't want to automatically assume there is. This is enough to get through blackouts that last a few minutes or less. However, if the power is down for several hours I need the server to properly shut itself down automatically before the UPS power fails. This will hopefully keep the RAID integrity from getting hosed in those situations.

This sort of thing is easy on a laptop b/c it has built-in sensors to determine when it is running on battery power, but this server does not (as far as I know). I have an unusual network setup where my DSL modem is separate from my router. The router is also on the UPS, which allows me to SSH in through my laptop and shut down the server in the event of a power failure. Unfortunately, this only works if I'm there at the time to do it. I need a solution that always works whether I'm there to intervene or not.

Since my DSL modem is not on the UPS, it would go down with everything else if the power fails. I set it up like that deliberately because I have an idea about how to solve this problem: I was thinking of writing a script that would ping google.com or some other high-traffic/high availability site. If the ping completes successfully, the server would keep running. If the ping fails (because the modem is off), the script would think the power is off and would tell the server to shut down. I would add the script to Cron and have it run every 5 minutes. That would hopefully give the server enough time to shut down before the UPS runs out of power.

Before I go to the trouble of writing the script, I would like to know if there is a better way to do what I need. Thanks in advance for any help.

Will Kraft

Posted 2014-04-06T21:19:45.507

Reputation: 351

15You should not ping an external site, but ping your DSL modem's IP. – LawrenceC – 2014-04-06T23:15:37.363

1@ultrasawblade...sounds like an answer, smells like an answer...it is an answer! – Pureferret – 2014-04-07T06:31:13.417

1As @ultrasawblade mentions, NEVER rely on outside sources for an internal setup. For example, if your internet suddenly fails, you lose connectivity and your server goes down. If Google.com decides to change their name for whatever reason, your server goes down. If you ping them using IP and that particular server farm goes down, your server goes down as well. this is the case with any outside source. Unless you are 100% sure that your server MUST ONLY be on when that other server is available, you don't want to rely on that other server for any reason. – Nzall – 2014-04-08T10:10:31.550

Do none of these answers work for you? – Dave – 2014-05-20T09:14:20.803

Answers

34

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.

Eugen Rieck

Posted 2014-04-06T21:19:45.507

Reputation: 15 128

1Good answer, but I'd suggest adding a safety where it checks a few times before it actually shuts down. (so you won't have to shutdown during a 5minute brown out) And I think pinging a swich would also suffice (just nothing outside your network). – HTDutchy – 2014-04-07T07:02:48.663

@HTDutchy See my edit! – Eugen Rieck – 2014-04-07T09:04:11.070

I know, but as he might not have a serial connection or an old modem hanging around pinging in house hardware would be the next best thing. and if the switch would stop working the server would appear down anyway. – HTDutchy – 2014-04-07T09:08:17.140

22

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.

LPChip

Posted 2014-04-06T21:19:45.507

Reputation: 42 190

I just checked my UPS and unfortunately it does not have anything like that. It just has 8 power plugs (4 on battery and 4 without) and line-in and line-out jacks for phone cords. – Will Kraft – 2014-04-06T21:33:26.137

27Then get a UPS that can communicate with the computer. – Ramhound – 2014-04-06T21:43:29.600

@Ramhound That may not be an option, UPSes can be quite expensive, and there are cheaper ways. – Vality – 2014-04-07T19:49:00.020

I disagree there are other ways to detect you are running on a battery of a ups you cannot even detect at the is level – Ramhound – 2014-04-07T23:29:11.210

1

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.

zako42

Posted 2014-04-06T21:19:45.507

Reputation: 143

As said before it looks like the OP's UPS doesn't have any way to communicate with the computer, no USB nor network. – None – 2014-04-08T09:46:54.690