十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。

awk分析nginx日志,获取pv

上一篇 / 下一篇  2011-06-14 13:54:41 / 个人分类:测试经验

查看( 1265 ) / 评论( 0 )
最近在深入系统运维的事情,从服务器配置、调优、维护,到各种脚本编写。实现自动化智能运维的目标还要很远。51Testing软件测试网"GQvo P y4\R

.z e6X8g2S G-dG0 从nginx的日志中分析每日有效的pv和各搜索引擎爬虫的请求数。脚本用awk实现。
&Z,b g#o ["K xl"Z@0
#W.FF_!j0 函数库文件 stat_func.sh51Testing软件测试网;t*vrcU oi#cu O3I

Im.Ji q0 51Testing软件测试网o0h,^,j6_p
view plaincopy to clipboardprint?
W"X2}f1j;?Gl,Pp0 #!/bin/bash   
+H1@Pr`,dl0 stat_log_path=/usr/local/qqsa/result   
GI.Qv!k:`"c.h0 stat_nginx_log()   51Testing软件测试网n1[dLtI
{   51Testing软件测试网;}h!S @Pd6c1N)B
    local basename=`basename $1`   
g&P!h H#`l O._*gi0     local newfile="${stat_log_path}/${basename%%.*}.pv.`date +'%Y-%m-%d'`"   
oPt4Ew0     awk -F\" '   51Testing软件测试网`S6CTQ5j gD
    {   
GeZ/ZSJ~S0     if( $2 ~ /css|txt|ico/ ) res["static"]++;   
#l#G)H$D%u0     else if ( $6 ~ /Baiduspider/ ) {res["baidu"]++;res["spider"]++;}   
Ebip2X6Q0     else if ( $6 ~ /Googlebot/ ) {res["google"]++;res["spider"]++;}   
"j5_@5YzOi|#m0     else if ( $6 ~ /Yahoo! Slurp/) {res["yahoo"]++;res["spider"]++;}   
b$e,S_u E0     else if ( $6 ~ /Sogou web spider/) {res["sogou"]++;res["spider"]++;}   51Testing软件测试网u E%[;L%QRh!k
    else if ( $6 ~ /Sosospider/) {res["soso"]++;res["spider"]++;}   
\)B$X^+f"Bx0     else if ( $6 ~ /YodaoBot/) {res["youdao"]++;res["spider"]++;}   
Iv x c2F0     else if ( $6 ~ /msnbot/) {res["msnbot"]++;res["spider"]++;}   51Testing软件测试网8]J `fo j)S8T
    else if ( $6 ~ /iaskspider/) {res["sina"]++;res["spider"]++;}   
't&v H-X5EYnG7z j0     else res["good"]++;   
4B5Rs'E1]+W1G8j'M0     }   
#gVQ*CG0D9a0N7X0     END {   51Testing软件测试网!}"@ LD+v`JQ
    for(item in res) print item ":" res[item]    51Testing软件测试网5a w,c]xI!Z b
    }' $1 > $newfile   51Testing软件测试网zEeTY
}  
Cb2KMV/gl0QMjj0   
,^0b X#J`]Y8G9te0 51Testing软件测试网2qM'Q#d;e#F2J$^
执行文件 stat_every_day.sh
.B~+x!v}I3\0 51Testing软件测试网Sq/|7E'fa0W
51Testing软件测试网(@sTw"raZ
view plaincopy to clipboardprint?51Testing软件测试网&D%Fe8PjRH!p
#!/bin/bash   
} ]E5I@9@.w!R1M0 . ./stat_func.sh   
s }+v9E-M C$n0 date=`date +'%Y-%m-%d'`   51Testing软件测试网Tu.\ s|1F8^
find /data/cs_log_backup/${date} -name "*.access.*" | \   51Testing软件测试网8WyeT%][/V]$]jW v
while read file   
Rr4].G ^0 do   
9Sw n/kUuf L0     $( stat_nginx_log "$file" )   
M J(?'CD0 done  51Testing软件测试网O yynx^.@F)S&b
  51Testing软件测试网cZ}7_%O

1v Baa0gK#m lo0 crontab -e
$r.?!\D}/cq0
5D Bs|!v0c/l ~0 最下面增加一行
0?LI#bKxX0
Z;N,e:G.[0 00 3 * * * /usr/local/maintain/stat_every_day.sh > /dev/null 2 >& 1
J'fi.A9QE0 51Testing软件测试网.p g!Wr)~3W,_:x5A U.R
参考文献:51Testing软件测试网J9T!C4]l,t-L

.gnW(mHl/{0 http://tech.foolpig.com/2008/07/09/linux-shell-char/    shell字符串的截取51Testing软件测试网Ji|e_1r/Y-qY

gzg~7OA&B0 http://storysky.blog.51cto.com/628458/270671  用AWK来过滤nginx日志中的特定值51Testing软件测试网@aGe3Y&A
51Testing软件测试网%v+LJ8lq3o En
http://storysky.blog.51cto.com/628458/271560  用SED+AWK来分析NGINX日志51Testing软件测试网gK.S?$e

"Lw/z;_~"n"\8B6FN0 http://book.douban.com/subject/1236944/   sed与awk

TAG:

我来说两句

(可选)

Open Toolbar