You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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.

The code needed at minimum to use SNMP4J with SNMP4J-SMI OID and Variable value formatting and parsing support is:

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 compile textual MIB specifications into the SNMP4J-SMI repository directory, use:

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

 

  • No labels