Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send trap v2 in Java

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

like image 585
EK. Avatar asked Jul 28 '10 14:07

EK.


2 Answers

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);                      
}
like image 115
hannes.koller Avatar answered Sep 18 '22 23:09

hannes.koller


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.

like image 21
Gopi Avatar answered Sep 18 '22 23:09

Gopi