Previous Next Contents Index


19


Developing SNMP Managers With the Java Dynamic Management Kit

The SNMP manager API is a set of Java classes that simplifies the development of applications for managing SNMP agents. The agents can be developed with the Java Dynamic Management Kit, or by other means.


SNMP Manager Overview

The Java Dynamic Management Kit SNMP manager API enables you to develop applications to manage SNMP agents. The SNMP manager API can be used synchronously for simple applications or asynchronously in more demanding situations. There are four main components of the SNMP manager API:

SNMP Peer

The SNMP manager uses instances of the SnmpPeer class to represent remote agents. Each remote agent is represented by a single SnmpPeer object. An SnmpPeer can be instantiated with the IP address, the hostname and port, or the hostname of the remote agent. The SnmpPeer object holds the following information:

SNMP Parameters

The SnmpParameter class contains information on the SNMP read and write communities, and the SNMP version. This information is used by an SnmpSession object while exchanging packets with an SnmpPeer. An SnmpPeer can be configured explicitly to use a specific SnmpParameter object. Multiple SnmpPeer objects can share a single parameter object. Changing values for an SnmpParameter object affects all SnmpPeer objects that use that object.

When you change these parameters, the changes are applied to all active messages. CODE EXAMPLE 19-1 shows the instantiation and configuration of an SnmpPeer object representing a remote agent using port 8085 on host summer.    


CODE  EXAMPLE  19-1     Instantiating and Configuring an SnmpPeer Object

String hostname = String ("summer");
int port = 8085;

// Create a SnmpPeer object for representing remote agent
SnmpPeer agent= new SnmpPeer(hostname, port);

// Create parameters to associate to the remote agent.
// When creating the parameter object, you can specify the read and write
// community to be used when querying the agent.
SnmpParameters params= new SnmpParameters("public", "private");

// The newly created parameter object must be associated with the agent.
agent.setSnmpParam(params);


SNMP Session

The SNMP manager session is controlled by an instance of the SnmpSession class. An SnmpSession object creates and manages SNMP requests to multiple peers. The SnmpSession object can be instantiated with a default peer so that all requests created without specifying a peer use the default. The default peer can be set while the SnmpSession object is running.

Each SNMP manager session uses a dispatcher to service all of the requests it creates. It uses an SNMP socket which, unless configured otherwise, uses the default socket specified by the SnmpSocket object. The SNMP manager session can also be configured to use a separate socket exclusively.

The SnmpSession class provides methods to create requests. Individual requests perform a specific SNMP operation. When used asynchronously, the session maintains the list of all active requests and responses. Requests are retried if the peer does not respond within a specified time. The maximum retry limit is specified by the SnmpPeer objects. You can cancel any active requests at any time during a session.

CODE EXAMPLE 19-2 shows the instantiation and configuration of an SnmpSession using the SnmpPeer instantiated in CODE EXAMPLE 19-1 as the default peer.     


CODE  EXAMPLE  19-2     Instantiating and Configuring an SnmpSession 

// Build the session.
SnmpSession session= new SnmpSession("Manager session");

// Use "agent" as the default peer
session.setDefaultPeer(agent);


SNMP Session Options

SNMP sessions can be configured for specific situations using methods provided in the SnmpOptions object. Setting these options affects all subsequent requests. Existing requests are also affected, depending on the nature of the option. The SNMP session options are:

Variable Bindings

The SnmpVarbindList contains a list of SnmpVar objects. Each SnmpVar object holds information for a MIB variable and consists of:

SNMP Request

Instances of the SnmpRequest class enable you to send requests, handle retries, time-outs, and process responses from an agent. An SnmpRequest is created using a specific SnmpSession and SnmpPeer. The SnmpPeer object determines the destination of the request and controls the characteristics of the messaging between manager and agent.

The SnmpRequest object creates a request that is used to perform one or more of the following SNMP operations:

Request States

A request becomes active when a user submits the request successfully. When any event happens that changes the state of the request to done, the request becomes inactive. At any time, one or more requests active in a session can be cancelled.


Operation of the SNMP Manager

The Java Dynamic Management Kit SNMP manager can operate in two modes:

Synchronous Mode

In synchronous mode all operations are blocking, that is, an SNMP request is sent and the request object waits for a specified time, blocking the user thread. If no response is received during the wait time, the request is considered to have failed.

The request object provides the waitForCompletion method. The waitForCompletion method is used to specify synchronous mode and the interval to wait for the response. This is done by blocking the user's thread for the required period. The user thread is notified whenever a request reaches completion, irrespective of its success or failure. Invoking the waitForCompletion method with an interval of zero blocks the request object until a response is received. CODE EXAMPLE 19-3 shows the instantiation of a synchronous request.   


CODE  EXAMPLE  19-3     Instantiating a Synchronous Request

// Make the SNMP get request and wait for the result.

SnmpRequest request= session.snmpGet(null, list);
System.out.println("SimpleManager::main: Send get request to SNMP Agent on " + host + " at port " + port);
boolean completed= request.waitForCompletion(10000);


Asynchronous Mode

In asynchronous mode, management applications are able to send multiple requests and wait for individual responses. Asynchronous mode also permits polling, as described in "Polling" on page 255.

You send a request by instantiating an SnmpRequest object that specifies a response handler. The response handler is a class that implements the SnmpHandlerIf interface. The response handler has three methods that inform you that:

When a request is submitted, it joins a queue. When the request is sent, it is placed in a PDU packet and a timer is started when the packet is sent. If specified in the SnmpOptions object, requests are multiplexed and sent in a PDU packet containing other requests. Responses are de-multiplexed automatically. All of the multiplexing operations are transparent.

