etingof / pysnmp

Python SNMP library
http://snmplabs.com/pysnmp/
BSD 2-Clause "Simplified" License
583 stars 200 forks source link

loadmibs() makes query really slow #399

Open teedu opened 3 years ago

teedu commented 3 years ago
python3 -c "import pysnmp; print(pysnmp.__version__)"
4.4.12

script itself:

from pysnmp.hlapi import *

iterator = getCmd(
  SnmpEngine(),
  CommunityData('some_comm', mpModel=0),
  UdpTransportTarget(('host', 161)),
  ContextData(),
  ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')), 
  ObjectType(ObjectIdentity('.1.3.6.1.2.1.1.2.0').loadMibs('JUNIPER-CHASSIS-DEFINES-MIB','JUNIPER-SMI','CISCO-PRODUCTS-MIB'))
)
errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:
    print(errorIndication)

elif errorStatus:
    print('%s at %s' % (errorStatus.prettyPrint(),
                        errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))

else:
    for varBind in varBinds:
        print(' = '.join([x.prettyPrint() for x in varBind]))

And the outcome is ca 10 seconds. Without loadMibs everything works fine... Any suggetions?

lextm commented 1 year ago

In short, your specific test case does not require any of the Juniper or Cisco MIB documents to be loaded, since those OIDs are just the standard ones.

If you do need to load more MIB documents, loadMibs unfortunately has to run a complex algorithm to search/compile the modules, and that takes an undetermined period of time. 10-second shouldn't be surprising based on your machine/network setup.