org.snmp4j.agent
Class BaseAgent

java.lang.Object
  extended by org.snmp4j.agent.BaseAgent
All Implemented Interfaces:
java.lang.Runnable

public abstract class BaseAgent
extends java.lang.Object
implements java.lang.Runnable

The BaseAgent abstract class defines a framework for writing SNMP agents using the SNMP4J-Agent API. To implement your own SNMP agent, extend this class and implement the abstract methods defined by BaseAgent. The hook methods do not need any specific implementation. They only provide a defined mechanism to customize your agent.

Version:
1.0
Author:
Frank Fock

Field Summary
protected  CommandProcessor agent
           
protected  int agentState
           
protected  java.io.File bootCounterFile
           
protected  java.lang.String configFileURI
           
protected  org.snmp4j.smi.OctetString defaultContext
           
protected  DefaultMOPersistenceProvider defaultPersistenceProvider
           
protected  ProxyForwarder defaultProxyForwarder
           
protected  org.snmp4j.MessageDispatcherImpl dispatcher
           
protected  org.snmp4j.mp.MPv3 mpv3
           
protected  NotificationOriginator notificationOriginator
           
protected  DefaultMOServer server
           
protected  org.snmp4j.Snmp session
           
protected  Snmp4jConfigMib snmp4jConfigMIB
           
protected  Snmp4jLogMib snmp4jLogMIB
           
protected  SnmpCommunityMIB snmpCommunityMIB
           
protected  SnmpFrameworkMIB snmpFrameworkMIB
           
protected  SnmpNotificationMIB snmpNotificationMIB
           
protected  SnmpProxyMIB snmpProxyMIB
           
protected  SnmpTargetMIB snmpTargetMIB
           
protected  SNMPv2MIB snmpv2MIB
           
static int STATE_CREATED
           
static int STATE_INIT_FINISHED
           
static int STATE_INIT_STARTED
           
static int STATE_RUNNING
           
static int STATE_STOPPED
           
protected  org.snmp4j.smi.OctetString sysDescr
           
protected  org.snmp4j.smi.OID sysOID
           
protected  org.snmp4j.smi.Integer32 sysServices
           
protected  org.snmp4j.TransportMapping[] transportMappings
           
protected  org.snmp4j.security.USM usm
           
protected  UsmMIB usmMIB
           
protected  VacmMIB vacmMIB
           
 
Constructor Summary
protected BaseAgent(java.io.File bootCounterFile, java.io.File configFile, CommandProcessor commandProcessor)
          Creates a base agent with boot-counter, config file, and a CommandProcessor for processing SNMP requests.
protected BaseAgent(java.lang.String configURI)
          Creates a base agent with a DefaultMOServer as MOServer.
 
Method Summary
protected abstract  void addCommunities(SnmpCommunityMIB communityMIB)
          Adds community to security name mappings needed for SNMPv1 and SNMPv2c.
protected abstract  void addNotificationTargets(SnmpTargetMIB targetMIB, SnmpNotificationMIB notificationMIB)
          Adds initial notification targets and filters.
protected  void addShutdownHook()
          Adds a shutdown hook that saves the internal config into the config file when a SIGTERM (Ctrl-C) is terminating the agent.
protected abstract  void addUsmUser(org.snmp4j.security.USM usm)
          Adds all the necessary initial users to the USM.
protected abstract  void addViews(VacmMIB vacmMIB)
          Adds initial VACM configuration.
protected  void finishInit()
          Finishes intialization of the agent by connecting server and command processor, setting up USM, VACM, notification targets, and finally sending a coldStart notification to configured targets.
 CommandProcessor getAgent()
           
 int getAgentState()
          Returns the agent's state.
 java.io.File getBootCounterFile()
           
 java.io.File getConfigFile()
           
protected  org.snmp4j.smi.OctetString getContext(MOGroup mibGroup)
          This method can be overwritten by a subagent to specify the contexts each MIB module (group) will be registered to.
 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.
 ProxyForwarder getDefaultProxyForwarder()
           
