diff --git a/src/main/java/com/tongran/agent/client/core/config/GlobalConfig.java b/src/main/java/com/tongran/agent/client/core/config/GlobalConfig.java index 0d6d0bf..13c5ef5 100644 --- a/src/main/java/com/tongran/agent/client/core/config/GlobalConfig.java +++ b/src/main/java/com/tongran/agent/client/core/config/GlobalConfig.java @@ -38,6 +38,11 @@ public class GlobalConfig { */ public static String DEVICE_SN; + /** + * 系统 HZ (每秒tick数) + */ + public static Integer systemHz = null; + /** * 逻辑标识 */ diff --git a/src/main/java/com/tongran/agent/client/core/vo/CpuVO.java b/src/main/java/com/tongran/agent/client/core/vo/CpuVO.java index 3ed0b47..fe11f60 100644 --- a/src/main/java/com/tongran/agent/client/core/vo/CpuVO.java +++ b/src/main/java/com/tongran/agent/client/core/vo/CpuVO.java @@ -25,8 +25,8 @@ public class CpuVO implements Serializable { @Schema(description = "CPU15分钟负载") private double avg15; - @Schema(description = "CPU硬件中断提供服务时间") - private long interrupt; + @Schema(description = "CPU硬件中断提供服务时间:秒") + private double interrupt; @Schema(description = "CPU使用率%") private double uti; @@ -34,23 +34,23 @@ public class CpuVO implements Serializable { @Schema(description = "CPU数量") private int num; - @Schema(description = "CPU正常运行时间/秒") + @Schema(description = "CPU正常运行时间:秒") private long normal; - @Schema(description = "CPU空闲时间") - private long idle; + @Schema(description = "CPU空闲时间:秒") + private double idle; - @Schema(description = "CPU等待响应时间") + @Schema(description = "CPU等待响应时间:秒") private double iowait; - @Schema(description = "CPU系统时间") - private long system; + @Schema(description = "CPU系统时间:秒") + private double system; @Schema(description = "CPU软件无响应时间") private double noresp; - @Schema(description = "CPU用户进程所花费的时间") - private long user; + @Schema(description = "CPU用户进程所花费的时间:秒") + private double user; @Schema(description = "时间戳") private long timestamp; diff --git a/src/main/java/com/tongran/agent/client/scheduler/service/AppInitializer.java b/src/main/java/com/tongran/agent/client/scheduler/service/AppInitializer.java index 841670a..9e74473 100644 --- a/src/main/java/com/tongran/agent/client/scheduler/service/AppInitializer.java +++ b/src/main/java/com/tongran/agent/client/scheduler/service/AppInitializer.java @@ -89,6 +89,8 @@ public class AppInitializer implements CommandLineRunner { AssertLog.info("启动多网IP探测定时任务 - 延迟: {}ms, 间隔: {}ms", milli, 300000); dynamicTaskService.scheduleTask("networkDetect", businessTasks::networkDetectTask, milli, 300000); + // 检测监控策略配置 + agentService.checkMonitor(); }else{ //未注册,发送注册 try { diff --git a/src/main/java/com/tongran/agent/client/service/AgentService.java b/src/main/java/com/tongran/agent/client/service/AgentService.java index 39dd493..92be1de 100644 --- a/src/main/java/com/tongran/agent/client/service/AgentService.java +++ b/src/main/java/com/tongran/agent/client/service/AgentService.java @@ -28,4 +28,6 @@ public interface AgentService { void dellRoute(String name, String gateway); String getLogicalNode(); + + void checkMonitor(); } diff --git a/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java b/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java index 93cb085..d8c649e 100644 --- a/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java +++ b/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java @@ -1,5 +1,6 @@ package com.tongran.agent.client.service.impl; +import cn.hutool.core.collection.CollectionUtil; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.TypeReference; @@ -34,6 +35,7 @@ import java.time.LocalDateTime; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; @Service public class AgentServiceImpl implements AgentService { @@ -276,6 +278,9 @@ public class AgentServiceImpl implements AgentService { long milli = AgentUtil.getMillisToNextMinute() + 60000; AssertLog.info("启动更新策略定时任务 - 延迟: {}ms, 间隔: {}ms", milli, 60000); dynamicTaskService.scheduleTask("policy", businessTasks::policyTask, milli, 60000); + + // 检测监控策略配置 + checkMonitor(); } @Override @@ -297,319 +302,321 @@ public class AgentServiceImpl implements AgentService { list.add("contents:["); String contents = monitorJson.getString("contents"); List collectList = JSON.parseObject(contents, new TypeReference>() {}); - AssertLog.info("开启或更新监控策略={}", JSON.toJSONString(collectList)); - for (CollectEO c : collectList) { - list.add(JSON.toJSONString(c)); - String type = c.getType(); - boolean collect = c.isCollect(); - int interval = c.getInterval(); - switch(type) { - case "cpuCollect": //cpu采集 - //判断与上次有无变化 - if(GlobalConfig.cpuCollect != collect || GlobalConfig.cpuInterval != interval){ - GlobalConfig.cpuCollect = collect; - if(collect){ - GlobalConfig.cpuInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::cpuTask, milli, GlobalConfig.cpuInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.cpuInterval = 300; + if(CollectionUtil.isNotEmpty(collectList)){ + AssertLog.info("开启或更新监控策略={}", JSON.toJSONString(collectList)); + for (CollectEO c : collectList) { + list.add(JSON.toJSONString(c)); + String type = c.getType(); + boolean collect = c.isCollect(); + int interval = c.getInterval(); + switch(type) { + case "cpuCollect": //cpu采集 + //判断与上次有无变化 + if(GlobalConfig.cpuCollect != collect || GlobalConfig.cpuInterval != interval){ + GlobalConfig.cpuCollect = collect; + if(collect){ + GlobalConfig.cpuInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::cpuTask, milli, GlobalConfig.cpuInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.cpuInterval = 300; + } } - } - break; - case "vfsCollect": //挂载采集 - if(GlobalConfig.vfsCollect != collect || GlobalConfig.vfsInterval != interval){ - GlobalConfig.vfsCollect = collect; - if(collect){ - GlobalConfig.vfsInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::pointTask, milli, GlobalConfig.vfsInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.vfsInterval = 300; + break; + case "vfsCollect": //挂载采集 + if(GlobalConfig.vfsCollect != collect || GlobalConfig.vfsInterval != interval){ + GlobalConfig.vfsCollect = collect; + if(collect){ + GlobalConfig.vfsInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::pointTask, milli, GlobalConfig.vfsInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.vfsInterval = 300; + } } - } - break; - case "netCollect": //网络采集 - if(GlobalConfig.netCollect != collect || GlobalConfig.netInterval != interval){ - GlobalConfig.netCollect = collect; - if(collect){ + break; + case "netCollect": //网络采集 + if(GlobalConfig.netCollect != collect || GlobalConfig.netInterval != interval){ + GlobalConfig.netCollect = collect; + if(collect){ // GlobalConfig.netInterval = interval; - long milli = AgentUtil.millisecondsToNext5Minute(); - dynamicTaskService.scheduleTask(type, - businessTasks::netTask, milli, GlobalConfig.netInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.netInterval = 300; + long milli = AgentUtil.millisecondsToNext5Minute(); + dynamicTaskService.scheduleTask(type, + businessTasks::netTask, milli, GlobalConfig.netInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.netInterval = 300; + } } - } - break; - case "diskCollect": //磁盘采集 - if(GlobalConfig.diskCollect != collect || GlobalConfig.diskInterval != interval){ - GlobalConfig.diskCollect = collect; + break; + case "diskCollect": //磁盘采集 + if(GlobalConfig.diskCollect != collect || GlobalConfig.diskInterval != interval){ + GlobalConfig.diskCollect = collect; + if(collect){ + GlobalConfig.diskInterval = interval; + 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采集 + if(GlobalConfig.dockerCollect != collect || GlobalConfig.dockerInterval != interval){ + GlobalConfig.dockerCollect = collect; + if(collect){ + GlobalConfig.dockerInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::dockerTask, milli, GlobalConfig.dockerInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.dockerInterval = 300; + } + } + break; + case "systemSwapSizeFreeCollect": //交换卷/文件的可用空间(字节)采集 + if(GlobalConfig.systemSwapSizeFreeCollect != collect || GlobalConfig.systemSwapSizeFreeInterval != interval){ + GlobalConfig.systemSwapSizeFreeCollect = collect; + if(collect){ + GlobalConfig.systemSwapSizeFreeInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemSwapSizeFreeTask, milli, GlobalConfig.systemSwapSizeFreeInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemSwapSizeFreeInterval = 300; + } + } + break; + case "memoryUtilizationCollect": //内存利用率采集 + if(GlobalConfig.memoryUtilizationCollect != collect || GlobalConfig.memoryUtilizationInterval != interval){ + GlobalConfig.memoryUtilizationCollect = collect; + if(collect){ + GlobalConfig.memoryUtilizationInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::memoryUtilizationTask, milli, GlobalConfig.memoryUtilizationInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.memoryUtilizationInterval = 300; + } + } + break; + case "systemSwapSizePercentCollect": //可用交换空间百分比采集 + if(GlobalConfig.systemSwapSizePercentCollect != collect || GlobalConfig.systemSwapSizePercentInterval != interval){ + GlobalConfig.systemSwapSizePercentCollect = collect; + if(collect){ + GlobalConfig.systemSwapSizePercentInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemSwapSizePercentTask, milli, GlobalConfig.systemSwapSizePercentInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemSwapSizePercentInterval = 300; + } + } + break; + case "memorySizeAvailableCollect": //可用内存采集 + if(GlobalConfig.memorySizeAvailableCollect != collect || GlobalConfig.memorySizeAvailableInterval != interval){ + GlobalConfig.memorySizeAvailableCollect = collect; + if(collect){ + GlobalConfig.memorySizeAvailableInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::memorySizeAvailableTask, milli, GlobalConfig.memorySizeAvailableInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.memorySizeAvailableInterval = 300; + } + } + break; + case "memorySizePercentCollect": //可用内存百分比采集 + if(GlobalConfig.memorySizePercentCollect != collect || GlobalConfig.memorySizePercentInterval != interval){ + GlobalConfig.memorySizePercentCollect = collect; + if(collect){ + GlobalConfig.memorySizePercentInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::memorySizePercentTask, milli, GlobalConfig.memorySizePercentInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.memorySizePercentInterval = 300; + } + } + break; + case "memorySizeTotalCollect": //总内存采集 + if(GlobalConfig.memorySizeTotalCollect != collect || GlobalConfig.memorySizeTotalInterval != interval){ + GlobalConfig.memorySizeTotalCollect = collect; + if(collect){ + GlobalConfig.memorySizeTotalInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::memorySizeTotalTask, milli, GlobalConfig.memorySizeTotalInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.memorySizeTotalInterval = 300; + } + } + break; + case "systemSwOsCollect": //操作系统采集 + if(GlobalConfig.systemSwOsCollect != collect || GlobalConfig.systemSwOsInterval != interval){ + GlobalConfig.systemSwOsCollect = collect; + if(collect){ + GlobalConfig.systemSwOsInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemSwOsTask, milli, GlobalConfig.systemSwOsInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemSwOsInterval = 300; + } + } + break; + case "systemSwArchCollect": //操作系统架构采集 + if(GlobalConfig.systemSwArchCollect != collect || GlobalConfig.systemSwArchInterval != interval){ + GlobalConfig.systemSwArchCollect = collect; + if(collect){ + GlobalConfig.systemSwArchInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemSwArchTask, milli, GlobalConfig.systemSwArchInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemSwArchInterval = 300; + } + } + break; + case "kernelMaxprocCollect": //最大进程数采集 + if(GlobalConfig.kernelMaxprocCollect != collect || GlobalConfig.kernelMaxprocInterval != interval){ + GlobalConfig.kernelMaxprocCollect = collect; + if(collect){ + GlobalConfig.kernelMaxprocInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::kernelMaxprocTask, milli, GlobalConfig.kernelMaxprocInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.kernelMaxprocInterval = 300; + } + } + break; + case "procNumRunCollect": //正在运行的进程数采集 + if(GlobalConfig.procNumRunCollect != collect || GlobalConfig.procNumRunInterval != interval){ + GlobalConfig.procNumRunCollect = collect; + if(collect){ + GlobalConfig.procNumRunInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::procNumRunTask, milli, GlobalConfig.procNumRunInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.procNumRunInterval = 300; + } + } + break; + case "systemUsersNumCollect": //登录用户数采集 + if(GlobalConfig.systemUsersNumCollect != collect || GlobalConfig.systemUsersNumInterval != interval){ + GlobalConfig.systemUsersNumCollect = collect; + if(collect){ + GlobalConfig.systemUsersNumInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemUsersNumTask, milli, GlobalConfig.systemUsersNumInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemUsersNumInterval = 300; + } + } + break; + case "systemDiskSizeTotalCollect": //硬盘总可用空间采集 + if(GlobalConfig.systemDiskSizeTotalCollect != collect || GlobalConfig.systemDiskSizeTotalInterval != interval){ + GlobalConfig.systemDiskSizeTotalCollect = collect; + if(collect){ + GlobalConfig.systemDiskSizeTotalInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemDiskSizeTotalTask, milli, GlobalConfig.systemDiskSizeTotalInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemDiskSizeTotalInterval = 300; + } + } + break; + case "systemBoottimeCollect": //系统启动时间采集 + if(GlobalConfig.systemBoottimeCollect != collect || GlobalConfig.systemBoottimeInterval != interval){ + GlobalConfig.systemBoottimeCollect = collect; + if(collect){ + GlobalConfig.systemBoottimeInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemBoottimeTask, milli, GlobalConfig.systemBoottimeInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemBoottimeInterval = 300; + } + } + break; + case "systemUnameCollect": //系统描述采集 + if(GlobalConfig.systemUnameCollect != collect || GlobalConfig.systemUnameInterval != interval){ + GlobalConfig.systemUnameCollect = collect; + if(collect){ + GlobalConfig.systemUnameInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemUnameTask, milli, GlobalConfig.systemUnameInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemUnameInterval = 300; + } + } + break; + case "systemLocaltimeCollect": //系统本地时间采集 + if(GlobalConfig.systemLocaltimeCollect != collect || GlobalConfig.systemLocaltimeInterval != interval){ + GlobalConfig.systemLocaltimeCollect = collect; + if(collect){ + GlobalConfig.systemLocaltimeInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemLocaltimeTask, milli, GlobalConfig.systemLocaltimeInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemLocaltimeInterval = 300; + } + } + break; + case "systemUptimeCollect": //系统正常运行时间采集 + if(GlobalConfig.systemUptimeCollect != collect || GlobalConfig.systemUptimeInterval != interval){ + GlobalConfig.systemUptimeCollect = collect; + if(collect){ + GlobalConfig.systemUptimeInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemUptimeTask, milli, GlobalConfig.systemUptimeInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemUptimeInterval = 300; + } + } + break; + default: //系统其他采集 + GlobalConfig.procNumCollect = collect; if(collect){ - GlobalConfig.diskInterval = interval; + GlobalConfig.procNumInterval = interval; long milli = AgentUtil.getMillisToNextMinute() + 60000; dynamicTaskService.scheduleTask(type, - businessTasks::diskTask, milli, GlobalConfig.diskInterval*1000L); + businessTasks::procNumTask, milli, GlobalConfig.procNumInterval*1000L); }else{ dynamicTaskService.cancelTask(type); - GlobalConfig.diskInterval = 300; + GlobalConfig.procNumInterval = 300; } - } - break; - case "dockerCollect": //docker采集 - if(GlobalConfig.dockerCollect != collect || GlobalConfig.dockerInterval != interval){ - GlobalConfig.dockerCollect = collect; - if(collect){ - GlobalConfig.dockerInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::dockerTask, milli, GlobalConfig.dockerInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.dockerInterval = 300; - } - } - break; - case "systemSwapSizeFreeCollect": //交换卷/文件的可用空间(字节)采集 - if(GlobalConfig.systemSwapSizeFreeCollect != collect || GlobalConfig.systemSwapSizeFreeInterval != interval){ - GlobalConfig.systemSwapSizeFreeCollect = collect; - if(collect){ - GlobalConfig.systemSwapSizeFreeInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::systemSwapSizeFreeTask, milli, GlobalConfig.systemSwapSizeFreeInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.systemSwapSizeFreeInterval = 300; - } - } - break; - case "memoryUtilizationCollect": //内存利用率采集 - if(GlobalConfig.memoryUtilizationCollect != collect || GlobalConfig.memoryUtilizationInterval != interval){ - GlobalConfig.memoryUtilizationCollect = collect; - if(collect){ - GlobalConfig.memoryUtilizationInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::memoryUtilizationTask, milli, GlobalConfig.memoryUtilizationInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.memoryUtilizationInterval = 300; - } - } - break; - case "systemSwapSizePercentCollect": //可用交换空间百分比采集 - if(GlobalConfig.systemSwapSizePercentCollect != collect || GlobalConfig.systemSwapSizePercentInterval != interval){ - GlobalConfig.systemSwapSizePercentCollect = collect; - if(collect){ - GlobalConfig.systemSwapSizePercentInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::systemSwapSizePercentTask, milli, GlobalConfig.systemSwapSizePercentInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.systemSwapSizePercentInterval = 300; - } - } - break; - case "memorySizeAvailableCollect": //可用内存采集 - if(GlobalConfig.memorySizeAvailableCollect != collect || GlobalConfig.memorySizeAvailableInterval != interval){ - GlobalConfig.memorySizeAvailableCollect = collect; - if(collect){ - GlobalConfig.memorySizeAvailableInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::memorySizeAvailableTask, milli, GlobalConfig.memorySizeAvailableInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.memorySizeAvailableInterval = 300; - } - } - break; - case "memorySizePercentCollect": //可用内存百分比采集 - if(GlobalConfig.memorySizePercentCollect != collect || GlobalConfig.memorySizePercentInterval != interval){ - GlobalConfig.memorySizePercentCollect = collect; - if(collect){ - GlobalConfig.memorySizePercentInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::memorySizePercentTask, milli, GlobalConfig.memorySizePercentInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.memorySizePercentInterval = 300; - } - } - break; - case "memorySizeTotalCollect": //总内存采集 - if(GlobalConfig.memorySizeTotalCollect != collect || GlobalConfig.memorySizeTotalInterval != interval){ - GlobalConfig.memorySizeTotalCollect = collect; - if(collect){ - GlobalConfig.memorySizeTotalInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::memorySizeTotalTask, milli, GlobalConfig.memorySizeTotalInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.memorySizeTotalInterval = 300; - } - } - break; - case "systemSwOsCollect": //操作系统采集 - if(GlobalConfig.systemSwOsCollect != collect || GlobalConfig.systemSwOsInterval != interval){ - GlobalConfig.systemSwOsCollect = collect; - if(collect){ - GlobalConfig.systemSwOsInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::systemSwOsTask, milli, GlobalConfig.systemSwOsInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.systemSwOsInterval = 300; - } - } - break; - case "systemSwArchCollect": //操作系统架构采集 - if(GlobalConfig.systemSwArchCollect != collect || GlobalConfig.systemSwArchInterval != interval){ - GlobalConfig.systemSwArchCollect = collect; - if(collect){ - GlobalConfig.systemSwArchInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::systemSwArchTask, milli, GlobalConfig.systemSwArchInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.systemSwArchInterval = 300; - } - } - break; - case "kernelMaxprocCollect": //最大进程数采集 - if(GlobalConfig.kernelMaxprocCollect != collect || GlobalConfig.kernelMaxprocInterval != interval){ - GlobalConfig.kernelMaxprocCollect = collect; - if(collect){ - GlobalConfig.kernelMaxprocInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::kernelMaxprocTask, milli, GlobalConfig.kernelMaxprocInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.kernelMaxprocInterval = 300; - } - } - break; - case "procNumRunCollect": //正在运行的进程数采集 - if(GlobalConfig.procNumRunCollect != collect || GlobalConfig.procNumRunInterval != interval){ - GlobalConfig.procNumRunCollect = collect; - if(collect){ - GlobalConfig.procNumRunInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::procNumRunTask, milli, GlobalConfig.procNumRunInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.procNumRunInterval = 300; - } - } - break; - case "systemUsersNumCollect": //登录用户数采集 - if(GlobalConfig.systemUsersNumCollect != collect || GlobalConfig.systemUsersNumInterval != interval){ - GlobalConfig.systemUsersNumCollect = collect; - if(collect){ - GlobalConfig.systemUsersNumInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::systemUsersNumTask, milli, GlobalConfig.systemUsersNumInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.systemUsersNumInterval = 300; - } - } - break; - case "systemDiskSizeTotalCollect": //硬盘总可用空间采集 - if(GlobalConfig.systemDiskSizeTotalCollect != collect || GlobalConfig.systemDiskSizeTotalInterval != interval){ - GlobalConfig.systemDiskSizeTotalCollect = collect; - if(collect){ - GlobalConfig.systemDiskSizeTotalInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::systemDiskSizeTotalTask, milli, GlobalConfig.systemDiskSizeTotalInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.systemDiskSizeTotalInterval = 300; - } - } - break; - case "systemBoottimeCollect": //系统启动时间采集 - if(GlobalConfig.systemBoottimeCollect != collect || GlobalConfig.systemBoottimeInterval != interval){ - GlobalConfig.systemBoottimeCollect = collect; - if(collect){ - GlobalConfig.systemBoottimeInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::systemBoottimeTask, milli, GlobalConfig.systemBoottimeInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.systemBoottimeInterval = 300; - } - } - break; - case "systemUnameCollect": //系统描述采集 - if(GlobalConfig.systemUnameCollect != collect || GlobalConfig.systemUnameInterval != interval){ - GlobalConfig.systemUnameCollect = collect; - if(collect){ - GlobalConfig.systemUnameInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::systemUnameTask, milli, GlobalConfig.systemUnameInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.systemUnameInterval = 300; - } - } - break; - case "systemLocaltimeCollect": //系统本地时间采集 - if(GlobalConfig.systemLocaltimeCollect != collect || GlobalConfig.systemLocaltimeInterval != interval){ - GlobalConfig.systemLocaltimeCollect = collect; - if(collect){ - GlobalConfig.systemLocaltimeInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::systemLocaltimeTask, milli, GlobalConfig.systemLocaltimeInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.systemLocaltimeInterval = 300; - } - } - break; - case "systemUptimeCollect": //系统正常运行时间采集 - if(GlobalConfig.systemUptimeCollect != collect || GlobalConfig.systemUptimeInterval != interval){ - GlobalConfig.systemUptimeCollect = collect; - if(collect){ - GlobalConfig.systemUptimeInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::systemUptimeTask, milli, GlobalConfig.systemUptimeInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.systemUptimeInterval = 300; - } - } - break; - default: //系统其他采集 - GlobalConfig.procNumCollect = collect; - if(collect){ - GlobalConfig.procNumInterval = interval; - long milli = AgentUtil.getMillisToNextMinute() + 60000; - dynamicTaskService.scheduleTask(type, - businessTasks::procNumTask, milli, GlobalConfig.procNumInterval*1000L); - }else{ - dynamicTaskService.cancelTask(type); - GlobalConfig.procNumInterval = 300; - } + } } } list.add("]"); @@ -1032,6 +1039,369 @@ public class AgentServiceImpl implements AgentService { return result; } + @Override + public void checkMonitor() { + String filePath = properties.getConfPath()+"/monitor.conf"; + try { + Path path = Paths.get(filePath); + // 判断文件是否存在 + if (!Files.exists(path)) { + System.err.println("文件不存在: " + filePath); + }else { + List lines = Files.readAllLines(path, StandardCharsets.UTF_8) + .stream() + .filter(line -> !line.trim().isEmpty()) // 去除空行(包括只有空格的行) + .collect(Collectors.toList()); + List collectList = new ArrayList<>(); + for (String data : lines) { + if(StringUtils.isNotBlank(data)){ + if(data.startsWith("{") && data.endsWith("}")){ + JSONObject object = JSONObject.parseObject(data); + boolean collect = false; + int interval = 300; + String type = ""; + if(object.containsKey("collect")){ + collect = object.getBoolean("collect"); + } + if(object.containsKey("interval")){ + interval = object.getIntValue("collect"); + } + if(object.containsKey("type")){ + type = object.getString("type"); + } + if(StringUtils.isNotBlank(type)){ + collectList.add(CollectEO.builder() + .collect(collect) + .interval(interval) + .type(type).build()); + } + } + + } + } + if(CollectionUtil.isNotEmpty(collectList)){ + for (CollectEO c : collectList) { + String type = c.getType(); + boolean collect = c.isCollect(); + int interval = c.getInterval(); + switch(type) { + case "cpuCollect": //cpu采集 + //判断与上次有无变化 + if(GlobalConfig.cpuCollect != collect || GlobalConfig.cpuInterval != interval){ + GlobalConfig.cpuCollect = collect; + if(collect){ + GlobalConfig.cpuInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::cpuTask, milli, GlobalConfig.cpuInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.cpuInterval = 300; + } + } + break; + case "vfsCollect": //挂载采集 + if(GlobalConfig.vfsCollect != collect || GlobalConfig.vfsInterval != interval){ + GlobalConfig.vfsCollect = collect; + if(collect){ + GlobalConfig.vfsInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::pointTask, milli, GlobalConfig.vfsInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.vfsInterval = 300; + } + } + break; + case "netCollect": //网络采集 + if(GlobalConfig.netCollect != collect || GlobalConfig.netInterval != interval){ + GlobalConfig.netCollect = collect; + if(collect){ +// GlobalConfig.netInterval = interval; + long milli = AgentUtil.millisecondsToNext5Minute(); + dynamicTaskService.scheduleTask(type, + businessTasks::netTask, milli, GlobalConfig.netInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.netInterval = 300; + } + } + break; + case "diskCollect": //磁盘采集 + if(GlobalConfig.diskCollect != collect || GlobalConfig.diskInterval != interval){ + GlobalConfig.diskCollect = collect; + if(collect){ + GlobalConfig.diskInterval = interval; + 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采集 + if(GlobalConfig.dockerCollect != collect || GlobalConfig.dockerInterval != interval){ + GlobalConfig.dockerCollect = collect; + if(collect){ + GlobalConfig.dockerInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::dockerTask, milli, GlobalConfig.dockerInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.dockerInterval = 300; + } + } + break; + case "systemSwapSizeFreeCollect": //交换卷/文件的可用空间(字节)采集 + if(GlobalConfig.systemSwapSizeFreeCollect != collect || GlobalConfig.systemSwapSizeFreeInterval != interval){ + GlobalConfig.systemSwapSizeFreeCollect = collect; + if(collect){ + GlobalConfig.systemSwapSizeFreeInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemSwapSizeFreeTask, milli, GlobalConfig.systemSwapSizeFreeInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemSwapSizeFreeInterval = 300; + } + } + break; + case "memoryUtilizationCollect": //内存利用率采集 + if(GlobalConfig.memoryUtilizationCollect != collect || GlobalConfig.memoryUtilizationInterval != interval){ + GlobalConfig.memoryUtilizationCollect = collect; + if(collect){ + GlobalConfig.memoryUtilizationInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::memoryUtilizationTask, milli, GlobalConfig.memoryUtilizationInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.memoryUtilizationInterval = 300; + } + } + break; + case "systemSwapSizePercentCollect": //可用交换空间百分比采集 + if(GlobalConfig.systemSwapSizePercentCollect != collect || GlobalConfig.systemSwapSizePercentInterval != interval){ + GlobalConfig.systemSwapSizePercentCollect = collect; + if(collect){ + GlobalConfig.systemSwapSizePercentInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemSwapSizePercentTask, milli, GlobalConfig.systemSwapSizePercentInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemSwapSizePercentInterval = 300; + } + } + break; + case "memorySizeAvailableCollect": //可用内存采集 + if(GlobalConfig.memorySizeAvailableCollect != collect || GlobalConfig.memorySizeAvailableInterval != interval){ + GlobalConfig.memorySizeAvailableCollect = collect; + if(collect){ + GlobalConfig.memorySizeAvailableInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::memorySizeAvailableTask, milli, GlobalConfig.memorySizeAvailableInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.memorySizeAvailableInterval = 300; + } + } + break; + case "memorySizePercentCollect": //可用内存百分比采集 + if(GlobalConfig.memorySizePercentCollect != collect || GlobalConfig.memorySizePercentInterval != interval){ + GlobalConfig.memorySizePercentCollect = collect; + if(collect){ + GlobalConfig.memorySizePercentInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::memorySizePercentTask, milli, GlobalConfig.memorySizePercentInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.memorySizePercentInterval = 300; + } + } + break; + case "memorySizeTotalCollect": //总内存采集 + if(GlobalConfig.memorySizeTotalCollect != collect || GlobalConfig.memorySizeTotalInterval != interval){ + GlobalConfig.memorySizeTotalCollect = collect; + if(collect){ + GlobalConfig.memorySizeTotalInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::memorySizeTotalTask, milli, GlobalConfig.memorySizeTotalInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.memorySizeTotalInterval = 300; + } + } + break; + case "systemSwOsCollect": //操作系统采集 + if(GlobalConfig.systemSwOsCollect != collect || GlobalConfig.systemSwOsInterval != interval){ + GlobalConfig.systemSwOsCollect = collect; + if(collect){ + GlobalConfig.systemSwOsInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemSwOsTask, milli, GlobalConfig.systemSwOsInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemSwOsInterval = 300; + } + } + break; + case "systemSwArchCollect": //操作系统架构采集 + if(GlobalConfig.systemSwArchCollect != collect || GlobalConfig.systemSwArchInterval != interval){ + GlobalConfig.systemSwArchCollect = collect; + if(collect){ + GlobalConfig.systemSwArchInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemSwArchTask, milli, GlobalConfig.systemSwArchInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemSwArchInterval = 300; + } + } + break; + case "kernelMaxprocCollect": //最大进程数采集 + if(GlobalConfig.kernelMaxprocCollect != collect || GlobalConfig.kernelMaxprocInterval != interval){ + GlobalConfig.kernelMaxprocCollect = collect; + if(collect){ + GlobalConfig.kernelMaxprocInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::kernelMaxprocTask, milli, GlobalConfig.kernelMaxprocInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.kernelMaxprocInterval = 300; + } + } + break; + case "procNumRunCollect": //正在运行的进程数采集 + if(GlobalConfig.procNumRunCollect != collect || GlobalConfig.procNumRunInterval != interval){ + GlobalConfig.procNumRunCollect = collect; + if(collect){ + GlobalConfig.procNumRunInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::procNumRunTask, milli, GlobalConfig.procNumRunInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.procNumRunInterval = 300; + } + } + break; + case "systemUsersNumCollect": //登录用户数采集 + if(GlobalConfig.systemUsersNumCollect != collect || GlobalConfig.systemUsersNumInterval != interval){ + GlobalConfig.systemUsersNumCollect = collect; + if(collect){ + GlobalConfig.systemUsersNumInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemUsersNumTask, milli, GlobalConfig.systemUsersNumInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemUsersNumInterval = 300; + } + } + break; + case "systemDiskSizeTotalCollect": //硬盘总可用空间采集 + if(GlobalConfig.systemDiskSizeTotalCollect != collect || GlobalConfig.systemDiskSizeTotalInterval != interval){ + GlobalConfig.systemDiskSizeTotalCollect = collect; + if(collect){ + GlobalConfig.systemDiskSizeTotalInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemDiskSizeTotalTask, milli, GlobalConfig.systemDiskSizeTotalInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemDiskSizeTotalInterval = 300; + } + } + break; + case "systemBoottimeCollect": //系统启动时间采集 + if(GlobalConfig.systemBoottimeCollect != collect || GlobalConfig.systemBoottimeInterval != interval){ + GlobalConfig.systemBoottimeCollect = collect; + if(collect){ + GlobalConfig.systemBoottimeInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemBoottimeTask, milli, GlobalConfig.systemBoottimeInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemBoottimeInterval = 300; + } + } + break; + case "systemUnameCollect": //系统描述采集 + if(GlobalConfig.systemUnameCollect != collect || GlobalConfig.systemUnameInterval != interval){ + GlobalConfig.systemUnameCollect = collect; + if(collect){ + GlobalConfig.systemUnameInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemUnameTask, milli, GlobalConfig.systemUnameInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemUnameInterval = 300; + } + } + break; + case "systemLocaltimeCollect": //系统本地时间采集 + if(GlobalConfig.systemLocaltimeCollect != collect || GlobalConfig.systemLocaltimeInterval != interval){ + GlobalConfig.systemLocaltimeCollect = collect; + if(collect){ + GlobalConfig.systemLocaltimeInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemLocaltimeTask, milli, GlobalConfig.systemLocaltimeInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemLocaltimeInterval = 300; + } + } + break; + case "systemUptimeCollect": //系统正常运行时间采集 + if(GlobalConfig.systemUptimeCollect != collect || GlobalConfig.systemUptimeInterval != interval){ + GlobalConfig.systemUptimeCollect = collect; + if(collect){ + GlobalConfig.systemUptimeInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::systemUptimeTask, milli, GlobalConfig.systemUptimeInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.systemUptimeInterval = 300; + } + } + break; + default: //系统其他采集 + GlobalConfig.procNumCollect = collect; + if(collect){ + GlobalConfig.procNumInterval = interval; + long milli = AgentUtil.getMillisToNextMinute() + 60000; + dynamicTaskService.scheduleTask(type, + businessTasks::procNumTask, milli, GlobalConfig.procNumInterval*1000L); + }else{ + dynamicTaskService.cancelTask(type); + GlobalConfig.procNumInterval = 300; + } + } + } + } + } + } catch (Exception e) { + System.err.println("读取文件失败: " + e.getMessage()); + e.printStackTrace(); + } + + } + public void caseTypeBySystem(String type, int interval, boolean collect){ diff --git a/src/main/java/com/tongran/agent/client/service/impl/CPUServiceImpl.java b/src/main/java/com/tongran/agent/client/service/impl/CPUServiceImpl.java index af9b602..e79fc65 100644 --- a/src/main/java/com/tongran/agent/client/service/impl/CPUServiceImpl.java +++ b/src/main/java/com/tongran/agent/client/service/impl/CPUServiceImpl.java @@ -2,6 +2,7 @@ package com.tongran.agent.client.service.impl; import com.tongran.agent.client.core.vo.CpuVO; import com.tongran.agent.client.service.CPUService; +import com.tongran.agent.client.utils.AgentUtil; import org.springframework.stereotype.Service; import oshi.SystemInfo; @@ -41,8 +42,12 @@ public class CPUServiceImpl implements CPUService { System.out.println("15分钟平均负载: " + loadAverage[2]); // 3. CPU使用率(需要两次采样) System.out.println("\n=== CPU使用率 ==="); + // 预热:第一次调用可能不准 + processor.getSystemCpuLoadBetweenTicks(processor.getSystemCpuLoadTicks()); + TimeUnit.SECONDS.sleep(1); + // 正式采样 long[] prevTicks = processor.getSystemCpuLoadTicks(); - TimeUnit.SECONDS.sleep(1); // 等待1秒 + TimeUnit.SECONDS.sleep(2); // 更长间隔,减少抖动 // 计算使用率 double cpuUsage = processor.getSystemCpuLoadBetweenTicks(prevTicks); cpuVO.setUti(cpuUsage * 100); // CPU使用率 @@ -68,27 +73,25 @@ public class CPUServiceImpl implements CPUService { // 4. CPU时间累计值 System.out.println("\n=== CPU时间累计值 ==="); long[] allTicks = processor.getSystemCpuLoadTicks(); - System.out.println("中断累计时间: " + - (allTicks[CentralProcessor.TickType.IRQ.getIndex()] + - allTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()])); - System.out.println("空闲累计时间: " + allTicks[CentralProcessor.TickType.IDLE.getIndex()]); - System.out.printf("I/O等待时间(CPU等待响应时间): %.2f%%\n", 100d * iowait / total); - System.out.println("系统累计时间: " + allTicks[CentralProcessor.TickType.SYSTEM.getIndex()]); + System.out.println("中断累计时间: " + AgentUtil.ticksToSeconds((allTicks[CentralProcessor.TickType.IRQ.getIndex()] + + allTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()]))); + System.out.println("空闲累计时间: " + AgentUtil.ticksToSeconds(allTicks[CentralProcessor.TickType.IDLE.getIndex()])); + System.out.println("I/O等待时间(CPU等待响应时间): " + AgentUtil.ticksToSeconds(iowait)); + System.out.println("系统累计时间: " + AgentUtil.ticksToSeconds(allTicks[CentralProcessor.TickType.SYSTEM.getIndex()])); long softwareTime = sys - irq - softirq; - System.out.printf("软件相关时间(近似无响应时间): %.2f%%\n", 100d * softwareTime / total); - System.out.println("用户进程累计时间: " + allTicks[CentralProcessor.TickType.USER.getIndex()]); - - cpuVO.setInterrupt((allTicks[CentralProcessor.TickType.IRQ.getIndex()] + - allTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()])); // CPU硬件中断提供服务时间 - cpuVO.setIdle(allTicks[CentralProcessor.TickType.IDLE.getIndex()]);// CPU空闲时间 - cpuVO.setIowait(100d * iowait / total);// CPU等待响应时间 - cpuVO.setSystem(allTicks[CentralProcessor.TickType.SYSTEM.getIndex()]);// CPU系统时间 - cpuVO.setNoresp(100d * softwareTime / total);// CPU软件无响应时间 - cpuVO.setUser(allTicks[CentralProcessor.TickType.USER.getIndex()]);// CPU用户进程所花费的时间 + System.out.println("软件相关时间(近似无响应时间): " + AgentUtil.ticksToSeconds(softwareTime)); + System.out.println("用户进程累计时间: " + AgentUtil.ticksToSeconds(allTicks[CentralProcessor.TickType.USER.getIndex()])); + cpuVO.setInterrupt(AgentUtil.ticksToSeconds((allTicks[CentralProcessor.TickType.IRQ.getIndex()] + + allTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()]))); // CPU硬件中断提供服务时间:秒 + cpuVO.setIdle(AgentUtil.ticksToSeconds(allTicks[CentralProcessor.TickType.IDLE.getIndex()]));// CPU空闲时间:秒 + cpuVO.setIowait(AgentUtil.ticksToSeconds(iowait));// CPU等待响应时间:秒 + cpuVO.setSystem(AgentUtil.ticksToSeconds(allTicks[CentralProcessor.TickType.SYSTEM.getIndex()]));// CPU系统时间:秒 + cpuVO.setNoresp(AgentUtil.ticksToSeconds(softwareTime));// CPU软件无响应时间:秒 + cpuVO.setUser(AgentUtil.ticksToSeconds(allTicks[CentralProcessor.TickType.USER.getIndex()]));// CPU用户进程所花费的时间:秒 // 5. 系统运行时间和CPU空闲时间 long uptime = os.getSystemUptime(); - cpuVO.setNormal(uptime);// CPU正常运行时间 + cpuVO.setNormal(uptime);// CPU正常运行时间:秒 System.out.println("CPU正常运行时间: " + cpuVO.getNormal()); } catch (InterruptedException e) { e.printStackTrace(); diff --git a/src/main/java/com/tongran/agent/client/utils/AgentUtil.java b/src/main/java/com/tongran/agent/client/utils/AgentUtil.java index f3b4216..14aaaf9 100644 --- a/src/main/java/com/tongran/agent/client/utils/AgentUtil.java +++ b/src/main/java/com/tongran/agent/client/utils/AgentUtil.java @@ -631,5 +631,49 @@ public class AgentUtil { // } } + /** + * 获取系统 HZ(每秒 tick 数) + * 典型值:100, 250, 300, 1000 + */ + public static int getSystemHz() { + if (GlobalConfig.systemHz != null) { + return GlobalConfig.systemHz; + } + try { + Process process = Runtime.getRuntime().exec( + "grep CONFIG_HZ /boot/config-$(uname -r)" + ); + BufferedReader reader = new BufferedReader( + new InputStreamReader(process.getInputStream()) + ); + + String line; + while ((line = reader.readLine()) != null) { + if (line.contains("CONFIG_HZ=")) { + String[] parts = line.split("="); + int hz = Integer.parseInt(parts[1].trim()); + GlobalConfig.systemHz = hz; + return hz; + } + } + reader.close(); + } catch (Exception e) { + System.err.println("无法读取系统 HZ,使用默认值 1000"); + } + + // 默认值(大多数现代 Linux 发行版使用 1000 Hz) + GlobalConfig.systemHz = 1000; + return GlobalConfig.systemHz; + } + + /** + * 将 ticks 转换为秒 + */ + public static double ticksToSeconds(long ticks) { + int hz = getSystemHz(); + return (double) ticks / hz; + } + + }