Java中的SNMP代理:如何在MOTable中添加新行 [英] SNMP Agent in Java: How adding new rows in MOTable
问题描述
我正在尝试用Java实现SNMP代理。
我使用snmp4j库( http://www.snmp4j.org/ )。
目前,我的代理在localhost / 4700上工作。
由于以下请求,我试图发送snmpget请求:
snmpget -v2c -c public localhost:4700 1.3 .6.1.4.1.1.99.5.4.1.3.1.1
但我只得到类似没有这样的东西实例当前存在于此OID
这是我的问题:我不知道如何创建一个。我试图在我的MOTable中添加行,但它似乎不起作用。
这是我实现MOGRoup的类的摘要
公共类MyMIB
// - AgentGen BEGIN = _EXTENDS
// - AgentGen END
实现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){
//索引定义
。
。
。
//表模型
tableEntryModel =
moFactory.createTableModel(oidTableEntry,
tableEntryIndex,
tableEntryColumns);
((MOMutableTableModel)tableEntryModel)。setRowFactory(
new TableEntryRowFactory());
tableEntry =
moFactory.createTable(oidTableEntry,
tableEntryIndex,
tableEntryColumns,
tableEntryModel);
//添加行
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) )
}));
}
}
但是下面的请求仍然不起作用。
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
我必须不明白如何正确创建行。你能解释一下我该怎么做吗?
非常感谢你!
只是有点精确:我将这些行添加到我的程序中:
System.out.println(获取行数:+ tableEntryModel.getRowCount()) ;
//第一行
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})));
第一个回报:2(按预期)
第二次返回:123(正如预期的那样)
第三次返回:null ...这里,我不明白为什么!
我想说,代码和请求不匹配。我想代码就是问题,
,因为在SNMP4J-Agent中,表格单元格的实例OID是由
< tableEntryOID>。< columnSubID>。< rowIndexOID>
因此,如果您的表对象具有OID 1.3.6.1.4.1.1.99.5.4.1.3。 1,您希望第一列中的第一个实例具有ID 1,然后您必须创建/添加行:
model.addRow(
model.createRow(
new OID(1),new Variable [] {new Integer32(123)}));
然后可以使用
$ b <$检索此单元格p $ p>
snmpget -v2c -c public localhost:4700 1.3.6.1.4.1.1.99.5.4.1.3.1.1.1
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 !
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
这篇关于Java中的SNMP代理:如何在MOTable中添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!