[SNMP4J] Writinf SNMP Manager using SNMP4J
Tsiyona.Dershowitz at alcatel.co.il
Tsiyona.Dershowitz at alcatel.co.il
Sun Mar 26 17:24:26 CEST 2006
Hello,
I am writing a SNMP Manager that is supposed to do the following:
(1) Send snmp commands to snmp-agents, and handle their response.
(2) Receive snmp traps from snmp-agents.
I am using SNMP4J software package for writing the snmp manager.
I would like to use a SINGLE Snmp object (SNMP4J object) , both
for sending a snmp command and for receiving snmp traps.
So far, I have written two separate programs:
****************
*** Program 1:
****************
** The first program only listens for Snmp trap-PDUs.
** Its main code is as follows:
// configure Snmp object
UdpAddress address = new UdpAddress("127.0.0.1/1990");
TransportMapping transport = new DefaultUdpTransportMapping(address);
Snmp snmp = new Snmp(transport);
transport.listen();
// handle received traps here
CommandResponder trapPrinter = new CommandResponder() {
public synchronized void processPdu(CommandResponderEvent e) {
PDU command = e.getPdu();
if (command != null) {
System.out.println(command.toString());
}
}
};
snmp.addCommandResponder(trapPrinter);
****************
*** Program 2:
****************
** The second program sends GET PDUs, and its main code is as follows:
// configure Snmp object
UdpAddress targetAddress = new UdpAddress("127.0.0.1/1985");
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(10000);
target.setVersion(SnmpConstants.version1);
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
// prepare the PDU for sending
PDU command = new PDU();
command.setType(PDU.GET);
command.add(new VariableBinding(new OID("1.3.6.1.4.1.1331.11.5.1.0")));
// now send the PDU
ResponseEvent responseEvent = snmp.send(pdu, target);
if (responseEvent != null)
{
// response has arrived. handle it
}
else
System.out.println("null response received...");
*******************************************************************
I would like to unite these two programs into one, however, I cannot
seem to
properly allocate one Snmp object to handle both the sending and
receiving of PDUs.
I run two threads: thread-1 that does the listening for snmp traps,
and thread-2 which in charge of sending get/set PDUs.
I allocate the Snmp object in one place as follows:
TransportMapping transport = new DefaultUdpTransportMapping(new
UdpAddress("127.0.0.1/1990"));
Snmp snmp = new Snmp(transport);
snmp.listen();
and I transfer it to both threads to be used there.
However, thread-2 sometimes fails to send PDUs using snmp.send(...);
What am I doing wrong?
Can anyone guide me as for how I should allocate the Snmp object so
it is good for both sending PDUs, and for receiving traps?
Thanks,
Tsiyona
More information about the SNMP4J
mailing list