fluent-plugin-geoip-filter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69bba19629edca4fda5e3e8fab24f2373a3421e5
4
+ data.tar.gz: 5252200643db4bb6ec7262d64805f8f0c9b56b93
5
+ SHA512:
6
+ metadata.gz: c589ab0d76dab39b6a2f4b64141e683540d835ad3ded57410e65d6daf0e4d478e464c0f79cc0bf18695ae26593a8fd7f95c479a8ee269cf78040e31c606c0946
7
+ data.tar.gz: 4d63b2023b783a1175a5b97bd0c8f91c13c7b9673fe9742338dd9a3d1372e76c92f0b946ad60f2ca93ef004a254dbf19891d30b6fa84eecdddf6d70c8f3567d1
@@ -0,0 +1 @@
1
+ *.dat filter=lfs diff=lfs merge=lfs -text
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.2
5
+ - 2.0
6
+ - 1.9.3
7
+
8
+ before_script:
9
+ - wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
10
+ - gunzip GeoLiteCity.dat.gz
11
+ - mv GeoLiteCity.dat data/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-geoip-filter.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2015 Yuri UMEZAKI
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,102 @@
1
+ # fluent-plugin-geoip-filter
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/fluent-plugin-geoip-filter.svg)](https://badge.fury.io/rb/fluent-plugin-geoip-filter)
4
+ [![Build Status](https://travis-ci.org/bungoume/fluent-plugin-geoip-filter.svg?branch=master)](https://travis-ci.org/bungoume/fluent-plugin-geoip-filter)
5
+ [![Dependency Status](https://gemnasium.com/bungoume/fluent-plugin-geoip-filter.svg)](https://gemnasium.com/bungoume/fluent-plugin-geoip-filter)
6
+
7
+ [Fluentd](http://fluentd.org) filter plugin to add geoip.
8
+
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ # for fluentd
14
+ $ gem install fluent-plugin-geoip-filter
15
+
16
+ # for td-agent2
17
+ $ sudo td-agent-gem install fluent-plugin-geoip-filter
18
+ ```
19
+
20
+
21
+ ## Usage
22
+
23
+ ### Example 1:
24
+
25
+ ```xml
26
+ <filter access.nginx.**>
27
+ @type geoip
28
+ </filter>
29
+ ```
30
+
31
+ Assuming following inputs are coming:
32
+
33
+ ```json
34
+ access.nginx: {
35
+ "client_ip":"93.184.216.34",
36
+ "scheme":"http", "method":"GET", "host":"example.com",
37
+ "path":"/", "query":"-", "req_bytes":200, "referer":"-",
38
+ "status":200, "res_bytes":800, "res_body_bytes":600, "taken_time":0.001, "user_agent":"Mozilla/5.0"
39
+ }
40
+ ```
41
+
42
+ then output bocomes as belows:
43
+
44
+ ```json
45
+ access.nginx: {
46
+ "client_ip":"93.184.216.34",
47
+ "scheme":"http", "method":"GET", "host":"example.com",
48
+ "path":"/", "query":"-", "req_bytes":200, "referer":"-",
49
+ "status":200, "res_bytes":800, "res_body_bytes":600, "taken_time":0.001, "user_agent":"Mozilla/5.0",
50
+ "geo": {
51
+ "coordinates": [-70.8228, 42.150800000000004],
52
+ "country_code": "US",
53
+ "city": "Norwell",
54
+ "region_code": "MA",
55
+ }
56
+ }
57
+ ```
58
+
59
+
60
+ ## Parameters
61
+ - key_name *field_key*
62
+
63
+ Target key name. default client_ip.
64
+
65
+ - out_key *string*
66
+
67
+ Output prefix key name. default geo.
68
+
69
+ - database_path *file_path*
70
+
71
+ Database file(GeoIPCity.dat) path.
72
+ Get from [MaxMind](http://dev.maxmind.com/geoip/legacy/geolite/)
73
+
74
+ - flatten *bool*
75
+ join hashed data by '_'. default false.
76
+
77
+
78
+ ## TODO
79
+
80
+ * patches welcome!
81
+
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it ( https://github.com/bungoume/fluent-plugin-geoip-filter/fork )
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create a new Pull Request
90
+
91
+
92
+ ## Copyright
93
+
94
+ Copyright (c) 2015 Yuri Umezaki
95
+
96
+
97
+ ## License
98
+
99
+ [Apache License, Version 2.0.](http://www.apache.org/licenses/LICENSE-2.0)
100
+
101
+ This product includes GeoLite data created by MaxMind, available from
102
+ [http://www.maxmind.com](http://www.maxmind.com).
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ Rake::TestTask.new(:test) do |test|
4
+ test.libs << 'lib' << 'test'
5
+ test.pattern = 'test/**/test_*.rb'
6
+ test.verbose = true
7
+ end
8
+
9
+ task :default => :test
Binary file
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "fluent-plugin-geoip-filter"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["Yuri Umezaki"]
9
+ spec.email = ["bungoume@gmail.com"]
10
+ spec.homepage = "https://github.com/bungoume/fluent-plugin-geoip-filter"
11
+ spec.summary = "Fluentd filter plugin to add geoip"
12
+ spec.description = spec.summary
13
+ spec.license = "Apache-2.0"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "fluentd", ">= 0.12"
21
+ spec.add_runtime_dependency "geoip", ">= 1.5.0"
22
+ spec.add_runtime_dependency "lru_redux", ">= 1.0.0"
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake"
25
+ if defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
26
+ spec.add_development_dependency "test-unit"
27
+ end
28
+ end
@@ -0,0 +1,69 @@
1
+ require 'geoip'
2
+ require 'lru_redux'
3
+
4
+ module Fluent
5
+ class GeoipFilter < Filter
6
+ Plugin.register_filter('geoip', self)
7
+
8
+ def initialize
9
+ @geoip_cache = LruRedux::Cache.new(10000)
10
+ super
11
+ end
12
+
13
+ config_param :database_path, :string, :default => File.dirname(__FILE__) + '/../../../data/GeoLiteCity.dat'
14
+ config_param :key_name, :string, :default => 'client_ip'
15
+ config_param :out_key, :string, :default => 'geo'
16
+ config_param :flatten, :bool, :default => false
17
+
18
+ def configure(conf)
19
+ super
20
+ begin
21
+ @geoip = GeoIP.new(@database_path)
22
+ rescue => e
23
+ @geoip = GeoIP.new
24
+ log.warn "Failed to configure parser. Use default pattern.", :error_class => e.class, :error => e.message
25
+ log.warn_backtrace
26
+ end
27
+ end
28
+
29
+ def filter(tag, time, record)
30
+ ip_addr = record[@key_name]
31
+ unless ip_addr.nil?
32
+ geo_ip = @geoip_cache.getset(ip_addr) { get_geoip(ip_addr) }
33
+ if flatten
34
+ record.merge! hash_flatten(geo_ip, [@out_key])
35
+ else
36
+ record[@out_key] = geo_ip
37
+ end
38
+ end
39
+ record
40
+ end
41
+
42
+ private
43
+
44
+ def get_geoip(ip_addr)
45
+ geo_ip = @geoip.city(ip_addr)
46
+ data = {}
47
+ return data if geo_ip.nil?
48
+ data["coordinates"] = [geo_ip["longitude"], geo_ip["latitude"]]
49
+ data["country_code"] = geo_ip["country_code2"]
50
+ data["city"] = geo_ip["city_name"]
51
+ data["region_code"] = geo_ip["region_name"]
52
+ data
53
+ end
54
+
55
+ def hash_flatten(a, keys=[])
56
+ ret = {}
57
+ a.each{|k,v|
58
+ ks = keys + [k]
59
+ if v.class == Hash
60
+ ret.merge!(hash_flatten(v, ks))
61
+ else
62
+ ret.merge!({ks.join('_')=> v})
63
+ end
64
+ }
65
+ ret
66
+ end
67
+
68
+ end if defined?(Filter) # Support only >= v0.12
69
+ end
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+ require 'fluent/test'
15
+ unless ENV.has_key?('VERBOSE')
16
+ nulllogger = Object.new
17
+ nulllogger.instance_eval {|obj|
18
+ def method_missing(method, *args)
19
+ # pass
20
+ end
21
+ }
22
+ $log = nulllogger
23
+ end
24
+
25
+ require 'fluent/plugin/filter_geoip'
26
+
27
+ class Test::Unit::TestCase
28
+ end
@@ -0,0 +1,63 @@
1
+ require 'helper'
2
+
3
+ class GeoipFilterTest < Test::Unit::TestCase
4
+ def setup
5
+ Fluent::Test.setup
6
+ end
7
+
8
+ CONFIG = %[
9
+ type geoip
10
+ key_name client_ip
11
+ out_key geo
12
+ ]
13
+
14
+ def create_driver(conf=CONFIG,tag='test')
15
+ Fluent::Test::FilterTestDriver.new(Fluent::GeoipFilter, tag).configure(conf)
16
+ end
17
+
18
+ def test_configure
19
+ d = create_driver(CONFIG)
20
+ assert_equal 'client_ip', d.instance.config['key_name']
21
+ assert_equal 'geo', d.instance.config['out_key']
22
+ end
23
+
24
+ def test_emit
25
+ d1 = create_driver(CONFIG)
26
+ ip_iddr = '93.184.216.34'
27
+
28
+ d1.run do
29
+ d1.emit({'client_ip' => ip_iddr})
30
+ end
31
+ emits = d1.emits
32
+ assert_equal 1, emits.length
33
+ assert_equal 'test', emits[0][0] # tag
34
+ geo_object = emits[0][2]['geo']
35
+ assert_equal [-70.8228, 42.150800000000004], geo_object['coordinates']
36
+ assert_equal 'US', geo_object['country_code']
37
+ assert_equal 'Norwell', geo_object['city']
38
+ assert_equal 'MA', geo_object['region_code']
39
+ end
40
+
41
+ def test_emit_flatten
42
+ d1 = d1 = create_driver(%[
43
+ type geoip
44
+ key_name ip_iddr
45
+ flatten
46
+ ], 'test')
47
+ ip_iddr = '93.184.216.34'
48
+
49
+ d1.run do
50
+ d1.emit({'ip_iddr' => ip_iddr})
51
+ end
52
+
53
+ emits = d1.emits
54
+ assert_equal 1, emits.length
55
+ assert_equal 'test', emits[0][0] # tag
56
+ geo_object = emits[0][2]
57
+ assert_equal [-70.8228, 42.150800000000004], geo_object['geo_coordinates']
58
+ assert_equal 'US', geo_object['geo_country_code']
59
+ assert_equal 'Norwell', geo_object['geo_city']
60
+ assert_equal 'MA', geo_object['geo_region_code']
61
+ end
62
+
63
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-geoip-filter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuri Umezaki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fluentd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.12'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: geoip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: lru_redux
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: test-unit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Fluentd filter plugin to add geoip
98
+ email:
99
+ - bungoume@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitattributes"
105
+ - ".gitignore"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - data/GeoLiteCity.dat
112
+ - fluent-plugin-geoip-filter.gemspec
113
+ - lib/fluent/plugin/filter_geoip.rb
114
+ - test/helper.rb
115
+ - test/plugin/test_filter_geoip.rb
116
+ homepage: https://github.com/bungoume/fluent-plugin-geoip-filter
117
+ licenses:
118
+ - Apache-2.0
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.4.5.1
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Fluentd filter plugin to add geoip
140
+ test_files:
141
+ - test/helper.rb
142
+ - test/plugin/test_filter_geoip.rb