简体   繁体   English

将JSON文件导入Logstash + Elasticsearch + Kibana

[英]Import JSON Files into Logstash + Elasticsearch + Kibana

So, I have a web platform that prints a JSON file per request containing some log data about that request. 所以,我有一个Web平台,每个请求打印一个JSON文件,其中包含有关该请求的一些日志数据。 I can configure several rules about when should it log stuff, only at certain levels, etc... 我可以配置几个关于何时记录内容的规则,仅限于某些级别等...

Now, I've been toying with the Logstash + Elasticsearch + Kibana3 stack, and I'd love to find a way to see those logs in Kibana. 现在,我一直在玩Logstash + Elasticsearch + Kibana3堆栈,我很想找到一种在Kibana中查看这些日志的方法。 My question is, is there a way to make Logstash import these kind of files, or would I have to write a custom input plugin for it? 我的问题是,有没有办法让Logstash导入这些类型的文件,还是我必须为它编写自定义输入插件? I've searched around and for what I've seen, plugins are written in Ruby, a language I don't have experience with. 我一直在寻找和看到我所看到的,插件是用Ruby编写的,这是一种我没有经验的语言。

Logstash is a very good tool for processing dynamic files. Logstash是处理动态文件的非常好的工具。

Here is the way to import your json file into elasticsearch using logstash: 以下是使用logstash将json文件导入elasticsearch的方法:

configuration file: 配置文件:

input 
{
    file 
    {
        path => ["/path/to/json/file"]
        start_position => "beginning"
        sincedb_path => "/dev/null"
        exclude => "*.gz"
    }
}

filter 
{
    mutate
    {
        replace => [ "message", "%{message}" ]
        gsub => [ 'message','\n','']
    }
    if [message] =~ /^{.*}$/
    {
        json { source => message }
    }

}

output
{ 
  elasticsearch {
    protocol => "http"
    codec => json
    host => "localhost"
    index => "json"
    embedded => true
  }

    stdout { codec => rubydebug }
}

example of json file: json文件的示例:

{"foo":"bar", "bar": "foo"}
{"hello":"world", "goodnight": "moon"}

Note the json need to be in one line. 请注意,json需要在一行中。 if you want to parse a multiline json file, replace relevant fields in your configuration file: 如果要解析多行json文件,请替换配置文件中的相关字段:

   input 
{   
    file 
    {
        codec => multiline
        {
            pattern => '^\{'
            negate => true
            what => previous                
        }
        path => ["/opt/mount/ELK/json/*.json"]
        start_position => "beginning"
        sincedb_path => "/dev/null"
        exclude => "*.gz"
    }
}

filter 
{
    mutate
    {
        replace => [ "message", "%{message}}" ]
        gsub => [ 'message','\n','']
    }
    if [message] =~ /^{.*}$/ 
    {
        json { source => message }
    }

}

Logstash is just a tool for converting various kinds of syslog files into JSON and loading them into elasticsearch (or graphite, or... ). Logstash只是一种工具,用于将各种syslog文件转换为JSON并将它们加载到elasticsearch(或graphite,或......)中。

Since your files are already in JSON, you don't need logstash. 由于您的文件已经是JSON,因此您不需要logstash。 You can upload them directly into elasticsearch using curl. 您可以使用curl将它们直接上传到elasticsearch。

See Import/Index a JSON file into Elasticsearch 请参阅将JSON文件导入/索引到Elasticsearch中

However, in order to work well with Kibana, your JSON files need to be at a minimum. 但是,为了与Kibana良好配合,您的JSON文件需要最少。

  1. Flat - Kibana does not grok nested JSON structs. Flat - Kibana不会嵌套嵌套的JSON结构。 You need a simple hash of key/value pairs. 您需要一个简单的键/值对散列。

  2. Have a identifiable timestamp. 有一个可识别的时间戳。

What I would suggest is looking the JSON files logstash outputs and seeing if you can massage your JSON files to match that structure. 我建议的是查看JSON文件的logstash输出,看看是否可以按摩你的JSON文件以匹配该结构。 You can do this in any language you like that supports JSON. 您可以使用任何支持JSON的语言执行此操作。 The program jq is very handy for filtering json from one format to another. 程序jq非常便于将json从一种格式过滤到另一种格式。

Logstash format - https://gist.github.com/jordansissel/2996677 Logstash格式 - https://gist.github.com/jordansissel/2996677

jq - http://stedolan.github.io/jq/ jq - http://stedolan.github.io/jq/

Logstash can import different formats and sources as it provides a lot of plugins. Logstash可以导入不同的格式和源,因为它提供了大量的插件。 There are also other log collector and forwarder tools that can send logs to logstash such as nxlog , rsyslog, syslog-ng, flume, kafka, fluentd, etc. From what I've heard most people use nxlog on windows (though it works on linux equally well) in combination with the ELK stack because of its low resource footprint. 还有其他日志收集器和转发器工具可以将日志发送到logstash,例如nxlogrsyslog ,syslog-ng,flume,kafka,fluentd等。据我所知,大多数人在Windows上使用nxlog(虽然它可以工作同样好的linux)与ELK堆栈相结合,因为它的资源占用少。 (Disclaimer: I'm affiliated with the project) (免责声明:我隶属于该项目)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM