snmp4j agent 配置

djCode 2013-03-25 11:04:07
在开发snmp4j agent时候,在配置V3 用户时出现这样的问题.设置DES加密时总是设置不成功.不加DES没有问题.现在付上代码,那位高手指点一下.把SecurityLevel设成AUTH_NOPRIV,能正常访问,设成AUTH_PRIV就出现这样的错误.Encryption support not enabled.snmpgetnext: USM encryption error,但是我的用户名密码这些方式都是正确的.

public class SNMPAgent extends BaseAgent
{

private Snmp4jHeartbeatMib heartbeatMIB;
private AgentppSimulationMib agentppSimulationMIB;

static {
LogFactory.setLogFactory(new Log4jLogFactory());
}

private String address;

public SNMPAgent(File bootCounterFile, File configFile) throws IOException {
super(bootCounterFile, configFile,
new CommandProcessor(new OctetString(MPv3.createLocalEngineID())));
agent.setWorkerPool(ThreadPool.create("RequestPool", 4));
}

protected SNMPAgent(String address)
{
super(new File("conf.agent"), new File("bootCounter.agent"), new CommandProcessor(new OctetString(
MPv3.createLocalEngineID())));
this.address = address;
}

/**
* 设置团体字,此方法只限于V1,V2
* */
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
};
MOTableRow row =
communityMIB.getSnmpCommunityEntry().createRow(new OctetString("public2public").toSubIndex(true), com2sec);
communityMIB.getSnmpCommunityEntry().addRow(row);
}

/**
* 注册mibs
* */
protected void registerSnmpMIBs() {
heartbeatMIB = new Snmp4jHeartbeatMib(super.getNotificationOriginator(),
new OctetString(),
super.snmpv2MIB.getSysUpTime());
agentppSimulationMIB = new AgentppSimulationMib();
super.registerSnmpMIBs();
}

/**
* 设置组安全级别,及过滤方式.
*
*/
protected void addNotificationTargets(SnmpTargetMIB targetMIB, SnmpNotificationMIB notificationMIB)
{
targetMIB.addDefaultTDomains();

targetMIB.addTargetParams(new OctetString("v3notify"),
MessageProcessingModel.MPv3,
SecurityModel.SECURITY_MODEL_USM,
new OctetString("v3notify"),
SecurityLevel.AUTH_NOPRIV,
StorageType.permanent);
}

/**
* 设置安全账户,指定加密方式,指定私有加密协议.此方法只用于V3
* */
protected void addUsmUser(USM usm)
{
UsmUser user =
new UsmUser(
new OctetString("enocsnmpv3"), //账户名
AuthMD5.ID, //加密协议
new OctetString("enocsnmpv3pw"), //密码
PrivDES.ID, //私有协议类型
new OctetString("enocsnmpv3pk") //私有密码
);
usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);// ghu update
}


/**
* 此方法先创建安全组,向安全组授权相应的视图,最后定义视图
* */
protected void addViews(VacmMIB vacmMIB)
{
//把用户加入安全组,并说明用户是V3用户
vacmMIB.addGroup(SecurityModel.SECURITY_MODEL_USM,
new OctetString("enocsnmpv3"),
new OctetString("v3group"),
StorageType.permanent);

vacmMIB.addAccess(new OctetString("v3group"), new OctetString(),//ghu add public
SecurityModel.SECURITY_MODEL_USM,
SecurityLevel.AUTH_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);

vacmMIB.addViewTreeFamily(new OctetString("fullWriteView"), new OID("1.3"),
new OctetString(), VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);

vacmMIB.addViewTreeFamily(new OctetString("fullNotifyView"), new OID("1.3"),
new OctetString(), VacmMIB.vacmViewIncluded,
StorageType.nonVolatile);

}


/**
* 此方法向服务注册其它托管对象
* */
protected void registerManagedObjects()
{
try {
agentppSimulationMIB.registerMOs(server, null);
heartbeatMIB.registerMOs(server, null);
}
catch (DuplicateRegistrationException ex) {
ex.printStackTrace();
}

}

protected void unregisterManagedObjects()
{
agentppSimulationMIB.unregisterMOs(server, null);
heartbeatMIB.unregisterMOs(server, null);
}

protected void initTransportMappings()
throws IOException
{

transportMappings = new TransportMapping[1];

Address addr = GenericAddress.parse(address);

TransportMapping tm = TransportMappings.getInstance().createTransportMapping(addr);

transportMappings[0] = tm;

}

public static void main(String[] args) {
String address;
if (args.length > 0) {
address = args[0];
}
else {
address = "0.0.0.0/163";
}
BasicConfigurator.configure();
try {
SNMPAgent testAgent1 = new SNMPAgent(new File("SNMP4JTestAgentBC.cfg"),
new File("SNMP4JTestAgentConfig.cfg"));
testAgent1.address = address;
testAgent1.init();
testAgent1.loadConfig(ImportModes.REPLACE_CREATE);
testAgent1.addShutdownHook();
//testAgent1.getServer().addContext(new OctetString("public"));//多块网卡时指定上下文用于区分。
testAgent1.finishInit();
testAgent1.run();
testAgent1.sendColdStartNotification();

while (true) {
try {
Thread.sleep(1000);
}
catch (InterruptedException ex1) {
break;
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}

}

}
...全文
308 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lycplayer 2013-04-23
  • 打赏
  • 举报
回复
usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user); 问题应该出在这里,试试将第二个参数设成null

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