2011-09-14 71 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