diff --git a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/controller/RmMonitorPolicyController.java b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/controller/RmMonitorPolicyController.java index 3d7adf3..8f03288 100644 --- a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/controller/RmMonitorPolicyController.java +++ b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/controller/RmMonitorPolicyController.java @@ -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); + } + } diff --git a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/domain/vo/RmMonitorPolicyVo.java b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/domain/vo/RmMonitorPolicyVo.java index ac39262..8ba8129 100644 --- a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/domain/vo/RmMonitorPolicyVo.java +++ b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/domain/vo/RmMonitorPolicyVo.java @@ -18,5 +18,7 @@ public class RmMonitorPolicyVo /** 采集周期 */ private Long collectionCycle; + /** 是否采集 */ + private String monitorStatus; } diff --git a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/mapper/RmTemplateLinuxMapper.java b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/mapper/RmTemplateLinuxMapper.java index 97dab9b..6804a02 100644 --- a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/mapper/RmTemplateLinuxMapper.java +++ b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/mapper/RmTemplateLinuxMapper.java @@ -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); } diff --git a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/mapper/RmTemplateSwitchMapper.java b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/mapper/RmTemplateSwitchMapper.java index 5e4e804..632e90f 100644 --- a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/mapper/RmTemplateSwitchMapper.java +++ b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/mapper/RmTemplateSwitchMapper.java @@ -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); } diff --git a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/service/IRmMonitorPolicyService.java b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/service/IRmMonitorPolicyService.java index ec4f51c..aec3919 100644 --- a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/service/IRmMonitorPolicyService.java +++ b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/service/IRmMonitorPolicyService.java @@ -68,9 +68,11 @@ public interface IRmMonitorPolicyService * @param rmMonitorPolicy * @return */ - int addSwitchPolicy(RmMonitorPolicy rmMonitorPolicy); + int addResourcePolicy(RmMonitorPolicy rmMonitorPolicy); Map getRmMonitorPolicyMsgById(Long id); int issuePolicy(Long id); + + int updateResourcePolicy(RmMonitorPolicy rmMonitorPolicy); } diff --git a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/service/impl/RmMonitorPolicyServiceImpl.java b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/service/impl/RmMonitorPolicyServiceImpl.java index 18172b8..6d455d0 100644 --- a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/service/impl/RmMonitorPolicyServiceImpl.java +++ b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/service/impl/RmMonitorPolicyServiceImpl.java @@ -81,11 +81,15 @@ public class RmMonitorPolicyServiceImpl implements IRmMonitorPolicyService List 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 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; diff --git a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/snmp/DynamicOidCollector.java b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/snmp/DynamicOidCollector.java new file mode 100644 index 0000000..2c0ff70 --- /dev/null +++ b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/snmp/DynamicOidCollector.java @@ -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 oidParams = switchVo.getOidsByType(deviceType); + if (oidParams == null || oidParams.isEmpty()) { + return new CollectionResult(switchVo.getIp(), deviceType, "该设备类型未配置OID"); + } + + + List> results = new ArrayList<>(); + + try { + // 创建目标 + CommunityTarget target = createTarget(switchVo); + // 判断是否需要索引处理 + boolean needIndex = needIndexProcessing(oidParams, deviceType); + // 获取过滤条件 + List 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 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 deviceInfo = getDeviceInfo(target, index, oidParams); + if (!deviceInfo.isEmpty()) { + deviceInfo.put("index", String.valueOf(index)); + results.add(deviceInfo); + } + } + + }else { + // 不需要索引处理的情况(标量类型OID),传入-1表示标量 + Map 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 oidParams, String deviceType) { + // 定义不需要索引处理的系统信息OID列表 + Set 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 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 getIndexes(CommunityTarget target, String indexOID, List filterValues) + throws IOException { + List 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 getDeviceInfo(CommunityTarget target, int index, + Map oidParams) + throws IOException { + Map 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 collectSwitch(SwitchOidDto switchVo) { + Map results = new ConcurrentHashMap<>(); + + // 获取交换机支持的所有设备类型 + List deviceTypes = switchVo.getSupportedDeviceTypes(); + if (deviceTypes.isEmpty()) { + System.out.printf("交换机 %s 未配置任何设备类型的OID%n", switchVo.getIp()); + return results; + } + + // 并发采集所有设备类型 + List> 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> collectAllSwitches(List switches) { + Map> allResults = new ConcurrentHashMap<>(); + + List> futures = switches.stream() + .map(switchVo -> CompletableFuture.runAsync(() -> { + System.out.printf("开始采集交换机: %s%n", switchVo.getIp()); + + Map 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 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 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 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> 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 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> results = collector.collectAllSwitches(switches); + + long endTime = System.currentTimeMillis(); + + // 显示统计信息 + System.out.println("\n=== 采集统计 ==="); + int totalDevices = 0; + for (Map.Entry> entry : results.entrySet()) { + String ip = entry.getKey(); + Map 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(); + } + } +} \ No newline at end of file diff --git a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/snmp/dto/CollectionResult.java b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/snmp/dto/CollectionResult.java new file mode 100644 index 0000000..81f7961 --- /dev/null +++ b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/snmp/dto/CollectionResult.java @@ -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> data; + long timestamp; + boolean success; + String errorMessage; + + public CollectionResult(String switchIp, String deviceType, + List> 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(); + } +} \ No newline at end of file diff --git a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/snmp/dto/SwitchOidDto.java b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/snmp/dto/SwitchOidDto.java new file mode 100644 index 0000000..0a8917c --- /dev/null +++ b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/snmp/dto/SwitchOidDto.java @@ -0,0 +1,77 @@ +package com.ruoyi.rocketmq.snmp.dto; + +import lombok.Data; + +import java.util.*; + +/** + * 交换机OID配置实体类 + */ +@Data +public class SwitchOidDto { + private Map netOID; // 网络接口OID映射 + private Map moduleOID; // 模块OID映射 + private Map mpuOID; // MPU OID映射 + private Map pwrOID; // 电源OID映射 + private Map fanOID; // 风扇OID映射 + private Map otherOID; // 其他OID映射 + + /** 团体名 */ + private String community; + /** IP地址 */ + private String ip; + /** 端口 */ + private Integer port; + /** 过滤值 Map<设备类型, 过滤值列表> */ + private Map> 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 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 getFilterByType(String deviceType) { + return filters.getOrDefault(deviceType.toLowerCase(), Collections.emptyList()); + } + + /** + * 获取所有支持的设备类型 + */ + public List getSupportedDeviceTypes() { + List 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; + } +} \ No newline at end of file diff --git a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/utils/DataProcessUtil.java b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/utils/DataProcessUtil.java index 100acff..c804f3f 100644 --- a/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/utils/DataProcessUtil.java +++ b/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/utils/DataProcessUtil.java @@ -105,6 +105,49 @@ public class DataProcessUtil { result.put("resourceType", "switch"); } } + /** + * 处理Linux模板数据 + */ + public void processLinuxPolicyData(Long templateId, Map result) { + RmTemplateLinux query = new RmTemplateLinux(); + query.setPolicyId(templateId); + List linuxItems = rmTemplateLinuxMapper.selectRmTemplateLinuxList(query); + + if (!linuxItems.isEmpty()) { + Map> 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 result) { + RmTemplateSwitch query = new RmTemplateSwitch(); + query.setPolicyId(templateId); + List switchItems = rmTemplateSwitchMapper.selectRmTemplateSwitchList(query); + + if (!switchItems.isEmpty()) { + Map> 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"); + } + } /** * 通用过滤方法 */ diff --git a/ruoyi-rocketmq/src/main/resources/mapper/rocketmq/RmTemplateLinuxMapper.xml b/ruoyi-rocketmq/src/main/resources/mapper/rocketmq/RmTemplateLinuxMapper.xml index 1ceaeab..05d9a0c 100644 --- a/ruoyi-rocketmq/src/main/resources/mapper/rocketmq/RmTemplateLinuxMapper.xml +++ b/ruoyi-rocketmq/src/main/resources/mapper/rocketmq/RmTemplateLinuxMapper.xml @@ -134,4 +134,11 @@ update rm_template_linux set collection_cycle = null where template_id = #{templateId} + + delete from rm_template_linux where policy_id = #{policyId} + + + update rm_template_linux set collection_cycle = null + where policy_id = #{policyId} + \ No newline at end of file diff --git a/ruoyi-rocketmq/src/main/resources/mapper/rocketmq/RmTemplateSwitchMapper.xml b/ruoyi-rocketmq/src/main/resources/mapper/rocketmq/RmTemplateSwitchMapper.xml index eed73c2..7559c78 100644 --- a/ruoyi-rocketmq/src/main/resources/mapper/rocketmq/RmTemplateSwitchMapper.xml +++ b/ruoyi-rocketmq/src/main/resources/mapper/rocketmq/RmTemplateSwitchMapper.xml @@ -165,4 +165,11 @@ update rm_template_switch set collection_cycle = null where template_id = #{templateId} + + delete from rm_template_switch where policy_id = #{policyId} + + + update rm_template_switch set collection_cycle = null + where policy_id = #{policyId} + \ No newline at end of file