[SNMP4J] How to handle traps that come from a specific ip address only

2010-08-23 Thread Tsiyona

I am writing a trap receiver that only intercepts traps and analyzes them.

In our system we have two servers on which snmp agents are generating traps.
At a given time, only one server out of the two is an active server, meaning
that only this
server's traps are to be processed, while the other server's traps are to be
These two servers have distinct ip addresses (different from one another's).

Is there a way I can open an snmp session and start listening only for traps
arrive from a specific ip address (i.e. from the active serve's ip address
only) ?

Of course I could check the agent address in each received trap pdu and
ignore those
traps that come from a non-active server, but I am asking here whether
snmp4j infrastructure
itself provides means of doing that.

SNMP4J mailing list

[SNMP4J] A Simple Trap Receiver using snmp4j

2010-08-17 Thread Tsiyona

I am trying to write a simple snmp manager that listens to traps on port 162
and processes them. This manager will only handle traps, and does not need
to send commands to agents.

Attached is the code I wrote. It is straightforward, but it does not work -
I do not get any snmp traps.
I make sure that the snmp agent really constantly generates traps, by using
a network sniffer, so my
problem is not lack of traps on the agent side. I am probably doing
something wrong.

Can anyone please help me?


My code:

package snmpmanager;

import java.io.IOException;

import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.transport.TransportMappings;

* A Simple Trap receiver using snmp4j.
public class SnmpClient

public static final String TRAP_ADDRESS =;//udp:;
 private TransportMapping transport;
private String listeningAddress;
 private Snmp snmp;

public SnmpClient(String address)
 this.listeningAddress = address;
try {
 } catch (IOException e) {
throw new RuntimeException(e);

private void start() throws IOException
UdpAddress udpAddress = new UdpAddress(this.listeningAddress);
transport = new DefaultUdpTransportMapping(udpAddress);
 snmp = new Snmp(transport);
 CommandResponder pduHandler = new CommandResponder() {
 public synchronized void processPdu(CommandResponderEvent e) {
   PDU pdu = e.getPDU();
   if (pdu != null) {
 public void stop() throws IOException

public static void main(String[] args)
SnmpClient client = new SnmpClient(TRAP_ADDRESS);
System.out.println(Listening for traps on address  + TRAP_ADDRESS);
SNMP4J mailing list