複数のソースのレプリケーションステータスメッセージのpython2同時モニタリング

私たちは、マルチソース環境、レプリケーションを使用し、そのためのPythonスクリプトのステータスを表示するために書き、Pythonの2.6.6は、システムに付属して、メッセージの内容がHTML形式、インターレースの色で、次のようにスクリプトは次のとおりです。

# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import datetime
import MySQLdb
date_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 style="color:White" 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 = 1
db = 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 style="width: 20%;">'''+ row[1].encode('utf-8','ignore') + "</td>" + '''
            <td style="width: 15%;">'''+ row[10].encode('utf-8','ignore') + "</td>" + '''
            <td style="width: 15%;">'''+ row[11].encode('utf-8','ignore') + "</td>" + '''
            <td style="width: 15%;">'''+ str(row[32]) + "</td>" + '''
            <td style="width: 20%;">'''+ 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

おすすめ

転載: blog.51cto.com/qhd2004/2436855