Random Post: VCAP-DCD Round-Up
RSS .92| RSS 2.0| ATOM 0.3
  • Home
  •  

    PowerCLI – Physical and Virtual CPU statistics

    June 29th, 2010

    I was recently tasked with providing information regarding CPU averages across our production environment, both physical and virtual.  I know there are other PowerCLI scripts floating around, but it was a good way for me to learn more PowerShell.

    Measure-object and getting the average to show two digits was one of the harder problems to solve (along with get-vmhost $_.NumCPU not working correctly with the version of PowerCLI I had installed).  Lines 29, 31, 49 and 51 are highlighted to show the snip-its.

    It will grab the CPU stats between the days you specify (lines 13,14) from your Virtual Center server, at line 10.  It will spit out a count of your physical hosts, CPUs, Virtual Machines and vCPUs.

    
    # Add in the VI Toolkit goodness
    if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
    {
     Add-PSsnapin VMware.VimAutomation.Core
    }
    
    # Connect to the vCenter server(s)
    $vcserver= @()
    
    $vcserver += connect-VIServer "_Your VC Server_"
    
    # define our start and finish days
    $startdate=(get-date).adddays(-7)
    $finishdate=(get-date).adddays(-1)
    
    # define our silly little counters
    $pCounter = 0
    $vCounter = 0
    $pCPU=0
    $vCPU=0
    
     # print stat dates
     write-host `n --- Statistics for $startdate through $finishdate --- `n
    
     # ---- grab stats for Physical ----
     foreach ($esx_server in (get-vmhost))
     {
     # grab the cpu.usage.average, days are configurable
     $pStats = (get-stat -entity $esx_server -stat cpu.usage.average -Start $startdate -Finish $finishdate | measure-object -property value -average)
     # we only want two decimal places
     $pAvg = [system.math]::round($pStats.average,2)
    
     write-host Physical: $esx_server.name CPU-Average: $pAvg
    
     # increment physical counter
     $pCounter++
    
     # add up the total number of pCPU in cluster
     $pCPU += $esx_server.NumCPU
     }
    
     write-host `nPhysical Host Count: $pCounter
     write-host Physical CPUs: $pCPU `n
    
     # ---- grab stats for Virtual ----
     foreach ($vm_server in (get-vm))
     {
     #grab the cpu.usage.average, days are configurable
     $vStats = (get-stat -entity $vm_server -stat cpu.usage.average -Start $startdate -Finish $finishdate | measure-object -property value -average)
     # we only want two decimal places
     $vAvg = [system.math]::round($vStats.average,2)
    
     write-host Virtual: $vm_server.name CPU-Average: $vAvg
    
     # increment virtual cointer
     $vCounter++
    
     # add up total number of vCPU in cluster
     $vCPU += $vm_server.NumCPU
     }
    
     write-host `nVirtual Host Count: $vCounter
     write-host Virtual CPUs: $vCPU `n
    
    # Disconnect from the vCenter server(s)
    disconnect-viserver $vcserver -Confirm:$False
    
    # END
    ###