[SNMP4J] Problem with SNMP Receiver

Adam Brons abrons at us.ibm.com
Thu Mar 29 02:22:18 CEST 2007


Hey Everyone, I'm trying to put together a SNMP Trap receiver and I am 
having some trouble with the CommandResponder.  I'm hoping you guys can 
point out what I'm doing wrong.

Thanks - in advance:

public class SNMPListener {
        // Number of SNMP Dispatcher Threads
        private static final int DISPATHER_THREAD_NUMBER = 2;
        private static Logger logger = 
Logger.getLogger(SNMPListener.class);
        private Snmp snmp = null;
        private USM usm = null;
 
        private InetAddress bindAddress;
        private Integer port;
        private Collection users;
 
        protected SNMPListener() throws UnknownHostException {
                this("0.0.0.0", 162, null);
        }
 
        protected SNMPListener(String bindAddress, Integer port, 
Collection users) throws UnknownHostException {
                this.bindAddress = InetAddress.getByName(bindAddress);
                this.port = port;
                this.users = users;
 
                // Create a thread pool to handle inbound SNMP events
                ThreadPool threadPool = ThreadPool.create("SNMPTrap", 
DISPATHER_THREAD_NUMBER);
                MessageDispatcher mtDispatcher = 
                        new MultiThreadedMessageDispatcher(threadPool, new 
MessageDispatcherImpl());
 
                // Add the SNMP versions we wish to monitor for
                mtDispatcher.addMessageProcessingModel(new MPv1());
                mtDispatcher.addMessageProcessingModel(new MPv2c());
                mtDispatcher.addMessageProcessingModel(new MPv3());
 
                snmp = new Snmp(mtDispatcher);
 
                // Setup Authentication and Encryption protocols for 
SNMPv3
                SecurityProtocols.getInstance().addDefaultProtocols();
 
            usm = new USM(SecurityProtocols.getInstance(),
                new OctetString(MPv3.createLocalEngineID()), 0);
            SecurityModels.getInstance().addSecurityModel(usm);
        }
 
        public synchronized void listen() throws IOException {
                // Make a tcp and udp based address for listening
                UdpAddress udpAddr = new UdpAddress(bindAddress,port);
                TcpAddress tcpAddr = new TcpAddress(bindAddress,port);
                SNMPEventDispatcher dispatcher = new 
SNMPEventDispatcher();
 
                // Add transport mapping and command handler to Snmp 
Engine
                if (snmp.addNotificationListener(udpAddr, dispatcher) &&
                        snmp.addNotificationListener(tcpAddr, dispatcher)) 
{
 
                        // Start listening
                        snmp.listen();
                        logger.info("Listening on 
UDP:"+udpAddr.toString()+"...");
                        logger.info("Listening on 
TCP:"+tcpAddr.toString()+"...");
 
                        // Just wait for something to happen.
                        try { this.wait(); }
                    catch (InterruptedException ex) { } 
                } else {
                        logger.error("Unable to bind 
address("+udpAddr.toString()+") to snmp listener");
                }
        }
 
        public static void main(String[] args) throws Exception {
                SNMPListener listener = new SNMPListener();
                listener.listen();
        }
}

public class SNMPEventDispatcher implements CommandResponder, 
AuthenticationFailureListener {
        private static Logger logger = 
Logger.getLogger(SNMPEventDispatcher.class);
 
        public SNMPEventDispatcher() {
                logger.debug("Instaniated SNMPEventDispatcher");
        }
        public synchronized void processPdu(CommandResponderEvent event) {
                logger.debug("ProcessPdu called for event 
["+event.toString()+"]");
                if (event.isProcessed()) {
                        logger.debug("Snmp Event ["+event.toString()+"] 
processed.");
                        return;
                }
 
                PDU pdu = event.getPDU();
                logger.debug("Received PDU: "+ pdu.toString());
 
                // This must be set when an CommandResponderEvent has been 
processed.
                event.setProcessed(true);
        }
 
        public void authenticationFailure(AuthenticationFailureEvent 
event) {
                logger.warn("Inbound SNMP event failed authentication");
        }
}


What's not happening is even with tcpdump traces and using tcp to show two 
way communication I never see logging from 
SNMPEventDispather.processPdu(CommandResponderEvent event).
As an added tidbit I'm running this in a JUNIT test in Eclipse so I don't 
know if that has anything to do with it.

Adam


More information about the SNMP4J mailing list