[ 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.*;
import java.util.concurrent.Executor; 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 @Component
@@ -137,12 +137,10 @@ public class NacosConfigurationCollector {
// need to keep update to nacos so need to clear the cache // need to keep update to nacos so need to clear the cache
ALL_APP_NEED_TO_MONITOR_STATUS.clear(); ALL_APP_NEED_TO_MONITOR_STATUS.clear();
all_app_from_nacos.stream().forEach( all_app_from_nacos.stream().forEach(app -> {
app -> { String[] split = app.split("/");
String[] split = app.split("/"); ALL_APP_NEED_TO_MONITOR_STATUS.put(split[0], split[1] + ".service");
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); 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 org.springframework.stereotype.Service;
import java.util.*; 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 java.util.stream.Collectors;
import static io.wdd.agent.status.AppStatusCollector.ALL_APP_NEED_TO_MONITOR_STATUS;
import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.groupingBy;
@Service @Service
@@ -18,7 +20,10 @@ import static java.util.stream.Collectors.groupingBy;
@Lazy @Lazy
public class AppStatusExecutor { 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 { 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 // check all app status
Map<String, List<String[]>> collect = ALL_APP_NEED_TO_MONITOR_STATUS.keySet().stream() Map<String, List<String[]>> collect = ALL_APP_NEED_TO_MONITOR_STATUS.keySet().stream()

View File

@@ -1,6 +1,7 @@
package io.wdd.agent.executor; package io.wdd.agent.executor;
import io.wdd.agent.executor.config.CommandPipelineBuilder; import io.wdd.agent.executor.config.CommandPipelineBuilder;
import io.wdd.common.beans.status.AppStatusEnum;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@@ -13,7 +14,8 @@ import java.util.List;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit; 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 @Slf4j
public class CheckSingleAppStatusCallable implements Callable<String[]> { public class CheckSingleAppStatusCallable implements Callable<String[]> {
@@ -48,7 +50,7 @@ public class CheckSingleAppStatusCallable implements Callable<String[]> {
if (StringUtils.isNotEmpty(error)) { if (StringUtils.isNotEmpty(error)) {
// app not existed! // app not existed!
log.debug("app not installed !"); log.debug("app not installed !");
result[0] = "NotInstall"; result[0] = AppStatusEnum.NOT_INSTALL.getName();
} else { } else {
log.debug("app existed! and then check the running status !"); log.debug("app existed! and then check the running status !");
// 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 !"); Assert.notNull(appStatusCommandResult, "app status command result is null !");
if (appStatusCommandResult.startsWith("1")) { if (appStatusCommandResult.startsWith("1")) {
result[0] = "Healthy"; result[0] = AppStatusEnum.HEALTHY.getName();
} else if (appStatusCommandResult.startsWith("0")) { } else if (appStatusCommandResult.startsWith("0")) {
result[0] = "Failure"; result[0] = AppStatusEnum.FAILURE.getName();
} else { } else {
result[0] = "NotInstall"; result[0] = AppStatusEnum.NOT_INSTALL.getName();
} }
} }
log.debug("app [ {} ] status check result is => {} ", appName, resultList); 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.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.agent.config.beans.init.AgentServerInfo; import io.wdd.agent.config.beans.init.AgentServerInfo;
import io.wdd.agent.executor.AppStatusExecutor;
import io.wdd.common.beans.status.*; import io.wdd.common.beans.status.*;
import io.wdd.common.utils.TimeUtils; import io.wdd.common.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -16,9 +17,7 @@ import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem; import oshi.software.os.OperatingSystem;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Collections; import java.util.*;
import java.util.List;
import java.util.Map;
@Service @Service
@Slf4j @Slf4j
@@ -50,6 +49,9 @@ public class AgentStatusCollector {
@Resource @Resource
AgentServerInfo agentServerInfo; AgentServerInfo agentServerInfo;
@Resource
AppStatusExecutor appStatusExecutor;
public AgentStatus collect() { public AgentStatus collect() {
AgentStatus agentStatus = AgentStatusCache.get(0); AgentStatus agentStatus = AgentStatusCache.get(0);
@@ -76,9 +78,23 @@ public class AgentStatusCollector {
/* Time */ /* Time */
agentStatus.setTime(TimeUtils.currentTimeString()); agentStatus.setTime(TimeUtils.currentTimeString());
/* App Status */
agentStatus.setAppStatus(
parseAppStatus(appStatusExecutor.checkAppStatus(true))
);
return agentStatus; 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 * when server first time boot up
* the server info are not collected completely * 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; package io.wdd.agent;
import io.wdd.agent.executor.AppStatusExecutor; import io.wdd.agent.executor.AppStatusExecutor;
import io.wdd.agent.status.AppStatusCollector;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;

View File

@@ -6,7 +6,6 @@ import lombok.Data;
@Data @Data
public class R<T> { public class R<T> {
int code; int code;
String msg; String msg;

View File

@@ -10,8 +10,6 @@ public enum ResultStat {
PARAM_ERROR(1003, "请求参数错误!"), PARAM_ERROR(1003, "请求参数错误!"),
BAD(5001, "all error !"); BAD(5001, "all error !");
int code; int code;

View File

@@ -5,7 +5,6 @@ import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import oshi.software.os.OperatingSystem;
import java.util.List; import java.util.List;
@@ -21,7 +20,6 @@ public class AgentStatus {
String agentTopicName; String agentTopicName;
CpuInfo cpuInfo; CpuInfo cpuInfo;
MemoryInfo memoryInfo; MemoryInfo memoryInfo;
@@ -32,4 +30,6 @@ public class AgentStatus {
AgentSystemInfo osInfo; AgentSystemInfo osInfo;
AppStatusInfo appStatus;
} }

View File

@@ -0,0 +1,28 @@
package io.wdd.common.beans.status;
public enum AppStatusEnum {
HEALTHY("Healthy", "app is running"),
FAILURE("Failure", "app is failed"),
NOT_INSTALL("NotInstall", "app not installed");
String name;
String description;
AppStatusEnum(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}

View File

@@ -0,0 +1,22 @@
package io.wdd.common.beans.status;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.util.Set;
@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder(toBuilder = true)
public class AppStatusInfo {
Set<String> Healthy;
Set<String> Failure;
Set<String> NotInstall;
}