SARG是一个开源工具,允许你分析Squid日志文件,并生成漂亮的报告以HTML格式,其中包含有关用户、IP地址、访问量最高的站点、总带宽使用情况、经过时间、下载、访问被拒绝的网站、每日报告、每周报告和每月报告的信息。
这个SARG工具非常方便,可以查看网络中每台机器使用了多少互联网带宽,并可以监视网络用户访问了哪些网站。

在本文中,我将指导你如何在RHEL/CentOS/Fedora和Debian/Ubuntu/Linux Mint系统上安装和配置SARG – Squid Analysis Report Generator。
在Linux中安装Sarg – Squid日志分析器
I assume that you already installed, configured and tested Squid server as a transparent proxy and DNS for the name resolution in caching mode. If not, please install and configure them first before moving further installation of Sarg.
重要提示:请记住,没有Squid和DNS设置,安装sarg在系统上毫无用处。在继续安装Sarg之前,请先安装它们。
按照以下指南在Linux系统中安装DNS和Squid:
安装仅缓存DNS服务器
安装透明代理的Squid
步骤1:从源代码安装Sarg
默认情况下,‘sarg’软件包在RedHat系发行版中不包含,因此我们需要手动从源代码tarball编译并安装它。为此,在编译之前,我们需要在系统上安装一些额外的先决软件包。
在RedHat/CentOS/Fedora
# yum install –y gcc gd gd-devel make perl-GD wget httpd
安装完所有所需软件包后,下载最新的sarg源代码tarball,或者您可以使用以下wget命令下载并安装,如下所示。
# wget http://liquidtelecom.dl.sourceforge.net/project/sarg/sarg/sarg-2.3.10/sarg-2.3.10.tar.gz # tar -xvzf sarg-2.3.10.tar.gz # cd sarg-2.3.10 # ./configure # make # make install
在Debian/Ubuntu/Linux Mint
在基于Debian的发行版上,可以使用apt-get软件包管理器轻松安装sarg软件包。
$ sudo apt-get install sarg
第2步:配置Sarg
现在是时候编辑SARG主配置文件的一些参数了。该文件包含许多可编辑选项,但我们只会编辑必需的参数,如:
- 访问日志路径
- 输出目录
- 日期格式
- 相同日期的覆盖报告。
使用您选择的编辑器打开sarg.conf文件并按照以下所示进行更改。
# vi /usr/local/etc/sarg.conf [On RedHat based systems]
$ sudo nano /etc/sarg/sarg.conf [On Debian based systems]
现在取消注释并添加原始路径到您的squid访问日志文件。
# sarg.conf # # TAG: access_log file # Where is the access.log file # sarg -l file # access_log /var/log/squid/access.log
接下来,添加正确的输出目录路径以在该目录中保存生成的squid报告。请注意,在基于Debian的发行版中,Apache的Web根目录是‘/var/www‘。因此,请在您的Linux发行版下添加正确的Web根路径时要小心。
# TAG: output_dir # The reports will be saved in that directory # sarg -o dir # output_dir /var/www/html/squid-reports
设置报告的正确日期格式。例如,“date_format e”将以‘dd/mm/yy’格式显示报告。
# TAG: date_format # Date format in reports: e (European=dd/mm/yy), u (American=mm/dd/yy), w (Weekly=yy.ww) # date_format e
接下来,取消注释并将覆盖报告设置为“是”。
# TAG: overwrite_report yes|no # yes - if report date already exist then will be overwritten. # no - if report date already exist then will be renamed to filename.n, filename.n+1 # overwrite_report yes
就这样!保存并关闭文件。
第3步:生成Sarg报告
完成配置部分后,现在是时候使用以下命令生成squid日志报告了。
# sarg -x [On RedHat based systems]
# sudo sarg -x [On Debian based systems]
示例输出
[root@localhost squid]# sarg -x SARG: Init SARG: Loading configuration from /usr/local/etc/sarg.conf SARG: Deleting temporary directory "/tmp/sarg" SARG: Parameters: SARG: Hostname or IP address (-a) = SARG: Useragent log (-b) = SARG: Exclude file (-c) = SARG: Date from-until (-d) = SARG: Email address to send reports (-e) = SARG: Config file (-f) = /usr/local/etc/sarg.conf SARG: Date format (-g) = USA (mm/dd/yyyy) SARG: IP report (-i) = No SARG: Keep temporary files (-k) = No SARG: Input log (-l) = /var/log/squid/access.log SARG: Resolve IP Address (-n) = No SARG: Output dir (-o) = /var/www/html/squid-reports/ SARG: Use Ip Address instead of userid (-p) = No SARG: Accessed site (-s) = SARG: Time (-t) = SARG: User (-u) = SARG: Temporary dir (-w) = /tmp/sarg SARG: Debug messages (-x) = Yes SARG: Process messages (-z) = No SARG: Previous reports to keep (--lastlog) = 0 SARG: SARG: sarg version: 2.3.7 May-30-2013 SARG: Reading access log file: /var/log/squid/access.log SARG: Records in file: 355859, reading: 100.00% SARG: Records read: 355859, written: 355859, excluded: 0 SARG: Squid log format SARG: Period: 2014 Jan 21 SARG: Sorting log /tmp/sarg/172_16_16_55.user_unsort ......
注意:‘sarg -x’命令将读取‘sarg.conf’配置文件,并获取Squid‘access.log’路径,生成一个HTML格式的报告。
第4步:评估Sarg报告
生成的报告放置在‘/var/www/html/squid-reports/’或‘/var/www/squid-reports/’下,可以通过浏览器地址访问。
http://localhost/squid-reports OR http://ip-address/squid-reports
Sarg主窗口

特定日期

用户报告

访问最多的网站

最常访问的网站和用户

最常下载的文件

拒绝访问

认证失败

第5步:自动生成Sarg报告
通过cron作业自动在给定时间段内生成sarg报告的过程。例如,假设您想要自动按小时生成报告,为此,您需要配置一个Cron作业。
# crontab -e
接下来,在文件底部添加以下行。保存并关闭。
* */1 * * * /usr/local/bin/sarg -x
上述Cron规则将每1小时生成一个SARG报告。
参考链接
就是这样了,SARG!我将会发布更多有关Linux的有趣文章,届时请继续关注TecMint.com,别忘了留下你宝贵的评论。
Source:
https://www.tecmint.com/sarg-squid-analysis-report-generator-and-internet-bandwidth-monitoring-tool/