交换机采集数据优化
This commit is contained in:
@@ -38,6 +38,26 @@ public class GlobalConfig {
|
||||
public static LinkedHashMap<String, String> SWITCH_PWR_OID = new LinkedHashMap<>(); //交换机电源发现OID
|
||||
public static LinkedHashMap<String, String> SWITCH_FAN_OID = new LinkedHashMap<>(); //交换机风扇发现OID
|
||||
public static LinkedHashMap<String, String> SWITCH_OTHER_OID = new LinkedHashMap<>(); //交换机系统其他OID
|
||||
public static String OTHER_INDEX_PARAM = "entIndex"; //交换机其他监控项索引参数
|
||||
public static String OTHER_INDEX_OID = "";
|
||||
public static List<String> OTHER_FILTER = new ArrayList<>(); //过滤值
|
||||
public static String NET_INDEX_PARAM = "ifIndex"; //交换机网络端口索引参数
|
||||
public static String NET_INDEX_OID = "";
|
||||
public static List<String> NET_FILTER = new ArrayList<>(); //过滤值
|
||||
public static String MODULE_INDEX_PARAM = "fiberEntIndex"; //交换机光模块端口索引参数
|
||||
public static String MODULE_INDEX_OID = "";
|
||||
public static List<String> MODULE_FILTER = new ArrayList<>(); //过滤值
|
||||
public static String MPU_INDEX_PARAM = "mpuEntIndex"; //交换机MPU索引参数
|
||||
public static String MPU_INDEX_OID = "";
|
||||
public static List<String> MPU_FILTER = new ArrayList<>(); //过滤值
|
||||
public static String PWR_INDEX_PARAM = "pwrEntIndex"; //交换机电源索引参数
|
||||
public static String PWR_INDEX_OID = "";
|
||||
public static List<String> PWR_FILTER = new ArrayList<>(); //过滤值
|
||||
public static String FAN_INDEX_PARAM = "fanEntIndex"; //交换机风扇索引参数
|
||||
public static String FAN_INDEX_OID = "";
|
||||
public static List<String> FAN_FILTER = new ArrayList<>(); //过滤值
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 采集标识
|
||||
|
||||
@@ -62,32 +62,87 @@ 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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
if(jsonObject.containsKey("filters")){
|
||||
String filters = jsonObject.getString("filters");
|
||||
JSONObject object = JSONObject.parseObject(filters);
|
||||
|
||||
if(object.containsKey("netOID")){
|
||||
String netOID = object.getString("netOID");
|
||||
List<String> list = JSON.parseObject(netOID, new TypeReference<List<String>>() {});
|
||||
GlobalConfig.NET_FILTER = list;
|
||||
}
|
||||
if(object.containsKey("moduleOID")){
|
||||
String moduleOID = object.getString("moduleOID");
|
||||
List<String> list = JSON.parseObject(moduleOID, new TypeReference<List<String>>() {});
|
||||
GlobalConfig.MODULE_FILTER = list;
|
||||
}
|
||||
if(object.containsKey("mpuOID")){
|
||||
String mpuOID = object.getString("mpuOID");
|
||||
List<String> list = JSON.parseObject(mpuOID, new TypeReference<List<String>>() {});
|
||||
GlobalConfig.MPU_FILTER = list;
|
||||
}
|
||||
if(object.containsKey("pwrOID")){
|
||||
String pwrOID = object.getString("pwrOID");
|
||||
List<String> list = JSON.parseObject(pwrOID, new TypeReference<List<String>>() {});
|
||||
GlobalConfig.PWR_FILTER = list;
|
||||
}
|
||||
if(object.containsKey("fanOID")){
|
||||
String fanOID = object.getString("fanOID");
|
||||
List<String> list = JSON.parseObject(fanOID, new TypeReference<List<String>>() {});
|
||||
GlobalConfig.FAN_FILTER = list;
|
||||
}
|
||||
if(object.containsKey("otherOID")){
|
||||
String otherOID = object.getString("otherOID");
|
||||
List<String> list = JSON.parseObject(otherOID, new TypeReference<List<String>>() {});
|
||||
GlobalConfig.OTHER_FILTER = list;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
GlobalConfig.isCollect = true;
|
||||
GlobalConfig.CLIENT_ID = clientId;
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.tongran.agent.client.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
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.*;
|
||||
@@ -377,21 +379,60 @@ public class SwitchBoardServiceImpl implements SwitchBoardService {
|
||||
}
|
||||
|
||||
private static String getInterfaceInfoByType(Snmp snmp, Target target, String type, LinkedHashMap<String,String> oidParams) throws IOException {
|
||||
if(oidParams.isEmpty()){
|
||||
return "";
|
||||
}
|
||||
JSONObject json = new JSONObject();
|
||||
// 获取接口数量
|
||||
String ifNumberOID = "1.3.6.1.2.1.2.1.0";
|
||||
String ifNumberOID = "";
|
||||
List<String> filter_value = new ArrayList<>();
|
||||
if(StringUtils.equals(type,"switchNet")){
|
||||
ifNumberOID = GlobalConfig.NET_INDEX_OID;
|
||||
filter_value = GlobalConfig.NET_FILTER;
|
||||
}else if(StringUtils.equals(type,"switchModule")){
|
||||
ifNumberOID = GlobalConfig.MODULE_INDEX_OID;
|
||||
filter_value = GlobalConfig.MODULE_FILTER;
|
||||
}else if(StringUtils.equals(type,"switchMpu")){
|
||||
ifNumberOID = GlobalConfig.MPU_INDEX_OID;
|
||||
filter_value = GlobalConfig.MPU_FILTER;
|
||||
}else if(StringUtils.equals(type,"switchPwr")){
|
||||
ifNumberOID = GlobalConfig.PWR_INDEX_OID;
|
||||
filter_value = GlobalConfig.PWR_FILTER;
|
||||
}else if(StringUtils.equals(type,"switchFan")){
|
||||
ifNumberOID = GlobalConfig.FAN_INDEX_OID;
|
||||
filter_value = GlobalConfig.FAN_FILTER;
|
||||
}else {
|
||||
ifNumberOID = GlobalConfig.OTHER_INDEX_OID;
|
||||
filter_value = GlobalConfig.OTHER_FILTER;
|
||||
}
|
||||
if(StringUtils.isBlank(ifNumberOID)){
|
||||
System.err.println("未初始化索引OID");
|
||||
return "";
|
||||
}
|
||||
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("无法获取接口数量");
|
||||
System.err.println("无法获取索引列表");
|
||||
return "";
|
||||
}
|
||||
int ifNumber = event.getResponse().get(0).getVariable().toInt();
|
||||
System.out.println("\n=== 接口数量: " + ifNumber + " ===");
|
||||
if(oidParams.isEmpty()){
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(CollectionUtil.isEmpty(index_list)){
|
||||
System.err.println("返回获取索引列表为空");
|
||||
return "";
|
||||
}
|
||||
String[] ifOIDs = oidParams.keySet().toArray(new String[0]);
|
||||
@@ -414,10 +455,10 @@ public class SwitchBoardServiceImpl implements SwitchBoardService {
|
||||
}
|
||||
ifOIDs = os.split(",");
|
||||
params = ps.split(",");
|
||||
ifNumber = 1;
|
||||
List<String> otherList = new ArrayList<>();
|
||||
otherList.add(index_list.get(0));
|
||||
index_list = otherList;
|
||||
}
|
||||
|
||||
|
||||
// 获取每个接口的信息
|
||||
// String[] ifOIDs = {
|
||||
// "1.3.6.1.2.1.2.2.1.2", // ifDescr
|
||||
@@ -433,10 +474,10 @@ public class SwitchBoardServiceImpl implements SwitchBoardService {
|
||||
return "";
|
||||
}
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int i = 1; i <= ifNumber; i++) {
|
||||
for (int i = 0; i < index_list.size(); i++) {
|
||||
pdu = new PDU();
|
||||
for (String baseOID : ifOIDs) {
|
||||
pdu.add(new VariableBinding(new OID(baseOID + "." + i)));
|
||||
pdu.add(new VariableBinding(new OID(baseOID + "." + index_list.get(i))));
|
||||
}
|
||||
pdu.setType(PDU.GET);
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.tongran.agent.client.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.snmp4j.smi.OID;
|
||||
import oshi.hardware.NetworkIF;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
@@ -225,4 +226,12 @@ public class AgentUtil {
|
||||
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
public static String getLastOid(OID oid){
|
||||
if(oid.size() > 0){
|
||||
int lastValue = oid.get(oid.size() - 1);
|
||||
return String.valueOf(lastValue);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user