GoAccess分析Nginx/Apache/IIS日志
CentOS 下用 yum 安装
yum install -y epel-release
yum install -y GeoIP-devel ncurses-devel
yum install -y goaccess
CowAxess - Windows 版 GoAccess


安装到 IIS 日志目录

生成今日日志数据
goaccess -f W3SVC6/*.log --log-format=W3C -a -o log.html

生成全部日志数据
goaccess -f W3SVC*/*.log --log-format=W3C -a -o all_log.html


浏览器访问 http://9duw.com/log.html


ubuntu 包管理器安装
apt-get install goaccess
使用 GoAccess 源码编译安装
编译安装之前需要安装 geoip 依赖开发包
git clone https://github.com/maxmind/geoip-api-c
cd geoip-api-c
./configure
make
make install
GoAccess 分析宝塔 nginx 日志文件生成 HTML 页面
GoAccess 可以将日志分析报告生成静态 HTML 页面以供展示,在使用 GoAccess 生成 HTML 页面之前,需要修改 GoAccess 的配置文件来指定日志格式,注意日志的格式需要根据情况而定
cat goaccessrc
time-format %T
date-format %d/%b/%Y
log_format %h %^[%d:%t %^] "%r" %s %b "%R" "%u" %^
使用 GoAccess 生成静态 HTML 页面
goaccess baota.com.log -p goaccessrc -a -o baota.com.html
使用 GoAccess 生成的静态 HTML 页面如下

不识别的格式可使用 nginx2goaccess.sh 脚本将 nginx 日志格式格式化为 goaccess 能识别的日志格式
https://github.com/stockrt/nginx2goaccess
log_format="$1"
# Usage
if [[ -z "$log_format" ]]; then echo "Usage: $0 '<log_format>'" ; exit 1 ; fi
# Variables map
conversion_table="time_local,%d:%t_%^
host,%v
http_host,%v
remote_addr,%h
request_time,%T
request_method,%m
request_uri,%U
server_protocol,%H
request,%r
status,%s
body_bytes_sent,%b
bytes_sent,%b
http_referer,%R
http_user_agent,%u"
# Conversion
for item in $conversion_table; do
nginx_var=${item%%,*}
goaccess_var=${item##*,}
goaccess_var=${goaccess_var//_/ }
log_format=${log_format//\$\{$nginx_var\}/$goaccess_var}
log_format=${log_format//\$$nginx_var/$goaccess_var}
done
log_format=$(echo "$log_format" | sed 's/${[a-z_]*}/%^/g')
log_format=$(echo "$log_format" | sed 's/$[a-z_]*/%^/g')
# Config output
echo "
- Generated goaccess config:
time-format %T
date-format %d/%b/%Y
log_format $log_format
"
使用如下方法获取日志格式:
sh nginx2goaccess.sh '<log_format>' #log_format为你nginx.conf中配置的日志格式
如:
bash nginx2goaccess.sh '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" $connection $upstream_addr $upstream_response_time $request_time'
会得到三个格式
time-format %T
date-format %d/%b/%Y
log_format %h - %^ [%d:%t %^] "%r" %s %b "%R" "%u" "%^" %^ %^ %^ %T
将上述格式填写到 goaccessrc 文件中即可
log-format 与 access.log 的 log_format 格式对应,每个参数以空格或者制表符分割
%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 请求所花费的时间,单位秒
%^ 忽略这一字段
日志格式 COMBINED | Combined Log Format VCOMBINED | Combined Log Format with Virtual Host COMMON | Common Log Format VCOMMON | Common Log Format with Virtual Host W3C | W3C Extended Log File Format SQUID | Native Squid Log Format CLOUDFRONT | Amazon CloudFront Web Distribution CLOUDSTORAGE | Google Cloud Storage AWSELB | Amazon Elastic Load Balancing AWSS3 | Amazon Simple Storage Service (S3)
goaccess iis w3c 自定义log 格式参考
goaccess 支持强大的自定义log 格式,比如我们需要分析iis w3c 格式日志
参考IIS W3C | W3C Extended Log File Format
goaccess -f u_ex211206.log --log-format=W3C -a -o u_ex211206.html
最后更新于