Created
          November 29, 2012 14:41 
        
      - 
      
- 
        Save meineerde/4169512 to your computer and use it in GitHub Desktop. 
    Python Regex to Parse HAProxy's HTTP Logs
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # 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) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
line 18 needs to be escaped_quotes rather than escape_quotes