序言:企业网络工程师/架构师(非程序员)。
概要:日志从 txt 输出移动到 json
问题:没有成功尝试从正常工作的 awk 数据提取迁移到使用 jq 对 json 使用单线。
活跃的awk:
awk '
BEGIN{ FS="\t" }
{ arr[$1 FS $2] += $3; count[$1 FS $2] += 1 }
END{ for (key in arr) printf "%s%s%s%s%s\n", key, FS, count[key], FS, arr[key] }
' | sort -nrk 4 | head -1 | awk '{ print $1" | "$2" | "$4/60/60 }'
最终结果:使用 jq 计算 src/dst ip 地址和 dst 端口的重复条目并添加连接的累积持续时间。
示例 JSON 输入
{
"ts": 1636xxxxx.41xxx34,
"uid": "hex_code",
"id.orig_h": "10.x.x.11",
"id.orig_p": 42996,
"id.resp_h": "10.x.x.123",
"id.resp_p": 53,
"proto": "udp",
"service": "dns",
"duration": 0.01117664844,
"conn_state": "SF",
"local_orig": true,
"local_resp": true,
"missed_bytes": 0,
"history": "Dd",
"orig_pkts": 1,
"orig_ip_bytes": 71,
"resp_pkts": 1,
"resp_ip_bytes": 71
}
{
"ts": 1xxxx0501.5xxx47,
"uid": "hex_code",
"id.orig_h": "10.x.x.11",
"id.orig_p": 36299,
"id.resp_h": "10.x.x.123",
"id.resp_p": 53,
"proto": "udp",
"service": "dns",
"duration": 0.00857415966797,
"conn_state": "SF",
"local_orig": true,
"local_resp": true,
"missed_bytes": 0,
"history": "Dd",
"orig_pkts": 1,
"orig_ip_bytes": 74,
"resp_pkts": 1,
"resp_ip_bytes": 74
}
targeted jq output...
10.xxx.xxx.21 | 18.xx1.xx1.158 | 45.6606 <--time is shown cumulative duration