根据指定规则检查日志,指定规则检查日志,#! /usr/loca


#! /usr/local/bin/python3import enviroment as eoimport datetimeimport redef check_logs(logpath):    date_format='%Y-%m-%d'    now_format=datetime.datetime.today().strftime(date_format)    re_begin=re.compile('start time is:'+now_format)    re_end=re.compile('end time is:'+now_format)    examine_tag=0    with open(logpath,'rb') as file_name:        line=file_name.readline()        while line:            line=line.decode('gb2312')            if re_begin.search(line):                examine_tag=1                pos=file_name.tell()                line=file_name.readline()                continue            if examine_tag==1:                if re_end.search(line):                    return                else:                    break            line=file_name.readline()        if pos:            file_name.seek(pos)            warn_content=file_name.read()            eo.send_mail_X('',warn_content)if __name__=='__main__' :    logpath=''    check_logs(logpath)enviroment.py中包括以下几个相关函数。def send_mail_X(mailname,mailcontent):    receivers=[]    gen_mail(receivers,mailname,mailcontent)def gen_mail(receivers,mailtag,mailname,mailcontent):    data_tag=datetime.datetime.today().strftime('%Y-%m-%d')    with tempfile.NamedTemporaryFile(prefix=mailtag+'.',suffix='.mail') as temp_file:        temp_file.write(('Subject: '+mailtag+''+mailname+' '+data_tag+'\\n').encode())        temp_file.write(('From:'+mailtag+'<邮件地址>'+'\\n').encode())        for receiver in receivers:            temp_file.write(('Cc:'+receiver+'\\n').encode())        temp_file.write(mailcontent)        temp_file.seek(0)        upload_file_fromtemp(temp_file)def upload_file_fromtemp(temp_file):    ftp=FTP()    ftp.connect('',21)    ftp.login('','')    filebasename=os.path.basename(temp_file.name)    ftmcmd='STOR '+filebasename    ftp.storbinary(ftmcmd,temp_file)#该片段来自于http://byrx.net

评论关闭