VMware Cloud Community
alex0
Enthusiast
Enthusiast
Jump to solution

Generating CSV file of VM statistics using get-stat

Hi All,

I have set my VC to collect 5 minute interval samples for 3 days.

I would like to create a PowerCLI script which I can then run on an ad-hoc basis to connect to my VC and get following details for ALL VMs managed by that VC and populate into a CSV file (c:\foo.csv):

  • VM name

  • ESX host VM is currently sitting on

  • total number of virtual CPUs allocated to VM

  • total memory allocated to VM

  • average active memory used by VM over the past 3 days using 5 minute samples

  • average CPU mhz used by VM over past 3 days using 5 minute samples

I have attached a sample XLS file of what the CSV output should look like.

If anyone can give me some code to achieve this or get me on well on my way I'd much appreciate it.

Regards,

Alex

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Ok so what you want is something like this then ?

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(-72) -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(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}}

I believe the issue you may be having with the missing stat might be to do with your logging levels in VC, if these are not set correctly it will not log these stats for that amount of time.

What do you get if you do a simple:

get-vm "SingleVM" | Get-Stat -Stat cpu.usagemhz.average

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com

View solution in original post

Reply
0 Kudos
5 Replies
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Sounds like a great one-liner to me, I will take up that challenge !

Back with ya soon !

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Reply
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Ok try this:

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

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Reply
0 Kudos
alex0
Enthusiast
Enthusiast
Jump to solution

Hey alan,

Thanks a lot for your script, couple of issues.

this checks memory usage, i needed memory active... i fixed this one up myself easily enough

when i run your script as supplied, cpu.usagemhz.average never gets populated...

#TYPE System.Management.Automation.PSCustomObject

Name,Host,NumCpu,MemoryMB,Mem.Usage.Average,Cpu.UsageMhz.Average

vm1,exhost1,1,3600,3.4,

vm2,esxhost2,4,16384,2.05,

vm3,esxhost1,1,1024,2.19,

vm4,esxhost1,1,3600,4.06,

Can you please advise on the cpu.usagemhz.average issue... have tried resolving but can't figure it out.

Regards

Alex

Reply
0 Kudos
alex0
Enthusiast
Enthusiast
Jump to solution

Also just found this script:

the maxsamples needs to be set to 72*12 (72 hours in 3days, 12 5min sampling periods in an hour) (not 1) for the 3 day sampling period I want to average over... otherwise it will just use 1 sampling value instead of averaging the 72.

Regards

Reply
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Ok so what you want is something like this then ?

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(-72) -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(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}}

I believe the issue you may be having with the missing stat might be to do with your logging levels in VC, if these are not set correctly it will not log these stats for that amount of time.

What do you get if you do a simple:

get-vm "SingleVM" | Get-Stat -Stat cpu.usagemhz.average

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Reply
0 Kudos