Skip to content

Commit 029e643

Browse files
author
Till Brehm
committed
Merge branch '4988-goaccess' into 'stable-3.1'
Resolve "Add support for GoAccess Log Analyzer" Closes #4988 See merge request ispconfig/ispconfig3!1075
2 parents 8d8480f + 8fff80b commit 029e643

File tree

6 files changed

+530
-8
lines changed

6 files changed

+530
-8
lines changed

interface/web/sites/form/web_vhost_domain.tform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@
629629
'datatype' => 'VARCHAR',
630630
'formtype' => 'SELECT',
631631
'default' => 'awstats',
632-
'value' => array('webalizer' => 'Webalizer', 'awstats' => 'AWStats', '' => 'None')
632+
'value' => array('awstats' => 'AWStats', 'goaccess' => 'GoAccess', 'webalizer' => 'Webalizer','' => 'None')
633633
),
634634
//#################################
635635
// END Datatable fields

server/conf/awstats_index.php.master

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,21 @@ arsort($awprev);
4545
$options = "";
4646
foreach ($awprev as $key => $value)
4747
{
48+
49+
if(file_exists($value.'/awsindex.html') && file_exists($value.'/goaindex.html')) {
50+
$awstatsindex = 'awsindex.html';
51+
} elseif(file_exists($value.'/awsindex.html') && !file_exists($value.'/goaindex.html')) {
52+
$awstatsindex = 'awsindex.html';
53+
} else {
54+
$awstatsindex = 'goaindex.html';
55+
}
56+
4857
if($key == $current) $options .= "<option selected=\"selected\" value=\"{$awstatsindex}\">{$value}</option>\n";
4958
else $options .= "<option value=\"{$value}/{$awstatsindex}\">{$value}</option>\n";
5059
}
5160

