fluent-plugin-filter-geoip 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 983e1b2b72f93c02932cb24604377948fc452e71
4
- data.tar.gz: 96b9b03b2ddd5116b50077ecf339554b128d594d
3
+ metadata.gz: 6af5d9d9d7027e164b87e91b793baad6f2feecd4
4
+ data.tar.gz: 1f82bceeab8a2a4111d245645636f704b5bbffc2
5
5
  SHA512:
6
- metadata.gz: c443452ffe44be24449954a8c41e6f2e3e17a06e0ddcea181c1c017958b1193cc15590251da3cc840eec5dc10813ae284218a58d0d5fd4fa7461d24257edcf97
7
- data.tar.gz: 1981eb0d8756276805fb46f23475da8a5ccc80dbf64ea56d2326e58f3ef2b223a2f28f69bdc7a854f9527eff37c760bc8f8b624901ff75ede3a9734e6166d72f
6
+ metadata.gz: 4796d4b9ebad60ccf9582eda74a0f63ea9f5a5d5060e39ffa479d87574a4b34ed769e3f9fd92d24449cd0e10fe4de4b7def362497c9fc0e8cdc1a3d3fad05a93
7
+ data.tar.gz: 086f1ac37d6c7bad1634a18c478671746af33d50c078a690ff2476f372c4f5f1ec92a8c7201eb214084f506f1d30de6869d015ec640b987ee1505d09d09951ad
data/README.md CHANGED
@@ -32,10 +32,16 @@ database_path /path/to/GeoLite2-City.mmdb
32
32
  lookup_field host
33
33
  ```
34
34
 
35
- ### output_field
35
+ ### field_prefix
36
36
 
37
37
  ```
38
- output_field geoip
38
+ field_prefix geoip
39
+ ```
40
+
41
+ ### field_delimiter
42
+
43
+ ```
44
+ field_delimiter _
39
45
  ```
40
46
 
41
47
  ### flatten
@@ -44,6 +50,66 @@ output_field geoip
44
50
  flatten true
45
51
  ```
46
52
 
53
+ ### continent
54
+
55
+ ```
56
+ continent true
57
+ ```
58
+
59
+ ### country
60
+
61
+ ```
62
+ country true
63
+ ```
64
+
65
+ ### city
66
+
67
+ ```
68
+ city true
69
+ ```
70
+
71
+ ### location
72
+
73
+ ```
74
+ location true
75
+ ```
76
+
77
+ ### postal
78
+
79
+ ```
80
+ postal true
81
+ ```
82
+
83
+ ### registered_country
84
+
85
+ ```
86
+ registered_country true
87
+ ```
88
+
89
+ ### represented_country
90
+
91
+ ```
92
+ represented_country true
93
+ ```
94
+
95
+ ### subdivisions
96
+
97
+ ```
98
+ subdivisions true
99
+ ```
100
+
101
+ ### traits
102
+
103
+ ```
104
+ traits true
105
+ ```
106
+
107
+ ### connection_type
108
+
109
+ ```
110
+ connection_type true
111
+ ```
112
+
47
113
  ## Plugin setup examples
48
114
 
49
115
  ```
@@ -52,8 +118,20 @@ flatten true
52
118
 
53
119
  database_path /path/to/GeoLite2-City.mmdb
54
120
  lookup_field host
55
- output_field geoip
121
+ field_prefix geoip
122
+ field_delimiter _
56
123
  flatten true
124
+
125
+ continent true
126
+ country true
127
+ city true
128
+ location true
129
+ postal true
130
+ registered_country true
131
+ represented_country true
132
+ subdivisions true
133
+ traits true
134
+ connection_type true
57
135
  </filter>
58
136
  ```
59
137
 
@@ -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-filter-geoip"
7
- spec.version = "0.1.0"
7
+ spec.version = "0.2.0"
8
8
  spec.authors = ["Minoru Osuka"]
