系统监控

v1.4.1
薛佳琪 4 years ago
parent 1ab38c5c8a
commit 6a041e54ad

@ -4,16 +4,26 @@
*/
package org.opsli.core.monitor;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.NumberUtil;
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.LinkedList;
import java.util.List;
import java.util.Properties;
@Data
public class SystemInfo implements Serializable {
public class SystemHardwareInfo implements Serializable {
private static final long serialVersionUID = 1L;
private static final int OSHI_WAIT_SECOND = 1000;
@ -47,18 +57,45 @@ public class SystemInfo implements Serializable {
public void copyTo() throws Exception {
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
OperatingSystem operatingSystem = si.getOperatingSystem();
setCpuInfo(hal.getProcessor());
setMemInfo(hal.getMemory());
setSysInfo();
setSysInfo(operatingSystem);
setJvmInfo();
setSysFiles(operatingSystem);
}
public void copyToCupInof() throws Exception {
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
setCpuInfo(hal.getProcessor());
}
public void copyToMemInof() throws Exception {
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
setMemInfo(hal.getMemory());
}
public void copyToSysInof() throws Exception {
SystemInfo si = new SystemInfo();
setSysInfo(si.getOperatingSystem());
}
public void copyToJvmInof() throws Exception {
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
setJvmInfo();
}
public void copyToSysFilesInof() throws Exception {
SystemInfo si = new SystemInfo();
setSysFiles(si.getOperatingSystem());
}
/**
* CPU
*/
@ -67,16 +104,17 @@ public class SystemInfo implements Serializable {
long[] prevTicks = processor.getSystemCpuLoadTicks();
Util.sleep(OSHI_WAIT_SECOND);
long[] ticks = processor.getSystemCpuLoadTicks();
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 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);
@ -96,9 +134,9 @@ public class SystemInfo implements Serializable {
/**
*
*/
private void setSysInfo() {
private void setSysInfo(OperatingSystem operatingSystem) {
Properties props = System.getProperties();
sys.setComputerName(IpUtil.getHostName());
sys.setComputerName(operatingSystem.getNetworkParams().getHostName());
sys.setComputerIp(NetUtil.getLocalhostStr());
sys.setOsName(props.getProperty("os.name"));
sys.setOsArch(props.getProperty("os.arch"));
@ -134,7 +172,7 @@ public class SystemInfo implements Serializable {
sysFile.setTotal(convertFileSize(total));
sysFile.setFree(convertFileSize(free));
sysFile.setUsed(convertFileSize(used));
sysFile.setUsage(NumberUtil.round(NumberUtil.mul(used, total, 4), 100).doubleValue());
sysFile.setUsage(NumberUtil.mul(NumberUtil.div(used, total, 4), 100));
sysFiles.add(sysFile);
}
}
@ -161,4 +199,5 @@ public class SystemInfo implements Serializable {
return String.format("%d B" , size);
}
}
}

@ -4,5 +4,68 @@
*/
package org.opsli.core.monitor.utils;
public class pu {
import cn.hutool.core.util.NumberUtil;
import lombok.Data;
import java.io.Serializable;
@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();
}
}

@ -4,5 +4,130 @@
*/
package org.opsli.core.monitor.utils;
public class JVM {
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;
@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 name;
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);
}
/**
* JDK
*/
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 + "分钟";
}
}

@ -4,5 +4,49 @@
*/
package org.opsli.core.monitor.utils;
public class Mem {
import cn.hutool.core.util.NumberUtil;
import lombok.Data;
import java.io.Serializable;
@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);
}
}

@ -4,5 +4,36 @@
*/
package org.opsli.core.monitor.utils;
public class Sys {
import lombok.Data;
import java.io.Serializable;
@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;
}

@ -4,5 +4,46 @@
*/
package org.opsli.core.monitor.utils;
public class SysFile {
import lombok.Data;
import java.io.Serializable;
@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;
}

