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