Getting Row Data

 

Sometimes, the specifications require to fetch a particular row from a SNMP table. To retrieve a particular row from a table, the index value of the row should be known. To retrieve the row data from a SNMP table, both High-Level API and Low-Level API can be used.

 

Using High-Level API

 

Using SnmpTable Class

 

The following methods in the SnmpTable class can be used for getting the row data from the SNMP tables.

ID managerHost managerPort

3

localhost

161

6

server

1030

8

printer

8001

12

switch

8080

 

To fetch the second row from the above table, use the following code snippet.

 

table.loadMibs("mib file");

table.setTableOID("tableOID"); //sets the table OID.

table.getRow(2); //fetches the second row.

 

This returns an array of SnmpVarBinds of data.

 

Alternatively, if the instance value of the row to be fetched is known, say 6 is the instance value of the second row, the getRow method returns a String array of row data.

 

table.loadMibs("mib file");

table.getRow("tableOID", "6") //sets the table OID and fetches the row data for the second row with index 6

 

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

 

Using SnmpTarget Class

 

The list of OIDs (i.e the column name along with the row index) has to be first set in the method setObjectIDList() of the SnmpTarget class. Then, the snmpGetList() is used to fetch the entire row data.

 

ID managerHost managerPort

3

localhost

161

6

server

1030

8

printer

8001

12

switch

8080

 

For example to fetch the second row of this sample table, the following piece of code is used.

 

SnmpTarget target = new SnmpTarget();

String str = {"id.6", "managerHost.6, "managerPort.6"};

target.setObjectIDList(str);

String result [] = target.snmpGetList();

 

Using Low-Level API

 

To get the data of a row from the table, the OIDs (columnsOID.instance) can be added to the PDU using pdu.addNull(oids) and the request can be sent by setting the request command SnmpAPI.GET_REQ_MSG in the PDU. This fetches the data for the specified row.

 

Consider 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, following is the code to fetch the values for the second row in the table.

 

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(".1.3.6.1.4.1.2.2.1.1.6");

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

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

 

for(int i=0;i<oids.length;i++)

{

pdu.addNull(oids[i]);

}

pdu.setCommand(SnmpAPI.GET_REQ_MSG);

 

try

{

session.open();

}

catch (SnmpException e )

{

System.err.println("Error opening socket: "+e);

}

 

try

{

session.syncSend(pdu);

}

catch (SnmpException ex)

{

System.err.println("Error sending SNMP request: "+ex);

}

 

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

 

 

 



Copyright © 2012, ZOHO Corp. All Rights Reserved.