corey201110141
Contributor
Contributor

How to identify datastores with too little remaining space to allow for vMotion?

I'm trying to use the powershell powercli to identify datastores that cannot vMotion at least one of the hosts.  The limit here is that the memory is copied to the local LUN (in this scenario datastore:LUN is 1:1 relationship).  I'm basically scanning to find all the datastores, then the maximum memory for a given host on that datastore, and then comparing the remaining space with that maximum.  The process takes a longtime for the size of our environment.  I'm wondering if anyone has a better solution to the same problem.

Related code:

$date = Get-Date
writeLog "Begin $date"
Add-PSSnapin VMware.VimAutomation.Core

$Debug = $True
$Verbose = $True
[string]$strVCenters = $args[0]
$arrVCenters = $strVCenters.split(" ")

[string]$strOutputPath = $args[1]
If($strOutputPath -eq ""){$strOutputPath = "RawData\"}

$arrVCenters = "VC-1","VC-2"

[string]$strProblem = ""

foreach($vCenter in $arrVCenters){
Disconnect-VIServer * -confirm:$false
If($Verbose){writeLog "--------------------Processing $vCenter"}
$connect = Connect-VIServer -server $vCenter
$strPath = $strOutputPath+"Failed to Connect.txt"
If(!$connect){
  IF($Debug){writelog "Could not connect to $vCenter"}
  writeOut $vCenter $strPath
}
else {
  #Get Datastore data
  If ($Debug) {writeLog "Grabbing datastore settings"}
  $DataStoreQuery = Get-Datastore -server $vCenter
  If($DataStoreQuery) {
   $strPath = $strOutputPath+"Storage.txt"
   writeOut "Virtual Center,Datastore,Max Memory for a Virtual Host (MB),Amount of free space(MB),Host with Max Provisioned space (Space in GB),Host with Minimum Provisioned space (Space in GB)" $strPath
   foreach ($dataStore in $DataStoreQuery) {
    $virtualHosts = get-vm -DataStore (get-dataStore -name $dataStore.Name)
    $MaxProvisionedSpace = 0
    $MinProvisionedSpace = 50000
    $maxMemory = 0
    $nonCompliant = $false
    foreach($virtualHost in $virtualHosts) {
     If($virtualHost.MemoryMB -ge $dataStore.FreeSpaceMb) {
      $nonCompliant = $true
     }
     If($virtualHost.ProvisionedSpaceGB -gt $MaxProvisionedSpace) {
      $MaxProvisionedSpace = $virtualHost.ProvisionedSpaceGB
      $MaxProvisionedHost = $virtualHost.Name
     }
     If($virtualHost.ProvisionedSpaceGB -lt $MinProvisionedSpace) {
      $MinProvisionedSpace = $virtualHost.ProvisionedSpaceGB
      $MinProvisionedHost = $virtualHost.Name
     }
     If($virtualHost.MemoryMB -gt $maxMemory) {$maxMemory = $virtualHost.MemoryMB}    
    }
    If($nonCompliant) {   
     $freedataStore = $dataStore.FreeSpaceMB
     $name = grabNameFromUID $dataStore.UID
     $value = $dataStore.Name
     writeOut "$name,$value,$maxMemory,$freedataStore,$MaxProvisionedHost ($MaxProvisionedSpace),$MinProvisionedHost ($MinProvisionedSpace)" $strPath
    }
   }
  }
}
}

0 Kudos
5 Replies
LucD
Leadership
Leadership

I'm not sure I understand why you compare the memory allocation of a guest with the remaining free space of a datastore ?


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

0 Kudos
corey201110141
Contributor
Contributor

There is some information here: http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=101417....  Basically I'm trying to ensure that there is enough space to copy the swap file to the local datastore in order to facilitate a vMotion to a different datastore.  If I cannot copy the swap to the local datastore, then the vmotion could fail.  The audit is identifying datastores with less remaining space then the max memory allocated to at least one host on that datastore.

The problem is, for the number of datastores and virtual centers we have in place, the query:

$virtualHosts = get-vm -DataStore (get-dataStore -name $dataStore.Name)

makes the script take forever to finish (~20 hours).

0 Kudos
RvdNieuwendijk
Leadership
Leadership

Your script will be faster if you change the line

$virtualHosts = get-vm -DataStore (get-dataStore -name $dataStore.Name)
 

into

$virtualHosts = get-vm -DataStore $dataStore
 

You already have the datastore object in the $dataStore variable. So you don't have to do a Get-Datastore cmdlet again.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
avlieshout
VMware Employee
VMware Employee

Welcome to the forum.

First of all, the terms "host" and "$VirtualHost" are confusing, better us "VM" and  "$vm".

Host are the ESX servers that host virtual machines (VM)

Second, whenever there isn't enough freespace available on your local datastore to host the swapfile, ESX will revert to placing the swapfile in the same directory as the VM. So insufficient space on your local datastore doesn't always mean you can't vMotion.

Third, there's no need to retrieve the datastore object twice as $datastore is already a datastore object in:

$virtualHosts = get-vm -DataStore (get-dataStore -name $dataStore.Name)

replace this line with:

$virtualHosts = get-vm -DataStore $dataStore

or even

$virtualHosts = $dataStore | get-vm

This will probably speed up things a little.

Cheers,

Arnim

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
corey201110141
Contributor
Contributor

Thanks for the feedback.  I implemented the change which reduced the time to execute, but I'm still looking at a considerable time to complete.  Any other suggestions would be appreciated.

0 Kudos