Scripting. Powershell, VMware, Windows, Active Directory & Exchange. All that kind of stuff…..
RSS icon Email icon Home icon
  • Basic VMware Cluster Capacity Check with PowerCLI

    Posted on January 18th, 2012 Jonathan Medd 8 comments

    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
    }
    }
    }
    
     

    5 responses to “Basic VMware Cluster Capacity Check with PowerCLI” RSS icon

    • Nice Script Jonathan! I am looking forward to trying it out in our environment. I would like to point out that I have trouble with the hostname being included on local datastores in parts of our environment. While I created a function to update those (I should post this out sometime) it has not been run everywhere yet. As such, I use the following for identifying local datastores:

      Get-Datastore | Where {$_.Extensiondata.Summary.MultipleHostAccess -eq $False}

      So far I have not run into any issues with this.

    • Nice one, cheers for that. I knew there was a better way to get the local datastores, but it had slipped my mind. Script updated :-)

      To exclude local datastores…..

      Get-Datastore | Where-Object {$_.Extensiondata.Summary.MultipleHostAccess -eq $True}

    • Hi Jonathan,

      Great function – it returns some very useful information. I have added this one to my PS profile. You have also inspired me to go back and write a function of my own that would be useful in my day-to-day tasks at the office.

      I often write scripts that report on useful information, but I don’t often do full functions that can utilise the pipeline or accept parameters. I think this will be good practise for me.

      Cheers,
      Sean

    • NP. I probably got it from Luc at some point :)

    • It would be cool if it had CPU MHz Capacity and usage as well. The other metrics are really good to know.


    3 Trackbacks / Pingbacks

    Leave a reply