Wednesday, June 3, 2009

Multi-threaded SNMP Polling

Learned how to implement a thread pool in Java today.  This is pretty straight forward to implement (assuming you setup the thread class correctly of course).  The key is to setup a class to execute as a single thread (although there is no reason the thread could not call additional threads).   Then, the process is fairly straight forward.

For SNAT, I created a 'poller' class which executes the SNMP polling for a single host and all of the associated interfaces.  From within the poller class, there is a number of database queries (gather SNMP parameters, interface information, etc).  These all execute just as in the previous version 'for' loop.  However, with the threaded implementation, the iterations through the for loop create a task for the thread pool.  The Java ExecutorService handles the thread scheduling tasks (which made this job much easier).

Here is the before thread code:

for (long hostPKEY : hostList) {


System.out.println("Found host pkey: " + hostPKEY);


poller(hostPKEY);


}  // End hostPKEY for loop



The threaded version looks like the following:

// Create the thread pool


ExecutorService threadPool = Executors.newFixedThreadPool(numberThreads);


for (long hostPKEY : hostList) {


System.out.println("Found host pkey: " + hostPKEY);


threadPool.execute(new poller(hostPKEY));


}  // End hostPKEY for loop



While executing the code, the output from the threads are all interlaced together (versus being serialized).  I do not have any data on the speed advantages for the threaded versus the non-threaded because I only have four hosts in the database.

The real test will come after I get the scanning component enabled (as opposed to hand entering the data for testing).  Also, I decided not to do the threads on a per-interface basis at this time.  This is to avoid overloading an individual host with too many bulk SNMP requests simultaneously.

No comments:

Post a Comment