顯示廣告
隱藏 ✕
看板 KnucklesNote
作者 Knuckles (站長 那克斯)
標題 [Apache] 讀取log檔的流量分析工具 GoAccess
時間 2023-11-24 Fri. 22:48:03



Apache 預設的 log 檔的每一筆連線記錄是像這樣
42.73.1.15 - - [23/Nov/2023:18:06:11 +0800] "GET /b/Gossiping/gENS HTTP/1.1" 200 8214 "https://disp.cc/b/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"

直接看 log 檔的話很難看出有什麼問題,必需使用分析工具才行
例如使用 GoAccess 來分析 Apache 的 log 檔
[圖]


官網: https://goaccess.io/
安裝環境: CentOS 7, RockyLinux 9


# 安裝 GoAccess

使用 yum 安裝 GoAccess
$ sudo yum install goaccess
CentOS 7 會安裝 goaccess.x86_64 0:1.3-1.el7
RockyLinux 9 會安裝 goaccess-1.8.1-1.el9.x86_64

查看安裝的版本
$ goaccess -V
GoAccess - 1.3.
For more details visit: http://goaccess.io
Copyright (C) 2009-2016 by Gerardo Orellana

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

目前官網上的版本為 1.8 版,若是用 yum 安裝的版本為 1.3 太舊了會有點問題,可以改用官網下載安裝,而且查詢 IP 所在地的 GeoIP legacy 已停止更新了,要換成新的 GeoIP mmdb,所以先移除 yum 安裝的
$ sudo yum remove goaccess

如果要使用 --enable-geoip=mmdb
需要先安裝 https://github.com/maxmind/libmaxminddb
點右邊的 Releases,複製新版的 libmaxminddb 下載連結
$ cd /usr/local/src
$ sudo wget https://github.com/maxmind/libmaxminddb/releases/download/1.8.0/libmaxminddb-1.8.0.tar.gz
$ sudo tar -zxvf libmaxminddb-1.8.0.tar.gz
$ cd libmaxminddb-1.8.0
$ sudo ./configure
$ sudo make
$ sudo make install
$ sudo sh -c "echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf"
$ sudo ldconfig

下載安裝 GoAccess
$ cd /usr/local/src
$ sudo wget https://tar.goaccess.io/goaccess-1.8.1.tar.gz
$ sudo tar -xzvf goaccess-1.8.1.tar.gz
$ cd goaccess-1.8.1/
$ sudo ./configure --enable-utf8 --enable-geoip=mmdb --with-getline --with-openssl
$ sudo make
$ sudo make install

執行檔會安裝在 /usr/local/bin/goaccess,若沒有在執行路徑的話,加個軟連結
$ ln -s /usr/local/bin/goaccess /usr/bin/goaccess

檢查版本
$ goaccess -V
GoAccess - 1.8.1.
For more details visit: https://goaccess.io/
Copyright (C) 2009-2023 by Gerardo Orellana

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


# 安裝 GeoIP

GeoIP 是 MaxMind 提供的 IP 所在地資料庫,只要註冊帳號就可以使用免費版的 GeoLite2,每段時間會固定更新資料,可以手動下載或是安裝自動更新程式

資料庫有三種,ASN 是公司單位名稱,City 是國家與城市,Country 是只有國家

要手動下載的話,到 MaxMind 登入後下載  GeoLite2 City (.mmdb)
https://www.maxmind.com/en/accounts/current/geoip/downloads
[圖]
 

下載到 /usr/local/share 後解壓縮
$ sudo tar -zxvf GeoLite2-City_20231121.tar.gz
更改資料夾名稱為 GeoIP
$ sudo mv GeoLite2-City_20231121 GeoIP
資料庫檔案的路徑為 /usr/local/share/GeoIP/GeoLite2-City.mmdb


要使用自動更新的話,到 MaxMind 登入後取得 license-key
https://www.maxmind.com/en/accounts/925914/license-key/
[圖]
 

