alexanderlloyd2
Contributor
Contributor

Could someone help with PowerCLI sample with VM memory and CPU average and peak usage

Could someone help with PowerCLI sample with VM memory and CPU average and peak usage on daily basis during business hours?

Thank you

 

Alex

0 Kudos
7 Replies
scott28tt
VMware Employee
VMware Employee

Thread reported to notify moderators it needs moving, there is a dedicated area for PowerCLI whereas the {code} area is for SDK matters,

 

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast

May be you can check Power CLI : Average CPU and Memory Utilization

 

Regards

Ranjithbabhu R

0 Kudos
LucD
Leadership
Leadership

That code is about Realtime stats with only 1 sample per cluster.
Not exactly what the question is about imho.

To the creator of this thread: you will have to provide some more information:

- what are your Statistics Levels in vCenter
- how long do you keep statistical data in your vCenter
- what time interval are you looking at


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

0 Kudos
alexanderlloyd2
Contributor
Contributor

Hi LucD,

 

- what are your Statistics Levels in vCenter level 2
- how long do you keep statistical data in your vCenter 3 month
- what time interval are you looking a every hour between 8Am and 8PM average and peak memory and CPU usage. 

 

Thank you

0 Kudos
LucD
Leadership
Leadership

I also need to know how far back in those three months of data you want to go.
Due to aggregation, the interval for metrics older than 1 month is 1 day.
And for metrics older than 1 month, it is 2 hours.
Only for the last week you can produce reports with 1 hour intervals.
See also PowerCLI & vSphere statistics – Part 1 – The basics

Also, with Statistics level 2 you only collect averages over the interval.
For maxima (peak) you need Statistics Level 4 in most cases.
See the PerformanceManager API Reference page.


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

0 Kudos
alexanderlloyd2
Contributor
Contributor

Hi LucD,

 

I am looking to collect average and peaks for the previous day between business hours. The concern is that 2hrs average would dilute the metrics as there is no activity after business hours. Thank you 

0 Kudos
LucD
Leadership
Leadership

You could do something like this.
You might have to adapt the $dayStart and $dayFinish variables to reflect your environment.

$finish = (Get-Date -Hour 0 -Minute 0 -second 0).AddMinutes(-1)

$dayStart = New-Object DateTime(1, 1, 1, 8, 0, 0)     # 08:00 AM
$dayEnd = New-Object DateTime(1, 1, 1, 18, 0, 0)      # 06:00 PM

$sStat = @{
  Entity = Get-VM | Where-Object { $_.PowerState -eq 'PoweredOn' }
  Start = $finish.adddays(-1)
  Finish = $finish
  Stat = 'cpu.usage.average','mem.usage.average'
}

Get-Stat @SStat | Where-Object {
  $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and
  $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay
} | group-object -property {$_.Entity.Name} -pipelinevariable vmHourly |
foreach-object -process {
  $vmHourly.Group | Group-Object -Property { $_.Timestamp.Hour } -pipelinevariable hour |
  foreach-object -process {
    new-object -typename PSObject -property @{
      VM = $vmHourly.Name
      Timestamp = Get-Date $finish -Hour $hour.Name -minute 0 -second 0
      CPUAvg = [math]::Round(($hour.Group | Where-Object { $_.MetricId -eq 'cpu.usage.average'} |
        measure-object -property Value -average | select -expandproperty average),1)
      MemAvg = [math]::Round(($hour.Group | Where-Object { $_.MetricId -eq 'mem.usage.average' } |
        measure-object -property Value -average | select -expandproperty average),1)
    }
  }
} | Export-Csv .\report.csv -NoTypeInformation -UseCulture


 


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

0 Kudos