Skip to content

Instantly share code, notes, and snippets.

@FlorianHeigl
Forked from gschora/configure_apcupsd
Last active November 6, 2020 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlorianHeigl/0dfbea090b5ac3741028d907fab49e2f to your computer and use it in GitHub Desktop.
Save FlorianHeigl/0dfbea090b5ac3741028d907fab49e2f to your computer and use it in GitHub Desktop.
configuring apcupsd to suspend all running VM and then shutdown the esxi 5.5 u1 host

Instructions

Description

these are instructions for automating the suspend and shutdown of ESXi VMs and host in case of a power failure. works with APC/Schneider SmartUPS 750xl and ESXi 5.5u1

Steps

  1. make a new VM and install Ubuntu-Server on it

  2. install apcupsd

# apt-get install apcupsd

  1. configure /etc/apcupsd/apcupsd.conf

  2. enable apcupsd

# vim /etc/default/apcupsd
    ISCONFIGURED=yes
  1. restart apcupsd # service apcupsd restart

  2. enable SSH on ESXi

    configuration-tab - security profile - properties (right upper corner)

  3. copy shutdown_esxi.sh on ESXi into datastore e.g. /vmfs/volumes/MyDataStore/scripts

  4. make the file executable # chmod +x shutdown_esxi.sh

  5. configure passwordless ssh access on ESXi

make keys on ubuntu server (as root, because apcupsd runs under root)

# ssh-keygen -i /root/.ssh/id_rsa-ups -t rsa -b 4096 -N ""

copy key to ESXi server
# cat /root/.ssh/id_rsa-ups.pub | ssh root@TARGET_IP "cat >> /etc/ssh/keys-root/authorized_keys"

now automated, passwordless login should work from the ubuntu server. Test using this command: # ssh root@TARGET_IP /bin/true ; echo $? (the first time it asks to store esxi's key in local database, answer with "yes")

  1. configure an apcupsd helper script to run remote script on esxi copy doshutdown to /etc/apcupsd (you mustn't rename it, otherwise apcupsd won't run it automatically) make it executable # chmod +x doshutdown

  2. restart apcupsd # service apcupsd restart

#!/bin/sh
WALL=wall
# this script is triggered in case the power fails
echo "suspending all VMs on ESXi..." | ${WALL}
# configured ssh-automatic-access with keys
ssh -i /root/.ssh/id_rsa-ups root@TARGET_IP "nohup /vmfs/volumes/MyDataStore/scripts/shutdown_esxi.sh > /dev/null 2>&1 &"
echo "finished shutting ESXi down..." | ${WALL}
# exit code 99 - apccontrol stops after this script, so no shutdown of this host. this is for testing purposes
# exit code 0 - apccontrol continues with shutdown after this script
exit 99
#/bin/sh
##########################################################################################################################
#courtesy of http://www.c-note.dk/2011/12/04/wmware-esxi-suspend-all-guests/
#
#copy this file somewhere to datastore
#e.g. ‘/vmfs/volumes/myDataStore/scripts/’
#
# looks which vm's are running, sends them into suspend, waits until suspending-process is finished, then powers off esxi
# completely
##########################################################################################################################
VMS=`vim-cmd vmsvc/getallvms | grep -v Vmid | awk '{print $1}'`
for VM in $VMS ; do
PWR=`vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"`
if [ "$PWR" == "Powered on" ] ; then
name=`vim-cmd vmsvc/get.config $VM | grep -i "name =" | awk '{print $3}' | head -1 | cut -d "\"" -f2`
echo "Powered on: $name"
echo "Suspending: $name"
vim-cmd vmsvc/power.suspend $VM > /dev/null &
fi
done
while true ; do
RUNNING=0
for VM in $VMS ; do
PWR=`vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"`
if [ "$PWR" == "Powered on" ] ; then
echo "Waiting..."
RUNNING=1
fi
done
if [ $RUNNING -eq 0 ] ; then
echo "Gone..."
break
fi
sleep 1
done
echo "Now we shutdown the host..."
/sbin/shutdown.sh && /sbin/poweroff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment