Basic VMware Cluster Capacity Check with PowerCLI

I recently needed to provide a high level capacity overview per VMware cluster looking at some metrics of interest that were being used as a guide to the capacity state of a cluster. Note: these are by no means definitive or the ones you should be using in your environment, but for these purposes they met the requirements. The metrics I looked at per cluster were the ratio of vCPUs to pCPUs, the amount of Effective, Allocated and average Active Memory and the amount of Free Diskspace.

A couple of things to note:

1.The section below on datastore freespace filters out the local datastore which contains the name of the host.


$VMHost = $Cluster | Get-VMHost | Select-Object -Last 1 $HostName = ($VMHost.name -split ".", 0, "simplematch")\[0\] $ClusterFreeDiskspaceGB = ($VMHost | Get-Datastore | Where-Object {$\_.Name -notmatch $HostName} | Measure-Object -Property FreeSpaceGB -Sum).Sum

2. I’ve recently changed the way I create custom objects to output reports with. For a long time I have used the cheat way of Select-Object , partly because of performance and partly because you can’t control the order of properties in New-Object. These are being addressed in PowerShell v3 (see here and here) so I thought it was about time to make the switch. This means for the time being that when working with the output you need to pipe it to Select-Object to control the order of the output, e.g.

Get-Cluster | Get-ClusterCapacityCheck | Select-Object Cluster,ClusterCPUCores,ClusterAllocatedvCPUs,ClustervCPUpCPURatio,ClusterEffectiveMemoryGB,

ClusterAllocatedMemoryGB,ClusterActiveMemoryPercentage,ClusterFreeDiskspaceGB


function Get-ClusterCapacityCheck { <# .SYNOPSIS Retrieves basic capacity info for VMware clusters

.DESCRIPTION Retrieves basic capacity info for VMware clusters

.PARAMETER  ClusterName Name of the computer to test the services for

.EXAMPLE PS C:\\> Get-ClusterCapacityCheck -ClusterName Cluster01

.EXAMPLE PS C:\\> Get-Cluster | Get-ClusterCapacityCheck

.NOTES Author: Jonathan Medd Date: 18/01/2012 #>

\[CmdletBinding()\] param( \[Parameter(Position=0,Mandatory=$true,HelpMessage="Name of the cluster to test", ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)\] \[System.String\] $ClusterName )

begin { $Finish = (Get-Date -Hour 0 -Minute 0 -Second 0) $Start = $Finish.AddDays(-1).AddSeconds(1)

New-VIProperty -Name FreeSpaceGB -ObjectType Datastore -Value { param($ds) \[Math\]::Round($ds.FreeSpaceMb/1KB,0) } -Force

}

process {

$Cluster = Get-Cluster $ClusterName

$ClusterCPUCores = $Cluster.ExtensionData.Summary.NumCpuCores $ClusterEffectiveMemoryGB = \[math\]::round(($Cluster.ExtensionData.Summary.EffectiveMemory / 1KB),0)

$ClusterVMs = $Cluster | Get-VM

$ClusterAllocatedvCPUs = ($ClusterVMs | Measure-Object -Property NumCPu -Sum).Sum $ClusterAllocatedMemoryGB = \[math\]::round(($ClusterVMs | Measure-Object -Property MemoryMB -Sum).Sum / 1KB)

$ClustervCPUpCPURatio = \[math\]::round($ClusterAllocatedvCPUs / $ClusterCPUCores,2) $ClusterActiveMemoryPercentage = \[math\]::round(($Cluster | Get-Stat -Stat mem.usage.average -Start $Start -Finish $Finish | Measure-Object -Property Value -Average).Average,0)

$VMHost = $Cluster | Get-VMHost | Select-Object -Last 1 $ClusterFreeDiskspaceGB = ($VMHost | Get-Datastore | Where-Object {$\_.Extensiondata.Summary.MultipleHostAccess -eq $True} | Measure-Object -Property FreeSpaceGB -Sum).Sum

New-Object -TypeName PSObject -Property @{ Cluster = $Cluster.Name ClusterCPUCores = $ClusterCPUCores ClusterAllocatedvCPUs = $ClusterAllocatedvCPUs ClustervCPUpCPURatio = $ClustervCPUpCPURatio ClusterEffectiveMemoryGB = $ClusterEffectiveMemoryGB ClusterAllocatedMemoryGB = $ClusterAllocatedMemoryGB ClusterActiveMemoryPercentage = $ClusterActiveMemoryPercentage ClusterFreeDiskspaceGB = $ClusterFreeDiskspaceGB } } } ```