服务器自动注册

This commit is contained in:
gaoyutao
2025-10-21 18:28:18 +08:00
parent 52728eba49
commit 4fdaadee65
25 changed files with 1373 additions and 9 deletions

View File

@@ -98,4 +98,13 @@ public interface RemoteRevenueConfigService
@PostMapping("/switchManagement/getSwitchNameByClientId")
public R<List<RmSwitchManagementRemote>> getSwitchNameByClientId(@RequestBody RmSwitchManagementRemote rmSwitchManagementRemote, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
* 自动注册
* @param rmRegisterMsgRemote
* @param source
* @return
*/
@PostMapping("/registration/innerAddRegist")
public R<Integer> innerAddRegist(@RequestBody RmRegisterMsgRemote rmRegisterMsgRemote, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
}

View File

@@ -0,0 +1,32 @@
package com.ruoyi.system.api.domain;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@Data
public class NetworkInfo {
// 运营商
@JsonProperty("carrier")
private String carrier;
// 市
@JsonProperty("city")
private String city;
// 网关
@JsonProperty("gateway")
private String gateway;
// IPv4 地址
@JsonProperty("ipv4")
private String ipv4;
// MAC 地址
@JsonProperty("mac")
private String mac;
// 接口名称eth0, enp3s0
@JsonProperty("name")
private String name;
// 省
@JsonProperty("province")
private String province;
// 公网 IP
@JsonProperty("publicIp")
private String publicIp;
}

View File

@@ -0,0 +1,21 @@
package com.ruoyi.system.api.domain;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.List;
@Data
public class RmRegisterMsgRemote {
@JsonProperty("clientId")
private String clientId;
@JsonProperty("sn")
private String sn;
@JsonProperty("networkInfo")
private List<NetworkInfo> networkInfo;
@JsonProperty("timestamp")
private long timestamp;
}

View File

@@ -127,5 +127,49 @@ public class RmResourceRegistrationRemote extends BaseEntity
private String monitorItems;
/** 自动发现项 */
private String discoveryRules;
/**
* 客户端ID
*/
private String clientId;
/**
* 运营商
*/
private String operator;
/**
* 省
*/
private String province;
/**
* 公网IP
*/
private String publicIp;
/**
* 业务名称
*/
private String businessName;
/**
* 逻辑节点标识
*/
private String logicalNodeId;
/**
* 多公网IP状态
*/
private String multiPublicIpStatus;
/**
* 心跳次数
*/
private Integer heartbeatCount;
/**
* 心跳周期(单位:秒)
*/
private Integer heartbeatInterval;
}

View File

@@ -71,6 +71,11 @@ public class RemoteRevenueConfigFallbackFactory implements FallbackFactory<Remot
public R<List<RmSwitchManagementRemote>> getSwitchNameByClientId(RmSwitchManagementRemote rmSwitchManagementRemote, String source) {
return R.fail("根据clientId获取交换机信息失败" + throwable.getMessage());
}
@Override
public R<Integer> innerAddRegist(RmRegisterMsgRemote rmRegisterMsgRemote, String source) {
return R.fail("自动注册失败" + throwable.getMessage());
}
};
}
}

View File

@@ -15,6 +15,10 @@ public enum MsgEnum {
多公网IP探测("PUBLICIPDETECT"),
获取最新策略("GET_POLICY"),
获取最新策略应答("GET_POLICY_RSP"),
注册("REGISTER"),
注册应答("REGISTER_RSP"),

View File

@@ -10,6 +10,7 @@ import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.RmRegisterMsgRemote;
import com.ruoyi.system.domain.RmResourceRegistration;
import com.ruoyi.system.service.IRmResourceRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -165,5 +166,28 @@ public class RmResourceRegistrationController extends BaseController
List<RmResourceRegistration> list = rmResourceRegistrationService.selectRmResourceRegistrationList(rmResourceRegistration);
return R.ok(list.get(0));
}
/**
* 查询资源注册列表
*/
@RequiresPermissions("system:registration:list")
@PostMapping("/getRegistList")
public AjaxResult getRegistList(@RequestBody RmResourceRegistration rmResourceRegistration)
{
List<RmResourceRegistration> list = rmResourceRegistrationService.selectRmResourceRegistrationList(rmResourceRegistration);
return success(list);
}
/**
* 自动注册服务器
* @param rmResourceRegistration mq接收的消息
* @return
*/
@PostMapping("/innerAddRegist")
@InnerAuth
public R<Integer> innerAddRegist(@RequestBody RmRegisterMsgRemote rmResourceRegistration)
{
int rows = rmResourceRegistrationService.innerAddRegist(rmResourceRegistration);
return R.ok(rows);
}
}

View File

@@ -0,0 +1,15 @@
package com.ruoyi.system.domain.vo;
import lombok.Data;
import java.time.Instant;
@Data
public class RspVo {
/** 状态码0、失败1、成功*/
private int resCode;
/** 描述 */
private String resMag;
/** 时间戳 */
private long timestamp = Instant.now().getEpochSecond();
}

View File

@@ -2,6 +2,7 @@ package com.ruoyi.system.service;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.domain.RmRegisterMsgRemote;
import com.ruoyi.system.domain.RmResourceRegistration;
import java.util.List;
@@ -90,4 +91,11 @@ public interface IRmResourceRegistrationService
AjaxResult getServerOnlineRate();
List<RmResourceRegistration> getRegistrationByIds(String[] ids);
/**
* 自动注册
* @param rmResourceRegistration
* @return
*/
int innerAddRegist(RmRegisterMsgRemote rmResourceRegistration);
}

View File

