VMware Cloud Community
bottomofbarrel
Contributor
Contributor

powercli script to capture cpu & mem usage stats

i am new to powercli and am not a good scriptor yet however, i need a script that will capture the max, min, avg cpu & mem usage stats per esxhost & vm for the past month and import into excel so i can create a pivot chart. key is i need to see the cpu/mem usage data per vm per host. can anyone help me with this. i have spent the last two weeks trying to work with powercli to do this and i am unable produce the results i want.

171 Replies
EChokshi
Contributor
Contributor

Thanks LucD for prompt response.

Yes, sorry $vms = Get-VM was part of the query and i checked its output. It was showing all VMs part of the selected vCenter. But not sure why it would only iterate for 2 days only?

Reply
0 Kudos
LucD
Leadership
Leadership

Do you see all the days under the Performance tab in the Web Client?

It could be that there is an issue with the aggregation jobs.


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

Reply
0 Kudos
EChokshi
Contributor
Contributor

Hi LucD,

Yes, Performance charts are all there hence i was confused why the script wouldnt iterate for the chosen range?

If aggregation jobs may be the culprit what can be done in terms of remediation? any idea?

Reply
0 Kudos
LucD
Leadership
Leadership

If the Performance  charts are there, the aggregation jobs should be ok.

Another option you might try, on the Get-Stat line add the following '-ErrorAction SilentlyContinue'.


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

Reply
0 Kudos
EChokshi
Contributor
Contributor

tried that... what i noticed was that when I also added -IntervalSecs 1800 it iterated for intended period otherwise only for 2 days (i think it does -IntervalSecs 300)???

I really need 5 min or 30 min or 1 hoursly stats and find an avg based on that.

Reply
0 Kudos
LucD
Leadership
Leadership

If you go back further in time, the intervals become longer (that is what the aggregation jobs are doing).

See also my PowerCLI & VSphere Statistics – Part 1 – The Basics post.


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

Reply
0 Kudos
EChokshi
Contributor
Contributor

thnx for your help, I will go through it and get back to you if I have any further queries.

Reply
0 Kudos
NeenaJim
Enthusiast
Enthusiast

Hello @LucD 

I would like to get the total CPU and Memory configured to the VMs in the output. Can you please tell me what needs to be added in your script:

Connect-VIServer <server> -User <user> -Password <password>$allvms = @() $vms = Get-Vm

$stats
= Get-Stat -Entity $vms -start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat "cpu.usage.average","mem.usage.average"  $stats | Group-Object -Property Entity | %{   $vmstat = "" | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
  $vmstat.VmName = $_.name   $cpu = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property value -Average -Maximum -Minimum
  $mem = $_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property value -Average -Maximum -Minimum
 
$vmstat.CPUMax = [int]$cpu.Maximum   $vmstat.CPUAvg = [int]$cpu.Average   $vmstat.CPUMin = [int]$cpu.Minimum   $vmstat.MemMax = [int]$mem.Maximum   $vmstat.MemAvg = [int]$mem.Average   $vmstat.MemMin = [int]$mem.Minimum    $allvms += $vmstat
}
$allvms | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\VMs.csv" -noTypeInformation
Reply
0 Kudos
LucD
Leadership
Leadership

The Entity property in each object returned by Get-Stat contains the VM's properties.
See for example Solved: Re: Average Memory Utilization - VMware Technology Network VMTN


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

Reply
0 Kudos
NeenaJim
Enthusiast
Enthusiast

Hello @LucD 

Thank you. I got it.

Reply
0 Kudos
NeenaJim
Enthusiast
Enthusiast

Hello @LucD 

I have generated the report by updating the value (60)   -   '(get-date).AddDays(-60)'.

And then I  have received the result like this. The CPUMax, CPUAvg, CPUMin is the actual number or is that the percentage of total cpu?

The reason for my question is eg: the total CPU is 2, but its CPUMax is 34 and average is 17. Trying to understand the logic

CpuTotalCPUMaxCPUAvgCPUMin
8753
82964
2341711

 

 

Reply
0 Kudos
LucD
Leadership
Leadership

Those are percentages


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

Reply
0 Kudos