设为首页 收藏本站
查看: 1548|回复: 0

[经验分享] 用goaccess每天自动分析nginx日志

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-12-5 09:03:49 | 显示全部楼层 |阅读模式
上次用awstats来自动分析nginx的日志,不过发现它只是累计的不能看一天或某个时间段内情况,于是就加了个goaccess来分析日志,
思路:每天将原来awstats集中过来的日志文件处理一下,生成符合goaccess的新的日志文件,然后用goaccess来生产静态页,nginx将这些静态页展示出来。
一、软件
    goaccess:
    ncurses(goaccess的依赖)


1
ubuntu:apt-get install libncursesw5-dev



二、安装
1
2
3
4
5
6
    wget http://tar.goaccess.io/goaccess-1.1.1.tar.gz
    tar -zxvf goaccess-1.1.1.tar.gz
    cd goaccess-1.1.1
    ./configure —enable-geoip —enable-utf8
    make
    make install



三、配置goaccess.conf
    默认goaccess的log-format是比较简单的,我这自定义了

1
2
3
    time-format %H:%S:%M
    date-format %d/%b/%Y
    log-format %d:%t %^ %h %s %T  %m %U %H %b %R %u




官方给了各日志段的代码,可以参考着修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
SPECIFIERS
%x A date and time field matching the time-format and date-format variables. This is used when a timestamp is given instead of the date and time being in two separate variables.
%ttime field matching the time-format variable.
%ddate field matching the date-format variable.
%vThe server name according to the canonical name setting (Server Blocks or Virtual Host).
%eThis is the userid of the person requesting the document as determined by HTTP authentication.
%hhost (the client IP address, either IPv4 or IPv6)
%rThe request line from the client. This requires specific delimiters around the request (as single quotes, double quotes, or anything else) to be parsable. If not, we have to use a combination of special format specifiers as %m %U %H.
%qThe query string.
%mThe request method.
%UThe URL path requested.
Note: If the query string is in %U, there is no need to use %q. However, if the URL path, does not include any query string, you may use %q and the query string will be appended to the request.
%HThe request protocol.
%sThe status code that the server sends back to the client.
%bThe size of the object returned to the client.
%RThe "Referer" HTTP request header.
%uThe user-agent HTTP request header.
%DThe time taken to serve the request, in microseconds.
%TThe time taken to serve the request, in seconds with milliseconds resolution.
%L The time taken to serve the request, in milliseconds as a decimal number.
%^Ignore this field.
%~Move forward through the log string until a non-space (!isspace) char is found.
|Vertical pipe or bar is used for either the character prior to the vertical pipe or followed by the vertical pipe.




四、编写脚本
    我这nginx得日志格式是自定义的,所有做了处理。不同线路的访问日志也合并到一起来处理的。

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
DATE=`/bin/date -d "1 days ago" +%Y%m%d`
PREFIXS='www p m news'
DOMS='xxx.com'
BASIC_DIR="/data/awstats"
for PREFIX in $PREFIXS
do
cat $BASIC_DIR/logs/$PREFIX/11/access_$DATE.log $BASIC_DIR/logs/$PREFIX/13/access_$DATE.log >$BASIC_DIR/goaccess/orig_logs/$PREFIX.$DOMS/access_$DATE.log
awk -F "|" '{print $2,$3,$5,$6,$12,$14,$11,$13}' $BASIC_DIR/goaccess/orig_logs/$PREFIX.$DOMS/access_$DATE.log >$BASIC_DIR/goaccess/logs/$PREFIX.$DOMS/access_$DATE.log
/usr/local/bin/goaccess -f $BASIC_DIR/goaccess/logs/$PREFIX.$DOMS/access_$DATE.log -p $BASIC_DIR/goaccess/scripts/goaccess.conf >$BASIC_DIR/goaccess/html/$PREFIX.$DOMS/access_$DATE.html
done



五、crontab自动处理
每天早上6点开始处理
1
0 6 * * * (bash /data/awstats/goaccess/scripts/goaccess.sh)



六、nginx展示goaccess的结果

cat goaccess.conf
1
2
3
4
5
6
7
server{
        listen 88;
        server_name 11.1.2.252;
        root /data/awstats/goaccess/html/;
        autoindex on;
        access_log logs/access.ga.log;
        }



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-309799-1-1.html 上篇帖子: Nginx 配置静态文件过期时间&防盗链 下篇帖子: nginx keepalived 配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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