交换机采集数据优化
This commit is contained in:
@@ -62,48 +62,54 @@ public class AgentEndpoint {
|
||||
String netOID = jsonObject.getString("netOID");
|
||||
LinkedHashMap<String, String> map = JSON.parseObject(netOID, new TypeReference<LinkedHashMap<String, String>>() {});
|
||||
GlobalConfig.SWITCH_NET_OID = map;
|
||||
if(map.containsKey(GlobalConfig.NET_INDEX_PARAM)){
|
||||
GlobalConfig.NET_INDEX_OID = map.get(GlobalConfig.NET_INDEX_PARAM);
|
||||
LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
|
||||
if(m.containsKey(GlobalConfig.NET_INDEX_PARAM)){
|
||||
GlobalConfig.NET_INDEX_OID = m.get(GlobalConfig.NET_INDEX_PARAM);
|
||||
}
|
||||
}
|
||||
if(jsonObject.containsKey("moduleOID")){
|
||||
String moduleOID = jsonObject.getString("moduleOID");
|
||||
LinkedHashMap<String, String> map = JSON.parseObject(moduleOID, new TypeReference<LinkedHashMap<String, String>>() {});
|
||||
GlobalConfig.SWITCH_MODULE_OID = map;
|
||||
if(map.containsKey(GlobalConfig.MODULE_INDEX_PARAM)){
|
||||
GlobalConfig.MODULE_INDEX_OID = map.get(GlobalConfig.MODULE_INDEX_PARAM);
|
||||
LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
|
||||
if(m.containsKey(GlobalConfig.MODULE_INDEX_PARAM)){
|
||||
GlobalConfig.MODULE_INDEX_OID = m.get(GlobalConfig.MODULE_INDEX_PARAM);
|
||||
}
|
||||
}
|
||||
if(jsonObject.containsKey("mpuOID")){
|
||||
String mpuOID = jsonObject.getString("mpuOID");
|
||||
LinkedHashMap<String, String> map = JSON.parseObject(mpuOID, new TypeReference<LinkedHashMap<String, String>>() {});
|
||||
GlobalConfig.SWITCH_MPU_OID = map;
|
||||
if(map.containsKey(GlobalConfig.MPU_INDEX_PARAM)){
|
||||
GlobalConfig.MPU_INDEX_OID = map.get(GlobalConfig.MPU_INDEX_PARAM);
|
||||
LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
|
||||
if(m.containsKey(GlobalConfig.MPU_INDEX_PARAM)){
|
||||
GlobalConfig.MPU_INDEX_OID = m.get(GlobalConfig.MPU_INDEX_PARAM);
|
||||
}
|
||||
}
|
||||
if(jsonObject.containsKey("pwrOID")){
|
||||
String pwrOID = jsonObject.getString("pwrOID");
|
||||
LinkedHashMap<String, String> map = JSON.parseObject(pwrOID, new TypeReference<LinkedHashMap<String, String>>() {});
|
||||
GlobalConfig.SWITCH_PWR_OID = map;
|
||||
if(map.containsKey(GlobalConfig.PWR_INDEX_PARAM)){
|
||||
GlobalConfig.PWR_INDEX_OID = map.get(GlobalConfig.PWR_INDEX_PARAM);
|
||||
LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
|
||||
if(m.containsKey(GlobalConfig.PWR_INDEX_PARAM)){
|
||||
GlobalConfig.PWR_INDEX_OID = m.get(GlobalConfig.PWR_INDEX_PARAM);
|
||||
}
|
||||
}
|
||||
if(jsonObject.containsKey("fanOID")){
|
||||
String fanOID = jsonObject.getString("fanOID");
|
||||
LinkedHashMap<String, String> map = JSON.parseObject(fanOID, new TypeReference<LinkedHashMap<String, String>>() {});
|
||||
GlobalConfig.SWITCH_FAN_OID = map;
|
||||
if(map.containsKey(GlobalConfig.FAN_INDEX_PARAM)){
|
||||
GlobalConfig.FAN_INDEX_OID = map.get(GlobalConfig.FAN_INDEX_PARAM);
|
||||
LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
|
||||
if(m.containsKey(GlobalConfig.FAN_INDEX_PARAM)){
|
||||
GlobalConfig.FAN_INDEX_OID = m.get(GlobalConfig.FAN_INDEX_PARAM);
|
||||
}
|
||||
}
|
||||
if(jsonObject.containsKey("otherOID")){
|
||||
String otherOID = jsonObject.getString("otherOID");
|
||||
LinkedHashMap<String, String> map = JSON.parseObject(otherOID, new TypeReference<LinkedHashMap<String, String>>() {});
|
||||
GlobalConfig.SWITCH_OTHER_OID = map;
|
||||
if(map.containsKey(GlobalConfig.OTHER_INDEX_PARAM)){
|
||||
GlobalConfig.OTHER_INDEX_OID = map.get(GlobalConfig.OTHER_INDEX_PARAM);
|
||||
LinkedHashMap<String,String> m = AgentUtil.swapMap(map);
|
||||
if(m.containsKey(GlobalConfig.OTHER_INDEX_PARAM)){
|
||||
GlobalConfig.OTHER_INDEX_OID = m.get(GlobalConfig.OTHER_INDEX_PARAM);
|
||||
}
|
||||
}
|
||||
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.vo.SwitchBoardVO;
|
||||
import com.tongran.agent.client.service.SwitchBoardService;
|
||||
import com.tongran.agent.client.utils.AgentUtil;
|
||||
import com.tongran.agent.client.utils.AssertLog;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.snmp4j.*;
|
||||
@@ -409,29 +408,45 @@ public class SwitchBoardServiceImpl implements SwitchBoardService {
|
||||
System.err.println("未初始化索引OID");
|
||||
return "";
|
||||
}
|
||||
OID oid = new OID(ifNumberOID);
|
||||
PDU pdu = new PDU();
|
||||
pdu.add(new VariableBinding(new OID(ifNumberOID)));
|
||||
pdu.setType(PDU.GET);
|
||||
ResponseEvent event = snmp.send(pdu, target);
|
||||
if (event == null || event.getResponse() == null) {
|
||||
System.err.println("无法获取索引列表");
|
||||
return "";
|
||||
}
|
||||
int ifNumber = event.getResponse().get(0).getVariable().toInt();
|
||||
System.out.println("\n=== 接口数量: " + ifNumber + " ===");
|
||||
List<String> index_list = new ArrayList<>();
|
||||
if (event != null && event.getResponse() != null) {
|
||||
VariableBinding[] vbs = event.getResponse().getVariableBindings().toArray(new VariableBinding[0]);
|
||||
for (int i = 0; i < vbs.length; i++) {
|
||||
String value = vbs[i].getOid().toString();
|
||||
AssertLog.info("索引OID:{},值={}", type,value);
|
||||
String lastValue = AgentUtil.getLastOid(vbs[i].getOid());
|
||||
if(!filter_value.contains(lastValue)){
|
||||
index_list.add(lastValue);
|
||||
pdu.setType(PDU.GETBULK);
|
||||
pdu.setMaxRepetitions(100); // 每次获取最多 100 个
|
||||
pdu.setNonRepeaters(0);
|
||||
List<Integer> indexes = new ArrayList<>();
|
||||
boolean finished = false;
|
||||
while (!finished) {
|
||||
ResponseEvent response = snmp.send(pdu, target);
|
||||
PDU responsePdu = response.getResponse();
|
||||
if (responsePdu == null) {
|
||||
System.err.println("Timeout: No response from device.");
|
||||
break;
|
||||
}
|
||||
if (responsePdu.getErrorStatus() != 0) {
|
||||
System.err.println("Error: " + responsePdu.getErrorStatusText());
|
||||
break;
|
||||
}
|
||||
// 处理每个返回结果
|
||||
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);
|
||||
}
|
||||
}
|
||||
// 更新下一次请求的起始 OID(SNMP 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("返回获取索引列表为空");
|
||||
return "";
|
||||
}
|
||||
@@ -451,13 +466,14 @@ public class SwitchBoardServiceImpl implements SwitchBoardService {
|
||||
ps += "," + params[i];
|
||||
os += "," + ifOIDs[i];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
ifOIDs = os.split(",");
|
||||
params = ps.split(",");
|
||||
List<String> otherList = new ArrayList<>();
|
||||
otherList.add(index_list.get(0));
|
||||
index_list = otherList;
|
||||
List<Integer> otherList = new ArrayList<>();
|
||||
otherList.add(indexes.get(0));
|
||||
indexes = otherList;
|
||||
}
|
||||
// 获取每个接口的信息
|
||||
// String[] ifOIDs = {
|
||||
@@ -474,14 +490,13 @@ public class SwitchBoardServiceImpl implements SwitchBoardService {
|
||||
return "";
|
||||
}
|
||||
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();
|
||||
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);
|
||||
|
||||
event = snmp.send(pdu, target);
|
||||
ResponseEvent event = snmp.send(pdu, target);
|
||||
// System.out.printf("%-5s %-15s %-10s %-15s %-8s %-12s %-12s%n",
|
||||
// "Index", "Name", "Type", "Speed", "Status", "InBytes", "OutBytes");
|
||||
// if (event != null && event.getResponse() != null) {
|
||||
|
||||
@@ -13,10 +13,7 @@ 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;
|
||||
import java.util.*;
|
||||
|
||||
public class AgentUtil {
|
||||
|
||||
@@ -234,4 +231,14 @@ public class AgentUtil {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user