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

2010-08-23 Thread Tsiyona
Hello,

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
ignored.
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
that
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.

Thanks,
Tsiyona
___
SNMP4J mailing list
SNMP4J@agentpp.org
http://lists.agentpp.org/mailman/listinfo/snmp4j


[SNMP4J] A Simple Trap Receiver using snmp4j

2010-08-17 Thread Tsiyona
Hello,

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?

Thanks,
Tsiyona


-
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 = 127.0.0.1/162;//udp:
127.0.0.1/162;
 private TransportMapping transport;
private String listeningAddress;
 private Snmp snmp;

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

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

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
SNMP4J@agentpp.org
http://lists.agentpp.org/mailman/listinfo/snmp4j