113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
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
|
# File 'lib/fluent/plugin/filter_geo.rb', line 113
def filter(tag, time, record)
ip = record[@lookup_field]
unless ip.nil? then
geoip = @database.lookup(ip)
if geoip.found? then
if @continent then
continent_hash = {}
unless geoip.continent.code.nil? then
continent_hash['code'] = geoip.continent.code
end
if @geohidden == true then
unless geoip.continent.geoname_id.nil? then
continent_hash['geoname_id'] = geoip.continent.geoname_id
end
end
unless geoip.continent.iso_code.nil? then
continent_hash['iso_code'] = geoip.continent.iso_code
end
unless geoip.continent.name(@locale).nil? then
continent_hash['name'] = geoip.continent.name(@locale)
end
unless continent_hash.empty? then
if @flatten then
record.merge!(to_flatten(continent_hash, [@output_field, 'continent'], @field_delimiter))
else
record[[@output_field, 'continent'].join(@field_delimiter)] = continent_hash.to_json
end
end
end
if @country then
country_hash = {}
unless geoip.country.code.nil? then
country_hash['code'] = geoip.country.code
end
if @geohidden == true then
unless geoip.country.geoname_id.nil? then
country_hash['geoname_id'] = geoip.country.geoname_id
end
end
unless geoip.country.iso_code.nil? then
country_hash['iso_code'] = geoip.country.iso_code
end
unless geoip.country.name(@locale).nil? then
country_hash['name'] = geoip.country.name(@locale)
end
unless country_hash.empty? then
if @flatten then
record.merge!(to_flatten(country_hash, [@output_field, 'country'], @field_delimiter))
else
record[[@output_field, 'country'].join(@field_delimiter)] = country_hash.to_json
end
end
end
if @city then
city_hash = {}
unless geoip.city.code.nil? then
city_hash['code'] = geoip.city.code
end
if @geohidden == true then
unless geoip.city.geoname_id.nil? then
city_hash['geoname_id'] = geoip.city.geoname_id
end
end
unless geoip.city.iso_code.nil? then
city_hash['iso_code'] = geoip.city.iso_code
end
unless geoip.city.name(@locale).nil? then
city_hash['name'] = geoip.city.name(@locale)
end
unless city_hash.empty? then
if @flatten then
record.merge!(to_flatten(city_hash, [@output_field, 'city'], @field_delimiter))
else
record[[@output_field, 'city'].join(@field_delimiter)] = city_hash.to_json
end
end
end
if @location then
location_hash = {}
unless geoip.location.latitude.nil? | geoip.location.longitude.nil? then
location_hash['location'] = geoip.location.longitude, geoip.location.latitude
end
if @geohidden == true then
unless geoip.location.metro_code.nil? then
location_hash['metro_code'] = geoip.location.metro_code
end
end
unless geoip.location.time_zone.nil? then
location_hash['time_zone'] = geoip.location.time_zone
end
unless location_hash.empty? then
if @flatten then
record.merge!(to_flatten(location_hash, [@output_field, 'location'], @field_delimiter))
else
record[[@output_field, 'location'].join(@field_delimiter)] = location_hash.to_json
end
end
end
if @postal then
postal_hash = {}
unless geoip.postal.code.nil? then
postal_hash['code'] = geoip.postal.code
end
unless postal_hash.empty? then
if @flatten then
record.merge!(to_flatten(postal_hash, [@output_field, 'postal'], @field_delimiter))
else
record[[@output_field, 'postal'].join(@field_delimiter)] = postal_hash.to_json
end
end
end
if @registered_country then
registered_country_hash = {}
unless geoip.registered_country.code.nil? then
registered_country_hash['code'] = geoip.registered_country.code
end
if @geohidden == true then
unless geoip.registered_country.geoname_id.nil? then
registered_country_hash['geoname_id'] = geoip.registered_country.geoname_id
end
end
unless geoip.registered_country.iso_code.nil? then
registered_country_hash['iso_code'] = geoip.registered_country.iso_code
end
unless geoip.registered_country.name(@locale).nil? then
registered_country_hash['name'] = geoip.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, 'registered_country'].join(@field_delimiter)] = registered_country_hash.to_json
end
end
end
if @represented_country then
represented_country_hash = {}
unless geoip.represented_country.code.nil? then
represented_country_hash['code'] = geoip.represented_country.code
end
if @geohidden == true then
unless geoip.represented_country.geoname_id.nil? then
represented_country_hash['geoname_id'] = geoip.represented_country.geoname_id
end
end
unless geoip.represented_country.iso_code.nil? then
represented_country_hash['iso_code'] = geoip.represented_country.iso_code
end
unless geoip.represented_country.name(@locale).nil? then
represented_country_hash['name'] = geoip.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, 'represented_country'].join(@field_delimiter)] = represented_country_hash.to_json
end
end
end
if @subdivisions then
subdivision_arry = []
i = 0
geoip.subdivisions.each do |subdivision|
subdivision_hash = {}
unless subdivision.code.nil? then
subdivision_hash['code'] = subdivision.code
end
if @geohidden == true then
unless subdivision.geoname_id.nil? then
subdivision_hash['geoname_id'] = subdivision.geoname_id
end
end
unless subdivision.iso_code.nil? then
subdivision_hash['iso_code'] = subdivision.iso_code
end
unless subdivision.name(@locale).nil? then
subdivision_hash['name'] = 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
i = 0
subdivision_arry.each do |subdivision|
record.merge!(to_flatten(subdivision, [@output_field, 'subdivisions', i.to_s], @field_delimiter))
i = i + 1
end
else
record[[@output_field, 'subdivisions'].join(@field_delimiter)] = subdivision_arry.to_json
end
end
end
if @traits then
traits_hash = {}
unless geoip.traits.is_anonymous_proxy.nil? then
traits_hash['is_anonymous_proxy'] = geoip.traits.is_anonymous_proxy
end
unless geoip.traits.is_satellite_provider.nil? then
traits_hash['is_satellite_provider'] = geoip.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, 'traits'].join(@field_delimiter)] = traits_hash.to_json
end
end
end
if @connection_type then
unless geoip.connection_type.nil? then
record[[@output_field, 'connection_type'].join(@field_delimiter)] = geoip.connection_type
end
end
else
log.warn "It was not possible to look up the #{ip}."
end
end
return record
end
|