-->
几台服务器的apache日志使用awstat进行统计,现在需要将每台主机的每日访问文件数按日期排序,打印成一张表:
awstat生成的数据文件的文件名格式为:awstats102008.hostname.txt
20081017
2826794 2826794 340568061 59
20081018
2736667 2736667 336467614 58
20081019
3314638 3314638 352853200 56
需要获取的数据为上面的第一二列,即日期和文件数
#!/usr/bin/perl -w
use strict;
my $data_dir="/opt/awstats/dirdata";
chdir $data_dir;
my %data;
my $month=shift;
my $year="2008";
my $mod=shift;
my @hosts;
@hosts=(21..28) if $mod eq "m";
@hosts=(31..38) if $mod eq "t";
print "<html>
<head>
<title>access log statistic</title>
</head>
<body>
<h1>access log statistic on $month $year</h1>
<h3><a href='/'>return to
index</a></h3>
<table border=1>
";
foreach my $host (@hosts){
my
$file="awstats".$month.$year.".".$host.".txt";
next unless -f $file;
open FD,$file;
while(<FD>){
my
$arrary=$_;
if ($arrary
=~ /^$year$month/){
my
@tmp=split /\s+/,$arrary;
my
$date=shift @tmp;
my
$num=shift @tmp;
$data{"d".$date}{"h".$host}=$num;
}
}
close FD;
}
print "<tr>\n";
print "<th>date</th>\n";
foreach my $host (@hosts){
print
"<th>$host</th>\n";
}
print "</tr>\n";
foreach my $date (sort keys %data){
print
"<tr>\n";
print
"<td>$date</td>\n";
foreach my
$host (@hosts){
print
"<td align='right'>
",$data{$date}{'h'.$host}?$data{$date}{'h'.$host}:0,"
</td>\n";
}
print
"</tr>\n";
}
print "</table>
<h3><a href='/'>return to
index</a></h3>
</body>
</html>";
生成的结果如下:
access statistic on 12 2008
return to index
| date | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 |
|---|---|---|---|---|---|---|---|---|
| d20081201 | 3889363 | 3510197 | 3822591 | 3705947 | 3648502 | 3507673 | 3575994 | 3595313 |
| d20081202 | 3460608 | 3584238 | 3793175 | 3847570 | 3541112 | 4261298 | 3440269 | 3479714 |
| d20081203 | 3543943 | 3480454 | 3747391 | 3865192 | 3400915 | 3621912 | 3579295 | 3459238 |
| d20081204 | 3426827 | 3319026 | 3290832 | 3301444 | 3290683 | 3437950 | 3378622 | 3384148 |
| d20081205 | 3029773 | 3124348 | 3057467 | 3026183 | 3367647 | 3028501 | 3196543 | 3221404 |
| d20081206 | 2816795 | 2872855 | 2990546 | 3240831 | 2965240 | 2813721 | 2821424 | 3318829 |

发表评论