修改clientId
This commit is contained in:
+8
-2
@@ -1,4 +1,4 @@
|
||||
package com.tongran.agentserver.server.collect.config;
|
||||
package com.tongran.agentserver.core.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -11,10 +11,16 @@ import java.util.Properties;
|
||||
*/
|
||||
public class GlobalConfig {
|
||||
|
||||
// 定义全局变量
|
||||
/**
|
||||
* 交换机 全局变量
|
||||
*/
|
||||
public static String COMMUNITY;
|
||||
public static String SWITCH_IP;
|
||||
public static int PORT;
|
||||
public static int SWITCH_DATA_TYPE = 0; //交换机流量采集类型:0、实时;1、累加;
|
||||
public static String[] ifOIDs; //交换机OID
|
||||
|
||||
|
||||
|
||||
// 静态代码块,在类加载时执行
|
||||
static {
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
package com.tongran.agentserver.server.collect.switchboard.impl;
|
||||
|
||||
import com.tongran.agentserver.server.collect.config.GlobalConfig;
|
||||
import com.tongran.agentserver.core.config.GlobalConfig;
|
||||
import com.tongran.agentserver.server.collect.switchboard.SwitchBoardService;
|
||||
import com.tongran.agentserver.server.collect.vo.SwitchBoardVO;
|
||||
import org.snmp4j.*;
|
||||
|
||||
@@ -1,29 +1,89 @@
|
||||
package com.tongran.agentserver.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import oshi.SystemInfo;
|
||||
import oshi.hardware.Baseboard;
|
||||
import oshi.hardware.HardwareAbstractionLayer;
|
||||
|
||||
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.util.Base64;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class AgentUtil {
|
||||
|
||||
public static String getMotherboardUUID() {
|
||||
SystemInfo si = new SystemInfo();
|
||||
HardwareAbstractionLayer hal = si.getHardware();
|
||||
Baseboard baseboard = hal.getComputerSystem().getBaseboard();
|
||||
if(StringUtils.isNotBlank(baseboard.getSerialNumber())){
|
||||
return baseboard.getSerialNumber();
|
||||
}
|
||||
return "client-001";
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user