Peter Nixon avatar Peter Nixon committed f6b05a5

Add multiple file support and HTTP 503 error stats to bmtop

Comments (0)

Files changed (1)

 #!/usr/bin/env python
 
+import curses
+import logging
+import multiprocessing
+import sys
 import time
-import curses
-import sys
-try:
-    from tailer import Tailer
-except:
-    from bmutils.tailer import Tailer
 try:
     from collections import defaultdict, Counter
 except:
     from bmutils.collections27 import defaultdict, Counter
 
-from bmutils import UnisonWebLogParser
+from bmutils import logWorker
+
 
 __author__ = "Peter Nixon"
 __copyright__ = "Copyright (C) 2012 Peter Nixon"
 __license__ = "Public Domain"
-__version__ = "1.0"
+__version__ = "1.2"
 
 DEBUG = False
 
     rootwin = stdscr.derwin(size[0],size[1], 0, 0)
     return rootwin
 
-def _main(stdscr, filepath, options):
-    tailer = Tailer(open(filepath, 'rb'))
-    log = UnisonWebLogParser()
+def _main(stdscr, files, options):
+    logQueue = multiprocessing.Queue()
+    
+    for filepath in files:
+        p = multiprocessing.Process(target=logWorker, args=(filepath, logQueue, options.code))
+        p.daemon = True
+        p.start()
 
     try:
         try:
-            # Seek to the end so we can follow
-            tailer.seek_end()
-
             # Setup an ncurses display
             rootwin = init_display(stdscr)
             size = rootwin.getmaxyx()
             total_requests = 0
             bypassed_requests = 0
             malformed_lines = 0
-            for line in tailer.follow(delay=options.sleep):
+            while (logQueue):
+                log = logQueue.get()
                 total_requests += 1
-                if log.run_regex(line, process_hostnames=True) is False:
-                    malformed_lines += 1
-                    continue    # This log line is malformed so continue to the next one
                 stime = time.ctime(log.utime)
                 if log.status == 0:
                     bypassed_requests += 1
                     status_dict[log.domainname][log.status] +=1
                 if int(time.time()) > nextrefresh:
                     bypass_percent = float(bypassed_requests) / float(total_requests) * 100
-                    proxy_fail_percent = float(status_counter[504]) / float(total_requests) * 100
+                    percent_503 = float(status_counter[503]) / float(total_requests) * 100
+                    percent_504 = float(status_counter[504]) / float(total_requests) * 100
                     rootwin.border(0)
-                    rootwin.addstr(1, 1, "#  Hits      Site                          Proxy Timeout")
-                    rootwin.addstr(height - 2, 1, "Sites: %d  Requests: %d  Bypassed: %d (%f%%) Proxy Fails: %d (%f%%)" % (len(host_counter), total_requests, bypassed_requests, bypass_percent, status_counter[504], proxy_fail_percent))
+                    rootwin.addstr(1, 1, "#  Hits      Site                          Gateway Timeout  Service unavailable")
+                    rootwin.addstr(height - 2, 1, "Sites: %d  Req: %d  Bypass: %d (%f%%) 503 Err: %d (%f%%) 504 Err: %d (%f%%)" % (len(host_counter), total_requests, bypassed_requests, bypass_percent, status_counter[503], percent_503, status_counter[504], percent_504))
                     rootwin.hline(2, 1, curses.ACS_HLINE, width)
                     n = 1
                     y = 3
                         rootwin.addstr(y, 14, str(key))
                         #for (key2, num2) in status_dict[key].most_common(1):
                         #    rootwin.addstr(y, 40, str(key2))
-                        host_proxy_fail_percent = float(status_dict[key][504]) / float(total_requests) * 100
-                        rootwin.addstr(y, 44, "%f%% (%d)" % (host_proxy_fail_percent, status_dict[key][504]))
+                        host_percent_503 = float(status_dict[key][503]) / float(total_requests) * 100
+                        host_percent_504 = float(status_dict[key][504]) / float(total_requests) * 100
+                        rootwin.addstr(y, 44, "%f%% (%d)" % (host_percent_503, status_dict[key][503]))
+                        rootwin.addstr(y, 61, "%f%% (%d)" % (host_percent_504, status_dict[key][504]))
                         n += 1
                         y += 1
                     nextrefresh = int(time.time()) + refreshinterval
             # Escape silently
             pass
     finally:
-        tailer.close()
+        pass
         
 def main(stdscr):
     from optparse import OptionParser
 
     if options.test:
         _test()
-    elif not len(args) == 1:
+    elif not len(args) > 0:
         parser.print_help()
         sys.exit(1)
     else:
-        _main(stdscr, args[0], options)
+        _main(stdscr, args, options)
         
 if __name__ == '__main__':
+    if DEBUG: multiprocessing.log_to_stderr(logging.DEBUG)
     curses.wrapper(main)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.