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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
# File 'lib/fluent/plugin/filter_geoip2.rb', line 138
def filter(tag, time, record)
ip = record[@lookup_field]
unless ip.nil? then
geoip_city = {}
geoip_asn = {}
begin
geoip_city = @database_city.lookup(ip)
geoip_asn = @database_asn.lookup(ip)
rescue IPAddr::InvalidAddressError => e
return record
end
if geoip_city.found? then
unless @flatten then
record.merge!({@output_field => {}})
end
if @continent then
continent_hash = {}
unless geoip_city.continent.code.nil? then
continent_hash['continent_code'] = geoip_city.continent.code
end
unless geoip_city.continent.geoname_id.nil? then
continent_hash['continent_geoname_id'] = geoip_city.continent.geoname_id
end
unless geoip_city.continent.iso_code.nil? then
continent_hash['continent_iso_code'] = geoip_city.continent.iso_code
end
unless geoip_city.continent.name(@locale).nil? then
continent_hash['continent_name'] = geoip_city.continent.name(@locale)
end
unless continent_hash.empty? then
if @flatten then
record.merge!(to_flatten(continent_hash, [@output_field], @field_delimiter))
else
record[@output_field].merge!(continent_hash)
end
end
end
if @country then
country_hash = {}
unless geoip_city.country.code.nil? then
country_hash['country_code'] = geoip_city.country.code
end
unless geoip_city.country.geoname_id.nil? then
country_hash['country_geoname_id'] = geoip_city.country.geoname_id
end
unless geoip_city.country.iso_code.nil? then
country_hash['country'] = geoip_city.country.iso_code
end
unless geoip_city.country.name(@locale).nil? then
country_hash['country_name'] = geoip_city.country.name(@locale)
end
unless country_hash.empty? then
if @flatten then
record.merge!(to_flatten(country_hash, [@output_field], @field_delimiter))
else
record[@output_field].merge!(country_hash)
end
end
end
if @city then
city_hash = {}
unless geoip_city.city.code.nil? then
city_hash['city_code'] = geoip_city.city.code
end
unless geoip_city.city.geoname_id.nil? then
city_hash['city_geoname_id'] = geoip_city.city.geoname_id
end
unless geoip_city.city.iso_code.nil? then
city_hash['city_iso_code'] = geoip_city.city.iso_code
end
unless geoip_city.city.name(@locale).nil? then
city_hash['city'] = geoip_city.city.name(@locale)
end
unless city_hash.empty? then
if @flatten then
record.merge!(to_flatten(city_hash, [@output_field], @field_delimiter))
else
record[@output_field].merge!(city_hash)
end
end
end
if @location then
location_hash = {}
unless geoip_city.location.latitude.nil? or geoip_city.location.longitude.nil? then
location_hash['location'] = { "lat" => geoip_city.location.latitude, "lon" => geoip_city.location.longitude}
end
unless geoip_city.location.latitude.nil? then
location_hash['latitude'] = geoip_city.location.latitude
end
unless geoip_city.location.longitude.nil? then
location_hash['longitude'] = geoip_city.location.longitude
end
unless geoip_city.location.metro_code.nil? then
location_hash['metro_code'] = geoip_city.location.metro_code
end
unless geoip_city.location.time_zone.nil? then
location_hash['time_zone'] = geoip_city.location.time_zone
end
unless location_hash.empty? then
if @flatten then
record.merge!(to_flatten(location_hash, [@output_field], @field_delimiter))
else
record[@output_field].merge!(location_hash)
end
end
end
if @postal then
postal_hash = {}
unless geoip_city.postal.code.nil? then
postal_hash['postal_code'] = geoip_city.postal.code
end
unless postal_hash.empty? then
if @flatten then
record.merge!(to_flatten(postal_hash, [@output_field], @field_delimiter))
else
record[@output_field].merge!(postal_hash)
end
end
end
if @registered_country then
registered_country_hash = {}
unless geoip_city.registered_country.code.nil? then
registered_country_hash['code'] = geoip_city.registered_country.code
end
unless geoip_city.registered_country.geoname_id.nil? then
registered_country_hash['geoname_id'] = geoip_city.registered_country.geoname_id
end
unless geoip_city.registered_country.iso_code.nil? then
registered_country_hash['iso_code'] = geoip_city.registered_country.iso_code
end
unless geoip_city.registered_country.name(@locale).nil? then
registered_country_hash['name'] = geoip_city.registered_country.name(@locale)
end
unless registered_country_hash.empty? then
if @flatten then
record.merge!(to_flatten(registered_country_hash, [@output_field, 'registered_country'], @field_delimiter))
else
record[@output_field].merge!({'registered_country' => registered_country_hash})
end
end
end
if @represented_country then
represented_country_hash = {}
unless geoip_city.represented_country.code.nil? then
represented_country_hash['code'] = geoip_city.represented_country.code
end
unless geoip_city.represented_country.geoname_id.nil? then
represented_country_hash['geoname_id'] = geoip_city.represented_country.geoname_id
end
unless geoip_city.represented_country.iso_code.nil? then
represented_country_hash['iso_code'] = geoip_city.represented_country.iso_code
end
unless geoip_city.represented_country.name(@locale).nil? then
represented_country_hash['name'] = geoip_city.represented_country.name(@locale)
end
unless represented_country_hash.empty? then
if @flatten then
record.merge!(to_flatten(represented_country_hash, [@output_field, 'represented_country'], @field_delimiter))
else
record[@output_field].merge!({'represented_country' => represented_country_hash})
end
end
end
if @subdivisions then
subdivision_arry = []
i = 0
geoip_city.subdivisions.each do |subdivision|
subdivision_hash = {}
unless subdivision.code.nil? then
subdivision_hash['code_%d' % [i]] = subdivision.code
end
unless subdivision.geoname_id.nil? then
subdivision_hash['geoname_id_%d' % [i]] = subdivision.geoname_id
end
unless subdivision.iso_code.nil? then
subdivision_hash['iso_code_%d' % [i]] = subdivision.iso_code
end
unless subdivision.name(@locale).nil? then
subdivision_hash['name_%d' % [i]] = subdivision.name(@locale)
end
unless subdivision_hash.empty? then
subdivision_arry.push(subdivision_hash)
end
i = i + 1
end
unless subdivision_arry.empty? then
if @flatten then
subdivision_arry.each do |subdivision|
record.merge!(to_flatten(subdivision, [@output_field, 'subdivisions'], @field_delimiter))
end
else
record[@output_field].merge!({'subdivisions' => subdivision_arry})
end
end
end
if @traits then
traits_hash = {}
unless geoip_city.traits.is_anonymous_proxy.nil? then
traits_hash['is_anonymous_proxy'] = geoip_city.traits.is_anonymous_proxy
end
unless geoip_city.traits.is_satellite_provider.nil? then
traits_hash['is_satellite_provider'] = geoip_city.traits.is_satellite_provider
end
unless traits_hash.empty? then
if @flatten then
record.merge!(to_flatten(traits_hash, [@output_field, 'traits'], @field_delimiter))
else
record[@output_field].merge!({'traits' => traits_hash})
end
end
end
if @connection_type then
unless geoip_city.connection_type.nil? then
if @flatten then
record.merge!(to_flatten(geoip_city.connection_type, [@output_field, 'connection_type'], @field_delimiter))
else
record[@output_field].merge!({'connection_type' => geoip_city.connection_type})
end
end
end
if geoip_asn.found? then
if @autonomous_system then
autonomous_system_hash = {}
autonomous_system_hash['number'] = geoip_asn['autonomous_system_number']
autonomous_system_hash['organization'] = geoip_asn['autonomous_system_organization']
unless autonomous_system_hash.empty? then
if @flatten then
record.merge!(to_flatten(autonomous_system_hash, [@output_field, 'as'], @field_delimiter))
else
record[@output_field].merge!({'as' => autonomous_system_hash})
end
end
end
end
log.debug "Record: %s" % record.inspect
else
log.debug "It was not possible to look up the #{ip}."
end
end
return record
end
|