如何在pysnmp中加载和移动自定义mib?

时间:2013-01-31 17:02:04

标签: snmp pysnmp

我是SNMP / pysnmp的新手,我正在尝试实现一个使用自定义MIB的代理。我正在查看pysnmp网站上的以下示例,但我似乎无法让它工作。以下是我正在采取的步骤:

  1. 使用build-pysnmp-mib将我的MIB文件转换为pysnmp模块(此操作成功且没有错误)
  2. 使用MibBuilder()
  3. 设置MIB源并加载MIB​​模块
  4. 使用上面引用的示例来设置代理(我添加的只是mibBuilder)
  5. 使用net-snmp的snmpwalk查看加载的MIB:
  6. snmpwalk命令是:

    snmpwalk -v3 -u usr-md5-none -l authNoPriv -A authkey1 -E 8000000001020304 -n my-context 127.0.0.1 .1.3.6
    

    snmpwalk返回超时错误,并在分析通信时(通过在pysnmp中启用I / O调试),我可以看到代理接收请求,但从不回复。我认为这可能与上下文有关,我目前可能无法正确理解。关于我可能做错事的任何指示都非常感谢。我正在使用following sample MIB进行测试。代码如下。感谢。

    #!/usr/local/bin/python
    
    from pysnmp.smi import builder
    from pysnmp.smi import view
    
    from pysnmp.entity import engine
    from pysnmp.entity import config
    from pysnmp.entity.rfc3413 import cmdrsp
    from pysnmp.entity.rfc3413 import context
    from pysnmp.smi import instrum
    from pysnmp.carrier.asynsock.dgram import udp
    from pysnmp.proto.api import v2c
    
    from pysnmp import debug
    
    # Create SNMP engine
    snmpEngine = engine.SnmpEngine()
    
    # Transport setup
    
    # UDP over IPv4
    config.addSocketTransport(
        snmpEngine,
        udp.domainName,
        udp.UdpTransport().openServerMode(('127.0.0.1', 161))
    )
    
    # SNMPv3/USM setup
    
    # user: usr-md5-none, auth: MD5, priv NONE
    config.addV3User(
        snmpEngine, 'usr-md5-none',
        config.usmHMACMD5AuthProtocol, 'authkey1'
    )
    
    # Allow full MIB access for each user at VACM
    config.addVacmUser(snmpEngine, 3, 'usr-md5-none', 'authNoPriv',
                       (1,3,6,1,2,1), (1,3,6,1,2,1)) 
    
    # Create an SNMP context with ContextEngineId = 8000000001020304
    snmpContext = context.SnmpContext(
        snmpEngine, contextEngineId=v2c.OctetString(hexValue='8000000001020304')
    )
    
    # load mibs
    MIBDIR = '/usr/local/share//mibdata'
    mibBuilder = builder.MibBuilder()
    mibSources = mibBuilder.getMibSources() + (builder.DirMibSource(MIBDIR),)
    mibBuilder.setMibSources(*mibSources)
    mibBuilder.loadModules('TOASTER-MIB')
    
    # pass mib to Management Instrumentation Controller and register at SNMP Context
    # under ContextName 'my-context'
    snmpContext.registerContextName(
        v2c.OctetString('my-context'),            # Context Name
        instrum.MibInstrumController(mibBuilder)  # Managed Objects - loading my custom TOASTER-MIB
    )
    
    # Register SNMP Applications at the SNMP engine for particular SNMP context
    cmdrsp.GetCommandResponder(snmpEngine, snmpContext)
    cmdrsp.SetCommandResponder(snmpEngine, snmpContext)
    cmdrsp.NextCommandResponder(snmpEngine, snmpContext)
    cmdrsp.BulkCommandResponder(snmpEngine, snmpContext)
    
    # Register an imaginary never-ending job to keep I/O dispatcher running forever
    snmpEngine.transportDispatcher.jobStarted(1)
    
    # Run I/O dispatcher which would receive queries and send responses
    try:
        snmpEngine.transportDispatcher.runDispatcher()
    except:
        snmpEngine.transportDispatcher.closeDispatcher()
        raise
    

1 个答案:

答案 0 :(得分:2)

你真的需要使用非默认的ContextEngineId& CONTEXTNAME?如果没有,我建议改用this example。您可以将自定义MIB加载到默认的SnmpEngine的MIB树(mibBuilder)中,该树可通过以下调用获得:

snmpContext = context.SnmpContext(snmpEngine)
mibBuilder = snmpContext.getMibInstrum().getMibBuilder()

还要确保允许管理员使用config.addVacmUser()功能访问您的OID。

要实现的另一个重要事项是,处于初始状态的已编译pysnmp MIB不包含MIB变量的实例,而只是向Manager提供有关代理内容的提示。因此,要使您的代理生效,您必须将管理对象的实例添加到您的MIB中,解释为in another example