Created
October 21, 2013 20:15
-
-
Save gravesm/7090207 to your computer and use it in GitHub Desktop.
This file contains 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 | |
from __future__ import print_function | |
import fileinput | |
import sys | |
import json | |
import apachelog | |
parser = apachelog.parser(apachelog.formats['extended']) | |
mappings = { | |
'%h': 'ip_address', | |
'%t': 'time', | |
'%r': 'request', | |
'%>s': 'status', | |
'%{Referer}i': 'referer', | |
'%{User-agent}i': 'user_agent', | |
} | |
def record_filter(record): | |
return record | |
def line_mapper(fields, mappings): | |
new_f = {} | |
for k,v in fields.items(): | |
if k in mappings: | |
new_f[mappings[k]] = v | |
return new_f | |
def parse_line(line, parser): | |
try: | |
return parser.parse(line) | |
except apachelog.ApacheLogParserError as err: | |
print(err, file=sys.stderr) | |
def get_writer(): | |
def writer(line): | |
if line: | |
print(json.dumps(line)) | |
return writer | |
def main(): | |
write = get_writer() | |
for line in fileinput.input(): | |
write(record_filter(line_mapper(parse_line(line, parser), mappings))) | |
if __name__ == "__main__": | |
if "-h" in sys.argv: | |
help = """ | |
To use, list file(s) as arguments or read from stdin | |
Example usage: | |
./kronstadt.py log_file1 log_file2 log_file3 | |
./kronstadt.py < log_file1 | |
head log_file1 | ./kronstadt.py""" | |
print(help) | |
sys.exit(0) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment