@@ -50,7 +50,6 @@ public Map<String,Object> getServers(){
5050 HardwareAbstractionLayer hal = si .getHardware ();
5151 // 系统信息
5252 resultMap .put ("sys" , getSystemInfo (os ));
53-
5453 // cpu 信息
5554 resultMap .put ("cpu" , getCpuInfo (hal .getProcessor ()));
5655 // 内存信息
@@ -75,9 +74,11 @@ private Map<String,Object> getDiskInfo(OperatingSystem os) {
7574 FileSystem fileSystem = os .getFileSystem ();
7675 List <OSFileStore > fsArray = fileSystem .getFileStores ();
7776 for (OSFileStore fs : fsArray ){
78- diskInfo .put ("total" , fs .getTotalSpace () > 0 ? FileUtil .getSize (fs .getTotalSpace ()) : "?" );
79- long used = fs .getTotalSpace () - fs .getUsableSpace ();
80- diskInfo .put ("available" , FileUtil .getSize (fs .getUsableSpace ()));
77+ long available = fs .getUsableSpace ();
78+ long total = fs .getTotalSpace ();
79+ long used = total - available ;
80+ diskInfo .put ("total" , total > 0 ? FileUtil .getSize (total ) : "?" );
81+ diskInfo .put ("available" , FileUtil .getSize (available ));
8182 diskInfo .put ("used" , FileUtil .getSize (used ));
8283 diskInfo .put ("usageRate" , df .format (used /(double )fs .getTotalSpace () * 100 ));
8384 }
@@ -91,10 +92,17 @@ private Map<String,Object> getDiskInfo(OperatingSystem os) {
9192 */
9293 private Map <String ,Object > getSwapInfo (GlobalMemory memory ) {
9394 Map <String ,Object > swapInfo = new LinkedHashMap <>();
94- swapInfo .put ("total" , FormatUtil .formatBytes (memory .getVirtualMemory ().getSwapTotal ()));
95- swapInfo .put ("used" , FormatUtil .formatBytes (memory .getVirtualMemory ().getSwapUsed ()));
96- swapInfo .put ("available" , FormatUtil .formatBytes (memory .getVirtualMemory ().getSwapTotal () - memory .getVirtualMemory ().getSwapUsed ()));
97- swapInfo .put ("usageRate" , df .format (memory .getVirtualMemory ().getSwapUsed ()/(double )memory .getVirtualMemory ().getSwapTotal () * 100 ));
95+ VirtualMemory virtualMemory = memory .getVirtualMemory ();
96+ long total = virtualMemory .getSwapTotal ();
97+ long used = virtualMemory .getSwapUsed ();
98+ swapInfo .put ("total" , FormatUtil .formatBytes (total ));
99+ swapInfo .put ("used" , FormatUtil .formatBytes (used ));
100+ swapInfo .put ("available" , FormatUtil .formatBytes (total - used ));
101+ if (used == 0 ){
102+ swapInfo .put ("usageRate" , 0 );
103+ } else {
104+ swapInfo .put ("usageRate" , df .format (used /(double )total * 100 ));
105+ }
98106 return swapInfo ;
99107 }
100108
0 commit comments