MonitorUtil.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package jnpf.base.util;
  2. import cn.hutool.core.date.BetweenFormatter;
  3. import cn.hutool.core.date.DateUtil;
  4. import jnpf.base.model.monitor.*;
  5. import jnpf.util.IpUtil;
  6. import lombok.Data;
  7. import oshi.SystemInfo;
  8. import oshi.hardware.CentralProcessor;
  9. import oshi.hardware.GlobalMemory;
  10. import oshi.hardware.HardwareAbstractionLayer;
  11. import oshi.software.os.FileSystem;
  12. import oshi.software.os.OSFileStore;
  13. import oshi.software.os.OperatingSystem;
  14. import oshi.util.FormatUtil;
  15. import oshi.util.Util;
  16. import java.lang.management.ManagementFactory;
  17. import java.net.InetAddress;
  18. import java.net.NetworkInterface;
  19. import java.text.DecimalFormat;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.Enumeration;
  23. import java.util.List;
  24. /**
  25. *
  26. * @author JNPF开发平台组
  27. * @version V3.1.0
  28. * @copyright 引迈信息技术有限公司
  29. * @date 2021/3/12 15:31
  30. */
  31. @Data
  32. public class MonitorUtil {
  33. private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.00");
  34. private CpuModel cpu = null;
  35. private DiskModel disk = null;
  36. private MemoryModel memory = null;
  37. private SwapModel swap = null;
  38. private SystemModel system = null;
  39. public MonitorUtil() {
  40. SystemInfo si = new SystemInfo();
  41. OperatingSystem os = si.getOperatingSystem();
  42. HardwareAbstractionLayer hal = si.getHardware();
  43. this.cpu = getCpuInfo(hal.getProcessor());
  44. this.memory = getMemoryInfo(hal.getMemory());
  45. this.disk = getDiskInfo(os);
  46. this.system = getSystemInfo(os);
  47. this.swap = getSwapInfo(hal.getMemory());
  48. }
  49. /**
  50. * 获取磁盘信息
  51. *
  52. * @return /
  53. */
  54. private DiskModel getDiskInfo(OperatingSystem os) {
  55. DiskModel diskInfo = new DiskModel();
  56. FileSystem fileSystem = os.getFileSystem();
  57. List<OSFileStore> fsArray = fileSystem.getFileStores();
  58. long total = 0L;
  59. long available = 0L;
  60. long used = 0L;
  61. for (OSFileStore fs : fsArray) {
  62. total += fs.getTotalSpace();
  63. available += fs.getUsableSpace();
  64. }
  65. used = total - available;
  66. diskInfo.setTotal(FormatUtil.formatBytes(total));
  67. diskInfo.setAvailable(FormatUtil.formatBytes(available));
  68. diskInfo.setUsed(FormatUtil.formatBytes(used));
  69. diskInfo.setUsageRate(DECIMAL_FORMAT.format(used / (double) total * 100));
  70. return diskInfo;
  71. }
  72. /**
  73. * 获取交换区信息
  74. *
  75. * @param memory /
  76. * @return /
  77. */
  78. private SwapModel getSwapInfo(GlobalMemory memory) {
  79. SwapModel swapInfo = new SwapModel();
  80. swapInfo.setTotal(FormatUtil.formatBytes(memory.getVirtualMemory().getSwapTotal()));
  81. swapInfo.setAvailable(FormatUtil.formatBytes(memory.getVirtualMemory().getSwapTotal() - memory.getVirtualMemory().getSwapUsed()));
  82. swapInfo.setUsageRate(DECIMAL_FORMAT.format(memory.getVirtualMemory().getSwapUsed() / (double) memory.getVirtualMemory().getSwapTotal() * 100));
  83. swapInfo.setUsed(FormatUtil.formatBytes(memory.getVirtualMemory().getSwapUsed()));
  84. return swapInfo;
  85. }
  86. /**
  87. * 获取内存信息
  88. *
  89. * @param memory /
  90. * @return /
  91. */
  92. private MemoryModel getMemoryInfo(GlobalMemory memory) {
  93. MemoryModel memoryInfo = new MemoryModel();
  94. memoryInfo.setTotal(FormatUtil.formatBytes(memory.getTotal()));
  95. memoryInfo.setAvailable(FormatUtil.formatBytes(memory.getAvailable()));
  96. memoryInfo.setUsed(FormatUtil.formatBytes(memory.getTotal() - memory.getAvailable()));
  97. memoryInfo.setUsageRate(DECIMAL_FORMAT.format((memory.getTotal() - memory.getAvailable()) / (double) memory.getTotal() * 100));
  98. return memoryInfo;
  99. }
  100. /**
  101. * 获取Cpu相关信息
  102. *
  103. * @param processor /
  104. * @return /
  105. */
  106. private CpuModel getCpuInfo(CentralProcessor processor) {
  107. CpuModel cpuInfo = new CpuModel();
  108. cpuInfo.setName(processor.getProcessorIdentifier().getName());
  109. cpuInfo.setPackageName(processor.getPhysicalPackageCount() + "个物理CPU");
  110. cpuInfo.setCore(processor.getPhysicalProcessorCount() + "个物理核心");
  111. cpuInfo.setCoreNumber(processor.getPhysicalProcessorCount());
  112. cpuInfo.setLogic(processor.getLogicalProcessorCount() + "个逻辑CPU");
  113. // CPU信息
  114. long[] prevTicks = processor.getSystemCpuLoadTicks();
  115. // 等待1秒...
  116. Util.sleep(1000);
  117. long[] ticks = processor.getSystemCpuLoadTicks();
  118. long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
  119. long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
  120. long sys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
  121. long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
  122. long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
  123. long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
  124. long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
  125. long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
  126. long totalCpu = user + nice + sys + idle + iowait + irq + softirq + steal;
  127. cpuInfo.setUsed(DECIMAL_FORMAT.format(100d * user / totalCpu + 100d * sys / totalCpu));
  128. cpuInfo.setIdle(DECIMAL_FORMAT.format(100d * idle / totalCpu));
  129. return cpuInfo;
  130. }
  131. /**
  132. * 获取系统相关信息,系统、运行天数、系统IP
  133. *
  134. * @param
  135. * @return /
  136. */
  137. private SystemModel getSystemInfo(OperatingSystem operatingSystem) {
  138. SystemModel systemInfo = new SystemModel();
  139. String osName = System.getProperty("os.name");
  140. String os = osName;
  141. if(osName.contains("Linux")){
  142. os = operatingSystem.toString();
  143. }
  144. // jvm 运行时间
  145. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  146. Date date = new Date(time);
  147. // 计算项目运行时间
  148. String formatBetween = DateUtil.formatBetween(date, new Date(), BetweenFormatter.Level.HOUR);
  149. // 系统信息
  150. systemInfo.setOs(os);
  151. systemInfo.setDay(formatBetween);
  152. systemInfo.setIp(getLocalhostIp());
  153. return systemInfo;
  154. }
  155. /**
  156. * <p>获取当前服务器所有符合条件的网络地址</p>
  157. *
  158. * @return List<InetAddress> 网络地址列表
  159. * @throws Exception 默认异常
  160. */
  161. private static String getLocalhostIp() {
  162. List<String> result = new ArrayList<>();
  163. try {
  164. // 遍历所有的网络接口
  165. for (Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) {
  166. NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement();
  167. // 在所有的接口下再遍历IP
  168. for (Enumeration addresses = ni.getInetAddresses(); addresses.hasMoreElements(); ) {
  169. InetAddress address = (InetAddress) addresses.nextElement();
  170. //排除LoopbackAddress、SiteLocalAddress、LinkLocalAddress、MulticastAddress类型的IP地址
  171. if (!address.isLoopbackAddress()
  172. /*&& !inetAddr.isSiteLocalAddress()*/
  173. && !address.isLinkLocalAddress() && !address.isMulticastAddress()) {
  174. String hostAddress = address.getHostAddress();
  175. result.add(hostAddress);
  176. }
  177. }
  178. }
  179. }catch (Exception e) {
  180. }
  181. String ip = String.join(",",result);
  182. return ip;
  183. }
  184. }