交换机监控策略、snmp工具
This commit is contained in:
+15
-3
@@ -123,10 +123,22 @@ public class RmMonitorPolicyController extends BaseController
|
||||
*/
|
||||
@RequiresPermissions("rocketmq:policy:add")
|
||||
@Log(title = "资源监控策略", businessType = BusinessType.INSERT)
|
||||
@PostMapping("/addSwitchPolicy")
|
||||
public AjaxResult addSwitchPolicy(@RequestBody RmMonitorPolicy rmMonitorPolicy)
|
||||
@PostMapping("/addResourcePolicy")
|
||||
public AjaxResult addResourcePolicy(@RequestBody RmMonitorPolicy rmMonitorPolicy)
|
||||
{
|
||||
int rows = rmMonitorPolicyService.addSwitchPolicy(rmMonitorPolicy);
|
||||
int rows = rmMonitorPolicyService.addResourcePolicy(rmMonitorPolicy);
|
||||
return toAjax(rows);
|
||||
}
|
||||
/**
|
||||
* 修改交换机监控策略
|
||||
*/
|
||||
@RequiresPermissions("rocketmq:policy:update")
|
||||
@Log(title = "资源监控策略", businessType = BusinessType.INSERT)
|
||||
@PostMapping("/updateResourcePolicy")
|
||||
public AjaxResult updateResourcePolicy(@RequestBody RmMonitorPolicy rmMonitorPolicy)
|
||||
{
|
||||
int rows = rmMonitorPolicyService.updateResourcePolicy(rmMonitorPolicy);
|
||||
return toAjax(rows);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,5 +18,7 @@ public class RmMonitorPolicyVo
|
||||
|
||||
/** 采集周期 */
|
||||
private Long collectionCycle;
|
||||
/** 是否采集 */
|
||||
private String monitorStatus;
|
||||
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ public interface RmTemplateLinuxMapper
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteRmTemplateLinuxById(Long id);
|
||||
public int deleteRmTemplateLinuxByPolicyId(Long policyId);
|
||||
|
||||
/**
|
||||
* 批量删除Linux系统监控项
|
||||
@@ -65,4 +66,6 @@ public interface RmTemplateLinuxMapper
|
||||
|
||||
int deleteByTemplateId(Long templateId);
|
||||
int updateByTemplateId(Long templateId);
|
||||
|
||||
int updateByPolicyId(Long policyId);
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ public interface RmTemplateSwitchMapper
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteRmTemplateSwitchById(Long id);
|
||||
public int deleteRmTemplateSwitchByPolicyId(Long policyId);
|
||||
|
||||
/**
|
||||
* 批量删除交换机监控模板
|
||||
@@ -70,4 +71,5 @@ public interface RmTemplateSwitchMapper
|
||||
int deleteByTemplateId(Long templateId);
|
||||
|
||||
int updateByTemplateId(Long templateId);
|
||||
int updateByPolicyId(Long policyId);
|
||||
}
|
||||
|
||||
+3
-1
@@ -68,9 +68,11 @@ public interface IRmMonitorPolicyService
|
||||
* @param rmMonitorPolicy
|
||||
* @return
|
||||
*/
|
||||
int addSwitchPolicy(RmMonitorPolicy rmMonitorPolicy);
|
||||
int addResourcePolicy(RmMonitorPolicy rmMonitorPolicy);
|
||||
|
||||
Map<String, Object> getRmMonitorPolicyMsgById(Long id);
|
||||
|
||||
int issuePolicy(Long id);
|
||||
|
||||
int updateResourcePolicy(RmMonitorPolicy rmMonitorPolicy);
|
||||
}
|
||||
|
||||
+82
-9
@@ -81,11 +81,15 @@ public class RmMonitorPolicyServiceImpl implements IRmMonitorPolicyService
|
||||
List<RmMonitorPolicy> list = rmMonitorPolicyMapper.selectRmMonitorPolicyList(rmMonitorPolicy);
|
||||
for (RmMonitorPolicy monitorPolicy : list) {
|
||||
// 资源组名称
|
||||
monitorPolicy.setResourceGroupName(dataProcessUtil.getResourceGroupNameById(monitorPolicy.getResourceGroupId()));
|
||||
if(monitorPolicy.getResourceGroupId() != null){
|
||||
monitorPolicy.setResourceGroupName(dataProcessUtil.getResourceGroupNameById(monitorPolicy.getResourceGroupId()));
|
||||
}
|
||||
// 模板名称
|
||||
Long templateId = monitorPolicy.getTemplateId();
|
||||
RmMonitorTemplate rmMonitorTemplate = rmMonitorTemplateMapper.selectRmMonitorTemplateById(templateId);
|
||||
monitorPolicy.setTemplateName(rmMonitorTemplate.getTemplateName());
|
||||
if(rmMonitorTemplate != null) {
|
||||
monitorPolicy.setTemplateName(rmMonitorTemplate.getTemplateName());
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
@@ -176,8 +180,13 @@ public class RmMonitorPolicyServiceImpl implements IRmMonitorPolicyService
|
||||
RmMonitorPolicy rmMonitorPolicy = rmMonitorPolicyMapper.selectRmMonitorPolicyById(id);
|
||||
Long templateId = rmMonitorPolicy.getTemplateId();
|
||||
// 删除策略的时候,把该模板的采集周期置空
|
||||
|
||||
rmTemplateLinuxMapper.updateByTemplateId(templateId);
|
||||
rmTemplateSwitchMapper.updateByTemplateId(templateId);
|
||||
if(rmMonitorPolicy.getTemplateId()==null){
|
||||
rmTemplateLinuxMapper.deleteRmTemplateLinuxByPolicyId(id);
|
||||
rmTemplateSwitchMapper.deleteRmTemplateSwitchByPolicyId(id);
|
||||
}
|
||||
}
|
||||
return rmMonitorPolicyMapper.deleteRmMonitorPolicyByIds(ids);
|
||||
}
|
||||
@@ -238,7 +247,7 @@ public class RmMonitorPolicyServiceImpl implements IRmMonitorPolicyService
|
||||
}
|
||||
|
||||
@Override
|
||||
public int addSwitchPolicy(RmMonitorPolicy rmMonitorPolicy) {
|
||||
public int addResourcePolicy(RmMonitorPolicy rmMonitorPolicy) {
|
||||
// 保存策略信息
|
||||
rmMonitorPolicy.setCreateBy(SecurityUtils.getUsername());
|
||||
rmMonitorPolicyMapper.insertRmMonitorPolicy(rmMonitorPolicy);
|
||||
@@ -279,6 +288,60 @@ public class RmMonitorPolicyServiceImpl implements IRmMonitorPolicyService
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/**
|
||||
* 修改资源监控策略
|
||||
*
|
||||
* @param rmMonitorPolicy 资源监控策略
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int updateResourcePolicy(RmMonitorPolicy rmMonitorPolicy)
|
||||
{
|
||||
rmMonitorPolicy.setUpdateTime(DateUtils.getNowDate());
|
||||
// 策略状态改为待下发
|
||||
rmMonitorPolicy.setStatus("0");
|
||||
rmMonitorPolicyMapper.updateRmMonitorPolicy(rmMonitorPolicy);
|
||||
// 拿到采集周期
|
||||
List<RmMonitorPolicyVo> collectionAndIdList = rmMonitorPolicy.getCollectionAndIdList();
|
||||
if(!collectionAndIdList.isEmpty()){
|
||||
if("linux".equals(rmMonitorPolicy.getResourceType())){
|
||||
// 修改之前先删除
|
||||
rmTemplateLinuxMapper.deleteRmTemplateLinuxByPolicyId(rmMonitorPolicy.getId());
|
||||
for (RmMonitorPolicyVo rmMonitorPolicyVo : collectionAndIdList) {
|
||||
RmInitialMonitorItem monitorItems = rmInitialMonitorItemMapper.selectRmInitialMonitorItemById(rmMonitorPolicyVo.getId());
|
||||
RmTemplateLinux linuxItem = new RmTemplateLinux();
|
||||
linuxItem.setPolicyId(rmMonitorPolicy.getId());
|
||||
linuxItem.setMetricKey(monitorItems.getMetricKey());
|
||||
linuxItem.setMetricName(monitorItems.getMetricName());
|
||||
linuxItem.setDataType(monitorItems.getDataType());
|
||||
linuxItem.setItemType(monitorItems.getItemType());
|
||||
linuxItem.setCollectionCycle(rmMonitorPolicyVo.getCollectionCycle());
|
||||
rmTemplateLinuxMapper.insertRmTemplateLinux(linuxItem);
|
||||
}
|
||||
}else if("switch".equals(rmMonitorPolicy.getResourceType())){
|
||||
// 修改之前先置空所有采集周期
|
||||
rmTemplateSwitchMapper.deleteRmTemplateSwitchByPolicyId(rmMonitorPolicy.getId());
|
||||
for (RmMonitorPolicyVo rmMonitorPolicyVo : collectionAndIdList) {
|
||||
// 添加采集周期
|
||||
RmInitialMonitorItem monitorItems = rmInitialMonitorItemMapper.selectRmInitialMonitorItemById(rmMonitorPolicyVo.getId());
|
||||
RmTemplateSwitch switchItem = new RmTemplateSwitch();
|
||||
switchItem.setPolicyId(rmMonitorPolicy.getId());
|
||||
switchItem.setMetricKey(monitorItems.getMetricKey());
|
||||
switchItem.setMetricName(monitorItems.getMetricName());
|
||||
switchItem.setOid(monitorItems.getOid());
|
||||
switchItem.setFilterValue(monitorItems.getFilterValue());
|
||||
switchItem.setSwitchDescription(monitorItems.getMonitorDescription());
|
||||
switchItem.setDataType(monitorItems.getDataType());
|
||||
switchItem.setItemType(monitorItems.getItemType());
|
||||
switchItem.setCollectionCycle(rmMonitorPolicyVo.getCollectionCycle());
|
||||
rmTemplateSwitchMapper.insertRmTemplateSwitch(switchItem);
|
||||
}
|
||||
}else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id查询监控策略详情
|
||||
@@ -292,16 +355,26 @@ public class RmMonitorPolicyServiceImpl implements IRmMonitorPolicyService
|
||||
// 1. 获取资源监控策略基础信息
|
||||
RmMonitorPolicy rmMonitorPolicy = rmMonitorPolicyMapper.selectRmMonitorPolicyById(id);
|
||||
if(rmMonitorPolicy != null){
|
||||
rmMonitorPolicy.setResourceGroupName(dataProcessUtil.getResourceGroupNameById(rmMonitorPolicy.getResourceGroupId()));
|
||||
if(rmMonitorPolicy.getResourceGroupId()!=null){
|
||||
rmMonitorPolicy.setResourceGroupName(dataProcessUtil.getResourceGroupNameById(rmMonitorPolicy.getResourceGroupId()));
|
||||
}
|
||||
result.put("policy", rmMonitorPolicy);
|
||||
// 模板id
|
||||
Long templateId = rmMonitorPolicy.getTemplateId();
|
||||
Long templateId = rmMonitorPolicy.getTemplateId() == null ? id : rmMonitorPolicy.getTemplateId();
|
||||
|
||||
// 2. 处理Linux模板数据
|
||||
dataProcessUtil.processLinuxTemplateData(templateId, result);
|
||||
if(rmMonitorPolicy.getTemplateId() == null){
|
||||
// 2. 处理Linux模板数据
|
||||
dataProcessUtil.processLinuxPolicyData(templateId, result);
|
||||
|
||||
// 3. 处理Switch模板数据
|
||||
dataProcessUtil.processSwitchTemplateData(templateId, result);
|
||||
// 3. 处理Switch模板数据
|
||||
dataProcessUtil.processSwitchPolicyData(templateId, result);
|
||||
}else{
|
||||
// 2. 处理Linux模板数据
|
||||
dataProcessUtil.processLinuxTemplateData(templateId, result);
|
||||
|
||||
// 3. 处理Switch模板数据
|
||||
dataProcessUtil.processSwitchTemplateData(templateId, result);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -0,0 +1,415 @@
|
||||
package com.ruoyi.rocketmq.snmp;
|
||||
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import com.ruoyi.rocketmq.snmp.dto.CollectionResult;
|
||||
import com.ruoyi.rocketmq.snmp.dto.SwitchOidDto;
|
||||
import org.snmp4j.CommunityTarget;
|
||||
import org.snmp4j.PDU;
|
||||
import org.snmp4j.Snmp;
|
||||
import org.snmp4j.TransportMapping;
|
||||
import org.snmp4j.event.ResponseEvent;
|
||||
import org.snmp4j.mp.SnmpConstants;
|
||||
import org.snmp4j.smi.*;
|
||||
import org.snmp4j.transport.DefaultUdpTransportMapping;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 动态OID多交换机采集管理器
|
||||
*/
|
||||
public class DynamicOidCollector {
|
||||
|
||||
// 全局SNMP实例
|
||||
private Snmp snmp;
|
||||
|
||||
// 线程池
|
||||
private final ExecutorService switchExecutor = Executors.newFixedThreadPool(10);
|
||||
private final ExecutorService deviceTypeExecutor = Executors.newFixedThreadPool(20);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 初始化SNMP
|
||||
*/
|
||||
public void init() throws IOException {
|
||||
TransportMapping<?> transport = new DefaultUdpTransportMapping();
|
||||
snmp = new Snmp(transport);
|
||||
transport.listen();
|
||||
System.out.println("SNMP服务初始化完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭资源
|
||||
*/
|
||||
public void shutdown() {
|
||||
try {
|
||||
if (snmp != null) {
|
||||
snmp.close();
|
||||
}
|
||||
switchExecutor.shutdown();
|
||||
deviceTypeExecutor.shutdown();
|
||||
System.out.println("采集器已关闭");
|
||||
} catch (IOException e) {
|
||||
System.err.println("关闭资源时出错: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据设备类型获取信息(核心方法 - 动态OID版本)
|
||||
*/
|
||||
public CollectionResult getInterfaceInfoByType(SwitchOidDto switchVo, String deviceType) {
|
||||
// 获取该设备类型的OID映射
|
||||
Map<String, String> oidParams = switchVo.getOidsByType(deviceType);
|
||||
if (oidParams == null || oidParams.isEmpty()) {
|
||||
return new CollectionResult(switchVo.getIp(), deviceType, "该设备类型未配置OID");
|
||||
}
|
||||
|
||||
|
||||
List<Map<String, String>> results = new ArrayList<>();
|
||||
|
||||
try {
|
||||
// 创建目标
|
||||
CommunityTarget target = createTarget(switchVo);
|
||||
// 判断是否需要索引处理
|
||||
boolean needIndex = needIndexProcessing(oidParams, deviceType);
|
||||
// 获取过滤条件
|
||||
List<String> filterValues = switchVo.getFilterByType(deviceType);
|
||||
System.out.printf("采集交换机 %s 的 %s 设备信息,筛选条件: %s%n",
|
||||
switchVo.getIp(), deviceType, filterValues);
|
||||
if(needIndex){
|
||||
String indexOID = getIndexOidFromOidParams(oidParams);
|
||||
// 获取索引OID(使用第一个OID作为索引基础)
|
||||
if (StringUtils.equals(deviceType, "net")) {
|
||||
indexOID = "1.3.6.1.2.1.2.2.1.1";
|
||||
}
|
||||
// 获取索引列表
|
||||
List<Integer> indexes = getIndexes(target, indexOID, filterValues);
|
||||
if (indexes.isEmpty()) {
|
||||
System.out.printf("交换机 %s 的 %s 设备索引为空%n", switchVo.getIp(), deviceType);
|
||||
return new CollectionResult(switchVo.getIp(), deviceType, results, true);
|
||||
}
|
||||
|
||||
// 获取每个索引的设备信息
|
||||
for (Integer index : indexes) {
|
||||
Map<String, String> deviceInfo = getDeviceInfo(target, index, oidParams);
|
||||
if (!deviceInfo.isEmpty()) {
|
||||
deviceInfo.put("index", String.valueOf(index));
|
||||
results.add(deviceInfo);
|
||||
}
|
||||
}
|
||||
|
||||
}else {
|
||||
// 不需要索引处理的情况(标量类型OID),传入-1表示标量
|
||||
Map<String, String> deviceInfo = getDeviceInfo(target, -1, oidParams);
|
||||
if (!deviceInfo.isEmpty()) {
|
||||
results.add(deviceInfo);
|
||||
}
|
||||
}
|
||||
return new CollectionResult(switchVo.getIp(), deviceType, results, true);
|
||||
} catch (Exception e) {
|
||||
String errorMsg = String.format("采集失败: %s", e.getMessage());
|
||||
System.err.printf("采集交换机 %s 的 %s 信息失败: %s%n",
|
||||
switchVo.getIp(), deviceType, errorMsg);
|
||||
return new CollectionResult(switchVo.getIp(), deviceType, errorMsg);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 判断是否需要索引处理
|
||||
* 针对特定的系统信息OID,不需要索引处理
|
||||
*/
|
||||
private boolean needIndexProcessing(Map<String, String> oidParams, String deviceType) {
|
||||
// 定义不需要索引处理的系统信息OID列表
|
||||
Set<String> noIndexOids = new HashSet<>(Arrays.asList(
|
||||
"1.3.6.1.2.1.1.1.0", // sysDescr
|
||||
"1.3.6.1.2.1.1.2.0", // sysObjectID
|
||||
"1.3.6.1.2.1.1.3.0", // sysUpTime
|
||||
"1.3.6.1.2.1.1.4.0", // sysContact
|
||||
"1.3.6.1.2.1.1.5.0", // sysName
|
||||
"1.3.6.1.2.1.1.6.0", // sysLocation
|
||||
"1.3.6.1.4.1.2011.5.25.183.1.4.0" // hwStackSystemMac
|
||||
));
|
||||
|
||||
// 检查所有配置的OID
|
||||
for (String oid : oidParams.keySet()) {
|
||||
// 如果OID在不需要索引的列表中,返回false
|
||||
if (noIndexOids.contains(oid)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 默认情况下,其他OID需要索引处理
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从OID参数中提取索引OID
|
||||
*/
|
||||
private String getIndexOidFromOidParams(Map<String, String> oidParams) {
|
||||
if (oidParams.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
// 获取第一个OID作为索引基础
|
||||
String firstOid = "1.3.6.1.2.1.47.1.1.1.1.5";
|
||||
return firstOid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备索引列表
|
||||
*/
|
||||
private List<Integer> getIndexes(CommunityTarget target, String indexOID, List<String> filterValues)
|
||||
throws IOException {
|
||||
List<Integer> indexes = new ArrayList<>();
|
||||
|
||||
PDU pdu = new PDU();
|
||||
pdu.add(new VariableBinding(new OID(indexOID)));
|
||||
pdu.setType(PDU.GETBULK);
|
||||
pdu.setMaxRepetitions(100);
|
||||
pdu.setNonRepeaters(0);
|
||||
|
||||
OID baseOid = new OID(indexOID);
|
||||
boolean finished = false;
|
||||
|
||||
while (!finished) {
|
||||
ResponseEvent response = snmp.send(pdu, target);
|
||||
PDU responsePdu = response.getResponse();
|
||||
|
||||
if (responsePdu == null || responsePdu.getErrorStatus() != 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (VariableBinding vb : responsePdu.getVariableBindings()) {
|
||||
OID returnedOid = vb.getOid();
|
||||
if (!returnedOid.startsWith(baseOid)) {
|
||||
finished = true;
|
||||
break;
|
||||
}
|
||||
|
||||
int index = returnedOid.get(returnedOid.size() - 1);
|
||||
String value = vb.getVariable().toString();
|
||||
|
||||
// 应用过滤条件
|
||||
if (filterValues == null || filterValues.isEmpty()) {
|
||||
indexes.add(index);
|
||||
} else if (filterValues.contains(value)) {
|
||||
indexes.add(index);
|
||||
}
|
||||
}
|
||||
|
||||
if (!finished && !responsePdu.getVariableBindings().isEmpty()) {
|
||||
pdu.set(0, new VariableBinding(responsePdu.getVariableBindings().get(0).getOid()));
|
||||
}
|
||||
}
|
||||
|
||||
return indexes.stream().distinct().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备信息(支持标量和表类型OID)
|
||||
* @param target SNMP目标
|
||||
* @param index 索引号,-1表示标量OID(不需要加索引后缀)
|
||||
* @param oidParams OID参数映射
|
||||
* @return 设备信息
|
||||
*/
|
||||
private Map<String, String> getDeviceInfo(CommunityTarget target, int index,
|
||||
Map<String, String> oidParams)
|
||||
throws IOException {
|
||||
Map<String, String> deviceInfo = new HashMap<>();
|
||||
|
||||
PDU pdu = new PDU();
|
||||
for (String oid : oidParams.keySet()) {
|
||||
String finalOid = oid;
|
||||
// 如果index不为-1,为表类型OID添加索引后缀
|
||||
if (index != -1) {
|
||||
finalOid = oid + "." + index;
|
||||
}
|
||||
pdu.add(new VariableBinding(new OID(finalOid)));
|
||||
}
|
||||
pdu.setType(PDU.GET);
|
||||
|
||||
ResponseEvent event = snmp.send(pdu, target);
|
||||
if (event != null && event.getResponse() != null) {
|
||||
PDU responsePdu = event.getResponse();
|
||||
if (responsePdu.getErrorStatus() == PDU.noError) {
|
||||
int i = 0;
|
||||
for (String paramName : oidParams.values()) {
|
||||
if (i < responsePdu.size()) {
|
||||
VariableBinding vb = responsePdu.get(i);
|
||||
deviceInfo.put(paramName, vb.getVariable().toString());
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return deviceInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建SNMP目标
|
||||
*/
|
||||
private CommunityTarget createTarget(SwitchOidDto switchVo) {
|
||||
Address targetAddress = new UdpAddress(switchVo.getIp() + "/" + switchVo.getPort());
|
||||
CommunityTarget target = new CommunityTarget();
|
||||
target.setCommunity(new OctetString(switchVo.getCommunity()));
|
||||
target.setAddress(targetAddress);
|
||||
target.setRetries(2);
|
||||
target.setTimeout(5000);
|
||||
target.setVersion(SnmpConstants.version2c);
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* 采集单个交换机的所有设备类型
|
||||
*/
|
||||
public Map<String, CollectionResult> collectSwitch(SwitchOidDto switchVo) {
|
||||
Map<String, CollectionResult> results = new ConcurrentHashMap<>();
|
||||
|
||||
// 获取交换机支持的所有设备类型
|
||||
List<String> deviceTypes = switchVo.getSupportedDeviceTypes();
|
||||
if (deviceTypes.isEmpty()) {
|
||||
System.out.printf("交换机 %s 未配置任何设备类型的OID%n", switchVo.getIp());
|
||||
return results;
|
||||
}
|
||||
|
||||
// 并发采集所有设备类型
|
||||
List<CompletableFuture<Void>> futures = deviceTypes.stream()
|
||||
.map(deviceType -> CompletableFuture.runAsync(() -> {
|
||||
CollectionResult result = getInterfaceInfoByType(switchVo, deviceType);
|
||||
results.put(deviceType, result);
|
||||
|
||||
if (result.isSuccess()) {
|
||||
System.out.printf("交换机 %s 的 %s 采集完成: %d 个设备%n",
|
||||
switchVo.getIp(), deviceType, result.getData().size());
|
||||
} else {
|
||||
System.out.printf("交换机 %s 的 %s 采集失败: %s%n",
|
||||
switchVo.getIp(), deviceType, result.getErrorMessage());
|
||||
}
|
||||
}, deviceTypeExecutor))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 等待所有设备类型采集完成
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 并发采集多个交换机
|
||||
*/
|
||||
public Map<String, Map<String, CollectionResult>> collectAllSwitches(List<SwitchOidDto> switches) {
|
||||
Map<String, Map<String, CollectionResult>> allResults = new ConcurrentHashMap<>();
|
||||
|
||||
List<CompletableFuture<Void>> futures = switches.stream()
|
||||
.map(switchVo -> CompletableFuture.runAsync(() -> {
|
||||
System.out.printf("开始采集交换机: %s%n", switchVo.getIp());
|
||||
|
||||
Map<String, CollectionResult> switchResults = collectSwitch(switchVo);
|
||||
allResults.put(switchVo.getIp(), switchResults);
|
||||
|
||||
System.out.printf("完成采集交换机: %s, 采集了 %d 种设备类型%n",
|
||||
switchVo.getIp(), switchResults.size());
|
||||
}, switchExecutor))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 等待所有交换机采集完成
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
|
||||
|
||||
return allResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建示例交换机配置
|
||||
*/
|
||||
public static SwitchOidDto createExampleSwitch(String ip, String community) {
|
||||
SwitchOidDto switchVo = new SwitchOidDto(ip, community);
|
||||
// 配置系统信息OID
|
||||
Map<String, String> otherOids = new LinkedHashMap<>();
|
||||
otherOids.put("1.3.6.1.2.1.1.1.0", "ifDescr");
|
||||
otherOids.put("1.3.6.1.2.1.1.2.0", "sysObjectID");
|
||||
otherOids.put("1.3.6.1.2.1.1.3.0", "sysUpTime");
|
||||
otherOids.put("1.3.6.1.2.1.1.4.0", "sysContact");
|
||||
otherOids.put("1.3.6.1.2.1.1.5.0", "sysName");
|
||||
otherOids.put("1.3.6.1.2.1.1.6.0", "sysLocation");
|
||||
otherOids.put("1.3.6.1.4.1.2011.5.25.183.1.4.0", "hwStackSystemMac");
|
||||
switchVo.setOtherOID(otherOids);
|
||||
// 配置网络接口OID
|
||||
Map<String, String> netOids = new LinkedHashMap<>();
|
||||
netOids.put("1.3.6.1.2.1.2.2.1.2", "ifDescr");
|
||||
netOids.put("1.3.6.1.2.1.2.2.1.3", "ifType");
|
||||
netOids.put("1.3.6.1.2.1.2.2.1.8", "ifOperStatus");
|
||||
switchVo.setNetOID(netOids);
|
||||
|
||||
// 配置电源OID
|
||||
// Map<String, String> pwrOids = new LinkedHashMap<>();
|
||||
// pwrOids.put("1.3.6.1.2.1.47.1.1.1.1.5", "pwrEntIndex");
|
||||
// pwrOids.put("1.3.6.1.2.1.47.1.1.1.1.7", "pwrName");
|
||||
// pwrOids.put("1.3.6.1.4.1.2011.5.25.31.1.1.18.1.6", "pwrEntityPwrState");
|
||||
// pwrOids.put("1.3.6.1.4.1.2011.5.25.31.1.1.18.1.7", "pwrEntityPwrCurrent");
|
||||
// pwrOids.put("1.3.6.1.4.1.2011.5.25.31.1.1.18.1.8", "pwrEntityPwrVoltage");
|
||||
// switchVo.setPwrOID(pwrOids);
|
||||
|
||||
// 配置过滤条件
|
||||
Map<String, List<String>> filters = new HashMap<>();
|
||||
// filters.put("pwr", Arrays.asList("6")); // 只采集正常和警告状态的电源
|
||||
switchVo.setFilters(filters);
|
||||
|
||||
return switchVo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主程序入口
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
DynamicOidCollector collector = new DynamicOidCollector();
|
||||
|
||||
try {
|
||||
// 初始化
|
||||
collector.init();
|
||||
|
||||
// 创建动态配置的交换机列表
|
||||
List<SwitchOidDto> switches = Arrays.asList(
|
||||
createExampleSwitch("123.182.89.17", "chengde_200G"),
|
||||
createExampleSwitch("1.194.193.81", "henan_anyang_10G")
|
||||
);
|
||||
|
||||
// 并发采集所有交换机
|
||||
System.out.println("开始并发采集 " + switches.size() + " 台交换机...");
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
Map<String, Map<String, CollectionResult>> results = collector.collectAllSwitches(switches);
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
|
||||
// 显示统计信息
|
||||
System.out.println("\n=== 采集统计 ===");
|
||||
int totalDevices = 0;
|
||||
for (Map.Entry<String, Map<String, CollectionResult>> entry : results.entrySet()) {
|
||||
String ip = entry.getKey();
|
||||
Map<String, CollectionResult> deviceResults = entry.getValue();
|
||||
|
||||
int switchDevices = deviceResults.values().stream()
|
||||
.filter(CollectionResult::isSuccess)
|
||||
.mapToInt(r -> r.getData().size())
|
||||
.sum();
|
||||
|
||||
totalDevices += switchDevices;
|
||||
System.out.printf("交换机 %s: %d 个设备%n", ip, switchDevices);
|
||||
}
|
||||
|
||||
System.out.printf("总计: %d 台交换机, %d 个设备%n", results.size(), totalDevices);
|
||||
System.out.printf("总采集时间: %.2f 秒%n", (endTime - startTime) / 1000.0);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("采集程序出错: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
collector.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.ruoyi.rocketmq.snmp.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 采集结果类
|
||||
*/
|
||||
@Data
|
||||
public class CollectionResult {
|
||||
String switchIp;
|
||||
String deviceType;
|
||||
List<Map<String, String>> data;
|
||||
long timestamp;
|
||||
boolean success;
|
||||
String errorMessage;
|
||||
|
||||
public CollectionResult(String switchIp, String deviceType,
|
||||
List<Map<String, String>> data, boolean success) {
|
||||
this.switchIp = switchIp;
|
||||
this.deviceType = deviceType;
|
||||
this.data = data;
|
||||
this.timestamp = System.currentTimeMillis();
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public CollectionResult(String switchIp, String deviceType, String errorMessage) {
|
||||
this.switchIp = switchIp;
|
||||
this.deviceType = deviceType;
|
||||
this.errorMessage = errorMessage;
|
||||
this.success = false;
|
||||
this.timestamp = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.ruoyi.rocketmq.snmp.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 交换机OID配置实体类
|
||||
*/
|
||||
@Data
|
||||
public class SwitchOidDto {
|
||||
private Map<String, String> netOID; // 网络接口OID映射
|
||||
private Map<String, String> moduleOID; // 模块OID映射
|
||||
private Map<String, String> mpuOID; // MPU OID映射
|
||||
private Map<String, String> pwrOID; // 电源OID映射
|
||||
private Map<String, String> fanOID; // 风扇OID映射
|
||||
private Map<String, String> otherOID; // 其他OID映射
|
||||
|
||||
/** 团体名 */
|
||||
private String community;
|
||||
/** IP地址 */
|
||||
private String ip;
|
||||
/** 端口 */
|
||||
private Integer port;
|
||||
/** 过滤值 Map<设备类型, 过滤值列表> */
|
||||
private Map<String, List<String>> filters;
|
||||
|
||||
// 构造函数
|
||||
public SwitchOidDto(String ip, String community) {
|
||||
this.ip = ip;
|
||||
this.community = community;
|
||||
this.port = 161;
|
||||
this.filters = new HashMap<>();
|
||||
this.netOID = new LinkedHashMap<>();
|
||||
this.moduleOID = new LinkedHashMap<>();
|
||||
this.mpuOID = new LinkedHashMap<>();
|
||||
this.pwrOID = new LinkedHashMap<>();
|
||||
this.fanOID = new LinkedHashMap<>();
|
||||
this.otherOID = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据设备类型获取OID映射
|
||||
*/
|
||||
public Map<String, String> getOidsByType(String deviceType) {
|
||||
switch (deviceType.toLowerCase()) {
|
||||
case "net": return netOID;
|
||||
case "module": return moduleOID;
|
||||
case "mpu": return mpuOID;
|
||||
case "pwr": return pwrOID;
|
||||
case "fan": return fanOID;
|
||||
default: return otherOID;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据设备类型获取过滤条件
|
||||
*/
|
||||
public List<String> getFilterByType(String deviceType) {
|
||||
return filters.getOrDefault(deviceType.toLowerCase(), Collections.emptyList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有支持的设备类型
|
||||
*/
|
||||
public List<String> getSupportedDeviceTypes() {
|
||||
List<String> types = new ArrayList<>();
|
||||
if (!netOID.isEmpty()) types.add("net");
|
||||
if (!moduleOID.isEmpty()) types.add("module");
|
||||
if (!mpuOID.isEmpty()) types.add("mpu");
|
||||
if (!pwrOID.isEmpty()) types.add("pwr");
|
||||
if (!fanOID.isEmpty()) types.add("fan");
|
||||
if (!otherOID.isEmpty()) types.add("other");
|
||||
return types;
|
||||
}
|
||||
}
|
||||
@@ -105,6 +105,49 @@ public class DataProcessUtil {
|
||||
result.put("resourceType", "switch");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 处理Linux模板数据
|
||||
*/
|
||||
public void processLinuxPolicyData(Long templateId, Map<String, Object> result) {
|
||||
RmTemplateLinux query = new RmTemplateLinux();
|
||||
query.setPolicyId(templateId);
|
||||
List<RmTemplateLinux> linuxItems = rmTemplateLinuxMapper.selectRmTemplateLinuxList(query);
|
||||
|
||||
if (!linuxItems.isEmpty()) {
|
||||
Map<String, List<RmTemplateLinux>> linuxData = new HashMap<>();
|
||||
linuxData.put("cpu", filterItemsByDataType(linuxItems, "CPU"));
|
||||
linuxData.put("other", filterItemsByDataType(linuxItems, "OTHER"));
|
||||
linuxData.put("vfs", filterItemsByDataType(linuxItems, "POINT"));
|
||||
linuxData.put("net", filterItemsByDataType(linuxItems, "NET"));
|
||||
linuxData.put("disk", filterItemsByDataType(linuxItems, "DISK"));
|
||||
linuxData.put("docker", filterItemsByDataType(linuxItems, "DOCKER"));
|
||||
|
||||
result.put("linux", linuxData);
|
||||
result.put("resourceType", "linux");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理Switch模板数据
|
||||
*/
|
||||
public void processSwitchPolicyData(Long templateId, Map<String, Object> result) {
|
||||
RmTemplateSwitch query = new RmTemplateSwitch();
|
||||
query.setPolicyId(templateId);
|
||||
List<RmTemplateSwitch> switchItems = rmTemplateSwitchMapper.selectRmTemplateSwitchList(query);
|
||||
|
||||
if (!switchItems.isEmpty()) {
|
||||
Map<String, List<RmTemplateSwitch>> switchData = new HashMap<>();
|
||||
switchData.put("switchOther", filterItemsByDataType(switchItems, "SYSTEM"));
|
||||
switchData.put("switchMpu", filterItemsByDataType(switchItems, "MPU"));
|
||||
switchData.put("switchPwr", filterItemsByDataType(switchItems, "POWERSOURCE"));
|
||||
switchData.put("switchNet", filterItemsByDataType(switchItems, "NETPORT"));
|
||||
switchData.put("switchModule", filterItemsByDataType(switchItems, "LIGHTMODULE"));
|
||||
switchData.put("switchFan", filterItemsByDataType(switchItems, "FAN"));
|
||||
|
||||
result.put("switch", switchData);
|
||||
result.put("resourceType", "switch");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 通用过滤方法
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user