-
-
Save dirkakrid/163ea1d65a8b2e0c69b88d58f330d631 to your computer and use it in GitHub Desktop.
Parsing log file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# log_format traffic '$http_host|$status|$bytes_sent|$msec|$remote_addr|$request_uri'; | |
import sys | |
import itertools, operator | |
from datetime import datetime | |
access_log = '/var/log/nginx/access.log' | |
def sort_uniq(sequence): | |
return itertools.imap( operator.itemgetter(0), itertools.groupby(sorted(sequence))) | |
def get_unique_hosts(lines): | |
hosts = [] | |
for line in lines: | |
hosts.append(line.split("|")[0]) | |
return list(sort_uniq(hosts)) | |
def parse_log_file(logfile): | |
print 'Parsing %s' % logfile | |
f = open(logfile) | |
lines = f.readlines() | |
hosts = get_unique_hosts(lines) | |
print hosts | |
for line in lines: | |
fullline = line.split('|') | |
host = fullline[0] | |
status_code = fullline[1] | |
bytes_sent = fullline[2] | |
date_time = float(fullline[3]) | |
request_uri = fullline[4] | |
print datetime.fromtimestamp(date_time) | |
print "request uri %s with bytes sent %s" % ( request_uri, bytes_sent ) | |
try: | |
with open(access_log): | |
parse_log_file(access_log) | |
except IOError: | |
print 'Unable to open %s' % access_log | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment