Created
October 21, 2019 06:38
-
-
Save tohokuaiki/e6df1c2d68c208a1bc8226eeb1bd42ce to your computer and use it in GitHub Desktop.
postfixのメールログを解析したくなった時にちょろっとPHPで
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$lines = file('mail.log'); | |
$logs = []; | |
$mails = []; | |
foreach ($lines as $k => $line){ | |
$line = trim($line); | |
$cell = explode(" ", $line); | |
$status = $to = $from = ''; | |
$size = 0; | |
if (preg_match('/status=(.*?)\s+/', $line, $m)){ | |
$status = $m[1]; | |
} | |
if (preg_match('/ from=<(.*?)>/', $line, $m)){ | |
$from = $m[1]; | |
if (strpos($from, '@') === false){ | |
$from .= '@anytimefitness.co.jp'; | |
} | |
} | |
if (preg_match('/ to=<(.*?)>/', $line, $m)){ | |
$to = $m[1]; | |
if (strpos($to, '@') === false){ | |
$to .= '@anytimefitness.co.jp'; | |
} | |
} | |
if (preg_match('/, size=(\d+)/', $line, $m)){ | |
$size = $m[1]; | |
} | |
$date = strtotime(substr($line, 0, 15)); | |
$id = $cell[5]; | |
if ($id === 'statistics:') continue; | |
if (!isset($logs[$id])){ | |
$logs[$id] = [ | |
'lines' => [], | |
'size' => [], | |
'status' => [], | |
'to' => [], | |
'from' => [], | |
'date' => [], | |
]; | |
} | |
$logs[$id]['lines'][] = $line; | |
if ($status) $logs[$id]['status'][] = $status; | |
if ($to) $logs[$id]['to'][] = $to; | |
if ($from) $logs[$id]['from'][] = $from; | |
if ($date) $logs[$id]['date'][] = $date; | |
if ($size) $logs[$id]['size'][] = $size; | |
} | |
// 整形 | |
foreach ($logs as &$log){ | |
$log['size'] = array_unique($log['size']); | |
$log['to'] = array_unique($log['to']); | |
$log['from'] = array_unique($log['from']); | |
$date_avg = floor(array_sum($log['date']) / count($log['date'])); | |
$log['date'] = array_unique(array_map(function($v) use ($date_avg) { | |
return abs($date_avg - $v) < 5 ? $date_avg : $v; | |
}, $log['date'])); | |
$log['status'] = array_unique($log['status']); | |
} | |
// 各プロパティ出力 | |
foreach (['to', 'from', 'size', 'status', 'date'] as $prop){ | |
echo "\n", $prop,"\n"; | |
foreach ($logs as $id=>$log){ | |
if (count($log[$prop]) > 1){ | |
printf("%s\t%s\n", $id, implode("\t", $log[$prop])); | |
} | |
} | |
} | |
echo "\n\n"; | |
// 送信成功のログ | |
foreach ($logs as $id=>$log){ | |
if (count($log['status']) === 1){ | |
if ($log['status'][0] !== 'sent'){ | |
printf("%s\t%s\t%s\t%s\n", | |
$id, | |
$log['status'][0], | |
date('Y-m-d H:i:s', min($log['date'])), | |
implode("\t", $log['to'])); | |
} | |
} | |
} | |
echo "\n\n"; | |
// deferredなメールを追跡して表示 | |
foreach ($logs as $id=>$log) { | |
if (count($log['status']) == 2){ | |
// 同時期に送られたメールを探す | |
$sent_date = min($log['date']); | |
$sent_with = []; | |
foreach ($logs as $_log) { | |
if ($_log['status'] && $_log['status'][0] === 'sent'){ | |
if (abs(min($_log['date']) - $sent_date) < 5){ | |
$sent_with[] = $_log['to'][0]; | |
} | |
} | |
} | |
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n", | |
$log['to'][0], | |
$id, | |
date('Y-m-d H:i:s', min($log['date'])), | |
$log['status'][0], | |
date('Y-m-d H:i:s', max($log['date'])), | |
next($log['status']), | |
implode("\t", $sent_with) | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment