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



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/logstash/filters/ip2location.rb', line 44

def filter(event)
  json = JSON.parse(event.to_json)
  ip = json["clientip"]

  return unless filter?(event)
  if value = Cache.find(event, ip, @ip2locationfilter, @cache_size).get('ip2location')
    event.set('ip2location', value)
    filter_matched(event)
  else
    tag_iplookup_unsuccessful(event)
  end
end

#registerObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/logstash/filters/ip2location.rb', line 29

def register
  if @database.nil?
    @database = ::Dir.glob(::File.join(::File.expand_path("../../../vendor/", ::File.dirname(__FILE__)),"IP2LOCATION-LITE-DB3.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)

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

#tag_iplookup_unsuccessful(event) ⇒ Object



57
58
59
# File 'lib/logstash/filters/ip2location.rb', line 57

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