parent
e85aa59882
commit
f6a541aeb7
@ -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(){}
|
||||
|
||||
}
|
@ -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 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<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();
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in new issue