From ea85a620d157bc882e38479c38a30243b6059f40 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期五, 04 二月 2022 21:46:48 +0800
Subject: [PATCH] 添加服务器信息获取能力。

---
 src/main/java/com/genersoft/iot/vmp/common/SystemInfoDto.java                |   22 +++++
 src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java |   46 +++++++++++
 src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java         |    6 +
 src/main/java/com/genersoft/iot/vmp/conf/SystemInfoTimerTask.java            |   34 ++++++++
 src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java               |   18 ++++
 pom.xml                                                                      |    8 +
 src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java        |    8 +
 src/main/java/com/genersoft/iot/vmp/utils/SystemInfoUtils.java               |   92 +++++++++++++++++++++++
 8 files changed, 232 insertions(+), 2 deletions(-)

diff --git a/pom.xml b/pom.xml
index 987e76e..abf4e9c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -155,7 +155,6 @@
 			<version>1.7.35</version>
 		</dependency>
 
-
 		<!-- xml瑙f瀽搴� -->
 		<dependency>
 			<groupId>org.dom4j</groupId>
@@ -212,6 +211,13 @@
 			<version>3.0.4</version>
 		</dependency>
 
+		<!-- 鑾峰彇绯荤粺淇℃伅 -->
+		<dependency>
+			<groupId>com.github.oshi</groupId>
+			<artifactId>oshi-core</artifactId>
+			<version>6.1.0</version>
+		</dependency>
+
 		<dependency>
 			<groupId>org.springframework.session</groupId>
 			<artifactId>spring-session-core</artifactId>
diff --git a/src/main/java/com/genersoft/iot/vmp/common/SystemInfoDto.java b/src/main/java/com/genersoft/iot/vmp/common/SystemInfoDto.java
new file mode 100644
index 0000000..81a93ac
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/common/SystemInfoDto.java
@@ -0,0 +1,22 @@
+package com.genersoft.iot.vmp.common;
+
+public class SystemInfoDto<T> {
+    private String time;
+    private T data;
+
+    public String getTime() {
+        return time;
+    }
+
+    public void setTime(String time) {
+        this.time = time;
+    }
+
+    public T getData() {
+        return data;
+    }
+
+    public void setData(T data) {
+        this.data = data;
+    }
+}
diff --git a/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java b/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java
index ffbed50..923e411 100644
--- a/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java
+++ b/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java
@@ -60,7 +60,13 @@
 
 	public static final String SIP_SN_PREFIX = "VMP_SIP_SN_";
 
-	public static final String SIP_SUBSCRIBE_PREFIX = "SIP_SUBSCRIBE_";
+	public static final String SIP_SUBSCRIBE_PREFIX = "VMP_SIP_SUBSCRIBE_";
+
+	public static final String SYSTEM_INFO_CPU_PREFIX = "VMP_SYSTEM_INFO_CPU_";
+
+	public static final String SYSTEM_INFO_MEM_PREFIX = "VMP_SYSTEM_INFO_MEM_";
+
+	public static final String SYSTEM_INFO_NET_PREFIX = "VMP_SYSTEM_INFO_NET_";
 
 	//************************** redis 娑堟伅*********************************
 	public static final String WVP_MSG_STREAM_CHANGE_PREFIX = "WVP_MSG_STREAM_CHANGE_";
