多网卡ip探测增加网卡创建时间字段上报

This commit is contained in:
gaoyutao
2026-01-12 18:28:32 +08:00
parent b4155ea9c8
commit f9a4de8c97
2 changed files with 34 additions and 1 deletions
@@ -27,6 +27,8 @@ public class NetworkInterfaceInfo {
String status; // 与外网连通状态
// 如果是子接口,存储父接口名称
private String parentInterface;
/** 网卡创建时间 */
private Long netCreateTime;
// 如果是父接口,存储子接口列表
private List<NetworkInterfaceInfo> subInterfaces;
@@ -142,7 +142,6 @@ public class NetworkInterfaceUtil {
}
}
}
// 创建网卡信息对象
NetworkInterfaceInfo info = NetworkInterfaceInfo.builder()
.name(name)
@@ -187,6 +186,9 @@ public class NetworkInterfaceUtil {
}
if(info.getIpv4() != null){
info.setStatus(getmacVlanStatus(info.getName())?"1":"0");
// 获取网卡创建时间戳
long createTime = getInterfaceCreateTime(name);
info.setNetCreateTime(createTime);
}
parent.getSubInterfaces().add(info);
AssertLog.info(" 添加到父接口 {} 的子接口列表", parentName);
@@ -220,6 +222,9 @@ public class NetworkInterfaceUtil {
}
if(child.getIpv4() != null){
child.setStatus(getmacVlanStatus(child.getName())?"1":"0");
// 获取网卡创建时间戳
long createTime = getInterfaceCreateTime(child.getName());
child.setNetCreateTime(createTime);
}
parent.getSubInterfaces().add(child);
AssertLog.info("子接口 {} 关联到父接口 {}", child.getName(), parentName);
@@ -446,6 +451,32 @@ public class NetworkInterfaceUtil {
return false; // 保守处理
}
}
/**
* 获取网卡创建时间戳(单位:秒)
* 通过 stat 命令获取 /sys/class/net/<interface>/ 目录的创建时间
* 返回Unix时间戳,失败返回-1
*/
public static long getInterfaceCreateTime(String interfaceName) {
try {
ProcessBuilder pb = new ProcessBuilder("stat", "-c", "%Y", "/sys/class/net/" + interfaceName);
Process process = pb.start();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String output = reader.readLine();
reader.close();
int exitCode = process.waitFor();
if (exitCode == 0 && output != null && !output.trim().isEmpty()) {
return Long.parseLong(output.trim());
}
return -1;
} catch (Exception e) {
AssertLog.error("获取网卡 {} 创建时间失败: {}", interfaceName, e.getMessage());
return -1;
}
}
/**
* 内部类:网卡详情