SNMP Agent in Java: How adding new rows in MOTable

穿精又带淫゛_ 提交于 2019-12-09 14:07:19

问题


I am trying to implement an SNMP Agent in Java. I use snmp4j library (http://www.snmp4j.org/). Currently, my agent works on localhost/4700. I tried to send snmpget request thanks to the following request:

snmpget -v2c -c public localhost:4700 1.3.6.1.4.1.1.99.5.4.1.3.1.1

but I only get something like "No such instance currently exists at this OID" Here is my problem: I don't know how to create one. I tried to add rows to my MOTable, but it doesn't seem to work.

Here is a summary of my class implementing MOGRoup

public class MyMIB 
//--AgentGen BEGIN=_EXTENDS
//--AgentGen END
implements MOGroup 
//--AgentGen BEGIN=_IMPLEMENTS
//--AgentGen END
{
    .
    .
    .

public static final OID oidTableEntry = 
    new OID(new int[] { 1,3,6,1,4,1,1,99,5,4,1,3,1 });
    .
    .
    .

private void createTableEntry(MOFactory moFactory) {
// Index definition
    .
    .
    .

// Table model
tableEntryModel = 
  moFactory.createTableModel(oidTableEntry,
                         tableEntryIndex,
                         tableEntryColumns);
((MOMutableTableModel)tableEntryModel).setRowFactory(
  new TableEntryRowFactory());
tableEntry = 
  moFactory.createTable(oidTableEntry,
                    tableEntryIndex,
                    tableEntryColumns,
                    tableEntryModel);

//Adding rows
ArrayList<Integer> oidMon1List= new ArrayList<Integer>();
oidRow1List = this.getListFromArray(oidTableEntry.getValue());
oidRow1List.add(1);

ArrayList<Integer> oidMon2List= new ArrayList<Integer>();
oidRow2List = this.getListFromArray(oidTableEntry.getValue());
oidRow2List.add(2);

DefaultMOMutableTableModel model =
               (DefaultMOMutableTableModel)tableEntry.getModel();

synchronized(model){
model.addRow(
        model.createRow(
                new OID(getArrayFromList(oidRow1List)), 
                new Variable[]{
                    new Integer32(123)
                    }));

model.addRow(
        model.createRow(
                new OID(getArrayFromList(oidRow2List)), 
                new Variable[]{
                    new Integer32(456)
                    }));   
    }
}

But the requests bellow still don't work.

snmpget -v2c -c public localhost:4700 1.3.6.1.4.1.1.99.5.4.1.3.1.1

snmpget -v2c -c public localhost:4700 1.3.6.1.4.1.1.99.5.4.1.3.1.1.0

snmpget -v2c -c public localhost:4700 1.3.6.1.4.1.1.99.5.4.1.3.1.1.1

I must did not understand how correctly create rows. Could you explain me how I should do ?

Thank you very much !

Just a little precision: I added those lines to my program:

System.out.println("Get row count: " +tableEntryModel.getRowCount());

//first row
System.out.println("TEST1: " +model.getRow(new OID(new int[] {1,3,6,1,4,1,1,99,5,4,1,3,1,1})).getValue(0));
System.out.println("TEST2: " +tableEntry.getValue(new OID(new int[] {1,3,6,1,4,1,1,99,5,4,1,3,1,1,0})));

The first returns: 2 (as expected)

The second returns: 123 (as expected)

The third returns: null... here, I don't understand why !


回答1:


I would say, the code and the request did not match. I guess that the code is the problem, because in SNMP4J-Agent the instance OID of a table's cell is built by

<tableEntryOID>.<columnSubID>.<rowIndexOID>

Thus, if your table object has the OID 1.3.6.1.4.1.1.99.5.4.1.3.1 and you want the first instance in the first column to have the ID 1 then you have to create/add the row with:

model.addRow(       
    model.createRow(       
            new OID(1), new Variable[]{ new Integer32(123) })); 

This cell can then be retrieved with

snmpget -v2c -c public localhost:4700 1.3.6.1.4.1.1.99.5.4.1.3.1.1.1



回答2:


ok, I found what was wrong. Actually, my code works, it was only my requests which weren't correct.

                                        | oidColumn 1.3.6.1.4.1.1.99.5.4.1.3.1.1
 oidRow1 1.3.6.1.4.1.1.99.5.4.1.3.1.1   |       oidColumn.oidRow1
 oidRow2 1.3.6.1.4.1.1.99.5.4.1.3.1.2   |       oidColumn.oidRow2

So if I want to get the first value of the Row1, I have to make this request:

snmpget -v2c -c public localhost:4700 1.3.6.1.4.1.1.99.5.4.1.3.1.1.1.3.6.1.4.1.1.99.5.4.1.3.1.1


来源:https://stackoverflow.com/questions/12193882/snmp-agent-in-java-how-adding-new-rows-in-motable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!