點「Generate new license key」後,取個名字,例如「GoAccess」,點「Confirm」
[圖]
 

複製 Account ID 和 License key,或是點「Download Config」下載設定檔
[圖]
 

下載新版的 geoipupdate,到 GitHub 的 Releases 頁
https://github.com/maxmind/geoipupdate/releases
複製新版的 rpm 下載連結,例如目前是 geoipupdate_6.0.0_linux_amd64.rpm

安裝 geoipupdate
$ cd /usr/local/src
$ sudo wget https://github.com/maxmind/geoipupdate/releases/download/v6.0.0/geoipupdate_6.0.0_linux_amd64.rpm
$ sudo rpm -Uvhi geoipupdate_6.0.0_linux_amd64.rpm

修改設定檔,輸入取得的 Account ID 和 License key
$ sudo vim /etc/GeoIP.conf
AccountID YOUR_ACCOUNT_ID_HERE
LicenseKey YOUR_LICENSE_KEY_HERE
EditionIDs GeoLite2-ASN GeoLite2-City GeoLite2-Country

執行自動更新
$ sudo geoipupdate -v
其中 -v 代表顯示更新過程
資料庫檔案的路徑為 /usr/share/GeoIP/GeoLite2-City.mmdb

設定一週更新一次,例如每週1的2:34更新
$ vim /etc/crontab
34 2  * * 1   root /usr/bin/geoipupdate


# 執行流量分析

執行 goaccess 輸入要分析的 log 檔
$ goaccess access.log

選擇記錄檔的格式,若是 Apache 設定中 CustomLog 是使用預設的 combined,
例如: CustomLog "/var/log/httpd/access.log" combined
那就選第一個 NCSA Combined 就好,按空白鍵選擇後按 Enter
[圖]
 

等待分析完成
[Parsing access.log] {1,080,211} @ {30,863/s}

執行的畫面,會即時更新新增的記錄
[圖]
 

按上下鍵或數字鍵可切換到其他項目,按o或→看詳細資料,按 q 離開
要到第10項之後的話按 shift+數字鍵,例如 shift+1 會跳到第11項
1 - Unique visitors per day - Including spiders 每天有多少不同的IP造訪,包含爬蟲
2 - Requested Files (URLs) 瀏覽的動態網頁
3 - Static Requests 瀏覽的靜態網頁
4 - Not Found URLs (404s) 找不到的網頁
5 - Visitor Hostnames and IPs 造訪量最高的 IP
6 - Operating Systems 使用的作業系統
7 - Browsers 使用的瀏覽器
8 - Time Distribution 依每個小時(00~23)的流量分佈
10 - Referrers URLs 來源網址
11 - Referring Sites 來源網站
12 - Keyphrases from Google's search engine 搜尋來的關鍵字
13 - HTTP Status Codes 狀態碼
16 - Geo Location 來源的地區
17 - ASN 來源的自治系統編號與自治組織的名稱

要排除搜尋引擎爬蟲的話
$ goaccess access.log --ignore-crawlers

可以一次輸入多個 log 檔
$ goaccess access.log*
[圖]
 

如果 log 檔被壓縮成 gz 檔,可以改用
$ zcat access.log.*.gz | goaccess

有指定 GeoLite2-City.mmdb 的話,第5項的詳細畫面會顯示 IP 的國家、地區
$ goaccess access.log --geoip-database /usr/local/share/GeoIP/GeoLite2-City.mmdb
[圖]
 

有指定 GeoLite2-ASN.mmdb 的話,第5項的詳細畫面會顯示自治組織
$ goaccess access.log --geoip-database /usr/share/GeoIP/GeoLite2-ASN.mmdb
[圖]
 

同時指定 ASN 和 City 就可以都顯示,但會增加分析時間
$ goaccess access.log --geoip-database /usr/share/GeoIP/GeoLite2-ASN.mmdb --geoip-database /usr/share/GeoIP/GeoLite2-City.mmdb
[圖]
 

