Class: Fluent::GeoIPFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_geoip.rb

Constant Summary collapse

DEFAULT_DOWNLOAD_URL =
'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz'
DEFAULT_MD5_URL =
'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5'
DEFAULT_DATABASE_PATH =
'./geoip/database/GeoLite2-City.mmdb'
DEFAULT_MD5_PATH =
'./geoip/database/GeoLite2-City.md5'
DEFAULT_LOOKUP_FIELD =
'ip'
DEFAULT_FIELD_PREFIX =
'geoip'
DEFAULT_FIELD_DELIMITER =
'_'
DEFAULT_FLATTEN =
false
DEFAULT_CITY =
true
DEFAULT_CONTINENT =
true
DEFAULT_COUNTRY =
true
DEFAULT_LOCATION =
true
DEFAULT_POSTAL =
true
DEFAULT_REGISTERED_COUNTRY =
true
DEFAULT_REPRESENTED_COUNTRY =
true
DEFAULT_SUBDIVISIONS =
true
DEFAULT_TRAITS =
true
DEFAULT_CONNECTION_TYPE =
true

Instance Method Summary collapse

Constructor Details

#initializeGeoIPFilter

Returns a new instance of GeoIPFilter.



85
86
87
# File 'lib/fluent/plugin/filter_geoip.rb', line 85

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fluent/plugin/filter_geoip.rb', line 89

def configure(conf)
  super

  @download_url = conf.has_key?('download_url') ? conf['download_url'] : DEFAULT_DOWNLOAD_URL

  @md5_url = conf.has_key?('md5_url') ? conf['md5_url'] : DEFAULT_MD5_URL

  @database_path = conf.has_key?('database_path') ? conf['database_path'] : DEFAULT_DATABASE_PATH

  @md5_path = conf.has_key?('md5_path') ? conf['md5_path'] : DEFAULT_MD5_PATH

  @lookup_field = conf.has_key?('lookup_field') ? conf['lookup_field'] : DEFAULT_LOOKUP_FIELD

  @field_prefix = conf.has_key?('field_prefix') ? conf['field_prefix'] : DEFAULT_FIELD_PREFIX

  @field_delimiter = conf.has_key?('field_delimiter') ? conf['field_delimiter'] : DEFAULT_FIELD_DELIMITER

  @flatten = conf.has_key?('flatten') ? to_boolean(conf['flatten']) : DEFAULT_FLATTEN

  @continent = conf.has_key?('continent') ? to_boolean(conf['continent']) : DEFAULT_CONTINENT

  @country = conf.has_key?('country') ? to_boolean(conf['country']) : DEFAULT_COUNTRY

  @city = conf.has_key?('city') ? to_boolean(conf['city']) : DEFAULT_CITY

  @location = conf.has_key?('location') ? to_boolean(conf['location']) : DEFAULT_LOCATION

  @postal = conf.has_key?('postal') ? to_boolean(conf['postal']) : DEFAULT_POSTAL

  @registered_country = conf.has_key?('registered_country') ? to_boolean(conf['registered_country']) : DEFAULT_REGISTERED_COUNTRY

  @represented_country = conf.has_key?('represented_country') ? to_boolean(conf['represented_country']) : DEFAULT_REPRESENTED_COUNTRY

  @subdivisions = conf.has_key?('subdivisions') ? to_boolean(conf['subdivisions']) : DEFAULT_SUBDIVISIONS

  @traits = conf.has_key?('traits') ? to_boolean(conf['traits']) : DEFAULT_TRAITS

  @connection_type = conf.has_key?('connection_type') ? to_boolean(conf['connection_type']) : DEFAULT_CONNECTION_TYPE

  download_database @download_url, @md5_url, @database_path, @md5_path

  @database = MaxMindDB.new(@database_path)
end

#download_database(download_url, md5_url, database_path, md5_path) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/fluent/plugin/filter_geoip.rb', line 263

def download_database(download_url, md5_url, database_path, md5_path)
  # database directory
  database_dir = File.dirname database_path
  md5_dir = File.dirname md5_path

  # create database directory if directory does not exist.
  FileUtils.mkdir_p(database_dir) unless File.exist?(database_dir)
  FileUtils.mkdir_p(md5_dir) unless File.exist?(md5_dir)

  # create empty md5 file if file does not exist.
  File.open(md5_path, 'wb').close() unless File.exist?(md5_path)

  # read saved md5
  current_md5 = nil
  begin
    open(md5_path, 'rb') do |data|
      current_md5 = data.read
    end
    log.info "Current MD5: %s" % current_md5
  rescue => e
    log.warn e.message
  end

  # fetch md5
  fetched_md5 = nil
  begin
    open(md5_url, 'rb') do |data|
      fetched_md5 = data.read
    end
    log.info "Fetched MD5: %s" % fetched_md5
  rescue => e
    log.warn e.message
  end

  # check md5
  unless current_md5 == fetched_md5 then
    # download new database
    download_path = database_dir + '/' + File.basename(download_url)
    begin
      log.info "Download: %s" % download_url
      open(download_path, 'wb') do |output|
        open(download_url, 'rb') do |data|
          output.write(data.read)
        end
      end
      log.info "Download done: %s" % download_path
    rescue => e
      log.warn e.message
    end

    # unzip new database temporaly
    tmp_database_path = database_dir + '/tmp_' + File.basename(database_path)
    begin
      log.info "Unzip: %s" % download_path
      open(tmp_database_path, 'wb') do |output|
        Zlib::GzipReader.open(download_path) do |gz|
          output.write(gz.read)
        end
      end
      log.info "Unzip done: %s" % tmp_database_path
    rescue => e
      puts e.message
    end

    # check mkd5
    temp_md5 = Digest::MD5.hexdigest(File.open(tmp_database_path, 'rb').read)
    log.info "New MD5: %s" % temp_md5
    if fetched_md5 == temp_md5 then
      log.info "Rename: %s to %s" % [tmp_database_path, database_path]
      FileUtils.mv(tmp_database_path, database_path)
      log.info "Rename done: %s to %s" % [tmp_database_path, database_path]

      # record new md5
      log.info "Save: %s" % md5_path
      File.write(md5_path, fetched_md5)
      log.info "Save done: %s" % md5_path
    else
      log.info "MD5 missmatch: Fetched MD5 (%s) != New MD5 (%s) ; " % [fetched_md5, temp_md5]
    end
  end
