javaheadersnmpsnmp4jsnmp-trap

Sending OID in SNMP Trap header


I have a java application which sends SNMP traps using SNMP4J. The problem is that OID is sent in trap body. All data I'm setting is successfully sent, but in trap body. I want Oid to be sent in trap header.

How can I send Oid in Trap header?

    UdpAddress managerUdpAddress = new UdpAddress("address");

    CommunityTarget ctarget = new CommunityTarget();
    ctarget.setAddress(managerUdpAddress);
    ctarget.setRetries(retryCount);
    ctarget.setCommunity(new OctetString(community));
    ctarget.setTimeout(timeout);
    ctarget.setVersion(SnmpConstants.version2c);

    PDU trap = new PDU();

    OID oid = new OID(myOid);
    trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
    trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));                
    trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString(
        "System Description")));
    trap.add(new VariableBinding(oid, new OctetString(message)));

    DefaultUdpTransportMapping  transport = new DefaultUdpTransportMapping();

    Snmp snmp = new Snmp(transport);

    snmp.notify(trap, ctarget);

When UPS is sending SNMP trap, OID is presented in SNMP trap header. Here are examples:

Trap from UPS:

Mon Mar 18 04:13:18 2019 .1.3.6.1.4.1.935.0.49 Normal "SNMP EVENT" x.x.x.x - UPS_212_bypass_ac_normal SNMP TRAP: Bypass AC Normal

Trap from JAVA:

Mon Mar 18 05:25:36 2019 .0.00 Critical "SNMP EVENT" x.x.x.x - my application snmp errors: System Description General error. Size=2"


Solution

  • I did it by adding this code:

    trap.setType(PDU.TRAP);
    trap.add(new VariableBinding(oid));
    

    Now SNMP trap sent from Java looks like this:

    Thu Mar 21 15:16:51 2019 .1.3.6.1.6.3.1.1.7.1.6 Critical "SNMP EVENT" x.x.x.x - my application snmp errors: System Description General error. Size=2"