9
9
  spec.email = ["minoru.osuka@gmail.com"]
10
10
 
@@ -1,25 +1,71 @@
1
1
  require 'maxminddb'
2
+ require 'json'
2
3
 
3
4
  module Fluent
4
5
  class GeoIPFilter < Filter
5
6
  Fluent::Plugin.register_filter('geoip', self)
6
7
 
7
8
  DEFAULT_LOOKUP_FIELD = 'ip'
8
- DEFAULT_OUTPUT_FIELD = 'geoip'
9
+ DEFAULT_FIELD_PREFIX = 'geoip'
10
+ DEFAULT_FIELD_DELIMITER = '_'
9
11
  DEFAULT_FLATTEN = false
10
12
 
13
+ DEFAULT_CITY = true
14
+ DEFAULT_CONTINENT = true
15
+ DEFAULT_COUNTRY = true
16
+ DEFAULT_LOCATION = true
17
+ DEFAULT_POSTAL = true
18
+ DEFAULT_REGISTERED_COUNTRY = true
19
+ DEFAULT_REPRESENTED_COUNTRY = true
20
+ DEFAULT_SUBDIVISIONS = true
21
+ DEFAULT_TRAITS = true
22
+ DEFAULT_CONNECTION_TYPE = true
23
+
11
24
  config_param :database_path, :string, :default => nil,
12
25
  :desc => ''
13
26
 
14
27
  config_param :lookup_field, :string, :default => DEFAULT_LOOKUP_FIELD,
15
28
  :desc => ''
16
29
 
17
- config_param :output_field, :string, :default => DEFAULT_OUTPUT_FIELD,
30
+ config_param :field_prefix, :string, :default => DEFAULT_FIELD_PREFIX,
31
+ :desc => ''
32
+
33
+ config_param :field_delimiter, :string, :default => DEFAULT_FIELD_DELIMITER,
18
34
  :desc => ''
19
35
 
20
36
  config_param :flatten, :bool, :default => DEFAULT_FLATTEN,
21
37
  :desc => ''
22
38
 
39
+ config_param :continent, :bool, :default => DEFAULT_CONTINENT,
40
+ :desc => ''
41
+
42
+ config_param :country, :bool, :default => DEFAULT_COUNTRY,
43
+ :desc => ''
44
+
45
+ config_param :city, :bool, :default => DEFAULT_CITY,
46
+ :desc => ''
47
+
48
+ config_param :location, :bool, :default => DEFAULT_LOCATION,
49
+ :desc => ''
50
+
51
+ config_param :postal, :bool, :default => DEFAULT_POSTAL,
52
+ :desc => ''
53
+
54
+ config_param :registered_country, :bool, :default => DEFAULT_REGISTERED_COUNTRY,
55
+ :desc => ''
56
+
57
+ config_param :represented_country, :bool, :default => DEFAULT_REPRESENTED_COUNTRY,
58
+ :desc => ''
59
+
60
+ config_param :subdivisions, :bool, :default => DEFAULT_SUBDIVISIONS,
61
+ :desc => ''
62
+
63
+ config_param :traits, :bool, :default => DEFAULT_TRAITS,
64
+ :desc => ''
65
+
66
+ config_param :connection_type, :bool, :default => DEFAULT_CONNECTION_TYPE,
67
+ :desc => ''
68
+
23
69
  def initialize
24
70
  super
25
71
  end
@@ -31,9 +77,31 @@ module Fluent
31
77
 
32
78
  @lookup_field = conf.has_key?('lookup_field') ? conf['lookup_field'] : DEFAULT_LOOKUP_FIELD
33
79
 
