snmp采集工具优化,采集数据入库优化。
This commit is contained in:
+36
@@ -1,5 +1,6 @@
|
||||
package com.ruoyi.rocketmq.controller;
|
||||
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.utils.poi.ExcelUtil;
|
||||
import com.ruoyi.common.core.web.controller.BaseController;
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
@@ -7,6 +8,7 @@ import com.ruoyi.common.core.web.page.PageDomain;
|
||||
import com.ruoyi.common.core.web.page.TableDataInfo;
|
||||
import com.ruoyi.common.log.annotation.Log;
|
||||
import com.ruoyi.common.log.enums.BusinessType;
|
||||
import com.ruoyi.common.security.annotation.InnerAuth;
|
||||
import com.ruoyi.common.security.annotation.RequiresPermissions;
|
||||
import com.ruoyi.rocketmq.domain.RmMonitorPolicy;
|
||||
import com.ruoyi.rocketmq.service.IRmMonitorPolicyService;
|
||||
@@ -152,4 +154,38 @@ public class RmMonitorPolicyController extends BaseController
|
||||
return toAjax(rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* 策略下发,内部调用
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Log(title = "issueSwitchPolicy", businessType = BusinessType.UPDATE)
|
||||
@GetMapping("/innerIssueSwitchPolicy")
|
||||
@InnerAuth
|
||||
public R<String> innerIssueSwitchPolicy(Long id)
|
||||
{
|
||||
rmMonitorPolicyService.issueSwitchPolicy(id);
|
||||
return R.ok("优先级0策略下发成功");
|
||||
}
|
||||
/**
|
||||
* 查询资源监控策略列表
|
||||
*/
|
||||
@PostMapping("/getPolicyMsgInner")
|
||||
@InnerAuth
|
||||
public R<List<RmMonitorPolicy>> getPolicyMsgInner(@RequestBody RmMonitorPolicy rmMonitorPolicy)
|
||||
{
|
||||
List<RmMonitorPolicy> list = rmMonitorPolicyService.selectRmMonitorPolicyList(rmMonitorPolicy);
|
||||
return R.ok(list);
|
||||
}
|
||||
/**
|
||||
* 修改资源监控策略
|
||||
*/
|
||||
@PostMapping("/updatePolicyMsgInner")
|
||||
@InnerAuth
|
||||
public R<Integer> updatePolicyMsgInner(@RequestBody RmMonitorPolicy rmMonitorPolicy)
|
||||
{
|
||||
int rows = rmMonitorPolicyService.updatePolicyMsgInner(rmMonitorPolicy);
|
||||
return R.ok(rows);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ public class CollectDataVo {
|
||||
private String type;
|
||||
/** 数据 */
|
||||
private String value;
|
||||
/** 交换机ip */
|
||||
private String switchIp;
|
||||
/** 时间戳 */
|
||||
private long timestamp;
|
||||
}
|
||||
|
||||
@@ -76,4 +76,11 @@ public interface IRmMonitorPolicyService
|
||||
int issueSwitchPolicy(Long id);
|
||||
|
||||
int updateResourcePolicy(RmMonitorPolicy rmMonitorPolicy);
|
||||
|
||||
/**
|
||||
* 修改资源监控策略内部调用
|
||||
* @param rmMonitorPolicy
|
||||
* @return
|
||||
*/
|
||||
int updatePolicyMsgInner(RmMonitorPolicy rmMonitorPolicy);
|
||||
}
|
||||
|
||||
+5
@@ -353,6 +353,11 @@ public class RmMonitorPolicyServiceImpl implements IRmMonitorPolicyService
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updatePolicyMsgInner(RmMonitorPolicy rmMonitorPolicy) {
|
||||
return rmMonitorPolicyMapper.updateRmMonitorPolicy(rmMonitorPolicy);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id查询监控策略详情
|
||||
* @param id
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.ruoyi.rocketmq.snmp.config;
|
||||
|
||||
import com.ruoyi.rocketmq.domain.RmMonitorPolicy;
|
||||
import com.ruoyi.rocketmq.service.IRmMonitorPolicyService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SwitchStrategyInitializer {
|
||||
|
||||
@Autowired
|
||||
private IRmMonitorPolicyService rmMonitorPolicyService;
|
||||
|
||||
|
||||
// 服务启动时下发默认策略
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
log.info("开始初始化交换机采集策略...");
|
||||
try {
|
||||
// 下发优先级为0的交换机策略
|
||||
RmMonitorPolicy queryParam = new RmMonitorPolicy();
|
||||
queryParam.setPriority("0");
|
||||
queryParam.setResourceType("switch");
|
||||
List<RmMonitorPolicy> defaultPolicy = rmMonitorPolicyService.selectRmMonitorPolicyList(queryParam);
|
||||
if(!defaultPolicy.isEmpty()){
|
||||
rmMonitorPolicyService.issueSwitchPolicy(defaultPolicy.get(0).getId());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("初始化交换机采集策略失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+86
-2
@@ -28,6 +28,8 @@ import java.util.concurrent.ScheduledFuture;
|
||||
@Slf4j
|
||||
public class MultiSwitchCollectionScheduler {
|
||||
|
||||
private static final String PREFIX = "switch";
|
||||
private static final String SUFFIX = "Collect";
|
||||
private final TaskScheduler taskScheduler;
|
||||
private final DynamicOidCollector dynamicOidCollector;
|
||||
private boolean collectorInitialized = false;
|
||||
@@ -95,8 +97,27 @@ public class MultiSwitchCollectionScheduler {
|
||||
|
||||
// 根据CollectVo配置创建不同的定时任务
|
||||
for (CollectVo collectVo : collectVos) {
|
||||
if (collectVo.isCollect()) { //剔除switchVo中的其他oid
|
||||
ScheduledFuture<?> task = createCollectionTask(switchIp, clientId, switchVo, collectVo);
|
||||
if (collectVo.isCollect()) {
|
||||
ScheduledFuture<?> task;
|
||||
// 剔除switchVo中的其他oid
|
||||
if (switchVo.getOtherOID() != null) {
|
||||
Map<String, String> otherOid = switchOidVo.getOtherOID();
|
||||
String type = collectVo.getType();
|
||||
|
||||
// 创建新的 Map,避免修改原始数据
|
||||
Map<String, String> filteredOid = new HashMap<>(otherOid);
|
||||
filteredOid.entrySet().removeIf(entry ->
|
||||
!entry.getValue().equalsIgnoreCase(extractMiddle(type))
|
||||
);
|
||||
|
||||
// 创建新的 SwitchOidDto,避免修改原始 switchVo
|
||||
SwitchOidDto resultMap = createSwitchMsg(switchVo);
|
||||
resultMap.setOtherOID(filteredOid);
|
||||
|
||||
task = createCollectionTask(switchIp, clientId, resultMap, collectVo);
|
||||
} else {
|
||||
task = createCollectionTask(switchIp, clientId, switchVo, collectVo);
|
||||
}
|
||||
if (task != null) {
|
||||
switchTasks.put(collectVo.getType(), task);
|
||||
}
|
||||
@@ -112,6 +133,69 @@ public class MultiSwitchCollectionScheduler {
|
||||
switchIp, switchTasks.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* 赋值oid
|
||||
* @param switchVo
|
||||
*/
|
||||
public SwitchOidDto createSwitchMsg(SwitchOidDto switchVo){
|
||||
SwitchOidDto resultMap = new SwitchOidDto();
|
||||
resultMap.setIp(switchVo.getIp());
|
||||
resultMap.setCommunity(switchVo.getCommunity());
|
||||
resultMap.setPort(switchVo.getPort());
|
||||
resultMap.setVersion(switchVo.getVersion());
|
||||
resultMap.setTimeout(switchVo.getTimeout());
|
||||
resultMap.setRetries(switchVo.getRetries());
|
||||
|
||||
// SNMP v3 特有参数
|
||||
resultMap.setSecurityName(switchVo.getSecurityName());
|
||||
resultMap.setAuthProtocol(switchVo.getAuthProtocol());
|
||||
resultMap.setAuthPassword(switchVo.getAuthPassword());
|
||||
resultMap.setPrivProtocol(switchVo.getPrivProtocol());
|
||||
resultMap.setPrivPassword(switchVo.getPrivPassword());
|
||||
resultMap.setSecurityLevel(switchVo.getSecurityLevel());
|
||||
|
||||
// OID配置(复制所有OID Map)
|
||||
resultMap.setNetOID(new LinkedHashMap<>(switchVo.getNetOID()));
|
||||
resultMap.setEntityOID(new LinkedHashMap<>(switchVo.getEntityOID()));
|
||||
resultMap.setPwrOID(new LinkedHashMap<>(switchVo.getPwrOID()));
|
||||
resultMap.setFanOID(new LinkedHashMap<>(switchVo.getFanOID()));
|
||||
resultMap.setMpuOID(new LinkedHashMap<>(switchVo.getMpuOID()));
|
||||
resultMap.setModuleOID(new LinkedHashMap<>(switchVo.getModuleOID()));
|
||||
resultMap.setPowerOID(new LinkedHashMap<>(switchVo.getPowerOID()));
|
||||
|
||||
// 过滤条件
|
||||
resultMap.setFilters(new HashMap<>(switchVo.getFilters()));
|
||||
return resultMap;
|
||||
}
|
||||
/**
|
||||
* 提取字符串的中间部分(去除固定的前缀和后缀)
|
||||
* @param type 输入字符串(如 "switchAssCollect")
|
||||
* @return 中间部分(如 "Ass")
|
||||
*/
|
||||
public String extractMiddle(String type) {
|
||||
if (type == null || type.isEmpty()) {
|
||||
return type;
|
||||
}
|
||||
|
||||
// 检查是否以 "switch" 开头
|
||||
if (!type.startsWith(PREFIX)) {
|
||||
throw new IllegalArgumentException("Type must start with 'switch'");
|
||||
}
|
||||
|
||||
// 检查是否以 "Collect" 结尾
|
||||
if (!type.endsWith(SUFFIX)) {
|
||||
throw new IllegalArgumentException("Type must end with 'Collect'");
|
||||
}
|
||||
|
||||
// 计算中间部分的起始和结束位置
|
||||
int prefixLength = PREFIX.length();
|
||||
int suffixLength = SUFFIX.length();
|
||||
int middleStart = prefixLength;
|
||||
int middleEnd = type.length() - suffixLength;
|
||||
|
||||
// 提取中间部分
|
||||
return type.substring(middleStart, middleEnd);
|
||||
}
|
||||
/**
|
||||
* 启动SNMP v3交换机采集任务
|
||||
*/
|
||||
|
||||
+6
-19
@@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.common.core.constant.SecurityConstants;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.utils.DateUtils;
|
||||
import com.ruoyi.rocketmq.domain.*;
|
||||
import com.ruoyi.rocketmq.domain.vo.CollectDataVo;
|
||||
@@ -13,7 +12,6 @@ import com.ruoyi.rocketmq.snmp.dto.CollectionResult;
|
||||
import com.ruoyi.rocketmq.utils.SwitchJsonDataParser;
|
||||
import com.ruoyi.system.api.RemoteRevenueConfigService;
|
||||
import com.ruoyi.system.api.domain.InitialSwitchInfoDetailsRemote;
|
||||
import com.ruoyi.system.api.domain.RmResourceRegistrationRemote;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -58,10 +56,11 @@ public class ProcessSwitchCollectDataService {
|
||||
List<Map<String, String>> resultData = result.getData();
|
||||
CollectDataVo switchDataVo = new CollectDataVo();
|
||||
switchDataVo.setTimestamp(result.getTimestamp());
|
||||
switchDataVo.setSwitchIp(result.getSwitchIp());
|
||||
switchDataVo.setValue(JSONObject.toJSONString(resultData));
|
||||
switch(result.getDeviceType()){
|
||||
case "net":
|
||||
// handleSwitchNetMessage(switchDataVo, clientId);
|
||||
handleSwitchNetMessage(switchDataVo, clientId);
|
||||
break;
|
||||
case "pwr":
|
||||
handleSwitchPwrMessage(switchDataVo, clientId);
|
||||
@@ -119,7 +118,7 @@ public class ProcessSwitchCollectDataService {
|
||||
insertData.setCollectType(fieldName);
|
||||
if (!"null".equals(fieldValue)) {
|
||||
insertData.setCollectValue(fieldValue);
|
||||
// insertInitialSwitchOtherInfo.insertInitialSwitchOtherCollectData(insertData);
|
||||
insertInitialSwitchOtherInfo.insertInitialSwitchOtherCollectData(insertData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,8 +133,7 @@ public class ProcessSwitchCollectDataService {
|
||||
insertData.setCollectType(fieldName);
|
||||
if (!"null".equals(fieldValue)) {
|
||||
insertData.setCollectValue(fieldValue);
|
||||
System.out.println("1");
|
||||
// insertInitialSwitchOtherInfo.insertInitialSwitchOtherCollectData(insertData);
|
||||
insertInitialSwitchOtherInfo.insertInitialSwitchOtherCollectData(insertData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,13 +152,6 @@ public class ProcessSwitchCollectDataService {
|
||||
private void handleSwitchNetMessage(CollectDataVo switchDataVo, String clientId) {
|
||||
List<InitialSwitchInfo> switchInfos = SwitchJsonDataParser.parseJsonData(switchDataVo.getValue(), InitialSwitchInfo.class);
|
||||
if(!switchInfos.isEmpty()){
|
||||
// 根据clientId查询交换机ip
|
||||
RmResourceRegistrationRemote queryParam = new RmResourceRegistrationRemote();
|
||||
queryParam.setHardwareSn(clientId);
|
||||
R<RmResourceRegistrationRemote> registMsgR = remoteRevenueConfigService.getListByHardwareSn(queryParam, SecurityConstants.INNER);
|
||||
if(registMsgR != null){
|
||||
RmResourceRegistrationRemote registMsg = registMsgR.getData();
|
||||
}
|
||||
// 时间戳转换
|
||||
long timestamp = switchDataVo.getTimestamp();
|
||||
long millis = timestamp * 1000;
|
||||
@@ -186,9 +177,7 @@ public class ProcessSwitchCollectDataService {
|
||||
switchInfos.forEach(switchInfo -> {
|
||||
switchInfo.setClientId(clientId);
|
||||
switchInfo.setCreateTime(createTime);
|
||||
if(registMsgR != null && registMsgR.getData()!=null && registMsgR.getData().getSnmpCollectAddr()!=null){
|
||||
switchInfo.setSwitchIp(registMsgR.getData().getSnmpCollectAddr());
|
||||
}
|
||||
switchInfo.setSwitchIp(switchDataVo.getSwitchIp());
|
||||
InitialSwitchInfoTemp tempInfo = tempMap.get(switchInfo.getName());
|
||||
if (tempInfo != null) {
|
||||
// 计算inSpeed
|
||||
@@ -212,9 +201,7 @@ public class ProcessSwitchCollectDataService {
|
||||
switchInfos.forEach(switchInfo -> {
|
||||
switchInfo.setClientId(clientId);
|
||||
switchInfo.setCreateTime(createTime);
|
||||
if(registMsgR != null && registMsgR.getData()!=null && registMsgR.getData().getSnmpCollectAddr()!=null){
|
||||
switchInfo.setSwitchIp(registMsgR.getData().getSnmpCollectAddr());
|
||||
}
|
||||
switchInfo.setSwitchIp(switchDataVo.getSwitchIp());
|
||||
});
|
||||
}
|
||||
// 清空临时表对应switch信息
|
||||
|
||||
Reference in New Issue
Block a user