3

I'm using Snmp4j 2.2.3 and I am observing a lag of up to 8 seconds when I construct a org.snmp4j.Snmp object via public Snmp(TransportMapping) I wonder if anyone knows where in Snmp I should focus my attention. I see this happening on Redhat Linux, but I don't see the lag when running from a Windows XP box.

In the following code, line 3 "Snmp snmp = new Snmp(transport);" is freezing for up to 8 seconds.

Address targetAddress = new UdpAddress(host + "/" + port);
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);

CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
target.setMaxSizeRequestPDU(65535);

snmp.listen();

Thanks

8BitCoder
  • 309
  • 1
  • 10

4 Answers4

3

Turin correctly pointed to the problem.

If you are only using SNMPv2c, you can make a cleaner initialization that does not load cryptography related classes of v3.

TransportMapping transport = new DefaultUdpTransportMapping();
MessageDispatcher disp = new MessageDispatcherImpl();
disp.addMessageProcessingModel(new MPv2c());
snmp = new Snmp(disp, transport);
...
snmp.listen();
Community
  • 1
  • 1
mamuso
  • 3,117
  • 3
  • 30
  • 32
2

I found the same issue with snmp4j 1.11.2. Adding -Djava.security.egd=file:/dev/./urandom to the jvm solved the problem.

The solution was found http://snmp4j.agentpp.narkive.com/xzGw7pL1/initialize-too-slow and How to solve performance problem with Java SecureRandom?

Community
  • 1
  • 1
Turin
  • 175
  • 5
1

Most likely, this is a problem in the RedHat random seed initialization of the operating system. As far as I know, there is a fix available for the OS that ensures the entropy computation does not take too much time.

ooSNMP
  • 337
  • 1
  • 8
0

I've seen this issue occur on red hat flavors that are running as VMs due to lack of access to hardware to generate entropy for urandom. If this is the case, installing haveged fixes the issue. Its an alternate source for entropy generation.

How to setup haveged: https://www.digitalocean.com/community/tutorials/how-to-setup-additional-entropy-for-cloud-servers-using-haveged

Security post related to haveged that mentions other methods of entropy generation: https://security.stackexchange.com/questions/34523/is-it-appropriate-to-use-haveged-as-a-source-of-entropy-on-virtual-machines

thaspius
  • 1,135
  • 3
  • 17
  • 33