SNMP GET

 

The SNMP GET operation is used by the SNMP manager applications to retrieve one or more values from the managed objects maintained by the SNMP agent. The applications typically perform an SNMP GET request by providing the host name of the agent and one or more OIDs along with the specific instance of the OID. The agent respond with a return value or with an error.

 

The SNMP GET operation is normally used to query the scalar variables in a MIB. Each scalar variable is identified by its OID and its instance. The instance is used to identify the specific instance of the scalar variable. It is specified by appending a ".0" to its OID.

 

For example, if we want to perform a SNMP GET on the OID .1.3.6.1.2.1.1.3, we need to specify 1.3.6.1.2.1.3.1.3.0 to retrieve the OID's value. If the request is made without the instance value, the agent returns an error.

 

The SnmpAPI, SnmpSession, and SnmpPDU classes are used for most of the management operations.

 

To use the communication services available with the API, we must instantiate and initialize 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 a socket for SNMP communications. 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 GET operation using the API.

  1. Instantiate the SnmpAPI class.

SnmpAPI api = new SnmpAPI();

  1. Instantiate and open the SnmpSession class.

SnmpSession session = new SnmpSession(api);

session.Open();

  1. The default protocol used for SNMP communications is UDP. Every packet that is sent through SnmpSession goes through the UDP implementation of SnmpTransportProvider. Parameters that are required for such operations are given through the UDPProtocolOptions class. After SnmpSession is opened for SNMP communication, the default values such as remoteHost and remotePort can be set using the UDPProtocolOptions object. If the remoteHost and remotePort is not specified in the SnmpPDU object, the API takes it from SnmpSession.

An SnmpPDU instance needs to be created to send any request to an SNMP peer. The SnmpPDU provides most of the communication parameters related methods that are available with SnmpSession and it overrides the value in the session.

 

Set the SNMP version using the Version() method and use the Command() method to send an SNMP request. The command constants are defined in the SnmpAPI class. The following command sets the constant to GET_REQ_MSG to perform an SNMP GET operation.

 

//Build GET Request PDU

SnmpPDU pdu = new SnmpPDU();

//get the value from the command line

UDPProtocolOptions option = new UDPProtocolOptions(remoteHost);

pdu.ProtocolOptions=option;

pdu.Command=SnmpAPI.GET_REQ_MSG;

  1. To make a query for an OID or a list of OIDs, the SnmpOID class is to be instantiated. SnmpOID is the sub class of the SnmpVar class that provides abstract methods to present a uniform interface for applications working with the SNMP variables.

The OID can be given in the form x.x.x... or .x.x.x.... The OID given in the form .x.x.x.x is assumed to be fully qualified, and the OID in the form x.x.x is not fully qualified in which case the value of the SnmpAPI.getOIDPrefix() method is added to the OID as a prefix.

 

This prefix can be changed by the user but it should be to applied across the entire application. The AddNull method in the SnmpPDU class adds a variable binding with the OID specified and a null variable value. Multiple OIDs can also be given as input.

 

SnmpOID oid = new SnmpOID("1.1.0"); //Here the OID is .1.3.6.1.2.1.1.0

pdu.AddNull(oid);

  1. After the SnmpPDU and the OID is setup using the above methods, it 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);

System.Console.Out.WriteLine(result.PrintVarBinds());

  1. Close the session and the API thread.

session.Close();

api.Close();

 



Copyright © 2009, ZOHO Corp. All Rights Reserved.