PowerCLI

 View Only
Expand all | Collapse all

Power CLI : Average CPU and Memory Utilization

  • 1.  Power CLI : Average CPU and Memory Utilization

    Posted Jul 26, 2017 12:42 PM

    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.



  • 2.  RE: Power CLI : Average CPU and Memory Utilization

    Posted Jul 26, 2017 12:44 PM

    Over what time interval?



  • 3.  RE: Power CLI : Average CPU and Memory Utilization

    Posted Jul 26, 2017 01:02 PM

    This is for current date and Time. No Intervals



  • 4.  RE: Power CLI : Average CPU and Memory Utilization

    Posted Jul 26, 2017 01:10 PM

    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}}



  • 5.  RE: Power CLI : Average CPU and Memory Utilization

    Posted Jul 26, 2017 01:49 PM

    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?



  • 6.  RE: Power CLI : Average CPU and Memory Utilization

    Posted Jul 26, 2017 01:51 PM

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

    you will get the data only for your cluster



  • 7.  RE: Power CLI : Average CPU and Memory Utilization

    Posted Jul 26, 2017 02:17 PM

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



  • 8.  RE: Power CLI : Average CPU and Memory Utilization

    Posted Jul 26, 2017 04:23 PM

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



  • 9.  RE: Power CLI : Average CPU and Memory Utilization

    Posted Oct 11, 2017 09:30 PM

    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!



  • 10.  RE: Power CLI : Average CPU and Memory Utilization

    Posted May 04, 2018 09:27 AM

    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:



  • 11.  RE: Power CLI : Average CPU and Memory Utilization

    Posted May 07, 2018 07:59 AM

    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.



  • 12.  RE: Power CLI : Average CPU and Memory Utilization

    Posted May 08, 2018 01:06 PM

    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?



  • 13.  RE: Power CLI : Average CPU and Memory Utilization

    Posted May 08, 2018 01:52 PM

    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:



  • 14.  RE: Power CLI : Average CPU and Memory Utilization

    Posted Jul 26, 2017 12:46 PM

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