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

Revision 1340, 4.5 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=10000;
9 // Emplacement du fichier apache (s'il se termine par .gz, gzopen et gzgets seront utilisés)
10 $apache="/var/log/apache/access.log.1";
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   if (ereg('^[^ ]* [^ ]* [^ ]* \\[([0-9]*)/([a-zA-Z]*)/([0-9]*):[0-9]*:[0-9]*:[0-9]* [^ ]* "[^"]*" ([0-9-]*) ([0-9-]*) "[^"]*" "[^"]*" [0-9]* ([^ ]*)$',$s,$mat)) {
49     // ok, 1: jour  2: mois (english)  3: année  4: http result (200/404 ...) 5: taille  6: domaine
50     // Ligne ok.
51     //    echo "Ligne ok : ";
52     //    for($j=1;$j<count($mat);$j++) echo "$j:".$mat[$j]." ";
53     // On calcule le jour : (= yearmonthday)
54     $day=$mat[3].$months[$mat[2]].$mat[1];
55     if ($mat[4]==200) {      //  HTTP/1.0 OK !
56       $domstat[$mat[6]."/".$day]["hit"]++;
57       $domstat[$mat[6]."/".$day]["size"]+=intval($mat[5]);
58     } else {   // HTTP/1.0 404, 304, 503 ... !
59       $domstat[$mat[6]."/".$day]["adminhit"]++;     
60     }
61   } else {
62     echo "Ligne non reconnue : $s";
63   }
64   if ($i/100==intval($i/100)) { echo "."; flush(); }
65   $i++; $l++;
66   if ($i==$each) {
67     echo " $l lines read, Inserting ... ";
68     // on insère le tableau dans mysql.
69     $update=0; $insert=0;
70     foreach($domstat as $key => $val) {
71       ereg("^([^/]*)/(.*)$",$key,$mat);
72       $mat[1]=strtolower($mat[1]);
73       // on cherche l'uid de ce domaine
74       if (!$domuid[$mat[1]]) {
75     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]."');"));
76       }
77       // a-t-on déjà ce domaine ce jour ?
78       list($ct)=@mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM stat_http WHERE day='".$mat[2]."' AND domain='".$mat[1]."';"));
79       if ($ct) {
80     $sql="UPDATE stat_http SET hit='".$val["hit"]."', size='".$val["size"]."', adminhit='".$val["adminhit"]."' WHERE domain='".$mat[1]."' AND day='".$mat[2]."';";
81     $update++;
82       } else {
83     $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"]."';";
84     $insert++;
85       }
86       mysql_query($sql);
87     }
88     echo "$update updates and $insert inserts (uidcache size = ".count($domuid).") \n";
89
90     $i=0;
91   }
92 }
93
94  echo " $l lines read, Inserting ... ";
95  // on insère le tableau dans mysql.
96  $update=0; $insert=0;
97  foreach($domstat as $key => $val) {
98    ereg("^([^/]*)/(.*)$",$key,$mat);
99    $mat[1]=strtolower($mat[1]);
100    // on cherche l'uid de ce domaine
101    if (!$domuid[$mat[1]]) {
102      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]."');"));
103    }
104    // a-t-on déjà ce domaine ce jour ?
105    list($ct)=@mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM stat_http WHERE day='".$mat[2]."' AND domain='".$mat[1]."';"));
106    if ($ct) {
107      $sql="UPDATE stat_http SET hit='".$val["hit"]."', size='".$val["size"]."', adminhit='".$val["adminhit"]."' WHERE domain='".$mat[1]."' AND day='".$mat[2]."';";
108      $update++;
109    } else {
110      $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"]."';";
111      $insert++;
112    }
113    mysql_query($sql);
114  }
115  echo "$update updates and $insert inserts (uidcache size = ".count($domuid).") \n";
116  
117
118  $close($f);
119
120
121 ?>
Note: See TracBrowser for help on using the browser.