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