|
|
@@ -0,0 +1,106 @@
|
|
|
+package org.ruoyi.chat.service;
|
|
|
+
|
|
|
+import lombok.Data;
|
|
|
+import oshi.SystemInfo;
|
|
|
+import oshi.hardware.CentralProcessor;
|
|
|
+import oshi.hardware.GlobalMemory;
|
|
|
+import oshi.hardware.HardwareAbstractionLayer;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class HardwareInfoService {
|
|
|
+
|
|
|
+ private final SystemInfo systemInfo = new SystemInfo();
|
|
|
+ private final HardwareAbstractionLayer hal = systemInfo.getHardware();
|
|
|
+ private long[] prevTicks = null;
|
|
|
+
|
|
|
+ public HardwareData getHardwareData() {
|
|
|
+ CentralProcessor processor = hal.getProcessor();
|
|
|
+ GlobalMemory memory = hal.getMemory();
|
|
|
+
|
|
|
+ // CPU 使用率(准确版)
|
|
|
+ double cpuLoad = getCpuLoad(processor);
|
|
|
+
|
|
|
+ // 内存信息
|
|
|
+ long totalMemory = memory.getTotal();
|
|
|
+ long availableMemory = memory.getAvailable();
|
|
|
+
|
|
|
+ // 磁盘信息
|
|
|
+ List<DiskData> disks = systemInfo.getOperatingSystem().getFileSystem().getFileStores().stream()
|
|
|
+ .map(store -> new DiskData(
|
|
|
+ store.getMount(),
|
|
|
+ store.getTotalSpace(),
|
|
|
+ store.getUsableSpace()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 网络信息
|
|
|
+ List<NetworkData> networks = hal.getNetworkIFs().stream()
|
|
|
+ .map(net -> new NetworkData(
|
|
|
+ net.getName(),
|
|
|
+ net.getBytesRecv(),
|
|
|
+ net.getBytesSent()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ return new HardwareData(cpuLoad, totalMemory, availableMemory, disks, networks);
|
|
|
+ }
|
|
|
+
|
|
|
+ public double getCpuLoad(CentralProcessor processor) {
|
|
|
+ if (prevTicks == null) {
|
|
|
+ prevTicks = processor.getSystemCpuLoadTicks();
|
|
|
+ return 0; // 第一次调用无法计算,返回0或其他默认值
|
|
|
+ }
|
|
|
+ long[] ticks = processor.getSystemCpuLoadTicks();
|
|
|
+ double load = processor.getSystemCpuLoadBetweenTicks(prevTicks);
|
|
|
+ prevTicks = ticks;
|
|
|
+ return load * 100;
|
|
|
+ }
|
|
|
+
|
|
|
+ // DTO 类
|
|
|
+ @Data
|
|
|
+ public static class HardwareData {
|
|
|
+ private double cpuUsage; // %
|
|
|
+ private long totalMemory; // bytes
|
|
|
+ private long availableMemory; // bytes
|
|
|
+ private List<DiskData> disks;
|
|
|
+ private List<NetworkData> networks;
|
|
|
+
|
|
|
+
|
|
|
+ public HardwareData(double cpuUsage, long totalMemory, long availableMemory, List<DiskData> disks, List<NetworkData> networks) {
|
|
|
+ this.cpuUsage = cpuUsage;
|
|
|
+ this.totalMemory = totalMemory;
|
|
|
+ this.availableMemory = availableMemory;
|
|
|
+ this.disks = disks;
|
|
|
+ this.networks = networks;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ @Data
|
|
|
+ public static class DiskData {
|
|
|
+ private String mountPoint;
|
|
|
+ private long totalSpace;
|
|
|
+ private long usableSpace;
|
|
|
+
|
|
|
+ public DiskData(String mountPoint, long totalSpace, long usableSpace) {
|
|
|
+ this.mountPoint = mountPoint;
|
|
|
+ this.totalSpace = totalSpace;
|
|
|
+ this.usableSpace = usableSpace;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ @Data
|
|
|
+ public static class NetworkData {
|
|
|
+ private String name;
|
|
|
+ private long bytesReceived;
|
|
|
+ private long bytesSent;
|
|
|
+
|
|
|
+ public NetworkData(String name, long bytesReceived, long bytesSent) {
|
|
|
+ this.name = name;
|
|
|
+ this.bytesReceived = bytesReceived;
|
|
|
+ this.bytesSent = bytesSent;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|