GoAccess安裝


編譯安裝

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

cd /usr/local/src
wget https://tar.goaccess.io/goaccess-1.3.tar.gz
tar -xzvf goaccess-1.3.tar.gz
cd goaccess-1.2/
./configure --enable-utf8 --enable-geoip=legacy --with-openssl --enable-tcb=memhash --with-getline
make
make install

注意: 僅僅需要依賴 ncurses 模塊。

提示:如果需要使用實時 HTML 報告, 請確保開放 7890 端口。

驗證安裝是否成功:goaccess --v

# goaccess --v
GoAccess - 1.3.
For more details visit: http://goaccess.io
Copyright (C) 2009-2016 by Gerardo Orellana

Build configure arguments:
  --enable-utf8
  --enable-geoip=mmdb
  --enable-tcb=memhash
  --with-getline
  --with-openssl

或者最簡單的使用yum方式安裝:yum install goaccess,不過不是最新版的

編譯選項

GoAccess 擁有多個配置選項。獲取完整的最新配置選項列表,請運行:./configure --help

  • --enable-debug

    使用調試標志編譯且關閉編譯器優化。

  • --enable-utf8

    寬字符支持。依賴 Ncursesw 模塊。

  • --enable-geoip=<legacy|mmdb>

    地理位置支持。依賴 MaxMind GeoIP 模塊。legacy 將使用原始 GeoIP 數據庫。mmdb 將使用增強版 GeoIP2 數據庫。

  • --enable-tcb=<memhash|btree>

    Tokyo Cabinet 存儲支持。 memhash 將使用 Tokyo Cabinet 的內存哈希數據庫。 btree 將使用 Tokyo Cabinet 的磁盤 B+Tree 數據庫。

  • --disable-zlib

    禁止在 B+Tree 數據庫上使用 zlib 壓縮。

  • --disable-bzip

    禁止在 B+Tree 數據庫上使用 bzip2 壓縮。

  • --with-getline

    使用動態擴展行緩沖區用來解析完整的行請求,否則將使用固定大小(4096)的緩沖區。

  • --with-openssl

    使 GoAccess 與其 WebSocket 服務器之間的通信能夠支持 OpenSSL。

安裝好后配置文件路徑:/usr/local/etc/goaccess.conf

相關發行版安裝包

GoAccess 使用純C編寫,僅依賴 ncurses 模塊。 但是如果通過源碼來構建 GoAccess,不同發行版還是有一些區別,下表列出了一些發行版的可選依賴模塊。

Distro NCurses GeoIP (optional) Tokyo Cabinet (optional) OpenSSL (optional)
Ubuntu/Debian libncursesw5-dev libgeoip-dev libtokyocabinet-dev libssl-dev
Fedora/RHEL/CentOS ncurses-devel geoip-devel tokyocabinet-devel openssl-devel
Arch Linux ncurses geoip compile from source openssl
Gentoo sys-libs/ncurses dev-libs/geoip dev-db/tokyocabinet dev-libs/openssl
Slackware ncurses GeoIP tokyocabinet openssl

注意: 通過源碼構建 GoAccess 您可能需要一些編譯/構建工具比如 gcc, make, base-devel, build-essential, "Development Tools" 等等。

GoAccess配置

在配置文件最后一行加入下面代碼,配置文件路徑:/usr/local/etc/goaccess.conf

log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"  
date-format %d/%b/%Y  
time-format %H:%M:%S 

1、中文展示

如果你的系統默認設置時英文顯示,想要以中文顯示日志報表,就需要進行字符設置

臨時設置為中文的命令:
# LANG="zh_CN.UTF-8"
# goaccess -p /usr/local/etc/goaccess/goaccess.conf -f /var/log/nginx/access.log  -o /usr/share/nginx/html/report.html

2、配置nginx中日志log_format中的日志格式

該功能是識別nginx日志中關於時間的格式,生成goaccess可以識別的時間格式,然后把這個時間格式配置到Goaccess配置文件中。若是nginx的access日志是格式使用默認的設置,則這一步不用配置

改完記得reload配置

2.1 獲取nginx.conf中log_format配置

2.2 獲取nginx2goaccess腳本,可以將日志格式轉換為goaccess能識別的日志格式,腳本地址:https://github.com/stockrt/nginx2goaccess/blob/master/nginx2goaccess.sh

