我正在使用SNMP4J发送SNMPv2陷阱。这个java应用程序将运行,发送陷阱并退出。
现在我有一个问题,每当第一轮我运行这个应用程序时,它执行得非常快。之后,从第二轮开始,完成snmp.send(pdu,comtarget)平均需要50多秒;
希望可以从你们那里得到一些想法
我在下面添加了我的代码和输出:
public void sendSnmpV2Trap(String message) {
try {
// Create Transport Mapping
long start = System.currentTimeMillis();
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
long end = System.currentTimeMillis();
long diff = end - start;
LOGGER.debug("TransportMapping listening took "+ diff + " ms");
start = System.currentTimeMillis();
// Create Target
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(community));
comtarget.setVersion(SnmpConstants.version2c);
comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(500);
// Create PDU for V2
PDU pdu = new PDU();
// variable binding for Enterprise Specific objects, Severity (should be defined in MIB file)
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OctetString("1.2")));
pdu.add(new VariableBinding(new OID(trapOid), new OctetString(message)));
pdu.setType(PDU.NOTIFICATION);
end = System.currentTimeMillis();
diff = end - start;
LOGGER.debug("Create object took "+ diff + " ms");
start = System.currentTimeMillis();
// Send the PDU
Snmp snmp = new Snmp(transport);
LOGGER.info("Sending V2 Trap to " + ipAddress + " on Port " + port+". Message="+ message);
snmp.send(pdu, comtarget);
snmp.close();
end = System.currentTimeMillis();
diff = end - start;
LOGGER.debug("Send trap took "+ diff + " ms");
} catch (Exception e) {
LOGGER.error("Error in Sending V2 Trap to " + ipAddress + " on Port " + port);
LOGGER.error("Exception Message = " + e.getMessage());
}
}
输出:
[root@responder01 healthCheck]# java -jar healthChecker.jar --test
2015-04-17 10:31:21 DEBUG SnmpJTrapSender:113 - TransportMapping listening took 19 ms
2015-04-17 10:31:21 DEBUG SnmpJTrapSender:133 - Create object took 6 ms
2015-04-17 10:31:21 INFO SnmpJTrapSender:138 - Sending V2 Trap to 192.168.6.45 on Port 162. Message=testing on Apr 17, 2015 10:31:21 AM
2015-04-17 10:31:21 DEBUG SnmpJTrapSender:143 - Send trap took 79 ms
[root@responder01 healthCheck]# java -jar healthChecker.jar --test
2015-04-17 10:31:24 DEBUG SnmpJTrapSender:113 - TransportMapping listening took 18 ms
2015-04-17 10:31:24 DEBUG SnmpJTrapSender:133 - Create object took 8 ms
2015-04-17 10:32:32 INFO SnmpJTrapSender:138 - Sending V2 Trap to 192.168.6.45 on Port 162. Message=testing on Apr 17, 2015 10:31:23 AM
2015-04-17 10:32:32 DEBUG SnmpJTrapSender:143 - Send trap took 68546 ms
答案 0 :(得分:0)
根据您输出的代码
LOGGER.debug("Create object took "+ diff + " ms");
start = System.currentTimeMillis();
// Send the PDU
Snmp snmp = new Snmp(transport);
LOGGER.info("Sending V2 Trap to " + ipAddress + " on Port " + port+". Message="+ message);
花时间。
这里没有什么需要花费这么多时间,所以也许整个系统都在爬行。