通过perl统计日志中请求/响应,获取单个用户成功/失败的操作次数
发布时间:2020-12-16 00:03:07 所属栏目:大数据 来源:网络整理
导读:通过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_H
通过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",$line)); if ($line=~m/.*$reqStr.*/) { if (index($line,"username")<=0) { next; } my @name=split "username=",$line; $tmpUsername=substr($name[1],index($name[1]," ")); #print "username=".$tmpUsername."n"; next; } &parse($line,"onlineDel",$tmpUsername,$onlineDelStr); &parse($line,"clearBind",$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); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |