Search code examples
snmppysnmpsnmp-trap

How to implement SET and TRAP in agent side with pysnmp?


I have implemented a SNMP Agent using pysnmp based on this example. This example demonstrates SNMP GET and GETNEXT queries. But I have found no pointer how can I implement SNMP SET and TRAP on top of this code. Examples I have found for SET and TRAP are completely different implementation. How can I implement SNMP SET and TRAP on top of this code?


Solution

  • Assuming you want to combine SNMP CommandResponder (which you have already implemented) with SNMP NotificationReceiver, look at this example. You basically can join both examples within the same Python module around a single I/O loop (e.g. transport dispatcher).

    However, typically, NotificationReceiver resides at the NMS while CommandResponder is SNMP agent running inside the managed software/device.

    Supporting SNMP SET within your existing code would require refactoring the way how your MIB objects are stored. With current example they are kept in a non-writeable storage (tuple) and the MIB objects are not designed to change their stored value (they return a constant). So you need to change that one way or the other.

    Otherwise supporting SNMP SET is simple - just add condition like this:

    ...
    elif reqPDU.isSameTypeWith(pMod.SetRequestPDU()):
        for oid, val in pMod.apiPDU.getVarBinds(reqPDU):
            if oid in mibInstrIdx:
                # put your MIB instrumentation update code here
                # smith like this, but not exactly:
                mibInstrIdx[oid] = mibInstrIdx[oid].clone(val)
                varBinds.append((oid, mibInstrIdx[oid](msgVer)))
            else:
                # No such instance
                varBinds.append((oid, val))
                pendingErrors.append(
                    (pMod.apiPDU.setNoSuchInstanceError, errorIndex)
                )
                break
    

    into your cbFun.