Add hashbang, catch exceptions, and output end results.

master
Sven Slootweg 13 years ago
parent 98d27206ad
commit 99e76ea6ae

@ -1,3 +1,5 @@
#!/usr/bin/python
import re, argparse, os
def show_sorted(dictionary):
@ -28,6 +30,7 @@ urls = {}
extensions = {}
total_digits = 10
current_lines = 0
try:
ignore_extensions = options['extensions'].split(',')
@ -39,6 +42,7 @@ for logpath in options['logfiles']:
log = open(logpath, 'r')
for line in log:
try:
ip, hostname, dash, datetime, timezone, method, uri, version, status, size, referer, useragent = line.split(' ', 11)
datetime = datetime[1:]
date = datetime.split(':')[0]
@ -75,31 +79,36 @@ for logpath in options['logfiles']:
files[filename] += 1
urls[uri] += 1
extensions[extension] += 1
except ValueError:
print "Corrupt log line at line %d, contents: %s" % (current_lines + 1, line[:-1])
print "Top days:"
show_sorted(days)
print ""
current_lines += 1
print "Top requested hostnames:"
show_sorted(hosts)
print ""
if current_lines % 1000 == 0:
print "Processed %d lines." % current_lines
except IOError:
print "Could not find file %s, ignored entry." % logpath
print "Top files:"
show_sorted(files)
print ""
print "Top days:"
show_sorted(days)
print ""
print "Top extensions:"
show_sorted(extensions)
print ""
print "Top requested hostnames:"
show_sorted(hosts)
print ""
print "Top referers:"
show_sorted(referers)
print ""
print "Top files:"
show_sorted(files)
print ""
print "Top URLs:"
show_sorted(urls)
print ""
print "Top extensions:"
show_sorted(extensions)
print ""
print "Top referers:"
show_sorted(referers)
print ""
except IOError:
print "Could not find file %s, ignored entry." % logpath
print "Top URLs:"
show_sorted(urls)
print ""

Loading…
Cancel
Save