[SNMP4J] AgentXSubagent enhancements

Andy Coulbeck andrew.coulbeck at unboundid.com
Thu Apr 22 20:08:32 CEST 2010


Here are some enhancements to AgentXSubagent in snmp4j-agentx-1.3.

1.  Add a new method registerRegionForMO to allow a new managed object to be registered with the MOServer when there is an existing session.  The registerRegions method is only suitable on a new session because it registers all objects in the MOServer.
2.  In the registerRegion method, set the send timeout to the session timeout.  Otherwise the send timeout cannot be changed from the default of 5 seconds.
3.  Similarly, in the unregisterRegion method, set the send timeout to the provided timeout value or the session timeout.  Otherwise the send timeout cannot be changed from the default of 5 seconds.  There is another issue with this method because it is setting the priority field in the PDU to the timeout value, but I have not attempted a fix for that.
4.  In the addMaster method, turn off server mode for TCP transport.  Perhaps I am missing something but I see no reason why the subagent should be listening for incoming connections.


--- snmp4j-agentx-1.3/src/org/snmp4j/agent/agentx/subagent/AgentXSubagent.java	2010-04-22 13:00:25.000000000 -0500
***************
*** 39,50 ****
  import org.snmp4j.mp.SnmpConstants;
  import org.snmp4j.smi.*;
  import org.snmp4j.transport.ConnectionOrientedTransportMapping;
  import org.snmp4j.transport.TransportMappings;
  import org.snmp4j.util.ThreadPool;
  import org.snmp4j.agent.mo.MOTableRow;
  import org.snmp4j.agent.agentx.subagent.index.AnyNewIndexOID;
  import org.snmp4j.agent.agentx.subagent.index.NewIndexOID;
- import org.snmp4j.agent.mo.snmp.SysUpTime;
  import java.util.Map.Entry;
  import org.snmp4j.util.WorkerPool;
  import org.snmp4j.util.WorkerTask;
--- 39,50 ----
  import org.snmp4j.mp.SnmpConstants;
  import org.snmp4j.smi.*;
  import org.snmp4j.transport.ConnectionOrientedTransportMapping;
+ import org.snmp4j.transport.DefaultTcpTransportMapping;
  import org.snmp4j.transport.TransportMappings;
  import org.snmp4j.util.ThreadPool;
  import org.snmp4j.agent.mo.MOTableRow;
  import org.snmp4j.agent.agentx.subagent.index.AnyNewIndexOID;
  import org.snmp4j.agent.agentx.subagent.index.NewIndexOID;
  import java.util.Map.Entry;
  import org.snmp4j.util.WorkerPool;
  import org.snmp4j.util.WorkerTask;
***************
*** 86,92 ****
    private OID subagentID;
    private OctetString subagentDescr;
  
!   private long timeout = AgentXProtocol.DEFAULT_TIMEOUT_SECONDS * 1000;
    private byte defaultPriority = AgentXProtocol.DEFAULT_PRIORITY;
  
    private Timer pingTimer;
--- 86,92 ----
    private OID subagentID;
    private OctetString subagentDescr;
  
! //  private long timeout = AgentXProtocol.DEFAULT_TIMEOUT_SECONDS * 1000;
    private byte defaultPriority = AgentXProtocol.DEFAULT_PRIORITY;
  
    private Timer pingTimer;
***************
*** 395,406 ****
--- 395,437 ----
            continue;
          }
        }
