采集业务细节优化

This commit is contained in:
qiminbao
2025-09-12 14:34:43 +08:00
parent 2c29156748
commit 71ad483c26
11 changed files with 451 additions and 169 deletions
@@ -4,7 +4,6 @@ import com.tongran.agent.client.core.config.GlobalConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.time.LocalDateTime;
@SpringBootApplication
public class TrAgentClientApplication {
@@ -40,21 +40,21 @@ public enum MsgEnum {
告警上报("ALARM"),
开启系统采集("SYSTEM_COLLECT_START"),
开启或更新系统采集("SYSTEM_COLLECT_START"),
开启系统采集应答("SYSTEM_COLLECT_START_RSP"),
开启或更新系统采集应答("SYSTEM_COLLECT_START_RSP"),
关闭系统采集("SYSTEM_COLLECT_STOP"),
关闭所有系统采集("SYSTEM_COLLECT_STOP"),
关闭系统采集应答("SYSTEM_COLLECT_STOP_RSP"),
关闭所有系统采集应答("SYSTEM_COLLECT_STOP_RSP"),
开启交换机采集("SWITCH_COLLECT_START"),
开启或更新交换机采集("SWITCH_COLLECT_START"),
开启交换机采集应答("SWITCH_COLLECT_START_RSP"),
开启或更新交换机采集应答("SWITCH_COLLECT_START_RSP"),
关闭交换机采集("SWITCH_COLLECT_STOP"),
关闭所有交换机采集("SWITCH_COLLECT_STOP"),
关闭交换机采集应答("SWITCH_COLLECT_STOP_RSP"),
关闭所有交换机采集应答("SWITCH_COLLECT_STOP_RSP"),
告警设置("ALARM_SET"),
@@ -119,11 +119,7 @@ public class AgentEndpoint {
//更改全局变量
GlobalConfig.isCollect = false;
//调用采集任务
try {
appInitializer.run();
} catch (Exception e) {
e.printStackTrace();
}
agentService.cancelCollect();
long timestamp = System.currentTimeMillis();
timestamp = Math.round(timestamp / 1000.0);
JSONObject json = new JSONObject();
@@ -135,7 +131,7 @@ public class AgentEndpoint {
}
}
@AgentDispatcher(msgId = MsgEnum.开启系统采集)
@AgentDispatcher(msgId = MsgEnum.开启或更新系统采集)
public class SysCollectStartHandler implements AgentHandler {
@Override
public UpMsgResponse upHandle(String data, String clientId) {
@@ -147,11 +143,11 @@ public class AgentEndpoint {
json.put("resCode",1);
json.put("resMag","");
json.put("timestamp",timestamp);
return UpMsgResponse.builder().dataType(MsgEnum.开启系统采集应答.getValue()).content(json.toString()).build();
return UpMsgResponse.builder().dataType(MsgEnum.开启或更新系统采集应答.getValue()).content(json.toString()).build();
}
}
@AgentDispatcher(msgId = MsgEnum.关闭系统采集)
@AgentDispatcher(msgId = MsgEnum.关闭所有系统采集)
public class SysCollectStopHandler implements AgentHandler {
@Override
public UpMsgResponse upHandle(String data, String clientId) {
@@ -163,11 +159,11 @@ public class AgentEndpoint {
json.put("resCode",1);
json.put("resMag","");
json.put("timestamp",timestamp);
return UpMsgResponse.builder().dataType(MsgEnum.关闭系统采集应答.getValue()).content(json.toString()).build();
return UpMsgResponse.builder().dataType(MsgEnum.关闭所有系统采集应答.getValue()).content(json.toString()).build();
}
}
@AgentDispatcher(msgId = MsgEnum.开启交换机采集)
@AgentDispatcher(msgId = MsgEnum.开启或更新交换机采集)
public class SwitchCollectStartHandler implements AgentHandler {
@Override
public UpMsgResponse upHandle(String data, String clientId) {
@@ -179,11 +175,11 @@ public class AgentEndpoint {
json.put("resCode",1);
json.put("resMag","");
json.put("timestamp",timestamp);
return UpMsgResponse.builder().dataType(MsgEnum.开启交换机采集应答.getValue()).content(json.toString()).build();
return UpMsgResponse.builder().dataType(MsgEnum.开启或更新交换机采集应答.getValue()).content(json.toString()).build();
}
}
@AgentDispatcher(msgId = MsgEnum.关闭交换机采集)
@AgentDispatcher(msgId = MsgEnum.关闭所有交换机采集)
public class SwitchCollectStopHandler implements AgentHandler {
@Override
public UpMsgResponse upHandle(String data, String clientId) {
@@ -195,7 +191,7 @@ public class AgentEndpoint {
json.put("resCode",1);
json.put("resMag","");
json.put("timestamp",timestamp);
return UpMsgResponse.builder().dataType(MsgEnum.关闭交换机采集应答.getValue()).content(json.toString()).build();
return UpMsgResponse.builder().dataType(MsgEnum.关闭所有交换机采集应答.getValue()).content(json.toString()).build();
}
}
@@ -28,7 +28,7 @@ public class SchedulerConfig {
@Bean("taskExecutor")
public ThreadPoolTaskScheduler taskExecutor() {
ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
executor.setPoolSize(10); // 工作线程数
executor.setPoolSize(11); // 工作线程数
executor.setThreadNamePrefix("task-worker-");
executor.setAwaitTerminationSeconds(30);
executor.setWaitForTasksToCompleteOnShutdown(true);
@@ -22,21 +22,18 @@ public class AppInitializer implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("应用启动完成,开始初始化定时任务...");
if(!GlobalConfig.isCollect){
if(GlobalConfig.isCollect){
// 开启定时任务
initScheduledTasks();
}else{
// 关闭定时任务
cancelTasks();
}
System.out.println("定时任务初始化完成");
}
private void initScheduledTasks() {
// 每30秒执行心跳任务
AssertLog.info("初始化启动心跳定时任务 - 延迟: {}ms, 间隔: {}ms", 5000, 30000);
AssertLog.info("初始化启动心跳定时任务 - 延迟: {}ms, 间隔: {}ms", 15000, 30000);
dynamicTaskService.scheduleTask("heartbeat",
businessTasks::heartbeatTask, 5000, 30000);
businessTasks::heartbeatTask, 15000, 30000);
// long milli = AgentUtil.getMillisToNextMinute() + 60000;
// // 每300秒执行CPU信息采集任务
@@ -84,15 +81,4 @@ public class AppInitializer implements CommandLineRunner {
}
private void cancelTasks() {
dynamicTaskService.cancelTask("heartbeat");
// dynamicTaskService.cancelTask("cpu");
// dynamicTaskService.cancelTask("disk");
// dynamicTaskService.cancelTask("system");
// dynamicTaskService.cancelTask("docker");
// dynamicTaskService.cancelTask("memory");
// dynamicTaskService.cancelTask("net");
// dynamicTaskService.cancelTask("point");
// dynamicTaskService.cancelTask("switchBoard");
}
}
@@ -99,6 +99,7 @@ public class BusinessTasks {
object.put("timestamp",timestamp);
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.心跳上报.getValue()).data(object.toString()).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送心跳包={}",JSON.toJSONString(message));
}
AssertLog.info("心跳定时任务执行 - task #{} completed", count);
}
@@ -120,6 +121,7 @@ public class BusinessTasks {
String data = JSON.toJSONString(cpuVO);
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.CPU上报.getValue()).data(data).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送CPU信息包={}",JSON.toJSONString(message));
}
AssertLog.info("CPU信息采集定时任务执行 - task #{} completed", count);
}
@@ -143,6 +145,7 @@ public class BusinessTasks {
}
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.磁盘上报.getValue()).data(data).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送磁盘信息包={}",JSON.toJSONString(message));
}
AssertLog.info("磁盘信息采集定时任务执行 - task #{} completed", count);
}
@@ -166,6 +169,7 @@ public class BusinessTasks {
}
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.容器上报.getValue()).data(data).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送容器信息包={}",JSON.toJSONString(message));
}
AssertLog.info("容器信息采集定时任务执行 - task #{} completed", count);
}
@@ -187,6 +191,7 @@ public class BusinessTasks {
String data = JSON.toJSONString(memoryVO);
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.内存上报.getValue()).data(data).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送内存信息包={}",JSON.toJSONString(message));
}
AssertLog.info("内存信息采集定时任务执行 - task #{} completed", count);
}
@@ -209,6 +214,7 @@ public class BusinessTasks {
}
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.网络上报.getValue()).data(data).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送网络信息包={}",JSON.toJSONString(message));
}
AssertLog.info("网络信息采集定时任务执行 - task #{} completed", count);
}
@@ -232,6 +238,7 @@ public class BusinessTasks {
}
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.挂载上报.getValue()).data(data).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送挂载信息包={}",JSON.toJSONString(message));
}
AssertLog.info("挂载点信息采集定时任务执行 - task #{} completed", count);
}
@@ -259,6 +266,7 @@ public class BusinessTasks {
jsonObject.put("timestamp", timestamp);
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.交换机上报.getValue()).data(jsonObject.toString()).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送交换机信息包={}",JSON.toJSONString(message));
}
AssertLog.info("交换机信息采集定时任务执行 - task #{} completed", count);
}
@@ -287,6 +295,7 @@ public class BusinessTasks {
jsonObject.put("timestamp", timestamp);
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.系统其他上报.getValue()).data(jsonObject.toString()).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送系统信息包={}",JSON.toJSONString(message));
}
AssertLog.info("系统信息采集定时任务执行 - task #{} completed", count);
}
@@ -309,6 +318,7 @@ public class BusinessTasks {
}
Message message = Message.builder().clientId(GlobalConfig.CLIENT_ID).dataType(MsgEnum.告警上报.getValue()).data(data).build();
sessionManager.writeAndFlush(sessionManager.getSessionById(GlobalConfig.CLIENT_ID).getChannel(), message);
AssertLog.info("发送告警监控信息包={}",JSON.toJSONString(message));
}
AssertLog.info("告警监控定时任务执行 - task #{} completed", count);
}
@@ -1,6 +1,8 @@
package com.tongran.agent.client.service;
public interface AgentService {
void cancelCollect();
void systemCollectStart(String data);
void systemCollectStop();
@@ -31,6 +31,13 @@ public class AgentServiceImpl implements AgentService {
this.businessTasks = businessTasks;
}
@Override
public void cancelCollect() {
systemCollectStop();
switchCollectStop();
dynamicTaskService.cancelTask("heartbeat");
}
@Override
public void systemCollectStart(String data) {
//更改全局变量
@@ -43,122 +50,257 @@ public class AgentServiceImpl implements AgentService {
boolean collect = c.isCollect();
switch(type) {
case "cpuCollect": //cpu采集
GlobalConfig.cpuCollect = collect;
GlobalConfig.cpuInterval = c.getInterval();
if(collect){
long milli = AgentUtil.getMillisToNextMinute() + 60000;
dynamicTaskService.scheduleTask(c.getType(),
businessTasks::cpuTask, milli, GlobalConfig.cpuInterval*1000L);
//判断与上次有无变化
if(GlobalConfig.cpuCollect != collect || GlobalConfig.cpuInterval != c.getInterval()){
GlobalConfig.cpuCollect = collect;
if(collect){
GlobalConfig.cpuInterval = c.getInterval();
long milli = AgentUtil.getMillisToNextMinute() + 60000;
dynamicTaskService.scheduleTask(type,
businessTasks::cpuTask, milli, GlobalConfig.cpuInterval*1000L);
}else{
dynamicTaskService.cancelTask(type);
GlobalConfig.cpuInterval = 300;
}
}
break;
case "vfsCollect": //挂载采集
GlobalConfig.vfsCollect = collect;
GlobalConfig.vfsInterval = c.getInterval();
if(collect){
long milli = AgentUtil.getMillisToNextMinute() + 60000;
dynamicTaskService.scheduleTask(c.getType(),
businessTasks::pointTask, milli, GlobalConfig.vfsInterval*1000L);
if(GlobalConfig.vfsCollect != collect || GlobalConfig.vfsInterval != c.getInterval()){
GlobalConfig.vfsCollect = collect;
if(collect){
GlobalConfig.vfsInterval = c.getInterval();
long milli = AgentUtil.getMillisToNextMinute() + 60000;
dynamicTaskService.scheduleTask(type,
businessTasks::pointTask, milli, GlobalConfig.vfsInterval*1000L);
}else{
dynamicTaskService.cancelTask(type);
GlobalConfig.vfsInterval = 300;
}
}
break;
case "netCollect": //网络采集
GlobalConfig.netCollect = collect;
GlobalConfig.netInterval = c.getInterval();
if(collect){
long milli = AgentUtil.millisecondsToNext5Minute();
dynamicTaskService.scheduleTask(c.getType(),
businessTasks::netTask, milli, GlobalConfig.netInterval*1000L);
if(GlobalConfig.netCollect != collect || GlobalConfig.netInterval != c.getInterval()){
GlobalConfig.netCollect = collect;
if(collect){
GlobalConfig.netInterval = c.getInterval();
long milli = AgentUtil.millisecondsToNext5Minute();
dynamicTaskService.scheduleTask(type,
businessTasks::netTask, milli, GlobalConfig.netInterval*1000L);
}else{
dynamicTaskService.cancelTask(type);
GlobalConfig.netInterval = 300;
}
}
break;
case "diskCollect": //磁盘采集
GlobalConfig.diskCollect = collect;
GlobalConfig.diskInterval = c.getInterval();
if(collect){
long milli = AgentUtil.getMillisToNextMinute() + 60000;
dynamicTaskService.scheduleTask(c.getType(),
businessTasks::diskTask, milli, GlobalConfig.diskInterval*1000L);
if(GlobalConfig.diskCollect != collect || GlobalConfig.diskInterval != c.getInterval()){
GlobalConfig.diskCollect = collect;
if(collect){
GlobalConfig.diskInterval = c.getInterval();
long milli = AgentUtil.getMillisToNextMinute() + 60000;
dynamicTaskService.scheduleTask(type,
businessTasks::diskTask, milli, GlobalConfig.diskInterval*1000L);
}else{
dynamicTaskService.cancelTask(type);
GlobalConfig.diskInterval = 300;
}
}
break;
case "dockerCollect": //docker采集
GlobalConfig.dockerCollect = collect;
GlobalConfig.dockerInterval = c.getInterval();
if(collect){
long milli = AgentUtil.getMillisToNextMinute() + 60000;
dynamicTaskService.scheduleTask(c.getType(),
businessTasks::dockerTask, milli, GlobalConfig.dockerInterval*1000L);
if(GlobalConfig.dockerCollect != collect || GlobalConfig.dockerInterval != c.getInterval()){
GlobalConfig.dockerCollect = collect;
if(collect){
GlobalConfig.dockerInterval = c.getInterval();
long milli = AgentUtil.getMillisToNextMinute() + 60000;
dynamicTaskService.scheduleTask(type,
businessTasks::dockerTask, milli, GlobalConfig.dockerInterval*1000L);
}else{
dynamicTaskService.cancelTask(type);
GlobalConfig.dockerInterval = 300;
}
}
break;
default: //系统其他采集
boolean flag = false;
if(StringUtils.equals(type, "systemSwapSizeFreeCollect")){
GlobalConfig.systemSwapSizeFreeCollect = collect;
GlobalConfig.systemSwapSizeFreeInterval = c.getInterval();
if(GlobalConfig.systemSwapSizeFreeCollect != collect || GlobalConfig.systemSwapSizeFreeInterval != c.getInterval()){
flag = true;
GlobalConfig.systemSwapSizeFreeCollect = collect;
GlobalConfig.systemSwapSizeFreeInterval = c.getInterval();
if(!collect){
GlobalConfig.systemSwapSizeFreeInterval = 300;
}
}
}
if(StringUtils.equals(type, "memoryUtilizationCollect")){
GlobalConfig.memoryUtilizationCollect = collect;
GlobalConfig.memoryUtilizationInterval = c.getInterval();
if(GlobalConfig.memoryUtilizationCollect != collect || GlobalConfig.memoryUtilizationInterval != c.getInterval()){
flag = true;
GlobalConfig.memoryUtilizationCollect = collect;
GlobalConfig.memoryUtilizationInterval = c.getInterval();
if(!collect){
GlobalConfig.memoryUtilizationInterval = 300;
}
}
}
if(StringUtils.equals(type, "systemSwapSizePercentCollect")){
GlobalConfig.systemSwapSizePercentCollect = collect;
GlobalConfig.systemSwapSizePercentInterval = c.getInterval();
if(GlobalConfig.systemSwapSizePercentCollect != collect || GlobalConfig.systemSwapSizePercentInterval != c.getInterval()){
flag = true;
GlobalConfig.systemSwapSizePercentCollect = collect;
GlobalConfig.systemSwapSizePercentInterval = c.getInterval();
if(!collect){
GlobalConfig.systemSwapSizePercentInterval = 300;
}
}
}
if(StringUtils.equals(type, "memorySizeAvailableCollect")){
GlobalConfig.memorySizeAvailableCollect = collect;
GlobalConfig.memorySizeAvailableInterval = c.getInterval();
if(GlobalConfig.memorySizeAvailableCollect != collect || GlobalConfig.memorySizeAvailableInterval != c.getInterval()){
flag = true;
GlobalConfig.memorySizeAvailableCollect = collect;
GlobalConfig.memorySizeAvailableInterval = c.getInterval();
if(!collect){
GlobalConfig.memorySizeAvailableInterval = 300;
}
}
}
if(StringUtils.equals(type, "memorySizePercentCollect")){
GlobalConfig.memorySizePercentCollect = collect;
GlobalConfig.memorySizePercentInterval = c.getInterval();
if(GlobalConfig.memorySizePercentCollect != collect || GlobalConfig.memorySizePercentInterval != c.getInterval()){
flag = true;
GlobalConfig.memorySizePercentCollect = collect;
GlobalConfig.memorySizePercentInterval = c.getInterval();
if(!collect){
GlobalConfig.memorySizePercentInterval = 300;
}
}
}
if(StringUtils.equals(type, "memorySizeTotalCollect")){
GlobalConfig.memorySizeTotalCollect = collect;
GlobalConfig.memorySizeTotalInterval = c.getInterval();
if(GlobalConfig.memorySizeTotalCollect != collect || GlobalConfig.memorySizeTotalInterval != c.getInterval()){
flag = true;
GlobalConfig.memorySizeTotalCollect = collect;
GlobalConfig.memorySizeTotalInterval = c.getInterval();
if(!collect){
GlobalConfig.memorySizeTotalInterval = 300;
}
}
}
if(StringUtils.equals(type, "systemSwOsCollect")){
GlobalConfig.systemSwOsCollect = collect;
GlobalConfig.systemSwOsInterval = c.getInterval();
if(GlobalConfig.systemSwOsCollect != collect || GlobalConfig.systemSwOsInterval != c.getInterval()){
flag = true;
GlobalConfig.systemSwOsCollect = collect;
GlobalConfig.systemSwOsInterval = c.getInterval();
if(!collect){
GlobalConfig.systemSwOsInterval = 300;
}
}
}
if(StringUtils.equals(type, "systemSwArchCollect")){
GlobalConfig.systemSwArchCollect = collect;
GlobalConfig.systemSwArchInterval = c.getInterval();
if(GlobalConfig.systemSwArchCollect != collect || GlobalConfig.systemSwArchInterval != c.getInterval()){
flag = true;
GlobalConfig.systemSwArchCollect = collect;
GlobalConfig.systemSwArchInterval = c.getInterval();
if(!collect){
GlobalConfig.systemSwArchInterval = 300;
}
}
}
if(StringUtils.equals(type, "kernelMaxprocCollect")){
GlobalConfig.kernelMaxprocCollect = collect;
GlobalConfig.kernelMaxprocInterval = c.getInterval();
if(GlobalConfig.kernelMaxprocCollect != collect || GlobalConfig.kernelMaxprocInterval != c.getInterval()){
flag = true;
GlobalConfig.kernelMaxprocCollect = collect;
GlobalConfig.kernelMaxprocInterval = c.getInterval();
if(!collect){
GlobalConfig.kernelMaxprocInterval = 300;
}
}
}
if(StringUtils.equals(type, "procNumRunCollect")){
GlobalConfig.procNumRunCollect = collect;
GlobalConfig.procNumRunInterval = c.getInterval();
if(GlobalConfig.procNumRunCollect != collect || GlobalConfig.procNumRunInterval != c.getInterval()){
flag = true;
GlobalConfig.procNumRunCollect = collect;
GlobalConfig.procNumRunInterval = c.getInterval();
if(!collect){
GlobalConfig.procNumRunInterval = 300;
}
}
}
if(StringUtils.equals(type, "systemUsersNumCollect")){
GlobalConfig.systemUsersNumCollect = collect;
GlobalConfig.systemUsersNumInterval = c.getInterval();
if(GlobalConfig.systemUsersNumCollect != collect || GlobalConfig.systemUsersNumInterval != c.getInterval()){
flag = true;
GlobalConfig.systemUsersNumCollect = collect;
GlobalConfig.systemUsersNumInterval = c.getInterval();
if(!collect){
GlobalConfig.systemUsersNumInterval = 300;
}
}
}
if(StringUtils.equals(type, "systemDiskSizeTotalCollect")){
GlobalConfig.systemDiskSizeTotalCollect = collect;
GlobalConfig.systemDiskSizeTotalInterval = c.getInterval();
if(GlobalConfig.systemDiskSizeTotalCollect != collect || GlobalConfig.systemDiskSizeTotalInterval != c.getInterval()){
flag = true;
GlobalConfig.systemDiskSizeTotalCollect = collect;
GlobalConfig.systemDiskSizeTotalInterval = c.getInterval();
if(!collect){
GlobalConfig.systemDiskSizeTotalInterval = 300;
}
}
}
if(StringUtils.equals(type, "systemBoottimeCollect")){
GlobalConfig.systemBoottimeCollect = collect;
GlobalConfig.systemBoottimeInterval = c.getInterval();
if(GlobalConfig.systemBoottimeCollect != collect || GlobalConfig.systemBoottimeInterval != c.getInterval()){
flag = true;
GlobalConfig.systemBoottimeCollect = collect;
GlobalConfig.systemBoottimeInterval = c.getInterval();
if(!collect){
GlobalConfig.systemBoottimeInterval = 300;
}
}
}
if(StringUtils.equals(type, "systemUnameCollect")){
GlobalConfig.systemUnameCollect = collect;
GlobalConfig.systemUnameInterval = c.getInterval();
if(GlobalConfig.systemUnameCollect != collect || GlobalConfig.systemUnameInterval != c.getInterval()){
flag = true;
GlobalConfig.systemUnameCollect = collect;
GlobalConfig.systemUnameInterval = c.getInterval();
if(!collect){
GlobalConfig.systemUnameInterval = 300;
}
}
}
if(StringUtils.equals(type, "systemLocaltimeCollect")){
GlobalConfig.systemLocaltimeCollect = collect;
GlobalConfig.systemLocaltimeInterval = c.getInterval();
if(GlobalConfig.systemLocaltimeCollect != collect || GlobalConfig.systemLocaltimeInterval != c.getInterval()){
flag = true;
GlobalConfig.systemLocaltimeCollect = collect;
GlobalConfig.systemLocaltimeInterval = c.getInterval();
if(!collect){
GlobalConfig.systemLocaltimeInterval = 300;
}
}
}
if(StringUtils.equals(type, "systemUptimeCollect")){
GlobalConfig.systemUptimeCollect = collect;
GlobalConfig.systemUptimeInterval = c.getInterval();
if(GlobalConfig.systemUptimeCollect != collect || GlobalConfig.systemUptimeInterval != c.getInterval()){
flag = true;
GlobalConfig.systemUptimeCollect = collect;
GlobalConfig.systemUptimeInterval = c.getInterval();
if(!collect){
GlobalConfig.systemUptimeInterval = 300;
}
}
}
if(StringUtils.equals(type, "procNumCollect")){
GlobalConfig.procNumCollect = collect;
GlobalConfig.procNumInterval = c.getInterval();
if(GlobalConfig.procNumCollect != collect || GlobalConfig.procNumInterval != c.getInterval()){
flag = true;
GlobalConfig.procNumCollect = collect;
GlobalConfig.procNumInterval = c.getInterval();
if(!collect){
GlobalConfig.procNumInterval = 300;
}
}
}
if (flag) {
if(collect){
long milli = AgentUtil.getMillisToNextMinute() + 60000;
dynamicTaskService.scheduleTask(type,
() -> businessTasks.systemTask(type), milli, c.getInterval()*1000L);
}else{
dynamicTaskService.cancelTask(type);
}
}
long milli = AgentUtil.getMillisToNextMinute() + 60000;
dynamicTaskService.scheduleTask(c.getType(),
() -> businessTasks.systemTask(c.getType()), milli, c.getInterval()*1000L);
break;
}
}
@@ -236,90 +378,209 @@ public class AgentServiceImpl implements AgentService {
for (CollectEO c : configList) {
String type = c.getType();
boolean collect = c.isCollect();
boolean flag = false;
switch(type) {
case "switchNetCollect": //交换机网络采集
GlobalConfig.switchNetCollect = collect;
GlobalConfig.switchNetInterval = c.getInterval();
if(GlobalConfig.switchNetCollect != collect || GlobalConfig.switchNetInterval != c.getInterval()){
flag = true;
GlobalConfig.switchNetCollect = collect;
GlobalConfig.switchNetInterval = c.getInterval();
if(!collect){
GlobalConfig.switchNetInterval = 300;
}
}
break;
case "switchModuleCollect": //光模块采集
GlobalConfig.switchModuleCollect = collect;
GlobalConfig.switchModuleInterval = c.getInterval();
if(GlobalConfig.switchModuleCollect != collect || GlobalConfig.switchModuleInterval != c.getInterval()){
flag = true;
GlobalConfig.switchModuleCollect = collect;
GlobalConfig.switchModuleInterval = c.getInterval();
if(!collect){
GlobalConfig.switchModuleInterval = 300;
}
}
break;
case "switchMpuCollect": //MPU采集
GlobalConfig.switchMpuCollect = collect;
GlobalConfig.switchMpuInterval = c.getInterval();
if(GlobalConfig.switchMpuCollect != collect || GlobalConfig.switchMpuInterval != c.getInterval()){
flag = true;
GlobalConfig.switchMpuCollect = collect;
GlobalConfig.switchMpuInterval = c.getInterval();
if(!collect){
GlobalConfig.switchMpuInterval = 300;
}
}
break;
case "switchPwrCollect": //电源采集
GlobalConfig.switchPwrCollect = collect;
GlobalConfig.switchPwrInterval = c.getInterval();
if(GlobalConfig.switchPwrCollect != collect || GlobalConfig.switchPwrInterval != c.getInterval()){
flag = true;
GlobalConfig.switchPwrCollect = collect;
GlobalConfig.switchPwrInterval = c.getInterval();
if(!collect){
GlobalConfig.switchPwrInterval = 300;
}
}
break;
case "dockerCollect": //风扇采集
GlobalConfig.switchFanCollect = collect;
GlobalConfig.switchFanInterval = c.getInterval();
if(GlobalConfig.switchFanCollect != collect || GlobalConfig.switchFanInterval != c.getInterval()){
flag = true;
GlobalConfig.switchFanCollect = collect;
GlobalConfig.switchFanInterval = c.getInterval();
if(!collect){
GlobalConfig.switchFanInterval = 300;
}
}
break;
default: //系统其他采集
if(StringUtils.equals(type, "switchSysDescrCollect")){
GlobalConfig.switchSysDescrCollect = collect;
GlobalConfig.switchSysDescrInterval = c.getInterval();
if(GlobalConfig.switchSysDescrCollect != collect || GlobalConfig.switchSysDescrInterval != c.getInterval()){
flag = true;
GlobalConfig.switchSysDescrCollect = collect;
GlobalConfig.switchSysDescrInterval = c.getInterval();
if(!collect){
GlobalConfig.switchSysDescrInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchSysObjectIDCollect")){
GlobalConfig.switchSysObjectIDCollect = collect;
GlobalConfig.switchSysObjectIDInterval = c.getInterval();
if(GlobalConfig.switchSysObjectIDCollect != collect || GlobalConfig.switchSysObjectIDInterval != c.getInterval()){
flag = true;
GlobalConfig.switchSysObjectIDCollect = collect;
GlobalConfig.switchSysObjectIDInterval = c.getInterval();
if(!collect){
GlobalConfig.switchSysObjectIDInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchSysUpTimeCollect")){
GlobalConfig.switchSysUpTimeCollect = collect;
GlobalConfig.switchSysUpTimeInterval = c.getInterval();
if(GlobalConfig.switchSysUpTimeCollect != collect || GlobalConfig.switchSysUpTimeInterval != c.getInterval()){
flag = true;
GlobalConfig.switchSysUpTimeCollect = collect;
GlobalConfig.switchSysUpTimeInterval = c.getInterval();
if(!collect){
GlobalConfig.switchSysUpTimeInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchSysContactCollect")){
GlobalConfig.switchSysContactCollect = collect;
GlobalConfig.switchSysContactInterval = c.getInterval();
if(GlobalConfig.switchSysContactCollect != collect || GlobalConfig.switchSysContactInterval != c.getInterval()){
flag = true;
GlobalConfig.switchSysContactCollect = collect;
GlobalConfig.switchSysContactInterval = c.getInterval();
if(!collect){
GlobalConfig.switchSysContactInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchSysNameCollect")){
GlobalConfig.switchSysNameCollect = collect;
GlobalConfig.switchSysNameInterval = c.getInterval();
if(GlobalConfig.switchSysNameCollect != collect || GlobalConfig.switchSysNameInterval != c.getInterval()){
flag = true;
GlobalConfig.switchSysNameCollect = collect;
GlobalConfig.switchSysNameInterval = c.getInterval();
if(!collect){
GlobalConfig.switchSysNameInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchSysLocationCollect")){
GlobalConfig.switchSysLocationCollect = collect;
GlobalConfig.switchSysLocationInterval = c.getInterval();
if(GlobalConfig.switchSysLocationCollect != collect || GlobalConfig.switchSysLocationInterval != c.getInterval()){
flag = true;
GlobalConfig.switchSysLocationCollect = collect;
GlobalConfig.switchSysLocationInterval = c.getInterval();
if(!collect){
GlobalConfig.switchSysLocationInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchHwStackSystemMacCollect")){
GlobalConfig.switchHwStackSystemMacCollect = collect;
GlobalConfig.switchHwStackSystemMacInterval = c.getInterval();
if(GlobalConfig.switchHwStackSystemMacCollect != collect || GlobalConfig.switchHwStackSystemMacInterval != c.getInterval()){
flag = true;
GlobalConfig.switchHwStackSystemMacCollect = collect;
GlobalConfig.switchHwStackSystemMacInterval = c.getInterval();
if(!collect){
GlobalConfig.switchHwStackSystemMacInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchEntIndexCollect")){
GlobalConfig.switchEntIndexCollect = collect;
GlobalConfig.switchEntIndexInterval = c.getInterval();
if(GlobalConfig.switchEntIndexCollect != collect || GlobalConfig.switchEntIndexInterval != c.getInterval()){
flag = true;
GlobalConfig.switchEntIndexCollect = collect;
GlobalConfig.switchEntIndexInterval = c.getInterval();
if(!collect){
GlobalConfig.switchEntIndexInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchEntPhysicalNameCollect")){
GlobalConfig.switchEntPhysicalNameCollect = collect;
GlobalConfig.switchEntPhysicalNameInterval = c.getInterval();
if(GlobalConfig.switchEntPhysicalNameCollect != collect || GlobalConfig.switchEntPhysicalNameInterval != c.getInterval()){
flag = true;
GlobalConfig.switchEntPhysicalNameCollect = collect;
GlobalConfig.switchEntPhysicalNameInterval = c.getInterval();
if(!collect){
GlobalConfig.switchEntPhysicalNameInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchEntPhysicalSoftwareRevCollect")){
GlobalConfig.switchEntPhysicalSoftwareRevCollect = collect;
GlobalConfig.switchEntPhysicalSoftwareRevInterval = c.getInterval();
if(GlobalConfig.switchEntPhysicalSoftwareRevCollect != collect || GlobalConfig.switchEntPhysicalSoftwareRevInterval != c.getInterval()){
flag = true;
GlobalConfig.switchEntPhysicalSoftwareRevCollect = collect;
GlobalConfig.switchEntPhysicalSoftwareRevInterval = c.getInterval();
if(!collect){
GlobalConfig.switchEntPhysicalSoftwareRevInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchHwEntityCpuUsageCollect")){
GlobalConfig.switchHwEntityCpuUsageCollect = collect;
GlobalConfig.switchHwEntityCpuUsageInterval = c.getInterval();
if(GlobalConfig.switchHwEntityCpuUsageCollect != collect || GlobalConfig.switchHwEntityCpuUsageInterval != c.getInterval()){
flag = true;
GlobalConfig.switchHwEntityCpuUsageCollect = collect;
GlobalConfig.switchHwEntityCpuUsageInterval = c.getInterval();
if(!collect){
GlobalConfig.switchHwEntityCpuUsageInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchHwEntityMemUsageCollect")){
GlobalConfig.switchHwEntityMemUsageCollect = collect;
GlobalConfig.switchHwEntityMemUsageInterval = c.getInterval();
if(GlobalConfig.switchHwEntityMemUsageCollect != collect || GlobalConfig.switchHwEntityMemUsageInterval != c.getInterval()){
flag = true;
GlobalConfig.switchHwEntityMemUsageCollect = collect;
GlobalConfig.switchHwEntityMemUsageInterval = c.getInterval();
if(!collect){
GlobalConfig.switchHwEntityMemUsageInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchHwAveragePowerCollect")){
GlobalConfig.switchHwAveragePowerCollect = collect;
GlobalConfig.switchHwAveragePowerInterval = c.getInterval();
if(GlobalConfig.switchHwAveragePowerCollect != collect || GlobalConfig.switchHwAveragePowerInterval != c.getInterval()){
flag = true;
GlobalConfig.switchHwAveragePowerCollect = collect;
GlobalConfig.switchHwAveragePowerInterval = c.getInterval();
if(!collect){
GlobalConfig.switchHwAveragePowerInterval = 300;
}
}
}
if(StringUtils.equals(type, "switchHwCurrentPowerCollect")){
GlobalConfig.switchHwCurrentPowerCollect = collect;
GlobalConfig.switchHwCurrentPowerInterval = c.getInterval();
if(GlobalConfig.switchHwCurrentPowerCollect != collect || GlobalConfig.switchHwCurrentPowerInterval != c.getInterval()){
flag = true;
GlobalConfig.switchHwCurrentPowerCollect = collect;
GlobalConfig.switchHwCurrentPowerInterval = c.getInterval();
if(!collect){
GlobalConfig.switchHwCurrentPowerInterval = 300;
}
}
}
break;
}
if(collect){
long milli = AgentUtil.millisecondsToNext5Minute();
dynamicTaskService.scheduleTask(c.getType(),
() -> businessTasks.switchBoardTask(c.getType()), milli, GlobalConfig.switchNetInterval*1000L);
if (flag) {
if(collect){
long milli = AgentUtil.millisecondsToNext5Minute();
dynamicTaskService.scheduleTask(c.getType(),
() -> businessTasks.switchBoardTask(c.getType()), milli, c.getInterval()*1000L);
}else{
dynamicTaskService.cancelTask(type);
}
}
}
}
@@ -49,42 +49,70 @@ public class AlarmServiceImpl implements AlarmService {
String threshold = alarmEO.getThreshold();
int compareType = alarmEO.getCompareType();
AlarmVO alarmVO = AlarmVO.builder().type(type).timestamp(timestamp).build();
String result = "";
switch(type) {
case "systemCpuUti": //CPU使用率
alarmVO.setResult(handleSystemCpuUti(threshold, compareType));
alarmVOList.add(alarmVO);
result = handleSystemCpuUti(threshold, compareType);
if(StringUtils.isNotBlank(result)){
alarmVO.setResult(result);
alarmVOList.add(alarmVO);
}
break;
case "memoryUtilization": //内存利用率
alarmVO.setResult(handleMemoryUtilization(threshold, compareType));
alarmVOList.add(alarmVO);
result = handleMemoryUtilization(threshold, compareType);
if(StringUtils.isNotBlank(result)){
alarmVO.setResult(result);
alarmVOList.add(alarmVO);
}
break;
case "systemSwapSizePercent": //可用交换空间百分比
alarmVO.setResult(handleSystemSwapSizePercent(threshold, compareType));
alarmVOList.add(alarmVO);
result = handleSystemSwapSizePercent(threshold, compareType);
if(StringUtils.isNotBlank(result)){
alarmVO.setResult(result);
alarmVOList.add(alarmVO);
}
break;
case "systemUsersNum": //登录用户数
alarmVO.setResult(handleSystemUsersNum(threshold, compareType));
alarmVOList.add(alarmVO);
result = handleSystemUsersNum(threshold, compareType);
if(StringUtils.isNotBlank(result)){
alarmVO.setResult(result);
alarmVOList.add(alarmVO);
}
break;
case "vfsFsUtil": //挂载点的空间利用率
alarmVO.setResult(handleVfsFsUtil(threshold, compareType));
alarmVOList.add(alarmVO);
result = handleVfsFsUtil(threshold, compareType);
if(StringUtils.isNotBlank(result)){
alarmVO.setResult(result);
alarmVOList.add(alarmVO);
}
break;
case "netUtil": //网络带宽使用率
alarmVO.setResult(handleNetUtil(threshold, compareType));
alarmVOList.add(alarmVO);
result = handleNetUtil(threshold, compareType);
if(StringUtils.isNotBlank(result)){
alarmVO.setResult(result);
alarmVOList.add(alarmVO);
}
break;
case "containerMemUtil": //容器内存使用率
alarmVO.setResult(handleContainerMemUtil(threshold, compareType));
alarmVOList.add(alarmVO);
result = handleContainerMemUtil(threshold, compareType);
if(StringUtils.isNotBlank(result)){
alarmVO.setResult(result);
alarmVOList.add(alarmVO);
}
break;
case "netIfStatus": //网络运行状态UP变down
alarmVO.setResult(handleNetIfStatus());
alarmVOList.add(alarmVO);
result = handleNetIfStatus();
if(StringUtils.isNotBlank(result)){
alarmVO.setResult(result);
alarmVOList.add(alarmVO);
}
break;
case "extraPorts": //多余端口
alarmVO.setResult(handleExtraPorts(threshold));
alarmVOList.add(alarmVO);
result = handleExtraPorts(threshold);
if(StringUtils.isNotBlank(result)){
alarmVO.setResult(result);
alarmVOList.add(alarmVO);
}
break;
default: //其他
break;