当前位置:首页 > 系统运维
GoAccess 的安装与使用
来源:靑龍一笑的博客  作者:靑龍一笑  发布时间:2020-12-08 17:09:17  点击量:775  评论:0

    GoAccess 是一款开源且具有交互视图界面的 web 日志分析工具。
    首先安装 GeoIP:

[root@RicenOS softwares]# tar xvf GeoIP-1.6.12.tar.gz
[root@RicenOS softwares]# cd GeoIP-1.6.12
[root@RicenOS GeoIP-1.6.12]# ./configure
[root@RicenOS GeoIP-1.6.12]# make
[root@RicenOS GeoIP-1.6.12]# make install
[root@RicenOS GeoIP-1.6.12]# ln -s /usr/local/lib/libGeoIP.so* /lib64/
[root@RicenOS GeoIP-1.6.12]# cd ..

    在安装 GoAccess 之前,还需要安装 openssl-devel、libmaxminddb-devel、ncurses-devel 这三个依赖包:

[root@RicenOS softwares]# yum install openssl-devel libmaxminddb-devel ncurses-devel

    安装 GoAccess:

[root@RicenOS softwares]# tar xvf goaccess-1.4.3.tar.gz
[root@RicenOS softwares]# cd goaccess-1.4.3
[root@RicenOS goaccess-1.4.3]# ./configure --enable-utf8 --enable-geoip=mmdb --with-openssl
[root@RicenOS goaccess-1.4.3]# make
[root@RicenOS goaccess-1.4.3]# make install
[root@RicenOS goaccess-1.4.3]# cd ..

    之所以使用 --enable-geoip=mmdb,是因为 Legacy GeoIP 已经停产了。GoAccess 官网推荐使用 GeoIP2,需要准备 DB-IP Lite 数据库:

[root@RicenOS softwares]# gunzip dbip-city-lite-2020-12.mmdb.gz

    由于我要分析的是 nginx 日志,而 nginx 的日志格式如下:

log_format  main '[$time_local] - $remote_addr:$remote_port $http_host $http_x_forwarded_for '
                 '"$request" $status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent" $request_time';

    这里需要配置 nginx 日志文件对应的 GoAccess 格式:

[root@RicenOS softwares]# vi nginx_log_format

    内容如下:

time-format %T
date-format %d/%b/%Y
log_format [%d:%t %^] - %h:%^ %v %^ "%r" %s %b "%R" "%u" %T

    其中,格式符说明如下:

%d    匹配日期格式
%t    匹配时间格式
%^    忽略此字段
%h    客户端 IP 地址
%v    服务器名称
~h    http_x_forwarded_for 字段中的 IP 地址
%r    来自客户端的请求行
%s    服务器发送回客户端的状态代码
%b    返回给客户端的对象的大小
%R    http_referer 字段中的请求前导页
%u    用户代理 http 请求头
%T    以毫秒分辨率表示请求的用秒时间

    由于 web 采用了负载均衡,我准备了两台 web 服务器的 nginx 日志文件:

[root@RicenOS softwares]# ls web01/ web02/
web01/:
ricen.net_access.log-20201207

web02/:
ricen.net_access.log-20201207

    最后,使用 goaccess 命令生成详细的服务器统计信息:

[root@RicenOS softwares]# goaccess web01/* web02/* -p nginx_log_format --html-report-title=www.ricen.net-服务器统计信息 --geoip-database=dbip-city-lite-2020-12.mmdb -o www.ricen.net.html

    效果如下:

版权所有 © 2005-2023 靑龍一笑的博客  Powered by C.S.Ricen
Copyright © 2005-2023 by www.ricensoftwares.com.cn  All Rights Reserved.

欢迎光临本站,这里是靑龍一笑的博客。

因资金匮乏,本站已迁到国外的免费空间,可能导致本站的访问速度较慢,由此给您带来的不便,敬请谅解。

您可以通过下方的“支持本站建设”链接,给本站提供资金支持。

Free Web Hosting