[ agent ] [ status ] - accomplish agent status - 5

This commit is contained in:
zeaslity
2023-01-05 15:36:18 +08:00
parent c600de8202
commit ff66acb950
6 changed files with 111 additions and 45 deletions

View File

@@ -1,26 +0,0 @@
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"));
}
}

View File

@@ -5,7 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.agent.config.beans.executor.CommandLog; import io.wdd.agent.config.beans.executor.CommandLog;
import io.wdd.agent.config.beans.executor.StreamSenderEntity; import io.wdd.agent.config.beans.executor.StreamSenderEntity;
import io.wdd.agent.config.utils.TimeUtils; import io.wdd.common.utils.TimeUtils;
import io.wdd.agent.executor.thread.LogToArrayListCache; import io.wdd.agent.executor.thread.LogToArrayListCache;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -20,11 +20,6 @@ import org.springframework.data.redis.connection.stream.StringRecord;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;

View File

@@ -3,13 +3,8 @@ 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.config.utils.TimeUtils; import io.wdd.common.beans.status.*;
import io.wdd.common.beans.status.CpuInfo; import io.wdd.common.utils.TimeUtils;
import io.wdd.common.beans.status.DiskInfo;
import io.wdd.common.beans.status.MemoryInfo;
import io.wdd.common.beans.status.NetworkInfo;
import io.wdd.common.beans.status.AgentStatus;
import io.wdd.common.utils.FormatUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.StreamRecords; import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.connection.stream.StringRecord; import org.springframework.data.redis.connection.stream.StringRecord;
@@ -17,7 +12,6 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import oshi.SystemInfo; import oshi.SystemInfo;
import oshi.hardware.HWDiskStore;
import oshi.hardware.HardwareAbstractionLayer; import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem; import oshi.software.os.OperatingSystem;
@@ -25,7 +19,6 @@ import javax.annotation.Resource;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
@Service @Service
@Slf4j @Slf4j
@@ -82,7 +75,9 @@ public class AgentStatusCollector {
); );
/* operating system info */ /* operating system info */
agentStatus.setOsInfo(os); agentStatus.setOsInfo(
AgentSystemInfo.mapFromOHSISystem(os)
);
/* Time */ /* Time */
agentStatus.setTime(TimeUtils.currentTimeString()); agentStatus.setTime(TimeUtils.currentTimeString());

View File

@@ -30,6 +30,6 @@ public class AgentStatus {
List<NetworkInfo> networkInfo; List<NetworkInfo> networkInfo;
OperatingSystem osInfo; AgentSystemInfo osInfo;
} }

View File

@@ -1,11 +1,17 @@
package io.wdd.common.beans.status; package io.wdd.common.beans.status;
import io.wdd.common.utils.TimeUtils;
import lombok.AllArgsConstructor; 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 oshi.software.os.OperatingSystem;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@@ -24,12 +30,18 @@ public class AgentSystemInfo {
public static AgentSystemInfo mapFromOHSISystem(OperatingSystem os) { public static AgentSystemInfo mapFromOHSISystem(OperatingSystem os) {
return AgentSystemInfo.builder() return AgentSystemInfo.builder()
.osInfo(String.valueOf(os.getVersionInfo())) .osInfo(String.valueOf(os.getVersionInfo()))
.family(os.getFamily()) .family(os.getFamily())
.manufacturer(os.getManufacturer()) .manufacturer(os.getManufacturer())
.bootTime() .bootTime(TimeUtils.localDateTimeString(
LocalDateTime.ofInstant(
Instant.ofEpochMilli(os.getSystemBootTime()),
ZoneId.of("UTC+8")
)
))
.upTime(TimeUtils.toRelative(os.getSystemUptime()*1000))
.build();
} }

View File

@@ -0,0 +1,90 @@
package io.wdd.common.utils;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
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"));
}
public static String localDateTimeString(LocalDateTime time) {
return time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
private static final Map<String, Long> times = new HashMap<>(16);
static {
times.put("year", TimeUnit.DAYS.toMillis(365));
times.put("month", TimeUnit.DAYS.toMillis(30));
times.put("week", TimeUnit.DAYS.toMillis(7));
times.put("day", TimeUnit.DAYS.toMillis(1));
times.put("hour", TimeUnit.HOURS.toMillis(1));
times.put("minute", TimeUnit.MINUTES.toMillis(1));
times.put("second", TimeUnit.SECONDS.toMillis(1));
}
public static String toRelative(long duration, int maxLevel) {
StringBuilder res = new StringBuilder();
int level = 0;
for (Map.Entry<String, Long> time : times.entrySet()){
long timeDelta = duration / time.getValue();
if (timeDelta > 0){
res.append(timeDelta)
.append(" ")
.append(time.getKey())
.append(timeDelta > 1 ? "s" : "")
.append(", ");
duration -= time.getValue() * timeDelta;
level++;
}
if (level == maxLevel){
break;
}
}
if ("".equals(res.toString())) {
return "0 seconds ago";
} else {
res.setLength(res.length() - 2);
res.append(" ago");
return res.toString();
}
}
public static String toRelative(long duration) {
return toRelative(duration, times.size());
}
public static String toRelative(Date start, Date end){
assert start.after(end);
return toRelative(end.getTime() - start.getTime());
}
public static String toRelative(Date start, Date end, int level){
assert start.after(end);
return toRelative(end.getTime() - start.getTime(), level);
}
}