[ agent ] [ status ]- fix some bug

This commit is contained in:
zeaslity
2023-02-01 15:02:02 +08:00
parent 38c5a6a3c1
commit 5aacdae89b
5 changed files with 75 additions and 31 deletions

View File

@@ -3,6 +3,7 @@ package io.wdd.agent.config.message.handler;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.agent.config.utils.AgentCommonThreadPool;
import io.wdd.agent.status.AgentStatusCollector; import io.wdd.agent.status.AgentStatusCollector;
import io.wdd.agent.status.HealthyReporter; import io.wdd.agent.status.HealthyReporter;
import io.wdd.common.beans.rabbitmq.OctopusMessage; import io.wdd.common.beans.rabbitmq.OctopusMessage;
@@ -12,6 +13,8 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import static io.wdd.common.beans.status.OctopusStatusMessage.*; import static io.wdd.common.beans.status.OctopusStatusMessage.*;
@Component @Component
@@ -26,6 +29,9 @@ public class OMHandlerStatus extends AbstractOctopusMessageHandler {
@Resource @Resource
AgentStatusCollector agentStatusCollector; AgentStatusCollector agentStatusCollector;
@Override @Override
public boolean handle(OctopusMessage octopusMessage) { public boolean handle(OctopusMessage octopusMessage) {
@@ -46,13 +52,30 @@ public class OMHandlerStatus extends AbstractOctopusMessageHandler {
healthyReporter.report(); healthyReporter.report();
} else if (statusType.equals(ALL_STATUS_MESSAGE_TYPE)) { } else if (statusType.equals(ALL_STATUS_MESSAGE_TYPE)) {
// all status report // all status report
agentStatusCollector.sendAgentStatusToRedis(); CompletableFuture.runAsync(
() -> {
agentStatusCollector.collect(
1,
1
);
}
,
AgentCommonThreadPool.pool
);
} else if (statusType.equals(METRIC_STATUS_MESSAGE_TYPE)) { } else if (statusType.equals(METRIC_STATUS_MESSAGE_TYPE)) {
// metric status // metric status
agentStatusCollector.collect( // 必须要交给异步任务完成,否则会阻塞主线程!
statusMessage.getMetricRepeatCount(), CompletableFuture.runAsync(
statusMessage.getMetricRepeatPinch() () -> {
agentStatusCollector.collect(
statusMessage.getMetricRepeatCount(),
statusMessage.getMetricRepeatPinch()
);
}
,
AgentCommonThreadPool.pool
); );
} else if (statusType.equals(APP_STATUS_MESSAGE_TYPE)) { } else if (statusType.equals(APP_STATUS_MESSAGE_TYPE)) {
// app status report // app status report

View File

@@ -18,7 +18,6 @@ import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer; import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem; import oshi.software.os.OperatingSystem;
import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -56,12 +55,6 @@ public class AgentStatusCollector {
AppStatusExecutor appStatusExecutor; AppStatusExecutor appStatusExecutor;
@Resource @Resource
NetworkStatusExecutor networkStatusExecutor; NetworkStatusExecutor networkStatusExecutor;
private String statusRedisStreamKey;
@PostConstruct
private void generateStatusRedisStreamKey() {
statusRedisStreamKey = AgentStatus.getRedisStatusKey(agentServerInfo.getAgentTopicName());
}
@SneakyThrows @SneakyThrows
public AgentStatus collect() { public AgentStatus collect() {
@@ -73,8 +66,10 @@ public class AgentStatusCollector {
agentStatus.setAgentTopicName(agentServerInfo.getAgentTopicName()); agentStatus.setAgentTopicName(agentServerInfo.getAgentTopicName());
/* CPU */ /* CPU */
agentStatus.setCpuInfo(new CpuInfo(hardware.getProcessor(), agentStatus.setCpuInfo(new CpuInfo(
1000)); hardware.getProcessor(),
1000
));
/* Memory */ /* Memory */
agentStatus.setMemoryInfo(MemoryInfo.build(hardware.getMemory())); agentStatus.setMemoryInfo(MemoryInfo.build(hardware.getMemory()));
@@ -85,8 +80,10 @@ public class AgentStatusCollector {
/* Network */ /* Network */
agentStatus.setNetworkInfo( agentStatus.setNetworkInfo(
// NetworkInfo.mapFromNetworkIFS(hardware.getNetworkIFs(false)) // NetworkInfo.mapFromNetworkIFS(hardware.getNetworkIFs(false))
networkStatusExecutor.collect(hardware.getNetworkIFs(false), networkStatusExecutor.collect(
false) hardware.getNetworkIFs(false),
false
)
); );
/* operating system info */ /* operating system info */
@@ -109,31 +106,40 @@ public class AgentStatusCollector {
private AppStatusInfo parseAppStatus(HashMap<String, Set<String>> checkAppStatus) { private AppStatusInfo parseAppStatus(HashMap<String, Set<String>> checkAppStatus) {
return AppStatusInfo.builder() return AppStatusInfo
.Healthy(checkAppStatus.get(AppStatusEnum.HEALTHY.getName())) .builder()
.Failure(checkAppStatus.get(AppStatusEnum.FAILURE.getName())) .Healthy(checkAppStatus.get(AppStatusEnum.HEALTHY.getName()))
.NotInstall(checkAppStatus.get(AppStatusEnum.NOT_INSTALL.getName())) .Failure(checkAppStatus.get(AppStatusEnum.FAILURE.getName()))
.build(); .NotInstall(checkAppStatus.get(AppStatusEnum.NOT_INSTALL.getName()))
.build();
} }
// agent boot up 120s then start to report its status // agent boot up 120s then start to report its status
// at the fix rate of 15s // at the fix rate of 15s
/*@Scheduled(initialDelay = ReportInitDelay, fixedRate = ReportFixedRate)*/ /*@Scheduled(initialDelay = ReportInitDelay, fixedRate = ReportFixedRate)*/
public void sendAgentStatusToRedis() { public void sendAgentStatusToRedis(String statusRedisStreamKey) {
try { try {
Map<String, String> map = Map.of(TimeUtils.currentTimeString(), // 装配 Agent状态 信息
objectMapper.writeValueAsString(collect())); Map<String, String> map = Map.of(
TimeUtils.currentTimeString(),
objectMapper.writeValueAsString(collect())
);
StringRecord stringRecord = StreamRecords.string(map) StringRecord stringRecord = StreamRecords
.withStreamKey(statusRedisStreamKey); .string(map)
.withStreamKey(statusRedisStreamKey);
log.debug("Agent Status is ==> {}", log.debug(
map); "Agent Status is ==> {}",
map
);
redisTemplate.opsForStream() // 发送到Redis
.add(stringRecord); redisTemplate
.opsForStream()
.add(stringRecord);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@@ -150,13 +156,18 @@ public class AgentStatusCollector {
*/ */
public void collect(int metricRepeatCount, int metricRepeatPinch) { public void collect(int metricRepeatCount, int metricRepeatPinch) {
String statusRedisStreamKey = AgentStatus
.getRedisStatusKey(agentServerInfo.getAgentTopicName());
log.debug("statusRedisStreamKey is => {}", statusRedisStreamKey);
for (int count = 0; count < metricRepeatCount; count++) { for (int count = 0; count < metricRepeatCount; count++) {
try { try {
// use async thread pool to call the status collect method // use async thread pool to call the status collect method
AgentCommonThreadPool.pool.submit( AgentCommonThreadPool.pool.submit(
() -> this.sendAgentStatusToRedis() () -> this.sendAgentStatusToRedis(statusRedisStreamKey)
); );
// main thread sleep for metricRepeatPinch // main thread sleep for metricRepeatPinch

View File

@@ -25,7 +25,7 @@ public class OSHITest {
@Test @Test
void testCollect(){ void testCollect(){
agentStatusCollector.sendAgentStatusToRedis(); agentStatusCollector.sendAgentStatusToRedis("123");
} }

View File

@@ -72,9 +72,11 @@ mybatis-plus:
logicDeleteField: isDelete logicDeleteField: isDelete
logic-not-delete-value: 0 logic-not-delete-value: 0
logic-delete-value: 1 logic-delete-value: 1
banner: false
configuration: configuration:
# 希望知道所有的sql是怎么执行的, 配置输出日志 # 希望知道所有的sql是怎么执行的, 配置输出日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 数据库下划线--实体类也是下划线 需要为false # 数据库下划线--实体类也是下划线 需要为false
map-underscore-to-camel-case: true map-underscore-to-camel-case: true
# 一级缓存的 缓存级别默认为 session如果要关闭一级缓存可以设置为 statement # 一级缓存的 缓存级别默认为 session如果要关闭一级缓存可以设置为 statement

View File

@@ -154,5 +154,13 @@ apt-cache madison openjdk-11-jdk | head -n 1 | awk '{print$3}'
java -jar /octopus-agent/agent.jar -Xms128m -Xmx512m -Dfile.encoding=utf-8 --spring.profiles.active=k3s --spring.cloud.nacos.config.group=k3s --spring.cloud.nacos.config.extension-configs[0].dataId=common-k3s.yaml --spring.cloud.nacos.config.extension-configs[0].group=k3s java -jar /octopus-agent/agent.jar -Xms128m -Xmx512m -Dfile.encoding=utf-8 --spring.profiles.active=k3s --spring.cloud.nacos.config.group=k3s --spring.cloud.nacos.config.extension-configs[0].dataId=common-k3s.yaml --spring.cloud.nacos.config.extension-configs[0].group=k3s
export OctopusServerContainerName="octopus-server"
docker container stop ${OctopusServerContainerName}
sleep 2
docker container rm ${OctopusServerContainerName}
docker image rmi icederce/wdd-octopus-server:latest
} }