# get vm name via VMID # $1 - VMID function GetVMName () { vmName=$(vim-cmd vmsvc/get.summary $1 | grep "name" | sed 's/.*"\(.*\)"[^"]*$/\1/') }
# get stop delay options of vms function GetStopDelay () { OUT=$(vim-cmd hostsvc/hostconfig | grep "stopDelay" | sed 's/[^-0-9]//g') stopDelay=( $OUT ) }
# vm shutdown # passing parameters to the function # echo "VMShutDown $1 $2" # $1 - VMId, $2 - stopDelay function VMShutDown () { GetVMName $1 stopTime=0 STATE=$(vim-cmd vmsvc/power.getstate $1 | grep "Power") if [ "$STATE" = "Powered off" ] then echo "VM $1 ($vmName) is stopped. " return 1 fi echo "Call VM $1 ($vmName) shutdown..." vim-cmd vmsvc/power.shutdown $1 sleep 5 if [ "$stopTime" -eq 0] then echo "Waiting for VM $1 ($vmName) shutdown..." fi while [ "$STATE" != "Powered off" ] do if [ "$stopTime" -ge "$2" ] then echo "Shutdown of VM $1 ($vmName) causes to fail. Call power off!" vim-cmd vmsvc/power.off $1 return 2 fi STATE=$(vim-cmd vmsvc/power.getstate $1 | grep "Power") stopTime=$(($stopTime+5)) sleep 5 done echo "VM $VM ($vmName) shutdown is successfully" return 3 }
# specific VM Shutdown # $1 - VMId function SpecificVMShutDown () { GetVMName $1 GetBootOrder element=1 for VM in ${bootOrder[@]} do if [ "$VM" -eq "$1" ] then GetVmStopDelay "${stopDelay[0]}" "${stopDelay["$element"]}" VMShutDown $VM $currentDelay return 1 fi element=$(($element+1)) done echo "VMId $1 is not found!!!" }
# Get full list of VMs ID function GetAllVMs () { OUT=$(vim-cmd vmsvc/getallvms |grep -o '^[0-9]*') allVMs=( $OUT ) }
# Find missed VMs in boot order # Add missed VM to boot order array function FindMissedVMs () { GetAllVMs for aVM in ${allVMs[@]} do exists=0 for oVM in ${bootOrder[@]} do if [ "$aVM" -eq "$oVM" ] then exists=1 break fi done if [ "$exists" -eq 0 ] then bootOrder=( "${bootOrder[@]}" "$aVM" ) stopDelay=( "${stopDelay[@]}" "-1" ) fi done }
# get boot order of vms function GetBootOrder () { OUT=$(vim-cmd hostsvc/hostconfig | grep "key = 'vim.VirtualMachine:" | sed 's/[^-0-9]//g') bootOrder=( $OUT ) GetStopDelay FindMissedVMs }
# use default or optional delay # $1 - default delay, $2 optional delay function GetVmStopDelay () { currentDelay="$1" if [ "$2" -gt 0 ] then currentDelay="$2" fi }
# order shutdown all VMS function OrderShutDown () { echo "Call order shutdown all VMs" GetBootOrder element=1 for VM in ${bootOrder[@]} do #echo "${stopDelay["$element"]}" GetVMName $VM echo "Beginning shutdown process: $vmName (VMID: $VM)..." GetVmStopDelay "${stopDelay[0]}" "${stopDelay["$element"]}" VMShutDown $VM $currentDelay element=$(($element+1)) done echo "Order shutdown has been executed" }
# verbose shutdown all VMS function VerboseShutDown () { echo "Call verbose shutdown all VMs" GetBootOrder for VM in ${bootOrder[@]} do STATE=$(vim-cmd vmsvc/power.getstate $VM | grep "Power") if [ "$STATE" != "Powered off" ] then GetVMName $VM echo "Call VM $VM ($vmName) shutdown" vim-cmd vmsvc/power.shutdown $VM fi done ControlVerboseShutDown }
# control process off verbose shutdown function ControlVerboseShutDown () { echo "Checking verbose shutdown all VMs" executed=0 stopTime=0 while [ "$executed" -eq 0 ] do errorCount=0 element=1 for VM in ${bootOrder[@]} do GetVMName $VM STATE=$(vim-cmd vmsvc/power.getstate $VM | grep "Power") if [ "$STATE" = "Powered off" ] then #echo "VM $VM ($vmName) is powered off. Checking next" element=$(($element+1)) continue fi if [ "$stopTime" -eq 0] then echo "Waiting for VM $1 ($vmName) shutdown..." fi GetVmStopDelay "${stopDelay[0]}" "${stopDelay["$element"]}" if [ "$stopTime" -ge "$currentDelay" ] then echo "Shutdown of VM $VM ($vmName) causes to fail. Call power Off!" vim-cmd vmsvc/power.off $VM fi errorCount=$(($errorCount+1)) element=$(($element+1)) done if [ "$errorCount" -eq 0 ] then echo "Verbose shutdown has been executed" executed=1 return 1 fi stopTime=$(($stopTime+10)) sleep 10 echo "Remaining time: $stopTime" done }
while getopts ":os:v" optname do case "$optname" in "o") OrderShutDown exit 1 ;; "s") VMID=$OPTARG SpecificVMShutDown "$VMID" exit 2 ;; "v") VerboseShutDown exit 3 ;; esac done
Source: https://habr.com/ru/post/188726/
All Articles