|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright (c) 2013, Marius Cramer, pixcept KG |
| 5 | +All rights reserved. |
| 6 | +
|
| 7 | +Redistribution and use in source and binary forms, with or without modification, |
| 8 | +are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | + * Redistributions of source code must retain the above copyright notice, |
| 11 | + this list of conditions and the following disclaimer. |
| 12 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + this list of conditions and the following disclaimer in the documentation |
| 14 | + and/or other materials provided with the distribution. |
| 15 | + * Neither the name of ISPConfig nor the names of its contributors |
| 16 | + may be used to endorse or promote products derived from this software without |
| 17 | + specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 | +IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 23 | +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 26 | +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 | +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +*/ |
| 30 | + |
| 31 | +class cronjob_goaccess extends cronjob { |
| 32 | + |
| 33 | + // job schedule |
| 34 | + protected $_schedule = '0 0 * * *'; |
| 35 | + |
| 36 | + /* this function is optional if it contains no custom code */ |
| 37 | + public function onPrepare() { |
| 38 | + global $app; |
| 39 | + |
| 40 | + parent::onPrepare(); |
| 41 | + } |
| 42 | + |
| 43 | + /* this function is optional if it contains no custom code */ |
| 44 | + public function onBeforeRun() { |
| 45 | + global $app; |
| 46 | + |
| 47 | + return parent::onBeforeRun(); |
| 48 | + } |
| 49 | + |
| 50 | + public function onRunJob() { |
| 51 | + global $app, $conf; |
| 52 | + |
| 53 | + //###################################################################################################### |
| 54 | + // Create goaccess statistics |
| 55 | + //###################################################################################################### |
| 56 | + |
| 57 | + $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 = ?"; |
| 58 | + $records = $app->db->queryAllRecords($sql, $conf['server_id']); |
| 59 | + |
| 60 | + $web_config = $app->getconf->get_server_config($conf['server_id'], 'web'); |
| 61 | + |
| 62 | + $goaccess_conf_dir = '/etc/'; |
| 63 | + $goaccess_conf_main = $goaccess_conf_dir . 'goaccess.conf'; |
| 64 | + |
| 65 | + if(!file_exists($goaccess_conf_main) || !isset($goaccess_conf_main)) |
| 66 | + { |
| 67 | + $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); |
| 68 | + } |
| 69 | + |
| 70 | + /* Check wether the goaccess binary is in path */ |
| 71 | + system('type goaccess', $retval); |
| 72 | + if ($retval === 0) { |
| 73 | + |
| 74 | + foreach($records as $rec) { |
| 75 | + $yesterday = date('Ymd', strtotime("-1 day", time())); |
| 76 | + |
| 77 | + $log_folder = 'log'; |
| 78 | + |
| 79 | + if($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') { |
| 80 | + $tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = ?', $rec['parent_domain_id']); |
| 81 | + $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']); |
| 82 | + if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id']; |
| 83 | + $log_folder .= '/' . $subdomain_host; |
| 84 | + unset($tmp); |
| 85 | + } |
| 86 | + |
| 87 | + $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log'); |
| 88 | + |
| 89 | + if(!@is_file($logfile)) { |
| 90 | + $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log.gz'); |
| 91 | + if(!@is_file($logfile)) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + $web_folder = (($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') ? $rec['web_folder'] : 'web'); |
| 97 | + $domain = escapeshellcmd($rec['domain']); |
| 98 | + $statsdir = escapeshellcmd($rec['document_root'].'/'.$web_folder.'/stats'); |
| 99 | + $goaccess_conf = escapeshellcmd($rec['document_root'].'/log/goaccess.conf'); |
| 100 | + |
| 101 | + /* |
| 102 | + 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/. |
| 103 | + By default the originally, with GoAccess shipped goaccess.conf from /etc/ will be used along with the log-format value COMBINED. |
| 104 | + */ |
| 105 | + |
| 106 | + if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master") && (!file_exists($goaccess_conf))) { |
| 107 | + copy("/usr/local/ispconfig/server/conf-custom/goaccess.conf.master", $goaccess_conf); |
| 108 | + } elseif(!file_exists($goaccess_conf)) { |
| 109 | + /* |
| 110 | + 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. |
| 111 | + Also there was no /usr/local/ispconfig/server/conf-custom/goaccess.conf.master, so we gonna use /etc/goaccess.conf as the base conf. |
| 112 | + */ |
| 113 | + copy($goaccess_conf_main, $goaccess_conf); |
| 114 | + file_put_contents($goaccess_conf, preg_replace('/^(#)?log-format COMBINED/m', "log-format COMBINED", file_get_contents($goaccess_conf))); |
| 115 | + } |
| 116 | + |
| 117 | + /* Update the primary domain name in the title, it could occasionally change */ |
| 118 | + if(is_file($goaccess_conf) && (filesize($goaccess_conf) > 0)) { |
| 119 | + $goaccess_content = file_get_contents($goaccess_conf); |
| 120 | + file_put_contents($goaccess_conf, preg_replace('/^(#)?html-report-title(.*)?/m', "html-report-title $domain", file_get_contents($goaccess_conf))); |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + if(!@is_dir($statsdir)) mkdir($statsdir); |
| 126 | + $username = escapeshellcmd($rec['system_user']); |
| 127 | + $groupname = escapeshellcmd($rec['system_group']); |
| 128 | + $docroot = $rec['document_root']; |
| 129 | + |
| 130 | + $goa_db_dir = $docroot.'/'.$web_folder.'/stats/.db/'; |
| 131 | + $output_html = $docroot.'/'.$web_folder.'/stats/goaindex.html'; |
| 132 | + if(!@is_dir($goa_db_dir)) mkdir($goa_db_dir); |
| 133 | + |
| 134 | + if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log'); |
| 135 | + symlink($logfile, '/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log'); |
| 136 | + |
| 137 | + |
| 138 | + chown($statsdir, $username); |
| 139 | + chgrp($statsdir, $groupname); |
| 140 | + |
| 141 | + $goamonth = date("n"); |
| 142 | + $goayear = date("Y"); |
| 143 | + |
| 144 | + if (date("d") == 1) { |
| 145 | + $goamonth = date("m")-1; |
| 146 | + if (date("m") == 1) { |
| 147 | + $goayear = date("Y")-1; |
| 148 | + $goamonth = "12"; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + if (date("d") == 2) { |
| 154 | + $goamonth = date("m")-1; |
| 155 | + if (date("m") == 1) { |
| 156 | + $goayear = date("Y")-1; |
| 157 | + $goamonth = "12"; |
| 158 | + } |
| 159 | + |
| 160 | + $statsdirold = $statsdir."/".$goayear."-".$goamonth."/"; |
| 161 | + |
| 162 | + if(!is_dir($statsdirold)) { |
| 163 | + mkdir($statsdirold); |
| 164 | + } |
| 165 | + |
| 166 | + rename($goa_db_dir, $statsdirold.'db'); |
| 167 | + mkdir($goa_db_dir); |
| 168 | + |
| 169 | + $files = scandir($statsdir); |
| 170 | + foreach ($files as $file) { |
| 171 | + if (substr($file, 0, 1) != "." && !is_dir("$statsdir"."/"."$file") && substr($file, 0, 1) != "w" && substr($file, 0, 1) != "i") copy("$statsdir"."/"."$file", "$statsdirold"."$file"); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + $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); |
| 176 | + |
| 177 | + if(!is_file($rec['document_root']."/".$web_folder."/stats/index.php")) { |
| 178 | + if(file_exists("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master")) { |
| 179 | + copy("/usr/local/ispconfig/server/conf-custom/goaccess_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php"); |
| 180 | + } else { |
| 181 | + copy("/usr/local/ispconfig/server/conf/goaccess_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php"); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + $app->log('Created GoAccess statistics for ' . $domain, LOGLEVEL_DEBUG); |
| 186 | + |
| 187 | + |
| 188 | + if(is_file($rec['document_root']."/".$web_folder."/stats/index.php")) { |
| 189 | + chown($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_user']); |
| 190 | + chgrp($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_group']); |
| 191 | + } |
| 192 | + |
| 193 | + $app->system->exec_safe('chown -R ?:? ?', $username, $groupname, $statsdir); |
| 194 | + |
| 195 | + } |
| 196 | + } else { |
| 197 | + $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_WARN); |
| 198 | + } |
| 199 | + |
| 200 | + parent::onRunJob(); |
| 201 | + } |
| 202 | + |
| 203 | + /* this function is optional if it contains no custom code */ |
| 204 | + public function onAfterRun() { |
| 205 | + global $app; |
| 206 | + |
| 207 | + parent::onAfterRun(); |
| 208 | + } |
| 209 | + |
| 210 | +} |
| 211 | + |
| 212 | +?> |
0 commit comments