python检查文件更新_python检查日志更新,有更新发送邮件

# 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]))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值