Skip to content

Commit

Permalink
[代码优化](v2.6):修复服务监控磁盘统计不准确的问题,修复本机IP获取不准确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
elunez committed Nov 22, 2020
1 parent 41cd930 commit d752b3d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
44 changes: 32 additions & 12 deletions eladmin-common/src/main/java/me/zhengjie/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@

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
Expand Down Expand Up @@ -248,20 +251,37 @@ public static String getWeekDay() {
* @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;
}
}
}
}
if (candidateAddress != null) {
return candidateAddress.getHostAddress();
}
ipAddrStr.append(ipAddr[i] & 0xFF);
// 如果没有发现 non-loopback地址.只能用最次选的方案
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
if (jdkSuppliedAddress == null) {
return "";
}
return jdkSuppliedAddress.getHostAddress();
} catch (Exception e) {
return "";
}
return ipAddrStr.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
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.*;

/**
Expand All @@ -50,7 +49,7 @@
@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;
Expand Down Expand Up @@ -138,6 +137,10 @@ protected void configure(HttpSecurity httpSecurity) throws Exception {
.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<>();
Expand Down Expand Up @@ -182,8 +185,4 @@ private Map<String, Set<String>> getAnonymousUrl(Map<RequestMappingInfo, Handler
anonymousUrls.put(RequestMethodEnum.ALL.getType(), all);
return anonymousUrls;
}

private TokenConfigurer securityConfigurerAdapter() {
return new TokenConfigurer(tokenProvider, properties, onlineUserService, userCacheClean);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -73,15 +74,24 @@ private Map<String,Object> getDiskInfo(OperatingSystem os) {
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;
}

Expand Down

0 comments on commit d752b3d

Please sign in to comment.