A little while back, I was looking into getting Power Chute set up for our ESX hosts. The unfortunate news is that APC does not yet support ESX 3.5 U2. While researching this, I came across a script to shut down all guest OSes and then an ESX host, though, which I have slightly modified and deployed to all of my ESX hosts. The original can be found at http://www.tooms.dk/ By copying the script to a location that can be accessed in my PATH, I can quickly log on to each server in the case of disaster and type 'esxkill". This will usually shut everything down gracefully in 5-10 minutes. Here's the syntax:
#####################################################################
#!/bin/sh
#
# UPS shutdown script for VMware ESX 3.5 U1
#
# 20060911 First version by tooms@tooms.dk
# 20081006 Revised by ian.reasor@gmail.com
#####################################################################
# Set PATH variable to the location of ESX utilities
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
#####################################################################
# Attempt a graceful shutdown through VMWare Tools
count_vm_on=0
for vm in `vmware-cmd -l` ; do
#echo "VM: " $vm
for VMstate in `vmware-cmd "$vm" getstate` ; do
#echo $VMstate
# If the VM is power ON
if [ $VMstate = "on" ] ; then
echo " "
echo "VM: " $vm
echo "State: is on and will now shut down"
echo "Shutting down: " $vm
vmware-cmd "$vm" stop trysoft
vmwarecmd_exitcode=$(expr $?)
if [ $vmwarecmd_exitcode -ne 0 ] ; then
echo "exitcode: $vmwarecmd_exitcode so will now power off"
vmware-cmd "$vm" stop hard
fi
count_vm_on=$count_vm_on+1
sleep 2
# if the VM is power OFF
elif [ $VMstate = "off" ] ; then
echo " "
echo "VM: " $vm
echo "State: is already powered off"
# if the VM is power suspended
elif [ $VMstate = "suspended" ] ; then
echo " "
echo "VM: " $vm
echo "State: is already suspended"
# if state is getstate or =
else
printf ""
#echo "unknown state: " $VMstate
fi
done
done
########################################################################
# wait for up to 5 min for VMs to shut down
#
if [ $count_vm_on = 0 ] ; then
echo " "
echo "All VMs are powered off or suspended"
else
echo " "
vm_time_out=300
count_vm_on=0
echo "Waiting for VMware virtual machines."
for (( second=0; second<$vm_time_out; second=second+5 )); do sleep 5 printf "." count_vm_on=0 for vm in `vmware-cmd -l` ; do for VMstate in `vmware-cmd "$vm" getstate` ; do if [ $VMstate = "on" ] ; then count_vm_on=$(expr $count_vm_on + 1) fi done done if [ $count_vm_on = 0 ] ; then #echo "exit for" break fi done #echo $VMstate fi #echo $count_vm_on ##################################################################### # Check to see if all VMs are off and if not then power them down for vm in `vmware-cmd -l` ; do #echo "VM: " $vm for VMstate in `vmware-cmd "$vm" getstate` ; do # If the VM is power ON if [ $VMstate = "on" ] ; then echo " " echo "VM: " $vm echo "it is still on but will now be powered off" vmware-cmd "$vm" stop hard sleep 2 fi done done ##################################################################### # Shutdown ESX Host echo " " echo "All VM's have been successfully shut down, halting ESX Host" echo " " shutdown -h now
Wednesday, November 5, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment