UPSalert – My solution to Graceful Server Shutdowns on Power Loss

   UPS’s are truly great devices! They save your butt when you have a short power out, or grandma accidentally has her curling iron and hair dryer on at the same time. But with a known runtime, you need a way to gracefully shutdown your systems to prevent data loss. Well, with APC PowerChute not looking very friendly, I decided to just go out and make my own little tool, and here it is! May I present to you, UPSalert – My solution to graceful server shutdowns on power loss.

Note:

  1. This script was developed for Debian Linux, so I can not promise it will work on other distros without modifications.
  2. This script is currently designed for use with an APC UPS. If you have a different UPS, and would like a modified version similar to what this does, shoot me an email at chris (at) servernetworktech.com and I will see what I can do for you.

Prerequisites

  1. A Server to host this script
  2. SSH Key Authentication between the Host and Servers (You can follow my guide on that Here)
  3. An APC UPS with SNMP Enabled

Install Instructions

First thing you will need to do is SSH into the Server that will host the script, and install the required packages for the script. Remember to run apt-get as root.

apt-get update
apt-get install snmp ping wakeonlan bsd-mailx

Now we will need to create the folder /opt/upsalert. After that you will need to download the script, and then make it executable.

mkdir /opt/upsalert
wget http://servernetworktech.com/uploads/scripts/UPSalert.sh -O /opt/upsalert/UPSalert.sh
chmod +x /opt/upsalert/UPSalert.sh

After this, you will need to configure the script, so just nano inside of it, and modify the values. This is where you define the MAC address and IPs of your servers, as well as your email and such

nano /opt/upsalert/UPSalert.sh

Now is where I would recommend setting it to debug mode, and running it to make sure it works. After you test it, and are satisfied with the results, turn off debugging, add a cronjob to call the script, and away you go!

Standalone Downloads

If you don’t want to use my guide, and you just want to setup the script on your own, then these are for you. I also included a Proxmox Version (which is what I use) for those of you who also want your cluster nodes to properly shutdown VM’s before halting the host. This helps prevent data loss.

Standard Version Download Here
Proxmox Version Download Here

PLEASE Let me know if you find any bugs/errors/issues with this software!

5 thoughts on “UPSalert – My solution to Graceful Server Shutdowns on Power Loss

  1. Hampton

    Do you have any suggestions for having a single USB controlled UPS shut down multiple servers and a router on low battery? The servers are all Debian 7 and the router is running pfSense. I haven’t bought the UPS just yet, I’m trying to figure out if there is a specific one that does this better than others. Thanks a lot for the great post!

    Reply
  2. Dave Klassen

    Did you look into using the Debian ‘apcupsd’ package before creating this script, and discover something that did not meet your needs? It would be interesting to learn about which aspect of apcupsd did not live up to your expectations on Debian. Personally I discovered certain firewall considerations needed to be taken into account, between servers and VMs, but for the most part apcupsd can provide for most situations. Actually the most difficult part was ensuring apcupsd used the proper halt option (-H) for the shutdown command, and in the right configuration (ie. -H for host hypervisor and no -H for VMs). Of course verifying your BIOS configuration is actually setup to help your system recover from a power failure event, is also really important, since powering back-on after the power is restored can be critical. Anyways I just wanted to check with you here, about why you chose to create your own alternative. I actually did the same thing for my VM nodes after looking at your post, however for the most part it was a waste of a day, since apcupsd was sufficient for my VMs as well (Just making use of the ‘net’ NIS configuration as opposed to ‘usb’). One usage model that I can imagine where it may actually be required is for an OS that doesn’t playwell with being a UPS slave node (Their might be one out there, I guess). I know my biggest frustration with the Power Chute application that my UPS came shipped with is that it only covers their windows solution, and doesn’t mention of anything about the apcupsd UNIX package. Come on who really needs a UPS for a laptop/desktop anyways right? However they might have done well to at least cover UNIX, given OSX is also a UNIX variety that could likely take advantage of apcupsd.

    One thing I really like about your script is that it uses a secure protocol like ssh to communicate, and while I have my systems communications shielded with usb, and firewalled for NIS/TCP, the actual data communications are not encrypted inside the hypervisor/node. However snmp v2 is not encrypted either, and well configuring non-password protected ssh keys is not something I would prefer. However the difference between the two is not really as important sometimes, compared with the potential to learn about how systems work, and actually testing whether your system can recover successfully or not. I recently switched over to proxmox so all of this has become really important. If you can believe it my systems were working/recovering just fine without actually using the apcupsd functionality available to them. What is even funnier is that XFS and other filesystems issues were never encountered because my older system could still auto-fsck with the more simple ext4 filesystem. Anyways as always working in a new technical paradigms holds some interesting new quirks to learn about and/or work around.

    Reply
    1. Chris B - Admin Post author

      Hey Dave,

      When this was written I took a look at apcupsd, but if I remember correctly there was a specific issue with my implementation that would have made it not work in my environment. I wish I could remember the exact details, but it’s been a few years now.

      As for implementation, I wish there was a more secure way with SNMP v2, but as long as you use a Community name that is set to Read Only, there isn’t too much to worry about. Besides, if it’s your home network you should hopefully be able to trust the traffic on the wire. 🙂

      I also forgot to post this, but you can find a slightly updated version of this script over on GitHub at https://github.com/riptidewave93/UPSalert

      Reply
  3. Pingback: Apcupsd Snmp How To | Information

Leave a Reply

Your email address will not be published. Required fields are marked *