How to display network statistics for virtual machines on ESXi vSwitch. Verify the total number of packets sent or received as well as the amounts of broadcast and multicast frames for a specific VM on a virtual switch.
On many physical switches it is easy to retrieve interface statistics including number of transmitted and received packets, the number of broadcast and multicast frames as well as error reporting.
Corresponding statistics are however not directly available in the GUI for VMware ESXi standard vSwitch, but they are possible to view from the ESXi command line e.g. through SSH.
The classic ESXTOP (above) is a great tool, but does only show real-time values and no cumulated statistics. If we would like to see the total amount of sent/received or other statistics we could use the esxcli tool.
We must first find the virtual switch port ID for the VM we are investigating. This could be seen in ESXTOP if few VMs are running, but could also be retrieved through two other commands:
First find the VM “world id” with the command:
esxcli network vm list
If you have large amounts of VMs displayed a grep filter could be attached with (parts of) the VM name, as displayed above. Note/copy the number first on the line.
Use the command:
esxcli network vm port list -w WORLD-id
This will display the name of the vSwitch, the name of the portgroup, the MAC address, the outgoing physical adapter (vmnic) and also the so called Port ID. The port id is the internal reference to the port for the VM on the virtual switch.
Make a note/copy of the Port ID which will be used in the final esxcli command. (The port ID could also be taken from ESXTOP.)
The command for displaying port statistics is:
esxcli network port stats get -p PORT-id
Note that statistics for standard vSwitch are per host, so if a virtual machine is migrated to another ESXi host the statistics will be renewed. If the virtual machine is rebooted the statistics will be kept, but is lost if doing a VM shutdown.
learn that ! also you can use command “net-stats -l” to get portID.
Great post
Awesome Thanks this is the one I was searching for 🙂
Do you know how to reset the stats without having to shut off/on the VM?
Can anyone tell me how to set internal stats
This seems to be the ONE place where you can grab actual interface statistics instead of “average kbps” samples. The API’s all seem to be missing it. Thanks for this post. I was able to reproduce your work, and then rewrote it in PowerCLI using syntax like:
$esxcli.network.port.stats.get.invoke($args)
I would have preferred a web API call, but this will have to do.