File: pivot.py

package info (click to toggle)
endlessh 1.1-5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 192 kB
  • sloc: ansic: 729; sh: 150; xml: 73; python: 26; makefile: 25; sql: 8
file content (35 lines) | stat: -rwxr-xr-x 1,093 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3

# This script accepts a log on standard input and produces a CSV table
# with one connection per row.
#
#   $ util/pivot.py <log | sqlite3 -init util/schema.sql log.db

import sys
import pyrfc3339

table = {}
for line in sys.stdin:
    parts = line.split(' ')
    entry = {}
    entry['logtime'] = pyrfc3339.parse(parts[0])
    action = parts[1]
    if action == 'ACCEPT' or action == 'CLOSE':
        for item in parts[2:]:
            key, value = item.split('=')
            entry[key] = value
        if action == 'ACCEPT':
            table[entry['fd']] = entry
        else:
            accept = table[entry['fd']]
            del table[entry['fd']]
            delta = (entry['logtime'] - accept['logtime']).total_seconds()
            host = entry['host']
            port = entry['port']
            if host.startswith('::ffff:'):
                host = host[7:]
            nbytes = int(entry['bytes'])
            print('%s,%s,%.3f,%d' % (host, port, delta, nbytes))

if len(table) > 0:
    print('warning: %d hanging entries' % len(table), file=sys.stderr)