优化mtr探测策略

This commit is contained in:
gaoyutao
2025-11-28 18:44:20 +08:00
parent 2f8fd79de5
commit ed6c0a4c80
9 changed files with 89 additions and 25 deletions
@@ -43,11 +43,11 @@ public class RmMtrClientRegistration extends BaseEntity
private Date registerTime;
/** 注册状态(0-未注册,1-已注册) */
@Excel(name = "注册状态(0-未注册,1-已注册)")
@Excel(name = "注册状态", readConverterExp = "0=未注册,1=已注册")
private String registerStatus;
/** 在线状态(0-离线,1-在线) */
@Excel(name = "在线状态(0-离线,1-在线)")
@Excel(name = "在线状态", readConverterExp = "0=离线,1=在线")
private String onlineStatus;
/** 心跳时间间隔(秒) */
@@ -59,11 +59,11 @@ public class RmMtrClientRegistration extends BaseEntity
private Integer heartbeatCount;
/** 更新方式 */
@Excel(name = "更新方式")
@Excel(name = "更新方式", readConverterExp = "0=立即执行,1=定时执行")
private String method;
/** 定时更新时间 */
@Excel(name = "定时更新时间")
@Excel(name = "定时更新时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date scheduledUpdateTime;
@@ -85,7 +85,7 @@ public class RmMtrClientRegistration extends BaseEntity
private Date lastUpdateTime;
/** 网卡信息(JSON格式) */
@Excel(name = "网卡信息(JSON格式)")
@Excel(name = "网卡信息")
private String networkInfo;
/** 部署设备 */
private String mtrClientIds;
@@ -36,13 +36,14 @@ public class RmMtrPolicyConfig extends BaseEntity
private String mtrClientId;
/** 服务器集合(换行符分割) */
@Excel(name = "服务器集合(换行符分割)")
@Excel(name = "服务器集合")
private String serverGroup;
/** 探测目标ip集合 */
@Excel(name = "目标ip集合")
private String serveripGroup;
/** 是否探测(0-否,1-是) */
@Excel(name = "是否探测(0-否,1-是)")
@Excel(name = "是否探测", readConverterExp = "0=否,1=是")
private Long probeFlag;
/** 开始时间 */
@@ -62,4 +63,8 @@ public class RmMtrPolicyConfig extends BaseEntity
private Map<String, List<String>> clientIdToIpsMap;
/** 查询条件 */
private String queryName;
@Excel(name = "修改时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
@Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
}
@@ -0,0 +1,16 @@
package com.tongran.mtragent.domain.vo;
import lombok.Data;
@Data
public class HopInfoVO {
private int hopNumber; // 跳数
private String ipAddress; // IP地址
private String hostname; // 主机名(如果有)
private double lossPercent; // 丢包率
private double avgLatency; // 平均延迟(ms)
private String country; // 国家
private String province; // 省份
private String city; // 城市
private String isp; // 运营商
}
@@ -2,6 +2,8 @@ package com.tongran.mtragent.domain.vo;
import lombok.Data;
import java.util.List;
@Data
public class MtrResultVo {
private String targetIp; // 目标IP
@@ -12,5 +14,6 @@ public class MtrResultVo {
private long timestamp; // 探测时间戳
private boolean hasRetry; // 是否重试
private String errorMsg; // 错误信息
private List<HopInfoVO> hopInfos; // 每跳路由信息列表
}
@@ -259,7 +259,7 @@ public class MessageHandler {
JSONObject.toJSONString(message)
);
int size = message.getData().getBytes(StandardCharsets.UTF_8).length;
log.info("消息大小: {} 字节, {} KB", size, size/1024.0);
log.debug("消息大小: {} 字节, {} KB", size, size/1024.0);
} catch (Exception e) {
log.error("发送mtr策略失败,mtrClientId: {}", mtrClientId, e);
}
@@ -1,8 +1,9 @@
package com.tongran.mtragent.mapper;
import java.util.List;
import com.tongran.mtragent.domain.RmMtrPolicyConfig;
import java.util.List;
/**
* mtr探测策略配置Mapper接口
*
@@ -58,4 +59,11 @@ public interface RmMtrPolicyConfigMapper
* @return 结果
*/
public int deleteRmMtrPolicyConfigByIds(Long[] ids);
/**
* 查询服务器是否在线
* @param serverIds
* @return
*/
List<String> getOnlineServerId(List<String> serverIds);
}
@@ -290,6 +290,12 @@ public class RmMtrPolicyConfigServiceImpl implements IRmMtrPolicyConfigService
serverIds.add(id.trim());
}
}
// 如果列表为空,直接返回空列表,避免不必要的数据库查询
if (serverIds.isEmpty()) {
return serverIds;
}
// 过滤离线的服务器
serverIds = rmMtrPolicyConfigMapper.getOnlineServerId(serverIds);
}
return serverIds;
}
@@ -41,6 +41,7 @@
or mtr_client_id like concat('%', #{queryName}, '%') or server_group like concat('%', #{queryName}, '%')
)</if>
</where>
order by create_time desc
</select>
<select id="selectRmMtrPolicyConfigById" parameterType="Long" resultMap="RmMtrPolicyConfigResult">
@@ -112,4 +113,12 @@
#{id}
</foreach>
</delete>
<select id="getOnlineServerId" parameterType="java.util.List" resultType="java.lang.String">
SELECT client_id FROM rm_resource_registration
WHERE client_id IN
<foreach collection="list" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
AND online_status = 1
</select>
</mapper>
@@ -957,7 +957,7 @@ public class MessageHandler {
// 只有达到3次心跳才执行数据库操作
if (newHeartbeatCount >= 3) {
log.info("客户端ID: {} 达到{}次心跳,开始执行数据库操作", clientId, newHeartbeatCount);
log.debug("客户端ID: {} 达到{}次心跳,开始执行数据库操作", clientId, newHeartbeatCount);
// 添加逻辑节点标识
RmResourceRegistrationRemote updateData = new RmResourceRegistrationRemote();
@@ -1202,6 +1202,12 @@ public class MessageHandler {
RmResourceRegistrationRemote query = new RmResourceRegistrationRemote();
query.setClientId(clientId);
R<RmResourceRegistrationRemote> registrationR = remoteRevenueConfigService.getListByHardwareSn(query, SecurityConstants.INNER);
if(registrationR != null){
RmResourceRegistrationRemote registrationMsg = registrationR.getData();
if(registrationMsg != null){
rmAlarmLog.setBusinessName(registrationMsg.getBusinessName());
}
}
rmAlarmLog.setClientId(clientId);
rmAlarmLog.setAlarmType("1");
}
@@ -1222,26 +1228,37 @@ public class MessageHandler {
rmAlarmPushConfig.setPushMethod(PushMethodEnum.企业微信.getCode());
rmAlarmPushConfig.setPushAlarmTypes(alarmTypeCode);
List<RmAlarmPushConfig> alarmConfigList = rmAlarmPushConfigService.selectRmAlarmPushConfigList(rmAlarmPushConfig);
RmAlarmLog updateData = new RmAlarmLog();
updateData.setId(rmAlarmLog.getId());
if (alarmConfigList != null && !alarmConfigList.isEmpty()) {
for (RmAlarmPushConfig alarmPushConfig : alarmConfigList) {
String contentTemplate = alarmPushConfig.getMessageContent();
String webhookUrl = alarmPushConfig.getPushAddress();
Map<String, Object> alarmMap = new HashMap<>();
alarmMap.put("告警时间", rmAlarmLog.getAlarmTime());
alarmMap.put("管理网-公网IP", rmAlarmLog.getMgmPublicIp());
alarmMap.put("告警类型", alarmTypeMsg);
alarmMap.put("告警设备", rmAlarmLog.getClientId());
alarmMap.put("告警内容", rmAlarmLog.getAlarmContent());
try {
for (RmAlarmPushConfig alarmPushConfig : alarmConfigList) {
String contentTemplate = alarmPushConfig.getMessageContent();
String webhookUrl = alarmPushConfig.getPushAddress();
Map<String, Object> alarmMap = new HashMap<>();
alarmMap.put("告警时间", rmAlarmLog.getAlarmTime());
alarmMap.put("IP", rmAlarmLog.getMgmPublicIp());
alarmMap.put("告警类型", alarmTypeMsg);
alarmMap.put("告警设备", rmAlarmLog.getClientId());
alarmMap.put("告警内容", rmAlarmLog.getAlarmContent());
alarmMap.put("业务名称", rmAlarmLog.getBusinessName());
if (alarmPushConfig.getContactPhones() != null) {
String[] phones = alarmPushConfig.getContactPhones().split(",");
WeChatWorkBot.sendTemplateMessage(webhookUrl, contentTemplate, alarmMap, rmAlarmLog.getAlarmContent(), phones, false);
} else {
WeChatWorkBot.sendTemplateMessage(webhookUrl, contentTemplate, alarmMap);
if (alarmPushConfig.getContactPhones() != null) {
String[] phones = alarmPushConfig.getContactPhones().split(",");
WeChatWorkBot.sendTemplateMessage(webhookUrl, contentTemplate, alarmMap, rmAlarmLog.getAlarmContent(), phones, false);
} else {
WeChatWorkBot.sendTemplateMessage(webhookUrl, contentTemplate, alarmMap);
}
}
updateData.setPushFlag(1L);
} catch (Exception e){
updateData.setPushFlag(0L);
log.error("消息推送失败:{}", e.getMessage());
}
}else{
updateData.setPushFlag(2L);
}
rmAlarmLogService.updateRmAlarmLog(updateData);
}
/**