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

Adam Brons abrons at us.ibm.com
Mon Apr 2 20:42:13 CEST 2007


Frank,

Thanks that did the trick. 

For reference purposes... Here's the completed working 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;
import org.snmp4j.util.ThreadPool;
import org.snmp4j.MessageDispatcher;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.mp.MPv1;

public class TestListener {

        public TestListener() {
        }

        public synchronized void start(String address) throws Exception {
                UdpAddress listenAddress = new UdpAddress(address);
                ThreadPool threadPool = 
ThreadPool.create("DispatcherPool", 2);
            MessageDispatcher mtDispatcher = 
                new MultiThreadedMessageDispatcher(threadPool, new 
MessageDispatcherImpl());

            // add message processing models
            mtDispatcher.addMessageProcessingModel(new MPv1());
 
            // add default security models
            SecurityProtocols.getInstance().addDefaultProtocols();
 
                Snmp snmp = new Snmp(mtDispatcher); 
                CommandResponder trapPrinter = new CommandResponder() {
                  public void processPdu(CommandResponderEvent e) {
                    System.out.println("CommandResponder-Event: 
"+e.toString());
                        PDU command = e.getPDU();
                    if (command != null) {
                       System.out.println("CommandResponder-PDU: 
"+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());
                }
        }
}



Hi Adam,

You need to add the SNMPv1 message processing model
and the corresponding security model.

Best regards,
Frank

Adam Brons wrote:
> 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
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> http://lists.agentpp.org/mailman/listinfo/snmp4j

-- 
AGENT++
http://www.agentpp.com
http://www.mibexplorer.com
http://www.mibdesigner.com





More information about the SNMP4J mailing list