VMware Cloud Community
NPXer
Contributor
Contributor

Script for Performance Stats - CPU/Memory (Max/Min/Avg)

We have monthly performance statistics to gather. A new requirement is for hourly CPU/Memory (Max/Min/Avg).Here's what we've come with so far:

$allhosts = @()
$allvms = @()
$hosts = Get-VMHost
$vms = Get-Vm
# connect-viserver


foreach($vmHost in $hosts){
$stats = Get-Stat -Entity $vmHost -Start (get-date).AddDays(-7) -Stat "cpu.usage.average","mem.usage.average"
$groups = $stats | Group-Object -Property {$_.Timestamp.DayOfYear,$_.Timestamp.Hour}
$groups | % {
$cpuMeasure = $_.Group | where{$_.MetricId -eq "cpu.usage.average" -and $_.Instance -eq ""} | `
Measure-Object -Property Value -Average -Maximum -Minimum
$memMeasure = $_.Group | where{$_.MetricId -eq "mem.usage.average"} | `
Measure-Object -Property Value -Average -Maximum -Minimum
$row = "" | Select HostName,Timestamp,CpuAvg,CpuMin,CpuMax,MemAvg,MemMin,MemMax
$row.HostName = $_.Group[0].Entity.Name
$row.Timestamp = $_.Group[0].Timestamp.Date.AddHours($_.Group[0].Timestamp.Hour)
$row.CpuAvg = ::Round($cpuMeasure.Average, 2)
$row.CpuMin = ::Round($cpuMeasure.Minimum, 2)
$row.CpuMax = ::Round($cpuMeasure.Maximum, 2)
$row.MemAvg = ::Round($memMeasure.Average, 2)
$row.MemMin = ::Round($memMeasure.Minimum, 2)
$row.MemMax = ::Round($memMeasure.Maximum, 2)
$allhosts += $row
}
}
$File = "C:\temp\VCScripts\Reports\Test\vmHosts_wk.csv"
$allhosts | Sort-Object -Property Timestamp | Export-Csv $File -noTypeInformation -useculture
foreach($vm in $vms){
$stats = Get-Stat -Entity $vm -Start (get-date).AddDays(-7) -Stat "cpu.usage.average","mem.usage.average"
$groups = $stats | Group-Object -Property {$_.Timestamp.DayOfYear,$_.Timestamp.Hour}
$groups | % {
$cpuMeasure = $_.Group | where{$_.MetricId -eq "cpu.usage.average" -and $_.Instance -eq ""} | `
Measure-Object -Property Value -Average -Maximum -Minimum
$memMeasure = $_.Group | where{$_.MetricId -eq "mem.usage.average"} | `
Measure-Object -Property Value -Average -Maximum -Minimum
$row = "" | Select HostName,Timestamp,CpuAvg,CpuMin,CpuMax,MemAvg,MemMin,MemMax
$row.HostName = $_.Group[0].Entity.Name
$row.Timestamp = $_.Group[0].Timestamp.Date.AddHours($_.Group[0].Timestamp.Hour)
$row.CpuAvg = ::Round($cpuMeasure.Average, 2)
$row.CpuMin = ::Round($cpuMeasure.Minimum, 2)
$row.CpuMax = ::Round($cpuMeasure.Maximum, 2)
$row.MemAvg = ::Round($memMeasure.Average, 2)
$row.MemMin = ::Round($memMeasure.Minimum, 2)
$row.MemMax = ::Round($memMeasure.Maximum, 2)
$allvms += $row
}
}
$File2 = "C:\temp\VCScripts\Reports\Test\vms_wk.csv"
$allvms | Sort-Object -Property Timestamp | Export-Csv $File2 -noTypeInformation -useculture

The problem is I'm getting the same line output for Avg/Min/Max. Any ideas?

VMware's 30 minute sampling should suffice, but given VC only holds the data for a week, I was planning to run the script every week to capture the data, then combine with the others for the montly report.

0 Kudos
16 Replies
LucD
Leadership
Leadership

Just tried the script and I see minor differences for Min, Max and Avg.

Since you only have 2 measured values for a 1 hour interval when your values are close together the Min, Max and Avg can look the same, especially when you round the values.

Perhaps try leaving out the Round function. Normally you should then see some differences.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
NPXer
Contributor
Contributor

Hmmm, I well used an original script, and finally saw some variance. However now when I run the same script on another Virtual Center environment, I'm only getting results every two hours and the reslts for CPU Min/Max/Average are the same as well as for Memory Min/Max/Average.

0 Kudos
LucD
Leadership
Leadership

If you are seeing 2 hour intervals that means you're in Historical Interval 3.

That means you requested a period for which part of it is older than 1 week.

Or the Statistics Levels on that vCenter have been updated.

See my PowerCLI & vSphere statistics – Part 1 – The basics for more info on historical intervals and sample periods.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
NPXer
Contributor
Contributor

I'm still digesting the info, but I did find when I changed the "$stats" command on line 8, to less than -7, I would get differentiation in the Min/Max/Avg.

I need a way to automate this script. What the best means of doing so? Obviously I need to kick off the VMware vSphere PowerCLI.

0 Kudos
LucD
Leadership
Leadership

That could mean that you fall into another Historical Interval once you go further back than 6 days.

As a consequence the values would be averaged over longer intervals and your calculated values would then indeed be the same for min, max and avg.

To schedule such a task you can use the Windows Task Scheduler.

Have a look at Alan's blog post Running a PowerCLI Scheduled task

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
NPXer
Contributor
Contributor

