How to Calculate pCPU to vCPU and pMem to vMem Ratio Using PowerCLI – VirtuallyThatGuy

In my time working for an Online betting company, I use to do a lot of capacity planning and cluster over commit analysis for our IT director. This sphered me on to try and automate the process…Thanks to the vmware powercli you can automate the process with below. So once again welcome to VirtuallyThatGuy – this is a quick script to help you determine your cluster compute resource and utilisation using the below script. This one is for the cluster capacity planners. VMTurbo or Turbonomics is a better alternative If you love fancy graphs.

 

# 

$outputFile​​ =​​ "C:\scripts\CPU-Memory-Ratio.csv"

$VC​​ =​​ "uk3p-vc01.lab.local","ntcp-vc01.lab.local"  #Vcenter Details Here

##Connect to the vCenter

Connect-VIServer​​ $VC​​ #-User​​ "rboadi@lab.local" -Password "xxxxxxx"

$Output​​ =@()

Get-Cluster​​ |​​ %{

$hypCluster​​ =​​ $_

## get the GenericMeasureInfo for the desired properties for this cluster's hosts

$infoCPUMEM​​ =​​ Get-View​​ -ViewType​​ HostSystem​​ -Property​​ Hardware.CpuInfo,Hardware.memorysize​​ -SearchRoot​​ $hypCluster.Id​​ |

Select​​ @{n="NumCpuSockets"; e={$_.Hardware.CpuInfo.NumCpuPackages}},​​ @{n="NumCpuCores"; e={$_.Hardware.CpuInfo.NumCpuCores}},​​ @{n="NumCpuThreads"; e={$_.Hardware.CpuInfo.NumCpuThreads}},@{n="PhysicalMem"; E={""+[math]::round($_.Hardware.MemorySize​​ /​​ 1GB,​​ 0)}}​​ |

Measure-Object​​ -Sum​​ NumCpuSockets,NumCpuCores,NumCpuThreads,PhysicalMem

## return an object with info about VMHosts' CPU characteristics

$temp=​​ New-Object​​ psobject

$datacenter​​ =​​ Get-Datacenter​​ -Cluster​​ $hypCluster.Name

$NumVMHosts​​ =​​ if​​ ($infoCPUMEM) {$infoCPUMEM[0].Count}​​ else​​ {0}

$NumCpuSockets​​ =​​ ($infoCPUMEM​​ |​​ ?{$_.Property​​ -eq​​ "NumCpuSockets"}).Sum

$NumCpuCores​​ =​​ ($infoCPUMEM​​ |​​ ?{$_.Property​​ -eq​​ "NumCpuCores"}).Sum

$vmdetails​​ =​​ Get-VM​​ -Location​​ $hypCluster

$NumvCPU​​ =​​ (​​ $vmdetails​​ |​​ Measure-Object​​ NumCpu​​ -Sum).Sum

$VirtualMem=​​ [Math]::Round(($vmdetails​​ |​​ Measure-Object​​ MemoryGB​​ -Sum).Sum,​​ 2)

$PhysicalMem​​ =​​ ($infoCPUMEM​​ |​​ ?{$_.Property​​ -eq​​ "PhysicalMem"}).Sum

##Calculating the vCPU to pCPU ratio​​ AND vRAM to pRAM ratio.

if​​ ($NumvCPU​​ -ne​​ "0") {$cpuRatio=​​ "$("{0:N2}"​​ -f​​ ($NumvCPU/$NumCpuCores))"​​ +​​ ":1"}

if​​ ($VirtualMem​​ -ne​​ "0") {$memRatio=​​ "$("{0:N2}"​​ -f​​ ($VirtualMem/$PhysicalMem))"​​ +​​ ":1"}

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "Datacenter"​​ -Value​​ $datacenter

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "ClusterName"​​ -Value​​ $hypCluster.Name

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "NumVMHosts"​​ -Value​​ $NumVMHosts

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "NumPCPUSockets"​​ -Value​​ $NumCpuSockets

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "NumPCPUCores"​​ -Value​​ $NumCpuCores

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "NumvCPU"​​ -Value​​ $NumvCPU

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "vCPU-pCPUCoreRatio"​​ -Value​​ $cpuRatio

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "PhysicalMem(GB)"​​ -Value​​ $PhysicalMem

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "VirtualMem(GB)"​​ -Value​​ $VirtualMem

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "vRAM-pRAMRatio"​​ -Value​​ $memRatio

$Output+=$temp

}

$Output​​ |​​ Sort-Object​​ Account​​ |​​ Export-Csv​​ -NoTypeInformation​​ $outputFile

 

 

 

 

 

 

 

Output like below

 

Datacenter

ClusterName

NumVMHosts

NumPCPUSockets

NumPCPUCores

NumvCPU

vCPU-pCPUCoreRatio

PhysicalMem(GB)

VirtualMem(GB)

vRAM-pRAMRatio

LON-Datacenter

DTCP-Cluster

3

