Python分析MySQL慢日誌並定期發送郵件

來源:互聯網
上載者:User

因為開發針對某系統做穩定性建設,需要對資料庫系統的慢日誌進行審計,檢查最佳化。和開發溝通選擇定期發送慢查詢到開發的郵箱的方式,每日匯總,然後一起評估 最佳化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 進行分割,關於如何切割,各位可以思考一下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.