diff --git a/src/main/java/com/genersoft/iot/vmp/conf/SystemInfoTimerTask.java b/src/main/java/com/genersoft/iot/vmp/conf/SystemInfoTimerTask.java
new file mode 100644
index 0000000..13ec692
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/conf/SystemInfoTimerTask.java
@@ -0,0 +1,34 @@
+package com.genersoft.iot.vmp.conf;
+
+import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
+import com.genersoft.iot.vmp.utils.SystemInfoUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+/**
+ * 鑾峰彇绯荤粺淇℃伅鍐欏叆redis
+ */
+@Component
+public class SystemInfoTimerTask {
+
+    @Autowired
+    private IRedisCatchStorage redisCatchStorage;
+
+    @Scheduled(fixedRate = 1000)   //姣�1绉掓墽琛屼竴娆�
+    public void execute(){
+        try {
+            double cpuInfo = SystemInfoUtils.getCpuInfo();
+            redisCatchStorage.addCpuInfo(cpuInfo);
+            double memInfo = SystemInfoUtils.getMemInfo();
+            redisCatchStorage.addMemInfo(memInfo);
+            Map<String, String> networkInterfaces = SystemInfoUtils.getNetworkInterfaces();
+            redisCatchStorage.addNetInfo(networkInterfaces);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+
+    }
+}
diff --git a/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java b/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
index bf2104c..edd6cbc 100644
--- a/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
+++ b/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
@@ -214,4 +214,10 @@
     List<SubscribeInfo> getAllSubscribe();
 
     List<String> getAllSubscribePlatform();
+
+    void addCpuInfo(double cpuInfo);
+
+    void addMemInfo(double memInfo);
+
+    void addNetInfo(Map<String, String> networkInterfaces);
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
index 56789d9..92fdf6c 100644
--- a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
@@ -2,6 +2,7 @@
 
 import com.alibaba.fastjson.JSONObject;
 import com.genersoft.iot.vmp.common.StreamInfo;
+import com.genersoft.iot.vmp.common.SystemInfoDto;
 import com.genersoft.iot.vmp.common.VideoManagerConstants;
 import com.genersoft.iot.vmp.conf.UserSetup;
 import com.genersoft.iot.vmp.gb28181.bean.*;
@@ -534,4 +535,49 @@
         }
         return result;
     }
