#!/usr/bin/python
import MySQLdb
import re
import memcache
import binascii
import os
import time
def crcHash(hash):
crc32_value = (binascii.crc32(hash) & 0xffffffff)%1001
return int(crc32_value)
def new_mail_mysql(ak):
conn=MySQLdb.connect(host='m3306.sae.sina.com.cn',user='sae_ol',passwd='7b149edb22ae7526',db='sae',port=3306)
cur=conn.cursor()
sql="select name from app where accesskey='%s'"%ak
cur.execute(sql)
app_name=cur.fetchall()
return app_name
cur.close()
conn.close()
def buffer_line():
buf = open("/etc/sae/buffer").read()
if not buf:
return 0
else:
return int(re.findall("^\d*", buf)[0])
def set_last_pos(pos):
open("/etc/sae/buffer", "w").write(str(pos))
def new_mail_mc_get(ak):
mc=memcache.Client(['10.67.15.223:7601'],debug=True)
ak_value=mc.get(ak)
return ak_value
def new_mail_mc_set(ak,ak_value):
mc=memcache.Client(['10.67.15.223:7601'],debug=True)
mc.set(ak,ak_value)
def mail_file(appname,dir,time,from_1,to_1,status,msg):
print dir
print status
if status == 'K':
file_path=dir+'/'+appname+'-access_log'
file_open=open(file_path,'a')
file_open.write("%s from:%s to:%s status:200 msg:%s\n"%(time,from_1,to_1,msg))
file_open.close()
else:
file_path=dir+'/'+appname+'-error_log'
file_open=open(file_path,'a')
file_open.write("%s from:%s to:%s status:500 msg:%s\n"%(time,from_1,to_1,status,msg))
file_open.close()
if __name__ == '__main__':
now_day=str(time.strftime("%Y-%m-%d",time.localtime()))
print now_day
fh=open("/data1/saelog/%s/newmail/sae.sina.com.cn-access.log"%now_day)
fh.seek(buffer_line())
content=fh.read()
new_total_lines=len(content)+buffer_line()
set_last_pos(new_total_lines)
new_lines=content.split("\n")
for i in new_lines:
a=re.findall(r'^\[INFO\].*sender_uid:\[52\]',i,re.M)
if a:
ak_name_m=re.match(r'^\[INFO\].*time:\[(\d.*)\].*MID.*sender:\[(\w.*)\].*project_id:.*from:\[(\w.*)\].*to:\[(\w.*)\].*head_from.*status:\[(\w)\].*msg:\[(\w
.*)\].*subject.*',i)
time=ak_name_m.group(1)
ak_name=ak_name_m.group(2)
from_1=ak_name_m.group(3)
to_1=ak_name_m.group(4)
status=ak_name_m.group(5)
msg=ak_name_m.group(6)
ak_mc=new_mail_mc_get(str(ak_name))
print ak_mc
if ak_mc:
app_name=ak_mc.split("-")[1]
dir_hash=ak_mc.split("-")[0]
print dir_hash
dir_1='/data1/saelog/'+now_day+'/mail/'+str(dir_hash)
dir_2='/data1/saelog/'+now_day+'/mail/'+str(dir_hash)+'/'+str(app_name)
print dir_2
if os.path.isdir(dir_2):
os.system('chmod -R 777 %s'%dir_1)
mail_file(app_name,dir_2,time,from_1,to_1,status,msg)
else:
try:
os.mkdir(dir_1)
os.mkdir(dir_2)
os.system('chmod -R 777 %s'%dir_1)
mail_file(app_name,dir_2,time,from_1,to_1,status,msg)
except OSError, error:
os.mkdir(dir_2)
mail_file(app_name,dir_2,time,from_1,to_1,status,msg)
else:
ak_mysql=new_mail_mysql(ak_name)[0][0]
hash_id=crcHash(ak_mysql)
ak_value=str(hash_id)+'-'+str(ak_mysql)
new_mail_mc_set(str(ak_name),str(ak_value))
c=new_mail_mc_get(str(ak_name))
dir_hash=c.split("-")[0]
app_name=c.split("-")[1]
dir_1='/data1/saelog/'+now_day+'/mail/'+str(dir_hash)
dir_2='/data1/saelog/'+now_day+'/mail/'+str(dir_hash)+'/'+str(app_name)
if os.path.isdir(dir_2):
mail_file(app_name,dir_2,time,from_1,to_1,status,msg)
else:
try:
os.mkdir(dir_1)
except OSError, error:
os.mkdir(dir_2)
mail_file(app_name,dir_2,time,from_1,to_1,status,msg)
print 'mysql'
print hash_id