Commit ea85a620d157bc882e38479c38a30243b6059f40
1 parent
d7afcab6
添加服务器信息获取能力。
Showing
8 changed files
with
232 additions
and
2 deletions
pom.xml
| ... | ... | @@ -155,7 +155,6 @@ |
| 155 | 155 | <version>1.7.35</version> |
| 156 | 156 | </dependency> |
| 157 | 157 | |
| 158 | - | |
| 159 | 158 | <!-- xml解析库 --> |
| 160 | 159 | <dependency> |
| 161 | 160 | <groupId>org.dom4j</groupId> |
| ... | ... | @@ -212,6 +211,13 @@ |
| 212 | 211 | <version>3.0.4</version> |
| 213 | 212 | </dependency> |
| 214 | 213 | |
| 214 | + <!-- 获取系统信息 --> | |
| 215 | + <dependency> | |
| 216 | + <groupId>com.github.oshi</groupId> | |
| 217 | + <artifactId>oshi-core</artifactId> | |
| 218 | + <version>6.1.0</version> | |
| 219 | + </dependency> | |
| 220 | + | |
| 215 | 221 | <dependency> |
| 216 | 222 | <groupId>org.springframework.session</groupId> |
| 217 | 223 | <artifactId>spring-session-core</artifactId> | ... | ... |
src/main/java/com/genersoft/iot/vmp/common/SystemInfoDto.java
0 → 100644
| 1 | +package com.genersoft.iot.vmp.common; | |
| 2 | + | |
| 3 | +public class SystemInfoDto<T> { | |
| 4 | + private String time; | |
| 5 | + private T data; | |
| 6 | + | |
| 7 | + public String getTime() { | |
| 8 | + return time; | |
| 9 | + } | |
| 10 | + | |
| 11 | + public void setTime(String time) { | |
| 12 | + this.time = time; | |
| 13 | + } | |
| 14 | + | |
| 15 | + public T getData() { | |
| 16 | + return data; | |
| 17 | + } | |
| 18 | + | |
| 19 | + public void setData(T data) { | |
| 20 | + this.data = data; | |
| 21 | + } | |
| 22 | +} | ... | ... |
src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java
| ... | ... | @@ -60,7 +60,13 @@ public class VideoManagerConstants { |
| 60 | 60 | |
| 61 | 61 | public static final String SIP_SN_PREFIX = "VMP_SIP_SN_"; |
| 62 | 62 | |
| 63 | - public static final String SIP_SUBSCRIBE_PREFIX = "SIP_SUBSCRIBE_"; | |
| 63 | + public static final String SIP_SUBSCRIBE_PREFIX = "VMP_SIP_SUBSCRIBE_"; | |
| 64 | + | |
| 65 | + public static final String SYSTEM_INFO_CPU_PREFIX = "VMP_SYSTEM_INFO_CPU_"; | |
| 66 | + | |
| 67 | + public static final String SYSTEM_INFO_MEM_PREFIX = "VMP_SYSTEM_INFO_MEM_"; | |
| 68 | + | |
| 69 | + public static final String SYSTEM_INFO_NET_PREFIX = "VMP_SYSTEM_INFO_NET_"; | |
| 64 | 70 | |
| 65 | 71 | //************************** redis 消息********************************* |
| 66 | 72 | public static final String WVP_MSG_STREAM_CHANGE_PREFIX = "WVP_MSG_STREAM_CHANGE_"; | ... | ... |
src/main/java/com/genersoft/iot/vmp/conf/SystemInfoTimerTask.java
0 → 100644
| 1 | +package com.genersoft.iot.vmp.conf; | |
| 2 | + | |
| 3 | +import com.genersoft.iot.vmp.storager.IRedisCatchStorage; | |
| 4 | +import com.genersoft.iot.vmp.utils.SystemInfoUtils; | |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 6 | +import org.springframework.scheduling.annotation.Scheduled; | |
| 7 | +import org.springframework.stereotype.Component; | |
| 8 | + | |
| 9 | +import java.util.Map; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * 获取系统信息写入redis | |
| 13 | + */ | |
| 14 | +@Component | |
| 15 | +public class SystemInfoTimerTask { | |
| 16 | + | |
| 17 | + @Autowired | |
| 18 | + private IRedisCatchStorage redisCatchStorage; | |
| 19 | + | |
| 20 | + @Scheduled(fixedRate = 1000) //每1秒执行一次 | |
| 21 | + public void execute(){ | |
| 22 | + try { | |
| 23 | + double cpuInfo = SystemInfoUtils.getCpuInfo(); | |
| 24 | + redisCatchStorage.addCpuInfo(cpuInfo); | |
| 25 | + double memInfo = SystemInfoUtils.getMemInfo(); | |
| 26 | + redisCatchStorage.addMemInfo(memInfo); | |
| 27 | + Map<String, String> networkInterfaces = SystemInfoUtils.getNetworkInterfaces(); | |
| 28 | + redisCatchStorage.addNetInfo(networkInterfaces); | |
| 29 | + } catch (InterruptedException e) { | |
| 30 | + e.printStackTrace(); | |
| 31 | + } | |
| 32 | + | |
| 33 | + } | |
| 34 | +} | ... | ... |
src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
| ... | ... | @@ -214,4 +214,10 @@ public interface IRedisCatchStorage { |
| 214 | 214 | List<SubscribeInfo> getAllSubscribe(); |
| 215 | 215 | |
| 216 | 216 | List<String> getAllSubscribePlatform(); |
| 217 | + | |
| 218 | + void addCpuInfo(double cpuInfo); | |
| 219 | + | |
| 220 | + void addMemInfo(double memInfo); | |
| 221 | + | |
| 222 | + void addNetInfo(Map<String, String> networkInterfaces); | |
| 217 | 223 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
| ... | ... | @@ -2,6 +2,7 @@ package com.genersoft.iot.vmp.storager.impl; |
| 2 | 2 | |
| 3 | 3 | import com.alibaba.fastjson.JSONObject; |
| 4 | 4 | import com.genersoft.iot.vmp.common.StreamInfo; |
| 5 | +import com.genersoft.iot.vmp.common.SystemInfoDto; | |
| 5 | 6 | import com.genersoft.iot.vmp.common.VideoManagerConstants; |
| 6 | 7 | import com.genersoft.iot.vmp.conf.UserSetup; |
| 7 | 8 | import com.genersoft.iot.vmp.gb28181.bean.*; |
| ... | ... | @@ -534,4 +535,49 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 534 | 535 | } |
| 535 | 536 | return result; |
| 536 | 537 | } |
| 538 | + | |
| 539 | + @Override | |
| 540 | + public void addCpuInfo(double cpuInfo) { | |
| 541 | + String key = VideoManagerConstants.SYSTEM_INFO_CPU_PREFIX + userSetup.getServerId(); | |
| 542 | + SystemInfoDto<Double> systemInfoDto = new SystemInfoDto<>(); | |
| 543 | + systemInfoDto.setTime(format.format(System.currentTimeMillis())); | |
| 544 | + systemInfoDto.setData(cpuInfo); | |
| 545 | + redis.lSet(key, systemInfoDto); | |
| 546 | + // 每秒一个,最多只存30个 | |
| 547 | + if (redis.lGetListSize(key) > 30) { | |
| 548 | + for (int i = 0; i < redis.lGetListSize(key) - 30; i++) { | |
| 549 | + redis.lLeftPop(key); | |
| 550 | + } | |
| 551 | + } | |
| 552 | + } | |
| 553 | + | |
| 554 | + @Override | |
| 555 | + public void addMemInfo(double memInfo) { | |
| 556 | + String key = VideoManagerConstants.SYSTEM_INFO_MEM_PREFIX + userSetup.getServerId(); | |
| 557 | + SystemInfoDto<Double> systemInfoDto = new SystemInfoDto<>(); | |
| 558 | + systemInfoDto.setTime(format.format(System.currentTimeMillis())); | |
| 559 | + systemInfoDto.setData(memInfo); | |
| 560 | + redis.lSet(key, systemInfoDto); | |
| 561 | + // 每秒一个,最多只存30个 | |
| 562 | + if (redis.lGetListSize(key) > 30) { | |
| 563 | + for (int i = 0; i < redis.lGetListSize(key) - 30; i++) { | |
| 564 | + redis.lLeftPop(key); | |
| 565 | + } | |
| 566 | + } | |
| 567 | + } | |
| 568 | + | |
| 569 | + @Override | |
| 570 | + public void addNetInfo(Map<String, String> networkInterfaces) { | |
| 571 | + String key = VideoManagerConstants.SYSTEM_INFO_NET_PREFIX + userSetup.getServerId(); | |
| 572 | + SystemInfoDto<Map<String, String>> systemInfoDto = new SystemInfoDto<>(); | |
| 573 | + systemInfoDto.setTime(format.format(System.currentTimeMillis())); | |
| 574 | + systemInfoDto.setData(networkInterfaces); | |
| 575 | + redis.lSet(key, systemInfoDto); | |
| 576 | + // 每秒一个,最多只存30个 | |
| 577 | + if (redis.lGetListSize(key) > 30) { | |
| 578 | + for (int i = 0; i < redis.lGetListSize(key) - 30; i++) { | |
| 579 | + redis.lLeftPop(key); | |
| 580 | + } | |
| 581 | + } | |
| 582 | + } | |
| 537 | 583 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/utils/SystemInfoUtils.java
0 → 100644
| 1 | +package com.genersoft.iot.vmp.utils; | |
| 2 | + | |
| 3 | +import oshi.SystemInfo; | |
| 4 | +import oshi.hardware.CentralProcessor; | |
| 5 | +import oshi.hardware.GlobalMemory; | |
| 6 | +import oshi.hardware.HardwareAbstractionLayer; | |
| 7 | +import oshi.hardware.NetworkIF; | |
| 8 | +import oshi.software.os.OperatingSystem; | |
| 9 | +import oshi.util.FormatUtil; | |
| 10 | + | |
| 11 | +import java.text.DecimalFormat; | |
| 12 | +import java.util.HashMap; | |
| 13 | +import java.util.List; | |
| 14 | +import java.util.Map; | |
| 15 | +import java.util.concurrent.TimeUnit; | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * 实现参考自xiaozhangnomoney原创文章, | |
| 19 | + * 版权声明:本文为xiaozhangnomoney原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明 | |
| 20 | + * 原文出处链接:https://blog.csdn.net/xiaozhangnomoney/article/details/107769147 | |
| 21 | + */ | |
| 22 | +public class SystemInfoUtils { | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * 获取cpu信息 | |
| 26 | + * @return | |
| 27 | + * @throws InterruptedException | |
| 28 | + */ | |
| 29 | + public static double getCpuInfo() throws InterruptedException { | |
| 30 | + SystemInfo systemInfo = new SystemInfo(); | |
| 31 | + CentralProcessor processor = systemInfo.getHardware().getProcessor(); | |
| 32 | + long[] prevTicks = processor.getSystemCpuLoadTicks(); | |
| 33 | + // 睡眠1s | |
| 34 | + TimeUnit.SECONDS.sleep(1); | |
| 35 | + long[] ticks = processor.getSystemCpuLoadTicks(); | |
| 36 | + long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()]; | |
| 37 | + long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()]; | |
| 38 | + long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()]; | |
| 39 | + long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()]; | |
| 40 | + long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()]; | |
| 41 | + long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()]; | |
| 42 | + long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()]; | |
| 43 | + long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()]; | |
| 44 | + long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal; | |
| 45 | + return 1.0-(idle * 1.0 / totalCpu); | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * 获取内存使用率 | |
| 50 | + * @return | |
| 51 | + */ | |
| 52 | + public static double getMemInfo(){ | |
| 53 | + SystemInfo systemInfo = new SystemInfo(); | |
| 54 | + GlobalMemory memory = systemInfo.getHardware().getMemory(); | |
| 55 | + //总内存 | |
| 56 | + long totalByte = memory.getTotal(); | |
| 57 | + //剩余 | |
| 58 | + long acaliableByte = memory.getAvailable(); | |
| 59 | + return (totalByte-acaliableByte)*1.0/totalByte; | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * 获取网络上传和下载 | |
| 64 | + * @return | |
| 65 | + */ | |
| 66 | + public static Map<String,String> getNetworkInterfaces() { | |
| 67 | + SystemInfo si = new SystemInfo(); | |
| 68 | + HardwareAbstractionLayer hal = si.getHardware(); | |
| 69 | + List<NetworkIF> networkIFs = hal.getNetworkIFs(); | |
| 70 | + int i= networkIFs.size() -1; | |
| 71 | + NetworkIF net= networkIFs.get(i); | |
| 72 | + | |
| 73 | + String in = FormatUtil.formatBytes(net.getBytesRecv()); | |
| 74 | + String out = FormatUtil.formatBytes(net.getBytesSent()); | |
| 75 | + HashMap<String, String> map = new HashMap<>(); | |
| 76 | + map.put("in",in); | |
| 77 | + map.put("out",out); | |
| 78 | + return map; | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * 获取进程数 | |
| 83 | + * @return | |
| 84 | + */ | |
| 85 | + public static int getProcessesCount(){ | |
| 86 | + SystemInfo si = new SystemInfo(); | |
| 87 | + OperatingSystem os = si.getOperatingSystem(); | |
| 88 | + | |
| 89 | + int processCount = os.getProcessCount(); | |
| 90 | + return processCount; | |
| 91 | + } | |
| 92 | +} | ... | ... |
src/main/java/com/genersoft/iot/vmp/utils/redis/RedisUtil.java
| ... | ... | @@ -661,6 +661,24 @@ public class RedisUtil { |
| 661 | 661 | } |
| 662 | 662 | |
| 663 | 663 | /** |
| 664 | + * 在键为 key 的 list中移除第一个元素 | |
| 665 | + * @param key 键 | |
| 666 | + * @return | |
| 667 | + */ | |
| 668 | + public Object lLeftPop(String key) { | |
| 669 | + return redisTemplate.opsForList().leftPop(key); | |
| 670 | + } | |
| 671 | + | |
| 672 | + /** | |
| 673 | + * 在键为 key 的 list中移除、最后一个元素 | |
| 674 | + * @param key 键 | |
| 675 | + * @return | |
| 676 | + */ | |
| 677 | + public Object lrightPop(String key) { | |
| 678 | + return redisTemplate.opsForList().rightPop(key); | |
| 679 | + } | |
| 680 | + | |
| 681 | + /** | |
| 664 | 682 | * 模糊查询 |
| 665 | 683 | * @param key 键 |
| 666 | 684 | * @return true / false | ... | ... |