Apache 日志统计perl脚本

#!/usr/bin/perl -w

#use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use POSIX;

$logfile=strftime("%Y-%m-%d",localtime);

my $param_d = param("d");
my $param_t = param("t");

@tfix=qw(01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24);

if ( $param_d ){
        $logfile=$param_d;
}

if ($param_t){
        @tfix=split(/,/,$param_t);
}


$log_name="/usr/local/apache2/logs/$logfile/access_log";

print header, start_html("Downloads count");
print "Use eg: http://211.94.156.162/cgi-bin/detailcount.cgi?d=2009-11-26&t=01,02<br><br>";

print strftime("NOW: %Y-%m-%d %H:%M:%S\n<br>",localtime);
print "Qurey: date<$logfile>    hours <@tfix> <br>";

%ip_count=();
$totle=0;
$i = 0;

print ("Valid hours:< ");

foreach $filesuf (@tfix){

                if (open (file_log,$log_name.".".$filesuf)){
#                 print ("Logfile: $log_name\.$filesuf\n<br>");
                    print (" $filesuf");
                while ($line_log = <file_log>)
                {

                 if($line_log=~    qr/n\.sis/){
                 #print $line_log;
                 @log_ip = split(/\s+/,$line_log);
                 $ip_count{$log_ip[0]}+=1;
                 }
                }
                close (file_log);
                }else{
#                 print ("<br>Open log file :$filesuf failed");
                }

}

print ("><br>");

print("<br>Capitalized ip and number of occurrences: \n<br>");


print "<table border=1px    cellSpacing=0 cellPadding=0 width=200px>";
foreach $cap_ip (keys(%ip_count))
{
        print("<tr>");
        #print("$cap_ip <--------->    $ip_count{$cap_ip} \n<br>");
        printf("<td>%-17s</td><td>%s</td>",$cap_ip,$ip_count{$cap_ip});
        print("</tr>");
        $totle += $ip_count{$cap_ip};
        $i +=1;
}

print "</table>";

print "\n----------------------------\n<br>";
print     "    Totle downloads : $totle <br>";
print     "    Totle ip             : $i        <br> ";

print end_html;

你可能感兴趣的:(apache,日志,脚本,perl,休闲)