Awstats total for day 脚本改装

awstats(中文网站:http://www.awstats.cn/)是GNU许可的免费软件,非常强大的日志分析工具,目前我们运维的分域名众多也是通过它来进行统一web日志分析和管理的。
awstats Totals(http://www.telartis.nl/xcms/awstats)是一个在awstats上的小的辅助工具,一个简单的PHP脚本,他通过分析awstats的数据库文件,把所有统计网站按照月份整理出来,提供浏览排列和月份查询。
根据报表和检索的需要,我对awstats Totals做了一些修改之后,提供了精细一些的按日期进行所有分统计网站的数据报表,提供和其相同形式的排http://www.wp1998.cn/wordpress/wp-admin/post.php?action=edit&post=139&message=1列和按日期查询。暂且称之为awstats totals for day,其实这是很早写的一个php脚本,今天有人问起来了就整理了一下发出来,可能有些使用awstats的同志需要。
php代码如下,也可以直接浏览http://www.wp1998.cn/awstatsdaytotals.phps

<?php
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Script System Name:                                                  ::
:: (c) 2003-2005 Heinet developer (www.heinet.cn)                       ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Author:  wp1998 (wp1998@e21.edu.cn)                                  ::
:: Version:            Time:                                            ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/
// Filename        :    awstatsdaytotals.phps
// introduce    :
/*******************************************************
* SETUP SECTION
*******************************************************/
/**
* Set this value to the directory where AWStats
* saves its database and working files into.
*/
$DirData = ‘/data/logweb/awstats/DATABASE’;
/**
* The URL of the AWStats script.
*/
$AWStatsURL = ‘/cgi-bin/awstats.pl’;
include(“now_request_key.inc.php”);
if (isset($_GET[“year”])) $year = $_GET[“year”]; else $year = date(“Y”,(time()-24*3600));
if (isset($_GET[“month”])) $month = $_GET[“month”]; else $month = date(“m”,(time()-24*3600));
if (isset($_GET[“day”])) $day = $_GET[“day”]; else $day = date(“d”,(time()-24*3600));
//echo $year.$month.$day;
if (isset($_GET[“sort”])) $sort = $_GET[“sort”]; else $sort = “bandwidth”;
function get_config($file) {
$r = ”;
if (preg_match(‘/awstats\d{6}\.(.+)\.txt/’, $file, $match)) $r = $match[1];
return $r;
}
function read_history($file,$dateStr) {
$f = fopen($file, “r”);
$s = fread($f, filesize($file));
fclose($f);
$visits_total = 0; $unique_total = 0; $pages_total = 0; $hits_total = 0; $bandwidth_total = 0;
$config = get_config($file);
/*
if (preg_match(‘/TotalVisits (\d+)/’, $s, $match)) $visits_total = (int)$match[1];
if (preg_match(‘/TotalUnique (\d+)/’, $s, $match)) $unique_total = (int)$match[1];
if (preg_match(‘/\nBEGIN_DAY \d+\n(.*)\nEND_DAY\n/s’, $s, $match)) {
foreach (explode(“\n”, $match[1]) as $row) {
list($date, $pages, $hits, $bandwidth, $visits) = split(‘ ‘, $row);
$pages_total += $pages; $hits_total += $hits; $bandwidth_total += $bandwidth;
}
}
*/
if (preg_match(‘/’.$dateStr.’ (\d+) (\d+) (\d+) (\d+)/’, $s, $match)){
$pages_total=$match[1];
$hits_total=$match[2];
$bandwidth_total=$match[3];
$visits_total=$match[4];
//echo $bandwidth_total.”<BR>”;
}
return array(“config”=>$config, “visits”=>$visits_total, “unique”=>$unique_total,
“pages”=>$pages_total, “hits”=>$hits_total, “bandwidth”=>$bandwidth_total);
}
$dirfiles = array();
$dir = @opendir($DirData);
if (!$dir) die(“Could not open directory $DirData”);
while ($file = readdir($dir)) $dirfiles[] = $file;
$files = array();
$config = array();
//if ($month == ‘all’) $pat = ‘\d{2}’; else $pat = substr(“0”.$month, -2);
if ($month == ‘all’) $pat = ‘\d{2}’; else $pat = $month;
$pat = ‘/awstats’.$pat.$year.’\.(.+)\.txt/’;
//echo $pat;
//exit;
foreach ($dirfiles as $file) if (preg_match($pat, $file, $match)) {
$config = $match[1];
if (!$FilterConfigs || in_array($config, $FilterConfigs)) {
$configs[] = $config;
$files[] = $file;
}
}
$rows = array();
if ($files) {
array_multisort($configs, $files);
$visits_total = 0; $unique_total = 0; $pages_total = 0; $hits_total = 0; $bandwidth_total = 0;
$row_prev = array();
for ($i = 0, $cnt = count($files); $i <= $cnt; $i++) {
$row = array();
if ($i < $cnt) {
$row = read_history($DirData.’/’.$files[$i],$year.$month.$day);
$visits_total += $row[“visits”];
$unique_total += $row[“unique”];
$pages_total += $row[“pages”];
$hits_total += $row[“hits”];
$bandwidth_total += $row[“bandwidth”];
}
if ( isset($row[“config”]) && isset($row_prev[“config”]) &&
($row[“config”] == $row_prev[“config”]) ) {
$row[“visits”] += $row_prev[“visits”];
$row[“unique”] += $row_prev[“unique”];
$row[“pages”] += $row_prev[“pages”];
$row[“hits”]  += $row_prev[“hits”];
$row[“bandwidth”] += $row_prev[“bandwidth”];
} elseif ($i > 0) $rows[] = $row_prev;
$row_prev = $row;
}
}
function multisort(&$array, $key) {
$cmp = create_function(‘$a, $b’,
‘if ($a[“‘.$key.'”] == $b[“‘.$key.'”]) return 0;’.
‘return ($a[“‘.$key.'”] > $b[“‘.$key.'”]) ? -1 : 1;’);
usort($array, $cmp);
}
if ($sort == “config”) sort($rows); else multisort($rows, $sort);
function byte_format($number, $decimals = 2) {
//global $dec_point, $thousands_sep;
// kilo, mega, giga, tera, peta, exa, zetta, yotta
/*
$prefix_arr = array(”,’k’,’M’,’G’,’T’,’P’,’E’,’Z’,’Y’);
$i = 0;
if ($number == 0) $result = 0; else {
$value = round($number, $decimals);
while ($value > 1024) { $value /= 1024; $i++; }
$result = number_format($value, $decimals, $dec_point, $thousands_sep);
}
$result .= ‘ ‘.$prefix_arr[$i].’B’; if (!$i) $result .= ‘ytes’;
*/
if($number<1024) {
$result=$number.”Btyes”;
} else if($number>=1024 && $number<(1024*1024)) {
$result=round($number/1024,2).”KB”;
} else if($number>=(1024*1024) && $number<(1024*1024*1024)) {
$result=round($number/(1024*1024),2).”MB”;
} else {
$result=round($number/(1024*1024*1024),2).”GB”;
}
return $result;
}
function num_format($number, $decimals = 0) {
//return number_format($number, $decimals, $dec_point, $thousands_sep);
return number_format($number);
}
?>
<html>
<head>
<title>每日各分网站访问统计</title>
<style type=”text/css”>
body { font: 12px arial,verdana,helvetica,sans-serif; background-color: white }
td   { font: 12px arial,verdana,helvetica,sans-serif; text-align: center; color: black }
.l { text-align: left }
.b { background-color: #ccccdd; padding: 2px; margin: 0 }
.d { background-color: white }
.f { font: 14px verdana,arial,helvetica }
a  { text-decoration: none }
a:hover { text-decoration: underline }
a.h  { color: black }
</style>
</head>
<body>
<form action=”<?=$SCRIPT_NAME?>”>
<table border=0 cellpadding=2 cellspacing=0 width=”100%”>
<tr><td>
<table border=0 cellpadding=8 cellspacing=0 width=”100%”>
<tr>
<th>选择查询的日期:</th>
<td>
<?
echo “<select name=year>\n”;
for ($curyear = date(“Y”), $i = $curyear – 3; $i <= $curyear; $i++) {
echo “<option value=$i”; if ($year == $i) echo ” selected”; echo “>$i\n”;
}
echo “</select> 年 “;
echo “<select name=month>\n”;
for ($i = 1; $i <= 12; $i++) {
echo “<option value=”;
if($i<10){
echo “0”.$i;
} else {
echo $i;
}
if ($month == $i) echo ” selected”; echo “>$i\n”;
}
echo “</select> 月 “;
echo “<select name=day>\n”;
for ($i = 1; $i <= 31; $i++) {
echo “<option value=”;
if($i<10){
echo “0”.$i;
} else {
echo $i;
}
if ($day == $i) echo ” selected”; echo “>$i\n”;
}
echo “</select> 日 “;
?>
<input type=submit value=”OK”>
</td></tr>
</table>
</td></tr>
</table>
</form>
<table align=center>
<tr>
<? $url = “$SCRIPT_NAME?month=$month&year=$year&day=$day&sort=”; ?>
<td>排名</td>
<td>网站名称</td>
<td bgcolor=#ECECEC nowrap>&nbsp;<a href=”<?=$url?>config”>统计网站</a>
<!– <td width=80 bgcolor=#FFB055><a href=”<?=$url?>unique”><?if ($sort==”unique”) echo “<b>”;?>参观者<?if ($sort==”unique”) echo “</b>”;?></a> –>
<td width=80 bgcolor=#F8E880><a href=”<?=$url?>visits”><?if ($sort==”visits”) echo “<b>”;?>参观人次<?if ($sort==”visits”) echo “</b>”;?></a>
<td width=80 bgcolor=#4477DD><a href=”<?=$url?>pages”><?if ($sort==”pages”) echo “<b>”;?>网页数<?if ($sort==”pages”) echo “</b>”;?></a>
<td width=80 bgcolor=#66F0FF><a href=”<?=$url?>hits”><?if ($sort==”hits”) echo “<b>”;?>文件数<?if ($sort==”hits”)
echo “</b>”;?></a>
<td width=80 bgcolor=#2EA495><a href=”<?=$url?>bandwidth”><?if ($sort==”bandwidth”) echo “<b>”;?>字节<?if ($sort==”bandwidth”) echo “</b>”;?></a>
<?
$i=1;
foreach ($rows as $row) {
$SiteArray=give_SiteArray($row[“config”]);
//echo “<tr><td>”.$i.”</td><td>”.$SiteArray[“SiteName”].”</td><td><a href=$AWStatsURL?month=$month&year=$year&config=”.
$row[“config”].”>”.$row[“config”].”</a><td>”.num_format($row[“unique”]).
“<td>”.num_format($row[“visits”]).”<td>”.num_format($row[“pages”]).
“<td>”.num_format($row[“hits”]).”<td>”.byte_format($row[“bandwidth”]).”\n”;
echo “<tr><td>”.$i.”</td><td>”.$SiteArray[“SiteName”].”</td><td><a href=$AWStatsURL?month=$month&year=$year&config=”.$row[“config”].”>”.$row[“config”].”</a><td>”.num_format($row[“visits”]).”<td>”.num_format($row[“pages”]).”<td>”.num_format($row[“hits”]).”<td>”.byte_format($row[“bandwidth”]).”\n”;
$i=$i+1;
}
echo “<tr><td>&nbsp;</td><td>&nbsp;</td><td bgcolor=#ECECEC>&nbsp;Total<td bgcolor=#ECECEC>”.num_format($visits_total).”<td bgcolor=#ECECEC>”.num_format($pages_total).
“<td bgcolor=#ECECEC>”.num_format($hits_total).”<td bgcolor=#ECECEC>”.byte_format($bandwidth_total).”\n”;
?>
</table>
</body>
</html>

发表评论