VMware Cloud Community
Guv
Enthusiast
Enthusiast

Get-stat CPU and Memory question.

Below is a script I got from alanrenouf regarding listing of virtual machines and their performance in a previous post :

Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select name, Host, NumCpu, MemoryMB, @{N="Mem.Usage.Average";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-3)) -Finish (Get-Date) -stat mem.usage.average -intervalmins 5 -MaxSamples 1).Value}}, @{N="Cpu.UsageMhz.Average";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-3)) -Finish (Get-Date) -stat cpu.usagemhz.average -intervalmins 5 -MaxSamples 1).Value}} | Export-Csv c:\temp\stats.csv

I just want to confirm few things regarding the script in particular the get-stat command and the mem usage and cpu usagemhz settings for the VM's. The values are based on the last 3 days and sampling every 5 minutes. So does this give the actual average memory % usage and cpu mhz usage of the virtual machines in the last 3 days. So I can use these figures to present to managemnt and they can see the actual usage of the virtual machines. I just want to confirm.

Also if I change the cpu.usagemhz.average to cpu.usage.average, would this give the % cpu used instead of the of the average mhz used. Is this correct.

Just want to confirm if my thinking is correct

0 Kudos
5 Replies
LucD
Leadership
Leadership

1) No, you ask the for the averages but you only ask 1 sample. You will only get that sample.

You can drop the -Maxsamples parameter and get all the samples and then use something like Measure-Object to make the average of the samples.

2) Yes, cpu.usage.average will give the percentage


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

0 Kudos
Guv
Enthusiast
Enthusiast

so to understand this more clearly, from the 5 minutes samples readings it takes for 3 days, it just gives you one sample reading, how does it choose which sample to give, or is this a silly question, apologies if it is.

Thats not what I need, i would need an average value of the 5 minute samples taken in the last 3 day. Where could I add the measure-object cmdlet to get this value.

Thanks again

0 Kudos
LucD
Leadership
Leadership

The reason that you will only get 1 sample is due to the -Maxsamples 1 parameter.

The -Intervalmins 5 parameter is a bit useless if you go back 3 days.

vCenter consolidates the statistics on a daily basis.

In this case, it's best to let Get-Stat decide on the interval to pick.

Something like this should do what I think you're trying to achieve.

Get-VM | where {$_.PowerState -eq "PoweredOn"} | `
	Select name, Host, NumCpu, MemoryMB, 
		@{N="Mem.Usage.Average";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-3)) -Finish (Get-Date) -stat mem.usage.average | Measure-Object -Property Value -Average).Average}},
		@{N="Cpu.Usage.Average";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-3)) -Finish (Get-Date) -stat cpu.usage.average | Measure-Object -Property Value -Average).Average}} | `
	Export-Csv c:\stats.csv


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

0 Kudos
Guv
Enthusiast
Enthusiast

Thanks for the script.

I ran the script, and I know get values for the mem.usage.average and cpu.usage.average. Can I know interpret these figures as average percentages usage of memory and cpu by the virtual machine for the last 3 days. Also does the get-stat do its own sampling for the last 3 days, is this something we can find out, or do we just leave it for get-stats.

Thanks

0 Kudos
LucD
Leadership
Leadership

Yes, these are the averages over the last 3 days.

The Get-Stat cmdlet doesn't do sampling

The initial sampling is done on the ESX server and then collected by the vCenter.

It's on the vCenter that the aggregation of the sampling intervals occurs.


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

0 Kudos