@@ -8,22 +8,27 @@ import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteRocketMqService;
import com.ruoyi.system.api.domain.NetworkInfo;
import com.ruoyi.system.api.domain.RmRegisterMsgRemote;
import com.ruoyi.system.domain.EpsNodeBandwidth;
import com.ruoyi.system.domain.EpsServerRevenueConfig;
import com.ruoyi.system.domain.RmEpsTopologyManagement;
import com.ruoyi.system.domain.RmResourceRegistration;
import com.ruoyi.system.domain.vo.MessageVo;
import com.ruoyi.system.domain.vo.ResourceVo;
import com.ruoyi.system.domain.vo.RspVo;
import com.ruoyi.system.mapper.EpsNodeBandwidthMapper;
import com.ruoyi.system.mapper.EpsServerRevenueConfigMapper;
import com.ruoyi.system.mapper.RmEpsTopologyManagementMapper;
import com.ruoyi.system.mapper.RmResourceRegistrationMapper;
import com.ruoyi.system.service.IRmResourceRegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -35,6 +40,7 @@ import java.util.Map;
* @date 2025-08-12
*/
@Service
@Slf4j
public class RmResourceRegistrationServiceImpl implements IRmResourceRegistrationService
{
@Autowired
@@ -350,5 +356,76 @@ public class RmResourceRegistrationServiceImpl implements IRmResourceRegistratio
return rmResourceRegistrationMapper.getRegistrationByIds(ids);
}
/**
* 自动注册
* @param registerMsg
* @return
*/
@Override
public int innerAddRegist(RmRegisterMsgRemote registerMsg) {
// 解析mq接收的消息
// 时间戳转换
long timestamp = registerMsg.getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
String timeStr = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", createTime);
List<NetworkInfo> networkInfoList = registerMsg.getNetworkInfo();
if(!networkInfoList.isEmpty()){
// 查询此数据是否存在
RmResourceRegistration queryParam = new RmResourceRegistration();
queryParam.setClientId(registerMsg.getClientId());
RmResourceRegistration exits = rmResourceRegistrationMapper.selectRegistMsgByClientId(queryParam);
if(exits == null){
// 构建服务器信息
RmResourceRegistration insertData = new RmResourceRegistration();
try {
// 如果网卡信息只有1条自动绑定多公网IP
if(networkInfoList.size()==1){
insertData.setClientId(registerMsg.getClientId());
insertData.setHardwareSn(registerMsg.getSn());
insertData.setRegistrationStatus("1");
insertData.setMultiPublicIpStatus("1");
insertData.setResourceType("1");
insertData.setCreateTime(createTime);
rmResourceRegistrationMapper.insertRmResourceRegistration(insertData);
}else{
insertData.setClientId(registerMsg.getClientId());
insertData.setHardwareSn(registerMsg.getSn());
insertData.setRegistrationStatus("0");
insertData.setMultiPublicIpStatus("0");
insertData.setResourceType("1");
insertData.setCreateTime(createTime);
rmResourceRegistrationMapper.insertRmResourceRegistration(insertData);
}
// 构建注册应答信息
MessageVo messageVo = new MessageVo();
messageVo.setClientId(registerMsg.getClientId());
messageVo.setDataType(MsgEnum.注册应答.getValue());
RspVo rspVo = new RspVo();
rspVo.setResCode(1);
rspVo.setResMag("注册成功");
messageVo.setData(JSONObject.toJSONString(rspVo));
remoteRocketMqService.sendAsyncProducerMessage(
"tr_agent_down", "", "", JSONObject.toJSONString(messageVo), SecurityConstants.INNER
);
}catch (Exception e){
log.error("注册服务器失败:{}",e.getMessage());
// 构建注册应答信息
MessageVo messageVo = new MessageVo();
messageVo.setClientId(registerMsg.getClientId());
messageVo.setDataType(MsgEnum.注册应答.getValue());
RspVo rspVo = new RspVo();
rspVo.setResCode(1);
rspVo.setResMag("平台错误信息:" + e.getMessage());
messageVo.setData(JSONObject.toJSONString(rspVo));
remoteRocketMqService.sendAsyncProducerMessage(
"tr_agent_down", "", "", JSONObject.toJSONString(messageVo), SecurityConstants.INNER
);
}
}
}
return 1;
}
}

View File

@@ -331,6 +331,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="updaterName != null">updater_name = #{updaterName},</if>
</trim>
where hardware_sn = #{hardwareSn}
<choose>
<when test="hardwareSn != null">
hardware_sn = #{hardwareSn}
</when>
<when test="clientId != null">
client_id = #{clientId}
</when>
<otherwise>
1=0 <!-- 如果两个条件都不满足,则不更新任何记录 -->
</otherwise>
</choose>
</update>
<select id="getRegistrationByIds" parameterType="String" resultMap="RmResourceRegistrationResult">
<include refid="selectRmResourceRegistrationVo"/>

View File

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.ruoyi.rocketmq.domain.DeviceMessage;
import com.ruoyi.rocketmq.enums.MessageCodeEnum;
import com.ruoyi.rocketmq.handler.DeviceMessageHandler;
import com.ruoyi.rocketmq.handler.MessageHandler;
import com.ruoyi.rocketmq.producer.ConsumeException;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
@@ -25,6 +26,8 @@ import java.util.List;
public class RocketMsgListener implements MessageListenerConcurrently {
@Autowired
private DeviceMessageHandler deviceMessageHandler;
@Autowired
private MessageHandler messageHandler;
/**
* 消费消息
@@ -62,6 +65,13 @@ public class RocketMsgListener implements MessageListenerConcurrently {
deviceMessageHandler.handleMessage(message);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;//业务处理成功
}
if(MessageCodeEnum.TR_AGENT_UP.getCode().equals(topic)){
// 拿到信息
DeviceMessage message = JSON.parseObject(body, DeviceMessage.class);
// 处理消息
messageHandler.handleMessage(message);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;//业务处理成功
}
// 根据不同的topic处理不同的业务 这里以订单消息为例子
}
}

View File

@@ -0,0 +1,34 @@
package com.ruoyi.rocketmq.domain;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.system.api.domain.NetworkInfo;
import java.io.IOException;
import java.util.List;
public class NetworkInfoDeserializer extends JsonDeserializer<List<NetworkInfo>> {
// 添加无参构造函数Jackson 需要)
public NetworkInfoDeserializer() {
}
@Override
public List<NetworkInfo> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
ObjectMapper mapper = (ObjectMapper) p.getCodec();
JsonNode node = mapper.readTree(p);
// 如果 networkInfo 是字符串(如日志中的情况),先解析字符串
if (node.isTextual()) {
String jsonStr = node.textValue();
return mapper.readValue(jsonStr, new TypeReference<List<NetworkInfo>>() {});
}
// 如果 networkInfo 是数组,直接解析
else if (node.isArray()) {
return mapper.readValue(node.traverse(), new TypeReference<List<NetworkInfo>>() {});
}
throw new IOException("Invalid networkInfo format");
}
}

View File

@@ -0,0 +1,26 @@
package com.ruoyi.rocketmq.domain.vo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.ruoyi.rocketmq.domain.NetworkInfoDeserializer;
import com.ruoyi.system.api.domain.NetworkInfo;
import lombok.Data;
import java.util.List;
@Data
public class RegisterMsgVo {
@JsonProperty("clientId")
private String clientId;
@JsonProperty("sn")
private String sn;
@JsonProperty("networkInfo")
@JsonDeserialize(using = NetworkInfoDeserializer.class)
private List<NetworkInfo> networkInfo;
@JsonProperty("timestamp")
private long timestamp;
}

View File

@@ -17,6 +17,8 @@ public enum MessageCodeEnum {
TONGRAN_AGENT_UP("tongran_agent_up","agent数据采集的信息topic"),
TR_AGENT_UP("tr_agent_up","agent数据采集的信息topic v1.1"),
/**
* 系统消息
*/

View File

@@ -14,7 +14,7 @@ public class MessageTopic {
List<String> getTopicLists=new ArrayList<>();
// agent采集消息
// getTopicLists.add("agent_up");
getTopicLists.add("tongran_agent_up");
getTopicLists.add("tr_agent_up");
return getTopicLists;
}

View File

@@ -0,0 +1,947 @@
package com.ruoyi.rocketmq.handler;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.enums.MsgEnum;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.rocketmq.domain.*;
import com.ruoyi.rocketmq.domain.vo.CollectDataVo;
import com.ruoyi.rocketmq.domain.vo.RegisterMsgVo;
import com.ruoyi.rocketmq.domain.vo.RspVo;
import com.ruoyi.rocketmq.service.*;
import com.ruoyi.rocketmq.utils.DataProcessUtil;
import com.ruoyi.rocketmq.utils.JsonDataParser;
import com.ruoyi.rocketmq.utils.SwitchJsonDataParser;
import com.ruoyi.system.api.RemoteRevenueConfigService;
import com.ruoyi.system.api.domain.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 设备消息处理器
*/
@Slf4j
@Component
@EnableScheduling
public class MessageHandler {
private final Map<String, Consumer<DeviceMessage>> messageHandlers = new HashMap<>();
// 心跳状态
private static final String HEARTBEAT_STATUS_PREFIX = "heartbeat:status:";
// 心跳时间
private static final String HEARTBEAT_TIME_PREFIX = "heartbeat:time:";
// 心跳告警
private static final String HEARTBEAT_ALERT_PREFIX = "heartbeat:alert:";
String HEARTBEAT_RECOVERY_COUNT_PREFIX = "heartbeat:recovery:count:";
private static final long HEARTBEAT_TIMEOUT = 30000; // 3分钟超时
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Autowired
private IInitialBandwidthTrafficService initialBandwidthTrafficService;
@Autowired
private RemoteRevenueConfigService remoteRevenueConfigService;
@Autowired
private IInitialDockerInfoService initialDockerInfoService;
@Autowired
private IInitialCpuInfoService initialCpuInfoService;
@Autowired
private IInitialDiskInfoService initialDiskInfoService;
@Autowired
private IInitialMemoryInfoService initialMemoryInfoService;
@Autowired
private IInitialMountPointInfoService initialMountPointInfoService;
@Autowired
private IInitialSwitchInfoService initialSwitchInfoService;
@Autowired
private IInitialSystemInfoService initialSystemInfoService;
@Autowired
private IInitialSwitchInfoTempService initialSwitchInfoTempService;
@Autowired
private IInitialHeartbeatListenLogService initialHeartbeatListenLog;
@Autowired
private IInitialSwitchPowerSupplyService initialSwitchPowerSupplyService;
@Autowired
private IInitialSwitchFanInfoService initialSwitchFanInfoService;
@Autowired
private IInitialSwitchMpuInfoService initialSwitchMpuInfoService;
@Autowired
private IInitialSwitchOpticalModuleService initialSwitchOpticalModuleService;
@Autowired
private IInitialSwitchOtherCollectDataService insertInitialSwitchOtherInfo;
@Autowired
private IInitialSystemOtherCollectDataService iInitialSystemOtherCollectDataService;
@Autowired
private IRmResourceRemoteService rmResourceRemoteService;
@Autowired
private IRmAgentManagementService rmAgentManagementService;
@Autowired
private DataProcessUtil dataProcessUtil;
@Autowired
private IRmNetworkInterfaceService rmNetworkInterfaceService;
// 在类中添加
private static final ObjectMapper objectMapper = new ObjectMapper();
/**
* 初始化处理器映射
*/
@PostConstruct
public void init() {
// 所有应答类消息使用同一个处理器
// registerHandler(MsgEnum.注册应答.getValue(), this::handleResponseMessage);
// registerHandler(MsgEnum.断开应答.getValue(), this::handleResponseMessage);
// registerHandler(MsgEnum.开启系统采集应答.getValue(), this::handleResponseMessage);
// registerHandler(MsgEnum.关闭系统采集应答.getValue(), this::handleResponseMessage);
// registerHandler(MsgEnum.开启交换机采集应答.getValue(), this::handleResponseMessage);
// registerHandler(MsgEnum.关闭交换机采集应答.getValue(), this::handleResponseMessage);
// registerHandler(MsgEnum.告警设置应答.getValue(), this::handleResponseMessage);
// registerHandler(MsgEnum.执行脚本策略应答.getValue(), this::handleScriptRspMessage);
// registerHandler(MsgEnum.Agent版本更新应答.getValue(), this::handleAgentUpdateRspMessage);
// 其他类型消息可以单独注册处理器
registerHandler(MsgEnum.注册.getValue(), this::handleRegisterMessage);
registerHandler(MsgEnum.获取最新策略.getValue(), this::handleNewPolicyMessage);
// registerHandler(MsgEnum.CPU上报.getValue(), this::handleCpuMessage);
// registerHandler(MsgEnum.磁盘上报.getValue(), this::handleDiskMessage);
// registerHandler(MsgEnum.容器上报.getValue(), this::handleDockerMessage);
// registerHandler(MsgEnum.内存上报.getValue(), this::handleMemoryMessage);
// registerHandler(MsgEnum.网络上报.getValue(), this::handleNetMessage);
// registerHandler(MsgEnum.挂载上报.getValue(), this::handleMountPointMessage);
// registerHandler(MsgEnum.系统其他上报.getValue(), this::handleOtherSystemMessage);
// registerHandler(MsgEnum.交换机上报.getValue(), this::handleSwitchDataMessage);
// registerHandler(MsgEnum.心跳上报.getValue(), this::handleHeartbeatMessage);
// registerHandler(MsgEnum.多公网IP探测.getValue(), this::handlePublicIpDetectMessage);
}
/**
* 获取最新策略
* @param deviceMessage
*/
private void handleNewPolicyMessage(DeviceMessage deviceMessage) {
List<RegisterMsgVo> interfaces = JsonDataParser.parseJsonData(deviceMessage.getData(), RegisterMsgVo.class);
if(!interfaces.isEmpty()) {
// 获取该服务器的策略信息
}
}
/**
* 服务器注册
* @param message
*/
private void handleRegisterMessage(DeviceMessage message) {
List<RegisterMsgVo> interfaces = JsonDataParser.parseJsonData(message.getData(), RegisterMsgVo.class);
if(!interfaces.isEmpty()) {
RegisterMsgVo registerMsg = interfaces.get(0);
// 自动注册服务器信息
RmRegisterMsgRemote rmRegisterMsgRemote = new RmRegisterMsgRemote();
BeanUtils.copyProperties(registerMsg, rmRegisterMsgRemote);
remoteRevenueConfigService.innerAddRegist(rmRegisterMsgRemote, SecurityConstants.INNER);
// 时间戳转换
long timestamp = registerMsg.getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
String timeStr = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", createTime);
List<NetworkInfo> networkInfoList = registerMsg.getNetworkInfo();
if(!networkInfoList.isEmpty()){
if(networkInfoList.size()==1){
NetworkInfo networkInfo = networkInfoList.get(0);
// 查询该网卡信息是否存在
RmNetworkInterface queryParam = new RmNetworkInterface();
queryParam.setMacAddress(networkInfo.getMac());
List<RmNetworkInterface> exits = rmNetworkInterfaceService.selectRmNetworkInterfaceList(queryParam);
if(exits.isEmpty()){
// 保存网卡信息
RmNetworkInterface insertData = new RmNetworkInterface();
// 业务ip和管理网ip
insertData.setBindIp("3");
insertData.setClientId(registerMsg.getClientId());
insertData.setIsp(networkInfo.getCarrier());
insertData.setCity(networkInfo.getCity());
insertData.setGateway(networkInfo.getGateway());
insertData.setInterfaceName(networkInfo.getName());
insertData.setIpv4Address(networkInfo.getIpv4());
insertData.setMacAddress(networkInfo.getMac());
insertData.setProvince(networkInfo.getProvince());
insertData.setPublicIp(networkInfo.getPublicIp());
insertData.setCreateTime(createTime);
rmNetworkInterfaceService.insertRmNetworkInterface(insertData);
}else{
RmNetworkInterface oldInterfaceMsg = exits.get(0);
if(!StringUtils.equals(networkInfo.getPublicIp(),oldInterfaceMsg.getPublicIp())){
RmNetworkInterface updateData = new RmNetworkInterface();
updateData.setMacAddress(networkInfo.getMac());
updateData.setIsp(networkInfo.getCarrier());
updateData.setCity(networkInfo.getCity());
updateData.setGateway(networkInfo.getGateway());
updateData.setInterfaceName(networkInfo.getName());
updateData.setIpv4Address(networkInfo.getIpv4());
updateData.setMacAddress(networkInfo.getMac());
updateData.setProvince(networkInfo.getProvince());
updateData.setPublicIp(networkInfo.getPublicIp());
rmNetworkInterfaceService.updateRmNetworkInterfaceByMac(updateData);
}
}
}else{
for (NetworkInfo networkInfo : networkInfoList) {
// 查询该网卡信息是否存在
RmNetworkInterface queryParam = new RmNetworkInterface();
queryParam.setMacAddress(networkInfo.getMac());
List<RmNetworkInterface> exits = rmNetworkInterfaceService.selectRmNetworkInterfaceList(queryParam);
if(exits.isEmpty()){
// 保存网卡信息
RmNetworkInterface insertData = new RmNetworkInterface();
// 业务ip和管理网ip
insertData.setClientId(registerMsg.getClientId());
insertData.setIsp(networkInfo.getCarrier());
insertData.setCity(networkInfo.getCity());
insertData.setGateway(networkInfo.getGateway());
insertData.setInterfaceName(networkInfo.getName());
insertData.setIpv4Address(networkInfo.getIpv4());
insertData.setMacAddress(networkInfo.getMac());
insertData.setProvince(networkInfo.getProvince());
insertData.setPublicIp(networkInfo.getPublicIp());
insertData.setCreateTime(createTime);
rmNetworkInterfaceService.insertRmNetworkInterface(insertData);
}else{
RmNetworkInterface oldInterfaceMsg = exits.get(0);
RmNetworkInterface updateData = new RmNetworkInterface();
updateData.setMacAddress(networkInfo.getMac());
updateData.setIsp(networkInfo.getCarrier());
updateData.setCity(networkInfo.getCity());
updateData.setGateway(networkInfo.getGateway());
updateData.setInterfaceName(networkInfo.getName());
updateData.setIpv4Address(networkInfo.getIpv4());
updateData.setMacAddress(networkInfo.getMac());
updateData.setProvince(networkInfo.getProvince());
updateData.setPublicIp(networkInfo.getPublicIp());
if(!StringUtils.equals(networkInfo.getPublicIp(),oldInterfaceMsg.getPublicIp())){
updateData.setBindIp("0");
// 修改绑定公网ip状态
RmResourceRegistrationRemote updateParam = new RmResourceRegistrationRemote();
updateParam.setClientId(registerMsg.getClientId());
updateParam.setMultiPublicIpStatus("0");
remoteRevenueConfigService.updateStatusByResource(updateParam, SecurityConstants.INNER);
}
rmNetworkInterfaceService.updateRmNetworkInterfaceByMac(updateData);
}
}
}
}
}
}
/**
* 多公网IP探测
* @param message
*/
private void handlePublicIpDetectMessage(DeviceMessage message) {
List<RspVo> rspVoList = JsonDataParser.parseJsonData(message.getData(), RspVo.class);
//TODO 公网信息入库
// 查询公网信息
// 如果公网信息有变化修改公网IP状态为需绑定
}
/**
* agent更新响应
* @param message
*/
private void handleAgentUpdateRspMessage(DeviceMessage message) {
List<RspVo> rspVoList = JsonDataParser.parseJsonData(message.getData(), RspVo.class);
if (!rspVoList.isEmpty()) {
RspVo rsp = rspVoList.get(0);
if(rsp.getResCode() == 1){
RmAgentManagement rmAgentManagement = new RmAgentManagement();
rmAgentManagement.setHardwareSn(message.getClientId());
rmAgentManagement.setLastUpdateResult("1");
rmAgentManagementService.updateRmAgentManagementByHardwareSn(rmAgentManagement);
}else{
RmAgentManagement rmAgentManagement = new RmAgentManagement();
rmAgentManagement.setHardwareSn(message.getClientId());
rmAgentManagement.setLastUpdateResult("0");
rmAgentManagementService.updateRmAgentManagementByHardwareSn(rmAgentManagement);
}
}
}
/**
* 注册消息处理器
*/
private void registerHandler(String dataType, Consumer<DeviceMessage> handler) {
messageHandlers.put(dataType, handler);
}
/**
* 处理设备消息(对外暴露的主方法)
*/
public void handleMessage(DeviceMessage message) {
String dataType = message.getDataType();
Consumer<DeviceMessage> handler = messageHandlers.get(dataType);
if (handler != null) {
handler.accept(message);
} else {
log.warn("未知数据类型:{}", dataType);
}
}
// ========== 具体的消息处理方法 ==========
/**
* 网络流量数据入库
* @param message
*/
private void handleNetMessage(DeviceMessage message) {
List<InitialBandwidthTraffic> interfaces = JsonDataParser.parseJsonData(message.getData(), InitialBandwidthTraffic.class);
if(!interfaces.isEmpty()){
// 时间戳转换
long timestamp = interfaces.get(0).getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
String timeStr = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",createTime);
InitialBandwidthTraffic data = new InitialBandwidthTraffic();
interfaces.forEach(iface -> {
iface.setClientId(message.getClientId());
iface.setCreateTime(createTime);
// 发送流量
iface.setOutSpeed(dataProcessUtil.bytesToBits(iface.getOutSpeed()));
// 接收流量
iface.setInSpeed(dataProcessUtil.bytesToBits(iface.getInSpeed()));
});
// 批量入库集合
data.setList(interfaces);
// 初始流量数据入库
initialBandwidthTrafficService.batchInsert(data);
EpsInitialTrafficDataRemote epsInitialTrafficDataRemote = new EpsInitialTrafficDataRemote();
epsInitialTrafficDataRemote.setStartTime(timeStr);
epsInitialTrafficDataRemote.setEndTime(timeStr);
// 复制到业务初始库
remoteRevenueConfigService.autoSaveServiceTrafficData(epsInitialTrafficDataRemote, SecurityConstants.INNER);
}else{
throw new RuntimeException("NET流量data数据为空");
}
}
/**
* docker数据入库
* @param message
*/
private void handleDockerMessage(DeviceMessage message) {
List<InitialDockerInfo> dockers = JsonDataParser.parseJsonData(message.getData(), InitialDockerInfo.class);
if(!dockers.isEmpty()){
// 时间戳转换
long timestamp = dockers.get(0).getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
dockers.forEach(iface -> {
iface.setClientId(message.getClientId());
iface.setCreateTime(createTime);
});
// 初始容器数据入库
initialDockerInfoService.batchInsertInitialDockerInfo(dockers);
}else{
throw new RuntimeException("DOCKER容器data数据为空");
}
}
/**
* cpu数据入库
* @param message
*/
private void handleCpuMessage(DeviceMessage message) {
List<InitialCpuInfo> cpus = JsonDataParser.parseJsonData(message.getData(),InitialCpuInfo.class);
// 时间戳转换
long timestamp = cpus.get(0).getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
if(!cpus.isEmpty()){
cpus.forEach(iface -> {
iface.setClientId(message.getClientId());
iface.setCreateTime(createTime);
});
// 初始CPU数据入库
initialCpuInfoService.batchInsertInitialCpuInfo(cpus);
}else{
throw new RuntimeException("CPUdata数据为空");
}
}
/**
* 磁盘数据入库
* @param message
*/
private void handleDiskMessage(DeviceMessage message) {
List<InitialDiskInfo> disks = JsonDataParser.parseJsonData(message.getData(), InitialDiskInfo.class);
// 时间戳转换
long timestamp = disks.get(0).getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
if(!disks.isEmpty()){
disks.forEach(iface -> {
iface.setClientId(message.getClientId());
iface.setCreateTime(createTime);
});
// 初始磁盘数据入库
initialDiskInfoService.batchInsertInitialDiskInfo(disks);
}else{
throw new RuntimeException("磁盘data数据为空");
}
}
/**
* 内存数据入库
* @param message
*/
private void handleMemoryMessage(DeviceMessage message) {
List<InitialMemoryInfo> memorys = JsonDataParser.parseJsonData(message.getData(), InitialMemoryInfo.class);
if(!memorys.isEmpty()){
// 时间戳转换
long timestamp = memorys.get(0).getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
memorys.forEach(iface -> {
iface.setClientId(message.getClientId());
iface.setCreateTime(createTime);
});
// 初始内存数据入库
initialMemoryInfoService.batchInsertInitialMemoryInfo(memorys);
}else{
throw new RuntimeException("内存data数据为空");
}
}
/**
* 挂载点数据入库
* @param message
*/
private void handleMountPointMessage(DeviceMessage message) {
List<InitialMountPointInfo> mountPointInfos = JsonDataParser.parseJsonData(message.getData(), InitialMountPointInfo.class);
if(!mountPointInfos.isEmpty()){
// 时间戳转换
long timestamp = mountPointInfos.get(0).getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
mountPointInfos.forEach(iface -> {
iface.setClientId(message.getClientId());
iface.setCreateTime(createTime);
});
// 初始挂载点数据入库
initialMountPointInfoService.batchInsertInitialMountPointInfo(mountPointInfos);
}else{
throw new RuntimeException("挂载点data数据为空");
}
}
/**
* 交换机所有数据入库
* @param message
*/
private void handleSwitchDataMessage(DeviceMessage message) {
List<CollectDataVo> switchData = JsonDataParser.parseJsonData(message.getData(), CollectDataVo.class);
if(!switchData.isEmpty()){
CollectDataVo switchDataVo = switchData.get(0);
switch(switchDataVo.getType()){
case "switchNetCollect":
handleSwitchNetMessage(switchDataVo, message.getClientId());
break;
case "switchPwrCollect":
handleSwitchPwrMessage(switchDataVo, message.getClientId());
break;
case "switchModuleCollect":
handleSwitchModuleMessage(switchDataVo, message.getClientId());
break;
case "switchMpuCollect":
handleSwitchMpuMessage(switchDataVo, message.getClientId());
break;
case "switchFanCollect":
handleSwitchFanMessage(switchDataVo, message.getClientId());
break;
default:
handleSwitchOtherMessage(switchDataVo, message.getClientId());
break;
}
}else{
throw new RuntimeException("交换机data数据为空");
}
}
/**
* 电源发现数据
* @param switchDataVo
*/
private void handleSwitchPwrMessage(CollectDataVo switchDataVo, String clientId){
List<InitialSwitchPowerSupply> powerSupplyList = SwitchJsonDataParser.parseJsonData(switchDataVo.getValue(), InitialSwitchPowerSupply.class);
if (!powerSupplyList.isEmpty()){
for (InitialSwitchPowerSupply insertData : powerSupplyList) {
// 时间戳转换
long timestamp = switchDataVo.getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
insertData.setClientId(clientId);
insertData.setCreateTime(createTime);
}
initialSwitchPowerSupplyService.insertBatchInitialSwitchPowerSupply(powerSupplyList);
}
}
/**
* 光模块发现数据
* @param switchDataVo
*/
private void handleSwitchModuleMessage(CollectDataVo switchDataVo, String clientId){
List<InitialSwitchOpticalModule> moduleList = SwitchJsonDataParser.parseJsonData(switchDataVo.getValue(), InitialSwitchOpticalModule.class);
if (!moduleList.isEmpty()){
for (InitialSwitchOpticalModule insertData : moduleList) {
// 时间戳转换
long timestamp = switchDataVo.getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
insertData.setClientId(clientId);
insertData.setCreateTime(createTime);
}
initialSwitchOpticalModuleService.batchInitialSwitchOpticalModule(moduleList);
}
}
/**
* MPU发现数据
* @param switchDataVo
*/
private void handleSwitchMpuMessage(CollectDataVo switchDataVo, String clientId){
List<InitialSwitchMpuInfo> mpuList = SwitchJsonDataParser.parseJsonData(switchDataVo.getValue(), InitialSwitchMpuInfo.class);
if (!mpuList.isEmpty()){
for (InitialSwitchMpuInfo insertData : mpuList) {
// 时间戳转换
long timestamp = switchDataVo.getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
insertData.setClientId(clientId);
insertData.setCreateTime(createTime);
}
initialSwitchMpuInfoService.insertBatchInitialSwitchMpuInfo(mpuList);
}
}
/**
* 风扇发现数据
* @param switchDataVo
*/
private void handleSwitchFanMessage(CollectDataVo switchDataVo, String clientId){
List<InitialSwitchFanInfo> fanList = SwitchJsonDataParser.parseJsonData(switchDataVo.getValue(), InitialSwitchFanInfo.class);
if (!fanList.isEmpty()){
for (InitialSwitchFanInfo insertData : fanList) {
// 时间戳转换
long timestamp = switchDataVo.getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
insertData.setClientId(clientId);
insertData.setCreateTime(createTime);
}
initialSwitchFanInfoService.insertBatchInitialSwitchFanInfo(fanList);
}
}
/**
* 其他发现数据(默认处理)
* @param switchDataVo
*/
private void handleSwitchOtherMessage(CollectDataVo switchDataVo, String clientId) {
if (switchDataVo != null) {
try {
InitialSwitchOtherCollectData insertData = new InitialSwitchOtherCollectData();
// 设置基本信息
long timestamp = switchDataVo.getTimestamp();
Date createTime = new Date(timestamp * 1000 / 1000 * 1000);
insertData.setClientId(clientId);
insertData.setCreateTime(createTime);
String value = switchDataVo.getValue();
JsonNode jsonNode = objectMapper.readTree(value);
// 处理数组中的字符串JSON
if (jsonNode.isArray() && jsonNode.size() > 0) {
JsonNode firstElement = jsonNode.get(0);
if (firstElement.isTextual()) {
// 二次解析JSON字符串
JsonNode innerJsonNode = objectMapper.readTree(firstElement.asText());
if (innerJsonNode.isObject()) {
Iterator<Map.Entry<String, JsonNode>> fields = innerJsonNode.fields();
if (fields.hasNext()) {
Map.Entry<String, JsonNode> entry = fields.next();
String fieldName = entry.getKey();
String fieldValue = entry.getValue().asText();
insertData.setCollectType(fieldName);
if (!"null".equals(fieldValue)) {
insertData.setCollectValue(fieldValue);
insertInitialSwitchOtherInfo.insertInitialSwitchOtherCollectData(insertData);
}
}
}
}
}
} catch (Exception e) {
log.error("解析JSON数据失败: {}, value: {}", e.getMessage(), switchDataVo.getValue(), e);
}
}
}
/**
* 交换机网卡信息数据入库
* @param switchDataVo
*/
private void handleSwitchNetMessage(CollectDataVo switchDataVo, String clientId) {
List<InitialSwitchInfo> switchInfos = SwitchJsonDataParser.parseJsonData(switchDataVo.getValue(), InitialSwitchInfo.class);
if(!switchInfos.isEmpty()){
// 根据clientId查询交换机ip
RmResourceRegistrationRemote queryParam = new RmResourceRegistrationRemote();
queryParam.setHardwareSn(clientId);
R<RmResourceRegistrationRemote> registMsgR = remoteRevenueConfigService.getListByHardwareSn(queryParam, SecurityConstants.INNER);
if(registMsgR != null){
RmResourceRegistrationRemote registMsg = registMsgR.getData();
}
// 时间戳转换
long timestamp = switchDataVo.getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
String timeStr = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",createTime);
// 查询临时表信息,计算实际流量值
InitialSwitchInfoTemp temp = new InitialSwitchInfoTemp();
temp.setClientId(clientId);
List<InitialSwitchInfoTemp> tempList = initialSwitchInfoTempService.selectInitialSwitchInfoTempList(temp);
if(!tempList.isEmpty()){
// 1. 构建快速查找的Map
Map<String, InitialSwitchInfoTemp> tempMap = tempList.stream()
.collect(Collectors.toMap(
InitialSwitchInfoTemp::getName,
Function.identity(),
(existing, replacement) -> existing
));
// 2. 预计算除数(避免重复创建对象)
BigDecimal divisor = new BigDecimal(300);
// 3. 计算速度
switchInfos.forEach(switchInfo -> {
switchInfo.setClientId(clientId);
switchInfo.setCreateTime(createTime);
if(registMsgR != null && registMsgR.getData()!=null && registMsgR.getData().getSnmpCollectAddr()!=null){
switchInfo.setSwitchIp(registMsgR.getData().getSnmpCollectAddr());
}
InitialSwitchInfoTemp tempInfo = tempMap.get(switchInfo.getName());
if (tempInfo != null) {
// 计算inSpeed
if (switchInfo.getInBytes() != null && tempInfo.getInBytes() != null) {
BigDecimal inDiff = switchInfo.getInBytes().subtract(tempInfo.getInBytes());
// 字节转为bit
BigDecimal inDiffBit = inDiff.multiply(new BigDecimal(8)).setScale(0, RoundingMode.HALF_UP);
switchInfo.setInSpeed(inDiffBit.divide(divisor, 2, RoundingMode.HALF_UP));
}
// 计算outSpeed
if (switchInfo.getOutBytes() != null && tempInfo.getOutBytes() != null) {
BigDecimal outDiff = switchInfo.getOutBytes().subtract(tempInfo.getOutBytes());
// 字节转为bit
BigDecimal outDiffBit = outDiff.multiply(new BigDecimal(8)).setScale(0, RoundingMode.HALF_UP);
switchInfo.setOutSpeed(outDiffBit.divide(divisor, 2, RoundingMode.HALF_UP));
}
}
});
}else{
switchInfos.forEach(switchInfo -> {
switchInfo.setClientId(clientId);
switchInfo.setCreateTime(createTime);
if(registMsgR != null && registMsgR.getData()!=null && registMsgR.getData().getSnmpCollectAddr()!=null){
switchInfo.setSwitchIp(registMsgR.getData().getSnmpCollectAddr());
}
});
}
// 清空临时表对应switch信息
initialSwitchInfoTempService.truncateSwitchInfoTemp(clientId);
// 临时表 用来计算inSpeed outSeppd
initialSwitchInfoTempService.batchInsertInitialSwitchInfoTemp(switchInfos);
// 初始交换机数据入库
initialSwitchInfoService.batchInsertInitialSwitchInfo(switchInfos);
// 业务表入库
InitialSwitchInfoDetailsRemote detailsRemote = new InitialSwitchInfoDetailsRemote();
detailsRemote.setClientId(clientId);
detailsRemote.setStartTime(timeStr);
detailsRemote.setEndTime(timeStr);
remoteRevenueConfigService.autoSaveSwitchTraffic(detailsRemote, SecurityConstants.INNER);
}else{
throw new RuntimeException("交换机data数据为空");
}
}
/**
* 系统其他信息
* @param message
*/
private void handleOtherSystemMessage(DeviceMessage message) {
List<CollectDataVo> otherData = JsonDataParser.parseJsonData(message.getData(), CollectDataVo.class);
if(!otherData.isEmpty()){
CollectDataVo systemDataVo = otherData.get(0);
if (systemDataVo != null){
try {
InitialSystemOtherCollectData insertData = new InitialSystemOtherCollectData();
// 时间戳转换
long timestamp = systemDataVo.getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
insertData.setClientId(message.getClientId());
insertData.setCreateTime(createTime);
String collectType = systemDataVo.getType();
String collectValue = systemDataVo.getValue();
insertData.setCollectType(collectType);
insertData.setCollectValue(collectValue);
iInitialSystemOtherCollectDataService.insertInitialSystemOtherCollectData(insertData);
} catch (Exception e) {
log.error("解析JSON数据失败: {}, value: {}", e.getMessage(), systemDataVo.getValue());
// 可以选择保存原始数据或进行其他错误处理
}
}
}
}
/**
* 监听心跳
* @param message
*/
private void handleHeartbeatMessage(DeviceMessage message) {
List<InitialHeartbeatListen> heartbeats = JsonDataParser.parseJsonData(message.getData(), InitialHeartbeatListen.class);
if(!heartbeats.isEmpty()){
InitialHeartbeatListen heartbeat = heartbeats.get(0);
String clientId = message.getClientId();
log.debug("处理心跳消息客户端ID: {}, 时间: {}", clientId, heartbeat.getTimestamp());
// 使用Redis存储状态
String statusKey = HEARTBEAT_STATUS_PREFIX + clientId;
String timeKey = HEARTBEAT_TIME_PREFIX + clientId;
String recoveryCountKey = HEARTBEAT_RECOVERY_COUNT_PREFIX + clientId;
try {
// 记录处理前状态(调试用)
String prevStatus = redisTemplate.opsForValue().get(statusKey);
String prevTime = redisTemplate.opsForValue().get(timeKey);
log.debug("客户端ID: {} 处理前状态 - status: {}, time: {}", clientId, prevStatus, prevTime);
// 使用事务确保原子性操作
redisTemplate.execute(new SessionCallback<Object>() {
@Override
public Object execute(RedisOperations operations) throws DataAccessException {
operations.multi();
// 重置丢失计数为0设置最后心跳时间
operations.opsForValue().set(statusKey, "0");
operations.opsForValue().set(timeKey, String.valueOf(System.currentTimeMillis()));
return operations.exec();
}
});
log.debug("客户端ID: {} 心跳处理完成,重置状态和时间", clientId);
// 检查是否之前有告警状态
if (Boolean.TRUE.equals(redisTemplate.hasKey(HEARTBEAT_ALERT_PREFIX + clientId))) {
// 获取当前恢复次数
String recoveryCountStr = redisTemplate.opsForValue().get(recoveryCountKey);
int recoveryCount = (recoveryCountStr == null) ? 1 : Integer.parseInt(recoveryCountStr) + 1;
if (recoveryCount == 2) {
// 达到2次恢复执行状态修改
log.warn("客户端ID: {} 心跳恢复达到2次修改设备状态为在线", clientId);
insertHeartbeatLog(clientId, "2", "心跳恢复,设备在线状态改为在线");
redisTemplate.delete(HEARTBEAT_ALERT_PREFIX + clientId);
redisTemplate.delete(recoveryCountKey); // 清除恢复计数器
// 修改资源状态
updateResourceStatus(clientId, "1");
} else {
// 未达到2次只记录恢复次数
log.info("客户端ID: {} 心跳恢复第{}次", clientId, recoveryCount);
redisTemplate.opsForValue().set(recoveryCountKey, String.valueOf(recoveryCount));
}
}
} catch (Exception e) {
log.error("处理心跳消息异常, clientId: {}", clientId, e);
}
}
}
// 添加一个定时任务方法,定期检查心跳状态
@Scheduled(fixedRate = 60000) // 每60s检查一次
public void checkHeartbeatStatus() {
long currentTime = System.currentTimeMillis();
log.debug("开始心跳状态检查,当前时间: {}", currentTime);
// 获取所有客户端时间键
Set<String> timeKeys = redisTemplate.keys(HEARTBEAT_TIME_PREFIX + "*");
if (timeKeys == null) {
log.debug("未找到任何心跳时间键");
return;
}
log.debug("找到 {} 个客户端需要检查", timeKeys.size());
for (String timeKey : timeKeys) {
String clientId = timeKey.substring(HEARTBEAT_TIME_PREFIX.length());
String statusKey = HEARTBEAT_STATUS_PREFIX + clientId;
String alertKey = HEARTBEAT_ALERT_PREFIX + clientId;
try {
// 检查是否已经存在告警
String existingAlert = redisTemplate.opsForValue().get(alertKey);
if ("1".equals(existingAlert)) {
log.debug("客户端ID: {} 已有告警,跳过检查", clientId);
continue; // 如果已有告警,跳过处理
}
String lastTimeStr = redisTemplate.opsForValue().get(timeKey);
if (lastTimeStr == null) {
log.debug("客户端ID: {} 时间键为空,跳过", clientId);
continue;
}
long lastHeartbeatTime = Long.parseLong(lastTimeStr);
long timeDiff = currentTime - lastHeartbeatTime;
log.debug("客户端ID: {} 最后心跳: {}, 时间差: {}ms, 超时阈值: {}ms",
clientId, lastHeartbeatTime, timeDiff, HEARTBEAT_TIMEOUT);
if (timeDiff > HEARTBEAT_TIMEOUT) {
// 心跳超时处理 - 使用原子操作增加计数
Long lostCount = redisTemplate.opsForValue().increment(statusKey);
if (lostCount == 1) {
// 确保第一次增加时值为1
redisTemplate.opsForValue().set(statusKey, "1");
lostCount = 1L;
}
log.warn("客户端ID: {} 心跳超时,连续次数: {}, 时间差: {}ms",
clientId, lostCount, timeDiff);
if (lostCount >= 3) {
log.warn("客户端ID: {} 连续三次心跳丢失,触发告警", clientId);
insertHeartbeatLog(clientId, "3", "连续三次心跳丢失");
redisTemplate.opsForValue().set(HEARTBEAT_ALERT_PREFIX + clientId, "1");
// 设置告警后删除timeKey和statusKey
redisTemplate.delete(timeKey);
redisTemplate.delete(statusKey);
log.info("客户端ID: {} 已设置告警并清理心跳记录", clientId);
// 修改资源状态
updateResourceStatus(clientId, "0");
}
} else {
// 如果心跳正常,重置丢失次数
String currentStatus = redisTemplate.opsForValue().get(statusKey);
if (!"0".equals(currentStatus)) {
redisTemplate.opsForValue().set(statusKey, "0");
log.debug("客户端ID: {} 心跳正常,重置丢失次数从 {} 到 0", clientId, currentStatus);
} else {
log.debug("客户端ID: {} 心跳正常状态已是0", clientId);
}
}
} catch (Exception e) {
log.error("检查心跳状态异常, clientId: {}", clientId, e);
}
}
log.debug("心跳状态检查完成");
}
// 更新资源状态的公共方法
private void updateResourceStatus(String clientId, String status) {
log.info("开启更新资源状态========");
RmResourceRegistrationRemote rmResourceRegistrationRemote = new RmResourceRegistrationRemote();
rmResourceRegistrationRemote.setOnlineStatus(status);
rmResourceRegistrationRemote.setRegistrationStatus(status);
rmResourceRegistrationRemote.setHardwareSn(clientId);
remoteRevenueConfigService.updateStatusByResource(rmResourceRegistrationRemote, SecurityConstants.INNER);
}
// 插入心跳日志到数据库
private void insertHeartbeatLog(String machineId, String status, String remark) {
try {
InitialHeartbeatListenLog listenLog = new InitialHeartbeatListenLog();
listenLog.setClientId(machineId);
listenLog.setStatus(status); // 0-离线 1-在线 2-恢复 3-三次丢失
listenLog.setRemark(remark);
listenLog.setCreateTime(new Date());
// 调用DAO或Service插入日志
initialHeartbeatListenLog.insertInitialHeartbeatListenLog(listenLog);
log.info("已记录心跳日志客户端ID: {}, 状态: {}", machineId, status);
} catch (Exception e) {
log.error("插入心跳日志失败", e);
}
}
/**
* 应答信息
* @param message
*/
private RspVo handleResponseMessage(DeviceMessage message) {
List<RspVo> rspVoList = JsonDataParser.parseJsonData(message.getData(), RspVo.class);
if (!rspVoList.isEmpty()) {
RspVo rsp = rspVoList.get(0);
log.info("应答信息:{}",rsp);
return rsp;
}
return null;
}
/**
* 脚本策略应答信息
* @param message
*/
private void handleScriptRspMessage(DeviceMessage message) {
List<RspVo> rspVoList = JsonDataParser.parseJsonData(message.getData(), RspVo.class);
if (!rspVoList.isEmpty()) {
RspVo rsp = rspVoList.get(0);
// 时间戳转换
long timestamp = rsp.getTimestamp();
long millis = timestamp * 1000;
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
// 查询资源信息
RmResourceRegistrationRemote queryParam = new RmResourceRegistrationRemote();
queryParam.setHardwareSn(message.getClientId());
R<RmResourceRegistrationRemote> resourceMsg = remoteRevenueConfigService.getListByHardwareSn(queryParam, SecurityConstants.INNER);
RmResourceRegistrationRemote resourceMsgData = resourceMsg.getData();
if(rsp.getResCode() == 1){
if(rsp.getResult() != null){
// 构建脚本执行结果实体类
RmResourceRemote insertData = new RmResourceRemote();
insertData.setHardwareSn(message.getClientId());
insertData.setResourceName(resourceMsgData.getResourceName());
insertData.setExternalIp(resourceMsgData.getIpAddress());
insertData.setManagementPort(22);
insertData.setConnectionMethod("1");
insertData.setResourceType(resourceMsgData.getResourceType());
insertData.setDescription(rsp.getResult());
insertData.setCreateTime(createTime);
// 执行插入sql
rmResourceRemoteService.insertRmResourceRemote(insertData);
log.info("脚本执行结果入库成功:{}",rsp);
}
}else{
log.error("脚本执行失败:{}", rsp);
}
}
}
}

View File

@@ -1,8 +1,9 @@
package com.ruoyi.rocketmq.mapper;
import java.util.List;
import com.ruoyi.rocketmq.domain.RmMonitorPolicy;
import java.util.List;
/**
* 资源监控策略Mapper接口
*
@@ -58,4 +59,11 @@ public interface RmMonitorPolicyMapper
* @return 结果
*/
public int deleteRmMonitorPolicyByIds(Long[] ids);
/**
* 查询符合条件的策略
* @param queryParam
* @return
*/
RmMonitorPolicy selectRmMonitorPolicyByClientId(RmMonitorPolicy queryParam);
}

View File

@@ -1,8 +1,9 @@
package com.ruoyi.rocketmq.mapper;
import java.util.List;
import com.ruoyi.rocketmq.domain.RmNetworkInterface;
import java.util.List;
/**
* 客户端网络接口信息Mapper接口
*
@@ -58,4 +59,6 @@ public interface RmNetworkInterfaceMapper
* @return 结果
*/
public int deleteRmNetworkInterfaceByIds(Long[] ids);
int updateRmNetworkInterfaceByMac(RmNetworkInterface rmNetworkInterface);
}

View File

@@ -1,6 +1,7 @@
package com.ruoyi.rocketmq.service;
import com.ruoyi.rocketmq.domain.RmMonitorPolicy;
import com.ruoyi.rocketmq.domain.vo.CollectVo;
import java.util.List;
import java.util.Map;
@@ -83,4 +84,11 @@ public interface IRmMonitorPolicyService
* @return
*/
int updatePolicyMsgInner(RmMonitorPolicy rmMonitorPolicy);
/**
*
* @param clientId
* @return
*/
public List<CollectVo> getPolicyMsgByClientId(String clientId);
}

View File

@@ -1,8 +1,9 @@
package com.ruoyi.rocketmq.service;
import java.util.List;
import com.ruoyi.rocketmq.domain.RmNetworkInterface;
import java.util.List;
/**
* 客户端网络接口信息Service接口
*
@@ -42,6 +43,13 @@ public interface IRmNetworkInterfaceService
* @return 结果
*/
public int updateRmNetworkInterface(RmNetworkInterface rmNetworkInterface);
/**
* 修改客户端网络接口信息
*
* @param rmNetworkInterface 客户端网络接口信息
* @return 结果
*/
public int updateRmNetworkInterfaceByMac(RmNetworkInterface rmNetworkInterface);
/**
* 批量删除客户端网络接口信息

View File

@@ -499,7 +499,37 @@ public class RmMonitorPolicyServiceImpl implements IRmMonitorPolicyService
return 0;
}
}
/**
* 根据clientId得到监控策略信息
* @param clientId
* @return
*/
@Override
public List<CollectVo> getPolicyMsgByClientId(String clientId) {
try {
// 根据clientId查询未下发的策略信息
RmMonitorPolicy queryParam = new RmMonitorPolicy();
queryParam.setDeployDevice(clientId);
queryParam.setPriority("1");
queryParam.setStatus("0");
queryParam.setResourceType("linux");
RmMonitorPolicy policy = rmMonitorPolicyMapper.selectRmMonitorPolicyByClientId(queryParam);
if (policy == null) {
log.error("无待下发策略clientId: {}", clientId);
return null;
}
// 构建并发送采集配置
Map<String, Object> policyDetails = getRmMonitorPolicyMsgById(policy.getId());
List<CollectVo> collectVos = buildCollectConfigurations(policyDetails);
// 去重
List<CollectVo> uniqueList = collectVos.stream().distinct().collect(Collectors.toList());
return collectVos;
} catch (Exception e) {
log.error("下发策略失败clientId: {}", clientId, e);
return null;
}
}
private SwitchOidVo buildOids(Map<String, Object> policyDetails) {
// 处理Switch配置
Map<String, List<RmTemplateSwitch>> switchConfigs = (Map<String, List<RmTemplateSwitch>>) policyDetails.get("switch");

View File

@@ -1,12 +1,13 @@
package com.ruoyi.rocketmq.service.impl;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.rocketmq.domain.RmNetworkInterface;
import com.ruoyi.rocketmq.mapper.RmNetworkInterfaceMapper;
import com.ruoyi.rocketmq.service.IRmNetworkInterfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rocketmq.mapper.RmNetworkInterfaceMapper;
import com.ruoyi.rocketmq.domain.RmNetworkInterface;
import com.ruoyi.rocketmq.service.IRmNetworkInterfaceService;
import java.util.List;
/**
* 客户端网络接口信息Service业务层处理
@@ -70,6 +71,12 @@ public class RmNetworkInterfaceServiceImpl implements IRmNetworkInterfaceService
return rmNetworkInterfaceMapper.updateRmNetworkInterface(rmNetworkInterface);
}
@Override
public int updateRmNetworkInterfaceByMac(RmNetworkInterface rmNetworkInterface) {
rmNetworkInterface.setUpdateTime(DateUtils.getNowDate());
return rmNetworkInterfaceMapper.updateRmNetworkInterfaceByMac(rmNetworkInterface);
}
/**
* 批量删除客户端网络接口信息
*

View File

@@ -31,7 +31,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<where>
<if test="templateId != null "> and template_id = #{templateId}</if>
<if test="resourceType != null "> and resource_type = #{resourceType}</if>
<if test="resourceType != null "> and resource_type = #{resourceType}</if>
<if test="resourceGroupId != null "> and resource_group_id = #{resourceGroupId}</if>
<if test="policyName != null and policyName != ''"> and policy_name like concat('%', #{policyName}, '%')</if>
<if test="description != null and description != ''"> and description = #{description}</if>
@@ -119,4 +118,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{id}
</foreach>
</delete>
<select id="selectRmMonitorPolicyByClientId" parameterType="RmMonitorPolicy" resultMap="RmMonitorPolicyResult">
<include refid="selectRmMonitorPolicyVo"/>
<where>
<if test="resourceType != null "> and resource_type = #{resourceType}</if>
<if test="priority != null "> and priority = #{priority}</if>
<if test="status != null "> and status = #{status}</if>
<if test="deployDevice != null ">
FIND_IN_SET(#{deployDevice}, REPLACE(deploy_device, '\n', ',')) > 0
</if>
</where>
limit 1
</select>
</mapper>

View File

@@ -108,6 +108,26 @@
</trim>
where id = #{id}
</update>
<update id="updateRmNetworkInterfaceByMac" parameterType="RmNetworkInterface">
update rm_network_interface
<trim prefix="SET" suffixOverrides=",">
<if test="isp != null">isp = #{isp},</if>
<if test="province != null">province = #{province},</if>
<if test="city != null">city = #{city},</if>
<if test="publicIp != null">public_ip = #{publicIp},</if>
<if test="interfaceName != null and interfaceName != ''">interface_name = #{interfaceName},</if>
<if test="macAddress != null">mac_address = #{macAddress},</if>
<if test="interfaceType != null">interface_type = #{interfaceType},</if>
<if test="ipv4Address != null">ipv4_address = #{ipv4Address},</if>
<if test="gateway != null">gateway = #{gateway},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="createBy != null">create_by = #{createBy},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="bindIp != null">bind_ip = #{bindIp},</if>
</trim>
where mac_address = #{macAddress}
</update>
<delete id="deleteRmNetworkInterfaceById" parameterType="Long">
delete from rm_network_interface where id = #{id}