#!/usr/bin/python # Overview: Displays the top inputs # Usage: kallahar_livestats logs/access.log 10 # # kallahar_livestats: # # keeps an array of all the reffers as well as how many hits it's had # displays the top (20) and their count, in real time # # by kallahar@quickwired.com and Aaron Klingaman, February 2006 import curses import sys import string import time def main(): uniquelines= {} filename = sys.argv[1] column = int(sys.argv[2]) f = open(sys.argv[1], "rb") f.seek(0, 2) # seek to the end rather than process the entire logfile while 1: data = f.read(1000000) if data: raw = str(data) lines = raw.split("\n") for line in lines: line = string.strip(line) # strip extra spaces if line != "": ra_val = line.split(" ") if len(ra_val) < 10: print "Error: line too short, try increasing the f.read length; ", line else: scr.clear() val = string.strip(ra_val[column]) if uniquelines.has_key( val ): uniquelines[val] += 1 else: uniquelines[val]= 1 entries=[(k,v) for v,k in uniquelines.items()] total_entries = sum(map(lambda x: x[0], entries)) entries.sort() entries.reverse() top_x= entries[:20] index= 0 scr.addstr( 0, 0, "kallahar_livestats: most popular items" ) for (value, key) in top_x: pct = float(float(value) / float(total_entries)) * 100 scr.addstr( index+1, 0, "%d\t%d %d%%\t%s" % (index+1, value, pct, key) ) index += 1 scr.addstr( index+1, 0, "Total Entries: %d" % total_entries ) scr.refresh() else: time.sleep(1) # wait a second f.seek(0, 1) # seek to the current position f.close() try: scr= curses.initscr() main() curses.endwin() except KeyboardInterrupt, e: curses.endwin() print "Usage: kallahar_livestats /var/log/access.log 10" print " where 10 is the column number to use" print " 1 = ip" print " 6 = requested file" print " 10 = referer"