Goal

This tutorial creates and registers an InterfaceStateListener, and also shows how to poll for interface statistics.

Tutorial Code

The code used in this tutorial is available in the InterfaceStatisticsTutorial.py file, located under <SDK Location>/python/tutorials/interfaces/InterfaceStatisticsTutorial.py.

Requirements/Prerequisites

  • These steps assume the application can connect properly to a network element. Please see the Connecting to a Network Element tutorial for information on how to make the initial connection.
  • For NX-OS platforms only: To configure the network element to run an Events tutorial, you must first enable the EVMED feature, which allows developers to use Embedded Event Manager (EEM) to monitor Application-, CLI-, and Interface Statistics-based events and then schedule actions on the occurrence of those events:
router(config)# feature evmed
router(config)# sh feature |i evmed
evmed           1               enabled

Steps In Detail

Example InterfaceStatisticsListener

Create the listener to listen for statistic events. In this tutorial, you simply print out the event that your application has received.

class MyInterfaceStatisticsListener(InterfaceStatisticsListener):
    name = str()

    def __init__(self, name):        
        super(MyInterfaceStatisticsListener, self).__init__()
        self.name = name
    
    """
      Invoked when an event is received from a network element.
      @param event
                 An event object that indicates that an event has occurred in a network element.
      @param clientData
                 The clientData is an object that is passed in when the application calls an API to add/register
                 the event listener. The application is responsible for casting the input clientData to the
                 appropriate class before using it.
    """     
    def handle_event(self, event, clientData):            
            print self.name + " has received statistics change event - from Event Handler "
            if InterfaceStatisticsParameter.ONEP_IF_STAT_RX_PKTS_BCAST == event.parameter:
                print "\tchange in ONEP_IF_STAT_RX_PKTS_BCAST value on interface " + event.interface.name

Register Statistic on Interfaces

Next, register the listener that you just created for each network interface on the network element. Note that when you register a listener, you receive an event handler identifier in return, which you can save for later use.

#Add interface stats listener to interface
statsFilter = None
statsFilter = InterfaceStatisticsFilter(
        InterfaceStatisticsParameter.ONEP_IF_STAT_RX_PKTS_BCAST, #parameter to monitor
        OnepOperatorType.ONEP_OP_GE, #Greater than or Equal to
        0, #value to trigger the event
        _InterfaceStatisticsType.ONEP_INTERFACE_STATISTICS_TYPE_VALUE)

listener = MyInterfaceStatisticsListener("Interface Statistics Tutorial")       
networkInterface = tutorial.get_an_interface()
networkelement = tutorial.get_network_element()
#networkInterface = networkelement.get_interface_by_name('Ethernet1/3')
if networkInterface == None:
    logger.error("No interfaces are available.")
    sys.exit(1)

#Polling statistics on network interface
tutorial.pollStatistics(networkInterface)

print "\nAdding statistics change listener on interface " + networkInterface.name
evtHandle = networkInterface.add_statistics_listener(listener, statsFilter, 'Stats Event')

Poll Statistics

Next, you poll the interfaces and ask for statistics for each network interface.

def pollStatistics(self, networkInterface):    
    print "\nPolling for network interface stats..."
    statistics = None
    i = 0
    try:
        while i < self.POLL_LOOP:                 
            statistics = networkInterface.get_statistics()
            print "-------- " + networkInterface.name+ " ----------"
            print "Number of broadcast packets received by the interface.: " + str(statistics.receive_broadcast)
            print "CRC Errors:" + str(statistics.in_error_crc)
            print "Receive Rate (in BPS): " + str(statistics.receive_rate_bps)
            print "Received multicast packets: " + str(statistics.receive_multicast)
            i += 1
            if i < self.POLL_LOOP:
                time.sleep(self.POLL_SLEEP)
    except Exception as e:
        print e
    print "\nDone polling for network interface stats"

Remove Statistic Listener on Interfaces

Finally, remove the listener from all of the network interfaces by using the event handler identifier that you received previously.

#Add interface stats listener to interface
statsFilter = None
statsFilter = InterfaceStatisticsFilter(
        InterfaceStatisticsParameter.ONEP_IF_STAT_RX_PKTS_BCAST, #parameter to monitor
        OnepOperatorType.ONEP_OP_GE, #Greater than or Equal to
        0, #value to trigger the event
        _InterfaceStatisticsType.ONEP_INTERFACE_STATISTICS_TYPE_VALUE)

listener = MyInterfaceStatisticsListener("Interface Statistics Tutorial")       
networkInterface = tutorial.get_an_interface()
networkelement = tutorial.get_network_element()
#networkInterface = networkelement.get_interface_by_name('Ethernet1/3')
if networkInterface == None:
    logger.error("No interfaces are available.")
    sys.exit(1)

#Polling statistics on network interface
tutorial.pollStatistics(networkInterface)

print "\nAdding statistics change listener on interface " + networkInterface.name
evtHandle = networkInterface.add_statistics_listener(listener, statsFilter, 'Stats Event')

Result

Congratulations! You now know how to create a listener, register the listener for all of the network interfaces, poll for statistics, and finally, remove the listener for the network interfaces.