Skip to content

Instantly share code, notes, and snippets.

@bactisme
Last active April 7, 2022 04:11
Show Gist options
  • Save bactisme/60e2eb74fba40bc4c8dcb0c0813a35c3 to your computer and use it in GitHub Desktop.
Save bactisme/60e2eb74fba40bc4c8dcb0c0813a35c3 to your computer and use it in GitHub Desktop.
Show a sorted table with path count, like ngxtop. Usage tail -f /var/log/nginx/access.log | /root/mynginx.py
#!/usr/bin/python
# charset: utf8
import sys
import re
import curses
from threading import Thread
import operator
table_count = {}
class CaptureInput(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
for line in sys.stdin:
path = re.search('(?:GET|POST) (.*) HTTP', line, re.IGNORECASE)
if path != None:
p = path.group(1)
if p in table_count:
table_count[p] += 1
else:
table_count[p] = 1
def get_sorted_table_count():
sorted_x = sorted(table_count.items(), key=operator.itemgetter(1), reverse=True)
return sorted_x
def draw_interface(stdscr):
stdscr.clear()
stdscr.refresh()
stdscr.keypad(True)
k = 0
curses.start_color()
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
while (k != ord('q')):
stdscr.clear()
height, width = stdscr.getmaxyx()
# start drawing
#stdscr.addstr(0, 0, "yolo")
table_sorted = get_sorted_table_count()
i = 0
for path,count in table_sorted:
stdscr.addstr(i, 0, path[:width-10])
stdscr.addstr(i, width-6, str(count)[:6])
i += 1
if i > (height - 10): break
stdscr.refresh()
k = stdscr.getch()
def main():
capture = CaptureInput()
capture.start()
curses.wrapper(draw_interface)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment