Logstash Grok过滤器Apache访问日志

时间:2014-03-11 08:57:31

标签: filter logstash logstash-grok

我一直在那里四处寻找,但找不到工作决议。我尝试在Logstash配置文件中使用Grok Filter来过滤Apache-Access日志文件。日志消息如下所示:{"message":"00.00.0.000 - - [dd/mm/YYYY:hh:mm:ii +0000] \"GET /index.html HTTP/1.1\" 200 00"}.

此时我只能使用grok { match => [ "message", "%{IP:client_ip}" ] }过滤客户端IP。

我想过滤:

- The GET method, 
- requested page (index.html), 
- HTTP/1.1\, 
- server response 200
- the last number 00 after 200 inside the message body

请注意,这些都不适用于我:

grok { match => { "message" => "%{COMBINEDAPACHELOG}" } } 

grok { match => [ "message", "%{COMBINEDAPACHELOG}" ] }

4 个答案:

答案 0 :(得分:18)

使用Grok Debugger获取日志格式的完全匹配。这是唯一的方法。

http://grokdebug.herokuapp.com/

答案 1 :(得分:17)

grok {
  match => [ "message", "%{IP:client_ip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:apache_timestamp}\] \"%{WORD:method} /%{NOTSPACE:request_page} HTTP/%{NUMBER:http_version}\" %{NUMBER:server_response} " ]
}

答案 2 :(得分:0)

使用以下内容:

filter {
    grok {
            match => { "message" => "%{COMMONAPACHELOG}" }
    }
}

正如您从模式中看到的那样COMBINEDAPACHELOG会失败,因为有一些缺失的组件:

COMBINEDAPACHELOG %{COMMONAPACHELOG} %{QS:referrer} %{QS:agent}

https://github.com/elastic/logstash/blob/v1.4.2/patterns/grok-patterns

答案 3 :(得分:0)

您可以使用COMBINEDAPACHELOG模式,

%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})" %{NUMBER:response} (?:%{NUMBER:bytes}|-) %{QS:referrer} %{QS:agent}
例如,请考虑此示例apache日志

  

111.222.333.123 HOME - [01 / Feb / 1998:01:08:46 -0800]" GET /bannerad/ad.htm HTTP / 1.0" 200 28083   " http://www.referrer.com/bannerad/ba_intro.htm" " Mozilla的/ 4.01   (Macintosh; I; PPC)"

上面的过滤器会产生,

{
  "clientip": [
    [
      "111.222.333.123"
    ]
  ],
  "HOSTNAME": [
    [
      "111.222.333.123"
    ]
  ],
  "IP": [
    [
      null
    ]
  ],
  "IPV6": [
    [
      null
    ]
  ],
  "IPV4": [
    [
      null
    ]
  ],
  "ident": [
    [
      "HOME"
    ]
  ],
  "USERNAME": [
    [
      "HOME",
      "-"
    ]
  ],
  "auth": [
    [
      "-"
    ]
  ],
  "timestamp": [
    [
      "01/Feb/1998:01:08:46 -0800"
    ]
  ],
  "MONTHDAY": [
    [
      "01"
    ]
  ],
  "MONTH": [
    [
      "Feb"
    ]
  ],
  "YEAR": [
    [
      "1998"
    ]
  ],
  "TIME": [
    [
      "01:08:46"
    ]
  ],
  "HOUR": [
    [
      "01"
    ]
  ],
  "MINUTE": [
    [
      "08"
    ]
  ],
  "SECOND": [
    [
      "46"
    ]
  ],
  "INT": [
    [
      "-0800"
    ]
  ],
  "verb": [
    [
      "GET"
    ]
  ],
  "request": [
    [
      "/bannerad/ad.htm"
    ]
  ],
  "httpversion": [
    [
      "1.0"
    ]
  ],
  "BASE10NUM": [
    [
      "1.0",
      "200",
      "28083"
    ]
  ],
  "rawrequest": [
    [
      null
    ]
  ],
  "response": [
    [
      "200"
    ]
  ],
  "bytes": [
    [
      "28083"
    ]
  ],
  "referrer": [
    [
      ""http://www.referrer.com/bannerad/ba_intro.htm""
    ]
  ],
  "QUOTEDSTRING": [
    [
      ""http://www.referrer.com/bannerad/ba_intro.htm"",
      ""Mozilla/4.01 (Macintosh; I; PPC)""
    ]
  ],
  "agent": [
    [
      ""Mozilla/4.01 (Macintosh; I; PPC)""
    ]
  ]
}

可以在这里测试,

https://grokdebug.herokuapp.com/