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:

NameHostNumCpuMemoryMBMem.Usage.AverageCpu.UsageMhz.Average
tstVIC01tstesx02151225.32121
tstVCT01tstesx041153614.32144
tstFOG01tstesx031204812.7972
ELGMSF01tstesx01151220.19748
tstADC02tstesx02138410.3217
ELGFIL01tstesx0413847.1920
tstADC01tstesx01138410.2516
VISTAtstesx04176810.995
tstWEB01tstesx01151249.79124
ELGXCH01tstesx02151215.5946
ELGADC01tstesx01151211.5219
SmallVMtstesx04151205
tstSQL01tstesx04151211.9958

22 thoughts on “PowerCLI: Stats One-liner

  1. ken

    follow up to my last comment – figured this out quickly, you need real quotes and not this &quot: text formatting error… and remove as well.

  2. ken

    ok, figured this out quickly, you need real quotes and not this &quot: text formatting error… and remove as well.

  3. Ken

    I just copied and pasted this script and ran it again my 5.5 test environment and got nothing but errors… I take it this was used for 4.x?? I’m talking NOTHING but errors and I run scripts against the environment frequently so I know this isn’t a problem within my environment. is there an updated version that will actually run in vSphere 5.5?

  4. Bhavesh

    Alan , Great script. One quick question – the Mem.Usage.Average is in MB or GB ? Nothing showing there. The MemoryMB is in MB but Mem.Usage.Average looks to be GB.

  5. Pingback: The co-stop problem – performance troubleshooting with PowerCLI | virtualscroll

  6. Pingback: The co-stop problem – step 2 | virtualscroll

  7. Jim

    Hi Alan,

    i would like to run this scripts to particular one cluster and gather all information for those VMs which are belongs to this cluster.

    Get-Cluster -Name ** | Get-VM | Where {$_.PowerState -eq “PoweredOn”}

    i’m trying add one more code in the beginning with cluster name but its just pull out cluster vm info not a real vm info which i’m looking forward to have using this script

    greatly appropriate if you can provide me a solution.

    Thank you,
    Jim

  8. Rohit Kakkar

    This Script is very good, i am able to get this report.

    but is it possible to get Max. CPU and Max. RAM utilization of VMs since last 12hr or 1 month

  9. Alberto

    Hey Alan – great output.
    Is it possible to add memory active, ballooned and cpu ready time to the report??

    thanks,
    Alberto

  10. Larry

    ok so this script is the memory and cpu is that what is currently using or assigned, and the average is that percentage of assigned per vm or percentage of host resources? Then is it possible to make it so it shows the average amount rather than percentage and get so emails report?

  11. Larry

    How do I get this script to run, all I get is a quick display of PS all in red from what I can see.

  12. ap

    Can i get the same code for java or code for listing all the VM’s on a ESX host using Java and VI-sdk

  13. Aureus

    I know this is an old script, but I have a question.

    Why do you have “-MaxSamples (12) ” in the script. Wouldn’t that limit the script to only 1 hour of data, rather then the entire day?

  14. James

    @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.

  15. Kenneth

    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 🙂

  16. Marc

    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

  17. Virtu-Al

    @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

  18. Virtu-Al

    @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.

  19. Marc

    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

  20. Mike

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.