From 3e4fb6b20cdb85b1bf290a88f68a4646915da78d Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期六, 03 八月 2024 11:06:52 +0800
Subject: [PATCH] 增加获取系统信息相关实用类,及相关依赖
---
pms-parent/pms-common/src/main/java/com/dy/common/util/IPUtils.java | 341 ++++++++++++++++++++
pms-parent/pms-common/src/main/java/com/dy/common/util/SysSysUtil.java | 68 ++++
pms-parent/pms-common/src/main/java/com/dy/common/util/SysUtil.java | 63 +++
pms-parent/pms-common/src/main/java/com/dy/common/util/SysJvmUtil.java | 78 ++++
pms-parent/pms-common/src/main/java/com/dy/common/util/SysCpuUtil.java | 110 ++++++
pms-parent/pms-common/src/main/java/com/dy/common/util/SysFileSysUtil.java | 113 ++++++
pms-parent/pms-common/src/main/java/com/dy/common/util/SysMemoryUtil.java | 58 +++
pms-parent/pms-common/src/main/java/com/dy/common/util/DoubleUtil.java | 103 ++++++
pms-parent/pms-common/pom.xml | 17 +
pms-parent/pom.xml | 28 +
10 files changed, 979 insertions(+), 0 deletions(-)
diff --git a/pms-parent/pms-common/pom.xml b/pms-parent/pms-common/pom.xml
index 2797a49..cacf02c 100644
--- a/pms-parent/pms-common/pom.xml
+++ b/pms-parent/pms-common/pom.xml
@@ -201,6 +201,23 @@
<version>${google.zxing.version}</version>
</dependency>
+ <!-- 鑾峰緱绯荤粺淇℃伅 -->
+ <dependency>
+ <groupId>com.github.oshi</groupId>
+ <artifactId>oshi-core</artifactId>
+ <version>${com.github.oshi-core.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>net.java.dev.jna</groupId>
+ <artifactId>jna</artifactId>
+ <version>${net.java.dev.jna.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>net.java.dev.jna</groupId>
+ <artifactId>jna-platform</artifactId>
+ <version>${net.java.dev.jna.version}</version>
+ </dependency>
+
<!-- Hutool鏄竴涓皬鑰屽叏鐨凧ava宸ュ叿绫诲簱(https://github.com/dromara/hutool) -->
<dependency>
<groupId>cn.hutool</groupId>
diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/DoubleUtil.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/DoubleUtil.java
new file mode 100644
index 0000000..b40e2dd
--- /dev/null
+++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/DoubleUtil.java
@@ -0,0 +1,103 @@
+package com.dy.common.util;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/8/2 13:58
+ * @Description 绮惧噯鐨刣ouble璁$畻
+ */
+public class DoubleUtil {
+ /** 榛樿闄ゆ硶杩愮畻绮惧害 */
+ private static final int DEF_DIV_SCALE = 10;
+
+ /** 杩欎釜绫讳笉鑳藉疄渚嬪寲 */
+ private DoubleUtil() {
+ }
+
+ /**
+ * 鎻愪緵绮剧‘鐨勫姞娉曡繍绠椼��
+ * @param v1 琚姞鏁�
+ * @param v2 鍔犳暟
+ * @return 涓や釜鍙傛暟鐨勫拰
+ */
+ public static double add(double v1, double v2) {
+ BigDecimal b1 = new BigDecimal(Double.toString(v1));
+ BigDecimal b2 = new BigDecimal(Double.toString(v2));
+ return b1.add(b2).doubleValue();
+ }
+
+ /**
+ * 鎻愪緵绮剧‘鐨勫噺娉曡繍绠椼��
+ * @param v1 琚噺鏁�
+ * @param v2 鍑忔暟
+ * @return 涓や釜鍙傛暟鐨勫樊
+ */
+ public static double sub(double v1, double v2) {
+ BigDecimal b1 = new BigDecimal(Double.toString(v1));
+ BigDecimal b2 = new BigDecimal(Double.toString(v2));
+ return b1.subtract(b2).doubleValue();
+ }
+
+ /**
+ * 鎻愪緵绮剧‘鐨勪箻娉曡繍绠椼��
+ * @param v1 琚箻鏁�
+ * @param v2 涔樻暟
+ * @return 涓や釜鍙傛暟鐨勭Н
+ */
+ public static double mul(double v1, double v2) {
+ BigDecimal b1 = new BigDecimal(Double.toString(v1));
+ BigDecimal b2 = new BigDecimal(Double.toString(v2));
+ return b1.multiply(b2).doubleValue();
+ }
+
+ /**
+ * 鎻愪緵锛堢浉瀵癸級绮剧‘鐨勯櫎娉曡繍绠楋紝褰撳彂鐢熼櫎涓嶅敖鐨勬儏鍐垫椂锛岀簿纭埌
+ * 灏忔暟鐐逛互鍚�10浣嶏紝浠ュ悗鐨勬暟瀛楀洓鑸嶄簲鍏ャ��
+ * @param v1 琚櫎鏁�
+ * @param v2 闄ゆ暟
+ * @return 涓や釜鍙傛暟鐨勫晢
+ */
+ public static double div(double v1, double v2)
+ {
+ return div(v1, v2, DEF_DIV_SCALE);
+ }
+
+ /**
+ * 鎻愪緵锛堢浉瀵癸級绮剧‘鐨勯櫎娉曡繍绠椼�傚綋鍙戠敓闄や笉灏界殑鎯呭喌鏃讹紝鐢眘cale鍙傛暟鎸�
+ * 瀹氱簿搴︼紝浠ュ悗鐨勬暟瀛楀洓鑸嶄簲鍏ャ��
+ * @param v1 琚櫎鏁�
+ * @param v2 闄ゆ暟
+ * @param scale 琛ㄧず琛ㄧず闇�瑕佺簿纭埌灏忔暟鐐逛互鍚庡嚑浣嶃��
+ * @return 涓や釜鍙傛暟鐨勫晢
+ */
+ public static double div(double v1, double v2, int scale) {
+ if (scale < 0) {
+ throw new IllegalArgumentException(
+ "The scale must be a positive integer or zero");
+ }
+ BigDecimal b1 = new BigDecimal(Double.toString(v1));
+ BigDecimal b2 = new BigDecimal(Double.toString(v2));
+ if (b1.compareTo(BigDecimal.ZERO) == 0) {
+ return BigDecimal.ZERO.doubleValue();
+ }
+ return b1.divide(b2, scale, RoundingMode.HALF_UP).doubleValue();
+ }
+
+ /**
+ * 鎻愪緵绮剧‘鐨勫皬鏁颁綅鍥涜垗浜斿叆澶勭悊銆�
+ * @param v 闇�瑕佸洓鑸嶄簲鍏ョ殑鏁板瓧
+ * @param scale 灏忔暟鐐瑰悗淇濈暀鍑犱綅
+ * @return 鍥涜垗浜斿叆鍚庣殑缁撴灉
+ */
+ public static double round(double v, int scale) {
+ if (scale < 0) {
+ throw new IllegalArgumentException(
+ "The scale must be a positive integer or zero");
+ }
+ BigDecimal b = new BigDecimal(Double.toString(v));
+ BigDecimal one = BigDecimal.ONE;
+ return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue();
+ }
+}
diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/IPUtils.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/IPUtils.java
new file mode 100644
index 0000000..3a4628c
--- /dev/null
+++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/IPUtils.java
@@ -0,0 +1,341 @@
+package com.dy.common.util;
+
+import jakarta.servlet.http.HttpServletRequest;
+
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.UnknownHostException;
+import java.util.Enumeration;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * 鑾峰彇IP鏂规硶
+ *
+ * @author liurunyu
+ */
+public class IPUtils {
+
+ /**
+ * 妫�鏌P鏄惁鍚堟硶
+ *
+ * @param ip
+ * @return
+ */
+ public static boolean ipValid(String ip) {
+ String regex0 = "(2[0-4]\\d)" + "|(25[0-5])";
+ String regex1 = "1\\d{2}";
+ String regex2 = "[1-9]\\d";
+ String regex3 = "\\d";
+ String regex = "(" + regex0 + ")|(" + regex1 + ")|(" + regex2 + ")|(" + regex3 + ")";
+ regex = "(" + regex + ").(" + regex + ").(" + regex + ").(" + regex + ")";
+ Pattern p = Pattern.compile(regex);
+ Matcher m = p.matcher(ip);
+ return m.matches();
+ }
+
+ /**
+ * 鑾峰彇鏈湴ip 閫傚悎windows涓巐inux
+ *
+ * @return
+ */
+ public static String getLocalIP() {
+ String localIP = "127.0.0.1";
+ try {
+ Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
+ while (netInterfaces.hasMoreElements()) {
+ NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
+ InetAddress ip = ni.getInetAddresses().nextElement();
+ if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
+ localIP = ip.getHostAddress();
+ break;
+ }
+ }
+ } catch (Exception e) {
+ try {
+ localIP = InetAddress.getLocalHost().getHostAddress();
+ } catch (UnknownHostException e1) {
+ e1.printStackTrace();
+ }
+ }
+ return localIP;
+ }
+ /**
+ * 鑾峰彇瀹㈡埛绔疘P
+ *
+ * @param request 璇锋眰瀵硅薄
+ * @return IP鍦板潃
+ */
+ public static String getClientIp(HttpServletRequest request)
+ {
+ if (request == null)
+ {
+ return "unknown";
+ }
+ String ip = request.getHeader("x-forwarded-for");
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
+ {
+ ip = request.getHeader("Proxy-Client-IP");
+ }
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
+ {
+ ip = request.getHeader("X-Forwarded-For");
+ }
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
+ {
+ ip = request.getHeader("WL-Proxy-Client-IP");
+ }
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
+ {
+ ip = request.getHeader("X-Real-IP");
+ }
+
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
+ {
+ ip = request.getRemoteAddr();
+ }
+
+ return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
+ }
+
+ /**
+ * 妫�鏌ユ槸鍚︿负鍐呴儴IP鍦板潃
+ *
+ * @param ip IP鍦板潃
+ * @return 缁撴灉
+ */
+ public static boolean internalIp(String ip)
+ {
+ byte[] addr = textToNumericFormatV4(ip);
+ return internalIp(addr) || "127.0.0.1".equals(ip);
+ }
+
+ /**
+ * 妫�鏌ユ槸鍚︿负鍐呴儴IP鍦板潃
+ *
+ * @param addr byte鍦板潃
+ * @return 缁撴灉
+ */
+ private static boolean internalIp(byte[] addr)
+ {
+ if (addr == null || addr.length < 2)
+ {
+ return true;
+ }
+ final byte b0 = addr[0];
+ final byte b1 = addr[1];
+ // 10.x.x.x/8
+ final byte SECTION_1 = 0x0A;
+ // 172.16.x.x/12
+ final byte SECTION_2 = (byte) 0xAC;
+ final byte SECTION_3 = (byte) 0x10;
+ final byte SECTION_4 = (byte) 0x1F;
+ // 192.168.x.x/16
+ final byte SECTION_5 = (byte) 0xC0;
+ final byte SECTION_6 = (byte) 0xA8;
+ switch (b0)
+ {
+ case SECTION_1:
+ return true;
+ case SECTION_2:
+ if (b1 >= SECTION_3 && b1 <= SECTION_4)
+ {
+ return true;
+ }
+ case SECTION_5:
+ switch (b1)
+ {
+ case SECTION_6:
+ return true;
+ }
+ default:
+ return false;
+ }
+ }
+ /**
+ * 鎶奿p杞寲涓烘暣鏁�
+ *
+ * @param ip
+ * @return
+ */
+ public static long translateIP2Int(String ip) {
+ String[] intArr = ip.split("\\.");
+ int[] ipInt = new int[intArr.length];
+ for (int i = 0; i < intArr.length; i++) {
+ ipInt[i] = Integer.parseInt(intArr[i]) ;
+ }
+ return ipInt[0] * 256 * 256 * 256 + +ipInt[1] * 256 * 256 + ipInt[2] * 256 + ipInt[3];
+ }
+ /**
+ * 灏咺Pv4鍦板潃杞崲鎴愬瓧鑺�
+ *
+ * @param text IPv4鍦板潃
+ * @return byte 瀛楄妭
+ */
+ public static byte[] textToNumericFormatV4(String text)
+ {
+ if (text.length() == 0)
+ {
+ return null;
+ }
+
+ byte[] bytes = new byte[4];
+ String[] elements = text.split("\\.", -1);
+ try
+ {
+ long l;
+ int i;
+ switch (elements.length)
+ {
+ case 1:
+ l = Long.parseLong(elements[0]);
+ if ((l < 0L) || (l > 4294967295L))
+ {
+ return null;
+ }
+ bytes[0] = (byte) (int) (l >> 24 & 0xFF);
+ bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
+ bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
+ bytes[3] = (byte) (int) (l & 0xFF);
+ break;
+ case 2:
+ l = Integer.parseInt(elements[0]);
+ if ((l < 0L) || (l > 255L))
+ {
+ return null;
+ }
+ bytes[0] = (byte) (int) (l & 0xFF);
+ l = Integer.parseInt(elements[1]);
+ if ((l < 0L) || (l > 16777215L))
+ {
+ return null;
+ }
+ bytes[1] = (byte) (int) (l >> 16 & 0xFF);
+ bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
+ bytes[3] = (byte) (int) (l & 0xFF);
+ break;
+ case 3:
+ for (i = 0; i < 2; ++i)
+ {
+ l = Integer.parseInt(elements[i]);
+ if ((l < 0L) || (l > 255L))
+ {
+ return null;
+ }
+ bytes[i] = (byte) (int) (l & 0xFF);
+ }
+ l = Integer.parseInt(elements[2]);
+ if ((l < 0L) || (l > 65535L))
+ {
+ return null;
+ }
+ bytes[2] = (byte) (int) (l >> 8 & 0xFF);
+ bytes[3] = (byte) (int) (l & 0xFF);
+ break;
+ case 4:
+ for (i = 0; i < 4; ++i)
+ {
+ l = Integer.parseInt(elements[i]);
+ if ((l < 0L) || (l > 255L))
+ {
+ return null;
+ }
+ bytes[i] = (byte) (int) (l & 0xFF);
+ }
+ break;
+ default:
+ return null;
+ }
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ return bytes;
+ }
+
+ /**
+ * 鑾峰彇IP鍦板潃
+ *
+ * @return 鏈湴IP鍦板潃
+ */
+ public static String getHostIp()
+ {
+ try
+ {
+ return InetAddress.getLocalHost().getHostAddress();
+ }
+ catch (UnknownHostException e)
+ {
+ }
+ return "127.0.0.1";
+ }
+
+ /**
+ * 鑾峰彇涓绘満鍚�
+ *
+ * @return 鏈湴涓绘満鍚�
+ */
+ public static String getHostName()
+ {
+ try
+ {
+ return InetAddress.getLocalHost().getHostName();
+ }
+ catch (UnknownHostException e)
+ {
+ }
+ return "鏈煡";
+ }
+
+ /**
+ * 浠庡绾у弽鍚戜唬鐞嗕腑鑾峰緱绗竴涓潪unknown IP鍦板潃
+ *
+ * @param ip 鑾峰緱鐨処P鍦板潃
+ * @return 绗竴涓潪unknown IP鍦板潃
+ */
+ public static String getMultistageReverseProxyIp(String ip)
+ {
+ // 澶氱骇鍙嶅悜浠g悊妫�娴�
+ if (ip != null && ip.indexOf(",") > 0)
+ {
+ final String[] ips = ip.trim().split(",");
+ for (String subIp : ips)
+ {
+ if (false == isUnknown(subIp))
+ {
+ ip = subIp;
+ break;
+ }
+ }
+ }
+ return ip;
+ }
+
+ /**
+ * 妫�娴嬬粰瀹氬瓧绗︿覆鏄惁涓烘湭鐭ワ紝澶氱敤浜庢娴婬TTP璇锋眰鐩稿叧
+ *
+ * @param checkString 琚娴嬬殑瀛楃涓�
+ * @return 鏄惁鏈煡
+ */
+ public static boolean isUnknown(String checkString)
+ {
+ return isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
+ }
+
+ private static boolean isBlank(final CharSequence cs) {
+ final int strLen = length(cs);
+ if (strLen == 0) {
+ return true;
+ }
+ for (int i = 0; i < strLen; i++) {
+ if (!Character.isWhitespace(cs.charAt(i))) {
+ return false;
+ }
+ }
+ return true;
+ }
+ public static int length(final CharSequence cs) {
+ return cs == null ? 0 : cs.length();
+ }
+}
\ No newline at end of file
diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/SysCpuUtil.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysCpuUtil.java
new file mode 100644
index 0000000..5675ae6
--- /dev/null
+++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysCpuUtil.java
@@ -0,0 +1,110 @@
+package com.dy.common.util;
+
+import lombok.Data;
+import oshi.hardware.CentralProcessor;
+import oshi.util.Util;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/8/2 11:17
+ * @Description
+ */
+public class SysCpuUtil {
+
+ private static final int OSHI_WAIT_SECOND = 1000;
+ /**
+ * 寰楀埌CPU淇℃伅
+ */
+ public static CpuInfoVo info(CentralProcessor processor){
+ /**
+ * 鑾峰彇绯荤粺鑼冨洿鍐呯殑CPU璐熻浇璁℃暟鍣ㄣ�傝繑鍥炰竴涓寘鍚叓涓厓绱犵殑鏁扮粍锛岃〃绀哄湪鐢ㄦ埛锛�0锛夈�佸凹鏂紙1锛夈�佺郴缁燂紙2锛夈�佺┖闂诧紙3锛夈��
+ * IOwait锛�4锛夈�佺‖浠朵腑鏂紙IRQ锛夛紙5锛夈�佽蒋浠朵腑鏂�/DPC锛圫oftIRQ锛夛級锛�6锛夋垨绐冨彇锛�7锛夌姸鎬佷笅鑺辫垂鐨勬绉掓暟銆備娇鐢�
+ * {@link-shi.hardware.TickType#getIndex锛堬級}妫�绱㈢浉搴旂殑绱㈠紩銆傞�氳繃娴嬮噺涓�涓椂闂撮棿
+ * 闅斿唴婊寸瓟澹颁箣闂寸殑宸紓锛屽彲浠ヨ绠楀嚭璇ユ椂闂撮棿闅斿唴鐨凜PU璐熻浇銆�
+ * 鍦ㄦ煇浜涢�昏緫澶勭悊鍣ㄦ暟閲忓彲鍙樼殑鎿嶄綔绯荤粺涓婏紝姝ら樀鍒楃殑澶у皬鍙兘浼氬彂鐢熷彉鍖栵紝骞朵笖鍙兘涓庡叾浠栨瘡澶勭悊鍣ㄦ柟娉曚笉涓�鑷淬��
+ * 璇锋敞鎰忥紝铏界劧婊寸瓟璁℃暟鍣ㄤ互姣涓哄崟浣嶏紝浣嗗畠浠彲鑳戒細闅忕潃锛堜緷璧栦簬骞冲彴鐨勶級鏃堕挓婊寸瓟澹颁互鏇村ぇ鐨勫閲忓墠杩涖�備緥濡傦紝榛樿
+ * 鎯呭喌涓嬶紝Windows鏃堕挓鑺傛媿涓�1/64绉掞紙绾�15鎴�16姣锛夛紝Linux鏃堕挓鑺傛媿鍙栧喅浜庡垎甯冨拰閰嶇疆锛屼絾閫氬父涓�1/100绉掞紙10
+ * 姣锛夈��
+ * Windows涓婃病鏈塏ice鍜孖OWait淇℃伅锛宮acOS涓婃病鏈塈OWait鍜孖RQ淇℃伅锛屽洜姝よ繖浜涙爣璁板皢濮嬬粓涓洪浂銆�
+ * 瑕佷娇鐢ㄦ鏂规硶璁$畻鎬荤┖闂叉椂闂达紝璇峰悓鏃跺寘鎷琁dle鍜孖OWait鏍囪銆傚悓鏍凤紝搴斿皢IRQ銆丼oftIRQ鍜孲teal鏍囪娣诲姞鍒扮郴缁熷��
+ * 涓互鑾峰緱鎬诲拰銆傜郴缁熻鏃惰繕鍖呮嫭鎵ц鍏朵粬铏氭嫙涓绘満锛堢獌鍙栵級鐨勬椂闂淬��
+ * *
+ * @return涓�涓敱8涓暱鍊肩粍鎴愮殑鏁扮粍锛岃〃绀哄湪User銆丯ice銆丼ystem銆両dle銆両Owait銆両RQ銆丼oftIRQ鍜孲teal鐘舵�佷笅鑺辫垂鐨勬椂闂淬��
+ */
+ long[] prevTicks = processor.getSystemCpuLoadTicks();
+ Util.sleep(OSHI_WAIT_SECOND);
+ long[] ticks = processor.getSystemCpuLoadTicks();
+ long nice = ticks[1] - prevTicks[1];
+ long irq = ticks[5] - prevTicks[5];
+ long softIrq = ticks[6] - prevTicks[6];
+ long steal = ticks[7] - prevTicks[7];
+ long cSys = ticks[2] - prevTicks[2];
+ long user = ticks[0] - prevTicks[0];
+ long ioWait = ticks[4] - prevTicks[4];
+ long idle = ticks[3] - prevTicks[3];
+
+ /*
+ long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()];
+ long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()];
+ long softIrq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()];
+ long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()];
+ long cSys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()];
+ long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()];
+ long ioWait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()];
+ long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()];
+ */
+
+ long totalCpu = user + nice + cSys + idle + ioWait + irq + softIrq + steal;
+
+ CpuInfoVo vo = new CpuInfoVo() ;
+ vo.cores = processor.getLogicalProcessorCount() ;
+ vo.totalRate = totalCpu ;
+ vo.sysRate = cSys ;
+ vo.userRate = user ;
+ vo.waitRate = ioWait ;
+ vo.freeRate = idle ;
+ return vo ;
+ }
+
+ @Data
+ public static class CpuInfoVo{
+ /**
+ * 鏍稿績鏁�
+ */
+ public int cores;
+ /**
+ * CPU鎬荤殑浣跨敤鐜�
+ */
+ public double totalRate;
+ /**
+ * CPU绯荤粺浣跨敤鐜�
+ */
+ public double sysRate;
+ /**
+ * CPU鐢ㄦ埛浣跨敤鐜�
+ */
+ public double userRate;
+ /**
+ * CPU褰撳墠绛夊緟鐜�
+ */
+ public double waitRate;
+ /**
+ * CPU褰撳墠绌洪棽鐜�
+ */
+ public double freeRate;
+
+ public String toString(){
+ StringBuilder sb = new StringBuilder() ;
+ sb.append(" CPU淇℃伅锛歕n");
+ sb.append(" 鏍稿績鏁帮細" + cores + "\n");
+ sb.append(" CPU鎬荤殑浣跨敤鐜囷細" + totalRate + "\n");
+ sb.append(" CPU绯荤粺浣跨敤鐜囷細" + sysRate + "\n");
+ sb.append(" CPU鐢ㄦ埛浣跨敤鐜囷細" + userRate + "\n");
+ sb.append(" CPU褰撳墠绛夊緟鐜囷細" + waitRate + "\n");
+ sb.append(" CPU褰撳墠绌洪棽鐜囷細" + freeRate + "\n");
+
+ return sb.toString() ;
+ }
+
+ }
+}
diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/SysFileSysUtil.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysFileSysUtil.java
new file mode 100644
index 0000000..fc1976a
--- /dev/null
+++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysFileSysUtil.java
@@ -0,0 +1,113 @@
+package com.dy.common.util;
+
+import lombok.Data;
+import oshi.software.os.FileSystem;
+import oshi.software.os.OSFileStore;
+import oshi.software.os.OperatingSystem;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/8/2 13:54
+ * @Description
+ */
+public class SysFileSysUtil {
+ /**
+ * 纾佺洏淇℃伅
+ */
+ public static LinkedList<SysFile> info(OperatingSystem os) {
+ LinkedList<SysFile> sysFiles = new LinkedList<SysFile>();
+ FileSystem fileSystem = os.getFileSystem();
+ List<OSFileStore> fsArray = fileSystem.getFileStores();
+ for (OSFileStore fs : fsArray) {
+ long free = fs.getUsableSpace();
+ long total = fs.getTotalSpace();
+ long used = total - free;
+ SysFile sysFile = new SysFile();
+ sysFile.setDirName(fs.getMount());
+ sysFile.setSysTypeName(fs.getType());
+ sysFile.setTypeName(fs.getName());
+ sysFile.setTotal(convertFileSize(total));
+ sysFile.setFree(convertFileSize(free));
+ sysFile.setUsed(convertFileSize(used));
+ sysFile.setUsage(DoubleUtil.mul(DoubleUtil.div(used, total, 4), 100));
+ sysFiles.add(sysFile);
+ }
+ return sysFiles ;
+ }
+
+ /**
+ * 瀛楄妭杞崲
+ *
+ * @param size 瀛楄妭澶у皬
+ * @return 杞崲鍚庡��
+ */
+ public static String convertFileSize(long size) {
+ long kb = 1024;
+ long mb = kb * 1024;
+ long gb = mb * 1024;
+ if (size >= gb) {
+ return String.format("%.1f GB", (float) size / gb);
+ } else if (size >= mb) {
+ float f = (float) size / mb;
+ return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
+ } else if (size >= kb) {
+ float f = (float) size / kb;
+ return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
+ } else {
+ return String.format("%d B", size);
+ }
+ }
+ @Data
+ public static class SysFile{
+ /**
+ * 鐩樼璺緞
+ */
+ public String dirName;
+
+ /**
+ * 鐩樼绫诲瀷
+ */
+ public String sysTypeName;
+
+ /**
+ * 鏂囦欢绫诲瀷
+ */
+ public String typeName;
+
+ /**
+ * 鎬诲ぇ灏�
+ */
+ public String total;
+
+ /**
+ * 鍓╀綑澶у皬
+ */
+ public String free;
+
+ /**
+ * 宸茬粡浣跨敤閲�
+ */
+ public String used;
+
+ /**
+ * 璧勬簮鐨勪娇鐢ㄧ巼
+ */
+ public double usage;
+
+ public String toString(){
+ StringBuilder sb = new StringBuilder() ;
+ sb.append(" 纾佺洏" + dirName + "\n");
+ sb.append(" 鐩樼绫诲瀷锛�" + sysTypeName + "\n");
+ sb.append(" 鏂囦欢绫诲瀷锛�" + typeName + "\n");
+ sb.append(" 鎬诲ぇ灏忥細" + total + "\n");
+ sb.append(" 鍓╀綑澶у皬锛�" + free + "\n");
+ sb.append(" 宸茬粡浣跨敤閲忥細" + used + "\n");
+ sb.append(" 璧勬簮浣跨敤鐜囷細" + usage + "\n");
+
+ return sb.toString() ;
+ }
+ }
+}
diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/SysJvmUtil.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysJvmUtil.java
new file mode 100644
index 0000000..fa0e1e0
--- /dev/null
+++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysJvmUtil.java
@@ -0,0 +1,78 @@
+package com.dy.common.util;
+
+import lombok.Data;
+
+import java.net.UnknownHostException;
+import java.text.DecimalFormat;
+import java.util.Properties;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/8/2 13:52
+ * @Description
+ */
+public class SysJvmUtil {
+
+ /**
+Java铏氭嫙鏈轰俊鎭�
+ */
+ public static JvmInfoVo info() throws UnknownHostException {
+ JvmInfoVo vo = new JvmInfoVo() ;
+ Properties props = System.getProperties();
+ vo.setTotal(toMbytes(Runtime.getRuntime().totalMemory()));
+ vo.setMax(toMbytes(Runtime.getRuntime().maxMemory()));
+ vo.setFree(toMbytes(Runtime.getRuntime().freeMemory()));
+ vo.setVersion(props.getProperty("java.version"));
+ vo.setHome(props.getProperty("java.home"));
+ return vo ;
+ }
+
+ private static double toMbytes(double value){
+ return value/1000000.0 ;
+ }
+
+ @Data
+ public static class JvmInfoVo {
+ /**
+ 褰撳墠JVM鍗犵敤鐨勫唴瀛樻�绘暟(M)
+ */
+ public double total;
+
+ /**
+ JVM鏈�澶у彲鐢ㄥ唴瀛樻�绘暟(M)
+ */
+ public double max;
+
+ /**
+ JVM绌洪棽鍐呭瓨(M)
+ */
+ public double free;
+
+ /**
+ JDK鐗堟湰
+ */
+ public String version;
+
+ /**
+ JDK璺緞
+ */
+ public String home;
+
+
+ public String toString(){
+ DecimalFormat df = new DecimalFormat("#.####"); // 璁惧畾鏍煎紡
+ StringBuilder sb = new StringBuilder() ;
+ sb.append(" JVM淇℃伅锛歕n");
+ sb.append(" JVM鏈�澶у彲鐢ㄥ唴瀛樻�绘暟(M)锛�" + df.format(max) + "\n");
+ sb.append(" JVM鍗犵敤鐨勫唴瀛樻暟(M)锛�" + df.format(total) + "\n");
+ sb.append(" JVM绌洪棽鍐呭瓨鏁�(M)锛�" + df.format(free) + "\n");
+ sb.append(" JDK鐗堟湰锛�" + version + "\n");
+ sb.append(" JDK璺緞锛�" + home + "\n");
+
+ return sb.toString() ;
+ }
+ }
+
+
+
+}
diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/SysMemoryUtil.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysMemoryUtil.java
new file mode 100644
index 0000000..62955bd
--- /dev/null
+++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysMemoryUtil.java
@@ -0,0 +1,58 @@
+package com.dy.common.util;
+
+import lombok.Data;
+import oshi.hardware.GlobalMemory;
+
+import java.text.DecimalFormat;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/8/2 13:42
+ * @Description
+ */
+public class SysMemoryUtil {
+
+ /**
+ * 鍐呭瓨淇℃伅
+ */
+ public static MemInfoVo info(GlobalMemory memory) {
+ MemInfoVo vo = new MemInfoVo() ;
+ vo.setTotal(toGbytes(memory.getTotal()));
+ vo.setUsed(toGbytes(memory.getTotal() - memory.getAvailable()));
+ vo.setFree(toGbytes(memory.getAvailable()));
+ return vo ;
+ }
+
+ private static double toGbytes(double value){
+ return value/1000000000.0 ;
+ }
+
+ @Data
+ public static class MemInfoVo {
+ /**
+ * 鍐呭瓨鎬婚噺
+ */
+ public double total;
+
+ /**
+ * 宸茬敤鍐呭瓨
+ */
+ public double used;
+
+ /**
+ * 鍓╀綑鍐呭瓨
+ */
+ public double free;
+
+ public String toString(){
+ DecimalFormat df = new DecimalFormat("#.####"); // 璁惧畾鏍煎紡
+ StringBuilder sb = new StringBuilder() ;
+ sb.append(" 鍐呭瓨淇℃伅锛歕n");
+ sb.append(" 鍐呭瓨鎬婚噺(G)锛�" + df.format(total) + "\n");
+ sb.append(" 宸茬敤鍐呭瓨(G)锛�" + df.format(used) + "\n");
+ sb.append(" 鍓╀綑鍐呭瓨(G)锛�" + df.format(free) + "\n");
+
+ return sb.toString() ;
+ }
+ }
+}
diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/SysSysUtil.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysSysUtil.java
new file mode 100644
index 0000000..bbaee01
--- /dev/null
+++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysSysUtil.java
@@ -0,0 +1,68 @@
+package com.dy.common.util;
+
+import lombok.Data;
+
+import java.util.Properties;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/8/2 13:46
+ * @Description
+ */
+public class SysSysUtil {
+
+ /**
+ * 璁剧疆鏈嶅姟鍣ㄤ俊鎭�
+ */
+ public static SysInfoVo info() {
+ SysInfoVo vo = new SysInfoVo() ;
+ Properties props = System.getProperties();
+ vo.setComputerName(IPUtils.getHostName());
+ vo.setComputerIp(IPUtils.getHostIp());
+ vo.setOsName(props.getProperty("os.name"));
+ vo.setOsArch(props.getProperty("os.arch"));
+ vo.setUserDir(props.getProperty("user.dir"));
+ return vo ;
+ }
+
+
+ @Data
+ public static class SysInfoVo{
+ /**
+ * 鏈嶅姟鍣ㄥ悕绉�
+ */
+ public String computerName;
+
+ /**
+ * 鏈嶅姟鍣↖p
+ */
+ public String computerIp;
+
+ /**
+ * 椤圭洰璺緞
+ */
+ public String userDir;
+
+ /**
+ * 鎿嶄綔绯荤粺
+ */
+ public String osName;
+
+ /**
+ * 绯荤粺鏋舵瀯
+ */
+ public String osArch;
+
+ public String toString(){
+ StringBuilder sb = new StringBuilder() ;
+ sb.append(" 绯荤粺淇℃伅锛歕n");
+ sb.append(" 涓绘満鍚嶇О锛�" + computerName + "\n");
+ sb.append(" 涓绘満IP锛�" + computerIp + "\n");
+ sb.append(" 椤圭洰璺緞锛�" + userDir + "\n");
+ sb.append(" 鎿嶄綔绯荤粺锛�" + osName + "\n");
+ sb.append(" 绯荤粺鏋舵瀯锛�" + osArch + "\n");
+
+ return sb.toString() ;
+ }
+ }
+}
diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/SysUtil.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysUtil.java
new file mode 100644
index 0000000..91d0ba4
--- /dev/null
+++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/SysUtil.java
@@ -0,0 +1,63 @@
+package com.dy.common.util;
+
+import lombok.Data;
+import oshi.SystemInfo;
+import oshi.hardware.HardwareAbstractionLayer;
+
+import java.util.LinkedList;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/8/2 15:37
+ * @Description
+ */
+public class SysUtil {
+
+ public static InfoVo info() throws Exception {
+ InfoVo vo = new InfoVo() ;
+ SystemInfo si = new SystemInfo();
+ HardwareAbstractionLayer hal = si.getHardware();
+
+ vo.cpuInfoVo = SysCpuUtil.info(hal.getProcessor());
+
+ vo.memInfoVo = SysMemoryUtil.info(hal.getMemory());
+
+ vo.sysInfoVo = SysSysUtil.info();
+
+ vo.jvmInfoVo = SysJvmUtil.info();
+
+ vo.sysFiles = SysFileSysUtil.info(si.getOperatingSystem());
+
+ return vo ;
+ }
+
+ @Data
+ public static class InfoVo {
+ public SysCpuUtil.CpuInfoVo cpuInfoVo ;
+ public SysJvmUtil.JvmInfoVo jvmInfoVo ;
+ public SysMemoryUtil.MemInfoVo memInfoVo ;
+ public LinkedList<SysFileSysUtil.SysFile> sysFiles ;
+ public SysSysUtil.SysInfoVo sysInfoVo ;
+
+ public String toString(){
+ StringBuilder sb = new StringBuilder() ;
+ sb.append("绯荤粺淇℃伅锛歕n");
+ sb.append(" " + (cpuInfoVo != null?cpuInfoVo.toString():"") + "\n");
+ sb.append(" " + (jvmInfoVo != null?jvmInfoVo.toString():"") + "\n");
+ sb.append(" " + (memInfoVo != null?memInfoVo.toString():"") + "\n");
+ if(sysFiles != null && sysFiles.size() > 0){
+ for(SysFileSysUtil.SysFile f : sysFiles){
+ sb.append(" " + f.toString() + "\n");
+ }
+ }
+ sb.append(" " + (sysInfoVo != null?sysInfoVo.toString():"") + "\n");
+
+ return sb.toString() ;
+ }
+ }
+
+ public static void main(String[] args) throws Exception{
+ InfoVo vo = SysUtil.info() ;
+ System.out.println(vo.toString());
+ }
+}
diff --git a/pms-parent/pom.xml b/pms-parent/pom.xml
index 3a9b035..4fd9c87 100644
--- a/pms-parent/pom.xml
+++ b/pms-parent/pom.xml
@@ -62,6 +62,10 @@
<!-- google zxing 鐢熸垚浜岀淮鐮� -->
<google.zxing.version>3.5.3</google.zxing.version>
+ <!-- 鑾峰緱绯荤粺淇℃伅 -->
+ <com.github.oshi-core.version>6.6.2</com.github.oshi-core.version>
+ <net.java.dev.jna.version>5.12.1</net.java.dev.jna.version>
+
<spring-boot-maven-plugin.version>3.2.0</spring-boot-maven-plugin.version>
<maven-jar-plugin-plugin.version>3.3.0</maven-jar-plugin-plugin.version>
<maven-dependency-plugin.version>3.5.0</maven-dependency-plugin.version>
@@ -319,6 +323,30 @@
<scope>import</scope>
</dependency>
+
+ <!-- 鑾峰緱绯荤粺淇℃伅 -->
+ <dependency>
+ <groupId>com.github.oshi</groupId>
+ <artifactId>oshi-core</artifactId>
+ <version>${com.github.oshi-core.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ <dependency>
+ <groupId>net.java.dev.jna</groupId>
+ <artifactId>jna</artifactId>
+ <version>${net.java.dev.jna.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ <dependency>
+ <groupId>net.java.dev.jna</groupId>
+ <artifactId>jna-platform</artifactId>
+ <version>${net.java.dev.jna.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+
<!-- Hutool鏄竴涓皬鑰屽叏鐨凧ava宸ュ叿绫诲簱(https://github.com/dromara/hutool) -->
<dependency>
<groupId>cn.hutool</groupId>
--
Gitblit v1.8.0