-
Notifications
You must be signed in to change notification settings - Fork 3
/
screen.py
211 lines (161 loc) · 5.82 KB
/
screen.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python
import curses
from datetime import datetime
class dbtopScreen:
"""
Implements the curses screen to show data
"""
ErrorOcurred = 0
stdscr = None
header_rows = 6
headerpad = None
queryrows = 20
querypad = None
locksrows = 15
lockspad = None
sessionspad = None
sessionsrows = 10
max_y=0
max_x=0
type=""
def __init__(self,type="MySQL"):
global headerpad,querypad,lockspad,sessionspad
self.type=type
try:
self.stdscr=curses.initscr()
curses.start_color()
#PAIR 2 is white text on black BG
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
#PAIR 2 is Red text on black BG
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
self.stdscr.clear()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
self.stdscr.nodelay(1)
self.stdscr.keypad(1)
(self.max_y,self.max_x) = self.stdscr.getmaxyx()
headerpad = self.stdscr.subpad(self.header_rows,self.max_x, 0,0)
headerpad.border()
headerpad.addstr(1,1,"DB Monitor for "+self.type)
headerpad.refresh()
querypad = self.stdscr.subpad(self.queryrows,self.max_x, self.header_rows,0)
querypad.addstr(0,3,"Running queries")
querypad.border()
querypad.refresh()
lockspad = self.stdscr.subpad(self.locksrows,self.max_x, self.header_rows+self.queryrows,0)
lockspad.border()
lockspad.addstr(0,3,"Current DB Locks")
lockspad.refresh()
sessionspad = self.stdscr.subpad(self.sessionsrows,self.max_x, self.header_rows+self.queryrows+self.locksrows,0)
sessionspad.border()
sessionspad.addstr(0,3,"Current Sessions")
sessionspad.refresh()
curses.doupdate()
except:
print ("Exception in class")
ErrorOcurred=1
raise
def __del__(self):
curses.nocbreak()
self.stdscr.keypad(0)
curses.echo()
if self.ErrorOcurred == 0 :
curses.endwin()
def getinput(self):
self.stdscr.getch()
def refreshRegion(self,SubPad):
self.updateTime()
SubPad.refresh()
#curses.doupdate()
def refreshAll(self):
headerpad.refresh()
querypad.refresh()
querypad.refresh()
curses.doupdate()
def updateConnectionString(self,connStr):
headerpad.addstr(1,1,"DB Monitor for "+self.type+" - "+connStr)
self.refreshRegion(headerpad)
def updateCurrentConnections(self,conn):
headerpad.addstr(2,1,"Current Connections:"+conn)
self.refreshRegion(headerpad)
def updateTime(self):
time=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
headerpad.addstr(1,self.max_x-20, time)
def updateQueries(self,queries):
i=0
posx=0
bgcolor=0;
querypad.clear()
querypad.border()
querypad.addstr(0,3,"Running queries")
querypad.addstr(1,1," Hostname | Username | Database | TimeRunning | Query ")
while (i < len(queries)) and (i < (self.queryrows-3)):
#Show long runnigng queries in RED
if round(queries[i][3]) > 5:
bgcolor=curses.color_pair(2)| curses.A_BOLD
else:
bgcolor=curses.color_pair(1) #DEFAULT COLOR
posy=2+i*2
querypad.addstr(posy,2,queries[i][1][:12],bgcolor) #hostname
querypad.addch (posy,13,chr(124),bgcolor) #print |
querypad.addstr(posy,15,queries[i][2][:12],bgcolor) #username
querypad.addch (posy,27,chr(124),bgcolor) #print |
querypad.addstr(posy,28,queries[i][0][:12],bgcolor) #database
querypad.addch (posy,40,chr(124),bgcolor) #print |
querypad.addstr(posy,44,str(round(queries[i][3]))[:12],bgcolor) #Time
querypad.addch (posy,56,chr(124),bgcolor) #print |
querypad.addstr(posy,58,queries[i][4][:self.max_x-59],bgcolor) #Query
querypad.addch (posy+1,56,chr(124),bgcolor) #print |
querypad.addstr(posy+1,58,queries[i][4][(self.max_x-59):(2*(self.max_x-59))],bgcolor) #Query2
i+=1
self.refreshRegion(querypad)
def updateLocks(self,locks):
i=0
lockspad.clear()
lockspad.border()
lockspad.addstr(0,3,"Current DB Locks")
lockspad.addstr(1,1," Object | Type | SID | LockType | LockMode | Block | LockTime |")
while (i < len(locks)) and (i< self.locksrows-3):
posy=2+i
lockspad.addstr(posy,1,locks[i][0][:16]) #object name
lockspad.addch (posy,17 ,chr(124)) #print |
lockspad.addstr(posy,19,locks[i][1][:8]) #object type
lockspad.addch (posy,28,chr(124)) #print |
lockspad.addstr(posy,30,str(locks[i][2])[:5]) #sessionID
lockspad.addch (posy,36,chr(124)) #print |
lockspad.addstr(posy,38,str(locks[i][3])[:8]) #LockType
lockspad.addch (posy,50,chr(124)) #print |
lockspad.addstr(posy,55,str(locks[i][4])[:8]) #LockMode
lockspad.addch (posy,63,chr(124)) #print |
lockspad.addstr(posy,67,str(locks[i][6])[:8]) #Block
lockspad.addch (posy,73,chr(124)) #print |
lockspad.addstr(posy,76,str(locks[i][7])[:10]) #LockTime
lockspad.addch (posy,86,chr(124)) #print |
i+=1
self.refreshRegion(lockspad)
def updateSessions(self,sessions):
i=0
sessionspad.clear()
sessionspad.border()
sessionspad.addstr(0,3,"Current Sessions")
sessionspad.addstr(1,1," SID | Username | Hostname | Schema | Start Time |")
try:
while (i < len(sessions)) and (i< (self.sessionsrows-3)):
posy=2+i
sessionspad.addstr(posy,2,str(sessions[i][0])[:6]) #SID
sessionspad.addch (posy,9 ,chr(124)) #print |
sessionspad.addstr(posy,12,str(sessions[i][1])[:12]) #username
sessionspad.addch (posy,26 ,chr(124)) #print |
sessionspad.addstr(posy,28,sessions[i][2][:12]) #hostname
sessionspad.addch (posy,41 ,chr(124)) #print |
sessionspad.addstr(posy,43,sessions[i][3][:10]) #schema name
sessionspad.addch (posy,54 ,chr(124)) #print |
sessionspad.addstr(posy,56,sessions[i][4].strftime('%Y-%m-%d %H:%M:%S')) #start time
sessionspad.addch (posy,76 ,chr(124)) #print |
i+=1
except:
None
self.refreshRegion(sessionspad)
def updateScreen(self):
curses.doupdate()