postfix log簡単整理

二つのファイルを処理する,一つは発送成功のケースです。もう一つは発送失敗場合、エラーログを書きこむ。

#!/bin/bash

#——————————————————-
#TITLE: Postfix Mail Log Handle
#AUTHOR: HankZhou
#TIME: 2013/07/17
#VERSION: 0.5V
#——————————————————-

#To determine whether a file exists
if [ ! -e $1 ]
then
printf “$1 No such file or directory\n\r"
exit 0
fi

dirName="maillog"$(date +%y%m%d%H%M%S)

#To determine whether a directory exists
if [ -d $dirName ]
then
printf “Folder already exists\n\r"
exit 0
fi

mkdir $dirName

#Sented File handling
cat $1 | \
awk '{if(match($12,/^status=sent$/))
{mat=match($7,/[a-zA-Z0-9\_]+\@[a-zA-Z0-9\_\.]/);
lenMatch=length($7);
matched=substr($7,5,lenMatch-6);
timeOri=$1″ “$2" “$3;
cmd="date \""timeOri"\" \"+%Y/%m/%d %T \"";
cmd|getline time;
lenTime=length(time);
timeH=substr(time,0,lenTime-1);
print timeH","matched “,Message accepted"}}’ | \
awk 'BEGIN{FS=OFS=","}
{a[$2]++;a[$0]++;
if(a[$2]<2) {print $0}}' \ >./$dirName/sented.csv

#calculate number of lines
lines=$(awk '{b++;}END{print b}’ ./$dirName/sented.csv)
printf “**\n\rSented mail has been generated.\n\r"
printf “Totle of $lines lines\n\r"

#Error File handling
cat $1 | \
awk '{if(match($12,/^status=(deferred|bounced|expired)$/))
{mat=match($7,/[a-zA-Z0-9\_]+\@[a-zA-Z0-9\_\.]/);
lenMatch=length($7);
matched=substr($7,5,lenMatch-6);
timeOri=$1″ “$2" “$3;
cmd="date -d \""timeOri"\" \"+%Y/%m/%d %T \"";
cmd|getline time;
lenTime=length(time);
timeH=substr(time,0,lenTime-1);
printf timeH","matched",";
printf substr($12,8);
for (i=13;i<=NF;i++){printf $i" "}; print "\n";}}' | \ awk 'BEGIN{FS=OFS=","} {a[$2]++;a[$0]++; if(a[$2]<2) {print $0}}' \ >./$dirName/ErrorMail.csv

lines=$(awk '{b++;}END{print b}’ ./$dirName/ErrorMail.csv)
printf “**\n\rError mail has been generated.\n\r"
printf “Totle of $lines lines\n\r"

Source

Posted by arkgame