`
isiqi
  • 浏览: 16034088 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

利用apache的日志文件进行网站流量统计zz

阅读更多
http://Tech.acnow.net 2006-10-27 15:39:43 互联网

一、环境:freebsd(linux)+apache+php+mysql



二、概述:对于使用apache作为web服务器的网站来说,apache的access_log是进行流量统计的重要的依据。

根据access_log文件来进行流量统计的方法有两种,一种是直接在程序中对log文件进行分析,这种

方法的一个明显的缺点是速度比较慢,比较耗费资源,因为一个浏览量较高的网站的log文件是非常

大的,一个典型的例子是老外用c写的analog;另一种方法是把每日的log文件记录导入数据库,再

用程序对数据库进行分析,这种方法相对前者来说有比较大的速度和性能上的优势。这里我写的这个
程序的功能虽然不及analog等强大,但是……,自己的东西用的还是比较舒服的,呵呵,不过这只是

我的方法,中间可能会有一些不对的地方,请大家指正。



三、具体实现:主要利用freebsd(linux)的crontab每天自动定时执行程序产生每日的html格式的流量报表文件。



注意:以下命令以apache默认的log文件格式处理,如果你定制了不同的log文件格式,具体命令要根据具体情况修改。

并且要以cgi方式编译一次php,以便以命令行方式运行php程序。



1.your_log数据表结构

根据apache默认的log文件中的子段,可以很容易地设计your_log数据表的结构:

CREATE TABLE your_log (

host varchar(15) NOT NULL,

ident varchar(8) NOT NULL,

auth varchar(20) NOT NULL,

time varchar(24) NOT NULL,

zone varchar(8) NOT NULL,

method varchar(6) NOT NULL,

file varchar(255) NOT NULL,

protocol varchar(10) NOT NULL,

status varchar(5) NOT NULL,

bytes int(10) DEFAULT '0' NOT NULL,

KEY host (host),

KEY file (file),

KEY status (status),

KEY bytes (bytes)

);



2.分离log文件,形成每天一个log文件,以日期为目录,文件名自定(假设是your_log)

考虑这个步骤时,想到过利用linux的logrotate功能和apache自带的rotatelogs功能,但是由于我用的是freebsd,没有找到

logrotate命令,而apache的rotatelogs我用之后没有发现能够指定生成文件名的功能,只能用timestamp作为文件名,对于

程序来说不好处理,所以我自己想了以下的方法,如果有人认为这样不好而能提供更好的方法,请不吝赐教:)

mkdir /your_log_dir/`date -v-1d "+%Y%m%d"` #建立每天的log文件目录

cat /your_apache_log_dir/access_log | grep `date -v-1d "+%d/%b/%Y"` > your_log_dir/`date -v-1d "+%Y%m%d"`/your_log

将此二命令放入crontab,我设为每天凌晨1点自动运行(时间可自己定,但是一定要在0点以后)。



3.将日报文件导入数据库

/your_mysql_bin_dir/mysqlimport -u mysql_username -p mysql_password -d --fields-terminated-by=' ' your_db_name your_log_dir/`date -v-1d "+%Y%m%d"`/your_log

这个命令先将your_db_name数据库中的your_log数据表清空,再从已生成的your_log文本文件里导入数据,以空格为字段分界。

将此命令放入crontab,我设为每天凌晨1:30自动运行(时间可自己定,但是一定要在执行第2步以后,先后顺序不能颠倒)。





4.生成每日流量统计日报html文件的php程序

a.假设网站的document_root下有以下四个子目录(栏目):aaa,bbb,ccc,ddd,如有更多,依此类推;

b.这里只在日报页面上计算以下几个统计数字:数据总流量、总请求次数、访问主机数、总pageview数量、

各个栏目之pageview数量、每小时的pageview数量;

c.根据RFC2616标准,请求状态编码中以4和5开头的均为错误或无效请求,在统计时应过滤掉这些数字;

d.这里我假设整个网站以php构建,计算流量时只考虑*.php和*.html文件以及纯目录请求的次数。

e.可以根据个人的需要增加很多统计数字,如工作时间与非工作时间的流量等等,这里就不赘述了,请大家自己考虑吧:)



[dreport.php代码文件]

<?php

//--连接数据库--

$db=@mysql_connect("localhost","user","password") or die(mysql_error());

@mysql_select_db("your_db_name",$db) or die(mysql_error());



//--取前一天的timestamp--

$yestoday_array=getdate(time()-24*60*60);

$start_time=mktime(0,0,0,$yestoday_array["mon"],$yestoday_array["mday"],$yestoday_array["year"]);

$end_time=mktime(23,59,59,$yestoday_array["mon"],$yestoday_array["mday"],$yestoday_array["year"]);


//--计算总数据流量--

$bytes=0;

$sql0="select bytes from your_log";

$res0=mysql_query($sql0) or die(mysql_error());

while($row0=@mysql_fetch_row($res0)){

$bytes+=$row0[0];

}



//--计算总请求次数--

$sql1="select count(host) from your_log";

$res1=mysql_query($sql1) or die(mysql_error());

$row1=@mysql_fetch_row($res1);

$hits=$row1[0];



//--计算访问主机数量--

$sql2="select distinct host from your_log";

$res2=mysql_query($sql2) or die(mysql_error());

$ips=@mysql_num_rows($res2);





//--计算总pageviews--

$sql4="select count(host) from your_log where (file like '%.html%' ";

$sql4.="or file like '%.php%' or file not like '%.%') ";

$sql4.="and left(status,1) <> '4' and left(status,1) <> '5'";

$res4=mysql_query($sql4) or die(mysql_error());

$row4=@mysql_fetch_row($res4);

$pageviews=$row4[0];



//--计算aaa栏目的pageviews--

$sql5="select count(host) from your_log where file like '%/aaa/%' ";

$sql5.="and (file like '%.php%' or file like '%.html%' or file not like '%.%') ";

$sql5.="and left(status,1) <> '4' and left(status,1) <> '5'";

$res5=mysql_query($sql5) or die(mysql_error());

$row5=@mysql_fetch_row($res5);

$aaapvs=$row5[0];



//--计算bbb栏目的pageviews--

$sql6="select count(host) from your_log where file like '%/bbb/%' ";

$sql6.="and (file like '%.php%' or file like '%.html%' or file not like '%.%') ";

$sql6.="and left(status,1) <> '4' and left(status,1) <> '5'";

$res6=mysql_query($sql6) or die(mysql_error());

$row6=@mysql_fetch_row($res6);

$bbbpvs=$row6[0];



//--计算ccc栏目的pageviews--

$sql7="select count(host) from your_log where file like '%/ccc/%' ";

$sql7.="and (file like '%.php%' or file like '%.html%' or file not like '%.%') ";

$sql7.="and left(status,1) <> '4' and left(status,1) <> '5'";

$res7=mysql_query($sql7) or die(mysql_error());

$row7=@mysql_fetch_row($res7);

$cccpvs=$row7[0];



//--计算ddd栏目的pageviews--

$sql8="select count(host) from your_log where file like '%/ddd/%' ";

$sql8.="and (file like '%.php%' or file like '%.html%' or file not like '%.%') ";

$sql8.="and left(status,1) <> '4' and left(status,1) <> '5'";

$res8=mysql_query($sql8) or die(mysql_error());

$row8=@mysql_fetch_row($res8);

$dddpvs=$row8[0];



//--计算首页的pageviews--

$sqlg="select count(host) from your_log where (file = '/' or file = '/index.html') ";

$sqlg.="and left(status,1) <> '4' and left(status,1) <> '5'";

$resg=mysql_query($sqlg) or die(mysql_error());

$rowg=@mysql_fetch_row($resg);

$indexpvs=$rowg[0];



//--report是生成日报文件的字符串--

$report="<HTML>n";

$report.="<HEAD>n";

$report.="<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">n";

$report.="<TITLE>流量统计日报</TITLE>n";

$report.="</HEAD>n";

$report.="<BODY>n";

$report.="时间:".date("Y-m-d H:i:s",$start_time)." -- ".date("Y-m-d H:i:s",$end_time)."<BR><BR>n";

$report.="<FONT SIZE="+2"><B><I>PageViews:</I></B></FONT>n";

$report.="<P>数据吞吐总量:".$bytes."&nbsp;Bytes<BR>请求次数:".$hits."  IP主机数:<FONT COLOR="green">".$ips."</FONT><BR>n";

$report.=" PageViews:<FONT COLOR="red">".$pageviews."</FONT></P>n";

$report.="<P><FONT FACE="宋体" COLOR="#008000" SIZE="3"><B>各个栏目PageViews:</B></FONT></P>n";

$report.="<TABLE WIDTH="300" BORDER="1">n";

$report.=" <TBODY>n";

$report.=" <TR>n";

$report.=" <TD>/</TD>n";

$report.=" <TD>首页</TD>n";

$report.=" <TD>".$indexpvs."</TD>n";

$report.=" </TR>n";

$report.=" <TR>n";

$report.=" <TD>/aaa/</TD>n";

$report.=" <TD>aaa栏目</TD>n";

$report.=" <TD>".$aaapvs."</TD>n";

$report.=" </TR>n";

$report.=" <TR>n";

$report.=" <TD>/bbb/</TD>n";

$report.=" <TD>bbb栏目</TD>n";

$report.=" <TD>".$bbbpvs."</TD>n";

$report.=" </TR>n";

$report.=" <TR>n";

$report.=" <TD>/ccc/</TD>n";

$report.=" <TD>ccc栏目</TD>n";

$report.=" <TD>".$cccpvs."</TD>n";

$report.=" </TR>n";

$report.=" <TR>n";

$report.=" <TD>/ddd/</TD>n";

$report.=" <TD>ddd栏目</TD>n";

$report.=" <TD>".$dddpvs."</TD>n";

$report.=" </TR>n";

$report.=" <TR>n";

$report.=" <TD> </TD>n";

$report.=" <TD>总数</TD>n";

$report.=" <TD>".$pageviews."</TD>n";

$report.=" </TR>n";

$report.=" </TBODY>n";

$report.="</TABLE>n";

$report.="<P><FONT FACE="宋体" COLOR="#008000" SIZE="3"><B>每小时PageViews:</B></FONT></P>n";

$report.="<TABLE WIDTH="300" BORDER="1">n";

$report.=" <COL WIDTH="439" STYLE="mso-width-source:userset;mso-width-alt:14048;width:329pt">n";

$report.=" <COL WIDTH="120" STYLE="mso-width-source:userset;mso-width-alt:3840;width:90pt">n";

$report.=" <TR HEIGHT="21" STYLE="height:15.75pt">n";

$report.=" <TD HEIGHT="21" CLASS="xl22" WIDTH="200" STYLE="height: 15.75pt">Hour</TD>n";

$report.=" <TD CLASS="xl22" WIDTH="100" HEIGHT="21"><SPAN STYLE="mso-spacerun:yes">&nbsp;</SPAN> Page Views</TD>n";

$report.=" </TR>n";



//--每小时的pageview--

for($i=0;$i<24;$i++){

$date_temp=date("[d/M/Y:",time()-24*60*60).sprintf("d",$i);

$sqlh="select count(host) from your_log where (file like '%.html%' or file like '%.php%' ";

$sqlh.="or file not like '%.%') and time like '$date_temp%' ";

$sqlh.="and left(status,1) <> '4' and left(status,1) <> '5'";

$resh=mysql_query($sqlh) or die(mysql_error());




$rowh=@mysql_fetch_row($resh);

$report.=" <TR HEIGHT="19" STYLE="height:14.25pt">n";

$report.=" <TD HEIGHT="19" CLASS="xl23" STYLE="height: 14.25pt" WIDTH="200">".sprintf("d",$i).":00 - ".sprintf("d",$i).":59</TD>n";

$report.=" <TD CLASS="xl23" x:num WIDTH="100" HEIGHT="19">".$rowh[0]."</TD>n";

$report.=" </TR>n";

}



//--关闭数据库--

mysql_close();



$report.="</TABLE>n";

$report.="</BODY>n";

$report.="</HTML>";



//--生成每天的流量统计html页面文件,以日期为文件名--

$report_file="/your_report_dir/".date("Ymd",time()-24*60*60).".html";

$fp=fopen($report_file,"w");

fwrite($fp,$report);

fclose($fp);

?>



5.每天凌晨执行2、3步后执行此程序,将自动产生前一天的流量统计html页面

/your_php_bin_dir/php /some_dir/dreport.php

将此命令放入crontab,我设为每天凌晨2:00自动运行(时间可自己定,但是一定要在第3步以后,先后顺序不能颠倒)。



四、结束语

终于要写完了,这里还要补充一些东西。对于流量非常大的大型网站,可能并不适合把以上的步骤都放在web服务器上

自动定时运行,因为可能会对服务器造成比较大的负荷,所以当遇到这种情况时,也许用php来完成这个任务并不是很好的

选择,可以考虑用效率最高的c/c++来写程序,还有就是可以考虑把apache的日志文件下载到本地的服务器,再用以上方法

来完成任务。还要提醒朋友们,这里主要阐述的是一个解决方案、一种思路,遇到不同的情况时还是要具体问题具体分析

的:),我把它写出来希望能够起到抛砖引玉的作用,期待您的意见与建议!(是不是有人嫌我太罗嗦了?我走……)
分享到:
评论

相关推荐

    apache日志文件说明

    apache日志文件说明

    如何按日期生成apache日志文件及限制apache日志文件大小

    如何按日期生成apache日志文件及限制apache日志文件大小

    Python程序设计:python apache日志分析.pptx

    python apache日志分析 INTERNATIONAL MEDICAL SUMMIT FORUM ...当我们安装并启动Apache后,Apache会自动生成两个日志文件,这两个日志文件分别是访问日志access_log(在Windows上是access.log)和错误日志err

    APACHE日志分析工具

    很多apache日志分析工具都是要安装到服务器上的,而且安装非常麻烦,于是我写了一个单机版(exe,Windows),方便大家分析apache访问日志,绿色版的,直接解压就可以用。 功能: 1、导入apache访问日志; 2、访问...

    Web应用安全:apache日志配置.pptx

    在kali中,apache的主配置文件是/etc/apache2/apache2.conf,我们在这个文件中进行查找,很容易找到日志配置信息,我们可以修改其中的参数,来对日志配置进行修改,如下图: apache日志配置 在kali,访问日志被记录...

    Web应用安全:apache日志配置(实验).doc

    修改apache主配置文件中的日志配置 实验内容 在kali中找到主配置文件 修改日志配置 访问服务器产生日志 查看日志 实验环境 Kali系统 实验步骤 1.在kali中找到apache的主配置文件“/etc/apache2/apache2.conf”。 2...

    天智Apache日志分析器

    天智Apache日志分析器是一款免费的Apache日志分析工具,使用VC++开发,专用于分析Apache网站日志,可监视、查看和分析Apache服务器日志,统计日志中正常访问和错误日志等信息。

    Web应用安全:apache日志配置.docx

    Kali系统自带了apache的相关内容,只需要简单的一行代码“service apache start”就可以运行,但在运行前,我们需要进行一定的设置,在kali中apache默认的配置文件为“/etc/apache2/apache2.conf”,我们可以修改这...

    shell 处理apache日志入库

    shell 处理apache日志入库 awk分析apache日志为*.sql文件 sqlplus写入oracle

    apache日志切割

    本文介绍了apache日志如何切割,通过自动切割apache日志切割,我们可以更方便查阅日志

    Apache日志分析手册

    apache在生产环境下遇到的诸多问题,此日志仅限于本人在Linux环境下总结下的问题,酌情下载,希望能帮到你们

    apache日志分析

    windows端日志分析软件,支持FTP远程获取linux,NT,OS日志

    apache的日志轮询

    对于大型的WEB服务来说,其往往使用实用负载均衡技术提高web站点服务能力,这样后台有多个服务器提供WEB服务,这大大方便了服务的分布规划和扩展性,但多个服务器的分布就需要对日志进行合并统一进行统计分析。...

    apache日志hadoop大数据

    apache日志hadoop大数据 hive与hbase是如何整合使用的

    Apache Web日志Hive实验数据

    该实验数据主要用于Hive进行Apache Web日志的统计分析学习使用,数据量不是大。

    debian apache日志服务器配置

    debian下apache日志集中管理,日志服务器配置

    lorg, Apache日志文件安全分析器.zip

    lorg, Apache日志文件安全分析器 LORG用于高级日志日志文件安全性分析和取证的工具Web服务器日志文件是重建事件的主要来源,如果网站受到pwned的影响,则会导致事件。 然而,从大型文件中提取相关信息是一项艰巨的...

    记录一次ssh日志分析和apache日志分析

    记录一次ssh日志分析和apache日志分析

    Apache24多站点配置及网站日志文件按日期进行分割

    由于以前经常用IIS配置网站,所以现在突然改成使用Apache会有很大的不适应和遇到一些从没见过的困难,相信很多朋友也一样有这个苦恼,于是在各搜索引擎找解决办法。因为自己的经验不足,经常被搜索出来的一些经验所...

    apache日志分析系统免费版V1.6

    apache日志分析系统免费版V1.6,感觉功能不错,这边留个底以后会用到!

Global site tag (gtag.js) - Google Analytics