Java Dynamic Management Kit 3.2 Programming Guide | ||||
---|---|---|---|---|
![]() | ![]() | Chapter 19. Developing SNMP Agents With the Java Dynamic Management Kit | ![]() | ![]() |
M-beans generated from SNMP MIBs require you to add access methods to be able to read and write m-bean properties. The following subsections show how to implement access methods for:
Simple groups
Tables and entries
Each SNMP group is represented by an m-bean named after the group name. In the case of MIB II, the system group is represented by an m-bean called System. The metadata of the System object are contained in an object called SystemMeta. The code generated by mibgen does not impose an inheritance scheme on the generated object. This is because the Java language supports only a single inheritance scheme. Therefore, you are free to use your own inheritance scheme when designing the implementation of your MIB. Example 19-1 shows the code for the System group. In this case there is no predefined inheritance.
Example 19-1. Code Generated by mibgen to Implement the System Group
// // Generated by mibgen version 3.0 when compiling RFC1213-MIB. // import java.io.*; import java.util.*; import com.sun.jaw.impl.adaptor.snmp.*; /** * The class is used for implementing the "System" group. * The group is defined with the following oid: 1.3.6.1.2.1.1. */ public class System implements Serializable { |
For each variable of a group, mibgen generates:
A variable for storing the value
The type used is a basic Java type as defined in Table 18-1. The variable is initialized with a default value so that the agent is able to run as soon as the generated code is compiled.
A getter, a setter and a checker depending on the rights defined for the variable in the MIB.
The checker is called to validate a set operation before it is performed. This enables the agent to comply with the SNMP standard, which states that a set operation on a group of variables must be atomic.
In each of the generated methods, you need to add code for accessing or setting the variable. For example, in the System group, the sysLocation variable is defined as a read-write variable. The mibgen compiler generates the code shown in Example 19-2.
Example 19-2. Code Generated by mibgen to Implement sysLocation
/** * Variable for storing the value of "SysLocation". * The variable is identified by: "1.3.6.1.2.1.1.6". */ protected String SysLocation= new String("Sample implementation of system group"); ... /** * Getter for the "SysLocation" variable. */ public String getSysLocation() throws SnmpStatusException { return SysLocation; } /** * Setter for the "SysLocation" variable. */ public void setSysLocation(String x) throws SnmpStatusException { SysLocation = x; } /** * Checker for the "SysLocation" variable. */ public void checkSysLocation(String x) throws SnmpStatusException { // // Add your own checking policy. // } |
Note - The checker method must throw an SnmpStatusException if the requested operation cannot be performed.
Tables are not represented as specific m-beans but as a set of entries. For each table, mibgen generates a Java class containing the SNMP view of the table. In the case of MIB II, the TCP group is represented by an m-bean called tcp. The mibgen compiler generates a Java class called TabletcpConnTable to represent the SNMP view of tcpConnTable. An instance of TabletcpConnTable is created in the constructor of the group. The code generated by mibgen to implement the TCP group is shown in Example 19-3.
Example 19-3. Code Generated by mibgen to Implement the TCP Group
/** * Constructor for the "tcp" group. */ public tcp(SnmpMib myMib) { tcpConnTable = new TabletcpConnTable (myMib); } |
Entries are represented in exactly the same way as groups, that is, as m-beans. A metadata object is generated for each entry type. The tcpConnTable table contains a tcpConnEntry object. Therefore, mibgen generates a tcpConnEntry class to represent an entry in tcpConnTable. The code generated by mibgen to implement tcpConnEntry is shown in Example 19-4.
Example 19-4. Code Generated by mibgen to Implement tcpConnEntry
// // Generated by mibgen version 3.0 when compiling RFC1213-MIB. // import java.io.*; import java.util.*; import com.sun.jaw.impl.adaptor.snmp.*; /** * The class is used for implementing the "tcpConnEntry" group. * The group is defined with the following oid: 1.3.6.1.2.1.6.13.1. */ public class tcpConnEntry implements Serializable { ... } |
Table objects provide methods for adding or removing entries from the table. Example 19-5 shows how to add a new tcpConnEntry to tcpConnTable.
Example 19-5. Adding a New Entry to a Table
/** * Constructor for the "tcp" group. */ public tcp(SnmpMib myMib) { tcpConnTable = new TabletcpConnTable (myMib); try { // Create the entry object // tcpConnEntry entry= new tcpConnEntry(myMib); // Initialize some fields // entry.tcpConnLocalAddress= "1.2.3.4"; // Add the entry into the table // tcpConnTable.addEntry(entry); } catch(Exception e) { e.printStackTrace(); } } |
If you want to be notified each time a table entry is added or removed, instantiate a class that implements the SnmpTableEntryListener interface. This interface defines two callback methods for classes that require such notification. Code for a class implementing the SnmpTableEntryListener interface is shown in Example 19-6. An SnmpTableEntryEvent is sent to the class implementing the SnmpTableEntryListener interface.
Example 19-6. Implementing the SnmpTableEntryListener interface
public class SnmpTableEntryListenerImpl implements SnmpTableEntryListener { // Invoked when an entry is added to a SNMP table public void entryAdded(SnmpTableEntryEvent event) { ... } //Invoked when an entry is removed from the SNMP table. public void entryRemoved(SnmpTableEntryEvent event) { ... } } |
The class implementing the SnmpTableEntryListener interface must be added as a listener to the table to receive SnmpTableEntryEvent objects. This is shown in Example 19-7.
Example 19-7. Adding an SnmpTableEntryListener
//add a listener to the table tcpConnTable.addSnmpTableEntryListener(new TableEntryListenerImpl()); |
When dealing with tables, SNMP indexes are handled automatically. The table object transparently maps an SNMP index to a Java table entry.
When a set request is received on a table object for a variable that does not exist, the table object rejects the operation. You can, however, customize this behavior so that a new entry is created when a set operation does not specify an existing entry. Do this by:
Replacing the inheritance of the table by SnmpMibTableRemCreate
Removing the comments around the createNewEntry() method generated by mibgen
Example 19-8 shows the tcpConnTable class as it appears before customization.
Example 19-8. TabletcpConnTable Class Before Customization
public class TabletcpConnTable extends SnmpMibTable implements Serializable { ... } |
Example 19-9 shows how to customize the TabletcpConnTable class to allow the addition of new entries.
Example 19-9. Customizing the TabletcpConnTable Class
public class TabletcpConnTable extends SnmpMibTableRemCreate implements Serializable { ... } |
Note - By default, the generated code does not register table entries with the framework. Table entries are indexed properties within a group.
![]() | ![]() | ![]() |
Overview of the Agent Development Process | ![]() | Loading MIBs Into an SNMP Adaptor |