SNMP Server Connection
I am sending a SNMP trap to the SNMP server. But I have to print the log if the connection to the server is not established.
UdpAddress targetAddress = new UdpAddress("127.0.0.1/1985");
CommunityTarg开发者_运维知识库et target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1000);
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
**// I HAVE TO CHECK WETHER CONNECTION IS ESTABLISHED OR NOT WITH SNMP SERVER. AS OF
NOW EVEN IF I DO NOT START SNMP SERVER THAN I AM NOT GETTING ANY EXCEPTION and IF I START
SNMP SERVER THEN MESSAGE HAS BEEN SENT TO SNMP SERVER.....MAY BE IN SEND METHOD..ITS
CONNECTS WITH SNMP SERVER....BUT I TRIED TO FIND OUT BUT COULDNT ABLE TO DO IT.....**
ResponseEvent responseEvent = snmp.send(pdu, target);
UDP is connectionless, so you have no way to know if the packet is received in this case.
as you been told UDP is connectionless, so you have no way to know if the packet is received in this case.
but if you really must to know that the SNMP manager is alive before you send the trap. you can create your own a handshake.
the logic is pretty simple and it goes something like that:
1) create in the MIB new leaf to indicates if the manager is connected and initial it to false.
2) send a trap to the manager.
2.1) if the manager is alive (receive the trap)
2.1.1) the manager send a set-request to the leaf with true.
the agent can read the value from it's on MIB and know if the manager is listening to traps
you can expend and improve the logic but the basic idea is clear I think
Although you want to send SNMP trap/notification in code you are doing the following
PDU command = new PDU();
command.setType(PDU.GET);
The above will result in sending an SNMP get request which should ideally fetch you a response however the port number (source or your client's source address) is where you should be listening. The above code snippet has some basic flaws as a result you are not getting desired results.
Some links that you may want to read up on SNMP4j to send notifications
https://www.jayway.com/2010/05/21/introduction-to-snmp4j/
http://lists.agentpp.org/pipermail/snmp4j/2006-April/001219.html
精彩评论