|
| 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