告警、脚本策略执行测试优化
This commit is contained in:
@@ -11,7 +11,6 @@ import com.tongran.agent.client.core.eo.AlarmEO;
|
||||
import com.tongran.agent.client.core.eo.ScriptPolicyEO;
|
||||
import com.tongran.agent.client.netty.annotation.AgentDispatcher;
|
||||
import com.tongran.agent.client.netty.basics.AgentHandler;
|
||||
import com.tongran.agent.client.netty.model.Message;
|
||||
import com.tongran.agent.client.netty.model.UpMsgResponse;
|
||||
import com.tongran.agent.client.scheduler.service.AdvancedAsyncDownloader;
|
||||
import com.tongran.agent.client.scheduler.service.AppInitializer;
|
||||
@@ -22,6 +21,7 @@ import com.tongran.agent.client.scheduler.task.SpecificTimeTaskService;
|
||||
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.Component;
|
||||
|
||||
@@ -203,8 +203,11 @@ public class AgentEndpoint {
|
||||
JSONObject jsonObject = JSONObject.parseObject(data);
|
||||
if(jsonObject.containsKey("alarms")){
|
||||
String alarms = jsonObject.getString("alarms");
|
||||
AssertLog.info("告警设置,alarms={}", alarms);
|
||||
GlobalConfig.ALARM_LIST = JSON.parseObject(alarms, new TypeReference<List<AlarmEO>>() {});
|
||||
AssertLog.info("告警设置,监控项={}", JSON.toJSONString(GlobalConfig.ALARM_LIST));
|
||||
if(CollectionUtil.isNotEmpty(GlobalConfig.ALARM_LIST)){
|
||||
AssertLog.info("告警设置,is_alarm={}", AgentDataUtil.hasAnyActiveAlarm(GlobalConfig.ALARM_LIST));
|
||||
GlobalConfig.IS_ALARM = AgentDataUtil.hasAnyActiveAlarm(GlobalConfig.ALARM_LIST);
|
||||
}
|
||||
}
|
||||
@@ -225,12 +228,36 @@ public class AgentEndpoint {
|
||||
public class ScriptPolicyHandler implements AgentHandler {
|
||||
@Override
|
||||
public UpMsgResponse upHandle(String data, String clientId) {
|
||||
ScriptPolicyEO policy = JSON.parseObject(data, ScriptPolicyEO.class);
|
||||
JSONObject jsonObject = JSONObject.parseObject(data);
|
||||
ScriptPolicyEO policy = ScriptPolicyEO.builder().build();
|
||||
if(jsonObject.containsKey("policyName")) {
|
||||
policy.setPolicyName(jsonObject.getString("policyName"));
|
||||
}
|
||||
if(jsonObject.containsKey("files")) {
|
||||
String files = jsonObject.getString("files");
|
||||
List<ScriptPolicyEO.ScriptFile> fileList = JSON.parseObject(files, new TypeReference<List<ScriptPolicyEO.ScriptFile>>() {});
|
||||
policy.setFiles(fileList);
|
||||
}
|
||||
if(jsonObject.containsKey("filePath")) {
|
||||
policy.setFilePath(jsonObject.getString("filePath"));
|
||||
}
|
||||
if(jsonObject.containsKey("commands")) {
|
||||
String commands = jsonObject.getString("commands");
|
||||
List<String> commandList = JSON.parseObject(commands, new TypeReference<List<String>>() {});
|
||||
policy.setCommands(commandList);
|
||||
}
|
||||
if(jsonObject.containsKey("method")) {
|
||||
policy.setMethod(jsonObject.getInteger("method"));
|
||||
}
|
||||
if(jsonObject.containsKey("policyTime")) {
|
||||
policy.setPolicyTime(jsonObject.getLong("policyTime"));
|
||||
}
|
||||
boolean isFalse = false;
|
||||
if(StringUtils.isNotBlank(policy.getFilePath())){
|
||||
if(AdvancedAsyncDownloader.createSingleDirectoryIfNotExists(policy.getFilePath())){
|
||||
if(CollectionUtil.isNotEmpty(policy.getFiles())){
|
||||
isFalse = true;
|
||||
GlobalConfig.DOWN_FILES.put(policy.getPolicyName(), policy.getFiles().size());
|
||||
for (ScriptPolicyEO.ScriptFile f : policy.getFiles()) {
|
||||
String finalSaveDir = policy.getFilePath();
|
||||
//文件类型:0、平台文件地址;1、外网HTTP(S)
|
||||
@@ -244,100 +271,75 @@ public class AgentEndpoint {
|
||||
AgentUtil.base64ToFile(f.getFileData(), filePath);
|
||||
System.out.println("文件保存成功: " + filePath);
|
||||
isFalse = true;
|
||||
try {
|
||||
AgentDataUtil.chmod(filePath,"+x");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
isFalse = false;
|
||||
System.err.println("文件保存失败: " + e.getMessage());
|
||||
}
|
||||
}else{
|
||||
Java8FileDownloader.DownloadResult result = Java8FileDownloader.downloadFile(f.getFileUrl(), finalSaveDir, f.getFileName());
|
||||
// 验证下载是否完成
|
||||
if (result.isSuccess()) {
|
||||
boolean isComplete = Java8FileDownloader.verifyFileCompletion(
|
||||
result.getFilePath(), result.getExpectedSize());
|
||||
if(isComplete){
|
||||
isFalse = true;
|
||||
System.out.println("下载完成: " + finalSaveDir+"/"+f.getFileName());
|
||||
}else{
|
||||
isFalse = false;
|
||||
System.err.println("下载失败");
|
||||
AdvancedAsyncDownloader.downloadWithProgress(f.getFileUrl(), finalSaveDir, f.getFileName(), progress -> {
|
||||
System.out.printf("下载进度: %.1f%%\n", progress);
|
||||
}).thenAccept(filePath -> {
|
||||
System.out.println("下载完成: " + filePath);
|
||||
try {
|
||||
AgentDataUtil.chmod(finalSaveDir+"/"+f.getFileName(),"775");
|
||||
int count = GlobalConfig.DOWN_FILES.get(policy.getPolicyName());
|
||||
if(count > 1){
|
||||
GlobalConfig.DOWN_FILES.put(policy.getPolicyName(), count--);
|
||||
}else{
|
||||
//所有文件下载完成,执行脚本命令
|
||||
agentService.command(policy, clientId);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}).exceptionally(ex -> {
|
||||
System.err.println("下载错误: " + ex.getMessage());
|
||||
return null;
|
||||
});
|
||||
// Java8FileDownloader.DownloadResult result = Java8FileDownloader.downloadFile(f.getFileUrl(), finalSaveDir, f.getFileName());
|
||||
// // 验证下载是否完成
|
||||
// if (result.isSuccess()) {
|
||||
// boolean isComplete = Java8FileDownloader.verifyFileCompletion(
|
||||
// result.getFilePath(), result.getExpectedSize());
|
||||
// if(isComplete){
|
||||
// isFalse = true;
|
||||
// System.out.println("下载完成: " + finalSaveDir+"/"+f.getFileName());
|
||||
// try {
|
||||
// AgentDataUtil.chmod(finalSaveDir+"/"+f.getFileName(),"775");
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }else{
|
||||
// isFalse = false;
|
||||
// System.err.println("下载失败");
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//判断文件是否保存成功
|
||||
if(isFalse){
|
||||
if(CollectionUtil.isNotEmpty(policy.getCommands())){
|
||||
//执行方式:0、立即执行;1、定时执行;
|
||||
if(policy.getMethod() == 1){
|
||||
SpecificTimeRequest request = SpecificTimeRequest.builder()
|
||||
.taskId("")
|
||||
.taskName("")
|
||||
.taskData(policy.getCommands())
|
||||
.specificDateTime(policy.getPolicyTime())
|
||||
.clientId(clientId)
|
||||
.build();
|
||||
try {
|
||||
taskService.createSpecificTimeTask(request);
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("resCode",1);
|
||||
json.put("resMsg", "执行脚本策略定时任务保存成功");
|
||||
Message message = Message.builder().clientId(clientId).dataType(MsgEnum.执行脚本策略应答.getValue()).data(json.toString()).build();
|
||||
return UpMsgResponse.builder().clientId(clientId).dataType(MsgEnum.执行脚本策略应答.getValue()).content(json.toString()).build();
|
||||
} catch (Exception e) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("resCode",0);
|
||||
json.put("resMsg", "执行脚本策略定时任务保存失败");
|
||||
Message message = Message.builder().clientId(clientId).dataType(MsgEnum.执行脚本策略应答.getValue()).data(json.toString()).build();
|
||||
return UpMsgResponse.builder().clientId(clientId).dataType(MsgEnum.执行脚本策略应答.getValue()).content(json.toString()).build();
|
||||
}
|
||||
}else{
|
||||
List<Map<String,String>> list = new ArrayList<>();
|
||||
for (String command : policy.getCommands()) {
|
||||
Map<String,String> map = new HashMap<>();
|
||||
List<String> cmd = Arrays.asList(command.split("\\s+"));
|
||||
CompletableFuture<AsyncCommandExecutor.CommandResult> future =
|
||||
AsyncCommandExecutor.executeCommandAsync(
|
||||
cmd,
|
||||
60, TimeUnit.SECONDS);
|
||||
future.thenAccept(result -> {
|
||||
if (result.isSuccess()) {
|
||||
System.out.println("脚本执行成功");
|
||||
System.out.println("[成功resOut] " + result.getOutput());
|
||||
map.put("command",command);
|
||||
map.put("resOut",result.getOutput());
|
||||
} else {
|
||||
System.out.println("脚本执行失败");
|
||||
System.out.println("[失败resOut] " + result.getOutput());
|
||||
map.put("command",command);
|
||||
map.put("resOut",result.getOutput());
|
||||
}
|
||||
}).exceptionally(ex -> {
|
||||
System.err.println("执行失败: " + ex.getMessage());
|
||||
map.put("command",command);
|
||||
map.put("resOut","Policy execute filed");
|
||||
return null;
|
||||
});
|
||||
list.add(map);
|
||||
}
|
||||
if(CollectionUtil.isNotEmpty(list)){
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("resCode",1);
|
||||
json.put("resMsg", "");
|
||||
json.put("result", AgentUtil.toJsonString(list));
|
||||
return UpMsgResponse.builder().clientId(clientId).dataType(MsgEnum.执行脚本策略应答.getValue()).content(json.toString()).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
//判断是否需要保存文件
|
||||
if(!isFalse){
|
||||
agentService.command(policy, clientId);
|
||||
return null;
|
||||
}else{
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("resCode",0);
|
||||
json.put("resMsg", "A执行脚本策略文件保存失败");
|
||||
json.put("resCode",1);
|
||||
json.put("resMsg", "A执行脚本策略文件保存中");
|
||||
return UpMsgResponse.builder().clientId(clientId).dataType(MsgEnum.执行脚本策略应答.getValue()).content(json.toString()).build();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -440,4 +442,7 @@ public class AgentEndpoint {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -109,9 +109,10 @@ public class AgentDecoderHandler extends ChannelInboundHandlerAdapter {
|
||||
JSONObject jsonObject = JSONObject.parseObject(message);
|
||||
String clientId = jsonObject.getString("clientId");
|
||||
String dataType = jsonObject.getString("dataType");
|
||||
String data = jsonObject.getString("data");
|
||||
AgentHandler msgHandler = agentDispatcherManager.getHandler(dataType + "&" + AgentDispatcher.VersionEnum.V1.value);
|
||||
if (ObjectUtil.isNotEmpty(msgHandler)) {
|
||||
UpMsgResponse response = msgHandler.upHandle(message, clientId );
|
||||
UpMsgResponse response = msgHandler.upHandle(data, clientId );
|
||||
AssertLog.info("<<[up-after-handle]:clientId:{},type={},[handle-content]={}", response.getClientId(), dataType, message);
|
||||
if(Objects.nonNull(response)){
|
||||
Message agentMessage = Message.builder().build();
|
||||
|
||||
Reference in New Issue
Block a user