SARG 是一款網路型工具,專門分析 Squid 記錄,並以清楚易讀的方式呈現結果。系統管理者可以用它看哪些網站被造訪、追蹤流量最大的網站與使用者。本文涵蓋完整的 SARG 設定流程。
問題在於,原始 Squid 記錄並不好讀。比如這一坨,你怎麼看出時間戳與那些數字的意思?
1404788984.429 1162 172.17.1.23 TCP_MISS/302 436 GET https://facebook.com/ - DIRECT/173.252.110.27 text/html
1404788985.046 12416 172.17.1.23 TCP_MISS/200 4169 CONNECT stats.pusher.com:443 - DIRECT/173.255.223.127 -
1404788986.124 174 172.17.1.23 TCP_MISS/200 955 POST https://ocsp.digicert.com/ - DIRECT/117.18.237.29 application/ocsp-response
接下來開始動手。
# yum install gcc make wget httpd crond
# service httpd start; service crond start
# chkconfig httpd on; chkconfig crond on
# wget https://downloads.sourceforge.net/project/sarg/sarg/sarg-2.3.8/sarg-2.3.8.tar.gz
# tar zxvf sarg-2.3.8.tar.gz
# cd sarg-2.3.8
注意:64 位元 Linux 需要對 log.c 打個補丁:
1506c1506
< if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%"PRIi64"\t%s\t%ld\t%s\n",dia,hora,ip,url,nbytes,code,elap_time,smartfilter)<=0) {
---
> if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%"PRIi64"\t%s\t%ld\t%s\n",dia,hora,ip,url,(int64_t)nbytes,code,elap_time,smartfilter)<=0) {
1513c1513
< fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%"PRIi64"\t%s\t%ld\t%s\n",dia,hora,user,ip,url,nbytes,code,elap_time,smartfilter);
---
> fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%"PRIi64"\t%s\t%ld\t%s\n",dia,hora,user,ip,url,(int64_t)nbytes,code,elap_time,smartfilter);
1564c1564
< printf("LEN=\t%"PRIi64"\n",nbytes);
---
> printf("LEN=\t%"PRIi64"\n",(int64_t)nbytes);
# ./configure
# make
# make install
# vim /usr/local/etc/sarg.conf
access_log /var/log/squid/access.log
temporary_dir /tmp
output_dir /var/www/html/squid-reports
date_format e ## 使用歐洲 DD-MM-YYYY 格式
## 同一天/週/月只留一份報告
overwrite_report yes
# sarg -x
用除錯模式先跑一次確認沒有錯誤。若一切正常,SARG 會解析 Squid 記錄,並在 /var/www/html/squid-reports 下產生報告,可透過 https://<伺服器IP>/squid-reports/ 在瀏覽器中查看。
時間範圍用「-d」指定,值的形式為 day-n、week-n 或 month-n,n 代表往前推的天/週/月數。例如 week-1 會產生前一週的報告,day-2 則是前兩天。
實務上通常掛成每日排程:
# vim /etc/cron.daily/sarg
#!/bin/sh
/usr/local/bin/sarg -d day-1
記得加上執行權限:
# chmod 755 /usr/local/bin/sarg
這樣 SARG 就會每天自動產出 Squid 流量報告,透過網頁介面即可查閱。你也可以進一步自訂設定以符合自身需求。