Skip navigation links

Package com.snmp4j.smi

Provides classes and interfaces for parsing, compiling, loading, and using Management Information Base (MIB) specifications written in Structure of Management Information (SMI) version 1 and 2 with SNMP4J to format and parse SNMP data.

See: Description

Package com.snmp4j.smi Description

Provides classes and interfaces for parsing, compiling, loading, and using Management Information Base (MIB) specifications written in Structure of Management Information (SMI) version 1 and 2 with SNMP4J to format and parse SNMP data.

The SmiManager class implements the SNMP4J interfaces OIDTextFormat and VariableTextFormat and thus provides MIB information for SNMP4J with a tight integration.

To setup a SmiManager, either a MIB repository directory for exclusive use by SNMP4J-SMI or an implementation of the RepositoryDriver interface is needed.

The following UML package diagram illustrates the dependencies between the classes of the core SNMP4J-SMI API. Users of the API normally only need to use the com.snmp4j.smi and the com.snmp4j.smi.util packages directly.

SNMP4J-SMI UML Class Diagram

SmiManager Creation

Integration with SNMP4J

The integration of the MIB parser (SMI compiler) with SNMP4J is astonishingly simple. You basically need only three lines of code to be executed in your application before initializing SNMP4J:

     /* Replace 'null' with your license key after evaluation: */
    SmiManager smiManager = new SmiManager(null, new File("/myMibRepositoryDir"));
    // Alternatively, if you cannot create a persistent directory on the target system:
    //SmiManager smiManager = new SmiManager(null, new MemRepositoryDriver());

    // Register the SmiManager as OID and VariableBinding formatter with SNMP4J:
    SNMP4JSettings.setOIDTextFormat(smiManager);
    SNMP4JSettings.setVariableTextFormat(smiManager);

Initially a SmiManager does not know of any MIB modules other than SNMPv2-CONF, SNMPv2-SMI, and SNMPv2-TC for SMIv2 and RFC-1212, RFC-1215, and RFC1155-SMI for SMIv1. Once SmiManager knows a MIB module, it can:

  1. format OIDs with object names instead of numerical notation: "sysDescr.0" instead "1.3.6.1.2.1.1.1.0".
  2. parse OIDs from object names
  3. format variable bindings according to their SMI syntax and display hint: "ifAdminStatus.4 = up(1)" instead "1.3.6.1.2.1.2.2.1.7.4 = 1"
  4. parse variable bindings from a text representation according to their SMI syntax and display hint.

To make a MIB module available to the SmiManager and thus to SNMP4J, two steps are necessary:

  1. Compile the MIB into the MIB repository of the SmiManager.
  2. Load the MIB module into the internal MIB cache.
Parsing and compiling a MIB specification text file into an internal format of the MIB repository is more effort (CPU) and time consuming than just loading the internal format into memory. The major difference is also, that during compilation all dependent MIB modules need to be parsed too (if not yet available in the cache).

To compile one or more MIB specification files into the internal MIB repository call one of the compile methods. First the simplest variant is shown and second the most complex but also the most flexible:

    // Easy MIB compilation:
    try {
        // Compile the MIB modules in a MIB file:
        String[] moduleNames = smiManager.compile(new File("C:\mymibfile.txt"));
        // Load compiled MIB modules into memory:
        for (String moduleName : moduleNames) {
            smiManager.loadModule(moduleName);
        }
    } catch (SmiParseException pex) {
        pex.printStacktrace();
    }

    // More complex but flexible:
    // 1. Create the input streams. Here two variants are shown and used in a single example:
    InputStream inputStreamSMIv2 = MyMibLoader.class.getResourceAsStream("SMIv2.zip");
    InputStream inputStreamAGENTPP = new FileInputStream("AGENTPP-CONFIG-MIB.txt");
    // 2. Implement a CompilationMonitor to monitor the progress. Here a very simple example that logs out the progress
    // messages:
    CompilationMonitor compilationMonitor = new CompilationMonitor() {
      public boolean loadingProgress(String fileName, int current, int maxCount) {
        System.out.println("Loading "+fileName+" "+current+"/"+maxCount);
        return true;
      }
      public boolean sortingProgress(String fileName, int current, int maxCount) {
        System.out.println("Sorting "+fileName+" "+current+"/"+maxCount);
        return true;
      }
      public boolean compilationProgress(String moduleName, int current, int maxCount) {
        System.out.println("Compiling "+moduleName+" "+current+"/"+maxCount);
        return true;
      }
    };
    // 3. Create the NamedInputStreams:
    List<CompilationResult> result =
        smiManager.compile(new NamedInputStream[] {
            new NamedInputStream(inputStreamSMIv2, "SMIv2", NamedInputStream.ZipFormat.ZIP),
            new NamedInputStream(inputStreamAGENTPP, "AGENTPP", NamedInputStream.ZipFormat.none),
        }, compilationMonitor, true, true, false);
    inputStreamSMIv2.close();
    inputStreamAGENTPP.close();
    // 4. Evaluate the compilation results:
    System.out.println(result.toString());
If you have already compiled MIB modules in your repository and you want to make them available to SNMP4J, simply load them by their MIB module name with SmiManager.loadModule(java.lang.String):
    smiManager.loadModule("SNMPv2-MIB");
    // Note: The SMIv1 RFC1213-MIB has been deprecated and replaced by several new SMIv2 MIBs, but you can still load
    // it if necessary. However, SNMP4J-SMI will use the SMIv2 object definitions instead SMIv1, if both are loaded:
    smiManager.loadModule("RFC1213-MIB");
A loaded MIB module can be unloaded to free memory by using the SmiManager.unloadModule(java.lang.String) method:
    smiManager.unloadModule("SNMPv2-MIB");
To remove a MIB module from the MIB repository, use the method SmiManager.deleteModule(java.lang.String, boolean). With the boolean option set to true, the deletion of a MIB module can be forced even if there are other MIB modules that depend on that module. Forcing the deletion can make sense if a repository is cleared for example. The MIB modules that depend on a deleted MIB module might get useless or might cause runtime errors if used.

In any case, the MIB module will not be unloaded from the cache.

    List<String> dependentModules = smiManager.deleteModule("SNMPv2-MIB", false);
    if (!dependentModules.isEmpty()) {
        System.out.println("Deletion failed because the following dependent modules exist: "+dependentModules);
    }
Skip navigation links
Copyright 2001-2018 Frank Fock (snmp4j.com)

Copyright © 2018 SNMP4J.com. All rights reserved.