top.doudou.common.tool.system.SystemMonitorUtil Maven / Gradle / Ivy
package top.doudou.common.tool.system;
import com.sun.management.OperatingSystemMXBean;
import lombok.extern.slf4j.Slf4j;
import oshi.SystemInfo;
import oshi.hardware.HWDiskStore;
import oshi.hardware.HardwareAbstractionLayer;
import top.doudou.base.exception.ExceptionUtils;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
/**
* @author 傻男人<[email protected]>
* @Date: 2020/4/20
* @Description: 系统环境监控工具类
*/
@Slf4j
public class SystemMonitorUtil {
private static SystemInfo systemInfo = new SystemInfo();
private static MonitorDto monitorDto = new MonitorDto();
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static DecimalFormat decimalFormat = new DecimalFormat(".00");
static {
monitorDto.setOs(System.getProperties().getProperty("os.name") + " " + System.getProperties().getProperty("os.arch"));
monitorDto.setCpuInfo(systemInfo.getHardware().getProcessor().getName());
monitorDto.setJvmJavaVersion(System.getProperty("java.version"));
monitorDto.setRunTime(simpleDateFormat.format(ManagementFactory.getRuntimeMXBean().getStartTime()));
}
public static MonitorDto getSysMonitor(){
//jvm
MemoryUsage heapInfo = getHeapInfo();
monitorDto.setJvmHeapInit(decimalFormat.format(heapInfo.getInit() / 1024 / 1024));
monitorDto.setJvmHeapMax(decimalFormat.format(heapInfo.getMax() / 1024 / 1024));
monitorDto.setJvmHeapUsed(decimalFormat.format(heapInfo.getUsed() / 1024 / 1024));
monitorDto.setJvmHeapCommitted(decimalFormat.format(heapInfo.getCommitted() / 1024 / 1024));
MemoryUsage noHeapInfo = getNoHeapInfo();
monitorDto.setJvmNonHeapInit(decimalFormat.format(noHeapInfo.getInit() / 1024 / 1024));
monitorDto.setJvmNonHeapMax(decimalFormat.format(noHeapInfo.getMax() / 1024 / 1024));
monitorDto.setJvmNonHeapUsed(decimalFormat.format(noHeapInfo.getUsed() / 1024 / 1024));
monitorDto.setJvmNonHeapCommitted(decimalFormat.format(noHeapInfo.getCommitted() / 1024 / 1024));
//系统信息
monitorDto.setCpuUseRate(decimalFormat.format(getCpuUsage() * 100));
OperatingSystemMXBean memoryUsage = getMemoryUsage();
monitorDto.setRamTotal(decimalFormat.format(memoryUsage.getTotalPhysicalMemorySize() / 1024 / 1024 / 1024));
monitorDto.setRamUsed(decimalFormat.format((memoryUsage.getTotalPhysicalMemorySize() - memoryUsage.getFreePhysicalMemorySize()) / 1024 / 1024 / 1024));
HashMap diskUsage = getDiskUsage();
monitorDto.setDiskTotal(decimalFormat.format(diskUsage.get("total")));
monitorDto.setDiskUsed(decimalFormat.format(diskUsage.get("used")));
return monitorDto;
}
/**
* 获取指定key的系统属性
* @param key
* @return
*/
public static String getProperty(@NotNull String key){
return System.getProperty(key);
}
/**
* 获取jvm中堆内存信息
*/
private static MemoryUsage getHeapInfo(){
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
}
/**
* 获取jvm中非堆内存信息
*/
private static MemoryUsage getNoHeapInfo(){
return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
}
/**
* 获取内存信息
*/
private static OperatingSystemMXBean getMemoryUsage() {
return (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
}
/**
* 获取CPU信息
*/
private static double getCpuUsage() {
return systemInfo.getHardware().getProcessor().getSystemCpuLoadBetweenTicks();
}
/**
* 获取磁盘信息
*/
private static HashMap getDiskUsage() {
HashMap hashMap = new HashMap<>();
File[] files = File.listRoots();
double total = 0;
double used = 0;
for (File file : files) {
total = total + file.getTotalSpace() / 1024 / 1024 / 1024;
used = used + file.getFreeSpace() / 1024 / 1024 / 1024;
}
hashMap.put("total",total);
hashMap.put("used",used);
return hashMap;
}
/**
* 判断系统是否为windows
*
* @return 是否
*/
private static boolean isWindows() {
return System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS");
}
/**
* 获取linux 磁盘使用率
*
* @return 磁盘使用率
*/
private static HashMap getUnixDiskUsage() {
HashMap hashMap = new HashMap<>();
String ioCmdStr = "df -h /";
String resultInfo = runCommand(ioCmdStr);
log.info(resultInfo);
String[] data = resultInfo.split(" +");
double total = Double.parseDouble(data[10].replace("%", ""));
return hashMap;
}
/**
* 获取Windows 磁盘使用率
*
* @return 磁盘使用率
*/
private static HashMap getWinDiskUsage() {
HardwareAbstractionLayer hal = systemInfo.getHardware();
HWDiskStore[] diskStores = hal.getDiskStores();
HashMap hashMap = new HashMap<>();
long total = 0;
long used = 0;
if (diskStores != null && diskStores.length > 0) {
for (HWDiskStore diskStore : diskStores) {
long size = diskStore.getSize();
long writeBytes = diskStore.getWriteBytes();
total += size;
used += writeBytes;
}
}
hashMap.put("total",total);
hashMap.put("used",used);
return hashMap;
}
/**
* Linux 执行系统命令
*
* @param CMD 命令
* @return 字符串结果
*/
private static String runCommand(String CMD) {
StringBuilder info = new StringBuilder();
InputStreamReader isr = null;
LineNumberReader lnr = null;
try {
Process pos = Runtime.getRuntime().exec(CMD);
pos.waitFor();
isr = new InputStreamReader(pos.getInputStream());
lnr = new LineNumberReader(isr);
String line;
while ((line = lnr.readLine()) != null) {
info.append(line).append("\n");
}
} catch (Exception e) {
//输出到日志文件中
log.error(ExceptionUtils.toString(e));
}finally {
try {
assert isr != null;
isr.close();
assert lnr != null;
lnr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return info.toString();
}
}