Saturday, May 3, 2008

How to Configure UPS for ESX3 Host

Storm was forecasted for the place where I was staying and my friend was worried that his VMware host at home is going to crash since the UPS power will not last long. He made every arrangement like running one single host so that if he has to loose he will loose one single host. So we were trying to figure out if windows UPS service can determine how to shut down the windows OS then I am sure there might be a way to do so on ESX (VMWare says its stripdown version of linux ). We started hunting on net and found first article which says how to shutdown the virtual machine and then shut down the host


#####################################################################
#!/bin/bash
#
# UPS shutdown script for VMware ESX 3.0
#
# 20060911 First version by tooms@tooms.dk
#
OLDIFS=$IFS
IFS=$'n'

#####################################################################
# set the paths that the vmware tools need
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"


#####################################################################
# try do a nice shutdown of VM there is power
count_vm_on=0

for vm in `vmware-cmd -l` ; do
#echo "VM: " $vm
for VMstate in `vmware-cmd "$vm" getstate -q` ; do
#echo $VMstate

# If the VM is power ON
if [ $VMstate = "on" ] ; then
echo " "
echo "VM: " $vm
echo "State: is on and will now tell it to 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 turn it off hard"
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 off, so i skip it"
# if the VM is power suspended
elif [ $VMstate = "suspended" ] ; then
echo " "
echo "VM: " $vm
echo "State: is suspended, so i skip it"
# if state is getstate or =
else
printf ""
#echo "unknown state: " $VMstate
fi

done
done


########################################################################
# wait for up to 5 min for the VM to shutd
#
if [ $count_vm_on = 0 ] ; then
echo " "
echo "All VM is 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 -q` ; 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


#####################################################################
# checking if all the VM are off and if not then turn them off
for vm in `vmware-cmd -l` ; do
#echo "VM: " $vm
for VMstate in `vmware-cmd "$vm" getstate -q` ; do
# If the VM is power ON
if [ $VMstate = "on" ] ; then
echo " "
echo "Found this VM: " $vm
echo "it is stille on but now i will turn it off"
vmware-cmd "$vm" stop hard
sleep 2
fi
done
done


#####################################################################
# Will now shutdown the ESX server as all VM are now off
echo " "
echo "now all VM is down, so will shutdown ESX host now"
echo " "
shutdown -h now
IFS=$OLDIFS



Now we were sure that there it can be done so friend of mine informed that APC have also recently released a version of PowerChute Network Shutdown for ESX 3. I found the following article (Thanks Stefan Schuller) which guide us step by step to set it up . Here is the brief :

At customers I often need to setup ESX servers together with APC UPS devices. this guide describes how to do that.

APC has a guide on how to do this but it doesn’t totally cover the installation for VMware ESX 3.0.x For example it forgets to tell you about the firewall in ESX 3 that blocks some ports that need to be open to make the Powerchute Network Shutdown agent communicate with your APC ups (with network management card option installed)

Assumed:
you have an ESX 3 server running.
You have an APC UPS with a management card that is configured.
The IP address of the ESX server is registered to allow it to communicate with the management card.
You know the login, password and authentication phrase of the UPS management card.

This is a 3 step process

1. Configure the VMware ESX 3 firewall
2. Install the APC Network Shutdown agent
3. Configure the APC Network Shutdown agent

Step 1 - Configure the VMware ESX 3 firewall

Logon to your service console with your favorite ssh client (putty)

open up the firewall with the following commands


esxcfg-firewall -o 80,tcp,out,"APC tcp Port 80"
esxcfg-firewall -o 3052,tcp,in,"APC tcp Port 3052"
esxcfg-firewall -o 3052,udp,out,"APC udp Port 3052"
esxcfg-firewall -o 3052,udp,in,"APC udp Port 3052"

Move to the next step.

Step 2 - Install the APC Network Shutdown agent

Download the “PowerChute Network Shutdown v2.2.1 for VMWare” from the APC website called pcns-2.2.1-100.i386.rpm link(need an account to get it)

copy the file to the ESX service console and run the following command

rpm -ihv pcns-2.2.1-100.i386.rpm

Step 3 - Configure the APC Network Shutdown agent

Run the configuration script command:

/usr/local/bin/PowerChute/PCNSConfig.sh

Now a “wizard” will guide you to configure the agent follow the instructions.

after this you can configure the rest of the setup through a webinterface http://youresxserver:3052

In the event log link you can check if the esx server is communicating with the UPS. It should show a message that it established a connection.

Resource Links
APC install guide link
VMTN forum link


Firstly open the firewall ports

esxcfg-firewall -o 80,tcp,out,"APC PowerChute Port 80"

esxcfg-firewall -o 3052,tcp,out,"APC PowerChute Port 3052"

esxcfg-firewall -o 3052,tcp,in,"APC PowerChute Port 3052"

esxcfg-firewall -o 3052,udp,out,"APC PowerChute Port 3052"

esxcfg-firewall -o 3052,udp,in,"APC PowerChute Port 3052"


Then install the APC Network Shutdown Software

rpm -ihv pcns-2.2.1-100.i386.rpm


Then configure the software

/usr/local/bin/PowerChute/PCNSConfig.sh

Type 3 and press enter

Type in the IP address of the Management Card and press enter

Port 80

Enter the Username, Password and Authentication Phrase

Repeat for the second management card

Answer No to registering another management card

Answer Yes to starting the service


Now you can configure the event actions etc by pointing your web browser to http://servername:3052/


Other commands

/etc/rc.d/init.d/PowerChute stop

/etc/rc.d/init.d/PowerChute start

To uninstall, type rpm -e pcns


MOST IMPORTANT SETTING OF ALL (IMHO)

Once logged on to the web interface

Click the ComputerName link

Specify the Configure Shutdown settings

UNTICK Turn off the UPS after the shutdown finishes.

Click Apply


This step is EXTREMELY important, otherwise once this server has shutdown it will power off the UPS regardless of other servers connected to the UPS.

No comments: