Over the weekend I received an email from a fellow colleague over in the NSBU who was interested in retrieving statistics on an individual Distributed Virtual Port using the vSphere API and had asked if I had done this before. Although I have written about using the vSphere API to access various properties of a Distributed Virtual Switch (here & here) and migrating from a Virtual Standard Switch (VSS) to Distributed Virtual Switch (VDS) and from a VDS back to VSS (here & here), I have not done a whole lot with the vSphere API with regards to individual Distributed Virtual Ports.
Having said that, with the easy to use PowerCLI Distributed Virtual Switch cmdlets being available, it is actually not too difficult to retrieve this information using the vSphere API. For those of you who have not worked with a Distributed Virtual Switch before, to view the statistics of an individual Distributed Virtual Port, you must drill down into a specific Distributed Virtual Portgroup which you can find using either the vSphere Web/C# Client. Below is a screenshot from my vSphere Web Client.
To retrieve information on a Distributed Virtual Port using the vSphere API, you can refer to the DistributedVirtualPort property. Within this object, there is a state property which you can then drill down further to retrieve statistics using the stat property. If you would like to be able to pull other properties as shown in the screenshot, you can use the runtimeInfo property which provides additional information on the Distributed Virtual Port such as connected entity, MAC Address, VLAN ID, state, etc.
The following two PowerCLI VDS cmdlets: Get-VDPortgroup & Get-VDPort will help us retrieve the information we are looking. However, out of the box these cmdlets do not support pulling statistics for a Distributed Virtual Port. To do so, we will need to access the ExtensionData property which will allows us to access the underlying vSphere API from the PowerCLI object.
Below is a simple PowerCLI script which connects to a vCenter Server and retrieves a specific Distributed Virtual Portgroup (dvPg) that we are interested in using the Get-VDPortgroup cmdlet. Once we have the dvPg, we can then identify a specific Distributed Virtual Port (dvP) by using the Get-VDPort and passing in the dvPg from the previous command as well as the specific port by using the -key property. From here, we can then access a few properties from the dvP and most importantly, the statistics which we were originally interested in. In the example below, I am displaying the entire "stat" property, but you can easily filter out the specific stats you care about.
$server = Connect-VIServer -Server reflex.primp-industries.com # Retrieve a Distributed Portgroup $vdportgroup = Get-VDPortgroup -Name dv-vlan333 # Retrieve a specific Distributed Virtual Port from the Distributed Portgroup using the "key" property $vdport = Get-VDPort -VDPortgroup $vdportgroup -Key 4 # Sample Distributed Virtual Port properties Write-Host "Link Up: " $vdport.ExtensionData.State.RuntimeInfo.Linkup Write-Host "Connectee: " $vdport.ExtensionData.State.RuntimeInfo.LinkPeer Write-Host "Runtime MAC Address: " $vdport.ExtensionData.State.RuntimeInfo.macAddress # Distributed Virtual Port Stats $vdport.ExtensionData.State.Stats Disconnect-VIServer -Server $server -Confirm:$false
Here is an example of the output from the script above and we can see that Distributed Virtual Port "4" matches to what we see in the vSphere Web Client screenshot.
Markus Berthold says
I know this article is not the very latest but I have a question:
We have vCenter 6.5 U2 running and it seems that the statistics were not updated automatically.
In the vCenter UI there is a button to force the update but I cannot find the same via the CLI.
Do you know how to achieve that?
Regards
Markus
Markus Berthold says
With help from the community the following should be inserted in the example between line 4 and 7:
$vdportgroup.VirtualSwitch.ExtensionData.RefreshDVPortState(4)