Skip to content

Instantly share code, notes, and snippets.

@meineerde
Created November 29, 2012 14:41
Show Gist options
  • Save meineerde/4169512 to your computer and use it in GitHub Desktop.
Save meineerde/4169512 to your computer and use it in GitHub Desktop.
Python Regex to Parse HAProxy's HTTP Logs
# Are quotes escaped?
escaped_quotes = True
haproxy_re = (r'haproxy\[(?P<pid>\d+)\]: '
r'(?P<client_ip>(\d{1,3}\.){3}\d{1,3}):(?P<client_port>\d{1,5}) '
r'\[(?P<date>\d{2}/\w{3}/\d{4}(:\d{2}){3}\.\d{3})\] '
r'(?P<listener_name>\S+) (?P<server_name>\S+) '
r'(?P<Tq>(-1|\d+))/(?P<Tw>(-1|\d+))/(?P<Tc>(-1|\d+))/(?P<Tr>(-1|\d+))/'
r'(?P<Tt>\+?\d+) '
r'(?P<HTTP_return_code>\d{3}) (?P<bytes_read>\d+) '
r'(?P<captured_request_cookie>\S+) (?P<captured_response_cookie>\S+) '
r'(?P<termination_state>[\w-]{4}) (?P<actconn>\d+)/(?P<feconn>\d+)/'
r'(?P<beconn>\d+)/(?P<srv_conn>\d+)/(?P<retries>\d+) '
r'(?P<server_queue>\d+)/(?P<listener_queue>\d+) '
r'(\{(?P<captured_request_headers>.*?)\} )?'
r'(\{(?P<captured_response_headers>.*?)\} )?')
if escape_quotes:
haproxy_re += r'\\"(?P<HTTP_request>.+)\\"'
else:
haproxy_re += r'"(?P<HTTP_request>.+)"'
haproxy_re = re.compile(haproxy_re)
@geoffnin
Copy link

line 18 needs to be escaped_quotes rather than escape_quotes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment