Posts Tagged ‘Network UPS Tools’

How-To: script to shutdown ESXi Free via an USB UPS monitoring tool

April 7th, 2011 3 comments


The purpose of this How-To is to let an USB UPS (in my case an APC Back-UPS 800VA) communicate to ESXi to shutdown or suspend VMs and then shutdown host in case of power failure.
The source script’s author is helux where if you can find the original thread and the original script in the sources links at the bottom of this article.
I have customized the script with a logging system and with an option to decide if to suspend or shutdown the virtual machines.


The problem

Due to the fact that, since VMWare ESXi 3.5U4, read and write access from RCLI (Remote Command Line Interface) has been disabled, we are unable to use scripts like written by lamw (William Lam).
Our only possibility is to install the USB UPS monitoring tool in a Virtual Machine, then communicate to ESXi via SSH and then run a script copied on the ESXi itself.

In my case I have an Ubuntu Server 10.10 installed as a Virtual Machine. Here I have installed apcupsd software to monitor the UPS.


Create ssh keys

I have generated an SSH key for root to let the VM communicate with ESXi.

[cce_bash]sudo ssh-keygen[/cce]

Do not set a passphrase for the private key otherwise we can’t automate the process.
After having enable the SSH server in ESXi, transfer the public key of the VM to ESXi

[cce_bash]sudo ssh-copy-id -i /root/.ssh/ root@[/cce]

Change the path to the public key file and the IP address of the ESXi.


The script

Download the script with wget in your linux VM or copy and paste the content in a new document


Edit the script and, at the beginning, customize it on your needs, such as enable logging system, set the log file, decide what VMs should do (suspend, shutdown) etc.

[cce_bash]# Set here if you want to suspend or shutdown the Virtual Machines
# Options available: suspend ; shutdown

# To enable logging, set the following variable to 1

Then transfer powerdown_esxi4. to ESXi

[cce_bash]scp root@[/cce]

Please note that if you transfer a text/script file from Windows via vSphere Client to a datastore, a line feed “^M” will be added so the file will be unusable.
If you want you can wget it.
Now login into your ESXi server and make executable

[cce_bash]chmod +x /bin/[/cce]


Change UPS monitor software behaviour

We have to tell our UPS monitor software that during the Shutdown Sequence he has to run our script.

Network UPS Tools

Edit /etc/nut/upsmon.conf, change the line

[cce_bash]SHUTDOWNCMD "/sbin/shutdown -h +0"[/cce]

and call a custom script instead

[cce_bash]SHUTDOWNCMD "/usr/local/bin/"[/cce]

Create the script /usr/local/bin/

ssh root@ ‘'[/cce]

The problem with Network UPS Tools and my APC UPS

I encountered 2 problems using my APC UPS:
1) NUT can’t power on my APC after power outage
2) My APC has ups.delay.shutdown= 20
That means that the amount of time the UPS will wait before shutting down is only 20s. I saw that I need at least 1:10 min, so I had to change it to 90s.
I did it with

[cce_bash]sudo upsrw -s ups.delay.shutdown=90 -u local_mon -p mypass apc@localhost[/cce]

but after a service restart, the UPS loses this setting.


Edit /etc/apcupsd/apccontrol this way

echo "UPS ${2} initiated Shutdown Sequence" | ${WALL}
ssh root@ ''
${SHUTDOWN} -h now "apcupsd UPS ${2} initiated shutdown"

The problem with apcupsd and my APC UPS

As stated in 2 thread in VMWare Communities:
ESXi-4.1: USB passthrough problem with APC Back-UPS CS 650
apcaccess: status output minimal when using USB device passthru on ESX

There is a problem in the USB passthrough of APC UPSes: apcupsd doesn’t recognize many parameters.
However essential parameters like LOADPCT (Percent Load Capacity), BCHARGE (battery charge), TIMELEFT, MBATTCHG (Minimum Battery Charge), MINTIMEL (Minimum Time Left) are properly recognized so we can continue using our script.
At the moment it is the best solution I have found.


Compress everything and generate oem.tgz

To load the ssh key and the script at system boot, you have to add it to /bootbank/oem.tgz, as ESXi will clear everything that doesn’t belong to the system itself.
If you don’t have any oem.tgz, create it with

[cce_bash]tar -cvzf /bootbank/oem.tgz .ssh/ /bin/[/cce]

If you have already an oem.tgz for something else, extract the content to a folder, as the tar installed in ESXi doesn’t support appending of files,

[cce_bash]mkdir oem
tar -xvzf oem.tgz -C oem/[/cce]

copy the script preserving the directory structure of /bin

[cce_bash]mkdir /bootbank/oem/bin/
cp /bin/ /bootbank/oem/bin/[/cce]

copy also the ssh key

[cce_bash]cp -R /.ssh/ /bootbank/oem/[/cce]

compress again the archive

[cce_bash]cd /bootbank/oem/
tar -cvzf /bootbank/oem.tgz `ls -A`[/cce]

Enjoy :)



VMWare /etc – ESXi U4 Ends Free Version Read and Write Access from the RCLI

VMWare Communities: esxi shutdown script for ups monitoring by helux

VMWare Communities: – Spegnere ESXi tramite un UPS APC

Categories: VMWare Tags: , , ,