Collecting ESX Network Statistics

The requirement was simple. Capture the network packets transmitted ( rx/tx) through the NICs in every 10 seconds for 1 hour.  Its quite simple..right…. Well,  it turned out to be difficult as I could not find any easy way to capture this information.

Why not esxtop:

Untitled

esxtop is a great tool to view esxi stats. It provides the required functionality of showing packets transmitted per second. esxtop allows storing the data in .csv format too. This link provides detailed information on how to use esxtop to dump stats from esxi. So I was planning to use esxtop, but then got stuck using the regular expressions to extract the required data.

Get the data from the source:

I was aware that majority of the data ( or probably the complete data) for esxtop is coming from vsish. So , why dont I take the required data directly from vsish. BTW, there is a good article to know about vsish at this blog. I was able to go to the specific node under vsish , but was not sure how to capture these values in regular interval .  Little bit of searching of various small scripts inside ESXi showed that there is a python module imported in some of the scripts called ‘vmware.vsi’.

>> import vmware.vsi as vsi
>>> print dir(vsi)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__','get', 'list', 'set', 'useCacheFile']

As we can see, there is a “get” and “set” method which can be used to get all the vsish nodes and the values.  For example, to collect the machines CPU configuration, we can use

>>> vsi.get('/hardware/cpu/cpuModelName')
'Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz'

So, coming back to the original requirement, I wanted to collect the number of packets received and sent in every 10 seconds from all the Network card from the machine. Using the vsi module ,  a python script was written which captures the network packets.  The same script can be downloaded from the GIT repository here

import vmware.vsi as vsi
import time
from collections import defaultdict

def calculate(xyz,nics):
        if xyz[0] == 0 and xyz[1] == 0:
                print "Lets wait for packets to come to %s" %(nics)
        else:
                print "The number of packets received for %s in the last 10 seconds are " %(nics.upper())+ str(xyz[2]-xyz[0])
                print "The number of packets sent from %s in the last 10 seconds are " %(nics.upper())+ str(xyz[3]-xyz[1])


vmnics = defaultdict(list)
no_nics=vsi.list('/net/pNics/')
print "There are total of %d network cards in the machine " %(len(no_nics))
for i in no_nics:
        vmnics[i]=[0,0]
while True:
        for nics in no_nics:
                ReceivePackets=vsi.get('/net/pNics/%s/stats' % nics)['rxpkt']
                vmnics[nics].append(ReceivePackets)
                SendPackets=vsi.get('/net/pNics/%s/stats' % nics)['txpkt']
                vmnics[nics].append(SendPackets)
                calculate(vmnics[nics],nics)
                del vmnics[nics][0:2]
        print "-"*20 +  "Sleeping for 10 seconds" + "-"*20
        print 2*'\n'
        time.sleep(10)

Make sure to indent the script appropriately ( wordpress do not give a good code editor for free !!)

Output from the script:

When the script is run on the ESXi host, the output is as below

Untitled1

As we can see from the above script, it captures the network stats from the vsish node through /net/pNics/%s/stats . The values captured stored into a list , print them and then delete values from the list. The script runs in a while loop , so interrupt it with Ctrl+c whenever needed. The script can be copied into ESXi host and run directly through the ssh shell.  The script can be modified as required to capture any other statistics such as Storage traffics.

Advertisements

One thought on “Collecting ESX Network Statistics

  1. THEJAS K V April 28, 2016 / 3:46 pm

    Great post…

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s