Have UPS shut down 🐧 Linux when the power goes out

Have UPS shut down 🐧 Linux when the power goes out

Summary:

In this article, I’ll share with you my experience in setting up “a graceful shutdown” for a Linux machine that is connected to a UPS (Smart-UPS SC620) using NUT software.

Recently we had an issue with a Linux server that was sitting in a datastore across the country, where power outages were happening often.

In the data center, we have UPS’s that are supposed to keep the servers running during those power outages, but UPS can give you an hour max. What happens if the outage lasts for hours, or even days? Well, the ideal solution would be for the machine to shutdown softly if the outage lasts for more than 30 minutes.

And that’s exactly what we were going for.

The main problem is: How to notify the machine that the power is off and that it’s running on UPS.

If the UPS is doing its job, the computer doesn’t know that the utility power went out. The only way for the computer to know to shut down, while it can still do so gracefully, is if the UPS can communicate the situation through a data connection cable between the UPS and the computer.

Data Connection

The UPS comes with whatever cable is needed for the data connection, most often USB.

In more advanced models, the UPS also uses this connection to communicate status information, like battery condition.

Software

You need software on the computer to make use of this information. The UPS typically comes bundled with software for Windows, but Linux often requires an extra step or two. A few distros come bundled with either a generic program or one from a particular UPS manufacturer.

apcupsd

apcupsd
apcupsd

Apcupsd can be used for power mangement and controlling most of APC’s UPS models on Unix and Windows machines. Apcupsd works with most of APC’s Smart-UPS models as well as most simple signalling models such a Back-UPS, and BackUPS-Office.

During a power failure, apcupsd will inform the users about the power failure and that a shutdown may occur. If power is not restored, a system shutdown will follow when the battery is exhausted, a timeout (seconds) expires, or runtime expires based on internal APC calculations determined by power consumption rates.

apcupsd
apcupsd GUI

One limitation is that it only supports APC products. However, if you don’t have a strong reason to buy a brand that lacks Linux software, picking from APC’s offerings is an easy option.

NUT

NUT
Network UPS Tools (NUT)

This is a free application that handles control, monitoring, and management of almost anything power-related. It’s probably overkill for a UPS on one computer, and it requires some configuration (not the best solution for newbies).

The primary goal of the Network UPS Tools (NUT) project is to provide support for Power Devices, such as Uninterruptible Power Supplies, Power Distribution Units, Automatic Transfer Switch, Power Supply Units and Solar Controllers.

WinPower

winpower
WinPower Software

Winpower UPS monitoring software provides a user-friendly interface to monitor and control your UPS. This unique software provides complete power protection for your computer equipment during a power failure. With the software users can monitor any UPS status on the same LAN. Furthermore, a UPS can provide security protection for more than one computer on the same LAN at the same time, such as shutting down the system security, saving application data and shutting down the UPS when power fails.


Adding a UPS to a Linux machine

In this article, I’ll share with you my experience in setting up “a graceful shutdown” for a Linux machine that is connected to a UPS (APC Back-ups RS-1500) using NUT software.

Adding a UPS to a Linux machine
Apc Back-ups Rs-1500

Installing the UPS hardware involves connecting a single terminal on the UPS battery, plugging the UPS into the mains power, and connecting the UPS to the computer using either an RS-232 serial interface or USB.

See also  📴 Remote Shutdown Apache Tomcat

Configuration Files

The NUT daemon has a collection of configuration files located in either /etc/nut or /etc/ups, depending on how your NUT was packaged.

Unfortunately, you must make changes to four configuration files in order to use your UPS. All of the files are well commented, so I will only mention the sorts of changes that are needed.

ups.conf

The ups.conf file should contain a section with the driver name and port to use to communicate with your UPS. The main changes you will likely have to make to upsd.conf are to ensure that the ACLs allow you to connect to the NUT daemon, at least allowing connections from localhost.

ups.conf file example:

[ups]
  driver = apcsmart
  port   = /dev/ttyS0

upsd.conf

upsd uses this file to control access to the server and set some other miscellaneous configuration values. This file contains details on access controls, so keep it secure. Ideally, only the upsd process should be able to read it.

upsd.conf file example:

ACL all 0.0.0.0/0
ACL localhost 127.0.0.1/32
ACL localnet  192.168.7.0/24
ACCEPT localhost
ACCEPT localnet
REJECT all

upsd.users

The upsd.users file is where you set up usernames and passwords and assign which actions each user can perform. You will need to have at least one user in upsd.users so that the monitoring daemon can connect.

upsd.users file example:

[monuser]
        password = xxx
        allowfrom = localhost
        upsmon master

upsdmon.conf

The fourth configuration file is upsmon.conf in which you tell NUT which UPS units to monitor. The upsmon.conf needs to use the monitoring username and password from upsd.users.

upsdmon.conf file example:

MONITOR ups@localhost 1 monuser xxx master

Shutting down Server and UPS

With the configuration files in place you should be able to start the NUT daemon by executing service ups start. Assuming that the UPS hardware is on and connected to the computer and that you have named your ups simply “ups” as shown above, you can query its settings with upsc ups.

Shutdown the Machine

If you are also using NUT (Network UPS Tool) to monitor your server, then your clients can do this. The host to which the UPS is connected should be the last to go down.

There is an option to run a script on various events (with delays if desired). In your case you would want to trigger shutdown on an outage of some seconds. If you have wake on LAN you you could trigger a wake event some time after power is restored.

In case you prefer apcups ovet NUT, you can shutdown the server using configuration parameters. If during a power failure, the remaining battery percentage (as reported by the UPS) is below or equal to the predefined BATTERYLEVEL, apcupsd will initiate a Linux system shutdown.

More information can be found here.

Shutdown UPS

You will find mentions of /etc/killpower throughout the NUT documentation. The main reason for this file is so that just before the computer is about to switch itself off it can tell the UPS itself to shutdown.

On Linux, a quick grep in /etc/init.d or a similar directory for the killpower string may show that the presence of this file is already handled for you. If /etc/killpower is not handled in your shutdown scripts then the UPS will remain on when the computer shuts down. This is not critical but may cause the UPS to slowly continue to use battery power until it runs out.

whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

1 comment
%d bloggers like this: