Getting Column Data

 

Similar to retrieving row data of a table, applications might also require to retrieve one or more column data. To retrieve a specific column or columns from a table, the columnOID of the row should be known. To retrieve the column 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 column data from SNMP tables.

To fetch the data of the second column (ManagerHost) in the above sample table, the following code snippet can be used.

 

table.loadMibs("mib file");

table.setTableOID("tableOID");

table.getColumn(1) // 1 represents the position of the column in the table.

 

or

 

table.loadMibs("mib file");

table.getColumn("managerHost");

 

can be used. These methods return a String array of column data. View the complete example present in <tutorials/highlevelapi/GetColumn.java>.

 

Following is another way of fetching data for more than one column.

 

table.loadMibs("mib file");

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

table.setObjectIDList(str); // set the column OIDs whose values are required.

table.addSnmpTableListener(listener);//register the listeners that need to get the column data.

 

The column data can now be received in the tableChanged() method.

 

Using SnmpTarget Class

 

One or more columnOIDs have to be first set in the setObjectIDList() method of the SnmpTarget class. Then the snmpGetAllList() is used to fetch the entire column data.

 

ID managerHost managerPort

3

localhost

161

6

server

1030

8

printer

8001

12

switch

8080

 

For example, to fetch the second column managerHost of this sample table, the following piece of code can be used.

 

SnmpTarget target = new SnmpTarget();

String[] str = {"managerHost"};

target.setObjectIDList(str);

String result [] = target.snmpGetAllList();

 

This fetches all the data in the second column of the table namely localhost, server, printer, and switch. Similarly, data for more than one column can also be retrieved by including the respective column name(s) in the ObjectIDList.

 

The snmpGetAllVariables() or snmpGetAllVariableBindings() methods can be used to get the column data, if it is required as an SnmpVar or SnmpVarBind object.

 

Using Low-Level API

 

If the data for a single column alone is required, the columnOID can be set in the PDU and request can be sent using session.syncSend() method.

 

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 code gets the second column(managerHost).

 

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 oid = new SnmpOID(".1.3.6.1.4.1.2.2.1.2");

pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG);

session.open();

session.syncSend(pdu);

 

 

SnmpOID oid = new SnmpOID(".1.3.6.1.4.1.2.2.1.2");

pdu.addNull(oid);

pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG);

try{

session.open();

session.syncSend(pdu);

}

catch(SnmpExeption e)

{

System.err.println("Error opening socket or sending SNMP request: "+e);

}

 

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

 

 

 

 



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