Skip to content

Commit 2ece9ca

Browse files
committed
clean up in 150-goaccess.inc.php
1 parent 6b4966d commit 2ece9ca

File tree

1 file changed

+95
-91
lines changed

1 file changed

+95
-91
lines changed

server/lib/classes/cron.d/150-goaccess.inc.php

Lines changed: 95 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -80,133 +80,137 @@ public function onRunJob() {
8080
system("type goaccess 2>&1>/dev/null", $retval);
8181
if ($retval === 0) {
8282

83-
foreach($records as $rec) {
84-
$yesterday = date('Ymd', strtotime("-1 day", time()));
83+
foreach($records as $rec) {
84+
$yesterday = date('Ymd', strtotime("-1 day", time()));
8585

86-
$log_folder = 'log';
86+
$log_folder = 'log';
8787

88-
if($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') {
89-
$tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = ?', $rec['parent_domain_id']);
90-
$subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
91-
if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id'];
92-
$log_folder .= '/' . $subdomain_host;
93-
unset($tmp);
94-
}
88+
if($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') {
89+
$tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = ?', $rec['parent_domain_id']);
90+
$subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
91+
if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id'];
92+
$log_folder .= '/' . $subdomain_host;
93+
unset($tmp);
94+
}
9595

96-
$logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log');
96+
$logfile = $rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log';
9797

98-
if(!@is_file($logfile)) {
99-
$logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log.gz');
10098
if(!@is_file($logfile)) {
101-
continue;
99+
$logfile = $rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log.gz';
100+
if(!@is_file($logfile)) {
101+
continue;
102+
}
102103
}
103-
}
104104

105-
$web_folder = (($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') ? $rec['web_folder'] : 'web');
106-
$domain = escapeshellcmd($rec['domain']);
107-
$statsdir = escapeshellcmd($rec['document_root'].'/'.$web_folder.'/stats');
108-
$goaccess_conf = escapeshellcmd($rec['document_root'].'/log/goaccess.conf');
105+
$web_folder = (($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') ? $rec['web_folder'] : 'web');
106+
$domain = $rec['domain'];
107+
$statsdir = $rec['document_root'].'/'.$web_folder.'/stats';
108+
$goaccess_conf = $rec['document_root'].'/log/goaccess.conf';
109109

110-
/*
111-
In case that you use a different log format, you should use a custom goaccess.conf which you'll have to put into /usr/local/ispconfig/server/conf-custom/.
112-
By default the originally, with GoAccess shipped goaccess.conf from /etc/ will be used along with the log-format value COMBINED.
113-
*/
114-
115-
if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master") && (!file_exists($goaccess_conf))) {
116-
copy("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master", $goaccess_conf);
117-
} elseif(!file_exists($goaccess_conf)) {
118110
/*
119-
By default the goaccess.conf should get copied by the webserver plugin but in case it wasn't, or it got deleted by accident we gonna copy it again to the destination dir.
120-
Also there was no /usr/local/ispconfig/server/conf-custom/goaccess.conf.master, so we gonna use /etc/goaccess.conf as the base conf.
111+
In case that you use a different log format, you should use a custom goaccess.conf which you'll have to put into /usr/local/ispconfig/server/conf-custom/.
112+
By default the originally, with GoAccess shipped goaccess.conf from /etc/ or /etc/goaccess will be used along with the log-format value COMBINED.
121113
*/
122-
copy($goaccess_conf_main, $goaccess_conf);
123-
file_put_contents($goaccess_conf, preg_replace('/^(#)?log-format COMBINED/m', "log-format COMBINED", file_get_contents($goaccess_conf)));
124-
}
125114

126-
/* Update the primary domain name in the title, it could occasionally change */
127-
if(is_file($goaccess_conf) && (filesize($goaccess_conf) > 0)) {
128-
$goaccess_content = file_get_contents($goaccess_conf);
129-
file_put_contents($goaccess_conf, preg_replace('/^(#)?html-report-title(.*)?/m', "html-report-title $domain", file_get_contents($goaccess_conf)));
130-
}
115+
if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master") && (!file_exists($goaccess_conf))) {
116+
copy("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master", $goaccess_conf);
117+
} elseif(!file_exists($goaccess_conf)) {
118+
/*
119+
By default the goaccess.conf should get copied by the webserver plugin but in case it wasn't, or it got deleted by accident we gonna copy it again to the destination dir.
120+
Also there was no /usr/local/ispconfig/server/conf-custom/goaccess.conf.master, so we gonna use /etc/goaccess.conf as the base conf.
121+
*/
122+
copy($goaccess_conf_main, $goaccess_conf);
123+
file_put_contents($goaccess_conf, preg_replace('/^(#)?log-format COMBINED/m', "log-format COMBINED", file_get_contents($goaccess_conf)));
124+
}
125+
126+
/* Update the primary domain name in the title, it could occasionally change */
127+
if(is_file($goaccess_conf) && (filesize($goaccess_conf) > 0)) {
128+
$goaccess_content = file_get_contents($goaccess_conf);
129+
file_put_contents($goaccess_conf, preg_replace('/^(#)?html-report-title(.*)?/m', "html-report-title $domain", file_get_contents($goaccess_conf)));
130+
unset($goaccess_content);
131+
}
131132

132133

133-
if(!@is_dir($statsdir)) mkdir($statsdir);
134-
$username = escapeshellcmd($rec['system_user']);
135-
$groupname = escapeshellcmd($rec['system_group']);
136-
$docroot = $rec['document_root'];
137134

138-
$goa_db_dir = $docroot.'/'.$web_folder.'/stats/.db/';
139-
$output_html = $docroot.'/'.$web_folder.'/stats/goaindex.html';
140-
if(!@is_dir($goa_db_dir)) mkdir($goa_db_dir);
135+
if(!@is_dir($statsdir)) mkdir($statsdir);
136+
$username = $rec['system_user'];
137+
$groupname = $rec['system_group'];
138+
$docroot = $rec['document_root'];
141139

142-
if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
143-
symlink($logfile, '/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
140+
$goa_db_dir = $docroot.'/'.$web_folder.'/stats/.db/';
141+
$output_html = $docroot.'/'.$web_folder.'/stats/goaindex.html';
142+
if(!@is_dir($goa_db_dir)) mkdir($goa_db_dir);
143+
144+
if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
145+
symlink($logfile, '/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
144146

145147

146-
chown($statsdir, $username);
147-
chgrp($statsdir, $groupname);
148+
chown($statsdir, $username);
149+
chgrp($statsdir, $groupname);
148150

149-
$goamonth = date("n");
150-
$goayear = date("Y");
151+
$goamonth = date("n");
152+
$goayear = date("Y");
151153

152-
if (date("d") == 1) {
153-
$goamonth = date("m")-1;
154-
if (date("m") == 1) {
155-
$goayear = date("Y")-1;
156-
$goamonth = "12";
154+
if (date("d") == 1) {
155+
$goamonth = date("m")-1;
156+
if (date("m") == 1) {
157+
$goayear = date("Y")-1;
158+
$goamonth = "12";
159+
}
157160
}
158-
}
159161

160162

161-
if (date("d") == 2) {
162-
$goamonth = date("m")-1;
163-
if (date("m") == 1) {
164-
$goayear = date("Y")-1;
165-
$goamonth = "12";
166-
}
163+
if (date("d") == 2) {
164+
$goamonth = date("m")-1;
165+
if (date("m") == 1) {
166+
$goayear = date("Y")-1;
167+
$goamonth = "12";
168+
}
167169

168-
$statsdirold = $statsdir."/".$goayear."-".$goamonth."/";
170+
$statsdirold = $statsdir."/".$goayear."-".$goamonth."/";
169171

170-
if(!is_dir($statsdirold)) {
171-
mkdir($statsdirold);
172-
}
172+
if(!is_dir($statsdirold)) {
173+
mkdir($statsdirold);
174+
}
173175

174-
rename($goa_db_dir, $statsdirold.'db');
175-
mkdir($goa_db_dir);
176+
// don't rotate db files per month
177+
//rename($goa_db_dir, $statsdirold.'db');
178+
//mkdir($goa_db_dir);
176179

177-
$files = scandir($statsdir);
178-
foreach ($files as $file) {
179-
if (substr($file, 0, 1) != "." && !is_dir("$statsdir"."/"."$file") && substr($file, 0, 1) != "w" && substr($file, 0, 1) != "i") copy("$statsdir"."/"."$file", "$statsdirold"."$file");
180+
$files = scandir($statsdir);
181+
182+
foreach ($files as $file) {
183+
if (substr($file, 0, 1) != "." && !is_dir("$statsdir"."/"."$file") && substr($file, 0, 1) != "w" && substr($file, 0, 1) != "i") copy("$statsdir"."/"."$file", "$statsdirold"."$file");
184+
}
180185
}
181-
}
182186

183187

184-
$output = shell_exec('goaccess --help');
188+
$output = shell_exec('goaccess --help');
185189

186-
if(preg_match('/keep-db-files/', $output)) {
187-
$app->system->exec_safe("goaccess -f ? --config-file ? --load-from-disk --keep-db-files --db-path=? --output=?", $logfile, $goaccess_conf, $goa_db_dir, $output_html);
190+
if(preg_match('/keep-db-files/', $output)) {
191+
$app->system->exec_safe("goaccess -f ? --config-file ? --load-from-disk --keep-db-files --db-path=? --output=?", $logfile, $goaccess_conf, $goa_db_dir, $output_html);
188192

189-
if(!is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
190-
if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master")) {
191-
copy("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
192-
} else {
193-
copy("/usr/local/ispconfig/server/conf/goaccess_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
193+
if(!is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
194+
if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master")) {
195+
copy("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
196+
} else {
197+
copy("/usr/local/ispconfig/server/conf/goaccess_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
198+
}
194199
}
195-
}
196200

197-
$app->log('Created GoAccess statistics for ' . $domain, LOGLEVEL_DEBUG);
198-
if(is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
199-
chown($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_user']);
200-
chgrp($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_group']);
201-
}
201+
$app->log('Created GoAccess statistics for ' . $domain, LOGLEVEL_DEBUG);
202+
if(is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
203+
chown($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_user']);
204+
chgrp($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_group']);
205+
}
202206

203-
$app->system->exec_safe('chown -R ?:? ?', $username, $groupname, $statsdir);
207+
$app->system->exec_safe('chown -R ?:? ?', $username, $groupname, $statsdir);
204208

205-
} else {
206-
$app->log("Stats not generated. The GoAccess binary was not compiled with btree support. Please recompile/reinstall GoAccess with btree support!", LOGLEVEL_ERROR);
207-
}
209+
} else {
210+
$app->log("Stats not generated. The GoAccess binary was not compiled with btree support. Please recompile/reinstall GoAccess with btree support!", LOGLEVEL_ERROR);
211+
}
208212

209-
unset($output);
213+
unset($output);
210214

211215
}
212216
} else {

0 commit comments

Comments
 (0)