首页 文章

Logstash nginx:字段不会被添加

提问于
浏览
0

我将设置我自己的ELK服务器来集中日志记录 . 到现在为止还挺好 .

我设置docker-compose.yml来运行ELK堆栈和另一个docker-compose.yml来运行filebeat,它会查看日志文件,添加env标签并发送到logstash . 解析应该在logstash中进行 .

filebeat.yml

name: xyz

fields:
  env: xyz

output.logstash:
  hosts: ["xyz:5044"]

filebeat.prospectors:
  - input_type: log
    fields:
      app_id: default
      type: nginx-access
    tags:
      - nginx
      - access
    paths:
      - /logs/nginx/access.log*
  - input_type: log
    fields:
      app_id: default
      type: nginx-error
    tags:
      - nginx
      - error
    paths:
      - /logs/nginx/error.log*

这是logstash.yml

input {
  beats {
    port => 5044
  }
}

filter {
  if ["nginx"] in [tags] and ["access"] in [tags] {
    grok {
      patterns_dir => "/etc/logstash/patterns"
      match => { "message" => "%{NGINXACCESS}" }
    }

    date {
      match => [ "timestamp", "dd/MMM/YYYY:HH:mm:ss Z" ]
    }
  }
}

output {
  elasticsearch {
    hosts => ["elasticsearch:9200"]
  }
  stdout { codec => rubydebug }
}

nginx模式就在这里

NGUSERNAME [a-zA-Z\.\@\-\+_%]+
NGUSER %{NGUSERNAME}
NGINXACCESS %{IPORHOST:clientip} %{NGUSER:indent} %{NGUSER:agent} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:verb} %{URIPATHPARAM:request}(?: HTTP/%{NUMBER:httpversion})?|)\" %{NUMBER:answer} (?:%{NUMBER:byte}|-) (?:\"(?:%{URI:referrer}|-))\" (?:%{QS:referree}) %{QS:agent}

我在grokconstructor.appspot.com上测试了Expression并且它命中了 .

这是一个演示线:

127.0.0.1 - - [11/May/2017:13:49:31 +0200] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586" "-"

但没有添加的字段

enter image description here

我想也许我的“如果”是错的,但我尝试了几种选择......没有任何帮助 .

任何的想法?

1 回答

  • 1

    我将首先删除if,然后逐个添加条件 . 那是从开始

    ["nginx"] in [tags]
    

    如果有效,那就进去吧

    ["nginx"] in [tags] and ["access"] in [tags]
    

    或者你可以尝试使用

    "nginx" in [tags] and "access" in [tags]
    

    UPDATE:

    要使用fields.type = nginx-access,请尝试

    "nginx-access" in [fields][type]
    

相关问题