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

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

Last edited by on 9 October 2011 at 17:05


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 ghettoUPSHostShutdown.pl 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/id_rsa.pub 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 powerdown_esxi4.1_v0.4.sh 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 powerdown_esxi4.sh executable

[cce_bash]chmod +x /bin/powerdown_esxi4.1_v0.4.sh[/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/nut-shutdown.sh"[/cce]

Create the script /usr/local/bin/nut-shutdown.sh

ssh root@ ‘powerdown-esxi4.sh'[/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@ 'powerdown_esxi4.1_v0.4.sh'
${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/powerdown_esxi4.1_v0.4.sh[/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/powerdown_esxi4.1_v0.4.sh /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: ghettoUPSHostShutdown.pl

vuemuer.it – Spegnere ESXi tramite un UPS APC

Categories: VMWare Tags: , multiplemultiplemultiple
  1. Phil
    October 4th, 2011 at 11:10 | #1

    Hi “Spike”,

    Thank you so much for your script.  This is now being used on my home ESXi 5.0 machine with a USB-attached APC UPS and apcupsd.

    I did notice a coupe of things when looking at the log though.  It seemed to me that the reference to “VM-FILE” in these two lines:

            echo -e `date “+%Y-%m-%d   %H:%M:%S”` “\t\tVM with ID: ${VM_FILE} is: $STATE, skipping…” >> $LOG_FILE

            echo -e `date “+%Y-%m-%d   %H:%M:%S”` “\t\tVM with ID: ${VM_FILE} is: $STATE, trying guest shutdown…” >> $LOG_FILE

    needed to be changed to “VM-LINE” to insert the ID of the VM.

    I also added a line  to dump the output from vmsvc/getallvms at the start of the log :

    #retrieve all VmId for VM(s) registered under ESXi host
    ${VIMSH_WRAPPER} vmsvc/getallvms >> $LOG_FILE

    I know almost nothing about scripts but these changes worked for me.

    Thank you once again.


  2. SteveL
    February 8th, 2012 at 19:37 | #2

    I almost have this working on ESXI 5.  I had to move the key into /etc/ssh/keys-root/authorized_keys where ESXI 5 actually preserves it.  I got the host script to shutdown the machine via the script on the VM too.  However, when the machine comes back up the script is missing even though it’s in oem.tgz.  Some Google searching implies ESXI 5 may no longer support oem.tgz?

  3. timic
    April 19th, 2013 at 09:53 | #3

    2 SteveL:

    You can place powerdown script on your virtual machine. Just add these two line to nut-shutdown.sh:

    scp /somewhere/powerdown-esxi4.sh root@
    ssh root@ ‘chmod +x /bin/powerdown-esxi4.sh
    ssh root@ ‘powerdown-esxi4.sh’

  1. February 3rd, 2014 at 16:42 | #1