diff --git a/server/lib/classes/cron.d/100-mailbox_stats_hourly.inc.php b/server/lib/classes/cron.d/100-mailbox_stats_hourly.inc.php new file mode 100644 index 0000000000000000000000000000000000000000..4ad612f601341b0426711c512b044ac879fe2efe --- /dev/null +++ b/server/lib/classes/cron.d/100-mailbox_stats_hourly.inc.php @@ -0,0 +1,113 @@ +db->queryAllRecords($sql, $conf['server_id']); + if(count($records) > 0) { + $this->update_last_mail_login(); + } + + parent::onRunJob(); + } + + /* this function is optional if it contains no custom code */ + public function onAfterRun() { + global $app; + + parent::onAfterRun(); + } + + // Parse the dovecot/postfix logs to update the last login time for mail_user's. + private function update_last_mail_login() { + global $app; + + // used for all monitor cronjobs + $app->load('monitor_tools'); + $this->_tools = new monitor_tools(); + + // Get the data of the log + $log_lines = $this->_tools->_getLogData('log_mail', 10000000); + + $updatedUsers = []; + + // Loop over all lines. + $line = strtok($log_lines, PHP_EOL); + while ($line !== FALSE) { + $matches = []; + // Match pop3/imap logings, or alternately smtp logins. + if (preg_match('/(.*) (imap|pop3)-login: Login: user=\<([\w\.@-]+)\>/', $line, $matches) || preg_match('/(.*) sasl_method=PLAIN, sasl_username=([\w\.@-]+)/', $line, $matches)) { + $user = $matches[3] ?? $matches[2]; + $updatedUsers[] = $user; + } + + // get the next line + $line = strtok(PHP_EOL); + } + + $uniqueUsers = array_unique($updatedUsers); + + $app->log('Updating last_access stats for ' . count($uniqueUsers) . ' mail users', LOGLEVEL_DEBUG); + + // Date/time rounded to hours. + $now = time() - (time() % (60 * 60 * 24)); + $nowFormatted = date('Y-m-d H:i:s', $now); + $sqlStatement = "UPDATE mail_user SET last_access=? WHERE email=?"; + + // Save to master db. + foreach ($uniqueUsers as $user) { + $ret = $app->dbmaster->query($sqlStatement, $now, $user); + } + } +} diff --git a/server/lib/classes/monitor_tools.inc.php b/server/lib/classes/monitor_tools.inc.php index 86fe3e672dbeb1c7638aa33b8aa6f38ba9710ff2..c6128fc7b502addb8f5a0c93b00155e58ed57841 100644 --- a/server/lib/classes/monitor_tools.inc.php +++ b/server/lib/classes/monitor_tools.inc.php @@ -677,14 +677,14 @@ class monitor_tools { $log = 'Logfile path error.'; } else { if (is_readable($logfile)) { - $log = $this->_getOutputFromExecCommand('tail -n '.intval($max_lines).' ' . escapeshellarg($logfile)); + $log = $this->_getOutputFromExecCommand('tail -n '.intval($max_lines).' ' . escapeshellarg($logfile), $max_lines); } else { $log = 'Unable to read ' . $logfile; } } } else { if($journalmatch != ''){ - $log = $this->_getOutputFromExecCommand('journalctl -n '.intval($max_lines).' --no-pager ' . escapeshellcmd($journalmatch)); + $log = $this->_getOutputFromExecCommand('journalctl -n '.intval($max_lines).' --no-pager ' . escapeshellcmd($journalmatch), $max_lines); }else{ $log = 'Unable to read logfile'; } @@ -694,7 +694,7 @@ class monitor_tools { return $log; } - private function _getOutputFromExecCommand ($command) { + private function _getOutputFromExecCommand ($command, $max4k = 1000) { $log = ''; $fd = popen($command, 'r'); if ($fd) { @@ -702,7 +702,7 @@ class monitor_tools { while (!feof($fd)) { $log .= fgets($fd, 4096); $n++; - if ($n > 1000) + if ($n > $max4k) break; } fclose($fd);