优化交换机数据采集、脚本策略、agent更新优化、注册增加路由信息

This commit is contained in:
gaoyutao
2025-10-24 18:55:21 +08:00
parent 37eff51e48
commit 36362e2236
23 changed files with 263 additions and 98 deletions
@@ -187,5 +187,15 @@ public class RmMonitorPolicyController extends BaseController
int rows = rmMonitorPolicyService.updatePolicyMsgInner(rmMonitorPolicy);
return R.ok(rows);
}
/**
* 修改资源监控策略
*/
@GetMapping("/issueDefaultPolicyByClientId")
@InnerAuth
public R<Integer> issueDefaultPolicyByClientId(String clientId)
{
int rows = rmMonitorPolicyService.issueDefaultPolicyByClientId(clientId);
return R.ok(rows);
}
}
@@ -6,6 +6,7 @@ import com.ruoyi.common.core.web.domain.BaseEntity;
import lombok.Data;
import java.util.Date;
import java.util.List;
/**
* 服务器脚本策略对象 rm_deployment_policy
@@ -89,4 +90,16 @@ public class RmDeploymentPolicy extends BaseEntity
private String scriptPath;
/** 业务脚本参数 */
private String defaultParams;
/** 不在线数量 */
private Integer offlineNum;
/** 执行成功数量 */
private Integer sucessNum;
/** 执行失败数量 */
private Integer failNum;
/** 不在线服务器clientId集合 */
private List<String> offlineClientIds;
/** 执行成功clientId集合 */
private List<String> sucessClientIds;
/** 执行失败clientIdji和 */
private List<String> failClientIds;
}
@@ -59,5 +59,7 @@ public class RmNetworkInterface extends BaseEntity
private String gateway;
/** 绑定ip 1业务IP2管理ip */
private String bindIp;
/** 是否为新信息 */
private String newFlag;
}
@@ -55,4 +55,10 @@ public class RmResourceRemote extends BaseEntity
private String connectionMethod;
/** 脚本执行结果 */
private String commandResult;
/** 客户端id */
private String clientId;
/** 执行结果标识(0 失败 1成功) */
private Integer resultFlag;
/** 脚本id */
private Long scriptId;
}
@@ -4,6 +4,8 @@ import lombok.Data;
@Data
public class ServerScriptPolicyVo {
/** 服务器脚本策略id */
private String scriptId;
/** 策略名称 */
private String policyName;
/** 外网Http(s)地址 */
@@ -90,12 +90,12 @@ public class MessageHandler {
*/
@PostConstruct
public void init() {
// registerHandler(MsgEnum.执行脚本策略应答.getValue(), this::handleScriptRspMessage);
registerHandler(MsgEnum.执行脚本策略应答.getValue(), this::handleScriptRspMessage);
// registerHandler(MsgEnum.Agent版本更新应答.getValue(), this::handleAgentUpdateRspMessage);
// 其他类型消息可以单独注册处理器
registerHandler(MsgEnum.注册.getValue(), this::handleRegisterMessage);
// registerHandler(MsgEnum.获取最新策略.getValue(), this::handleNewPolicyMessage);
registerHandler(MsgEnum.获取最新策略.getValue(), this::handleNewPolicyMessage);
// registerHandler(MsgEnum.CPU上报.getValue(), this::handleCpuMessage);
// registerHandler(MsgEnum.磁盘上报.getValue(), this::handleDiskMessage);
// registerHandler(MsgEnum.容器上报.getValue(), this::handleDockerMessage);
@@ -116,7 +116,7 @@ public class MessageHandler {
RegisterMsgVo registerMsgVo = interfaces.get(0);
String clientId = registerMsgVo.getClientId();
// 如果未下发监控策略,下发
rmMonitorPolicyService.issuePolicyMsgByClientId(clientId);
// rmMonitorPolicyService.issuePolicyMsgByClientId(clientId);
// 如果未下发服务器脚本策略,下发
rmDeploymentPolicyService.issueDeployPolicyMsgByClientId(clientId);
}
@@ -654,19 +654,15 @@ public class MessageHandler {
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
// 查询资源信息
RmResourceRegistrationRemote queryParam = new RmResourceRegistrationRemote();
queryParam.setHardwareSn(message.getClientId());
queryParam.setClientId(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.setClientId(message.getClientId());
insertData.setResultFlag(1);
insertData.setDescription(rsp.getResult());
insertData.setCreateTime(createTime);
// 执行插入sql
@@ -674,6 +670,16 @@ public class MessageHandler {
log.info("脚本执行结果入库成功:{}",rsp);
}
}else{
// 构建脚本执行结果实体类
RmResourceRemote insertData = new RmResourceRemote();
insertData.setClientId(message.getClientId());
insertData.setResultFlag(0);
if(rsp.getResult() != null){
insertData.setDescription(rsp.getResult());
}
insertData.setCreateTime(createTime);
// 执行插入sql
rmResourceRemoteService.insertRmResourceRemote(insertData);
log.error("脚本执行失败:{}", rsp);
}
}
@@ -110,9 +110,16 @@ public class InitialSystemOtherCollectDataServiceImpl implements IInitialSystemO
*/
@Override
public Map getMonitorMsg(InitialSystemOtherCollectData initialSystemOtherCollectData) {
Map map = initialSystemOtherCollectDataMapper.getMonitorMsg(initialSystemOtherCollectData);
Map<String, Object> map = initialSystemOtherCollectDataMapper.getMonitorMsg(initialSystemOtherCollectData);
// 如果返回null,初始化一个空的Map
if (map == null) {
map = new HashMap<>();
}
// 获取cpu数量
InitialCpuInfo cpuInfo = initialCpuInfoMapper.getCpuInfoByClientId(initialSystemOtherCollectData.getClientId());
if (cpuInfo == null) {
cpuInfo = new InitialCpuInfo();
}
map.put("systemCpuNum", cpuInfo.getNum());
return map;
}
@@ -8,11 +8,13 @@ import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.rocketmq.domain.DeviceMessage;
import com.ruoyi.rocketmq.domain.RmDeploymentPolicy;
import com.ruoyi.rocketmq.domain.RmResourceRemote;
import com.ruoyi.rocketmq.domain.vo.PolicyTypeVo;
import com.ruoyi.rocketmq.domain.vo.PolicyVo;
import com.ruoyi.rocketmq.domain.vo.ScriptPolicyVo;
import com.ruoyi.rocketmq.domain.vo.ServerScriptPolicyVo;
import com.ruoyi.rocketmq.mapper.RmDeploymentPolicyMapper;
import com.ruoyi.rocketmq.mapper.RmResourceRemoteMapper;
import com.ruoyi.rocketmq.model.ProducerMode;
import com.ruoyi.rocketmq.producer.MessageProducer;
import com.ruoyi.rocketmq.service.IRmDeploymentPolicyService;
@@ -45,6 +47,8 @@ public class RmDeploymentPolicyServiceImpl implements IRmDeploymentPolicyService
private DataProcessUtil dataProcessUtil;
@Autowired
private ProducerMode producerMode;
@Autowired
private RmResourceRemoteMapper rmResourceRemoteMapper;
/**
* 查询服务器脚本策略
@@ -74,6 +78,30 @@ public class RmDeploymentPolicyServiceImpl implements IRmDeploymentPolicyService
// 包含设备名称赋值
deploymentPolicyList.forEach(policy -> {
getIncludeNameById(policy);
// 不在线数量赋值
String deviceMsg = policy.getDeployDevice();
if (deviceMsg != null) {
List<String> offOnlineList = new ArrayList<>();
List<String> successList = new ArrayList<>();
List<String> failList = new ArrayList<>();
for (String clientId : deviceMsg.split("\n")) {
RmResourceRemote query = new RmResourceRemote();
query.setScriptId(policy.getId());
query.setClientId(clientId);
List<RmResourceRemote> resultList = rmResourceRemoteMapper.selectRmResourceRemoteList(query);
if (resultList.isEmpty()) {
offOnlineList.add(clientId);
continue;
}
resultList.forEach(resourceRemote -> {
if (resourceRemote.getResultFlag() == 0) {
failList.add(clientId);
} else if (resourceRemote.getResultFlag() == 1) {
successList.add(clientId);
}
});
}
}
});
return deploymentPolicyList;
@@ -279,6 +307,7 @@ public class RmDeploymentPolicyServiceImpl implements IRmDeploymentPolicyService
for (RmDeploymentPolicy policy : policyList) {
// 构建并发送脚本配置
ServerScriptPolicyVo scriptPolicyVo = new ServerScriptPolicyVo();
scriptPolicyVo.setScriptId(policy.getId().toString());
scriptPolicyVo.setPolicyName(policy.getPolicyName());
scriptPolicyVo.setFileUrl(policy.getScriptPath());
scriptPolicyVo.setCommandParams(policy.getDefaultParams());
@@ -101,7 +101,8 @@ public class DynamicOidCollector {
// 判断是否需要索引处理
boolean needIndex = needIndexProcessing(oidParams, deviceType);
// 获取过滤条件
List<String> filterValues = switchVo.getFilterByType(deviceType);
String filterType = deviceType + "OID";
List<String> filterValues = switchVo.getFilterByType(filterType);
log.info("采集交换机 {} 的 {} 设备信息,筛选条件: {}",
switchVo.getIp(), deviceType, filterValues);
@@ -156,7 +157,9 @@ public class DynamicOidCollector {
"1.3.6.1.2.1.1.4.0", // sysContact
"1.3.6.1.2.1.1.5.0", // sysName
"1.3.6.1.2.1.1.6.0", // sysLocation
"1.3.6.1.4.1.2011.5.25.183.1.4.0" // hwStackSystemMac
"1.3.6.1.4.1.2011.5.25.183.1.4.0", // hwStackSystemMac
"1.3.6.1.4.1.2011.6.157.1.3.0", // hwAveragePower
"1.3.6.1.4.1.2011.6.157.1.6.0" // hwCurrentPower
));
// 检查所有配置的OID
@@ -156,7 +156,6 @@ public class MultiSwitchCollectionScheduler {
// OID配置(复制所有OID Map
resultMap.setNetOID(new LinkedHashMap<>(switchVo.getNetOID()));
resultMap.setEntityOID(new LinkedHashMap<>(switchVo.getEntityOID()));
resultMap.setPwrOID(new LinkedHashMap<>(switchVo.getPwrOID()));
resultMap.setFanOID(new LinkedHashMap<>(switchVo.getFanOID()));
resultMap.setMpuOID(new LinkedHashMap<>(switchVo.getMpuOID()));
@@ -157,7 +157,7 @@
<where>
<if test="policyStatus != null and policyStatus != ''"> and policy_status = #{policyStatus}</if>
<if test="deployDevice != null ">
FIND_IN_SET(#{deployDevice}, REPLACE(deploy_device, '\n', ',')) > 0
and FIND_IN_SET(#{deployDevice}, REPLACE(deploy_device, '\n', ',')) > 0
</if>
</where>
</select>
@@ -21,10 +21,11 @@
<result property="createBy" column="create_by" />
<result property="updateBy" column="update_by" />
<result property="bindIp" column="bind_ip" />
<result property="newFlag" column="new_flag" />
</resultMap>
<sql id="selectRmNetworkInterfaceVo">
select id, client_id, isp, province, city, public_ip, interface_name, mac_address, interface_type, ipv4_address, gateway, create_time, update_time, create_by, update_by, bind_ip from rm_network_interface
select id, client_id, isp, province, city, public_ip, interface_name, mac_address, interface_type, ipv4_address, gateway, create_time, update_time, create_by, update_by, bind_ip, new_flag from rm_network_interface
</sql>
<select id="selectRmNetworkInterfaceList" parameterType="RmNetworkInterface" resultMap="RmNetworkInterfaceResult">
@@ -41,6 +42,7 @@
<if test="ipv4Address != null and ipv4Address != ''"> and ipv4_address = #{ipv4Address}</if>
<if test="gateway != null and gateway != ''"> and gateway = #{gateway}</if>
<if test="bindIp != null and bindIp != ''"> and bind_ip = #{bindIp}</if>
<if test="newFlag != null "> and new_flag = #{newFlag}</if>
</where>
</select>
@@ -67,6 +69,7 @@
<if test="createBy != null">create_by,</if>
<if test="updateBy != null">update_by,</if>
<if test="bindIp != null">bind_ip,</if>
<if test="newFlag != null">new_flag,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="clientId != null and clientId != ''">#{clientId},</if>
@@ -84,6 +87,7 @@
<if test="createBy != null">#{createBy},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="bindIp != null">#{bindIp},</if>
<if test="newFlag != null">#{newFlag},</if>
</trim>
</insert>
@@ -105,9 +109,21 @@
<if test="createBy != null">create_by = #{createBy},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="bindIp != null">bind_ip = #{bindIp},</if>
<if test="newFlag != null">new_flag = #{newFlag},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteRmNetworkInterfaceById" parameterType="Long">
delete from rm_network_interface where id = #{id}
</delete>
<delete id="deleteRmNetworkInterfaceByIds" parameterType="String">
delete from rm_network_interface where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<update id="updateRmNetworkInterfaceByMac" parameterType="RmNetworkInterface">
update rm_network_interface
<trim prefix="SET" suffixOverrides=",">
@@ -125,18 +141,8 @@
<if test="createBy != null">create_by = #{createBy},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="bindIp != null">bind_ip = #{bindIp},</if>
<if test="newFlag != null">new_flag = #{newFlag},</if>
</trim>
where mac_address = #{macAddress}
</update>
<delete id="deleteRmNetworkInterfaceById" parameterType="Long">
delete from rm_network_interface where id = #{id}
</delete>
<delete id="deleteRmNetworkInterfaceByIds" parameterType="String">
delete from rm_network_interface where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.rocketmq.mapper.RmResourceRemoteMapper">
<resultMap type="RmResourceRemote" id="RmResourceRemoteResult">
<result property="id" column="id" />
<result property="hardwareSn" column="hardware_sn" />
@@ -19,15 +19,18 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="updateTime" column="update_time" />
<result property="createBy" column="create_by" />
<result property="updateBy" column="update_by" />
<result property="clientId" column="client_id" />
<result property="resultFlag" column="result_flag" />
<result property="scriptId" column="script_id" />
</resultMap>
<sql id="selectRmResourceRemoteVo">
select id, hardware_sn, resource_type, resource_name, description, internal_ip, external_ip, management_port, online_status, connection_method, create_time, update_time, create_by, update_by from rm_resource_remote
select id, hardware_sn, resource_type, resource_name, description, internal_ip, external_ip, management_port, online_status, connection_method, create_time, update_time, create_by, update_by, client_id, result_flag, script_id from rm_resource_remote
</sql>
<select id="selectRmResourceRemoteList" parameterType="RmResourceRemote" resultMap="RmResourceRemoteResult">
<include refid="selectRmResourceRemoteVo"/>
<where>
<where>
<if test="hardwareSn != null and hardwareSn != ''"> and hardware_sn = #{hardwareSn}</if>
<if test="resourceType != null and resourceType != ''"> and resource_type = #{resourceType}</if>
<if test="resourceName != null and resourceName != ''"> and resource_name like concat('%', #{resourceName}, '%')</if>
@@ -37,16 +40,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="managementPort != null "> and management_port = #{managementPort}</if>
<if test="onlineStatus != null and onlineStatus != ''"> and online_status = #{onlineStatus}</if>
<if test="connectionMethod != null and connectionMethod != ''"> and connection_method = #{connectionMethod}</if>
<if test="clientId != null and clientId != ''"> and client_id = #{clientId}</if>
<if test="resultFlag != null "> and result_flag = #{resultFlag}</if>
<if test="scriptId != null "> and script_id = #{scriptId}</if>
</where>
</select>
<select id="selectRmResourceRemoteById" parameterType="Long" resultMap="RmResourceRemoteResult">
<include refid="selectRmResourceRemoteVo"/>
where id = #{id}
</select>
<insert id="insertRmResourceRemote" parameterType="RmResourceRemote" useGeneratedKeys="true" keyProperty="id">
insert IGNORE into rm_resource_remote
insert into rm_resource_remote
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="hardwareSn != null and hardwareSn != ''">hardware_sn,</if>
<if test="resourceType != null and resourceType != ''">resource_type,</if>
@@ -61,7 +67,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="updateTime != null">update_time,</if>
<if test="createBy != null">create_by,</if>
<if test="updateBy != null">update_by,</if>
</trim>
<if test="clientId != null">client_id,</if>
<if test="resultFlag != null">result_flag,</if>
<if test="scriptId != null">script_id,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="hardwareSn != null and hardwareSn != ''">#{hardwareSn},</if>
<if test="resourceType != null and resourceType != ''">#{resourceType},</if>
@@ -76,7 +85,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="updateTime != null">#{updateTime},</if>
<if test="createBy != null">#{createBy},</if>
<if test="updateBy != null">#{updateBy},</if>
</trim>
<if test="clientId != null">#{clientId},</if>
<if test="resultFlag != null">#{resultFlag},</if>
<if test="scriptId != null">#{scriptId},</if>
</trim>
</insert>
<update id="updateRmResourceRemote" parameterType="RmResourceRemote">
@@ -95,6 +107,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<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="clientId != null">client_id = #{clientId},</if>
<if test="resultFlag != null">result_flag = #{resultFlag},</if>
<if test="scriptId != null">script_id = #{scriptId},</if>
</trim>
where id = #{id}
</update>
@@ -104,7 +119,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</delete>
<delete id="deleteRmResourceRemoteByIds" parameterType="String">
delete from rm_resource_remote where id in
delete from rm_resource_remote where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>