PowerCLI Script to Check Host Over-commitment Percentage & Ratios
- Ben Liebowitz
- 5
- 3674
At my previous employer, we had a Dev/QA vSphere environment that was very over provisioned. The environment had lots of CPUReady issues with VMs having to wait for CPU time, which caused things to be slow, etc. We knew this but the application teams didn’t understand. I ended up writing a PowerCLI script to check the CPU overcommitment and sent the results to my manager who was able to go to the application team’s management and get them to clean up, etc. The other day, my old boss reached out to ask if I had anything to check memory overcommitment as well. I decided to update my script.
A Prerequisite for this is the IMPORTEXCEL PowerShell module. If you’re on PowerShell v5, you can install it from the PowerShell Gallery with the command below:
Install-Module ImportExcel
I used this module to be able to write both the CPU and Memory stats to the same file. The script will export the CPU Overcommitment on one tab and memory on the other.
I added a section where it will prompt you to choose one of the vCenters to sign into, then list the clusters to run against. Also, the cluster name is included in the exported filename as well.
######################################################### # # PowerCLI Script to check Host Overcommitment (CPU & Memory) # Written by BLiebowitz on 6/29/17 # ######################################################### # Select which vCenter you want to connect to # Build array for each vCenter Write-host "Select which vCenter to connect to:" Write-Host "" Write-Host "1. vCenter1" Write-Host "2. vCenter2" Write-Host "3. vCenter3" $Ivcenter = read-host “Select a vCenter Server. Enter Number:“ if ($Ivcenter -match "1") { $vcenter = "vCenter1" } elseif ($Ivcenter -match "2") { $vcenter = "vCenter2" } else { $vcenter = "vCenter3" } write-host "" Write-Host "You Picked: $($vcenter)" write-host "" start-sleep -s 3 # connect to selected vCenter connect-viserver $vcenter # List Clusters write-host "" Write-host "Choose which Cluster you want to gather ratios on:" write-host "(it may take a few seconds to build the list)" write-host "" $ICLUSTER = get-cluster | Select Name | Sort-object Name $i = 1 $ICLUSTER | %{Write-Host $i":" $_.Name; $i++} $HCLUSTER = Read-host "Enter the number for the host to Patch." $SCLUSTER = $ICLUSTER[$HCLUSTER -1].Name write-host "You have selected $($SCLUSTER)." start-sleep -s 3 # Get CPU Overcommitment Information &{ForEach ($esx in (get-cluster $SCLUSTER | Get-VMHost)) { $vCPU = Get-VM -Location $esx | where {$_.PowerState -match "on"} | Measure-Object -Property NumCpu -Sum | select -ExpandProperty Sum $esx | Select Name, @{N='pCPU cores available';E={$_.NumCpu}}, @{N='vCPU assigned to VMs';E={$vCPU}}, @{N='Ratio';E={[math]::Round($vCPU/$_.NumCpu,1)}}, @{N='CPU Overcommit (%)';E={[Math]::Round(100*(($vCPU - $_.NumCpu) / $_.NumCpu), 1)}} # Export results to Excel }} | Export-Excel -WorkSheetname "CPU Overcommitment" -path e:\vmware\Host_overcommitment_$($SCLUSTER).xlsx # Get Memory Overcommitment Information &{ForEach ($esx in (get-cluster $SCLUSTER | Get-VMHost)) { $vMem = get-vm -location $esx | where {$_.PowerState -match "on"} | measure-object -property MemoryGB -SUM | Select -Expandproperty Sum $esx | Select Name, @{N='Total Memory Available';E={[Math]::Round($_.MemoryTotalGB),1}}, @{N='Memory Assigned to VMs';E={$vMem}}, @{N='Ratio';E={[math]::Round(100*($vMem / $_.MemoryTotalGB), 1)}}, @{N='Memory Overcommit (%)';E={[Math]::Round(100*(($vMem - $_.MemoryTotalGB) / $_.MemoryTotalGB), 1)}} # Export results to CSV }} | Export-Excel -WorkSheetname "Memory Overcommitment" -path E:\vmware\Host_overcommitment_$($SCLUSTER).xlsx # Disconnect from vCenter Disconnect-viserver $vcenter -confirm:$false
Here is what the results look like.
CPU Overcommitment:
Memory Overcommitment:
File Name:
I hope someone finds this useful like I did.
Ben Liebowitz, VCP, vExpert
NJ VMUG Leader
5 thoughts on “PowerCLI Script to Check Host Over-commitment Percentage & Ratios”
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Awesome stuff, Ben
Thanks!
Ben,
Your scripts says “patch” may want to update the verbiage to, Which Host do you want to report on? Looks like you re-used the selection/discovery phases from another script.
Thanks for finding that. Yes, I typically copy/paste the top section of my scripts from others to save time. I forgot to update the Choose a cluster comments, I’ve fixed it. 🙂
Great Stuff, Ben!