Module: HAProxyLogParser

Defined in:
lib/haproxy_log_parser.rb,
lib/haproxy_log_parser/entry.rb,
lib/haproxy_log_parser/error_entry.rb

Defined Under Namespace

Classes: Entry, ErrorEntry, ParseError

Constant Summary collapse

VERSION =
IO.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).chomp.freeze

Class Method Summary collapse

Class Method Details

Converts a captured cookie string to a Hash.

Parameters:

  • string (String)

    a captured cookie string

Returns:

  • (Hash{String => String})

    a one-entry Hash with the cookie name and value, or an empty Hash if no cookie was captured



67
68
69
70
71
72
73
74
# File 'lib/haproxy_log_parser.rb', line 67

def decode_captured_cookie(string)
  if string == '-'
    {}
  else
    key, value = string.split('=', 2)
    {unescape(key) => unescape(value)}
  end
end

.decode_captured_headers(string) ⇒ Array<String>

Converts a captured headers string to an Array.

Parameters:

  • string (String)

    a captured headers section

Returns:

  • (Array<String>)

    array of captured header values



80
81
82
# File 'lib/haproxy_log_parser.rb', line 80

def decode_captured_headers(string)
  string.split('|', -1).map! { |header| unescape(header) }
end

.parse(line) ⇒ Entry, ErrorEntry

Returns an Entry or ErrorEntry object resulting from the given HAProxy HTTP-format log line.

Parameters:

  • line (String)

    a line from an HAProxy log

Returns:

Raises:

  • (ParseError)

    if the line was not parsed successfully



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/haproxy_log_parser.rb', line 20

def parse(line)
  parser = LineParser.new
  result = parser.parse(line)

  unless result
    raise ParseError, parser.failure_reason
  end

  entry =
    if result.suffix.respond_to?(:http_request)
      create_normal_entry(result.suffix)
    else
      create_error_entry(result.suffix)
    end
  entry.client_ip = result.client_ip.text_value
  entry.client_port = result.client_port.text_value.to_i
  entry.accept_date = parse_accept_date(result.accept_date.text_value)

  entry
end

.parse_accept_date(string) ⇒ Time

Converts the value of an accept_date field to a Time object.

Parameters:

  • string (String)

    value of an accept_date field

Returns:

  • (Time)


56
57
58
59
# File 'lib/haproxy_log_parser.rb', line 56

def parse_accept_date(string)
  parts = string.split(/[\/:.]/)
  Time.local(*parts.values_at(2, 1, 0, 3..6))
end

.unescape(string) ⇒ String

Returns the given string un-escaped. See the “Logging > Non-printable characters” section in HAProxy documentation.

Parameters:

  • string (String)

    string to unescape

Returns:

  • (String)

    an unescaped string



46
47
48
49
50
# File 'lib/haproxy_log_parser.rb', line 46

def unescape(string)
  string.gsub(/#[[:xdigit:]]{2}/) do |match|
    match[1..-1].to_i(16).chr
  end
end