awk分析nginx日志里面的接口响应时间

最近,有客户反应客户端卡,老板集合技术人员开会讨论,找出慢的原因,由此产生了分析nginx响应时间,由于线上环境nginx日志格式带上了引号,处理起来有点麻烦,以下是处理过程

一、nginx日志格式

log_format main ‘$remote_addr – $remote_user [$time_iso8601] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for” ‘
‘ “$upstream_addr” “$upstream_status” “$request_time” ‘;

二、nginx访问日志(/var/log/nginx)

12.124.127.44 – – [29/Jul/2014:20:54:20 +0800] “GET / HTTP/1.1″ 200 211 “-” “Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)” “-” “127.0.0.1:8081″ “200” “0.001”
115.29.113.101 – – [29/Jul/2014:20:54:22 +0800] “GET / HTTP/1.1″ 200 211 “-” “Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)” “-” “127.0.0.1:8081″ “200” “0.005”
112.124.127.53 – – [29/Jul/2014:20:56:49 +0800] “GET / HTTP/1.1″ 200 211 “-” “Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)” “-” “127.0.0.1:8081″ “200” “0.002”
112.124.127.44 – – [29/Jul/2014:20:59:20 +0800] “GET / HTTP/1.1″ 200 211 “-” “Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)” “-” “127.0.0.1:8081″ “200” “0.002”
115.29.113.101 – – [29/Jul/2014:20:59:22 +0800] “GET / HTTP/1.1″ 200 211 “-” “Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)” “-” “127.0.0.1:8081″ “200” “0.002”

三、分析nginx日志

3.1打印响应时间并且去除引号

cat website.access.log| awk ‘{print $(NF)}’ | awk -F “\”” ‘{print $2′}>a.txt

3.2合并文件,将时间重新补上去

paste -d ” ” website.access.log  a.txt > b.txt

3.3查找响应时间大于1秒的url

cat b.txt |awk ‘($NF>1){print $6$7 ” ” $NF}’>c.txt

3.4最后写成一个hsell脚本

vi loganalysis.sh
cat $1 | awk ‘{print $(NF)}’ | awk -F “\”” ‘{print $2′} >a.txt
paste -d ” ” $1 a.txt > b.txt
cat b.txt |awk ‘($NF>1){print $1$4$6$7 ” ” $NF}’ >$2

使用方法
./loganalysis.sh 日志绝对路径 新建日志文件名

结果如下图
QQ图片20141027145344

PS:如果您想和业内技术大牛交流的话,请加qq群(527933790)或者关注微信公众 号(AskHarries),谢谢!

转载请注明原文出处:Harries Blog™ » awk分析nginx日志里面的接口响应时间

赞 (1)

分享到:更多 ()

评论 7

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. 极品飞鸽撸主,友链已添加!回复
  2. 小月博客我的评论一针见血回复