protected  int getEngineBoots()
          Reads the engine boots counter from the corresponding input stream (file).
 org.snmp4j.mp.MPv3 getMPv3()
           
 NotificationOriginator getNotificationOriginator()
           
 DefaultMOServer getServer()
           
 org.snmp4j.Snmp getSession()
           
 Snmp4jConfigMib getSnmp4jConfigMIB()
           
 Snmp4jLogMib getSnmp4jLogMIB()
           
 SnmpCommunityMIB getSnmpCommunityMIB()
           
 SnmpFrameworkMIB getSnmpFrameworkMIB()
           
 SnmpNotificationMIB getSnmpNotificationMIB()
           
 SnmpProxyMIB getSnmpProxyMIB()
           
 SnmpTargetMIB getSnmpTargetMIB()
           
 SNMPv2MIB getSnmpv2MIB()
           
 org.snmp4j.smi.OctetString getSysDescr()
           
 org.snmp4j.smi.OID getSysOID()
           
 org.snmp4j.smi.Integer32 getSysServices()
           
 org.snmp4j.security.USM getUsm()
           
 UsmMIB getUsmMIB()
           
 VacmMIB getVacmMIB()
           
 void init()
          Initialize transport mappings, message dispatcher, basic MIB modules, proxy forwarder, VACM and USM security, and custom MIB modules and objects provided by sub-classes.
protected  void initConfigMIB()
           
protected  void initMessageDispatcher()
          Initializes the message dispatcher (MessageDispatcherImpl) with the transport mappings.
protected  void initSnmpSession()
           
protected  void initTransportMappings()
          Initializes the transport mappings (ports) to be used by the agent.
 void loadConfig(int importMode)
          Loads the configuration using the specified import mode from the set config file.
protected abstract  void registerManagedObjects()
          Register additional managed objects at the agent's server.
protected  void registerSnmpMIBs()
          Register the basic MIB modules at the agent's MOServer.
 void run()
          Starts the agent by let it listen on the configured SNMP agent ports (transpot mappings).
 void saveConfig()
          Save the current (serializable) managed object configuration into the config file.
protected  void sendColdStartNotification()
           
 void setAgent(CommandProcessor agent)
           
 void setBootCounterFile(java.io.File bootCounterFile)
           
 void setConfigFile(java.io.File configFile)
           
 void setDefaultContext(org.snmp4j.smi.OctetString defaultContext)
          Sets the default context for this base agent.
 void setDefaultProxyForwarder(ProxyForwarder defaultProxyForwarder)
           
protected  void setEngineBoots(int engineBoots)
           
 void setSysDescr(org.snmp4j.smi.OctetString sysDescr)
           
 void setSysOID(org.snmp4j.smi.OID sysOID)
           
 void setSysServices(org.snmp4j.smi.Integer32 sysServices)
           
protected  void setupDefaultProxyForwarder()
          Creates and registers the default proxy forwarder application (ProxyForwarderImpl).
 void stop()
          Stops the agent by closing the SNMP session and associated transport mappings.
protected abstract  void unregisterManagedObjects()
          Unregister additional managed objects from the agent's server.
protected  void unregisterSnmpMIBs()
          Unregister the basic MIB modules from the agent's MOServer.
protected  int updateEngineBoots()
          Updates the engine boots counter and returns the actual value.
protected  void updateSession(org.snmp4j.Session session)
          Updates all objects with a new session instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATE_CREATED

public static final int STATE_CREATED
See Also:
Constant Field Values

STATE_INIT_STARTED

public static final int STATE_INIT_STARTED
See Also:
Constant Field Values

STATE_INIT_FINISHED

public static final int STATE_INIT_FINISHED
See Also:
Constant Field Values

STATE_RUNNING

public static final int STATE_RUNNING
See Also:
Constant Field Values

STATE_STOPPED

public static final int STATE_STOPPED
See Also:
Constant Field Values

snmpv2MIB

protected SNMPv2MIB snmpv2MIB

snmpFrameworkMIB

protected SnmpFrameworkMIB snmpFrameworkMIB

snmpTargetMIB

protected SnmpTargetMIB snmpTargetMIB

snmpNotificationMIB

protected SnmpNotificationMIB snmpNotificationMIB

snmpProxyMIB

protected SnmpProxyMIB snmpProxyMIB

snmpCommunityMIB

protected SnmpCommunityMIB snmpCommunityMIB

snmp4jLogMIB

protected Snmp4jLogMib snmp4jLogMIB

snmp4jConfigMIB

protected Snmp4jConfigMib snmp4jConfigMIB

usmMIB

protected UsmMIB usmMIB

vacmMIB

protected VacmMIB vacmMIB

server

protected DefaultMOServer server

session

protected org.snmp4j.Snmp session

transportMappings

protected org.snmp4j.TransportMapping[] transportMappings

dispatcher

protected org.snmp4j.MessageDispatcherImpl dispatcher

agent

protected CommandProcessor agent

mpv3

protected org.snmp4j.mp.MPv3 mpv3

usm

protected org.snmp4j.security.USM usm

bootCounterFile

protected java.io.File bootCounterFile

notificationOriginator

protected NotificationOriginator notificationOriginator

defaultProxyForwarder

protected ProxyForwarder defaultProxyForwarder

sysDescr

protected org.snmp4j.smi.OctetString sysDescr

sysOID

protected org.snmp4j.smi.OID sysOID

sysServices

protected org.snmp4j.smi.Integer32 sysServices

agentState

protected int agentState

defaultContext

protected org.snmp4j.smi.OctetString defaultContext

defaultPersistenceProvider

protected DefaultMOPersistenceProvider defaultPersistenceProvider

configFileURI

protected java.lang.String configFileURI
Constructor Detail

BaseAgent

protected BaseAgent(java.lang.String configURI)
Creates a base agent with a DefaultMOServer as MOServer. To use a different server implementation, modify the server member after construction.

Parameters:
configURI - the URI of the config file holding persistent data for this agent. If persistent data is not used then set this parameter to null.

BaseAgent

protected BaseAgent(java.io.File bootCounterFile,
                    java.io.File configFile,
                    CommandProcessor commandProcessor)
Creates a base agent with boot-counter, config file, and a CommandProcessor for processing SNMP requests.

Parameters:
bootCounterFile - a file with serialized boot-counter information (read/write). If the file does not exist it is created on shutdown of the agent.
configFile - a file with serialized configuration information (read/write). If the file does not exist it is created on shutdown of the agent.
commandProcessor - the CommandProcessor instance that handles the SNMP requests.
Method Detail

init

public void init()
          throws java.io.IOException
Initialize transport mappings, message dispatcher, basic MIB modules, proxy forwarder, VACM and USM security, and custom MIB modules and objects provided by sub-classes.

Throws:
java.io.IOException - if initialization fails because transport initialization fails.

initConfigMIB

protected void initConfigMIB()

getContext

protected org.snmp4j.smi.OctetString getContext(MOGroup mibGroup)
This method can be overwritten by a subagent to specify the contexts each MIB module (group) will be registered to.

Parameters:
mibGroup - a group of ManagedObjects (i.e., a MIB module).
Returns:
the context for which the module should be registered.
Since:
1.1

registerSnmpMIBs

protected void registerSnmpMIBs()
Register the basic MIB modules at the agent's MOServer.


unregisterSnmpMIBs

protected void unregisterSnmpMIBs()
Unregister the basic MIB modules from the agent's MOServer.


registerManagedObjects

protected abstract void registerManagedObjects()
Register additional managed objects at the agent's server.


unregisterManagedObjects

protected abstract void unregisterManagedObjects()
Unregister additional managed objects from the agent's server.


setupDefaultProxyForwarder

protected void setupDefaultProxyForwarder()
Creates and registers the default proxy forwarder application (ProxyForwarderImpl).


loadConfig

public void loadConfig(int importMode)
Loads the configuration using the specified import mode from the set config file.

Parameters:
importMode - one of the import modes defined by ImportModes.

saveConfig

public void saveConfig()
Save the current (serializable) managed object configuration into the config file.


addShutdownHook

protected void addShutdownHook()
Adds a shutdown hook that saves the internal config into the config file when a SIGTERM (Ctrl-C) is terminating the agent.


finishInit

protected void finishInit()
Finishes intialization of the agent by connecting server and command processor, setting up USM, VACM, notification targets, and finally sending a coldStart notification to configured targets.


sendColdStartNotification

protected void sendColdStartNotification()

run

public void run()
Starts the agent by let it listen on the configured SNMP agent ports (transpot mappings).

Specified by:
run in interface java.lang.Runnable

stop

public void stop()
Stops the agent by closing the SNMP session and associated transport mappings.

Since:
1.1

initMessageDispatcher

protected void initMessageDispatcher()
Initializes the message dispatcher (MessageDispatcherImpl) with the transport mappings.


initSnmpSession

protected void initSnmpSession()

updateSession

protected void updateSession(org.snmp4j.Session session)
Updates all objects with a new session instance. This method must be overwritten, if non-default SNMP MIB instances are created by a subclass.

Parameters:
session - a SNMP Session instance.

updateEngineBoots

protected int updateEngineBoots()
Updates the engine boots counter and returns the actual value.

Returns:
the actual boots counter value.

getEngineBoots

