Package com.snmp4j.smi
The SmiManager
class implements the SNMP4J interfaces
OIDTextFormat
and VariableTextFormat
and thus provides
MIB information for SNMP4J with a tight integration.
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.
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:
- format OIDs with object names instead of numerical notation: "sysDescr.0" instead "1.3.6.1.2.1.1.1.0".
- parse OIDs from object names
- 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"
- 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:
- Compile the MIB into the MIB repository of the SmiManager.
- Load the MIB module into the internal MIB 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); }
-
InterfaceDescriptionThe
CompilationMonitor
interface is implemented by classes that are supposed to monitor the progress of a multi MIB file compilation operation of theSmiManager
.The RepositoryDriver adapts between theSmiManager
and the (persistent) storage that holds the compiled MIB modules.TheSmiAgentCapabilities
interface represents a SMIv2 AGENT-CAPABILITIES statement.The SmiCompiler provides a simple yet complete interface to check, compile, store, and load MIB files content (i.e., MIB modules).TheSmiGroupCompliance
interface represents the GROUP or OBJECT clause of a MODULE-COMPLIANCE statement in SMIv2.TheSmiComplianceModule
interface defines the attributes for the "Module" element of the SMIv2 MODULE-COMPLIANCE macro.TheSmiErrorInfo
interface represents a syntax or semantic error in a SMI specification file.The SmiGroup represents a SMI OBJECT-GROUP or NOTIFICATION-GROUP object.The SmiImport represents an element within the IMPORTS construct of a SMIv1 or SMv2 MIB module.The SmiIndexInfo interface represents the meta information of a SMI INDEX clause element which are relevant for converting an OID index value to an INDEX object and vice versa.The SmiModule represents a SMIv1 or SMIv2 MIB MODULE-DEFINITION.TheSmiModuleCompliance
interface defines the attributes of the SMIv2 MODULE-COMPLIANCE macro.The SmiModule represents a SMIv2 MODULE-IDENTITY.The SmiNotificationType interface extends theSmiObject
by attributes specific to the SMI NOTIFICATION-TYPE and TRAP-TYPE constructs, for example the OBJECTS clause.The SmiObject interface represents the SMI information of a MIB object.TheSmiObjectVariation
interface defines the attributes that are included in MODULE-COMPLIANCE module parts (SmiComplianceModule
) OBJECT clause and is also used in the AGENT-CAPABILITIES module section.SmiObjectFilter<S extends SmiObject>The SmiObjectFilter can be used to filterSmiObject
s according to self defined criteria.The SmiObjectType interface extends theSmiObject
by attributes specific to the SMI OBJECT-TYPE construct, i.e.The SmiObjectTypeIndex represents an OBJECT-TYPE definition that is used as an INDEX element for a table.TheSmiObjectVariation
interface defines the attributes for the object variation part of an AGENT-CAPABILITIES statement.The SmiRevision represents the REVISION clause of a SMIv2 MODULE-IDENTITY construct.TheSmiSupportedModule
interface represents a SMIv2 MODULE clause of the AGENT-CAPABILITIES statement.The SmiSyntax represents the SYNTAX clause of OBJECT-TYPE or TEXTUAL-CONVENTION SMI constructs.The SmiSyntaxElement represents a enumerated value or a range restriction.The SmiTextualConvention represents the SMIv2 TEXTUAL-CONVENTION and the SMIv1 type assignment construct.Ths SmiValueType interface abstracts the common element of the OBJECT-TYPE and the TEXTUAL-CONVENTION SMI constructs. -
ClassDescriptionThe
CompilationResult
class holds the MIB module names successfully parsed for each MIB file or the parsing errors that were detected during compilation.The NamedInputStream class represents an input stream with a name to identify the stream.The RepositoryIO class is used to return theInputStream
orOutputStream
instances provided by aRepositoryDriver
instance to read or store a compiled MIB module from/into persistent storage.TheSmiError
class represents a syntax or semantic error in a SMI specification file.TheSmiManager
Pro class manages the Structure of Management Information (SMI) specifications.TheSmiSyntaxImpl
class represents the attributes of a SMI SYNTAX clause. -
Enum ClassDescriptionA supported ZIP file formatThe SmiMaxAccess enumerates the ACCESS values of SMIv2 for MODULE-COMPLIANCE and AGENT-CAPABILITIES statements, including the deprecated
SmiAccess.writeOnly
for backward-compatibility.The OverwriteMode defines whether existing MIB modules should be overwritten or not.The Strictness defines the number of syntax checks done on the sources.The TargetMode defines the target for the compiled MIB modules.TheSmiComplianceType
distinguishes between GROUP and OBJECT compliance definitions in MODULE-COMPLIANCE statements.The enumeration SmiGroupType distinguishes the type of a SMI group.Defines the format for non-printable strings in formatted object identifiers (OIDs).The default formats to be applied for displayingOctetString
s.The OIDFormat defines the formatting ofOID
values.The SmiStatus enumerates the values of the SMIv1 and v2 STATUS clause.The type of enumerated value.The SmiSyntaxType identifies one of four SYNTAX clause types of OBJECT-TYPE or TEXTUAL-CONVENTION SMI constructs.The SmiType defines the SMI construct (for exampleOBJECT-TYPE
) that defined the SMI object. -
ExceptionDescriptionThe
SmiParseException
provides information about violations of the Structure of Management Information (SMI) v1 or v2 standards.