Pysnmp SNMP Trap with own MIB

95 Views Asked by At

I'm currently trying to send a Trap with my own OID. In commend Line the command looks like this: ``` $ snmptrap -v 2c -c '' 1.9.9.9.0 1.9.9.9.1 i 1 1.9.9.9.8 s "text" ```

I tried to write the same command in Python with PySNMP. As reference I used the docu: https://pysnmp.readthedocs.io/en/latest/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/common-notifications.html My code currently looks like this:

g = sendNotification(
            SnmpEngine(),
            CommunityData(community_string, mpModel=1),
            UdpTransportTarget((host, 162)),
            ContextData(),
            'trap',
            NotificationType(
                 ObjectIdentity('1.9.9.9.0'),
                 objects={
                     '1.9.9.9.1': 1,
                     '1.9.9.9.8': 'text'
                 }

My Script will send the Message but without the objects. My Wireshark output: Wireshark output with a Trap just on the OID 1.9.9.9.0

What else can I do to send my Trap-Message? Where did I went wrong?

Alternativley: If I try to write the "extra" OID's to the Function .addVarBinds() like this:

# Assemble MIB viewer
mibBuilder = builder.MibBuilder()
# raw mibs
compiler.addMibCompiler(mibBuilder, sources=['http://mibs.snmplabs.com/asn1/@mib@',
                                                 '/.'])
mibBuilder.loadModules('SNMPv2-MIB', 'SNMP-COMMUNITY-MIB', 'MY-MIB')
mibView = view.MibViewController(mibBuilder)

# send Trap
g = sendNotification(
            SnmpEngine(),
            CommunityData(community_string, mpModel=1),
            UdpTransportTarget((host, 162)),
            ContextData(),
            'trap',
            NotificationType(
                 ObjectIdentity('1.9.9.9.0').addVarBinds(
                     ObjectType(ObjectIdentity('1.9.9.9.1'), Integer(1))
                     ObjectType(ObjectIdentity('1.9.9.9.8'), OctetString("text"))
                 ).resolveWithMib(mibView)

I get an Error & the Trap is not send:

pysnmp: StatusInformation: {'errorIndication': NotInView('Requested OID is out of MIB view')}

But resolving a Single ObjectType works just fine:

obj = ObjectType(ObjectIdentity('1.9.9.9.1'), Integer(1))
print(obj.resolveWithMib(mibView))
1

There are 1 best solutions below

0
Lex Li On

Since you only wanted to send the message out, .resolveWithMib(mibView) is not needed.