-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht8.py
executable file
·371 lines (327 loc) · 14.8 KB
/
t8.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python
"""
T8.py
Static (mysql) Terminal viewer for the L8 redis-backed logger.
Copyright (C) 2014-2015 Alan McFarlane <[email protected]>
Copyright (C) 2014-2015 Rob Chett <[email protected]>
Copyright (C) 2015 Ben Gosney <[email protected]>
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""
import math
import json
import curses
from curses import wrapper
import sys
import time
from data.data import Data
from data.data import ConnectionError
from data.config import args
class T8:
class screens:
DOMAINS = 1
ERRORS = 2
CONTEXT = 3
def __init__(self, data_source):
self.domain_max = 0
self.error_max = 0
self.context_max = 0
self.data = data_source
self.current_screen = self.screens.DOMAINS
self.domain_index = 0
self.error_index = 0
self.context_index = 0
self.options_index = 0
self.error_mode = data.mode.totals
self.window_height = 0
self.window_width = 0
self.screen_offsets = [0, 0, 0]
self.current_domain = None
self.current_error = None
# Setup screen
self.screen1 = None
self.screen2 = None
self.screen3 = None
self.stdscr = None
# Create looping function
try:
wrapper(self.init_windows)
except KeyboardInterrupt:
curses.endwin()
pass
def write_lines(self, screen, string, color):
lines = 0
while (lines * curses.COLS) < len(string):
lines += 1
screen.addstr('{0: <{width}}'.format(string, width=(lines * curses.COLS)), curses.color_pair(color))
return lines
def domain_list(self):
domains = self.data.domains()
self.domain_max = len(domains)
self.screen_offsets[0] = self.write_lines(
self.screen1,
'Domains ({0} of {1}) {2} {3}'.format(self.domain_index + 1, self.domain_max, self.data.get_level_sql(), self.data.get_time_sql()),
1 if self.screens.DOMAINS != self.current_screen else 3
)
cnt = 0
self.current_domain = None
if self.domain_max:
for i in domains:
if self.domain_index == cnt:
self.current_domain = i
if self.should_skip(self.domain_index, cnt, self.window_height, 1):
self.screen1.addstr('{0: <{width1}}{1: >{width2}}'.format(i.host, i.error_count, width1=curses.COLS - 10, width2=10), curses.color_pair(2 if self.domain_index == cnt else 0))
cnt += 1
def should_skip(self, index, cnt, height, lines):
skip_cnt = index
return cnt >= skip_cnt and ((cnt - skip_cnt) * lines) < height - lines - 2
def error_list(self):
self.screen_offsets[1] = self.write_lines(
self.screen2,
'Errors ({0} of {1})'.format(self.error_index + 1, self.error_max),
1 if self.screens.ERRORS != self.current_screen else 3
)
if self.current_domain:
errors = self.data.errors(self.current_domain.host, self.error_mode)
self.error_max = len(errors)
cnt = 0
self.current_error = None
for i in errors:
try:
wide = curses.COLS > 120
if self.error_index == cnt:
self.current_error = i
if self.should_skip(self.error_index, cnt, self.window_height, 1 if wide else 2):
if wide:
mid_width = curses.COLS - (30 + 5 + 10 + 5 + 10 + 1)
self.screen2.addstr('{0: <{width1}}:{1: <{width2}}{2: <{width3}}{3: <{width4}}{4: <{width5}}{5: <{width6}}'.format(
i.file[-30:],
i.line,
i.message.replace("\n", "|")[-mid_width:],
i.level,
i.count,
Data.format_datetime(i.time),
width1=30, width2=5, width3=mid_width, width4=10, width5=5, width6=10
), curses.color_pair(2 if self.error_index == cnt else 0))
else:
self.screen2.addstr('{0: <{width2}}{1: <{width3}}{2: <{width4}}{3: <{width5}}\n{4}\n'.format(
"{0}:{1}".format(
i.file[-30:],
i.line
),
i.level,
i.count,
Data.format_datetime(i.time),
i.message.replace("\n", "|"),
width2=(curses.COLS - 10 - 5 - 10 - 1), width3=10, width4=5, width5=10
), curses.color_pair(2 if self.error_index == cnt else 0))
except curses.error as e:
pass
cnt += 1
else:
self.write_lines(
self.screen2,
'No domain',
3
)
def error_context(self):
if self.current_error:
text = json.dumps(self.current_error.context, indent=4).split('\n')
self.context_max = len(text)
lines = self.screen_offsets[2] = self.write_lines(
self.screen3,
'Error Context ({0} of {1})'.format(self.context_index,self.context_max),
1 if self.screens.CONTEXT != self.current_screen else 3
)
cnt = 0
for i in self.current_error.message.split('\n'):
self.write_lines(
self.screen3,
'{0}'.format(i),
4
)
cnt += max(math.ceil(len(self.current_error.message) / self.window_width), 1)
total = self.window_height - cnt
for i in text:
if self.should_skip(self.context_index, cnt, self.window_height - lines, 1):
self.screen3.addstr('{0}\n'.format(i))
cnt += max(math.ceil(len(i) / self.window_width), 1)
else:
self.screen_offsets[2] = self.write_lines(
self.screen3,
'No error',
3
)
def get_help_screen(self):
self.clear(True, True, True)
self.screen1.addstr('\
Key commands\n\
tab : move between windows \n\
up : Move through the current window\n\
down: Move through the current window\n\
h : View this screen\n\
q : Quit or close a sub screen\n\
o : Open the options screen'
)
self.refresh(True, True, True)
while True:
c = self.stdscr.getch()
if c == ord('q'):
self.options_index = 0
return
def get_options_screen(self):
while True:
self.clear(True, True, True)
cnt = 0
self.screen1.addstr('Options\n')
self.screen1.addstr('Dates\n')
self.screen1.addstr('\tStart date: {0}\n'.format(self.data.format_timestamp(self.data.start_time)), curses.color_pair(2 if self.options_index == cnt else 0))
cnt += 1
self.screen1.addstr('\tEnd date : {0}\n'.format(self.data.format_timestamp(self.data.end_time)), curses.color_pair(2 if self.options_index == cnt else 0))
cnt += 1
self.screen1.addstr('\n')
self.screen1.addstr('Error levels\n')
for i in self.data.levels.keys:
self.screen1.addstr('\t[{0}] {1}\n'.format('x' if self.data.error_levels[cnt - 2] else ' ', i), curses.color_pair(2 if self.options_index == cnt else 0))
cnt += 1
self.refresh(True, True, True)
c = self.stdscr.getch()
if c == ord('q'):
return
elif c == curses.KEY_UP:
self.options_index = max(self.options_index - 1, 0)
elif c == curses.KEY_DOWN:
self.options_index = min(self.options_index + 1, cnt - 1)
elif c == ord(' '):
if self.options_index >= 2:
self.data.error_levels[self.options_index - 2] = self.data.error_levels[self.options_index - 2] = not self.data.error_levels[self.options_index - 2]
elif c == curses.KEY_LEFT:
if self.options_index == 0:
self.data.start_time -= 24 * 60 * 60
self.data.start_time -= 24 * 60 * 60
elif self.options_index == 1:
self.data.end_time -= 24 * 60 * 60
elif c == curses.KEY_RIGHT:
if self.options_index == 0:
self.data.start_time += 24 * 60 * 60
elif self.options_index == 1:
self.data.end_time += 24 * 60 * 60
def refresh(self, screen1, screen2, screen3):
if screen1:
self.screen1.noutrefresh()
if screen2:
self.screen2.noutrefresh()
if screen3:
self.screen3.noutrefresh()
curses.doupdate()
def clear(self, screen1, screen2, screen3):
if screen1:
self.screen1.clear()
if screen2:
self.screen2.clear()
if screen3:
self.screen3.clear()
def redraw(self, level):
self.clear(level <= self.screens.DOMAINS, level <= self.screens.ERRORS, level <= self.screens.CONTEXT)
if level <= self.screens.DOMAINS:
self.domain_list()
if level <= self.screens.ERRORS:
self.error_list()
if level <= self.screens.CONTEXT:
self.error_context()
self.refresh(level <= self.screens.DOMAINS, level <= self.screens.ERRORS, level <= self.screens.CONTEXT)
def init_windows(self, stdscr):
# Disable echo of key presses
curses.noecho()
# Disable key buffer with release on enter
curses.cbreak()
# Format cursor keys as constants
stdscr.keypad(True)
# Setup colours
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_GREEN)
curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_RED)
curses.init_pair(4, curses.COLOR_WHITE, curses.COLOR_MAGENTA)
stdscr.clear()
self.window_height = (curses.LINES / 3)
self.window_width = curses.COLS
# Create a sub window at half height
self.stdscr = stdscr
self.screen1 = curses.newwin(self.window_height, self.window_width, 0, 0)
self.screen2 = curses.newwin(self.window_height, self.window_width, self.window_height, 0)
self.screen3 = curses.newwin(self.window_height, self.window_width, self.window_height * 2, 0)
stdscr.refresh()
self.redraw(self.screens.DOMAINS)
while True:
c = self.stdscr.getch()
if c == curses.KEY_UP:
if self.current_screen == self.screens.DOMAINS:
self.domain_index = max(self.domain_index - 1, 0)
self.error_index = 0
self.context_index = 0
elif self.current_screen == self.screens.ERRORS:
self.error_index = max(self.error_index - 1, 0)
self.context_index = 0
elif self.current_screen == self.screens.CONTEXT:
self.context_index = max(self.context_index - 1, 0)
self.redraw(self.current_screen)
elif c == curses.KEY_DOWN:
if self.current_screen == self.screens.DOMAINS:
self.domain_index = min(self.domain_index + 1, self.domain_max - 1)
self.error_index = 0
self.context_index = 0
elif self.current_screen == self.screens.ERRORS:
self.error_index = min(self.error_index + 1, self.error_max - 1)
self.context_index = 0
elif self.current_screen == self.screens.CONTEXT:
self.context_index = min(self.context_index + 1, self.context_max - 1)
self.redraw(self.current_screen)
elif c == ord('a'):
self.error_mode = data.mode.totals if self.error_mode == data.mode.latest else data.mode.latest
self.redraw(self.screens.ERRORS)
elif c == curses.KEY_DC:
if self.current_screen == self.screens.ERRORS:
if self.error_mode == data.mode.latest:
self.data.delete_entry(self.current_error)
else:
self.data.delete_type(self.current_error)
self.current_error = None
self.context_index = 0
self.redraw(self.screens.ERRORS)
elif c == ord('\t'):
self.current_screen = (self.current_screen + 1) if self.current_screen != self.screens.CONTEXT else self.screens.DOMAINS
self.redraw(self.screens.DOMAINS)
elif c == ord('h'):
self.get_help_screen()
self.redraw(self.screens.DOMAINS)
elif c == ord('o'):
self.get_options_screen()
self.redraw(self.screens.DOMAINS)
elif c == ord('q'):
return True
if __name__ == "__main__":
try:
data = Data(args)
t8 = T8(data)
except ConnectionError as err:
Data.invalid_connection(err, args)