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 javax.servlet.http.HttpServletRequest;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.net.NetworkInterface;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Zheng Jie
|
* @author Zheng Jie
|
||||||
|
@ -248,20 +251,37 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public static String getLocalIp() {
|
public static String getLocalIp() {
|
||||||
InetAddress addr;
|
|
||||||
try {
|
try {
|
||||||
addr = InetAddress.getLocalHost();
|
InetAddress candidateAddress = null;
|
||||||
} catch (UnknownHostException e) {
|
// 遍历所有的网络接口
|
||||||
return "unknown";
|
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
|
||||||
}
|
NetworkInterface anInterface = interfaces.nextElement();
|
||||||
byte[] ipAddr = addr.getAddress();
|
// 在所有的接口下再遍历IP
|
||||||
StringBuilder ipAddrStr = new StringBuilder();
|
for (Enumeration<InetAddress> inetAddresses = anInterface.getInetAddresses(); inetAddresses.hasMoreElements();) {
|
||||||
for (int i = 0; i < ipAddr.length; i++) {
|
InetAddress inetAddr = inetAddresses.nextElement();
|
||||||
if (i > 0) {
|
// 排除loopback类型地址
|
||||||
ipAddrStr.append(".");
|
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.method.HandlerMethod;
|
||||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,7 +49,7 @@ import java.util.*;
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
|
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
|
||||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
private final TokenProvider tokenProvider;
|
private final TokenProvider tokenProvider;
|
||||||
private final CorsFilter corsFilter;
|
private final CorsFilter corsFilter;
|
||||||
|
@ -138,6 +137,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
.and().apply(securityConfigurerAdapter());
|
.and().apply(securityConfigurerAdapter());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private TokenConfigurer securityConfigurerAdapter() {
|
||||||
|
return new TokenConfigurer(tokenProvider, properties, onlineUserService, userCacheClean);
|
||||||
|
}
|
||||||
|
|
||||||
private Map<String, Set<String>> getAnonymousUrl(Map<RequestMappingInfo, HandlerMethod> handlerMethodMap) {
|
private Map<String, Set<String>> getAnonymousUrl(Map<RequestMappingInfo, HandlerMethod> handlerMethodMap) {
|
||||||
Map<String, Set<String>> anonymousUrls = new HashMap<>(6);
|
Map<String, Set<String>> anonymousUrls = new HashMap<>(6);
|
||||||
Set<String> get = new HashSet<>();
|
Set<String> get = new HashSet<>();
|
||||||
|
@ -182,8 +185,4 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
anonymousUrls.put(RequestMethodEnum.ALL.getType(), all);
|
anonymousUrls.put(RequestMethodEnum.ALL.getType(), all);
|
||||||
return anonymousUrls;
|
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.BetweenFormater;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import me.zhengjie.modules.system.service.MonitorService;
|
import me.zhengjie.modules.system.service.MonitorService;
|
||||||
|
import me.zhengjie.utils.ElAdminConstant;
|
||||||
import me.zhengjie.utils.FileUtil;
|
import me.zhengjie.utils.FileUtil;
|
||||||
import me.zhengjie.utils.StringUtils;
|
import me.zhengjie.utils.StringUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -73,15 +74,24 @@ public class MonitorServiceImpl implements MonitorService {
|
||||||
Map<String,Object> diskInfo = new LinkedHashMap<>();
|
Map<String,Object> diskInfo = new LinkedHashMap<>();
|
||||||
FileSystem fileSystem = os.getFileSystem();
|
FileSystem fileSystem = os.getFileSystem();
|
||||||
List<OSFileStore> fsArray = fileSystem.getFileStores();
|
List<OSFileStore> fsArray = fileSystem.getFileStores();
|
||||||
|
String osName = System.getProperty("os.name");
|
||||||
|
long available = 0, total = 0;
|
||||||
for (OSFileStore fs : fsArray){
|
for (OSFileStore fs : fsArray){
|
||||||
long available = fs.getUsableSpace();
|
// windows 需要将所有磁盘分区累加
|
||||||
long total = fs.getTotalSpace();
|
if(osName.toLowerCase().startsWith(ElAdminConstant.WIN)) {
|
||||||
long used = total - available;
|
available += fs.getUsableSpace();
|
||||||
diskInfo.put("total", total > 0 ? FileUtil.getSize(total) : "?");
|
total += fs.getTotalSpace();
|
||||||
diskInfo.put("available", FileUtil.getSize(available));
|
} else {
|
||||||
diskInfo.put("used", FileUtil.getSize(used));
|
available = fs.getUsableSpace();
|
||||||
diskInfo.put("usageRate", df.format(used/(double)fs.getTotalSpace() * 100));
|
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;
|
return diskInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue