mirror of https://github.com/elunez/eladmin
[代码优化](v2.6):修复服务监控磁盘统计不准确的问题,修复本机IP获取不准确的问题
close https://github.com/elunez/eladmin/issues/527pull/545/head
parent
41cd93053a
commit
d752b3d9cc
|
@ -29,10 +29,13 @@ import org.springframework.core.io.ClassPathResource;
|
|||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
|
@ -248,20 +251,37 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
|||
* @return /
|
||||
*/
|
||||
public static String getLocalIp() {
|
||||
InetAddress addr;
|
||||
try {
|
||||
addr = InetAddress.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
return "unknown";
|
||||
}
|
||||
byte[] ipAddr = addr.getAddress();
|
||||
StringBuilder ipAddrStr = new StringBuilder();
|
||||
for (int i = 0; i < ipAddr.length; i++) {
|
||||
if (i > 0) {
|
||||
ipAddrStr.append(".");
|
||||
InetAddress candidateAddress = null;
|
||||
// 遍历所有的网络接口
|
||||
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
|
||||
NetworkInterface anInterface = interfaces.nextElement();
|
||||
// 在所有的接口下再遍历IP
|
||||
for (Enumeration<InetAddress> inetAddresses = anInterface.getInetAddresses(); inetAddresses.hasMoreElements();) {
|
||||
InetAddress inetAddr = inetAddresses.nextElement();
|
||||
// 排除loopback类型地址
|
||||
if (!inetAddr.isLoopbackAddress()) {
|
||||
if (inetAddr.isSiteLocalAddress()) {
|
||||
// 如果是site-local地址,就是它了
|
||||
return inetAddr.getHostAddress();
|
||||
} else if (candidateAddress == null) {
|
||||
// site-local类型的地址未被发现,先记录候选地址
|
||||
candidateAddress = inetAddr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ipAddrStr.append(ipAddr[i] & 0xFF);
|
||||
if (candidateAddress != null) {
|
||||
return candidateAddress.getHostAddress();
|
||||
}
|
||||
// 如果没有发现 non-loopback地址.只能用最次选的方案
|
||||
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
|
||||
if (jdkSuppliedAddress == null) {
|
||||
return "";
|
||||
}
|
||||
return jdkSuppliedAddress.getHostAddress();
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
return ipAddrStr.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,7 +40,6 @@ import org.springframework.web.filter.CorsFilter;
|
|||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
|
@ -50,7 +49,7 @@ import java.util.*;
|
|||
@EnableWebSecurity
|
||||
@RequiredArgsConstructor
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
private final TokenProvider tokenProvider;
|
||||
private final CorsFilter corsFilter;
|
||||
|
@ -138,6 +137,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||
.and().apply(securityConfigurerAdapter());
|
||||
}
|
||||
|
||||
private TokenConfigurer securityConfigurerAdapter() {
|
||||
return new TokenConfigurer(tokenProvider, properties, onlineUserService, userCacheClean);
|
||||
}
|
||||
|
||||
private Map<String, Set<String>> getAnonymousUrl(Map<RequestMappingInfo, HandlerMethod> handlerMethodMap) {
|
||||
Map<String, Set<String>> anonymousUrls = new HashMap<>(6);
|
||||
Set<String> get = new HashSet<>();
|
||||
|
@ -182,8 +185,4 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||
anonymousUrls.put(RequestMethodEnum.ALL.getType(), all);
|
||||
return anonymousUrls;
|
||||
}
|
||||
|
||||
private TokenConfigurer securityConfigurerAdapter() {
|
||||
return new TokenConfigurer(tokenProvider, properties, onlineUserService, userCacheClean);
|
||||
}
|
||||
}
|
|
@ -18,6 +18,7 @@ package me.zhengjie.modules.system.service.impl;
|
|||
import cn.hutool.core.date.BetweenFormater;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import me.zhengjie.modules.system.service.MonitorService;
|
||||
import me.zhengjie.utils.ElAdminConstant;
|
||||
import me.zhengjie.utils.FileUtil;
|
||||
import me.zhengjie.utils.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -73,15 +74,24 @@ public class MonitorServiceImpl implements MonitorService {
|
|||
Map<String,Object> diskInfo = new LinkedHashMap<>();
|
||||
FileSystem fileSystem = os.getFileSystem();
|
||||
List<OSFileStore> fsArray = fileSystem.getFileStores();
|
||||
String osName = System.getProperty("os.name");
|
||||
long available = 0, total = 0;
|
||||
for (OSFileStore fs : fsArray){
|
||||
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));
|
||||
// windows 需要将所有磁盘分区累加
|
||||
if(osName.toLowerCase().startsWith(ElAdminConstant.WIN)) {
|
||||
available += fs.getUsableSpace();
|
||||
total += fs.getTotalSpace();
|
||||
} else {
|
||||
available = fs.getUsableSpace();
|
||||
total = fs.getTotalSpace();
|
||||
break;
|
||||
}
|
||||
}
|
||||
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)total * 100));
|
||||
return diskInfo;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue