Tuesday, April 12, 2011

Shutting down VMware ESXi 4.x from apcupsd remote Server

I needed to automatically shutdown my ESXi 4.0 Server remotely by my apcupsd controlled Linux Server.
For this, I used this steps:

1. Enable the SSH Management Console on the ESXi Server (see ESXi console)

2. Create the /.ssh directory
ssh <esxi_server>
mkdir /.ssh
chmod 700 /.ssh

exit

3. copy your ssh private key (yes, you read right) to the ESXi server
#> scp ~/.ssh/id_rsa <vmware_server>:/.ssh/

4. Convert the ssh priv key to dropbear format and generate a public key from it
ssh <esxi_server>

dropbearconvert openssl dropbear /.ssh/id_rsa /.ssh/id_rsa.dropbear

dropbearkey -y /.ssh/id_rsa.dropbear > /.ssh/authorized_keys

rm /.ssh/id_rsa /.ssh/id_rsa.dropbear


5. Create the shutdown script /shutdown_apcupsd.sh on the esxi server:
#!/bin/sh

echo "Shutdown of VMs and ESXi Server initated"
echo " "
echo "Step1/2: Shutdown Autostart-VMs"
/sbin/shutdown.sh
echo "Step2/2: Shutdown Remaining VMs and Server"
/sbin/poweroff.sh
echo "Good Bye."

#> chmod 755 /shutdown_apcupsd.sh


6. On the apcupsd Server, create the shutdown script:
#> vi /etc/apcupsd/shutdown_esxi.sh



#!/bin/sh
########################################
ESXI_HOST=<esxi_server.yourdomain.tld>
ESXI_LOCAL_SHUTDOWN_SCRIPT=/shutdown_apcupsd.sh
########################################
echo "Invoking remote Shutdown of $
ESXI_HOST"
ssh $ESXI_HOST $ESXI_LOCAL_SHUTDOWN_SCRIPT


7. Add the shutdown script to apccontrol "doshutdown" case:
#> vi /etc/apcupsd/apccontrol

doshutdown)
echo "UPS ${2} initiated Shutdown Sequence" | ${WALL}
# ADDED FOR ESXI SHUTDOWN
/etc/apcupsd/shutdown-vmware-esxi &
# ADDED FOR ESXI SHUTDOWN END

${SHUTDOWN} -h now "apcupsd UPS ${2} initiated shutdown"
;;

No comments: