增加ping丢包率采集
This commit is contained in:
@@ -40,6 +40,9 @@ public class NetVO implements Serializable {
|
|||||||
@Schema(description = "出站丢包")
|
@Schema(description = "出站丢包")
|
||||||
private long outDropped;
|
private long outDropped;
|
||||||
|
|
||||||
|
@Schema(description = "ping丢包率")
|
||||||
|
private Double pingDropped;
|
||||||
|
|
||||||
@Schema(description = "发送流量(发送总字节)")
|
@Schema(description = "发送流量(发送总字节)")
|
||||||
private long outSpeed;
|
private long outSpeed;
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import com.tongran.agent.client.core.config.GlobalConfig;
|
|||||||
import com.tongran.agent.client.core.vo.NetVO;
|
import com.tongran.agent.client.core.vo.NetVO;
|
||||||
import com.tongran.agent.client.service.NetService;
|
import com.tongran.agent.client.service.NetService;
|
||||||
import com.tongran.agent.client.utils.AgentUtil;
|
import com.tongran.agent.client.utils.AgentUtil;
|
||||||
|
import com.tongran.agent.client.utils.AssertLog;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import oshi.SystemInfo;
|
import oshi.SystemInfo;
|
||||||
import oshi.hardware.HardwareAbstractionLayer;
|
import oshi.hardware.HardwareAbstractionLayer;
|
||||||
@@ -14,14 +17,15 @@ import java.io.BufferedReader;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class NetServiceImpl implements NetService {
|
public class NetServiceImpl implements NetService {
|
||||||
|
|
||||||
// 用于保存总流量统计
|
// 缓存最近一次的ping结果
|
||||||
private Map<String, Long> totalBytesRecvMap = new HashMap<>();
|
private final Map<String, Double> pingCache = new ConcurrentHashMap<>();
|
||||||
private Map<String, Long> totalBytesSentMap = new HashMap<>();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<NetVO> netList(long timestamp) {
|
public List<NetVO> netList(long timestamp) {
|
||||||
@@ -121,7 +125,52 @@ public class NetServiceImpl implements NetService {
|
|||||||
}
|
}
|
||||||
return 0L;
|
return 0L;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 启动异步ping监控
|
||||||
|
*/
|
||||||
|
private void startPingMonitorAsync(String interfaceName, long timeStapm) {
|
||||||
|
CompletableFuture.runAsync(() -> {
|
||||||
|
try {
|
||||||
|
Double lossRate = getPingLossRateSync(interfaceName);
|
||||||
|
if (lossRate != null) {
|
||||||
|
AssertLog.info("网卡 " + interfaceName + " 5分钟平均丢包率: " + lossRate + "%");
|
||||||
|
// 更新缓存,供下次使用
|
||||||
|
pingCache.put(interfaceName+timeStapm, lossRate);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
AssertLog.error("网卡" + interfaceName + "ping监控异常: " + e.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步获取ping丢包率(在异步线程中调用)
|
||||||
|
*/
|
||||||
|
private Double getPingLossRateSync(String interfaceName) {
|
||||||
|
String[] cmd = {
|
||||||
|
"/bin/sh", "-c",
|
||||||
|
String.format("ping -I %s -c 290 -i 1 -W 1 -w 290 %s 2>&1 | grep 'packet loss' | awk -F'[ ,%]+' '{print $6}'",
|
||||||
|
interfaceName, "223.5.5.5")
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
Process process = Runtime.getRuntime().exec(cmd);
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||||
|
|
||||||
|
int exitCode = process.waitFor();
|
||||||
|
if (exitCode != 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String result = reader.readLine();
|
||||||
|
if (result != null && !result.trim().isEmpty()) {
|
||||||
|
return Double.parseDouble(result.trim());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
AssertLog.error("执行ping命令失败: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 处理OSHI能识别到的网卡
|
* 处理OSHI能识别到的网卡
|
||||||
*/
|
*/
|
||||||
@@ -139,7 +188,18 @@ public class NetServiceImpl implements NetService {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
String[] ipv4Arr = net.getIPv4addr();
|
||||||
|
CompletableFuture<Double> pingLossFuture = null;
|
||||||
|
boolean hasIpv4 = false;
|
||||||
|
Double pingDropped = null;
|
||||||
|
if(ipv4Arr != null){
|
||||||
|
for (String ipv4 : ipv4Arr) {
|
||||||
|
if(ipv4 != "" && !ipv4.startsWith("127.")){
|
||||||
|
hasIpv4 = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
System.out.println("接口名称: " + interfaceName + "(" + net.getDisplayName() + ")");
|
System.out.println("接口名称: " + interfaceName + "(" + net.getDisplayName() + ")");
|
||||||
System.out.println("MAC地址: " + net.getMacaddr());
|
System.out.println("MAC地址: " + net.getMacaddr());
|
||||||
System.out.println("运行状态: " + (net.isConnectorPresent() ? "已连接" : "未连接"));
|
System.out.println("运行状态: " + (net.isConnectorPresent() ? "已连接" : "未连接"));
|
||||||
@@ -191,7 +251,10 @@ public class NetServiceImpl implements NetService {
|
|||||||
.ipv6OutSpeed(ipv6BytesSent)
|
.ipv6OutSpeed(ipv6BytesSent)
|
||||||
.timestamp(timestamp)
|
.timestamp(timestamp)
|
||||||
.build();
|
.build();
|
||||||
|
if(hasIpv4){
|
||||||
|
startPingMonitorAsync(interfaceName, timestamp);
|
||||||
|
netVO.setPingDropped(pingCache.get(interfaceName+(timestamp-300)));
|
||||||
|
}
|
||||||
// 设置协商速度和工作模式
|
// 设置协商速度和工作模式
|
||||||
Map<String, String> ethtoolMap = getNetworkMode(net.getName());
|
Map<String, String> ethtoolMap = getNetworkMode(net.getName());
|
||||||
if (ethtoolMap != null && !ethtoolMap.isEmpty()) {
|
if (ethtoolMap != null && !ethtoolMap.isEmpty()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user