SNMP4j 代理中的 DuplicateRegistrationException
DuplicateRegistrationException in SNMP4j Agent
我正在尝试使用 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 的原因是否与地址结构或排序有关?
每个对象都是一个具有唯一 OctetString
值的 org.snmp4j.agent.mo.MOScalar
。
相关:registering OIDs with snmp agent
对于那些遵循我的路径的人,这里的关键是在每个地址的末尾添加一个 .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")));
我正在尝试使用 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 的原因是否与地址结构或排序有关?
每个对象都是一个具有唯一 OctetString
值的 org.snmp4j.agent.mo.MOScalar
。
相关:registering OIDs with snmp agent
对于那些遵循我的路径的人,这里的关键是在每个地址的末尾添加一个 .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")));