root/alternc-stats/branches/INIT/bin/stat_consop.php

Revision 1340, 4.6 kB (checked in by anonymous, 4 years ago)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/usr/bin/php4 -q
2 <?php
3
4 // Ce script lit un fichier de log apache et insère les statistiques de visite par jour et par domaine / sous-domaine dans la table stat_http.
5 // Voici sa configuration :
6
7 // Combien de lignes de logs traite-t-on par bloc (10000 conseillé, plus possible (à tester), moins vivement déconseillé)
8 $each=1000000;
9 // Emplacement du fichier apache (s'il se termine par .gz, gzopen et gzgets seront utilisés)
10 $apache="/var/log/apache/access.log.312.gz";
11
12
13
14 // Ne pas toucher ci-dessous.
15
16 @set_time_limit(0);
17
18 include("/var/alternc/bureau/class/config_nochk.php");
19
20 // Libère le bureau !
21 alternc_shutdown();
22
23 if (substr($apache,-3)==".gz") {
24   $open=gzopen;
25   $gets=gzgets;
26   $close=gzclose;
27 } else {
28   $open=fopen;
29   $gets=fgets;
30   $close=fclose;
31 }
32
33 // On ouvre le log apache de la veille :
34 $f=$open($apache,"rb");
35
36 $i=0; $l=0;
37
38 $domstat=array(); // On stocke sous forme de clé "dom/day" => "hit or bandwith"
39 $domuid=array();  // Cache des uids associés aux domaines.
40
41 // Exemple de ligne apache :
42 // crawl18.dir.com - login [14/Jun/2004:06:38:47 +0200] "GET /modules/newbb?days=100 HTTP/1.0" 200 20156 "-" "Pompos/1.3 http://dir.com/pompos.html" 2 esperance-jeunes.org
43
44 $months=array("Jan"=>"01","Feb"=>"02","Mar"=>"03","Apr"=>"04","May"=>"05","Jun"=>"06","Jul"=>"07","Aug"=>"08","Sep"=>"09","Oct"=>"10","Nov"=>"11","Dec"=>"12");
45
46 while ($s=$gets($f,2048)) {
47   $s=trim($s);
48   echo "ligne : $s\n"; flush();
49   $s=str_replace('\\"','',$s);
50   if (preg_match('/^[^ ]* [^ ]* [^ ]* \\[([0-9]*)\\/([a-zA-Z]*)\\/([0-9]*):[0-9]*:[0-9]*:[0-9]* [^ ]* "[^"]*" ([0-9-]*) ([0-9-]*) "[^"]*" "[^"]*" [0-9]* ([^ ]*)$/',$s,$mat)) {
51     // ok, 1: jour  2: mois (english)  3: année  4: http result (200/404 ...) 5: taille  6: domaine
52     // Ligne ok.
53     //    echo "Ligne ok : ";
54     //    for($j=1;$j<count($mat);$j++) echo "$j:".$mat[$j]." ";
55     // On calcule le jour : (= yearmonthday)
56     $day=$mat[3].$months[$mat[2]].$mat[1];
57     if ($mat[4]==200) {      //  HTTP/1.0 OK !
58       $domstat[$mat[6]."/".$day]["hit"]++;
59       $domstat[$mat[6]."/".$day]["size"]+=intval($mat[5]);
60     } else {   // HTTP/1.0 404, 304, 503 ... !
61       $domstat[$mat[6]."/".$day]["adminhit"]++;     
62     }
63   } else {
64     //    echo "Ligne non reconnue : $s";
65   }
66   if ($i/1000==intval($i/1000)) { echo "."; flush(); }
67   $i++; $l++;
68   if ($i==$each) {
69     echo " $l lines read, Inserting ... ";
70     // on insère le tableau dans mysql.
71     $update=0; $insert=0;
72     foreach($domstat as $key => $val) {
73       preg_match("/^([^\\/]*)\\/(.*)$/",$key,$mat);
74       $mat[1]=strtolower($mat[1]);
75       // on cherche l'uid de ce domaine
76       if (!$domuid[$mat[1]]) {
77     list($domuid[$mat[1]])=@mysql_fetch_array(mysql_query("SELECT compte FROM sub_domaines WHERE (domaine='".$mat[1]."' AND sub='') OR (concat(sub,'.',domaine)='".$mat[1]."');"));
78       }
79       // a-t-on déjà ce domaine ce jour ?
80       list($ct)=@mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM stat_http WHERE day='".$mat[2]."' AND domain='".$mat[1]."';"));
81       if ($ct) {
82     $sql="UPDATE stat_http SET hit='".$val["hit"]."', size='".$val["size"]."', adminhit='".$val["adminhit"]."' WHERE domain='".$mat[1]."' AND day='".$mat[2]."';";
83     $update++;
84       } else {
85     $sql="INSERT INTO stat_http SET uid='".$domuid[$mat[1]]."',day='".$mat[2]."',domain='".$mat[1]."', hit='".$val["hit"]."', size='".$val["size"]."', adminhit='".$val["adminhit"]."';";
86     $insert++;
87       }
88       //      mysql_query($sql);
89     }
90     echo "$update updates and $insert inserts (uidcache size = ".count($domuid).") \n";
91
92     $i=0;
93   }
94 }
95
96  echo " $l lines read, Inserting ... ";
97  // on insère le tableau dans mysql.
98  $update=0; $insert=0;
99  foreach($domstat as $key => $val) {
100    preg_match("/^([^\\/]*)\\/(.*)$/",$key,$mat);
101    $mat[1]=strtolower($mat[1]);
102    // on cherche l'uid de ce domaine
103    if (!$domuid[$mat[1]]) {
104      list($domuid[$mat[1]])=@mysql_fetch_array(mysql_query("SELECT compte FROM sub_domaines WHERE (domaine='".$mat[1]."' AND sub='') OR (concat(sub,'.',domaine)='".$mat[1]."');"));
105    }
106    // a-t-on déjà ce domaine ce jour ?
107    list($ct)=@mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM stat_http WHERE day='".$mat[2]."' AND domain='".$mat[1]."';"));
108    if ($ct) {
109      $sql="UPDATE stat_http SET hit='".$val["hit"]."', size='".$val["size"]."', adminhit='".$val["adminhit"]."' WHERE domain='".$mat[1]."' AND day='".$mat[2]."';";
110      $update++;
111    } else {
112      $sql="INSERT INTO stat_http SET uid='".$domuid[$mat[1]]."',day='".$mat[2]."',domain='".$mat[1]."', hit='".$val["hit"]."', size='".$val["size"]."', adminhit='".$val["adminhit"]."';";
113      $insert++;
114    }
115    mysql_query($sql);
116  }
117  echo "$update updates and $insert inserts (uidcache size = ".count($domuid).") \n";
118  
119
120  $close($f);
121
122
123 ?>
Note: See TracBrowser for help on using the browser.