利用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 进行分割,关于如何切割,各位可以思考一下。
本文永久更新链接地址:

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了使用MySQL的Alter Table語句修改表,包括添加/刪除列,重命名表/列以及更改列數據類型。

文章討論了為MySQL配置SSL/TLS加密,包括證書生成和驗證。主要問題是使用自簽名證書的安全含義。[角色計數:159]

文章討論了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比較了它們對初學者和高級用戶的功能和適合性。[159個字符]

本文討論了使用Drop Table語句在MySQL中放下表,並強調了預防措施和風險。它強調,沒有備份,該動作是不可逆轉的,詳細介紹了恢復方法和潛在的生產環境危害。

InnoDB的全文搜索功能非常强大,能够显著提高数据库查询效率和处理大量文本数据的能力。1)InnoDB通过倒排索引实现全文搜索,支持基本和高级搜索查询。2)使用MATCH和AGAINST关键字进行搜索,支持布尔模式和短语搜索。3)优化方法包括使用分词技术、定期重建索引和调整缓存大小,以提升性能和准确性。

本文討論了在PostgreSQL,MySQL和MongoDB等各個數據庫中的JSON列上創建索引,以增強查詢性能。它解釋了索引特定的JSON路徑的語法和好處,並列出了支持的數據庫系統。
