跳转到内容
搜索文档

解析 Cloudflare Logs JSON 数据

最后更新 查看 MarkdownAgent 设置

下载 Cloudflare Logs 数据后,你可以使用不同工具解析和分析日志。

用于解析 JSON 日志数据的工具之一是 jq

有关获取和安装 jq 的更多信息,请参阅 Download jq

聚合字段

要对日志中出现的字段进行聚合(例如按 IP 地址、URI 或 referrer),可以使用多个 jq 命令。这有助于识别流量模式;例如,识别最受欢迎的页面或阻止攻击。

以下示例匹配字段名,并提供每个字段实例的计数,按计数升序排序。

jq -r .ClientRequestURI logs.json | sort -n | uniq -c | sort -n | tail
2 /nginx-logo.png
2 /poweredby.png
2 /testagain
3 /favicon.ico
3 /testing
3 /testing123
6 /test
7 /testing1234
10 /cdn-cgi/nexp/dok3v=1613a3a185/cloudflare/rocket.js
54 /
jq -r .ClientRequestUserAgent logs.json | sort -n | uniq -c | sort -n | tail
1 python-requests/2.9.1
2 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17
4 Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
5 curl/7.47.2-DEV
36 Mozilla/5.0 (X11; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0
51 curl/7.46.0-DEV
jq -r .ClientRequestReferer logs.json | sort -n | uniq -c | sort -n | tail
2 http://example.com/testagain
3 http://example.com/testing
5 http://example.com/
5 http://example.com/testing123
7 http://example.com/testing1234
77 null

过滤字段

另一个常见用例是按特定字段值过滤数据,然后再进行聚合。这有助于回答诸如 哪些 URL 出现了最多的 502 错误? 等问题。例如:

jq 'select(.OriginResponseStatus == 502) | .ClientRequestURI' logs.json | sort -n | uniq -c | sort -n | tail
1 "/favicon.ico"
1 "/testing"
3 "/testing123"
6 "/test"
6 "/testing1234"
18 "/"

要查找被 Cloudflare WAF 阻止的热门 IP 地址,请使用以下查询:

jq -r 'select(.SecurityAction == "block") | .ClientIP' logs.json | sort -n | uniq -c | sort -n
1 127.0.0.1

显示缓存请求

要检索缓存比率,可尝试以下查询:

jq -r '.CacheCacheStatus' logs.json | sort -n | uniq -c | sort -n
3 hit
3 null
3 stale
4 expired
6 miss
81 unknown

显示 TLS 版本

要了解访客使用的 TLS 版本——例如,以决定是否可以禁用 1.2 之前的 TLS 版本——请使用以下查询:

jq -r '.ClientSSLProtocol' logs.json | sort -n | uniq -c | sort -n
42 none
58 TLSv1.2

这篇文档对您有帮助吗?