Fetching Tables

The table data can be retrieved using the SnmpTable bean of the high-Level API. The SnmpTarget, SnmpTableModel, TableBean, or SnmpTablePanel beans can be used for retrieving table data. The following section discusses on the retrieval of table data using SnmpTable and SnmpTarget class.

Using SnmpTable

Retrieval Mode

The table data is retrieved by performing repeated SNMP GETNEXT or SNMP GETBULK operations. The SnmpTable bean, by default, uses the SNMP GETNEXT to retrieve the SNMP tables. For tables with less number of rows the GETNEXT operation proves efficient in fetching the values. To retrieve larger tables, the GETBULK mode is preferable.

To use the GETBULK mode, the agent should either be SNMPv2c or SNMPv3. To set the retrieval mode to GETBULK, the following method of the SnmpTable class is used.

table.setRetrievalMode(false);

Setting the value of the above method to true allows the table to use GETNEXT to retrieve the table data.

If the retrieval mode is set to false, we need to modify the Max-Repetitions value of GETBULK depending on the requirement. The following command sets the Max-Repetitions value to 60.

table.setMaxRepetitions(60);

The default value of the Max-Repetitions is 50. Depending on the table size and the maximum PDU size that the agent can handle, the Max-Repetitions value can be set. If the table size is not known, this can be set to an arbitrary value.

Retrieving Table Data

To retrieve the table data using the SnmpTable class, we need to know the OID of the table that is to be queried. The method setTableOID() uses this tableOID to get the table data. This method starts polling of table data automatically. The MIB containing the table has to be loaded in the application as follows.

SnmpTable table = new Snmptable(); //instantiate SnmpTable

table.setTargetHost("localhost"); // set the mandatory parameters

table.loadMibs("RFC1213-MIB"); // load the MIB

table.setTableOIDWoStart("ifTable"); //set the tableOID

table.run(); // Fetches the data.

To set the table OID without starting the data polling, the setTableOIDWoStart(tableOID) method can be used. The following methods of the SnmpTable class can be used to get the table data in the desired format.

getColumnName(int) - gets the name of the column in the SNMP table.

getColumnCount(int) - returns the number of columns in the SNMP table.

The SnmpTable class provides two methods getValueAt() and getCellValue() for getting values of a single cell. If getCellValue() is used, the tableOID need not be set separately using setTableOID(). This method is preferred when the user needs to get only one value from the table. If the entire table data is required, the getValueAt() method can be used.

The type in which the table data is returned can also be changed using setDataType() method. By default, the getValueAt() method returns data as a String. If the table data is to be returned as a SnmpVar or SnmpVarBind object, the type can be set using setDataType(). This method can take any of the following constants.

STRING DATA

SNMP_VARIABLE_DATA

SNMP_VARIABLE_BINDING_DATA

The following code snippet makes use of some of the above methods to retrieve the table values.

The disadvantage with this procedure is that if we do not call Thread.sleep(5000), the data from the table is not read. This is because, the Snmp Table takes some time to fetch all the table data. The value "5000" is also arbitrary which depends on the table size. If the table is very large, this value might not be adequate leading to loss of data. Therefore, this way of retrieving data is not preferred only when the size of the table is known. View the complete example present in <tutorials/highlevelapi/SnmpGetTable.java>.

Implementing Listener

Using Thread.sleep() can be avoided if we allow event listeners to be registered to the SnmpTable component. The SnmpTableListener interface has to be implemented in the application to respond to the changes in the SnmpTable objects. The method addSnmpTableListener(SnmpTableListener) of the SnmpTable is used for registering as a listener. The SnmpTable instance while performing requests and receiving data, generates a table event. The registered listeners get the data from the response. The following piece of code gives a brief idea about implementing the listeners.

public class getSnmpTable implements SnmpTableListener

{

public static void main(String args[])

{

//instantiate a snmp table instance

SnmpTable table = new SnmpTable();

.....

.....

.....

// register this class to receive table events

table.addSnmpTableListener(this);

// set the tableOID in the snmp table

table.setTableOID(tableOID);

....

}

// implement the method in the SnmpTableListener interface

public void tableChanged(SnmpTableEvent tableevent)

{

// code goes here

}

}

Retrieving Partial Table

Partial table data can be viewed by setting only those columns that are needed to be fetched using setObjectIDList() method.

Using SnmpAugmnetTable

SnmpAugmentTable is an extended class of SnmpTable provided for obtaining augmented tables (which has one-to-one dependency between rows of one table and rows in another table) . Augmneted table can be retrieved using similar methodology which was used for obtaining ordinary SnmpTable .

The SnmpTable class is preferred to the SnmpTarget class for retrieve the SNMP table data. The SnmpTable class has additional table-related methods. To get the table data as SnmpVar objects, the snmpGetAllVariables() method can be used and to get the data as SnmpVarBind objects, the snmpGetAllVariableBindings() method can be used.

If the SnmpTarget class is used for retrieving tables, then the following methods can be made use of in applications.

setObjectIDList() - sets the OIDs.

snmpGetAllList() - Gets the values for the OID already set in an OID list as String[][].

To get the table data as SnmpVar objects, the snmpGetAllVariables() method can be used and to get the data as SnmpVarBind objects, the snmpGetAllVariableBindings() method can be used.

Consider a table with 3 column IDs, managerHost, and managerPort. Following is the sample code to get the table values.

SnmpTarget target=new SnmpTarget();

target.setTargetHost("localhost");

target.loadMibs("mib");

String[] str={"id","managerHost","managerPort"};

target.setObjectIDList(str);

target.snmpGetAllList();

The snmpGetAllList() method returns the table data as a two-dimensional String array.

Therefore to fetch only partial table data, the required column OIDs can be set in setObjectIDList() and snmpGetAllList() can be performed.

Using Low Level API

To retrieve the table data using the low-level API, the SnmpAPI, SnmpSession, and SnmpPDU classes of the snmp2 package are used. The table data can be fetched by adding the columnOIDs(column names) of the table to the PDU and performing GETNEXT repeatedly. Following is the sample table.

ID

managerHost

managerPort

3

localhost

161

6

server

1030

8

printer

8001

12

switch

8080

If the columnOIDs are .1.3.6.1.4.1.2.2.1.1, .1.3.6.1.4.1.2.2.1.2, .1.3.6.1.4.1.2.2.1.3, to get the table data the following code snippet can be used.

SnmpAPI api = new SnmpAPI();

SnmpSession session = new SnmpSession(api);

SnmpPDU pdu = new SnmpPDU();

UDPProtocolOptions option = new UDPProtocolOptions("localhost");

pdu.setProtocolOptions(option); //sets the host in which the agent is running