From b4c1e1d24ae728dda58250e79c47cef20c0a1799 Mon Sep 17 00:00:00 2001 From: tangzh Date: Thu, 3 Jul 2025 19:38:31 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eladmin/eladmin-system/pom.xml | 53 ++--- .../system/service/MonitorService.java | 31 --- .../service/impl/MonitorServiceImpl.java | 194 ------------------ 3 files changed, 16 insertions(+), 262 deletions(-) delete mode 100644 eladmin/eladmin-system/src/main/java/me/zhengjie/modules/system/service/MonitorService.java delete mode 100644 eladmin/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MonitorServiceImpl.java diff --git a/eladmin/eladmin-system/pom.xml b/eladmin/eladmin-system/pom.xml index eb6edc8..acca57b 100644 --- a/eladmin/eladmin-system/pom.xml +++ b/eladmin/eladmin-system/pom.xml @@ -12,36 +12,15 @@ 0.11.5 - - 5.8.0 - - - me.zhengjie - eladmin-generator - 1.1 - - - me.zhengjie - eladmin-common - - - - me.zhengjie eladmin-logging 1.1 - - - org.springframework.boot - spring-boot-starter-quartz - - io.jsonwebtoken @@ -59,24 +38,24 @@ ${jjwt.version} - - - ch.ethz.ganymed - ganymed-ssh2 - build210 - - - com.jcraft - jsch - 0.1.55 - + + + + + + + + + + + - - com.github.oshi - oshi-core - 6.6.5 - + + + + + diff --git a/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/system/service/MonitorService.java b/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/system/service/MonitorService.java deleted file mode 100644 index df7d448..0000000 --- a/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/system/service/MonitorService.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2019-2025 Tz - * - * 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 me.zhengjie.modules.system.service; - -import java.util.Map; - -/** - * @author Tz - * @date 2020-05-02 - */ -public interface MonitorService { - - /** - * 查询数据分页 - * @return Map - */ - Map getServers(); -} diff --git a/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MonitorServiceImpl.java b/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MonitorServiceImpl.java deleted file mode 100644 index d37fc80..0000000 --- a/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MonitorServiceImpl.java +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Copyright 2019-2025 Tz - * - * 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 me.zhengjie.modules.system.service.impl; - -import cn.hutool.core.date.BetweenFormatter.Level; -import cn.hutool.core.date.DateUtil; -import lombok.extern.slf4j.Slf4j; -import me.zhengjie.modules.system.service.MonitorService; -import me.zhengjie.utils.ElConstant; -import me.zhengjie.utils.FileUtil; -import me.zhengjie.utils.StringUtils; -import org.springframework.stereotype.Service; -import oshi.SystemInfo; -import oshi.hardware.*; -import oshi.software.os.FileSystem; -import oshi.software.os.OSFileStore; -import oshi.software.os.OperatingSystem; -import oshi.util.FormatUtil; -import oshi.util.Util; -import java.lang.management.ManagementFactory; -import java.text.DecimalFormat; -import java.util.*; - -/** -* @author Tz -* @date 2020-05-02 -*/ -@Slf4j -@Service -public class MonitorServiceImpl implements MonitorService { - - private final DecimalFormat df = new DecimalFormat("0.00"); - - @Override - public Map getServers(){ - Map resultMap = new LinkedHashMap<>(8); - try { - SystemInfo si = new SystemInfo(); - OperatingSystem os = si.getOperatingSystem(); - HardwareAbstractionLayer hal = si.getHardware(); - // 系统信息 - resultMap.put("sys", getSystemInfo(os)); - // cpu 信息 - resultMap.put("cpu", getCpuInfo(hal.getProcessor())); - // 内存信息 - resultMap.put("memory", getMemoryInfo(hal.getMemory())); - // 交换区信息 - resultMap.put("swap", getSwapInfo(hal.getMemory())); - // 磁盘 - resultMap.put("disk", getDiskInfo(os)); - resultMap.put("time", DateUtil.format(new Date(), "HH:mm:ss")); - } catch (Exception e) { - log.error(e.getMessage(), e); - } - return resultMap; - } - - /** - * 获取磁盘信息 - * @return / - */ - private Map getDiskInfo(OperatingSystem os) { - Map diskInfo = new LinkedHashMap<>(); - FileSystem fileSystem = os.getFileSystem(); - List fsArray = fileSystem.getFileStores(); - String osName = System.getProperty("os.name"); - long available = 0, total = 0; - for (OSFileStore fs : fsArray){ - // windows 需要将所有磁盘分区累加,linux 和 mac 直接累加会出现磁盘重复的问题,待修复 - if(osName.toLowerCase().startsWith(ElConstant.WIN)) { - available += fs.getUsableSpace(); - total += fs.getTotalSpace(); - } else { - available = fs.getUsableSpace(); - total = fs.getTotalSpace(); - break; - } - } - long used = total - available; - diskInfo.put("total", total > 0 ? FileUtil.getSize(total) : "?"); - diskInfo.put("available", FileUtil.getSize(available)); - diskInfo.put("used", FileUtil.getSize(used)); - if(total != 0){ - diskInfo.put("usageRate", df.format(used/(double)total * 100)); - } else { - diskInfo.put("usageRate", 0); - } - return diskInfo; - } - - /** - * 获取交换区信息 - * @param memory / - * @return / - */ - private Map getSwapInfo(GlobalMemory memory) { - Map swapInfo = new LinkedHashMap<>(); - VirtualMemory virtualMemory = memory.getVirtualMemory(); - long total = virtualMemory.getSwapTotal(); - long used = virtualMemory.getSwapUsed(); - swapInfo.put("total", FormatUtil.formatBytes(total)); - swapInfo.put("used", FormatUtil.formatBytes(used)); - swapInfo.put("available", FormatUtil.formatBytes(total - used)); - if(used == 0){ - swapInfo.put("usageRate", 0); - } else { - swapInfo.put("usageRate", df.format(used/(double)total * 100)); - } - return swapInfo; - } - - /** - * 获取内存信息 - * @param memory / - * @return / - */ - private Map getMemoryInfo(GlobalMemory memory) { - Map memoryInfo = new LinkedHashMap<>(); - memoryInfo.put("total", FormatUtil.formatBytes(memory.getTotal())); - memoryInfo.put("available", FormatUtil.formatBytes(memory.getAvailable())); - memoryInfo.put("used", FormatUtil.formatBytes(memory.getTotal() - memory.getAvailable())); - memoryInfo.put("usageRate", df.format((memory.getTotal() - memory.getAvailable())/(double)memory.getTotal() * 100)); - return memoryInfo; - } - - /** - * 获取Cpu相关信息 - * @param processor / - * @return / - */ - private Map getCpuInfo(CentralProcessor processor) { - Map cpuInfo = new LinkedHashMap<>(); - cpuInfo.put("name", processor.getProcessorIdentifier().getName()); - cpuInfo.put("package", processor.getPhysicalPackageCount() + "个物理CPU"); - cpuInfo.put("core", processor.getPhysicalProcessorCount() + "个物理核心"); - cpuInfo.put("coreNumber", processor.getPhysicalProcessorCount()); - cpuInfo.put("logic", processor.getLogicalProcessorCount() + "个逻辑CPU"); - // CPU信息 - long[] prevTicks = processor.getSystemCpuLoadTicks(); - // 默认等待300毫秒... - long time = 300; - Util.sleep(time); - long[] ticks = processor.getSystemCpuLoadTicks(); - while (Arrays.toString(prevTicks).equals(Arrays.toString(ticks)) && time < 1000){ - time += 25; - Util.sleep(25); - ticks = processor.getSystemCpuLoadTicks(); - } - long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()]; - long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()]; - long sys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()]; - long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()]; - long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.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 totalCpu = user + nice + sys + idle + iowait + irq + softirq + steal; - cpuInfo.put("used", df.format(100d * user / totalCpu + 100d * sys / totalCpu)); - cpuInfo.put("idle", df.format(100d * idle / totalCpu)); - return cpuInfo; - } - - /** - * 获取系统相关信息,系统、运行天数、系统IP - * @param os / - * @return / - */ - private Map getSystemInfo(OperatingSystem os){ - Map systemInfo = new LinkedHashMap<>(); - // jvm 运行时间 - long time = ManagementFactory.getRuntimeMXBean().getStartTime(); - Date date = new Date(time); - // 计算项目运行时间 - String formatBetween = DateUtil.formatBetween(date, new Date(), Level.HOUR); - // 系统信息 - systemInfo.put("os", os.toString()); - systemInfo.put("day", formatBetween); - systemInfo.put("ip", StringUtils.getLocalIp()); - return systemInfo; - } -}