forked from marcosgildavid/dbmonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql.py
More file actions
157 lines (124 loc) · 3.63 KB
/
mysql.py
File metadata and controls
157 lines (124 loc) · 3.63 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python
import MySQLdb
import datetime
class MySQLDatabase:
username = ''
password = ''
hostname = ''
database = ''
connection = None
def __init__(self,username='',password='',hostname='',database=''):
self.username=username
self.password=password
self.hostname=hostname
self.database=database
def setConnectionParameters(self,username,hostname,database):
self.username=username
self.password=password
self.hostname=hostname
self.database=database
def connect(self):
try:
self.connection=MySQLdb.connect(host=self.hostname, user=self.username, passwd=self.password, db=self.database)
except:
raise
def getCurrentConnections(self):
conns=0
cursor = self.connection.cursor()
cursor.execute("""
SHOW STATUS WHERE `variable_name` = 'Threads_connected';
""")
row=cursor.fetchone()
if row != None:
conns=row[1]
return conns
def getCurrentQueries(self):
#TODO: differentiate between MySQL >5.1.7 and < 5.1.7 (information_schema!)
myQueries=[]
myrow=[]
previousID=""
sqltext=""
cursor = self.connection.cursor()
cursor.execute("""
SELECT user username, host client,db , time/60 mins_running,info query
FROM information_schema.processlist
WHERE STATE='executing'
#AND time>60;
""")
rows=cursor.fetchall()
if len(rows) > 0:
for row in rows:
#Join SQL TEXT
myrow=list(row[:4])
sqltext=row[4].strip().replace("\t","").replace("\n"," ")
myrow.append(sqltext)
myQueries.append(tuple(myrow))
return myQueries
def getCurrentLocks(self):
myLocks=[]
rows=None
myrow=None
cursor = self.connection.cursor()
cursor.execute("""
show open tables where In_Use > 0 || Name_locked > 0;
""")
rows=cursor.fetchall()
#returned: Database, Table, In_use, Name_locked
#" Object | Type | SID | LockType | LockMode | Block | LockTime |")
if len(rows) > 0:
for row in rows:
myrow=list(myrow)
myrow.append(row[1])
myrow.append("TABLE") #TYPE
myrow.append("-") #SID
#LOCKTYPE
if row[3] > 0:
myrow.append("NAMELOCK")
elif row[2] > 0:
myrow.append("LOCK")
else:
myrow.append("")
myrow.append("-") #LOCKMODE
myrow.append(row[2]) #BLOCK
myrow.append("-") #Locktime
myLocks.append(tuple(myrow))
myrow=None
return myLocks
def getCurrentConnectionData(self):
connData=""
'''
cursor = self.connection.cursor()
cursor.execute("""
SELECT username,service_name,sid
FROM v$session
WHERE sid=(select sys_context('USERENV','SID') from dual)
""")
row=cursor.fetchone()
if row != None:
connData = str(row[0])+"@"+str(row[1])+"(SID: "+str(row[2])+")"
'''
connData=self.username+"@"+self.database+" connected to server: "+self.hostname
return connData
def getCurrentSessions(self):
rows=None
myrow=[]
mySessions=[]
cursor = self.connection.cursor()
cursor.execute("""
SHOW PROCESSLIST
""")
rows=cursor.fetchall()
#returns: ID, user, host,db, command, time, state ,info
#" SID | Username | Hostname | Schema | Start Time |")
if len(rows) > 0:
for row in rows:
myrow=list(row[:3])
if row[3]==None:
myrow.append("")
else:
myrow.append(row[3])
stime=datetime.datetime.now()-datetime.timedelta(seconds=row[5])
myrow.append(stime)
mySessions.append(tuple(myrow))
myrow=None
return mySessions