Automation

 View Only
  • 1.  PowerCLI script to display vCPU usage count per VMhost

    Posted Jan 16, 2015 06:43 AM

    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



  • 2.  RE: PowerCLI script to display vCPU usage count per VMhost
    Best Answer

    Posted Jan 16, 2015 06:52 AM

    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)}}

    }



  • 3.  RE: PowerCLI script to display vCPU usage count per VMhost

    Posted Jan 16, 2015 07:02 AM

    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.



  • 4.  RE: PowerCLI script to display vCPU usage count per VMhost

    Posted Jan 16, 2015 07:06 AM

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

    Otherwise change the calculation to pCPU/vCPU.



  • 5.  RE: PowerCLI script to display vCPU usage count per VMhost

    Posted Jan 19, 2015 02:00 AM

    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,



  • 6.  RE: PowerCLI script to display vCPU usage count per VMhost

    Posted Jan 19, 2015 05:51 AM

    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



  • 7.  RE: PowerCLI script to display vCPU usage count per VMhost

    Posted Jan 19, 2015 06:50 AM

    Thakns Luc !



  • 8.  RE: PowerCLI script to display vCPU usage count per VMhost

    Posted Oct 04, 2018 09:20 PM

    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!