34
- @output_field = conf.has_key?('output_field') ? conf['output_field'] : DEFAULT_OUTPUT_FIELD
80
+ @field_prefix = conf.has_key?('field_prefix') ? conf['field_prefix'] : DEFAULT_FIELD_PREFIX
81
+
82
+ @field_delimiter = conf.has_key?('field_delimiter') ? conf['field_delimiter'] : DEFAULT_FIELD_DELIMITER
83
+
84
+ @flatten = conf.has_key?('flatten') ? to_boolean(conf['flatten']) : DEFAULT_FLATTEN
85
+
86
+ @continent = conf.has_key?('continent') ? to_boolean(conf['continent']) : DEFAULT_CONTINENT
87
+
88
+ @country = conf.has_key?('country') ? to_boolean(conf['country']) : DEFAULT_COUNTRY
89
+
90
+ @city = conf.has_key?('city') ? to_boolean(conf['city']) : DEFAULT_CITY
91
+
92
+ @location = conf.has_key?('location') ? to_boolean(conf['location']) : DEFAULT_LOCATION
93
+
94
+ @postal = conf.has_key?('postal') ? to_boolean(conf['postal']) : DEFAULT_POSTAL
95
+
96
+ @registered_country = conf.has_key?('registered_country') ? to_boolean(conf['registered_country']) : DEFAULT_REGISTERED_COUNTRY
35
97
 
36
- @flatten = conf.has_key?('flatten') ? conf['flatten'] : DEFAULT_FLATTEN
98
+ @represented_country = conf.has_key?('represented_country') ? to_boolean(conf['represented_country']) : DEFAULT_REPRESENTED_COUNTRY
99
+
100
+ @subdivisions = conf.has_key?('subdivisions') ? to_boolean(conf['subdivisions']) : DEFAULT_SUBDIVISIONS
101
+
102
+ @traits = conf.has_key?('traits') ? to_boolean(conf['traits']) : DEFAULT_TRAITS
103
+
104
+ @connection_type = conf.has_key?('connection_type') ? to_boolean(conf['connection_type']) : DEFAULT_CONNECTION_TYPE
37
105
 
38
106
  @database = MaxMindDB.new(@database_path)
39
107
  end
@@ -45,12 +113,133 @@ module Fluent
45
113
  geoip = @database.lookup(ip)
46
114
 
47
115
  if geoip.found? then
