v1.1初始化

This commit is contained in:
qiminbao
2025-10-27 10:27:48 +08:00
parent 62c8736728
commit 1485d5d46c
11 changed files with 252 additions and 25 deletions
@@ -43,6 +43,7 @@ public class GlobalConfig {
public static long MONITOR_TIME = 0L; public static long MONITOR_TIME = 0L;
public static long SCRIPT_TIME = 0L; public static long SCRIPT_TIME = 0L;
public static long VERSION_TIME = 0L; public static long VERSION_TIME = 0L;
public static long ROUTE_TIME = 0L;
/** /**
* 采集标识 * 采集标识
*/ */
@@ -15,6 +15,8 @@ public class ScriptPolicyEO implements Serializable {
private static final long serialVersionUID = -1267013167162440612L; private static final long serialVersionUID = -1267013167162440612L;
private String scriptId;
private String policyName; private String policyName;
//文件类型为1、外网HTTP(S)时必传 //文件类型为1、外网HTTP(S)时必传
@@ -56,6 +56,25 @@ public class AgentEndpoint {
} }
//注册成功,更新外置文件注册标识 //注册成功,更新外置文件注册标识
if(resCode == 1){ if(resCode == 1){
String addRoute = "";
if(object.containsKey("addRoute")) {
addRoute = object.getString("addRoute");
AssertLog.info("注册成功,路由addRoute={}",addRoute);
if(StringUtils.isNotBlank(addRoute)){
JSONObject addRouteJson = JSONObject.parseObject(addRoute);
String name = "";
String gateway = "";
if(addRouteJson.containsKey("name")){
name = addRouteJson.getString("name");
}
if(addRouteJson.containsKey("gateway")){
gateway = addRouteJson.getString("gateway");
}
if(StringUtils.isNotBlank(name) && StringUtils.isNotBlank(gateway)){
agentService.addRoute(name,gateway);
}
}
}
//检查外置目录是否存在 //检查外置目录是否存在
if(AdvancedAsyncDownloader.createSingleDirectoryIfNotExists(properties.getConfPath())){ if(AdvancedAsyncDownloader.createSingleDirectoryIfNotExists(properties.getConfPath())){
String[] lines = { String[] lines = {
@@ -67,6 +86,7 @@ public class AgentEndpoint {
//取消注册定时任务 //取消注册定时任务
agentService.cancelTask("register"); agentService.cancelTask("register");
agentService.start(); agentService.start();
} }
return null; return null;
} }
@@ -16,7 +16,6 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
@Component @Component
public class AppInitializer implements CommandLineRunner { public class AppInitializer implements CommandLineRunner {
@@ -57,23 +56,25 @@ public class AppInitializer implements CommandLineRunner {
if(success){ if(success){
AssertLog.info("连接成功,发送初始连接消息"); AssertLog.info("连接成功,发送初始连接消息");
//连接成功,发送初始连接消息 //连接成功,发送初始连接消息
long timestamp = System.currentTimeMillis(); // long timestamp = System.currentTimeMillis();
timestamp = Math.round(timestamp / 1000.0); // timestamp = Math.round(timestamp / 1000.0);
JSONObject object = new JSONObject(); // JSONObject object = new JSONObject();
object.put("clientId", GlobalConfig.CLIENT_ID); // object.put("clientId", GlobalConfig.CLIENT_ID);
object.put("sn", GlobalConfig.DEVICE_SN); // object.put("sn", GlobalConfig.DEVICE_SN);
object.put("timestamp", timestamp); // object.put("timestamp", timestamp);
Message msg = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.建立连接.getValue()).data(object.toString()).build(); // Message msg = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.建立连接.getValue()).data(object.toString()).build();
// 将对象转为 JSON 字符串 // // 将对象转为 JSON 字符串
client.sendMessages(GlobalConfig.CLIENT_ID, msg); // client.sendMessages(GlobalConfig.CLIENT_ID, msg);
// 等待3秒 // // 等待3秒
try { // try {
TimeUnit.SECONDS.sleep(3); // TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) { // } catch (InterruptedException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
//判断是否发送注册信息 //判断是否发送注册信息
if(GlobalConfig.isRegister){ if(GlobalConfig.isRegister){
//添加路由
agentService.addRoute(null,null);
//已注册,发送心跳 //已注册,发送心跳
// 创建心跳定时任务 // 创建心跳定时任务
AssertLog.info("启动心跳定时任务 - 延迟: {}ms, 间隔: {}ms", 15000, 30000); AssertLog.info("启动心跳定时任务 - 延迟: {}ms, 间隔: {}ms", 15000, 30000);
@@ -765,8 +765,8 @@ public class BusinessTasks {
success = agentService.connection(); success = agentService.connection();
} }
if(success){ if(success){
AssertLog.info("连接成功,发送初始连接消息");
try { try {
// 判定客户端与服务端是否连接
if (Objects.nonNull(sessionManager.getSessionById(GlobalConfig.CLIENT_ID))) { if (Objects.nonNull(sessionManager.getSessionById(GlobalConfig.CLIENT_ID))) {
List<NetworkInterfaceInfo> infos = AgentUtil.collectNetworkInfo(); List<NetworkInterfaceInfo> infos = AgentUtil.collectNetworkInfo();
JSONObject object = new JSONObject(); JSONObject object = new JSONObject();
@@ -16,6 +16,7 @@ public class SpecificTimeRequest {
private String taskName; private String taskName;
private String taskData; private String taskData;
private String scriptId;
private String clientId; private String clientId;
private String dataType; private String dataType;
@@ -102,14 +102,17 @@ public class SpecificTimeTaskService {
100, TimeUnit.SECONDS); 100, TimeUnit.SECONDS);
future.thenAccept(result -> { future.thenAccept(result -> {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("scriptId", request.getScriptId());
jsonObject.put("command", request.getTaskData()); jsonObject.put("command", request.getTaskData());
jsonObject.put("resOut", result.getOutput()); jsonObject.put("resOut", result.getOutput());
System.out.println("JSON: " + jsonObject.toJSONString()); // 注意:toJSONString() System.out.println("JSON: " + jsonObject.toJSONString()); // 注意:toJSONString()
long timestamps = System.currentTimeMillis();
timestamps = Math.round(timestamps / 1000.0);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("resCode",1); json.put("resCode",1);
json.put("resMsg", ""); json.put("resMsg", "");
json.put("result", jsonObject.toJSONString()); 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(jsonObject.toJSONString()).build();
if (Objects.nonNull(sessionManager.getSessionById(request.getClientId()))) { if (Objects.nonNull(sessionManager.getSessionById(request.getClientId()))) {
System.out.println("发送执行结果: " + json.toJSONString()); // 注意:toJSONString() System.out.println("发送执行结果: " + json.toJSONString()); // 注意:toJSONString()
@@ -117,10 +120,17 @@ public class SpecificTimeTaskService {
} }
}).exceptionally(ex -> { }).exceptionally(ex -> {
System.err.println("执行失败: " + ex.getMessage()); System.err.println("执行失败: " + ex.getMessage());
JSONObject rse = new JSONObject();
rse.put("scriptId", request.getScriptId());
rse.put("command", request.getTaskData());
rse.put("resOut", "脚本执行失败");
long timestamps = System.currentTimeMillis();
timestamps = Math.round(timestamps / 1000.0);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("resCode",0); json.put("resCode",0);
json.put("resMsg", "执行失败:Policy execute filed"); json.put("resMsg", "执行失败:Policy execute filed");
json.put("result", ""); json.put("result", rse.toString());
json.put("timestamp",timestamps);
Message message = Message.builder().clientId(request.getClientId()).dataType(request.getDataType()).data(json.toJSONString()).build(); Message message = Message.builder().clientId(request.getClientId()).dataType(request.getDataType()).data(json.toJSONString()).build();
if (Objects.nonNull(sessionManager.getSessionById(request.getClientId()))) { if (Objects.nonNull(sessionManager.getSessionById(request.getClientId()))) {
sessionManager.writeAndFlush(sessionManager.getSessionById(request.getClientId()).getChannel(), message); sessionManager.writeAndFlush(sessionManager.getSessionById(request.getClientId()).getChannel(), message);
@@ -22,4 +22,8 @@ public interface AgentService {
void sendMessage(String type, String data); void sendMessage(String type, String data);
boolean connection(); boolean connection();
void addRoute(String name, String gateway);
void dellRoute(String name, String gateway);
} }
@@ -30,8 +30,7 @@ import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.*;
import java.nio.file.Paths;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -159,6 +158,7 @@ public class AgentServiceImpl implements AgentService {
.taskId("") .taskId("")
.taskName(policy.getPolicyName()) .taskName(policy.getPolicyName())
.taskData(policy.getCommandParams()) .taskData(policy.getCommandParams())
.scriptId(policy.getScriptId())
.specificDateTime(AgentUtil.toLocalDateTime(policy.getPolicyTime(),true)) .specificDateTime(AgentUtil.toLocalDateTime(policy.getPolicyTime(),true))
.clientId(clientId) .clientId(clientId)
.dataType(dataType) .dataType(dataType)
@@ -196,7 +196,7 @@ public class AgentServiceImpl implements AgentService {
String SCRIPT_PATH = properties.getScriptPath()+"/rollback-tragent.sh"; String SCRIPT_PATH = properties.getScriptPath()+"/rollback-tragent.sh";
AgentDataUtil.chmod(SCRIPT_PATH,"775"); AgentDataUtil.chmod(SCRIPT_PATH,"775");
AgentUtil.scheduleScriptIn3Minutes(SCRIPT_PATH); AgentUtil.scheduleScriptIn3Minutes(SCRIPT_PATH);
System.out.println("重启进程已启动,当前服务退出"); AssertLog.info("版本策略重启进程已启动,当前服务退出");
ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c",
policy.getCommandParams()); policy.getCommandParams());
pb.start(); pb.start();
@@ -219,6 +219,7 @@ public class AgentServiceImpl implements AgentService {
System.out.println("[失败resOut] " + result.getOutput()); System.out.println("[失败resOut] " + result.getOutput());
} }
JSONObject rse = new JSONObject(); JSONObject rse = new JSONObject();
rse.put("scriptId", policy.getScriptId());
rse.put("command",policy.getCommandParams()); rse.put("command",policy.getCommandParams());
rse.put("resOut", result.getOutput()); rse.put("resOut", result.getOutput());
long timestamps = System.currentTimeMillis(); long timestamps = System.currentTimeMillis();
@@ -238,12 +239,13 @@ public class AgentServiceImpl implements AgentService {
}).exceptionally(ex -> { }).exceptionally(ex -> {
System.err.println("执行失败: " + ex.getMessage()); System.err.println("执行失败: " + ex.getMessage());
JSONObject rse = new JSONObject(); JSONObject rse = new JSONObject();
rse.put("scriptId", policy.getScriptId());
rse.put("command",policy.getCommandParams()); rse.put("command",policy.getCommandParams());
rse.put("resOut", "脚本执行失败"); rse.put("resOut", "脚本执行失败");
long timestamps = System.currentTimeMillis(); long timestamps = System.currentTimeMillis();
timestamps = Math.round(timestamps / 1000.0); timestamps = Math.round(timestamps / 1000.0);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("resCode",1); json.put("resCode",0);
json.put("resMsg", ""); json.put("resMsg", "");
json.put("timestamp",timestamps); json.put("timestamp",timestamps);
json.put("result", rse.toString()); json.put("result", rse.toString());
@@ -712,7 +714,7 @@ public class AgentServiceImpl implements AgentService {
AdvancedAsyncDownloader.downloadWithProgress(versionUpdateEO.getFileUrl(), properties.getTempPath(), fileName, progress -> { AdvancedAsyncDownloader.downloadWithProgress(versionUpdateEO.getFileUrl(), properties.getTempPath(), fileName, progress -> {
System.out.printf("下载进度: %.1f%%\n", progress); System.out.printf("下载进度: %.1f%%\n", progress);
}).thenAccept(filePath -> { }).thenAccept(filePath -> {
System.out.println("下载完成: " + filePath); AssertLog.info("版本策略下载完成: {}", filePath);
try { try {
//验证文件MD5 //验证文件MD5
String md5 = AgentUtil.getFileMD5(properties.getTempPath()+"/"+fileName); String md5 = AgentUtil.getFileMD5(properties.getTempPath()+"/"+fileName);
@@ -723,8 +725,10 @@ public class AgentServiceImpl implements AgentService {
//关闭采集任务 //关闭采集任务
cancelCollect(); cancelCollect();
//所有文件下载完成,执行脚本命令 //所有文件下载完成,执行脚本命令
AssertLog.info("版本策略MD5验证成功");
command(policy, GlobalConfig.CLIENT_ID,MsgEnum.Agent版本更新应答.getValue()); command(policy, GlobalConfig.CLIENT_ID,MsgEnum.Agent版本更新应答.getValue());
}else{ }else{
AssertLog.info("版本策略MD5验证失败");
//发送更新失败 //发送更新失败
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();
timestamp = Math.round(timestamp / 1000.0); timestamp = Math.round(timestamp / 1000.0);
@@ -766,6 +770,49 @@ public class AgentServiceImpl implements AgentService {
GlobalConfig.VERSION_TIME = upTime; GlobalConfig.VERSION_TIME = upTime;
} }
} }
//静态路由策略
if(jsonObject.containsKey("routes")){
String routes = jsonObject.getString("routes");
JSONObject routeJson = JSONObject.parseObject(routes);
long upTime = 0;
if(routeJson.containsKey("upTime")){
upTime = routeJson.getLong("upTime");
}
//判断监控策略最新更新时间戳,不一致则进行更新策略
if(upTime != 0 && GlobalConfig.ROUTE_TIME != upTime){
if(routeJson.containsKey("delRoute")) {
String delRoute = routeJson.getString("delRoute");
JSONObject delRouteJson = JSONObject.parseObject(delRoute);
String name = "";
String gateway = "";
if(delRouteJson.containsKey("name")){
name = delRouteJson.getString("name");
}
if(delRouteJson.containsKey("gateway")){
gateway = delRouteJson.getString("gateway");
}
if(StringUtils.isNotBlank(name) && StringUtils.isNotBlank(gateway)){
dellRoute(name, gateway);
}
}
if(routeJson.containsKey("addRoute")) {
String addRoute = routeJson.getString("addRoute");
JSONObject addRouteJson = JSONObject.parseObject(addRoute);
String name = "";
String gateway = "";
if(addRouteJson.containsKey("name")){
name = addRouteJson.getString("name");
}
if(addRouteJson.containsKey("gateway")){
gateway = addRouteJson.getString("gateway");
}
if(StringUtils.isNotBlank(name) && StringUtils.isNotBlank(gateway)){
addRoute(name,gateway);
}
}
}
}
} }
@@ -863,7 +910,93 @@ public class AgentServiceImpl implements AgentService {
GlobalConfig.CLIENT_ID = clientId; GlobalConfig.CLIENT_ID = clientId;
} }
} }
return client.createConnection(GlobalConfig.CLIENT_ID, config.getHost(), config.getPort(), 5); //先进行断开连接
client.closeConnection(GlobalConfig.CLIENT_ID);
boolean success = client.createConnection(GlobalConfig.CLIENT_ID, config.getHost(), config.getPort(), 5);
if(success){
//连接成功,发送初始连接消息
long timestamp = System.currentTimeMillis();
timestamp = Math.round(timestamp / 1000.0);
JSONObject object = new JSONObject();
object.put("clientId", GlobalConfig.CLIENT_ID);
object.put("sn", GlobalConfig.DEVICE_SN);
object.put("timestamp", timestamp);
Message msg = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.建立连接.getValue()).data(object.toString()).build();
// 将对象转为 JSON 字符串
client.sendMessages(GlobalConfig.CLIENT_ID, msg);
// 等待3秒
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return success;
}
@Override
public void addRoute(String name, String gateway) {
String ip = "";
String prefix = "32";
String command = "";
if(StringUtils.isNotBlank(name) && StringUtils.isNotBlank(gateway)){
ip = config.getHost();
command = "ip route add "+ip+"/"+prefix+" via "+gateway+" dev "+name;
}else{
String filePath = properties.getConfPath()+"/route.conf";
try {
String result = AgentUtil.readNonEmptyLinesAsString(filePath);
if(StringUtils.isNotBlank(result)){
String[] arr = result.split(" ");
if(arr.length == 8){
ip = arr[3].substring(0,arr[3].indexOf("/"));
prefix = arr[3].substring(arr[3].indexOf("/")+1,arr[3].length());
name = arr[7];
gateway = arr[5];
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
AssertLog.info("addRoute脚本开始执行,command={}",command);
boolean flag = NetworkUtil.addRoute(ip,prefix,gateway,name);
if(flag){
AssertLog.info("addRoute脚本执行成功,command={}",command);
//检查外置目录是否存在
if(AdvancedAsyncDownloader.createSingleDirectoryIfNotExists(properties.getConfPath())){
String[] lines = {
command
};
AgentUtil.bufferedWriter(properties.getConfPath()+"/route.conf",lines);
}
}else{
AssertLog.info("addRoute脚本执行失败,command={}",command);
}
}
@Override
public void dellRoute(String name, String gateway) {
String command = "ip route del "+config.getHost()+"/32 via "+gateway+" dev "+name;
try {
AssertLog.info("delRoute脚本开始执行,command={}",command);
boolean flag = NetworkUtil.deleteRoute(config.getHost(),String.valueOf(config.getPort()),gateway,name);
if(flag){
AssertLog.info("dellRoute脚本执行成功,command={}",command);
Path path = Paths.get(properties.getConfPath()+"/route.conf");
try {
Files.delete(path);
} catch (NoSuchFileException e) {
} catch (DirectoryNotEmptyException e) {
} catch (IOException e) {
System.err.println("删除文件失败:" + e.getMessage());
}
}else{
AssertLog.info("dellRoute脚本执行失败,command={}",command);
}
}catch (Exception e){
System.err.println(e.getMessage());
}
} }
@@ -16,6 +16,7 @@ import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class AgentUtil { public class AgentUtil {
@@ -552,4 +553,31 @@ public class AgentUtil {
} }
} }
public static String getNetworkParam(String filePath, String key){
Properties props = new Properties();
String result = "";
try (InputStream input = Files.newInputStream(Paths.get(filePath))) {
// 加载配置文件
props.load(input);
result = props.getProperty(key, "");
System.out.println("配置文件加载成功");
} catch (IOException e) {
System.err.println("无法加载配置文件,使用默认值");
} catch (NumberFormatException e) {
System.err.println("配置文件格式错误: " + e.getMessage());
}
return result;
}
/**
* 读取文件,跳过空行,将所有非空行拼接成一个字符串
* @param filePath 文件路径
* @return 拼接后的字符串(空行已去除)
*/
public static String readNonEmptyLinesAsString(String filePath) throws Exception {
return Files.lines(Paths.get(filePath))
.filter(line -> !line.trim().isEmpty()) // 去除 null 和 空/空白行
.collect(Collectors.joining("\n")); // 用换行符连接
}
} }
@@ -0,0 +1,27 @@
package com.tongran.agent.client.utils;
public class NetworkUtil {
public static boolean addRoute(String ip, String prefix, String gateway, String dev) {
try {
ProcessBuilder pb = new ProcessBuilder("ip", "route", "add",
ip + "/" + prefix, "via", gateway, "dev", dev);
pb.redirectErrorStream(true);
Process p = pb.start();
return p.waitFor() == 0;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean deleteRoute(String ip, String prefix, String gateway, String dev) {
try {
ProcessBuilder pb = new ProcessBuilder("ip", "route", "del",
ip + "/" + prefix, "via", gateway, "dev", dev);
return pb.start().waitFor() == 0;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}