增加多网IP探测上报;
优化公网运营商信息; 逻辑标识改为读取外部文件; 增加定时任务清理日志临时文件;
This commit is contained in:
@@ -2,24 +2,32 @@ package com.tongran.agent.client.utils;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.tongran.agent.client.core.config.GlobalConfig;
|
||||
import com.tongran.agent.client.core.vo.NetworkInterfaceInfo;
|
||||
import org.snmp4j.smi.OID;
|
||||
import oshi.hardware.NetworkIF;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class AgentUtil {
|
||||
|
||||
@Resource
|
||||
private GlobalConfig globalConfig;
|
||||
|
||||
public static String getMotherboardUUID() {
|
||||
String hostName = getHostname();
|
||||
String primaryIp = getPrimaryIp();
|
||||
@@ -243,6 +251,8 @@ public class AgentUtil {
|
||||
return swapped;
|
||||
}
|
||||
|
||||
// 正则表达式匹配:当前 IP:xxx.xxx.xxx.xxx 来自于:中国 江苏省 南京市 电信
|
||||
private static final String REGEX = "来自于:(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)";
|
||||
/**
|
||||
* 收集所有 Ethernet 类型网卡信息
|
||||
*/
|
||||
@@ -272,6 +282,7 @@ public class AgentUtil {
|
||||
|
||||
NetworkInterfaceInfo info = NetworkInterfaceInfo.builder()
|
||||
.name(name)
|
||||
.type("Ethernet")
|
||||
.mac(getMacAddress(ni))
|
||||
.ipv4(getIPv4Address(ni))
|
||||
.gateway(getGatewayAddress()) // 网关通常是默认路由,全局一致
|
||||
@@ -280,18 +291,36 @@ public class AgentUtil {
|
||||
|
||||
// 如果有公网 IP,查询归属地
|
||||
if (publicIp != null && !publicIp.isEmpty()) {
|
||||
JSONObject location = queryIpLocation(publicIp);
|
||||
if (location != null) {
|
||||
info.setCarrier(location.getStr("org", "未知").replaceAll("^AS\\d+\\s*", ""));
|
||||
info.setProvince(location.getStr("region", "未知"));
|
||||
info.setCity(location.getStr("city", "未知"));
|
||||
} else {
|
||||
info.setCarrier("查询失败");
|
||||
info.setProvince("查询失败");
|
||||
info.setCity("查询失败");
|
||||
Pattern pattern = Pattern.compile(REGEX);
|
||||
Matcher matcher = pattern.matcher(ipInfo);
|
||||
if (matcher.find()) {
|
||||
// group(1): 国家(通常是“中国”)
|
||||
// group(2): 省份
|
||||
// group(3): 城市
|
||||
// group(4): 运营商
|
||||
String province = matcher.group(2);
|
||||
String city = matcher.group(3);
|
||||
String isp = matcher.group(4);
|
||||
// 去掉可能的标点符号(如句号)
|
||||
province = province.replaceAll("[。]", "");
|
||||
city = city.replaceAll("[。]", "");
|
||||
isp = isp.replaceAll("[。]", "");
|
||||
info.setCarrier(isp);
|
||||
info.setProvince(province);
|
||||
info.setCity(city);
|
||||
}
|
||||
// ipInfo.substring(ipInfo.indexOf("来自于:")+4,ipInfo.length())
|
||||
// JSONObject location = queryIpLocation(publicIp);
|
||||
// if (location != null) {
|
||||
// info.setCarrier(location.getStr("org", "未知").replaceAll("^AS\\d+\\s*", ""));
|
||||
// info.setProvince(location.getStr("region", "未知"));
|
||||
// info.setCity(location.getStr("city", "未知"));
|
||||
// } else {
|
||||
// info.setCarrier("查询失败");
|
||||
// info.setProvince("查询失败");
|
||||
// info.setCity("查询失败");
|
||||
// }
|
||||
}
|
||||
|
||||
result.add(info);
|
||||
}
|
||||
|
||||
@@ -580,4 +609,27 @@ public class AgentUtil {
|
||||
.collect(Collectors.joining("\n")); // 用换行符连接
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件最后修改时间
|
||||
* @param filePath
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime getLastModifiedTime(String filePath) {
|
||||
try {
|
||||
BasicFileAttributes attrs = Files.readAttributes(Paths.get(filePath), BasicFileAttributes.class);
|
||||
return attrs.lastModifiedTime()
|
||||
.toInstant()
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDateTime();
|
||||
} catch (IOException e) {
|
||||
System.err.println("无法读取文件时间: " + filePath);
|
||||
return null;
|
||||
}
|
||||
// LocalDateTime mtime = FileUtils.getLastModifiedTime("/tmp/data.log");
|
||||
// if (mtime != null) {
|
||||
// System.out.println("修改时间: " + mtime);
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user