How can i send snmpv2 traps from Java application. I tried to do example on snmp4j, but it didn't work. Thanks a lot!
do you have a simple example, how to do it right?thanks!
EK
2010-07-28 17:16:49
import org.snmp4j.*;import org.snmp4j.event.*;...CommunityTarget target = new CommunityTarget();target.setCommunity(new OctetString("public"));target.setAddress(targetAddress);target.setVersion(SnmpConstants.version2c);PDU request = new PDU();request.setType(PDU.V2TRAP);request.setGenericTrap(PDUv2.COLDSTART);Snmp snmp = new Snmp(new DefaultUdpTransportMapping());snmp.listen();snmp.sendPDU(request, target, null, listener);
Daniel Voina
2010-08-20 11:36:31
A:
I use SNMP4J for this.
This javadoc might help you write your code. You can use the Snmp.trap() method
Edit:
Well, I dont have code of my own at this moment, but you may refer this one . You have to use Snmp.notify() for sending V2 trap instead of Snmp.trap() as trap() only supports sending V1 traps.
Gopi
2010-07-28 16:29:07
+1
A:
Hi.It took me some time but I finally figured out how to use SNMP4J to send a trap: Hope that helps..
public static void main(String[] args) throws Exception {
// Create PDU
PDU trap = new PDU();
trap.setType(PDU.TRAP);
OID oid = new OID("1.2.3.4.5");
trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000))); // put your uptime here
trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
//Add Payload
Variable var = new OctetString("some string");
trap.add(new VariableBinding(oid, var));
// Specify receiver
Address targetaddress = new UdpAddress("10.101.21.32/162");
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(targetaddress);
// Send
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.send(trap, target, null, null);
}
hannes.koller
2010-08-24 12:30:40