+       registerRegionForMO(mo, session, context, sysUpTime, registrationCallback);
+     }
+   }
+ 
+   /**
+    * Registers the subagent regions at the master agent for a single managed
+    * object.  This method can be called when managed objects are registered
+    * with the MOServer after a session has been established.
+    *
+    * @param mo
+    *    the managed object whose regions are to be registered.
+    * @param session
+    *    the session on whose behalf regions are registered.
+    * @param context
+    *    the context to use for registration.
+    * @param sysUpTime
+    *    if not <code>null</code>, the master agent's notion of the sysUpTime
+    *    for the registered context is returned. The input value is always
+    *    ignored!
+    * @param registrationCallback
+    *    a possibly <code>null</code> reference to a
+    *    <code>RegistrationCallback</code> instance to handle registration
+    *    events.
+    */
+   public void registerRegionForMO(
+       ManagedObject mo,
+       AgentXSession session, OctetString context,
+       TimeTicks sysUpTime,
+       RegistrationCallback registrationCallback)
+   {
      if (mo instanceof AgentXSharedMOTable) {
        registerSharedTableRows(session, context,
                                (AgentXSharedMOTable)mo,
                                registrationCallback);
      }
      else {
+       MOScope scope = mo.getScope();
        AgentXRegion region =
            new AgentXRegion(scope.getLowerBound(), scope.getUpperBound());
        if (mo instanceof MOScalar) {
***************
*** 412,418 ****
                                        getPriority(mo, region), sysUpTime);
            if (status != AgentXProtocol.AGENTX_SUCCESS) {
              if (LOGGER.isWarnEnabled()) {
!               LOGGER.warn("Failed to registered MO " + scope +
                            " with status = " +
                            status);
              }
--- 443,449 ----
                                      getPriority(mo, region), sysUpTime);
          if (status != AgentXProtocol.AGENTX_SUCCESS) {
            if (LOGGER.isWarnEnabled()) {
!             LOGGER.warn("Failed to register MO " + scope +
                          " with status = " +
                          status);
            }
***************
*** 436,442 ****
          }
        }
      }
-   }
  
    /**
     * Registers the indexes and (row) regions of a shared table. This method
--- 467,472 ----
***************
*** 576,582 ****
      if ((session == null) || (session.isClosed())) {
        return AgentXProtocol.AGENTX_NOT_OPEN;
      }
!     long t = (this.timeout == 0) ? session.getTimeout()*1000 : this.timeout;
      AgentXRegisterPDU pdu =
          new AgentXRegisterPDU(context, region.getLowerBound(), priority,
                                region.getRangeSubID(),
--- 606,612 ----
      if ((session == null) || (session.isClosed())) {
        return AgentXProtocol.AGENTX_NOT_OPEN;
      }
!     long t = session.getTimeout()*1000;
      AgentXRegisterPDU pdu =
          new AgentXRegisterPDU(context, region.getLowerBound(), priority,
                                region.getRangeSubID(),
***************
*** 598,611 ****
        return AgentXProtocol.AGENTX_NOT_OPEN;
      }
      byte t = (timeout == 0) ? session.getTimeout() : timeout;
      AgentXUnregisterPDU pdu =
          new AgentXUnregisterPDU(context, region.getLowerBound(), t,
                                  region.getRangeSubID(),
                                  region.getUpperBoundSubID());
      pdu.setSessionAttributes(session);
      AgentXResponseEvent event =
!         agentX.send(pdu, new AgentXTarget(session.getPeer().getAddress(),
!                                           this.timeout),
                      session.getPeer().getTransport());
      return getResponseStatus(event);
    }
--- 628,642 ----
        return AgentXProtocol.AGENTX_NOT_OPEN;
      }
      byte t = (timeout == 0) ? session.getTimeout() : timeout;
+ 
+     // FIXME: Why are we setting the priority to a timeout value?
      AgentXUnregisterPDU pdu =
          new AgentXUnregisterPDU(context, region.getLowerBound(), t,
                                  region.getRangeSubID(),
                                  region.getUpperBoundSubID());
      pdu.setSessionAttributes(session);
      AgentXResponseEvent event =
!         agentX.send(pdu, new AgentXTarget(session.getPeer().getAddress(), t),
                      session.getPeer().getTransport());
      return getResponseStatus(event);
    }
***************
*** 634,639 ****
--- 665,679 ----
        tcpTransport.setConnectionTimeout(0);
        tcpTransport.setMessageLengthDecoder(new AgentXProtocol());
      }
+ 
+     // Turn off server mode for TCP transport
+     if (transport instanceof DefaultTcpTransportMapping)
+     {
+       DefaultTcpTransportMapping defaultTcpTransport =
+           (DefaultTcpTransportMapping)transport;
+       defaultTcpTransport.setServerEnabled(false);
+     }
+ 
      agentX.addTransportMapping(transport);
      transport.listen();
      return transport;




More information about the SNMP4J mailing list