fluent-plugin-geoip 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -36,29 +36,18 @@ $ sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-geoip
36
36
 
37
37
  ## Usage
38
38
 
39
- ```
39
+ ```xml
40
40
  <match access.apache>
41
41
  type geoip
42
42
 
43
- # buffering time (default: 60s)
44
- flush_interval 1s
45
-
46
- # Queued chunks are flushed at shutdown process.
47
- flush_at_shutdown yes
48
-
49
- # tag settings
50
- remove_tag_prefix access.
51
- add_tag_prefix geoip.
52
- include_tag_key false
53
-
54
- # specify geoip lookup field (default: host)
43
+ # Specify geoip lookup field (default: host)
55
44
  # in the case of accessing nested value, delimit keys by dot like 'host.ip'.
56
45
  geoip_lookup_key host
57
46
 
58
- # specify geoip database (using bundled GeoLiteCity databse by default)
47
+ # Specify geoip database (using bundled GeoLiteCity databse by default)
59
48
  geoip_database 'data/GeoLiteCity.dat'
60
49
 
61
- # record settings (enable more than one keys are required.)
50
+ # Set adding field of geolocate results (more than one settings are required.)
62
51
  enable_key_city geoip_city
63
52
  enable_key_latitude geoip_lat
64
53
  enable_key_longitude geoip_lon
@@ -68,6 +57,36 @@ $ sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-geoip
68
57
  enable_key_dma_code geoip_dma
69
58
  enable_key_area_code geoip_area
70
59
  enable_key_region geoip_region
60
+
61
+ # Setting for tag
62
+ remove_tag_prefix access.
63
+ add_tag_prefix geoip.
64
+ include_tag_key false
65
+
66
+ # Buffering time (default: 60s)
67
+ flush_interval 1s
68
+ </match>
69
+ ```
70
+
71
+ #### Tips: how to geolocate multiple key
72
+
73
+ ```xml
74
+ <match access.apache>
75
+ type geoip
76
+
77
+ # Set ip address key to geolocate
78
+ geoip_lookup_key user1_host, user2_host
79
+
80
+ # Set adding field of geolocate results
81
+ enable_key_city user1_city, user2_city
82
+ enable_key_country_name user1_country, user2_country
83
+
84
+ # Setting for tag
85
+ remove_tag_prefix access.
86
+ add_tag_prefix geoip.
87
+
88
+ # Buffering time
89
+ flush_interval 1s
71
90
  </match>
