Enthusiast
Enthusiast

Maximum, Minimum and Average CPU & Memory usage per cluster

Hi,

I need to get Maximum, Minimum and Average CPU & Memory usage per cluster. For a specific amount of time. for example a week. Currently I am using this:

foreach($vmHost in Get-VMHost){
  $hoststat = "" | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
  $hoststat.HostName = $vmHost.name

  $statcpu = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-7) -Finish (Get-Date)-MaxSamples 100 -stat cpu.usage.average
  $statmem = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-7) -Finish (Get-Date)-MaxSamples 100 -stat mem.usage.average

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

  $hoststat.CPUMax = $cpu.Maximum
  $hoststat.CPUAvg = $cpu.Average
  $hoststat.CPUMin = $cpu.Minimum
  $hoststat.MemMax = $mem.Maximum
  $hoststat.MemAvg = $mem.Average
  $hoststat.MemMin = $mem.Minimum
  $allhosts += $hoststat
}
$allhosts | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv $outputfile -noTypeInformation

This though gives me the information I want, per host, not per cluster.

How can I get this information per cluster?

Thanks

12 Replies
Leadership
Leadership

For the Historical Intervals you can use a cluster on the Entity parameter.

Like this

$allClusters = @()

Get-Cluster | %{

    $clusstat = "" | Select ClusterName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin

    $clusstat.ClusterName = $_.Name

    $statcpu = Get-Stat -Entity $_ -Start (get-date).AddDays(-7) -Finish (Get-Date)-MaxSamples 100 -Stat cpu.usage.average

    $statmem = Get-Stat -Entity $_ -Start (get-date).AddDays(-7) -Finish (Get-Date)-MaxSamples 100 -Stat mem.usage.average

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

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

    $clusstat.CPUMax = $cpu.Maximum

    $clusstat.CPUAvg = $cpu.Average

    $clusstat.CPUMin = $cpu.Minimum

    $clusstat.MemMax = $mem.Maximum

    $clusstat.MemAvg = $mem.Average

    $clusstat.MemMin = $mem.Minimum

    $allClusters += $hoststat

}

$allClusters |

Select ClusterName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin |

Export-Csv $outputfile -noTypeInformation


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

0 Kudos
Enthusiast
Enthusiast

Thanks for the info. I have a couple of problems.

I get message in ISE saying "Get-Stat, A specific parameter was not correct, querySpec.size"

and the spread sheet shows identical information for all the clusters, and no cluster name. (see screen shot)

0 Kudos
Leadership
Leadership

You seem to be hitting the issue documented in KB2107096


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

0 Kudos
Enthusiast
Enthusiast

Hi,

just responding as per reply from you on Re:Solved: get cluster utilization info as this may be better as i only want a cluster average etc rthe than on hosts.

the KB2107096 i do not appear to have config.vpxd.stats.MaxQueryMetrics?

running 6.7

thanks

mark

0 Kudos
Leadership
Leadership

Then you might need to add it (provided you are seeing that error).

Get-Cluster |

New-AdvancedSetting -Name config.vpxd.stats.maxQueryMetrics -Value -1 -Confirm:$false


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

0 Kudos
Enthusiast
Enthusiast

i just get the below:-

PS C:\temp> Get-Cluster | New-AdvancedSetting -Name config.vpxd.stats.maxQueryMetrics -Value -1 -Confirm:$true

New-AdvancedSetting : 13/11/2019 13:56:35 New-AdvancedSetting 'Type' parameter is mandatory for Cluster

entities.

At line:1 char:15

+ ... t-Cluster | New-AdvancedSetting -Name config.vpxd.stats.maxQueryMetri ...

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-AdvancedSetting], ViError

    + FullyQualifiedErrorId : Core_NewAdvancedSetting_TryValidateTypeParameter_TypeNotSpecifiedForCluster,VM

   ware.VimAutomation.ViCore.Cmdlets.Commands.NewAdvancedSetting

0 Kudos
Leadership
Leadership

Yes, that needs the Type parameter

Get-Cluster |

New-AdvancedSetting -Name config.vpxd.stats.maxQueryMetrics -Value -1 -Type VIServer -Confirm:$false


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

0 Kudos
Enthusiast
Enthusiast

no luck? Smiley Sad

New-AdvancedSetting : 13/11/2019 14:33:32 New-AdvancedSetting Settings of type 'VIServer' are not valid for

entity of type 'ClusterImpl'.

At line:2 char:1

