读取 Logstash 日志但未推送到 elasticsearch

Logstash logs are read but are not pushed to elasticsearch

我有以下 logstash 配置:

input {
  file {
    codec => "json_lines"
    path => ["/etc/logstash/input.log"]
    sincedb_path => "/etc/logstash/dbfile"
    start_position => "beginning"
    ignore_older => "0"
  }
}
output {
   elasticsearch {
      hosts => ["192.168.169.46:9200"]
   }
   stdout {
      codec => rubydebug
   }
}

/etc/logstash/input.log 文件中填充了来自 运行 java 应用程序的日志。日志采用以下 json 格式(它们以内联形式写入,由 \n 字符分隔):

{
"exception": {
    "exception_class": "java.lang.RuntimeException",
    "exception_message": "Test runtime exception stack: 0",
    "stacktrace": "java.lang.RuntimeException: Test runtime exception stack: 0"
},
"@version": 1,
"source_host": "WS-169-046",
"message": "Test runtime exception stack: 0",
"thread_name": "parallel-1",
"@timestamp": "2019-12-02T16:30:14.084+02:00",
"level": "ERROR",
"logger_name": "nl.hnf.logs.aggregator.demo.LoggingTest",
"aplication-name": "demo-log-aggregation"
}

我还使用 elasticsearch API(将请求正文放在:http://192.168.169.46:9200/_template/logstash?pretty)更新了 logstash 默认模板:

{
"index_patterns": "logstash-*",
"version": 60002,
"settings": {
    "index.refresh_interval": "5s",
    "number_of_shards": 1
},
"mappings": {
    "dynamic_templates": [
        {
            "message_field": {
                "path_match": "message",
                "match_mapping_type": "string",
                "mapping": {
                    "type": "text",
                    "norms": false
                }
            }
        },
        {
            "string_fields": {
                "match": "*",
                "match_mapping_type": "string",
                "mapping": {
                    "type": "text",
                    "norms": false,
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    ],
    "properties": {
        "@timestamp": {
            "type": "date"
        },
        "@version": {
            "type": "keyword"
        },
        "source_host": {
            "type": "keyword"
        },
        "message": {
            "type": "text"
        },
        "thread_name": {
            "type": "text"
        },
        "level": {
            "type": "keyword"
        },
        "logger_name": {
            "type": "keyword"
        },
        "aplication_name": {
            "type": "keyword"
        },
        "exception": {
            "dynamic": true,
            "properties": {
                "exception_class": {
                    "type": "text"
                },
                "exception_message": {
                    "type": "text"
                },
                "stacktrace": {
                    "type": "text"
                }
            }
        }
    }
}

Elasticsearch 响应 "acknowledged": true,我可以看到模板正在通过 API 更新。 现在以 debug 日志级别启动 logstash 我看到正在读取输入日志但未发送到 elasticsearch,虽然索引已创建但它始终为空(0 个文档):

[2019-12-03T09:30:51,655][DEBUG][logstash.inputs.file     ][custom] Received line {:path=>"/etc/logstash/input.log", :text=>"{\"@version\":1,\"source_host\":\"ubuntu\",\"message\":\"Generating some logs: 65778 - 2019-12-03T09:30:50.775\",\"thread_name\":\"parallel-1\",\"@timestamp\":\"2019-12-03T09:30:50.775+00:00\",\"level\":\"INFO\",\"logger_name\":\"nl.hnf.logs.aggregator.demo.LoggingTest\",\"aplication-name\":\"demo-log-aggregation\"}"}
[2019-12-03T09:30:51,656][DEBUG][filewatch.sincedbcollection][custom] writing sincedb (delta since last write = 1575365451)

此外,elasticsearch 日志也在 debug 级别,但我没有看到任何错误或任何可以给我提示问题根源的东西。

你们有什么想法或建议,为什么日志没有被推送到elasticsearch?

在 filebeat 中,将 ignore_older 设置为零意味着 "do not check how old the file is"。对于 logstash 文件输入,它表示 "ignore files more than zero seconds old",实际上是 "ignore everything"。删除它。如果这没有帮助,则增加日志级别以进行跟踪,并查看 filewatch 模块对它正在监视的文件的描述。

通过使用 json 编解码器而不是 json_lines 并删除 start_positionignore_oldersincedb_path

解决了问题
input {
  file {
    codec => "json"
    path => ["/etc/logstash/input.log"]
  }
}
output {
   elasticsearch {
      hosts => ["192.168.169.46:9200"]
   }
   stdout {
      codec => rubydebug
   }
}

此外,json_lines 编解码器似乎与文件输入不兼容(\n 分隔符未按预期工作)