48
- log.info geoip
49
- record[@output_field] = geoip.to_hash
116
+ geoip_hash = geoip.to_hash
117
+
118
+ if @continent && geoip_hash.has_key?('continent') then
119
+ if @flatten then
120
+ record.merge!(to_flatten(geoip_hash['continent'], [@field_prefix, 'continent'], @field_delimiter))
121
+ else
122
+ record[[@field_prefix, 'continent'].join(@field_delimiter)] = geoip_hash['continent'].to_json
123
+ end
124
+ end
125
+
126
+ if @country && geoip_hash.has_key?('country') then
127
+ if @flatten then
128
+ record.merge!(to_flatten(geoip_hash['country'], [@field_prefix, 'country'], @field_delimiter))
129
+ else
130
+ record[[@field_prefix, 'country'].join(@field_delimiter)] = geoip_hash['country'].to_json
131
+ end
132
+ end
133
+
134
+ if @city && geoip_hash.has_key?('city') then
135
+ if @flatten then
136
+ record.merge!(to_flatten(geoip_hash['city'], [@field_prefix, 'city'], @field_delimiter))
137
+ else
138
+ record[[@field_prefix, 'city'].join(@field_delimiter)] = geoip_hash['city'].to_json
139
+ end
140
+ end
141
+
142
+ if @location && geoip_hash.has_key?('location') then
143
+ if @flatten then
144
+ record.merge!(to_flatten(geoip_hash['location'], [@field_prefix, 'location'], @field_delimiter))
145
+ else
146
+ record[[@field_prefix, 'location'].join(@field_delimiter)] = geoip_hash['location'].to_json
147
+ end
148
+ end
149
+
150
+ if @postal && geoip_hash.has_key?('postal') then
151
+ if @flatten then
152
+ record.merge!(to_flatten(geoip_hash['postal'], [@field_prefix, 'postal'], @field_delimiter))
153
+ else
154
+ record[[@field_prefix, 'postal'].join(@field_delimiter)] = geoip_hash['postal'].to_json
155
+ end
156
+ end
157
+
158
+ if @registered_country && geoip_hash.has_key?('registered_country') then
159
+ if @flatten then
160
+ record.merge!(to_flatten(geoip_hash['registered_country'], [@field_prefix, 'registered_country'], @field_delimiter))
161
+ else
162
+ record[[@field_prefix, 'registered_country'].join(@field_delimiter)] = geoip_hash['registered_country'].to_json
163
+ end
164
+ end
165
+
166
+ if @represented_country && geoip_hash.has_key?('represented_country') then
167
+ if @flatten then
168
+ record.merge!(to_flatten(geoip_hash['represented_country'], [@field_prefix, 'represented_country'], @field_delimiter))
169
+ else
170
+ record[[@field_prefix, 'represented_country'].join(@field_delimiter)] = geoip_hash['represented_country'].to_json
171
+ end
172
+ end
173
+
174
+ if @subdivisions && geoip_hash.has_key?('subdivisions') then
175
+ if @flatten then
176
+ record.merge!(to_flatten(geoip_hash['subdivisions'], [@field_prefix, 'subdivisions'], @field_delimiter))
177
+ else
178
+ record[[@field_prefix, 'subdivisions'].join(@field_delimiter)] = geoip_hash['subdivisions'].to_json
179
+ end
180
+ end
181
+
182
+ if @traits && geoip_hash.has_key?('traits') then
183
+ if @flatten then
184
+ record.merge!(to_flatten(geoip_hash['traits'], [@field_prefix, 'traits'], @field_delimiter))
185
+ else
186
+ record[[@field_prefix, 'traits'].join(@field_delimiter)] = geoip_hash['traits'].to_json
187
+ end
188
+ end
189
+
190
+ if @connection_type && geoip_hash.has_key?('connection_type') then
191
+ if @flatten then
192
+ record.merge!(to_flatten(geoip_hash['connection_type'], [@field_prefix, 'connection_type'], @field_delimiter))
193
+ else
194
+ record[[@field_prefix, 'connection_type'].join(@field_delimiter)] = geoip_hash['connection_type'].to_json
195
+ end
196
+ end
197
+
198
+ log.info "Record: %s" % record.inspect
199
+ else
200
+ log.warn "It was not possible to look up the #{ip}."
201
+ end
202
+ end
203
+
204
+ return record
205
+ end
206
+
207
+ def to_flatten(hash, stack=[], delimiter='/')
208
+ output = {}
209
+
210
+ hash.keys.each do |key|
211
+ stack.push key
212
+
213
+ if hash[key].instance_of?(Hash) then
214
+ output.merge!(to_flatten(hash[key], stack, delimiter))
215
+ elsif hash[key].instance_of?(Array) then
216
+ i = 0
217
+ hash[key].each do |data|
218
+ stack.push i
219
+ if data.instance_of?(Hash) then
220
+ output.merge!(to_flatten(data, stack, delimiter))
221
+ end
222
+ i = i + 1
223
+ stack.pop
224
+ end
225
+ else
226
+ output[stack.join(delimiter)] = hash[key]
50
227
  end
51
228
 
229
+ stack.pop
52
230
  end
53
231
 
232
+ return output
54
233
  end
234
+
235
+ def to_boolean(string)
236
+ if string== true || string =~ (/(true|t|yes|y|1)$/i) then
237
+ return true
238
+ elsif string== false || string.nil? || string =~ (/(false|f|no|n|0)$/i)
239
+ return false
240
+ else
241
+ return false
242
+ end
243
+ end
55
244
  end
56
245
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-filter-geoip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Minoru Osuka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-17 00:00:00.000000000 Z
11
+ date: 2016-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd