1.启动增加检查监控策略机制;
2.cpu上报数据单位优化
This commit is contained in:
@@ -38,6 +38,11 @@ public class GlobalConfig {
|
||||
*/
|
||||
public static String DEVICE_SN;
|
||||
|
||||
/**
|
||||
* 系统 HZ (每秒tick数)
|
||||
*/
|
||||
public static Integer systemHz = null;
|
||||
|
||||
/**
|
||||
* 逻辑标识
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -28,4 +28,6 @@ public interface AgentService {
|
||||
void dellRoute(String name, String gateway);
|
||||
|
||||
String getLogicalNode();
|
||||
|
||||
void checkMonitor();
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user