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.
- Download Centos 6.3
- Create a new VM in ESX with the following:
- 512 MB ram
- 8 gig hdd – Thin provisioned
- Install Centos as normal
- Edit /etc/sysconfig/network-scripts/ifcfg-eth0 – set
onbootto yes reboot
- After installation add the USB controller and pass-through device from ESX. You should see something labeled “APC”
yum install usbutils
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” (http://elrepo.org/tiki/tiki-index.php)
- Add epel repo
rpm -Uvh http://ftp.osuosl.org/pub/fedora-epel/6/i386/epel-release-6-7.noarch.rpm
yum install apcupsd
chkconfig apcupsd on
- edit /etc/apcupsd/apcupsd.conf and set the following options
- UPSNAME – any name will do –
- UPSCABLE – usb –
- UPSTYPE – usb –
- DEVICE – leave this blank –
- UPSNAME – any name will do –
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
- Edit /etc/aliases and setup and alias for root, perferably an in internal and external user ex.
/etc/apcupsd/changmethis will simulate a “battery needs changing” alert and should trigger an email to be sent
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)
- ESX Console -> Vm -> Guest -> Install Tools
mount /dev/cdrom /mnt
cp /mnt/VMwareTools* /tmp
tar xzvf VMWareTools...
./vmware-install.pl(choose all defaults)
- reboot and confirm that ESX summary for the vm shows vmware tools running
- Test “guest” reboot through VM -> Power -> Restart Guest
Test Default UPS shutdown
- Safely shutdown all ESX vms “just in case”
- Leave the Centos vm running
- Unplug the power
tail /var/log/apcupsd.eventsand watch the UPS events. By default when 3 mins left will initiate a 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.
Wrapping It All Up
After you’ve tested remote access to the ESX server (run something like
ssh firstname.lastname@example.org "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
- Replace the traditional “shutdown -h ….” line with
ssh email@example.com "/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.
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.
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
- Browse to http://22.214.171.124/apcupsd
- http://blog.shvetsov.com/2011/11/ssh-into-esxi-5-host-using-public-key.html – How to add SSH keys to ESXi
- http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1014165 –