Squid report generator (MySQL storage, reports/graphs online).
[squid-reports.git] / fetch / fetch.pl
1 #!/usr/bin/perl
2
3 #build 20191105
4
5 use DBI; # DBI  Perl!!!
6 use URI;
7
8 #=======================CONFIGURATION BEGIN============================
9
10 #mysql default config
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
16
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
21
22 my $filetoparse="/var/log/squid/access.log";
23 #==========================================================
24
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 #Период, старше которого данные будут удаляться. Указывается в днях.
28
29 my $deleteperiod=120; #days
30 #==========================================================
31 #min bytes of traffic in one record to write into DB. By default - all data stored.
32
33 my $minbytestoparse=-1; #bytes, default -1
34
35 #=======================CONFIGURATION END==============================
36
37 $count=0;
38 $lastdate=0;
39 $sqltext="";
40 $sql_getlastdate="";
41
42 $sqlbatch=100;
43 $overlap=300;
44
45 #datetime when parse started
46 print $now=localtime;
47 $startnow=time;
48
49 $dbh = DBI->connect("DBI:mysql:$db:$host:$port",$user,$pass);
50
51 $sth = $dbh->prepare("SELECT ifnull(unix_timestamp(max(access_date))-$overlap,0) from access_log");
52 $sth->execute;
53 @row=$sth->fetchrow_array;
54 $lastdate=$row[0];
55
56 if ($deleteperiod) {
57   $sth = $dbh->prepare("delete from access_log where access_date <  date_sub(now(), interval $deleteperiod day)");
58   $sth->execute;
59 }
60
61 #open log file for reading
62 open(IN, "<$filetoparse"); 
63
64 $countlines=0; 
65 $countadded=0;
66
67 print "\n";
68
69 $sqlspool = 0;
70 $sqltext = "";
71
72 #loop for get strings from file one by one.
73 while (my $line=<IN>) {
74
75     if(time > $seconds+1) {
76         $seconds=time;
77         $insertspeed=$countinsert;
78     }
79     $countinsert++;
80
81     @item = split " ", $line; 
82
83     $bytes = $item[4];
84    $time  = $item[0];
85     if (($bytes>0) && ($time>$lastdate)) {
86         $ms = $item[1];
87         $client_ip = $item[2];
88         $http_code = $item[3];
89         $method = $item[5];
90         $url = $item[6];
91         @matches = ($url=~ /(?:^[a-z][a-z0-9+\-.]*:\/\/)?(?:[a-z0-9\-._~%!$&'()*+,;=]+(?::.*)?@)?([a-z0-9\-._~%]+|\[[a-z0-9\-._~%!$&'()*+,;=:]+\])/i);
92         $host=$matches[0];
93         $user = $item[7];
94         $mime = $item[9];
95         if ($sqltext) {
96           $sqltext=$sqltext.",($time,$ms,'$client_ip','$http_code',$bytes,'$method','$host','$user','$mime')";
97         } else {
98           $sqltext="($time,$ms,'$client_ip','$http_code',$bytes,'$method','$host','$user','$mime')";
99         }
100         $sqlspool++;
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);
104           $sth->execute;
105           $sqlspool = 0;
106           $sqltext = "";
107         }
108         print "Completed: ".$countlines." ".$insertspeed." lines/sec\r";
109         $countlines++;
110     }
111     
112 }
113
114 if ($sqltext) {
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);
117   $sth->execute;
118 }
119
120 print "\n";
121 close(IN);
122
123 $sth = $dbh->prepare("CALL Process_Traffic();");
124 $sth->execute;
125
126 $rc = $dbh->disconnect;