diff --git a/esxi-scripts/esxi-reboot-inmain.sh b/esxi-scripts/esxi-reboot-inmain.sh new file mode 100644 index 00000000..9f65470f --- /dev/null +++ b/esxi-scripts/esxi-reboot-inmain.sh @@ -0,0 +1,37 @@ +#!/bin/sh + + +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 put the Host into maintenance mode..." +vim-cmd hostsvc/maintenance_mode_enter + + +esxcli system shutdown reboot -d 10 -r "Automated ESXi host reboot" + +exit \ No newline at end of file diff --git a/esxi-scripts/esxi-reboot.sh b/esxi-scripts/esxi-reboot.sh new file mode 100644 index 00000000..a53ea415 --- /dev/null +++ b/esxi-scripts/esxi-reboot.sh @@ -0,0 +1,39 @@ +#!/bin/sh + + +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 put the Host into maintenance mode..." +vim-cmd hostsvc/maintenance_mode_enter + + +esxcli system shutdown reboot -d 10 -r "Automated ESXi host reboot" + +esxcli system maintenanceMode set -e false -t 0 + +exit \ No newline at end of file diff --git a/esxi-scripts/esxi-shutdown-inmain.sh b/esxi-scripts/esxi-shutdown-inmain.sh new file mode 100644 index 00000000..ad5abf20 --- /dev/null +++ b/esxi-scripts/esxi-shutdown-inmain.sh @@ -0,0 +1,81 @@ +#!/bin/sh +# ESXi 5.1 host automated shutdown script + +# these are the VM IDs to shutdown in the order specified +# use the SSH shell, run "vim-cmd vmsvc/getallvms" to get ID numbers +# specify IDs separated by a space +SERVERIDS=$(vim-cmd vmsvc/getallvms | sed -e '1d' -e 's/ \[.*$//' | awk '$1 ~ /^[0-9]+$/ {print $1}') + +# New variable to allow script testing, assuming the vim commands all work to issue shutdowns +# can be "0" or "1" +TEST=0 + +# script waits WAIT_TRYS times, WAIT_TIME seconds each time +# number of times to wait for a VM to shutdown cleanly before forcing power off. +WAIT_TRYS=20 + +# how long to wait in seconds each time for a VM to shutdown. +WAIT_TIME=10 + +# ------ DON'T CHANGE BELOW THIS LINE ------ + +validate_shutdown() +{ + vim-cmd vmsvc/power.getstate $SRVID | grep -i "off\|Suspended" > /dev/null 2<&1 + STATUS=$? + + if [ $STATUS -ne 0 ]; then + if [ $TRY -lt $WAIT_TRYS ]; then + # if the vm is not off, wait for it to shut down + TRY=$((TRY + 1)) + echo "Waiting for guest VM ID $SRVID to suspend (attempt #$TRY)..." + sleep $WAIT_TIME + validate_shutdown + else + # shutdown and wait a little (you could use vmsvc/power.suspend here instead) + echo "Unable to suspend guest VM ID $SRVID... shutting down instead." + if [ $TEST -eq 0 ]; then + vim-cmd vmsvc/power.shutdown $SRVID + fi + sleep $WAIT_TIME + fi + fi +} + + +# read each line as a server ID and suspend/shutdown +for SRVID in $SERVERIDS +do + TRY=0 + + vim-cmd vmsvc/power.getstate $SRVID | grep -i "off\|Suspended" > /dev/null 2<&1 + STATUS=$? + + if [ $STATUS -ne 0 ]; then + echo "Shutting down guest VM ID $SRVID..." + if [ $TEST -eq 0 ]; then + vim-cmd vmsvc/power.shutdown $SRVID + fi +# validate_shutdown + else + echo "Guest VM ID $SRVID already off..." + fi +done + +# guest vm shutdown complete +echo "Guest VM shutdown complete..." + +# enter maintenance mode immediately +echo "Entering maintenance mode..." +if [ $TEST -eq 0 ]; then + esxcli system maintenanceMode set -e true -t 0 & +fi + +# shutdown the ESXi host +echo "Shutting down ESXi host after 10 seconds..." +if [ $TEST -eq 0 ]; then + esxcli system shutdown poweroff -d 10 -r "Automated ESXi host shutdown - esxidown.sh" +fi + +# exit the session +exit \ No newline at end of file diff --git a/esxi-scripts/esxi-shutdown.sh b/esxi-scripts/esxi-shutdown.sh new file mode 100644 index 00000000..ecc3766f --- /dev/null +++ b/esxi-scripts/esxi-shutdown.sh @@ -0,0 +1,87 @@ +#!/bin/sh +# ESXi 5.1 host automated shutdown script + +# these are the VM IDs to shutdown in the order specified +# use the SSH shell, run "vim-cmd vmsvc/getallvms" to get ID numbers +# specify IDs separated by a space +SERVERIDS=$(vim-cmd vmsvc/getallvms | sed -e '1d' -e 's/ \[.*$//' | awk '$1 ~ /^[0-9]+$/ {print $1}') + +# New variable to allow script testing, assuming the vim commands all work to issue shutdowns +# can be "0" or "1" +TEST=0 + +# script waits WAIT_TRYS times, WAIT_TIME seconds each time +# number of times to wait for a VM to shutdown cleanly before forcing power off. +WAIT_TRYS=20 + +# how long to wait in seconds each time for a VM to shutdown. +WAIT_TIME=10 + +# ------ DON'T CHANGE BELOW THIS LINE ------ + +validate_shutdown() +{ + vim-cmd vmsvc/power.getstate $SRVID | grep -i "off\|Suspended" > /dev/null 2<&1 + STATUS=$? + + if [ $STATUS -ne 0 ]; then + if [ $TRY -lt $WAIT_TRYS ]; then + # if the vm is not off, wait for it to shut down + TRY=$((TRY + 1)) + echo "Waiting for guest VM ID $SRVID to suspend (attempt #$TRY)..." + sleep $WAIT_TIME + validate_shutdown + else + # shutdown and wait a little (you could use vmsvc/power.suspend here instead) + echo "Unable to suspend guest VM ID $SRVID... shutting down instead." + if [ $TEST -eq 0 ]; then + vim-cmd vmsvc/power.shutdown $SRVID + fi + sleep $WAIT_TIME + fi + fi +} + +# enter maintenance mode immediately +echo "Entering maintenance mode..." +if [ $TEST -eq 0 ]; then + esxcli system maintenanceMode set -e true -t 0 & +fi + +# read each line as a server ID and suspend/shutdown +for SRVID in $SERVERIDS +do + TRY=0 + + vim-cmd vmsvc/power.getstate $SRVID | grep -i "off\|Suspended" > /dev/null 2<&1 + STATUS=$? + + if [ $STATUS -ne 0 ]; then + echo "Shutting Down guest VM ID $SRVID..." + if [ $TEST -eq 0 ]; then + vim-cmd vmsvc/power.shutdown $SRVID + fi + validate_shutdown + else + echo "Guest VM ID $SRVID already off..." + fi +done + +# guest vm shutdown complete +echo "Guest VM shutdown complete..." + +# shutdown the ESXi host +echo "Shutting down ESXi host after 10 seconds..." +if [ $TEST -eq 0 ]; then + esxcli system shutdown poweroff -d 10 -r "Automated ESXi host shutdown - esxidown.sh" +fi + +# exit maintenance mode immediately before server has a chance to shutdown/power off +# NOTE: it is possible for this to fail, leaving the server in maintenance mode on reboot! +echo "Exiting maintenance mode..." +if [ $TEST -eq 0 ]; then + esxcli system maintenanceMode set -e false -t 0 +fi + +# exit the session +exit \ No newline at end of file diff --git a/esxi-scripts/esxi-shutdown2.sh b/esxi-scripts/esxi-shutdown2.sh new file mode 100644 index 00000000..4547453a --- /dev/null +++ b/esxi-scripts/esxi-shutdown2.sh @@ -0,0 +1,45 @@ +#/bin/sh + +#Shutdown servers with pass-through devices +#vim-cmd vmsvc/power.shutdown 3 + + +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 put the Host into maintenance mode..." +vim-cmd hostsvc/maintenance_mode_enter + + +echo "Now we suspend the Host..." +#vim-cmd hostsvc/standby_mode_enter +esxcli system shutdown poweroff -d 10 -r "Shell initiated system shutdown" + + +esxcli system maintenanceMode set -e false -t 0 + +exit \ No newline at end of file diff --git a/esxi-scripts/esxi-vmshutdown.sh b/esxi-scripts/esxi-vmshutdown.sh new file mode 100644 index 00000000..7a587895 --- /dev/null +++ b/esxi-scripts/esxi-vmshutdown.sh @@ -0,0 +1,31 @@ +#!/bin/sh + + +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 "Shutting down: $name" + vim-cmd vmsvc/power.shutdown $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 + +exit \ No newline at end of file diff --git a/esxi-scripts/esxi-vmsuspend.sh b/esxi-scripts/esxi-vmsuspend.sh new file mode 100644 index 00000000..3ca17ebf --- /dev/null +++ b/esxi-scripts/esxi-vmsuspend.sh @@ -0,0 +1,31 @@ +#!/bin/sh + + +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 + +exit \ No newline at end of file