[ agent ] [ status ]- optimize the network status

This commit is contained in:
zeaslity
2023-01-12 17:38:32 +08:00
committed by IceDerce
parent 88e5428f9e
commit 1b7b4f4104
9 changed files with 146 additions and 16 deletions

View File

@@ -17,7 +17,7 @@ import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.Executor;
import static io.wdd.agent.executor.AppStatusExecutor.ALL_APP_NEED_TO_MONITOR_STATUS;
import static io.wdd.agent.executor.status.AppStatusExecutor.ALL_APP_NEED_TO_MONITOR_STATUS;
@Component

View File

@@ -14,7 +14,7 @@ import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import static io.wdd.agent.executor.AppStatusExecutor.ALL_APP_NEED_TO_MONITOR_STATUS;
import static io.wdd.agent.executor.status.AppStatusExecutor.ALL_APP_NEED_TO_MONITOR_STATUS;
@Slf4j

View File

@@ -19,7 +19,6 @@ import java.util.stream.Collectors;
@Slf4j
public class CommandPipelineBuilder {
public static List<String> runGetResult(ArrayList<ArrayList<String>> commandList){
try {

View File

@@ -1,7 +1,8 @@
package io.wdd.agent.executor;
package io.wdd.agent.executor.status;
import io.wdd.agent.config.utils.AgentCommonThreadPool;
import io.wdd.agent.executor.CheckSingleAppStatusCallable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

View File

@@ -0,0 +1,104 @@
package io.wdd.agent.executor.status;
import io.wdd.agent.config.utils.AgentCommonThreadPool;
import io.wdd.common.beans.status.NetworkInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import oshi.hardware.NetworkIF;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 计算网卡接口的网速信息
* 获取网卡接口的公网Ipv4和Ipv6信息
*/
@Service
@Lazy
public class NetworkStatusExecutor {
private static final ArrayList<String> GET_PUBLIC_IPV4_ADDRESS_COMMAND;
private static final ArrayList<String> GET_PUBLIC_IPV6_ADDRESS_COMMAND;
static {
GET_PUBLIC_IPV4_ADDRESS_COMMAND = new ArrayList<>(List.of(
"wget",
"-q",
"-T10",
"-O-",
"ipinfo.io/ip"
));
GET_PUBLIC_IPV6_ADDRESS_COMMAND = new ArrayList<>(List.of(
"curl",
"--max-time",
"5",
"-6",
"https://ifconfig.co/ip"
));
}
public List<NetworkInfo> collect(List<NetworkIF> networkIFList, boolean updatePublicIpAddr) {
// 获取网卡接口的公网Ipv4和Ipv6信息
// 不需要AgentBootUp通过命令行即可获取
// 需要定时更新Agent的信息根据Boolean来决定
if (updatePublicIpAddr) {
String[] publicIpAddress = collectAgentPublicIpAddress();
}
// 计算网卡接口的网速信息
networkIFList.forEach(
networkIF -> {
// AgentCommonThreadPool.pool.submit();
}
);
return null;
}
/**
* @return 第一位公网Ipv4 第二位公网Ipv6
*/
private String[] collectAgentPublicIpAddress() {
return new String[]{
collectAgentPublicIpAddress(GET_PUBLIC_IPV4_ADDRESS_COMMAND),
collectAgentPublicIpAddress(GET_PUBLIC_IPV6_ADDRESS_COMMAND)
};
}
private String collectAgentPublicIpAddress(ArrayList<String> getPublicIPAddrCommandList) {
try {
ProcessBuilder processBuilder = new ProcessBuilder(getPublicIPAddrCommandList);
Process process = processBuilder.start();
boolean waitFor = process.waitFor(5,
TimeUnit.SECONDS);
String ipAddr = "null";
if (ObjectUtils.isNotEmpty(waitFor) && process.exitValue() == 0) {
ipAddr = new BufferedReader(new InputStreamReader(process.getInputStream())).readLine();
}
return ipAddr;
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -4,7 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.agent.config.beans.init.AgentServerInfo;
import io.wdd.agent.config.utils.AgentCommonThreadPool;
import io.wdd.agent.executor.AppStatusExecutor;
import io.wdd.agent.executor.status.AppStatusExecutor;
import io.wdd.common.beans.status.*;
import io.wdd.common.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
@@ -19,7 +19,6 @@ import oshi.software.os.OperatingSystem;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
@Service

View File

@@ -1,6 +1,6 @@
package io.wdd.agent;
import io.wdd.agent.executor.AppStatusExecutor;
import io.wdd.agent.executor.status.AppStatusExecutor;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;