Search code examples
pythonpysnmp

how to get NotificationType() objects from compiled pysnmp mib files?


I have many custom mib files compiled into pysnmp required format and stored under a directory. I am trying to load them and pickup only those who are of type NotificationType(). By default it will include everything

How can I accomplish this? This is what I am talking about (For example snmpAgentNotResponding event):

from pysnmp.smi import builder, view
mibBuilder = builder.MibBuilder().loadModules('entuity-mib')
mibView = view.MibViewController(mibBuilder)
>>> a = mibView.mibBuilder.mibSymbols
>>> a.keys()
['ENTUITY-MIB', 'ASN1-REFINEMENT', 'RFC1155-SMI', 'ASN1', 'RFC-1212', 'SNMPv2-SMI', 'RFC-1215', 'ASN1-ENUMERATION', 'RFC1213-MIB']
>>> a['ENTUITY-MIB']['snmpAgentNotResponding']
NotificationType((1, 3, 6, 1, 4, 1, 2626, 1, 1, 1, 0, 655363))

I know that this is not a good idea (compare based on the class name) but this what I have so far to achieve this.

>>> b = a['ENTUITY-MIB']['snmpAgentNotResponding']
>>> b.__class__.__name__
'NotificationType'

This is my code to walk over the mib file (from pysnmp forums).

oid, label, suffix = mibView.getFirstNodeName()
while True:
     try:
        modName, nodeDesc, suffix = mibView.getNodeLocation(oid)
        print '%s::%s == %s' % (modName, nodeDesc, oid)
        oid, label, suffix = mibView.getNextNodeName(oid)
     except Exception, e:
        print e
        break

How to pick only the NotificationTypes in a better way? Different mib files uses different RFC imports for NotificationTypes. This is where I got confused.


Solution

  • I can think of two ways:

    1. Duck-typing: check for the .getObjects method. It should be only defined in NOTIFICATION-TYPE objects
    2. Stick to comparison with NotificationType class. However that may break if you somehow manage to have multiple instances of NotificationType classes loaded.

      nt, = mibBuilder.importSymbols('SNMPv2-SMI', 'NotificationType')
      ...
      if isinstance(nt, a['ENTUITY-MIB']['snmpAgentNotResponding']):
          print('is notification type')
      

    If you can share better ideas on MIB constructs identification techniques - I'm open to suggestions.

    What is your ultimate goal? Could that be done with the ObjectType/NotificationType classes?