-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_log_analyzer_mysqllog.py
101 lines (92 loc) · 3.25 KB
/
request_log_analyzer_mysqllog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
#========================================================================
# FileName: request_log_analyzer_mysqllog.py
# Author: Crazyunix
# Email: [email protected]
# HomePage: http://www.yunvi.com
# LastChange: 2013-04-23 15:29:37
#========================================================================
'''
import MySQLdb as mdb
import sys
import os
import glob
import time
path = '/opt/mysql/'
slowlog='mysql-slow.log'
when = time.strftime('%Y-%m-%d',time.localtime())
req_log = 'slow'+when
def Splitlog():
if not os.path.isdir(path):
print (path,'is not a dir !')
sys.exit(0)
try:
os.chdir(path)
except:
print ("Permission denied")
sys.exit(0)
if not os.path.isfile('mysql-slow.log'):
print (path,'is not "mysql-slow.log"')
sys.exit(0)
src_size = float('%.2f' % float('.'.join(str(x)for x in (divmod(os.path.getsize(slowlog),1024*1024)))))
if src_size > 500:
os.system("mv msandbox-slow.log mysql-slow")
try:
con = mdb.connect(host="localhost",user='root',passwd='',charset='utf-8',port=3306,unix_socket='/tmp/mysql.sock')
except:
print 'Could not connect to MySQL server!'
sys.exit(0)
try:
#cur = con.cursor()
#cur.execute("flush logs")
#cur.execute("create database yong")
con.close()
except Exception,e:
print e
os.system("mv mysql-slow.log mysql-slow")
sys.exit(0)
os.system("split -b 500M mysql-slow slow`date +%F`")
else:
os.system("mv mysql-slow.log slow`date +%F`")
try:
con = mdb.connect("localhost",'root','','',25562)
except:
print 'Could not connect to MySQL server!'
sys.exit(0)
try:
cur = con.cursor()
cur.execute("flush logs")
#cur = con.cursor()
#cur.execute("show databases")
#print "Databases all : %s" % cur.fetchall()
con.close()
except:
print 'Could not flush!'
if os.path.isfile(path+req_log):
os.system("mv slow`date +%F` mysql-slow.log")
sys.exit(0)
def Request_log():
os.chdir(path)
files=[os.path.basename(x) for x in glob.glob(req_log + '*')]
print files
date=time.strftime("%Y-%m-%d",(time.localtime()))
req = "request-log-analyzer %s/%s --output HTML --mail [email protected] --mailhost mail.163.com --mailsubject 'rails log-analyzer' --after %s" % ( path,files[-1],date)
print req
if len(files) == 1:
os.system(req)
else:
for i in range(len(files)):
req1 = "request-log-analyzer %s/%s --output HTML --mail [email protected] --mailhost mail.163.com --mailsubject 'rails log-analyzer' --after %s" % (path,files[i],date)
os.system(req1)
def Remove_log():
if os.path.isfile(path+'msandbox-slow'):
os.system('mv /opt/mysql/mysql-slow.log /opt/backup/mysql-slow')
os.system('mv /opt/mysql/slow`date +%F`* /opt/backup-slow')
else:
os.system('mv /opt/mysql/slow`date +%F`* /opt/backup-slow')
if __name__ == '__main__':
Splitlog()
Request_log()
Remove_log()