tags:

views:

187

answers:

3

How can i send snmpv2 traps from Java application. I tried to do example on snmp4j, but it didn't work. Thanks a lot!

A: 

I would go for snmp4j library http://www.snmp4j.org/.

Daniel Voina
do you have a simple example, how to do it right?thanks!
EK
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
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
do you have a simple example, how to do it right?thanks!
EK
edited my answer to provide a link to code
Gopi
+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