root/alternc/tags/0.9.6/src/rawstat.daily

Revision 659, 6.3 kB (checked in by anarcat, 3 years ago)

[project @ alternc: changeset 2005-05-11 15:39:34 by arnaud-lb]
Ne parse les logs qu'une fois N'ouvre/ferme les fichiers de logs
qu'une fois Plus de grep Créer les fichiers avec le bon umask et fait
chown/chgrp dessus

Original author: arnaud-lb
Date: 2005-05-11 15:39:34

Line 
1 #!/usr/bin/php4 -q
2 <?php
3 /*
4  $Id: rawstat.daily,v 1.4 2005/05/11 15:39:34 arnaud-lb Exp $
5  ----------------------------------------------------------------------
6  AlternC - Web Hosting System
7  Copyright (C) 2002 by the AlternC Development Team.
8  http://alternc.org/
9  ----------------------------------------------------------------------
10  Based on:
11  Valentin Lacambre's web hosting softwares: http://altern.org/
12  ----------------------------------------------------------------------
13  LICENSE
14
15  This program is free software; you can redistribute it and/or
16  modify it under the terms of the GNU General Public License (GPL)
17  as published by the Free Software Foundation; either version 2
18  of the License, or (at your option) any later version.
19
20  This program is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  GNU General Public License for more details.
24
25  To read the license please visit http://www.gnu.org/copyleft/gpl.html
26  ----------------------------------------------------------------------
27  Original Author of file: Arnaud lb
28  Purpose of file: Provides raw statistics to members
29  ----------------------------------------------------------------------
30 */
31
32 require("/var/alternc/bureau/class/local.php");
33 $FILES_OWNER = 33;
34 $APACHE_LOG = '/var/log/apache/access.log.1';
35 umask(0177);
36 $hosts = array();
37 $nolog_hosts = array();
38 $LOGS_SUFIX='.log';//For testing
39
40 if (!mysql_connect($L_MYSQL_HOST,$L_MYSQL_LOGIN,$L_MYSQL_PWD)) {
41     echo "Cannot connect to Mysql !\n";
42     return 1;
43 }
44
45 if (!mysql_select_db($L_MYSQL_DATABASE)) {
46     echo "Cannot connect to Mysql database $L_MYSQL_DATABASE !\n";
47     return 1;
48 }
49
50 $query = "SELECT mid,folder,hostname FROM stats2 ORDER BY CHAR_LENGTH(hostname) ASC";
51 $result = mysql_query($query);
52 if (mysql_errno()) {
53     echo 'MySQL error: '.mysql_error()."\n";
54 }
55
56 //Fetch the list of raw stats, and caching some data
57 while ($row = mysql_fetch_assoc($result)) {
58     $hosts[$row['hostname']] = array(
59         'mid'=>$row['mid'],
60         'log_files'=>array(
61             $row['folder'].'/'.$row['hostname'].$LOGS_SUFIX =>''
62         )
63     );
64     
65     //A log line with host.test.com should go to the test.com's log file too, if the member want logs for host.test.com and test.com.
66     $parent_host = $row['hostname'];
67     while ($pos = strpos($parent_host, '.')) {
68         $parent_host = substr($parent_host, $pos+1);
69         if (!isset($hosts[$parent_host])) {
70             continue;
71         }
72         //link the parent-hostnames's log-files in this host
73         $parent_log_files = array_keys($hosts[$parent_host]['log_files']);
74         foreach($parent_log_files as $parent_log_file) {
75             $hosts[$row['hostname']]['log_files'][$parent_log_file] = &$hosts[$parent_host]['log_files'][$parent_log_file];
76         }
77
78         break;
79     }
80 }
81
82 //Open apache log file
83 if (!$apache_log_file = fopen($APACHE_LOG, 'r')) {
84     return 1;
85 }
86
87 //Parsing log file
88 while ($line = fgets($apache_log_file)) {
89
90     //Get the hostname in this log line
91     //assume that hostname is at end of line and separated with a space
92     $host = substr($line, strrpos($line, ' ')+1, -1);
93
94     if (is_null($hosts[$host])) {
95         unset($hosts[$host]);
96         $nolog_hosts[$host]='';
97         continue;
98     }
99
100     if (isset($nolog_hosts[$host])) {
101         continue;
102     }
103
104     //If hostname is not listed in hostnames to log, link it to a listed parent if exists.
105     //Processed only one time by not listed hostname
106     if (!isset($hosts[$host])) {
107         $parent_host = $host;
108         while ($pos = strpos($parent_host, '.')) {
109             $parent_host = substr($parent_host, $pos+1);
110             if (isset($nolog_hosts[$parent_host])) {
111                 $pos = false;
112                 break;
113             }
114             if (is_null($hosts[$parent_host])) {
115                 unset($hosts[$parent_host]);
116                 $nolog_hosts[$parent_host]='';
117                 $pos = false;
118                 break;
119             }
120             if (isset($hosts[$parent_host])) {
121                 //link this host to the parent hostname
122                 $hosts[$host] = &$hosts[$parent_host];
123                 break;
124             }
125         }
126         if ($pos === false) {
127             //We will not have to search again for this host
128             $nolog_hosts[$host]='';
129             continue;
130         }
131     }
132
133     $log_files = array_keys($hosts[$host]['log_files']);
134     if (count($log_files) < 1) {
135         $hosts[$host] = null;
136         unset($hosts[$host]);
137         $nolog_hosts[$host]='';
138         continue;
139     }
140
141     foreach($log_files as $log_file) {
142
143         if (is_null($hosts[$host]['log_files'][$log_file])) {
144             // has been set to null throught a reference for a future unset
145             unset($hosts[$host]['log_files'][$log_file]);
146             continue;
147         }
148
149         //file isn't opened yet
150         if (!is_resource($hosts[$host]['log_files'][$log_file])) {
151     
152             //Log file doesn't exists, we create an empty one with good owner and perms
153             if (!file_exists($log_file)) {
154                 if (!touch($log_file)) {
155                     $hosts[$host]['log_files'][$log_file] = null;
156                     unset($hosts[$host]['log_files'][$log_file]);
157                     continue;
158                 }
159                 if (!chgrp($log_file, (int)$hosts[$host]['mid'])) {
160                     unlink($log_file);
161                     $hosts[$host]['log_files'][$log_file] = null;
162                     unset($hosts[$host]['log_files'][$log_file]);
163                     continue;
164                 }
165                 if (!chown($log_file, (int)$FILES_OWNER)) {
166                     unlink($log_file);
167                     $hosts[$host]['log_files'][$log_file] = null;
168                     unset($hosts[$host]['log_files'][$log_file]);
169                     continue;
170                 }
171             }
172     
173             //Open the log file
174             if (!$hosts[$host]['log_files'][$log_file] = fopen($log_file, 'a')) {
175                 $hosts[$host]['log_files'][$log_file] = null;
176                 unset($hosts[$host]['log_files'][$log_file]);
177                 continue;
178             }
179         }
180
181         //Write the log line
182         if (!fwrite($hosts[$host]['log_files'][$log_file], $line)) {
183             fclose($hosts[$host]['log_files'][$log_file]);
184             $hosts[$host]['log_files'][$log_file] = null;
185             unset($hosts[$host]['log_files'][$log_file]);
186         }
187     }
188 }
189
190 //Close all opened files
191 $hostnames = array_keys($hosts);
192 while ($host = array_pop($hostnames)) {
193     if (is_null($hosts[$hostname]['log_files'])) {
194         unset($hosts[$hostname]['log_files'][$log_file]);
195         continue;
196     }
197     $log_files = array_keys($hosts[$hostname]['log_files']);
198     while ($log_file = array_pop($log_files)) {
199         if (is_null($hosts[$hostname]['log_files'][$log_file])) {
200             unset($hosts[$hostname]['log_files'][$log_file]);
201             continue;
202         }
203         if (is_resource($hosts[$hostname]['log_files'][$log_file])) {
204             fclose($hosts[$hostname]['log_files'][$log_file]);
205             $hosts[$hostname]['log_files'][$log_file] = null;
206             unset($hosts[$hostname]['log_files'][$log_file]);
207         }
208     }
209     $hosts[$hostname] = null;
210     unset($hosts[$hostname]);
211 }
212
213 fclose($apache_log_file);
214
215 return 0;
216 ?>
217
Note: See TracBrowser for help on using the browser.