From 8cf95456a599df38124cfe791d0b1f5e5dc48bb6 Mon Sep 17 00:00:00 2001 From: gaoyutao Date: Mon, 24 Nov 2025 18:28:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96mtr=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/core/eo/MtrPolicyConfigEO.java | 4 ++ .../tongran/agent/client/core/vo/MtrVO.java | 1 + .../client/service/impl/AgentServiceImpl.java | 63 +++++++++++++++++++ .../client/service/impl/MtrServiceImpl.java | 25 ++++++++ 4 files changed, 93 insertions(+) diff --git a/src/main/java/com/tongran/agent/client/core/eo/MtrPolicyConfigEO.java b/src/main/java/com/tongran/agent/client/core/eo/MtrPolicyConfigEO.java index d42a4b3..4107477 100644 --- a/src/main/java/com/tongran/agent/client/core/eo/MtrPolicyConfigEO.java +++ b/src/main/java/com/tongran/agent/client/core/eo/MtrPolicyConfigEO.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.util.Date; +import java.util.List; +import java.util.Map; /** * mtr策略信息 @@ -42,4 +44,6 @@ public class MtrPolicyConfigEO { /** 探测频率(秒) */ private Long probeFrequency; + /** clientId和ip对应集合 */ + private Map> clientIdToIpsMap; } diff --git a/src/main/java/com/tongran/agent/client/core/vo/MtrVO.java b/src/main/java/com/tongran/agent/client/core/vo/MtrVO.java index c089fae..a48d26a 100644 --- a/src/main/java/com/tongran/agent/client/core/vo/MtrVO.java +++ b/src/main/java/com/tongran/agent/client/core/vo/MtrVO.java @@ -7,6 +7,7 @@ import java.util.Date; @Data public class MtrVO { private String targetIp; // 目标IP + private String clientId; // 对应的clientId private Long policyId; // 策略ID private double firstLossPercent; // 第一次探测丢包率 private double finalLossPercent; // 最终丢包率 diff --git a/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java b/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java index 56a7d54..0c7d494 100644 --- a/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java +++ b/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java @@ -33,6 +33,7 @@ import javax.annotation.Resource; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.*; +import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -287,6 +288,8 @@ public class AgentServiceImpl implements AgentService { // 检测监控策略配置 AssertLog.info("检测监控策略配置"); checkMonitor(); + AssertLog.info("检测mtr探测策略配置"); + checkMtrDelect(); } @Override @@ -833,7 +836,24 @@ public class AgentServiceImpl implements AgentService { } if(jsonObject.containsKey("mtrPolicys")){ String mtrPolicys = jsonObject.getString("mtrPolicys"); + AssertLog.info("接收的mtrPolicys原始值: {}", mtrPolicys); + AssertLog.info("GlobalConfig.MTRPOLICYMSG当前值: {}", GlobalConfig.MTRPOLICYMSG); if(!mtrPolicys.equals(GlobalConfig.MTRPOLICYMSG)){ + // 准备写入的内容 + String[] lines = { + "# MTR探测策略配置文件", + "# 生成时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), + "", + "# MTR策略配置(JSON格式)", + "mtrPolicys=" + mtrPolicys, + "", + "# 配置元信息", + "lastUpdateTime=" + System.currentTimeMillis() + }; + //检查外置目录是否存在 + if(AdvancedAsyncDownloader.createSingleDirectoryIfNotExists(properties.getConfPath())){ + AgentUtil.bufferedWriter(properties.getConfPath()+"/mtrdelect.conf",lines); + } GlobalConfig.MTRPOLICYMSG = mtrPolicys; List mtrPolicyConfigEOList = JSON.parseArray(mtrPolicys, MtrPolicyConfigEO.class); try { @@ -1495,6 +1515,49 @@ public class AgentServiceImpl implements AgentService { } + public void checkMtrDelect(){ + File mtrFile = new File(properties.getConfPath() + "/mtrdelect.conf"); + if (mtrFile.exists()) { + Properties props = new Properties(); + try (InputStream input = Files.newInputStream(mtrFile.toPath())) { + props.load(input); + + String mtrPolicys = props.getProperty("mtrPolicys"); + if (StringUtils.isNotBlank(mtrPolicys)) { + GlobalConfig.MTRPOLICYMSG = mtrPolicys; + + // 解析策略并启动定时任务 + List mtrPolicyConfigEOList = JSON.parseArray(mtrPolicys, MtrPolicyConfigEO.class); + if (CollectionUtil.isNotEmpty(mtrPolicyConfigEOList)) { + // 检查策略是否仍然有效 + long currentTimestamp = System.currentTimeMillis() / 1000; + boolean hasValidPolicy = false; + + for (MtrPolicyConfigEO policy : mtrPolicyConfigEOList) { + if (isPolicyTimeValid(policy, currentTimestamp)) { + hasValidPolicy = true; + break; + } + } + + if (hasValidPolicy) { + long milli = AgentUtil.millisecondsToNext5Minute(); + dynamicTaskService.scheduleTask("mtrTask", + businessTasks::mtrTask, milli, 300000L); + + AssertLog.info("应用启动时加载MTR配置成功,策略数量: {}", mtrPolicyConfigEOList.size()); + } else { + AssertLog.info("应用启动时加载的MTR配置已过期"); + } + } + } + + } catch (Exception e) { + AssertLog.error("应用启动时加载MTR配置异常", e); + } + } + } + public void caseTypeBySystem(String type, int interval, boolean collect){ diff --git a/src/main/java/com/tongran/agent/client/service/impl/MtrServiceImpl.java b/src/main/java/com/tongran/agent/client/service/impl/MtrServiceImpl.java index 6b15987..f7d874e 100644 --- a/src/main/java/com/tongran/agent/client/service/impl/MtrServiceImpl.java +++ b/src/main/java/com/tongran/agent/client/service/impl/MtrServiceImpl.java @@ -101,11 +101,36 @@ public class MtrServiceImpl implements MtrService { } } + /** + * 预先构建IP到clientId的反向映射 + */ + private Map buildIpToClientIdMap(Map> clientIdToIpMap) { + Map ipToClientIdMap = new HashMap<>(); + + if (clientIdToIpMap == null) { + return ipToClientIdMap; + } + + for (Map.Entry> entry : clientIdToIpMap.entrySet()) { + String clientId = entry.getKey(); + List ips = entry.getValue(); + if (ips != null) { + for (String ip : ips) { + ipToClientIdMap.put(ip.trim(), clientId); + } + } + } + + return ipToClientIdMap; + } /** * 执行单个IP的MTR探测 */ private MtrVO executeSingleMtrProbe(String targetIp, MtrPolicyConfigEO policy, long timestamp) { + // 构建IP到clientId的反向映射 + Map ipToClientIdMap = buildIpToClientIdMap(policy.getClientIdToIpsMap()); MtrVO mtrVO = new MtrVO(); + mtrVO.setClientId(ipToClientIdMap.getOrDefault(targetIp, "unknown")); mtrVO.setTargetIp(targetIp); mtrVO.setPolicyId(policy.getId()); mtrVO.setTimestamp(timestamp);