AlbertWT
Virtuoso
Virtuoso

PowerCLI script to display vCPU usage count per VMhost

Jump to solution

Hi All,

I’m looking to get some help here with powerCLI script to list each ESXi host total number of vCPU used to determine if there is any ESXi host that CPU is over committed.

As per performance suggestion in some vExpert blog, it is around 1: 3 ratio of pCPU core : vCPU allocated, so how to display the report with the powerCLI for something like this:

PRODESXi01
Total cores available: 32
Used by VM: 28

PRODESXi02
Total cores available: 32
Used by VM: 38

Therefore based on the result above, the PRODESXi02 is over committed by 6vCPU allocated to the VM.

Note: The total cores available is taken from the Logical processors value from the Summary tab when clicking on the vSphere console.

How to achieve that report using PowerCLI ?

Thanks

/* Any kind of comment or input would be greatly appreciated */
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You mean something like this ?

Foreach($esx in Get-VMHost){

    $vCPU = Get-VM -Location $esx | Measure-Object -Property NumCpu -Sum | select -ExpandProperty Sum

    $esx | Select Name,@{N='pCPU';E={$_.NumCpu}},

        @{N='vCPU';E={$vCPU}},

        @{N='Ratio';E={[math]::Round($vCPU/$_.NumCpu,1)}}

}


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

View solution in original post

7 Replies
LucD
Leadership
Leadership

You mean something like this ?

Foreach($esx in Get-VMHost){

    $vCPU = Get-VM -Location $esx | Measure-Object -Property NumCpu -Sum | select -ExpandProperty Sum

    $esx | Select Name,@{N='pCPU';E={$_.NumCpu}},

        @{N='vCPU';E={$vCPU}},

        @{N='Ratio';E={[math]::Round($vCPU/$_.NumCpu,1)}}

}


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

View solution in original post

AlbertWT
Virtuoso
Virtuoso

Whoa... that was quick 🙂

so yes, based on my understanding is that if the ratio is closer to 0.3 then the VMhost is CPU saturated or fully committed already.

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
LucD
Leadership
Leadership

Since I did vCPU/pCPU, you probably want to look at ratios of 3 or higher.

Otherwise change the calculation to pCPU/vCPU.


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

AlbertWT
Virtuoso
Virtuoso

Luc,

Many thanks for the quick script to calculate the CPU oversubscription ratio, I have updated the script below to the following:

ForEach ($esx in Get-VMHost) {

    $vCPU = Get-VM -Location $esx | 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)}}

}

but somehow it cannot be exported to Excel as CSV?

Please correct me if I'm wrong, Does the formula to calculate the over commit % is make sense to you ? so for example the CPU Overcommit (%) shown as -x% it means that the host is underutilized and if the CPU Overcommit (%) is at 100% it means that the 1:1 ratio of CPU overcommit ratio is fully allocated,

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
LucD
Leadership
Leadership

That is because the ForEach doesn't place anything on the pipeline.

This can be fixed by encapsulating the complete script in a call operator block.

&{ForEach ($esx in Get-VMHost) { 

     $vCPU = Get-VM -Location $esx | 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-Csv report.csv -NoTypeInformation -UseCulture


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

AlbertWT
Virtuoso
Virtuoso

Thakns Luc !

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
CaryTompkins_DS
Contributor
Contributor

I realize this is a 2 year old post, but wow was it helpful.  I had a director ask me for stats on an environment.  With a little modification I was able to use Luc's answer to provide CPU and Memory information for a cluster on a host-by-host basis.  As always, thank you for all your help and guidance Luc!

0 Kudos