httplog2ts/httplog2code_perminute_ts

26 lines
794 B
Python

#!/usr/bin/python3
import re
import sys
import dateutil.parser
codes = ["200", "301", "302", "400", "401", "403", "404", "405", "408"]
data = {}
with open(sys.argv[1]) as f:
for ln in f:
if m := re.match(r'\S+ - \S+ \[(../.../....:..:..:.. .....)\] "[^"]*" (\d+) ', ln):
dt = dateutil.parser.parse(m.group(1).replace(':', ' ', 1))
ts = int(dt.timestamp() // 60) * 60 # truncate to minute
code = m.group(2)
if ts not in data:
data[ts] = {c: 0 for c in codes}
data[ts][code] += 1
empty = {c: 0 for c in codes}
for ts in range(min(data.keys()), max(data.keys()) + 1, 60):
if ts not in data:
data[ts] = empty
for code in codes:
print(ts, code, data[ts][code], sep="\t")
# vim: tw=99