# This Python file uses the following encoding: utf-8
import os
import time
import linecache
import smtplib
from email.MIMEText import MIMEText
mailto_list=["luxiaobo@kedacom.com","libo@kedacom.com","zhouqingbo@kedacom.com"]
mail_host="smtp.163.com"
mail_user="lurespo3@163.com"
mail_pass="******"
mail_postfix="163.com"
#邮件发送
def send_mail(to_list,sub,content):
me=mail_user+""
msg = MIMEText(content)
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
msg["Accept-Language"]="zh-CN"
msg["Accept-Charset"]="ISO-8859-1,utf-8"
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
def mailet():
file=os.popen("ls -lt /usr/local/lxb/log/collapselog| head -n2|awk '{print $9}'").read()
files=file.strip().split('\n')
filename=files[0]
nums=len(file)
for i in range(nums):
if file[i]=='e':
break
ipaddr=file[:i]
count = 0
thefile = open('/usr/local/lxb/log/collapselog/%s' %filename, 'rb')
while True:
buffer = thefile.read(8192*1024)
if not buffer:
break
count += buffer.count('\n')
linecache.clearcache()
thefile.close( )
content=linecache.getline('/usr/local/lxb/log/collapselog/%s' %filename,count)
return (ipaddr,content)
linecache.clearcache()
while True:
status=0
firstdate=os.popen("ls -l /usr/local/lxb/log/collapselog|grep log|awk '{print $7}'").read()
fdate=firstdate.strip().split('\n')
fnums=len(fdate)
firsttimes=os.popen("ls -l /usr/local/lxb/log/collapselog|grep log|awk '{print $8}'").read()
ftime=firsttimes.strip().split('\n')
# print '时间1:%s' %ftime
time.sleep(61)
seconddate=os.popen("ls -l /usr/local/lxb/log/collapselog|grep log|awk '{print $7}'").read()
sdate=seconddate.strip().split('\n')
snums=len(sdate)
secondtimes=os.popen("ls -l /usr/local/lxb/log/collapselog|grep log|awk '{print $8}'").read()
stime=secondtimes.strip().split('\n')
# print '时间2:%s' %stime
if snums>fnums:
# print '数量有变'
status=1
maildetail=mailet()
# opencpu = open('/usr/local/lxb/test.log','a')
# opencpu.writelines('%s\t%s' %(maildetail[0],maildetail[1]))
# opencpu.close()
send_mail(mailto_list,"Collapse alarm","Collapse ipaddr:%s\n Collapse detail:%s\nCheck the server logs.\nserver:172.16.185.109\nPath:/usr/local/lxb/log/collapselog" %(maildetail[0],maildetail[1]))
elif fnums>0:
for i in range(fnums):
if fdate[i]!=sdate[i] and status==0:
# print '日期有变'
status=1
maildetail=mailet()
send_mail(mailto_list,"Collapse alarm","Collapse ipaddr:%s\n Collapse detail:%s\nCheck the server logs.\nserver:172.16.185.109\nPath:/usr/local/lxb/log/collapselog" %(maildetail[0],maildetail[1]))
elif ftime[i]!=stime[i] and status==0:
# print '时间有变'
status=1
maildetail=mailet()
send_mail(mailto_list,"Collapse alarm","Collapse ipaddr:%s\n Collapse detail:%s\nCheck the server logs.\nserver:172.16.185.109\nPath:/usr/local/lxb/log/collapselog" %(maildetail[0],maildetail[1]))