好的,这是一个场景:我需要解析我的日志,找出有多少次图片缩略图被下载了,而没有真正看到“大图像”页面.这基本上是一个基于“拇指”和“完整”图像视图比例的热链接保护系统。
考虑到服务器经常受到对缩略图的请求的轰炸,最有效的解决方案似乎是使用缓冲的apache日志,每隔1 1Mb写一次磁盘,然后定期解析日志。
我的问题是:如何在PHP中解析apache日志以保存数据,其中如下所示:
php记录器脚本将每60秒调用一次,并在此期间处理尽可能多的日志行。
我尝试将一些代码合并在一起,但是我在使用最小内存时遇到了问题,我找到了一种用“移动”文件大小来跟踪指针的方法
下面是日志的一部分:
212.180.168.244 - - [18/Jan/2012:20:06:57 +0100] "GET /t/0/11/11441/11441268.jpg HTTP/1.1" 200 3072 "-" "Opera/9.80 (Windows NT 6.1; U; pl) Presto/2.10.229 Version/11.60" "-"
122.53.168.123 - - [18/Jan/2012:20:06:57 +0100] "GET /t/0/11/11441/11441276.jpg HTTP/1.1" 200 3007 "-" "Opera/9.80 (Windows NT 6.1; U; pl) Presto/2.10.229 Version/11.60" "-"
143.22.203.211 - - [18/Jan/2012:20:06:57 +0100] "GET /t/0/11/11441/11441282.jpg HTTP/1.1" 200 4670 "-" "Opera/9.80 (Windows NT 6.1; U; pl) Presto/2.10.229 Version/11.60" "-"在这里附加供您审阅的代码:
<?php
//limit for running it every minute
error_reporting(E_ALL);
ini_set('display_errors',1);
set_time_limit(0);
include(dirname(__FILE__).'/../kframework/kcore.class.php');
$aj = new kajaxpage;
$aj->use_db=1;
$aj->init();
$db=kdbhandler::getInstance();
$d=kdebug::getInstance();
$d->debug=TRUE;
$d->verbose=TRUE;
$log_file = "/var/log/nginx/access.log"; //full path to log file when run by cron
$pid_file = dirname(__FILE__)."/../kframework/cron/cron_log.pid";
//$images_id = array("8308086", "7485151", "6666231", "8343336");
if (file_exists($pid_file)) {
$pid = file_get_contents($pid_file);
$temp = explode(" ", $pid);
$pid_timestamp = $temp[0];
$now_timestamp = strtotime("now");
//if (($now_timestamp - $pid_timestamp) < 90) return;
$pointer = $temp[1];
if ($pointer > filesize($log_file)) $pointer = 0;
}
else $pointer = 0;
$pattern = "/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})[^\[]*\[([^\]]*)\][^\"]*\"([^\"]*)\"\s([0-9]*)\s([0-9]*)(.*)/";
$last_time = 0;
$lines_processed=0;
if ($fp = fopen($log_file, "r+")) {
fseek($fp, $pointer);
while (!feof($fp)) {
//if ($lines_processed>100) exit;
$lines_processed++;
$log_line = trim(fgets($fp));
if (!empty($log_line)) {
preg_match_all($pattern, $log_line, $matches);
//print_r($matches);
$size = $matches[5][0];
$matches[3][0] = str_replace("GET ", "", $matches[3][0]);
$matches[3][0] = str_replace("HTTP/1.1", "", $matches[3][0]);
$matches[3][0] = str_replace(".jpg/", ".jpg", $matches[3][0]);
if (substr($matches[3][0],0,3) == "/t/") {
$get = explode("-",end(explode("/",$matches[3][0])));
$imgid = $get[0];
$type='thumb';
}
elseif (substr($matches[3][0], 0, 5) == "/img/") {
$get1 = explode("/", $matches[3][0]);
$get2 = explode("-", $get1[2]);
$imgid = $get2[0];
$type='raw';
}
echo $matches[3][0];
// put here your sql insert or update
$imgid=(int) $imgid;
if (isset($type) && $imgid!=1) {
switch ($type) {
case 'thumb':
//use the second slave in the registry
$sql=$db->slave_query("INSERT INTO hotlink SET thumbviews=1, imageid=".$imgid." ON DUPLICATE KEY UPDATE thumbviews=thumbviews+1 ",2);
echo "INSERT INTO hotlink SET thumbviews=1, imageid=".$imgid." ON DUPLICATE KEY UPDATE thumbviews=thumbviews+1";
break;
case 'raw':
//use the second slave in the registry
$sql=$db->slave_query("INSERT INTO hotlink SET rawviews=1, imageid=".$imgid." ON DUPLICATE KEY UPDATE rawviews=rawviews+1",2);
echo "INSERT INTO hotlink SET rawviews=1, imageid=".$imgid." ON DUPLICATE KEY UPDATE rawviews=rawviews+1";
break;
}
}
// $imgid - image ID
// $size - image size
$timestamp = strtotime("now");
if (($timestamp - $last_time) > 30) {
file_put_contents($pid_file, $timestamp . " " . ftell($fp));
$last_time = $timestamp;
}
}
}
file_put_contents($pid_file, (strtotime("now") - 95) . " " . ftell($fp));
fclose($fp);
}
?>发布于 2012-01-19 12:24:32
也许您可以调整我的PHP版本的尾,以搜索您的最后一个时间戳,而不是数行,然后从这一点读行,处理它们一个又一个?
我会自己试一试,因为我有点好奇,但不幸的是,现在做不到。
发布于 2012-01-18 19:13:26
一个解决方案是将日志存储到mysql数据库中。也许您可以在mysql中存储日志文件之后,编写一个C语言程序来解析日志文件。这将是一个更快的震级,也不是很困难。另一个选择是使用phyton,但我认为使用数据库是必要的。可以使用全文索引来匹配字符串。Python也可以编译成二进制文件。这使它更有效率。根据请求:日志文件堆栈增量。这并不是说你一次就给10 at。
发布于 2012-01-19 11:33:55
我会亲自将日志条目发送到正在运行的脚本中。Apache将通过使用管道启动日志的文件名来允许这一点。如果这不起作用,您也可以创建一个fifo (参见mkfifo)。
正在运行的脚本(不管它是什么)可以缓冲x行,并在此基础上执行它需要做的事情。读取数据并不那么困难,也不应该成为瓶颈。
我确实怀疑您在数据库中的INSERT语句会遇到问题。
https://stackoverflow.com/questions/8915816
复制相似问题