Receiving SNMPv2 traps using snmp4j in Java

Hello,
I’m new to snmp4j.

I’ve used v 2.8.6 and the recipe at http://techdive.in/snmp/snmp4j-trap-receiver to implement a CommandResponder to receive traps on port 162 from a UPS device. Using tcpdump, I see traffic from the agent to the host/port where I’m running the snmp4j program. However, the processPdu method is never called. I tried both addCommandResponder and addNotificationListener to no avail.

Any assistance would be appreciated.

Source code snippet:

public synchronized void listen(String address, String community) throws IOException
{
TransportMapping<?> transport;
//TRANSPORT
Address listenAddress = GenericAddress.parse(System.getProperty(
“snmp4j.listenAddress”, address));

if (listenAddress instanceof UdpAddress) {
	transport = new DefaultUdpTransportMapping(
			(UdpAddress) listenAddress);
} else {
	transport = new DefaultTcpTransportMapping(
			(TcpAddress) listenAddress);
}

ThreadPool threadPool = ThreadPool.create("DispatcherPool", 10);
MessageDispatcher mtDispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());

// add message processing models
mtDispatcher.addMessageProcessingModel(new MPv1());
mtDispatcher.addMessageProcessingModel(new MPv2c());

// add all security protocols
SecurityProtocols.getInstance().addDefaultProtocols();
SecurityProtocols.getInstance().addPrivacyProtocol(new Priv3DES());

//Create Target
CommunityTarget target = new CommunityTarget();
target.setCommunity( new OctetString(community));


Snmp snmp = new Snmp(mtDispatcher, transport);
snmp.addCommandResponder(this);
transport.listen();

System.out.println("Listening on " + transport.getListenAddress() + " - community " + community);

try
{
  this.wait();
}
catch (InterruptedException ex)
{
  Thread.currentThread().interrupt();
}

}

/**

  • This method will be called whenever a pdu is received on the given port specified in the listen() method
    */
    public synchronized void processPdu(CommandResponderEvent cmdRespEvent)
    {
    System.out.println(“Received PDU…”);

    }

Hi,

The name CommandResponder should indicate that this listener interface is designed to process “commands”. In SNMP, commands are defined as GET, GETNEXT, GETBULK, SET, and INFORM. Traps and notifications are not included.

Thus I recommend using the snmp.addNotificationListener method instead snmp.addCommandResponder for receiving notifications and traps.

BTW, the DEBUG log output would indicate why the received message is not forwarded to your application.

Best regards,
Frank

Thank you Frank. I’ve converted to addNotificationListener.

The lack of packet forwarding turned out to be a firewall problem. It is working now

Hal