采集信息增加时间戳

This commit is contained in:
qiminbao
2025-08-26 18:14:37 +08:00
parent 3149e0add3
commit 25572f7804
17 changed files with 49 additions and 9 deletions
@@ -86,8 +86,10 @@ public class HeartScheduler {
}
}
// 发送心跳包
long timestamp = System.currentTimeMillis();
JSONObject object = new JSONObject();
object.put("strength","31");
object.put("timestamp",timestamp);
String clientId = AgentUtil.getMotherboardUUID();
Message message = Message.builder().clientId(clientId).dataType("HEARTBEAT").data(object.toString()).build();
// 将对象转为 JSON 字符串 标识
@@ -19,7 +19,9 @@ public class CpuServiceImpl implements CpuService {
@Override
public CpuVO query() {
CpuVO cpuVO = CpuVO.builder().build();
// 获取当前时间戳(毫秒级)
long timestamp = System.currentTimeMillis();
CpuVO cpuVO = CpuVO.builder().timestamp(timestamp).build();
try {
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
@@ -123,6 +125,9 @@ public class CpuServiceImpl implements CpuService {
// duration.toHours() % 24,
// duration.toMinutes() % 60,
// duration.getSeconds() % 60);
return cpuVO;
} catch (InterruptedException e) {
e.printStackTrace();
@@ -20,12 +20,12 @@ public class DiskServiceImpl implements DiskService {
List<DiskVO> tempList = new ArrayList<>();
List<DiskVO> resultList = new ArrayList<>();
SystemInfo si = new SystemInfo();
long timestamp = System.currentTimeMillis();
System.out.println("=========================================================");
// 获取磁盘IO信息
System.out.println("\n=== 磁盘IO信息 ===");
for (HWDiskStore disk : si.getHardware().getDiskStores()) {
DiskVO diskVO = DiskVO.builder().build();
DiskVO diskVO = DiskVO.builder().timestamp(timestamp).build();
diskVO.setName(disk.getName());//磁盘名称
diskVO.setSerial(disk.getSerial());//序列号
diskVO.setTotal(disk.getSize());//磁盘大小
@@ -94,6 +94,7 @@ public class DiskServiceImpl implements DiskService {
@Override
public List<PointVO> queryPointList() {
long timestamp = System.currentTimeMillis();
List<PointVO> list = new ArrayList<>();
SystemInfo si = new SystemInfo();
// 获取文件系统信息
@@ -106,7 +107,7 @@ public class DiskServiceImpl implements DiskService {
double usagePercentage = totalSpace > 0 ?
(double) (totalSpace - freeSpace) / totalSpace * 100 : 0;
PointVO pointVO = PointVO.builder().build();
PointVO pointVO = PointVO.builder().timestamp(timestamp).build();
pointVO.setMount(fs.getMount());//挂载点
pointVO.setVfsType(fs.getType());//文件系统类型
pointVO.setVfsTotal(totalSpace);//总空间
@@ -47,8 +47,9 @@ public class DockerServiceImpl implements DockerService {
// 打印容器信息
System.out.println("运行中的Docker容器:");
System.out.println("容器ID\t\t镜像\t\t状态\t\t名称");
long timestamp = System.currentTimeMillis();
for (Container container : containers) {
DockerVO dockerVO = DockerVO.builder().build();
DockerVO dockerVO = DockerVO.builder().timestamp(timestamp).build();
String id = container.getId().substring(0, 12); // 只显示短ID
String image = container.getImage().length() > 15 ?
container.getImage().substring(0, 15) + "..." : container.getImage();
@@ -14,7 +14,8 @@ import java.util.Map;
public class MemoryServiceImpl implements MemoryService {
@Override
public MemoryVO query() {
MemoryVO memoryVO = MemoryVO.builder().build();
long timestamp = System.currentTimeMillis();
MemoryVO memoryVO = MemoryVO.builder().timestamp(timestamp).build();
try {
Map<String, Long> memInfo = parseMemInfo();
@@ -18,6 +18,7 @@ import java.util.concurrent.TimeUnit;
public class NetServiceImpl implements NetService {
@Override
public List<NetVO> query() {
long timestamp = System.currentTimeMillis();
List<NetVO> list = new ArrayList<>();
try {
SystemInfo si = new SystemInfo();
@@ -75,6 +76,7 @@ public class NetServiceImpl implements NetService {
netVO.setOutDropped(net.getCollisions());//出站丢包
netVO.setInSpeed(bytesRecv);//接收流量
netVO.setOutSpeed(bytesSent);//发送流量
netVO.setTimestamp(timestamp);
list.add(netVO);
}
}
@@ -106,6 +106,7 @@ public class SwitchBoardServiceImpl implements SwitchBoardService {
System.out.printf("%-5s %-15s %-10s %-15s %-8s %-12s %-12s%n",
"Index", "Name", "Type", "Speed", "Status", "InBytes", "OutBytes");
List<SwitchBoardVO> list = new ArrayList<>();
long timestamp = System.currentTimeMillis();
for (int i = 1; i <= ifNumber; i++) {
pdu = new PDU();
for (String baseOID : ifOIDs) {
@@ -128,6 +129,7 @@ public class SwitchBoardServiceImpl implements SwitchBoardService {
.status(status)
.inBytes(inBytes)
.outBytes(outBytes)
.timestamp(timestamp)
.build();
list.add(boardVO);
System.out.printf("%-5d %-15s %-10s %-15s %-8s %-12d %-12d%n",
@@ -18,7 +18,8 @@ import java.time.format.DateTimeFormatter;
public class SystemServiceImpl implements SystemService {
@Override
public SystemVO query() {
SystemVO systemVO = SystemVO.builder().build();
long timestamp = System.currentTimeMillis();
SystemVO systemVO = SystemVO.builder().timestamp(timestamp).build();
try {
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
@@ -52,6 +52,9 @@ public class CpuVO implements Serializable {
@Schema(description = "CPU用户进程所花费的时间")
private long user;
@Schema(description = "时间戳")
private long timestamp;
}
@@ -43,5 +43,8 @@ public class DiskVO implements Serializable {
@Schema(description = "磁盘读取字节")
private long readBytes;
@Schema(description = "时间戳")
private long timestamp;
}
@@ -37,5 +37,8 @@ public class DockerVO implements Serializable {
@Schema(description = "容器网络发送速率")
private String netOutSpeed;
@Schema(description = "时间戳")
private long timestamp;
}
@@ -34,4 +34,7 @@ public class MemoryVO implements Serializable {
@Schema(description = "总内存")
private long total;
@Schema(description = "时间戳")
private long timestamp;
}
@@ -46,4 +46,7 @@ public class NetVO implements Serializable {
// @Schema(description = "接收速度")
// private String speed;
@Schema(description = "时间戳")
private long timestamp;
}
@@ -30,4 +30,7 @@ public class PointVO implements Serializable {
@Schema(description = "空间利用率")
private double vfsUtil;
@Schema(description = "时间戳")
private long timestamp;
}
@@ -31,5 +31,8 @@ public class SwitchBoardVO implements Serializable {
@Schema(description = "发送流量")
private long outBytes;
@Schema(description = "时间戳")
private long timestamp;
}
@@ -48,8 +48,8 @@ public class SystemVO implements Serializable {
private String uuid;
// @Schema(description = "时间戳")
// private long timeStamp;
@Schema(description = "时间戳")
private long timestamp;
}
@@ -60,8 +60,10 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<Message> {
IdleStateEvent event = (IdleStateEvent) evt;
if (event.state() == IdleState.WRITER_IDLE) {
// 发送心跳包
long timestamp = System.currentTimeMillis();
JSONObject object = new JSONObject();
object.put("strength","31");
object.put("timestamp",timestamp);
String clientId = AgentUtil.getMotherboardUUID();
Message message = Message.builder().clientId(clientId).dataType("HEARTBEAT").data(object.toString()).build();
// 将对象转为 JSON 字符串
@@ -86,9 +88,11 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<Message> {
retryCount = 0; // 连接成功时重置重试次数
// 连接建立后可以发送登录认证消息
long timestamp = System.currentTimeMillis();
String clientId = AgentUtil.getMotherboardUUID();
JSONObject object = new JSONObject();
object.put("uuid",clientId);
object.put("timestamp",timestamp);
Message message = Message.builder().clientId(clientId).dataType("LOGIN").data(object.toString()).build();
// 将对象转为 JSON 字符串
String json = "agent-tcp:"+JSON.toJSONString(message)+"@tong-ran";