Hello All,
I am new to Powercli and Powershell and have been trying to develop a script to get utilization statistics.
We have three folders (s1, s2, and s3) in vCenter > Hosts and Clusters that house many different clusters.
The goal of these stats is to show cpu and memory resource allocation for each cluster, then add up all of the clusters in each folder to show overall utilization of resources at the folder level.
To do that we have been adding stats in Excel Sheet like the following.
The table below was pulled from our Excel sheet for all of the clusters in a Folder called S1.
The first table is used to generate the second table of totals.
S1 Clusters
ClusterName | CPUTotalCapacity | CPUReservedCapacity | CPUAvailableCapacity | MemoryTotalCapacity | MemoryReserved | MemoryAvailalbleCapacity |
clus01 | 650528 | 142702 | 507826 | 1980315 | 407727 | 1572588 |
clus02 | 650528 | 136692 | 513836 | 1979504 | 477111 | 1502393 |
clus03 | 650528 | 181016 | 469512 | 1979176 | 810630 | 1168546 |
S1 Totals
S1 | Total Capacity | Reserved Capacity | % utilized |
CPU | 1951584 | 445154 | 23% |
Mem | 5938881 | 1591655 | 27% |
For the past couple weeks I've been I've been researching and have developed this script below.
I know there is probably a must better way to do this.
The problems I'm having:
1. I've found blogs detailing how to pull cluster resource allocation stats like cpu and memory above, however I cannot find a way to do it at the cluster level.
If the stats cannot be pulled this way and they need to be added up for each cluster, I am not sure how that can be done yet.
2. With what I've written, the output shows the Table Headings over each cluster. I'm unsure how to output the results similar to the first table above.
3. For some reason MemoryAvailableCapacity always shows up blank.
Here is the script.
$cluster01 = Get-Cluster clus01
$cluster02 = Get-Cluster clus02
$cluster03 = Get-Cluster clus03
$rp01 = Get-View $cluster01.ExtensionData.ResourcePool
$rp02 = Get-View $cluster02.ExtensionData.ResourcePool
$rp03 = Get-View $cluster03.ExtensionData.ResourcePool
$row01 = New-Object PSObject -Property @{
"ClusterName"=$cluster01.Name;
"CPUTotalCapacity" = $rp01.Runtime.Cpu.MaxUsage;
"CPUReservedCapacity" = $rp01.Runtime.Cpu.ReservationUsed;
"CPUAvailableCapacity" = $rp01.Runtime.Cpu.MaxUsage - $rp01.Runtime.Cpu.ReservationUsed;
"MemoryTotalCapacity" = $rp01.Runtime.Memory.MaxUsage/1MB;
"MemoryReserved"= $rp01.Runtime.Memory.ReservationUsed/1MB;
"MemoryAvailableCapacity"=($rp01.Runtime.Memory.MaxUsage - $rp01.Runtime.Memory.ReservationUsed)/1MB;
}
$row02 = New-Object PSObject -Property @{
"ClusterName"=$cluster02.Name;
"CPUTotalCapacity" = $rp02.Runtime.Cpu.MaxUsage;
"CPUReservedCapacity"= $rp02.Runtime.Cpu.ReservationUsed;
"CPUAvailableCapacity"=$rp02.Runtime.Cpu.MaxUsage - $rp02.Runtime.Cpu.ReservationUsed;
"MemoryTotalCapacity" = $rp02.Runtime.Memory.MaxUsage/1MB;
"MemoryReserved"= $rp02.Runtime.Memory.ReservationUsed/1MB;
"MemoryAvailableCapacity"=($rp02.Runtime.Memory.MaxUsage - $rp02.Runtime.Memory.ReservationUsed)/1MB;
}
$row03 = New-Object PSObject -Property @{
"ClusterName"=$cluster03.Name;
"CPUTotalCapacity" = $rp03.Runtime.Cpu.MaxUsage;
"CPUReservedCapacity"= $rp03.Runtime.Cpu.ReservationUsed;
"CPUAvailableCapacity"=$rp03.Runtime.Cpu.MaxUsage - $rp03.Runtime.Cpu.ReservationUsed;
"MemoryTotalCapacity" = $rp03.Runtime.Memory.MaxUsage/1MB;
"MemoryReserved"= $rp03.Runtime.Memory.ReservationUsed/1MB;
"MemoryAvailableCapacity"=($rp01.Runtime.Memory.MaxUsage - $rp03.Runtime.Memory.ReservationUsed)/1MB;
}
$row01 | select-Object ClusterName,CPUTotalCapacity,CPUReservedCapacity,CPUAvailableCapacity,MemoryTotalCapacity,MemoryReserved,MemoryAvailalbleCapacity |
Sort-Object -Property Name | Format-Table -AutoSize
$row02 | select-Object ClusterName,CPUTotalCapacity,CPUReservedCapacity,CPUAvailableCapacity,MemoryTotalCapacity,MemoryReserved,MemoryAvailalbleCapacity |
Sort-Object -Property Name | Format-Table -AutoSize
$row03 | select-Object ClusterName,CPUTotalCapacity,CPUReservedCapacity,CPUAvailableCapacity,MemoryTotalCapacity,MemoryReserved,MemoryAvailalbleCapacity |
Sort-Object -Property Name | Format-Table -AutoSize
Attached is a screenshot of the ouptut of the script from the command line.
Any advice or input would be greatly appreciated.