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

【转】awk分析nginx日志,获取pv

上一篇 / 下一篇  2011-06-14 13:49:15 / 个人分类:nginx

最近在深入系统运维的事情,从服务器配置、调优、维护,到各种脚本编写。实现自动化智能运维的目标还要很远。

R6y W|r| lC#sGPN0

nginx日志中分析每日有效的pv和各搜索引擎爬虫的请求数。脚本用awk实现。51Testing软件测试网2jA ~CRMb;g

函数库文件 stat_func.sh51Testing软件测试网l7lC.n^1XP

'x2^5u)fS1L n/X0
  1. #!/bin/bash  
  2. stat_log_path=/usr/local/qqsa/result  
  3. stat_nginx_log()  
  4. {  
  5.     local basename=`basename $1`  
  6.     local newfile="${stat_log_path}/${basename%%.*}.pv.`date +'%Y-%m-%d'`"  
  7.     awk -F\" '  
  8.     {  
  9.     if( $2 ~ /css|txt|ico/ ) res["static"]++;  
  10.     else if ( $6 ~ /Baiduspider/ ) {res["baidu"]++;res["spider"]++;}  
  11.     else if ( $6 ~ /Googlebot/ ) {res["google"]++;res["spider"]++;}  
  12.     else if ( $6 ~ /Yahoo! Slurp/) {res["yahoo"]++;res["spider"]++;}  
  13.     else if ( $6 ~ /Sogou web spider/) {res["sogou"]++;res["spider"]++;}  
  14.     else if ( $6 ~ /Sosospider/) {res["soso"]++;res["spider"]++;}  
  15.     else if ( $6 ~ /YodaoBot/) {res["youdao"]++;res["spider"]++;}  
  16.     else if ( $6 ~ /msnbot/) {res["msnbot"]++;res["spider"]++;}  
  17.     else if ( $6 ~ /iaskspider/) {res["sina"]++;res["spider"]++;}  
  18.     else res["good"]++;  
  19.     }  
  20.     END {  
  21.     for(item in res) print item ":" res[item]   
  22.     }' $1 > $newfile  
  23. }  
 51Testing软件测试网t RIWN~ F

9~r{]n0

执行文件 stat_every_day.sh

N$fl6Hl0

~3|H:i$Tx)_ i0
  1. #!/bin/bash  
  2. . ./stat_func.sh  
  3. date=`date +'%Y-%m-%d'`  
  4. find /data/cs_log_backup/${date} -name "*.access.*" | \  
  5. while read file  
  6. do  
  7.     $( stat_nginx_log "$file" )  
  8. done  
 51Testing软件测试网 z|3f*[Kw.D1V V

51Testing软件测试网 _ ylb%UV*~V

crontab -e51Testing软件测试网;Z5N+a3Q#C!RP vl{

最下面增加一行51Testing软件测试网7@y;ZO5D

00 3 * * * /usr/local/maintain/stat_every_day.sh > /dev/null 2 >& 1

1J `btu0

参考文献:51Testing软件测试网5Do1`;H+M:i+jJS1`S

http://tech.foolpig.com/2008/07/09/linux-shell-char/    shell字符串的截取51Testing软件测试网)Zw5[%L^Z

http://storysky.blog.51cto.com/628458/270671  用AWK来过滤nginx日志中的特定值51Testing软件测试网ebN5j2fO%i$On;X!n

http://storysky.blog.51cto.com/628458/271560  用SED+AWK来分析NGINX日志

9?Ex+u/N\S0

http://book.douban.com/subject/1236944/   sed与awk  

R3O7BV)S0

TAG: awk nginx Nginx

 

评分:0

我来说两句

Open Toolbar