PowerCLI: Stats One-liner
Ever wondered how your VM’s are doing ? What resources are they using, do they have too many CPU’s or too much memory ?
This simple one-liner will give you a list of each of your vms, it will tell you how many cpu’s, the amount of memory, average cpu usage for x amount of days and the average memory usage for x amount of days, how cool is that ?!
Get-VM |Where {$_.PowerState -eq "PoweredOn"} |
Select Name,Host,NumCpu,MemoryMB, @{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}}, @{N="Mem.Usage.Average";E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}} |Export-Csv c:\Temp\stats.csv
Update 03 July 2009: This has been updated as it was not collecting the average over the given timescales, the original code was selecting 1 sample from the timeframe.
Sample output:
| Name | Host | NumCpu | MemoryMB | Mem.Usage.Average | Cpu.UsageMhz.Average |
| tstVIC01 | tstesx02 | 1 | 512 | 25.32 | 121 |
| tstVCT01 | tstesx04 | 1 | 1536 | 14.32 | 144 |
| tstFOG01 | tstesx03 | 1 | 2048 | 12.79 | 72 |
| ELGMSF01 | tstesx01 | 1 | 512 | 20.19 | 748 |
| tstADC02 | tstesx02 | 1 | 384 | 10.32 | 17 |
| ELGFIL01 | tstesx04 | 1 | 384 | 7.19 | 20 |
| tstADC01 | tstesx01 | 1 | 384 | 10.25 | 16 |
| VISTA | tstesx04 | 1 | 768 | 10.99 | 5 |
| tstWEB01 | tstesx01 | 1 | 512 | 49.79 | 124 |
| ELGXCH01 | tstesx02 | 1 | 512 | 15.59 | 46 |
| ELGADC01 | tstesx01 | 1 | 512 | 11.52 | 19 |
| SmallVM | tstesx04 | 1 | 512 | 0 | 5 |
| tstSQL01 | tstesx04 | 1 | 512 | 11.99 | 58 |







about 8 months ago
Love it. I definitely have a use for this, thanks.
about 8 months ago
This is great. But I’m not sure if I understand the Mem.Usage.Average, is the Average in percentage format? On your screenshot for an example VM tstFOG01 has 2048 of RAM and on an average is only utilizing 12.79. So if that’s a percentage it’s using under 300MB to round it up. So if that’s the case, the memory is over allocated. So would it be safe to say that 512mb should be enough? I have over 600 VM’s and couple admins that logon and make changes. I’m sure there are a lot of VM’s that are way overallocated when it comes to RAM and I been trying to find an easy way to figure this out. We don’t really have a good tool to easily get the memory utilization. I been using VMware Consolidation by adding VM’s, it’s been working OK but it only gives me a 24hr view.
about 7 months ago
Is there a way to calculate average usage during working hours? Say 9am-5pm? This is based on a 24 hour period and could potentially skew the numbers. I would love to see this if it is possible.
Thanks,
Marc
about 7 months ago
@Mike
Yes this is a percentage, you would obviously need constant monitoring to make that sort of judgement though to ensure you catch trend data.
about 7 months ago
@Marc
I’m afraid that is not possible with the Get-Stat cmdlet itself, nor with the underlying QueryPerf (http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.PerformanceManager.html#queryStats) method from the SDK.
You could get all your statistical data and then use a Select-Object or an If-Then-Else construct on the Timestamp property to filter out the data you want.
Thanks @LucD – PowerCLI Community answer to a separate post: http://webmail.virtu-al.net/webmail-n/parse.php?redirect=http://communities.vmware.com/message/1325196%231325196
about 5 months ago
Does this script calculate over 24hours? If I wanted to calculate over 1 month would I have to use the following?
Get-VM |Where {$_.PowerState -eq “PoweredOn”} |
Select Name,Host,NumCpu,MemoryMB, @{N=”Cpu.UsageMhz.Average”;E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-720)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}}, @{N=”Mem.Usage.Average”;E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-720)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}} |Export-Csv c:\Temp\stats.csv
about 5 months ago
well technically it is possible to calculate 9am to 5pm, run the script at 5pm with addhours(-9) should give you what you are looking for!
And run it again at 9am with addhours(-16) and you get the offhours load, might be interesting too !
Well this should work, unless ofcause I am wrong about the addhours variable
about 5 months ago
@Marc
That would extend the timeframe but wouldn’t you want more samples processed?
I may be entirely wrong but -MaxSamples (12) over -720 hours wouldnt be very granular to average over.