Contributor
Contributor

VM - Host-DS Affinity/Stretched cluster environment

Hi,


We have a stretched cluster active/active. We need to ensure VM's are running on their local SAN storage as such.  DRS is Automated so VM/host affinity is fine. I found a script that works against another smaller vSphere 5.5 estate, but the below does not work for my larger vSphere 6.0 estate.

Each host has a three letter prefix of its data center as do the Datastores. So each DC relates to its Host and VMFS DS.

I also need to exclude multiple servers  not just one. At the end of it, an output in an email of a VM that is running across the DC link out of its neighboring SAN is what I want to achieve.

Is the script incorrect for vSphere 6, how is the best way to troubleshoot step by step and ensure the formatting is correct to achieve the correct results? Help appreciated Smiley Happy

$vms = Get-View -ViewType VirtualMachine -Property Name,Datastore,"Runtime.Host",  "Runtime.PowerState"

#

#

$emailbody = foreach($vm in $vms){

    if ($vm.Name -ne "vM name" -and $vm.Name -ne "balh")

    {

       if ($vm.Runtime.PowerState -eq "poweredOn")

       {

        $esx = Get-View $vm.Runtime.Host -Property Name

        if ($esx.Name.Split('.')[0] -ne "rara" )

        {

            $ds = Get-View $vm.Datastore -Property Name

            if ($ds | where {$_.Name.Substring(1,3) -notmatch $esx.Name.Substring(0,3)})

          

            {

         

              

                       $vm | Select Name,

                        @{N="Host";E={$esx.Name.Split('.')[0]}},

                        @{N="DataStore";E={[string]::Join(',',($ds | %{$_.Name}))}} | out-string

#write-host $emailbody

              

            }

        }

        }

    }

}

$MailSender = "xx"

$MailSmtpServer = "aa"

$MailSendingTo = "rar"

if ($emailbody)

{

write-host "These VMs are misconfigured and will fail during an outage of either data center, and they will fail with an outage of the link between data centres.  Fix the VMware DRS rule on $MailSender so these VMs run on host and disks in the same data center.  These VMs with disks and hosts spanning two locations are at risk of needless loss of service to our customers:

$emailbody"

$emailbody2 = "These VMs are misconfigured and will fail during an outage of either data center, and they will fail with an outage of the link between data centres.  Fix the VMware DRS rule on $MailSender so these VMs run on host and disks in the same data center.  These VMs with disks and hosts spanning two locations are at risk of needless loss of service to our customers:

$emailbody"

Send-MailMessage -from $MailSender -to $MailSendingTo -subject "alert: VM Host/Disk affinity violations" -body $emailbody2 -smtpServer $MailSmtpServer

}

else

{

write-host "ok: host/datastore affinity is correct for all VMs in $MailSender"

$emailbody2 = "host/datastore affinity is correct for all VMs in $MailSender"

Send-MailMessage -from $MailSender -to $MailSendingTo -subject "ok: no VM Host/Disk affinity violations" -body $emailbody2 -smtpServer $MailSmtpServer

9 Replies
Leadership
Leadership

Could you give a sample name layout with the prefix, for the ESXi nodes and the datastores?
I'm not understanding the SubString starting at 0 and 1.

Summarised, you want to discover running VMs where the 3-letter prefix for ESXi node and datastore do not match?


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

Contributor
Contributor

Thanks for the reply. DC's are mirrored, and I do not want IO read across the WAN link. I could use storage DRS, but we also have two different storage arrays. VM's need to run on their local sites hosted storage.

Host/Datastore DC1: COL

Host/Datastore DC2: IWB

In some cases the hosts have a lower case name, as do the DS.

Example:

DS:Col_Main_Shared_01

Host:colvh001.null.null.org

DS:IWB_Main_Shared_01

Host: iwbvh001.x.x.org

0 Kudos
Leadership
Leadership

Can we first check if the following snippet selects the correct VMs?
Define the VMs that need to be excluded in the $excludedVM

$excludedVM = 'vm1','vm2'

$sView = @{

    ViewType = 'VirtualMachine'

    Property = 'Name','Datastore','Runtime.Host','Runtime.PowerState'

    Filter = @{

        'Name' = "^((?!$($excludedVM -join '|')).)*$"

        'Runtime.PowerState' = 'poweredOn'

    }

}

Get-View @sView | ForEach-Object -Process {

    $esx = Get-View -Id $_.Runtime.Host -Property Name

    $esxPre = $esx.Name.Substring(0,3)

    $ds = Get-View -Id $_.Datastore[0] -Property Name

    $dsPre = $ds.Name.Substring(0,3)

    if($esxPre -ne $dsPre){

        $_ | Select Name,

            @{N='VMHost';E={$esx.Name}},

            @{N='Datastore';E={$ds.Name}}

    }

}


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

0 Kudos
Contributor
Contributor

It seemed to work in a fashion, but also report on correct entries too for the following DS, seemed to be more so for the '_VNX' DS.

1. Col_XXX_shared_01_VNX

2. IWB_XXXX_shared_03

3. Col_xxxx_main_shared_01

There were lots more, but this covers it I hope.

Smiley Sad  How can I add exclusion of certain RDM attached hosts too please?

Thanks

0 Kudos
Leadership
Leadership

Could you give 1 or more examples of such false positives?
Not sure how entries where the names of the datastore and the ESXi node both start with the same 3-letter prefix.

To exclude specific RDM, we will need to look at the harddisk(s) for each VM.

How would these RDMs be provided?
By the underlying LUN's Canonicalname?


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

0 Kudos
Contributor
Contributor

I don't need to exclude RDM's, just hosts, as we have specific clusters. Can I PM you? Thanks

0 Kudos
Leadership
Leadership

Sure


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

0 Kudos
Contributor
Contributor

I can't PM you, bombs out with a small red banner, I'll have to message you another way Smiley Happy

0 Kudos
Leadership
Leadership

On my blog (see my footer) there is a Contact form you can use.

It also allows for attachments.


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

0 Kudos