61+
$awstatsindex = 'awsindex.html';
62+
5263
$html = "<!DOCTYPE html>\n<html>\n<head>\n<title>Stats</title>\n";
5364
$html .= "<style>\nhtml,body {margin:0px;padding:0px;width:100%;height:100%;background-color: #ccc;}\n";
5465
$html .= "#header\n{\nwidth:100%;margin:0px auto;\nheight:20px;\nposition:fixed;\npadding:4px;\ntext-align:center;\n}\n";
@@ -60,4 +71,4 @@ $html .= $options;
6071
$html .= "</select>\n</div>\n<iframe src=\"{$awstatsindex}\" id=\"content\"></iframe>\n";
6172
$html .= "</body></html>";
6273
echo $html;
63-
?>
74+
?>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
$yearmonth_text = "Jump to previous stats: ";
3+
$script = "<script>function load_content(url){var iframe = document.getElementById(\"content\");iframe.src = url;}</script>\n";
4+
5+
if ($handle = opendir('.'))
6+
{
7+
while(false !== ($file = readdir($handle)))
8+
{
9+
if (substr($file,0,1) != "." && is_dir($file))
10+
{
11+
$orderkey = substr($file,0,4).substr($file,5,2);
12+
if (substr($file,5,2) < 10 )
13+
{
14+
$orderkey = substr($file,0,4)."0".substr($file,5,2);
15+
}
16+
$goaprev[$orderkey] = $file;
17+
}
18+
}
19+
20+
$month = date("n");
21+
$year = date("Y");
22+
23+
if (date("d") == 1)
24+
{
25+
$month = date("m")-1;
26+
if (date("m") == 1)
27+
{
28+
$year = date("Y")-1;
29+
$month = "12";
30+
}
31+
}
32+
33+
$current = $year.$month;
34+
if ( $month < 10 ) {
35+
$current = $year."0".$month;
36+
}
37+
$goaprev[$current] = $year."-".$month;
38+
39+
closedir($handle);
40+
}
41+
42+
arsort($goaprev);
43+
44+
$options = "";
45+
foreach ($goaprev as $key => $value)
46+
{
47+
48+
if(file_exists($value.'/awsindex.html') && file_exists($value.'/goaindex.html')) {
49+
$goaccessindex = 'goaindex.html';
50+
} elseif(file_exists($value.'/awsindex.html') && !file_exists($value.'/goaindex.html')) {
51+
$goaccessindex = 'awsindex.html';
52+
} else {
53+
$goaccessindex = 'goaindex.html';
54+
}
55+
56+
if($key == $current) $options .= "<option selected=\"selected\" value=\"{$goaccessindex}\">{$value}</option>\n";
57+
else $options .= "<option value=\"{$value}/{$goaccessindex}\">{$value}</option>\n";
58+
59+
}
60+
$goaccessindex = 'goaindex.html';
61+
62+
$html = "<!DOCTYPE html>\n<html>\n<head>\n<title>Stats</title>\n";
63+
$html .= "<style>\nhtml,body {margin:0px;padding:0px;width:100%;height:100%;background-color: #ccc;}\n";
64+
$html .= "#header\n{\nwidth:100%;margin:0px auto;\nheight:20px;\nposition:fixed;\npadding:4px;\ntext-align:center;\n}\n";
65+
$html .= "iframe {width:100%;height:95%;margin:0px;margin-top:40px;border:0px;padding:0px;}\n</style>\n</head>\n<body>\n";
66+
$html .= $script;
67+
$html .= "<div id=\"header\">{$yearmonth_text}\n";
68+
$html .= "<select name=\"goadate\" onchange=\"load_content(this.value)\">\n";
69+
$html .= $options;
70+
$html .= "</select>\n</div>\n<iframe src=\"{$goaccessindex}\" id=\"content\"></iframe>\n";
71+
$html .= "</body></html>";
72+
echo $html;
73+
?>
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2013, Marius Cramer, pixcept KG
5+
Copyright (c) 2020, Michael Seevogel
6+
All rights reserved.
7+
8+
Redistribution and use in source and binary forms, with or without modification,
9+
are permitted provided that the following conditions are met:
10+
11+
* Redistributions of source code must retain the above copyright notice,
12+
this list of conditions and the following disclaimer.
13+
* Redistributions in binary form must reproduce the above copyright notice,
14+
this list of conditions and the following disclaimer in the documentation
15+
and/or other materials provided with the distribution.
16+
* Neither the name of ISPConfig nor the names of its contributors
17+
may be used to endorse or promote products derived from this software without
18+
specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
class cronjob_goaccess extends cronjob {
33+
34+
// job schedule
35+
protected $_schedule = '0 0 * * *';
36+
37+
/* this function is optional if it contains no custom code */
38+
public function onPrepare() {
39+
global $app;
40+
41+
parent::onPrepare();
42+
}
43+
44+
/* this function is optional if it contains no custom code */
45+
public function onBeforeRun() {
46+
global $app;
47+
48+
return parent::onBeforeRun();
49+
}
50+
51+
public function onRunJob() {
52+
global $app, $conf;
53+
54+
55+
//######################################################################################################
56+
// Create goaccess statistics
57+
//######################################################################################################
58+
59+
$sql = "SELECT domain_id, domain, document_root, web_folder, type, system_user, system_group, parent_domain_id FROM web_domain WHERE (type = 'vhost' or type = 'vhostsubdomain' or type = 'vhostalias') and stats_type = 'goaccess' AND server_id = ?";
60+
$records = $app->db->queryAllRecords($sql, $conf['server_id']);
61+
62+
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
63+
64+
$goaccess_conf_locs = array('/etc/goaccess.conf', '/etc/goaccess/goaccess.conf');
65+
$count = 0;
66+
67+
foreach($goaccess_conf_locs as $goa_loc) {
68+
if(is_file($goa_loc) && (filesize($goa_loc) > 0)) {
69+
$goaccess_conf_main = $goa_loc;
70+
break;
71+
} else {
72+
$count++;
73+
if($count == 2) {
74+
$app->log("No GoAccess base config found. Make sure that GoAccess is installed and that the goaccess.conf does exist in /etc or /etc/goaccess", LOGLEVEL_ERROR);
75+
}
76+
}
77+
}
78+
79+
80+
/* Check if goaccess binary is in path */
81+
system("type goaccess 2>&1>/dev/null", $retval);
82+
if ($retval === 0) {
83+
84+
foreach($records as $rec) {
85+
$yesterday = date('Ymd', strtotime("-1 day", time()));
86+
87+
$log_folder = 'log';
88+
89+
if($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') {
90+
$tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = ?', $rec['parent_domain_id']);
91+
$subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
92+
if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id'];
93+
$log_folder .= '/' . $subdomain_host;
94+
unset($tmp);
95+
}
96+
97+
$logfile = $rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log';
98+
99+
if(!@is_file($logfile)) {
100+
$logfile = $rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log.gz';
101+
if(!@is_file($logfile)) {
102+
continue;
103+
}
104+
}
105+
106+
$web_folder = (($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') ? $rec['web_folder'] : 'web');
107+
$domain = $rec['domain'];
108+
$statsdir = $rec['document_root'].'/'.$web_folder.'/stats';
109+
$goaccess_conf = $rec['document_root'].'/log/goaccess.conf';
110+
111+
/*
112+
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/.
113+
By default the originally, with GoAccess shipped goaccess.conf from /etc/ or /etc/goaccess will be used along with the log-format value COMBINED.
114+
*/
115+
116+
if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master") && (!file_exists($goaccess_conf))) {
117+
$app->system->copy("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master", $goaccess_conf);
118+
} elseif(!file_exists($goaccess_conf)) {
119+
/*
120+
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.
121+
Also there was no /usr/local/ispconfig/server/conf-custom/goaccess.conf.master, so we gonna use /etc/goaccess.conf as the base conf.
122+
*/
123+
124+
$app->system->copy($goaccess_conf_main, $goaccess_conf);
125+
$content = $app->system->file_get_contents($goaccess_conf, true);
126+
$content = preg_replace('/^(#)?log-format COMBINED/m', "log-format COMBINED", $content);
127+
$app->system->file_put_contents($goaccess_conf, $content, true);
128+
unset($content);
129+
}
130+
131+
/* Update the primary domain name in the title, it could occasionally change */
132+
if(is_file($goaccess_conf) && (filesize($goaccess_conf) > 0)) {
133+
$content = $app->system->file_get_contents($goaccess_conf, true);
134+
$content = preg_replace('/^(#)?html-report-title(.*)/m', "html-report-title $domain", $content);
135+
$app->system->file_put_contents($goaccess_conf, $content, true);
136+
unset($content);
137+
}
138+
139+
$username = $rec['system_user'];
140+
$groupname = $rec['system_group'];
141+
$docroot = $rec['document_root'];
142+
143+
if(!@is_dir($statsdir)) $app->system->mkdirpath($statsdir, 0755, $username, $groupname);
144+
145+
$goa_db_dir = $docroot.'/'.$web_folder.'/stats/.db/';
146+
$output_html = $docroot.'/'.$web_folder.'/stats/goaindex.html';
147+
if(!@is_dir($goa_db_dir)) $app->system->mkdirpath($goa_db_dir);
148+
149+
if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) $app->system->unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
150+
symlink($logfile, '/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
151+
152+
153+
$app->system->exec_safe('chown -R ?:? ?', $username, $groupname, $statsdir);
154+
155+
$goamonth = date("n");
156+
$goayear = date("Y");
157+
158+
if (date("d") == 1) {
159+
$goamonth = date("m")-1;
160+
if (date("m") == 1) {
161+
$goayear = date("Y")-1;
162+
$goamonth = "12";
163+
}
164+
}
165+
166+
if (date("d") == 2) {
167+
$goamonth = date("m")-1;
168+
if (date("m") == 1) {
169+
$goayear = date("Y")-1;
170+
$goamonth = "12";
171+
}
172+
173+
$statsdirold = $statsdir."/".$goayear."-".$goamonth."/";
174+
175+
if(!is_dir($statsdirold)) {
176+
$app->system->mkdirpath($statsdirold, 0755, $username, $groupname);
177+
}
178+
179+
// don't rotate db files per month
180+
//rename($goa_db_dir, $statsdirold.'db');
181+
//mkdir($goa_db_dir);
182+
183+
$files = scandir($statsdir);
184+
185+
foreach ($files as $file) {
186+
if (substr($file, 0, 1) != "." && !is_dir("$statsdir"."/"."$file") && substr($file, 0, 1) != "w" && substr($file, 0, 1) != "i") $app->system->copy("$statsdir"."/"."$file", "$statsdirold"."$file");
187+
}
188+
}
189+
190+
// Get the GoAccess version
191+
$match = array();
192+
193+
$goaccess_version = $app->system->system_safe('goaccess --version 2>&1');
194+
195+
if(preg_match('/[0-9]\.[0-9]{1,2}/', $goaccess_version, $match)) {
196+
$goaccess_version = $match[0];
197+
}
198+
199+
200+
/*
201+
* GoAccess removed with 1.4 btree support and supports from this version on only "In-Memory with On-Disk Persitance Storage".
202+
* For versions prior 1.4 you need GoAccess with btree support compiled!
203+
*/
204+
205+
$cust_lang = $conf['language']."_".strtoupper($conf['language']);
206+
207+
if(version_compare($goaccess_version,1.4) >= 0) {
208+
$app->system->exec_safe("LANG=? goaccess -f ? --config-file ? --restore --persist --db-path=? --output=?", $cust_lang, $logfile, $goaccess_conf, $goa_db_dir, $output_html);
209+
} else {
210+
$output = $app->system->system_safe('goaccess --help 2>&1');
211+
preg_match('/keep-db-files/', $output, $match);
212+
if($match[0] == "keep-db-files") {
213+
$app->system->exec_safe("LANG=? goaccess -f ? --config-file ? --load-from-disk --keep-db-files --db-path=? --output=?", $cust_lang, $logfile, $goaccess_conf, $goa_db_dir, $output_html);
214+
} else {
215+
$app->log("Stats not generated. The GoAccess binary was not compiled with btree support. Please recompile/reinstall GoAccess with btree support, or install GoAccess version >= 1.4!", LOGLEVEL_ERROR);
216+
}
217+
unset($output);
218+
}
219+
unset($cust_lang);
220+
221+
if(!is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
222+
if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master")) {
223+
$app->system->copy("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
224+
} else {
225+
$app->system->copy("/usr/local/ispconfig/server/conf/goaccess_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
226+
}
227+
}
228+
229+
$app->log('Created GoAccess statistics for ' . $domain, LOGLEVEL_DEBUG);
230+
if(is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
231+
chown($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_user']);
232+
chgrp($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_group']);
233+
}
234+
235+
$app->system->exec_safe('chown -R ?:? ?', $username, $groupname, $statsdir);
236+
}
237+
238+
} else {
239+
$app->log("Stats not generated. The GoAccess binary couldn't be found. Make sure that GoAccess is installed and that it is in \$PATH", LOGLEVEL_ERROR);
240+
}
241+
242+
parent::onRunJob();
243+
}
244+
245+
/* this function is optional if it contains no custom code */
246+
public function onAfterRun() {
247+
global $app;
248+
249+
parent::onAfterRun();
250+
}
251+
252+
}
253+
254+
?>

0 commit comments

Comments
 (0)