按IP统计近7日每日事件数,计算3/7日日均并定位活动激增异常
修正后的Splunk查询方案
要解决无事件日期缺失导致的平均值偏差问题,核心是先补全所有IP对应的每日记录(无事件时count为0),再计算真实平均值。以下提供两种可行的修正方案:
方案一:笛卡尔积补全法
// 1. 统计近7天内每个IP每日的事件数 index=blah_blah earliest=-7d | bucket _time span=1d | stats count as daily_events by ip _time | eval date=strftime(_time, "%Y-%m-%d") // 2. 生成近7天的完整日期列表 | append [ | makeresults count=7 | eval day_offset=-(6 - (row-1)) | eval _time=relative_time(now(), day_offset."d@d") | eval date=strftime(_time, "%Y-%m-%d") | fields date ] // 3. 提取近7天内出现过的所有IP | append [ index=blah_blah earliest=-7d | stats dc(ip) as dummy by ip | fields ip ] // 4. 生成IP与日期的全组合,补全无事件日期的0值 | stats values(daily_events) as daily_events by ip date | eval daily_events=coalesce(daily_events, 0) // 5. 计算近7日、近3日的总事件数与平均值 | eval _time=strptime(date, "%Y-%m-%d") | sort ip _time | streamstats sum(daily_events) as total by ip | eval last7d_total=total - coalesce(prev(total,7), 0) | eval last3d_total=total - coalesce(prev(total,3), 0) | eval avg_7d=last7d_total/7 | eval avg_3d=last3d_total/3 // 6. 筛选符合条件的IP并去重 | where avg_3d >= avg_7d * 2.5 | dedup ip | table ip avg_7d avg_3d
方案二:Timechart简化法
利用timechart自动生成IP-日期全组合,再用fillnull填充0值,更简洁高效:
index=blah_blah earliest=-7d | bucket _time span=1d | timechart span=1d count by ip | fillnull value=0 // 补全无事件日期的0值 | untable _time ip daily_events // 将宽表转为窄表格式 | sort ip _time | streamstats sum(daily_events) as total by ip | eval last7d_total=total - coalesce(prev(total,7), 0) | eval last3d_total=total - coalesce(prev(total,3), 0) | eval avg_7d=last7d_total/7 | eval avg_3d=last3d_total/3 | where _time>=relative_time(now(), "-0d@d") // 取最新的计算结果 | where avg_3d >= avg_7d * 2.5 | table ip avg_7d avg_3d
关键说明
- 补全逻辑:两种方案都确保每个IP在近7天的每一天都有记录,无事件时用
0填充,避免trendline因数据缺失导致的错误插值。 - 平均值计算:通过累计事件数的差值计算近7天/近3天的总事件数,再除以天数得到真实平均(包含0值的情况)。
- 筛选条件:
avg_3d >= avg_7d * 2.5对应“近3日每日平均比近7日至少高出150%”(即3日平均是7日平均的2.5倍及以上)。
内容的提问来源于stack exchange,提问作者Off Grid
相关产品推荐
相关产品推荐

