The Java Dynamic Management Kit provides a toolkit for developing SNMP agents and managers, which includes the SNMP MIB Compiler, mibgen. This is a Java technology-based SNMP MIB compiler which takes an SNMP MIB as input and outputs a set of Managed Beans. These MBeans implement the MIBs, enabling the Java Dynamic Management Kit agent to be managed by an SNMP manager.
The Java Dynamic Management Kit provides the mibgen compiler for compiling SNMP MIBs into Java source code. The mibgen compiler generates Java source code for agents and managers. The mibgen compiler takes as input a set of SNMP MIBs and generates MBeans that implement the MIBs.
The mibgen compiler is able to process:
Tables with cross references indexed across several MIBs
MIBs that contain both v1 and v2 definitions
The mibgen compiler does not support
embedded groups
row status for the creation/deletion of entries
To start mibgen, type the command for your operating environment:
In a Solaris operating environment:
prompt% installDir/SUNWjdmk/jdmk4.0/JDKversion/bin/mibgen [options] mib1 ... mibN |
In a Windows NT operating environment:
C:\> installDir\SUNWjdmk\jdmk4.0\JDKversion\bin\mibgen [options] mib1 ... mibN |
In order to invoke the java.com.sun.jdmk.tools.MibGen class you need to invoke java.com.sun.jdmk.tools.MibGen <options> <mib files>. The following options include:
mibgen<options><classes> |
where <options> include:
-n
Parses the MIB files without generating code.
-d<dir>
Generates code in the specified target directory.
-tp pkgName
Generates code within the specified Java package.
-desc
Includes the "DESCRIPTION" clause of OBJECT-TYPE as comment in generated code.
-mo
Only generated code for the metadata definitions file for the MIB variables (SnmpOidTable file);.
-mc
Specify to not use the default MIB-CORE definitions file provided with JDMK;In this case, the user must specify the MIB-CORE definitions file as one of the <mib files>.
-a
Generate code for all the MIB files.
-p<prefix>
Use the specified prefix for naming generated classes.
-help
Print this help message.
The mibgen compiler also generates the Java source code required for representing a whole MIB in an SNMP manager.
mibgen parses an SNMP MIB and generates the following:
an MBean representing the whole MIB
a class representing the metadata definitions for the MIB variables
classes representing SNMP groups/entities as MBeans (and a metadata class for each instance generated)
classes representing SNMP tables
classes representing SNMP enumerated types
MBeans generated by mibgen need to be updated to provide the definitive implementation. For more information, see the corresponding section in the Java Dynamic Management Kit 4.0 Tutorial.
The mibgen compiler generates a Java file that represents and initializes the whole MIB. This class extends the class SnmpMib, which is an abstract Java class in the com.sun.jdmk.agent package and is a logical abstraction of an SNMP MIB. The SNMP adaptor uses the SnmpMib class to implement agent behavior. You can edit the generated file if, for example, you want to remove support for a whole group from your MIB.
Files representing whole MIBs are named using the module name specified in the MIB definition. Special characters are removed by mibgen and replaced with an underscore character (_).
The mibgen compiler generates a Java file that contains the code required for representing a whole MIB in an SNMP manager OidTable. This class extends the com.sun.jdmk.snmp.snmpOidTableSupportclass which implements the javax.management.snmp.snmpOidTable class and maintains a database of MIB variables. A name can be resolved against the database. This file can be used by both the agent and the manager API. It contains metadata definitions for the compiled MIB. The metadata can then be loaded into the SNMP OidTable.
The file is always generated when mibgen is invoked and is called MiBnameOidTable. The ---mo option generates only the MiBnameOidTable file.
For each SNMP group defined in the MIB, mibgen generates:
A skeletal MBean, with it's interface
A metadata file
The mibgen compiler generates an MBean for each group defined in the MIB. These skeletal MBeans need to be completed by adding implementation-specific code (access methods). The generated code is initialized with default values for the different MIB variables. Therefore, if you compile the generated code directly, you will obtain a running agent. In this case, values returned by the agent when querying the MIBs will not be meaningful.
MBeans generated from groups are named using the group names specified in the MIB definition.
As well as generating skeletal MBeans to represent each group, mibgen generates a metadata file. The metadata file contains Java source code that provides the SNMP view of the MBean. Metadata files do not need to be modified. For metadata files, the Meta suffix is added.
For each SNMP table defined in the MIB, mibgen generates:
A class containing the SNMP view of the table
A skeletal MBean representing a table entry, with it's interface
A metadata file corresponding to the skeletal MBean
This class contains all the management of the table index. The class is also prefixed with Table followed by the name of the table.
For each table in a MIB, mibgen generates an MBean representing a table entry. These skeletal MBeans need to be completed by adding implementation specific code (access methods). The generated code is initialized with default values for table entry fields. Therefore, if you compile the generated code directly, you will obtain a running agent. In this case, values returned by the agent when querying the MIBs will not be meaningful.
MBeans generated from table entries are named using the entry names specified in the MIB definition.
In addition to generating skeletal MBeans to represent each table entry, mibgen generates a Java file containing the SNMP view of the MBean. Metadata files do not need to be modified. For metadata files, the Meta suffix is added.
The mibgen compiler generates a specific class for each enumerated type defined in the MIB. This class contains all the possible values defined in the enumerated type. The generated class extends the generic class Enumerated, defined in the com.sun.jdmk package. The HTML adaptor is able to use the Enumerated class to display all the labels contained in an enumeration. The mibgen tool is able to handle enumerated types defined as part of a type definition or in-line definition.
Generated code representing SNMP enumerated types is prefixed with Enum followed by the type name or the variable name for in-line definition.
The mibgen tool has an option ---p <prefix> that allows you to prefix the names of all generated files with a specific string.
In MIB II, TCP connection states are represented by an enumeration containing all the possible states for a TCP connection. The mibgen tool generates a Java class named EnumTcpConnState to represent the enumeration.
For each group defined in your MIB, mibgen generates an MBean. Each variable in the group is represented as a property of the MBean. If the MIB allows read access to a variable, mibgen generates a getter method for the corresponding property. If the MIB allows write access to a variable, mibgen generates a setter method for the property. Tables are seen as indexed properties whose type corresponds to the table entry type. The SNMP view of the table is maintained by a specific table object contained in the generated MBean. The mibgen compiler maps the MIB variable syntax to a well-defined Java type.
The MBeans that mibgen generates do not have any dependency on specific SNMP objects. Therefore, they can very easily be browsed or integrated into the different Java Dynamic Management Kit components. The translation between the SNMP syntax and the MBean syntax is performed by the metadata.
To change the Java type of a specific MIB variable within a generated MBean, edit the metadata file associated with the group that contains the variable.
MBeans generated by mibgen have to be updated to provide the definitive implementation. The generated code is an operational agent, thus, it can be compiled, executed, and tested without any modification. If MBeans are generated too often, use sub-classes to reduce major modifications in your code.
public class g1 implements g1MBean, Serializable {
protected Integer myVar = new Integer (1);
public g1(SnmpMib myMib) {
{
public Integer getMyVar() throws SnmpStatusException {
return myVar;
}
public void setMyVar(Integer x) throws SnmpStatusExeption {
myVar = x;
}
You need to modify the skeletal MBean to implement your MIB behaviour. For information about developing an SNMP agent, SNMP Manager, SNMP API and SNMP Proxy, see the corresponding section in Java Dynamic Management Kit 4.0 Tutorial.