7 Replies Latest reply on Oct 4, 2018 2:20 PM by CaryTompkins_DST

    PowerCLI script to display vCPU usage count per VMhost

    AlbertWT Master

      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

        • 1. Re: PowerCLI script to display vCPU usage count per VMhost
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: PowerCLI script to display vCPU usage count per VMhost
            AlbertWT Master

            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.

            • 3. Re: PowerCLI script to display vCPU usage count per VMhost
              LucD Guru
              Community WarriorsvExpertUser Moderators

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

               

              Otherwise change the calculation to pCPU/vCPU.

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: PowerCLI script to display vCPU usage count per VMhost
                AlbertWT Master

                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,

                • 5. Re: PowerCLI script to display vCPU usage count per VMhost
                  LucD Guru
                  Community WarriorsUser ModeratorsvExpert

                  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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  2 people found this helpful
                  • 7. Re: PowerCLI script to display vCPU usage count per VMhost
                    CaryTompkins_DST Lurker

                    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!