拿Python写了一个备份结果监控程序
1、背景说明
前段时间为公司制定了一套备份解决方案
需要每天被告知备份是否OK
2、流程
3、代码
#!/usr/bin/env python # -*- coding: utf-8 -*- import smtplib import email.mime.multipart import email.mime.text import time import subprocess def mailalter(user,receivers,subject,servername,status): msg=email.mime.multipart.MIMEMultipart() msg['from']='[email protected]' msg['to']= user msg['subject']= subject content = ''' Dear ITers: %s bakcup status %s ##Powered by voidyao000## ##Life is short,you need python!## '''%(servername,status) txt=email.mime.text.MIMEText(content) msg.attach(txt) smtp=smtplib smtp=smtplib.SMTP() smtp.connect('*.*.*.*','25') #注意点:这里的receivers是列表字符串 smtp.sendmail('[email protected]',receivers,str(msg)) smtp.quit() def chkbak(servername): user = '[email protected],[email protected]' receivers = ['[email protected]','[email protected]',] subjectok = '%s %sbackup OK'%(time.strftime("%Y-%m-%d"),servername) subjectfail = '%s %sbackup fail'%(time.strftime("%Y-%m-%d"),servername) bakdate = time.strftime("%y%m%d") logfile = '/home/%sbackup/%sbak%s.log' %(servername,servername,bakdate) cmd = 'tail -1 %s' %(logfile) cmd_call = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE) cmd_result = str(cmd_call.stdout.read(),'utf8').strip() chk_line = '%s backup sucessful'%(servername) if chk_line ==cmd_result: status = 'ok' print('backup ok') mailalter(user,receivers,subjectok,servername,"OK") else: status = 'fail' print("backup fail") mailalter(user,receivers,subjectfail,servername,"Fail") chkbak("servername")
转载于:https://blog.51cto.com/voidyao000/1920521