调整
This commit is contained in:
parent
9cf672b041
commit
b4c1e1d24a
@ -12,36 +12,15 @@
|
||||
|
||||
<properties>
|
||||
<jjwt.version>0.11.5</jjwt.version>
|
||||
<!-- oshi监控需要指定jna版本, 问题详见 https://github.com/oshi/oshi/issues/1040 -->
|
||||
<jna.version>5.8.0</jna.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- 代码生成模块 -->
|
||||
<dependency>
|
||||
<groupId>me.zhengjie</groupId>
|
||||
<artifactId>eladmin-generator</artifactId>
|
||||
<version>1.1</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>me.zhengjie</groupId>
|
||||
<artifactId>eladmin-common</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>me.zhengjie</groupId>
|
||||
<artifactId>eladmin-logging</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- quartz -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-quartz</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- jwt -->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
@ -59,24 +38,24 @@
|
||||
<version>${jjwt.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- linux的管理 -->
|
||||
<dependency>
|
||||
<groupId>ch.ethz.ganymed</groupId>
|
||||
<artifactId>ganymed-ssh2</artifactId>
|
||||
<version>build210</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jcraft</groupId>
|
||||
<artifactId>jsch</artifactId>
|
||||
<version>0.1.55</version>
|
||||
</dependency>
|
||||
<!-- <!– linux的管理 –>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>ch.ethz.ganymed</groupId>-->
|
||||
<!-- <artifactId>ganymed-ssh2</artifactId>-->
|
||||
<!-- <version>build210</version>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.jcraft</groupId>-->
|
||||
<!-- <artifactId>jsch</artifactId>-->
|
||||
<!-- <version>0.1.55</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<!-- 获取系统信息 -->
|
||||
<dependency>
|
||||
<groupId>com.github.oshi</groupId>
|
||||
<artifactId>oshi-core</artifactId>
|
||||
<version>6.6.5</version>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.github.oshi</groupId>-->
|
||||
<!-- <artifactId>oshi-core</artifactId>-->
|
||||
<!-- <version>6.6.5</version>-->
|
||||
<!-- </dependency>-->
|
||||
</dependencies>
|
||||
|
||||
<!-- 打包 -->
|
||||
|
@ -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<String,Object>
|
||||
*/
|
||||
Map<String,Object> getServers();
|
||||
}
|
@ -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<String,Object> getServers(){
|
||||
Map<String, Object> 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<String,Object> getDiskInfo(OperatingSystem os) {
|
||||
Map<String,Object> diskInfo = new LinkedHashMap<>();
|
||||
FileSystem fileSystem = os.getFileSystem();
|
||||
List<OSFileStore> 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<String,Object> getSwapInfo(GlobalMemory memory) {
|
||||
Map<String,Object> 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<String,Object> getMemoryInfo(GlobalMemory memory) {
|
||||
Map<String,Object> 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<String,Object> getCpuInfo(CentralProcessor processor) {
|
||||
Map<String,Object> 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<String,Object> getSystemInfo(OperatingSystem os){
|
||||
Map<String,Object> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user