此配置会建立文件/var/logs/logfile.nnnn,其中的nnnn是名义上的日志启动时的系统时间 (此时间总是回卷时间的倍数,可以用于cron脚本的同步)。
在回卷时间到达时(在此例中是24小时以后),会产生一个新的日志。
CustomLog "|/opt/apache/bin/rotatelogs /opt/apache/logs/access_log.%d-%m-%y 800M" common
#!/usr/bin/perl
my (%month,%date_log);
BEGIN{
$month{Jan}="01";$month{Feb}="02";$month{Mar}="03";
$month{Apr}="04";$month{May}="05";$month{Jun}="06";
$month{Jul}="07";$month{Aug}="08";$month{Sep}="09";
$month{Oct}="10";$month{Nov}="11";$month{Dec}="12";
}
my $hostip=GetIp();
my ($
hostname)=$hostip=~/.(d+)$/;
my $localt;
#print "$hostipt$hostnamen";
my ($file,$flag)=('',0);
my $tmpfile="";
while(1){
system("mkdir /var/apache_log") unless(-e '/var/apache_log');
system("mkdir /var/tmp") unless(-e '/var/tmp');
my ($sec,$min,$hour,$day,$mon,$year) = localtime();
$mon++;$year -= 100;
$year="0$year" if($year<10);
$mon="0$mon" if($mon<10);
$day="0$day" if($day<10);
$tmpfile=$hostname.'_'.$year.$mon.$day.".log";
if($hour>0 && $hour<2){
if($flag==0){
$file='';
my $back_file="_$year$mon$day";
system("cp /usr/local/apache2/logs/access_log /var/tmp/$back_file");
split_apache_log($hostname,'/usr/local/apache2/logs/access_log');
$flag=1;
}
}else{
#print $flag,"n";
$flag=0 if($flag==1);
}
sleep(1800);
}
#-------------------------------------
sub split_apache_log{
my ($hostid,$access_log)=@_;
my %hash=();
my $flag_new_log=0;
my $localt = localtime();
printlog("nsplit apapche log at $localtnn");
open(LIST,"$access_log")||die "$!";
while(<LIST>){
chomp;
my (@array)=$_=~/^(S+)s+S+s+S+s+[(S+).+]s+"(.+)"s+(d+)s+S+/;
next if($array[3] eq '404');
my $time_str=$array[1];
$time_str=~s///t/g;
$time_str=~s/:/t/g;
my @t_s=split(/t/,$time_str);
next if(not exists $t_s[2]);
$t_s[2]=substr($t_s[2],2,2);
my $date_tmp="$t_s[2]$month{$t_s[1]}$t_s[0]";
$file=$hostid.'_'."$date_tmp.log";
push(@{$hash{$file}},$_);
if(scalar(@{$hash{$file}})>=20000){
my @arraytmp=splice(@{$hash{$file}}, 0, 20000);
my $line=join("n",@arraytmp);
if(-e "/var/log/apache/$file"){
open(OUT,">>/var/log/apache/$file");
print OUT "$linen";
close (OUT);
}else{
$flag_new_log=1 if($flag_new_log==0);
open(OUT,">>/var/apache_log/$file");
print OUT "$linen";
close (OUT);
}
}
}
close(LIST);
foreach $file(sort {$a cmp $b} keys %hash){
next if(not exists ${$hash{$file}}[0]);
my $line=join("n",@{$hash{$file}});
if(-e "/var/log/apache/$file"){
open(OUT,">>/var/log/apache/$file");
print OUT "$linen";
close (OUT);
}else{
$flag_new_log=1 if($flag_new_log==0);
open(OUT,">>/var/apache_log/$file");
print OUT "$linen";
close (OUT);
}
}
if($flag_new_log==1){
system("cat /var/apache_log/$tmpfile>$access_log");
system("rm -fr /var/apache_log/$tmpfile");
system("mv /var/apache_log/* /var/log/apache/");
}
$localt = localtime();
printlog("nFinished split apapche log at $localtnn");
}
#-----------------------------------
sub GetIp{
my $ifc=`ifconfig`;
my ($ip,$ip1,$ip2);
if($ifc=~/
eth0/){
my $str=`ifconfig eth0` ;
($ip1)=$str=~/inet addr:(S+)/ ;
$ip=$ip1;
}
if($ifc=~/eth1/){
my $str=`ifconfig eth1` ;
($ip2)=$str=~/inet addr:(S+)/ ;
if($ip1 && ($ip1 ne $ip2)){
$ip="$ip1 $ip2" ;
}else{
$ip=$ip2;
}
}
return $ip;
}
#--------------------------
sub printlog{
my $str=shift;
open(LOGOUT,">>/var/lib/SplitApacheLog.log");
print LOGOUT "$str";
close LOGOUT;
}