51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 4567|回复: 0
打印 上一主题 下一主题

[转贴] awk分析nginx日志,获取pv

[复制链接]
  • TA的每日心情
    奋斗
    2022-5-8 19:23
  • 签到天数: 137 天

    连续签到: 1 天

    [LV.7]测试师长

    跳转到指定楼层
    1#
    发表于 2011-6-14 13:54:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    最近在深入系统运维的事情,从服务器配置、调优、维护,到各种脚本编写。实现自动化智能运维的目标还要很远。

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

    函数库文件 stat_func.sh


    view plaincopy to clipboardprint?
    #!/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


    view plaincopy to clipboardprint?
    #!/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
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-28 00:01 , Processed in 0.074842 second(s), 27 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表