<?
//*********************************************************************
//
// MARYAN
// Apache logfile monitor
// version 1.24
//
// Script by Andrew Maryan
// Andrew MARYAN internet development studio
// www.maryan.com.ua
//
// (c) 2001-2002 Copyright Andrew MARYAN. All rights reserved.
//
// New from 1.23: Added Refferer servers report, decode URL, correct GMT offset, etc.
//
//*********************************************************************
// Settings:
$log="/usr/home/.../logs/httpd-access_log"; //path & name to Apache logfile
$nrow=30; // rows for output page
// database connected parameters:
$dhost="localhost"; // host name
$dname=""; // database name
$duser=""; // username
$dpass=""; // password
?><?
setcookie ("base", "on");
?>
<?
if($sdat){
$qustr="where date_time>$sdat"."000000 and date_time<$sdat"."235959";
$qustr2="and date_time>$sdat"."000000 and date_time<$sdat"."235959";
} else {$qustr="";}
if($field){
if($field=="reffersrv"){
$r=mysqlres("select code,date_time, page, host,reffer, uagent from m_stat where reffer like '%$fval%' $qustr2 order by date_time desc");
}else{
$r=mysqlres("select code, date_time, page, host,reffer, uagent from m_stat where $field='$fval' $qustr2 order by date_time desc");
}
echo "<b>Report time: </b>";
if($sdat){
echo(substr($sdat,4,2)."/".substr($sdat,6,2)."/".substr($sdat,0,4));
}else{echo"All time";}
echo "<br><b>Records: </b>".mysql_num_rows($r);
echo "<br><a href='$self?field=$field&fval=$fval'>Report for all time</a>";
$f=mysql_fetch_object($r);
$clr=" bgcolor=\"#C0D9E6\"";
echo"<table width=\"100%\">\n";
echo"<tr><td$clr><b>Code</td><td$clr><b>Date-time</td>
<td$clr><b>Page</td><td$clr><b>Refferer</td><td$clr><b>Hostname</td><td$clr><b>UserAgent</b></td></tr>";
for($ii=0;$f;$ii++){ if($ii%2==0){$clr=" bgcolor=\"#F2FAFF\"";} else {$clr="";}
echo"<tr><td$clr>$f->code</td><td$clr>$f->date_time</td><td$clr>$f->page</td>
<td$clr>".rawurldecode($f->reffer)."</td><td$clr>$f->host</td><td$clr>$f->uagent</td></tr>";
$f=mysql_fetch_object($r);}
}
elseif($act=="customres"){
$dat1="$sdy$sdm$sdd$sdh$sdi$sds";
$dat2="$edy$edm$edd$edh$edi$eds";
$stcust="";
if(trim($sthost)!=""){ $stcust.=" and host like '%$sthost%'";}
if(trim($stref)!=""){ $stcust.=" and reffer like '%$stref%'";}
if(trim($stua)!=""){ $stcust.=" and uagent like '%$stua%'";}
$r=mysqlres("select code,date_time, page, host,reffer, uagent from m_stat
where date_time>$dat1 and date_time<$dat2 $stcust order by date_time desc");
if(mysql_num_rows($r)>0)
{ $f=mysql_fetch_object($r);
$clr=" bgcolor=\"#C0D9E6\"";
echo"<table width=\"100%\">\n";
echo"<tr><tr><td$clr><b>Code</td><td$clr><b>Date-time</td><td$clr><b>Page</td>
<td$clr><b>Refferer</td><td$clr><b>Hostname</td><td$clr><b>UserAgent</b></td></tr>";
for($ii=0;$f;$ii++){ if($ii%2==0){$clr=" bgcolor=\"#F2FAFF\"";} else {$clr="";}
echo"<tr><td$clr>$f->code</td><td$clr>$f->date_time</td><td$clr>$f->page</td>
<td$clr>".rawurldecode($f->reffer)."</td><td$clr>$f->host</td><td$clr>$f->uagent</td></tr>";
$f=mysql_fetch_object($r);}} else {echo "<b>NO RESULT !!!</b>";}
}else
{
if(!$base){
mysqlres("drop table m_stat");
$fl=file($log);
//make data table
mysqlres("create table m_stat(
id int default 0 not null auto_increment,
date_time DATETIME,
host VARCHAR(250),
page VARCHAR(250),
protocol VARCHAR(100),
code int,
size int,
reffer VARCHAR(250),
uagent VARCHAR(250),
primary key (id))");
for($i=0;$i<count($fl);$i++){
$str=explode(" ", $fl[$i]);
$host=$str[0];
$dt=substr($str[3],1)." ".substr($str[4],0,5);
//translate date-time to mysql format
$dt=substr($dt,0,2)." ".substr($dt,3,3)." ".substr($dt,7,4)." ".substr($dt,12,8)." ".substr($dt,-5,5);
$dt=date("Y-m-d H:i:s",strtotime ($dt));
$method=substr($str[5],1);
$pageon=$str[6];
$protocol=substr($str[7],0,strpos($str[7],'"'));
$code=$str[8];
$size=$str[9];
$reffer=substr($str[10],1,strlen($str[10])-2);
$uagent="";
for($ii=11;$ii<count($str);$ii++){
$uagent.=$str[$ii];
}
$uagent=substr($uagent,1,strlen($uagent)-3);
if(strtolower(substr($pageon,strlen($pageon)-4))!=".jpg" //clean pictures & JavaScript files
&&strtolower(substr($pageon,strlen($pageon)-4))!=".gif"
&&strtolower(substr($pageon,strlen($pageon)-4))!=".css"
&&strtolower(substr($pageon,strlen($pageon)-3))!=".js")
{
mysqlres("insert into m_stat
(date_time,host,page,protocol,code,size,reffer,uagent)
values
('$dt','$host','$pageon','$protocol','$code','$size','$reffer','$uagent')");
}
} $pageon=""; }
// stat datatable added
//**************************************
if(is_file("log_pages.php")){
$lfp=file("log_pages.php");
for($l=0;$l<count($lfp);$l++){
mysqlres("delete from m_stat where page like '%".trim($lfp[$l])."%'");
}}
if(is_file("log_reffer.php")){
$lfp=file("log_reffer.php");
for($l=0;$l<count($lfp);$l++){
mysqlres("delete from m_stat where reffer like '%".trim($lfp[$l])."%'");
}}
//**************************************
// output stat info
$nblock=5; //number of info block-windows
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title>MARYAN log-monitor</title>
</head>
<body>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="1%" bgcolor="#617AA3"><font size="2"> </font></td>
<td width="49%" bgcolor="#617AA3" align="center"><b><font color="#FFFFFF" size="2">www.maryan.com.ua</font></b></td>
<td width="50%" bgcolor="#C0D9E6"><font size="2"> version 1.24</font></td>
</tr>
<tr>
<td width="1%"><a href="http://www.maryan.com.ua/loghref.php" target="_blank"><img border="0" src="http://www.maryan.com.ua/img/tit.jpg" width="230" height="79"></a></td>
<td width="49%" valign="middle" align="center"><font size="4"><b>MARYAN<br><font color="#CB3B17">Apache
logfile monitor</font></b></font></td>
<td width="50%" align="center" bgcolor="#F2FAFF">
<b> TODAY: Hosts:<?
$r=mysqlres("select host as count from m_stat where date_time>".date("Ymd")."000000 and date_time<".date("Ymd")."235959 group by host");
echo mysql_num_rows($r);
?> - Hits:<?
$r=mysqlres("select host as count from m_stat where date_time>".date("Ymd")."000000 and date_time<".date("Ymd")."235959");
echo mysql_num_rows($r);
?>
<br>
ALL LOG TIME: Hosts:<?
$r=mysqlres("select host as count from m_stat group by host");
echo mysql_num_rows($r);
?> - Hits:<?
$r=mysqlres("select host as count from m_stat");
echo mysql_num_rows($r);
?> </b>
<br>
REPORT TIME: Hosts:<?
$r=mysqlres("select host as count from m_stat $qustr group by host");
echo mysql_num_rows($r);
?> - Hits:<?
$r=mysqlres("select host as count from m_stat $qustr");
echo mysql_num_rows($r);
?> </b>
</td>
</tr>
<tr>
<td width="100%" bgcolor="#617AA3" colspan="3" height="20"><font color="#FFFFFF" size="2"><b>
<a href="<?echo "$self?sdat=$sdat";?>"><font color="#FFFFFF">Home</font></a>
| <a href="<?echo "$self?sdat=$sdat&pageon=on&act=page";?>"><font color="#FFFFFF">Page report</font></a>
| <a href="<?echo "$self?sdat=$sdat&pageon=on&act=reffersrv";?>"><font color="#FFFFFF">Refferer servers report</font></a>
| <a href="<?echo "$self?sdat=$sdat&pageon=on&act=reffer";?>"><font color="#FFFFFF">Refferer report</font></a>
| <a href="<?echo "$self?sdat=$sdat&pageon=on&act=uagent";?>"><font color="#FFFFFF">User Agent report</font></a>
| <a href="<?echo "$self?sdat=$sdat&pageon=on&act=host";?>"><font color="#FFFFFF">Hostname report</font></a>
| <a href="<?echo "$self?pageon=on&act=custom";?>"><font color="#FFFFFF">Custom report</b></font></a></td>
</tr>
</table><?
?>
<hr noshade size="3" color="#CB3B17">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="25%" bgcolor="#C0D9E6" rowspan="3" valign="top"><b>Code statistic:</b>
<table border="0" width="100%" cellspacing="0" cellpadding="0"><?
$r=mysqlres("select code, count(code) as count from m_stat $qustr group by code order by count desc");
$f=mysql_fetch_object($r);
$coden[200]="(OK)";
$coden[206]="(Partial Content)";
$coden[301]="(Moved Permanently)";
$coden[302]="(Found)";
$coden[304]="(Not Modified)";
$coden[404]="(Not Found)";
while($f){
echo "<tr><td> <b><a href='$self?field=code&fval=$f->code&sdat=$sdat' target='_blank'><font color='#617AA3'>$f->code</font></a></b> ".$coden[$f->code]."</td><td><font color='#617AA3'>$f->count</font></td></tr>";
$f=mysql_fetch_object($r);}
?></table></td>
<td width="75%" align="center"><?
$f=mysql_fetch_object(mysqlres("select date_time from m_stat order by date_time"));
$startall=$f->date_time;
$tstartall=mktime(substr($f->date_time,11,2),substr($f->date_time,14,2),substr($f->date_time,17,2),substr($f->date_time,5,2),substr($f->date_time,8,2),substr($f->date_time,0,4));
$f=mysql_fetch_object(mysqlres("select date_time from m_stat order by date_time desc"));
$endall=$f->date_time;
$tendall=mktime(substr($f->date_time,11,2),substr($f->date_time,14,2),substr($f->date_time,17,2),substr($f->date_time,5,2),substr($f->date_time,8,2),substr($f->date_time,0,4));
if($sdat){
echo "<b><font size='+2'>Report for ".date("M,d",mktime(0,0,0,substr($sdat,4,2),substr($sdat,6,2),substr($sdat,0,4) ))."</font></b> ($startall --- $endall)";
}else {
echo "<b><font size='+2'>Report for all log time</font></b> ($startall --- $endall)";
}
?></td>
</tr>
<tr>
<td width="75%" align="center"><?
$dat1=$tstartall;
while($dat1<=$tendall){
echo "<a href='$self?pageon=$pageon&act=$act&val=$val&sdat=".date("Ymd",$dat1)."'><b>".date("M,d",$dat1)."</b></a> - ";
$dat1=mktime(0,0,0,date("m",$dat1),date("d",$dat1)+1,date("Y",$dat1));
} echo "<a href='$self?pageon=$pageon&act=$act&val=$val&sdat='><b>All time</b></a>";
?></td>
</tr>
<tr>
<td width="75%"> </td>
</tr>
</table>
<hr noshade size="3" color="#CB3B17">
<?
if($act=="custom"){
?>
<table border="0" width="100%"> <form method="POST" action="<? echo "$self";?>" target="_blank">
<input type="hidden" name="act" value="customres">
<tr>
<td width="28%" align="right" bgcolor="#C0D9E6">Start date (mm-dd-yyyy hh-mm-ss)</td>
<td width="72%" bgcolor="#C0D9E6">
<input type="text" name="sdm" size="1" value="<?echo date("m",$tstartall)?>">
-<input type="text" name="sdd" size="1" value="<?echo date("d",$tstartall)?>">
-<input type="text" name="sdy" size="2" value="<?echo date("Y",$tstartall)?>">
<input type="text" name="sdh" size="1" value="<?echo date("h",$tstartall)?>">:
<input type="text" name="sdi" size="1" value="<?echo date("i",$tstartall)?>">:
<input type="text" name="sds" size="1" value="<?echo date("s",$tstartall)?>"></td>
</tr>
<tr>
<td width="28%" align="right" bgcolor="#C0D9E6">End date (mm-dd-yyyy hh-mm-ss)</td>
<td width="72%" bgcolor="#C0D9E6">
<input type="text" name="edm" size="1" value="<?echo date("m",$tendall)?>">
-<input type="text" name="edd" size="1" value="<?echo date("d",$tendall)?>">
-<input type="text" name="edy" size="2" value="<?echo date("Y",$tendall)?>">
<input type="text" name="edh" size="1" value="<?echo date("h",$tendall)?>">:
<input type="text" name="edi" size="1" value="<?echo date("i",$tendall)?>">:
<input type="text" name="eds" size="1" value="<?echo date("s",$tendall)?>"></td>
</td>
</tr>
<tr>
<td width="28%" align="right" bgcolor="#F2FAFF">Substring in Host name </td>
<td width="72%" bgcolor="#F2FAFF"><input type="text" name="sthost" size="53"></td>
</tr>
<tr>
<td width="28%" align="right" bgcolor="#F2FAFF">Substring in Refferer </td>
<td width="72%" bgcolor="#F2FAFF"><input type="text" name="stref" size="53"></td>
</tr>
<tr>
<td width="28%" align="right" bgcolor="#F2FAFF">Substring in User Agent </td>
<td width="72%" bgcolor="#F2FAFF"><input type="text" name="stua" size="53"></td>
</tr>
<tr>
<td width="28%" align="right"><input type="submit" value="Submit Query" name="B1"></td>
<td width="72%"></td>
</tr>
</table>
</form>
<?
}else{
if($act=="reffersrv"){
$prmf[0]="reffersrv";$prmf[1]=" ";$prmf[2]="Sum";
outinfo("select SUBSTRING(REPLACE(REPLACE(reffer,'http://',''),'www.',''),1,LOCATE('/',REPLACE(REPLACE(reffer,'http://',''),'www.',''))) as refserv, count(*) as cnt from m_stat $qustr group by refserv order by cnt desc","1",$prmf,999,1);
}
else{ if($pageon&&!$val){
$prmf[0]="$act";$prmf[1]=" ";$prmf[2]="Sum";
outinfo("select $act, count($act) as count from m_stat $qustr group by $act order by count desc","1",$prmf,999,1);
}
if ($act=="code"&&$val){
echo'<hr noshade size="3" color="#CB3B17">';
$prmf[0]="code";$prmf[1]="Num";
outinfo("select page from m_stat where code='$val' $qustr2 order by date_time desc","0",$prmf,5);
}} }
?>
<?if(!$pageon):?>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="49%" valign="top" align="center" bgcolor="#C0D9E6"><b>Pages of site statistic</b></td><td width="2%" valign="top"> </td>
<td width="49%" valign="top" align="center" bgcolor="#C0D9E6"><b>Refferer statistic</b></td>
</tr> <tr>
<td width="49%" valign="top"><?
$prmf[0]="page";$prmf[1]="Site page";$prmf[2]="Sum";
outinfo("select page, count(page) as count from m_stat $qustr group by page order by count desc","2",$prmf,5);
?>
</td><td width="2%" valign="top"> </td>
<td width="49%" valign="top"><?
$prmf[0]="reffer";$prmf[1]="Reffere";$prmf[2]="Sum";
outinfo("select reffer, count(reffer) as count from m_stat $qustr group by reffer order by count desc","3",$prmf,5);
?>
</td>
</tr>
</table>
<p> </p>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="49%" valign="top" align="center" bgcolor="#C0D9E6"><b>Host name statistic</b></td><td width="2%" valign="top"> </td>
<td width="49%" valign="top" align="center" bgcolor="#C0D9E6"><b>User Agent statistic</b></td>
</tr> <tr>
<td width="49%" valign="top"><?
$prmf[0]="host";$prmf[1]="Host name"; $prmf[2]="Sum";
outinfo("select host, count(host) as count from m_stat $qustr group by host order by count desc","4",$prmf,5);
?>
</td><td width="2%" valign="top"> </td>
<td width="49%" valign="top"><?
$prmf[0]="uagent";$prmf[1]="User Agent";$prmf[2]="Sum";
outinfo("select uagent, count(uagent) as count from m_stat $qustr group by uagent order by count desc","5",$prmf,5);
?>
</td>
</tr>
</table>
<? endif; }
// end output stat info
// functions
function mysqlres($q) {
global $dhost,$duser,$dpass,$dname;
if(!mysql_pconnect($dhost,$duser,$dpass)) //MySql connected parameters: HostName,UserName,Password
{ echo "No database connect!<br>";
exit; }
mysql_select_db($dname) or die("No select database!<br>"); //Database name
return MYSQL_QUERY($q);
}
function outinfo($q,$id,$prmf,$mynrow=999,$hre=1)
{
$npage="npage$id";
$npages="npages$id";
global $nrow,$$npage,$$npages,$nblock,$act,$sdat;
if($mynrow==999){$mynrow=$nrow;}
if(!$$npages){$$npages=mysql_num_rows(mysqlres($q));}
if(!$$npage){$$npage=0;}
if($$npage<0){$$npage=0;}
if($$npage>=$$npages){$$npage=$$npages-$mynrow;}
echo"<table width=\"100%\">\n";
if($mynrow!=5){ echo "<tr>";
for($i=1;$i<count($prmf);$i++){
echo "<td>";
echo "<b>".$prmf[$i]."</b>";
echo "</td>";
}
echo "</tr>";
}
$r=mysqlres($q." limit ".$$npage.",$mynrow");
$f=mysql_fetch_array($r);
for($ii=0;$f;$ii++){ if($ii%2==0){$clr=" bgcolor=\"#F2FAFF\"";} else {$clr="";}
echo "<tr>";
for($i=0;$i<count($prmf);$i++){
echo "<td$clr>";
if($hre==0){echo rawurldecode($f[$i]);}else
{ echo "<a href='$self?field=$prmf[0]&fval=$f[0]&sdat=$sdat' target='_blank'>".rawurldecode($f[$i])."</a>";
}
echo "</td>";
}
echo "</tr>\n";
$f=mysql_fetch_array($r);}
echo "</table>\n";
if($mynrow!=5){
echo "<b>Start ".$$npage." from ".$$npages."</b><br> "; if($mynrow!=5){$ppp="pageon=on&";}else{$ppp="";}
if($$npage!=0){ echo "<a href='?$ppp"."act=$act&npage$id=".($$npage-$mynrow)."&sdat=$sdat'>Preview page</a> | ";}
if($$npage+$mynrow<=$$npages){ echo "<a href='?$ppp"."act=$act&npage$id=".($$npage+$mynrow)."&sdat=$sdat'>Next page</a> ";}
} else{ if($$npage+$mynrow<=$$npages&&$prmf[0]!="ncode") {echo ' <a href="'.$self.'?sdat='.$sdat.'&pageon=on&act='.$prmf[0].'"><b><font color="#617AA3">List more . . .</font></b></a>';}}
return true;}
?>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" align="center" bgcolor="#617AA3"><a href="http://www.maryan.com.ua"><b><font color="#FFFFFF">www.maryan.com.ua</font></b></a></td>
</tr>
<tr>
<td width="100%" align="center">© 2001 Copyright Andrew MARYAN Internet
studio. All rights reserved.</td>
</tr>
</table>