资讯详情

Squid 代理服务之日志分析 --- sarg 软件的安装及应用

文章目录

  • 1. sarg 简述
  • 2. 安装图像处理软件包
  • 3. 编译安装源码 sarg
  • 4. 修改配置文件 /etc/sarg/sarg.conf
  • 5. 创建不计入站点文件,添加的域名将不被显示在排序中
  • 6. 创建软链接到 PATH 路径
  • 7. 安装 apache 并生成 sarg
  • 8. 浏览器访问查看
  • 9. 增加计划任务,执行每日生成报告
    • 9.1 编写执行脚本
    • 9.2 添加计划任务


1. sarg 简述

??sarg(Squid Analysis Report Generator),是一款 squid 采用日志分析工具 HTML 格式详细列出每个用户访问 Internet 网站信息、时间占用信息、排名、连接次数、访问量等。

2. 安装图像处理软件包

yum -y install pcre-devel gd gd-devel 

3. 编译安装源码 sarg

以下是软件下载方式:

wget http://101.34.22.188/squid/sarg/sarg-2.3.7.tar.gz -P /opt

[root@squid_server ~]# mkdir /usr/local/sarg #创建软件安装目录 [root@squid_server ~]# cd /opt [root@squid_server opt]# rz -E #传入sarg软件安装包 rz waiting to receive. [root@squid_server opt]# tar zxvf sarg-2.3.7.tar.gz -C /opt [root@squid_server opt]# cd sarg-2.3.7/ [root@squid_server sarg-2.3.7]# ./configure \ > --prefix=/usr/local/sarg \      #软件安装目录 > --sysconfdir=/etc/sarg \        #配置文件目录,默认为/usr/local/etc > --enable-extraprotection        #额外的安全防护 [root@squid_server sarg-2.3.7]# make -j 2 && make install 

4. 修改配置文件 /etc/sarg/sarg.conf

[root@squid_server sarg-2.3.7]# vim /etc/sarg/sarg.conf   ##7行,取消注释 access_log /usr/local/squid/var/logs/access.log    ##指定访问日志文件 ##25行取消注释(并修改) title "Squid User Access Reports"                  ###网页标题可以自定义修改 ##120行取消注释(并修改) output_dir /var/www/html/sarg                      ##报告输出目录可自定义修改 ##178行取消注释 user_ip no                                         ##显示用户名 ##184行取消注释并修改 topuser_sort_field connect reverse                 ##top在排序中,指定的连接次数采用降序排列,升序为normal ##190行取消注释并修改 user_sort_field connect reverse                    ##对于用户访问记录,连接次数按降序排列 ##206行取消注释并修改 exclude_hosts /usr/local/sarg/noreport             ##指定不计入排名的网站列表的文件,必须自行创建,必须有文件(可以是空文件),否则会报错 ##257行取消注释 overwrite_report no                                ##同名同日期日志是否覆盖 ##289行取消注释并修改 mail_utility mailq.postfix                         ##发邮件报告命令 ##434行取消注释并修改 charset UTF-8                                      ##指定字符集UTF-8 ##51行,取消注释
weekdays 0-6                                       ##top排行的周期周期,0表示周日
##525行,取消注释
hours 0-23                                         ##top排行的时间周期
##633行,取消注释(并修改)
www_document_root /var/www/html                    ##指定网页根目录,务必为当前网页服务的根目录

5. 创建不计入站点文件,添加的域名将不被显示在排序中

[root@squid_server sarg-2.3.7]# cd /usr/local/sarg/
[root@squid_server sarg]# ls
bin  share
[root@squid_server sarg]# touch noreport

6. 创建软链接到 PATH 路径

[root@squid_server sarg]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin

7. 安装 apache 并生成 sarg

[root@squid_server sarg]# yum install -y httpd
[root@squid_server sarg]# systemctl start httpd
[root@squid_server sarg]# netstat -natp | grep 'httpd'
tcp6       0      0 :::80                   :::*                    LISTEN      4808/httpd 
[root@squid_server sarg]# sarg
SARG: 纪录在文件: 19, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/sarg/2021Sep09-2021Sep10

8. 浏览器访问查看

在这里插入图片描述

9. 添加计划任务,执行每天生成报告

9.1 编写执行脚本

[root@squid_server sarg]# vim /usr/local/sarg/report.sh

#!/bin/bash

#Get current date
TODAY=$(date +%d/%/%m/%Y)
#Get yesterday date
YESTERDAY=$(date -d '-1 day' +%d/%m/%Y)

#Create daily_report
/usr/local/sarg/bin/sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/sarg -z -d $YESTERDAY-$TODAY &> /dev/null

#Delete old directory which mtime>30
find /var/www/html/sarg -type d -a -name '20*' -a -mtime +30 -exec rm -rf { 
        } \;

exit 0

[root@squid_server sarg]# chmod +x /usr/local/sarg/report.sh
#赋予脚本执行权限

9.2 添加计划任务

[root@squid_server sarg]# crontab -e

##分时日月周,添加计划性任务
0 0 * * * /usr/local/sarg/report.sh

[root@squid_server sarg]# crontab -l
##查看计划性任务列表
0 0 * * * /usr/local/sarg/report.sh
[root@squid_server sarg]# systemctl status crond
##查看crond服务状态,需保持开启状态
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2021-09-10 07:04:56 CST; 15s ago
 Main PID: 11502 (crond)
   CGroup: /system.slice/crond.service
           └─11502 /usr/sbin/crond -n

9月 10 07:04:56 squid_server systemd[1]: Started Command Scheduler.
9月 10 07:04:56 squid_server systemd[1]: Starting Command Scheduler...
9月 10 07:04:56 squid_server crond[11502]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 33% if used.)
9月 10 07:04:56 squid_server crond[11502]: (CRON) INFO (running with inotify support)
9月 10 07:04:56 squid_server crond[11502]: (CRON) INFO (@reboot jobs will be run at computer's startup.)

标签: 5代6代连接器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台