一个与awstats相关的小报表

| 暂无评论 | 暂无引用通告

-->

几台服务器的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>";


运行perl awstat_html.pl 12 m

第一个参数是月份,第二个参数是服务器分组,我在脚本里定义了两个分组

生成的结果如下:

 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


将就着看,呵呵。

 

暂无引用通告

发送引用通告网址: http://supersun.info/mt/mt-tb.cgi/908
如果您想引用这篇日记到您的Blog,请复制上面的链接,放置到您发表文章时的相应界面中。

发表评论

最新资源

  • IMG_1437.JPG
  • line.png
  • bar.png
  • perl_calander.jpg

关于此日记

此日记由 supersun 发表于 2008年12月25日 17:46

此Blog上的上一篇日记jdk快速安装脚本

此Blog上的下一篇日记使用模块Tie::File 将文件当着数组来操作

首页归档页可以看到最新的日记和所有日记。