OK when I kick off the following task, I get the error message below (in red).

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\

Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& 'C:\temp\VCScr

ipts\cpu-mem-stats-ESXHosts.ps1' servername"

Get-VMHost : 7/23/2010 11:33:20 AM Get-VMHost You are not currently c onnected to any servers. Please connect first using Connect-VIServer or one of its aliases. At C:\temp\VCScripts\cpu-mem-stats-ESXHosts.ps1:12 char:20 + $hosts = Get-VMHost <<<< Get-Stat : Value cannot be found for the mandatory parameter Entity At C:\temp\VCScripts\cpu-mem-stats-ESXHosts.ps1:16 char:19 + $stats = Get-Stat <<<< -Entity $vmHost -Start (get-date).AddDays(-1) -St at "cpu.usage.average","mem.usage.average" -ErrorAction SilentlyContinue

0 Kudos
LucD
Leadership
Leadership

You should have a Connect-VIServer line at the beginning of your script.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Bean78
Contributor
Contributor

Hello,

am running 6.7 vSphere environment, am using windows powershell with VMware modules deployed in it. I tried the script provided in the blog, but i obtain an error stating

"::Round : The term '::Round' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was

included, verify that the path is correct and try again"

As per the specified path in the script, i do see 2 text file getting generated one for ESXi hosts and another for virtual machines, but no data related to CPU and Memory is recorded in the text file

"HostName","Timestamp","CpuAvg","CpuMin","CpuMax","MemAvg","MemMin","MemMax"

"xxxxxx","9/2/2019 9:00:00 PM",,,,,,

"xxxxxx","9/2/2019 9:00:00 PM",,,,,,

"xxxxxx","9/2/2019 9:00:00 PM",,,,,,

"xxxxxx","9/2/2019 9:00:00 PM",,,,,,

"xxxxxx","9/2/2019 9:00:00 PM",,,,,,

"xxxxxx","9/2/2019 9:00:00 PM",,,,,,

"xxxxxx","9/2/2019 9:00:00 PM",,,,,,

Let me know if anyone can assist me on this

0 Kudos
LucD
Leadership
Leadership

Looks like the poster had an issue with his copy/paste.

The Round method is defined in the Math class, so you would need to do

$row.CpuAvg = [math]::Round($cpuMeasure.Average, 2)


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

0 Kudos
Bean78
Contributor
Contributor

Thanks LucD appreciate your quick help on this post, now am able to obtain the values can you please clarify whether the value populated against each section is the utilization percentage or the actual resource usage in their relevant metrics. Do we have a script which populates the statistical performance data for a month time (including compute, network, storage utilization)

HostName,Timestamp,CpuAvg,CpuMin,CpuMax,MemAvg,MemMin,MemMax

xxxxx,9/2/2019 23:00,0.14,0.14,0.15,1.01,0.99,1.06

xxxxx,9/2/2019 23:00,0.15,0.14,0.15,1.01,0.99,1.08

xxxxx,9/2/2019 23:00,4.98,4.76,5.44,5.87,5.32,6.51

xxxxx,9/2/2019 23:00,0.14,0.14,0.16,1.01,0.99,1.15

xxxxx,9/2/2019 23:00,4.49,3.74,6.33,23.3,17.47,26.78

xxxxx,9/2/2019 23:00,2.22,1.97,5.06,13.69,10.28,19.41

xxxxx,9/2/2019 23:00,0.48,0.28,7.33,4.6,2.94,16.4

xxxxx,9/2/2019 23:00,0.49,0.47,0.6,1.66,1.29,2.49

xxxxx,9/2/2019 23:00,1.7,1.56,2.89,2.43,1.98,4.84

xxxxx,9/2/2019 23:00,0.04,0.04,0.05,0.99,0.99,0.99

I need one more guidance, can you help me with a script which generates the virtual machine inventory from a vCenter console that includes even the custom annotations against each object. Any assistance on this is much appreciated

0 Kudos
LucD
Leadership
Leadership

The meaning of the different metrics is explained in the PerformanceManager documentation.

Also, have a look at my PowerCLI & VSphere Statistics – Part 1 – The Basics post, which tries to explain the concept of vSphere performance statistics.

For your additional question, may I suggest you start a new thread.


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

0 Kudos
Bean78
Contributor
Contributor

0 Kudos
Bean78
Contributor
Contributor

Thanks LucD it was a great post, just want to know do you have a script which provides a report of these statistical performance for a month time.

A format of something like this,

VMname,Day 1(CPU-min,CPU-Avg,CPU-Max, Mem-min,Mem-Avg,Mem-Max, HDD1 cap, HDD1 utilized), Day 2 (CPU-min,CPU-Avg,CPU-Max, Mem-min,Mem-Avg,Mem-Max, HDD1 cap, HDD1 utilized), etc... Day 30th

0 Kudos
LucD
Leadership
Leadership

If you read my "Basics" post, you will have noticed/discovered that keeping minimum and maximum metrics, besides the average ones, will require a higher statistics level.

If you want to go back 30 days or more, you will probably want to look at the Statistics Levels for Historic Period 3 and 4.

But be aware that increasing the Statistics Levels will also increase the size of the vCenter database.


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

0 Kudos
Bean78
Contributor
Contributor

LucD, can you clarify whether the script provides us with the CPU and memory usage of a VM and a host Min, average and Max values for a week time. If yes then how do we modify the same script to provide us with the report for a month time.

0 Kudos
LucD
Leadership
Leadership

As I tried to explain in my previous reply, it depends what Statistics Levels you have defined.


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

0 Kudos