1

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"

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
JiboOne
  • 1,438
  • 4
  • 22
  • 55
  • You cannot violate the SNMP standard itself, so what you asked is impossible. – Lex Li Mar 20 '19 at 17:05
  • @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? – JiboOne Mar 21 '19 at 09:36

2 Answers2

0

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.

Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
  • 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? – JiboOne Mar 21 '19 at 09:35
  • 2
    There is so called Trap OID and trap variables. The trap oid referenced by its own OID SnmpConstants.snmpTrapOID is also a part of PDU. It is a first variable binding. So the Trap Receiver treats it as identifier of TRAP. The rest of variables in PDU can be treated as additional information. What I'm trying to tell you is that if PDU type is TRAP then first two variable bindings are mandatory. The first one is Trap OID and the second one is System Uptime. The rest are user data. – Andrew Komiagin Mar 21 '19 at 13:00
  • Thanks for explanation, Now I understand why changing PDU type to trap solved my problem. – JiboOne Mar 21 '19 at 13:09
0

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"

JiboOne
  • 1,438
  • 4
  • 22
  • 55