8 #=======================CONFIGURATION BEGIN============================
11 $host = "localhost"; # host s DB
12 $port = "3306"; # port DB
13 $user = "squid"; # username k DB
14 $pass = "ASxjC7Gftx8IvRfF"; # pasword k DB
15 $db = "squid"; # name DB
17 #==========================================================
18 #Path to access.log. It could be full path, e.g. /var/log/squid/access.log
19 #Put k access.log. Eto mozhet bit polnii put. Naprimer, /var/log/squid/access.log
20 #Путь к файлу access.log(имя может другим). Это может быть и полный путь, например, /var/log/squid/access.log
22 my $filetoparse="/var/log/squid/access.log";
23 #==========================================================
25 #How older data must be deleted. In example, older than 100 days from max date.
26 #Period, starshe kotorogo dannie budut udaliatsia. Ukazivaetsia v dniah.
27 #Период, старше которого данные будут удаляться. Указывается в днях.
29 my $deleteperiod=120; #days
30 #==========================================================
31 #min bytes of traffic in one record to write into DB. By default - all data stored.
33 my $minbytestoparse=-1; #bytes, default -1
35 #=======================CONFIGURATION END==============================
45 #datetime when parse started
49 $dbh = DBI->connect("DBI:mysql:$db:$host:$port",$user,$pass);
51 $sth = $dbh->prepare("SELECT ifnull(unix_timestamp(max(access_date))-$overlap,0) from access_log");
53 @row=$sth->fetchrow_array;
57 $sth = $dbh->prepare("delete from access_log where access_date < date_sub(now(), interval $deleteperiod day)");
61 #open log file for reading
62 open(IN, "<$filetoparse");
72 #loop for get strings from file one by one.
73 while (my $line=<IN>) {
75 if(time > $seconds+1) {
77 $insertspeed=$countinsert;
81 @item = split " ", $line;
85 if (($bytes>0) && ($time>$lastdate)) {
87 $client_ip = $item[2];
88 $http_code = $item[3];
91 @matches = ($url=~ /(?:^[a-z][a-z0-9+\-.]*:\/\/)?(?:[a-z0-9\-._~%!$&'()*+,;=]+(?::.*)?@)?([a-z0-9\-._~%]+|\[[a-z0-9\-._~%!$&'()*+,;=:]+\])/i);
96 $sqltext=$sqltext.",($time,$ms,'$client_ip','$http_code',$bytes,'$method','$host','$user','$mime')";
98 $sqltext="($time,$ms,'$client_ip','$http_code',$bytes,'$method','$host','$user','$mime')";
101 if ($sqlspool > $sqlbatch) {
102 $sqltext = "insert into tmp_traffic(timestamp,process_time,client_host,http_code,bytes,http_method,server_host,username,mime_type) values".$sqltext;
103 $sth = $dbh->prepare($sqltext);
108 print "Completed: ".$countlines." ".$insertspeed." lines/sec\r";
115 $sqltext = "insert into tmp_traffic(timestamp,process_time,client_host,http_code,bytes,http_method,server_host,username,mime_type) values".$sqltext;
116 $sth = $dbh->prepare($sqltext);
123 $sth = $dbh->prepare("CALL Process_Traffic();");
126 $rc = $dbh->disconnect;