m_agi
Contributor
Contributor

Power CLI : Average CPU and Memory Utilization

HI,


Can some body provide me PowerCLI Script to find out Average CPU and Memory Utilization percentage for an ESX Cluster (Containing multiple ESX Hosts)?

Note : Require Average CPU and memory Utilization for multiple ESX Hosts together in a Cluster.

0 Kudos
13 Replies
LucD
Leadership
Leadership

Over what time interval?


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

0 Kudos
LucD
Leadership
Leadership

If you are looking for historical data, have a look at Re: Maximum, Minimum and Average CPU & Memory usage per cluster


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

0 Kudos
m_agi
Contributor
Contributor

This is for current date and Time. No Intervals

0 Kudos
LucD
Leadership
Leadership

Try something like this

Get-Cluster |

Select Name,

    @{N='CPUAvgPercent';E={$script:stats = Get-Stat -Entity $_ -Stat cpu.usage.average,mem.usage.average -Realtime -MaxSamples 1 -Instance '*';

        $script:stats | where{$_.MetricId -eq 'cpu.usage.average'} | Select -ExpandProperty Value}},

    @{N='MemAvgPercent';E={$script:stats | where{$_.MetricId -eq 'mem.usage.average'} | Select -ExpandProperty Value}}


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

0 Kudos
m_agi
Contributor
Contributor

I tried the script. But it is not showing for the cluster for which I want.

Can you get me a script like getting an input of the cluster name and finding out the average CPU and Memory Utilization percentage?

0 Kudos
LucD
Leadership
Leadership

If you change Get-Cluster into Get-Cluster -Name YourCluster

you will get the data only for your cluster


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

0 Kudos
m_agi
Contributor
Contributor

Thanks. There are three clusters. it worked only for one cluster. For other two clusters its not coming...

0 Kudos
LucD
Leadership
Leadership

Can you see realtime performance data for those 2 missing clusters in the Web Client under the Performance - Advanced tab?


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

0 Kudos
VM_Denton
Contributor
Contributor

Sorry to bump an old post but I'm having the same problem. Get-Stat won't return any metrics. I tried the script mentioned by LucD and I get a list of my clusters but no stats. I can see real time stats for the cluster on the advanced tab.  I can even export it out to .csv. But none of the get-stats regarding cpu or memory will return anything. I'm pretty new to PowerShell and I've hunted heavilty on this issue.

Could use some help if anyone has a suggestion!

0 Kudos
virtualDD
Enthusiast
Enthusiast

I have stumbled upon this today and like seeminlgy everyone else the original solution did not provide data for all clusters. Since I have access to the hosts’s current usage metrics in the vmhost objects I decided to skip the stats and read them out directly on the hosts and average them across the cluster.

(sorry to bump this old thread, but since Google lead me here I thought I might as well post my solution here)

Here is the code (copy & pasting it on the iPad so it might no look pretty) :

$clusters = get-cluster

$myClusters = @()

foreach ($cluster in $clusters) {

   $hosts = $cluster |get-vmhost

   [double]$cpuAverage = 0

   [double]$memAverage = 0

   Write-Host $cluster

   foreach ($esx in $hosts) {

   Write-Host $esx

   [double]$esxiCPUavg = [double]($esx | Select-Object @{N = 'cpuAvg'; E = {[double]([math]::Round(($_.CpuUsageMhz) / ($_.CpuTotalMhz) * 100, 2))}} |Select-Object -ExpandProperty cpuAvg)

   $cpuAverage = $cpuAverage + $esxiCPUavg

   [double]$esxiMEMavg = [double]($esx | Select-Object @{N = 'memAvg'; E = {[double]([math]::Round(($_.MemoryUsageMB) / ($_.MemoryTotalMB) * 100, 2))}} |select-object -ExpandProperty memAvg)

   $memAverage = $memAverage + $esxiMEMavg

  }

   $cpuAverage = [math]::Round(($cpuAverage / ($hosts.count) ), 1)

   $memAverage = [math]::Round(($memAverage / ($hosts.count) ), 1)

   $ClusterInfo = "" | Select-Object Name, CPUAvg, MEMAvg

   $ClusterInfo.Name = $cluster.Name

   $ClusterInfo.CPUAvg = $cpuAverage

   $ClusterInfo.MEMAvg = $memAverage

   $myClusters += $ClusterInfo

}

$myClusters

Edit: Updated my script to be multi-vcenter compatible. Old version used VMhost ID which is not unique across vCenters. The new approach gets the vmhost object from the cluster object which proved to work with multiple vCenter servers connected at the same time when running this script.

Here is a sample output of the script:

image003.png

0 Kudos
LucD
Leadership
Leadership

Nice solution for the issue, but be aware that this way of doing it also assumes that all the nodes in a cluster are identical (CPU & Mem-wise).

If that is not the case, you will need a slightly different way, i.e. adding weighing factors for each ESXi node, of calculating the cluster averages.


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

virtualDD
Enthusiast
Enthusiast

Hi Luc,

That's interesting. I really do make the assumption that all hosts have the same amount of CPU and RAM resources because that is the case at the customer's where I needed this script.

How would you go about adding weighing factors to this? I assume that this is not an issue with your original approach since the performance stats on the vCenter somehow calculate the "weight" of each esxi host in the cluster?

0 Kudos
LucD
Leadership
Leadership

I would keep it simple.

For example; host A has 4 times the amount of memory of host B.

In that case I would just use that ratio to calculate the average:

$memAvg = ($memAvgHostA * 4 + $memAvgHostB)/5

That is an excellent question.
Not too sure how, and if, vCenter uses weighing factors for ESXi node that are not identical.
I'll have to do some experimenting :smileygrin:


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

0 Kudos