Automation

 View Only
Expand all | Collapse all

Powercli Script to Capture ESXi Cluster CPU & Memory Usage

  • 1.  Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 12, 2016 04:24 PM

    Hello All,

    I'm having the hardest time creating a script that will give me Cluster CPU and Memory Usage. I just need something simple if there is such a thing?

    Example of what I'm looking for:

    Cluster nameCPU GHz CapacityCPU GHz UsedCPU % Free

     

    Cluster nameMemory CapacityMemory UsedMemory % Free

    I need to present these in graphical format but I could look to do something with a pivot table afterwards.

    Any help would be greatly appreciated.


    Thanks



  • 2.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 12, 2016 05:54 PM

    The following will get the data into an Excel spreadheet.

    It uses the Export-Excel cmdlet from the Doug's ImportExcel module.

    You can change the Start date.

    The script places everything in one worksheet, but it can easily be adapted to have separate worksheets for CPU and memory.

    $clusterName = 'MyCluster'

    $stat = 'cpu.usagemhz.average','mem.usage.average'

    $entity = Get-Cluster -Name $clusterName

    $start = (Get-Date).AddDays(-2)

    Get-Stat -Entity $clusterName -Stat $stat -Start $start |

    Group-Object -Property Timestamp |

    Sort-Object -Property Name |

    Select @{N='Cluster';E={$entity.Name}},

        @{N='Time';E={$_.Group[0].Timestamp}},

        @{N='CPU GHz Capacity';E={$script:capacity = [int]($entity.ExtensionData.Summary.TotalCPU/1000); $script:capacity}},

        @{N='CPU GHz Used';E={$script:used = [int](($_.Group | where{$_.MetricId -eq 'cpu.usagemhz.average'} | select -ExpandProperty Value)/1000); $script:used}},

        @{N='CPU % Free';E={[int](100 - $script:used/$script:capacity*100)}},

        @{N='Mem Capacity GB';E={$script:mcapacity = [int]($entity.ExtensionData.Summary.TotalMemory/1GB); $script:mcapacity}},

        @{N='Mem Used GB';E={$script:mused = [int](($_.Group | where{$_.MetricId -eq 'mem.usage.average'} | select -ExpandProperty Value) * $script:mcapacity/100); $script:mused}},

        @{N='Mem % Free';E={[int](100 - $script:mused/$script:mcapacity*100)}} |

    Export-Excel -Path C:\cluster-stats.xlsx -WorkSheetname 'Stats' -AutoSize -FreezeTopRow



  • 3.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 13, 2016 10:52 AM

    Thanks for the very quick reply LucD!

    I've got Doug's ImportExcel module downloaded but I'm having issues getting it imported. We are using PowerCli 5.8. This is my error

    Unable to find type [PSPlot]: make sure that the assembly containing this type is loaded.

    At C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ImportExcel\ImportExcel.psm1:25 char:29

    +         [OutputType([PSPlot] <<<< )]

        + CategoryInfo          : InvalidOperation: (PSPlot:String) [], ParentContainsErrorRecordException

        + FullyQualifiedErrorId : TypeNotFound

    Import-Module : The specified module '.\ImportExcel.psm1' was not loaded because no valid module file was found in any module directory.

    At line:1 char:14

    + Import-Module <<<<  .\ImportExcel.psm1

        + CategoryInfo          : ResourceUnavailable: (.\ImportExcel.psm1:String) [Import-Module], FileNotFoundException

        + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

    I checked the .psd1 and there wasn't a  minimum version specified

    Instead of specifying the Cluster would it be possible to return all clusters in each vCenter server? or would that require a loop?



  • 4.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 13, 2016 12:51 PM

    After a bit of research it looks like it doesn't like Get-Stat -Entity "Cluster"

    If I replace the cluster name with the name of my vcenter server the command runs successfully.

    Get-Stat -Entity 'vcenter' -Stat $stat -Start $start

    works without any issues

    This is the error

    Get-Stat : 13/07/2016 15:24:58    Get-Stat        A specified parameter was not correct.

    querySpec.size   

    At line:13 char:9

    + Get-Stat <<<<  -Entity $clusterName -Stat $stat -Start $start |

        + CategoryInfo          : NotSpecified: (:) [Get-Stat], InvalidArgument

        + FullyQualifiedErrorId : Client20_RuntimeDataServiceImpl_GetStats_ErrorRetreivingPerfMetrics,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetViStats



  • 5.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 14, 2016 06:19 PM

    Are you by any chance on vSPhere 5.5U3 ?

    Then it might be the problem described in Veeam's KB2071



  • 6.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 16, 2016 05:19 AM

    Hello LucD

    Get-Stat -Entity clustername is resulting nothing after upgrade of vSpehre 6 U2 even after setting adavance setting to -1

    Get-AdvancedSetting -Entity YourvCenter -Name config.vpxd.stats.MaxQueryMetrics |

    Set-AdvancedSetting -Value -1



  • 7.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 16, 2016 07:10 AM

    Did you check the value of maxQuerySize in the file vpxd.cfg ?



  • 8.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 16, 2016 08:15 AM

    Hello LucD,

    Thanks for your reply.

    Below is entry in vpxd.cfg file

    <stats>

          <maxQueryMetrics>-1</maxQueryMetrics>

        </stats>

        <support>



  • 9.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 16, 2016 09:37 AM

    Depending on which Historical Interval you are looking at, you will have to wait till the aggregation jobs have done their thing.

    What interval are you looking at?

    When did you change the value?



  • 10.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 16, 2016 03:15 PM

    Before upgrading the VC I was able to collect up to 90 days of CPU and memory usage at cluster entity . Now I dont see results for even 2 days ..

    I have changed the advance settings a day before



  • 11.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 18, 2016 12:51 PM

    And do you see that Performance data in the vSphere or Web client?



  • 12.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 23, 2016 04:30 PM

    Hello Luc,

    I do see performance chart in webclient but no results when I run below coomand

    Webclient > clutserts > select cluster name > performance > advanced > CPU I do see values available from Feb

    PowerCLI C:\> Get-Cluster -Name Clustername | Get-Stat -start (get-date).AddDays(-40) -Finish (Get-Date).AddDays(-10) -MaxSamples 10000 -stat cpu.usage.average | Measure-Object -Property value -Average -Maximum -Minimum | select Average

    PowerCLI C:\>



  • 13.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 27, 2016 12:30 PM

    Hello LucD

    I opened a support case with VMware on this . They pointing the problem with latest version of PCLI interacion with vSpehre6.

    The same set of command works fine when I run against my other VC which is VCSA.



  • 14.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 19, 2016 12:57 PM

    We are on vSphere 5.5U3. Unfortunately we won't be able to apply their workaround as that applies to the Veeam One application



  • 15.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 19, 2016 12:59 PM

    That is indeed a Veeam KB article, but has no further relation to a Veeam product.

    It's purely a vCenter issue.



  • 16.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 19, 2016 02:24 PM

    Yep. It helped when I clicked through to the vmware kb rather than just reading that page.

    I'm followed that in our lab and it seems to have done the trick although I'll have to revisit the Export-Excel function.

    The output has displayed in 30 minute intervals. Is there a way to total these in the script as if I run this for the 30 day interval requested there will be a lot of data generated.

    I really appreciate your help



  • 17.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 19, 2016 03:45 PM

    If you run for a 30 day interval you will get 1 observation per day.

    See my PowerCLI & vSphere statistics – Part 1 – The basics for some info on Historical Intervals.

    But if want to have an average per day/per week/per month, that can be done.

    Let me know what you would to have.



  • 18.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 20, 2016 02:40 PM

    Hi Luc

    That makes a bit more sense after reading your guide. I think I've got what I need with

    Add-PSSnapin VMware.VimAutomation.Core

    Connect-VIServer 'vcenter'

    $clusterName = 'cluster name'

    $stat = 'cpu.usagemhz.average','mem.usage.average'

    $entity = Get-Cluster -Name $clusterName

    $start = (Get-Date).AddDays(-30)

    Get-Stat -Entity $clusterName -Stat $stat -Start $start -MaxSamples 30 -IntervalMins 1440 |

    Group-Object -Property Timestamp |

    Sort-Object -Property Name |

    Select @{N='Cluster';E={$entity.Name}},

        @{N='Time';E={$_.Group[0].Timestamp}},

        @{N='CPU GHz Capacity';E={$script:capacity = [int]($entity.ExtensionData.Summary.TotalCPU/1000); $script:capacity}},

        @{N='CPU GHz Used';E={$script:used = [int](($_.Group | where{$_.MetricId -eq 'cpu.usagemhz.average'} | select -ExpandProperty Value)/1000); $script:used}},

        @{N='CPU % Free';E={[int](100 - $script:used/$script:capacity*100)}},

        @{N='Mem Capacity GB';E={$script:mcapacity = [int]($entity.ExtensionData.Summary.TotalMemory/1GB); $script:mcapacity}},

        @{N='Mem Used GB';E={$script:mused = [int](($_.Group | where{$_.MetricId -eq 'mem.usage.average'} | select -ExpandProperty Value) * $script:mcapacity/100); $script:mused}},

        @{N='Mem % Free';E={[int](100 - $script:mused/$script:mcapacity*100)}} |

    Export-Csv -Path C:\temp\cluster-stats.csv -notypeinformation

    Would you say that was the best way to do it until I can get the Export-Excel function to work?



  • 19.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 20, 2016 03:45 PM

    The Export-Excel function is quite easy to use.

    First, do you see the cmdlet?

    If you don't, you may have place the module in a wrong folder. Check your $PSModulePath variable.

    To use, you just pipe your objects to it.

    Something like this

    $clusterName = 'cluster name'

    $stat = 'cpu.usagemhz.average','mem.usage.average'

    $entity = Get-Cluster -Name $clusterName

    $start = (Get-Date).AddDays(-30)

    Get-Stat -Entity $clusterName -Stat $stat -Start $start -MaxSamples 30 -IntervalMins 1440 |

    Group-Object -Property Timestamp |

    Sort-Object -Property Name |

    Select @{N='Cluster';E={$entity.Name}},

        @{N='Time';E={$_.Group[0].Timestamp}},

        @{N='CPU GHz Capacity';E={$script:capacity = [int]($entity.ExtensionData.Summary.TotalCPU/1000); $script:capacity}},

        @{N='CPU GHz Used';E={$script:used = [int](($_.Group | where{$_.MetricId -eq 'cpu.usagemhz.average'} | select -ExpandProperty Value)/1000); $script:used}},

        @{N='CPU % Free';E={[int](100 - $script:used/$script:capacity*100)}},

        @{N='Mem Capacity GB';E={$script:mcapacity = [int]($entity.ExtensionData.Summary.TotalMemory/1GB); $script:mcapacity}},

        @{N='Mem Used GB';E={$script:mused = [int](($_.Group | where{$_.MetricId -eq 'mem.usage.average'} | select -ExpandProperty Value) * $script:mcapacity/100); $script:mused}},

        @{N='Mem % Free';E={[int](100 - $script:mused/$script:mcapacity*100)}} |

    Export-Excel -Path C:\Temp\cluster-stats.xlsx



  • 20.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 21, 2016 07:26 AM

    HI Luc

    I figured out the problem. The function needs Powershell V3 at least and the server I was running it from was only V2. I've got it imported to my desktop now so hopefully it will work.

    Thanks a lot for all your help :smileyhappy:



  • 21.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 21, 2016 09:21 AM

    I've got a couple of questions Luc if you could spare me some more time.

    How would I be able to change the time of the collection. At the minute it has taken a counter for 01:00 every day. Is there a way to change this to sometime during the day or has it been aggregated already and that is just the display?

    Is there a way to do a Get-Cluster and the a foreach so that the script would only need to run against each vcenter server once? Ideally each cluster would appear on a separate worksheet

    Thanks



  • 22.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage
    Best Answer

    Posted Jul 21, 2016 11:51 AM

    Yes, that is the time the aggregation was done.

    To run against all clusters, and store each cluster's data in a separate worksheet in the spreadsheet, you could do

    $fileName = 'C:\Temp\cluster-stats.xlsx'

    $stat = 'cpu.usagemhz.average','mem.usage.average'

    $start = (Get-Date).AddDays(-30)

    foreach($cluster in Get-Cluster){

        Get-Stat -Entity $cluster -Stat $stat -Start $start -MaxSamples 30 -IntervalMins 1440 |

        Group-Object -Property Timestamp |

        Sort-Object -Property Name |

        Select @{N='Cluster';E={$cluster.Name}},

            @{N='Time';E={$_.Group[0].Timestamp}},

            @{N='CPU GHz Capacity';E={$script:capacity = [int]($cluster.ExtensionData.Summary.TotalCPU/1000); $script:capacity}},

            @{N='CPU GHz Used';E={$script:used = [int](($_.Group | where{$_.MetricId -eq 'cpu.usagemhz.average'} | select -ExpandProperty Value)/1000); $script:used}},

            @{N='CPU % Free';E={[int](100 - $script:used/$script:capacity*100)}},

            @{N='Mem Capacity GB';E={$script:mcapacity = [int]($cluster.ExtensionData.Summary.TotalMemory/1GB); $script:mcapacity}},

            @{N='Mem Used GB';E={$script:mused = [int](($_.Group | where{$_.MetricId -eq 'mem.usage.average'} | select -ExpandProperty Value) * $script:mcapacity/100); $script:mused}},

            @{N='Mem % Free';E={[int](100 - $script:mused/$script:mcapacity*100)}} |

        Export-Excel -Path $fileName -WorkSheetname $cluster.Name

    }

     



  • 23.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 21, 2016 02:28 PM

    Something has gone a bit wrong. When running the original script I get

      

    01/07/2016 01:0086415257641228
    02/07/2016 01:0086336257641628
    03/07/2016 01:0086326357641628
    04/07/2016 01:008699057612379
    05/07/2016 01:0086336257636337
    06/07/2016 01:0086316457637235
    07/07/2016 01:0086316457637435
    08/07/2016 01:0086306557637934
    09/07/2016 01:0086316457638334
    10/07/2016 01:0086326357638433
    11/07/2016 01:0086296657638533
    12/07/2016 01:0086296657638633
    13/07/2016 01:0086316457638433
    14/07/2016 01:0086296657638533
    15/07/2016 01:0086316457638633
    16/07/2016 01:0086326357638733
    17/07/2016 01:0086316457638932
    18/07/2016 01:0086306557639032
    21/06/2016 01:0086326357641428
    22/06/2016 01:0086316457641528
    23/06/2016 01:0086296657641528
    24/06/2016 01:0086296657641528
    25/06/2016 01:0086306557641628
    26/06/2016 01:0086346057641628
    27/06/2016 01:0086336257641628
    28/06/2016 01:0086316457641628
    29/06/2016 01:0086326357641628
    30/06/2016 01:00863263576416

    28

    With the new script it returns

         

    01/07/2016 01:0086010057613177
    02/07/2016 01:0086010057613177
    03/07/2016 01:0086010057613177
    04/07/2016 01:0086010057613177
    05/07/2016 01:0086010057613177
    06/07/2016 01:0086010057613177
    07/07/2016 01:0086010057613177
    08/07/2016 01:0086010057613177
    09/07/2016 01:0086010057613177
    10/07/2016 01:0086010057613177
    11/07/2016 01:0086010057613177
    12/07/2016 01:0086010057613177
    13/07/2016 01:0086010057613177
    14/07/2016 01:0086010057613177
    15/07/2016 01:0086010057613177
    16/07/2016 01:0086010057613177
    17/07/2016 01:0086010057613177
    18/07/2016 01:0086010057613177
    19/07/2016 01:0086010057613177
    22/06/2016 01:0086010057612578
    23/06/2016 01:0086010057612578
    24/06/2016 01:0086010057612578
    25/06/2016 01:0086010057612578
    26/06/2016 01:0086010057612578
    27/06/2016 01:0086010057612578
    28/06/2016 01:0086010057612578
    29/06/2016 01:0086010057612578
    30/06/2016 01:0086010057612578


  • 24.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 21, 2016 03:28 PM

    Seems I forgot to replace some $entity by $cluster.

    I updated the script above



  • 25.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Jul 22, 2016 07:39 AM

    Brilliant.

    Thanks a lot for your help



  • 26.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Dec 14, 2022 09:04 PM

    How to change the date:

    $start = (Get-Date).AddDays(-30)

    By interval Date example:

    $start = 2022-09-01

    $finish= 2022-09-30



  • 27.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Dec 14, 2022 09:08 PM

    You can't with the Get-Stat cmdlet.
    You will have to use a Where-clause after the Get-Stat, like I showed in PowerCLI & vSphere statistics – Part 2 – Come together



  • 28.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Feb 18, 2020 07:38 AM

    This script works for some Cluster, but it does not work for some Cluster (in same datacenter).

    Get-Stat : 18/02/2020 08:32:02 Get-Stat This operation is restricted by the administrator - 'vpxd.stats.maxQueryMetrics'. Contact your

    system administrator.

    At line:7 char:16

    + $clusterName | Get-Stat -Stat $stat -MaxSamples 20  |

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

        + CategoryInfo          : NotSpecified: (:) [Get-Stat], RestrictedByAdministrator

        + FullyQualifiedErrorId : Client20_RuntimeDataServiceImpl_GetStats_ErrorRetreivingPerfMetrics,VMware.VimAutomation.ViCore.Cmdlets.Com

       mands.GetViStats

    Thank you for your answer.



  • 29.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Feb 18, 2020 07:43 AM


  • 30.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Feb 18, 2020 08:18 AM

    Thank you so much for your answer.



  • 31.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Feb 14, 2020 10:59 AM

    Hi, Thank you for your script. But I do not know why in last 30 Min result is wrong.

    When I used -Realtime and -MaxSamples.

    Time               CPU GHz Capacity CPU GHz Used CPU % Free Mem Capacity GB Mem Used GB Mem % Free

    ----                ---------------- ------------ ---------- --------------- ----------- ----------

    14/02/2020 10:10:00              441          211         52            5342        4038         24

    14/02/2020 10:15:00              441          216         51            5342        4037         24

    14/02/2020 10:20:00              441          218         51            5342        4037         24

    14/02/2020 10:25:00              441          209         53            5342        4037         24

    14/02/2020 10:30:00              441          208         53            5342        4037         24

    14/02/2020 10:35:00              441          213         52            5342        4037         24

    14/02/2020 10:40:00              441          206         53            5342        4037         24

    14/02/2020 10:45:00              441          197         55            5342        4037         24

    14/02/2020 10:50:00              441          210         52            5342        4037         24

    14/02/2020 10:55:00              441          203         54            5342        4037         24

    14/02/2020 11:00:00              441          210         52            5342        4037         24

    14/02/2020 11:05:00              441          240         46            5342        4037         24

    14/02/2020 11:10:00              441          226         49            5342        4037         24

    14/02/2020 11:15:00              441          222         50            5342        4037         24

    14/02/2020 11:20:00              441          238         46            5342        4037         24

    14/02/2020 11:25:00              441            0        100            5342        3575         33

    14/02/2020 11:30:00              441            0        100            5342        2881         46

    14/02/2020 11:35:00              441            0        100            5342        2881         46

    14/02/2020 11:40:00              441            0        100            5342        2297         57

    14/02/2020 11:45:00              441            0        100            5342        1283         76

    Thank you  for your answer.



  • 32.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Feb 14, 2020 11:44 AM

    Cluster performance data is created by one of the rollup processes running on the vCenter.

    This rollup job runs every 30 minutes.

    So for cluster data, the Realtime data is not really 'realtime', and you should not take the last 30 minutes into account.



  • 33.  RE: Powercli Script to Capture ESXi Cluster CPU & Memory Usage

    Posted Feb 17, 2020 10:38 AM

    Thank you so much for your answer.