View unanswered posts | View active topics It is currently Thu Jun 21, 2018 4:18 am



Reply to topic  [ 5 posts ] 
 How-To: shutdown ESXi Free via an USB UPS 
Author Message

Joined: Fri Jul 23, 2010 12:08 am
Posts: 39
Location: Prato, Italy
Reply with quote
Post How-To: shutdown ESXi Free via an USB UPS
Author: Spike
Link to my blog post



Introduction

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.

Code:
sudo ssh-keygen


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

Code:
sudo ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.0.100


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

powerdown_esxi4.x_v0.4

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.

Code:
# Set here if you want to suspend or shutdown the Virtual Machines
# Options available: suspend ; shutdown
VM_STATE=suspend

# To enable logging, set the following variable to 1
LOG_ENABLED=1
LOG_FILE=/vmfs/volumes/4c55e39f-3d85baef-7253-e0cb4e42250b/powerdown-esxi4.log


Then transfer powerdown_esxi4. to ESXi

Code:
scp powerdown_esxi4.1_v0.4.sh root@192.168.0.100:/bin


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

Code:
chmod +x /bin/powerdown_esxi4.1_v0.4.sh




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

Code:
SHUTDOWNCMD "/sbin/shutdown -h +0"


and call a custom script instead

Code:
SHUTDOWNCMD "/usr/local/bin/nut-shutdown.sh"


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

Code:
#!/bin/sh
ssh root@192.168.0.100 'powerdown-esxi4.sh'


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

Code:
sudo upsrw -s ups.delay.shutdown=90 -u local_mon -p mypass apc@localhost


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

apcupsd

Edit /etc/apcupsd/apccontrol this way

Code:
doshutdown)
echo "UPS ${2} initiated Shutdown Sequence" | ${WALL}
ssh root@192.168.0.100 '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

Code:
tar -cvzf /bootbank/oem.tgz .ssh/ /bin/powerdown_esxi4.1_v0.4.sh


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,

Code:
mkdir oem
tar -xvzf oem.tgz -C oem/


copy the script preserving the directory structure of /bin

Code:
mkdir /bootbank/oem/bin/
cp /bin/powerdown_esxi4.1_v0.4.sh /bootbank/oem/bin/


copy also the ssh key

Code:
cp -R /.ssh/ /bootbank/oem/


compress again the archive

Code:
cd /bootbank/oem/
tar -cvzf /bootbank/oem.tgz `ls -A`


Enjoy :)



SOURCES:
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


Thu Apr 07, 2011 6:36 am
Profile

Joined: Sat Apr 23, 2011 10:02 pm
Posts: 1
Reply with quote
Post Re: How-To: shutdown ESXi Free via an USB UPS
I picked up a APC UPS today in the hopes of finding something like this. Tomorrow I'll see if i can get it to work.

Thanks


Sat Apr 23, 2011 10:07 pm
Profile

Joined: Thu Feb 25, 2010 1:08 am
Posts: 12
Reply with quote
Post Re: How-To: shutdown ESXi Free via an USB UPS
You can get something native here :
http://rene.margar.fr/2010/03/client-nu ... ware-esxi/


Wed May 18, 2011 5:37 am
Profile

Joined: Fri Jul 23, 2010 12:08 am
Posts: 39
Location: Prato, Italy
Reply with quote
Post Re: How-To: shutdown ESXi Free via an USB UPS
Thanks for the recommendation!
I already saw that post and I think what it is supposed to do, because I don't know very well french!
I think that the problem between NUT and my APC UPS will persist but I will give it a try!


Wed May 18, 2011 6:11 am
Profile

Joined: Sat Dec 08, 2012 11:46 pm
Posts: 2
Reply with quote
Post Re: How-To: shutdown ESXi Free via an USB UPS
Hi,

Im having a major issue trying to get /bootbank/oem.tgz to unpack the /bin/power.sh script. Its driving me crazy.

When i reboot the machine the power.sh file is gone. If i unpack the oem.tgz file, the power.sh file appears in /bin/ and will run (shutting down the VM's and then host fine)

I wonder if the issue is due to the way the file is being tar'd, as i get this error about absolute paths

Image

That said, if it was tar wrong, then why is is unpacking fine and then able to execute? Please any help would be HIGHLY appreciated!!!!

EDIT: ok, found the issue. I didn't realise the changes that came in ESX5.0

| The oem.tgz needs to be placed at /bootbank as well as /altbootbank
| "--- oem.tgz" needs to be added to both boot.cfg files at /altbootbank and /bootbank.

Sorted, my scripts are persistent!!


Sat Dec 08, 2012 11:54 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.