我正在尝试使用SNMP4j代理在SNMP代理上注册一组MO。
public class MyAgent extends BaseAgent {
public MyAgent() {
super(new File("bootCounterFileTmpSNMP.txt"), new File("configFileTmpSNMP.txt"),
new CommandProcessor(new OctetString(MPv3.createLocalEngineID())));
}
@Override
protected void initTransportMappings() throws IOException {
transportMappings = new TransportMapping<?>[1];
Address addr = GenericAddress.parse("0.0.0.0/8551"); // not 161 so we dont have to run sudo
TransportMapping<? extends Address> tm = TransportMappings.getInstance().createTransportMapping(addr);
transportMappings[0] = tm;
}
public void start() throws IOException {
init();
addShutdownHook();
getServer().addContext(new OctetString("public"));
finishInit();
run();
sendColdStartNotification();
}
@Override
protected void registerManagedObjects() {
getSnmpv2MIB().unregisterMOs(server, getContext(getSnmpv2MIB()));
registerManagedObject(new MOScalar(new OID("1.3.6.1.4.1.32437.1.5.1.4.2"), MOAccessImpl.ACCESS_READ_ONLY, new OctetString("object 1")));
registerManagedObject(new MOScalar(new OID("1.3.6.1.4.1.32437.1.5.1.2.20"), MOAccessImpl.ACCESS_READ_ONLY, new OctetString("object 2")));
registerManagedObject(new MOScalar(new OID("1.3.6.1.4.1.32437.1.5.1.2.23"), MOAccessImpl.ACCESS_READ_ONLY, new OctetString("object 3")));
registerManagedObject(new MOScalar(new OID("1.3.6.1.4.1.32437.1.5.1.1.21"), MOAccessImpl.ACCESS_READ_ONLY, new OctetString("object 4")));
registerManagedObject(new MOScalar(new OID("1.3.6.1.4.1.32437.1.5.1.4.2.1.2"), MOAccessImpl.ACCESS_READ_ONLY, new OctetString("object 5")));
}
private void registerManagedObject(MOScalar mo) {
try {
server.register(mo, null);
System.out.print("Successfully registered ");
System.out.println(mo.getID());
} catch (DuplicateRegistrationException e) {
System.out.print("Failed to register ");
System.out.println(mo.getID());
}
}
@Override
protected void unregisterManagedObjects() {
// do nothing
}
@Override
protected void addUsmUser(USM usm) {
// do nothing
}
@Override
protected void addNotificationTargets(SnmpTargetMIB targetMIB, SnmpNotificationMIB notificationMIB) {
// do nothing
}
@Override
protected void addViews(VacmMIB vacmMIB) {
vacmMIB.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c, new OctetString("cpublic"), new OctetString("v1v2group"),
StorageType.nonVolatile);
vacmMIB.addAccess(new OctetString("v1v2group"), new OctetString("public"), SecurityModel.SECURITY_MODEL_ANY,
SecurityLevel.NOAUTH_NOPRIV, MutableVACM.VACM_MATCH_EXACT, new OctetString("fullReadView"),
new OctetString("fullWriteView"), new OctetString("fullNotifyView"), StorageType.nonVolatile);
vacmMIB.addViewTreeFamily(new OctetString("fullReadView"), new OID(".1.3"), new OctetString(),
VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
}
@Override
protected void addCommunities(SnmpCommunityMIB communityMIB) {
Variable[] com2sec = new Variable[] {
new OctetString("public"), // community name
new OctetString("cpublic"), // security name
getAgent().getContextEngineID(), // local engine ID
new OctetString("public"), // default context name
new OctetString(), // transport tag
new Integer32(StorageType.nonVolatile), // storage type
new Integer32(RowStatus.active) // row status
};
SnmpCommunityMIB.SnmpCommunityEntryRow row = communityMIB.getSnmpCommunityEntry().createRow(
new OctetString("public2public").toSubIndex(true), com2sec);
communityMIB.getSnmpCommunityEntry().addRow(row);
}
}
然后运行它:
agent = new MyAgent();
agent.start();
这里的输出是:
Successfully registered 1.3.6.1.4.1.32473.1.5.1.4.2
Successfully registered 1.3.6.1.4.1.32473.1.5.1.2.20
Failed to register 1.3.6.1.4.1.32473.1.5.1.2.23
Successfully registered 1.3.6.1.4.1.32473.1.5.1.1.21
Failed to register 1.3.6.1.4.1.32473.1.5.1.4.2.1.2
我得到与地址结构或顺序有关的DuplicateRegistrationException的原因吗?
每个对象都是一个org.snmp4j.agent.mo.MOScalar
值唯一的OctetString
。
答案 0 :(得分:0)
对于那些跟随我的人,这里的关键是在每个地址的末尾添加一个.0
。
这个额外的零标识该OID处的变量,并且意味着地址不会彼此冲突。没有它,它只是到位置的路径,而没有为变量本身创建位置。
所以更正的代码是:
registerManagedObject(new MOScalar(new OID("1.3.6.1.4.1.32437.1.5.1.4.2.0"), MOAccessImpl.ACCESS_READ_ONLY, new OctetString("object 1")));
registerManagedObject(new MOScalar(new OID("1.3.6.1.4.1.32437.1.5.1.2.20.0"), MOAccessImpl.ACCESS_READ_ONLY, new OctetString("object 2")));
registerManagedObject(new MOScalar(new OID("1.3.6.1.4.1.32437.1.5.1.2.23.0"), MOAccessImpl.ACCESS_READ_ONLY, new OctetString("object 3")));
registerManagedObject(new MOScalar(new OID("1.3.6.1.4.1.32437.1.5.1.1.21.0"), MOAccessImpl.ACCESS_READ_ONLY, new OctetString("object 4")));
registerManagedObject(new MOScalar(new OID("1.3.6.1.4.1.32437.1.5.1.4.2.1.2.0"), MOAccessImpl.ACCESS_READ_ONLY, new OctetString("object 5")));