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

Revision 1340, 4.8 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 @set_time_limit(0);
4
5 include("/var/alternc/bureau/class/config_nochk.php");
6
7 // Libère le bureau !
8 alternc_shutdown();
9
10 // 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.
11 // Voici sa configuration :
12
13 // Combien de lignes de logs traite-t-on par bloc (10000 conseillé, plus possible (à tester), moins vivement déconseillé)
14 $each=1000000;
15 // Emplacement du fichier apache (s'il se termine par .gz, gzopen et gzgets seront utilisés)
16
17 for ($k=3;$k>=2;$k--) {
18
19 $apache="/var/log/apache/access.log.$k.gz";
20
21
22  echo "################################################\n";
23  echo date("d/m/Y H:i:s")." : Traitement de $apache \n";
24  echo "################################################\n";
25
26
27 if (substr($apache,-3)==".gz") {
28   $open=gzopen;
29   $gets=gzgets;
30   $close=gzclose;
31 } else {
32   $open=fopen;
33   $gets=fgets;
34   $close=fclose;
35 }
36
37 // On ouvre le log apache de la veille :
38 $f=$open($apache,"rb");
39
40 $i=0; $l=0;
41
42 $domstat=array(); // On stocke sous forme de clé "dom/day" => "hit or bandwith"
43 $domuid=array();  // Cache des uids associés aux domaines.
44
45 // Exemple de ligne apache :
46 // 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
47
48 $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");
49
50 while ($s=$gets($f,2048)) {
51   $s=trim($s);
52   if (preg_match('/^[^ ]* [^ ]* [^ ]* \\[([0-9]*)\\/([a-zA-Z]*)\\/([0-9]*):[0-9]*:[0-9]*:[0-9]* [^ ]* "[^"]*" ([0-9-]*) ([0-9-]*) "[^"]*" "[^"]*" [0-9]* ([^ ]*)$/',$s,$mat)) {
53     // ok, 1: jour  2: mois (english)  3: année  4: http result (200/404 ...) 5: taille  6: domaine
54     // Ligne ok.
55     //    echo "Ligne ok : ";
56     //    for($j=1;$j<count($mat);$j++) echo "$j:".$mat[$j]." ";
57     // On calcule le jour : (= yearmonthday)
58     $day=$mat[3].$months[$mat[2]].$mat[1];
59     if ($mat[4]==200) {      //  HTTP/1.0 OK !
60       $domstat[$mat[6]."/".$day]["hit"]++;
61       $domstat[$mat[6]."/".$day]["size"]+=intval($mat[5]);
62     } else {   // HTTP/1.0 404, 304, 503 ... !
63       $domstat[$mat[6]."/".$day]["adminhit"]++;     
64     }
65   } else {
66     //    echo "Ligne non reconnue : $s";
67   }
68   if ($i/1000==intval($i/1000)) { echo "."; flush(); }
69   $i++; $l++;
70   if ($i==$each) {
71     echo " $l lines read, Inserting ... ";
72     // on insère le tableau dans mysql.
73     $update=0; $insert=0;
74     foreach($domstat as $key => $val) {
75       preg_match("/^([^\\/]*)\\/(.*)$/",$key,$mat);
76       $mat[1]=strtolower($mat[1]);
77       // on cherche l'uid de ce domaine
78       if (!$domuid[$mat[1]]) {
79     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]."');"));
80       }
81       // a-t-on déjà ce domaine ce jour ?
82       list($ct)=@mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM stat_http WHERE day='".$mat[2]."' AND domain='".$mat[1]."';"));
83       if ($ct) {
84     $sql="UPDATE stat_http SET hit='".$val["hit"]."', size='".$val["size"]."', adminhit='".$val["adminhit"]."' WHERE domain='".$mat[1]."' AND day='".$mat[2]."';";
85     $update++;
86       } else {
87     $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"]."';";
88     $insert++;
89       }
90       mysql_query($sql);
91     }
92     echo "$update updates and $insert inserts (uidcache size = ".count($domuid).") \n";
93
94     $i=0;
95   }
96 }
97
98  echo " $l lines read, Inserting ... ";
99  // on insère le tableau dans mysql.
100  $update=0; $insert=0;
101  foreach($domstat as $key => $val) {
102    preg_match("/^([^\\/]*)\\/(.*)$/",$key,$mat);
103    $mat[1]=strtolower($mat[1]);
104    // on cherche l'uid de ce domaine
105    if (!$domuid[$mat[1]]) {
106      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]."');"));
107    }
108    // a-t-on déjà ce domaine ce jour ?
109    list($ct)=@mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM stat_http WHERE day='".$mat[2]."' AND domain='".$mat[1]."';"));
110    if ($ct) {
111      $sql="UPDATE stat_http SET hit='".$val["hit"]."', size='".$val["size"]."', adminhit='".$val["adminhit"]."' WHERE domain='".$mat[1]."' AND day='".$mat[2]."';";
112      $update++;
113    } else {
114      $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"]."';";
115      $insert++;
116    }
117    mysql_query($sql);
118  }
119  echo "$update updates and $insert inserts (uidcache size = ".count($domuid).") \n";
120  
121
122  $close($f);
123
124
125
126  echo "\n$apache terminé...\n\n";
127  
128 }
129
130
131 ?>
132
Note: See TracBrowser for help on using the browser.