fluent-plugin-geoip 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ *.swp
15
+ vendor/
16
+ Gemfile.lock
17
+
18
+ # YARD artifacts
19
+ .yardoc
20
+ _yardoc
21
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-geoip.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (c) 2013- Kentaro Yoshida
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.
14
+
@@ -0,0 +1,111 @@
1
+ # fluent-plugin-geoip
2
+
3
+ Fluentd Output plugin to add information about geographical location of IP addresses with Maxmind GeoIP databases.
4
+
5
+ ## Installation
6
+
7
+ install with `gem` or `fluent-gem` command as:
8
+
9
+ ```
10
+ # for fluentd
11
+ $ gem install fluent-plugin-geoip
12
+
13
+ # for td-agent
14
+ $ sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-geoip
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```
20
+ <match access.apache>
21
+ type geoip
22
+
23
+ # buffering time
24
+ flush_interval 1s
25
+
26
+ # tag settings
27
+ remove_tag_prefix access.
28
+ add_tag_prefix geoip.
29
+ include_tag_key false
30
+
31
+ # geoip settings
32
+ geoip_lookup_key host
33
+
34
+ # record settings
35
+ enable_key_city geoip_city
36
+ enable_key_latitude geoip_lat
37
+ enable_key_longitude geoip_lon
38
+ enable_key_country_code3 geoip_country3
39
+ enable_key_country_code geoip_country
40
+ enable_key_country_name geoip_country_name
41
+ enable_key_dma_code geoip_dma
42
+ enable_key_area_code geoip_area
43
+ enable_key_region geoip_region
44
+ </match>
45
+ ```
46
+
47
+ ## Tutorial
48
+
49
+ #### configuration
50
+
51
+ ```
52
+ <source>
53
+ type forward
54
+ </source>
55
+
56
+ <match test.geoip>
57
+ type copy
58
+ <store>
59
+ type stdout
60
+ </store>
61
+ <store>
62
+ type geoip
63
+ geoip_lookup_key host
64
+ enable_key_city city
65
+ enable_key_latitude lat
66
+ enable_key_longitude lon
67
+ remove_tag_prefix test.
68
+ add_tag_prefix debug.
69
+ flush_interval 5s
70
+ </store>
71
+ </match>
72
+
73
+ <match debug.**>
74
+ type stdout
75
+ </match>
76
+ ```
77
+
78
+ #### result
79
+
80
+ ```
81
+ # forward record with Google's ip address.
82
+ $ echo '{"host":"66.102.9.80","message":"test"}' | fluent-cat test.geoip
83
+
84
+ # check the result at stdout
85
+ $ tail /var/log/td-agent/td-agent.log
86
+ 2013-08-04 16:21:32 +0900 test.geoip: {"host":"66.102.9.80","message":"test"}
87
+ 2013-08-04 16:21:32 +0900 debug.geoip: {"host":"66.102.9.80","message":"test","city":"Mountain View","lat":37.4192008972168,"lon":-122.05740356445312}
88
+ ```
89
+
90
+ ## TODO
91
+
92
+ Pull requests are very welcome!!
93
+
94
+ ## Contributing
95
+
96
+ 1. Fork it
97
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
98
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
99
+ 4. Push to the branch (`git push origin my-new-feature`)
100
+ 5. Create new Pull Request
101
+
102
+ ## Copyright
103
+
104
+ Copyright (c) 2013- Kentaro Yoshida (@yoshi_ken)
105
+
106
+ ## License
107
+
108
+ Apache License, Version 2.0
109
+
110
+ This product includes GeoLite data created by MaxMind, available from
111
+ <a href="http://www.maxmind.com">http://www.maxmind.com</a>.
@@ -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,23 @@
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"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Kentaro Yoshida"]
9
+ spec.email = ["y.ken.studio@gmail.com"]
10
+ spec.summary = %q{Fluentd Output plugin to add information about geographical location of IP addresses with Maxmind GeoIP databases.}
11
+ spec.homepage = "https://github.com/y-ken/fluent-plugin-geoip"
12
+ spec.license = "Apache License, Version 2.0"
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler"
20
+ spec.add_development_dependency "rake"
21
+ spec.add_runtime_dependency "fluentd"
22
+ spec.add_runtime_dependency "geoip-c"
23
+ end
@@ -0,0 +1,56 @@
1
+ class Fluent::GeoipOutput < Fluent::BufferedOutput
2
+ Fluent::Plugin.register_output('geoip', self)
3
+
4
+ GEOIP_KEYS = %w(city latitude longitude country_code3 country_code country_name dma_code area_code region)
5
+ config_param :geoip_database, :string, :default => 'data/GeoLiteCity.dat'
6
+ config_param :geoip_lookup_key, :string, :default => 'host'
7
+
8
+ include Fluent::HandleTagNameMixin
9
+ include Fluent::SetTagKeyMixin
10
+ config_set_default :include_tag_key, false
11
+
12
+ def initialize
13
+ require 'geoip.bundle'
14
+ super
15
+ end
16
+
17
+ def configure(conf)
18
+ super
19
+
20
+ @geoip_keys_map = Hash.new
21
+ conf.keys.select{|k| k =~ /^enable_key_/}.each do |key|
22
+ geoip_key_name = key.sub('enable_key_','')
23
+ raise Fluent::ConfigError, "geoip: unsupported key #{geoip_key_name}" unless GEOIP_KEYS.include?(geoip_key_name)
24
+ @geoip_keys_map.store(geoip_key_name, conf[key])
25
+ end
26
+
27
+ if ( !@remove_tag_prefix && !@remove_tag_suffix && !@add_tag_prefix && !@add_tag_suffix )
28
+ raise Fluent::ConfigError, "geoip: missing remove_tag_prefix, remove_tag_suffix, add_tag_prefix or add_tag_suffix."
29
+ end
30
+
31
+ @geoip = GeoIP::City.new(@geoip_database, :memory, false)
32
+ end
33
+
34
+ def start
35
+ super
36
+ end
37
+
38
+ def format(tag, time, record)
39
+ [tag, time, record].to_msgpack
40
+ end
41
+
42
+ def shutdown
43
+ super
44
+ end
45
+
46
+ def write(chunk)
47
+ chunk.msgpack_each do |tag, time, record|
48
+ result = @geoip.look_up(record[@geoip_lookup_key])
49
+ @geoip_keys_map.each do |geoip_key,record_key|
50
+ record.store(record_key, result[geoip_key.to_sym])
51
+ end
52
+ $log.info "geoip: record:#{record}, result:#{result}"
53
+ Fluent::Engine.emit(tag, time, record)
54
+ end
55
+ end
56
+ 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/out_geoip'
26
+
27
+ class Test::Unit::TestCase
28
+ end
@@ -0,0 +1,47 @@
1
+ require 'helper'
2
+
3
+ class GeoipOutputTest < Test::Unit::TestCase
4
+ def setup
5
+ Fluent::Test.setup
6
+ end
7
+
8
+ CONFIG = %[
9
+ geoip_lookup_key host
10
+ enable_key_city geoip_city
11
+ remove_tag_prefix input.
12
+ add_tag_prefix geoip.
13
+ ]
14
+
15
+ def create_driver(conf=CONFIG,tag='test')
16
+ Fluent::Test::OutputTestDriver.new(Fluent::GeoipOutput, tag).configure(conf)
17
+ end
18
+
19
+ def test_configure
20
+ assert_raise(Fluent::ConfigError) {
21
+ d = create_driver('')
22
+ }
23
+ assert_raise(Fluent::ConfigError) {
24
+ d = create_driver('enable_key_cities')
25
+ }
26
+ d = create_driver %[
27
+ enable_key_city geoip_city
28
+ remove_tag_prefix input.
29
+ add_tag_prefix geoip.
30
+ ]
31
+ puts d.instance.inspect
32
+ assert_equal 'geoip_city', d.instance.config['enable_key_city']
33
+ end
34
+
35
+ def test_emit
36
+ d1 = create_driver(CONFIG, 'input.access')
37
+ d1.run do
38
+ d1.emit({'host' => '66.102.3.80', 'message' => 'action foo'})
39
+ end
40
+ emits = d1.emits
41
+ assert_equal 1, emits.length
42
+ p emits[0]
43
+ assert_equal 'geoip.access', emits[0][0] # tag
44
+ assert_equal 'Mountain View', emits[0][2]['geoip_city']
45
+ end
46
+
47
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-geoip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kentaro Yoshida
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: fluentd
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: geoip-c
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description:
79
+ email:
80
+ - y.ken.studio@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - data/GeoLiteCity.dat
91
+ - fluent-plugin-geoip.gemspec
92
+ - lib/fluent/plugin/out_geoip.rb
93
+ - test/helper.rb
94
+ - test/plugin/test_out_geoip.rb
95
+ homepage: https://github.com/y-ken/fluent-plugin-geoip
96
+ licenses:
97
+ - Apache License, Version 2.0
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.23
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Fluentd Output plugin to add information about geographical location of IP
120
+ addresses with Maxmind GeoIP databases.
121
+ test_files:
122
+ - test/helper.rb
123
+ - test/plugin/test_out_geoip.rb