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

Loading…
Cancel
Save