Requests are automatically retried if a response does not arrive within a specified interval. If the agent responds with an error, the SnmpRequest object uses the options defined in the SnmpOptions object to determine the subsequent actions. CODE EXAMPLE 19-4 shows the instantiation of an asynchronous request.   


CODE  EXAMPLE  19-4     Instantiating an Asynchronous Request

// Build the list of variables you want to query.
// For debug purposes, you can associate a name to your list.
SnmpVarbindList list= new SnmpVarbindList("AsyncManager varbind list");

// We want to read the "sysDescr" variable.
list.addVariable("sysDescr.0");

// Create a simple implementation of a SnmpHandlerIf.
AsyncRspHandler handler= new AsyncRspHandler(Thread.currentThread());

// Make the SNMP walk request.
// Read all the MIB variables starting at "sysDescr" and ending
// up at "sysServices".
// The responses will be received through the handler specified.

SnmpRequest request= session.snmpWalkUntil(handler,list, new              SnmpOid("sysServices"));
System.out.println("AsyncManager::main: Start snmpWalkUntil for SNMP Agent on              " + host + " at port " + port);



Loading Metadata Into the MibStore

Metadata generated by the mibgen compiler can be loaded into the MibStore for use by SNMP manager applications. CODE EXAMPLE 19-5 shows two ways of loading a metadata file generated from a MIB called RFC1213.   

CODE  EXAMPLE  19-5     Loading Metadata into the MibStore

MibStore.addMib(new RFC1213_MIBStore());

// or

SnmpMain.initializeSNMP(new RFC1213_MIBStore());



SNMP Traps

The SNMP manager API provides classes that enable you to receive SNMP v1 and SNMP v2 trap PDUs.

SNMP v1 Traps

To receive SNMP v1 trap PDUs, you must instantiate an SnmpTrapAgent. The SnmpTrapAgent allows you to create a receiver/dispatcher for SNMP traps. It is run as a thread. The SnmpTrapListener interface defines one callback method that must be implemented for classes that are required to receive SNMP v1 trap PDUs. CODE EXAMPLE 19-6 shows the instantiation of a trap listener.  

CODE  EXAMPLE  19-6     Instantiating a Trap Listener 

// Create a listener and dispatcher for SNMP Traps
// trapAgent is run as a thread and listens for traps in UDP port= 8086
// trapAgent will receive a callback when a valid trap PDU is received

SnmpTrapAgent trapAgent=newSnmpTrapAgent(8086);
trapAgent.addTrapListener(new TrapListenerImpl());
new Thread(trapAgent).start();


A class implementing the SnmpTrapListener interface is added to the SnmpTrapAgent. It is the callback object that is called when a valid SNMP v1 trap PDU is present. CODE EXAMPLE 19-7 shows the code for a class implementing the SnmpTrapListener interface.


CODE  EXAMPLE  19-7     Implementing an SNMP v1 Trap Callback Class 

// This class implements the SnmpTrapListener interface. The callback method
// processSnmpTrap is called when a valid SNMP v1 trap PDU is received.
public class TrapListenerImpl implements SnmpTrapListener {
   public void processSnmpTrap(SnmpPduTrap trap) {
      java.lang.System.out.println("NOTE: TrapListenerImpl received Trap :");
      java.lang.System.out.println("\tGeneric "+trap.genericTrap);
      java.lang.System.out.println("\tSpecific "+trap.specificTrap);
      java.lang.System.out.println("\tTimeStamp "+trap.timeStamp);
      java.lang.System.out.println("\tAgent address "           +trap.agentAddr.stringValue());
   }
}


SNMP v2 Traps

To receive SNMP v2 trap PDUs, you must instantiate an SnmpTrapAgent. The SnmpTrapAgent allows you to create a receiver/dispatcher for SNMP traps. It is run as a thread. The SnmpV2TrapListener interface defines one callback method that must be implemented for classes that are required to receive SNMP v2 trap PDUs. CODE EXAMPLE 19-8 shows the instantiation of a trap listener.  

CODE  EXAMPLE  19-8     Instantiating a Trap Listener 

// Create a listener and dispatcher for SNMP Traps
// trapAgent is run as a thread and listens for traps in UDP port= 8086
// trapAgent will receive a callback when a valid trap PDU is received

SnmpTrapAgent trapAgent=newSnmpTrapAgent(8086);
trapAgent.addTrapListener(new TrapListenerImpl());
new Thread(trapAgent).start();


A class implementing the SnmpV2TrapListener interface is added to the SnmpTrapAgent. It is the callback object that is called when a valid trap PDU is present. CODE EXAMPLE 19-9 shows the code for a class implementing the SnmpV2TrapListener interface.


CODE  EXAMPLE  19-9     Implementing an SNMP v2 Trap Callback Class

// This class implements the SnmpV2TrapListener interface.
// The callback method processSnmpTrap is called when a
// valid SNMP v2 trap PDU is received.

public class TrapListenerImpl implements SnmpV2TrapListener {

   public void processSnmpTrap(SnmpPduRequest trap) {
   ...
   }
}



Polling

The SNMP manager API provides a polling capability through the SnmpPollRequest class. The SnmpPollRequest class extends the SnmpRequest class. The polling operation is stopped if an error of any type is encountered.

For SnmpGet and SnmpGetNext operations, each polling cycle is started with the original SnmpVarbindList specified. The SnmpWalk operation is done on the response SnmpVarbindList until the first list meets the specified condition with testOid.

You must explicitly cancel the poll operation to terminate polling operation.



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

Previous Next Contents Index