1 2 3 Previous Next 66 Replies Latest reply on Sep 21, 2018 9:08 AM by nicolasporta5

    Powercli Script to Capture Cluster utilization

    nicolasporta5 Novice

      Hello All,

       

      I'm having having difficulties to create a script that will give me the following:

      Cluster CPU, Memory and vSAN Usage. Number of hosts, number of VMs

      Needs to export as html and collect the information from multiple vcenter.

      Would be something like

      Total CPU, CPU Usage, CPU Free, Total RAM, RAM Usage, RAM Free, Total Capacity, Used Capacity, Free Capacity, N° Hosts, N°VMs

       

      I have this, but i need it at the cluster level, i think is easier, but you know more than me :)

       

       

       

       
      $Output =@();
      $hosts = @();
       
      Connect-VIServer $hosts -User "ey\a2139242-3" -Password "F3#~5ct7~kdYxNm"
       
      $a = "<style>"
      $a = $a + "BODY{background-color:white;}"
      $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
      $a = $a + "TH{border-width: 1px;padding: 1px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
      $a = $a + "TD{border-width: 1px;padding: 1px;border-style: solid;border-color: black;background-color:white}"
      $a = $a + "</style>"
       
      Write-Host ("Getting Cluster Information from $hosts")
       
      $vcenters| % {
      $cluster =Get-cluster -Name *VSAN*
      get-cluster $cluster | get-vmhost | % {
      $vmhost =$_
      $VMHostView = $VMHost | Get-View
      $VMs = $VMHost | Get-VM #| ? { $_.PowerState -eq "PoweredOn" }
      $TotalRAMGB = [math]::round($vmhost.MemoryTotalGB)
      $TotalRAMUsageGB = [math]::round($vmhost.MemoryUsageGB)
      $TotalRAMfreeGB = [math]::round($TotalRAMGB-$TotalRAMUsageGB)
      $PercRAMUsed = [math]::round(($TotalRAMUsageGB/$TotalRAMGB)*100)
      $TotalRAMReservedFree = [math]::round(($TotalRAMGB/100)*15)
      $TotalRAMAvailable = [math]::round(($TotalRAMfreegb-$totalramreservedfree))
       
      $temp = New-Object PSObject
      $temp | Add-Member -pass NoteProperty "VMhost" $vmhost.Name
      $temp | Add-Member -pass NoteProperty "ClusterName" $cluster.name
      $temp |Add-Member -pass NoteProperty Sockets $VMHostView.Hardware.cpuinfo.NumCPUPackages
      $temp |Add-Member -pass NoteProperty Cores $VMHostView.Hardware.cpuinfo.NumCPUCores
      $temp |Add-Member -pass NoteProperty Threads $VMHostView.Hardware.cpuinfo.NumCPUThreads
      $temp |Add-Member -pass NoteProperty VMCount (($VMs | measure-object).Count)
      $temp |Add-Member -pass NoteProperty vCPU (0 + ($VMs | measure-object -Sum NumCPU).Sum)
      $temp |Add-Member -pass NoteProperty vCPUperCore ((0 + ($VMs | measure-object -Sum NumCPU).Sum)/$VMHostView.Hardware.cpuinfo.NumCPUCores)
      $temp |Add-Member -pass NoteProperty "RAMGB" $TotalRAMGB
      $temp |Add-Member -pass NoteProperty "RAMUsageGB" $totalramusageGB
      $temp |Add-Member -pass NoteProperty "RAMFreeGB" $totalramfreeGB
      $temp |Add-Member -pass NoteProperty "RAMUsage%" $PercRAMused
      $temp |Add-Member -pass NoteProperty "RAMReservedGB(15%)" $totalramreservedfree
      $temp |Add-Member -pass NoteProperty "RAM Available for NEW VMs in GB" $totalramavailable
       
      $Output+=$temp
      }
      }
       
      $Output | ConvertTo-Html -Head $a | Out-File C:\Users\nicolas.porta\Desktop\xrails_hosts.htm -width 400
      Disconnect-VIServer * -confirm:$false

       
        • 1. Re: Powercli Script to Capture Cluster utilization
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          Is that 15% reserved RAM a hard-coded value for your environment?

           

          The VSAN Usage is that the datastores on VSAN usage?
          And how can the script detect these VSAN datastores? Do they have specific names?
          Or does the script need to discover which datastores are sitting on VSAN?

          • 2. Re: Powercli Script to Capture Cluster utilization
            nicolasporta5 Novice

            Hi LucD,

            I have a vsan script but i need to put the name manually, and for multiple vcenters is a hard task. All the vsan datastores in all the vcenters got the word VSAN in the name.

            • 3. Re: Powercli Script to Capture Cluster utilization
              nicolasporta5 Novice

              I also manage to get the datastore capacity this way. If i can add the other stuff here that will work for me (Total CPU, CPU Usage, CPU Free, Total RAM, RAM Usage, RAM Free N° Hosts, N°VMs), but i don't know how to add them to the script below and to do it for multiple vcenters at the same time

              Get-Datacenter -PipelineVariable dc |
              Get-Cluster -PipelineVariable cl -ErrorAction SilentlyContinue |
              Get-Datastore -PipelineVariable ds|
              Get-VMHost -PipelineVariable VMHost |
              Get-VM -PipelineVariable vm |
              Select-Object
              @{N='Datacenter';E={$dc.Name}},
              @{N='Cluster';E={$cl.Name}},
              @{N="Host Memory Size GB";E={[math]::Round($cl.ExtensionData.Hardware.MemorySize/1073741824)}},
              @{N="Host Num CPU Cores";E={[math]::Round($cl.ExtensionData.Hardware.CpuInfo.NumCpuCores)}},
              @{N="N° VMs" ;E={($vm | measure-object).Count}},
              @{N="VM Datastores";E={
              $script:vmds = Get-View -Id $vm.DatastoreIdList -Property Name,Summary.Capacity,Summary.FreeSpace
              $script:vmds.Name -join '|'}},
              @{N='VM Datastores CapacityGB';E={
              [math]::Round(($script:vmds.Summary.Capacity | Measure-Object -Sum | Select -ExpandProperty Sum)/1GB,0)}},
              @{N=" VM Datastores UsedSpaceGB";E={
              [Math]::Round((($script:vmds.Summary.Capacity | Measure-Object -Sum | Select -ExpandProperty Sum)-($script:vmds.Summary.FreeSpace | Measure-Object -Sum | Select -ExpandProperty Sum))/1GB,0)}},
              @{N='VM Datastores FreeGB';E={
              [math]::Round(($script:vmds.Summary.FreeSpace | Measure-Object -Sum | Select -ExpandProperty Sum)/1GB,0)}} |
              • 4. Re: Powercli Script to Capture Cluster utilization
                LucD Guru
                User ModeratorsvExpertCommunity Warriors

                Ok, got it, I can use the datastorenames in that case.

                Do you need to have the total VSAN storage per cluster, or per datastore?

                • 5. Re: Powercli Script to Capture Cluster utilization
                  nicolasporta5 Novice
                  I think that per datastore will be more precise as i'll know that is only collecting the vsan datastore and nothing else. 
                  • 6. Re: Powercli Script to Capture Cluster utilization
                    LucD Guru
                    Community WarriorsUser ModeratorsvExpert

                    Does that mean you have only 1 datastore on VSAN per cluster?

                    • 7. Re: Powercli Script to Capture Cluster utilization
                      nicolasporta5 Novice

                      Each vcenter got only 1 vSAN datstore. But also each vcenter got a service datastore for each host. I only need to collect the capacity for vsan

                      • 8. Re: Powercli Script to Capture Cluster utilization
                        LucD Guru
                        User ModeratorsCommunity WarriorsvExpert

                        Before we start presenting this in HTML format, can you check that the following brings what you want to see?
                        Note that you need to run PowerCLI in 'multiple' mode (check with Get-PowerCLIConfiguration) and connect to all the vCenters before you run the script.

                         

                        foreach($vc in $global:DefaultVIServers){

                            Get-Cluster -Server $vc | ForEach-Object -Process {

                                $ds = Get-Datastore -Name *VSAN* -RelatedObject $_

                                $obj = [ordered]@{

                                    vCenter = $vc.Name

                                    Cluster = $_.Name

                                    'Total CPU Ghz' = [math]::Round($_.ExtensionData.Summary.TotalCpu/1000,0)

                                    'CPU Usage' = "{0:P0}" -f ($_.ExtensionData.Summary.EffectiveCpu/$_.ExtensionData.Summary.TotalCpu)

                                    'CPU Free' = "{0:P0}" -f (($_.ExtensionData.Summary.TotalCpu - $_.ExtensionData.Summary.EffectiveCpu)/$_.ExtensionData.Summary.TotalCpu)

                                    'Total RAM GB' = [math]::Round($_.ExtensionData.Summary.TotalMemory/1GB)

                                    'RAM Usage' = [math]::Round($_.ExtensionData.Summary.TotalMemory/1GB - $_.ExtensionData.Summary.EffectiveMemory/1KB,0)

                                    'RAM Free GB' = [math]::Round($_.ExtensionData.Summary.EffectiveMemory/1KB,0)

                                    'Total Capacity GB' = [math]::Round($ds.CapacityGB,0)

                                    'Used Capacity GB' = [math]::Round($ds.CapacityGB - $ds.FreeSpaceGB,0)

                                    'Free Capacity GB' = [math]::Round($ds.FreeSpaceGB,0)

                                    'N° Hosts' = $_.ExtensionData.Host.Count

                                    'N°VMs' = Get-View -Id $cluster.ExtensionData.Host | %{$_.VM.Count} | Measure-Object -Sum | select -ExpandProperty Sum

                                }

                                New-Object PSObject -Property $obj

                            }

                        }

                        • 9. Re: Powercli Script to Capture Cluster utilization
                          nicolasporta5 Novice

                          Hi LucD,

                          Thanks for the reply!.

                           

                          I tried running the code but i got this error:

                           

                          Attempted to divide by zero.

                          At C:\Users\Nicolas.Porta\Desktop\vcenter stats - Copy.ps1:24 char:9

                          +         $obj = [ordered]@{

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

                              + CategoryInfo          : NotSpecified: (:) [], RuntimeException

                              + FullyQualifiedErrorId : RuntimeException

                          • 10. Re: Powercli Script to Capture Cluster utilization
                            LucD Guru
                            User ModeratorsvExpertCommunity Warriors

                            Can you try with this version?

                            It fixes 1 bug and adds extra data to the screen.

                             

                            foreach($vc in $global:DefaultVIServers){

                                Get-Cluster -Server $vc | ForEach-Object -Process {

                                    $_.ExtensionData.Summary

                                    $ds = Get-Datastore -Name *VSAN* -RelatedObject $_

                                    $obj = [ordered]@{

                                        vCenter = $vc.Name

                                        Cluster = $_.Name

                                        'Total CPU Ghz' = [math]::Round($_.ExtensionData.Summary.TotalCpu/1000,0)

                                        'CPU Usage' = "{0:P0}" -f ($_.ExtensionData.Summary.EffectiveCpu/$_.ExtensionData.Summary.TotalCpu)

                                        'CPU Free' = "{0:P0}" -f (($_.ExtensionData.Summary.TotalCpu - $_.ExtensionData.Summary.EffectiveCpu)/$_.ExtensionData.Summary.TotalCpu)

                                        'Total RAM GB' = [math]::Round($_.ExtensionData.Summary.TotalMemory/1GB)

                                        'RAM Usage' = [math]::Round($_.ExtensionData.Summary.TotalMemory/1GB - $_.ExtensionData.Summary.EffectiveMemory/1KB,0)

                                        'RAM Free GB' = [math]::Round($_.ExtensionData.Summary.EffectiveMemory/1KB,0)

                                        'Total Capacity GB' = [math]::Round($ds.CapacityGB,0)

                                        'Used Capacity GB' = [math]::Round($ds.CapacityGB - $ds.FreeSpaceGB,0)

                                        'Free Capacity GB' = [math]::Round($ds.FreeSpaceGB,0)

                                        'N° Hosts' = $_.ExtensionData.Host.Count

                                        'N°VMs' = Get-View -Id $_.ExtensionData.Host | %{$_.VM.Count} | Measure-Object -Sum | select -ExpandProperty Sum

                                    }

                                    New-Object PSObject -Property $obj

                                }

                            }

                             

                            • 11. Re: Powercli Script to Capture Cluster utilization
                              nicolasporta5 Novice

                              I still get the error but i got the info:

                               

                              Attempted to divide by zero.

                              At C:\Users\Nicolas.Porta\Desktop\vcenter stats - Copy.ps1:29 char:9

                              +         $obj = [ordered]@{

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

                                  + CategoryInfo          : NotSpecified: (:) [], RuntimeException

                                  + FullyQualifiedErrorId : RuntimeException

                               

                               

                               

                               

                              vCenter           : ussecvaphgvc01.ey.net

                              Cluster           : USSEC-PROD-VXRL01-VSAN01

                              Total CPU Ghz     : 1162

                              CPU Usage         : 87 %

                              CPU Free          : 13 %

                              Total RAM GB      : 12267

                              RAM Usage         : 918

                              RAM Free GB       : 11349

                              Total Capacity GB : 228927

                              Used Capacity GB  : 77087

                              Free Capacity GB  : 151840

                              N° Hosts         : 16

                              N°VMs            : 554

                               

                              I run it over only 1 cluster

                              • 12. Re: Powercli Script to Capture Cluster utilization
                                LucD Guru
                                Community WarriorsUser ModeratorsvExpert

                                These values look ok.
                                Do you perhaps have an empty cluster in that vCenter?

                                Can you check with

                                 

                                 

                                foreach($vc in $global:DefaultVIServers){

                                    Get-Cluster -Server $vc |

                                    Select @{N='VC';E={$vc.Name}},

                                        @{N='Cluster';E={$_.Name}}

                                }

                                 

                                • 13. Re: Powercli Script to Capture Cluster utilization
                                  nicolasporta5 Novice

                                  Yes i got an empty cluster, that was the issue. I tried with anotherone and no issues.

                                  • 14. Re: Powercli Script to Capture Cluster utilization
                                    LucD Guru
                                    Community WarriorsUser ModeratorsvExpert

                                    Ok, now that we have the basic part of the script (the part that produces the values), can you handle putting this a HTML format?

                                    Or do you need some assistance?

                                    How should the HTML report look?

                                    Similar to the one the first script in this thread produced?

                                    1 2 3 Previous Next