这篇文章上次修改于 766 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

access.log

...
{"@timestamp":"2021-04-06T16:54:05+08:00","host":"127.0.0.1","clientip":"11.11.11.11","size":595,"responsetime":0.210,"upstreamtime":0.212,"upstreamhost":"unix:///tmp/lightpower.sock","http_host":"test.com","url":"/HelloWorld","domain":"test.com","xff":"-","referer":"-","status":"200"}
{"@timestamp":"2021-04-06T16:54:05+08:00","host":"127.0.0.1","clientip":"22.22.22.22","size":410,"responsetime":0.011,"upstreamtime":0.008,"upstreamhost":"unix:///tmp/lightpower.sock","http_host":"test.com","url":"/HelloWorld","domain":"test.com","xff":"-","referer":"-","status":"200"}
...

1. 统计每秒请求数量

从大到小排列,取前 5 个最高。

# -F 输入文件分隔符

$ awk -F ',' '{s[$1]+=1} END {for(i in s) print s[i]" "i}' access.log | sort -nr | head -n 5

175 {"@timestamp":"2021-04-06T12:49:55+08:00"
160 {"@timestamp":"2021-04-06T16:54:15+08:00"
159 {"@timestamp":"2021-04-06T16:54:59+08:00"
159 {"@timestamp":"2021-04-06T16:54:41+08:00"
157 {"@timestamp":"2021-04-06T16:54:19+08:00"

2. 统计某一时间段内 Action 请求数量

查看 15:30 这一分钟内各 Action 的请求数量,取最高的 5 个。

$ cat access.log | grep '15:30:' | awk -F ':' '{s[$14]+=1} END {for(i in s) print s[i]" "i}' | sort -nr | head -n 5

311 "/HelloWorld","domain"
23 "/Test1","domain"
22 "/Test2","domain"
5 "/Test3","domain"
5 "/Test4","domain"

3. 统计某一时间段内 uwsgi 相应时间过长数量

查看 15:30 这一分钟内 uwsgi 响应超过 500ms 的请求数量。

$ cat access.log | grep '15:30:' | awk -F ':' '{if($10 > 0.5) print $0}' | wc -l

20

4. 计算平均请求时间

cat access.log | grep '15:30:' | awk -F ':' '{print $10}' | awk -F ',' '{s += $1} END {print s/NR}'

0.231119