超详细搭建Squid日志分析

Squid日志分析概述

Sarg的全名是Squid Analysis ReportGenerator,是一款Squid日志分析工具,采用HTML格式,详细列出每一位用户访问Internet的站点信息、时间占用信息、排名、连接次数和访问量等

实验环境

  • 系统环境:centos7.4
  • 服务器IP地址:192.168.100.71
  • 相关源码信息:sarg-2.3.7、squid-3.4.6

搭建步骤

一、安装Squid服务

没有安装squid服务的朋友可以查看我之前的博文 http://blog.51cto.com/11905606/2177108

二、Squid日志分析

1、安装web服务

1)、安装httpd软件包

[[email protected] ~]# yum -y install httpd

2)、修改配置文件

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf

#编辑以下参数
ServerName www.bt.com:80  #设置完全主机名,根据自己的需求
Listen 192.168.100.71:80  #设置服务监听端口

3)、启动服务

[[email protected] ~]# systemctl start httpd.service
[[email protected] ~]# netstat -anpt | grep ‘:80‘

超详细搭建Squid日志分析

2、配置Sarg分析Squid日志

1)、安装GD库

[[email protected] ~]# yum -y install gd gd-devel

2)、安装sarg

[[email protected] ~]# tar xvfz sarg-2.3.7.tar.gz
[[email protected] ~]# cd sarg-2.3.7/
[[email protected] sarg-2.3.7]# ./configure \
--prefix=/usr/local/sarg \
--sysconfdir=/etc/sarg \
--enable-extraprotection

#参数详解
prefix                 #指定配置文件安装路径
sysconfdir             #配置文件目录
enable-extraprotection #安全保护功能

[[email protected] sarg-2.3.7]# make && make install

3)、修改sarg配置文件,添加要监控的squid的日志信息等

[[email protected] ~]# vim /etc/sarg/sarg.conf

#修改以下参数 
access_log /usr/local/squid/var/logs/access.log    #约第7行,  打开squid访问日志
title "Squid User Access Reports"                  #约第25行, 打开网页标题
output_dir /var/www/html/squid-reports             #约第120行,sarg报告输出目录
user_ip no                                         #约第178行,使用用户名显示,no代表禁用
exclude_hosts /usr/local/sarg/noreport             #约第206行,指定不计入排序的站点列表文件
topuser_sort_field BYTES reverse                   #约第184行,在top排序中,指定连接次数、访问字节数,reverse代表降序
user_sort_field BYTES reverse                      #约第190行,对于用户访问记录,连接次数按降序排列
overwrite_report no                                #约第257行,当日期报告已经存在,是否覆盖报告
mail_utility mailx                                 #约第289行,发送邮件报告的命令
charset UTF-8                                      #约第434行,使用字符集,utf-8
weekdays 0-6                                       #约第518行,指定top排序时的星期周期,0为周日
hours 7-12,14,16,18-20                             #约第523行,指定top排序时的时间周期
www_document_root /var/www/html          #约第633行,网页根目录

[[email protected] ~]# touch /usr/local/sarg/noreport #创建不计入排序的站点列表文件

4)、启动sarg并对观察squid的访问日志

[[email protected] ~]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin #添加链接
[[email protected] ~]# sarg #启动sarg

SARG: 纪录在文件: 53,reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2018Sep19-2018Sep19

5)、测试

输入 http://192.168.100.103/squid-reports/

超详细搭建Squid日志分析

超详细搭建Squid日志分析

6、引入周期性计划任务

1).编辑每日报告脚本

[[email protected] ~]# cd /usr/local/sarg/
[[email protected] sarg]# vim sarg.sh
00 00 * sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/squid-reports/ -z -d $(date -d "1 day ago" +%d/%m/%Y)-$(date +%d/%m/%Y)

2).添加周期性计划任务

[[email protected] ~]# chmod +x /usr/local/sarg/sarg.sh
[[email protected] ~]# crontab -e

00 00 * * * /usr/local/sarg/sarg.sh   #每天00:00执行

[[email protected] ~]# systemctl enable crond #设为开机自启动

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native