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);





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。