protected int getEngineBoots()
Reads the engine boots counter from the corresponding input stream (file).

Returns:
the boots counter value read or zero if it could not be read.

setEngineBoots

protected void setEngineBoots(int engineBoots)

addUsmUser

protected abstract void addUsmUser(org.snmp4j.security.USM usm)
Adds all the necessary initial users to the USM.

Parameters:
usm - the USM instance used by this agent.

addNotificationTargets

protected abstract void addNotificationTargets(SnmpTargetMIB targetMIB,
                                               SnmpNotificationMIB notificationMIB)
Adds initial notification targets and filters.

Parameters:
targetMIB - the SnmpTargetMIB holding the target configuration.
notificationMIB - the SnmpNotificationMIB holding the notification (filter) configuration.

addViews

protected abstract void addViews(VacmMIB vacmMIB)
Adds initial VACM configuration.

Parameters:
vacmMIB - the VacmMIB holding the agent's view configuration.

addCommunities

protected abstract void addCommunities(SnmpCommunityMIB communityMIB)
Adds community to security name mappings needed for SNMPv1 and SNMPv2c.

Parameters:
communityMIB - the SnmpCommunityMIB holding coexistence configuration for community based security models.

initTransportMappings

protected void initTransportMappings()
                              throws java.io.IOException
Initializes the transport mappings (ports) to be used by the agent.

Throws:
java.io.IOException

getNotificationOriginator

public NotificationOriginator getNotificationOriginator()

setDefaultProxyForwarder

public void setDefaultProxyForwarder(ProxyForwarder defaultProxyForwarder)

setSysDescr

public void setSysDescr(org.snmp4j.smi.OctetString sysDescr)

setSysOID

public void setSysOID(org.snmp4j.smi.OID sysOID)

setSysServices

public void setSysServices(org.snmp4j.smi.Integer32 sysServices)

setAgent

public void setAgent(CommandProcessor agent)

setBootCounterFile

public void setBootCounterFile(java.io.File bootCounterFile)

setConfigFile

public void setConfigFile(java.io.File configFile)

setDefaultContext

public void setDefaultContext(org.snmp4j.smi.OctetString defaultContext)
Sets the default context for this base agent. By setting this value before any MIB modules have been registered at the internal server, the context for which the registration is performed can be changed. The default context is null which causes MIB objects to be virtually registered for all contexts.

Parameters:
defaultContext - the context for default MIB objects.
Since:
1.1

getDefaultProxyForwarder

public ProxyForwarder getDefaultProxyForwarder()

getSysDescr

public org.snmp4j.smi.OctetString getSysDescr()

getSysOID

public org.snmp4j.smi.OID getSysOID()

getSysServices

public org.snmp4j.smi.Integer32 getSysServices()

getAgent

public CommandProcessor getAgent()

getBootCounterFile

public java.io.File getBootCounterFile()

getConfigFile

public java.io.File getConfigFile()

getSnmp4jConfigMIB

public Snmp4jConfigMib getSnmp4jConfigMIB()

getSnmp4jLogMIB

public Snmp4jLogMib getSnmp4jLogMIB()

getSnmpCommunityMIB

public SnmpCommunityMIB getSnmpCommunityMIB()

getSnmpFrameworkMIB

public SnmpFrameworkMIB getSnmpFrameworkMIB()

getSnmpNotificationMIB

public SnmpNotificationMIB getSnmpNotificationMIB()

getSnmpProxyMIB

public SnmpProxyMIB getSnmpProxyMIB()

getSnmpTargetMIB

public SnmpTargetMIB getSnmpTargetMIB()

getSnmpv2MIB

public SNMPv2MIB getSnmpv2MIB()

getUsmMIB

public UsmMIB getUsmMIB()

getVacmMIB

public VacmMIB getVacmMIB()

getSession

public org.snmp4j.Snmp getSession()

getServer

public DefaultMOServer getServer()

getMPv3

public org.snmp4j.mp.MPv3 getMPv3()

getUsm

public org.snmp4j.security.USM getUsm()

getAgentState

public int getAgentState()
Returns the agent's state.

Returns:
one of the state's starting from STATE_CREATED to STATE_RUNNING.
Since:
1.1

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 is null which 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 empty OctetString should be used as default context instead.

Returns:
null or an OctetString (normally the empty string) denoting the context used for registering default MIBs.
Since:
1.1

Copyright 2005-2010 Frank Fock (SNMP4J.org)

Copyright © 2011 SNMP4J.org. All Rights Reserved.