系统监控优化

v1.4.1
hiparker 4 years ago
parent e85aa59882
commit f6a541aeb7

@ -124,7 +124,6 @@
│ │ │ │ ├── general 核心模块 - 打印信息
│ │ │ │ ├── handler 核心模块 - 异常拦截处理
│ │ │ │ ├── listener 核心模块 - 系统监听器
│ │ │ │ ├── monitor 核心模块 - 系统监控
│ │ │ │ ├── msg 核心模块 - 信息
│ │ │ │ ├── persistence 核心模块 - 查询条件构造器
│ │ │ │ │ └── querybuilder

@ -0,0 +1,72 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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(){}
}

@ -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;
}
/**

@ -1,223 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<SysFile> 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);
}
}
}

@ -1,90 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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();
}
}

@ -1,172 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 + "分钟";
}
}

@ -1,68 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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);
}
}

@ -1,56 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
}

@ -1,65 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
}

@ -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 202142814: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<DiskInfo> getDiskInfo(){
List<DiskInfo> diskInfoList = Lists.newArrayList();
SystemInfo si = new SystemInfo();
FileSystem fileSystem = si.getOperatingSystem().getFileSystem();
List<OSFileStore> 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;
}
}

@ -1,32 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
}

@ -1,61 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<SysFile> getSysFiles();
}

@ -1,71 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<SysFile> getSysFiles() {
SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo();
systemHardwareInfo.copyToSysFilesInfo();
return systemHardwareInfo.getSysFiles();
}
}

@ -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<String,Object> 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());
}
}

@ -8,7 +8,7 @@ spring:
#redis 配置
redis:
database: 0
host: 10.0.0.254
host: 10.0.0.28
password: '123456'
port: 6379

@ -67,8 +67,8 @@
<pagehelper.version>1.3.0</pagehelper.version>
<jwt.version>3.10.3</jwt.version>
<oshi.version>3.9.1</oshi.version>
<jna.version>4.5.2</jna.version>
<oshi.version>5.6.1</oshi.version>
<jna.version>5.8.0</jna.version>
<ehcache.version>3.9.0</ehcache.version>
<redisson.version>3.15.0</redisson.version>
<captcha.version>1.6.2</captcha.version>

Loading…
Cancel
Save