6

12

8

0.67:1

48

15

0.31:1

LON-Datacenter

UK3P-Cluster

3

6

12

4

0.33:1

48

6

0.13:1

LON-MGMT-Datacenter

LON-MGMT-Cluster

2

4

8

14

1.75:1

46

41

0.89:1

USA-Datacenter

HDCP-Cluster

3

6

12

15

1.25:1

48

28.5

0.59:1

USA-Datacenter

NTCP-Cluster

3

6

12

20

1.67:1

48

56.7

1.18:1

USA-MGMT-Datacenter

USA-MGMT-Cluster

2

4

8

13

1.63:1

46

42

0.91:1

 

# 
$outputFile = "C:\scripts\CPU-Memory-Ratio.csv"
$VC = "uk3p-vc01.lab.local","ntcp-vc01.lab.local"  #Vcenter Details Here
##Connect to the vCenter
Connect-VIServer $VC #-User "rboadi@lab.local" -Password "xxxxxxx"
$Output =@()
Get-Cluster | %{
$hypCluster = $_
## get the GenericMeasureInfo for the desired properties for this cluster's hosts
$infoCPUMEM = Get-View -ViewType HostSystem -Property Hardware.CpuInfo,Hardware.memorysize -SearchRoot $hypCluster.Id |
Select @{n="NumCpuSockets"; e={$_.Hardware.CpuInfo.NumCpuPackages}}, @{n="NumCpuCores"; e={$_.Hardware.CpuInfo.NumCpuCores}}, @{n="NumCpuThreads"; e={$_.Hardware.CpuInfo.NumCpuThreads}},@{n="PhysicalMem"; E={""+[math]::round($_.Hardware.MemorySize / 1GB, 0)}} |
Measure-Object -Sum NumCpuSockets,NumCpuCores,NumCpuThreads,PhysicalMem
## return an object with info about VMHosts' CPU characteristics
$temp= New-Object psobject
$datacenter = Get-Datacenter -Cluster $hypCluster.Name
$NumVMHosts = if ($infoCPUMEM) {$infoCPUMEM[0].Count} else {0}
$NumCpuSockets = ($infoCPUMEM | ?{$_.Property -eq "NumCpuSockets"}).Sum
$NumCpuCores = ($infoCPUMEM | ?{$_.Property -eq "NumCpuCores"}).Sum
$vmdetails = Get-VM -Location $hypCluster
$NumvCPU = ( $vmdetails | Measure-Object NumCpu -Sum).Sum
$VirtualMem= [Math]::Round(($vmdetails | Measure-Object MemoryGB -Sum).Sum, 2)
$PhysicalMem = ($infoCPUMEM | ?{$_.Property -eq "PhysicalMem"}).Sum
##Calculating the vCPU to pCPU ratio AND vRAM to pRAM ratio.
if ($NumvCPU -ne "0") {$cpuRatio= "$("{0:N2}" -f ($NumvCPU/$NumCpuCores))" + ":1"}
if ($VirtualMem -ne "0") {$memRatio= "$("{0:N2}" -f ($VirtualMem/$PhysicalMem))" + ":1"}
$temp | Add-Member -MemberType Noteproperty "Datacenter" -Value $datacenter
$temp | Add-Member -MemberType Noteproperty "ClusterName" -Value $hypCluster.Name
$temp | Add-Member -MemberType Noteproperty "NumVMHosts" -Value $NumVMHosts
$temp | Add-Member -MemberType Noteproperty "NumPCPUSockets" -Value $NumCpuSockets
$temp | Add-Member -MemberType Noteproperty "NumPCPUCores" -Value $NumCpuCores
$temp | Add-Member -MemberType Noteproperty "NumvCPU" -Value $NumvCPU
$temp | Add-Member -MemberType Noteproperty "vCPU-pCPUCoreRatio" -Value $cpuRatio
$temp | Add-Member -MemberType Noteproperty "PhysicalMem(GB)" -Value $PhysicalMem
$temp | Add-Member -MemberType Noteproperty "VirtualMem(GB)" -Value $VirtualMem
$temp | Add-Member -MemberType Noteproperty "vRAM-pRAMRatio" -Value $memRatio
$Output+=$temp
}
$Output | Sort-Object Account | Export-Csv -NoTypeInformation $outputFile

You May Also Like

About the Author: VirtuallyThatGuy

3 Comments

  1. Helpful script, thanks! I changed it a bit for our environment, including limiting it to only powered on VM’s. (We have a large number of decommissioned VM’s that are powered off but can’t be deleted for various reasons, and I didn’t want them included in capacity metrics since they won’t be used again)

    I modified this line:
    $vmdetails​​ =​​ Get-VM​​ -Location​​ $hypCluster
    to
    $vmdetails​​ =​​ Get-VM​​ -Location​​ $hypCluster | where-object “PowerState” -eq “PoweredOn”

    and it worked well.

Leave a Reply

Your email address will not be published. Required fields are marked *