python2监控多源复制状态并发邮件

python2监控多源复制状态并发邮件,第1张

概述我们环境中用到了多源复制,因此写个查看状态的Python脚本,python为系统中自带的2.6.6,邮件内容为html格式,隔行变色,脚本如下: # -*- coding: UTF-8 -*-import smtplibfrom email.mime.text import MIMETextfrom email.header import Headerimport datetimeimp 我们环境中用到了多源复制,因此写个查看状态的Python脚本,python为系统中自带的2.6.6,邮件内容为HTML格式,隔行变色,脚本如下:

# -*- Coding: UTF-8 -*-import smtplibfrom email.mime.text import MIMETextfrom email.header import headerimport datetimeimport MysqLdbdate_end = datetime.date.today()HTML_part1 = """<HTML>  <head></head>  <body><h2>Report of 10.10.100.10 multi source repl status {current_time}</h2>    <table border=\"1\" bordercolor=\"#000000\" wIDth=\"350\"  style=\"wIDth:85%;border-collapse: collapse\" >        <tbody>            <tr  bgcolor=#0066CC>                <th>Master_Host</th>                <th>Slave_IO_Running</th>                <th>Slave_sql_Running</th>                <th>Seconds_Behind_Master</th>                <th>Channel_name</th>            </tr>""".format(current_time=date_end)HTML_part2 = """  </body></HTML>"""td_bgcolor_num = 1db = MysqLdb.connect("10.10.100.10","MysqLdba","MysqL-dba-168" )cursor = db.cursor()cursor.execute("show slave status")results = cursor.fetchall()with open(‘/tmp/slavesof10010.HTML‘,mode=‘w‘) as f:    f.write(HTML_part1)    for row in results:        Master_Host = row[1]        Slave_IO_Running = row[10]        Slave_sql_Running = row[11]        Seconds_Behind_Master = row[32]        Channel_name = row[-2]        if td_bgcolor_num%2==0:            td_bgcolor=‘#F0F0F0‘        else:            td_bgcolor=‘#FFFFCE‘        td_bgcolor_num += 1        pro = ‘‘‘        <tr bgcolor="‘‘‘+td_bgcolor+‘‘‘" align=\"center\" >            <td >‘‘‘+ row[1].encode(‘utf-8‘,‘ignore‘) + "</td>" + ‘‘‘            <td >‘‘‘+ row[10].encode(‘utf-8‘,‘ignore‘) + "</td>" + ‘‘‘            <td >‘‘‘+ row[11].encode(‘utf-8‘,‘ignore‘) + "</td>" + ‘‘‘            <td >‘‘‘+ str(row[32]) + "</td>" + ‘‘‘            <td >‘‘‘+ Channel_name + ‘‘‘</td>        </tr>        ‘‘‘        f.write(pro)    f.write(HTML_part2)db.close()mail_host="smtp.xxxx.com"sender = ‘[email protected]‘receivers = [‘[email protected]‘,‘[email protected]‘,‘[email protected]‘,‘[email protected]‘]with open(‘/tmp/slavesof10010.HTML‘,mode=‘r‘) as f:    HTML=f.read()message = MIMEText(HTML,‘HTML‘)message[‘From‘] = header("[email protected]")message[‘To‘] = header(";".join(v for v in receivers))subject = ‘multi-source-repl of 100.10汇总‘message[‘Subject‘] = header(subject,‘utf-8‘)try:    smtpObj = smtplib.SMTP()    smtpObj.connect(mail_host,25)    # 25 为 SMTP 端口号    smtpObj.sendmail(sender,receivers,message.as_string())    print "邮件发送成功"except smtplib.SMTPException as e:    print "Error: 无法发送邮件",print e

计划任务

00 09 * * * python /server/scripts/get_HTML_10010.py &> /dev/null
总结

以上是内存溢出为你收集整理的python2监控多源复制状态并发邮件全部内容,希望文章能够帮你解决python2监控多源复制状态并发邮件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1191465.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存