root/alternc-stats/trunk/bin/alternc_stats_mail.php

Revision 2063, 3.4 kB (checked in by anarcat, 8 months ago)

remove unnecessary trailing ?>

  • 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/php -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 mail log (s'il se termine par .gz, gzopen et gzgets seront utilisés)
10 $log="/var/log/syslog";
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($log,-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($log,"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 POP/IMAP :
42 // Nov  1 07:11:33 brassens courierpop3login: LOGOUT, user=bbayard_bayard-distribution.com, ip=[::ffff:83.192.40.215], top=0, retr=0
43 // Nov  1 07:31:42 brassens imaplogin: LOGOUT, user=oberursel_beatitudes.org, ip=[::ffff:217.24.84.7], headers=1661, body=0
44
45 $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");
46
47 while ($s=$gets($f,2048)) {
48   $s=trim($s);
49   echo "ligne : $s\n"; flush();
50   $s=str_replace('\\"','',$s);
51   if (preg_match('/^([^\s]*)\s([0-9]*)\s[0-9:]*\s[a-zA-Z0-9]*\scourierpop3login: LOGOUT, user=([^,]*), ip=\[[^\]]*, top=([0-9]*), retr=([0-9]*)$/',$s,$mat)) {
52     // POP OK.
53
54     // ok, 1: jour  2: mois (english)  3: année  4: http result (200/404 ...) 5: taille  6: domaine
55     // Ligne ok.
56     //    echo "Ligne ok : ";
57     //    for($j=1;$j<count($mat);$j++) echo "$j:".$mat[$j]." ";
58     // On calcule le jour : (= yearmonthday)
59     $day=date("Y").$months[$mat[1]].$mat[2];
60       $domstat[$mat[6]."/".$day]["hit"]++;
61       $domstat[$mat[6]."/".$day]["size"]+=intval($mat[5]);
62   } elseif () {
63   } else {
64     // echo "Ligne non reconnue : $s\n";
65   }
66   if ($i/1000==intval($i/1000)) { echo "."; flush(); }
67   $i++;
68 }
69
70
71
72
73 // Change from here ;)
74
75
76  echo " $i lines read, Inserting ... ";
77  // on insère le tableau dans mysql.
78  $update=0; $insert=0;
79  foreach($domstat as $key => $val) {
80    preg_match("/^([^\\/]*)\\/(.*)$/",$key,$mat);
81    $mat[1]=strtolower($mat[1]);
82    // on cherche l'uid de ce domaine
83    if (!$domuid[$mat[1]]) {
84      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]."');"));
85    }
86    // a-t-on déjà ce domaine ce jour ?
87    list($ct)=@mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM stat_http WHERE day='".$mat[2]."' AND domain='".$mat[1]."';"));
88    if ($ct) {
89      $sql="UPDATE stat_http SET hit='".$val["hit"]."', size='".$val["size"]."', adminhit='".$val["adminhit"]."' WHERE domain='".$mat[1]."' AND day='".$mat[2]."';";
90      $update++;
91    } else {
92      $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"]."';";
93      $insert++;
94    }
95    mysql_query($sql);
96  }
97  echo "$update updates and $insert inserts (uidcache size = ".count($domuid).") \n";
98  
99
100  $close($f);
101
Note: See TracBrowser for help on using the browser.