增加ping丢包率告警

This commit is contained in:
gaoyutao
2026-03-11 17:55:42 +08:00
parent cffcb2efd3
commit 03a77cc741
3 changed files with 144 additions and 8 deletions
@@ -8,7 +8,8 @@ public enum AlarmTypeEnum {
交换机下线("2", "交换机下线"),
CPU使用率高("4", "CPU使用率高"),
磁盘缺失("5", "磁盘缺失"),
出省流量占比过高("6", "出省流量占比过高");
出省流量占比过高("6", "出省流量占比过高"),
ping丢包率过高("7", "ping丢包率过高");
private final String code;
private final String msg;
AlarmTypeEnum(String code, String msg){
@@ -5,7 +5,8 @@ import lombok.Getter;
@Getter
public enum ConditionItemEnum {
服务器CPU使用率("1", "服务器CPU使用率"),
出省流量占比过高("2", "出省流量占比过高");
出省流量占比过高("2", "出省流量占比过高"),
ping丢包率过高("3", "ping丢包率过高");
private final String code;
private final String msg;
ConditionItemEnum(String code, String msg){
@@ -2,9 +2,13 @@ package com.tongran.rocketmq.service.impl;
import com.tongran.common.core.utils.*;
import com.tongran.rocketmq.domain.*;
import com.tongran.rocketmq.enums.AlarmTypeEnum;
import com.tongran.rocketmq.enums.ConditionItemEnum;
import com.tongran.rocketmq.mapper.*;
import com.tongran.rocketmq.service.IInitialBandwidthTrafficService;
import com.tongran.rocketmq.service.IInitialDiskInfoService;
import com.tongran.rocketmq.service.IRmNetworkInterfaceService;
import com.tongran.rocketmq.utils.SendAlarmPushUtil;
import com.tongran.rocketmq.utils.TableRouterUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
@@ -40,6 +44,14 @@ public class InitialBandwidthTrafficServiceImpl implements IInitialBandwidthTraf
private InitialCpuInfoMapper initialCpuInfoMapper;
@Autowired
private IInitialDiskInfoService diskInfoService;
@Autowired
private RmAlarmThresholdMapper rmAlarmThresholdMapper;
@Autowired
private RmAlarmLogMapper rmAlarmLogMapper;
@Autowired
private SendAlarmPushUtil sendAlarmPushUtil;
@Autowired
private IRmNetworkInterfaceService rmNetworkInterfaceService;
/**
* 查询初始带宽流量
@@ -147,12 +159,107 @@ public class InitialBandwidthTrafficServiceImpl implements IInitialBandwidthTraf
if (dataList.isEmpty()){
return;
}
// 按表名分组批量插入
// 创建dataList的副本,避免外部修改影响
List<InitialBandwidthTraffic> safeDataList = new ArrayList<>(dataList);
// 查询业务网卡
String mac = "";
boolean hasSubInterface = false;
Map<String,Boolean> childMap = new HashMap();
String clientId = safeDataList.get(0).getClientId();
RmNetworkInterface rmNetworkInterface = new RmNetworkInterface();
rmNetworkInterface.setClientIds(clientId);
List<RmNetworkInterface> networkInterfacesList = rmNetworkInterfaceService.selectRmNetworkInterfaceList(rmNetworkInterface);
if(networkInterfacesList != null && !networkInterfacesList.isEmpty()){
RmNetworkInterface networkInterface = networkInterfacesList.get(0);
mac = networkInterface.getMacAddress();
String name = networkInterface.getInterfaceName();
RmNetworkInterfaceChild query = new RmNetworkInterfaceChild();
query.setClientId(clientId);
query.setParentInterface(name);
List<RmNetworkInterfaceChild> children = rmNetworkInterfaceChildMapper.selectRmNetworkInterfaceChildList(query);
if(children != null && !children.isEmpty()) {
hasSubInterface = true;
for (RmNetworkInterfaceChild child : children) {
childMap.put(child.getMacAddress(),true);
}
}
}
BigDecimal pingDropped = calculatePingDropped(safeDataList, mac, hasSubInterface, childMap);
if(hasSubInterface){
// 有子网卡时,累加所有子网卡的丢包率
for (InitialBandwidthTraffic bandwidthTraffic : dataList) {
// 使用containsKey检查MAC是否在子网卡列表中
if(childMap.containsKey(bandwidthTraffic.getMac())){
// 添加空值判断
Double pingValue = bandwidthTraffic.getPingDropped();
if(pingValue != null) {
pingDropped = pingDropped.add(BigDecimal.valueOf(pingValue));
}
}
}
}else{
// 没有子网卡时,只取主网卡的丢包率
for (InitialBandwidthTraffic bandwidthTraffic : dataList) {
if(mac.equals(bandwidthTraffic.getMac())){
// 添加空值判断
Double pingValue = bandwidthTraffic.getPingDropped();
if(pingValue != null) {
pingDropped = BigDecimal.valueOf(pingValue);
} else {
pingDropped = BigDecimal.ZERO; // 或者可以保持为null,根据业务需求决定
}
break;
}
}
}
// 查询告警阈值
RmAlarmThreshold thresholdQuery = new RmAlarmThreshold();
thresholdQuery.setAlarmType(AlarmTypeEnum.ping丢包率过高.getCode());
thresholdQuery.setConditionItem(ConditionItemEnum.ping丢包率过高.getCode());
List<RmAlarmThreshold> rmAlarmThresholdList = rmAlarmThresholdMapper.selectRmAlarmThresholdList(thresholdQuery);
if(rmAlarmThresholdList != null && !rmAlarmThresholdList.isEmpty()) {
String operator = rmAlarmThresholdList.get(0).getCompareOperator();
BigDecimal threshold = rmAlarmThresholdList.get(0).getThresholdValue();
if(operator != null) {
// 根据运算符1大于,2小于,3等于 判断是否进行告警
boolean shouldAlarm = false;
// 根据运算符判断是否进行告警
switch (operator) {
case "1": // 大于
shouldAlarm = pingDropped.compareTo(threshold) > 0;
break;
case "2": // 小于
shouldAlarm = pingDropped.compareTo(threshold) < 0;
break;
case "3": // 等于
shouldAlarm = pingDropped.compareTo(threshold) == 0;
break;
default:
log.warn("未知的运算符: {}", operator);
}
if (shouldAlarm) {
RmAlarmLog rmAlarmLog = new RmAlarmLog();
rmAlarmLog.setAlarmType(AlarmTypeEnum.ping丢包率过高.getCode());
rmAlarmLog.setClientId(clientId);
rmAlarmLog.setAlarmTime(DateUtils.getNowDate());
rmAlarmLog.setAlarmContent("丢包率:" + pingDropped);
rmAlarmLogMapper.insertRmAlarmLog(rmAlarmLog);
// 推送消息
sendAlarmPushUtil.sendAlarmPush(rmAlarmLog, AlarmTypeEnum.ping丢包率过高.getMsg());
}
}
}
// 批量插入
batchInsertData(initialBandwidthTraffic, safeDataList);
}
private void batchInsertData(InitialBandwidthTraffic original, List<InitialBandwidthTraffic> dataList) {
Map<String, List<InitialBandwidthTraffic>> groupedData = dataList.stream()
.map(data -> {
try {
InitialBandwidthTraffic processed = new InitialBandwidthTraffic();
BeanUtils.copyProperties(data,processed);
BeanUtils.copyProperties(data, processed);
if (data.getCreateTime() == null) {
data.setCreateTime(DateUtils.getNowDate());
}
@@ -162,18 +269,20 @@ public class InitialBandwidthTrafficServiceImpl implements IInitialBandwidthTraf
processed.setTableName(TableRouterUtil.getTableName(createTime));
return processed;
} catch (Exception e){
log.error("数据处理失败",e.getMessage());
log.error("数据处理失败", e.getMessage());
return null;
}
}).collect(Collectors.groupingBy(
})
.filter(Objects::nonNull)
.collect(Collectors.groupingBy(
InitialBandwidthTraffic::getTableName,
LinkedHashMap::new, // 保持插入顺序
LinkedHashMap::new,
Collectors.toList()));
groupedData.forEach((tableName, list) -> {
try {
InitialBandwidthTraffic data = new InitialBandwidthTraffic();
BeanUtils.copyProperties(initialBandwidthTraffic,data);
BeanUtils.copyProperties(original, data);
data.setTableName(tableName);
data.setList(list);
initialBandwidthTrafficMapper.batchInsert(data);
@@ -233,6 +342,31 @@ public class InitialBandwidthTrafficServiceImpl implements IInitialBandwidthTraf
}
});
}
private BigDecimal calculatePingDropped(List<InitialBandwidthTraffic> dataList,
String mac,
boolean hasSubInterface,
Map<String, Boolean> childMap) {
BigDecimal result = BigDecimal.ZERO;
if(hasSubInterface){
for (InitialBandwidthTraffic bandwidthTraffic : dataList) {
if(childMap.containsKey(bandwidthTraffic.getMac())){
Double pingValue = bandwidthTraffic.getPingDropped();
if(pingValue != null) {
result = result.add(BigDecimal.valueOf(pingValue));
}
}
}
}else{
for (InitialBandwidthTraffic bandwidthTraffic : dataList) {
if(mac.equals(bandwidthTraffic.getMac())){
Double pingValue = bandwidthTraffic.getPingDropped();
result = pingValue != null ? BigDecimal.valueOf(pingValue) : BigDecimal.ZERO;
break;
}
}
}
return result;
}
/**
* 网络接口基础信息
* @param initialBandwidthTraffic