Heim  >  Artikel  >  Datenbank  >  PHP Apache Access Log 分析工具 拆分字段成CSV文件并插入Mysql_MySQL

PHP Apache Access Log 分析工具 拆分字段成CSV文件并插入Mysql_MySQL

WBOY
WBOYOriginal
2016-06-01 13:11:21882Durchsuche

Apache

网站被黑了 挂马了 服务器中毒了 防火墙没有开 双机热备失效了。。。种种奇葩。。


现在需要分析访问日志,怎么办?

比如分析D:/Servers/Apache2.2/logs/access2014-05-22.log

http://my.oschina.net/cart/针对这个问题特意开发了一款小工具分析Apache 日志,拆分字段成CSV文件并插入Mysql数据库分析

$date = '2014-05-23';preg_match_all('/(.*?) .*? .*? /[(.*?) (.*?)/] "(.*?) (.*?) (.*?)" (.*?) (.*?) "(.*?)" "(.*?)"/isu', file_get_contents('../Servers/Apache2.2/logs/access'.$date.'.log'), $matches);$num = count($matches[0]);$fp = fopen($date.'.csv', 'cb');fputcsv($fp, array('IP','Date','Zone','Protocol','URL','Version','Status','Size','Referer','User-Agent'));for($i = 0; $i < $num; $i ++){	fputcsv($fp, array(trim($matches[1][$i]), $matches[2][$i], $matches[3][$i], $matches[4][$i], $matches[5][$i], $matches[6][$i], $matches[7][$i], $matches[8][$i], $matches[9][$i], $matches[10][$i]));}fclose($fp);




最后在Mysql中按照CSV字段信息建立字段,通过Mysql的 CSV using LOAD DATA 导入你的CSV文件即可。

导入2G的CSV日志,速度都很快!!!

版权所有:http://my.oschina.net/cart/


哈哈,http://my.oschina.net/cart/日志已经成功拆成列入到数据库了,剩下怎么筛选,怎么折腾http://my.oschina.net/cart/,随你意!哈哈!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn