3
votes

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"

2
You cannot violate the SNMP standard itself, so what you asked is impossible.Lex Li
@LexLi UPS also sends SNMP traps but in this case OID is filled in SNMP trap header. What is the difference? How can UPS send OID in header?mariami

2 Answers

2
votes

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"

0
votes

The SNMP TRAP format has fixed structure defined in RFC 1157 or RFC 3412 (in case of SNMPv3). This structure consists of header and PDU (Packet Data Unit). The PDU is basically a set of so called variable bindings. Each binding has OID, Syntax and value. So you can only change the PDU part. The header structure cannot be changed.