[代码优化](v2.6):服务监控兼容Mac Os Big Sur,交换空间查询优化

pull/530/head
ZhengJie 2020-11-16 19:21:28 +08:00
parent 7b6f5af6aa
commit 9591cf3e53
3 changed files with 19 additions and 10 deletions

View File

@ -15,7 +15,7 @@
<properties>
<jjwt.version>0.11.1</jjwt.version>
<!-- oshi监控需要指定jna版本, 问题详见 https://github.com/oshi/oshi/issues/1040 -->
<jna.version>5.5.0</jna.version>
<jna.version>5.6.0</jna.version>
</properties>
<dependencies>
@ -84,7 +84,7 @@
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>5.0.1</version>
<version>5.3.6</version>
</dependency>
</dependencies>

View File

@ -50,7 +50,6 @@ public class MonitorServiceImpl implements MonitorService {
HardwareAbstractionLayer hal = si.getHardware();
// 系统信息
resultMap.put("sys", getSystemInfo(os));
// cpu 信息
resultMap.put("cpu", getCpuInfo(hal.getProcessor()));
// 内存信息
@ -75,9 +74,11 @@ public class MonitorServiceImpl implements MonitorService {
FileSystem fileSystem = os.getFileSystem();
List<OSFileStore> fsArray = fileSystem.getFileStores();
for (OSFileStore fs : fsArray){
diskInfo.put("total", fs.getTotalSpace() > 0 ? FileUtil.getSize(fs.getTotalSpace()) : "?");
long used = fs.getTotalSpace() - fs.getUsableSpace();
diskInfo.put("available", FileUtil.getSize(fs.getUsableSpace()));
long available = fs.getUsableSpace();
long total = fs.getTotalSpace();
long used = total - available;
diskInfo.put("total", total > 0 ? FileUtil.getSize(total) : "?");
diskInfo.put("available", FileUtil.getSize(available));
diskInfo.put("used", FileUtil.getSize(used));
diskInfo.put("usageRate", df.format(used/(double)fs.getTotalSpace() * 100));
}
@ -91,10 +92,17 @@ public class MonitorServiceImpl implements MonitorService {
*/
private Map<String,Object> getSwapInfo(GlobalMemory memory) {
Map<String,Object> swapInfo = new LinkedHashMap<>();
swapInfo.put("total", FormatUtil.formatBytes(memory.getVirtualMemory().getSwapTotal()));
swapInfo.put("used", FormatUtil.formatBytes(memory.getVirtualMemory().getSwapUsed()));
swapInfo.put("available", FormatUtil.formatBytes(memory.getVirtualMemory().getSwapTotal() - memory.getVirtualMemory().getSwapUsed()));
swapInfo.put("usageRate", df.format(memory.getVirtualMemory().getSwapUsed()/(double)memory.getVirtualMemory().getSwapTotal() * 100));
VirtualMemory virtualMemory = memory.getVirtualMemory();
long total = virtualMemory.getSwapTotal();
long used = virtualMemory.getSwapUsed();
swapInfo.put("total", FormatUtil.formatBytes(total));
swapInfo.put("used", FormatUtil.formatBytes(used));
swapInfo.put("available", FormatUtil.formatBytes(total - used));
if(used == 0){
swapInfo.put("usageRate", 0);
} else {
swapInfo.put("usageRate", df.format(used/(double)total * 100));
}
return swapInfo;
}

View File

@ -144,6 +144,7 @@
<artifactId>ip2region</artifactId>
<version>1.7.2</version>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>