#!/bin/bash # ログファイルのパスを指定 LOGFILE="/tmp/sample_maillog.txt" # 出力ファイル名を生成する関数(ファイル名に日付を付ける) generate_output_filename() { local current_date=$(date +%Y%m%d) echo "bounce_report_${current_date}.txt" } # バウンスログを集計してファイルに保存する関数 collect_bounce_logs() { local logfile=$1 local output_file=$2 # バウンスログを抽出して集計 grep "status=bounced" $logfile | awk ' { # メールアドレスとバウンス理由を抽出 if ($0 ~ /to=<[^>]+>/) { match($0, /to=<[^>]+>/); email = substr($0, RSTART+4, RLENGTH-5); } if ($0 ~ /status=bounced.*\((.*)\)/) { match($0, /status=bounced.*\((.*)\)/, arr); reason = arr[1]; } if (email && reason) { # バウンスメールアドレスと理由をキーにカウント bounce[email "|" reason]++; email = ""; reason = ""; } } END { # 一時ファイルに出力する for (key in bounce) { split(key, arr, "|"); printf "%-10s | %-30s | %s\n", bounce[key], arr[1], arr[2]; } }' | sort -nr > $output_file # 回数でソートして出力 } # メイン処理 main() { # 出力ファイル名を生成 output_file=$(generate_output_filename) # バウンスログを集計 collect_bounce_logs $LOGFILE $output_file # 完了メッセージ echo "バウンスログの集計が完了しました。出力ファイル: $output_file" } # スクリプトの実行 main
こんな感じで出力
54 | user2@example.com | host mail.example.com said: 550 5.1.1 <user2@example.com>: user unknown 51 | user4@example.com | host mail.example.com said: 550 5.1.1 <user4@example.com>: spam detected 49 | user4@example.com | host mail.example.com said: 550 5.1.1 <user4@example.com>: connection timed out 44 | user5@example.com | host mail.example.com said: 550 5.1.1 <user5@example.com>: user unknown 44 | user5@example.com | host mail.example.com said: 550 5.1.1 <user5@example.com>: host not found 44 | user4@example.com | host mail.example.com said: 550 5.1.1 <user4@example.com>: mailbox full 44 | user1@example.com | host mail.example.com said: 550 5.1.1 <user1@example.com>: mailbox full 43 | user3@example.com | host mail.example.com said: 550 5.1.1 <user3@example.com>: spam detected 42 | user3@example.com | host mail.example.com said: 550 5.1.1 <user3@example.com>: user unknown 42 | user1@example.com | host mail.example.com said: 550 5.1.1 <user1@example.com>: user unknown 41 | user4@example.com | host mail.example.com said: 550 5.1.1 <user4@example.com>: user unknown 41 | user2@example.com | host mail.example.com said: 550 5.1.1 <user2@example.com>: spam detected 40 | user2@example.com | host mail.example.com said: 550 5.1.1 <user2@example.com>: host not found 40 | user1@example.com | host mail.example.com said: 550 5.1.1 <user1@example.com>: connection timed out 39 | user5@example.com | host mail.example.com said: 550 5.1.1 <user5@example.com>: mailbox full 39 | user5@example.com | host mail.example.com said: 550 5.1.1 <user5@example.com>: connection timed out 38 | user1@example.com | host mail.example.com said: 550 5.1.1 <user1@example.com>: spam detected 37 | user3@example.com | host mail.example.com said: 550 5.1.1 <user3@example.com>: connection timed out 35 | user5@example.com | host mail.example.com said: 550 5.1.1 <user5@example.com>: spam detected 35 | user4@example.com | host mail.example.com said: 550 5.1.1 <user4@example.com>: host not found 35 | user2@example.com | host mail.example.com said: 550 5.1.1 <user2@example.com>: mailbox full 33 | user3@example.com | host mail.example.com said: 550 5.1.1 <user3@example.com>: host not found 31 | user3@example.com | host mail.example.com said: 550 5.1.1 <user3@example.com>: mailbox full 31 | user1@example.com | host mail.example.com said: 550 5.1.1 <user1@example.com>: host not found 28 | user2@example.com | host mail.example.com said: 550 5.1.1 <user2@example.com>: connection timed out