#!/usr/bin/env python-- coding: utf-8 --
import re
import threading
import MySQLdb
import datetime,time
def Gainid(log):
#mail id re
mailid = re.compile(r'\w{11}\:')
list_id = []
for line in log:
m = mailid.search(line)
if m is not None:
list_id.append(m.group().strip(':'))
else:
continue
return list(set(list_id))
def HandleLog(log,mailid):
tomail = ''
fmail = ''
status = 'other'
sdict={'status\=sent':'ok',
'Sender address rejected':'Sender address rejected',
'Recipient address rejected':'Recipient address rejected',
'said\: 550 [Mm]ailbox':'Mailbox not found',
'said\: 550 Domain frequency limited':'Domainfrequenc limited',
'said\: 55[3-4]':'junk mail',
'[uU]ser not exist':'user not exist',
'said\: 550 User suspended':'User suspended',
'said\: 550 User not found':'User not found',
'Message rejected as spam by Content Filterin':'Message rejected as spam by Content Filterin',
'said\: 550 No such user':'user not exist',
'[Uu]ser unknown':'user unknown'}
for line in log:
m = re.search(mailid,line)
if m is not None:
mailre = re.compile(r'[^\._-][\w\.-]+@(?:[A-Za-z0-9]+\.)+[A-Za-z]+')
timere = re.compile(r'^\w+\s+\d+\s\d{2}\:\d{2}:\d{2}')
nt = timere.search(line)
mtime = nt.group()
n = mailre.search(line)
if n is not None:
f = re.search(r'from\=',line)
t = re.search(r'to\=',line)
if f is not None:
fmail = n.group().strip('<')
if t is not None:
tmail = n.group().strip('<')
for key in sdict:
statre = re.search(key,line)
if statre is not None:
status = sdict [key]
else:
pass
print ("id: %s date :%s |frdr: %s | todr: %s | status: %s" % (mailid,mtime ,fmail,tmail,status))
ttime = fromttime(mtime)
sql = "insert into mailtest values('%s','%s','%s','%s','%s')" % (mailid,ttime ,fmail,tmail,status)
insertmail(sql)
def main(logfile):
#open file
f = open(logfile,'r')
log = f.readlines()
f.close()
#mail id
mailid = Gainid(log)
#HandleLog
for i in range(len(mailid)):