Browse Source

硬件信息监控功能

zhaohan 9 months ago
parent
commit
a40ebb6ae0

+ 6 - 0
ruoyi-modules/ruoyi-chat/pom.xml

@@ -113,6 +113,12 @@
             <artifactId>ruoyi-chat-api</artifactId>
             <artifactId>ruoyi-chat-api</artifactId>
         </dependency>
         </dependency>
 
 
+        <dependency>
+            <groupId>com.github.oshi</groupId>
+            <artifactId>oshi-core</artifactId>
+            <version>6.4.0</version>
+        </dependency>
+
         <dependency>
         <dependency>
             <groupId>org.ruoyi</groupId>
             <groupId>org.ruoyi</groupId>
             <artifactId>ruoyi-system-api</artifactId>
             <artifactId>ruoyi-system-api</artifactId>

+ 43 - 0
ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/controller/chat/HardwareInfoController.java

@@ -0,0 +1,43 @@
+package org.ruoyi.chat.controller.chat;
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.ruoyi.chat.service.HardwareInfoService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@RestController
+@Slf4j
+@RequiredArgsConstructor
+@RequestMapping("/sys")
+public class HardwareInfoController {
+    @Autowired
+    private HardwareInfoService hardwareInfoService;
+
+    public HardwareInfoController(HardwareInfoService hardwareInfoService) {
+        this.hardwareInfoService = hardwareInfoService;
+    }
+
+    @GetMapping("/hardwareInfo")
+    @ResponseBody
+    public Map<String, Object> getHardwareInfo() {
+        HardwareInfoService.HardwareData data = hardwareInfoService.getHardwareData();
+
+        List<HardwareInfoService.HardwareData> list = Collections.singletonList(data);
+
+        Map<String, Object> response = new HashMap<>();
+        response.put("code", 200);
+        response.put("msg", "查询成功");
+        response.put("rows", list);
+
+        return response;
+    }
+}

+ 106 - 0
ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/HardwareInfoService.java

@@ -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;
+        }
+
+    }
+}