初始化
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package com.tongran.agent.client.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class AgentDataUtil {
|
||||
|
||||
public static Map<String, Long> parseMemInfo() throws IOException {
|
||||
Map<String, Long> memInfo = new HashMap<>();
|
||||
try (BufferedReader br = new BufferedReader(new FileReader("/proc/meminfo"))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
String[] parts = line.split("\\s+");
|
||||
if (parts.length >= 2) {
|
||||
String key = parts[0].replace(":", "");
|
||||
long value = Long.parseLong(parts[1]);
|
||||
memInfo.put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return memInfo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
package com.tongran.agent.client.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import oshi.hardware.NetworkIF;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Base64;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class AgentUtil {
|
||||
|
||||
public static String getMotherboardUUID() {
|
||||
String hostName = getHostname();
|
||||
String primaryIp = getPrimaryIp();
|
||||
// SystemInfo si = new SystemInfo();
|
||||
// HardwareAbstractionLayer hal = si.getHardware();
|
||||
// Baseboard baseboard = hal.getComputerSystem().getBaseboard();
|
||||
// if(StringUtils.isNotBlank(baseboard.getSerialNumber())){
|
||||
// return baseboard.getSerialNumber();
|
||||
// }
|
||||
return hostName+":"+primaryIp;
|
||||
}
|
||||
|
||||
public static String getHostname() {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostName();
|
||||
} catch (UnknownHostException e) {
|
||||
return "unknown-host";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本机首选 IP 地址(通常是第一个非回环、非虚拟网卡的 IPv4 地址)
|
||||
*/
|
||||
public static String getPrimaryIp() {
|
||||
try {
|
||||
InetAddress localHost = InetAddress.getLocalHost();
|
||||
String ip = localHost.getHostAddress();
|
||||
|
||||
// 如果是 127.x.x.x,说明可能 hosts 配置有问题,需要手动查找
|
||||
if (ip.startsWith("127.")) {
|
||||
return getExternalIp();
|
||||
}
|
||||
return ip;
|
||||
} catch (UnknownHostException e) {
|
||||
return "127.0.0.1";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取第一个非回环、非虚拟网卡的 IPv4 地址
|
||||
*/
|
||||
public static String getExternalIp() {
|
||||
try {
|
||||
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||
while (interfaces.hasMoreElements()) {
|
||||
NetworkInterface iface = interfaces.nextElement();
|
||||
// 跳过虚拟网卡(如 docker, veth, lo)
|
||||
if (iface.isLoopback() || iface.isVirtual() || !iface.isUp()) {
|
||||
continue;
|
||||
}
|
||||
Enumeration<InetAddress> addresses = iface.getInetAddresses();
|
||||
while (addresses.hasMoreElements()) {
|
||||
InetAddress addr = addresses.nextElement();
|
||||
if (addr.isLoopbackAddress()) {
|
||||
continue; // 跳过 127.0.0.1
|
||||
}
|
||||
if (addr.getHostAddress().contains(":")) {
|
||||
continue; // 跳过 IPv6
|
||||
}
|
||||
return addr.getHostAddress();
|
||||
}
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "127.0.0.1";
|
||||
}
|
||||
|
||||
|
||||
public static String toJsonString(List<Map<String, String>> list) {
|
||||
try {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
return mapper.writeValueAsString(list);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("JSON转换失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void base64ToFile(String base64String, String filePath) throws IOException {
|
||||
// 解码Base64字符串
|
||||
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
|
||||
|
||||
// 写入文件
|
||||
try (FileOutputStream fos = new FileOutputStream(filePath)) {
|
||||
fos.write(decodedBytes);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getInterfaceType(NetworkIF net) {
|
||||
String name = net.getName().toLowerCase();
|
||||
String displayName = net.getDisplayName().toLowerCase();
|
||||
|
||||
if (name.startsWith("eth") || name.startsWith("en") || displayName.contains("ethernet")) {
|
||||
return "Ethernet";
|
||||
} else if (name.startsWith("wlan") || name.startsWith("wl") || displayName.contains("wireless")) {
|
||||
return "Wi-Fi";
|
||||
} else if (name.startsWith("lo") || displayName.contains("loopback")) {
|
||||
return "Loopback";
|
||||
} else if (name.startsWith("ppp") || displayName.contains("point-to-point")) {
|
||||
return "PPP";
|
||||
} else if (name.startsWith("vmnet") || displayName.contains("virtual")) {
|
||||
return "Virtual";
|
||||
} else if (name.startsWith("tun") || name.startsWith("tap")) {
|
||||
return "TUN/TAP";
|
||||
} else if (name.startsWith("br") || displayName.contains("bridge")) {
|
||||
return "Bridge";
|
||||
} else if (name.startsWith("bond") || displayName.contains("bond")) {
|
||||
return "Bond";
|
||||
} else {
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间距离下一个“分钟为 0 或 5”的时间点还差多少毫秒
|
||||
* 例如:xx:00, xx:05, xx:10, ..., xx:55
|
||||
*/
|
||||
public static long millisecondsToNext5Minute() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
// 当前分钟
|
||||
int minute = now.getMinute();
|
||||
|
||||
// 计算下一个 5 分钟整点(向上取整)
|
||||
int nextMinute = ((minute / 5) + 1) * 5;
|
||||
|
||||
LocalDateTime nextTime;
|
||||
if (nextMinute < 60) {
|
||||
// 在当前小时内
|
||||
nextTime = now.withMinute(nextMinute).withSecond(0).withNano(0);
|
||||
} else {
|
||||
// 跨小时,如 10:58 → 11:00
|
||||
nextTime = now.plusHours(1).withMinute(0).withSecond(0).withNano(0);
|
||||
}
|
||||
|
||||
// 计算相差的毫秒数
|
||||
return ChronoUnit.MILLIS.between(now, nextTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取距离下一个分钟整点的毫秒数
|
||||
*/
|
||||
public static long getMillisToNextMinute() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
// 获取下一个分钟整点时间
|
||||
LocalDateTime nextMinute = now
|
||||
.truncatedTo(ChronoUnit.MINUTES) // 截断到当前分钟
|
||||
.plusMinutes(1); // 加1分钟
|
||||
|
||||
// 计算时间差(毫秒)
|
||||
return ChronoUnit.MILLIS.between(now, nextMinute);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取距离下一个指定分钟整点的毫秒数
|
||||
*/
|
||||
public static long getMillisToNextMinuteInterval(int intervalMinutes) {
|
||||
if (intervalMinutes <= 0) {
|
||||
throw new IllegalArgumentException("Interval must be positive");
|
||||
}
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
// 计算当前分钟在间隔中的位置
|
||||
int currentMinute = now.getMinute();
|
||||
int remainder = currentMinute % intervalMinutes;
|
||||
|
||||
// 计算需要增加的分钟数
|
||||
int minutesToAdd = remainder == 0 ? intervalMinutes : intervalMinutes - remainder;
|
||||
|
||||
// 获取下一个间隔整点时间
|
||||
LocalDateTime nextInterval = now
|
||||
.truncatedTo(ChronoUnit.MINUTES) // 截断到当前分钟
|
||||
.plusMinutes(minutesToAdd) // 增加到下一个整点
|
||||
.withSecond(0) // 秒设为0
|
||||
.withNano(0); // 纳秒设为0
|
||||
|
||||
return ChronoUnit.MILLIS.between(now, nextInterval);
|
||||
}
|
||||
|
||||
public static long roundMinutes(){
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
// 四舍五入到最近的5分钟
|
||||
LocalDateTime roundedTime = now
|
||||
.truncatedTo(ChronoUnit.MINUTES) // 先去掉秒和纳秒
|
||||
.withMinute((int) (Math.round(now.getMinute() / 5.0) * 5))
|
||||
.withSecond(0)
|
||||
.withNano(0);
|
||||
// 处理分钟进位的情况
|
||||
if (roundedTime.getMinute() == 60) {
|
||||
roundedTime = roundedTime.plusHours(1).withMinute(0);
|
||||
}
|
||||
// 转换为10位时间戳
|
||||
long timestamp = roundedTime.atZone(ZoneId.systemDefault()).toEpochSecond();
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.tongran.agent.client.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 日志断言类
|
||||
*
|
||||
* @author BAO
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
public class AssertLog {
|
||||
|
||||
/**
|
||||
* 打印Info 日志
|
||||
*
|
||||
* @param format
|
||||
* @param arguments
|
||||
*/
|
||||
public static void info(String format, Object... arguments) {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.info(format, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印Debug 日志
|
||||
*
|
||||
* @param format
|
||||
* @param arguments
|
||||
*/
|
||||
public static void debug(String format, Object... arguments) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(format, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印Error 日志
|
||||
*
|
||||
* @param format
|
||||
* @param arguments
|
||||
*/
|
||||
public static void error(String format, Object... arguments) {
|
||||
if (log.isErrorEnabled()) {
|
||||
log.error(format, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印Trace 日志
|
||||
*
|
||||
* @param format
|
||||
* @param arguments
|
||||
*/
|
||||
public static void trace(String format, Object... arguments) {
|
||||
if (log.isTraceEnabled()) {
|
||||
log.trace(format, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印Warn 日志
|
||||
*
|
||||
* @param format
|
||||
* @param arguments
|
||||
*/
|
||||
public static void warn(String format, Object... arguments) {
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn(format, arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.tongran.agent.client.utils;
|
||||
|
||||
import com.tongran.agent.client.exception.code.ErrorCode;
|
||||
import com.tongran.agent.client.exception.code.GlobalErrorCode;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author BAO
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(description = "接口交互统一数据返回标准")
|
||||
public class R<T> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(description = "返回代码")
|
||||
private Integer code;
|
||||
|
||||
@Schema(description = "消息描述")
|
||||
private String msg;
|
||||
|
||||
@Schema(description = "结果对象")
|
||||
private T data;
|
||||
|
||||
public static <T> R<T> success(String msg, T t) {
|
||||
R<T> r = new R<>();
|
||||
r.setData(t);
|
||||
r.setMsg(msg);
|
||||
r.setCode(GlobalErrorCode.SUCCESS.getCode());
|
||||
return r;
|
||||
}
|
||||
|
||||
public static <T> R<T> success(T t) {
|
||||
return R.success(GlobalErrorCode.SUCCESS.getMsg(), t);
|
||||
}
|
||||
|
||||
public static <T> R<T> success() {
|
||||
return R.success(null);
|
||||
}
|
||||
|
||||
public static <T> R<T> error(String msg, Integer code) {
|
||||
R<T> r = new R<>();
|
||||
r.setMsg(msg);
|
||||
r.setCode(code);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static <T> R<T> error(Integer code, String msg) {
|
||||
R<T> r = new R<>();
|
||||
r.setMsg(msg);
|
||||
r.setCode(code);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static <T> R<T> error(ErrorCode err) {
|
||||
return R.error(err.getMsg(), err.getCode());
|
||||
}
|
||||
|
||||
public static <T> R<T> error() {
|
||||
return R.error(GlobalErrorCode.ERROR.getMsg(), GlobalErrorCode.ERROR.getCode());
|
||||
}
|
||||
|
||||
public static <T> R<T> error(String msg) {
|
||||
return R.error(msg, GlobalErrorCode.ERROR.getCode());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.tongran.agent.client.utils;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
public class RoundMinutes {
|
||||
public static long convertMillisToRoundedSeconds(long millisTimestamp) {
|
||||
// 1. 先将毫秒时间戳四舍五入到秒
|
||||
long roundedSeconds = Math.round(millisTimestamp / 1000.0);
|
||||
|
||||
// 2. 转换为LocalDateTime进行分钟调整
|
||||
LocalDateTime dateTime = LocalDateTime.ofInstant(
|
||||
Instant.ofEpochSecond(roundedSeconds),
|
||||
ZoneId.systemDefault()
|
||||
);
|
||||
|
||||
// 3. 将分钟四舍五入到5分钟间隔
|
||||
int minute = dateTime.getMinute();
|
||||
int roundedMinute = (int) (Math.round(minute / 5.0) * 5);
|
||||
|
||||
// 4. 调整时间
|
||||
LocalDateTime adjustedDateTime = dateTime
|
||||
.withMinute(roundedMinute % 60)
|
||||
.withSecond(0)
|
||||
.withNano(0);
|
||||
|
||||
// 处理进位
|
||||
if (roundedMinute == 60) {
|
||||
adjustedDateTime = adjustedDateTime.plusHours(1);
|
||||
}
|
||||
|
||||
// 5. 转换回时间戳
|
||||
return adjustedDateTime.atZone(ZoneId.systemDefault()).toEpochSecond();
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// long millisTimestamp = System.currentTimeMillis();
|
||||
// System.out.println("原始13位时间戳: " + millisTimestamp);
|
||||
// System.out.println("原始时间: " + new java.util.Date(millisTimestamp));
|
||||
//
|
||||
// long result = convertMillisToRoundedSeconds(millisTimestamp);
|
||||
// System.out.println("调整后10位时间戳: " + result);
|
||||
// System.out.println("调整后时间: " + new java.util.Date(result * 1000));
|
||||
// }
|
||||
//
|
||||
public static void main(String[] args) {
|
||||
long timestamp = 1757471699499L;
|
||||
long time = Math.round(timestamp / 1000.0);
|
||||
System.out.println(time);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user