goaccess日志分析工具
简述
什么是goaccess
GoAccess 是一款开源的且具有交互视图界面的实时 Web 日志分析工具,通过你的 Web 浏览器或者 *nix 系统下的终端程序(terminal)即可访问。能为系统管理员提供快速且有价值的 HTTP 统计,并以在线可视化服务器的方式呈现。
goaccess主要功能
- 统计概况,流量消耗等
- 访客排名
- 动态Web请求
- 静态web请求,如图片、样式表、脚本等。
- 来路域名
- 404 错误
- 操作系统
- 浏览器和搜索引擎
- 主机、DNS和ip地址
- HTTP 响应代码
- 引荐网站
- 键盘布局
- 自定义显示
- 支持超大日志(分析速度很快)
安装
[root@lnmp software]# wget http://tar.goaccess.io/goaccess-1.2.tar.gz
[root@lnmp software]# tar -xzvf goaccess-1.2.tar.gz
[root@lnmp software]# cd goaccess-1.2/
[root@lnmp goaccess-1.2]# ./configure --enable-utf8 --enable-geoip=legacy
...
config.status: executing depfiles commands
Your build configuration:
Prefix : /usr/local
Package : goaccess
Version : 1.2
Dynamic buffer : no
Geolocation : GeoIP Legacy
Storage method : In-memory Hash Database (Default)
TLS/SSL : no
Compiler flags : -pthread
Linker flags : -lnsl -lncursesw -lGeoIP -lpthread
Bugs : goaccess@prosoftcorp.com
[root@lnmp goaccess-1.2]# make
[root@lnmp goaccess-1.2]# make install
[root@lnmp software]# goaccess
GoAccess - 1.2
Usage: goaccess [filename] [ options ... ] [-c][-M][-H][-q][-d][...]
The following options can also be supplied to the command:
Log & Date Format Options
--date-format=<dateformat> - Specify log date format. e.g., %d/%b/%Y
--log-format=<logformat> - Specify log format. Inner quotes need to be
escaped, or use single quotes.
--time-format=<timeformat> - Specify log time format. e.g., %H:%M:%S
配置
修改配置。
log-format 与 access.log 的 log_format 格式对应,每个参数以空格或者制表符分割。
[root@lnmp conf]# vim /data/software/goaccess-1.2/config/goaccess.conf
...
#compression zlib
time-format %H:%M:%S
date-format %d/%b/%Y
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
参数
参数 | 含义 |
---|---|
%t | 匹配time-format格式的时间字段 |
%d | 匹配date-format格式的日期字段 |
%h | host(客户端ip地址,包括ipv4和ipv6) |
%r | 来自客户端的请求行 |
%m | 请求的方法 |
%U | URL路径 %H 请求协议 |
%s | 服务器响应的状态码 |
%b | 服务器返回的内容大小 |
%R | HTTP请求头的referer字段 |
%u | 用户代理的HTTP请求报头 |
%D | 请求所花费的时间,单位微秒 |
%T | 请求所花费的时间,单位秒 |
%^ | 忽略这一字段 |
基本语法
语法
goaccess [filename] [ options ... ] [-c][-M][-H][-q][-d][...]
常用参数
参数 | 含义 |
---|---|
-f | 日志文件名 |
-b | 开启流量统计,如果希望加快分析速度不建议使用该参数 |
-s | 开启HTTP响应代码统计 |
-a | 开启用户代理统计 |
-e | 开启指定IP地址统计,默认禁用 |
-p | -指定goacces用户配置文件路径 |
生成html
[root@lnmp conf]# goaccess -f /data/wwwlogs/access.log -o /usr/local/nginx/html/index.html -p /data/software/goaccess-1.2/config/goaccess.conf
goaccess支持实时生成html(我测试没有实时生成^_^),具体参数如下:
参数 | 含义 |
---|---|
--real-time-html | 实时 HTML 报告 |
--daemonize | 使 GoAccess 作为守护程序运行(仅在 --real-time-html 开启下有效 |
--port= | 指定服务使用的端口。GoAccess 默认使用端口 7890 作为 WebSocket 服务器。请确保此端口可用。 |
鉴于对实时性要求不高,加上测试的时候并不能实时生成html,所以我还是走shell脚本+crontab来生成把。
#!/usr/bin/env bash
DATE=$(date +%Y-%m-%d)
LOG_FILE="/data/wwwlogs/access.log"
WEB_ROOT_DIR="/data/wwwroot/goaccess"
goaccess -f $LOG_FILE -o $WEB_ROOT_DIR/$DATE.html -p /data/software/goaccess-1.2/config/goaccess.conf
chmod +x /data/script/goaccess.sh
加入crontab计划任务,至于时间间隔根据自己的场景定义把。
* */1 * * * /data/script/goaccess.sh > /dev/null
测试
参考资料
- 上一篇: 你好,崭新的2020!
- 下一篇: Redis数据导入导出方法