Last active
August 29, 2015 13:57
-
-
Save x/9818537 to your computer and use it in GitHub Desktop.
ping logs to csv
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/python | |
import sys | |
from urlparse import parse_qs | |
from operator import itemgetter | |
try: | |
# first argument is input ping log | |
in_file = open(sys.argv[1]) | |
# second argument is the csv target file | |
out_file = open(sys.argv[2], 'w') | |
# additional arguments are keys to filter on | |
# if none passed then all keys are used | |
filter_keys = sys.argv[3:] | |
except: | |
print 'usage: ./ping_to_csv.py in_ping_file.log parsed_pings.csv' | |
exit(1) | |
lines = in_file.read().splitlines() | |
# get all the keys | |
keys = set() | |
for line in lines: | |
_, ping = line.split(' ')[:2] | |
keys = keys | set(parse_qs(ping).keys()) | |
# if filter keys were passed, restrict the csv to only those | |
if filter_keys: | |
print 'only creating csv for keys ' + ', '.join(filter_keys) | |
keys = keys & set(filter_keys) | |
# assign an index to each one | |
key_map = dict(map(reversed, enumerate(list(keys)))) | |
# create output lines | |
out_lines = [] | |
for line in lines: | |
out_line = [None] * len(key_map) | |
_, ping = line.split(' ')[:2] | |
for k, v in parse_qs(ping).iteritems(): | |
if k in key_map: | |
out_line[key_map[k]] = v[0] | |
out_lines.append(map(str, out_line)) | |
# create a header to describe each column | |
header = map(itemgetter(0), sorted(key_map.items(), key=itemgetter(1))) | |
# join on commas and new lins and write | |
out_file.write('\n'.join([','.join(line) for line in [header] + out_lines])) | |
out_file.flush() | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment