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.
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.
onbootto yes reboot
yum install usbutils
lsusb– see if apc is there
The default package repo in Centos did not have the apcups packages so we need to add “epelrepo” (http://elrepo.org/tiki/tiki-index.php)
rpm -Uvh http://ftp.osuosl.org/pub/fedora-epel/6/i386/epel-release-6-7.noarch.rpm
yum install apcupsd
chkconfig apcupsd on
service apcupsd start
apcaccess(to test). You should see something like the output below
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 :-)
yum install sendmail
chkconfig sendmail on
service sendmail start
/etc/apcupsd/changmethis will simulate a “battery needs changing” alert and should trigger an email to be sent
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)
mount /dev/cdrom /mnt
cp /mnt/VMwareTools* /tmp
tar xzvf VMWareTools...
./vmware-install.pl(choose all defaults)
tail /var/log/apcupsd.eventsand watch the UPS events. By default when 3 mins left will initiate a shutdown
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.
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
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.
After you’ve tested remote access to the ESX server (run something like
ssh email@example.com "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.
/etc/apcupsd/apccontroland find the line for doshutdown
ssh firstname.lastname@example.org "/scripts/shutdown-all-vms.sh"
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.
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.
yum install apcupsd-cgi
chkconfig httpd on
service httpd start
/etc/sysconfig/iptables(add a line to allow 80 in – copy the line for ssh/22)
service iptables restart
/etc/httpd/conf.d/apcupsd.conf(comment out Allow from lines and add Allow from all)
service httpd reload