Setup UPS on Ubuntu Server
For any computer, whether it be a desktop or especially a server, the use of a power conditioner/UPS (Uninterruptible Power Supply) is a MUST.
For one thing the power coming into your home/office is not "clean", ie there are numerous fluctuations in the voltage levels on a regular basis. In addition, power outages can occur, especially in outer lying areas.
These power failures can affect a running computer system in 2 main areas.
1) It's a Very bad idea to simply power off a running computer. In order to shut it down you need to tell it to "gracefully shutdown", before you turn off any power switch(es). If the computer is in process of reading data from the hard drive, this is not so much of an issue. On the other hand, if there is new data still in memory, which has not yet been written to the hard drive, if/when a power failure occurs, that data is lost.
Even more importantly, if the computer is in process of writing data to the hard drive, and a power failure occurs, this can result in corruption of the files on the drive.
2) When the power comes back on, a huge spike in the line voltage can occur.
Note: Many people think those "power strips" are a power conditioner, or can protect your system from voltage surges. In my opinion, they are not sufficient to handle a big spike in the power after a power outage occurs, and power is restored.
Back when I first got into computing in the late 70s, there were 2 basic types of power conditioners. One had a battery backup, and the other type did not. In addition, a power conditioner with a battery backup was a serious investment. Nowadays they are much more reasonably priced, so it's worth the extra money to get one with a battery backup unit.
The battery backup will keep the computer running for a period of time after a power outage, and gives us time to "gracefully" power down the system.
On a server, we may not be in front of the console when a power failure occurs. So the best way is to automate the shutdown procedure, so the system is automatically shutdown in the event of a power failure. In the case of a server, we likely want the system to automatically come back online when the power comes back on.
For my server(s), I purchased an APC UPS for each machine, complete with a battery backup. In most cases, when you purchase a UPS, it will come with a cable, to connect between the UPS and the computer, and a CD/DVD with software. This software allows the UPS and the computer to communicate.
Years ago, this cable connected via a serial port on the computer. Serial communication has pretty much been eliminated, in favor of USB. In the case of my UPSs, they came with USB cables, and software meant for Windows.
I have at least one Ubuntu Server installation on each of my ESXi servers. I currently have 2 physical machines both running ESXi. One for web server(s) and the other for email/calendar/contacts server(s).
So the next question became, how do we communicate with the UPS from Linux? More Googling resulted in my discovery of a software package which has been designed specifically for this purpose, by the name of APCUPSD.
Note: APCUPSD can be installed/used on either Linux or Windows.
Before we can install the software, we need to make sure the UPS is connected to the system via the USB cable.
The following are my notes for the installation/configuration of this software:
- Log into Ubuntu Server, either by using the Console tab in vSphere, or use an SSH client such as Putty, or Bitvise.
- Enter administrative mode, if you aren't already logged in as root.
Install the software
apt-get install apcupsd
Edit the configuration file
In the configuration file, give the UPS a name, and configure to use USB
If you have a "DEVICE /dev/ttyS0" command after the UPSTYPE line, comment it out by adding a hash symbol at the start of the line like so:
# DEVICE /dev/ttys0
Note: There are 3 main parameters which can be changed in the configuration file, which control long the system waits until it does a shutdown of the system.
These values co-incide with the values being reported to the software by the UPS. Mainly and the estimated time before the battery is fully discharged, which the UPS calculates based on the load and the remaining charge in the battery.
The 3 parameters in the apcupsd.conf file are
a) BATTERYLEVEL, the battery charge level (expressed as a percentage)
b) MINUTES, the estimated time before the battery is fully discharged and
c) TIMEOUT, an "override", expressed in seconds which if set to a non-zero value instruct apcupsd to ignore the above 2 parameters.
How you set these values are up to you. There are several factors involved. For instance, in your area, how frequently "short" power failures occur. Since I live fairly close to the city, power failures occur here very rarely. When they do occur, they usually last for more than a few minutes.
The other factor which may affect your decision is the age of your battery. In my case, I have had my UPS units for over 10 years, so the battery life on them is not what they used to be.
Therefore, I decided to play it safe, and use the TIMEOUT value. I set mine to 60 seconds, ie:
In addition, I set
This causes apcupsd to start generating messages to the console, or to logged in users, such as through SSH, within 5 seconds of a power failure, notifying them (me) about the fact the power has failed.
Save the file (in nano we use ctrl+X).
Now we need to tell the system that the UPS daemon has been setup and is essentially ready to go.
And change the following parameter to look like this:
Hit ctrl+X to save the changes.
Now we can start the service by typing:
service apcupsd restart
Check the UPS Monitoring Software
The amount of information this command will show depends on the UPS being used. In my case, my UPS divulges quite a bit of information to the software. The following is a snippet I found on the web which will give you an idea of what this command shows us:
APC : 001,043,1045 DATE : Tue Feb 01 00:02:36 EST 2013 HOSTNAME : site5 VERSION : 3.14.6 (16 May 2009) debian UPSNAME : APC1000 CABLE : USB Cable MODEL : Back-UPS BR 800 UPSMODE : Stand Alone STARTTIME: Mon Jan 31 23:43:37 EST 2011 STATUS : ONLINE LINEV : 250.0 Volts LOADPCT : 40.0 Percent Load Capacity BCHARGE : 100.0 Percent TIMELEFT : 19.9 Minutes MBATTCHG : 5 Percent MINTIMEL : 3 Minutes MAXTIME : 0 Seconds OUTPUTV : 230.0 Volts SENSE : Medium DWAKE : 000 Seconds DSHUTD : 000 Seconds LOTRANS : 194.0 Volts HITRANS : 264.0 Volts RETPCT : 000.0 Percent ITEMP : 29.2 C Internal ALARMDEL : Always BATTV : 27.4 Volts LINEFREQ : 50.0 Hz LASTXFER : Low line voltage NUMXFERS : 0 TONBATT : 0 seconds CUMONBATT: 0 seconds XOFFBATT : N/A SELFTEST : NO STATFLAG : 0x07000008 Status Flag SERIALNO : xxxxxxxxxxxx BATTDATE : 2001-09-25 NOMOUTV : 230 Volts NOMINV : 230 Volts NOMBATTV : 24.0 Volts NOMPOWER : 540 Watts FIRMWARE : 9.o5 .I USB FW:o5 APCMODEL : Back-UPS BR 800 END APC : Tue Feb 01 00:02:41 EST 2011
Testing the UPS Software
In order to test the functionality of the software, I physically pulled the power cable, going from the UPS to the wall, and observed the behaviour of the system. Since I was logged into the system at the time with Putty, I observed a number of status messages being displayed on the console. These messages re-affirmed to me the software was communicating with the UPS, and after a while it gracefully shutdown the system, and then the UPS itself.
The above instructions are for a standalone Ubuntu Server system, connected to an APC UPS. Since I discovered ESXi, and virtual machine technology, I now run ESXi on all my physical server systems.
Since I authored this article, I wrote another article on how to shutdown all the VMs and ESXi, using an extension of the techniques prescribed in this article. You can find the new article by clicking HERE.