[ agent ] [ status ] - accomplish agent status - 3
This commit is contained in:
@@ -5,6 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class AgentApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -7,31 +7,24 @@ import io.wdd.agent.config.utils.TimeUtils;
|
||||
import io.wdd.agent.status.hardware.CpuInfo;
|
||||
import io.wdd.agent.status.hardware.MemoryInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.connection.stream.StreamRecords;
|
||||
import org.springframework.data.redis.connection.stream.StringRecord;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import oshi.SystemInfo;
|
||||
import oshi.hardware.HardwareAbstractionLayer;
|
||||
import oshi.software.os.OperatingSystem;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class AgentStatusCollector {
|
||||
|
||||
@Resource
|
||||
RedisTemplate redisTemplate;
|
||||
|
||||
@Resource
|
||||
ObjectMapper objectMapper;
|
||||
|
||||
@Resource
|
||||
AgentServerInfo agentServerInfo;
|
||||
|
||||
|
||||
private static final SystemInfo systemInfo;
|
||||
/**
|
||||
* 硬件信息
|
||||
@@ -41,7 +34,6 @@ public class AgentStatusCollector {
|
||||
* 系统信息
|
||||
*/
|
||||
private static final OperatingSystem os;
|
||||
|
||||
private static final List<AgentStatus> AgentStatusCache = Collections.singletonList(new AgentStatus());
|
||||
|
||||
static {
|
||||
@@ -50,6 +42,12 @@ public class AgentStatusCollector {
|
||||
os = systemInfo.getOperatingSystem();
|
||||
}
|
||||
|
||||
@Resource
|
||||
RedisTemplate redisTemplate;
|
||||
@Resource
|
||||
ObjectMapper objectMapper;
|
||||
@Resource
|
||||
AgentServerInfo agentServerInfo;
|
||||
|
||||
public AgentStatus collect() {
|
||||
|
||||
@@ -81,11 +79,21 @@ public class AgentStatusCollector {
|
||||
}
|
||||
|
||||
|
||||
// agent boot up 60s then start to report its status
|
||||
// at the fix rate of 15s
|
||||
@Scheduled(initialDelay = 60000, fixedRate = 5000)
|
||||
public void sendAgentStatusToRedis() {
|
||||
|
||||
try {
|
||||
|
||||
log.info("time is [{}] , and agent status are [{}]", LocalDateTime.now(), objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(collect()));
|
||||
String statusStreamKey = agentServerInfo.getServerName() + "-status";
|
||||
|
||||
Map<String, String> map = Map.of(TimeUtils.currentTimeString(), objectMapper.writeValueAsString(collect()));
|
||||
|
||||
StringRecord stringRecord = StreamRecords.string(map).withStreamKey(statusStreamKey);
|
||||
|
||||
log.debug("Agent Status is ==> {}",map);
|
||||
redisTemplate.opsForStream().add(stringRecord);
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
Reference in New Issue
Block a user