#!/bin/bash
#
# Convert from this:
#   http://nginx.org/en/docs/http/ngx_http_log_module.html
# To this:
#   https://goaccess.io/man
#
# 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
#
# Samples:
#
# log_format combined '$remote_addr - $remote_user [$time_local] '
# '"$request" $status $body_bytes_sent '
# '"$http_referer" "$http_user_agent"';
#   ./nginx2goaccess.sh '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'
#
# log_format compression '$remote_addr - $remote_user [$time_local] '
# '"$request" $status $bytes_sent '
# '"$http_referer" "$http_user_agent" "$gzip_ratio"';
#   ./nginx2goaccess.sh '$remote_addr - $remote_user [$time_local] "$request" $status $bytes_sent "$http_referer" "$http_user_agent" "$gzip_ratio"'
#
# log_format main
# '$remote_addr\t$time_local\t$host\t$request\t$http_referer\t$http_x_mobile_group\t'
# 'Local:\t$status\t$body_bytes_sent\t$request_time\t'
# 'Proxy:\t$upstream_cache_status\t$upstream_status\t$upstream_response_length\t$upstream_response_time\t'
# 'Agent:\t$http_user_agent\t'
# 'Fwd:\t$http_x_forwarded_for';
#   ./nginx2goaccess.sh '$remote_addr\t$time_local\t$host\t$request\t$http_referer\t$http_x_mobile_group\tLocal:\t$status\t$body_bytes_sent\t$request_time\tProxy:\t$upstream_cache_status\t$upstream_status\t$upstream_response_length\t$upstream_response_time\tAgent:\t$http_user_agent\tFwd:\t$http_x_forwarded_for'
#
# log_format main
# '${time_local}\t${remote_addr}\t${host}\t${request_method}\t${request_uri}\t${server_protocol}\t'
# '${http_referer}\t${http_x_mobile_group}\t'
# 'Local:\t${status}\t*${connection}\t${body_bytes_sent}\t${request_time}\t'
# 'Proxy:\t${upstream_status}\t${upstream_cache_status}\t'
# '${upstream_response_length}\t${upstream_response_time}\t${uri}${log_args}\t'
# 'Agent:\t${http_user_agent}\t'
# 'Fwd:\t${http_x_forwarded_for}';
#   ./nginx2goaccess.sh '${time_local}\t${remote_addr}\t${host}\t${request_method}\t${request_uri}\t${server_protocol}\t${http_referer}\t${http_x_mobile_group}\tLocal:\t${status}\t*${connection}\t${body_bytes_sent}\t${request_time}\tProxy:\t${upstream_status}\t${upstream_cache_status}\t${upstream_response_length}\t${upstream_response_time}\t${uri}${log_args}\tAgent:\t${http_user_agent}\tFwd:\t${http_x_forwarded_for}'
#
# Author: Rogério Carvalho Schneider <stockrt@gmail.com>

# Params
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
"
# EOF
# sh nginx2goaccess.sh '$remote_addr $http_x_forwarded_for [$time_local] $host "$request_uri" $status "$http_referer" "$http_user_agent"'

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

2.3 將轉換后的內容加入到配置/usr/local/etc/goaccess.conf

3.添加定時任務,每隔30分鍾執行一次

# vim /opt/goaccess_nginx_log.sh
#!/bin/env /bash

LANG="zh_CN.UTF-8"
/usr/local/bin/goaccess -p /usr/local/etc/goaccess/goaccess.conf -f /var/log/nginx/access.log  -o /usr/share/nginx/html/report.html

# chmod a+x /opt/goaccess_nginx_log.sh
# crontab -e 
# 每隔1分鍾生成一次html文件
*/1 * * * * /usr/bin/bash /opt/goaccess_nginx_log.sh

4.配置一個Nginx虛擬主機,將root指向/usr/share/nginx/html/,這樣可以通過域名去訪問html頁面

1.虛擬主機,在原有的配置加一個location

location / {
	root /usr/share/nginx/html/;
	index report.html;
}

5.完善的地方

  1. nginx的access日志每天輪詢,每天生成report頁面

后期考慮到每天都要對nginx的access日志進行切割,可以使用腳本的形式每天根據切割后的日志文件生成響應的report頁面,然后使用瀏覽器訪問的時候根據日期進行選擇

進一步完善/opt/goaccess_nginx_log.sh腳本即可,使用此腳本獲取nginx日志分割的信息,定期刪除日志等

  1. 忽略自身查看頁面的訪問要如何做?
  2. 顯示來源IP所在地區?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM