如果windows
所在网段和lxd
容器【即SNMP
协议需要访问的设备】所在网段不一致,我们不能直接访问,则需要进行路由表的设置
以如下ip
地址为例,进行路由表的设置,其中
windows
的ip
地址为192.168.31.224
Linux
服务器的ip
地址为192.168.31.104
Linux
服务器中lxd
容器的ip
地址为10.137.5.86
,网关地址通过route -n
查看后得知为10.137.5.1
所以思路为:
每当windows
访问容器的时候,将流量转发到服务器中
同理,容器回复windows
的时候,因为网关本身可以和服务器连通,所以流量转发到网关中即可
windows
中的设置
# 查看路由表
route print
# 将访问ip地址为10.137.5.86,且掩码为255.255.255.255的流量转发到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
容器中的设置
# 查看路由表
ip route
# 将访问ip地址为192.168.31.224/32的流量转发到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
此时,我们才可以保证windows
访问lxd
容器
<dependency>
<groupId>org.snmp4jgroupId>
<artifactId>snmp4jartifactId>
<version>2.8.15version>
dependency>
建立一个类来存储常见oid
//定义snmp类,存储常量
public class MySnmp {
//设备描述信息
public final String Sys_Desc = "1.3.6.1.2.1.1.1";
//设备开启时间
public final String Sys_Up_Time = "1.3.6.1.2.1.1.5";
//设备名称
public final String Sys_Name = "1.3.6.1.2.1.1.3";
//网卡接口速率
public final String If_Speed = "1.3.6.1.2.1.2.2.1.5.126";
//网卡接口当前时刻进流量
public final String If_In_Octets = "1.3.6.1.2.1.2.2.1.10.126";
//网卡接口当前时刻出流量
public final String If_Out_Octets = "1.3.6.1.2.1.2.2.1.16.126";
}
建立连接以及传参函数
//用来获取设备信息
public class GetMessage {
public String getMessageByIpAndOid(String ip,String oid) throws IOException {
String result = null ;
// 1. 创建 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. 创建 OID
OID oid1 = new OID(oid);
// 3. 发送 SNMP 请求并处理响应
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("没有得到响应");
} else {
result = String.valueOf(response.get(0).getVariable());
}
// 4. 关闭 SNMP 管理器
snmp.close();
return result;
}
}
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("获取容器开启时长:"+getMessage.getMessageByIpAndOid("10.137.5.86",mySnmp.Sys_Up_Time));
System.out.println("获取容器eth0网口当前速率:"+getMessage.getMessageByIpAndOid("10.137.5.86",mySnmp.If_Speed));
System.out.println("获取容器eth0网口当前时刻进流量:"+getMessage.getMessageByIpAndOid("10.137.5.86", mySnmp.If_In_Octets));
System.out.println("获取容器eth0网口当前时刻出流量:"+getMessage.getMessageByIpAndOid("10.137.5.86", mySnmp.If_Out_Octets));
}
}
实现了利用snmp
命令行监控流量以及如何使用java
调用snmp
,下面使用java
调用snmp
监控流量
思路如下:
io
流用来写入日志文件for
循环连续读取当前时刻流量//实现流量监控
public class TrafficMonitoring {
public void getTrafficMonitoring(int time) throws IOException {
//获取oid
MySnmp mySnmp = new MySnmp();
//输入流
FileOutputStream fos = null;
OutputStreamWriter writer = null;
BufferedWriter bf = null;
//根据oid获取结果
GetMessage getMessage = new GetMessage();
try {
//表示内容的追加
fos = new FileOutputStream("src/main/java/org/example/logFile.log",true);
writer = new OutputStreamWriter(fos);
bf = new BufferedWriter(writer);
bf.write("====================切割线==========================\n");
//格式化当前时间
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (int i = 0 ; i<time ; i++) {
Date date = new Date(System.currentTimeMillis());
//获得当前时间
String nowTime = formatter.format(date);
//活得当前流量
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+"秒内的流量监控完毕,请查看日志文件");
} 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();
}
}
}
}