From 4af3371b2f7773f13e26199871c3b4ec008f17b6 Mon Sep 17 00:00:00 2001
From: whycxzp <perryhsu@163.com>
Date: 星期三, 24 五月 2023 16:52:54 +0800
Subject: [PATCH] 更新cpu获取

---
 src/main/java/com/whyc/service/ServerStateService.java |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/whyc/service/ServerStateService.java b/src/main/java/com/whyc/service/ServerStateService.java
index 7ba3f90..9fc7ff8 100644
--- a/src/main/java/com/whyc/service/ServerStateService.java
+++ b/src/main/java/com/whyc/service/ServerStateService.java
@@ -1,10 +1,15 @@
 package com.whyc.service;
 
+import cn.hutool.system.oshi.CpuInfo;
+import cn.hutool.system.oshi.OshiUtil;
 import com.sun.management.OperatingSystemMXBean;
 import com.whyc.dto.ShowDTO;
 import com.whyc.mapper.ServerStateMapper;
 import com.whyc.pojo.ServerState;
 import org.springframework.stereotype.Service;
+import oshi.hardware.CentralProcessor;
+import oshi.hardware.HardwareAbstractionLayer;
+import oshi.util.Util;
 
 import javax.annotation.Resource;
 import java.io.File;
@@ -71,8 +76,12 @@
         int connRate = (int) (connDivide*100);
 
         //cpu浣跨敤鐜�
-        double cpuLoad = osmb.getSystemCpuLoad();
-        int cpuLoadRate = (int)(BigDecimal.valueOf(cpuLoad).setScale(2,RoundingMode.CEILING).doubleValue()*100);
+        //double cpuLoad = osmb.getSystemCpuLoad();
+        CpuInfo cpuInfo = OshiUtil.getCpuInfo();
+        double free = cpuInfo.getFree();
+        double cpuLoad = 100-free;
+        long cpuLoadRate = Math.round(cpuLoad);
+        //int cpuLoadRate = (int)(BigDecimal.valueOf(cpuLoad).setScale(2,RoundingMode.CEILING).doubleValue()*100);
 
         state.setFreeMen(freeMemorySize);
         state.setTotalMem(totalMemorySize);

--
Gitblit v1.9.1