Class: LogStash::Filters::IP2Location

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

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/logstash/filters/ip2location.rb', line 64

def filter(event)
  ip = event.get(@source)

  return unless filter?(event)
  if @lookup_type == "ws"
    if @ip2locationfilter.handleEvent(event)
      filter_matched(event)
    else
      tag_iplookup_unsuccessful(event)
    end
  else
    if @use_cache
      if value = IP2LocationCache.find(event, ip, @ip2locationfilter, @cache_size).get('ip2location')
        event.set('ip2location', value)
        filter_matched(event)
      else
        tag_iplookup_unsuccessful(event)
      end
    else
      if @ip2locationfilter.handleEvent(event)
        filter_matched(event)
      else
        tag_iplookup_unsuccessful(event)
      end
    end
  end
end

#registerObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/logstash/filters/ip2location.rb', line 43

def register
  if @lookup_type == "ws"
    @logger.info("Using IP2Location.io API")
    if @api_key == ""
      raise "An IP2Location.io API key is required. You may sign up for a free API key at https://www.ip2location.io/pricing."
    end
  else
    if @database.nil?
      @database = ::Dir.glob(::File.join(::File.expand_path("../../../vendor/", ::File.dirname(__FILE__)),"IP2LOCATION-LITE-DB1.IPV6.BIN")).first

      if @database.nil? || !File.exists?(@database)
        raise "You must specify 'database => ...' in your ip2location filter (I looked for '#{@database}')"
      end
    end
    @logger.info("Using ip2location database", :path => @database)
  end

  @ip2locationfilter = org.logstash.filters.IP2LocationFilter.new(@source, @target, @database, @use_memory_mapped, @hide_unsupported_fields, @lookup_type, @api_key)
end

#tag_iplookup_unsuccessful(event) ⇒ Object



92
93
94
# File 'lib/logstash/filters/ip2location.rb', line 92

def tag_iplookup_unsuccessful(event)
  @logger.debug? && @logger.debug("IP #{event.get(@source)} was not found in the database", :event => event)
end