Skip to content

Instantly share code, notes, and snippets.

@yaauie
Created April 20, 2020 22:18
Show Gist options
  • Save yaauie/5ebc788ec1cc12e2674c20000e5f6d25 to your computer and use it in GitHub Desktop.
Save yaauie/5ebc788ec1cc12e2674c20000e5f6d25 to your computer and use it in GitHub Desktop.
###############################################################################
# find-field.logstash-filter-ruby.rb
# ---------------------------------
# A script for a Logstash Ruby Filter to find occurences of a field by name
# from an event given a list of places to search.
###############################################################################
#
# Copyright 2020 Ry Biesemeyer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
def register(params)
params = params.dup # isolate
@field = params.delete('field') || report_configuration_error("missing required param `field`")
@target = params.delete('target') || report_configuration_error("missing required param `target`")
@search = params.delete('search') || report_configuration_error("missing required param `search`")
@search = [@search] unless @search.kind_of?(Array)
params.empty? || report_configuration_error("unknown script parameter(s): #{params.keys}.")
end
def report_configuration_error(message)
raise LogStash::ConfigurationError, message
end
def filter(event)
found = []
@search.each do |namespace|
neeedle = namespace.empty? ? field : "[#{namespace}][#{field}]"
found << event.get(needle)
end
found.compact!
event.set(@target, found.size > 1 ? found : found.first) uness found.empty?
rescue => e
logger.error('failed to find field', exception: e.message)
event.tag('_findfielderror')
ensure
return [event]
end
@yaauie
Copy link
Author

yaauie commented Apr 20, 2020

  ruby {
    path => "${PWD}/find-field.logstash-filter-ruby.rb"
    script_params => {
      field => "foo"
      target => "result"
      search => ["", "a", "[a][b][c]"]
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment