VMware Cloud Community
Rajhyd
Contributor
Contributor
Jump to solution

How to get each CPU core utilization using powershell cmdlets?

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

Thank you,

Raj

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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
}


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

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.


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

Rajhyd
Contributor
Contributor
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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
}


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

Reply
0 Kudos
coco26
Contributor
Contributor
Jump to solution

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

}}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
coco26
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos