mtrAgent更新优化

This commit is contained in:
gaoyutao
2025-12-08 19:21:01 +08:00
parent 8ae81db4e8
commit 2781671c78
6 changed files with 91 additions and 33 deletions
@@ -216,10 +216,6 @@ public class AgentEndpoint {
String md5 = AgentUtil.getFileMD5(properties.getTempPath()+"/"+fileName);
if(StringUtils.isNotBlank(md5) && StringUtils.isNotBlank(versionUpdateEO.getFileMd5())
&& StringUtils.equals(md5,versionUpdateEO.getFileMd5())){
//更改全局变量
GlobalConfig.isCollect = false;
//调用采集任务
agentService.cancelCollect();
//所有文件下载完成,执行脚本命令
agentService.command(policy, clientId,MsgEnum.Agent版本更新应答.getValue());
}else{
@@ -94,6 +94,8 @@ public class AppInitializer implements CommandLineRunner {
agentService.checkMonitor();
AssertLog.info("检测mtr探测策略配置");
agentService.checkMtrDelect();
AssertLog.info("检测agent更新配置");
agentService.checkAgentUpdate();
}else{
//未注册,发送注册
try {
@@ -139,6 +139,7 @@ public class BusinessTasks {
object.put("clientId", GlobalConfig.CLIENT_ID);
object.put("logicalNode", agentService.getLogicalNode());
object.put("sn", GlobalConfig.DEVICE_SN);
object.put("cpucores", Runtime.getRuntime().availableProcessors());
object.put("strength","31");
object.put("name", properties.getName());
object.put("version", properties.getVersion());
@@ -855,6 +856,8 @@ public class BusinessTasks {
AssertLog.info("检测mtr探测策略配置");
agentService.checkMtrDelect();
AssertLog.info("检测agent更新配置");
agentService.checkAgentUpdate();
}else{
//未注册,发送注册
try {
@@ -2,12 +2,15 @@ package com.tongran.agent.client.scheduler.task;
import com.alibaba.fastjson2.JSONObject;
import com.tongran.agent.client.core.config.ApplicationProperties;
import com.tongran.agent.client.core.config.GlobalConfig;
import com.tongran.agent.client.core.enums.MsgEnum;
import com.tongran.agent.client.core.session.SessionManager;
import com.tongran.agent.client.netty.model.Message;
import com.tongran.agent.client.scheduler.service.AsyncCommandExecutor;
import com.tongran.agent.client.service.AgentService;
import com.tongran.agent.client.utils.AgentDataUtil;
import com.tongran.agent.client.utils.AgentUtil;
import com.tongran.agent.client.utils.AssertLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@@ -30,6 +33,8 @@ public class SpecificTimeTaskService {
@Resource
private SpecificTimeTaskConfig taskConfig;
@Resource
private AgentService agentService;
public SpecificTimeTaskService() {
this.sessionManager = SessionManager.getInstance();
@@ -81,6 +86,11 @@ public class SpecificTimeTaskService {
String key = request.getTaskName()+"-"+System.currentTimeMillis();
if(StringUtils.equals(request.getDataType(), MsgEnum.Agent版本更新应答.getValue())){
try {
AssertLog.info("定时更新启动...暂停心跳和采集任务");
//更改全局变量
GlobalConfig.isCollect = false;
//调用采集任务
agentService.cancelCollect();
//设置检查回滚任务
String SCRIPT_PATH = properties.getScriptPath()+"/rollback-tragent.sh";
AgentDataUtil.chmod(SCRIPT_PATH,"775");
@@ -113,7 +123,7 @@ public class SpecificTimeTaskService {
json.put("resMsg", "");
json.put("result", jsonObject.toJSONString());
json.put("timestamp",timestamps);
Message message = Message.builder().clientId(request.getClientId()).dataType(request.getDataType()).data(jsonObject.toJSONString()).build();
Message message = Message.builder().clientId(request.getClientId()).dataType(request.getDataType()).data(json.toJSONString()).build();
if (Objects.nonNull(sessionManager.getSessionById(request.getClientId()))) {
System.out.println("发送执行结果: " + json.toJSONString()); // 注意:toJSONString()
sessionManager.writeAndFlush(sessionManager.getSessionById(request.getClientId()).getChannel(), message);
@@ -32,4 +32,6 @@ public interface AgentService {
void checkMonitor();
void checkMtrDelect();
void checkAgentUpdate();
}
@@ -171,35 +171,25 @@ public class AgentServiceImpl implements AgentService {
.clientId(clientId)
.dataType(dataType)
.build();
try {
taskService.createSpecificTimeTask(request);
JSONObject json = new JSONObject();
json.put("resCode",1);
json.put("resMsg", "执行脚本策略定时任务保存成功");
json.put("timestamp",timestamp);
// 判定客户端与服务端是否连接
if (Objects.nonNull(sessionManager.getSessionById(GlobalConfig.CLIENT_ID))) {
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.执行脚本策略应答.getValue())
.data(json.toString()).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送执行脚本策略应答={}",JSON.toJSONString(message));
}
} catch (Exception e) {
JSONObject json = new JSONObject();
json.put("resCode",0);
json.put("resMsg", "执行脚本策略定时任务保存失败");
json.put("timestamp",timestamp);
// 判定客户端与服务端是否连接
if (Objects.nonNull(sessionManager.getSessionById(GlobalConfig.CLIENT_ID))) {
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.执行脚本策略应答.getValue())
.data(json.toString()).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送执行脚本策略应答={}",JSON.toJSONString(message));
if(StringUtils.equals(dataType, MsgEnum.Agent版本更新应答.getValue())){
// 将request对象转换为JSON字符串
String jsonContent = "updateMsg=" + JSONObject.toJSONString(request);
// 检查并创建外置目录
if (AdvancedAsyncDownloader.createSingleDirectoryIfNotExists(properties.getConfPath())) {
// 将JSON字符串写入文件
AgentUtil.bufferedWriter(properties.getConfPath() + "/agentupdate.conf",
new String[]{jsonContent});
}
}
processCommand(request, timestamp);
}else{
if(StringUtils.equals(dataType, MsgEnum.Agent版本更新应答.getValue())){
try {
AssertLog.info("agent更新启动...暂停心跳和采集任务");
//更改全局变量
GlobalConfig.isCollect = false;
//调用采集任务
cancelCollect();
//设置检查回滚任务
String SCRIPT_PATH = properties.getScriptPath()+"/rollback-tragent.sh";
AgentDataUtil.chmod(SCRIPT_PATH,"775");
@@ -269,6 +259,39 @@ public class AgentServiceImpl implements AgentService {
}
}
}
/**
* 辅助方法:持久化定时更新
* @param request
* @param timestamp
*/
public void processCommand(SpecificTimeRequest request, long timestamp){
try {
taskService.createSpecificTimeTask(request);
JSONObject json = new JSONObject();
json.put("resCode",1);
json.put("resMsg", "执行脚本策略定时任务保存成功");
json.put("timestamp",timestamp);
// 判定客户端与服务端是否连接
if (Objects.nonNull(sessionManager.getSessionById(GlobalConfig.CLIENT_ID))) {
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.执行脚本策略应答.getValue())
.data(json.toString()).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送执行脚本策略应答={}",JSON.toJSONString(message));
}
} catch (Exception e) {
JSONObject json = new JSONObject();
json.put("resCode",0);
json.put("resMsg", "执行脚本策略定时任务保存失败");
json.put("timestamp",timestamp);
// 判定客户端与服务端是否连接
if (Objects.nonNull(sessionManager.getSessionById(GlobalConfig.CLIENT_ID))) {
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.执行脚本策略应答.getValue())
.data(json.toString()).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送执行脚本策略应答={}",JSON.toJSONString(message));
}
}
}
@Override
public void cancelTask(String taskId) {
@@ -290,6 +313,8 @@ public class AgentServiceImpl implements AgentService {
checkMonitor();
AssertLog.info("检测mtr探测策略配置");
checkMtrDelect();
AssertLog.info("检测agent更新配置");
checkAgentUpdate();
}
@Override
@@ -742,10 +767,6 @@ public class AgentServiceImpl implements AgentService {
String md5 = AgentUtil.getFileMD5(properties.getTempPath()+"/"+fileName);
if(StringUtils.isNotBlank(md5) && StringUtils.isNotBlank(versionUpdateEO.getFileMd5())
&& StringUtils.equals(md5,versionUpdateEO.getFileMd5())){
//更改全局变量
GlobalConfig.isCollect = false;
//关闭采集任务
cancelCollect();
//所有文件下载完成,执行脚本命令
AssertLog.info("版本策略MD5验证成功");
command(policy, GlobalConfig.CLIENT_ID,MsgEnum.Agent版本更新应答.getValue());
@@ -1563,4 +1584,28 @@ public class AgentServiceImpl implements AgentService {
public void caseTypeBySystem(String type, int interval, boolean collect){
}
@Override
public void checkAgentUpdate() {
File agentUpdateFile = new File(properties.getConfPath() + "/agentupdate.conf");
if (agentUpdateFile.exists()) {
Properties props = new Properties();
try (InputStream input = Files.newInputStream(agentUpdateFile.toPath())) {
props.load(input);
String updateMsg = props.getProperty("updateMsg");
if (StringUtils.isNotBlank(updateMsg)) {
long timestamp = System.currentTimeMillis();
timestamp = Math.round(timestamp / 1000.0);
SpecificTimeRequest request = JSON.parseObject(updateMsg, SpecificTimeRequest.class);
processCommand(request, timestamp);
AssertLog.info("应用启动时加载agent更新配置成功: {}", updateMsg);
}
} catch (Exception e) {
AssertLog.error("应用启动时加载agent更新配置异常", e);
}
} else {
AssertLog.debug("agent更新配置文件不存在: {}", agentUpdateFile.getPath());
}
}
}