1.GoAccess是什么

GoAccess 是一款开源的且具有交互视图界面的实时 Web 日志分析工具,通过你的 Web 浏览器或者 *nix 系统下的终端程序(terminal)即可访问。

能为系统管理员提供快速且有价值的 HTTP 统计,并以在线可视化服务器的方式呈现

2.GoAccess功能

完全实时:所有指标在终端上每 200 毫秒更新一次,在 HTML 输出上每秒更新一次。

跟踪应用程序响应时间:跟踪服务请求所需的时间。如果你想跟踪减慢了网站速度的网页,则非常有用。

访问者:按小时或日期确定最慢运行的请求的点击量、访问者数、带宽数和指标。

按虚拟主机的度量标准:如果有多个虚拟主机(Server),它提供了一个面板,可显示哪些虚拟主机正在消耗大部分 Web 服务器资源

goaccess

3.安装GoAccess

编译安装

有的下载会遇到证书问题

[root@web01 ~]$ wget https://tar.goaccess.io/goaccess-1.6.2.tar.gz
–2022-08-16 12:48:12– https://tar.goaccess.io/goaccess-1.6.2.tar.gz
Resolving tar.goaccess.io (tar.goaccess.io)… 67.205.130.138
Connecting to tar.goaccess.io (tar.goaccess.io)|67.205.130.138|:443… connected.
ERROR: cannot verify tar.goaccess.io’s certificate, issued by ‘/C=US/O=Let’s Encrypt/CN=R3’:
Issued certificate has expired.
To connect to tar.goaccess.io insecurely, use `–no-check-certificate’.

解决方案

[root@web01 ~]$ yum install -y ca-certificates

下载

[root@web01 ~]$ wget https://tar.goaccess.io/goaccess-1.6.2.tar.gz

解压

[root@web01 ~]$ tar -xzvf goaccess-1.6.2.tar.gz

[root@web01 ~]$ cd goaccess-1.6.2/
[root@web01 ~/goaccess-1.6.2]$

字符集会出现报错

[root@web01 ~/goaccess-1.6.2]$ ./configure –enable-utf8 –enable-geoip=mmdb

configure: error:

*** Missing development files for the GeoIP library

configure: error:
*** Missing development libraries for  ncursesw

configure: error:
*** Missing development files for libmaxminddb library.

配置选项

–enable-debug:使用调试符号编译,并关闭编译器优化

–enable-utf8:用宽字符支持编译。Ncursesw是必需的。

–enable-geoip=<legacy|mmdb>:使用地理位置支持进行编译。需要MaxMind的GeoIP。legacy将利用原始的GeoIP数据库。mmdb将利用增强的GeoIP2数据库

–with-getline:动态扩展行缓冲区,以便解析整行请求,而不是使用4096的固定大小缓冲区

–with-openssl:为其WebSocket服务器编译支持OpenSSL的GoAccess

解决方案

第一种

configure: error:

*** Missing development files for the GeoIP library

yum install -y GeoIP-devel

第二种

configure: error:
*** Missing development libraries for  ncursesw

yum install -y ncurses-devel

第三种

configure: error:
*** Missing development files for libmaxminddb library.

yum install libmaxminddb-devel

依赖

yum install geoip-devel openssl-devel libmaxminddb-devel ncurses-devel bzip2-devel tokyocabinet-devel

安装完成

Your build configuration:

Prefix : /usr/local
Package : goaccess
Version : 1.6.2
Compiler flags : -pthread
Linker flags : -lnsl -lncursesw -lmaxminddb -lpthread
UTF-8 support : yes
Dynamic buffer : no
Geolocation : GeoIP2
Storage method : In-Memory with On-Disk Persistent Storage
TLS/SSL : no
Bugs : hello@goaccess.io

编译

[root@web01 ~/goaccess-1.6.2]$ make
[root@web01 ~/goaccess-1.6.2]$ make install

查看命令是否安装成功

[root@web01 ~/goaccess-1.6.2]$ which goaccess
/usr/local/bin/goaccess

4.goaccess的使用

参数

-o:输出报告
-q:地址统计不带 query 参数
–hour-spec:默认为 hr 按小时统计,min 按分钟统计
–log-format:指定日志格式,默认的 Nginx 日志需要指定 COMBINED
-f:指定日志文件
-p:指定配置文件(html页面显示使用)

命令行使用

[root@web01 ~]$ goaccess -f /var/log/nginx/access.log

选择日志格式

空格

log-format说明符

%x:与时间格式和日期格式变量匹配的日期和时间字段。当给出时间戳而不是日期和时间在两个独立的变量中时,使用这种方法。
%t:与时间格式变量匹配的时间字段。
%d:与日期格式变量匹配的日期字段。
%v:根据规范名称设置的服务器名称(服务器块或虚拟主机)。
%e:这是请求文档的人的用户id,由HTTP身份验证确定。
%C:服务器服务的对象的缓存状态。
%h:主机(客户端IP地址,IPv4或IPv6)
%r:来自客户端的请求行。这要求请求周围的特定分隔符(单引号、双引号等)是可解析的。否则,使用特殊格式说明符的组合,如%m, %U, %q和%H解析单个字段。
注意:使用任一种%r要获得完整的请求运筹学 %m, %U, %q和%H要形成你的请求,不要两者都用。
%m:请求方法。
%U:请求的URL路径。
注意:如果查询字符串在%U,没有必要使用%q。但是,如果URL路径不包含任何查询字符串,您可以使用%q并且查询字符串将被附加到请求中。
%q:查询字符串。
%H:请求协议。
%s:服务器发送回客户端的状态代码。
%b:返回给客户端的对象的大小。
%R:“Referer”HTTP请求标头。
%u:用户代理HTTP请求标头。
%K:为连接选择的TLS加密设置。(在Apache日志格式中:%{SSL_PROTOCOL}x).
%k:为连接选择的TLS加密设置。(在Apache日志格式中:%{SSL_CIPHER}x).
%M:所请求资源的MIME类型。(在Apache日志格式中:%{Content-Type}o)
%D:为请求提供服务所用的时间,以微秒计。
%T:为请求提供服务所用的时间,以秒为单位,分辨率为毫秒。
%L:为请求提供服务所用的时间,以毫秒为十进制数。
%^:忽略此字段。
%~:在日志字符串中向前移动,直到出现非空格(!isspace)字符。
~h:X-Forwarded-For (XFF)字段中的主机(客户端IP地址,IPv4或IPv6)。

命令行日志分析(回车)(tab切换)

html(页面)方式显示

修改配置文件

[root@web01 ~]$ egrep -n ‘^(time|date|log)’ /usr/local/etc/goaccess/goaccess.conf
13:time-format %H:%M:%S
36:date-format %d/%b/%Y
80:log-format %h %^[%d:%t %^] “%r” %s %b “%R” “%u”

输出报告

[root@web01 ~]$ goaccess -f /var/log/nginx/access.log -o /code/log/index.html

Cleaning up resources…

写一个web配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/log.goaccess.com.conf
server {
listen 80;
server_name log.goaccess.com;
root /code/log;
index index.html;
}

检查语法

[root@web01 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx

[root@web01 ~]$ systemctl reload nginx.service

web测试(静态)

如何实现实时更新

第一种命令行

[root@web01 ~]$ goaccess -f /var/log/nginx/access.log -o /code/log/index.html  –real-time-html

WebSocket server ready to accept new client connections

但是他会占用这个界面不能操作,如何解决这个问题(后台运行)

[root@web01 ~]$ goaccess -f /var/log/nginx/access.log -o /code/log/index.html–real-time-html –daemonize
Daemonized GoAccess: 33053

[root@web01 ~]$ ss -lntup | grep goaccess
tcp LISTEN 0 128 *:7890 *:* users:((“goaccess”,pid=34419,fd=9))

[root@web01 ~]$ ps -ef | grep goaccess
root 33053 1 0 21:11 ? 00:00:00 goaccess -f /var/log/nginx/access.log -o /code/log/index.html –real-time-html –daemonize

第二种修改配置文件

[root@web01 ~]$ egrep -n ‘^(daemonize|real-time-html)’ /usr/local/etc/goaccess/goaccess.conf
291:daemonize true
310:real-time-html true

[root@web01 ~]$ goaccess -f /var/log/nginx/access.log -o /code/log/index.html
Daemonized GoAccess: 33390

[root@web01 ~]$ ss -lntup | grep goaccess
tcp LISTEN 0 128 *:7890 *:* users:((“goaccess”,pid=34419,fd=9))

[root@web01 ~]$ ps -ef | grep goaccess
root 33390 1 0 21:15 ? 00:00:00 goaccess -f /var/log/nginx/access.log -o /code/log/index.html

多个日志

[root@web01 ~]$ cat /var/log/nginx/access.log.* | goaccess -d -o /code/log/index.html –
Daemonized GoAccess: 4102

[root@web01 ~]$ ps -ef | grep go
root 2007 1 0 08:45 ? 00:00:01 goaccess -f /var/log/nginx/access.log -o /code/log/index.html –real-time-html –daemonize
root 4107 4014 0 21:54 pts/0 00:00:00 grep –color=auto go

另一种放入后台

daemonize true
real-time-html true
with-output-resolver true

作者 dmxsp

发表回复

您的电子邮箱地址不会被公开。