Class SubagentXConfigManager
java.lang.Object
org.snmp4j.agent.agentx.subagent.SubagentXConfigManager
- All Implemented Interfaces:
Runnable, org.snmp4j.agent.mo.util.VariableProvider
public abstract class SubagentXConfigManager
extends Object
implements Runnable, org.snmp4j.agent.mo.util.VariableProvider
The
SubagentXConfigManager is the main component of a SNMP4J AgentX Subagent.
It puts together agent configuration and agent components like command
processor, message dispatcher, managed objects and server.- Since:
- 3.0.0
- Version:
- 3.0.0
- Author:
- Frank Fock
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassTheAgentStatemaintains the current run state of this agent and the list of errors that occurred since the agent was launched.static classTheErrorDescriptordescribes an error that occurred during an agent state transition, including the causing exception and the source and target states involved. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AgentXCommandListenerThe AgentX command listener that handles incoming AgentX packages and manages the associated AgentX sessions.protected List<org.snmp4j.agent.AgentStateListener<SubagentXConfigManager>> The list of listeners that are notified about agent state changes.protected org.snmp4j.agent.io.MOInputFactoryThe factory that provides theMOInputstream with the serialized agent configuration, ornullif no configuration is loaded.protected org.snmp4j.smi.OctetStringThe default SNMPv3 context of this agent.protected AgentXMessageDispatcherThe AgentX message dispatcher used to send and receive AgentX messages.protected org.snmp4j.agent.mo.MOFactoryThe factory used to create the managed objects of this agent.protected org.snmp4j.agent.NotificationOriginatorThe notification originator used to emit notifications, ornullif notifications are not supported.protected intThe import mode used when restoring persistent managed objects.protected org.snmp4j.agent.io.MOPersistenceProviderThe primary persistence provider used to load and store persistent managed objects.protected org.snmp4j.agent.mo.MOPriorityProviderThe provider that defines the order in which managed objects are persisted.protected SubagentXConfigManager.AgentStateThe current run state of this agent.protected org.snmp4j.agent.MOServer[]The managed object server(s) that serve the managed objects available to this agent.protected org.snmp4j.agent.mo.util.MOTableSizeLimit<org.snmp4j.agent.mo.MOTableRow> An optional table size limit that restricts the maximum number of rows of managed object tables, ornullif unlimited.protected org.snmp4j.util.WorkerPoolThe worker pool used to process incoming requests concurrently. -
Constructor Summary
ConstructorsConstructorDescriptionSubagentXConfigManager(AgentXCommandListener agentXCommandListener, AgentXMessageDispatcher messageDispatcher, org.snmp4j.agent.MOServer[] moServers, org.snmp4j.util.WorkerPool workerPool, org.snmp4j.agent.io.MOInputFactory configurationFactory, org.snmp4j.agent.io.MOPersistenceProvider persistenceProvider, org.snmp4j.agent.mo.MOFactory moFactory) Creates a SNMP agent configuration which can be run by callingrun()later. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAgentStateListener(org.snmp4j.agent.AgentStateListener<SubagentXConfigManager> agentStateListener) Adds a listener that is notified whenever the state of this agent changes.voidConfigures components and managed objects.booleanContinues processing of SNMP requests by coupling message dispatcher and agent.protected voidNotifies all registeredAgentStateListeners about the current state of this agent.protected voidFire notifications after agent start, i.e. sending a coldStart trap.protected org.snmp4j.smi.OctetStringgetContext(org.snmp4j.agent.MOGroup mibGroup, org.snmp4j.smi.OctetString defaultContext) This method can be overwritten by a subagent to specify the contexts each MIB module (group) will be registered to.org.snmp4j.smi.OctetStringReturns the default context - which is the context that is used by the base agent to register its MIB objects.intReturns the currently active import mode for theMOPersistenceProvider.org.snmp4j.agent.mo.MOPriorityProviderGets the priority provider defining the order of storing and restoring persistent data.org.snmp4j.agent.MOServergetServer(org.snmp4j.smi.OctetString context) Get theMOServerassociated with the given context.org.snmp4j.agent.MOServer[]Gets the managed object servers of this agent.org.snmp4j.agent.AgentStategetState()Returns the state of the agent.org.snmp4j.smi.VariablegetVariable(String name) org.snmp4j.util.WorkerPoolGets the worker pool of this agent.voidInitialize the agent by registering all MIB modules.protected voidlaunch()Launch the agent by registering and launching (i.e., set to listen mode) transport mappings.abstract voidEstablish AgentX connection to one or more AgentX master agents by implementing this method.protected voidLaunch the transport mappings.protected static voidlaunchTransportMappings(Collection<? extends org.snmp4j.TransportMapping<?>> transportMappings) Puts a list of transport mappings into listen mode.protected voidHook method for overriding in sub-classes that is called byinitialize()beforeregisterMIBs(OctetString).protected voidregisterMIBs(org.snmp4j.smi.OctetString context) Register the initialized MIB modules in the specified context of the agent.voidRegisters a shutdown hookThreadat theRuntimeinstance.booleanremoveAgentStateListener(org.snmp4j.agent.AgentStateListener<SubagentXConfigManager> agentStateListener) Removes a previously added agent state change listener.booleanRestore a previously persistently saved state - if available.voidrun()Initializes, configures, restores agent state, and then launches the SNMP agent depending on its current run state.booleanSave the state of the agent persistently - if necessary persistent storage is available.voidsetPersistenceImportMode(int importMode) Sets the import mode for theMOPersistenceProvider.voidsetPriorityProvider(org.snmp4j.agent.mo.MOPriorityProvider priorityProvider) Sets theMOPriorityProviderthat defines in which orderManagedObjects of this sub-agent are stored and restored.voidsetTableSizeLimit(int sizeLimit) Sets the table size limit for the tables in this agent.voidsetTableSizeLimits(Properties sizeLimits) Sets the table size limits for the tables in this agent.voidshutdown()Shutdown the agent by closing the internal SNMP session - including the transport mappings provided through the configuredMessageDispatcherand then store the agent state to persistent storage (if available).protected static voidstopTransportMappings(Collection<? extends org.snmp4j.TransportMapping<?>> transportMappings) Closes a list of transport mappings.voidSuspends processing of SNMP requests.protected voidunregisterMIBs(org.snmp4j.smi.OctetString context) Unregister the initialized MIB modules from the default context of the agent.
-
Field Details
-
agent
The AgentX command listener that handles incoming AgentX packages and manages the associated AgentX sessions. -
workerPool
protected org.snmp4j.util.WorkerPool workerPoolThe worker pool used to process incoming requests concurrently. -
servers
protected org.snmp4j.agent.MOServer[] serversThe managed object server(s) that serve the managed objects available to this agent. -
dispatcher
The AgentX message dispatcher used to send and receive AgentX messages. -
notificationOriginator
protected org.snmp4j.agent.NotificationOriginator notificationOriginatorThe notification originator used to emit notifications, ornullif notifications are not supported. -
configuration
protected org.snmp4j.agent.io.MOInputFactory configurationThe factory that provides theMOInputstream with the serialized agent configuration, ornullif no configuration is loaded. -
persistenceProvider
protected org.snmp4j.agent.io.MOPersistenceProvider persistenceProviderThe primary persistence provider used to load and store persistent managed objects. -
persistenceImportMode
protected int persistenceImportModeThe import mode used when restoring persistent managed objects. -
priorityProvider
protected org.snmp4j.agent.mo.MOPriorityProvider priorityProviderThe provider that defines the order in which managed objects are persisted. -
moFactory
protected org.snmp4j.agent.mo.MOFactory moFactoryThe factory used to create the managed objects of this agent. -
defaultContext
protected org.snmp4j.smi.OctetString defaultContextThe default SNMPv3 context of this agent. -
runState
The current run state of this agent. -
tableSizeLimit
protected org.snmp4j.agent.mo.util.MOTableSizeLimit<org.snmp4j.agent.mo.MOTableRow> tableSizeLimitAn optional table size limit that restricts the maximum number of rows of managed object tables, ornullif unlimited. -
agentStateListeners
The list of listeners that are notified about agent state changes.
-
-
Constructor Details
-
SubagentXConfigManager
public SubagentXConfigManager(AgentXCommandListener agentXCommandListener, AgentXMessageDispatcher messageDispatcher, org.snmp4j.agent.MOServer[] moServers, org.snmp4j.util.WorkerPool workerPool, org.snmp4j.agent.io.MOInputFactory configurationFactory, org.snmp4j.agent.io.MOPersistenceProvider persistenceProvider, org.snmp4j.agent.mo.MOFactory moFactory) Creates a SNMP agent configuration which can be run by callingrun()later.- Parameters:
agentXCommandListener- theAgentXCommandListenerthat handles the AgentX packages and that is able to close allAgentXSessions.messageDispatcher- the MessageDispatcher to use. The message dispatcher must be configured outside, i.e. transport mappings have to be added before this constructor is being called.moServers- the managed object server(s) that serve the managed objects available to this agent.workerPool- theWorkerPoolto be used to process incoming request.configurationFactory- aMOInputFactorythat creates aMOInputstream with containing serialized ManagedObject information with the agent's configuration ornullotherwise.persistenceProvider- the primaryMOPersistenceProviderto be used to load and store persistent MOs.moFactory- theMOFactoryto be used to createManagedObjects created by this config manager. IfnulltheDefaultMOFactorywill be used.
-
-
Method Details
-
getWorkerPool
public org.snmp4j.util.WorkerPool getWorkerPool()Gets the worker pool of this agent.- Returns:
- the internal worker pool.
-
run
public void run()Initializes, configures, restores agent state, and then launches the SNMP agent depending on its current run state. For example, ifinitialize()has not yet been called it will be called before the agent is being configured in the next step.See also
initialize(),configure(),restoreState(), andlaunch(). -
addAgentStateListener
public void addAgentStateListener(org.snmp4j.agent.AgentStateListener<SubagentXConfigManager> agentStateListener) Adds a listener that is notified whenever the state of this agent changes.- Parameters:
agentStateListener- the listener to add.
-
removeAgentStateListener
public boolean removeAgentStateListener(org.snmp4j.agent.AgentStateListener<SubagentXConfigManager> agentStateListener) Removes a previously added agent state change listener.- Parameters:
agentStateListener- the listener to remove.- Returns:
trueif the listener was registered and has been removed,falseotherwise.
-
fireAgentStateChange
protected void fireAgentStateChange()Notifies all registeredAgentStateListeners about the current state of this agent. -
getServer
public org.snmp4j.agent.MOServer getServer(org.snmp4j.smi.OctetString context) Get theMOServerassociated with the given context.- Parameters:
context- a SNMPv3 context ornull(or emptyOctetString) for the default context.- Returns:
- the associated
MOServerfor the context that acutally also supports the context ornullotherwise.
-
getServers
public org.snmp4j.agent.MOServer[] getServers()Gets the managed object servers of this agent.- Returns:
- the array of
MOServerinstances that serve the managed objects of this agent.
-
getState
public org.snmp4j.agent.AgentState getState()Returns the state of the agent.- Returns:
- a
AgentStateinstance.
-
launch
protected void launch()Launch the agent by registering and launching (i.e., set to listen mode) transport mappings. -
launchAgentXSessions
Establish AgentX connection to one or more AgentX master agents by implementing this method.- Throws:
IOException- if the session(s) cannot be established due to IO error and the agent should not advance to the stateAgentState.STATE_RUNNING.
-
fireLaunchNotifications
protected void fireLaunchNotifications()Fire notifications after agent start, i.e. sending a coldStart trap. -
continueProcessing
public boolean continueProcessing()Continues processing of SNMP requests by coupling message dispatcher and agent. To succeed, the current state of the agent must beAgentState.STATE_SUSPENDED.- Returns:
trueif the running state could be restored,falseotherwise.
-
suspendProcessing
public void suspendProcessing()Suspends processing of SNMP requests. This call decouples message dispatcher and agent. All transport mappings remain unchanged and thus all ports remain opened. -
shutdown
public void shutdown()Shutdown the agent by closing the internal SNMP session - including the transport mappings provided through the configuredMessageDispatcherand then store the agent state to persistent storage (if available). -
registerShutdownHook
public void registerShutdownHook()Registers a shutdown hookThreadat theRuntimeinstance. -
launchTransportMappings
Launch the transport mappings. By defaultlaunchTransportMappings(Collection)is called with the transport mappings of the dispatcher.- Throws:
IOException- if a transport mapping throws anIOExceptionofTransportMapping.listen().
-
launchTransportMappings
protected static void launchTransportMappings(Collection<? extends org.snmp4j.TransportMapping<?>> transportMappings) throws IOException Puts a list of transport mappings into listen mode.- Parameters:
transportMappings- a list ofTransportMappinginstances.- Throws:
IOException- if a transport cannot listen to incoming messages.
-
stopTransportMappings
protected static void stopTransportMappings(Collection<? extends org.snmp4j.TransportMapping<?>> transportMappings) throws IOException Closes a list of transport mappings.- Parameters:
transportMappings- a list ofTransportMappinginstances.- Throws:
IOException- if a transport cannot be closed.
-
saveState
public boolean saveState()Save the state of the agent persistently - if necessary persistent storage is available.- Returns:
trueif state has been saved successfully,falseis returned if an error occurred or nopersistenceProvideris set. The error details can be found in therunStateobject.
-
restoreState
public boolean restoreState()Restore a previously persistently saved state - if available.- Returns:
trueif the agent state could be restored successfully,falseotherwise.
-
configure
public void configure()Configures components and managed objects. -
linkCounterListener
protected void linkCounterListener()Hook method for overriding in sub-classes that is called byinitialize()beforeregisterMIBs(OctetString). This method does nothing by default. -
initialize
public void initialize()Initialize the agent by registering all MIB modules. -
setTableSizeLimits
Sets the table size limits for the tables in this agent. If this method is called while the agent's registration is being changed, aConcurrentModificationExceptionmight be thrown.- Parameters:
sizeLimits- a set of properties as defined byMOTableSizeLimit.- Since:
- 1.4
-
setTableSizeLimit
public void setTableSizeLimit(int sizeLimit) Sets the table size limit for the tables in this agent. If this method is called while the agent's registration is being changed, aConcurrentModificationExceptionmight be thrown.- Parameters:
sizeLimit- the maximum size (numer of rows) of tables allowed for this agent.- Since:
- 1.4
-
getDefaultContext
public org.snmp4j.smi.OctetString getDefaultContext()Returns the default context - which is the context that is used by the base agent to register its MIB objects. By default it isnullwhich causes the objects to be registered virtually for all contexts. In that case, subagents for example my not register their own objects under the same subtree(s) in any context. To allow subagents to register their own instances of those MIB modules, an emptyOctetStringshould be used as default context instead.- Returns:
nullor anOctetString(normally the empty string) denoting the context used for registering default MIBs.
-
getContext
protected org.snmp4j.smi.OctetString getContext(org.snmp4j.agent.MOGroup mibGroup, org.snmp4j.smi.OctetString defaultContext) This method can be overwritten by a subagent to specify the contexts each MIB module (group) will be registered to.- Parameters:
mibGroup- a group ofManagedObjects (i.e., a MIB module).defaultContext- the context to be used by default (i.e., thenullcontext)- Returns:
- the context for which the module should be registered.
-
registerMIBs
protected void registerMIBs(org.snmp4j.smi.OctetString context) throws org.snmp4j.agent.DuplicateRegistrationException Register the initialized MIB modules in the specified context of the agent.- Parameters:
context- the context to register the internal MIB modules. This should benullby default.- Throws:
org.snmp4j.agent.DuplicateRegistrationException- if some of the MIB modules registration regions conflict with already registered regions.
-
unregisterMIBs
protected void unregisterMIBs(org.snmp4j.smi.OctetString context) Unregister the initialized MIB modules from the default context of the agent.- Parameters:
context- the context where the MIB modules have been previously registered.
-
getPriorityProvider
public org.snmp4j.agent.mo.MOPriorityProvider getPriorityProvider()Gets the priority provider defining the order of storing and restoring persistent data.- Returns:
- a
MOPriorityProviderinstance.
-
setPriorityProvider
public void setPriorityProvider(org.snmp4j.agent.mo.MOPriorityProvider priorityProvider) Sets theMOPriorityProviderthat defines in which orderManagedObjects of this sub-agent are stored and restored. A defined order can be important for initializing dependent objects and to avoid security issues during restore operations.- Parameters:
priorityProvider- aMOPriorityProviderornullif the default lexicographic order by OID should be applied.- Since:
- 3.5.0
-
setPersistenceImportMode
public void setPersistenceImportMode(int importMode) Sets the import mode for theMOPersistenceProvider.- Parameters:
importMode- one of the import modes defined byImportMode.
-
getPersistenceImportMode
public int getPersistenceImportMode()Returns the currently active import mode for theMOPersistenceProvider.- Returns:
- one of the import modes defined by
ImportMode.
-
getVariable
- Specified by:
getVariablein interfaceorg.snmp4j.agent.mo.util.VariableProvider
-