goaccess 分析nginx的 log

111人浏览   2023-10-23 14:36:39

目录


一、需求背景

二、安装

三、分析静态

四、动态分析

一、需求背景

统计AP 使用峰值。客户端访问AP 是通过Nginx 代理实现的。因此可以从Nginx的log着手分析,配合管道命令可以定向分析某些具体请求或者某段时间的nginx log,因此通过goaccess 来分析Nginx可满足需求。

二、安装

安装goaccess之前唯一需要依赖的包是ncurses。


1、tar xvf goaccess-1.2.tar.gz -C /usr/local

这里请注意,将解压文件压在/use/local 的原因是

有的依赖库会找不到而报错

例如

: Failed dependencies: GeoIP = 1.6.5-1.el6 is needed by GeoIP-devel-1.6.5-1

这个浪费了我快3个小时,从github上把geoip的项目clone下来也没有解决,最后是使用-C 参数将/usr/local下。


2、cd goaccess-1.2/


3、 ./configure --enable-utf8 --enable-geoip=legacy

4、 make

5、 make install

3 4 5步骤分别是什么意思呢?

第一步:./configure

首先检查机器的一些配置和环境,系统的相关依赖。如果缺少相关依赖,脚本会停止执行,软件安装失败

根据之前检查环境和依赖的结果,生产Makefile文件(main job)


第二步:make

make是Unix系统下的一个包。执行make命令需Makefile文件。make会根据Makefile文件中指令来安装软件

Makefile文件中有许多标签,来表示不同的section。一般的,make会编译源代码并生成可执行文件,其实Makefile主要就是描述文件编译的相互依赖关系

第三步:make install

当执行make命令不加任何参数,程序就会按照Makefile的指令在相应的section间跳转并且执行相应的命令

加上install参数即执行make install时,程序只会执行install section处的命令。install section的指令会将make阶段生产的可执行文件拷贝到相应的地方,例如/usr/local/bin

make clean 会删除上次make生产的obj文件以及可执行文件

三、分析静态

具体其他参数详见 这里。

goaccess -a -d -f /usr/local/nginx/logs/access-2020-05-18_151.log -p ./config/goaccess.conf >/tmp/out.html


四、动态分析

关键参数 :--real-time-html 实时通过websocket 协议将分析结果推送给客户端。

#!/bin/bash

#desc: generate html with goaccess based on nginx log

#author :ninesun

#parp: null

cd /usr/local/goaccess-1.2/

LANG="zh_CN.UTF-8"

goaccess /usr/local/nginx/logs/access.log -o /opt/goaccess/html/out.html --time-format='%H:%M:%S' --date-format='%d/%b/%Y' --log-format=COMBINED --real-time-html

执行该脚本后,会提示

goaccess]# ps -ef|grep goacc

root 8757 8755 7 11:23 pts/1 00:00:08 goaccess /usr/local/nginx/logs/access.log -o /opt/goaccess/html/out.html --time-format=%H:%M:%S --date-format=%d/%b/%Y --log-format=COMBINED --real-time-html

WebSocket server ready to accept new client connections

服务端目前已经ok了,现在在nginx中配置客户端请求路径。使用nginx代理websocket 请求。

#ninesun add 2020年5月20日17:04:31

location /out.html {

alias /opt/goaccess/html/out.html;

auth_basic "请输入用户名和密码!"; #这里是验证时的提示信息

auth_basic_user_file
/opt/goaccess/html/out_passwd; #这是你生成密码存放的文件

proxy_http_version 1.1; # 这三行是为了实现websocket

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";

}


为了安全性,使用 htpasswd 生成秘钥。

htpasswd -c /opt/goaccess/html/out_passwd test

设置好密码后登陆即可以实时查看了。



在这里我一直有一个疑问,websocket 如何和客户端交互?

socket 是实时将数据发送给客户端的。


相关推荐