200-logfiles.inc.php 12.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

/*
Copyright (c) 2013, Marius Cramer, pixcept KG
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

class cronjob_logfiles extends cronjob {
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

	// job schedule
	protected $_schedule = '0 0 * * *';

	/* this function is optional if it contains no custom code */
	public function onPrepare() {
		global $app;

		parent::onPrepare();
	}

	/* this function is optional if it contains no custom code */
	public function onBeforeRun() {
		global $app;

		return parent::onBeforeRun();
	}

	public function onRunJob() {
		global $app, $conf;
Florian Schaal's avatar
Florian Schaal committed
52
		
53
54
55
56
		$app->uses('getconf');
		$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
		
		if($server_config['log_retention'] > 0) {
57
			$max_syslog = $app->functions->intval($server_config['log_retention']);
58
59
60
		} else {
			$max_syslog = 10;
		}
61
62
63
64
65
66
67
68
69
70
71

		//######################################################################################################
		// Make the web logfiles directories world readable to enable ftp access
		//######################################################################################################

		if(is_dir('/var/log/ispconfig/httpd')) exec('chmod +r /var/log/ispconfig/httpd/*');

		//######################################################################################################
		// Manage and compress web logfiles and create traffic statistics
		//######################################################################################################

72
		$sql = "SELECT domain_id, domain, type, document_root, web_folder, parent_domain_id, log_retention FROM web_domain WHERE (type = 'vhost' or type = 'vhostsubdomain' or type = 'vhostalias') AND server_id = ?";
73
		$records = $app->db->queryAllRecords($sql, $conf['server_id']);
74
75
76
77
78
79
		foreach($records as $rec) {

			//* create traffic statistics based on yesterdays access log file
			$yesterday = date('Ymd', time() - 86400);

			$log_folder = 'log';
Dominik's avatar
Dominik committed
80
			if($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') {
81
				$tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = ?', $rec['parent_domain_id']);
82
83
84
85
86
87
				$subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
				if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id'];
				$log_folder .= '/' . $subdomain_host;
				unset($tmp);
			}

Florian Schaal's avatar
Florian Schaal committed
88
			$log_retention = $rec['log_retention'];
89

90
91
92
93
94
95
96
97
98
99
100
101
102
			$logfile = $rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log';
			$total_bytes = 0;

			$handle = @fopen($logfile, "r");
			if ($handle) {
				while (($line = fgets($handle, 4096)) !== false) {
					if (preg_match('/^\S+ \S+ \S+ \[.*?\] "\S+.*?" \d+ (\d+) ".*?" ".*?"/', $line, $m)) {
						$total_bytes += intval($m[1]);
					}
				}

				//* Insert / update traffic in master database
				$traffic_date = date('Y-m-d', time() - 86400);
103
				$tmp = $app->dbmaster->queryOneRecord("select hostname from web_traffic where hostname=? and traffic_date=?", $rec['domain'], $traffic_date);
104
				if(is_array($tmp) && count($tmp) > 0) {
105
106
					$sql = "UPDATE web_traffic SET traffic_bytes=traffic_bytes + ? WHERE hostname = ? AND traffic_date = ?";
					$app->dbmaster->query($sql, $total_bytes, $rec['domain'], $traffic_date);
107
				} else {
108
109
					$sql = "INSERT INTO web_traffic (hostname, traffic_date, traffic_bytes) VALUES (?, ?, ?)";
					$app->dbmaster->query($sql, $rec['domain'], $traffic_date, $total_bytes);
110
111
112
113
114
115
				}

				fclose($handle);
			}

			$yesterday2 = date('Ymd', time() - 86400*2);
116
			$logfile = $rec['document_root'].'/' . $log_folder . '/'.$yesterday2.'-access.log';
117
118
119
120

			//* Compress logfile
			if(@is_file($logfile)) {
				// Compress yesterdays logfile
121
				$app->system->exec_safe("gzip -c ? > ?", $logfile, $logfile . '.gz');
122
123
				unlink($logfile);
			}
124
125
126
			
			$cron_logfiles = array('cron.log', 'cron_error.log', 'cron_wget.log');
			foreach($cron_logfiles as $cron_logfile) {
127
				$cron_logfile = $rec['document_root'].'/' . $log_folder . '/' . $cron_logfile;
128
129
				
				// rename older files (move up by one)
Florian Schaal's avatar
Florian Schaal committed
130
				$num = $log_retention;
Florian Schaal's avatar
Florian Schaal committed
131
132
				while($num >= 1) {
					if(is_file($cron_logfile . '.' . $num . '.gz')) rename($cron_logfile . '.' . $num . '.gz', $cron_logfile . '.' . ($num + 1) . '.gz');
133
134
135
136
					$num--;
				}
				
				// compress current logfile
137
				if(is_file($cron_logfile)) {
138
139
					$app->system->exec_safe("gzip -c ? > ?", $cron_logfile, $cron_logfile . '.1.gz');
					$app->system->exec_safe("cat /dev/null > ?", $cron_logfile);
140
141
				}
				// remove older logs
Florian Schaal's avatar
Florian Schaal committed
142
				$num = $log_retention;
143
144
145
146
147
				while(is_file($cron_logfile . '.' . $num . '.gz')) {
					@unlink($cron_logfile . '.' . $num . '.gz');
					$num++;
				}
			}
148

149
150
151
152
			// rotate and compress the error.log 
			$error_logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/error.log');
			// rename older files (move up by one)
			$num = $log_retention;
153
154
			while($num >= 1) {
				if(is_file($error_logfile . '.' . $num . '.gz')) rename($error_logfile . '.' . $num . '.gz', $error_logfile . '.' . ($num + 1) . '.gz');
155
156
157
158
				$num--;
			}
			// compress current logfile
			if(is_file($error_logfile)) {
159
160
				$app->system->exec_safe("gzip -c ? > ?", $error_logfile, $error_logfile . '.1.gz');
				$app->system->exec_safe("cat /dev/null > ?", $error_logfile);
161
162
			}

Florian Schaal's avatar
Florian Schaal committed
163
164
165
166
167
168
169
170
171
			// delete logfiles after x days (default 10)
			if($log_retention > 0) {
				foreach (glob($rec['document_root'].'/' . $log_folder . '/'."*.log*") as $logfile) {
					$now = time();
					if (is_file($logfile))
						if ($now - filemtime($logfile) >= 60 * 60 * 24 * $log_retention)
							unlink($logfile);
				}
			}
172
173
174
175
176
177

		}

		//* Delete old logfiles in /var/log/ispconfig/httpd/ that were created by vlogger for the hostname of the server
		exec('hostname -f', $tmp_hostname);
		if($tmp_hostname[0] != '' && is_dir('/var/log/ispconfig/httpd/'.$tmp_hostname[0])) {
178
			$app->system->exec_safe("cd ?; find . -mtime +$max_syslog -name '*.log' | xargs rm > /dev/null 2> /dev/null", '/var/log/ispconfig/httpd/'.$tmp_hostname[0]);
179
180
181
182
183
184
185
186
		}
		unset($tmp_hostname);

		//######################################################################################################
		// Rotate the ispconfig.log file
		//######################################################################################################


187
188
		$ispconfig_logfiles = array('ispconfig.log', 'cron.log', 'auth.log');
		foreach($ispconfig_logfiles as $ispconfig_logfile) {
Florian Schaal's avatar
Florian Schaal committed
189
			$num = $max_syslog;
190
191
			$ispconfig_logfile = escapeshellcmd($conf['ispconfig_log_dir'].'/'.$ispconfig_logfile);
			// rename older files (move up by one)
Florian Schaal's avatar
Florian Schaal committed
192
193
			while($num >= 1) {
				if(is_file($ispconfig_logfile . '.' . $num . '.gz')) rename($ispconfig_logfile . '.' . $num . '.gz', $ispconfig_logfile . '.' . ($num + 1) . '.gz');
194
195
196
197
				$num--;
			}
			// compress current logfile
			if(is_file($ispconfig_logfile)) {
198
199
				$app->system->exec_safe("gzip -c ? > ?", $ispconfig_logfile, $ispconfig_logfile . '.1.gz');
				$app->system->exec_safe("cat /dev/null > ?", $ispconfig_logfile);
200
201
			}
			// remove older logs
Florian Schaal's avatar
Florian Schaal committed
202
			$num = $max_syslog;
203
204
205
206
			while(is_file($ispconfig_logfile . '.' . $num . '.gz')) {
				@unlink($ispconfig_logfile . '.' . $num . '.gz');
				$num++;
			}
207
208
209
210
211
212
		}

		//######################################################################################################
		// Cleanup website tmp directories
		//######################################################################################################

213
214
		$sql = "SELECT domain_id, domain, document_root, system_user FROM web_domain WHERE server_id = ?";
		$records = $app->db->queryAllRecords($sql, $conf['server_id']);
215
216
217
		$app->uses('system');
		if(is_array($records)) {
			foreach($records as $rec){
218
				$tmp_path = realpath($rec['document_root'].'/tmp');
219
				if($tmp_path != '' && strlen($tmp_path) > 10 && is_dir($tmp_path) && $app->system->is_user($rec['system_user'])){
220
					exec("cd ?; find . -mtime +1 -name 'sess_*' | grep -v -w .no_delete | xargs rm > /dev/null 2> /dev/null", $tmp_path);
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
				}
			}
		}

		//######################################################################################################
		// Cleanup logs in master database (only the "master-server")
		//######################################################################################################

		if ($app->dbmaster == $app->db) {
			/** 7 days */


			$tstamp = time() - (60*60*24*7);

			/*
236
237
238
239
240
             *  Keep 7 days in sys_log
             * (we can delete the old items, because if they are OK, they don't interrest anymore
             * if they are NOT ok, the server will try to process them in 1 minute and so the
             * error appears again after 1 minute. So it is no problem to delete the old one!
             */
241
242
			$sql = "DELETE FROM sys_log WHERE tstamp < ? AND server_id != 0";
			$app->dbmaster->query($sql, $tstamp);
243

244
			/*
245
246
247
248
             * Delete all remote-actions "done" and older than 7 days
             * ATTENTION: We have the same problem as described in cleaning the datalog. We must not
             * delete the last entry
             */
249
250
251
			$sql = "SELECT max(action_id) FROM sys_remoteaction";
			$res = $app->dbmaster->queryOneRecord($sql);
			$maxId = $res['max(action_id)'];
252
253
			$sql =  "DELETE FROM sys_remoteaction WHERE tstamp < ? AND action_state = 'ok' AND action_id < ?";
			$app->dbmaster->query($sql, $tstamp, $maxId);
254
255

			/*
256
257
258
259
260
261
262
263
264
265
266
267
268
269
             * The sys_datalog is more difficult.
             * 1) We have to keet ALL entries with
             *    server_id=0, because they depend on ALL servers (even if they are not
             *    actually in the system (and will be insered in 3 days or so).
             * 2) We have to keey ALL entries which are not actually precessed by the
             *    server never mind how old they are!
             * 3) We have to keep the entry with the highest autoinc-id, because mysql calculates the
             *    autoinc-id as "new value = max(row) +1" and does not store this in a separate table.
             *    This means, if we delete to entry with the highest autoinc-value then this value is
             *    reused as autoinc and so there are more than one entries with the same value (over
             *    for example 4 Weeks). This is confusing for our system.
             *    ATTENTION 2) and 3) is in some case NOT the same! so we have to check both!
             */

270
271
272
273
274
275
276
277
278
279
280
281
282
			/* First we need all servers and the last sys_datalog-id they processed */
			$sql = "SELECT server_id, updated FROM server ORDER BY server_id";
			$records = $app->dbmaster->queryAllRecords($sql);

			/* Then we need the highest value ever */
			$sql = "SELECT max(datalog_id) FROM sys_datalog";
			$res = $app->dbmaster->queryOneRecord($sql);
			$maxId = $res['max(datalog_id)'];

			/* Then delete server by server */
			foreach($records as $server) {
				$tmp_server_id = intval($server['server_id']);
				if($tmp_server_id > 0) {
283
284
285
					$sql =  "DELETE FROM sys_datalog WHERE tstamp < ? AND server_id = ? AND datalog_id < ? AND datalog_id < ?";
					//  echo $sql . "\n";
					$app->dbmaster->query($sql, $tstamp, $server['server_id'], $server['updated'], $maxId);
286
287
288
289
290
291
292
293
294
295
296
297
298
299
				}
			}
		}


		parent::onRunJob();
	}

	/* this function is optional if it contains no custom code */
	public function onAfterRun() {
		global $app;

		parent::onAfterRun();
	}
300
301
302
303

}

?>