poemlo代码分析系列2————web-server介绍,代码,工具,方法

通过perl统计日志中请求/响应,获取单个用户成功/失败的操作次数。
use Encode;

#$WORK_HOME="G:/Cache/120876992/study_perl";
$WORK_HOME="/lc/log";
#$inFile="$WORK_HOME/yndx-log.log";
$inFile=<STDIN>;
$outFile="$WORK_HOME/out.tmp";
$outTjResult="$WORK_HOME/tj_result.txt";
$reqStr="- Req: ";
$onlineDelStr="宽带在线用户记录删除结果:returncode=";
$clearBindStr="宽带用户自动绑定属性操作结果:returncode=";

#删除临时文件
sub deleteOldTmpFile{
	unlink $outFile,$outTjResult;
}


#将原始日志精简后生成临时文件
sub initLog{
	open(INFILE,"<$inFile")||die "can‘t open myfile:$!";
	open(OUTFILE,">>$outFile")||die "can‘t open myfile:$!";
	while($line=<INFILE>)
	{
		if (index($line,$reqStr)>0) {
			print OUTFILE $line;
		}elsif (index($line,$onlineDelStr)>0) {
			print OUTFILE $line;
		}elsif (index($line,$clearBindStr)>0) {
			print OUTFILE $line;
		}
	}
	close(INFILE);
	close(OUTFILE);
}


sub trim
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    if(length($string)<1){
        return " ";
    }else{
        return $string;
    }    
}    

#calc user succ/fail num
sub parse{
	($line,$tjType,$username,$grepStr)=@_;
	$grepStr=encode("gbk", decode("utf8", $grepStr));
	if (index($line,$grepStr)>0){
		#print $line."---\n";
		#response data
		$succFlag=substr($line,-1);
		#print $succFlag."====\n";
		$key=$tjType.":::".$username.":::".$succFlag;
		if(exists ($map{$key}))
		{
			$v=$map{$key};
			$v++;
			$map{$key}=$v;
		}else{
			$map{$key}=1;
		}
	}
}


print "------------Start process------------\n";
&deleteOldTmpFile();
&initLog();


%map=();
open(MYFILE,"$outFile")||die "can‘t open myfile:$!";
$tmpLine="";
$tmpUsername="";
$num=0;
while($line=<MYFILE>)
{
	
	$line=trim($line);
	$line=encode("gbk", decode("utf8", $line));
	if ($line=~m/.*$reqStr.*/) {
		if (index($line,"username")<=0) {
			next;
		}

	
		my @name=split "username=",$line;
		$tmpUsername=substr($name[1],0,index($name[1]," "));
		#print "username=".$tmpUsername."\n";
		next;
	}	

	&parse($line,"onlineDel",$tmpUsername,$onlineDelStr);
	&parse($line,"clearBind",$tmpUsername,$clearBindStr);
	
}
close(MYFILE);



print "-----------Total Calc--------------\n";
open(OUTTJRESULT,">$outTjResult")||die "can‘t open myfile:$!";
$result="";
while (($key, $value) = each(%map) ) {
	$result=$key."----".$value."\n";
	print $result;
	print OUTTJRESULT $result;
}
close(OUTTJRESULT);





poemlo代码分析系列2————web-server介绍,代码,工具,方法

上一篇:php文件链接数据库基本代码


下一篇:推荐一些编程学习网站