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