Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to monitor the status of multi-source replication and concurrent mail by python2

Shulou Source: shulou.com Published: 2022-06-01 11:23:11 09月11日 Update

This article mainly explains "how to monitor the status of multi-source replication and concurrent mail by python2". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how python2 monitors the status of multi-source replication and concurrent mail".

Multi-source replication is used in our environment, so write a Python script to check the status. Python is 2.6.6 included in the system. The content of the email is in html format, and the interlacing changes color. The script is as follows:

#-*-coding: UTF-8-*-import smtplibfrom email.mime.text import MIMETextfrom email.header import Headerimport datetimeimport MySQLdbdate_end = datetime.date.today () html_part1 = "" Report of 10.10.100.10 multi source repl status {current_time} Master_Host Slave_IO_Running Slave_SQL_Running Seconds_Behind_Master Channel_Name "" .format (current_time=date_end) html_part2 = "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 ='+ row [1] .encode ('utf-8' Ignore') + "+ row [10] .encode ('utf-8','ignore') +" +' + row [11] .encode ('utf-8') 'ignore') + "+' + str (row [32]) +" +'+ Channel_Name + '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'] 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 summary' message ['Subject'] = Header (subject,' utf-8') try: smtpObj = smtplib.SMTP () smtpObj.connect (mail_host, 25) # 25 is SMTP port number smtpObj.sendmail (sender, receivers, message.as_string () print" message sent successfully "except smtplib.SMTPException as e: print" Error: unable to send message ", print e

Schedule a task

00 09 * python / server/scripts/get_html_10010.py & > / dev/null here, I believe you have a better understanding of "how python2 monitors concurrent mail in the status of multi-source replication". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Email status multi-source monitoring content script learning practical deeper successful useful task interest slogan practical practical easy to operate method more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MySQL Xiaomi Huawei Docker