這篇文章將為大家詳細講解有關怎樣利用python 發送MySQL慢日志郵件,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

一 需求
因為開發針對某系統做穩定性建設,需要對數據庫系統的慢日志進行審計,檢查優化。和開發溝通選擇定期發送慢查詢到開發的郵箱的方式,每日匯總,然后一起評估 優化slow query 。
二 工具實現
mail.py 腳本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# mail.py
import os.path
import time
import sys
import os
import json
import string
import random
import smtplib
import time
from datetime import date
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.Header import Header
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
mail_user="xxx@xxxx.com";
mail_pass="xxxxx";
mail_smtp_server="smtp.xxxxx.com";
mail_smtp_port= 25
def sendMail(contents,subject,attach,mail_to):
lTime=str(time.strftime('%Y%m%d_%H',time.localtime(time.time())))
msg = MIMEMultipart('related');
if subject=="":
subject='test';
msg['Subject'] = subject+" Time:"+ lTime;
msg['From'] = mail_user;
msg['To'] = ";".join(mail_to) ;
html="";
for cont in contents:
html = html+cont[0];
msgHtml = MIMEMultipart('alternative');
msgHtml.set_charset('UTF-8');
msgAtt = MIMEText(open(attach,'rb').read(),'base','gbk');
msgAtt["Content-Type"] = 'application/octet-stream'
msgAtt["Content-Disposition"] = 'attachment; filename="'+subject+'"'
msg.attach(msgAtt);
try:
smtp = smtplib.SMTP();
smtp.connect(mail_smtp_server,mail_smtp_port)
smtp.login(mail_user,mail_pass);
smtp.sendmail(mail_user,mail_to,msg.as_string());
smtp.close();
except Exception,e:
print str(e)
sendSlowlog.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import mail ##使用了上述腳本
import sys
import time
import os
import datetime
from datetime import date
from os.path import join, getsize
def sendSlowlog(subject_info,attach,mail_to):
size = os.path.getsize(attach)
if(size > 1):
mail.sendMail("",subject_info,attach,mail_to)
if __name__ == '__main__':
mail_to=["qilong.yangql@xxxx.com"]
lDate=str(time.strftime('%Y%m%d',time.localtime(time.time())))
lTime=str(time.strftime('%Y%m%d_%H',time.localtime(time.time())))
subject_info="Slowlog of DBname" + lTime
attach="/u01/my3306/log/slow_"+ lDate +"/slow.log."+lTime
sendSlowlog(subject_info,attach,mail_to)
注
本系統已經每小時將slow log 進行分割,關于如何切割,各位可以思考一下。
關于怎樣利用python 發送MySQL慢日志郵件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當前標題:怎樣利用python發送MySQL慢日志郵件-創新互聯
轉載來于:http://www.yijiale78.com/article10/ddhddo.html
成都網站建設公司_創新互聯,為您提供移動網站建設、定制網站、營銷型網站建設、網站維護、網站制作、小程序開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