将多个Apache访问日志合并到一个整体访问日志中
版本1.0
作者:Falko Timme
假设您有一个运行Apache节点群集的Web应用程序。 每个节点都生成自己的Apache访问日志,您可以使用Webalizer或AWStats等工具生成页面浏览统计信息。 显然,您不希望每个Apache节点都有页面浏览统计信息,而不是整个页面浏览统计信息。 为了实现这一点,我们必须将每个节点的访问日志合并到一个整体访问日志中,然后我们可以将其提供给Webalizer或AWstats。 有一个名为logresolvemerge.pl
(AWStats包的一部分)的Perl脚本可以为我们做到这一点。
我不会保证这将为您工作!
1初步说明
我已经在Debian系统上进行了测试,但除了软件包安装之外,其他发行版的过程是相同的。 使用发行版的软件包管理器(例如apt,yum,yast,urpmi)来安装软件包。
我假设您正在使用单个主机(通常这是运行Webalizer或AWStats以生成统计信息的主机)从Apache节点收集访问日志(我不介绍如何将访问日志从Apache节点到我们收集日志的主机 - 您可以使用rsync,例如,如本教程所示: 使用rsync镜像您的网站 ) - 我在这里使用目录/ var / log / webcluster
存储Apache节点的访问日志。
2安装logresolvemerge.pl
如前所述,logresolvemerge.pl是AWStats软件包的一部分,所以我们现在安装(即使你不想使用它 - 如果你喜欢,你仍然可以使用Webalizer):
apt-get install awstats
logresolvemerge.pl现在位于/ usr / share / doc / awstats / examples
目录中。 我们把它移到/ usr / local / bin
,这样我们的路径是:
mv /usr/share/doc/awstats/examples/logresolvemerge.pl /usr/local/bin
(如果您找不到您的logresolvemerge.pl,您可以这样搜索:
updatedb
locate logresolvemerge.pl
然后将其移动到/ usr / local / bin
。)
3使用logresolvemerge.pl
假设我们在/ var / log / webcluster
, / var / log / webcluster / access_log_server1
和/ var / log / webcluster / access_log_server2中
有两个访问日志。
我在这里使用两个示例访问日志来演示logresolvemerge.pl如何工作。 两个访问日志都包含相同的数据用于演示,但是请求的日期在server1和server2上不同一秒。 在将这两个日志与logresolvemerge.pl合并后,整个访问日志应包含按时间顺序的请求,以便Webalizer和AWStats可以正确处理它们。
/ var / log / webcluster / access_log_server1
如下所示:
cat /var/log/webcluster/access_log_server1
192.6.178.101 - - [17/Oct/2007:18:50:27 +0200] "GET /themes/htf_glass/images/header_tab6.png HTTP/1.0" 200 7434 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 72.149.148.248 - - [17/Oct/2007:18:50:29 +0200] "GET /misc/menu-leaf.png HTTP/1.1" 200 108 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" 24.8.231.74 - - [17/Oct/2007:18:50:31 +0200] "GET /forums/showthread.php?t=15338 HTTP/1.0" 200 59342 "" "Wget/1.10.2" 24.127.251.14 - - [17/Oct/2007:18:50:33 +0200] "POST /mailgust/index.php HTTP/1.1" 200 1662 "" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)" 72.149.148.248 - - [17/Oct/2007:18:50:35 +0200] "GET /images/print.gif HTTP/1.1" 200 217 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" 192.6.178.101 - - [17/Oct/2007:18:50:37 +0200] "GET /themes/htf_glass/images/youcl_logo_trans.gif HTTP/1.0" 200 184 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 76.22.105.74 - - [17/Oct/2007:18:50:39 +0200] "GET /themes/htf_glass/images/search_small.gif HTTP/1.1" 200 1367 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Debian)" 192.6.178.101 - - [17/Oct/2007:18:50:41 +0200] "GET /themes/htf_glass/images/join_small.gif HTTP/1.0" 200 1212 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 192.6.178.101 - - [17/Oct/2007:18:50:43 +0200] "GET /forums/clientscript/vbulletin_md5.js HTTP/1.0" 200 9661 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 24.127.251.14 - - [17/Oct/2007:18:50:45 +0200] "GET /mailgust/i/menushadow.gif HTTP/1.1" 200 59 "" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)"
/ var / log / webcluster / access_log_server2
如下所示:
cat /var/log/webcluster/access_log_server2
192.6.178.101 - - [17/Oct/2007:18:50:28 +0200] "GET /themes/htf_glass/images/header_tab6.png HTTP/1.0" 200 7434 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 72.149.148.248 - - [17/Oct/2007:18:50:30 +0200] "GET /misc/menu-leaf.png HTTP/1.1" 200 108 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" 24.8.231.74 - - [17/Oct/2007:18:50:32 +0200] "GET /forums/showthread.php?t=15338 HTTP/1.0" 200 59342 "" "Wget/1.10.2" 24.127.251.14 - - [17/Oct/2007:18:50:34 +0200] "POST /mailgust/index.php HTTP/1.1" 200 1662 "" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)" 72.149.148.248 - - [17/Oct/2007:18:50:36 +0200] "GET /images/print.gif HTTP/1.1" 200 217 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" 192.6.178.101 - - [17/Oct/2007:18:50:38 +0200] "GET /themes/htf_glass/images/youcl_logo_trans.gif HTTP/1.0" 200 184 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 76.22.105.74 - - [17/Oct/2007:18:50:40 +0200] "GET /themes/htf_glass/images/search_small.gif HTTP/1.1" 200 1367 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Debian)" 192.6.178.101 - - [17/Oct/2007:18:50:42 +0200] "GET /themes/htf_glass/images/join_small.gif HTTP/1.0" 200 1212 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 192.6.178.101 - - [17/Oct/2007:18:50:44 +0200] "GET /forums/clientscript/vbulletin_md5.js HTTP/1.0" 200 9661 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 24.127.251.14 - - [17/Oct/2007:18:50:46 +0200] "GET /mailgust/i/menushadow.gif HTTP/1.1" 200 59 "" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)"
要将两个访问日志合并到整个访问日志/ var / log / webcluster / access_log_overall中
,我们运行:
logresolvemerge.pl /var/log/webcluster/access_log_server* > /var/log/webcluster/access_log_overall
如果您收到以下错误消息:
server1:~# logresolvemerge.pl /var/log/webcluster/access_log_server* > /var/log/webcluster/access_log_overall
-bash: /usr/local/bin/logresolvemerge.pl: /usr/bin/perl^M: bad interpreter: No such file or directory
server1:~#
这意味着logresolvemerge.pl包含Windows linebreaks。 要解决这个问题,我们可以使用Debian上的sysutils
软件包的一部分工具dos2unix
将它们转换为Unix linebreaks。 因此,我们现在安装sysutils
包:
apt-get install sysutils
之后,我们修复了logresolvemerge.pl脚本:
dos2unix /usr/local/bin/logresolvemerge.pl /usr/local/bin/logresolvemerge.pl
现在你可以跑
logresolvemerge.pl /var/log/webcluster/access_log_server* > /var/log/webcluster/access_log_overall
再次,这一次应该没有错误。
(如果不想使用通配符,而是指定每个单一访问日志,则可以使用与此相同结果的logresolvemerge.pl:
logresolvemerge.pl /var/log/webcluster/access_log_server1 /var/log/webcluster/access_log_server2 > /var/log/webcluster/access_log_overall
)
现在您应该有一个整体访问日志, / var / log / webcluster / access_log_overall
,它们应该包含/ var / log / webcluster / access_log_server1
和/ var / log / webcluster / access_log_server2的请求
,时间顺序如下:
cat /var/log/webcluster/access_log_overall
192.6.178.101 - - [17/Oct/2007:18:50:27 +0200] "GET /themes/htf_glass/images/header_tab6.png HTTP/1.0" 200 7434 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 192.6.178.101 - - [17/Oct/2007:18:50:28 +0200] "GET /themes/htf_glass/images/header_tab6.png HTTP/1.0" 200 7434 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 72.149.148.248 - - [17/Oct/2007:18:50:29 +0200] "GET /misc/menu-leaf.png HTTP/1.1" 200 108 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" 72.149.148.248 - - [17/Oct/2007:18:50:30 +0200] "GET /misc/menu-leaf.png HTTP/1.1" 200 108 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" 24.8.231.74 - - [17/Oct/2007:18:50:31 +0200] "GET /forums/showthread.php?t=15338 HTTP/1.0" 200 59342 "" "Wget/1.10.2" 24.8.231.74 - - [17/Oct/2007:18:50:32 +0200] "GET /forums/showthread.php?t=15338 HTTP/1.0" 200 59342 "" "Wget/1.10.2" 24.127.251.14 - - [17/Oct/2007:18:50:33 +0200] "POST /mailgust/index.php HTTP/1.1" 200 1662 "" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)" 24.127.251.14 - - [17/Oct/2007:18:50:34 +0200] "POST /mailgust/index.php HTTP/1.1" 200 1662 "" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)" 72.149.148.248 - - [17/Oct/2007:18:50:35 +0200] "GET /images/print.gif HTTP/1.1" 200 217 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" 72.149.148.248 - - [17/Oct/2007:18:50:36 +0200] "GET /images/print.gif HTTP/1.1" 200 217 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" 192.6.178.101 - - [17/Oct/2007:18:50:37 +0200] "GET /themes/htf_glass/images/youcl_logo_trans.gif HTTP/1.0" 200 184 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 192.6.178.101 - - [17/Oct/2007:18:50:38 +0200] "GET /themes/htf_glass/images/youcl_logo_trans.gif HTTP/1.0" 200 184 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 76.22.105.74 - - [17/Oct/2007:18:50:39 +0200] "GET /themes/htf_glass/images/search_small.gif HTTP/1.1" 200 1367 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Debian)" 76.22.105.74 - - [17/Oct/2007:18:50:40 +0200] "GET /themes/htf_glass/images/search_small.gif HTTP/1.1" 200 1367 "https://www.youcl.com/the_perfect_desktop_mandriva_2008.0/" "Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Debian)" 192.6.178.101 - - [17/Oct/2007:18:50:41 +0200] "GET /themes/htf_glass/images/join_small.gif HTTP/1.0" 200 1212 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 192.6.178.101 - - [17/Oct/2007:18:50:42 +0200] "GET /themes/htf_glass/images/join_small.gif HTTP/1.0" 200 1212 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 192.6.178.101 - - [17/Oct/2007:18:50:43 +0200] "GET /forums/clientscript/vbulletin_md5.js HTTP/1.0" 200 9661 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 192.6.178.101 - - [17/Oct/2007:18:50:44 +0200] "GET /forums/clientscript/vbulletin_md5.js HTTP/1.0" 200 9661 "https://www.youcl.com/data_recovery_with_testdisk/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)" 24.127.251.14 - - [17/Oct/2007:18:50:45 +0200] "GET /mailgust/i/menushadow.gif HTTP/1.1" 200 59 "" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)" 24.127.251.14 - - [17/Oct/2007:18:50:46 +0200] "GET /mailgust/i/menushadow.gif HTTP/1.1" 200 59 "" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)"
4为logresolvemerge.pl创建Cron作业
当然,你不想每天手动运行logresolvemerge.pl; 因此,我们现在创建一个cron工作:
crontab -e
如果你想每天晚上4点00分运行logresolvemerge.pl,你的cron工作可能会像这样(调整你需要的时间):
0 4 * * * /usr/local/bin/logresolvemerge.pl /var/log/webcluster/access_log_server* > /var/log/webcluster/access_log_overall |
5链接
- AWStats: http : //awstats.sourceforge.net
- Webalizer: http : //www.mrunix.net/webalizer
- Debian: http : //www.debian.org