1#!/usr/bin/env python3 2 3# This script accepts a log on standard input and produces a CSV table 4# with one connection per row. 5# 6# $ util/pivot.py <log | sqlite3 -init util/schema.sql log.db 7 8import sys 9import pyrfc3339 10 11table = {} 12for line in sys.stdin: 13 parts = line.split(' ') 14 entry = {} 15 entry['logtime'] = pyrfc3339.parse(parts[0]) 16 action = parts[1] 17 if action == 'ACCEPT' or action == 'CLOSE': 18 for item in parts[2:]: 19 key, value = item.split('=') 20 entry[key] = value 21 if action == 'ACCEPT': 22 table[entry['fd']] = entry 23 else: 24 accept = table[entry['fd']] 25 del table[entry['fd']] 26 delta = (entry['logtime'] - accept['logtime']).total_seconds() 27 host = entry['host'] 28 port = entry['port'] 29 if host.startswith('::ffff:'): 30 host = host[7:] 31 nbytes = int(entry['bytes']) 32 print('%s,%s,%.3f,%d' % (host, port, delta, nbytes)) 33 34if len(table) > 0: 35 print('warning: %d hanging entries' % len(table), file=sys.stderr) 36