Skip to content

Commit f6d3c92

Browse files
author
florian030
committed
update logrotate
1 parent fe8ea3f commit f6d3c92

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

server/lib/classes/cron.d/200-logfiles.inc.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function onBeforeRun() {
4949

5050
public function onRunJob() {
5151
global $app, $conf;
52+
53+
$max_syslog = 10;
5254

5355
//######################################################################################################
5456
// Make the web logfiles directories world readable to enable ftp access
@@ -76,7 +78,7 @@ public function onRunJob() {
7678
unset($tmp);
7779
}
7880

79-
$log_retention = $rec['log_retention'];
81+
$log_retention = $rec['log_retention'];
8082

8183
$logfile = $rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log';
8284
$total_bytes = 0;
@@ -118,7 +120,7 @@ public function onRunJob() {
118120
$cron_logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/' . $cron_logfile);
119121

120122
// rename older files (move up by one)
121-
$num = 7;
123+
$num = $log_retention;
122124
while($num >= 1 && is_file($cron_logfile . '.' . $num . '.gz')) {
123125
rename($cron_logfile . '.' . $num . '.gz', $cron_logfile . '.' . ($num + 1) . '.gz');
124126
$num--;
@@ -130,7 +132,7 @@ public function onRunJob() {
130132
exec("cat /dev/null > $cron_logfile");
131133
}
132134
// remove older logs
133-
$num = 7;
135+
$num = $log_retention;
134136
while(is_file($cron_logfile . '.' . $num . '.gz')) {
135137
@unlink($cron_logfile . '.' . $num . '.gz');
136138
$num++;
@@ -151,38 +153,37 @@ public function onRunJob() {
151153
exec("cat /dev/null > $error_logfile");
152154
}
153155

154-
// delete logfiles after x days (default 30)
155-
if($log_retention > 0) {
156-
foreach (glob($rec['document_root'].'/' . $log_folder . '/'."*.log*") as $logfile) {
157-
$now = time();
158-
if (is_file($logfile))
159-
if ($now - filemtime($logfile) >= 60 * 60 * 24 * $log_retention)
160-
unlink($logfile);
161-
}
162-
163-
}
156+
// delete logfiles after x days (default 10)
157+
if($log_retention > 0) {
158+
foreach (glob($rec['document_root'].'/' . $log_folder . '/'."*.log*") as $logfile) {
159+
$now = time();
160+
if (is_file($logfile))
161+
if ($now - filemtime($logfile) >= 60 * 60 * 24 * $log_retention)
162+
unlink($logfile);
163+
}
164+
}
164165

165166
}
166167

167168
//* Delete old logfiles in /var/log/ispconfig/httpd/ that were created by vlogger for the hostname of the server
168169
exec('hostname -f', $tmp_hostname);
169170
if($tmp_hostname[0] != '' && is_dir('/var/log/ispconfig/httpd/'.$tmp_hostname[0])) {
170-
exec('cd /var/log/ispconfig/httpd/'.$tmp_hostname[0]."; find . -mtime +30 -name '*.log' | xargs rm > /dev/null 2> /dev/null");
171+
exec('cd /var/log/ispconfig/httpd/'.$tmp_hostname[0]."; find . -mtime +$max_syslog -name '*.log' | xargs rm > /dev/null 2> /dev/null");
171172
}
172173
unset($tmp_hostname);
173174

174175
//######################################################################################################
175176
// Rotate the ispconfig.log file
176177
//######################################################################################################
177178

178-
$num = 10;
179179

180180
$ispconfig_logfiles = array('ispconfig.log', 'cron.log', 'auth.log');
181181
foreach($ispconfig_logfiles as $ispconfig_logfile) {
182+
$num = $max_syslog;
182183
$ispconfig_logfile = escapeshellcmd($conf['ispconfig_log_dir'].'/'.$ispconfig_logfile);
183184
// rename older files (move up by one)
184-
while($num >= 1 && is_file($ispconfig_logfile . '.' . $num . '.gz')) {
185-
rename($ispconfig_logfile . '.' . $num . '.gz', $ispconfig_logfile . '.' . ($num + 1) . '.gz');
185+
while($num >= 1) {
186+
if(is_file($ispconfig_logfile . '.' . $num . '.gz')) rename($ispconfig_logfile . '.' . $num . '.gz', $ispconfig_logfile . '.' . ($num + 1) . '.gz');
186187
$num--;
187188
}
188189
// compress current logfile
@@ -191,6 +192,7 @@ public function onRunJob() {
191192
exec("cat /dev/null > $ispconfig_logfile");
192193
}
193194
// remove older logs
195+
$num = $max_syslog;
194196
while(is_file($ispconfig_logfile . '.' . $num . '.gz')) {
195197
@unlink($ispconfig_logfile . '.' . $num . '.gz');
196198
$num++;

0 commit comments

Comments
 (0)