Skip to content

Commit 3729a0c

Browse files
author
Till Brehm
committed
Implemented #5246 Mail traffic update - handle different syslog timestamps when parsing logs
1 parent 19513ef commit 3729a0c

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

server/lib/classes/cron.d/100-mailbox_stats.inc.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,6 @@ public function onRunJob() {
107107
$mail_boxes = array();
108108
$mail_rewrites = array(); // we need to read all mail aliases and forwards because the address in amavis is not always the mailbox address
109109

110-
function parse_mail_log_line($line) {
111-
//Oct 31 17:35:48 mx01 amavis[32014]: (32014-05) Passed CLEAN, [IPv6:xxxxx] [IPv6:xxxxx] <xxx@yyyy> -> <aaaa@bbbb>, Message-ID: <xxxx@yyyyy>, mail_id: xxxxxx, Hits: -1.89, size: 1591, queued_as: xxxxxxx, 946 ms
112-
113-
if(preg_match('/^(\w+\s+\d+\s+\d+:\d+:\d+)\s+[^ ]+\s+amavis.* <([^>]+)>\s+->\s+((<[^>]+>,)+) .*Message-ID:\s+<([^>]+)>.* size:\s+(\d+),.*$/', $line, $matches) == false) return false;
114-
115-
$timestamp = strtotime($matches[1]);
116-
if(!$timestamp) return false;
117-
118-
$to = array();
119-
$recipients = explode(',', $matches[3]);
120-
foreach($recipients as $recipient) {
121-
$recipient = substr($recipient, 1, -1);
122-
if(!$recipient || $recipient == $matches[2]) continue;
123-
$to[] = $recipient;
124-
}
125-
126-
return array('line' => $line, 'timestamp' => $timestamp, 'size' => $matches[6], 'from' => $matches[2], 'to' => $to, 'message-id' => $matches[5]);
127-
}
128-
129110
function add_mailbox_traffic(&$traffic_array, $address, $traffic,$mail_boxes, $mail_rewrites) {
130111
//global $mail_boxes, $mail_rewrites;
131112
//echo '##'.print_r($mail_boxes).'##';
@@ -275,9 +256,26 @@ public function onAfterRun() {
275256
private function parse_mail_log_line($line) {
276257
//Oct 31 17:35:48 mx01 amavis[32014]: (32014-05) Passed CLEAN, [IPv6:xxxxx] [IPv6:xxxxx] <xxx@yyyy> -> <aaaa@bbbb>, Message-ID: <xxxx@yyyyy>, mail_id: xxxxxx, Hits: -1.89, size: 1591, queued_as: xxxxxxx, 946 ms
277258

278-
if(preg_match('/^(\w+\s+\d+\s+\d+:\d+:\d+)\s+[^ ]+\s+amavis.* <([^>]+)>\s+->\s+((<[^>]+>,)+) .*Message-ID:\s+<([^>]+)>.* size:\s+(\d+),.*$/', $line, $matches) == false) return false;
259+
// Oct 31 17:35:48
260+
$timestampSyslog = '\w+\s+\d+\s+\d+:\d+:\d+';
261+
$timePatternRsyslog = 'Y-m-d\TH:i:sT';
262+
// 2019-02-12T18:17:19+01:00
263+
$timestampRsyslog = '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[\+-]\d{2}:\d{2}';
264+
// 2019-02-12T18:17:19.203313+01:00
265+
$timestampHighPrecision = '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+[\+-]\d{2}:\d{2}';
266+
$timePatternHighPrecision = 'Y-m-d\TH:i:s.uT';
267+
268+
$timestampAll = $timestampSyslog . '|' . $timestampRsyslog . '|' . $timestampHighPrecision;
269+
270+
if(preg_match('/^('. $timestampAll .')\s+[^ ]+\s+amavis.* <([^>]+)>\s+->\s+((<[^>]+>,)+) .*Message-ID:\s+<([^>]+)>.* size:\s+(\d+),.*$/', $line, $matches) == false) return false;
279271

280272
$timestamp = strtotime($matches[1]);
273+
if(!$timestamp) {
274+
$timestamp = DateTime::createFromFormat($timePatternRsyslog, $matches[1]);
275+
}
276+
if(!$timestamp) {
277+
$timestamp = DateTime::createFromFormat($timePatternHighPrecision, $matches[1]);
278+
}
281279
if(!$timestamp) return false;
282280

283281
$to = array();

0 commit comments

Comments
 (0)