溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python2怎么監(jiān)控多源復(fù)制狀態(tài)并發(fā)郵件

發(fā)布時間:2021-09-09 15:52:47 來源:億速云 閱讀:118 作者:chen 欄目:系統(tǒng)運維

本篇內(nèi)容主要講解“python2怎么監(jiān)控多源復(fù)制狀態(tài)并發(fā)郵件”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“python2怎么監(jiān)控多源復(fù)制狀態(tài)并發(fā)郵件”吧!

我們環(huán)境中用到了多源復(fù)制,因此寫個查看狀態(tài)的Python腳本,python為系統(tǒng)中自帶的2.6.6,郵件內(nèi)容為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><h3>Report of 10.10.100.10 multi source repl status {current_time}</h3>
    <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 = 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 >'''+ 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 = 'devops@xxxx.com'
receivers = ['devops@xxxx.com','123456789@qq.com','126@126.com','163@163.com']

with open('/tmp/slavesof10010.html',mode='r') as f:
    html=f.read()

message = MIMEText(html, 'html')
message['From'] = Header("devops@xxxx.com")
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 "郵件發(fā)送成功"
except smtplib.SMTPException as e:
    print "Error: 無法發(fā)送郵件",
    print e

計劃任務(wù)

00 09 * * * python /server/scripts/get_html_10010.py &> /dev/null

到此,相信大家對“python2怎么監(jiān)控多源復(fù)制狀態(tài)并發(fā)郵件”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI