[agent]-[status] accomplish base - 1
This commit is contained in:
@@ -110,4 +110,10 @@ public class AgentServerInfo {
|
||||
@Value("${machineId}")
|
||||
private String machineId;
|
||||
|
||||
/*
|
||||
* get from octopus server at the end of initialization
|
||||
*
|
||||
* */
|
||||
private String agentTopicName;
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
26
agent/src/main/java/io/wdd/agent/config/utils/TimeUtils.java
Normal file
26
agent/src/main/java/io/wdd/agent/config/utils/TimeUtils.java
Normal file
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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<String, String> map = new HashMap<>(16);
|
||||
|
||||
map.put(currentTimeString(), content);
|
||||
map.put(TimeUtils.currentTimeString(), content);
|
||||
|
||||
return doSendLogToStream(streamKey, map);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
|
||||
@@ -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()))
|
||||
|
||||
@@ -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<HWDiskStore> diskStoreInfo;
|
||||
|
||||
List<NetworkIF> networkInfo;
|
||||
|
||||
}
|
||||
|
||||
@@ -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<HWDiskStore> 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 {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user