十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。
【转】awk分析nginx日志,获取pv
上一篇 /
下一篇 2011-06-14 13:49:15
/ 个人分类:nginx
最近在深入系统运维的事情,从服务器配置、调优、维护,到各种脚本编写。实现自动化智能运维的目标还要很远。51Testing软件测试网N(z$s*Zu+t
从nginx的日志中分析每日有效的pv和各搜索引擎爬虫的请求数。脚本用awk实现。51Testing软件测试网NZc@W$r(t4Z
函数库文件 stat_func.sh51Testing软件测试网*h\ J:fpr}{6_y/T
R9w4x-x7C9a0- #!/bin/bash
- stat_log_path=/usr/local/qqsa/result
- stat_nginx_log()
- {
- local basename=`basename $1`
- local newfile="${stat_log_path}/${basename%%.*}.pv.`date +'%Y-%m-%d'`"
- awk -F\" '
- {
- if( $2 ~ /css|txt|ico/ ) res["static"]++;
- else if ( $6 ~ /Baiduspider/ ) {res["baidu"]++;res["spider"]++;}
- else if ( $6 ~ /Googlebot/ ) {res["google"]++;res["spider"]++;}
- else if ( $6 ~ /Yahoo! Slurp/) {res["yahoo"]++;res["spider"]++;}
- else if ( $6 ~ /Sogou web spider/) {res["sogou"]++;res["spider"]++;}
- else if ( $6 ~ /Sosospider/) {res["soso"]++;res["spider"]++;}
- else if ( $6 ~ /YodaoBot/) {res["youdao"]++;res["spider"]++;}
- else if ( $6 ~ /msnbot/) {res["msnbot"]++;res["spider"]++;}
- else if ( $6 ~ /iaskspider/) {res["sina"]++;res["spider"]++;}
- else res["good"]++;
- }
- END {
- for(item in res) print item ":" res[item]
- }' $1 > $newfile
- }
D"pE7J-X~t)t051Testing软件测试网:m0aPG8J e
执行文件 stat_every_day.sh51Testing软件测试网?@\
]|/`'e1MIt
"y/`rER,[%W0- #!/bin/bash
- . ./stat_func.sh
- date=`date +'%Y-%m-%d'`
- find /data/cs_log_backup/${date} -name "*.access.*" | \
- while read file
- do
- $( stat_nginx_log "$file" )
- done
51Testing软件测试网2|9~qm&`1pIT:q51Testing软件测试网C1KO5~&XL {&i
crontab -e
H#n@(~&D:Mv0最下面增加一行
*a#fN
n7S8UC6c000 3 * * * /usr/local/maintain/stat_every_day.sh > /dev/null 2 >& 1
I'|%KU0T)@9eWR0参考文献:51Testing软件测试网%zp#m
ZS Vs4Lt
http://tech.foolpig.com/2008/07/09/linux-shell-char/ shell字符串的截取51Testing软件测试网ed-Z\ee v5g^ QO+u
http://storysky.blog.51cto.com/628458/270671 用AWK来过滤nginx日志中的特定值
y_Dj(k2yw_7l0http://storysky.blog.51cto.com/628458/271560 用SED+AWK来分析NGINX日志51Testing软件测试网n5jo+\!J,T
http://book.douban.com/subject/1236944/ sed与awk
0wlE(M
k2?-n4f Fa0
收藏
举报
TAG:
awk
nginx
Nginx