Skip to content

Instantly share code, notes, and snippets.

@nahuel-dallavecchia
Created January 27, 2012 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nahuel-dallavecchia/1690896 to your computer and use it in GitHub Desktop.
Save nahuel-dallavecchia/1690896 to your computer and use it in GitHub Desktop.
package com.mulesoft;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.MessageDispatcher;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.mp.MPv1;
import org.snmp4j.mp.MPv2c;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.TransportIpAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.AbstractTransportMapping;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;
public class SnmpHelper implements CommandResponder {
private static final String THREAD_POOL_NAME = "SnmpDispatcherPool";
private static final OctetString SECURITY_NAME = new OctetString("SECURITY");
private static final byte[] ENGINE_ID = MPv3.createLocalEngineID();
private final List<PDU> pduList;
private ThreadPool threadPool;
private Snmp snmp;
/**
* SNMP version 1 and 2c.
*/
public static SnmpHelper getLocalhostCommunityTargetInstance(int port) {
final SnmpHelper snmpHelper = new SnmpHelper();
try {
snmpHelper.listenForTrapCommunityTarget("localhost", port);
} catch (IOException e) {
throw new RuntimeException("Snmp Community Target setup", e);
}
return snmpHelper;
}
/**
* SNMP version 3.
*/
public static SnmpHelper getLocalhostUserTargetInstance(int port, String userName, String password, String passphrase) {
final SnmpHelper snmpHelper = new SnmpHelper();
try {
snmpHelper.listenForTrapUserTarget("localhost", port, userName, password, passphrase);
} catch (IOException e) {
throw new RuntimeException("Snmp User Target setup", e);
}
return snmpHelper;
}
private SnmpHelper() {
pduList = new ArrayList<PDU>();
}
/**
* @param host (eg. "localhost")
* @param port (eg. "164")
*/
private synchronized void listenForTrapCommunityTarget(String host, int port) throws IOException {
final TransportIpAddress address = new UdpAddress(host + "/" + port);
final AbstractTransportMapping transport;
if (address instanceof TcpAddress) {
transport = new DefaultTcpTransportMapping((TcpAddress) address);
} else {
transport = new DefaultUdpTransportMapping((UdpAddress) address);
}
threadPool = ThreadPool.create(THREAD_POOL_NAME, 10);
final MessageDispatcher multiThreadMessagetDispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
// Add message processing models.
multiThreadMessagetDispatcher.addMessageProcessingModel(new MPv1());
multiThreadMessagetDispatcher.addMessageProcessingModel(new MPv2c());
SecurityProtocols.getInstance().addDefaultProtocols();
snmp = new Snmp(multiThreadMessagetDispatcher, transport);
snmp.addCommandResponder(this);
transport.listen();
}
/**
* @param host (eg. "localhost")
* @param port (eg. "164")
*/
private synchronized void listenForTrapUserTarget(String host, int port, String userName, String password, String passphrase) throws IOException {
final TransportIpAddress address = new UdpAddress(host + "/" + port);
final AbstractTransportMapping transport;
if (address instanceof TcpAddress) {
transport = new DefaultTcpTransportMapping((TcpAddress) address);
} else {
transport = new DefaultUdpTransportMapping((UdpAddress) address);
}
threadPool = ThreadPool.create(THREAD_POOL_NAME, 10);
final MessageDispatcher multiThreadMessagetDispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
// Add message processing models.
multiThreadMessagetDispatcher.addMessageProcessingModel(new MPv3());
SecurityProtocols.getInstance().addDefaultProtocols();
snmp = new Snmp(multiThreadMessagetDispatcher, transport);
// User Based Security Model.
final USM usm = createSecurityModel(userName, password, passphrase);
SecurityModels.getInstance().addSecurityModel(usm);
snmp.addCommandResponder(this);
transport.listen();
}
protected USM createSecurityModel(String userName, String password, String passphrase) {
final USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(ENGINE_ID), 0);
final UsmUser user = new UsmUser(SECURITY_NAME, AuthMD5.ID, new OctetString(password), PrivDES.ID, new OctetString(passphrase));
usm.addUser(new OctetString(userName), user);
return usm;
}
/**
* This method will be called whenever a PDU is received on the given port specified when
* instantiating this class.
*/
@Override
public synchronized void processPdu(CommandResponderEvent commandResponderEvent) {
if (commandResponderEvent == null) {
throw new RuntimeException("Command Responder Event expected to be not null");
}
final PDU pdu = commandResponderEvent.getPDU();
if (pdu != null) {
if (pdu.getErrorStatus() == PDU.noError) {
addPduToList(pdu);
} else {
throw new RuntimeException("Error while retrieving PDU trap fro SNMP: Error Status: " + pdu.getErrorStatus() + ", Error Index: " + pdu.getErrorIndex()
+ ", Error Status Text: " + pdu.getErrorStatusText());
}
}
}
public synchronized void clearPduList() {
pduList.clear();
}
public synchronized void addPduToList(PDU pdu) {
pduList.add(pdu);
}
public synchronized List<PDU> getPduList() {
return pduList;
}
public synchronized void releaseInstance() {
if (threadPool != null) {
threadPool.stop();
}
try {
if (snmp != null) {
snmp.close();
}
} catch (IOException e) {
// Do nothing.
}
}
public synchronized List<PDU> getPduListAndClear() {
final List<PDU> result = new ArrayList<PDU>(getPduList());
clearPduList();
return result;
}
@Override
public void finalize() throws Throwable {
releaseInstance();
super.finalize();
}
}
@trunghoangminh
Copy link

Hi guys,

Why do you use ThreadPool with value 10? Can you explain for me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment