[代码优化](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> <properties>
<jjwt.version>0.11.1</jjwt.version> <jjwt.version>0.11.1</jjwt.version>
<!-- oshi监控需要指定jna版本, 问题详见 https://github.com/oshi/oshi/issues/1040 --> <!-- oshi监控需要指定jna版本, 问题详见 https://github.com/oshi/oshi/issues/1040 -->
<jna.version>5.5.0</jna.version> <jna.version>5.6.0</jna.version>
</properties> </properties>
<dependencies> <dependencies>
@ -84,7 +84,7 @@
<dependency> <dependency>
<groupId>com.github.oshi</groupId> <groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId> <artifactId>oshi-core</artifactId>
<version>5.0.1</version> <version>5.3.6</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

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

View File

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