commit | author | age
|
313e33
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
Copyright (c) 2007, Till Brehm, projektfarm Gmbh |
|
5 |
All rights reserved. |
|
6 |
|
|
7 |
Redistribution and use in source and binary forms, with or without modification, |
|
8 |
are permitted provided that the following conditions are met: |
|
9 |
|
|
10 |
* Redistributions of source code must retain the above copyright notice, |
|
11 |
this list of conditions and the following disclaimer. |
|
12 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
13 |
this list of conditions and the following disclaimer in the documentation |
|
14 |
and/or other materials provided with the distribution. |
|
15 |
* Neither the name of ISPConfig nor the names of its contributors |
|
16 |
may be used to endorse or promote products derived from this software without |
|
17 |
specific prior written permission. |
|
18 |
|
|
19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
20 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
22 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
23 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
24 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
26 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
*/ |
|
30 |
|
|
31 |
require("lib/config.inc.php"); |
|
32 |
require("lib/app.inc.php"); |
|
33 |
|
|
34 |
set_time_limit(0); |
|
35 |
|
|
36 |
// make sure server_id is always an int |
|
37 |
$conf["server_id"] = intval($conf["server_id"]); |
|
38 |
|
|
39 |
|
|
40 |
// Load required base-classes |
|
41 |
$app->uses('ini_parser,file,services'); |
|
42 |
|
|
43 |
|
|
44 |
####################################################################################################### |
|
45 |
// store the mailbox statistics in the database |
|
46 |
####################################################################################################### |
|
47 |
|
|
48 |
$sql = "SELECT mailuser_id,maildir FROM mail_user WHERE server_id = ".$conf["server_id"]; |
|
49 |
$records = $app->db->queryAllRecords($sql); |
|
50 |
foreach($records as $rec) { |
9855c7
|
51 |
if(@is_file($rec["maildir"].'/ispconfig_mailsize')) { |
313e33
|
52 |
|
T |
53 |
// rename file |
9855c7
|
54 |
rename($rec["maildir"].'/ispconfig_mailsize',$rec["maildir"].'/ispconfig_mailsize_save'); |
313e33
|
55 |
|
T |
56 |
// Read the file |
9855c7
|
57 |
$lines = file($rec["maildir"].'/ispconfig_mailsize_save'); |
313e33
|
58 |
$mail_traffic = 0; |
T |
59 |
foreach($lines as $line) { |
|
60 |
$mail_traffic += intval($line); |
|
61 |
} |
|
62 |
unset($lines); |
|
63 |
|
|
64 |
// Delete backup file |
9855c7
|
65 |
if(@is_file($rec["maildir"].'/ispconfig_mailsize_save')) unlink($rec["maildir"].'/ispconfig_mailsize_save'); |
313e33
|
66 |
|
T |
67 |
// Save the traffic stats in the sql database |
|
68 |
$tstamp = date("Y-m"); |
|
69 |
|
|
70 |
$sql = "SELECT * FROM mail_traffic WHERE month = '$tstamp' AND mailuser_id = ".$rec["mailuser_id"]; |
8c4aa3
|
71 |
$tr = $app->dbmaster->queryOneRecord($sql); |
313e33
|
72 |
|
T |
73 |
$mail_traffic += $tr["traffic"]; |
|
74 |
if($tr["traffic_id"] > 0) { |
|
75 |
$sql = "UPDATE mail_traffic SET traffic = $mail_traffic WHERE traffic_id = ".$tr["traffic_id"]; |
|
76 |
} else { |
|
77 |
$sql = "INSERT INTO mail_traffic (month,mailuser_id,traffic) VALUES ('$tstamp',".$rec["mailuser_id"].",$mail_traffic)"; |
|
78 |
} |
8c4aa3
|
79 |
$app->dbmaster->query($sql); |
313e33
|
80 |
echo $sql; |
T |
81 |
|
|
82 |
} |
|
83 |
|
|
84 |
} |
|
85 |
|
|
86 |
####################################################################################################### |
|
87 |
// Create webalizer statistics |
|
88 |
####################################################################################################### |
|
89 |
|
8ba08e
|
90 |
function setConfigVar( $filename, $varName, $varValue ) { |
T |
91 |
if($lines = @file($filename)) { |
|
92 |
$out = ''; |
|
93 |
$found = 0; |
|
94 |
foreach($lines as $line) { |
|
95 |
list($key, $value) = split("[\t= ]+", $line, 2); |
|
96 |
if($key == $varName) { |
|
97 |
$out .= $varName." ".$varValue."\n"; |
|
98 |
$found = 1; |
|
99 |
} else { |
|
100 |
$out .= $line; |
|
101 |
} |
|
102 |
} |
|
103 |
if($found == 0) { |
|
104 |
//* add \n if the last line does not end with \n or \r |
|
105 |
if(substr($out,-1) != "\n" && substr($out,-1) != "\r") $out .= "\n"; |
|
106 |
//* add the new line at the end of the file |
|
107 |
if($append == 1) $out .= $varName." ".$varValue."\n"; |
|
108 |
} |
|
109 |
|
|
110 |
file_put_contents($filename,$out); |
|
111 |
} |
|
112 |
} |
|
113 |
|
313e33
|
114 |
|
T |
115 |
$sql = "SELECT domain_id, domain, document_root FROM web_domain WHERE server_id = ".$conf["server_id"]; |
|
116 |
$records = $app->db->queryAllRecords($sql); |
8ba08e
|
117 |
|
313e33
|
118 |
foreach($records as $rec) { |
641cb3
|
119 |
$yesterday = date("Ymd",time() - 86400); |
313e33
|
120 |
$logfile = escapeshellcmd($rec["document_root"].'/log/'.$yesterday.'-access.log'); |
69944a
|
121 |
if(!@is_file($logfile)) { |
T |
122 |
$logfile = escapeshellcmd($rec["document_root"].'/log/'.$yesterday.'-access.log.gz'); |
8ba08e
|
123 |
if(!@is_file($logfile)) { |
T |
124 |
continue; |
313e33
|
125 |
} |
8ba08e
|
126 |
} |
T |
127 |
|
|
128 |
$domain = escapeshellcmd($rec["domain"]); |
|
129 |
$statsdir = escapeshellcmd($rec["document_root"].'/web/stats'); |
|
130 |
$webalizer = '/usr/bin/webalizer'; |
|
131 |
$webalizer_conf_main = '/etc/webalizer/webalizer.conf'; |
|
132 |
$webalizer_conf = escapeshellcmd($rec["document_root"].'/log/webalizer.conf'); |
|
133 |
|
|
134 |
if(!@is_file($webalizer_conf)) { |
|
135 |
exec("cp $webalizer_conf_main $webalizer_conf"); |
156953
|
136 |
} |
8ba08e
|
137 |
|
156953
|
138 |
if(@is_file($webalizer_conf)) { |
8ba08e
|
139 |
setConfigVar($webalizer_conf, 'Incremental', 'yes'); |
156953
|
140 |
setConfigVar($webalizer_conf, 'IncrementalName', $statsdir.'/webalizer.current'); |
T |
141 |
setConfigVar($webalizer_conf, 'HistoryName', $statsdir.'/webalizer.hist'); |
8ba08e
|
142 |
} |
T |
143 |
|
|
144 |
if(!@is_dir($statsdir)) mkdir($statsdir); |
69944a
|
145 |
exec("$webalizer -c $webalizer_conf -n $domain -s $domain -r $domain -q -T -p -o $statsdir $logfile"); |
313e33
|
146 |
} |
T |
147 |
|
|
148 |
####################################################################################################### |
|
149 |
// Manage and compress web logfiles |
|
150 |
####################################################################################################### |
|
151 |
|
|
152 |
$sql = "SELECT domain_id, domain, document_root FROM web_domain WHERE server_id = ".$conf["server_id"]; |
|
153 |
$records = $app->db->queryAllRecords($sql); |
|
154 |
foreach($records as $rec) { |
641cb3
|
155 |
$yesterday = date("Ymd",time() - 86400); |
313e33
|
156 |
$logfile = escapeshellcmd($rec["document_root"].'/log/'.$yesterday.'-access.log'); |
T |
157 |
if(@is_file($logfile)) { |
|
158 |
// Compress yesterdays logfile |
|
159 |
exec("gzip -c $logfile > $logfile.gz"); |
b28db4
|
160 |
unlink($logfile); |
313e33
|
161 |
} |
T |
162 |
|
|
163 |
// delete logfiles after 30 days |
641cb3
|
164 |
$month_ago = date("Ymd",time() - 86400 * 30); |
b28db4
|
165 |
$logfile = escapeshellcmd($rec["document_root"].'/log/'.$month_ago.'-access.log.gz'); |
313e33
|
166 |
if(@is_file($logfile)) { |
T |
167 |
unlink($logfile); |
|
168 |
} |
|
169 |
} |
|
170 |
|
d7f14d
|
171 |
####################################################################################################### |
e265dc
|
172 |
// Cleanup logs in master database (only the "master-server") |
d7f14d
|
173 |
####################################################################################################### |
313e33
|
174 |
|
e265dc
|
175 |
if ($app->dbmaster == $app->db) { |
V |
176 |
/** 7 days */ |
|
177 |
$tstamp = time() - (60*60*24*7); |
313e33
|
178 |
|
e265dc
|
179 |
/* |
V |
180 |
* Keep 7 days in sys_log |
|
181 |
* (we can delete the old items, because if they are OK, they don't interrest anymore |
|
182 |
* if they are NOT ok, the server will try to process them in 1 minute and so the |
|
183 |
* error appears again after 1 minute. So it is no problem to delete the old one! |
|
184 |
*/ |
|
185 |
$sql = "DELETE FROM sys_log WHERE tstamp < $tstamp AND server_id != 0"; |
|
186 |
$app->dbmaster->query($sql); |
|
187 |
|
|
188 |
/* |
|
189 |
* The sys_datalog is more difficult. |
|
190 |
* 1) We have to keet ALL entries with |
|
191 |
* server_id=0, because they depend on ALL servers (even if they are not |
|
192 |
* actually in the system (and will be insered in 3 days or so). |
|
193 |
* 2) We have to keey ALL entries which are not actually precessed by the |
|
194 |
* server never mind how old they are! |
|
195 |
*/ |
|
196 |
|
|
197 |
/* First we need all servers and the last sys_datalog-id they processed */ |
|
198 |
$sql = "SELECT server_id, updated FROM server ORDER BY server_id"; |
|
199 |
$records = $app->dbmaster->queryAllRecords($sql); |
|
200 |
|
|
201 |
/* Then delete server by server */ |
|
202 |
foreach($records as $server) { |
|
203 |
$sql = "DELETE FROM sys_datalog WHERE tstamp < " . $tstamp . |
|
204 |
" AND server_id != 0 " . // to be more secure! |
|
205 |
" AND server_id = " . intval($server['server_id']) . |
|
206 |
" AND datalog_id < " . intval($server['updated']); |
|
207 |
// echo $sql . "\n"; |
|
208 |
$app->dbmaster->query($sql); |
|
209 |
} |
|
210 |
} |
313e33
|
211 |
|
T |
212 |
die("finished.\n"); |
|
213 |
?> |