[SNMP4J] SNMP.close() not working for UdpTransport

Frank Fock fock at agentpp.com
Wed Aug 15 21:38:19 CEST 2007


Hi Adam,

Thanks for the clarification! To fix the problem
add the following methods to UdpAddress.java
and TcpAddress.java respectively:

   public boolean equals(Object o) {
     if (o instanceof UdpAddress) {
       return super.equals(o);
     }
     return false;
   }

   public boolean equals(Object o) {
     if (o instanceof TcpAddress) {
       return super.equals(o);
     }
     return false;
   }

Best regards,
Frank

Adam Brons wrote:
> Frank,
> 
> Thanks for the quick response.  Since my last email I did a bit more 
> digging.  I've noticed that if I only use one protocol that I do not have 
> a problem using Snmp.close() regardless of protocol. 
> I'm kinda learning towards this being a problem with the fact that the UDP 
> and TCP addresses are both 0.0.0.0/162.  I've looked at the source for 
> addNotificationListener and the inner class NotificationDispatcher.  I 
> believe what might be happening is that transport for UDP is overwritten 
> by the TCP transport since the key is the ListenAddress.
> 
> As for my design, the purpose is to allow a user to shutdown the listening 
> for SNMP messages.  As it stands in my last email.  A user would disable 
> the SNMP Listener, but the process was still bound to the port until the 
> application was closed.  This application is one such that it wouldn't not 
> close regularly.  So if a user needed to disable the SNMP conduit this was 
> not working.  I mentioned restarting because the user was not able to 
> re-enable the SNMP listening until the application was completely closed 
> and opened again.
> 
> Adam Brons
> TSOM Software Engineer
> IBM Tivoli Software
> 
> 
> 
> Frank Fock <fock at agentpp.com> 
> 08/15/07 02:17 PM
> 
> To
> Adam Brons/Atlanta/IBM at IBMUS
> cc
> snmp4j at agentpp.org
> Subject
> Re: [SNMP4J] SNMP.close() not working for UdpTransport
> 
> 
> 
> 
> 
> 
> Hi Adam,
> 
> You need to extend DefaultUDPTransportMapping to
> set the socket option "setReuseAddress(true);"
> before calling Snmp.listen(). Alternatively,
> you can (should) change your design to avoid
> repetitive socket creation.
> 
> Best regards,
> Frank
> 
> Adam Brons wrote:
>> I'm wondering if anyone has had a problem with releasing UPD resources 
>> when calling a Snmp.close().
>>
>> I'm attempting to listen to both UDP and TCP port 162 on IP 0.0.0.0. 
> When 
>> I call Snmp.close() the TCP resources are immediately returned, however 
>> even after 65 seconds the UDP resources are still held.  Once the 
>> application exists and the JVM stops, the resources are released. 
>> Unfortunately, I need to be able to "restart" the listening on the TCP 
> and 
>> UDP ports via user intervention.  Also it should be noted that I'm 
> created 
>> a new Snmp object for each start.  I'm using version 1.8.1 of the SNMP4J 
> 
>> API.
>>
>> Here are some code snippets:
>>
>> Constructor: 
>>         protected SNMPListener(SnmpConduitVariables variables, String 
>> conduitType) throws UnknownHostException { 
>>                 this.variables = variables;
>>                 this.conduitType = conduitType;
>>
>>                 if (variables != null && variables.getUsers() != null) {
>>                         configSnmpUsers(variables.getUsers());
>>                 }
>>
>>                 // Create a thread pool to handle inbound SNMP events
>>                 threadPool = ThreadPool.create("SNMPConduit", 
>> DISPATHER_THREAD_NUMBER);
>>                 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());
>>
>>                 // Setup Authentication and Encryption protocols for 
>> SNMPv3
>>                 SecurityProtocols.getInstance().addDefaultProtocols();
>>
>>                 /*
>>                  * Builds the User Based Security Model up containing 
> all 
>> supported security
>>                  * protocols, and setting a default engineID.
>>                  * 
>>                  * MPv3.createLocalEngineID might be something we allow 
>> the user to specify as part of the 
>>                  * Sensor or Conduit configuration.
>>                  */
>>             usm = new USM(SecurityProtocols.getInstance(),
>>               new OctetString(MPv3.createLocalEngineID()), 0);
>>             SecurityModels.getInstance().addSecurityModel(usm);
>>
>>
>> Listen Method:
>>         public synchronized void listen() throws IOException, 
>> UnknownHostException {
>>                 if (variables == null) {
>>                         logger.error("No bind address or bind port were 
>> specificed");
>>                         throw new 
>> UnknownHostException("SnmpConduitVariables is null therefore no bind 
>> address and port can be determined");
>>                 }
>>
>>                 // Create Snmp4j Processor using our supplied 
>> MessageProcessingModel
>>                 snmp = new Snmp(mtDispatcher);
>>
>>                 // Make a tcp and udp based address for listening
>>                 UdpAddress udpAddr = new 
>> UdpAddress(variables.getBindAddress() + "/" + variables.getBindPort());
>>                 TcpAddress tcpAddr = new 
>> TcpAddress(variables.getBindAddress() + "/" + variables.getBindPort());
>>                 SNMPEventDispatcher dispatcher = new 
>> SNMPEventDispatcher(variables, conduitType);
>>
>>                 // Add transport mapping and command handler to Snmp 
>> Engine
>>                 if (snmp.addNotificationListener(udpAddr, dispatcher) &&
>>                         snmp.addNotificationListener(tcpAddr, 
> dispatcher)) 
>> {
>>
>>                         // Start listening
>>                         snmp.listen();
>>                         logger.info("SNMP Conduit Listening on 
>> UDP:"+udpAddr.toString()+"...");
>>                         logger.info("SNMP Conduit Listening on 
>> TCP:"+tcpAddr.toString()+"...");
>>
>>                         // Just wait for something to happen.
>>                         try { 
>>                                 synchronized (mutex) {
>>                                         if (logger.isDebugEnabled()) 
>> logger.debug("SnmpListener '"+variables.getBindAddress()+"' 
> Waiting...");
>>                                         mutex.wait();
>>                                 }
>>                         }
>>                     catch (InterruptedException ex) {
>>                         if (logger.isDebugEnabled()) {
>>                                 logger.debug("SNMPConduit Caught 
>> Interrupted Exception: ", ex);
>>                         }
>>                     } 
>>                 } else {
>>                         logger.error("Unable to bind using 
>> address("+udpAddr.toString()+")");
>>                         throw new IOException("Unable to bind using 
>> address("+udpAddr.toString()+")");
>>                 }
>>         }
>>
>> Close Method:
>>         protected void close() {
>>                 synchronized (mutex) {
>>                         if (logger.isDebugEnabled()) 
>> logger.debug("SnmpListener '"+variables.getBindAddress()+"' 
> stopping...");
>>                         mutex.notify();
>>                         try {
>>                                 snmp.close();
>>                                 mutex.wait(65000);
>>                                 if (logger.isDebugEnabled()) 
>> logger.debug("SnmpListener: '"+variables.getBindAddress()+"' has 
> closed");
>>                         }
>>                         catch(IOException ioe) {}
>>                         catch(InterruptedException ie) {}
>>                 }
>>         }
>>
>> 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