package com.product.admin.util;
|
|
import cn.hutool.core.util.NumberUtil;
|
import com.product.core.spring.context.SpringMVCContextHolder;
|
import com.sun.management.OperatingSystemMXBean;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.PostConstruct;
|
import java.io.File;
|
import java.io.IOException;
|
import java.lang.management.ManagementFactory;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.text.DecimalFormat;
|
import java.time.LocalTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@Component
|
public class MonitorInfoUtil extends Thread {
|
static List<MonitorInfoBean> monitorInfoBeanList = new ArrayList<>();
|
|
public List<MonitorInfoBean> getMonitorInfoBeanList() {
|
return this.monitorInfoBeanList;
|
}
|
|
public void removeMonitorInfoList() {
|
monitorInfoBeanList.clear();
|
}
|
|
@PostConstruct
|
private void runThread() {
|
this.start();
|
}
|
|
|
public void run() {
|
while (true) {
|
try {
|
IMonitorService service = new MonitorServiceImpl();
|
MonitorInfoBean monitorInfo = service.getMonitorInfoBean();
|
// System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
|
// System.out.println("cpu占有率=" + monitorInfo.getCpuRatio());
|
//// System.out.println("可使用内存=" + monitorInfo.getTotalMemory());
|
// System.out.println("操作系统=" + monitorInfo.getOsName());
|
// System.out.println("总的物理内存=" + monitorInfo.getTotalMemorySize() + "GB");
|
// System.out.println("剩余的物理内存=" + monitorInfo.getFreePhysicalMemorySize() + "GB");
|
// System.out.println("已使用的物理内存=" + monitorInfo.getUsedMemory() + "GB");
|
// System.out.println("线程总数=" + monitorInfo.getTotalThread());
|
// System.out.println("内存使用率=" + monitorInfo.getMemoryRate() + "%");
|
// System.out.println(monitorInfoBeanList.size());
|
synchronized (monitorInfoBeanList) {
|
this.monitorInfoBeanList.add(monitorInfo);
|
while (this.monitorInfoBeanList.size() > 20) {
|
this.monitorInfoBeanList.remove(0);
|
}
|
}
|
Thread.sleep(5000);
|
} catch (Exception e) {
|
SpringMVCContextHolder.getSystemLogger().error(e);
|
}
|
}
|
|
}
|
|
/**
|
* 获取系统各个硬盘的总容量、已经使用的容量、剩余容量和使用率
|
*
|
* @throws IOException
|
*/
|
public static List<Map<String, Object>> getDiskInfo() {
|
DecimalFormat df = new DecimalFormat("#0.00");
|
File[] disks = File.listRoots();
|
List<Map<String, Object>> result = new ArrayList<>();
|
for (File file : disks) {
|
// 获取总容量
|
long totalSpace = file.getTotalSpace();
|
if (totalSpace == 0L) {
|
continue;
|
}
|
// 获取剩余容量
|
long usableSpace = file.getUsableSpace();
|
// 获取已经使用的容量
|
long freeSpace = totalSpace - usableSpace;
|
// 获取使用率
|
float useRate = (float) ((freeSpace * 1.0 / totalSpace) * 100);
|
// SpringMVCContextHolder.getSystemLogger().error("总容量: " + transformation(totalSpace));
|
// SpringMVCContextHolder.getSystemLogger().error("已经使用: " + transformation(freeSpace));
|
// SpringMVCContextHolder.getSystemLogger().error("剩余容量: " + transformation(usableSpace));
|
// SpringMVCContextHolder.getSystemLogger().error("使用率: " + Double.parseDouble(df.format(useRate)) + "% ");
|
Map<String, Object> map = new HashMap<>();
|
map.put("totalCapacity", transformation(totalSpace));
|
map.put("useCapacity", transformation(freeSpace));
|
map.put("surplusCapacity", transformation(usableSpace));
|
map.put("useRate", Double.parseDouble(df.format(useRate)));
|
try {
|
//盘符
|
map.put("drive", file.getCanonicalPath().replace(":", "").replaceAll("\\\\", ""));
|
} catch (Exception e) {
|
continue;
|
}
|
result.add(map);
|
|
|
}
|
return result;
|
}
|
|
/**
|
* 将字节容量转化为GB
|
*/
|
public static double transformation(long size) {
|
return NumberUtil.round(size / 1024.00 / 1024.00 / 1024.00, 2).doubleValue();
|
}
|
|
public static class MonitorInfoBean {
|
private List<Map<String, Object>> diskInfo;
|
|
private int availableProcessors;
|
/**
|
* 剩余内存.
|
*/
|
private double freeMemory;
|
/**
|
* 操作系统.
|
*/
|
private String osName;
|
/**
|
* 总的物理内存.
|
*/
|
private double totalMemorySize;
|
/**
|
* 剩余的物理内存.
|
*/
|
private double freePhysicalMemorySize;
|
/**
|
* 已使用的物理内存.
|
*/
|
private double usedMemory;
|
/**
|
* 线程总数.
|
*/
|
private int totalThread;
|
/**
|
* cpu使用率.
|
*/
|
private double cpuRatio;
|
|
private double memoryRate;
|
|
private String time;
|
|
public int getAvailableProcessors() {
|
return Runtime.getRuntime().availableProcessors();
|
}
|
|
public List<Map<String, Object>> getDiskInfo() {
|
return diskInfo;
|
}
|
|
public void setDiskInfo(List<Map<String, Object>> diskInfo) {
|
this.diskInfo = diskInfo;
|
}
|
|
public double getMemoryRate() {
|
return NumberUtil.round(NumberUtil.div(usedMemory, totalMemorySize) * 100, 2).doubleValue();
|
}
|
|
public void setMemoryRate(double memoryRate) {
|
this.memoryRate = memoryRate;
|
}
|
|
public String getTime() {
|
return time;
|
}
|
|
public void setTime(String time) {
|
this.time = time;
|
}
|
|
public double getFreeMemory() {
|
return freeMemory;
|
}
|
|
public void setFreeMemory(double freeMemory) {
|
this.freeMemory = freeMemory;
|
}
|
|
public double getFreePhysicalMemorySize() {
|
return freePhysicalMemorySize;
|
}
|
|
public void setFreePhysicalMemorySize(double freePhysicalMemorySize) {
|
this.freePhysicalMemorySize = freePhysicalMemorySize;
|
}
|
|
public String getOsName() {
|
return osName;
|
}
|
|
public void setOsName(String osName) {
|
this.osName = osName;
|
}
|
|
public double getTotalMemorySize() {
|
return totalMemorySize;
|
}
|
|
public void setTotalMemorySize(double totalMemorySize) {
|
this.totalMemorySize = totalMemorySize;
|
}
|
|
public int getTotalThread() {
|
return totalThread;
|
}
|
|
public void setTotalThread(int totalThread) {
|
this.totalThread = totalThread;
|
}
|
|
public double getUsedMemory() {
|
return usedMemory;
|
}
|
|
public void setUsedMemory(double usedMemory) {
|
this.usedMemory = usedMemory;
|
}
|
|
public double getCpuRatio() {
|
return cpuRatio;
|
}
|
|
public void setCpuRatio(double cpuRatio) {
|
this.cpuRatio = cpuRatio;
|
}
|
}
|
|
//之后,建立bean的接口
|
|
public interface IMonitorService {
|
public MonitorInfoBean getMonitorInfoBean();
|
}
|
|
//然后,就是最关键的,得到cpu的利用率,已用内存,可用内存,最大内存等信息。
|
|
|
/**
|
* 获取系统信息的业务逻辑实现类.
|
*/
|
public static class MonitorServiceImpl implements IMonitorService {
|
private static final int CPUTIME = 30;
|
private static final int PERCENT = 100;
|
private static final int FAULTLENGTH = 10;
|
private static final File versionFile = new File("/proc/version");
|
private static String linuxVersion = null;
|
|
/**
|
* 获得当前的监控对象.
|
*
|
* @return 返回构造好的监控对象
|
* @throws Exception
|
*/
|
public MonitorInfoBean getMonitorInfoBean() {
|
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
|
.getOperatingSystemMXBean();
|
// 操作系统
|
String osName = System.getProperty("os.name");
|
// 总的物理内存
|
double totalMemorySize = transformation(osmxb.getTotalPhysicalMemorySize());
|
// System.out.println("计算总的物理内存耗时:" + timer.intervalMs() + "ms");
|
// 剩余的物理内存
|
double freePhysicalMemorySize = transformation(osmxb.getFreePhysicalMemorySize());
|
// 已使用的物理内存
|
double usedMemory = transformation((osmxb.getTotalPhysicalMemorySize() - osmxb
|
.getFreePhysicalMemorySize()));
|
// 获得线程总数
|
ThreadGroup parentThread;
|
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
|
.getParent() != null; parentThread = parentThread.getParent())
|
;
|
int totalThread = parentThread.activeCount();
|
|
double cpuRatio = new BigDecimal(osmxb.getSystemCpuLoad() * 100).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
List<Map<String, Object>> diskInfo = getDiskInfo();
|
// 构造返回对象
|
MonitorInfoBean infoBean = new MonitorInfoBean();
|
infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);
|
infoBean.setOsName(osName);
|
infoBean.setTotalMemorySize(totalMemorySize);
|
infoBean.setTotalThread(totalThread);
|
infoBean.setUsedMemory(usedMemory);
|
infoBean.setCpuRatio(cpuRatio);
|
infoBean.setDiskInfo(diskInfo);
|
LocalTime now2 = LocalTime.now();
|
infoBean.setTime(now2.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
|
return infoBean;
|
}
|
|
|
}
|
|
// 其中,Bytes类用来处理字符串
|
public static class Bytes {
|
public static String substring(String src, int start_idx, int end_idx) {
|
byte[] b = src.getBytes();
|
String tgt = "";
|
for (int i = start_idx; i <= end_idx; i++) {
|
tgt += (char) b[i];
|
}
|
return tgt;
|
}
|
}
|
|
}
|