Previous Next Contents Index


18


Developing SNMP Agents With the Java Dynamic Management Kit

The Java Dynamic Management Kit provides a toolkit for developing SNMP agents. This toolkit enables you to implement SNMP MIBs as a set of m-beans. They can then be integrated into the framework. Once integrated, they can be accessed from different protocols just like any other m-beans.


SNMP Agent Overview

The Java Dynamic Management Kit enables you to develop SNMP agents using the Java programming language. These can be:

Integrated Agents

Integrated agents run within the framework of the Java Dynamic Management Kit. These agents can access services provided by the Java Dynamic Management Kit and are manageable through different protocols.

A direct benefit of using an SNMP agent integrated in this way is that the information contained in the MIB can be browsed through the adaptors provided by the Java Dynamic Management Kit. For instance, if you are using the HTML adaptor, you can view the MIB through a web browser.

FIGURE 18-1 shows the management of a Java Dynamic Management agent by an SNMP manager application. This shows how a Java Dynamic Management agent communicating with an SNMP manager requires metadata so that the managed m-beans are visible to the manager. The SNMP manager application cannot access m-beans registered in the repository that have not been generated by mibgen. RMI or HTTP manager applications, however, are able to manage all m-beans, including those generated by mibgen.

FIGURE  18-1 Integrated SNMP Agent

Standalone Agents

Standalone agents run independently of all other Java Dynamic Management Kit components. These agents are accessible only through SNMP and cannot benefit from Java Dynamic Management Kit services. However, standalone agents minimize the resources used by the Java Dynamic Management Kit at runtime.


Overview of the Agent Development Process

The main steps involved in developing an SNMP agent using the Java Dynamic Management Kit are:


Implementing Access Methods

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:

Implementing Access Methods for Simple Groups

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. CODE EXAMPLE 18-1 shows the code for the System group. In this case there is no predefined inheritance.  

CODE  EXAMPLE  18-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:

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 CODE EXAMPLE 18-2

CODE  EXAMPLE  18-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. 

Implementing Access Methods for Tables and Entries

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 CODE EXAMPLE 18-3.

CODE  EXAMPLE  18-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 CODE EXAMPLE 18-4.


CODE  EXAMPLE  18-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. CODE EXAMPLE 18-5 shows how to add a new tcpConnEntry to tcpConnTable.


CODE  EXAMPLE  18-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 CODE EXAMPLE 18-6. An SnmpTableEntryEvent is sent to the class implementing the SnmpTableEntryListener interface.


CODE  EXAMPLE  18-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 CODE EXAMPLE 18-7.


CODE  EXAMPLE  18-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:

CODE EXAMPLE 18-8 shows the tcpConnTable class as it appears before customization.

CODE  EXAMPLE  18-8     TabletcpConnTable Class Before Customization

public class TabletcpConnTable extends SnmpMibTable implements
Serializable {

...
}


CODE EXAMPLE 18-9 shows how to customize the TabletcpConnTable class to allow the addition of new entries.


CODE  EXAMPLE  18-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. 


Loading MIBs Into an SNMP Adaptor

In the Java Dynamic Management Kit, a MIB is represented by a subclass of the SnmpMib class in the com.sun.jaw.impl.adaptor.snmp package. The generated class is named using the module name specified in the MIB definition. For example, mibgen generates a class called RFC1213_MIB.java to represent MIB II. The class initializes all the SNMP metadata required to handle the MIB and creates a new instance of each group.

If you do not need to support a specific group, you can edit the MIB class file and comment out the SNMP group you do not want to support.

If you do not need to support a specific variable in a group, you can do one of the following:

The MIB object provides two init functions, as shown in TABLE 18-1.

TABLE  18-1 init Functions

init

Initializes all the SNMP metadata required to handle the MIB and creates a new instance of each group.

initCmf

The same as init but in addition it registers each group in the .


Port Allocations

When an agent instantiates an SNMP adaptor in the Solaris operating environment, it binds by default to port 161. To be able to bind to port 161, the agent must be started by the root user of the system.

If the Solaris SNMP agent (snmpdx) is also running on the same machine, there will be a conflict over port allocations. To resolve this conflict, stop the Solaris SNMP agent (snmpdx) or specify that the agent binds to another port.

To stop the Solaris SNMP agent (snmpdx), as root type:


prompt# /etc/init.d/init.snmpdx stop

To bind to a different port, set the object name of the SNMP adaptor explicitly, specifying the port number by using the port=portno search key.

Initializing a MIB Within the Framework

CODE EXAMPLE 18-10 shows how to initialize the MIB within the framework by calling the newObject method of the framework.

CODE  EXAMPLE  18-10     Initializing a MIB Within the Framework

cmf.newObject("RFC1213_MIB", "SNMP:RFC1213_MIB");


Initializing a MIB in a Standalone Agent

CODE EXAMPLE 18-11 shows how to initialize a MIB in a standalone agent.

CODE  EXAMPLE  18-11     Initializing a MIB in a Standalone Agent

RFC1213_MIB mib2= new RFC1213_MIB();

// Now initialize the mib
//
mib2.init();


Binding a MIB to an SNMP Adaptor

A MIB object needs to be bound to an SNMP adaptor to be manageable through the SNMP protocol. A MIB can be bound or unbound at any time.

To retrieve the bind state of a MIB, invoke the getBindingState method of the SnmpMib class.

To bind a MIB to an SNMP adaptor, do either of the following:

Statically Loading a MIB

Loading a MIB statically enables you to build a standalone agent that does not require any Java Dynamic Management Kit services. CODE EXAMPLE 18-12 shows how to load a MIB statically. Loading a MIB statically involves:


CODE  EXAMPLE  18-12     Loading a MIB Statically

// Create the SNMP adaptor. (Use default SNMP port 161)
//
AdaptorServerImpl snmpStack= new AdaptorServerImpl()
RFC1213_MIB mib2= new RFC1213_MIB();

// Now initialize the mib
//
mib2.init();

// Bind the MIB to the adaptor
//
mib2.setSnmpAdaptor(snmpStack);


Dynamic Loading

To load a MIB dynamically using the HTML adaptor:

1. Use the m-bean browser web page to instantiate the MIB you want to load.

2. If you want your MIB to be accessible through SNMP, bind it to an existing SNMP adaptor:

Set the SnmpAdaptorName property of the MIB instance using the object name of the SNMP adaptor to which you want to bind your MIB. The default name is: defaultDomain:com.sun.jaw.impl.adaptor.AdaptorMO.protocol=snmp, port=161 



Copyright 1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California 94303 U.S.A.
Copyright in French

Previous Next Contents Index