perl时间函数与日志分析脚本

#!/usr/bin/perl
#--------------------------------------#
# Script Name: AccessLog.pl            #
# Writer by : Tat                      #
# Create time: 2012-03-27              #
# Last update: 2012-03-30              #
# Ver: 0.1 beta                        #
#--------------------------------------#
use strict;
use warnings;
#----------数据统计变量----------------
my $sum = 0;          #总日志数量
my $code5 = 0;        #Http 500错误页
my $code2 = 0;        #Http 200页
my $tophome=0;        #进入主页数据
#----------条件匹配变量---------------
my $home = "/home.htm";        #进入主页匹配
#-------------时间涵数------------------
sub getTime(){
 (my $sec,my $min,my $hour,my $day,my $mon,my $year,
  my $weekday,my $yeardate,my $savinglightday) = (localtime(time-86400));#time-86400前一天
 $sec = ($sec < 10)? "0$sec":$sec;
 $min = ($min < 10)? "0$min":$min;
 $hour = ($hour < 10)? "0$hour":$hour;
 $day = ($day < 10)? "0$day":$day;
 $mon = ($mon < 9)? "0".($mon+1):($mon+1);
 $year += 1900;

 my $now = "$year-$mon-$day";
 return $now;
}
#------------其他变量-----------------
my $namemin=getTime();
my $filename="access.$namemin.log";
my $tmpfile="/tmp/access.log";
my $logfile=$tmpfile;

if(-e $tmpfile)
{
   system("rm $tmpfile");
}
system("find /logs/*/ -name $filename|xargs cat |
        awk \'{print \$1 \" \" \$2 \" \" \$3 \
               " \" \$6 \" \" \$10 \" \" \$12}\' >> $tmpfile");

open(INFILE,$logfile)||die "can't open ddns_log file $logfile: $!\n"; 

line: while(my $line=<INFILE>){
        $line =~ /^\[
                ([\d\w:\/]+)\s\+\d{4}\]         #Date matching
        \s+
        (\d+)                        #Http Status code matching
        \s+
        (\d+|-)                            #phone
        \s+
        (\w+)                              #Request type
        \s+
            (\/([^\s]*)|.*)                    #url
            $/ix or do
        {
        next line;

        };

    if ($2 >=500){
       $code5 ++;
    }
    if($2 == 200){
       $code2 ++;
    }
    if($4 eq "GET" && $5 eq $home){
      $top ++;
    }    

   $sum ++;
}

print "####################【日志统计】#####################\n";
print "总处理日志条数:: $sum\n";
print "页面50X代码条数:: $code5\n";
print "页面200代码条数 :: $code2\n";
print "##########################################################\n";
system("rm $tmpfile");

脚本是用来统计一个access日志用户行为的初本,由于考虑机密问题只放出初本供大家参考。

tomcat日志格式

pattern=”%t %s %T %b %{x-up-calling-line-id}i %{x-up-bear-type}i %a %{x-forwarded-for}i %m [%{HOST}i] %U%q [%{User-Agent}i] [%{Accept-Encoding}i]“

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*