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
2012vm
Contributor
Contributor

Hi LucD,

Thanks for your quick response!!!!

I ran this script for capturing monthly Mem/Cpu Usage for maximum,Minimun,Avg which is working fine.

But i need this information daily so that i can generate a weekly report for my client using this details.

Can you provide me with the modifications in the script so that i can make that changes.

Actually i dont know anything about scripting.

Thnaks for helping me!!

Reply
0 Kudos
LucD
Leadership
Leadership

Try this.

The report looks at the last 7 days and will produce a line per VM per day.

Connect-VIServer <server> -User <user> -Password <password> 
$allvms = @() $vms = Get-Vm $start = (Get-Date).AddDays(-7) $metrics = "cpu.usage.average","mem.usage.average" $stats = Get-Stat -Entity $vms -Start $start -Stat $metrics   $stats | Group-Object -Property {$_.Timestamp.Day},{$_.Entity.Name} | %{   $vmstat = "" | Select VmName, Day, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
  $vmstat.VmName = $_.Values[1]   $vmstat.Day = $_.Group[0].Timestamp.Date   $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 | Export-Csv "c:\VMs.csv" -noTypeInformation


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

2012vm
Contributor
Contributor

Hi LucD,

Sorry for late reply.

I tried using this script which you have posted but i got some errors which i have attached,

I think those are very small errors but i not able to figure them out.

I am getting the CSV file with no values on it

can you please go through it and modify the script.

Thnaks for responding & helping me

Reply
0 Kudos
LucD
Leadership
Leadership

Looks like the $vmstat Select didn't work.

Are you sure the copy/paste was ok ?

I attached the script to make sure you have a good copy.


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

Reply
0 Kudos
2012vm
Contributor
Contributor

Yes Sir,

It worked now... Thanks

I will assign you points.

Thanks for helping!!!!

Reply
0 Kudos
Andavan
Contributor
Contributor

HI LuCD ,

I am getting error message when i run the below script. Find attached err.txt.

Please provide same script for ESX mem and CPU usage

Reply
0 Kudos
LucD
Leadership
Leadership

The attached script does CPU and Memory statistics.

Or do you mean something else ?

Concerning the error, that could be caused by a VM that has no statistical data present for the selected time range.


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

Reply
0 Kudos
Andavan
Contributor
Contributor

Hi LucD ,

Thanks for reply. Even thoguh the specific VM did not have any data then It should give output of other VM's but here I did not get any data and the file is empty.

Reply
0 Kudos
LucD
Leadership
Leadership

I'm afraid that's a flaw in the current version.

You could check if the VM is powered on with a Where-clause.


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

Reply
0 Kudos
Andavan
Contributor
Contributor

HI LucD,

Could you provide me the correct version of script for ESX perf report 

Thanks

Reply
0 Kudos
lsvare
Contributor
Contributor

I am looking a script like what been listed in here for cpu/mem useage per host per vm and average over last month in a single line report that can have emailed to my email every night.  I am a newbie to powercli so please give me step by step what I need to do on any script posted please...  I know this is a piece of cake to many of the people on here.  But gotta start somewhere.

Reply
0 Kudos
LucD
Leadership
Leadership

Per host, per VM on a single line ?

How do you see that output ? Perhaps show a dummy sample line of what you are looking for ?


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

Reply
0 Kudos
lsvare
Contributor
Contributor

I do not have a sample to send but have seen some that on a single line show each vm cpu.mem stat current and average and then also list each host cpu/mem stat current and average on a single line. That’s what I am looking for. I have tried to copy paste the code have found but am unable to make run. I do have the vcheck 5 eport working and that’s a great script but doesn’t give me the vm and hosts cpu/mem stats needed. Appreciate any and all help anyone can provide.

Thank You, and Have a Great Day!

Larry Svare

Infrastructure Management Analyst

ACS: Application Management Services

A Xerox Company

Office Phone: 1-218-998-1622

Cell Phone: 1-320-761-7782

larry.svare@xerox.com<mailto:larry.svare@xerox.com

Reply
0 Kudos
lsvare
Contributor
Contributor

I am playing around with some scripts trying to find one that will give me a single line status of cpu/memory min, max average per vm but by  showing the amount not percentage which I assume the script above must be doing.  Reason I say that am getting some numbers that exceed the amount, so am assuming is percentages.  But then is that the precentage of the assigned cpu/memory or of the hosts resources?

Reply
0 Kudos
LucD
Leadership
Leadership

Attach your script (or send it to me) and I can have a look.


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

Reply
0 Kudos
lsvare
Contributor
Contributor

I have attached 2, one that gives a report on the vm’s only the other on hosts and vm’s. I am a newb to the scripting but am finding it so helpful. I am also looking for one that will give me a report on what each host has for cpu/mem and what is assigned to each vm for cpu/memory. I am an admin on about 8 different VMware farms and manually getting this information is no longer an option with my other duties.

Thank You, and Have a Great Day!

Larry Svare

Infrastructure Management Analyst

ACS: Application Management Services

A Xerox Company

Office Phone: 1-218-998-1622

Cell Phone: 1-320-761-7782

larry.svare@xerox.com<mailto:larry.svare@xerox.com

Reply
0 Kudos
MikeTrow
Contributor
Contributor

Great script that works fine in my small Vcenter however when I run it in a larger environment it stops part way through with the following error.

Get-Stat : 26/07/2012 11:00:21    Get-Stat        Object reference not set to a
n instance of an object.
At \\xxxxxxxxxxxxxxxxxxxxxxxx\Install\VMware\Migration_Tools\WIP\cpu-mem-stats4.ps
1:6 char:18
+ $stats = Get-Stat <<<<  -Entity $vms -Start $start -Stat $metrics
    + CategoryInfo          : NotSpecified: (:) [Get-Stat], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio
   n.ViCore.Cmdlets.Commands.GetViStats

Script adopted slightly to show vCPU and Memoray allocations attached.

The CSV is being created and populated but then stops.

Any thoughts ????

Connect-VIServer xxxxxxxxx

$allvms = @()

$vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"}

$start = (Get-Date).AddDays(-30)

$metrics = "cpu.usage.average","mem.usage.average"

$stats = Get-Stat -Entity $vms -Start $start -Stat $metrics

$stats | Group-Object -Property {$_.Timestamp.Day},{$_.Entity.Name} | %{

$vmstat = "" | Select VmName, Day, MemAlloc, MemMax, MemAvg, MemMin, VCPU, CPUMax, CPUAvg, CPUMin

$vmstat.VmName = $_.Values[1]

$vmstat.Day = $_.Group[0].Timestamp.Date

$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.vCPU = $_.Group[0].Entity.NumCpu

$vmstat.MemMax = [int]$mem.Maximum

$vmstat.MemAvg = [int]$mem.Average

$vmstat.MemMin = [int]$mem.Minimum

$vmstat.MemAlloc = $_.Group[0].Entity.MemoryMB

$allvms += $vmstat

}

$allvms | Export-Csv "c:\VMs.csv" -noTypeInformation

Reply
0 Kudos
LucD
Leadership
Leadership

Your PowerShell session probably runs out of mememory while the Get-Stat data comes in or is being handled.

You should be able to see this in Task Explorer.

When I encountered that problem before, a solution was to do the report in 2 stages. First 1 half of the VMs, then the other half of the VMs.

But it all depends on the size of the environment and the amount of available memory on the machine where you run the script


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

Reply
0 Kudos
MikeTrow
Contributor
Contributor

Thanks

Managed to get this running now

Reply
0 Kudos
Gautam201110141
Contributor
Contributor

HI,

Am new to power cli scripts. I have used the below scripts and working fine for me. Need to capture cpu & mem usage stats for particular given date. Is there is any scripts to achieve this. Because we need to provide the past months cpu & mem usage. so anybody help on this. It will be very useful. Just want to change getting data for -30 days instead  it should get the From date and End date.

Reply
0 Kudos