Class: LogStash::Filters::Varnishlog

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/varnishlog.rb

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/logstash/filters/varnishlog.rb', line 40

def filter(event)
  items = event.get("[message]").split("\n")
  if request=/\*+\s+<<\s+(?<type>\w+)\s+>>\s+/.match(items[0])
    event.set("type", request['type'].downcase)
  end
  ##Remove Blacklisted items from items hash
  items = items.grepv(blacklist_sections.join("|")) if blacklist_sections.any?
  ##

  ##timestamps
  timestamps = items.grep(/Timestamp/)
  timestamps.each do |timestamp|
    if match = /-\s+Timestamp\s+(?<step>.*): (?<time_a>.*) (?<time_b>.*) (?<time_c>.*)/.match(timestamp)
      event.set(normalize_fields("timestamp_" + match['step'] ), match['time_a'])
      event.set(normalize_fields("timestamp_" + match['step'] + "_raw"), match['time_a'] + " " + match['time_b'] + " " + match['time_c'])
    end
  end

  ##Acct
   = items.grep(/(Be|Req)Acct/)
  .each do |acct|
    if acct_match = /-\s+(Be|Req)Acct\s+(?<size_a>\d+)\s+(?<size_b>\d+)\s+(?<size_c>\d+)\s+(?<size_d>\d+)\s+(?<size_e>\d+)\s+(?<size_f>\d+)/.match(acct)
      event.set("bytes", acct_match['size_e'])
    end
  end
  ## VCL Log
  vcl_log = items.grep(/VCL_Log/)
  log_lines = []
  vcl_log.each_with_index do |log, index|
    if match = /-\s+VCL_Log\s+(?<log_line>.*)/.match(log)
      log_lines.push(match['log_line'])
    end
    if index == log_lines.size - 1
      event.set(normalize_fields("VCL_Log"), log_lines)
    end
  end

  # Requests
  ## Request headers.
  request_headers = items.grep(/(Be)?([rR]eq|[rR]esp)Header/)
  request_headers.each do |header|
    if match = /-+\s+(Be)?([rR]eq|[rR]esp)Header\s+(?<header_name>.*?): (?<header_value>.*)/.match(header)
      event.set(normalize_fields(match['header_name']), match['header_value'])
    end
  end
  ## Match ReqMethod.
  if method_match = /-+\s+(Be)?([rR]eq|[rR]esp)Method\s+(?<method>.*)/.match(items.grep(/(Be)?([rR]eq|[rR]esp)Method/)[0])
    event.set("http_method", method_match['method'])
  end
  ## Match ReqURL.
  if url_match = /-+\s+(Be)?([rR]eq|[rR]esp)URL\s+(?<url>\/.*)/.match(items.grep(/(Be)?([rR]eq|[rR]esp)URL/)[0])
    event.set("url", url_match['url'])
  end
  ## Match ReqProtocol.
  if protocol_match = /-+\s+(Be)?([rR]eq|[rR]esp)Protocol\s+(?<protocol>.*)/.match(items.grep(/(Be)?([rR]eq|[rR]esp)Protocol/)[0])
    event.set("protocol", protocol_match['protocol'])
  end
  ## FetchError.
  if error_match = /-+\s+FetchError\s+(?<error>.*)/.match(items.grep(/FetchError/)[0])
    event.set("FetchError", error_match['error'])
  end
  ## Match RespStatus
  status_match = items.grep(/(Be)?([rR]eq|[rR]esp)Status/)
  states = []
  status_match.each_with_index do |status, index|
    if match = /-+\s+(Be)?([rR]eq|[rR]esp)Status\s+(?<status>.*)/.match(status)
      states.push(match['status'].to_i)
    end
    if index == status_match.size - 1
      event.set("http-rc", states)
    end
  end
  ## Match RespReason
  response_reason = items.grep(/(Be)?([rR]eq|[rR]esp)Reason/)
  reasons = []
  response_reason.each_with_index do |reason, index|
    if match = /-+\s+(Be)?([rR]eq|[rR]esp)Reason\s+(?<reason>.*)/.match(reason)
      reasons.push(match['reason'])
    end
    if index == response_reason.size - 1
      event.set("reason", reasons)
    end
  end

  if @message
    # Replace the event message with our message as configured in the
    # config file.
    event.set("message", @message)
  end

  # filter_matched should go in the last line of our successful code
  filter_matched(event)
end

#registerObject



35
36
37
# File 'lib/logstash/filters/varnishlog.rb', line 35

def register
  # Add instance variables
end