-
Notifications
You must be signed in to change notification settings - Fork 6
/
MysqlLogMonitor.py
61 lines (60 loc) · 1.97 KB
/
MysqlLogMonitor.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
import os
import time
import pymysql,json
__author__="misskiki"
"www.t00ls.net"
def MysqlContent(user,password,port,dbname,host):
try:
conn = pymysql.connect(
host = host,
user = user,
password = password,
db = "mysql",
charset = "utf8"
)
except Exception as e:
print("数据库链接失败")
else:
cursor = conn.cursor()
sql = "show variables like '%general%'"
cursor.execute(sql)
row = cursor.fetchall()
conn.commit()
for i in row:
if "OFF" in i:
onsql = "SET GLOBAL general_log = 'On'"
cursor.execute(onsql)
path = os.getcwd()
logfile = path + "/mysql_log" #if windows "//" or liunx "/"
logfile = logfile.replace("\\","//")
cursor.execute("SET GLOBAL general_log_file = '{}'".format(logfile))
conn.commit()
MysqlMonitor(logfile)
break
if "ON" in i:
path = os.getcwd()
logfile = path + "/mysql_log" #if windows "//" or liunx "/"
logfile = logfile.replace("\\","//")
print(logfile)
cursor.execute("SET GLOBAL general_log_file = '{}'".format(logfile))
conn.commit()
MysqlMonitor(logfile)
break
def MysqlMonitor(path):
pos = 0
while True:
fd = open(path,"r",encoding='utf8')
if pos != 0:
fd.seek(pos,0)
while True:
line = fd.read()
if line.strip():
print(line.strip())
pos = pos + len(line)
if not line.strip():
break
fd.close()
if __name__ == "__main__":
with open("mysql_config.ini","r") as f:
MysqlIni = json.loads(f.read())
MysqlContent(MysqlIni["name"],MysqlIni["pass"],MysqlIni["port"],MysqlIni["dbname"],MysqlIni["host"])