@ -7,10 +7,10 @@ package org.opsli.modulars.system.monitor.entity;
import lombok.Data;
@Data
public class SysInfo {
public class SysServiceInfo {
private String name;
private String value;
private Object value;
}

@ -4,5 +4,23 @@
*/
package org.opsli.modulars.system.monitor.service;
public interface monitorService {
import org.opsli.core.monitor.utils.*;
import java.util.List;
public interface IMonitorService {
public Sys getSysInfo() throws Exception;
public CPU getCpuInfo() throws Exception;
public Mem getMemInfo() throws Exception;
public JVM getJVMInfo() throws Exception;
public List<SysFile> getSysFiles() throws Exception;
}

@ -4,5 +4,62 @@
*/
package org.opsli.modulars.system.monitor.service.impl;
public class monitorServiceImpl {
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;
@Service
public class MonitorServiceImpl implements IMonitorService {
/**
*
* @return
* @throws Exception
*/
@Override
public Sys getSysInfo() throws Exception {
SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo();
systemHardwareInfo.copyToSysInof();
return systemHardwareInfo.getSys();
}
/**
* CPU
* @return
* @throws Exception
*/
@Override
public CPU getCpuInfo() throws Exception {
SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo();
systemHardwareInfo.copyToCupInof();
return systemHardwareInfo.getCpu();
}
@Override
public Mem getMemInfo() throws Exception {
SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo();
systemHardwareInfo.copyToMemInof();
return systemHardwareInfo.getMem();
}
@Override
public JVM getJVMInfo() throws Exception {
SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo();
systemHardwareInfo.copyToJvmInof();
return systemHardwareInfo.getJvm();
}
@Override
public List<SysFile> getSysFiles() throws Exception {
SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo();
systemHardwareInfo.copyToSysFilesInof();
return systemHardwareInfo.getSysFiles();
}
}

@ -4,5 +4,70 @@
*/
package org.opsli.modulars.system.monitor.web;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@ApiRestController("/sys/monitor")
public class monitorController {
@Autowired
private IMonitorService monitorService;
/**
*
* @return ResultVo
*/
@RequestMapping("/getSystemInfo")
@ApiOperation(value = "当前服务器信息", notes = "当前服务器信息")
public ResultVo<?> getSystemInfo(HttpServletRequest request) throws Exception {
Map<String,Object> map = new HashMap<>();
//服务器信息
map.put("systemInfo",monitorService.getSysInfo());
//CPU信息
map.put("cpuInfo",monitorService.getCpuInfo());
//内存信息
map.put("memInfo",monitorService.getMemInfo());
//JVM信息
map.put("JVMInfo",monitorService.getJVMInfo());
//磁盘信息
map.put("sysFileInfo",monitorService.getSysFiles());
return ResultVo.success(map);
}
/**
* CPU
* @return ResultVo
*/
@RequestMapping("/getCpuInfo")
@ApiOperation(value = "当前CPU信息", notes = "当前CPU信息")
public ResultVo<?> getCpuInfo(HttpServletRequest request) throws Exception {
return ResultVo.success(monitorService.getCpuInfo());
}
/**
*
* @return ResultVo
*/
@RequestMapping("/getMemInfo")
@ApiOperation(value = "当前内存信息", notes = "当前内存信息")
public ResultVo<?> getMemInfo(HttpServletRequest request) throws Exception {
return ResultVo.success(monitorService.getMemInfo());
}
/**
* JVM
* @return ResultVo
*/
@RequestMapping("/getJVMInfo")
@ApiOperation(value = "当前JVM信息", notes = "当前JVM信息")
public ResultVo<?> getJVMInfo(HttpServletRequest request) throws Exception {
return ResultVo.success(monitorService.getJVMInfo());
}
}

@ -35,7 +35,7 @@ spring:
master:
url: jdbc:mysql://127.0.0.1:3306/opsli-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&serverTimezone=Asia/Shanghai
username: root
password: 12345678
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
# 多数据源配置
#slave-datasource:

@ -76,6 +76,8 @@
<commons.io.version>2.8.0</commons.io.version>
<guava.version>29.0-jre</guava.version>
<hutool.version>5.4.2</hutool.version>
<oshi.version>3.9.1</oshi.version>
<jna.version>4.5.2</jna.version>
<ehcache.version>3.9.0</ehcache.version>
<kaptcha.version>0.0.9</kaptcha.version>
<shiro.version>1.6.0</shiro.version>
@ -263,6 +265,17 @@
<version>${snakeyaml.version}</version>
</dependency>
<!--OSHI系统监控-->
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>${oshi.version}</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>${jna.version}</version>
</dependency>
<!-- ———————————————————— 集成工具 - 结束 ———————————————————— -->
</dependencies>

Loading…
Cancel
Save