增加多网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);
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.tongran.agent.client.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FileCleaner {
|
||||
|
||||
/**
|
||||
* 删除指定目录中 30 天前修改的文件
|
||||
*
|
||||
* @param directoryPath 目录路径,例如:"/tmp/logs"
|
||||
* @param days 保留天数,传入 30 表示删除 30 天以上的文件
|
||||
*/
|
||||
public static void cleanOldFiles(String directoryPath, int days) {
|
||||
Path dir = Paths.get(directoryPath);
|
||||
|
||||
// 检查目录是否存在且是目录
|
||||
if (!Files.exists(dir)) {
|
||||
System.err.println("目录不存在: " + directoryPath);
|
||||
return;
|
||||
}
|
||||
if (!Files.isDirectory(dir)) {
|
||||
System.err.println("路径不是目录: " + directoryPath);
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算 30 天前的时间点(Instant)
|
||||
Instant cutoffTime = Instant.now().minus(days, ChronoUnit.DAYS);
|
||||
List<Path> deletedFiles = new ArrayList<>();
|
||||
List<Path> failedFiles = new ArrayList<>();
|
||||
|
||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
|
||||
for (Path file : stream) {
|
||||
// 只处理普通文件(跳过子目录等)
|
||||
if (Files.isRegularFile(file)) {
|
||||
try {
|
||||
BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
|
||||
Instant lastModifiedTime = attrs.lastModifiedTime().toInstant();
|
||||
|
||||
if (lastModifiedTime.isBefore(cutoffTime)) {
|
||||
Files.delete(file); // 删除文件
|
||||
deletedFiles.add(file);
|
||||
System.out.println("已删除: " + file + " (修改时间: " + lastModifiedTime + ")");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("无法读取或删除文件: " + file + " -> " + e.getMessage());
|
||||
failedFiles.add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("遍历目录失败: " + e.getMessage());
|
||||
}
|
||||
|
||||
// 统计结果
|
||||
System.out.println("✅ 清理完成:");
|
||||
System.out.println(" 删除文件数: " + deletedFiles.size());
|
||||
System.out.println(" 删除失败数: " + failedFiles.size());
|
||||
}
|
||||
|
||||
// 使用示例
|
||||
public static void main(String[] args) {
|
||||
// String logDir = "/tmp/logs"; // 替换为你的实际目录
|
||||
// cleanOldFiles(logDir, 30); // 删除 30 天以上的文件
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public class PublicIpFetcher {
|
||||
|
||||
// 读取响应(注意:myip.ipip.net 返回的是 GBK 编码)
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(connection.getInputStream(), "GBK"))) { // 使用 GBK 解码
|
||||
new InputStreamReader(connection.getInputStream(), "UTF-8"))) { // 使用 GBK 解码
|
||||
StringBuilder response = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
@@ -71,6 +71,12 @@ public class PublicIpFetcher {
|
||||
} else {
|
||||
System.out.println("未能提取 IP 地址");
|
||||
}
|
||||
String province = "";
|
||||
String city = "";
|
||||
String carrier = "";
|
||||
|
||||
System.out.println(ipInfo.substring(ipInfo.indexOf("来自于:")+4,ipInfo.length()));
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("获取公网 IP 失败: " + e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user