8

我一直在四处寻找,但找不到有效的解决方案。我尝试在 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}" ] }.

我要过滤:

- 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

4 回答 4

19

使用 Grok 调试器获得与您的日志格式完全匹配的信息。这是唯一的方法。

http://grokdebug.herokuapp.com/

于 2014-03-12T16:43:06.113 回答
19
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} " ]
}
于 2014-03-13T13:58:12.543 回答
4

使用以下内容:

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

从您的模式中可以看出,COMBINEDAPACHELOG由于缺少一些组件,因此会失败:

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

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

于 2017-04-04T13:17:37.107 回答
2

您可以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 主页 - [01/Feb/199​​8: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/

于 2018-05-02T06:36:01.030 回答