Back

使用 goaccess分析nginx日志(analyze Nginx log using GoAccess)

发布时间: 2014-04-14 01:41:00

nginx 日志无法用rails-request-analyzer 来分析。 需要使用 goaccess 。 因为前者一分析就死机,后者速度更快,分析1.2G的日志大约20秒。 ( if you want to analyze nginx log, use GoAccess)

安装: $ aptitude install goaccess

用法非常简单:  (quite simple to use) 

$ goaccess -f access.log (然后会出现弹窗,要求选择日志的格式. 对于nginx来说,默认第一个 NCSC combined 就可以了:

                                               +--------------------------------------------------+
                                               | Log Format Configuration                         |
                                               | [SPACE] to toggle - [ENTER] to proceed           |
                                               |                                                  |
                                               | [x] NCSA Combined Log Format                     |
                                               | [ ] NCSA Combined Log Format with Virtual Host   |
                                               | [ ] Common Log Format (CLF)                      |
                                               | [ ] Common Log Format (CLF) with Virtual Host    |
                                               | [ ] W3C                                          |
                                               | [ ] CloudFront (Download Distribution)           |
                                               |                                                  |
                                               | Log Format - [c] to add/edit format              |
                                               | %h %^[%d:%t %^] "%r" %s %b "%R" "%u"" "%u"       |
                                               |                                                  |
                                               | Date Format - [d] to add/edit format             |
                                               | %d/%b/%Y                                         |
                                               |                                                  |
                                               | Time Format - [t] to add/edit format             |
                                               | %H:%M:%S                                         |
                                               +--------------------------------------------------+

光标移动到对应的格式,然后按空格,回车,我们等待,就可以看到结果. 

如果需要 html格式的话,我们使用下面的命令. 之前需要一个配置文件:

$ vim /etc/goaccess.conf

 
# 就是依次根据 nginx 的格式,打开下面的注释:
time-format %H:%M:%S
date-format %d/%b/%Y
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
$ goaccess -f <your_log_file> -a > result.html

就可以看到对应的日志的细节了. 

需要注意的是: 上面的配置文件是 “空格敏感"的,如果你的log文件中某个关键的内容之间有2个空格,你需要也在配置文件中增加两个空格。

例如:log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"   (%r 前面是2个空格)

但是goaccess的一个缺点是:  分析大日志时,如果你的机器内存太小,就会报错退出。例如,你的机器是4G内存,但是要分析的内容是7G大小,这时候就会 在机器运行2,3分钟,接近死机是,出现 Killed 的结果(还好GoAccess会自动 干掉这个进程 )  ( but it's a weakpoint that goaccess can't analyze big file, e.g. 7G size.  ) 

所以解决办法是: 1. 把大日志切成小文件。  2. 分析小文件。

$ split -b 2G <your_log_file>

$ goaccess... 略

Back