end

#filter(tag, time, record) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/fluent/plugin/filter_geoip.rb', line 133

def filter(tag, time, record)
  ip = record[@lookup_field]

  unless ip.nil? then
    geoip = @database.lookup(ip)

    if geoip.found? then
      geoip_hash = geoip.to_hash

      if @continent && geoip_hash.has_key?('continent') then
        if @flatten then
          record.merge!(to_flatten(geoip_hash['continent'], [@field_prefix, 'continent'], @field_delimiter))
        else
          record[[@field_prefix, 'continent'].join(@field_delimiter)] = geoip_hash['continent'].to_json
        end
      end

      if @country && geoip_hash.has_key?('country') then
        if @flatten then
          record.merge!(to_flatten(geoip_hash['country'], [@field_prefix, 'country'], @field_delimiter))
        else
          record[[@field_prefix, 'country'].join(@field_delimiter)] = geoip_hash['country'].to_json
        end
      end

      if @city && geoip_hash.has_key?('city') then
        if @flatten then
          record.merge!(to_flatten(geoip_hash['city'], [@field_prefix, 'city'], @field_delimiter))
        else
          record[[@field_prefix, 'city'].join(@field_delimiter)] = geoip_hash['city'].to_json
        end
      end

      if @location && geoip_hash.has_key?('location') then
        if @flatten then
          record.merge!(to_flatten(geoip_hash['location'], [@field_prefix, 'location'], @field_delimiter))
        else
          record[[@field_prefix, 'location'].join(@field_delimiter)] = geoip_hash['location'].to_json
        end
      end

      if @postal && geoip_hash.has_key?('postal') then
        if @flatten then
          record.merge!(to_flatten(geoip_hash['postal'], [@field_prefix, 'postal'], @field_delimiter))
        else
          record[[@field_prefix, 'postal'].join(@field_delimiter)] = geoip_hash['postal'].to_json
        end
      end

      if @registered_country && geoip_hash.has_key?('registered_country') then
        if @flatten then
          record.merge!(to_flatten(geoip_hash['registered_country'], [@field_prefix, 'registered_country'], @field_delimiter))
        else
          record[[@field_prefix, 'registered_country'].join(@field_delimiter)] = geoip_hash['registered_country'].to_json
        end
      end

      if @represented_country && geoip_hash.has_key?('represented_country') then
        if @flatten then
          record.merge!(to_flatten(geoip_hash['represented_country'], [@field_prefix, 'represented_country'], @field_delimiter))
        else
          record[[@field_prefix, 'represented_country'].join(@field_delimiter)] = geoip_hash['represented_country'].to_json
        end
      end

      if @subdivisions && geoip_hash.has_key?('subdivisions') then
        if @flatten then
          i = 0
          geoip_hash['subdivisions'].each do |subdivision|
            record.merge!(to_flatten(subdivision, [@field_prefix, 'subdivisions', i.to_s], @field_delimiter))
            i = i + 1
          end
        else
          record[[@field_prefix, 'subdivisions'].join(@field_delimiter)] = geoip_hash['subdivisions'].to_json
        end
      end

      if @traits && geoip_hash.has_key?('traits') then
        if @flatten then
          record.merge!(to_flatten(geoip_hash['traits'], [@field_prefix, 'traits'], @field_delimiter))
        else
          record[[@field_prefix, 'traits'].join(@field_delimiter)] = geoip_hash['traits'].to_json
        end
      end

      if @connection_type && geoip_hash.has_key?('connection_type') then
        if @flatten then
          record.merge!(to_flatten(geoip_hash['connection_type'], [@field_prefix, 'connection_type'], @field_delimiter))
        else
          record[[@field_prefix, 'connection_type'].join(@field_delimiter)] = geoip_hash['connection_type'].to_json
        end
      end

      log.info "Record: %s" % record.inspect
    else
      log.warn "It was not possible to look up the #{ip}."
    end
  end

  return record
end

#to_boolean(string) ⇒ Object



253
254
255
256
257
258
259
260
261
# File 'lib/fluent/plugin/filter_geoip.rb', line 253

def to_boolean(string)
  if string== true || string =~ (/(true|t|yes|y|1)$/i) then
    return true
  elsif string== false || string.nil? || string =~ (/(false|f|no|n|0)$/i)
    return false
  else
    return false
  end
end

#to_flatten(hash, stack = [], delimiter = '/') ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/fluent/plugin/filter_geoip.rb', line 235

def to_flatten(hash, stack=[], delimiter='/')
  output = {}

  hash.keys.each do |key|
    stack.push key

    if hash[key].instance_of?(Hash) then
      output.merge!(to_flatten(hash[key], stack, delimiter))
    else
      output[stack.join(delimiter)] = hash[key]
    end

    stack.pop
  end

  return output
end