PowerCLI

 View Only
  • 1.  Stats over time - again

    Posted Feb 06, 2020 09:00 AM

    Hi all,

    I am trying to write (well, modify) a script that gives the maximum and average CPU and memory stats per cluster over the last week.

    I serached quite a bit, and all paths seem to lead to the same thread from 2016 (VMware PowerCLI Forum - VMware {code}) where LucD  seems to have solved the problem with a script that looks like this (I cut the minimum paramters since I don't need them):

    $allClusters = @()

    Get-Cluster | %{

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

        $clusstat.ClusterName = $_.Name

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

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

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

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

        $clusstat.CPUMax = $cpu.Maximum

        $clusstat.CPUAvg = $cpu.Average

        $clusstat.MemMax = $mem.Maximum

        $clusstat.MemAvg = $mem.Average

       

        $allClusters += $hoststat

    }

    $allClusters |

    Select ClusterName, MemMax, MemAvg, CPUMax, CPUAvg

    Export-Csv $outputfile -noTypeInformation

    This script, however, gives nothing in return. When I replace $outputfile with a filew path, the file is empty.

    Tried out-gridview, but nothing there either.

    This is probably a minor problem, but I cannot for the life of me figure out why I get an empty return here....?



  • 2.  RE: Stats over time - again

    Posted Feb 06, 2020 09:12 AM

    The first thing that comes to mind, is your vCenter keeping the performance data that long.

    Have a look at my PowerCLI & vSphere statistics - Part 1 - The basics  post to understand statistical levels, performance data collection and aggregation.



  • 3.  RE: Stats over time - again

    Posted Feb 06, 2020 09:17 AM

    That could be, of course, but changing AddDays(-30) to (-7) yields the same result. I am sure the vCenter keeps data for at least that long.

    And I can see these stats in the vCetner advanced performance GUI, just trying to automate the reporting.



  • 4.  RE: Stats over time - again

    Posted Feb 06, 2020 09:39 AM

    I found this little gem from  2008, that works better. It looks similar, but uses a foreach loop:

    $allclust = @()

    $clust = Get-Cluster

    foreach($clu in $clust){

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

      $clusstat.ClusterName = $clu.name

     

      $statcpu = Get-Stat -Entity ($clu) `

               -start (get-date).AddDays(-7) `

               -Finish (Get-Date) `

                -MaxSamples 10000 `

                 -stat cpu.usagemhz.average

      $statmem = Get-Stat -Entity ($clu) `

               -start (get-date).AddDays(-7) `

               -Finish (Get-Date) `

                -MaxSamples 10000 `

                 -stat mem.usage.average

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

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

     

      $clusstat.CPUMax = $cpu.Maximum

      $clusstat.CPUAvg = $cpu.Average

      $clusstat.MemMax = $mem.Maximum

      $clusstat.MemAvg = $mem.Average

     

      $allclust += $clusstat

    }

    $allclust | `

      select ClusterName, MemMax, MemAvg, CPUMax, CPUAvg

    Gives the following output:

    ClusterName : Prod_B

    MemMax      : 45,3600006103516

    MemAvg      : 45,2667361276592

    CPUMax      : 131887

    CPUAvg      : 69396,2994011976

    ClusterName : Prod_A

    MemMax      : 47,0299987792969

    MemAvg      : 46,8672157904345

    CPUMax      : 108343

    CPUAvg      : 61335,8293413174

    ClusterName : Management

    MemMax      : 41,0499992370605

    MemAvg      : 40,9491614952773

    CPUMax      : 103128

    CPUAvg      : 77024,6107784431

    Is there a quick way to get CPU stats in per cent?



  • 5.  RE: Stats over time - again
    Best Answer

    Posted Feb 06, 2020 09:45 AM

    Use the metric cpu.usage.average instead of cpu.usagemhz.average



  • 6.  RE: Stats over time - again

    Posted Feb 06, 2020 09:53 AM

    Thank you, you rock.

    But then again, you already knew that :smileyhappy: