From f6a541aeb7f81fd3fb9c5c3aaeb8ff61b66b5ff9 Mon Sep 17 00:00:00 2001 From: hiparker Date: Wed, 28 Apr 2021 19:16:12 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E7=9B=91=E6=8E=A7=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 - .../opsli/common/utils/ConvertBytesUtil.java | 72 +++++ .../org/opsli/core/general/StartPrint.java | 9 +- .../core/monitor/SystemHardwareInfo.java | 223 ------------- .../org/opsli/core/monitor/utils/CPU.java | 90 ------ .../org/opsli/core/monitor/utils/JVM.java | 172 ---------- .../org/opsli/core/monitor/utils/Mem.java | 68 ---- .../org/opsli/core/monitor/utils/Sys.java | 56 ---- .../org/opsli/core/monitor/utils/SysFile.java | 65 ---- .../org/opsli/core/utils/SystemInfoUtil.java | 305 ++++++++++++++++++ .../system/monitor/entity/SysServiceInfo.java | 32 -- .../monitor/service/IMonitorService.java | 61 ---- .../service/impl/MonitorServiceImpl.java | 71 ---- .../system/monitor/web/MonitorController.java | 23 +- .../src/main/resources/application-dev.yaml | 2 +- pom.xml | 4 +- 16 files changed, 391 insertions(+), 863 deletions(-) create mode 100644 opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/ConvertBytesUtil.java delete mode 100644 opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/SystemHardwareInfo.java delete mode 100644 opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/CPU.java delete mode 100644 opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/JVM.java delete mode 100644 opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/Mem.java delete mode 100644 opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/Sys.java delete mode 100644 opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/SysFile.java create mode 100644 opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/SystemInfoUtil.java delete mode 100644 opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/entity/SysServiceInfo.java delete mode 100644 opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/service/IMonitorService.java delete mode 100644 opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/service/impl/MonitorServiceImpl.java diff --git a/README.md b/README.md index f2e8e66..a2c6f57 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,6 @@ │ │ │ │ ├── general 核心模块 - 打印信息 │ │ │ │ ├── handler 核心模块 - 异常拦截处理 │ │ │ │ ├── listener 核心模块 - 系统监听器 -│ │ │ │ ├── monitor 核心模块 - 系统监控 │ │ │ │ ├── msg 核心模块 - 信息 │ │ │ │ ├── persistence 核心模块 - 查询条件构造器 │ │ │ │ │ └── querybuilder diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/ConvertBytesUtil.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/ConvertBytesUtil.java new file mode 100644 index 0000000..36dd766 --- /dev/null +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/ConvertBytesUtil.java @@ -0,0 +1,72 @@ +/** + * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.opsli.common.utils; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.common.utils + * @Author: Parker + * @CreateTime: 2020-09-19 23:21 + * @Description: 驼峰转换 + */ +public final class ConvertBytesUtil { + + private static final long KB = 1024; + private static final long MB = KB * 1024; + private static final long GB = MB * 1024; + + /** + * 字节转换 + * + * @param size 字节大小 + * @return 转换后值 + */ + public static String convertFileSizeToString(long size) { + float fileSize = convertFileSize(size); + if (size >= GB) { + return String.format("%.1f GB" , fileSize); + } else if (size >= MB) { + return String.format(fileSize > 100 ? "%.0f MB" : "%.1f MB" , fileSize); + } else if (size >= KB) { + float f = (float) size / KB; + return String.format(fileSize > 100 ? "%.0f KB" : "%.1f KB" , fileSize); + } else { + return String.format("%d B" , size); + } + } + + /** + * 字节转换 + * + * @param size 字节大小 + * @return 转换后值 + */ + public static float convertFileSize(long size) { + if (size >= GB) { + return (float) size / GB; + } else if (size >= MB) { + return (float) size / MB; + } else if (size >= KB) { + return (float) size / KB; + } else { + return size; + } + } + + + private ConvertBytesUtil(){} + +} diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/general/StartPrint.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/general/StartPrint.java index 4328b3d..55fbca4 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/general/StartPrint.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/general/StartPrint.java @@ -18,6 +18,7 @@ package org.opsli.core.general; import cn.hutool.core.date.BetweenFormatter; import cn.hutool.core.date.DateUtil; import cn.hutool.core.lang.Console; +import cn.hutool.core.net.NetUtil; import cn.hutool.core.thread.ThreadUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -26,8 +27,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.Date; import java.util.concurrent.TimeUnit; @@ -87,11 +86,7 @@ public class StartPrint { * @return String */ public String getBasePath(){ - String ip = "127.0.0.1"; - try { - ip = InetAddress.getLocalHost().getHostAddress(); - }catch (UnknownHostException ignored){} - return ip + ":" + serverPort + serverContextPath; + return NetUtil.getLocalhostStr() + ":" + serverPort + serverContextPath; } /** diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/SystemHardwareInfo.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/SystemHardwareInfo.java deleted file mode 100644 index 46d8231..0000000 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/SystemHardwareInfo.java +++ /dev/null @@ -1,223 +0,0 @@ -/** - * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com - *

- * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.opsli.core.monitor; - -import cn.hutool.core.net.NetUtil; -import cn.hutool.core.util.NumberUtil; -import com.google.common.collect.Lists; -import lombok.Data; -import org.opsli.core.monitor.utils.*; -import oshi.SystemInfo; -import oshi.hardware.CentralProcessor; -import oshi.hardware.GlobalMemory; -import oshi.hardware.HardwareAbstractionLayer; -import oshi.software.os.FileSystem; -import oshi.software.os.OSFileStore; -import oshi.software.os.OperatingSystem; -import oshi.util.Util; - -import java.io.Serializable; -import java.util.List; -import java.util.Properties; -/** - * 系统监控 - * - * @author 薛佳琪 - */ -@Data -public class SystemHardwareInfo implements Serializable { - - private static final long serialVersionUID = 1L; - - /** 监控等待时间 */ - private static final int OSHI_WAIT_SECOND = 1000; - - /** - * CPU相关信息 - */ - private CPU cpu = new CPU(); - - /** - * 內存相关信息 - */ - private Mem mem = new Mem(); - - /** - * JVM相关信息 - */ - private JVM jvm = new JVM(); - - /** - * 服务器相关信息 - */ - private Sys sys = new Sys(); - - /** - * 磁盘相关信息 - */ - private List sysFiles = Lists.newLinkedList(); - - - public void copyTo() { - SystemInfo si = new SystemInfo(); - HardwareAbstractionLayer hal = si.getHardware(); - OperatingSystem operatingSystem = si.getOperatingSystem(); - setCpuInfo(hal.getProcessor()); - - setMemInfo(hal.getMemory()); - - setSysInfo(operatingSystem); - - setJvmInfo(); - - setSysFiles(operatingSystem); - } - - - public void copyToCupInfo() { - SystemInfo si = new SystemInfo(); - HardwareAbstractionLayer hal = si.getHardware(); - setCpuInfo(hal.getProcessor()); - } - public void copyToMemInfo() { - SystemInfo si = new SystemInfo(); - HardwareAbstractionLayer hal = si.getHardware(); - setMemInfo(hal.getMemory()); - } - public void copyToSysInfo() { - SystemInfo si = new SystemInfo(); - setSysInfo(si.getOperatingSystem()); - } - public void copyToJvmInfo() { - setJvmInfo(); - } - public void copyToSysFilesInfo() { - SystemInfo si = new SystemInfo(); - setSysFiles(si.getOperatingSystem()); - } - - - - /** - * 设置CPU信息 - */ - private void setCpuInfo(CentralProcessor processor) { - // CPU信息 - long[] prevTicks = processor.getSystemCpuLoadTicks(); - - // OSHI 等待睡眠 - Util.sleep(OSHI_WAIT_SECOND); - - 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; - cpu.setCpuNum(processor.getLogicalProcessorCount()); - cpu.setCpuName(processor.getName()); - cpu.setTotal(totalCpu); - cpu.setSys(cSys); - cpu.setUsed(user); - cpu.setWait(ioWait); - cpu.setFree(idle); - } - - /** - * 设置内存信息 - */ - private void setMemInfo(GlobalMemory memory) { - mem.setTotal(memory.getTotal()); - mem.setUsed(memory.getTotal() - memory.getAvailable()); - mem.setFree(memory.getAvailable()); - } - - /** - * 设置服务器信息 - */ - private void setSysInfo(OperatingSystem operatingSystem) { - Properties props = System.getProperties(); - sys.setComputerName(operatingSystem.getNetworkParams().getHostName()); - sys.setComputerIp(NetUtil.getLocalhostStr()); - sys.setOsName(props.getProperty("os.name")); - sys.setOsArch(props.getProperty("os.arch")); - sys.setUserDir(props.getProperty("user.dir")); - } - - /** - * 设置Java虚拟机 - */ - private void setJvmInfo() { - Properties props = System.getProperties(); - jvm.setTotal(Runtime.getRuntime().totalMemory()); - jvm.setMax(Runtime.getRuntime().maxMemory()); - jvm.setFree(Runtime.getRuntime().freeMemory()); - jvm.setVersion(props.getProperty("java.version")); - jvm.setHome(props.getProperty("java.home")); - jvm.setVendor(props.getProperty("java.vendor")); - jvm.setVendorUrl(props.getProperty("java.vendor.url")); - } - - /** - * 设置磁盘信息 - */ - private void setSysFiles(OperatingSystem os) { - FileSystem fileSystem = os.getFileSystem(); - 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(NumberUtil.mul(NumberUtil.div(used, total, 4), 100)); - sysFiles.add(sysFile); - } - } - - /** - * 字节转换 - * - * @param size 字节大小 - * @return 转换后值 - */ - public 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); - } - } - -} diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/CPU.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/CPU.java deleted file mode 100644 index e158e57..0000000 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/CPU.java +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com - *

- * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.opsli.core.monitor.utils; - -import cn.hutool.core.util.NumberUtil; -import lombok.Data; - -import java.io.Serializable; -import java.math.BigDecimal; - -/** - * 系统监控 CPU - * - * @author 薛佳琪 - */ -@Data -public class CPU implements Serializable { - - private static final long serialVersionUID = 1L; - - /** - * 核心名称 - */ - private String cpuName; - /** - * 核心数 - */ - private int cpuNum; - - /** - * CPU总使用率 - */ - private double total; - - /** - * CPU系统使用率 - */ - private double sys; - - /** - * CPU用户使用率 - */ - private double used; - - /** - * CPU当前等待率 - */ - private double wait; - - /** - * CUP当前空闲率 - */ - private double free; - - // ================= - - public double getTotal() { - return NumberUtil.round(NumberUtil.mul(total, 100), 2).doubleValue(); - } - - public double getSys() { - return NumberUtil.round(NumberUtil.mul(sys / total, 100), 2).doubleValue(); - } - - public double getUsed() { - return NumberUtil.round(NumberUtil.mul(used / total, 100), 2).doubleValue(); - } - - public double getWait() { - return NumberUtil.round(NumberUtil.mul(wait / total, 100), 2).doubleValue(); - } - - public double getFree() { - return NumberUtil.round(NumberUtil.mul(free / total, 100), 2).doubleValue(); - } - -} diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/JVM.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/JVM.java deleted file mode 100644 index 3eaa900..0000000 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/JVM.java +++ /dev/null @@ -1,172 +0,0 @@ -/** - * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com - *

- * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.opsli.core.monitor.utils; - -import cn.hutool.core.date.DateUnit; -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.NumberUtil; -import lombok.Data; - -import java.io.Serializable; -import java.lang.management.ManagementFactory; -import java.util.Date; -/** - * 系统监控 JVM - * - * @author 薛佳琪 - */ -@Data -public class JVM implements Serializable { - - private static final long serialVersionUID = 1L; - - /** - * 当前JVM占用的内存总数(M) - */ - private double total; - - /** - * JVM最大可用内存总数(M) - */ - private double max; - - /** - * JVM空闲内存(M) - */ - private double free; - - /** - * JDK版本 - */ - private String version; - - /** - * JDK路径 - */ - private String home; - - /** - * 已使用 - */ - private Double used; - - /** - * 使用率 - */ - private Double usage; - - /** - * JDK启动时间 - */ - private String startTime; - - /** - * JDK运行时间 - */ - private String runTime; - - - /** Java的运行环境供应商 */ - private String vendor; - - /** - * Java的运行环境供应商 URL - */ - private String vendorUrl; - - // ============================= - - public double getTotal() { - return NumberUtil.div(total, (1024 * 1024), 2); - } - - public double getMax() { - return NumberUtil.div(max, (1024 * 1024), 2); - } - - public double getFree() { - return NumberUtil.div(free, (1024 * 1024), 2); - } - - public double getUsed() { - return NumberUtil.div(total - free, (1024 * 1024), 2); - } - - public String getVersion() { - return version; - } - - public String getHome() { - return home; - } - - public double getUsage() { - return NumberUtil.mul(NumberUtil.div(total - free, total, 4), 100); - } - - public String getVendor() { - return vendor; - } - - public void setVendor(String vendor) { - this.vendor = vendor; - } - - public String getVendorUrl() { - return vendorUrl; - } - - public void setVendorUrl(String vendorUrl) { - this.vendorUrl = vendorUrl; - } - - /** - * 获取 JVM 虚拟机名称 - */ - public String getName() { - return ManagementFactory.getRuntimeMXBean().getVmName(); - } - - - /** - * JDK启动时间 - */ - public String getStartTime() { - long time = ManagementFactory.getRuntimeMXBean().getStartTime(); - Date date = new Date(time); - return DateUtil.formatDateTime(date); - } - - /** - * JDK运行时间 - */ - public String getRunTime() { - long time = ManagementFactory.getRuntimeMXBean().getStartTime(); - Date date = new Date(time); - - //运行多少分钟 - long runMS = DateUtil.between(date, new Date(), DateUnit.MS); - - long nd = 1000 * 24 * 60 * 60; - long nh = 1000 * 60 * 60; - long nm = 1000 * 60; - - long day = runMS / nd; - long hour = runMS % nd / nh; - long min = runMS % nd % nh / nm; - return day + "天" + hour + "小时" + min + "分钟"; - } -} diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/Mem.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/Mem.java deleted file mode 100644 index 6b941ff..0000000 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/Mem.java +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com - *

- * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.opsli.core.monitor.utils; - -import cn.hutool.core.util.NumberUtil; -import lombok.Data; - -import java.io.Serializable; -/** - * 系统监控 内存 - * - * @author 薛佳琪 - */ -@Data -public class Mem implements Serializable { - - private static final long serialVersionUID = 1L; - - /** - * 内存总量 - */ - private double total; - - /** - * 已用内存 - */ - private double used; - - /** - * 剩余内存 - */ - private double free; - - /** - * 使用率 - */ - private double usage; - - public double getTotal() { - return NumberUtil.div(total, (1024 * 1024 * 1024), 2); - } - - public double getUsed() { - return NumberUtil.div(used, (1024 * 1024 * 1024), 2); - } - - - public double getFree() { - return NumberUtil.div(free, (1024 * 1024 * 1024), 2); - } - - public double getUsage() { - return NumberUtil.mul(NumberUtil.div(used, total, 4), 100); - } -} diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/Sys.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/Sys.java deleted file mode 100644 index 83d1f4a..0000000 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/Sys.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com - *

- * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.opsli.core.monitor.utils; - -import lombok.Data; - -import java.io.Serializable; - -/** - * 系统监控 系统信息 - * - * @author 薛佳琪 - */ -@Data -public class Sys implements Serializable { - - private static final long serialVersionUID = 1L; - - /** - * 服务器名称 - */ - private String computerName; - - /** - * 服务器Ip - */ - private String computerIp; - - /** - * 项目路径 - */ - private String userDir; - - /** - * 操作系统 - */ - private String osName; - - /** - * 系统架构 - */ - private String osArch; -} diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/SysFile.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/SysFile.java deleted file mode 100644 index e4b7d86..0000000 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/monitor/utils/SysFile.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com - *

- * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.opsli.core.monitor.utils; - -import lombok.Data; - -import java.io.Serializable; -/** - * 系统监控 磁盘监控 - * - * @author 薛佳琪 - */ -@Data -public class SysFile implements Serializable { - - private static final long serialVersionUID = 1L; - - /** - * 盘符路径 - */ - private String dirName; - - /** - * 盘符类型 - */ - private String sysTypeName; - - /** - * 文件类型 - */ - private String typeName; - - /** - * 总大小 - */ - private String total; - - /** - * 剩余大小 - */ - private String free; - - /** - * 已经使用量 - */ - private String used; - - /** - * 资源的使用率 - */ - private double usage; -} diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/SystemInfoUtil.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/SystemInfoUtil.java new file mode 100644 index 0000000..dcf7c0a --- /dev/null +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/SystemInfoUtil.java @@ -0,0 +1,305 @@ +package org.opsli.core.utils; + +import cn.hutool.core.convert.Convert; +import cn.hutool.core.date.BetweenFormatter; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.NumberUtil; +import cn.hutool.system.*; +import cn.hutool.system.oshi.CpuInfo; +import cn.hutool.system.oshi.OshiUtil; +import com.google.common.collect.Lists; +import lombok.Data; +import org.opsli.common.utils.ConvertBytesUtil; +import oshi.SystemInfo; +import oshi.hardware.GlobalMemory; +import oshi.software.os.FileSystem; +import oshi.software.os.OSFileStore; + +import java.lang.management.RuntimeMXBean; +import java.util.List; + + +/** + * 系统信息工具类 + * + * @author 薛佳琪 + * @date 2021年4月28日14:14:35 + */ +public enum SystemInfoUtil { + + /** 实例对象 */ + INSTANCE; + + /** 监控等待时间 */ + private static final int WAITING_TIME = 1000; + + /** + * 获得系统信息 + * @return SysInfo + */ + public SysInfo getSysInfo(){ + OsInfo osInfo = SystemUtil.getOsInfo(); + UserInfo userInfo = SystemUtil.getUserInfo(); + HostInfo hostInfo = SystemUtil.getHostInfo(); + + SysInfo sysInfo = new SysInfo(); + + sysInfo.setComputerName(hostInfo.getName()); + sysInfo.setComputerIp(hostInfo.getAddress()); + sysInfo.setUserName(userInfo.getName()); + sysInfo.setUserDir(userInfo.getCurrentDir()); + sysInfo.setOsArch(osInfo.getArch()); + sysInfo.setOsName(osInfo.getName()); + + return sysInfo; + } + + /** + * 获得磁盘信息 + * @return List DiskInfo + */ + public List getDiskInfo(){ + List diskInfoList = Lists.newArrayList(); + SystemInfo si = new SystemInfo(); + + FileSystem fileSystem = si.getOperatingSystem().getFileSystem(); + List fileStores = fileSystem.getFileStores(); + for (OSFileStore fs : fileStores) { + long free = fs.getUsableSpace(); + long total = fs.getTotalSpace(); + long used = total - free; + + DiskInfo diskInfo = new DiskInfo(); + diskInfo.setDiskName(fs.getMount()); + diskInfo.setDiskType(fs.getType()); + diskInfo.setFileName(fs.getName()); + diskInfo.setTotal(ConvertBytesUtil.convertFileSizeToString(total)); + diskInfo.setFree(ConvertBytesUtil.convertFileSizeToString(free)); + diskInfo.setUsed(ConvertBytesUtil.convertFileSizeToString(used)); + diskInfo.setUsage(NumberUtil.mul(NumberUtil.div(used, total, 4), 100)); + diskInfoList.add(diskInfo); + } + + return diskInfoList; + } + + + /** + * 获得内存信息 + * @return MemoryInfo + */ + public MemoryInfo getMemoryInfo(){ + MemoryInfo memoryInfo = new MemoryInfo(); + GlobalMemory memory = OshiUtil.getMemory(); + if(memory != null){ + String total = Convert.toStr(memory.getTotal()); + String used = Convert.toStr(memory.getTotal() - memory.getAvailable()); + + memoryInfo.setTotal( + ConvertBytesUtil + .convertFileSizeToString(memory.getTotal())); + memoryInfo.setUsed( + ConvertBytesUtil + .convertFileSizeToString(memory.getTotal() - memory.getAvailable())); + memoryInfo.setFree( + ConvertBytesUtil + .convertFileSizeToString(memory.getAvailable())); + memoryInfo.setUsage( + NumberUtil.mul(NumberUtil.div(used, total, 4), 100).doubleValue()); + } + return memoryInfo; + } + + /** + * 获得CPU信息 + * @return CpuInfo + */ + public CpuInfo getCpuInfo(){ + return OshiUtil.getCpuInfo(WAITING_TIME); + } + + /** + * 获得Jvm信息 + * @return JvmInfo + */ + public JvmInfo getJvmInfo(){ + JavaInfo javaInfo = SystemUtil.getJavaInfo(); + JavaRuntimeInfo javaRuntimeInfo = SystemUtil.getJavaRuntimeInfo(); + RuntimeMXBean runtimeMxBean = SystemUtil.getRuntimeMXBean(); + + + long total = Runtime.getRuntime().totalMemory(); + long max = Runtime.getRuntime().maxMemory(); + long free = Runtime.getRuntime().freeMemory(); + long used = total - free; + + JvmInfo jvmInfo = new JvmInfo(); + + // Jvm 信息 + jvmInfo.setTotal( + ConvertBytesUtil + .convertFileSizeToString(total)); + jvmInfo.setMax( + ConvertBytesUtil + .convertFileSizeToString(max)); + jvmInfo.setFree( + ConvertBytesUtil + .convertFileSizeToString(free)); + jvmInfo.setUsed( + ConvertBytesUtil + .convertFileSizeToString(used)); + jvmInfo.setUsage( + NumberUtil.mul( + NumberUtil.div( + Convert.toStr(used), Convert.toStr(total), 4) + , 100).doubleValue()); + + // Java 供应商 + jvmInfo.setVendor(javaInfo.getVendor()); + jvmInfo.setVendorUrl(javaInfo.getVendorURL()); + // Java 安装目录 + jvmInfo.setHome(javaRuntimeInfo.getHomeDir()); + // Java 版本 + jvmInfo.setVersion(javaInfo.getVersion()); + + // JDK 启动时间戳 + long startTime = runtimeMxBean.getStartTime(); + // JDK 运行时间 + String runTimed = DateUtil.formatBetween( + DateUtil.date(startTime), DateUtil.date(), BetweenFormatter.Level.SECOND); + + + // 虚拟机名称 + jvmInfo.setJvmName(runtimeMxBean.getVmName()); + + // JDK 启动时间 + jvmInfo.setStartTime( + DateUtil.formatDateTime(DateUtil.date(startTime))); + + // JDK 运行时间 + jvmInfo.setRunTime(runTimed); + + return jvmInfo; + } + + // ====================================== + + /** + * 内存信息 静态内部类 + */ + @Data + private static class MemoryInfo { + + /** 内存总量 */ + private String total; + + /** 已用内存 */ + private String used; + + /** 剩余内存 */ + private String free; + + /** 使用率 */ + private double usage; + + } + + /** + * Jvm信息 静态内部类 + */ + @Data + public static class JvmInfo { + + /** 虚拟机名称 */ + private String jvmName; + + /** 当前JVM占用的内存总数(M) */ + private String total; + + /** JVM最大可用内存总数(M) */ + private String max; + + /** JVM空闲内存(M) */ + private String free; + + /** 已使用 */ + private String used; + + /** 使用率 */ + private double usage; + + /** JDK版本 */ + private String version; + + /** JDK路径 */ + private String home; + + /** Java的运行环境供应商 */ + private String vendor; + + /** Java的运行环境供应商 URL */ + private String vendorUrl; + + /** JDK启动时间 */ + private String startTime; + + /** JDK运行时间 */ + private String runTime; + + } + + /** + * 系统信息 + */ + @Data + public static class SysInfo { + + /** 服务器名称 */ + private String computerName; + + /** 服务器Ip */ + private String computerIp; + + /** 启动用户名 */ + private String userName; + + /** 项目路径 */ + private String userDir; + + /** 操作系统 */ + private String osName; + + /** 系统架构 */ + private String osArch; + + } + + /** + * 磁盘监控 + */ + @Data + public static class DiskInfo { + + /** 盘符路径 */ + private String diskName; + + /** 盘符类型 */ + private String diskType; + + /** 文件系统 */ + private String fileName; + + /** 总大小 */ + private String total; + + /** 剩余大小 */ + private String free; + + /** 已经使用量 */ + private String used; + + /** 资源的使用率 */ + private double usage; + } + +} diff --git a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/entity/SysServiceInfo.java b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/entity/SysServiceInfo.java deleted file mode 100644 index 4a18ab4..0000000 --- a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/entity/SysServiceInfo.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com - *

- * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.opsli.modulars.system.monitor.entity; - -import lombok.Data; - -/** - * 系统监控 - * - * @author 薛佳琪 - */ -@Data -public class SysServiceInfo { - - private String name; - - private Object value; - -} diff --git a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/service/IMonitorService.java b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/service/IMonitorService.java deleted file mode 100644 index 7095909..0000000 --- a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/service/IMonitorService.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com - *

- * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.opsli.modulars.system.monitor.service; - -import org.opsli.core.monitor.utils.*; - -import java.util.List; - - -/** - * 系统监控 - * @author 薛佳琪 - */ -public interface IMonitorService { - - /** - * 获得系统信息 - * @return - */ - Sys getSysInfo(); - - - /** - * 获得CPU信息 - * @return - */ - CPU getCpuInfo(); - - - /** - * 获得内存信息 - * @return - */ - Mem getMemInfo(); - - /** - * 获得JVM信息 - * @return - */ - JVM getJVMInfo(); - - /** - * 获得系统磁盘信息 - * @return - */ - List getSysFiles(); - -} diff --git a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/service/impl/MonitorServiceImpl.java b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/service/impl/MonitorServiceImpl.java deleted file mode 100644 index 279015d..0000000 --- a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/service/impl/MonitorServiceImpl.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com - *

- * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.opsli.modulars.system.monitor.service.impl; - -import org.opsli.core.monitor.SystemHardwareInfo; -import org.opsli.core.monitor.utils.*; -import org.opsli.modulars.system.monitor.service.IMonitorService; -import org.springframework.stereotype.Service; - -import java.util.List; - -/** - * 系统监控 - * - * @author 薛佳琪 - */ -@Service -public class MonitorServiceImpl implements IMonitorService { - - - @Override - public Sys getSysInfo() { - SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo(); - systemHardwareInfo.copyToSysInfo(); - return systemHardwareInfo.getSys(); - } - - - @Override - public CPU getCpuInfo() { - SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo(); - systemHardwareInfo.copyToCupInfo(); - return systemHardwareInfo.getCpu(); - } - - @Override - public Mem getMemInfo() { - SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo(); - systemHardwareInfo.copyToMemInfo(); - return systemHardwareInfo.getMem(); - } - - @Override - public JVM getJVMInfo() { - SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo(); - systemHardwareInfo.copyToJvmInfo(); - return systemHardwareInfo.getJvm(); - } - - @Override - public List getSysFiles() { - SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo(); - systemHardwareInfo.copyToSysFilesInfo(); - return systemHardwareInfo.getSysFiles(); - } - - -} diff --git a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/web/MonitorController.java b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/web/MonitorController.java index b5376f9..d2892b4 100644 --- a/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/web/MonitorController.java +++ b/opsli-modulars/opsli-modulars-system/src/main/java/org/opsli/modulars/system/monitor/web/MonitorController.java @@ -22,10 +22,8 @@ import lombok.extern.slf4j.Slf4j; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.opsli.api.base.result.ResultVo; import org.opsli.common.annotation.ApiRestController; -import org.opsli.modulars.system.monitor.service.IMonitorService; -import org.springframework.beans.factory.annotation.Autowired; +import org.opsli.core.utils.SystemInfoUtil; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; import java.util.Map; @@ -39,9 +37,6 @@ import java.util.Map; @ApiRestController("/sys/monitor") public class MonitorController { - @Autowired - private IMonitorService iMonitorService; - /** * 查询服务器信息 * @return ResultVo @@ -52,15 +47,15 @@ public class MonitorController { public ResultVo getSystemInfo() { Map map = Maps.newHashMapWithExpectedSize(5); //服务器信息 - map.put("systemInfo",iMonitorService.getSysInfo()); + map.put("systemInfo", SystemInfoUtil.INSTANCE.getSysInfo()); //CPU信息 - map.put("cpuInfo",iMonitorService.getCpuInfo()); + map.put("cpuInfo", SystemInfoUtil.INSTANCE.getCpuInfo()); //内存信息 - map.put("memInfo",iMonitorService.getMemInfo()); + map.put("memInfo", SystemInfoUtil.INSTANCE.getMemoryInfo()); //JVM信息 - map.put("JVMInfo",iMonitorService.getJVMInfo()); + map.put("JVMInfo", SystemInfoUtil.INSTANCE.getJvmInfo()); //磁盘信息 - map.put("sysFileInfo",iMonitorService.getSysFiles()); + map.put("sysFileInfo", SystemInfoUtil.INSTANCE.getDiskInfo()); return ResultVo.success(map); } @@ -73,7 +68,7 @@ public class MonitorController { @ApiOperation(value = "当前CPU信息", notes = "当前CPU信息") public ResultVo getCpuInfo() { return ResultVo.success( - iMonitorService.getCpuInfo()); + SystemInfoUtil.INSTANCE.getCpuInfo()); } /** @@ -85,7 +80,7 @@ public class MonitorController { @ApiOperation(value = "当前内存信息", notes = "当前内存信息") public ResultVo getMemInfo() { return ResultVo.success( - iMonitorService.getMemInfo()); + SystemInfoUtil.INSTANCE.getMemoryInfo()); } /** @@ -97,7 +92,7 @@ public class MonitorController { @ApiOperation(value = "当前JVM信息", notes = "当前JVM信息") public ResultVo getJVMInfo() { return ResultVo.success( - iMonitorService.getJVMInfo()); + SystemInfoUtil.INSTANCE.getJvmInfo()); } } diff --git a/opsli-starter/src/main/resources/application-dev.yaml b/opsli-starter/src/main/resources/application-dev.yaml index 2492bde..98bd8b3 100644 --- a/opsli-starter/src/main/resources/application-dev.yaml +++ b/opsli-starter/src/main/resources/application-dev.yaml @@ -8,7 +8,7 @@ spring: #redis 配置 redis: database: 0 - host: 10.0.0.254 + host: 10.0.0.28 password: '123456' port: 6379 diff --git a/pom.xml b/pom.xml index 235c2dd..5ab15fc 100644 --- a/pom.xml +++ b/pom.xml @@ -67,8 +67,8 @@ 1.3.0 3.10.3 - 3.9.1 - 4.5.2 + 5.6.1 + 5.8.0 3.9.0 3.15.0 1.6.2