python 检查目录零字节文件并发送邮件

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 3753

2016/04/11 01:29


#encoding=gb2312
'''
Created on 2016年4月11日

@author: wangzi
'''

import smtplib
from email.mime.text import MIMEText
import time
import re

mailto_list = ['123@qq.com','234@qq.com']
mail_host = 'mail.server.com'
mail_user = '123'
mail_pass = '123'
mail_postfix='qq.com'
def send_mail(to_list,sub,content):
    me = '王梓'+'<'+mail_user+'@'+mail_postfix+'>'
    msg = MIMEText(content,_subtype = 'html',_charset='gb2312')
    msg['Subject'] = sub
    msg['From'] = me

    msg['to'] = ';'.join(to_list)

    try:
        server = smtplib.SMTP()
        server.connect(mail_host)
        server.login(mail_user,mail_pass)
        server.sendmail(me,to_list,msg.as_string())
        server.close()
        return True
    except Exception as e:
        print (e)
        return False
import os
if __name__ == '__main__':
    warning = '基地,同事好:<br> <DD>  如下日志0字节,请检查谢谢。</DD><br>'
    warning_init = warning
    path = ('/logcenter02/data/backup_data/base/base_book/')
    for path,dir,files in os.walk(path):
        matchfile = filter(lambda filterfile:not re.match('a_b-updated.*|i_b-statistics.*|a_b-author.*|a_b-catalog.*|a_b-package.*|a_b-book.*|bookorder.*|catalogorder.*|i_b-rankmonth.*|i_b-ranktotal.*|i_b-rankweek.*',filterfile),files)
        sorted_matchfile = sorted(matchfile)
        for file in sorted_matchfile:
            check_file = os.path.join(path,file)
            if  os.stat(check_file).st_size == 0:
                warning += '<span style="color:#E53333;">%s</span> 大小为 <span style="color:#E53333;">%s</span>,请检查!<br>'% (file,os.stat(check_file).st_size)
    check_time = time.strftime('%Y%m%d',time.localtime(time.time()))
    if warning != warning_init:
        if send_mail(mailto_list, '%s 阅读基地0字节日志统计。' % (check_time), warning):
            print('%s 基地发送成功' % (check_time) )
        else:
            print('%s 基地发送失败'  % (check_time))
    else:
        print("%s 基地无0字节日志" % (check_time))

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1