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 deleted file mode 100644 index 542ff92..0000000 --- a/agent/src/main/java/io/wdd/agent/config/utils/TimeUtils.java +++ /dev/null @@ -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")); - } -} 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 855a9d0..aad6fed 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,7 +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.common.utils.TimeUtils; import io.wdd.agent.executor.thread.LogToArrayListCache; import lombok.SneakyThrows; 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 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.HashMap; import java.util.List; diff --git a/agent/src/main/java/io/wdd/agent/status/AgentStatusCollector.java b/agent/src/main/java/io/wdd/agent/status/AgentStatusCollector.java index 5cf4663..1bd0ac7 100644 --- a/agent/src/main/java/io/wdd/agent/status/AgentStatusCollector.java +++ b/agent/src/main/java/io/wdd/agent/status/AgentStatusCollector.java @@ -3,13 +3,8 @@ package io.wdd.agent.status; 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.common.beans.status.CpuInfo; -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 io.wdd.common.beans.status.*; +import io.wdd.common.utils.TimeUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.connection.stream.StreamRecords; 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.stereotype.Service; import oshi.SystemInfo; -import oshi.hardware.HWDiskStore; import oshi.hardware.HardwareAbstractionLayer; import oshi.software.os.OperatingSystem; @@ -25,7 +19,6 @@ import javax.annotation.Resource; import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; @Service @Slf4j @@ -82,7 +75,9 @@ public class AgentStatusCollector { ); /* operating system info */ - agentStatus.setOsInfo(os); + agentStatus.setOsInfo( + AgentSystemInfo.mapFromOHSISystem(os) + ); /* Time */ agentStatus.setTime(TimeUtils.currentTimeString()); diff --git a/common/src/main/java/io/wdd/common/beans/status/AgentStatus.java b/common/src/main/java/io/wdd/common/beans/status/AgentStatus.java index 16e1e61..c77684d 100644 --- a/common/src/main/java/io/wdd/common/beans/status/AgentStatus.java +++ b/common/src/main/java/io/wdd/common/beans/status/AgentStatus.java @@ -30,6 +30,6 @@ public class AgentStatus { List networkInfo; - OperatingSystem osInfo; + AgentSystemInfo osInfo; } diff --git a/common/src/main/java/io/wdd/common/beans/status/AgentSystemInfo.java b/common/src/main/java/io/wdd/common/beans/status/AgentSystemInfo.java index fe63dde..dccf589 100644 --- a/common/src/main/java/io/wdd/common/beans/status/AgentSystemInfo.java +++ b/common/src/main/java/io/wdd/common/beans/status/AgentSystemInfo.java @@ -1,11 +1,17 @@ package io.wdd.common.beans.status; +import io.wdd.common.utils.TimeUtils; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; import oshi.software.os.OperatingSystem; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.concurrent.TimeUnit; + @Data @AllArgsConstructor @NoArgsConstructor @@ -24,12 +30,18 @@ public class AgentSystemInfo { public static AgentSystemInfo mapFromOHSISystem(OperatingSystem os) { - return AgentSystemInfo.builder() .osInfo(String.valueOf(os.getVersionInfo())) .family(os.getFamily()) .manufacturer(os.getManufacturer()) - .bootTime() + .bootTime(TimeUtils.localDateTimeString( + LocalDateTime.ofInstant( + Instant.ofEpochMilli(os.getSystemBootTime()), + ZoneId.of("UTC+8") + ) + )) + .upTime(TimeUtils.toRelative(os.getSystemUptime()*1000)) + .build(); } diff --git a/common/src/main/java/io/wdd/common/utils/TimeUtils.java b/common/src/main/java/io/wdd/common/utils/TimeUtils.java new file mode 100644 index 0000000..5bebdd7 --- /dev/null +++ b/common/src/main/java/io/wdd/common/utils/TimeUtils.java @@ -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 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 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); + } +}