0
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?

Mustafa Genç
  • 2,569
  • 19
  • 34

2 Answers2

2

The difference is in the roles: SET is executed by a manager against an agent, and TRAP is the reverse. The basic Snmp4j supports only the implementation of an SNMP manager. It is possible to also implement some aspects of an agent, but not trivial. It is hard to tell which of those two you are actually trying to implement, though.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • I'm a bit confused. Anyway, thanks. I may ask some questions later on – Mustafa Genç Nov 07 '13 at 13:22
  • "Manager" and "agent" are SNMP-specified concepts, so you my perhaps read the relevant RFC. It is accessible material and probably worth reading in any case. – Marko Topolnik Nov 07 '13 at 13:24
  • I almost understand the concept and the difference. But I'm confused with our need. First we need to clarify it. – Mustafa Genç Nov 07 '13 at 13:57
  • I'm pretty sure that SNMP4j actually supports both manager and agent features. However, implementing a whole SNMP agent is a lot of work if you start with a low-level API like SNMP4j, so I won't try to argue that it's the best tool for the job. – Jolta Nov 11 '13 at 08:07
  • @Jolta I've done just that (implement agent) and it required rumbling through its source code to find a pidgeonhole through which to squeeeze. That's the only way to make SNMP4j handle SET requests (at least was 2 years ago). NB there's a separate, paid-for SNMP4j-Agent. – Marko Topolnik Nov 11 '13 at 09:06
1

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.
ooSNMP
  • 337
  • 1
  • 8