+
+    @Override
+    public void addCpuInfo(double cpuInfo) {
+        String key = VideoManagerConstants.SYSTEM_INFO_CPU_PREFIX + userSetup.getServerId();
+        SystemInfoDto<Double> systemInfoDto = new SystemInfoDto<>();
+        systemInfoDto.setTime(format.format(System.currentTimeMillis()));
+        systemInfoDto.setData(cpuInfo);
+        redis.lSet(key, systemInfoDto);
+        // 姣忕涓�涓紝鏈�澶氬彧瀛�30涓�
+        if (redis.lGetListSize(key) > 30) {
+            for (int i = 0; i < redis.lGetListSize(key) - 30; i++) {
+                redis.lLeftPop(key);
+            }
+        }
+    }
+
+    @Override
+    public void addMemInfo(double memInfo) {
+        String key = VideoManagerConstants.SYSTEM_INFO_MEM_PREFIX + userSetup.getServerId();
+        SystemInfoDto<Double> systemInfoDto = new SystemInfoDto<>();
+        systemInfoDto.setTime(format.format(System.currentTimeMillis()));
+        systemInfoDto.setData(memInfo);
+        redis.lSet(key, systemInfoDto);
+        // 姣忕涓�涓紝鏈�澶氬彧瀛�30涓�
+        if (redis.lGetListSize(key) > 30) {
+            for (int i = 0; i < redis.lGetListSize(key) - 30; i++) {
+                redis.lLeftPop(key);
+            }
+        }
+    }
+
+    @Override
+    public void addNetInfo(Map<String, String> networkInterfaces) {
+        String key = VideoManagerConstants.SYSTEM_INFO_NET_PREFIX + userSetup.getServerId();
+        SystemInfoDto<Map<String, String>> systemInfoDto = new SystemInfoDto<>();
+        systemInfoDto.setTime(format.format(System.currentTimeMillis()));
+        systemInfoDto.setData(networkInterfaces);
+        redis.lSet(key, systemInfoDto);
+        // 姣忕涓�涓紝鏈�澶氬彧瀛�30涓�
+        if (redis.lGetListSize(key) > 30) {
+            for (int i = 0; i < redis.lGetListSize(key) - 30; i++) {
+                redis.lLeftPop(key);
+            }
+        }
+    }
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/utils/SystemInfoUtils.java b/src/main/java/com/genersoft/iot/vmp/utils/SystemInfoUtils.java
new file mode 100644
index 0000000..2d588fa
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/utils/SystemInfoUtils.java
@@ -0,0 +1,92 @@
+package com.genersoft.iot.vmp.utils;
+
+import oshi.SystemInfo;
+import oshi.hardware.CentralProcessor;
+import oshi.hardware.GlobalMemory;
+import oshi.hardware.HardwareAbstractionLayer;
+import oshi.hardware.NetworkIF;
+import oshi.software.os.OperatingSystem;
+import oshi.util.FormatUtil;
+
+import java.text.DecimalFormat;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 瀹炵幇鍙傝�冭嚜xiaozhangnomoney鍘熷垱鏂囩珷锛�
+ * 鐗堟潈澹版槑锛氭湰鏂囦负xiaozhangnomoney鍘熷垱鏂囩珷锛岄伒寰� CC 4.0 BY-SA 鐗堟潈鍗忚锛岃浆杞借闄勪笂鍘熸枃鍑哄閾炬帴鍜屾湰澹版槑
+ * 鍘熸枃鍑哄閾炬帴锛歨ttps://blog.csdn.net/xiaozhangnomoney/article/details/107769147
+ */
+public class SystemInfoUtils {
+
+    /**
+     * 鑾峰彇cpu淇℃伅
+     * @return
+     * @throws InterruptedException
+     */
+    public static double getCpuInfo() throws InterruptedException {
+        SystemInfo systemInfo = new SystemInfo();
+        CentralProcessor processor = systemInfo.getHardware().getProcessor();
+        long[] prevTicks = processor.getSystemCpuLoadTicks();
+        // 鐫$湢1s
+        TimeUnit.SECONDS.sleep(1);
+        long[] ticks = processor.getSystemCpuLoadTicks();
+        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
+        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
+        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
+        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
+        long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
+        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
+        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
+        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
+        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
+        return 1.0-(idle * 1.0 / totalCpu);
+    }
+
+    /**
+     * 鑾峰彇鍐呭瓨浣跨敤鐜�
+     * @return
+     */
+    public static double getMemInfo(){
+        SystemInfo systemInfo = new SystemInfo();
+        GlobalMemory memory = systemInfo.getHardware().getMemory();
+        //鎬诲唴瀛�
+        long totalByte = memory.getTotal();
+        //鍓╀綑
+        long acaliableByte = memory.getAvailable();
+        return (totalByte-acaliableByte)*1.0/totalByte;
+    }
+
+    /**
+     * 鑾峰彇缃戠粶涓婁紶鍜屼笅杞�
+     * @return
+     */
+    public static Map<String,String> getNetworkInterfaces() {
+        SystemInfo si = new SystemInfo();
+        HardwareAbstractionLayer hal = si.getHardware();
+        List<NetworkIF> networkIFs = hal.getNetworkIFs();
+        int i= networkIFs.size() -1;
+        NetworkIF net= networkIFs.get(i);
+
+        String in  = FormatUtil.formatBytes(net.getBytesRecv());
+        String out = FormatUtil.formatBytes(net.getBytesSent());
+        HashMap<String, String> map = new HashMap<>();
+        map.put("in",in);
+        map.put("out",out);
+        return map;
+    }
+
+    /**
+     * 鑾峰彇杩涚▼鏁�
+     * @return
+     */
+    public static int getProcessesCount(){
+        SystemInfo si = new SystemInfo();
+        OperatingSystem os = si.getOperatingSystem();
+
+        int processCount = os.getProcessCount();
+        return processCount;
+    }
+}
diff --git a/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java b/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java
index da09851..8ab0672 100644
--- a/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java
+++ b/src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java
@@ -661,6 +661,24 @@
     }
 
     /**
+     * 鍦ㄩ敭涓� key 鐨� list涓Щ闄ょ涓�涓厓绱�
+     * @param key 閿�
+     * @return
+     */
+    public Object lLeftPop(String key) {
+        return redisTemplate.opsForList().leftPop(key);
+    }
+
+    /**
+     * 鍦ㄩ敭涓� key 鐨� list涓Щ闄ゃ�佹渶鍚庝竴涓厓绱�
+     * @param key 閿�
+     * @return
+     */
+    public Object lrightPop(String key) {
+        return redisTemplate.opsForList().rightPop(key);
+    }
+
+    /**
      * 妯$硦鏌ヨ
      * @param key 閿�
      * @return true / false

--
Gitblit v1.8.0