由前端埋点,获得的日志,就perl简单进行处理。大概格式如下。
0.000 - 172.21.10.66 - - [16/Jan/2017:00:01:09 +0800] count-log.csyd.com "GET /count.js?webID=WEBID&webTitle=WEBTITLE&upURL=UPURL&userID=USERID&machineScreen=MACHINESCREEN&webBrowser=WEBBROWSER&webAppver=WEBAPPVER&machineSystem=MACHINESYSTEM&webLanguage=WEBLANGUAGE&cookieEnabled=COOKIEENABLED&userAgent=USERAGENT&openTime=OPENTIME&upOpenTime=UPOPENTIME&webURL=WEBURL&isNewUser=ISNEWUSER&upStayTime=UPSTAYTIME&zIndex=ZINDEX&userPlat=USERPLAT&mark_1=&mark_2=&mark_3=&mark_4=&mark_5=&mark_6=&mark_7=&mark_8=&mark_9=&mark_10= HTTP/1.0" 200 247 "http://blog.csdn.net/chsyd1028" "Mozilla/5.0 (Linux; Android 6.0; Le X620 Build/HEXCNFN5902012151S; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 Mobile MQQBrowser/6.2 TBS/043015 Safari/537.36 MicroMessenger/6.5.3.980 NetType/WIFI Language/zh_CN" "223.96.64.15, 101.226.61.193" - "172.21.10.69"
所有=后面数值的部分我都用大写形式代替了。 需要取出的就是这部分数据。
代码:
#!/usr/bin/perl
use strict;
use warnings;
use Date::Parse;
use 5.010;
use POSIX;
use URI::Escape;
my $processDate = strftime("%Y-%m-%d",localtime()); #当前日期
my $webID=""; #站点id
my $ip=""; #ip
my $userID = ""; #userID,最后不需要输出
my $accessTime=""; #访问时间,用的服务器时间
my $webURL=""; #URL
my $upWeb=""; #上一级网址
my $downWeb=""; #下一级网址
my $stayTime=""; #网页停留时间
my $webSign=""; #网站标识
my $visitorSign=""; #访客标识
my @arrayAll=""; #这个数组存放数组,每个数组里都有userID、upURL、webURL、downURL、accessTime
my $index=0;
#站点id
sub webID{
my $line = $_[0];
if ($line =~ /webID=(.*?)\./) {
$1;
}else{
"";
}
}
#访问时间
sub accessTime{
my $line=$_[0];
my $unix_timestamp;
if ($line =~ /\[(.*?)\]/){
#print $1."\n";
$unix_timestamp = str2time($1);
#print $unix_timestamp."\n";
}else{
"";
}
}
#ip
sub ip{
my $line=$_[0];
if ($line =~ /([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})"\s-/){
#print $1."\n";
$1;
}else{
"";
}
}
#访客标识
sub visitorSign{
my $line=$_[0];
if ($line =~ /&isNewUser=(.)&userPlat/){
#print $1."\n";
$1;
}else{
"";
}
}
#上一级网址
sub upWeb{
my $url;
my $line=$_[0];
if ($line =~ /&upURL=(.*?)&userID/){
#print $1."\n";
$url = uri_unescape($1);
$url;
}else{
"";
}
}
#URL
sub webURL{
my $url;
my $line=$_[0];
if ($line =~ /&webURL=(.*?)&isNewUser/){
#print $1."\n";
$url = uri_unescape($1);
$url;
}else{
"";
}
}
#下一级网址和停留时间
sub downWeb{
if ($index != 0) {
my $i = $index-1;
while ($i >= 0) {
if (!$arrayAll[$index][5]eq"" and $arrayAll[$index][2].$arrayAll[$index][1]eq$arrayAll[$i][2].$arrayAll[$index][1] and $arrayAll[$index][5]eq$arrayAll[$i][4]) {
#判断条件:1.有上一级url 2.这条记录的userID连接IP和上条记录的一样 3.这条的upWeb和上一条的weburl一样(这里的上一条是指从最近的一条到第1条判断)
if ($arrayAll[$i][6]eq"") { #判断这条记录的nextUrl有没有值,没有值就存进去
$arrayAll[$i][6] = $arrayAll[$index][4]; #把这级的网址存到他上一级网址的下一级网站中
$arrayAll[$i][7] = strftime("%H:%M:%S", gmtime($arrayAll[$index][3] - $arrayAll[$i][3])); #计算这级的访问时间-上级访问时间,结果放到上级的stayTime中
last;
}
}
$i--;
}
}
}
#userID
sub userID{
my $line=$_[0];
if ($line =~ /&userID=(.*?)&machineScreen/){
#print $1."\n";
$1;
}else{
"";
}
}
#网页停留时间
#sub stayTime{
#
#}
#网站标识
sub webSign{
my $line=$_[0];
if ($line =~ /&userPlat=(.*?)&mark_1/){
#print $1."\n";
$1;
}else{
"";
}
}
while (defined (my $eachline =<>)) {
if ($eachline=~/webID.*?&webTitle.*?&upURL/) {
chomp $eachline;
$userID = &userID($eachline);
$webID = &webID($eachline);
$accessTime = &accessTime($eachline);
$ip = &ip($eachline);
$visitorSign=&visitorSign($eachline);
$upWeb = &upWeb($eachline);
$webURL=&webURL($eachline);
$webSign=&webSign($eachline);
$arrayAll[$index] = [$webID,$ip,$userID,$accessTime,$webURL,$upWeb,$downWeb,$stayTime,$webSign,$visitorSign];
&downWeb;
$index++;
}
}
my $out = strftime("%Y%m%d",localtime())."_log.dat";
open (OUT, ">E:/工作文档/日志/运行和成功/$out")||die"can not open the file: $!";
my $indexArray=0;
while ($indexArray<@arrayAll) {
if ($arrayAll[$indexArray][4] =~ s/\?.*//) {}
if ($arrayAll[$indexArray][5] =~ s/\?.*//) {}
if ($arrayAll[$indexArray][6] =~ s/\?.*//) {}
print OUT $processDate."\#".$arrayAll[$indexArray][0]."\#".$arrayAll[$indexArray][1]."\#".$arrayAll[$indexArray][2]."\#".strftime("%Y-%m-%d %H:%M:%S",localtime($arrayAll[$indexArray][3]))."\#".$arrayAll[$indexArray][4]."\#".$arrayAll[$indexArray][5]."\#".$arrayAll[$indexArray][6]."\#".$arrayAll[$indexArray][7]."\#".$arrayAll[$indexArray][8]."\#".$arrayAll[$indexArray][9]."\n";
$indexArray++;
}
close OUT;