|  |  |  | 
|---|
|  |  |  | package com.genersoft.iot.vmp.utils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookListener; | 
|---|
|  |  |  | import org.slf4j.Logger; | 
|---|
|  |  |  | import org.slf4j.LoggerFactory; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import oshi.SystemInfo; | 
|---|
|  |  |  | import oshi.hardware.CentralProcessor; | 
|---|
|  |  |  | import oshi.hardware.GlobalMemory; | 
|---|
|  |  |  | import oshi.hardware.HardwareAbstractionLayer; | 
|---|
|  |  |  | import oshi.hardware.NetworkIF; | 
|---|
|  |  |  | import oshi.hardware.*; | 
|---|
|  |  |  | import oshi.software.os.OperatingSystem; | 
|---|
|  |  |  | import oshi.util.FormatUtil; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.io.File; | 
|---|
|  |  |  | import java.text.DecimalFormat; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | 
|---|
|  |  |  | * 原文出处链接:https://blog.csdn.net/xiaozhangnomoney/article/details/107769147 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public class SystemInfoUtils { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final static Logger logger = LoggerFactory.getLogger(SystemInfoUtils.class); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取cpu信息 | 
|---|
|  |  |  | 
|---|
|  |  |  | * 获取网络上传和下载 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static Map<String,String> getNetworkInterfaces() { | 
|---|
|  |  |  | public static Map<String,Double> getNetworkInterfaces() { | 
|---|
|  |  |  | SystemInfo si = new SystemInfo(); | 
|---|
|  |  |  | HardwareAbstractionLayer hal = si.getHardware(); | 
|---|
|  |  |  | List<NetworkIF> networkIFs = hal.getNetworkIFs(); | 
|---|
|  |  |  | int i= networkIFs.size() -1; | 
|---|
|  |  |  | NetworkIF net= networkIFs.get(i); | 
|---|
|  |  |  | List<NetworkIF> beforeRecvNetworkIFs = hal.getNetworkIFs(); | 
|---|
|  |  |  | NetworkIF beforeBet= beforeRecvNetworkIFs.get(beforeRecvNetworkIFs.size() - 1); | 
|---|
|  |  |  | long beforeRecv = beforeBet.getBytesRecv(); | 
|---|
|  |  |  | long beforeSend = beforeBet.getBytesSent(); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Thread.sleep(1000); | 
|---|
|  |  |  | } catch (InterruptedException e) { | 
|---|
|  |  |  | logger.error("[线程休眠失败] : {}", e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<NetworkIF> afterNetworkIFs = hal.getNetworkIFs(); | 
|---|
|  |  |  | NetworkIF afterNet = afterNetworkIFs.get(afterNetworkIFs.size() - 1); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | HashMap<String, Double> map = new HashMap<>(); | 
|---|
|  |  |  | // 速度单位: Mbps | 
|---|
|  |  |  | map.put("in",formatUnits(afterNet.getBytesRecv()-beforeRecv, 1048576L)); | 
|---|
|  |  |  | map.put("out",formatUnits(afterNet.getBytesSent()-beforeSend, 1048576L)); | 
|---|
|  |  |  | return map; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取带宽总值 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static long getNetworkTotal() { | 
|---|
|  |  |  | SystemInfo si = new SystemInfo(); | 
|---|
|  |  |  | HardwareAbstractionLayer hal = si.getHardware(); | 
|---|
|  |  |  | List<NetworkIF> recvNetworkIFs = hal.getNetworkIFs(); | 
|---|
|  |  |  | NetworkIF networkIF= recvNetworkIFs.get(recvNetworkIFs.size() - 1); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return networkIF.getSpeed()/1048576L/8L; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static double formatUnits(long value, long prefix) { | 
|---|
|  |  |  | return (double)value / (double)prefix; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | int processCount = os.getProcessCount(); | 
|---|
|  |  |  | return processCount; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static List<Map<String, Object>> getDiskInfo() { | 
|---|
|  |  |  | List<Map<String, Object>> result = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String osName = System.getProperty("os.name"); | 
|---|
|  |  |  | List<String> pathArray = new ArrayList<>(); | 
|---|
|  |  |  | if (osName.startsWith("Mac OS")) { | 
|---|
|  |  |  | // 苹果 | 
|---|
|  |  |  | pathArray.add("/"); | 
|---|
|  |  |  | } else if (osName.startsWith("Windows")) { | 
|---|
|  |  |  | // windows | 
|---|
|  |  |  | pathArray.add("C:"); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | pathArray.add("/"); | 
|---|
|  |  |  | pathArray.add("/home"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (String path : pathArray) { | 
|---|
|  |  |  | Map<String, Object> infoMap = new HashMap<>(); | 
|---|
|  |  |  | infoMap.put("path", path); | 
|---|
|  |  |  | File partitionFile = new File(path); | 
|---|
|  |  |  | // 单位: GB | 
|---|
|  |  |  | infoMap.put("use", (partitionFile.getTotalSpace() - partitionFile.getFreeSpace())/1024/1024/1024D); | 
|---|
|  |  |  | infoMap.put("free", partitionFile.getFreeSpace()/1024/1024/1024D); | 
|---|
|  |  |  | result.add(infoMap); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|