最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

利用Python发送MySQL慢日志邮件

来源:动视网 责编:小采 时间:2020-11-09 14:24:32
文档

利用Python发送MySQL慢日志邮件

利用Python发送MySQL慢日志邮件:因为开发针对某系统做稳定性建设,需要对数据库系统的慢日志进行审计,检查优化。和开发沟通选择定期发送慢查询到开发的邮箱的方 一 需求 因为开发针对某系统做稳定性建设,需要对数据库系统的慢日志进行审计,检查优化。和开发沟通选择定期发送慢查询到
推荐度:
导读利用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="[email protected]";

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=["[email protected]"]

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慢日志邮件:因为开发针对某系统做稳定性建设,需要对数据库系统的慢日志进行审计,检查优化。和开发沟通选择定期发送慢查询到开发的邮箱的方 一 需求 因为开发针对某系统做稳定性建设,需要对数据库系统的慢日志进行审计,检查优化。和开发沟通选择定期发送慢查询到
推荐度:
标签: 邮件 发送 日志
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top