vCloud Director VM Utilization Metrics using PowerCLI

vCloud Director VM Utilization Metrics using PowerCLI

vCloud Director VM Utilization Metrics using PowerCLI

vCloud Director VM Utilization Metrics using PowerCLI – In this blog post we would see how we can use powercli to find the utilization metrics for a virtual machine within a orgvdc in vcloud director, we would be covering the CPU and Memory utilization statistics.

The script calculates metric units like memory, and CPU usage for the powered on VMs in the vCloud Director environment.

The script can be used to identify unused and under-utilized VMs in in a vCloud Director Organization.

test

 

vCenter View of the same performance counters.

test

In our test environment we had a deployment of vcloud director 5.5 and vsphere 6.0, i referred to an excellent blogpost written by Alan Renouf Linking your vCloud Director VMs and vSphere VMsRETRIEVING VM METRICS FROM VCLOUD AIR where alan talks about a cool function Convert-ToVM, Get-CIVMMetric which connects to both vcloud and vsphere using powercli and does a conversion of the vm shown in vcloud to a vsphere vm object.

During my test with this script and also in discussions with Alan i found that the support was included from vcloud director 5.6 onwards and the earlier versions were not supported.

PS C:\Windows\system32> (Get-CIVM | select -First 1 | select -ExpandProperty ExtensionData).GetMetricsCurrent()
Exception calling "GetMetricsCurrent" with "0" argument(s): "Resource not found"
At line:1 char:1

(Get-CIVM | select -First 1 | select -ExpandProperty ExtensionData).G ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : CloudException

Now in my environment upgrade was not that easy as there were large number of projects running on vCloud and getting a downtime was not that easy.

Hence i resorted to a little powershell magic to make this work, during my investigation i found that when ever a VM is created in vCloud director the resultant VM in the adjacent vcenter gets a guid appended to its name, using this I was able to co-relate  the names of the VM’s created in vcloud ovdc to the ones in vcenter.

Below diagram shows the entire work flow of the script.

vCloud Director VM Utilization Metrics using PowerCLI

 

Here you can see below the small script block which converts the VM information obtained from Get-CIVM cmdlet to the resultant vsphere VM object with the GUID appended to its name.

$entities = Get-OrgVdc "Your ORGVDC Name" | Get-CIVM -Status PoweredOn | select @{l='VMName';e={$($_.name)+ " " +"("+($($_.id).replace('urn:vcloud:vm:',''))+")"}} | % {Get-VM -Name $_.vmname}

Once we extract the names of these vm’s, its pretty easy to extract the VM utilization metrics for these VM’s using the Get-Stat cmdlet, where i utilized the “cpu.usage.average”, “mem.usage.average” counters to extract utilization.

Also in Vcenter the interval mins for new data collection is 2 hours ==> 120 mins, so i set the same during my test.

Get-Stat -Entity $entities -Stat $metrics -Start (Get-Date).AddDays(-30) -Finish (get-date) -IntervalMins 120 | `
Group-Object -Property {$_.Entity.Name} | %{
$row = “”| Select Vcloud_VM_Name,Vcenter_VM_Name, PowerState, NumCpus, MemoryGB, "CPU_Usage_(Average)_%", "CPU_Usage_(Maximum)_%", "Memory_Usage_(Average)_%", "Memory_Usage_(Maximum)_%"

$row.Vcloud_VM_Name = ($_.Group[0].Entity.Name -split '\s+')[0]
$row.Vcenter_VM_Name = $_.Group[0].Entity.Name
$row.PowerState = $_.Group[0].Entity.PowerState
$row.NumCpus = $_.Group[0].Entity.NumCpu
$row.MemoryGB = $_.Group[0].Entity.MemoryGB

$cpuStatusage = $_.Group | where {$_.MetricId -eq “cpu.usage.average”} | Measure-Object -Property Value -Maximum -Average
$row.'CPU_Usage_(Average)_%' = “{0:f2}” -f ($cpuStatusage.Average)
$row.'CPU_Usage_(Maximum)_%' = “{0:f2}” -f ($cpuStatusage.Maximum)

$memStatusage = $_.Group | where {$_.MetricId -eq “mem.usage.average”} | Measure-Object -Property Value -Maximum -Average
$row.'Memory_Usage_(Average)_%' = “{0:f2}” -f ($memStatusage.Average)
$row.'Memory_Usage_(Maximum)_%' = “{0:f2}” -f ($memStatusage.Maximum)


$report += $row

}

Once the scripts completes execution, voila!! you would be greeted with a detailed CSV file with a monthly performance statistics for your vcloud vm’s based on which you can take necessary actions to clean up the VM’s or decrease the amount of resource assigned per VM.

Here’s the entire script for your reference, you can modify it as per your environment and reuse.

Connect-CIServer -Server $vcloudservername -org $Orgvdcname -Credential $vclcredential | Out-Null
Connect-VIServer -Server $vcenterserver -Credential (Get-Credential)


$report = @()
$metrics = “cpu.usage.average”, “mem.usage.average”

$entities = Get-OrgVdc "Your Org VDC Name" | Get-CIVM -Status PoweredOn | select @{l='VMName';e={$($_.name)+ " " +"("+($($_.id).replace('urn:vcloud:vm:',''))+")"}} | % {Get-VM -Name $_.vmname}

Get-Stat -Entity $entities -Stat $metrics -Start (Get-Date).AddDays(-30) -Finish (get-date) -IntervalMins 120 | `
Group-Object -Property {$_.Entity.Name} | %{
$row = “”| Select Vcloud_VM_Name,Vcenter_VM_Name, PowerState, NumCpus, MemoryGB, "CPU_Usage_(Average)_%", "CPU_Usage_(Maximum)_%", "Memory_Usage_(Average)_%", "Memory_Usage_(Maximum)_%"

$row.Vcloud_VM_Name = ($_.Group[0].Entity.Name -split '\s+')[0]
$row.Vcenter_VM_Name = $_.Group[0].Entity.Name
$row.PowerState = $_.Group[0].Entity.PowerState
$row.NumCpus = $_.Group[0].Entity.NumCpu
$row.MemoryGB = $_.Group[0].Entity.MemoryGB

$cpuStatusage = $_.Group | where {$_.MetricId -eq “cpu.usage.average”} | Measure-Object -Property Value -Maximum -Average
$row.'CPU_Usage_(Average)_%' = “{0:f2}” -f ($cpuStatusage.Average)
$row.'CPU_Usage_(Maximum)_%' = “{0:f2}” -f ($cpuStatusage.Maximum)

$memStatusage = $_.Group | where {$_.MetricId -eq “mem.usage.average”} | Measure-Object -Property Value -Maximum -Average
$row.'Memory_Usage_(Average)_%' = “{0:f2}” -f ($memStatusage.Average)
$row.'Memory_Usage_(Maximum)_%' = “{0:f2}” -f ($memStatusage.Maximum)


$report += $row

}

$report | export-csv “rally.txt” -noTypeInformation

$elapsedTime = new-timespan $script:StartTime $(get-date)
write-host $elapsedTime

I hope this blog post helps out folks to get the performance metrics of VM’s who are using vcd 5.5 in their data-center environment.

Share this post

Post Comment