Search code examples
javasnmpmibsnmp4j

PDU.TRAP vs PDU.SET snmp4j


PDU pdu = new PDU();
pdu.setType(PDU.SET);

pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.100.1.1"), new Counter64(System.currentTimeMillis())));
pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.100.2.1"), new OctetString("some string")));

TransportMapping transport = null;
Snmp snmp = null;

try {
    transport = new DefaultUdpTransportMapping();
    transport.listen();

    snmp = new Snmp(transport);
    snmp.send(pdu, getCommunityTarget());
} catch (Exception e) {
    // error occurred
}

This is how I send my snmp trap to raise alarm. I'm setting a varible in snmp agent and browse it in MIB browser. I want to know that what if I set pdu type as PDU.TRAP. It works on port 162 rather than 161 and I cannot see it MIB browser but in Trap Receiver. What is the difference? What is the aim of using PDU.TRAP? How can my agent catch it? What is the best practice to raise and clear alarms?


Solution

  • When implementing a SNMP entity, it is often better to implement the so called "trap directed polling" concept instead of a simple "trap sender". For the latter SNMP4J can be used out-of-the-box, for the first SNMP4J-Agent can be used. This is licensed under the Apache 2 Open Source license as well.

    As traps could get lost on the network, the trap-directed-polling approach has many advantages, although it needs more (few) effort to implement the agent part.

    Regarding the original question:

    • When sending a SET pdu you ask the command responder (agent) to change some information on its entity.
    • When sending a TRAP or better NOTIFICATION pdu you want to inform the command generator (manager) about an event. With the trap-directed-polling concept in place, the command generator would also send GET requests regularly (e.g. every 5 minutues) on *LastChanged objects to effeciently detect data changes in certain (crictical) sub-trees of the command responder's MIB. If a change is detected (either through a notification PDU or the polling, the changed data is requested from the comamnd responder by the command generator.