Java Dynamic Management Kit 3.2 Programming Guide | ||||
---|---|---|---|---|
![]() | ![]() | Chapter 18. The mibgen Compiler | ![]() | ![]() |
The mibgen tool generates these types of Java source code:
A class representing the whole MIB
A class representing the whole MIB in a MibStore
Classes representing SNMP groups
Classes representing SNMP tables
Classes representing SNMP enumerated types
Figure 18-1 shows the mibgen output from a MIB containing two groups and two tables.
For information on how to start mibgen, see Using the mibgen Compiler.
The mibgen compiler generates a Java file that represents and initializes the whole MIB. This class extends the class SnmpMib. The class SnmpMib is an abstract Java class in the com.sun.jaw.snmp.agent.SnmpMib 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 MibStore. This class extends the com.sun.jaw.snmp.manager.MibStore class which maintains a database of MIB variables. A name can be resolved against the database. The file is used by the SNMP manager API. It contains metadata definitions for the compiled MIB. The metadata can then be loaded into the SNMP manager MibStore.
The file is only generated when mibgen is invoked using the -m or -mo options and is called MIBnameStore.java.
For each SNMP group defined in the MIB, mibgen generates:
A skeletal m-bean
A metadata file
The mibgen compiler generates an m-bean for each group defined in the MIB. These skeletal m-beans 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. See Implementing Access Methods for Simple Groups in Chapter 19 for more information on how to add access methods.
M-beans generated from groups are named using the group names specified in the MIB definition.
As well as generating skeletal m-beans to represent each group, mibgen generates a metadata file. The metadata file contains Java source code that provides the "SNMP view" of the m-bean. 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 m-bean representing a table entry
A metadata file corresponding to the skeletal m-bean
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 m-bean representing a table entry. These skeletal m-beans 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. See Implementing Access Methods for Tables and Entries in Chapter 19 for more information on how to add access methods.
M-beans generated from table entries are named using the entry names specified in the MIB definition.
In addition to generating skeletal m-beans to represent each table entry, mibgen generates a Java file containing the "SNMP view" of the m-bean. 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.jaw.reference.common package. The job tool and the HTML adaptor are 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 that allows you to prefix the names of all generated files with a specific string. See Options for mibgen.
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, as shown in Example 18-1.
Example 18-1. Representing MIB II TCP Connection States
/** * The class is used for representing "tcpConnState". */ public class EnumtcpConnState extends Enumerated implements Serializable { protected static Hashtable intTable= new Hashtable(); protected static Hashtable stringTable= new Hashtable() ; static { intTable.put(new Integer(10), "closing"); intTable.put(new Integer(12), "deleteTCB"); intTable.put(new Integer(2), "listen"); intTable.put(new Integer(3), "synSent"); intTable.put(new Integer(1), "closed"); intTable.put(new Integer(9), "lastAck"); intTable.put(new Integer(5), "established"); intTable.put(new Integer(7), "finWait2"); intTable.put(new Integer(6), "finWait1"); intTable.put(new Integer(4), "synReceived"); intTable.put(new Integer(11), "timeWait"); intTable.put(new Integer(8), "closeWait"); stringTable.put("closing", new Integer(10)); stringTable.put("deleteTCB", new Integer(12)); stringTable.put("listen", new Integer(2)); stringTable.put("synSent", new Integer(3)); stringTable.put("closed", new Integer(1)); stringTable.put("lastAck", new Integer(9)); stringTable.put("established", new Integer(5)); stringTable.put("finWait2", new Integer(7)); stringTable.put("finWait1", new Integer(6)); stringTable.put("synReceived", new Integer(4)); stringTable.put("timeWait", new Integer(11)); stringTable.put("closeWait", new Integer(8)); } |
Example 18-2 shows how to create an enumerated value to represent the closed state of a TCP connection.
Example 18-2. Representing the Closed State of a TCP Connection
EnumtcpConnState state= new EnumtcpConnState("closed"); |
For each group defined in your MIB, mibgen generates an m-bean. Each variable in the group is represented as a property of the m-bean. 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 m-bean.The mibgen compiler maps the MIB variable syntax to a well-defined Java type. The mapping is performed according to the set of syntax mapping rules given in Table 18-1.
Table 18-1. Syntax Mapping Rules
SNMP Syntax | M-Bean Syntax |
---|---|
Object identifier | Java String object |
IP address | Java String object |
Display String | Java String object |
Opaque string | Array of Byte objects |
Integer | Java Integer object |
Counter | Java Integer object |
Integer64 | Java Long object |
Counter64 | Java Long object |
TimeTicks | Java Integer object |
Truth value | Java Boolean object |
Enumerated List of Integers | Specific Enumerated class |
The m-beans 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 m-bean syntax is performed by the metadata.
Note - To change the Java type of a specific MIB variable within a generated m-bean, edit the metadata file associated with the group that contains the variable.
![]() | ![]() | ![]() |
Using the mibgen Compiler | ![]() | Developing SNMP Agents With the Java Dynamic Management Kit |