From 69eb330f2ff850d653087789f9e8acadc1a8c488 Mon Sep 17 00:00:00 2001 From: IceDerce Date: Wed, 4 Jan 2023 16:52:59 +0800 Subject: [PATCH] [agent]-[status] accomplish base - 1 --- .../config/beans/init/AgentServerInfo.java | 6 ++ .../config/message/handler/OMHandlerInit.java | 3 + .../io/wdd/agent/config/utils/TimeUtils.java | 26 +++++++++ .../agent/executor/redis/StreamSender.java | 13 +---- .../bootup/CollectSystemInfo.java | 56 ++++++++++--------- .../status/hardware/AgentStatusCollector.java | 24 ++++++-- .../agent/status/hardware/cpu/CpuInfo.java | 6 ++ .../status/hardware/memory/MemoryInfo.java | 2 +- .../status/redisReporter/AgentStatus.java | 11 +++- .../src/test/java/io/wdd/agent/OSHITest.java | 30 ++++++++++ 10 files changed, 130 insertions(+), 47 deletions(-) create mode 100644 agent/src/main/java/io/wdd/agent/config/utils/TimeUtils.java diff --git a/agent/src/main/java/io/wdd/agent/config/beans/init/AgentServerInfo.java b/agent/src/main/java/io/wdd/agent/config/beans/init/AgentServerInfo.java index 261de0d..0828348 100644 --- a/agent/src/main/java/io/wdd/agent/config/beans/init/AgentServerInfo.java +++ b/agent/src/main/java/io/wdd/agent/config/beans/init/AgentServerInfo.java @@ -110,4 +110,10 @@ public class AgentServerInfo { @Value("${machineId}") private String machineId; + /* + * get from octopus server at the end of initialization + * + * */ + private String agentTopicName; + } diff --git a/agent/src/main/java/io/wdd/agent/config/message/handler/OMHandlerInit.java b/agent/src/main/java/io/wdd/agent/config/message/handler/OMHandlerInit.java index d65d1dc..29e74c9 100644 --- a/agent/src/main/java/io/wdd/agent/config/message/handler/OMHandlerInit.java +++ b/agent/src/main/java/io/wdd/agent/config/message/handler/OMHandlerInit.java @@ -46,6 +46,9 @@ public class OMHandlerInit extends AbstractOctopusMessageHandler { // 2. send PassThroughTopicName successful info to the server String success = String.format("[Octopus Agent] - [ %s ] has successfully PassThroughTopicName with server [ %s ] !", agentServerInfo.getServerName(), octopusMessage.getUuid()); + // set the topic name to bean + agentServerInfo.setAgentTopicName(octopusMessage.getUuid()); + octopusMessage.setResult(success); // log.info(success); diff --git a/agent/src/main/java/io/wdd/agent/config/utils/TimeUtils.java b/agent/src/main/java/io/wdd/agent/config/utils/TimeUtils.java new file mode 100644 index 0000000..542ff92 --- /dev/null +++ b/agent/src/main/java/io/wdd/agent/config/utils/TimeUtils.java @@ -0,0 +1,26 @@ +package io.wdd.agent.config.utils; + +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; + +public class TimeUtils { + + public static ByteBuffer currentTimeByteBuffer() { + + byte[] timeBytes = LocalDateTime.now(ZoneId.of("UTC+8")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).getBytes(StandardCharsets.UTF_8); + + return ByteBuffer.wrap(timeBytes); + } + + + /** + * @return UTC+8 [ yyyy-MM-dd HH:mm:ss ] Time String + */ + public static String currentTimeString() { + + return LocalDateTime.now(ZoneId.of("UTC+8")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + } +} diff --git a/agent/src/main/java/io/wdd/agent/executor/redis/StreamSender.java b/agent/src/main/java/io/wdd/agent/executor/redis/StreamSender.java index 9728c22..855a9d0 100644 --- a/agent/src/main/java/io/wdd/agent/executor/redis/StreamSender.java +++ b/agent/src/main/java/io/wdd/agent/executor/redis/StreamSender.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import io.wdd.agent.config.beans.executor.CommandLog; import io.wdd.agent.config.beans.executor.StreamSenderEntity; +import io.wdd.agent.config.utils.TimeUtils; import io.wdd.agent.executor.thread.LogToArrayListCache; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -49,17 +50,7 @@ public class StreamSender { private final ArrayList cacheLogList = new ArrayList<>(256); - private static ByteBuffer currentTimeByteBuffer() { - byte[] timeBytes = LocalDateTime.now(ZoneId.of("UTC+8")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).getBytes(StandardCharsets.UTF_8); - - return ByteBuffer.wrap(timeBytes); - } - - private static String currentTimeString() { - - return LocalDateTime.now(ZoneId.of("UTC+8")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); - } @SneakyThrows private static Map generateFakeData() { @@ -130,7 +121,7 @@ public class StreamSender { HashMap map = new HashMap<>(16); - map.put(currentTimeString(), content); + map.put(TimeUtils.currentTimeString(), content); return doSendLogToStream(streamKey, map); diff --git a/agent/src/main/java/io/wdd/agent/initialization/bootup/CollectSystemInfo.java b/agent/src/main/java/io/wdd/agent/initialization/bootup/CollectSystemInfo.java index 734800d..52155c8 100644 --- a/agent/src/main/java/io/wdd/agent/initialization/bootup/CollectSystemInfo.java +++ b/agent/src/main/java/io/wdd/agent/initialization/bootup/CollectSystemInfo.java @@ -33,8 +33,35 @@ public class CollectSystemInfo implements ApplicationContextAware { public AgentServerInfo agentServerInfo; - @Bean - @Lazy + + + + @PostConstruct + private void getInjectServerInfo(){ + + log.info("[ Octopus Agent ]-- Starting getInjectServerInfo"); + + agentServerInfo = (AgentServerInfo) context.getBean("agentServerInfo"); + + if (ObjectUtils.isEmpty(agentServerInfo)) { + throw new MyRuntimeException(" Collect server info error !"); + } + + //log.info("host server info has been collected == {}", agentServerInfo); + + // start to send message to Octopus Server + octopusAgentInitService.SendInfoToServer(agentServerInfo); + + //log.info("PassThroughTopicName server info has been send to octopus server !"); + + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.context = applicationContext; + } + + @Deprecated public void initialReadingEnvironment(){ // https://zhuanlan.zhihu.com/p/449416472 @@ -84,29 +111,4 @@ public class CollectSystemInfo implements ApplicationContextAware { } } - - @PostConstruct - private void getInjectServerInfo(){ - - log.info("Octopus Agent -- Starting getInjectServerInfo"); - - agentServerInfo = (AgentServerInfo) context.getBean("agentServerInfo"); - - if (ObjectUtils.isEmpty(agentServerInfo)) { - throw new MyRuntimeException(" Collect server info error !"); - } - - //log.info("host server info has been collected == {}", agentServerInfo); - - // start to send message to Octopus Server - octopusAgentInitService.SendInfoToServer(agentServerInfo); - - //log.info("PassThroughTopicName server info has been send to octopus server !"); - - } - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.context = applicationContext; - } } diff --git a/agent/src/main/java/io/wdd/agent/status/hardware/AgentStatusCollector.java b/agent/src/main/java/io/wdd/agent/status/hardware/AgentStatusCollector.java index 43f9117..5e29be4 100644 --- a/agent/src/main/java/io/wdd/agent/status/hardware/AgentStatusCollector.java +++ b/agent/src/main/java/io/wdd/agent/status/hardware/AgentStatusCollector.java @@ -2,10 +2,13 @@ package io.wdd.agent.status.hardware; 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.TimeUtils; import io.wdd.agent.status.hardware.cpu.CpuInfo; import io.wdd.agent.status.hardware.memory.MemoryInfo; import io.wdd.agent.status.redisReporter.AgentStatus; import lombok.extern.slf4j.Slf4j; +import org.apache.catalina.util.ServerInfo; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import oshi.SystemInfo; @@ -27,6 +30,9 @@ public class AgentStatusCollector { @Resource ObjectMapper objectMapper; + @Resource + AgentServerInfo agentServerInfo; + private static final SystemInfo systemInfo; /** @@ -47,14 +53,15 @@ public class AgentStatusCollector { } - - public AgentStatus collect(){ + public AgentStatus collect() { AgentStatus agentStatus = AgentStatusCache.get(0); + /* base */ + agentStatus.setAgentName(agentServerInfo.getServerName()); + agentStatus.setAgentTopicName(agentServerInfo.getAgentTopicName()); /* CPU */ - // help gc agentStatus.setCpuInfo(new CpuInfo(hardware.getProcessor(), 1000)); /* Memory */ @@ -63,16 +70,21 @@ public class AgentStatusCollector { /* Storage */ agentStatus.setDiskStoreInfo(hardware.getDiskStores()); - return agentStatus; + /* Network */ + agentStatus.setNetworkInfo(hardware.getNetworkIFs(false)); + + /* Time */ + agentStatus.setTime(TimeUtils.currentTimeString()); + + return agentStatus; } - private void sendAgentStatusToRedis(){ + public void sendAgentStatusToRedis() { try { - log.info("time is [{}] , and agent status are [{}]", LocalDateTime.now(), objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(collect())); } catch (JsonProcessingException e) { diff --git a/agent/src/main/java/io/wdd/agent/status/hardware/cpu/CpuInfo.java b/agent/src/main/java/io/wdd/agent/status/hardware/cpu/CpuInfo.java index ebe3925..65c6179 100644 --- a/agent/src/main/java/io/wdd/agent/status/hardware/cpu/CpuInfo.java +++ b/agent/src/main/java/io/wdd/agent/status/hardware/cpu/CpuInfo.java @@ -1,9 +1,15 @@ package io.wdd.agent.status.hardware.cpu; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; import oshi.hardware.CentralProcessor; import java.text.DecimalFormat; +@AllArgsConstructor +@Data +@NoArgsConstructor public class CpuInfo { diff --git a/agent/src/main/java/io/wdd/agent/status/hardware/memory/MemoryInfo.java b/agent/src/main/java/io/wdd/agent/status/hardware/memory/MemoryInfo.java index f3fe425..e515ab0 100644 --- a/agent/src/main/java/io/wdd/agent/status/hardware/memory/MemoryInfo.java +++ b/agent/src/main/java/io/wdd/agent/status/hardware/memory/MemoryInfo.java @@ -36,7 +36,7 @@ public class MemoryInfo { return MemoryInfo.builder() .memoryType(memory.getPhysicalMemory().get(0).getMemoryType()) .total(FormatUtils.formatData(memory.getTotal())) - .available(FormatUtils.formatRate(memory.getAvailable())) + .available(FormatUtils.formatData(memory.getAvailable())) .usage(FormatUtils.formatData(memory.getTotal() - memory.getAvailable())) .swapTotal(FormatUtils.formatData(virtualMemory.getSwapTotal())) .swapUsage(FormatUtils.formatData(virtualMemory.getSwapUsed())) diff --git a/agent/src/main/java/io/wdd/agent/status/redisReporter/AgentStatus.java b/agent/src/main/java/io/wdd/agent/status/redisReporter/AgentStatus.java index 6e1edaf..2d75ad6 100644 --- a/agent/src/main/java/io/wdd/agent/status/redisReporter/AgentStatus.java +++ b/agent/src/main/java/io/wdd/agent/status/redisReporter/AgentStatus.java @@ -8,6 +8,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; import oshi.hardware.HWDiskStore; +import oshi.hardware.NetworkIF; import java.util.List; @@ -17,13 +18,19 @@ import java.util.List; @SuperBuilder(toBuilder = true) public class AgentStatus { - CpuInfo cpuInfo; + String time; + String agentName; + + String agentTopicName; + + + CpuInfo cpuInfo; MemoryInfo memoryInfo; - List diskStoreInfo; + List networkInfo; } diff --git a/agent/src/test/java/io/wdd/agent/OSHITest.java b/agent/src/test/java/io/wdd/agent/OSHITest.java index fb53452..f2947cb 100644 --- a/agent/src/test/java/io/wdd/agent/OSHITest.java +++ b/agent/src/test/java/io/wdd/agent/OSHITest.java @@ -2,6 +2,7 @@ package io.wdd.agent; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import io.wdd.agent.status.hardware.AgentStatusCollector; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import oshi.SystemInfo; @@ -10,13 +11,24 @@ import oshi.software.os.OSFileStore; import oshi.software.os.OperatingSystem; import oshi.util.tuples.Pair; +import javax.annotation.Resource; import java.io.File; import java.net.URISyntaxException; import java.util.List; +import java.util.concurrent.TimeUnit; @SpringBootTest public class OSHITest { + @Resource + AgentStatusCollector agentStatusCollector; + + @Test + void testCollect(){ + agentStatusCollector.sendAgentStatusToRedis(); + + } + @Test void getSystemHardwareInfo(){ @@ -45,8 +57,24 @@ public class OSHITest { GlobalMemory mem = hal.getMemory(); System.out.println(mapper.writeValueAsString(mem)); + for (int i = 0; i < 100; i++) { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.setDefaultLeniency(true); + String s = objectMapper.writeValueAsString(mem); + } + + TimeUnit.SECONDS.sleep(10); + GlobalMemory globalMemory = hal.getMemory(); + System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(globalMemory)); + + // DiskStorage + List diskStores = hal.getDiskStores(); + System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(diskStores)); + } catch (JsonProcessingException e) { System.out.println("Exception encountered: " + e.getMessage()); + } catch (InterruptedException e) { + throw new RuntimeException(e); } } @@ -114,4 +142,6 @@ public class OSHITest { } + + }