Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-01 13:11:21
Original
906 people have browsed it

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);
Copy after login




最后在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/,随你意!哈哈!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template