Java Dynamic Management Kit 4.1 Tools Reference

Chapter 2 SNMP MIB Compiler (mibgen)

The mibgen Compiler

The Java Dynamic Management Kit provides a toolkit for developing SNMP agents and managers. This toolkit includes the SNMP MIB Compiler, mibgen, which is used for compiling SNMP MIBs into Java source code for agents and managers. mibgen is a Java technology-based SNMP MIB compiler which takes an SNMP MIB as input and outputs a set of Managed Beans. These MBeans can be customized to implement the MIBs, enabling the Java Dynamic Management Kit agent to be managed by an SNMP manager.

The mibgen compiler is able to process:

The mibgen compiler is able to parse table definitions containing objects of type RowStatus, but the semantics of RowStatus is ignored by the toolkit. No special support is provided in this case.

How to Start the mibgen Compiler

To start mibgen, type the command for your operating environment:

mibgen Options

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>.


mibgen<options><mib files>

where <options> include:

-n

Parses the MIB files without generating code.

-d <dir>

Generates code in the specified target directory.

-tp pkgName (target package)

Generates code within the specified Java package.

-desc

Includes the "DESCRIPTION" clause of OBJECT-TYPE as comment in generated code.

-mo (manager-only)

Generates code for the SNMP manager only, that is the metadata file for the MIB variables (SnmpOidTable file). By default, mibgen generates code for both SNMP agents and managers. By selecting the -mo option, you enable mibgen to generate code for only the manager and not for agents.

-mc (MIB-CORE)

Specify to not use the default MIB-CORE definitions file provided with the Java Dynamic Management Kit; 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: without this option, the java code is generated only for the first mib file. In this case, the following mib files are simply used to resolve mib definitions.

-p <prefix>

Use the specified prefix for naming generated classes.

-help

Prints this help message:

Note:

The order followed by mibgen to find the MIB_CORE definitions file is:

  1. The user MIB_CORE definitions file specified in the <mib files> using the -mc mibgen option.

  2. The user command line parameter specified using the -Dmibcore.file java option.

  3. The default MIB_CORE definitions file provided with jdmk under <INSTALL_DIR>/etc/mibgen (mib_core.txt). To succeed, you must be able to derive the jdmk installation directory from the CLASSPATH environment variable. Otherwise, mibgen will look for the mib_core.txt file under <CURRENT_DIR>/etc/mibgen.

Output From the mibgen Compiler

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:

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.1 Tutorial.

Representation of the Whole MIB

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.snmp.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 (_).

Representation of the Whole MIB in an SNMP OidTable

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.snmpOidTableSupport class 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. This file is the only file generated for SNMP managers. All other files are dedicated to the SNMP agents.

Classes Representing SNMP Groups

For each SNMP group defined in the MIB, mibgen generates:

Skeletal MBeans Representing Groups

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. If the MIB specifies a default value for an SNMP variable, this value will be used to initialize the corresponding variable in the MBean skeleton. 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 be default values or meaningless values, if no default value has been provided in the MIB file for the variable.

MBeans generated from groups are named using the group names specified in the MIB definition.

Metadata Files

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.

Classes Representing SNMP Tables

For each SNMP table defined in the MIB, mibgen generates:

Class Containing the SNMP View of a Table

This class contains all the management of the table index. The class is also prefixed with Table followed by the name of the table.

Skeletal MBeans Representing SNMP Table Entries

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.

Metadata Files

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.

Classes Representing SNMP Enumerated Types

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.


Note -

The mibgen tool has an option -p <prefix> that allows you to prefix the names of all generated files with a specific string.


For example, 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.

Information Mapping

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.


Note -

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.


Example 2-1 Implementing a skeletal MBean

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 behavior. For information about developing an SNMP agent, SNMP Manager, SNMP API and SNMP Proxy, see the corresponding section in Java Dynamic Management Kit 4.1 Tutorial.