[SNMP4J] Cannot get CommandResponder.processPdu() to work

Adam Brons abrons at us.ibm.com
Mon Apr 2 19:43:22 CEST 2007


I'm having trouble receiving snmp trap messages.  I've used example code 
provided in the java doc and looked through a large section of the mail 
archives.  I know I have to be missing something, but don't appear to see 
what it is.  Any help would be greatly appreciated.

Here's my test code:

import org.snmp4j.TransportMapping;
import org.snmp4j.Snmp;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.PDU;
import org.snmp4j.smi.UdpAddress;

public class TestListener {
  public TestListener() {
  }

  public synchronized void start(String address) throws Exception {
    UdpAddress listenAddress = new UdpAddress(address);
    Snmp snmp = new Snmp();

    CommandResponder trapPrinter = new CommandResponder() {
      public void processPdu(CommandResponderEvent e) {
        System.out.println("I'm in processPdu");
        PDU command = e.getPDU();
        if (command != null) {
          System.out.println(command.toString());
        }
      }
    };
 
    if (snmp.addNotificationListener(listenAddress, trapPrinter)) {
      snmp.listen();
      System.out.println("Waiting for snmp events on " + 
listenAddress.toString());
      try { this.wait(); } 
      catch(InterruptedException ex) { System.out.println("Caught 
InterruptException: "+ex.toString()); }
      catch(Exception ex) { System.out.println("CaughtException: 
"+ex.toString()); }
    } else {
      System.out.println("snmp.addNotificationListener returned FALSE");
      System.out.println("TestListener closing...");
    }
  }

  public static void main(String [ ] args) {
    String address = null;

    if (args.length > 0) {
      address = args[0];
    }

    if (address == null || address.equals("")) {
      address = "0.0.0.0/16200";
    }

    TestListener test = new TestListener();
    try {
      test.start(address);
    } catch (Exception ex) {
      System.out.println("CaughtException: "+ex.toString());
    }
  }
}


Here's the output trying to run this code:
$ java -classpath 
.:/home/abrons/adm/prometheus/snmp4j/snmp4j-1.8.1/dist/lib/SNMP4J.jar 
TestListener 127.0.0.1/16200
Waiting for snmp events on 127.0.0.1/16200

Here's the output trying to send a snmp v1trap to the TestListener using 
the SNMP tool provided with snmp4j:
$ ./SNMP4J-linux -p V1TRAP -v 1 127.0.0.1/16200
Apr 2, 2007 1:36:39 PM org.snmp4j.log.JavaLogAdapter log
INFO: UDP receive buffer size for socket 127.0.0.1/0 is set to: 55296
V1TRAP sent successfully

Here's the output trying to run this code using the SNMP tool provided 
with snmp4j to receive the trap and send the trap the same as the previous 
send output:
$ ./SNMP4J-linux -Ol 127.0.0.1/16200
Listening on 127.0.0.1/16200
Apr 2, 2007 1:38:47 PM org.snmp4j.log.JavaLogAdapter log
INFO: UDP receive buffer size for socket 127.0.0.1/16200 is set to: 55296
V1TRAP[reqestID=0,timestamp=0:00:00.00,enterprise=0.0,genericTrap=0,specificTrap=0, 
VBS[1.3.6 = Null]]


Adam Brons
TSOM Software Engineer
IBM Tivoli Software


More information about the SNMP4J mailing list