PowerCLI

 View Only
  • 1.  How to get each CPU core utilization using powershell cmdlets?

    Posted Jul 21, 2009 01:33 PM

    Can get-stat used for getting all cpu cores utilization seprately?

    Thank you,

    Raj



  • 2.  RE: How to get each CPU core utilization using powershell cmdlets?

    Posted Jul 21, 2009 03:08 PM

    Yes, it can. The Get-Stat cmdlet that is present in PowerCLI v4 returns a value for all instances and one aggregate value.

    The instance field contains the CPU-id for the per core values and an empty string for the aggregate value.

    With a script like this you get all the CPU instances.

    $stats = Get-Stat -Entity (Get-VMHost <ESX-hostname>) -Stat cpu.usage.average -Realtime -MaxSamples 1
    $stats | where {$_.Instance -ne ""} | %{
    	Write-Host "Core" $_.Instance "avg" $_.Value
    }
    

    Note that when you have multiple CPU blocks (for example 2 quad-core processors) the instance numbering will be sequential (0,1,2,3,4,5,6,7...)

    The consecutive numbers belong to one processor block.

    You can use the HW info to find out what type of processor is in the ESX host.



  • 3.  RE: How to get each CPU core utilization using powershell cmdlets?

    Posted Jul 23, 2009 11:18 AM

    Many thanks Lucd.. I'm able to retrive only realtime stats, however, not able to retrive the archived CPU cores stats seprately. Can we get the archived stats for all CPU cores using get-stat directly?



  • 4.  RE: How to get each CPU core utilization using powershell cmdlets?
    Best Answer

    Posted Jul 23, 2009 11:35 AM

    Of course, the Get-Stat cmdlet can, besides the realtime statistics, also retrieve the metrics for each of the 4 historical intervals.

    You control that with the -IntervalMins (or -IntervalSecs), the -Start and the -Finish parameters.

    Some examples

    # Historical interval 1
    $stats = Get-Stat -Entity (Get-VMHost <ESX-hostname>) -Stat cpu.usage.average -IntervalMins 5 -Start (Get-Date).addhours(-6) -Finish (Get-Date).addhours(-5) -MaxSamples 12
    $stats | where {$_.Instance -ne ""} | %{
    	Write-Host "Core" $_.Instance "avg" $_.Value
    }
    

    # Historical interval 4
    $stats = Get-Stat -Entity (Get-VMHost <ESX-hostname>) -Stat cpu.usage.average -IntervalMins 1440 -Start (Get-Date).adddays(-2) -Finish (Get-Date).adddays(-1) -MaxSamples 24
    $stats | where {$_.Instance -ne ""} | %{
    	Write-Host "Core" $_.Instance "avg" $_.Value
    }
    



  • 5.  RE: How to get each CPU core utilization using powershell cmdlets?

    Posted Aug 20, 2009 02:01 PM

    hi guys, very helpful thread, and really useful cmdlet get-stat....

    One question if you can provide some help, im trying to write a script that will save the stats for cpu0 as an average for every host in my vc, put them into a table and mail it off to the support team... so far my efforts are pulling back results quite slowly, am i doing something horribly slow here?

    $hosts = Get-VMHost

    foreach ($esx in $hosts)

    {

    $stats = Get-Stat -Entity (Get-VMHost $esx) -Stat cpu.usage.average -Realtime -MaxSamples 1

    $stats | where {$_.Instance -eq "0"} | %{

    Write-Host $_.Entity "Core" $_.Instance "avg" $_.Value

    }}



  • 6.  RE: How to get each CPU core utilization using powershell cmdlets?

    Posted Aug 20, 2009 04:42 PM

    You're doing the Get-VMHost cmdlet twice for the each host.

    The value in $esx is already a HostSystemImpl object that you can pass to the -Entity parameter.

    This should be a bit faster

    $hosts = Get-VMHost
    foreach ($esx in $hosts)
    {
    	$stats = Get-Stat -Entity $esx -Stat cpu.usage.average -Realtime -MaxSamples 1
    	$stats | where {$_.Instance -eq "0"} | %{
    		Write-Host $_.Entity "Core" $_.Instance "avg" $_.Value
    	}
    }
    



  • 7.  RE: How to get each CPU core utilization using powershell cmdlets?

    Posted Aug 21, 2009 08:30 AM

    you're absolutely right. my bad, should have spotted that. Much quicker now, Thanks a lot LucD