awk分析nginx日志,获取pv

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

从nginx的日志中分析每日有效的pv和各搜索引擎爬虫的请求数。脚本用awk实现。

函数库文件 stat_func.sh

[c-sharp] view plain copy print ?
  1. #!/bin/bash
  2. stat_log_path=/usr/local/qqsa/result
  3. stat_nginx_log()
  4. {
  5. localbasename=`basename$1`
  6. localnewfile="${stat_log_path}/${basename%%.*}.pv.`date+'%Y-%m-%d'`"
  7. awk-F/"'
  8. {
  9. if($2~/css|txt|ico/)res["static"]++;
  10. elseif($6~/Baiduspider/){res["baidu"]++;res["spider"]++;}
  11. elseif($6~/Googlebot/){res["google"]++;res["spider"]++;}
  12. elseif($6~/Yahoo!Slurp/){res["yahoo"]++;res["spider"]++;}
  13. elseif($6~/Sogouwebspider/){res["sogou"]++;res["spider"]++;}
  14. elseif($6~/Sosospider/){res["soso"]++;res["spider"]++;}
  15. elseif($6~/YodaoBot/){res["youdao"]++;res["spider"]++;}
  16. elseif($6~/msnbot/){res["msnbot"]++;res["spider"]++;}
  17. elseif($6~/iaskspider/){res["sina"]++;res["spider"]++;}
  18. elseres["good"]++;
  19. }
  20. END{
  21. for(iteminres)printitem":"res[item]
  22. }'$1>$newfile
  23. }
#!/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 }

执行文件 stat_every_day.sh

[c-sharp] view plain copy print ?
  1. #!/bin/bash
  2. ../stat_func.sh
  3. date=`date+'%Y-%m-%d'`
  4. find/data/cs_log_backup/${date}-name"*.access.*"|/
  5. whilereadfile
  6. do
  7. $(stat_nginx_log"$file")
  8. done
#!/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

crontab -e

最下面增加一行

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

参考文献:

http://tech.foolpig.com/2008/07/09/linux-shell-char/ shell字符串的截取

http://storysky.blog.51cto.com/628458/270671用AWK来过滤nginx日志中的特定值

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

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

转载http://blog.csdn.net/LongMarch12/archive/2011/02/24/6204550.aspx

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

从nginx的日志中分析每日有效的pv和各搜索引擎爬虫的请求数。脚本用awk实现。

函数库文件 stat_func.sh

[c-sharp] view plain copy print ?
  1. #!/bin/bash
  2. stat_log_path=/usr/local/qqsa/result
  3. stat_nginx_log()
  4. {
  5. localbasename=`basename$1`
  6. localnewfile="${stat_log_path}/${basename%%.*}.pv.`date+'%Y-%m-%d'`"
  7. awk-F/"'
  8. {
  9. if($2~/css|txt|ico/)res["static"]++;
  10. elseif($6~/Baiduspider/){res["baidu"]++;res["spider"]++;}
  11. elseif($6~/Googlebot/){res["google"]++;res["spider"]++;}
  12. elseif($6~/Yahoo!Slurp/){res["yahoo"]++;res["spider"]++;}
  13. elseif($6~/Sogouwebspider/){res["sogou"]++;res["spider"]++;}
  14. elseif($6~/Sosospider/){res["soso"]++;res["spider"]++;}
  15. elseif($6~/YodaoBot/){res["youdao"]++;res["spider"]++;}
  16. elseif($6~/msnbot/){res["msnbot"]++;res["spider"]++;}
  17. elseif($6~/iaskspider/){res["sina"]++;res["spider"]++;}
  18. elseres["good"]++;
  19. }
  20. END{
  21. for(iteminres)printitem":"res[item]
  22. }'$1>$newfile
  23. }
#!/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 }

执行文件 stat_every_day.sh

[c-sharp] view plain copy print ?
  1. #!/bin/bash
  2. ../stat_func.sh
  3. date=`date+'%Y-%m-%d'`
  4. find/data/cs_log_backup/${date}-name"*.access.*"|/
  5. whilereadfile
  6. do
  7. $(stat_nginx_log"$file")
  8. done
#!/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

crontab -e

最下面增加一行

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

参考文献:

http://tech.foolpig.com/2008/07/09/linux-shell-char/ shell字符串的截取

http://storysky.blog.51cto.com/628458/270671用AWK来过滤nginx日志中的特定值

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

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

转载http://blog.csdn.net/LongMarch12/archive/2011/02/24/6204550.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值