|
| 1 | +# encoding: utf-8 |
| 2 | +require "logstash/filters/base" |
| 3 | +require "logstash/namespace" |
| 4 | + |
| 5 | +require "logstash-filter-ip2proxy_jars" |
| 6 | + |
| 7 | +class LogStash::Filters::IP2Proxy < LogStash::Filters::Base |
| 8 | + config_name "ip2proxy" |
| 9 | + |
| 10 | + # The path to the IP2Proxy.BIN database file which Logstash should use. |
| 11 | + # If not specified, this will default to the IP2PROXY-LITE-PX1.BIN database that embedded in the plugin. |
| 12 | + config :database, :validate => :path |
| 13 | + |
| 14 | + # The field containing the IP address. |
| 15 | + # If this field is an array, only the first value will be used. |
| 16 | + config :source, :validate => :string, :required => true |
| 17 | + |
| 18 | + # The field used to define iplocation as target. |
| 19 | + config :target, :validate => :string, :default => 'ip2proxy' |
| 20 | + |
| 21 | + # The field used to allow user to enable the use of cache. |
| 22 | + config :use_cache, :validate => :boolean, :default => true |
| 23 | + |
| 24 | + # The field used to allow user to enable the use of memory mapped file. |
| 25 | + config :use_memory_mapped, :validate => :boolean, :default => false |
| 26 | + |
| 27 | + # The field used to allow user to hide unsupported fields. |
| 28 | + config :hide_unsupported_fields, :validate => :boolean, :default => false |
| 29 | + |
| 30 | + # The field used to define the size of the cache. It is not required and the default value is 10 000 |
| 31 | + config :cache_size, :validate => :number, :required => false, :default => 10_000 |
| 32 | + |
| 33 | + public |
| 34 | + def register |
| 35 | + if @database.nil? |
| 36 | + @database = ::Dir.glob(::File.join(::File.expand_path("../../../vendor/", ::File.dirname(__FILE__)),"IP2PROXY-LITE-PX1.BIN")).first |
| 37 | + |
| 38 | + if @database.nil? || !File.exists?(@database) |
| 39 | + raise "You must specify 'database => ...' in your ip2proxy filter (I looked for '#{@database}')" |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + @logger.info("Using ip2proxy database", :path => @database) |
| 44 | + |
| 45 | + @ip2proxyfilter = org.logstash.filters.IP2ProxyFilter.new(@source, @target, @database, @use_memory_mapped, @hide_unsupported_fields) |
| 46 | + end |
| 47 | + |
| 48 | + public |
| 49 | + def filter(event) |
| 50 | + ip = event.get(@source) |
| 51 | + |
| 52 | + return unless filter?(event) |
| 53 | + if @use_cache |
| 54 | + if value = IP2ProxyCache.find(event, ip, @ip2proxyfilter, @cache_size).get('ip2proxy') |
| 55 | + event.set('ip2proxy', value) |
| 56 | + filter_matched(event) |
| 57 | + else |
| 58 | + tag_iplookup_unsuccessful(event) |
| 59 | + end |
| 60 | + else |
| 61 | + if @ip2proxyfilter.handleEvent(event) |
| 62 | + filter_matched(event) |
| 63 | + else |
| 64 | + tag_iplookup_unsuccessful(event) |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + def tag_iplookup_unsuccessful(event) |
| 70 | + @logger.debug? && @logger.debug("IP #{event.get(@source)} was not found in the database", :event => event) |
| 71 | + end |
| 72 | + |
| 73 | +end # class LogStash::Filters::IP2Proxy |
| 74 | + |
| 75 | +class IP2ProxyOrderedHash |
| 76 | + ONE = 1 |
| 77 | + |
| 78 | + attr_reader :times_queried # ip -> times queried |
| 79 | + attr_reader :hash |
| 80 | + |
| 81 | + def initialize |
| 82 | + @times_queried = Hash.new(0) # ip -> times queried |
| 83 | + @hash = {} # number of hits -> array of ips |
| 84 | + end |
| 85 | + |
| 86 | + def add(key) |
| 87 | + hash[ONE] ||= [] |
| 88 | + hash[ONE] << key |
| 89 | + times_queried[key] = ONE |
| 90 | + end |
| 91 | + |
| 92 | + def reorder(key) |
| 93 | + number_of_queries = times_queried[key] |
| 94 | + |
| 95 | + hash[number_of_queries].delete(key) |
| 96 | + hash.delete(number_of_queries) if hash[number_of_queries].empty? |
| 97 | + |
| 98 | + hash[number_of_queries + 1] ||= [] |
| 99 | + hash[number_of_queries + 1] << key |
| 100 | + end |
| 101 | + |
| 102 | + def increment(key) |
| 103 | + add(key) unless times_queried.has_key?(key) |
| 104 | + reorder(key) |
| 105 | + times_queried[key] += 1 |
| 106 | + end |
| 107 | + |
| 108 | + def delete_least_used |
| 109 | + first_pile_with_something.shift.tap { |key| times_queried.delete(key) } |
| 110 | + end |
| 111 | + |
| 112 | + def first_pile_with_something |
| 113 | + hash[hash.keys.min] |
| 114 | + end |
| 115 | +end |
| 116 | + |
| 117 | +class IP2ProxyCache |
| 118 | + ONE_DAY_IN_SECONDS = 86_400 |
| 119 | + |
| 120 | + @cache = {} # ip -> event |
| 121 | + @timestamps = {} # ip -> time of caching |
| 122 | + @times_queried = IP2ProxyOrderedHash.new # ip -> times queried |
| 123 | + @mutex = Mutex.new |
| 124 | + |
| 125 | + class << self |
| 126 | + attr_reader :cache |
| 127 | + attr_reader :timestamps |
| 128 | + attr_reader :times_queried |
| 129 | + |
| 130 | + def find(event, ip, filter, cache_size) |
| 131 | + synchronize do |
| 132 | + if cache.has_key?(ip) |
| 133 | + refresh_event(event, ip, filter) if too_old?(ip) |
| 134 | + else |
| 135 | + if cache_full?(cache_size) |
| 136 | + make_room |
| 137 | + end |
| 138 | + cache_event(event, ip, filter) |
| 139 | + end |
| 140 | + times_queried.increment(ip) |
| 141 | + cache[ip] |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + def too_old?(ip) |
| 146 | + timestamps[ip] < Time.now - ONE_DAY_IN_SECONDS |
| 147 | + end |
| 148 | + |
| 149 | + def make_room |
| 150 | + key = times_queried.delete_least_used |
| 151 | + cache.delete(key) |
| 152 | + timestamps.delete(key) |
| 153 | + end |
| 154 | + |
| 155 | + def cache_full?(cache_size) |
| 156 | + cache.size >= cache_size |
| 157 | + end |
| 158 | + |
| 159 | + def cache_event(event, ip, filter) |
| 160 | + filter.handleEvent(event) |
| 161 | + cache[ip] = event |
| 162 | + timestamps[ip] = Time.now |
| 163 | + end |
| 164 | + |
| 165 | + def synchronize(&block) |
| 166 | + @mutex.synchronize(&block) |
| 167 | + end |
| 168 | + |
| 169 | + alias_method :refresh_event, :cache_event |
| 170 | + end |
| 171 | +end |
0 commit comments