How can I send snmpv2 traps from Java application. I tried to do example on snmp4j, but it didn't work.
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With