Commit 637a4543 authored by Michael's avatar Michael

GoAccess implementation V2

parent 9d5329c7
......@@ -610,11 +610,7 @@ $form["tabs"]['stats'] = array (
'datatype' => 'VARCHAR',
'formtype' => 'SELECT',
'default' => 'awstats',
<<<<<<< HEAD
'value' => array('webalizer' => 'Webalizer', 'awstats' => 'AWStats', 'goaccess' => 'GoAccess', '' => 'None')
=======
'value' => array('webalizer' => 'Webalizer', 'awstats' => 'AWStats', '' => 'None')
>>>>>>> upstream/master
'value' => array('awstats' => 'AWStats', 'goaccess' => 'GoAccess', 'webalizer' => 'Webalizer', '' => 'None')
),
//#################################
// ENDE Datatable fields
......
<?php
$yearmonth_text = "Jump to previous stats: ";
$goaccessindex = 'goaindex.html';
$script = "<script>function load_content(url){var iframe = document.getElementById(\"content\");iframe.src = url;}</script>\n";
if ($handle = opendir('.'))
{
while(false !== ($file = readdir($handle)))
{
if (substr($file,0,1) != "." && is_dir($file))
{
$orderkey = substr($file,0,4).substr($file,5,2);
if (substr($file,5,2) < 10 )
{
$orderkey = substr($file,0,4)."0".substr($file,5,2);
}
$goaprev[$orderkey] = $file;
}
}
$month = date("n");
$year = date("Y");
if (date("d") == 1)
{
$month = date("m")-1;
if (date("m") == 1)
{
$year = date("Y")-1;
$month = "12";
}
}
$current = $year.$month;
if ( $month < 10 ) {
$current = $year."0".$month;
}
$goaprev[$current] = $year."-".$month;
closedir($handle);
}
arsort($goaprev);
$options = "";
foreach ($goaprev as $key => $value)
{
if($key == $current) $options .= "<option selected=\"selected\" value=\"{$goaccessindex}\">{$value}</option>\n";
else $options .= "<option value=\"{$value}/{$goaccessindex}\">{$value}</option>\n";
}
$html = "<!DOCTYPE html>\n<html>\n<head>\n<title>Stats</title>\n";
$html .= "<style>\nhtml,body {margin:0px;padding:0px;width:100%;height:100%;background-color: #ccc;}\n";
$html .= "#header\n{\nwidth:100%;margin:0px auto;\nheight:20px;\nposition:fixed;\npadding:4px;\ntext-align:center;\n}\n";
$html .= "iframe {width:100%;height:90%;margin:0px;margin-top:40px;border:0px;padding:0px;}\n</style>\n</head>\n<body>\n";
$html .= $script;
$html .= "<div id=\"header\">{$yearmonth_text}\n";
$html .= "<select name=\"goadate\" onchange=\"load_content(this.value)\">\n";
$html .= $options;
$html .= "</select>\n</div>\n<iframe src=\"{$goaccessindex}\" id=\"content\"></iframe>\n";
$html .= "</body></html>";
echo $html;
?>
......@@ -54,15 +54,28 @@ class cronjob_goaccess extends cronjob {
// Create goaccess statistics
//######################################################################################################
$sql = "SELECT domain_id, domain, document_root, web_folder, type, parent_domain_id, system_user, system_group FROM web_domain WHERE (type = 'vhost' or type = 'vhostsubdomain' or type = 'vhostalias') and stats_type = 'goaccess' AND server_id = ?";
$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 = ?";
$records = $app->db->queryAllRecords($sql, $conf['server_id']);
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
$goaccess_conf_dir = '/etc/';
$goaccess_conf_main = $goaccess_conf_dir . 'goaccess.conf';
if(!file_exists($goaccess_conf_main) || !isset($goaccess_conf_main))
{
$app->log("No GoAccess base config found. Make sure that GoAccess is installed and that the goaccess.conf does exist in ".$goaccess_conf_dir.".", LOGLEVEL_WARN);
}
/* Check wether the goaccess binary is in path */
system('type goaccess', $retval);
if ($retval === 0) {
foreach($records as $rec) {
//$yesterday = date('Ymd',time() - 86400);
$yesterday = date('Ymd', strtotime("-1 day", time()));
$log_folder = 'log';
if($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') {
$tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = ?', $rec['parent_domain_id']);
$subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
......@@ -70,7 +83,9 @@ class cronjob_goaccess extends cronjob {
$log_folder .= '/' . $subdomain_host;
unset($tmp);
}
$logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log');
if(!@is_file($logfile)) {
$logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log.gz');
if(!@is_file($logfile)) {
......@@ -78,48 +93,104 @@ class cronjob_goaccess extends cronjob {
}
}
$web_folder = (($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') ? $rec['web_folder'] : 'web');
$domain = escapeshellcmd($rec['domain']);
$statsdir = escapeshellcmd($rec['document_root'].'/'.(($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') ? $rec['web_folder'] : 'web').'/stats');
$stats_file= $statsdir.'/index.html';
$goa_db_dir = $statsdir.'/db/';
$goaccess = '/usr/bin/goaccess';
$goaccess_conf_main = '/etc/goaccess.conf';
if(!@is_file($goaccess_conf_main)) {
$goaccess_conf_main = '/etc/goaccess/goaccess.conf';
}
$statsdir = escapeshellcmd($rec['document_root'].'/'.$web_folder.'/stats');
$goaccess_conf = escapeshellcmd($rec['document_root'].'/log/goaccess.conf');
if(is_file($statsdir.'/index.php')) unlink($statsdir.'/index.php');
if(!file_exists($goaccess_conf) || (filesize($goaccess_conf) == 0)) {
copy($goaccess_conf_main, $goaccess_conf);
/*
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/.
By default the originaly with GoAccess shipped goaccess.conf from /etc/ will be used along with the log-format value COMBINED.
*/
if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master") && (!file_exists($goaccess_conf))) {
copy("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master", $goaccess_conf);
} elseif(!file_exists($goaccess_conf)) {
/*
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.
Also there was no /usr/local/ispconfig/server/conf-custom/goaccess.conf.master, so we gonna use /etc/goaccess.conf as the base conf.
*/
copy($goaccess_conf_main, $goaccess_conf);
file_put_contents($goaccess_conf, preg_replace('/^(#)?log-format COMBINED/m', "log-format COMBINED", file_get_contents($goaccess_conf)));
}
if(@is_file($goaccess_conf)) {
/* Update the primary domain name in the title, it could occasionally change... */
if(is_file($goaccess_conf) && (filesize($goaccess_conf) > 0)) {
$goaccess_content = file_get_contents($goaccess_conf);
file_put_contents($goaccess_conf, preg_replace('/#log-format COMBINED/', 'log-format COMBINED', file_get_contents($goaccess_conf)));
file_put_contents($goaccess_conf, preg_replace('/(#)?html-report-title(.*)/', "html-report-title $domain", file_get_contents($goaccess_conf)));
file_put_contents($goaccess_conf, preg_replace('/^(#)?html-report-title(.*)?/m', "html-report-title $domain", file_get_contents($goaccess_conf)));
}
if(!@is_dir($statsdir)) mkdir($statsdir);
if(!@is_dir($goa_db_dir)) mkdir($goa_db_dir);
$username = escapeshellcmd($rec['system_user']);
$groupname = escapeshellcmd($rec['system_group']);
$docroot = $rec['document_root'];
$goa_db_dir = $docroot.'/'.$web_folder.'/stats/.db/';
$output_html = $docroot.'/'.$web_folder.'/stats/goaindex.html';
if(!@is_dir($goa_db_dir)) mkdir($goa_db_dir);
if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
symlink($logfile, '/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
chown($statsdir, $username);
chgrp($statsdir, $groupname);
exec("$goaccess -f $logfile --config-file=$goaccess_conf --load-from-disk --keep-db-files --db-path=$goa_db_dir --output=$stats_file 2>&1", $output, $return_var);
var_dump($goaccess_conf);
var_dump($goa_db_dir);
var_dump($return_var);
var_dump($output);
$goamonth = date("n");
$goayear = date("Y");
if (date("d") == 1) {
$goamonth = date("m")-1;
if (date("m") == 1) {
$goayear = date("Y")-1;
$goamonth = "12";
}
}
$command = "goaccess -f $logfile --config-file=$goaccess_conf --load-from-disk --keep-db-files --db-path=$goa_db_dir --output=$output_html";
if (date("d") == 2) {
$goamonth = date("m")-1;
if (date("m") == 1) {
$goayear = date("Y")-1;
$goamonth = "12";
}
$statsdirold = $statsdir."/".$goayear."-".$goamonth."/";
mkdir($statsdirold);
rename($goa_db_dir, $statsdirold.'db');
$files = scandir($statsdir);
foreach ($files as $file) {
if (substr($file, 0, 1) != "." && !is_dir("$statsdir"."/"."$file") && substr($file, 0, 1) != "w" && substr($file, 0, 1) != "i") copy("$statsdir"."/"."$file", "$statsdirold"."$file");
}
}
exec('chown -R '.$username.':'.$groupname.' '.$statsdir);
}
exec($command);
if(!is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master")) {
copy("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
} else {
copy("/usr/local/ispconfig/server/conf/goaccess_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
}
}
$app->log('Created GoAccess statistics with command: '.$command, LOGLEVEL_DEBUG);
if(is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
chown($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_user']);
chgrp($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_group']);
}
exec('chown -R '.$username.':'.$groupname.' '.$statsdir);
}
} else {
$app->log("Stats not generated. The GoAccess binary couldn't be found by which. Make sure that GoAccess is installed and that it is in \$PATH", LOGLEVEL_WARN);
}
parent::onRunJob();
}
......
......@@ -1250,7 +1250,7 @@ class nginx_plugin {
/* we don't need to store it.
/* Update the DB of the (local) Server */
$app->db->query("UPDATE web_domain SET ssl_request = '', ssl_cert = '', ssl_key = '' WHERE domain = ?", $data['new']['domain']);
$app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain_id = ?", $data['new']['domain']);
$app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain = ?", $data['new']['domain']);
/* Update also the master-DB of the Server-Farm */
$app->dbmaster->query("UPDATE web_domain SET ssl_request = '', ssl_cert = '', ssl_key = '' WHERE domain = ?", $data['new']['domain']);
$app->dbmaster->query("UPDATE web_domain SET ssl_action = '' WHERE domain = ?", $data['new']['domain']);
......@@ -1833,6 +1833,12 @@ class nginx_plugin {
$this->awstats_update($data, $web_config);
}
//* Create GoAccess configuration
if($data['new']['stats_type'] == 'goaccess' && ($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain' || $data['new']['type'] == 'vhostalias')) {
$this->goaccess_update($data, $web_config);
}
$this->php_fpm_pool_update($data, $web_config, $pool_dir, $pool_name, $socket_dir);
$this->hhvm_update($data, $web_config);
......@@ -2214,6 +2220,12 @@ class nginx_plugin {
$this->awstats_delete($data, $web_config);
}
//* Remove the GoAccess configuration file
if($data['old']['stats_type'] == 'goaccess') {
$this->goaccess_delete($data, $web_config);
}
//* Delete the web-backups
if($data['old']['type'] == 'vhost') {
$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
......@@ -2507,6 +2519,68 @@ class nginx_plugin {
}
}
//* Update the GoAccess configuration file
private function goaccess_update ($data, $web_config) {
global $app;
$web_folder = $data['new']['web_folder'];
if($data['new']['type'] == 'vhost') $web_folder = 'web';
$goaccess_conf_dir = '/etc/';
$goaccess_conf_main = $goaccess_conf_dir.'goaccess.conf';
if(!is_dir($data['new']['document_root']."/" . $web_folder . "/stats/")) mkdir($data['new']['document_root']."/" . $web_folder . "/stats/.db");
$goaccess_conf = escapeshellcmd($data['new']['document_root'].'/log/goaccess.conf');
/*
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/.
By default the originaly with GoAccess shipped goaccess.conf from /etc/ will be used along with the log-format value COMBINED.
*/
if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master") && (!file_exists($goaccess_conf))) {
copy("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master", $goaccess_conf);
} elseif(!file_exists($goaccess_conf)) {
/*
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.
Also there was no /usr/local/ispconfig/server/conf-custom/goaccess.conf.master, so we gonna use /etc/goaccess.conf as the base conf.
*/
copy($goaccess_conf_main, $goaccess_conf);
file_put_contents($goaccess_conf, preg_replace('/^(#)?log-format COMBINED/m', "log-format COMBINED", file_get_contents($goaccess_conf)));
}
if(file_exists($goaccess_conf)) {
$domain = escapeshellcmd($data['new']['domain']);
file_put_contents($goaccess_conf, preg_replace('/^(#)?html-report-title(.*)/m', "html-report-title $domain", file_get_contents($goaccess_conf)));
}
if(is_file($goaccess_conf) && (filesize($goaccess_conf) > 0)) {
$app->log('Created GoAccess config file: '.$goaccess_conf, LOGLEVEL_DEBUG);
} else {
$app->log("No GoAccess base config found. Make sure that GoAccess is installed and that the goaccess.conf does exist in ".$goaccess_conf_dir.".", LOGLEVEL_WARN);
}
if(is_file($data['new']['document_root']."/" . $web_folder . "/stats/index.html")) $app->system->unlink($data['new']['document_root']."/" . $web_folder . "/stats/index.html");
if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master")) {
$app->system->copy("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master", $data['new']['document_root']."/" . $web_folder . "/stats/index.php");
} else {
$app->system->copy("/usr/local/ispconfig/server/conf/goaccess_index.php.master", $data['new']['document_root']."/" . $web_folder . "/stats/index.php");
}
}
//* Delete the GoAccess configuration file
private function goaccess_delete ($data, $web_config) {
global $app;
$goaccess_conf = escapeshellcmd($data['new']['document_root'].'/log/goaccess.conf');
if ( @is_file($goaccess_conf) ) {
$app->system->unlink($goaccess_conf);
$app->log('Removed GoAccess config file: '.$goaccess_conf, LOGLEVEL_DEBUG);
}
}
private function hhvm_update($data, $web_config) {
global $app, $conf;
......@@ -2589,6 +2663,8 @@ class nginx_plugin {
private function php_fpm_pool_update ($data, $web_config, $pool_dir, $pool_name, $socket_dir) {
global $app, $conf;
$pool_dir = trim($pool_dir);
$rh_releasefiles = array('/etc/centos-release', '/etc/redhat-release');
// HHVM => PHP-FPM-Fallback
if($data['new']['php'] == 'php-fpm' || $data['new']['php'] == 'hhvm'){
if(trim($data['new']['fastcgi_php_version']) != ''){
......@@ -2649,9 +2725,28 @@ class nginx_plugin {
$tpl->setVar('fpm_pool', $pool_name);
$tpl->setVar('fpm_port', $web_config['php_fpm_start_port'] + $data['new']['domain_id'] - 1);
$tpl->setVar('fpm_user', $data['new']['system_user']);
$tpl->setVar('fpm_group', $data['new']['system_group']);
//Red Hat workaround for group ownership of socket files
foreach($rh_releasefiles as $rh_file) {
if(file_exists($rh_file) && (filesize($rh_file) > 0)) {
$tmp = file_get_contents($rh_file);
if(preg_match('/[67]+\.[0-9]+/m', $tmp)) {
$tpl->setVar('fpm_group', $data['new']['system_group']);
$tpl->setVar('fpm_listen_group', $data['new']['system_group']);
}
unset($tmp);
} elseif(!file_exists($rh_file)) {
//OS seems to be not Red Hat'ish
$tpl->setVar('fpm_group', $web_config['group']);
$tpl->setVar('fpm_listen_group', $web_config['group']);
}
break;
}
//$tpl->setVar('fpm_group', $web_config['group']);
$tpl->setVar('fpm_listen_user', $data['new']['system_user']);
$tpl->setVar('fpm_listen_group', $web_config['group']);
//$tpl->setVar('fpm_listen_group', $web_config['group']);
$tpl->setVar('fpm_domain', $data['new']['domain']);
$tpl->setVar('pm', $data['new']['pm']);
$tpl->setVar('pm_max_children', $data['new']['pm_max_children']);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment