Class SampleAgent

java.lang.Object
org.snmp4j.agent.db.sample.SampleAgent
All Implemented Interfaces:
org.snmp4j.agent.AgentStateListener<org.snmp4j.agent.AgentConfigManager>

public class SampleAgent extends Object implements org.snmp4j.agent.AgentStateListener<org.snmp4j.agent.AgentConfigManager>
The SampleAgent uses an AgentConfigManager instance to create a minimal SNMP agent using the configuration defined by SampleAgentConfig.properties in this package. That properties file defines the initial content of the registered MIB objects of this agent which may differ from the hard coded defaults.

This SampleAgent uses the MOXodusPersistenceProvider to persistently store ManagedObjects. It assumes that all managed objects that are non-volatile are implementing the RandomAccessManagedObject interfaces.

The agent uses the console logging to log messages.

Since:
3.0
Version:
3.9.0
Author:
Frank Fock
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected org.snmp4j.agent.AgentConfigManager
    The agent configuration manager that controls and runs this sample agent.
    static final String
    The ArgumentParser format string describing the supported command line options.
    static final String
    The ArgumentParser format string describing the required command line parameters.
    protected org.snmp4j.agent.DefaultMOServer
    The managed object server that holds the managed objects served by this agent.
    protected Properties
    The table size limits applied to the agent's tables, keyed by table OID.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    SampleAgent(Map<String, List<Object>> args, org.snmp4j.agent.audit.AgentAuditPolicy auditPolicy)
    Creates a new SampleAgent from the parsed command line arguments.
    protected
    SampleAgent(Map<String, List<Object>> args, org.snmp4j.agent.MOServer[] moServers, MOXodusPersistence xodusPersistence, Properties tableSizeLimits, org.snmp4j.agent.io.ImportMode importMode, org.snmp4j.agent.audit.AgentAuditPolicy auditPolicy)
    Creates a new SampleAgent with explicitly provided servers, persistence, and table size limits.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    addListenAddresses(org.snmp4j.MessageDispatcher md, List<Object> addresses)
    Parses the given address strings and adds a matching TransportMapping for each to the provided MessageDispatcher.
    void
    agentStateChanged(org.snmp4j.agent.AgentConfigManager agentConfigManager, org.snmp4j.agent.AgentState newState)
    The agent state has changed to the new state as provided.
    protected org.snmp4j.agent.io.MOInputFactory
    createMOInputFactory(String configFilename, org.snmp4j.agent.io.ImportMode importMode)
    Creates a MOInputFactory that supplies the initial agent configuration from a properties file.
    protected org.snmp4j.agent.mo.MOFactory
    Get the MOFactory that creates the various MOs (MIB Objects).
    protected static Properties
    getTableSizeLimitsProperties(Map<String, List<Object>> args, org.snmp4j.agent.audit.AgentAuditPolicy auditPolicy)
    Read the content of the SampleAgentTableSizeLimits.properties file.
    static void
    main(String[] args)
    Runs a sample agent with a default configuration defined by SampleAgentConfig.properties.
    protected void
    Register your own MIB modules in the specified context of the agent.
    void
    run()
    Initializes the agent, registers the sample MIB modules, applies the table size limits, registers a shutdown hook for persisting the configuration, and finally starts the agent.
    protected void
    setupAgent(Map<String, List<Object>> args, org.snmp4j.agent.MOServer[] moServers, org.snmp4j.agent.io.MOInputFactory configurationFactory, List<Object> listenAddress, String dhKickstartInfoPath, org.snmp4j.agent.audit.AgentAuditPolicy auditPolicy)
    Sets up the AgentConfigManager including its message dispatcher, listen addresses, optional Diffie-Hellman kickstart parameters, and persistence provider.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • COMMAND_LINE_OPTIONS

      public static final String COMMAND_LINE_OPTIONS
      The ArgumentParser format string describing the supported command line options.
      See Also:
    • COMMAND_LINE_PARAMS

      public static final String COMMAND_LINE_PARAMS
      The ArgumentParser format string describing the required command line parameters.
      See Also:
    • agent

      protected org.snmp4j.agent.AgentConfigManager agent
      The agent configuration manager that controls and runs this sample agent.
    • server

      protected org.snmp4j.agent.DefaultMOServer server
      The managed object server that holds the managed objects served by this agent.
    • tableSizeLimits

      protected Properties tableSizeLimits
      The table size limits applied to the agent's tables, keyed by table OID.
  • Constructor Details

    • SampleAgent

      public SampleAgent(Map<String, List<Object>> args, org.snmp4j.agent.audit.AgentAuditPolicy auditPolicy)
      Creates a new SampleAgent from the parsed command line arguments. Initial configuration is loaded from the configuration properties file only if there is no persistent data for the default context.
      Parameters:
      args - the parsed command line arguments as returned by the ArgumentParser.
      auditPolicy - the audit policy to use for the agent.
    • SampleAgent

      protected SampleAgent(Map<String, List<Object>> args, org.snmp4j.agent.MOServer[] moServers, MOXodusPersistence xodusPersistence, Properties tableSizeLimits, org.snmp4j.agent.io.ImportMode importMode, org.snmp4j.agent.audit.AgentAuditPolicy auditPolicy)
      Creates a new SampleAgent with explicitly provided servers, persistence, and table size limits. This constructor is intended for subclasses and tests that need full control over the agent's collaborators.
      Parameters:
      args - the parsed command line arguments as returned by the ArgumentParser.
      moServers - the managed object servers the agent registers its managed objects with.
      xodusPersistence - the MOXodusPersistence used to persistently store managed objects.
      tableSizeLimits - the table size limits to apply to the agent's tables.
      importMode - the ImportMode that controls how the initial configuration is applied.
      auditPolicy - the AgentAuditPolicy to use for the agent.
  • Method Details

    • getTableSizeLimitsProperties

      protected static Properties getTableSizeLimitsProperties(Map<String, List<Object>> args, org.snmp4j.agent.audit.AgentAuditPolicy auditPolicy)
      Read the content of the SampleAgentTableSizeLimits.properties file.
      Parameters:
      args - the command line arguments. If the arguements contain the key "ts" the default file name is replaced by the key's value.
      auditPolicy - the audit policy to use or null if no audit policy is to be used.
      Returns:
      a Properties instance with the table size properties read from the given file or an empty Properties instance if the file could not be read.
    • createMOInputFactory

      protected org.snmp4j.agent.io.MOInputFactory createMOInputFactory(String configFilename, org.snmp4j.agent.io.ImportMode importMode)
      Creates a MOInputFactory that supplies the initial agent configuration from a properties file.
      Parameters:
      configFilename - the path to the configuration properties file or null to use the bundled SampleAgentConfig.properties resource.
      importMode - the ImportMode that controls how the configuration is applied to existing data.
      Returns:
      a MOInputFactory providing the parsed configuration.
    • setupAgent

      protected void setupAgent(Map<String, List<Object>> args, org.snmp4j.agent.MOServer[] moServers, org.snmp4j.agent.io.MOInputFactory configurationFactory, List<Object> listenAddress, String dhKickstartInfoPath, org.snmp4j.agent.audit.AgentAuditPolicy auditPolicy)
      Sets up the AgentConfigManager including its message dispatcher, listen addresses, optional Diffie-Hellman kickstart parameters, and persistence provider.
      Parameters:
      args - the parsed command line arguments as returned by the ArgumentParser.
      moServers - the managed object servers the agent registers its managed objects with.
      configurationFactory - the MOInputFactory providing the initial configuration, or null if none.
      listenAddress - the list of transport addresses the agent should listen on.
      dhKickstartInfoPath - the path to the Diffie-Hellman kickstart parameters file, or null if none.
      auditPolicy - the AgentAuditPolicy to use for the agent.
    • addListenAddresses

      protected void addListenAddresses(org.snmp4j.MessageDispatcher md, List<Object> addresses)
      Parses the given address strings and adds a matching TransportMapping for each to the provided MessageDispatcher.
      Parameters:
      md - the message dispatcher the transport mappings are added to.
      addresses - the list of address strings (e.g. udp:0.0.0.0/161) to listen on.
    • run

      public void run()
      Initializes the agent, registers the sample MIB modules, applies the table size limits, registers a shutdown hook for persisting the configuration, and finally starts the agent.
    • getFactory

      protected org.snmp4j.agent.mo.MOFactory getFactory()
      Get the MOFactory that creates the various MOs (MIB Objects).
      Returns:
      a DefaultMOFactory instance by default.
      Since:
      1.3.2
    • registerMIBs

      protected void registerMIBs()
      Register your own MIB modules in the specified context of the agent. The MOFactory provided to the Modules constructor is returned by getFactory().
    • main

      public static void main(String[] args)
      Runs a sample agent with a default configuration defined by SampleAgentConfig.properties. A sample command line is:
      -c SampleAgent.cfg -bc SampleAgent.bc udp:127.0.0.1/4700 tcp:127.0.0.1/4700
      
      Parameters:
      args - the command line arguments defining at least the listen addresses. The format is -c[s{=SampleAgent.cfg}] -bc[s{=SampleAgent.bc}] +ts[s] +cfg[s] #address[s&lt;(udp|tcp|tls):.*[/[0-9]+]?&gt;] ... For the format description see ArgumentParser.
    • agentStateChanged

      public void agentStateChanged(org.snmp4j.agent.AgentConfigManager agentConfigManager, org.snmp4j.agent.AgentState newState)
      The agent state has changed to the new state as provided.
      Specified by:
      agentStateChanged in interface org.snmp4j.agent.AgentStateListener<org.snmp4j.agent.AgentConfigManager>
      Parameters:
      agentConfigManager - the agent's configuration manager. Use this object to access all agent resources, if needed to process this event.
      newState - the new state of the agent. Although the listener may advance to agent state further, it is not recommended to do so, because the AgentConfigManager will do it anyway.