Fetching Tables


An entire SNMP table from a remote agent can be retrieved either by using the High-Level API or the Low-Level API.


Using High-Level API


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.

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.

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.

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.


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 isCellEditable(rowIndex, columnIndex) method checks whether the specified cell is editable.


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.

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

    1. StringBuffer sb = new StringBuffer();

      for int i=0; i < table.getColumnCount();i ++)// print column names sb.append(table.getColumnName(i)+" \t");



      StringBuffer sb2 = new StringBuffer();


      for (int i=0;i < table.getColumnCount();i++)// get number of columns

      for (int j=0;j < table.getRowCount();j++) // get number of rows


      sb2.append(table.getValueAt(j,i)+" \t"); // get the cell value



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.

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 .

View the complete example present in <tutorials/highlevelapi/GetSnmpAugmentTable.java>.



Using SnmpTarget class


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.

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.

    1. SnmpTarget target=new SnmpTarget();



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



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














If the columnOIDs are ., ., ., 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


SnmpOID[] oids = new SnmpOID[3];

oids[0]=new SnmpOID(".");

oids[1]=new SnmpOID(".");

oids[2]=new SnmpOID(".");


for(int i=0;i<3;i++)





//send PDU

SnmpPDU result = session.syncSend(pdu);

System.out.println(result.printVarBinds() + "\n");


View the complete example present in <tutorials/lowlevelapi/FetchTable.java>





Copyright © 2012, ZOHO Corp. All Rights Reserved.