VMware Cloud Community
dharq
Contributor
Contributor

Automated Graceful shutdown in case of power outage

Automated Graceful shutdown in case of power outage

specs : vSphere 6.7 HP R5000 UPS

I am looking for a way to shutdown in a certain sequence not just mass shutdown

example vm1, vm2, vm3  and last your DC, then host, UPD etc

I have read a lot of the option, posted online .. wondering if anyone here in the community

might have a better option. Step by Step

one of the option Scripts I have found

freenasid=`vim-cmd vmsvc/getallvms | sed '1d' | awk '/Eds-FS.vmx/{print$1}'`

vmids=`vim-cmd vmsvc/getallvms | sed '1d' | awk '{print$1}'`

for vmid in $vmids

do

if [ $vmid != $freenasid ]

then

powerstate=`vim-cmd vmsvc/power.getstate $vmid | sed '1d'`

if [ "$powerstate" = "Powered on" ]

then

onvmids="$onvmids $vmid"

fi

fi

done

for vmid in $onvmids

do

vim-cmd vmsvc/power.shutdown $vmid

done

exit 0

I have heard it works, but does not solve the problem -  I am looking for an answer to

the script is free for all, as the developer gave it out free to all and myself.

if someone has some experience in this that would be  great

thanks in advance.

10 Replies
IRIX201110141
Champion
Champion

Single Host with only free vSphere Hypervisor license or vCenter around?

Regards,
Joerg

0 Kudos
dharq
Contributor
Contributor

Vcenter / Cloud environment Multi Host

0 Kudos
IRIX201110141
Champion
Champion

Than you can use Tags or Custom Attributes to place the information in which order or grouped order you wanna shutdown your VMs. These information can be fetched by a powershell/perl/python  script. Same Script can use SNMP to fetch the UPS status.  APC and Eaton have Virtual Appliances for this.

Regards,
Joerg

0 Kudos
dharq
Contributor
Contributor

HP does not have a script as far as I can tell

APC has power chute, not compatible though

I have the R5000 HP UPS x2 , they are network ready now

via IPs

I need a template or something to do this with .. fill in the order and preferred

method ..

0 Kudos
dharq
Contributor
Contributor

so for a powershell

$VMList = Get-Content c:\temp\vm.txt

foreach($vmName in $VMList){

    $vm = Get-VM -Name $vmName

    if($vm.Guest.State -eq "Running"){

        Shutdown-VMGuest -VM $vm -Confirm:$false}

    else{   

        Stop-VM -VM $vm -confirm:$false

        write-host "Powering Down VM..."

        Start-Sleep 20

        write-host "Adjusting Compute..."

        set-vm -numCPU 2 -memorygb 4 -confirm:$false

        start-vm -confirm:$false

        write-host "Powering On Server..."

    }

}

how would I be sure things get entered into maintenance mode

before shutting down .. to avoid read/write corruption

what would you add to this to improve

0 Kudos
scott28tt
VMware Employee
VMware Employee

Moderator: Thread moved to the PowerCLI area.


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
LucD
Leadership
Leadership

I would definitely add a loop where I check the PowerState of these VMs.

Just to make sure they are all powered off before I continue shutting down the ESXi nodes.

I do hope that the script doesn't run on one of those VMs


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
daphnissov
Immortal
Immortal

I wrote an implementation for my home lab in PowerShell. Some things you'll need to think about and implement:

  • You'll want a way to delay the start of the script until N amount of seconds/minutes have elapsed. Otherwise, your script could fire when there's a momentary drop in power and it quickly returns. You'll have no good way to abort in that case.
  • You need to know how much runtime you have available and time your shutdown sequence to ensure it completes before your batteries are exhausted. Have you done this and know how much time you have to use?
  • A danger in using a guest shutdown method is when VMs hang. You'll need a watch loop to ensure a given VM gracefully shuts down within a given period of time. If that time is exceeded, you'll want to probably kill it.
  • You probably want to implement an ability to shutdown whatever storage you're using in the case it's not local storage.
0 Kudos
sjesse
Leadership
Leadership

Here is a PowerShell script I use that uses a custom function, it gives a VM 150 seconds to shut down before I force it off. All your really need to do is change the IP address to vcenter or ESXi host

. The stop-vmhost at the bottom shuts the esxi host off at the end.

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

function Shutdown-MyVM

{

    Param(

        [Parameter(Mandatory=$true)]

        [string] $vmname

    )

    $myvm=Get-VM $vmname

    $i=0

    while($myvm.PowerState -ne 'PoweredOff')

    {

        $ToolsStatus = ($myvm|  Get-View).Guest.ToolsStatus

        $ToolsStatus

        $i

        if($ToolsStatus -eq "toolsNotInstalled" -or $i -gt 5)

        {

            Write-Host "Stopping" $myvm.Name

            Stop-VM -VM $myvm -Confirm:$false

        }else{

            Write-Host "Shutting down" $myvm.Name

            Shutdown-VMGuest -VM $myvm -Confirm:$false

            sleep 30

            $i++

        }

        $myvm=Get-VM $vmname

        if($myvm.PowerState -eq 'PoweredOff')

        {

        break;

        }

    }

}

Connect-VIServer 192.168.2.6 -User root -Password Password

Shutdown-MyVM -vmname esxi2.sjlab.net

Shutdown-MyVM -vmname esxi3.sjlab.net

Shutdown-MyVM -vmname file3.sjlab.net

Shutdown-MyVM -vmname file1.sjlab.net

Stop-VMHost -Server 192.168.2.6 -Confirm:$false -Force

Disconnect-VIServer * -Confirm:$false -Force

0 Kudos
dharq
Contributor
Contributor

can this be moded to have shut down vm s from a text file in a certain order with maintenance mode

0 Kudos