有指定 GeoLite2-ASN.mmdb 的話,第17項會統計來源的自治組織 (Autonomous System, AS),通常每個 AS 都是由單一大型組織運作,例如網際網路服務提供者 (ISP)、大型企業科技公司、大學或政府機構。
[圖]
 

有加 -a 參數的話,會產生每個 IP 的 user-agent 列表,但會增加分析的時間
$ goaccess access.log -a
在第5項的詳細資料,用 j k 上下選擇 IP 後按 Enter
[圖]
 

第5項很適合用來抓造成異常流量的爬蟲機器人,例如下圖,發現一個香港的 IP 流量特別高,而且每秒就新增好幾個連線,打開記錄檔搜尋這個 IP 看看都在抓什麼,確認不是正常的連線後,就可以在防火牆將他擋掉
[圖]
 


# 產生網頁檔的流量分析

$ goaccess access.log* -o /var/www/html/report.html --log-format=COMBINED

想要即時更新的話加上 --real-time-html

用瀏覽器打開來看
[圖]
 

最多造訪次數的IP資料,有加參數 -a 的話可以按左邊的箭頭顯示 user-agent 清單
[圖]
 


# 修改設定檔

執行 $ goaccess --dcf 查詢設定檔的位置
/etc/goaccess/goaccess.conf
使用編譯安裝的話會在
/usr/local/etc/goaccess/goaccess.conf

編輯設定檔
$ sudo vim /usr/local/etc/goaccess/goaccess.conf
# 預設的時間格式
# Time Format Options (required)
# Apache/NGINX's log formats below.
#time-format %H:%M:%S
time-format %H:%M:%S

# 預設的時期格式
# Date Format Options (required)
# Apache/NGINX's log formats below.
#date-format %d/%b/%Y
date-format %d/%b/%Y

# 設定預設的 Log 檔格式,之後就不用再輸入 --log-format
# Log Format Options (required)
# NCSA Combined Log Format
#log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"

#第10和12項沒顯示的話,把這兩行註解掉
#ignore-panel REFERRERS
#ignore-panel KEYPHRASES

#設定下載的 GeoIP 資料庫檔案路徑,之後不用再指定 --geoip-database
#geoip-database /usr/local/share/GeoIP/GeoLiteCity.dat
geoip-database /usr/share/GeoIP/GeoLite2-City.mmdb

#想要預設顯示自治組織可以再加這個,但會增加分析時間
#geoip-database /usr/share/GeoIP/GeoLite2-ASN.mmdb


錯誤解決記錄:

GoAccess 1.3 版 修改設定檔無效,執行 $ goaccess --dcf 會顯示
No default config file found.
也無法使用 -p 指定設定檔路徑
→ 自行安裝新的 1.8 版

編譯安裝時出現錯誤訊息 goaccess Missing development files for libmaxminddb library.
→ 要先安裝 https://github.com/maxmind/libmaxminddb

出現錯誤訊息 error while loading shared libraries: libmaxminddb.so.0: cannot open shared object file: No such file or directory
→ 安裝 libmaxminddb 後,要執行
$ sudo sh -c "echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf"
$ ldconfig


相關文章:
[CentOS7] Apache 安裝與設定 - KnucklesNote板 - Disp BBS
[RockyLinux9] 網頁伺服器 Apache 安裝與設定 - KnucklesNote板 - Disp BBS
[Apache] log分析工具 AWStats - KnucklesNote板 - Disp BBS
[CentOS7] 設定 Apache mod_remoteip 修正 NodeBalancer 的IP - KnucklesNote板 - Disp BBS

參考:
GoAccess 官網文件
https://www.digitalocean.com/communi...b-log-analyzer-on-ubuntu-20-04



--
※ 作者: Knuckles 時間: 2023-11-24 22:48:03 (台灣)
※ 編輯: Knuckles 時間: 2023-11-26 04:09:14 (台灣)
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 66 
分享網址: 複製 已複製
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