The Helpful Hacker

Coding, hacking and self-improvement


Zach Peters

zpeters@gmail.com
zpeters
zpeters
KC9SDM
Subscribe to this site

Graceful UPS shutdowns for your ESXi server with Centos

02 Nov 2012

spacer

Background

When I initially setup my home ESXi server the UPS was an afterthought.  I vaguely knew that ESXi could be setup to trigger a shutdown from a UPS and i figured if I had Vmware Guest utilities installed on all my vm’s that would take care of safe shutdown.  Searching around most of the methods were either network shutdown (my UPS only has a USB connection) or homebrew.  If it’s homebrew already I might as well learn what this is all about and build my own solution.

Base setup

The heart of the UPS monitor/shutdown service will be a small vm running Linux. I chose Centos since it is reasonably light-weight feel free adapt this to your favorite distro-of-choice.

  1. Download Centos 6.3

  2. Create a new VM in ESX with the following:

* 512 MB ram


* 8 gig hdd - Thin provisioned
  1. Install Centos as normal

  2. Edit /etc/sysconfig/network-scripts/ifcfg-eth0 - set onboot to yes reboot

  3. yum upgrade

  4. After installation add the USB controller and pass-through device from ESX.  You should see something labeled “APC”

  5. yum install usbutils

  6. lsusb - see if apc is there

Install APC Stuff

The default package repo in Centos did not have the apcups packages so we need to add “epelrepo” (elrepo.org/tiki/tiki-index.php)

  1. Add epel repo
* `rpm -Uvh ftp.osuosl.org/pub/fedora-epel/6/i386/epel-release-6-7.noarch.rpm`
  1. yum install apcupsd

  2. chkconfig apcupsd on

  3. edit /etc/apcupsd/apcupsd.conf and set the following options

* _UPSNAME_ - any name will do - `UPSNAME myups01`


* _UPSCABLE_ - usb - `UPSCABLE usb`


* _UPSTYPE_ - usb - `UPSTYPE usb`


* _DEVICE_ - leave this blank - `DEVICE`
  1. service apcupsd start

  2. apcaccess (to test). You should see something like the output below

spacer

Email alerts

Next we’ll setup UPS alerts so the UPS can warn you of power outages, battery status, etc. The detail and options available will be different for each UPS to make sure to review the apcupsd manual for details. Remember if you intend to got notifications of power outages your modems, routers, switches, etc. need to be powered at that time :-)

spacer

Install Vmware Tools

At this point we have the apcupsd tools installed, we can monitor the battery and have the default alerting rules setup.  Our plan will be to have a manually specified list of vms and the order to shut them down in.  The purpose for these is we may be running vms that don’t support Vmware Tools, thus can’t do a “safe shutdown” when ESXi shuts down.

When apcupsd triggers a shudown (senses loss of power) we’ll but using our Centos install to control the shutdowns of all of our vms and the ESX server itself.  However we don’t have a good way to tell our vm to shut itself down (it is possible we could add our instance to the end of the shutdown script or do a simple shutdown -h now … this seemed like a cleaner solution at the time)

Test Default UPS shutdown

Shut Down Everything

At this point we have tested all the pieces of our glorious UPS shutdown system.  Time to put the final pieces in place to do the actual work.

Setup SSH access

To accomplish the shutdowns we will need to run some commands on the ESX server, which means our Centos VM will need ssh access to the server.

Following this guide create a ssh key and add it to the ESX servers authorized keys.  Make sure to follow this guide since the authorized_keys file isn’t in the usual location

 Shutting down the VMs

In ESXi there is a built in command language to allow us to do some of our regular maintenance tasks from the command line.  One of these is to send a “shutdown signal” to the vm.  Since we want to control the order our VMs are shutdown we need to get a list of the VMs and their unique IDs first

Log on to your ESX server and run the vim-cmd vmsvc/getallvms command and note the VM names, IDs and the order you want to shut them down in.

Next create a script similar to the following and place it somewhere on your ESX server.

https://gist.github.com/zpeters/3909385

Wrapping It All Up

After you’ve tested remote access to the ESX server (run something like ssh root@1.1.1.1 "ls" and making sure you get output) and you’ve done a test run of your shutdown script. It’s time to tell apcupsd to run this instead of the traditional ‘shut myself down’ command.

And that is pretty much it.  You have a small Centos instance monitoring your UPS battery state, capable of initiating shutdown (or any other commands you wish) on a power outage.  Timing for power outages, how long you wait to shutdown the host, etc. are up to you, however the defaults are reasonable.

Have fun and enjoy.

Bonus - CGI interface

As a bonus I’m throwing in instructions on how to setup the CGI interface that will allow you to see the UPS/Battery status in real time.

spacer

References

This is one of my more popular articles. If you have any suggestions for updates or new articles like this please don’t hesitate to leave a comment below, or email me directly at zpeters@gmail.com

Related Posts

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.