代码很简单 不做过多说明
DecimalFormat DECIMALFORMAT = new DecimalFormat("#.##");
//系统监控
@PostMapping("/systemInformation")
public Results systemInformation() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
String osJson = JSON.toJSONString(operatingSystemMXBean);
System.out.println("osJson is " + osJson);
JSONObject jsonObject = JSON.parseObject(osJson);
SystemInformation systemInformation = new SystemInformation();
//操作系统
systemInformation.setOperatingSystem(jsonObject.getString("name"));
//系统架构
systemInformation.setSystemArchitecture(jsonObject.getString("arch"));
CPU核心数
systemInformation.setSystemCPU(jsonObject.getString("availableProcessors"));
//总物理内存
systemInformation.setTotalPhysicalMemory(DECIMALFORMAT.format((1.0 * jsonObject.getLong("totalPhysicalMemorySize")) / (1024 * 1024 * 1024)));
//可用物理内存
systemInformation.setAvailablePhysicalMemory(DECIMALFORMAT.format(1.0 * jsonObject.getLong("freePhysicalMemorySize") / (1024 * 1024 * 1024)));
//总内存
Long totalPhysicalMemorySize = jsonObject.getLong("totalPhysicalMemorySize");
//剩余内存
Long freePhysicalMemorySize = jsonObject.getLong("freePhysicalMemorySize");
//已使用用物理内存
double totalPhysicalMemorySized = Double.valueOf(DECIMALFORMAT.format(1.0 * totalPhysicalMemorySize / (1024 * 1024 * 1024)));
double freePhysicalMemorySized = Double.valueOf(DECIMALFORMAT.format(1.0 * freePhysicalMemorySize / (1024 * 1024 * 1024)));
double c = Double.valueOf(DECIMALFORMAT.format(totalPhysicalMemorySized - freePhysicalMemorySized));
systemInformation.setAvailablePhysicalMemoryUsed(DECIMALFORMAT.format(totalPhysicalMemorySized - freePhysicalMemorySized));
//内存使用率
double v = Double.valueOf(DECIMALFORMAT.format((totalPhysicalMemorySized - freePhysicalMemorySized) / totalPhysicalMemorySized * 100));
systemInformation.setMemoryUsage(DECIMALFORMAT.format((totalPhysicalMemorySized - freePhysicalMemorySized) / totalPhysicalMemorySized * 100));
//系统CPU占用率
systemInformation.setSystemCpuLoad(DECIMALFORMAT.format(jsonObject.getDouble("systemCpuLoad") * 100));
//进程占用CPU
systemInformation.setProcessProportion(DECIMALFORMAT.format(jsonObject.getDouble("processCpuLoad") * 100));
log.info(String.valueOf(systemInformation));
return Results.success(systemInformation);
}
DecimalFormat DECIMALFORMAT = new DecimalFormat("#.##");
//磁盘监控
@PostMapping("/diskUsage")
public Results diskUsage() {
List<DiskUsages> diskUsagesList = new ArrayList<>();
// 获取磁盘分区列表
File[] roots = File.listRoots();
for (File file : roots) {
DiskUsages diskUsages = new DiskUsages();
//可用空间
Long freeSpace = file.getFreeSpace();
//总空间
Long totalSpace = file.getTotalSpace();
//已使用空间
Long usableSpace = totalSpace - freeSpace;
//盘符
diskUsages.setPath(file.getPath());
//空闲空间
diskUsages.setFreeSpace(file.getFreeSpace() / (1024 * 1024 * 1024) + "GB");
//总空间
diskUsages.setTotalSpace(file.getTotalSpace() / (1024 * 1024 * 1024) + "GB");
//已使用用空间
diskUsages.setUsableSpace(usableSpace / (1024 * 1024 * 1024) + "GB");
//使用百分比
diskUsages.setPercentage(DECIMALFORMAT.format(((double) usableSpace / (double) totalSpace) * 100) + "%");
diskUsagesList.add(diskUsages);
}
log.info(String.valueOf(diskUsagesList));
return Results.success(diskUsagesList);
}