vSphere Tags & vROps Metrics

This week i had been tasked with collecting lists of VMs that are tagged within certain vSphere tag categories and then compiling lists of their metrics so that service group owners can identify resource demand during testing. This information would also be useful if rightsizing your environment or planning new infrastructure. I used the following scripts:

#export tagged VMs to list
Get-TagAssignment | Where{ ($_.Tag.Category.Name -eq "category*") } | Select Entity | Export-Csv -Path "c:\csv\categorylist.csv" -noTypeInformation

Initially i came up with the below script for exporting metrics. While it does work, the formatting isnt great making it hard to work with the data.

#export metrics
Connect-VIServer vCenter1,vCenter2 -User user -Password password
Connect-OMServer vrops -User user -Password password
$From = '14/12/18 07:00:00'
$To = '14/12/18 20:00:00'
$OMvmlist = Get-VM | where {(Get-TagAssignment -Entity $_ | Select -ExpandProperty Tag) -like 'category*'}
$metrics = "cpu|demandmhz"

Get-OMResource $OMvmList | Get-OMStat -Key "cpu|demandmhz" -From $From -To $To | Select Time,Resource,Key,Value | Export-Csv -Path "c:\csv\cpudemand.csv" -noTypeInformation

With some additional help from the VMTN community (LucD is nothing short of a legend). We were able to create the following script which exports the metrics in an easy to view format:

$From = '14/12/18 07:00:00'
$To = '14/12/18 09:00:00'
$OMvmlist = Get-VM | where {(Get-TagAssignment -Entity $_ | Select -ExpandProperty Tag) -like 'category*'} | Select -ExpandProperty Name
$metrics = "cpu|demandmhz", "mem|workload"

Get-OMResource $OMvmList |
Get-OMStat -Key $metrics -From $From -To $To |
Sort-Object -Property Resource, Time |
Group-Object -Property Resource, Time |
ForEach-Object -Process {
$obj = [ordered]@{
Time = $_.Group[0].Time
Resource = $_.Group[0].Resource
CPUvalue = $_.Group | where {$_.Key -match 'cpu'} | Select -ExpandProperty Value
MemoryValue = $_.Group | where {$_.Key -match 'mem'} | Select -ExpandProperty Value
}
New-Object PSObject -Property $obj
} |
Export-Csv -Path "c:\csv\cpumemdemand.csv" -noTypeInformation
Set-PowerCLIConfiguration -DisplayDeprecationWarnings:$false -Confirm:$false

 909 total views,  7 views today

Leave a Reply

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