# converts squid emulate_http_log access logs with enabled # mime_headers to apache-style access-combined logs. # # If virtual host is given as parameter, only # those log entries will be printed which vhost # ends with the virtual host argument. import re lineMatcher = re.compile('^\s*(?P[^ ]+) +(?P[^ ]+) +(?P[^ ]+) +(?P\[[^]]+\]) +(?P"[^"]*") +(?P[^ ]+) +(?P[^ ]+) +(?P[^ ]*) +\[(?P
[^]]*)\]') refererMatcher = re.compile(r'Referr?er:\s*(?P.*?)\\r\\n') uaMatcher = re.compile(r'User-Agent:\s*(?P.*?)\\r\\n') vhostMatcher = re.compile(r'Host:\s*(?P.*?)\\r\\n') def convert(infile, outfile, filterVhost=None, ignoredVhosts=(), ignoredIps=()): filterVhostMethod = filterVhost is not None and (lambda a: not a in ignoredVhosts and a.endswith(filterVhost)) or (lambda b: b not in ignoredVhosts) for line in infile: matched = lineMatcher.match(line) if matched: d = matched.groupdict() header = d.get('header', '') refMatch = refererMatcher.search(header) d['referer'] = refMatch and refMatch.groupdict().get('referer', '-') or '-' uaMatch = uaMatcher.search(header) d['ua'] = uaMatch and uaMatch.groupdict().get('ua', '-') or '-' if d['vhost'].strip() == '-': # get vhost from Host header vhostMatch = vhostMatcher.search(header) d['vhost'] = vhostMatch and vhostMatch.groupdict().get('vhost', '-') or '-' if d['ip'] in ignoredIps or not filterVhostMethod(d['vhost'].lower()): continue print >>outfile, "%(ip)s %(user)s %(vhost)s %(date)s %(req)s %(resp)s %(bytes)s \"%(referer)s\" \"%(ua)s\"" % d