1、优化agent本地存储流量文件删除逻辑。

2、开发tcpdump探测策略下发接口。
3、开发tcpdump探测接口。
4、开发出省流量计算统计接口。
This commit is contained in:
gaoyutao
2026-01-22 18:00:17 +08:00
parent fe0225012f
commit 493ecba0ba
9 changed files with 144 additions and 204 deletions
@@ -44,6 +44,7 @@ import javax.annotation.PostConstruct;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
@@ -126,6 +127,8 @@ public class MessageHandler {
private ProducerMode producerMode;
@Autowired
private RedissonClient redissonClient;
@Autowired
private IRmOutboundTrafficStatisticsService rmOutboundTrafficStatisticsService;
/**
@@ -158,26 +161,116 @@ public class MessageHandler {
private void handleTcpdumpResultMessage(DeviceMessage message) {
List<TcpdumpVo> tcpdumpVoList = JsonDataParser.parseJsonData(message.getData(), TcpdumpVo.class);
if(tcpdumpVoList != null && !tcpdumpVoList.isEmpty()){
for (TcpdumpVo tcpdumpVo : tcpdumpVoList) {
String ip = tcpdumpVo.getIp();
Integer count = tcpdumpVo.getCount();
String type = checkIPVersion(ip);
// 查询IP地址归属地信息
Map<String, String> locationInfo = queryIpLocation(ip);
if (locationInfo != null) {
String operator = locationInfo.get("operator"); // 运营商
String province = locationInfo.get("province"); // 省份
System.out.println("IP: " + ip +
", 运营商: " + operator +
", 省份: " + province +
", 抓包数量: " + count);
// 设置到对象中(如果TcpdumpVo有这些字段)
// tcpdumpVo.setOperator(operator);
// tcpdumpVo.setProvince(province);
String localProvince = "";
// 根据clientId查询省份信息
RmNetworkInterface networkInterfaceQuery = new RmNetworkInterface();
networkInterfaceQuery.setClientId(message.getClientId());
networkInterfaceQuery.setNewFlag(1);
List<RmNetworkInterface> networkInfoList = rmNetworkInterfaceService.selectRmNetworkInterfaceList(networkInterfaceQuery);
if(networkInfoList != null && !networkInfoList.isEmpty()){
for (RmNetworkInterface rmNetworkInterface : networkInfoList) {
if(rmNetworkInterface.getProvince() != null){
localProvince = rmNetworkInterface.getProvince();
break;
}
}
}
// 计算总数量
Double totalCount = 0.0;
for (TcpdumpVo tcpdumpVo : tcpdumpVoList) {
totalCount += tcpdumpVo.getCount();
}
// 分别统计IPv4和IPv6
Map<String, List<TcpdumpVo>> ipTypeMap = new HashMap<>();
ipTypeMap.put("IPv4", new ArrayList<>());
ipTypeMap.put("IPv6", new ArrayList<>());
// 按类型分类
for (TcpdumpVo tcpdumpVo : tcpdumpVoList) {
String ip = tcpdumpVo.getIp();
String type = checkIPVersion(ip);
if ("IPv4".equals(type)) {
ipTypeMap.get("IPv4").add(tcpdumpVo);
} else if ("IPv6".equals(type)) {
ipTypeMap.get("IPv6").add(tcpdumpVo);
}
}
// 构建content内容 - 根据图片格式
StringBuilder content = new StringBuilder();
// 添加时间戳 - 根据图片格式
content.append("时间:").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).append("\n");
// 处理IPv4统计
if (!ipTypeMap.get("IPv4").isEmpty()) {
content.append("##### V4 统计 ##### ##\n");
// 按省份运营商分组统计(过滤本地省份)
Map<String, Double> v4StatMap = new HashMap<>();
for (TcpdumpVo tcpdumpVo : ipTypeMap.get("IPv4")) {
Map<String, String> locationInfo = queryIpLocation(tcpdumpVo.getIp());
if (locationInfo != null) {
String operator = locationInfo.get("operator");
String province = locationInfo.get("province");
// 过滤本地省份和空值
if (province != null && operator != null && !localProvince.equals(province)) {
String key = province + operator; // 根据图片格式,省份和运营商之间没有空格
double currentCount = v4StatMap.getOrDefault(key, 0.0);
v4StatMap.put(key, currentCount + tcpdumpVo.getCount());
}
}
}
// 计算百分比并排序
List<Map.Entry<String, Double>> sortedV4List = new ArrayList<>(v4StatMap.entrySet());
sortedV4List.sort((a, b) -> b.getValue().compareTo(a.getValue())); // 降序排序
for (Map.Entry<String, Double> entry : sortedV4List) {
BigDecimal percentage = new BigDecimal(entry.getValue() / totalCount * 100)
.setScale(2, RoundingMode.HALF_UP);
content.append(entry.getKey()).append(": ").append(percentage).append("%\n");
}
}
// 处理IPv6统计
if (!ipTypeMap.get("IPv6").isEmpty()) {
content.append("##### V6 统计 ##### ##\n");
// 按省份运营商分组统计(过滤本地省份)
Map<String, Double> v6StatMap = new HashMap<>();
for (TcpdumpVo tcpdumpVo : ipTypeMap.get("IPv6")) {
Map<String, String> locationInfo = queryIpLocation(tcpdumpVo.getIp());
if (locationInfo != null) {
String operator = locationInfo.get("operator");
String province = locationInfo.get("province");
// 过滤本地省份和空值
if (province != null && operator != null && !localProvince.equals(province)) {
String key = province + operator; // 根据图片格式,省份和运营商之间没有空格
double currentCount = v6StatMap.getOrDefault(key, 0.0);
v6StatMap.put(key, currentCount + tcpdumpVo.getCount());
}
}
}
// 计算百分比并排序
List<Map.Entry<String, Double>> sortedV6List = new ArrayList<>(v6StatMap.entrySet());
sortedV6List.sort((a, b) -> b.getValue().compareTo(a.getValue())); // 降序排序
for (Map.Entry<String, Double> entry : sortedV6List) {
BigDecimal percentage = new BigDecimal(entry.getValue() / totalCount * 100)
.setScale(2, RoundingMode.HALF_UP);
content.append(entry.getKey()).append(": ").append(percentage).append("%\n");
}
}
RmOutboundTrafficStatistics insertData = new RmOutboundTrafficStatistics();
insertData.setClientId(message.getClientId());
insertData.setDescription(content.toString());
rmOutboundTrafficStatisticsService.insertRmOutboundTrafficStatistics(insertData);
}
}
@@ -608,7 +701,7 @@ public class MessageHandler {
}
}
/**
* 网络流量数据入库
* 网络重试流量数据入库
* @param message
*/
private void processNetRecoverMessageInternal(DeviceMessage message) {
@@ -617,9 +710,10 @@ public class MessageHandler {
String clientId = message.getClientId();
// 时间戳转换
long timestamp = interfaces.get(0).getTimestamp();
boolean lasttrafficFlag = interfaces.get(0).isLastTrafficFlag();
// 时间戳存储到redis
storeTimestampToRedis(clientId, timestamp);
if(message.isLastTrafficFlag()){
if(lasttrafficFlag){
// 把redis中存储的时间戳提取出来,删除redis中的时间戳
processExitsTraffic(clientId);
}