Java基于Snmp4j庫實(shí)現(xiàn)SNMP協(xié)議的調(diào)用方式
在進(jìn)行SNMP協(xié)議調(diào)用之前,要先保證主機(jī)和目的機(jī)器可以通信,可以用ping工具來嘗試連通性,如果可以通信,可直接跳轉(zhuǎn)到第二章節(jié)和第三章節(jié)查看協(xié)議的調(diào)用;如果不能通信,即不在同一個(gè)網(wǎng)段,那么需要先按照第一章節(jié)的步驟設(shè)置路由表,再根據(jù)第二章節(jié)和第三章節(jié)的步驟實(shí)現(xiàn)協(xié)議的調(diào)用
1. 設(shè)置路由表
1.1 原理
如果windows所在網(wǎng)段和lxd容器【即SNMP協(xié)議需要訪問的設(shè)備】所在網(wǎng)段不一致,我們不能直接訪問,則需要進(jìn)行路由表的設(shè)置
以如下ip地址為例,進(jìn)行路由表的設(shè)置,其中
windows的ip地址為192.168.31.224Linux服務(wù)器的ip地址為192.168.31.104Linux服務(wù)器中lxd容器的ip地址為10.137.5.86,網(wǎng)關(guān)地址通過route -n查看后得知為10.137.5.1
所以思路為:
每當(dāng)windows訪問容器的時(shí)候,將流量轉(zhuǎn)發(fā)到服務(wù)器中
同理,容器回復(fù)windows的時(shí)候,因?yàn)榫W(wǎng)關(guān)本身可以和服務(wù)器連通,所以流量轉(zhuǎn)發(fā)到網(wǎng)關(guān)中即可
1.2 設(shè)置
windows中的設(shè)置
# 查看路由表 route print # 將訪問ip地址為10.137.5.86,且掩碼為255.255.255.255的流量轉(zhuǎn)發(fā)到192.168.31.104中 route add 10.137.5.86 mask 255.255.255.255 192.168.31.104 # 刪除路由表 route delete 10.137.5.86 mask 255.255.255.255 192.168.31.104

lxd容器中的設(shè)置
# 查看路由表 ip route # 將訪問ip地址為192.168.31.224/32的流量轉(zhuǎn)發(fā)到10.137.5.1中 ip route add 192.168.31.224/32 via 10.137.5.1 # 刪除路由表 ip route del 192.168.31.224/32 via 10.137.5.1

此時(shí),我們才可以保證windows訪問lxd容器

2. Java實(shí)現(xiàn)SNMP簡(jiǎn)單操作
2.1 導(dǎo)入依賴
<dependency>
<groupId>org.snmp4j</groupId>
<artifactId>snmp4j</artifactId>
<version>2.8.15</version>
</dependency>
2.2 設(shè)置連接
建立一個(gè)類來存儲(chǔ)常見oid
//定義snmp類,存儲(chǔ)常量
public class MySnmp {
//設(shè)備描述信息
public final String Sys_Desc = "1.3.6.1.2.1.1.1";
//設(shè)備開啟時(shí)間
public final String Sys_Up_Time = "1.3.6.1.2.1.1.5";
//設(shè)備名稱
public final String Sys_Name = "1.3.6.1.2.1.1.3";
//網(wǎng)卡接口速率
public final String If_Speed = "1.3.6.1.2.1.2.2.1.5.126";
//網(wǎng)卡接口當(dāng)前時(shí)刻進(jìn)流量
public final String If_In_Octets = "1.3.6.1.2.1.2.2.1.10.126";
//網(wǎng)卡接口當(dāng)前時(shí)刻出流量
public final String If_Out_Octets = "1.3.6.1.2.1.2.2.1.16.126";
}
建立連接以及傳參函數(shù)
//用來獲取設(shè)備信息
public class GetMessage {
public String getMessageByIpAndOid(String ip,String oid) throws IOException {
String result = null ;
// 1. 創(chuàng)建 SNMP 管理器
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(GenericAddress.parse("udp:"+ip+"/161"));
target.setRetries(2);
target.setTimeout(1000);
TransportMapping<UdpAddress> transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
snmp.listen();
// 2. 創(chuàng)建 OID
OID oid1 = new OID(oid);
// 3. 發(fā)送 SNMP 請(qǐng)求并處理響應(yīng)
PDU pdu = new PDU();
pdu.add(new VariableBinding(oid1));
pdu.setType(PDU.GETNEXT);
ResponseEvent event = snmp.send(pdu, target);
PDU response = event.getResponse();
if (response == null) {
System.out.println("沒有得到響應(yīng)");
} else {
result = String.valueOf(response.get(0).getVariable());
}
// 4. 關(guān)閉 SNMP 管理器
snmp.close();
return result;
}
}
2.3 測(cè)試獲取信息
public class Test {
public static void main(String[] args) throws IOException {
MySnmp mySnmp = new MySnmp();
GetMessage getMessage = new GetMessage();
System.out.println("獲取容器信息:"+getMessage.getMessageByIpAndOid("10.137.5.86",mySnmp.Sys_Desc));
System.out.println("獲取容器名稱:"+getMessage.getMessageByIpAndOid("10.137.5.86",mySnmp.Sys_Name));
System.out.println("獲取容器開啟時(shí)長(zhǎng):"+getMessage.getMessageByIpAndOid("10.137.5.86",mySnmp.Sys_Up_Time));
System.out.println("獲取容器eth0網(wǎng)口當(dāng)前速率:"+getMessage.getMessageByIpAndOid("10.137.5.86",mySnmp.If_Speed));
System.out.println("獲取容器eth0網(wǎng)口當(dāng)前時(shí)刻進(jìn)流量:"+getMessage.getMessageByIpAndOid("10.137.5.86", mySnmp.If_In_Octets));
System.out.println("獲取容器eth0網(wǎng)口當(dāng)前時(shí)刻出流量:"+getMessage.getMessageByIpAndOid("10.137.5.86", mySnmp.If_Out_Octets));
}
}

