如何用CentOS上的SARG日志分析器来分析Squid日志

62950ababa12d169612eaf027ad088a9 的头像

·

·

·

8,176 次阅读

上一节教程中,我们展示了如何在CentOS上使用Squid配置透明代理。Squid提供了很多有用的特性,但是分析一个原始Squid日志文件并不直接。比如,你如何分析下面Squid日志中的时间戳和数字?

1404788984.429   1162 172.17.1.23 TCP_MISS/302 436 GET http://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 http://ocsp.digicert.com/ - DIRECT/117.18.237.29 application/ocsp-response
1404788989.738    342 172.17.1.23 TCP_MISS/200 3890 CONNECT www.google.com:443 - DIRECT/74.125.200.106 -
1404788989.757    226 172.17.1.23 TCP_MISS/200 942 POST http://clients1.google.com/ocsp - DIRECT/74.125.200.113 application/ocsp-response
1404788990.839   3939 172.17.1.23 TCP_MISS/200 78944 CONNECT fbstatic-a.akamaihd.net:443 - DIRECT/184.26.162.35 -
1404788990.846   2148 172.17.1.23 TCP_MISS/200 118947 CONNECT fbstatic-a.akamaihd.net:443 - DIRECT/184.26.162.35 -
1404788990.849   2151 172.17.1.23 TCP_MISS/200 76809 CONNECT fbstatic-a.akamaihd.net:443 - DIRECT/184.26.162.35 -
1404788991.140    611 172.17.1.23 TCP_MISS/200 110073 CONNECT fbstatic-a.akamaihd.net:443 - DIRECT/184.26.162.35 –

我们使用yum来安装安装必要的依赖。

# yum install gcc make wget httpd crond 

在启动时加载必要的服务

# service httpd start; service crond start
# chkconfig httpd on; chkconfig crond on 

现在我们下载并解压SARG

# wget http://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, "%st%st%st%st%"PRIi64"t%st%ldt%sn",dia,hora,ip,url,nbytes,code,elap_time,smartfilter)<=0) {

>                fprintf(fp_log, "%st%st%st%st%st%"PRIi64"t%st%ldt%sn",dia,hora,user,ip,url,(int64_t)nbytes,code,elap_time,smartfilter);
1564c1564
<                printf("LEN=t%"PRIi64"n",nbytes);

access_log /var/log/squid/access.log
temporary_dir /tmp
output_dir /var/www/html/squid-reports
date_format e ## We use Europian DD-MM-YYYY format here ##

we don’t want multiple reports for single day/week/month

overwrite_report yes


现在是时候测试运行了,我们用调试模式运行sarg来查看是否存在错误。

sarg -x


如果i一切正常,sarg会根系Squid日志,并在/var/www/html/squid-reports下创建报告。报告也可以在浏览器中通过地址http://<服务器IP>/squid-reports/访问。

[![](https://camo.githubusercontent.com/5f3e90eaa7c9f09de466633855fa7e6ea3e0c03c/68747470733a2f2f6661726d332e737461746963666c69636b722e636f6d2f323933312f31343633313430333933355f346666333465303762395f7a2e6a7067)](https://camo.githubusercontent.com/5f3e90eaa7c9f09de466633855fa7e6ea3e0c03c/68747470733a2f2f6661726d332e737461746963666c69636b722e636f6d2f323933312f31343633313430333933355f346666333465303762395f7a2e6a7067)

[![](https://camo.githubusercontent.com/73b63de6bf943e4082f4ac9372cee93a5ceb8090/68747470733a2f2f6661726d342e737461746963666c69636b722e636f6d2f333930312f31343632393238313634345f666563333130313463355f7a2e6a7067)](https://camo.githubusercontent.com/73b63de6bf943e4082f4ac9372cee93a5ceb8090/68747470733a2f2f6661726d342e737461746963666c69636b722e636f6d2f333930312f31343632393238313634345f666563333130313463355f7a2e6a7067)

、SARG可以用于创建日、周、月度报告。时间范围用“-d”参数来指定,值的形式很可能为day-n、 week-n 或者 month-n,n的值为向前推移的天/周/月的数量。比如,使用week-1,SARG会生成之前一星期的报告。使用day-2,SARG会生成之前两天的报告。

作为演示,我们会准备一个计划任务来每天运行SARG。

vim /etc/cron.daily/sarg



[Sarmed Rahman](http://amar-linux.blogspot.com/)

* [Twitter 地址](http://twitter.com/SarmedRahman)
* [LinkedIn 地址](http://www.linkedin.com/in/sarmedrahman)

Sarmed Rahman是一名孟加拉国的IT专业人士。他坚持时不时地写作技术文章,并坚信技术可以通过分享而提高。在空闲时间里,他喜欢打游戏,喜欢和朋友一起度过。

---

via: <http://xmodulo.com/2014/07/analyze-squid-logs-sarg-log-analyzer-centos.html>

译者:[geekpi](https://github.com/geekpi) 校对:[Caroline](https://github.com/carolinewuyan)

本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注