[ agent ] [status] - app status - 5

This commit is contained in:
zeaslity
2023-01-06 17:26:23 +08:00
parent 520d835db4
commit 9d04af3dd1
11 changed files with 92 additions and 68 deletions

View File

@@ -17,7 +17,7 @@ import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.Executor;
import static io.wdd.agent.status.AppStatusCollector.ALL_APP_NEED_TO_MONITOR_STATUS;
import static io.wdd.agent.executor.AppStatusExecutor.ALL_APP_NEED_TO_MONITOR_STATUS;
@Component
@@ -137,12 +137,10 @@ public class NacosConfigurationCollector {
// need to keep update to nacos so need to clear the cache
ALL_APP_NEED_TO_MONITOR_STATUS.clear();
all_app_from_nacos.stream().forEach(
app -> {
String[] split = app.split("/");
ALL_APP_NEED_TO_MONITOR_STATUS.put(split[0], split[1] + ".service");
}
);
all_app_from_nacos.stream().forEach(app -> {
String[] split = app.split("/");
ALL_APP_NEED_TO_MONITOR_STATUS.put(split[0], split[1] + ".service");
});
log.info("ALL_APP_NEED_TO_MONITOR_STATUS are => {}", ALL_APP_NEED_TO_MONITOR_STATUS);

View File

@@ -7,10 +7,12 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import static io.wdd.agent.status.AppStatusCollector.ALL_APP_NEED_TO_MONITOR_STATUS;
import static java.util.stream.Collectors.groupingBy;
@Service
@@ -18,7 +20,10 @@ import static java.util.stream.Collectors.groupingBy;
@Lazy
public class AppStatusExecutor {
private static ArrayList<ArrayList<String>> APP_STATUS_CHECK_COMMAND;
// storage all the applications agent status should report
public static final HashMap<String, String> ALL_APP_NEED_TO_MONITOR_STATUS = new HashMap<>(16);
private static final ArrayList<ArrayList<String>> APP_STATUS_CHECK_COMMAND;
static {
@@ -42,7 +47,7 @@ public class AppStatusExecutor {
}
public HashMap<String, Set<String>> checkAppStatus(boolean allAppStatus){
public HashMap<String, Set<String>> checkAppStatus(boolean allAppStatus) {
// check all app status
Map<String, List<String[]>> collect = ALL_APP_NEED_TO_MONITOR_STATUS.keySet().stream()

View File

@@ -1,6 +1,7 @@
package io.wdd.agent.executor;
import io.wdd.agent.executor.config.CommandPipelineBuilder;
import io.wdd.common.beans.status.AppStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
@@ -13,7 +14,8 @@ import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import static io.wdd.agent.status.AppStatusCollector.ALL_APP_NEED_TO_MONITOR_STATUS;
import static io.wdd.agent.executor.AppStatusExecutor.ALL_APP_NEED_TO_MONITOR_STATUS;
@Slf4j
public class CheckSingleAppStatusCallable implements Callable<String[]> {
@@ -48,7 +50,7 @@ public class CheckSingleAppStatusCallable implements Callable<String[]> {
if (StringUtils.isNotEmpty(error)) {
// app not existed!
log.debug("app not installed !");
result[0] = "NotInstall";
result[0] = AppStatusEnum.NOT_INSTALL.getName();
} else {
log.debug("app existed! and then check the running status !");
// app existed! and then check the running status !
@@ -63,11 +65,11 @@ public class CheckSingleAppStatusCallable implements Callable<String[]> {
Assert.notNull(appStatusCommandResult, "app status command result is null !");
if (appStatusCommandResult.startsWith("1")) {
result[0] = "Healthy";
result[0] = AppStatusEnum.HEALTHY.getName();
} else if (appStatusCommandResult.startsWith("0")) {
result[0] = "Failure";
result[0] = AppStatusEnum.FAILURE.getName();
} else {
result[0] = "NotInstall";
result[0] = AppStatusEnum.NOT_INSTALL.getName();
}
}
log.debug("app [ {} ] status check result is => {} ", appName, resultList);

View File

@@ -3,6 +3,7 @@ package io.wdd.agent.status;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.agent.config.beans.init.AgentServerInfo;
import io.wdd.agent.executor.AppStatusExecutor;
import io.wdd.common.beans.status.*;
import io.wdd.common.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
@@ -16,9 +17,7 @@ import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
@Service
@Slf4j
@@ -50,6 +49,9 @@ public class AgentStatusCollector {
@Resource
AgentServerInfo agentServerInfo;
@Resource
AppStatusExecutor appStatusExecutor;
public AgentStatus collect() {
AgentStatus agentStatus = AgentStatusCache.get(0);
@@ -76,9 +78,23 @@ public class AgentStatusCollector {
/* Time */
agentStatus.setTime(TimeUtils.currentTimeString());
/* App Status */
agentStatus.setAppStatus(
parseAppStatus(appStatusExecutor.checkAppStatus(true))
);
return agentStatus;
}
private AppStatusInfo parseAppStatus(HashMap<String, Set<String>> checkAppStatus) {
return AppStatusInfo.builder()
.Healthy(checkAppStatus.get(AppStatusEnum.HEALTHY.getName()))
.Failure(checkAppStatus.get(AppStatusEnum.FAILURE.getName()))
.NotInstall(checkAppStatus.get(AppStatusEnum.NOT_INSTALL.getName()))
.build();
}
/**
* when server first time boot up
* the server info are not collected completely

View File

@@ -1,43 +0,0 @@
package io.wdd.agent.status;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.HashMap;
@Service
@Slf4j
public class AppStatusCollector {
// storage all the applications agent status should report
public static final HashMap<String, String> ALL_APP_NEED_TO_MONITOR_STATUS = new HashMap<>(16);
/**
* not very good
* but also a kind of method to dynamically listen to nacos configuration change
*/
/*@NacosValue(value = "${octopus.agent.status.enable}" , autoRefreshed = true)
private String all_app_from_nacos;*
/*@NacosConfigListener(
groupId = "k3s",
dataId = "octopus-agent-k3s.yaml",
type = ConfigType.YAML,
properties =
)
public void onMessage(String content){
log.debug("update octopus-agent nacos config are ==> {} ", content);
Yaml yaml = new Yaml();
Object load = yaml.load(content);
System.out.println("load = " + load);
}*/
}

View File

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