交换机采集数据优化

This commit is contained in:
qiminbao
2025-09-26 14:13:19 +08:00
parent 90af1f83ca
commit b07abc881d
3 changed files with 70 additions and 42 deletions
@@ -62,48 +62,54 @@ public class AgentEndpoint {
String netOID = jsonObject.getString("netOID"); String netOID = jsonObject.getString("netOID");
LinkedHashMap<String, String> map = JSON.parseObject(netOID, new TypeReference<LinkedHashMap<String, String>>() {}); LinkedHashMap<String, String> map = JSON.parseObject(netOID, new TypeReference<LinkedHashMap<String, String>>() {});
GlobalConfig.SWITCH_NET_OID = map; GlobalConfig.SWITCH_NET_OID = map;
if(map.containsKey(GlobalConfig.NET_INDEX_PARAM)){ LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
GlobalConfig.NET_INDEX_OID = map.get(GlobalConfig.NET_INDEX_PARAM); if(m.containsKey(GlobalConfig.NET_INDEX_PARAM)){
GlobalConfig.NET_INDEX_OID = m.get(GlobalConfig.NET_INDEX_PARAM);
} }
} }
if(jsonObject.containsKey("moduleOID")){ if(jsonObject.containsKey("moduleOID")){
String moduleOID = jsonObject.getString("moduleOID"); String moduleOID = jsonObject.getString("moduleOID");
LinkedHashMap<String, String> map = JSON.parseObject(moduleOID, new TypeReference<LinkedHashMap<String, String>>() {}); LinkedHashMap<String, String> map = JSON.parseObject(moduleOID, new TypeReference<LinkedHashMap<String, String>>() {});
GlobalConfig.SWITCH_MODULE_OID = map; GlobalConfig.SWITCH_MODULE_OID = map;
if(map.containsKey(GlobalConfig.MODULE_INDEX_PARAM)){ LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
GlobalConfig.MODULE_INDEX_OID = map.get(GlobalConfig.MODULE_INDEX_PARAM); if(m.containsKey(GlobalConfig.MODULE_INDEX_PARAM)){
GlobalConfig.MODULE_INDEX_OID = m.get(GlobalConfig.MODULE_INDEX_PARAM);
} }
} }
if(jsonObject.containsKey("mpuOID")){ if(jsonObject.containsKey("mpuOID")){
String mpuOID = jsonObject.getString("mpuOID"); String mpuOID = jsonObject.getString("mpuOID");
LinkedHashMap<String, String> map = JSON.parseObject(mpuOID, new TypeReference<LinkedHashMap<String, String>>() {}); LinkedHashMap<String, String> map = JSON.parseObject(mpuOID, new TypeReference<LinkedHashMap<String, String>>() {});
GlobalConfig.SWITCH_MPU_OID = map; GlobalConfig.SWITCH_MPU_OID = map;
if(map.containsKey(GlobalConfig.MPU_INDEX_PARAM)){ LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
GlobalConfig.MPU_INDEX_OID = map.get(GlobalConfig.MPU_INDEX_PARAM); if(m.containsKey(GlobalConfig.MPU_INDEX_PARAM)){
GlobalConfig.MPU_INDEX_OID = m.get(GlobalConfig.MPU_INDEX_PARAM);
} }
} }
if(jsonObject.containsKey("pwrOID")){ if(jsonObject.containsKey("pwrOID")){
String pwrOID = jsonObject.getString("pwrOID"); String pwrOID = jsonObject.getString("pwrOID");
LinkedHashMap<String, String> map = JSON.parseObject(pwrOID, new TypeReference<LinkedHashMap<String, String>>() {}); LinkedHashMap<String, String> map = JSON.parseObject(pwrOID, new TypeReference<LinkedHashMap<String, String>>() {});
GlobalConfig.SWITCH_PWR_OID = map; GlobalConfig.SWITCH_PWR_OID = map;
if(map.containsKey(GlobalConfig.PWR_INDEX_PARAM)){ LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
GlobalConfig.PWR_INDEX_OID = map.get(GlobalConfig.PWR_INDEX_PARAM); if(m.containsKey(GlobalConfig.PWR_INDEX_PARAM)){
GlobalConfig.PWR_INDEX_OID = m.get(GlobalConfig.PWR_INDEX_PARAM);
} }
} }
if(jsonObject.containsKey("fanOID")){ if(jsonObject.containsKey("fanOID")){
String fanOID = jsonObject.getString("fanOID"); String fanOID = jsonObject.getString("fanOID");
LinkedHashMap<String, String> map = JSON.parseObject(fanOID, new TypeReference<LinkedHashMap<String, String>>() {}); LinkedHashMap<String, String> map = JSON.parseObject(fanOID, new TypeReference<LinkedHashMap<String, String>>() {});
GlobalConfig.SWITCH_FAN_OID = map; GlobalConfig.SWITCH_FAN_OID = map;
if(map.containsKey(GlobalConfig.FAN_INDEX_PARAM)){ LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
GlobalConfig.FAN_INDEX_OID = map.get(GlobalConfig.FAN_INDEX_PARAM); if(m.containsKey(GlobalConfig.FAN_INDEX_PARAM)){
GlobalConfig.FAN_INDEX_OID = m.get(GlobalConfig.FAN_INDEX_PARAM);
} }
} }
if(jsonObject.containsKey("otherOID")){ if(jsonObject.containsKey("otherOID")){
String otherOID = jsonObject.getString("otherOID"); String otherOID = jsonObject.getString("otherOID");
LinkedHashMap<String, String> map = JSON.parseObject(otherOID, new TypeReference<LinkedHashMap<String, String>>() {}); LinkedHashMap<String, String> map = JSON.parseObject(otherOID, new TypeReference<LinkedHashMap<String, String>>() {});
GlobalConfig.SWITCH_OTHER_OID = map; GlobalConfig.SWITCH_OTHER_OID = map;
if(map.containsKey(GlobalConfig.OTHER_INDEX_PARAM)){ LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
GlobalConfig.OTHER_INDEX_OID = map.get(GlobalConfig.OTHER_INDEX_PARAM); if(m.containsKey(GlobalConfig.OTHER_INDEX_PARAM)){
GlobalConfig.OTHER_INDEX_OID = m.get(GlobalConfig.OTHER_INDEX_PARAM);
} }
} }
if(jsonObject.containsKey("filters")){ if(jsonObject.containsKey("filters")){
@@ -6,7 +6,6 @@ import com.alibaba.fastjson2.JSONObject;
import com.tongran.agent.client.core.config.GlobalConfig; import com.tongran.agent.client.core.config.GlobalConfig;
import com.tongran.agent.client.core.vo.SwitchBoardVO; import com.tongran.agent.client.core.vo.SwitchBoardVO;
import com.tongran.agent.client.service.SwitchBoardService; import com.tongran.agent.client.service.SwitchBoardService;
import com.tongran.agent.client.utils.AgentUtil;
import com.tongran.agent.client.utils.AssertLog; import com.tongran.agent.client.utils.AssertLog;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.snmp4j.*; import org.snmp4j.*;
@@ -409,29 +408,45 @@ public class SwitchBoardServiceImpl implements SwitchBoardService {
System.err.println("未初始化索引OID"); System.err.println("未初始化索引OID");
return ""; return "";
} }
OID oid = new OID(ifNumberOID);
PDU pdu = new PDU(); PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(ifNumberOID))); pdu.add(new VariableBinding(new OID(ifNumberOID)));
pdu.setType(PDU.GET); pdu.setType(PDU.GETBULK);
ResponseEvent event = snmp.send(pdu, target); pdu.setMaxRepetitions(100); // 每次获取最多 100 个
if (event == null || event.getResponse() == null) { pdu.setNonRepeaters(0);
System.err.println("无法获取索引列表"); List<Integer> indexes = new ArrayList<>();
return ""; boolean finished = false;
} while (!finished) {
int ifNumber = event.getResponse().get(0).getVariable().toInt(); ResponseEvent response = snmp.send(pdu, target);
System.out.println("\n=== 接口数量: " + ifNumber + " ==="); PDU responsePdu = response.getResponse();
List<String> index_list = new ArrayList<>(); if (responsePdu == null) {
if (event != null && event.getResponse() != null) { System.err.println("Timeout: No response from device.");
VariableBinding[] vbs = event.getResponse().getVariableBindings().toArray(new VariableBinding[0]); break;
for (int i = 0; i < vbs.length; i++) { }
String value = vbs[i].getOid().toString(); if (responsePdu.getErrorStatus() != 0) {
AssertLog.info("索引OID{},值={}", type,value); System.err.println("Error: " + responsePdu.getErrorStatusText());
String lastValue = AgentUtil.getLastOid(vbs[i].getOid()); break;
if(!filter_value.contains(lastValue)){ }
index_list.add(lastValue); // 处理每个返回结果
for (VariableBinding vb : responsePdu.getVariableBindings()) {
OID returnedOid = vb.getOid();
// 检查是否仍在目标 OID 子树下
if (!returnedOid.startsWith(oid)) {
finished = true;
break;
}
// 提取最后一个值:就是 ifIndex 编号
int ifIndex = returnedOid.get(returnedOid.size() - 1);
// System.out.println("Found port index: " + ifIndex);
if(!filter_value.contains(String.valueOf(ifIndex))){
indexes.add(ifIndex);
} }
} }
// 更新下一次请求的起始 OIDSNMP Walk
pdu.set(0, new VariableBinding(responsePdu.getVariableBindings().get(0).getOid()));
} }
if(CollectionUtil.isEmpty(index_list)){ System.out.println("\n=== 接口数量: " + indexes.size() + " ===");
if(CollectionUtil.isEmpty(indexes)){
System.err.println("返回获取索引列表为空"); System.err.println("返回获取索引列表为空");
return ""; return "";
} }
@@ -451,13 +466,14 @@ public class SwitchBoardServiceImpl implements SwitchBoardService {
ps += "," + params[i]; ps += "," + params[i];
os += "," + ifOIDs[i]; os += "," + ifOIDs[i];
} }
} }
} }
ifOIDs = os.split(","); ifOIDs = os.split(",");
params = ps.split(","); params = ps.split(",");
List<String> otherList = new ArrayList<>(); List<Integer> otherList = new ArrayList<>();
otherList.add(index_list.get(0)); otherList.add(indexes.get(0));
index_list = otherList; indexes = otherList;
} }
// 获取每个接口的信息 // 获取每个接口的信息
// String[] ifOIDs = { // String[] ifOIDs = {
@@ -474,14 +490,13 @@ public class SwitchBoardServiceImpl implements SwitchBoardService {
return ""; return "";
} }
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
for (int i = 0; i < index_list.size(); i++) { for (int i = 0; i < indexes.size(); i++) {
pdu = new PDU(); pdu = new PDU();
for (String baseOID : ifOIDs) { for (String baseOID : ifOIDs) {
pdu.add(new VariableBinding(new OID(baseOID + "." + index_list.get(i)))); pdu.add(new VariableBinding(new OID(baseOID + "." + indexes.get(i))));
} }
pdu.setType(PDU.GET); pdu.setType(PDU.GET);
ResponseEvent event = snmp.send(pdu, target);
event = snmp.send(pdu, target);
// System.out.printf("%-5s %-15s %-10s %-15s %-8s %-12s %-12s%n", // System.out.printf("%-5s %-15s %-10s %-15s %-8s %-12s %-12s%n",
// "Index", "Name", "Type", "Speed", "Status", "InBytes", "OutBytes"); // "Index", "Name", "Type", "Speed", "Status", "InBytes", "OutBytes");
// if (event != null && event.getResponse() != null) { // if (event != null && event.getResponse() != null) {
@@ -13,10 +13,7 @@ import java.net.UnknownHostException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Base64; import java.util.*;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
public class AgentUtil { public class AgentUtil {
@@ -234,4 +231,14 @@ public class AgentUtil {
return null; return null;
} }
public static LinkedHashMap<String, String> swapMap(LinkedHashMap<String, String> original) {
LinkedHashMap<String, String> swapped = new LinkedHashMap<>();
for (Map.Entry<String, String> entry : original.entrySet()) {
if (entry.getValue() != null) { // 避免 null key
swapped.put(entry.getValue(), entry.getKey());
}
}
return swapped;
}
} }