VMware Cloud Community
vmhyperv
Contributor
Contributor
Jump to solution

powercli script to capture cpu & mem usage stats of Individual VM

Hi ,

Is it possible to find out or   to capture cpu & mem usage stats of individual VMs  for last 5 Days thru PowerCli and Export to CSV report

Thanks

vmguy

0 Kudos
37 Replies
JayLB
Enthusiast
Enthusiast
Jump to solution

Perfect!  Thanks LucD!

0 Kudos
JayLB
Enthusiast
Enthusiast
Jump to solution

After 12 hours I had to kill the process.  The Powershell process was running at 50% with 250mb consumed the whole time.  Modified the script with a foreach statement to break it up into VM size bites.  Takes about a minute per VM to run on this customer's vCenter.

Takes much longer to run but this script also reduced my csv file by 5 times of the size rather than collecting each metric by MetricID into a csv file.

170 VMs * 1 mins per = 2.8 hours to run

About 2.xMB of Mem per VM in Powershell.exe = 360-400MB Memory required for Powershell

# VM Performance metrics in a single line per interval

$report = @()
$metrics = "cpu.usage.average","mem.usage.average","disk.maxTotalLatency.latest","net.usage.average"
$vm = Get-Vm
$start = (get-date).AddDays(-31)
Foreach($vms in $vm) {
  Get-Stat -Entity ($vms) -start $start -stat $metrics -IntervalMins 120 |`
   Group-Object -Property {$_.Entity.Name, $_.Timestamp}| %{
    $row = ""| Select "DateTime", "Virtual Machine", "vCPU Count", "Avg Cpu Usage (%)", "vRAM Provisioned (GB)", "Avg Memory Usage (%)", "Avg Disk Latency (ms)", "Avg Net Usage (kb)"  
    $row."Virtual Machine" = $_.Group[0].Entity.Name
    $row."DateTime" = $_.Group[0].Timestamp
    $row."vCPU Count" = $_.Group[0].Entity.NumCpu
    $cpuStat = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value
    $row."Avg Cpu Usage (%)" = $cpuStat
    $row."vRAM Provisioned (GB)" = $_.Group[0].Entity.MemoryGB
    $memStat = $_.Group | where {$_.MetricId -eq "mem.usage.average"} | Select -ExpandProperty Value
    $row."Avg Memory Usage (%)" = $memStat
    $diskStat = $_.Group | where {$_.MetricId -eq "disk.maxTotalLatency.latest"} | Select -ExpandProperty Value
    $row."Avg Disk Latency (ms)" = $diskStat
    $netStat = $_.Group | where {$_.MetricId -eq "net.usage.average"} | Select -ExpandProperty Value
    $row."Avg Net Usage (kb)" = $netStat
    $report += $row
}
}
$report | Export-Csv "C:\ReportData\VMPerfData.csv" -NoTypeInformation -UseCulture
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I had similar experiences; when running Get-Stat in "bigger" environments it tends to consume a lot of memory and potentialy hang at some point.

That's most probably caused by the way the Get-Stat cmdlet is implemented, the vSphere PerformanceManager works  and/or the way PowerShell uses the stack and garbage cleaning.

There are a number of measures you can take:

  • use my Get-Stat2 function, it consumes less memory. But in the end it will also come to a halt
  • split the VMs in managable groups, instead of 1 Get-Stat call use several. The number to split at will depend on the environment and the memory size of the client where you run the script

The solution you picked takes of course the longest time to run, but probably uses the least amount of memory on the client.


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

0 Kudos
AviShimon
Contributor
Contributor
Jump to solution

Hi LucD,

i'm having a similar problem which following this discussion, i still do not get it to work.

when i run get-stattype for VM's ,I get the list of available metrics, but without "mem.active.average".

I have raised statistics level to 2 and still No 'mem.active..' metric.

i can get this for a specific VM using VC performance charts but it is not very usefull.

please advise.

Thanks, Avi

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which intervals did you specify ?

Try like this and see if it returns the metric(s) for any of the intervals

Get-StatType -Entity $vm -Interval "Past Day" | where {$_ -match "mem.active"}

Get-StatType -Entity $vm -Interval "Past Week" | where {$_ -match "mem.active"}

Get-StatType -Entity $vm -Interval "Past Month" | where {$_ -match "mem.active"}

Get-StatType -Entity $vm -Interval "Past Year" | where {$_ -match "mem.active"}


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

0 Kudos
AviShimon
Contributor
Contributor
Jump to solution

Hi,

thanks for your prompt reply.

I now get the metric for "Past Day"  interval .  i guess it need time to build for "Past Week" ( also in level 2).

Now this command Works :

PS C:\Windows\system32> Get-Stat -Entity (Get-VM my-vm) -Stat mem.active.average -IntervalMins 5 -MaxSamples 3 | where{$_.instance -eq ""}

MetricId                Timestamp                          Value Unit     Instance
--------                ---------                          ----- ----     --------
mem.active.average      24/01/2013 13:45:00                    0 KB              
mem.active.average      24/01/2013 13:40:00               748226 KB              
mem.active.average      24/01/2013 13:35:00               351286 KB    

But, when i"m trying to use the start/finish , i get the mistake,

$today = (Get-Date -Hour 10 -Minute 30 -Second 0)
$now = (Get-Date -Hour 13 -Minute 30 -Second 0)
Get-Stat -Entity (Get-VM my-vm) -Stat mem.active.average -Start $today -Finish $now | where{$_.Instance -eq ""}   

Get-Stat : 24/01/2013 13:53:14    Get-Stat        Object reference not set to an instance of an object.  
At line:3 char:9
+ Get-Stat <<<<  -Entity (Get-VM my-vm) -Stat mem.active.average -Start $today -Finish $now | where{$_.Instance -eq ""}
    + CategoryInfo          : NotSpecified: (:) [Get-Stat], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetViStats    

please advise.

thanks, Avi 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, it will take time to see the metric appear in all intervals.

Your lines of code seem to work for me. It looks as if the problem is that 'Get-VM my-vm' doesn't return anything.


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

0 Kudos
AviShimon
Contributor
Contributor
Jump to solution

Get-vm is the same on both lines , the only thing changed in the Time Limit/Frame.

if i want to get the data using max samples and interval  for a whole day , how would you suggest to do it , (or any other way )

Thanks,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that the VM was not running at the time specified in $start ?

Do you see statistics for that time in the vSphere client ?


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

0 Kudos
AviShimon
Contributor
Contributor
Jump to solution

VM is running, there are 2 adjacent powercli lines, one is working the other is Not, as I written , the only different is the Interval/Start/Finish.

Yes, i am monitoring VC performance couters of that VM at the same time.

Thanks, Avi

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The only times I have seen that error with the Get-Stat cmdlet , is when there was no statistical data available at the time specified by the $Start variable.

Can you try changing the value in the $Start variable I see if that makes a difference ?


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

0 Kudos
AviShimon
Contributor
Contributor
Jump to solution

Hi LucD,

Problem Solved.

I guess it's kind of a bug in the powercli version that I used.

I have upgraded to verison 5.1 (latest) and things started to role.

Thanks for all your support.

i must say you're doing a Holy job in hte community (without offending anyone ...) Smiley Happy

Avi

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks, glad your problem got solved


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

0 Kudos
jlcjr2k8
Contributor
Contributor
Jump to solution

Capture.JPG

Hi Luc,

Is there a way to get this report in a graph like the pic above? I would like to present the report in graph format instead of numbers.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can let Excel produce the graph, have a look at Export-Xlsx, the sequel, and ordered data.

And there are other ways, have a look at my Dutch VMUG: The Statistics Reporting Session


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

0 Kudos
jlcjr2k8
Contributor
Contributor
Jump to solution

linkerror.JPG

Luc,

Both links are not working.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There was an error in the links. Still getting used to this new Jive version :smileyshocked:

Should be working now, can you try again ?


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

0 Kudos
jlcjr2k8
Contributor
Contributor
Jump to solution

Links working now.

0 Kudos