Java Dynamic Management Kit 5.0 Tools Reference

Chapter 2 SNMP MIB Compiler (mibgen)

The Java Dynamic Management Kit (DMK) 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. The mibgen tool is a Java technology-based SNMP MIB compiler that 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 DMK agent to be managed by an SNMP manager. You can use standard MBeans, model MBeans, dynamic MBeans and open MBeans in conjunction with the mibgen compiler.

The mibgen compiler is able to process:

Starting the mibgen Compiler

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

mibgen Options

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 includes:

-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, the mibgen compiler generates code for both SNMP agents and managers. By selecting the -mo option, you enable the mibgen compiler to generate code for only the manager and not for agents. The -mo option is incompatible with the -n option.

-mc

(MIB-CORE)

Does not use the default MIB-CORE definitions file provided with the Java DMK. In this case, the user must specify the MIB-CORE definitions file as one of the MIB files (for example, java.com.sun.jdmk.tools.MibGen -mc mib my_mib_core).

-a

Generates 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 some definitions of the first MIB file.

-p prefix

Uses the specified prefix for naming generated classes.

-g

Generates a generic version of the metadata that will access the MBeans through the MBean server instead of using a direct reference. This enables you to plug in dynamic MBeans, instead of the generated standard MBean skeletons.

-gp prefix

Uses the specified prefix to name the generic metadata classes. For example, the metadata class for group system will be named SystemprefixMeta. Default is no prefix.

-sp prefix

Uses the specified prefix to name the standard metadata classes. For example: the metadata class for group system will be named SystemprefixMeta. Default is no prefix.

-help

Prints a usage message explaining how to invoke the compiler, as follows:

  • <mib files>: By default mibgen generates code only for the modules specified in the first file. The other files are only used for closure analysis except when the -a is specified.

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

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

  3. The default MIB_CORE definitions file provided with Java DMK in installDir/etc/mibgen (mib_core.txt). To succeed, you must be able to derive the installation directory from the CLASSPATH environment variable. Otherwise, the mibgen compiler will look for the mib_core.txt file in currentDir/etc/mibgen.

  4. When using generic metadata (-g option), backward compatibility is not ensured. Using the -g option has generic advantages (MBeans are accessed through the MBean server, and any kind of MBeans can be plugged in), but slightly reduces the overall performance.


Note –

SNMP MIB implementations generated using the mibgen compiler from Java DMK 4.2 can run and recompile on Java DMK 5.0 without modification.


Output From the mibgen Compiler

The mibgen compiler also generates the Java source code required for representing a whole MIB in an SNMP manager. The mibgen compiler parses an SNMP MIB and generates the following:

MBeans generated by the mibgen compiler need to be updated to provide the definitive implementation. For more information, see the corresponding section in the Java Dynamic Management Kit 5.0 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. The generated MIB file offers factory methods for group MBeans.

To Implement the Generated MIB File
  1. Subclass the group MBean skeleton you want to implement, completing the getter, checker, and setter methods.

  2. Subclass the generated MIB file.

  3. Redefine the factory methods for the group MBeans you have implemented, ensuring that they instantiate the actual implementation class and not the skeleton.

The mibgen compiler uses the module name specified in the MIB definition to name files representing whole MIBs. The compiler removes special characters and replaces them 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, the mibgen compiler 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 various MIB variables. If the MIB specifies a default value for an SNMP variable, this value is used to initialize the corresponding variable in the MBean skeleton. Therefore, if you compile the generated code directly, you 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.

The mibgen compiler uses the group names specified in the MIB definition to name MBeans generated from groups.

Metadata Files

In addition to generating skeletal MBeans to represent each group, the mibgen compiler 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, the mibgen compiler generates:

Class Containing the SNMP View of a Table (Metadata Class)

The metadata class containing the SNMP view of a table contains all the management of the table index. The class is also prefixed with Table, followed by the name of the table.

Class Containing the MBean View of the Table

The class containing the MBean view of a table enables you to dynamically add or remove entries from the table. It contains callbacks and factory methods that enable you to instantiate or delete entries upon receiving requests from a remote SNMP manager (see RowStatus example). This class also is prefixed with Table, followed by the name of the table.

Skeletal MBeans Representing SNMP Table Entries

For each table in a MIB, the mibgen compiler generates an MBean representing a table entry. These skeletal MBeans must 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 obtain a running agent. In this case, values returned by the agent when querying the MIBs are not meaningful. The mibgen compiler uses the entry names specified in the MIB definition to name MBeans generated from table entries.


Note –

Remote creation of table entries is disabled by default, for security reasons. You can dynamically enable and disable remote creation of table entries by calling the setCreationEnabled operation on the generated MBean-like object.


The RowStatus convention, defined in RFC 2579, is fully supported by the code generator. When a table is defined using v2, and if it contains a control variable with row status syntax, the mibgen compiler generates a set of methods allowing this table to be remotely controlled by this variable. However, the remote creation and deletion of rows remains disabled by default.

Table objects are divided into two categories:

When remote table-entry creation is enabled, and the creation of a new table is requested, a factory method is called on the MBean-like object to instantiate the new table entry. By default, an instance of the skeleton class for that table entry is instantiated.

To Instantiate Your Own Implementation Class
  1. Subclass the MBean-like object to redefine the factory method for remote entry creation.

  2. Redefine this factory method so that it returns an instance of your implementation class, instead of the default skeleton.

  3. Subclass the group MBean, to which this table belongs, to instantiate your new MBean-like object, instead of the generated default object.

    This is demonstrated in the RowStatus example.

Metadata Files

In addition to generating skeletal MBeans to represent each table entry, the mibgen compiler 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 can use the Enumerated class to display all the labels contained in an enumeration. The mibgen compiler can 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 inline definition.


Note –

The mibgen compiler has an option -p prefix that you can use 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 compiler generates a Java class named EnumTcpConnState to represent the enumeration.

Information Mapping

For each group defined in your MIB, the mibgen compiler 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, the mibgen compiler generates a getter method for the corresponding property. If the MIB allows write access to a variable, the mibgen compiler 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 the mibgen compiler generates do not have any dependencies on specific SNMP objects. Therefore, they can be easily browsed or integrated into the various Java DMK 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 the mibgen compiler must be updated to provide the definitive implementation. The generated code is an operational agent. Thus, it can be compiled, run, and tested without any modification. If MBeans are generated too often, use subclasses to reduce major modifications in your code.

Example 2–1 shows how to implement a skeletal MBean.


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 SnmpStatusException {
           myVar = x;
      }

}

You must 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 sections in the Java Dynamic Management Kit 5.0 Tutorial.