Class: LogStash::Filters::ExtractNumbers

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/extractnumbers.rb

Overview

This filter automatically extracts all numbers found inside a string

This is useful when you have lines that don’t match a grok pattern or use json but you still need to extract numbers.

Each numbers is returned in a @fields.intX or @fields.floatX field where X indicates the position in the string.

The fields produced by this filter are extra useful used in combination with kibana number plotting features.

Constant Summary

Constants inherited from Base

Base::RESERVED

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#execute, #initialize, #threadsafe?

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Filters::Base

Instance Method Details

#filter(event) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/logstash/filters/extractnumbers.rb', line 28

def filter(event)
  integers = nil
  floats = nil

  msg = event[@source]

  if not msg
    return
  end

  # If for some reason the field is an array of values, take the first only.
  msg = msg.first if msg.is_a?(Array)


  fields = msg.split
  for elem in fields
    int = str_as_integer(elem)
    if int != nil
      if not integers
        integers = Array.new
      end
      integers.push(int)
      next
    end
    f = str_as_float(elem)
    if f != nil
      if not floats
        floats = Array.new
      end
      floats.push(f)
    end
  end

  if integers
    index = 0
    for i in integers
      index += 1
      event["int" + index.to_s] = i
    end
  end
  if floats
    index = 0
    for f in floats
      index += 1
      event["float" + index.to_s] = f
    end
  end
end

#registerObject



24
25
# File 'lib/logstash/filters/extractnumbers.rb', line 24

def register
end

#str_as_float(str) ⇒ Object



81
82
83
# File 'lib/logstash/filters/extractnumbers.rb', line 81

def str_as_float(str)
  Float(str) rescue nil
end

#str_as_integer(str) ⇒ Object



77
78
79
# File 'lib/logstash/filters/extractnumbers.rb', line 77

def str_as_integer(str)
  Integer(str) rescue nil
end