public abstract class BaseAgent extends Object implements Runnable
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.Modifier and Type | Field and Description |
---|---|
protected CommandProcessor |
agent |
protected int |
agentState |
protected EngineBootsCounterFile |
bootCounterFile |
protected 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<? extends org.snmp4j.smi.Address>[] |
transportMappings |
protected org.snmp4j.security.USM |
usm |
protected UsmMIB |
usmMIB |
protected VacmMIB |
vacmMIB |
Modifier | Constructor and Description |
---|---|
protected |
BaseAgent(File bootCounterFile,
File configFile,
CommandProcessor commandProcessor)
Creates a base agent with boot-counter, config file, and a CommandProcessor
for processing SNMP requests.
|
protected |
BaseAgent(String configURI)
Creates a base agent with a
DefaultMOServer as MOServer . |
Modifier and Type | Method and Description |
---|---|
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 initialization 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.
|
File |
getBootCounterFile() |
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(File bootCounterFile) |
void |
setConfigFile(File configFile) |
void |
setDefaultContext(org.snmp4j.smi.OctetString defaultContext)
Sets the default context for this base agent.
|
void |
setDefaultProxyForwarder(ProxyForwarder defaultProxyForwarder) |
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.
|
public static final int STATE_CREATED
public static final int STATE_INIT_STARTED
public static final int STATE_INIT_FINISHED
public static final int STATE_RUNNING
public static final int STATE_STOPPED
protected SNMPv2MIB snmpv2MIB
protected SnmpFrameworkMIB snmpFrameworkMIB
protected SnmpTargetMIB snmpTargetMIB
protected SnmpNotificationMIB snmpNotificationMIB
protected SnmpProxyMIB snmpProxyMIB
protected SnmpCommunityMIB snmpCommunityMIB
protected Snmp4jLogMib snmp4jLogMIB
protected Snmp4jConfigMib snmp4jConfigMIB
protected UsmMIB usmMIB
protected VacmMIB vacmMIB
protected DefaultMOServer server
protected org.snmp4j.Snmp session
protected org.snmp4j.TransportMapping<? extends org.snmp4j.smi.Address>[] transportMappings
protected org.snmp4j.MessageDispatcherImpl dispatcher
protected CommandProcessor agent
protected org.snmp4j.mp.MPv3 mpv3
protected org.snmp4j.security.USM usm
protected EngineBootsCounterFile bootCounterFile
protected NotificationOriginator notificationOriginator
protected ProxyForwarder defaultProxyForwarder
protected org.snmp4j.smi.OctetString sysDescr
protected org.snmp4j.smi.OID sysOID
protected org.snmp4j.smi.Integer32 sysServices
protected int agentState
protected org.snmp4j.smi.OctetString defaultContext
protected DefaultMOPersistenceProvider defaultPersistenceProvider
protected String configFileURI
protected BaseAgent(String configURI)
DefaultMOServer
as MOServer
.
To use a different server implementation, modify the server
member
after construction.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
.protected BaseAgent(File bootCounterFile, File configFile, CommandProcessor commandProcessor)
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.public void init() throws IOException
IOException
- if initialization fails because transport initialization fails.protected void initConfigMIB()
protected org.snmp4j.smi.OctetString getContext(MOGroup mibGroup)
mibGroup
- a group of ManagedObject
s (i.e., a MIB module).protected void registerSnmpMIBs()
MOServer
.protected void unregisterSnmpMIBs()
MOServer
.protected abstract void registerManagedObjects()
protected abstract void unregisterManagedObjects()
protected void setupDefaultProxyForwarder()
ProxyForwarderImpl
).public void loadConfig(int importMode)
importMode
- one of the import modes defined by ImportModes
.public void saveConfig()
protected void addShutdownHook()
protected void finishInit()
protected void sendColdStartNotification()
public void run()
public void stop()
protected void initMessageDispatcher()
MessageDispatcherImpl
) with
the transport mappings.protected void initSnmpSession()
protected void updateSession(org.snmp4j.Session session)
session
- a SNMP Session instance.protected int updateEngineBoots()
protected int getEngineBoots()
protected abstract void addUsmUser(org.snmp4j.security.USM usm)
usm
- the USM instance used by this agent.protected abstract void addNotificationTargets(SnmpTargetMIB targetMIB, SnmpNotificationMIB notificationMIB)
targetMIB
- the SnmpTargetMIB holding the target configuration.notificationMIB
- the SnmpNotificationMIB holding the notification (filter)
configuration.protected abstract void addViews(VacmMIB vacmMIB)
vacmMIB
- the VacmMIB holding the agent's view configuration.protected abstract void addCommunities(SnmpCommunityMIB communityMIB)
communityMIB
- the SnmpCommunityMIB holding coexistence configuration for community
based security models.protected void initTransportMappings() throws IOException
IOException
- if an IO exception occurs while initializing the default transport mapping on all local IP addresses on
port 161.public NotificationOriginator getNotificationOriginator()
public void setDefaultProxyForwarder(ProxyForwarder defaultProxyForwarder)
public void setSysDescr(org.snmp4j.smi.OctetString sysDescr)
public void setSysOID(org.snmp4j.smi.OID sysOID)
public void setSysServices(org.snmp4j.smi.Integer32 sysServices)
public void setAgent(CommandProcessor agent)
public void setBootCounterFile(File bootCounterFile)
public void setConfigFile(File configFile)
public void setDefaultContext(org.snmp4j.smi.OctetString defaultContext)
null
which causes MIB objects to be virtually registered
for all contexts.defaultContext
- the context for default MIB objects.public ProxyForwarder getDefaultProxyForwarder()
public org.snmp4j.smi.OctetString getSysDescr()
public org.snmp4j.smi.OID getSysOID()
public org.snmp4j.smi.Integer32 getSysServices()
public CommandProcessor getAgent()
public File getBootCounterFile()
public File getConfigFile()
public Snmp4jConfigMib getSnmp4jConfigMIB()
public Snmp4jLogMib getSnmp4jLogMIB()
public SnmpCommunityMIB getSnmpCommunityMIB()
public SnmpFrameworkMIB getSnmpFrameworkMIB()
public SnmpNotificationMIB getSnmpNotificationMIB()
public SnmpProxyMIB getSnmpProxyMIB()
public SnmpTargetMIB getSnmpTargetMIB()
public SNMPv2MIB getSnmpv2MIB()
public UsmMIB getUsmMIB()
public VacmMIB getVacmMIB()
public org.snmp4j.Snmp getSession()
public DefaultMOServer getServer()
public org.snmp4j.mp.MPv3 getMPv3()
public org.snmp4j.security.USM getUsm()
public int getAgentState()
STATE_CREATED
to
STATE_RUNNING
.public org.snmp4j.smi.OctetString getDefaultContext()
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.null
or an OctetString
(normally the empty
string) denoting the context used for registering default MIBs.Copyright © 2016 SNMP4J.org. All rights reserved.