SNMP SET

 

The SNMP SET operation is used by the management applications and applets to modify the value of the managed object. Most of the managed objects have a default value maintained by the agent. Sometimes the applications might want to modify one or more MIB variables by using the SNMP SET operation.

 

The applications typically perform an SNMP SET operation by providing the host name of the agent, one or more OIDs along with its instance, and the new value. The agent processes the request and assigns the new value to the MIB variable. If an error occurs, the new value is not assigned.

 

Using High-Level API explains the usage of the high-level API in performing SNMP SET operation.

 

Using Low-Level API explains the usage of the low-level API in performing SNMP SET operation.

 

Using High-Level API

 

To perform the SNMP SET operation, the snmpSet() method can be used. The following are the steps involved in performing a simple SNMP SET operation using the SnmpTarget bean.

 

//instantiate the SnmpTarget

SnmpTarget target = new Snmptarget();

//set the host in which the SNMP agent is running

target.setTargetHost("localhost");

// set the OID

target.setObjectID(".1.3.6.1.2.1.1.5");

 

We need to load the MIB file to perform the SNMP SET operation. The MIB file is used to find the type of the object used in the SET operation.

 

target.loadMibs("RFC-1213MIB");

 

To perform the SET operation, we need the OID, its type, and its value. If the corresponding MIB file is loaded, the API gets the type of the OID from it. If the MIB file is not available, we need to give the type of the OID.

 

The following code shows a new value in the snmpSet() method.

 

// perform SNMP SET

String result = target.snmpSet("testing ....");

System.out.println("OBJECT ID: "+target.getObjectID()); System.out.println("Response: "+result);

 

The above code performs a SET operation with the SNMP agent running on localhost to set the value of the variable 1.5. (sysName) to the new value "testing...." and displays the results. Note that the OIDs should have write access for performing the SET operations. View the complete example present in <tutorials/highlevelapi/SnmpSet.java>.

 

The following methods can also be used for performing SNMP SET requests.

Multiple OIDs have to be set using the setObjectIDList() method and snmpSetList() should be used to query the agent.

 

The following method can also be used for performing SNMP SET requests with multiple OIDs.

Using Low-Level API

 

The SnmpAPI, SnmpSession, and SnmpPDU classes are used for most of the management operations in the low-level API.

 

To use the communication services available with the API, we must instantiate SnmpAPI. The SnmpAPI class is a thread which monitors SNMP sessions and it contains various SNMP parameters.

 

To communicate with SNMP entities, we need to instantiate the SnmpSession class. The open() method is to be invoked to get the socket, SnmpTransportProvider, (DatagramSocket in case of UDP) for SNMP communication. Various parameters, such as remote host, remote port, version, community, retries, and timeouts can be set using this class.

 

Following are the steps involved in performing a simple SNMP SET operation using the low-level API.

 

Instantiate the SnmpAPI class.

 

SnmpAPI api = new SnmpAPI();

 

Instantiate and open the SnmpSession class.

 

SnmpSession session = new SnmpSession(api);

session.open();

 

To perform SNMP SET operations, the command constant SET_REQ_MSG defined in the SnmpAPI class should be used.

 

// Build set request PDU

SnmpPDU pdu = new SnmpPDU();

pdu.setCommand(SnmpAPI.SET_REQ_MSG);

 

To perform SET operations we need to know the OID, its type, and its value. These values are needed to build the varbind and can be received as user input. The variable binding or the varbind is the pairing of the OID and its corresponding value. This varbind should be added to the PDU for performing SET operations.

 

The type of the variable can be INTEGER, STRING, COUNTER, etc. The SnmpAPI class provides constants for all the SNMP data types. Using this type, the value an instance of SnmpVar is created.

 

String value = "localhost";

byte dataType = SnmpAPI.STRING;

SnmpOID oid = new SnmpOID("1.5.0"); // sysName

SnmpVar var = null;

// create SnmpVar instance for the value and the type

var = SnmpVar.createVariable(value, dataType);

 

This SnmpVar object is used to create the varbind.

 

//create varbind

SnmpVarBind varbind = new SnmpVarBind(oid, var);

 

The variable binding is then added to the PDU.

 

//add variable binding

pdu.addVariableBinding(varbind);

 

Now the request should be sent over a session to the peer SNMP entity. The method syncSend(pdu) is used to send synchronous requests. The printVarBinds() methods is used to print the descriptive value of the OID and the variables. An error message is displayed if the request fails.

 

SnmpPDU result = session.syncSend(pdu);

 

Close the session and the API thread.

 

session.close();

api.close();

 

Writing applications using low-level APIs is slightly complex when compared to writing applications using high-level APIs. View the complete example present in <tutorials/lowlevelapi/SnmpSet.java>.

 

 

 



Copyright © 1996-2006, AdventNet Inc. All Rights Reserved.