非常小又精悍的 Nginx 日志分析工具 Goaccess,处理速度非常快,我用 XShell 查看,还可以生成 html 报告,先看图
GoAccess 在 CentOS 上的安装方法:
1. 需要安装 GeoIP, ncurses, glib2, 一句搞定
yum -y install glib2 glib2-devel ncurses ncurses-devel geoIP geoIP-devel
2. 下载 GoAccess 解压编译安装
# tar zxvf goaccess-0.6.1.tar.gz
# cd goaccess-0.6.1
# ./configure –enable-geoip –enable-utf8
# make && make install
# make clean
3. GoAccess 使用方法
进入 Nginx 日志所在目录
命令行直接查看
也可以生成 HTML 报告
或者干脆分析目前下所有日志
zcat access.log* | goaccess
如果需要分析某天的日志,例如10月5号那天的日志,我们让linux管道命令来大显身手^_^。
sed -n ‘/05\/Dec\/2010/,$ p’ access.log | goaccess -s –b
分析从11月5号到12月5号一个月内的日志
sed -n ‘/5\/Nov\/2010/,/5\/Dec\/2010/ p’ access.log | goaccess -s –b
当你不希望在服务器上安装goaccess程序,可以通过调用本地的goaccess程序来分析服务器上的日志(很神奇吧^_^):
ssh user@server ‘cat /var/log/apache2/access.log’ | goaccess -s -a
附下我的NGINX日志格式:
log_format main ’$remote_addr – $remote_user [$time_local] “$request” ‘
color_scheme 0
date_format %d/%b/%Y
log_format %h %^[%d:%^] “%r” %s %b “%R” “%u” %T
for Apache HTTP Server 预设设定:
CustomLog logs/access_log common可选Common Log Format (CLF)
CustomLog logs/access_log combined可选NCSA Combined Log Formatfor Lighttpd mod_accesslog预设格式 : (需于GoAccess设定画面手动编辑Log Format)
Log Format: %h %^ %^ [%d:%^] “%r” %s %b “%R” “%u”
Date Format: %d/%b/%Y完成设定后产生~/.goaccessrc,日后执行goaccess -f /var/log/httpd/access_log不会再跳出这个设定画面;若要修改设定,可加上-c参数,例如: goaccess -c -f / var/log/httpd/access_log或直接编辑设定档案vi ~/.goaccessrc
基本操作
# goaccess -f /var/log/httpd/access_log
t:回到顶端
b:卷到最末
q:关闭视窗或离开程式
上下方向键:卷动画面
数字键0 ~ 9,接着按英文字母o或右方向键:查看某项目的细节(*注)*注: 0 表示第十项; Shift + 1 表示第十一项
排除统计某来源IP
# goaccess -e 123.123.123.123 -f /var/log/httpd/access_log检视Host详细资料的时候显示来自该Host的User-Agents资讯
# goaccess -a -f /var/log/httpd/access_log产生HTML报表(静态报表)
# goaccess -a -f /var/log/httpd/access_log > result.html只统计来自某IP的记录
# grep ^123.123.123.123 /var/log/httpd/access_log | goaccess注:目前GoAccess无法即时统计pipe进来的资料,日后改版或许会补强这部份(Ref: man goaccess )
Trouble Shooting
无法使用F1 检视help 讯息的替代方案
Ctrl + h
无法使用F10 完成设定画面的替代方案
# vi ~/.goaccessrc
color_scheme 1 date_format %d/%b/%Y log_format %h %^ %^ [%d:%^] "%r" %s %b "%R" "%u"
其他log_format 参考:
Common Log Format (CLF): %h %^[%d:%^] "%r" %s %b Common Log Format (CLF) with Virtual Host: %^:%^ %h %^[%d:%^] "%r" %s %b NCSA Combined Log Format: %h %^[%d:%^] "%r" %s %b "%R" "%u" NCSA Combined Log Format with Virtual Host: %^:%^ %h %^[%d:%^] "%r" %s %b "%R" "%u" W3C: %d %^ %h %^ %^ %^ %^ %r %^ %s %b %^ %^ %u %R
变更热键
# cd /path/to/goaccess-0.5
# vi goaccess.c
#将F1热键改为F2
找到[F1]Help [O]pen detail view
改为[ F2 ]Help [O]pen detail view找到Case 265:
改为Case 266 :# vi commons.c
#将Help讯息的F1改为F2
找到” ^F1^ or ^CTRL^ + ^h^ [main help]“,
改为” ^ F2 ^ or ^CTRL^ + ^h^ [main help]“,# vi goaccess.1
#将manpage的F1字眼改为F2
找到.IP “F1″
改为.IP “ F2 ”# vi settings.c
#将F10热键改为F9
找到mvwprintw (win, 2, 2, “[SPACE] to toggle – [F10] to proceed”);
改为mvwprintw (win, 2, 2, “[SPACE] to toggle – [ F9 ] to proceed”);找到Case 274:
改为Case 273 :