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
BRomister
Contributor
Contributor

LucD wrote:

Are you running this against a vCenter or an ESXi server ?

To which did you the Connect-VIServer ?

That could also mean that your Statistic Level on the vCenter is not set up correctly.

Check what it says in <Administration><vCenter Server Settings><Statistics> in the vSphere client.

I am running vCenterAnd connecting to the same name like vsphere client, same address vCenter-eng.inc.com


Connect-VIServer vCenter-eng.inc.com -User <user> -Password <password>

That could also mean that your Statistic Level on the vCenter is not set up correctly.

Check what it says in <Administration><vCenter Server Settings><Statistics> in the vSphere client.

I do not have permittions to see it, but if need I'll ask to see someone, what shoud I/he see(s)?

And is it important for which Os to use, because I have trouble only if use Win2008r2, in win7 it works?

PS I think it is vCenter, how to check? I am begginer in this ))

Thanks again

Reply
0 Kudos
LucD
Leadership
Leadership

There are some issues with the 64-bit version on W2K8R2.


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

Reply
0 Kudos
BRomister
Contributor
Contributor

Maybe you can suggest something, I need it running with Jenkins which is on Windows2008r2

Some "campatibility mode" or other tool? I need this statistic to make "desicion" where to deploy VM(deploy to host wihe less load)

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

Sorry, but besides running the script in another, supported OS, I have no other suggestions.


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

Reply
0 Kudos
BRomister
Contributor
Contributor

maybe use perl API Smiley Happy?

Reply
0 Kudos
LucD
Leadership
Leadership

You could also try using my Get-Stat2 function


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

Reply
0 Kudos
BRomister
Contributor
Contributor

that look good, but I did not catch hoe to use this

I am trying

Get-Stat2 -Entity "test" -Stat "cpu.usage.average" -Interval "RT"

where test is the name of virt machine, it tells me

-Entity parameters should be of type HostSystem, VirtualMachine, ClusterCompute

Resource, Datastore or ResourcePool

And before I i"ve connected to vCenter vi Connect-VIServer

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

The function doesn't do OBN (yet), so you have to provide the actual vSphere object.

In your case that would be

Get-Stat2 -Entity (Get-VM -Name test).ExtensionData -Stat "cpu.usage.average" -Interval "RT"

In my Datastore usage statistics post you will find some more examples.


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

Reply
0 Kudos
BRomister
Contributor
Contributor

LucD wrote:

The function doesn't do OBN (yet), so you have to provide the actual vSphere object.

In your case that would be

Get-Stat2 -Entity (Get-VM -Name test).ExtensionData -Stat "cpu.usage.average" -Interval "RT"

In my Datastore usage statistics post you will find some more examples.

That is cool but too complicated, becouse I need output to CSV file like next

"HostName","MemMax","MemAvg","CPUMax","CPUAvg"

"autoesx02.esx","71","54","37","16"

"autoesx01.esx","56","48","44","24"

and I decided just to add one more VM with Win7 64bit and run my script there,

and what I see- the same picture! empty lines

"HostName","MemMax","MemAvg","CPUMax","CPUAvg"

"autoesx02.esx","","","",""

"autoesx01.esx","","","",""

It makes me crazy, no Idea what to do.

As I wroute that returns nothing when to call Get-Stat -Entity "engautoesx01.esx" -Start (Get-Date).AddDays(-1) -Finish Get-Date -MaxSamples 2 -Stat cpu.usage.average

if to call

Get-Stat -Entity "engautoesx01.esx" -Realtime -MaxSamples 2 -Stat cpu.usage.average

it works,

or maybe that issue becouse it runs on VM which is located on the same vSphere, but I dot think that is true

UPDATE

if run from my machine like this- it does not work too

Get-Stat -Entity  "engautoesx01.esx" -Start 11.05.2013 -Finish 11.06.2013 -MaxSamples 2 -Stat cpu.usage.average

Get-Stat -Entity  "engautoesx01.esx" -Start 11/05/2013 -Finish 11/06/2013 -MaxSamples 2 -Stat cpu.usage.average

Get-Stat -Entity  "engautoesx01.esx" -Start 05.11.2013 -Finish 06.11.2013 -MaxSamples 2 -Stat cpu.usage.average

Get-Stat -Entity  "engautoesx01.esx" -Start 05/11/2013 -Finish 06/11/2013 -MaxSamples 2 -Stat cpu.usage.average

returns nothing

Please, answer me if have any ideas

Thanks in advance

Reply
0 Kudos
RohitVCP7
Enthusiast
Enthusiast

Hi,

above script is very good and use full,

VmNameDayMemMaxMemAvgMemMinCPUMaxCPUAvgCPUMin
VM121/01/2014 00:00:001574811
VM120/01/2014 00:00:001365811
VM119/01/2014 00:00:001364811
VM118/01/2014 00:00:001264711
VM117/01/2014 00:00:001365711
VM116/01/2014 00:00:001365811
VM115/01/2014 00:00:0016644021
VM114/01/2014 00:00:0075434141

