#!/usr/bin/env python#coding: utf-8
importsmtplib, re, sys, osimportxlwtfrom email.mime.multipart importMIMEMultipartfrom email.mime.text importMIMETextfrom email.mime.image importMIMEImagedefchuli_data(filepath):
list1=[]
with open(filepath) as fobj:for line infobj:if re.match(".*\"_source\".*",line):
list1.append([])if re.match(".*\"@timestamp\".*",line):
tmp_value= re.findall(".*\"@timestamp\" *: *\"(.*)\" *",line)
list1[-1].append(tmp_value[0])if re.match(".*\"servicetype\".*",line):
tmp_value= re.findall(".*\"servicetype\" *: *\"(.*)\" *",line)
list1[-1].append(tmp_value[0])if re.match(".*\"source\".*",line):
tmp_value= re.findall(".*\"source\" *: *\"(.*)\" *",line)
list1[-1].append(tmp_value[0])if re.match(".*\"logmsg\".*",line):
tmp_value= re.findall(".*\"logmsg\" *: *\"(.*)\" *",line)
list1[-1].append(tmp_value[0])for ele inlist1:printelereturnlist1deftrans(str1):
upperlist=string.uppercase
str1=int(str1)if str1 < 26:
trans_str=upperlist[str1]else:
par= int(str1/26)
sub= str1 % 26trans_str=upperlist[sub]
trans_str= trans(par) +trans_strreturntrans_strdefrewrite_xml(list1):
wb=xlwt.Workbook()
sheet= wb.add_sheet('error日志')
ws=wb.active
col_num= 1
for row inlist1:
row_num=0for col inrow:
ws[trans(row_num)+str(col_num)] =col
row_num+= 1col_num+= 1wb.save("/tmp/tmp.xlsx")returnTruedefsend_email(config_file,FROM,TO,xlsxfile_list,email_password):
location_name="unknown"SUBJECT="error日志"with open(config_file) as fobj:for line infobj:if re.match(".*",line):
SUBJECT= re.search("(.*)",line).groups()[0]
location_name= re.split("Scarlet",SUBJECT)[0]breaklocation_name= "[%s]" % location_name.decode('utf-8')
HOST= "smtp.ipanel.cn"SUBJECT= SUBJECT.decode('utf-8')
missfile_list=[]
errfile_list=[]
inscribe= """
personal infomation
your own description
"""for file inxlsxfile_list:if notos.path.isfile(file):
missfile_list.append(os.path.basename(file))ifmissfile_list:
mail_head= """All:
附件是今日的errlog,请及时查收
此邮件为系统自动发送,请勿回复
""" % ",".join(missfile_list)else:
mail_head= """
"""msg= MIMEMultipart('related')
n= 1contents= "%s
" %mail_headfor pngfile inpngfile_list:try:
msg.attach(addimg(pngfile,"daily"+str(n)))
contents+= "" %nif n % 2 ==0:
contents+= "
"
exceptException,e:
errfile_list.append(os.path.basename(pngfile))finally:
n+= 1contents+=inscribe
msgtext= MIMEText(contents,"html","utf-8")
msg.attach(msgtext)try:
attach1= MIMEText(open(xlsxfile_list[0],"rb").read(), "base64", "UTF-8")
time_tag= "-".join(re.split("[_.]",xlsxfile_list[0])[-3:-1])
attach1["Content-Type"] = "application/octet-stream"attach1["Content-Disposition"] = "attachment; filename=\"%s error日志 %s.xlsx\"" % (location_name.encode('utf-8'),time_tag)exceptException,e:
errfile_list.append(os.path.basename(xlsxfile_list[0]))
msg.attach(attach1)
msg['Subject'] =SUBJECT
msg['From'] =FROM
msg['To'] = ";".join(TO)try:
server=smtplib.SMTP()
server.connect(HOST,"25")
server.login("Scarlet@zzz.cn",email_password)
server.sendmail(FROM, TO, msg.as_string())
server.quit()print "邮件发送成功!"
returnTrueexceptException, e:print "失败:"+str(e)returnFalseif __name__ == '__main__':
config_file= "/opt/Scarlet/elkerrlog/monitor_elk.conf"FROM= "Scarlet@zzz.cn"TO= ["Scarlet@zzz.cn"]
FILELOG= "logfile.`date +%Y%m%d`"xlsxfile_list= [r"/opt/Scarlet/elkerrlog/$FILELOG"]
email_password= "Scarlet_password"list1=chuli_data(xlsxfile_list[0])
rewrite_xml(list1)
tmp_result= send_email(config_file,FROM,TO,/tmp/tmp.xlsx,email_password)if nottmp_result:
sys.exit(1)
sys.exit(0)