Capacity reporting when using percentage based HA policy 2

I was recently tasked with generating a human-readable capacity report for an environment that uses full memory reservations for all virtual machines (please, for the love of God do not ask!).  This environment had just changed over to using a percentage based HA policy instead of using designated failover hosts.

Previously, while using the designated failover hosts, the vSphere administrators would simply look at the Resource Allocation tab within the vSphere client to see how much memory capacity was remaining.  They would then generate a report by hand.

The problem that I needed to solve for this environment is due to how things change when using a percentage based HA policy.  The numbers displayed on the Resource Allocation tab no longer reflect the HA reserved resources (I am sure this is by design on VMware’s part).

I whipped up the PowerCLI script found below using a few snippets of code authored by Luc Dekens, and modified it to suit my needs. It gets the job done very quickly and with minimal effort.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<#
Description:
This script calculates the remaining cluster capacity in human-readable numbers, showing
CPU and Memory in GHz and GB.
 
Assumptions:
   - Percentage based HA policy is in use
   - This script works best against "balanced" clusters (all hosts have the same hardware specs)
#>
 
$report = @()
$clusterName = "789ucs01","789ucs02" 
#$clusterName = "*"	# Use asterisk to report on all clusters
 
foreach($cluster in Get-Cluster -Name $clusterName){
    $cluster_hosts = Get-Cluster $cluster | Get-VMHost | measure
	$esx = $cluster | Get-VMHost    
    $ds = Get-Datastore -VMHost $esx | where {$_.Type -eq "VMFS" -and $_.Extensiondata.Summary.MultipleHostAccess}
 
	$cluster_memory_total = ($esx | Measure-Object -Property MemoryTotalMB -Sum).Sum
	$ha_res_memory_percentage = $cluster.ExtensionData.Configuration.DasConfig.AdmissionControlPolicy.MemoryFailoverResourcesPercent
	$memory_capacity_after_ha = $cluster_memory_total - ($cluster_memory_total * ($ha_res_memory_percentage / 100))
	$memory_current_failover_percent = $cluster.Extensiondata.Summary.AdmissionControlInfo.CurrentMemoryFailoverResourcesPercent
	$memory_capacity_remaining = ($cluster_memory_total * ($memory_current_failover_percent / 100)) - ($cluster_memory_total * ($ha_res_memory_percentage / 100))
 
	$cluster_cpu_total = ($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum
	$ha_res_cpu_percentage = $cluster.ExtensionData.Configuration.DasConfig.AdmissionControlPolicy.CpuFailoverResourcesPercent
	$cpu_capacity_after_ha = $cluster_cpu_total - ($cluster_cpu_total * ($ha_res_cpu_percentage / 100))
	$cpu_current_failover_percent = $cluster.Extensiondata.Summary.AdmissionControlInfo.CurrentCPUFailoverResourcesPercent
	$cpu_capacity_remaining = ($cluster_cpu_total * ($cpu_current_failover_percent / 100)) - ($cluster_cpu_total * ($ha_res_cpu_percentage / 100))
 
    $row = "" | Select VCname,DCname,Clustername,"Number of Hosts",
				"HA Reserved Memory Percentage", "HA Current Memory Failover Capacity Percentage", "Total Physical Memory (MB)",
                "Used Memory MB","Available Memory (MB)","Memory Capacity After HA Reservations (MB)",
				"Memory Capacity Remaining (MB)",
				"HA Reserved CPU Percentage", "HA Current CPU Failover Capacity Percentage",
                "Total CPU (Mhz)","Used CPU (Mhz)",
                "Available CPU (Mhz)","CPU Capacity After HA Reservations (Mhz)","CPU Capacity Remaining (Mhz)",
				"Total Disk Space (MB)",
                "Used Disk Space (MB)","Available Disk Space (MB)"
    $row.VCname = $cluster.Uid.Split(':@')[1]
    $row.DCname = (Get-Datacenter -Cluster $cluster).Name
    $row.Clustername = $cluster.Name
	$row."Number of Hosts" = $cluster_hosts.Count
 
	$row."HA Reserved Memory Percentage" = $ha_res_memory_percentage 
	$row."HA Current Memory Failover Capacity Percentage" = $memory_current_failover_percent
    $row."Total Physical Memory (MB)" = $cluster_memory_total
    $row."Used Memory MB" = $memory_usage = ($esx | Measure-Object -Property MemoryUsageMB -Sum).Sum
    $row."Available Memory (MB)" = ($esx | Measure-Object -InputObject {$_.MemoryTotalMB - $_.MemoryUsageMB} -Sum).Sum
    $row."Memory Capacity After HA Reservations (MB)" = $memory_capacity_after_ha
	$row."Memory Capacity Remaining (MB)" = $memory_capacity_remaining
 
	$row."HA Reserved CPU Percentage" = $ha_res_cpu_percentage
	$row."HA Current CPU Failover Capacity Percentage" = $cpu_current_failover_percent
	$row."Total CPU (Mhz)" = $cluster_cpu_total
    $row."Used CPU (Mhz)" = ($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum
    $row."Available CPU (Mhz)" = ($esx | Measure-Object -InputObject {$_.CpuTotalMhz - $_.CpuUsageMhz} -Sum).Sum
	$row."CPU Capacity After HA Reservations (Mhz)" = $cpu_capacity_after_ha
	$row."CPU Capacity Remaining (Mhz)" = $cpu_capacity_remaining
 
    $row."Total Disk Space (MB)" = ($ds | where {$_.Type -eq "VMFS"} | Measure-Object -Property CapacityMB -Sum).Sum
    $row."Used Disk Space (MB)" = ($ds | Measure-Object -InputObject {$_.CapacityMB - $_.FreeSpaceMB} -Sum).Sum
    $row."Available Disk Space (MB)" = ($ds | Measure-Object -Property FreeSpaceMB -Sum).Sum
    $report += $row
} 
$date = Get-Date -UFormat "%Y-%m-%d"
$report | Export-Csv ".\reports\Cluster-Capacity-Report-$date.csv" -NoTypeInformation -UseCulture
$report