but i am getting above report when i am generating last 7 day reports, so it possible to get last 7 day Max and Min. CPU/Memory report in One Line like below;

VmNameDaysMemMaxMemAvgMemMinCPUMaxCPUAvgCPUMin
VM17167540141

Thanks in Advance

Reply
0 Kudos
Jason_McElvoy
Contributor
Contributor

This script is exactly what I'm looking for, but I'm not exactly sure what the output is.  For example, one of my vm's has a MemMax=77.46, MemAvg=29.47, MemMin=10.66.  Are these numbers percentages?  So MemMax is saying that the maximum memory this vm has used in the past 30 days is 77% of it's total available memory? 

Reply
0 Kudos
LucD
Leadership
Leadership

The available counters, and a short description of their meaning, can be found under the PerformanceManager entry in the API Reference


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

Reply
0 Kudos
Jason_McElvoy
Contributor
Contributor

Thanks for that link.  I did read through it and it answered some of my questions. What I can't seem to figure out is this report shows for example a vm that has only used a max of 26% of it's memory but when I look at the OS task manager it's constantly over 12GB of 16GB possible.  I would have expected this script to show the maxmemory for this server to be over 75% and not 26%.  There are several vm's in my environment that are experiencing similar results.  This report shows maxmemory much lower than is currently being used by the OS.

Reply
0 Kudos
LucD
Leadership
Leadership

Remember that this counter the active memory usage as seen by the hypervisor.

The task manager, and I guess you are running a Windows OS on that guest, states what is allocated.

But that doesn't mean that memory is actually used (or active).

As long as there is no stress on memory inside the guest OS, the guest OS will not change that allocation.


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

Reply
0 Kudos
Pinball
Enthusiast
Enthusiast

Good Day Luc

all the examples in this tread is based on a per vm stat or per datastore, but will it be possible to do it a one level higher ie.. per Cluster?

Thanks

Johan

Example of output i'm trying to get.

VCenter NameClusterTotal No VM'sNo VM powered Off non templateNo vm TemplatesCluster DRS CPU StatCluster DRS Mem Stat
Vcenter1Cluster 1463138%60%
Vcenter1Cluster 2385042%44%
Vcenter1 Cluster 3
Vcenter1 Cluster 4
Vcenter2Cluster 1103138%60%
Vcenter2Cluster 2605042%

88%

Reply
0 Kudos
LucD
Leadership
Leadership

That is possible, but you will have to calculate the cluster percentages in the script.

Get the metrics for all the VMs in the cluster in one Get-Stat call, then take the average of the returned metrics.


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

Reply
0 Kudos
YouyouL
Contributor
Contributor

Hi LucD,

I have a similar question than the principal one. I need to make a script for take metrics on all VMs on our ESXs, i use this one :

Connect-VIServer -Server XXXX

$allvms = @()

$vms = Get-Vm where {$_.OperatingSystem -eq "Windows"}

$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

The issue with this script is that i can't exclude our Linux VMs i don't know how to do for exclude a part of Operating system, any idea ?

Reply
0 Kudos
LucD
Leadership
Leadership

You can do a few things to improve the script.

1)Get an overview of the OS you have running in your VMs.

You can do something like this

Get-VM | Select Name,@{N="OS";E={$_.Guest.OSFullName}}

This requires that you have the VMware Tools installed in your VMs.

2) Use a -match filter in your Where-clause (with -eq you need to have a complete match).

For example something like this

Get-VM | Where {$_.Guest.OSFullName -match "Windows"}


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

Reply
0 Kudos
YouyouL
Contributor
Contributor

Thanks a lot for this quick answer i've just test it :

I still got an error for a Blackberry server, but it's ok. I still have an issue ... it doesn't create any extract file and i don't understand why.

ps.JPG

Reply
0 Kudos
YouyouL
Contributor
Contributor

It's ok now i've just modify a little the script and it's ok :

Connect-VIServer -Server srvpar5vcent01

$allvms = @()

$vms = Get-Vm | Where {$_.Guest.OSFullName -match "Windows"}

foreach($vm in $vms){

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

  $vmstat.VmName = $vm.name

 

  $statcpu = Get-Stat -Entity ($vm)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat cpu.usage.average

  $statmem = Get-Stat -Entity ($vm)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat mem.usage.average

  $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum

  $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum

 

  $vmstat.CPUMax = $cpu.Maximum

  $vmstat.CPUAvg = $cpu.Average

  $vmstat.CPUMin = $cpu.Minimum

  $vmstat.MemMax = $mem.Maximum

  $vmstat.MemAvg = $mem.Average

  $vmstat.MemMin = $mem.Minimum

  $allvms += $vmstat

}

$allvms | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\VMs.csv" -noTypeInformation

Now to improve a little bit the script how can i add metrics for linux VM ?

Reply
0 Kudos