Versions Compared

Key

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

 

The integration of SNMP4J-SMI in SNMP4J is very easy and complete. If you create an OID or Variable value from a String value or converting them using the toString() or format() methods, SNMP4J-SMI will parse and format the values as human readable object names and value combinations on-the-fly.

...

Code Block
static {
    SmiManager smiManager = new SmiManager("myLicenseKey", new File("myEmptyDirectory"));
    SNMP4JSettings.setOIDTextFormat(smiManager);
    SNMP4JSettings.setVariableTextFormat(smiManager);
    // If you need to disable full index formatting, then choose a different format below and uncomment the line:
    // smiManager.setOidFormat(OIDFormat.ObjectNameAndDecodedIndex4RoundTrip);
 }

To Sample code to compile textual MIB specifications into the SNMP4J-SMI repository directory, use:

Code Block
File f = new File(fname);
if (f.exists() && f.canRead()) {
  File[] files;
  if (f.isDirectory()) {
    files = f.listFiles();
  }
  else {
    files = new File[1];
    files[0] = f;
  }
  // Compile MIB files without monitor (null), load them directly into the MIB repository in memory,
  // update existant MIB modules, and do not ignore syntax errors:
  List<CompilationResult> results = smiManager.compile(files, null, true, true, false);
  int ok = 0;
  for (CompilationResult result : results) {
    if (result.getSmiErrorList() == null) {
      ok += result.getModuleNames().size();
    }
  }
  out.println("" + f + " contains ");
  out.println(" " + ok + " syntactically correct MIB modules and");
  out.println(" " + (results.size() - ok) +
      " MIB modules with errors.");
  String lastFile = null;
  for (CompilationResult result : results) {
    List<SmiError> smiErrors = result.getSmiErrorList();
    String n = result.getFileName();
    n = n.substring(n.lastIndexOf('/') + 1);
    if ((lastFile == null) || (!lastFile.equals(n))) {
      out.println("------ " + n + " ------");
      lastFile = n;
    }
    if (smiErrors != null) {
      for (int j = 0; j < smiErrors.size(); j++) {
        SmiError error = smiErrors.get(j);
        String txt = n + " #" + (j + 1) + ": " + error.getMessage();
        out.println(txt);
      }
    }
    else {
      String txt = n + ": " + "OK";
      out.println(txt);
    }
  }
}
else {
  out.println("Cannot access MIB file '" + fname + "'");
}

If you specify more than one MIB file, the MIB modules in those files will be automatically processed in the order of their import dependencies This avoids manual sorting or syntax errors because of unresolved IMPORTs.

Loading MIB Modules

Sample code to load (already compiled) a MIB module from the MIB repository directory:

Code Block
smiManager.loadModule("SNMP-VIEW-BASED-ACM-MIB");
smiManager.loadModule("SNMPv2-MIB");

The SmiManager will also load all MIB objects that are imported by the specified modules! You do not have to implement this logic yourself

Formatting and Parsing Examples

Code Block
OID testPrivOID = new OID("1.3.6.1.4.1.4976");
assertEquals("enterprises.4976", testPrivOID.toString());

 
OID vacmAccessContextMatch = new OID("1.3.6.1.6.3.16.1.4.1.4.7.118.51.103.114.111.117.112.0.3.1");
assertEquals("vacmAccessContextMatch.\"v3group\".\"\".3.'noAuthNoPriv(1)'", vacmAccessContextMatch.toString());
assertEquals("vacmAccessContextMatch.\"v3group\".\"\".3.'noAuthNoPriv(1)'", vacmAccessContextMatch.format());
assertEquals(vacmAccessContextMatch, new OID("vacmAccessContextMatch.\"v3group\".\"\".3.'noAuthNoPriv(1)'"));