Use Python to send MySQL slow log emails

Source: Internet
Author: User

Use Python to send MySQL slow log emails

Requirement 1
Because development performs Stability Construction for a system, it is necessary to audit the slow logs of the database system and check and optimize the system. Communicate with developers by regularly sending slow queries to the developed email address, summarizing them on a daily basis, and evaluating and optimizing slow queries together.

2. Tool implementation
Mail. py script

#! /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 ['subobject'] = 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 t Exception, e:

Print str (e)
SendSlowlog. py
 

#! /Usr/bin/env python
 
#-*-Coding: UTF-8 -*-

Import mail # use the above script

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)

Note
The system has split the slow log every hour. You can think about how to cut it.

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.