Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. The code has been generated by AgenPro into a folder that can be used to further develop the agent software. Otherwise, modifications to the generated code cannot be (easily) integrated into regenerated code, for example if there is a MIB specification update.
    For this tutorial that gen-folder is C:\Users\myuser\Documents\agenpro4\generated\snmp4j\src\org\snmp4j\agent\tutorial. You can replace the name gen-folder in the following by this folder.

  2. Optionally: Install the latest SNMP4J.jar, SNMP4J-Agent.jar, and optionally SNMP4J-AgentX.jar in a lib directory. For this tutorial we use C:\Users\myuser\Documents\agenpro4\lib for that. You can dowload latest release JARs from the AGENT++ Maven repository at: https://oosnmp.net/dist/release.
  3. Download and install (unpack) the AgenPro Maven Plugin ZIP file and unpack it at C:\Users\myuser\Documents\agenpro4 (which is typically the AgenPro installation directory.
  4. Install Java JDK (the JRE is not sufficient, because you need the Java compiler).
  5. Optionally: Install Maven 3.x. Note: If you do not install Maven, you need to perform step 2 and manually compile the sources (not explained here).
  6. If you have already a main program for your SNMP4J-Agent ready, then continue with step ?? of the Step-by-step Guide below.

Step-by-step Guide

  1. Change your working directory to the AgenPro Maven Plugin installation directory: C:\Users\myuser\Documents\agenpro4.
  2. Change into the subdirectory agenpro-mvn-task\src\main\java\com\oosnmp\agenpro.  
  3. Copy the file Agent.java to the directory (package) where your agent's main should be located. For simplicity, we choose the gen-folder
  4. Change back to the AgenPro Maven Plugin installation directory.
  5. Change into the subdirectory agenpro-mvn-task\src\main\resources\com\oosnmp\agenpro.
  6. Copy the file AgentConfig.properties to the directory (package) where your agent's main should be located, i.e. the gen-folder
  7. Edit the copied file at the gen-folder location and insert the following code fragement in the import sectionOptionally: Only if you are using your own main agent program implementation, you need first to fix the import section as described in the box below and then add the following function registerMIBs() to that class:

    Code Block
    languagejava
    titleImport Statements for Integration
    // Alternatively you can also import the complete package you specified in AgenPro project wizard step 1:
    // import <package>.*; 
    import org.snmp4j.agent.tutorial.Modules;
    import org.snmp4j.agent.tutorial.Snmp4jAgentTutorialMib;

    If you are using your own main agent program implementation, you need first to fix the import section as described in the previous step and then add the following function to that class:

    Code Block
    languagejava
    titleCode to Register Generated MIBs
      /**
       * Register your own MIB modules in the specified context of the agent.
       * The {@link MOFactory} provided to the <code>Modules</code> constructor
       * is returned by {@link #getFactory()}.
       */
      protected void registerMIBs()
      {
        if (modules == null) {
          modules = new Modules(getFactory());
        }
        try {
          modules.registerMOs(server, null);
        }
        catch (DuplicateRegistrationException drex) {
          logger.error("Duplicate registration: "+drex.getMessage()+"."+
                       " MIB object registration may be incomplete!", drex);
        }
      }
    

    Then call the above function in your agent initialization code before the agent is actually started with agent.run():

    Code Block
    languagejava
    titleCall Module Registration
      public void run() {
        // initialize agent before registering our own modules
        agent.initialize();
        // this requires sysUpTime to be available.
        registerMIBs(); // <---- here the MIB module registration is called
        // add proxy forwarder
        agent.setupProxyForwarder();
        // now continue agent setup and launch it.
        agent.run();
      }
    
  8. Compile the sources with Maven:

    Code Block
    languagejava
    titleAgent Compilation
    cd C:
    cd \Users\myuser\Documents\agenpro4\generated\snmp4j\src\org\snmp4j\agent\tutorial
    javac -cp ..\..\..\..\..\lib\* *.java
    mvn clean install
  9. Run the agent:

    Code Block
    languagebash
    java -jar target\tutorial-1.0.0-SNAPSHOT-jar-with-dependencies.jar udp:0.0.0.0/161

    or alternatively without Maven supportRun the agent:

    Code Block
    languagejava
    titleRun the Agent
    cd ..\..\..\..
    java -cp .;..\lib\* org.snmp4j.agent.tutorial.Agent udp:0.0.0.0/161

...