3. Java實(shí)現(xiàn)SNMP流量監(jiān)控
第二章節(jié)實(shí)現(xiàn)了利用snmp的簡(jiǎn)單操作,本章節(jié)介紹如何實(shí)現(xiàn)流量監(jiān)控
思路如下:
- 創(chuàng)建
io流用來寫入日志文件 for循環(huán)連續(xù)讀取當(dāng)前時(shí)刻流量- 利用線程進(jìn)行休眠來控制讀取時(shí)間間隔
- 將流量寫入日志文件并且刷新
//實(shí)現(xiàn)流量監(jiān)控
public class TrafficMonitoring {
public void getTrafficMonitoring(int time) throws IOException {
//獲取oid
MySnmp mySnmp = new MySnmp();
//輸入流
FileOutputStream fos = null;
OutputStreamWriter writer = null;
BufferedWriter bf = null;
//根據(jù)oid獲取結(jié)果
GetMessage getMessage = new GetMessage();
try {
//表示內(nèi)容的追加
fos = new FileOutputStream("src/main/java/org/example/logFile.log",true);
writer = new OutputStreamWriter(fos);
bf = new BufferedWriter(writer);
bf.write("====================切割線==========================\n");
//格式化當(dāng)前時(shí)間
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (int i = 0 ; i<time ; i++) {
Date date = new Date(System.currentTimeMillis());
//獲得當(dāng)前時(shí)間
String nowTime = formatter.format(date);
//活得當(dāng)前流量
String If_In_Octets = getMessage.getMessageByIpAndOid("10.137.5.86", mySnmp.If_In_Octets);
String If_Out_Octets = getMessage.getMessageByIpAndOid("10.137.5.86", mySnmp.If_Out_Octets);
bf.write(nowTime+"-"+"If_In_Octets:"+If_In_Octets+"bytes,If_Out_Octets:"+If_Out_Octets+"bytes\n");
Thread.sleep(1000);
}
bf.flush();
System.out.println(time+"秒內(nèi)的流量監(jiān)控完畢,請(qǐng)查看日志文件");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (fos != null) {
fos.close();
}
if (writer != null) {
writer.close();
}
if (bf != null) {
bf.close();
}
}
}
}

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?BeanPostProcessor后處理器源碼解析
這篇文章主要介紹了Spring?BeanPostProcessor后處理器源碼解析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-09-09
Java實(shí)現(xiàn)字符串倒序輸出的四種方法匯總
這篇文章主要介紹了Java實(shí)現(xiàn)字符串倒序輸出的四種方法匯總,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Spring Boot Shiro在Web應(yīng)用中的作用詳解
這篇文章主要為大家介紹了Spring Boot Shiro在Web應(yīng)用中的作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
java使用Socket實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了java使用Socket實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
SpringCloud:feign對(duì)象傳參和普通傳參及遇到的坑解決
這篇文章主要介紹了SpringCloud:feign對(duì)象傳參和普通傳參及遇到的坑解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