+ New-AdvancedSetting -Name config.vpxd.stats.maxQueryMetrics -Value -1 ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (HEX-IT:ClusterImpl) [New-AdvancedSetting], ViError

    + FullyQualifiedErrorId : Client20_SystemManagementServiceImpl_NewAdvancedSetting_InvalidSettingType,VMw

   are.VimAutomation.ViCore.Cmdlets.Commands.NewAdvancedSetting

0 Kudos
Leadership
Leadership

I'm obviously not yet fully awake :smileygrin:
That setting goes on the VCSA

New-AdvancedSetting -Entity $global:DefaultVIServer -Name config.vpxd.stats.maxQueryMetrics -Value -1 -Type VIServer -Confirm:$false


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

Enthusiast
Enthusiast

haha i know that feeling Smiley Happy

Perfect that worked this time without error.

I've shortened this script as only want average and not too fussed about min/max etc but nothing appears in $allclusters yet one of the clusters with averages appears in $clusstat, $statcpu etc.... i know what im missing but dont know how to add the collected details to $allclusters?  sorry for being a pain...

$allClusters = @()

Get-Cluster | %{

    $clusstat = "" | Select ClusterName, MemAvg, CPUAvg

    $clusstat.ClusterName = $_.Name

    $statcpu = Get-Stat -Entity $_ -Start (get-date).AddDays(-7) -Finish (Get-Date) -Stat cpu.usage.average

    $statmem = Get-Stat -Entity $_ -Start (get-date).AddDays(-7) -Finish (Get-Date) -Stat mem.usage.average

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

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

    $clusstat.CPUAvg = $cpu.Average

    $clusstat.MemAvg = $mem.Average

}

0 Kudos
Leadership
Leadership

Try like this

$allClusters = @()

Get-Cluster | %{

    $clusstat = "" | Select ClusterName, MemAvg, CPUAvg

    $clusstat.ClusterName = $_.Name

    $statcpu = Get-Stat -Entity $_ -Start (get-date).AddDays(-7) -Finish (Get-Date) -Stat cpu.usage.average

    $statmem = Get-Stat -Entity $_ -Start (get-date).AddDays(-7) -Finish (Get-Date) -Stat mem.usage.average

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

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

    $clusstat.CPUAvg = $cpu.Average

    $clusstat.MemAvg = $mem.Average

    $allClusters += $clusstat

}


$allClusters


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

Enthusiast
Enthusiast

Brilliant - worked a treat - ThankYou Smiley Happy

I've also managed to add your logic in for 'business hours/working days' for a month as below Smiley Happy

$workingDays = "Monday","Tuesday","Wednesday","Thursday","Friday"

$dayStart = New-Object DateTime(1,1,1,7,30,0)     # 07:30 AM

$dayEnd = New-Object DateTime(1,1,1,17,30,0)      # 05:30 PM

$allClusters = @()

Get-Cluster | %{

    $clusstat = "" | Select ClusterName, CPUAvg, MemAvg

    $clusstat.ClusterName = $_.Name

    #$statcpu = Get-Stat -Entity $_ -Start $todayMidnight.AddDays(-14) -Finish $todayMidnight.AddDays(-7) -Stat cpu.usage.average | Where-Object {$workingDays -contains $_.Timestamp.DayOfWeek -and $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay}

    #$statmem = Get-Stat -Entity $_ -Start $todayMidnight.AddDays(-14) -Finish $todayMidnight.AddDays(-7) -Stat mem.usage.average | Where-Object {$workingDays -contains $_.Timestamp.DayOfWeek -and $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay}

    $statcpu = Get-Stat -Entity $_ -Start (get-date).AddDays(-30) -Finish (Get-Date) -Stat cpu.usage.average | Where-Object {$workingDays -contains $_.Timestamp.DayOfWeek -and $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay}

    $statmem = Get-Stat -Entity $_ -Start (get-date).AddDays(-30) -Finish (Get-Date) -Stat mem.usage.average | Where-Object {$workingDays -contains $_.Timestamp.DayOfWeek -and $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay}

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

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

    $clusstat.CPUAvg = $cpu.Average

    $clusstat.MemAvg = $mem.Average

    $allClusters += $clusstat

}

$allClusters | select ClusterName, @{n="CPUAvg"; e={[Math]::ceiling($_.CPUavg)}}, @{n="MemAvg"; e={[Math]::ceiling($_.Memavg)}}

0 Kudos