2011-09-14 74 views
1

我发送SNMP陷阱到SNMP服务器。但是如果与服务器的连接没有建立,我必须打印日志。SNMP服务器连接

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(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); 

回答

4

听起来像你甚至不知道UDP够好。它是无连接的,所以你无法知道在这种情况下是否收到数据包。

+0

从理论上说,将运行在TCP SNMP的可能性。有一个标准 - RFC3440 - 和一个方法。虽然没有常见的用法。 – tofro

+0

@tofro这是一个草案,这意味着并非所有的供应商都支持它。 –

-1

虽然要发送SNMP陷阱/通知,代码你正在做以下

PDU command = new PDU(); 
command.setType(PDU.GET); 

以上将导致发送SNMP get请求理想情况下应该给你拿但是一个响应的端口号(源或您客户的来源地址)是您应该听的地方。上面的代码片段有一些基本的缺陷,结果你没有得到想要的结果。

,你可能要在SNMP4J读了一些链接发送通知

https://www.jayway.com/2010/05/21/introduction-to-snmp4j/

http://lists.agentpp.org/pipermail/snmp4j/2006-April/001219.html