Logstash,将来自 xml 文件的事件拆分为多个文档,保留根标签中的信息 [英] Logstash, split event from an xml file in multiples documents keeping information from root tags

查看:30
本文介绍了Logstash,将来自 xml 文件的事件拆分为多个文档,保留根标签中的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:我有 XML 文件,其中包含我想使用 Logstash 解析的事件,然后使用 Kibana 请求它.我想在每个事件中保留来自 ROOT 标签的所有信息.

My problem: I have XML files that contain events I want to parse using Logstash to request it using Kibana after. I want to keep all the information from the ROOT tag in each event.

输入看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<ROOT number="34">
  <EVENTLIST>
    <EVENT name="hey"/>
    <EVENT name="you"/>
  </EVENTLIST>
</ROOT>

我想要的是这样的两个文件:

What I want, two documents like that:

{
  "number":"34"
  "name": "Hey"
}
{
  "number":"34"
  "name": "you"
}

Logstash 配置:

Logstash conf:

input {
  stdin { }
}
filter {
  xml {
    store_xml => "false"
    source => "message"
    target => "EVENT"
    xpath => [
      "/ROOT/@number","number",
      "/ROOT/EVENTLIST/EVENT/@name","name"
    ]
  }
}
output { elasticsearch { host => localhost } stdout { codec => rubydebug } }

没用.我得到了什么:

{
  "number" : ["34"]
  "name":["hey,"you""]
}

我遵循了这篇文章的解决方案:https://serverfault.com/questions/615196/logstash-parsing-xml-document- contains-multiple-log-entries

I followed the solution of this post : https://serverfault.com/questions/615196/logstash-parsing-xml-document-containing-multiple-log-entries

但我的问题仍然存在,我丢失了根标签中的信息.

But my problem remains, I lose information from root tag.

解决方案之一可能是使用一些 ruby​​ 过滤器来处理它,但我不知道 ruby​​.另一种是使用一些java编程将XML转换成JSON,然后再发送到elasticsearch...

One of solution could be to use some ruby filter to handle that, but I don't know ruby. Another is to use some java programing to convert XML into JSON before sending it to elasticsearch...

有什么想法可以解决这个问题,还是我必须学习 ruby​​?

Any ideas to handle that or do I have to learn ruby?

推荐答案

如果你的结构像你展示的一样简单,你可以使用我写的 memorize 插件.

If your structure is as simple as you show, you can use a memorize plugin that I wrote.

您的配置如下所示:

filter {
  if ([message] =~ /<ROOT/) {
    grok {
      match => [ "message", 
        'number="(?<number>d+)" number2="(?<number1>d+)"'
      ] 
    }
  } else if ([message] =~ /<EVENT /) {
    grok { 
      match => [ "message", 'name="(?<name>[^"]+)"']
    }
  }
  memorize {
    fields => ["number","number1"]
  }
  if ([message] !~ /<EVENT /) {
    drop {}
  } else {
    mutate { remove_field => ["message"] }
  }
}

我的示例显示了根据您在下面的评论在 ROOT 元素中查找多个内容.这是支持记忆多个字段的插件版本:

My example shows looking for multiple things in the ROOT element based on your comments below. And here's the version of the plugin that supports memorizing multiple fields:

# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"
require "set"
#
# This filter will look for fields from an event and record the last value
# of them.  If any are not present, their last value will be added to the
# event
#
# The config looks like this:
#
#     filter {
#       memorize {
#         fields => ["time"]
#         default => { "time" => "00:00:00.000" }
#       }
#     }
#
# The `fields` is an array of the field NAMES that you want to memorize
# The `default` is a map of field names to field values that you want
# to use if the field isn't present and has no memorized value (optional)

class LogStash::Filters::Memorize < LogStash::Filters::Base

  config_name "memorize"
  milestone 2

  # An array of the field names to to memorize
  config :fields, :validate => :array, :required => true
  # a map for default values to use if its not seen before we need it
  config :default, :validate => :hash, :required => false

  # The stream identity is how the filter determines which stream an
  # event belongs to. See the multiline plugin if you want more details on how
  # this might work
  config :stream_identity , :validate => :string, :default => "%{host}.%{path}.%{type}"

  public
  def initialize(config = {})
    super

    @threadsafe = false

    # This filter needs to keep state.
    @memorized = Hash.new
  end # def initialize

  public
  def register
    # nothing needed
  end # def register

  public
  def filter(event)
    return unless filter?(event)

    any = false
    @fields.each do |field|
      if event[field].nil?
    map = @memorized[@stream_identity]
        val = map.nil? ? nil : map[field]
        if val.nil?
          val = @default.nil? ? nil : @default[field]
        end
    if !val.nil?
          event[field] = val
          any = true
    end
      else
        map = @memorized[@stream_identity]
    if map.nil?
          map = @memorized[@stream_identity] = Hash.new
    end
    val = event[field]
    map[field] = event[field]
      end #if
      if any
        filter_matched(event)
      end
    end #field.each
  end
end

对于logstash 1.5 及更高版本,此插件可通过以下方式安装

For logstash 1.5 and later, this plugin is available for installation via

bin/plugin install logstash-filter-memorize

这篇关于Logstash,将来自 xml 文件的事件拆分为多个文档,保留根标签中的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