72
91
  ```
73
92
 
@@ -75,7 +94,7 @@ $ sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-geoip
75
94
 
76
95
  #### configuration
77
96
 
78
- ```
97
+ ```xml
79
98
  <source>
80
99
  type forward
81
100
  </source>
@@ -104,7 +123,7 @@ $ sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-geoip
104
123
 
105
124
  #### result
106
125
 
107
- ```
126
+ ```bash
108
127
  # forward record with Google's ip address.
109
128
  $ echo '{"host":"66.102.9.80","message":"test"}' | fluent-cat test.geoip
110
129
 
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "fluent-plugin-geoip"
7
- spec.version = "0.0.5"
7
+ spec.version = "0.0.6"
8
8
  spec.authors = ["Kentaro Yoshida"]
9
9
  spec.email = ["y.ken.studio@gmail.com"]
10
10
  spec.summary = %q{Fluentd Output plugin to add information about geographical location of IP addresses with Maxmind GeoIP databases.}
@@ -8,6 +8,7 @@ class Fluent::GeoipOutput < Fluent::BufferedOutput
8
8
  include Fluent::HandleTagNameMixin
9
9
  include Fluent::SetTagKeyMixin
10
10
  config_set_default :include_tag_key, false
11
+ attr_reader :geoip_keys_map
11
12
 
12
13
  def initialize
13
14
  require 'geoip'
@@ -21,11 +22,19 @@ class Fluent::GeoipOutput < Fluent::BufferedOutput
21
22
  conf.keys.select{|k| k =~ /^enable_key_/}.each do |key|
22
23
  geoip_key_name = key.sub('enable_key_','')
23
24
  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
+ @geoip_keys_map.store(geoip_key_name, conf[key].split(/\s*,\s*/))
25
26
  end
26
27
 
27
- @geoip_lookup_key = @geoip_lookup_key.split(".")
28
-
28
+ @geoip_lookup_key = @geoip_lookup_key.split(/\s*,\s*/).map {|lookupkey|
29
+ lookupkey.split(".")
30
+ }
31
+ if @geoip_lookup_key.size > 1
32
+ @geoip_keys_map.each{|name, key|
33
+ if key.size != @geoip_lookup_key.size
34
+ raise Fluent::ConfigError, "geoip: lookup key length is not match #{name}"
35
+ end
36
+ }
37
+ end
29
38
 
30
39
  if ( !@remove_tag_prefix && !@remove_tag_suffix && !@add_tag_prefix && !@add_tag_suffix )
31
40
  raise Fluent::ConfigError, "geoip: missing remove_tag_prefix, remove_tag_suffix, add_tag_prefix or add_tag_suffix."
@@ -53,21 +62,25 @@ class Fluent::GeoipOutput < Fluent::BufferedOutput
53
62
  end
54
63
 
55
64
  def get_address(record)
56
- obj = record
57
- @geoip_lookup_key.each do |key|
58
- return nil if not obj.has_key?(key)
59
- obj = obj[key]
60
- end
61
- obj
65
+ @geoip_lookup_key.map {|key|
66
+ obj = record
67
+ key.each {|k|
68
+ break obj = nil if not obj.has_key?(k)
69
+ obj = obj[k]
70
+ }
71
+ obj
72
+ }
62
73
  end
63
74
 
64
75
  def add_geoip_field(record)
65
- address = get_address(record)
66
- return record if address.nil?
67
- result = @geoip.look_up(address)
68
- return record if result.nil?
69
- @geoip_keys_map.each do |geoip_key,record_key|
70
- record.store(record_key, result[geoip_key.to_sym])
76
+ addresses = get_address(record)
77
+ return record if addresses.all? {|address| address == nil }
78
+ results = addresses.map {|address| @geoip.look_up(address) }
79
+ return record if results.all? {|result| result == nil }
80
+ @geoip_keys_map.each do |geoip_key,record_keys|
81
+ record_keys.each_with_index {|record_key, idx|
82
+ record.store(record_key, results[idx][geoip_key.to_sym])
83
+ }
71
84
  end
72
85
  return record
73
86
  end
@@ -28,8 +28,29 @@ class GeoipOutputTest < Test::Unit::TestCase
28
28
  remove_tag_prefix input.
29
29
  add_tag_prefix geoip.
30
30
  ]
31
- puts d.instance.inspect
32
31
  assert_equal 'geoip_city', d.instance.config['enable_key_city']
32
+ assert_equal ['geoip_city'], d.instance.geoip_keys_map['city']
33
+
34
+ # multiple key config
35
+ d = create_driver %[
36
+ geoip_lookup_key from.ip, to.ip
37
+ enable_key_city from_city, to_city
38
+ remove_tag_prefix input.
39
+ add_tag_prefix geoip.
40
+ ]
41
+ assert_equal 'from_city, to_city', d.instance.config['enable_key_city']
42
+ assert_equal ['from_city', 'to_city'], d.instance.geoip_keys_map['city']
43
+
44
+ # multiple key config (bad configure)
45
+ assert_raise(Fluent::ConfigError) {
46
+ d = create_driver %[
47
+ geoip_lookup_key from.ip, to.ip
48
+ enable_key_city from_city
49
+ enable_key_region from_region
50
+ remove_tag_prefix input.
51
+ add_tag_prefix geoip.
52
+ ]
53
+ }
33
54
  end
34
55
 
35
56
  def test_emit
@@ -40,10 +61,8 @@ class GeoipOutputTest < Test::Unit::TestCase
40
61
  end
41
62
  emits = d1.emits
42
63
  assert_equal 2, emits.length
43
- # p emits[0]
44
64
  assert_equal 'geoip.access', emits[0][0] # tag
45
65
  assert_equal 'Mountain View', emits[0][2]['geoip_city']
46
- # p emits[1]
47
66
  assert_equal nil, emits[1][2]['geoip_city']
48
67
  end
49
68
 
@@ -60,10 +79,8 @@ class GeoipOutputTest < Test::Unit::TestCase
60
79
  end
61
80
  emits = d1.emits
62
81
  assert_equal 2, emits.length
63
- p emits[0]
64
82
  assert_equal 'geoip.access', emits[0][0] # tag
65
83
  assert_equal 'Mountain View', emits[0][2]['geoip_city']
66
- p emits[1]
67
84
  assert_equal nil, emits[1][2]['geoip_city']
68
85
  end
69
86
 
@@ -76,12 +93,53 @@ class GeoipOutputTest < Test::Unit::TestCase
76
93
  end
77
94
  emits = d1.emits
78
95
  assert_equal 2, emits.length
79
- # p emits[0]
80
96
  assert_equal 'geoip.access', emits[0][0] # tag
81
97
  assert_equal nil, emits[0][2]['geoip_city']
82
- # p emits[1]
83
98
  assert_equal 'geoip.access', emits[1][0] # tag
84
99
  assert_equal nil, emits[1][2]['geoip_city']
85
100
  end
86
101
 
102
+ def test_emit_multiple_key
103
+ d1 = create_driver(%[
104
+ geoip_lookup_key from.ip, to.ip
105
+ enable_key_city from_city, to_city
106
+ remove_tag_prefix input.
107
+ add_tag_prefix geoip.
108
+ ], 'input.access')
109
+ d1.run do
110
+ d1.emit({'from' => {'ip' => '66.102.3.80'}, 'to' => {'ip' => '125.54.95.42'}})
111
+ d1.emit({'message' => 'missing field'})
112
+ end
113
+ emits = d1.emits
114
+ assert_equal 2, emits.length
115
+ assert_equal 'geoip.access', emits[0][0] # tag
116
+ assert_equal 'Mountain View', emits[0][2]['from_city']
117
+ assert_equal 'Musashino', emits[0][2]['to_city']
118
+ assert_equal nil, emits[1][2]['from_city']
119
+ assert_equal nil, emits[1][2]['to_city']
120
+ end
121
+
122
+ def test_emit_multiple_key_multiple_record
123
+ d1 = create_driver(%[
124
+ geoip_lookup_key from.ip, to.ip
125
+ enable_key_city from_city, to_city
126
+ enable_key_country_name from_country, to_country
127
+ remove_tag_prefix input.
128
+ add_tag_prefix geoip.
129
+ ], 'input.access')
130
+ d1.run do
131
+ d1.emit({'from' => {'ip' => '66.102.3.80'}, 'to' => {'ip' => '125.54.95.42'}})
132
+ d1.emit({'message' => 'missing field'})
133
+ end
134
+ emits = d1.emits
135
+ assert_equal 2, emits.length
136
+ assert_equal 'geoip.access', emits[0][0] # tag
137
+ assert_equal 'Mountain View', emits[0][2]['from_city']
138
+ assert_equal 'United States', emits[0][2]['from_country']
139
+ assert_equal 'Musashino', emits[0][2]['to_city']
140
+ assert_equal 'Japan', emits[0][2]['to_country']
141
+ assert_equal nil, emits[1][2]['from_city']
142
+ assert_equal nil, emits[1][2]['to_city']
143
+ end
144
+
87
145
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-geoip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-16 00:00:00.000000000 Z
12
+ date: 2014-02-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler