All pastes #397222 Raw Edit

Miscellany

public python v1 · immutable
·published 2007-03-16 10:35 UTC
rendered paste body
# 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 relineMatcher = re.compile('^\s*(?P<ip>[^ ]+) +(?P<user>[^ ]+) +(?P<vhost>[^ ]+) +(?P<date>\[[^]]+\]) +(?P<req>"[^"]*") +(?P<resp>[^ ]+) +(?P<bytes>[^ ]+) +(?P<cache_state>[^ ]*) +\[(?P<header>[^]]*)\]')refererMatcher = re.compile(r'Referr?er:\s*(?P<referer>.*?)\\r\\n')uaMatcher = re.compile(r'User-Agent:\s*(?P<ua>.*?)\\r\\n')vhostMatcher = re.compile(r'Host:\s*(?P<vhost>.*?)\\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