fluent-plugin-filter-geo 0.0.1
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 +7 -0
- data/.gitignore +38 -0
- data/Gemfile +4 -0
- data/LICENSE +201 -0
- data/README.md +313 -0
- data/Rakefile +10 -0
- data/fluent-plugin-filter-geo.gemspec +29 -0
- data/fluent-plugin-filter-geo.zip +0 -0
- data/lib/fluent/plugin/filter_geo.rb +469 -0
- data/maxminddb.rb +78 -0
- metadata +138 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 2f8f54e3f7baa38ca77f61e373cc1a2dc77e809a
|
|
4
|
+
data.tar.gz: 976648ef8f25245cb02f1d77a82286d87b765fbc
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e591325c2656d0013746d8996ac2fcfa2058b29d8a5ae8a67fc7b266a2f602ccfb3b34a83356eb736fb9aeb3d2153a8cc4c5302c4e688a0912b63ff3f5e74599
|
|
7
|
+
data.tar.gz: 42d27b36d1a3d012a583ee0e5e17c87026ed1439c06aaf96b10945646903c58b7be260296a825b9b10eff6d07a3193f6baf75d73174cfb039e4a04ad90ff791d
|
data/.gitignore
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
*.gem
|
|
2
|
+
*.rbc
|
|
3
|
+
/.config
|
|
4
|
+
/coverage/
|
|
5
|
+
/InstalledFiles
|
|
6
|
+
/pkg/
|
|
7
|
+
/spec/reports/
|
|
8
|
+
/test/tmp/
|
|
9
|
+
/test/version_tmp/
|
|
10
|
+
/tmp/
|
|
11
|
+
|
|
12
|
+
## Specific to RubyMotion:
|
|
13
|
+
.dat*
|
|
14
|
+
.repl_history
|
|
15
|
+
build/
|
|
16
|
+
|
|
17
|
+
## Documentation cache and generated files:
|
|
18
|
+
/.yardoc/
|
|
19
|
+
/_yardoc/
|
|
20
|
+
/doc/
|
|
21
|
+
/rdoc/
|
|
22
|
+
|
|
23
|
+
## Environment normalisation:
|
|
24
|
+
/.bundle/
|
|
25
|
+
/vendor/bundle
|
|
26
|
+
/lib/bundler/man/
|
|
27
|
+
|
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
30
|
+
# Gemfile.lock
|
|
31
|
+
# .ruby-version
|
|
32
|
+
# .ruby-gemset
|
|
33
|
+
|
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
|
35
|
+
.rvmrc
|
|
36
|
+
|
|
37
|
+
Gemfile.lock
|
|
38
|
+
geoip
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# Fluent::Plugin::GeoFilter
|
|
2
|
+
|
|
3
|
+
This is a [Fluentd](http://fluentd.org/) filter plugin for adding [GeoIP data](http://dev.maxmind.com/geoip/geoip2/geolite2/) to record. Supports the new Maxmind v2 database formats.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
gem 'fluent-plugin-filter-geo'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install fluent-plugin-filter-geo
|
|
20
|
+
|
|
21
|
+
## Config parameters
|
|
22
|
+
|
|
23
|
+
### enable_auto_download
|
|
24
|
+
|
|
25
|
+
If true, enable to download GeoIP2 database autometically (default: true).
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
enable_auto_download true
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### md5_url
|
|
32
|
+
|
|
33
|
+
GeoIP2 MD5 checksum URL (default: http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5)
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
md5_url http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### download_url
|
|
40
|
+
|
|
41
|
+
GeoIP2 database download URL (default: http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz).
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
download_url http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### md5_path
|
|
48
|
+
|
|
49
|
+
GeoIP2 MD5 checksum path. (default: ./geoip/database/GeoLite2-City.md5)
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
md5_path ./geoip/database/GeoLite2-City.md5
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### database_path
|
|
56
|
+
|
|
57
|
+
GeoIP2 database path. (default: ./geoip/database/GeoLite2-City.md5)
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
database_path ./geoip/database/GeoLite2-City.mmdb
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### lookup_field
|
|
64
|
+
|
|
65
|
+
Specify the field name that IP address is stored (default: ip).
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
lookup_field host
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### output_field
|
|
72
|
+
|
|
73
|
+
Specify the field name that store the result (default: geoip).
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
output_field geoip
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### field_delimiter
|
|
80
|
+
|
|
81
|
+
Specify the field delimiter (default .).
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
field_delimiter .
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### flatten
|
|
88
|
+
|
|
89
|
+
If true, to flatten the result using field_delimiter (default: true).
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
flatten true
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### geohidden
|
|
96
|
+
|
|
97
|
+
If true, If true, to geohidden geoname_id,metro_code the result using geohidden (default: false).
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
geohidden false
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### locale
|
|
104
|
+
|
|
105
|
+
Get the data for the specified locale (default: en).
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
locale en
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### continent
|
|
112
|
+
|
|
113
|
+
If true, to get continent information (default: true).
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
continent true
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### country
|
|
120
|
+
|
|
121
|
+
If true, to get country information (default: true).
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
country true
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### city
|
|
128
|
+
|
|
129
|
+
If true, to get city information (default: true).
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
city true
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### location
|
|
136
|
+
|
|
137
|
+
If true, to get location information (default: true).
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
location true
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### postal
|
|
144
|
+
|
|
145
|
+
If true, to get postal information (default: true).
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
postal true
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### registered_country
|
|
152
|
+
|
|
153
|
+
If true, to get registered country information (default: true).
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
registered_country true
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### represented_country
|
|
160
|
+
|
|
161
|
+
If true, to get represented country information (default: true).
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
represented_country true
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### subdivisions
|
|
168
|
+
|
|
169
|
+
If true, to get subdivisions information (default: true).
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
subdivisions true
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### traits
|
|
176
|
+
|
|
177
|
+
If true, to get traits information (default: true).
|
|
178
|
+
|
|
179
|
+
```
|
|
180
|
+
traits true
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### connection_type
|
|
184
|
+
|
|
185
|
+
If true, to get connection type information (default: true).
|
|
186
|
+
|
|
187
|
+
```
|
|
188
|
+
connection_type true
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Plugin setup examples
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
<filter tail.log>
|
|
195
|
+
@type geo
|
|
196
|
+
|
|
197
|
+
enable_auto_download true
|
|
198
|
+
md5_url http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5
|
|
199
|
+
download_url http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz
|
|
200
|
+
md5_path ./geoip/database/GeoLite2-City.md5
|
|
201
|
+
database_path ./geoip/database/GeoLite2-City.mmdb
|
|
202
|
+
|
|
203
|
+
lookup_field host
|
|
204
|
+
output_field geoip
|
|
205
|
+
field_delimiter .
|
|
206
|
+
flatten true
|
|
207
|
+
geohidden false
|
|
208
|
+
|
|
209
|
+
locale en
|
|
210
|
+
|
|
211
|
+
continent true
|
|
212
|
+
country true
|
|
213
|
+
city true
|
|
214
|
+
location true
|
|
215
|
+
postal true
|
|
216
|
+
registered_country true
|
|
217
|
+
represented_country true
|
|
218
|
+
subdivisions true
|
|
219
|
+
traits true
|
|
220
|
+
connection_type true
|
|
221
|
+
</filter>
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Assuming following inputs are coming:
|
|
225
|
+
|
|
226
|
+
```javascript
|
|
227
|
+
{
|
|
228
|
+
"host":"180.195.25.228",
|
|
229
|
+
"user":"-",
|
|
230
|
+
"method":"GET",
|
|
231
|
+
"path":"/category/giftcards?from=20",
|
|
232
|
+
"code":"200",
|
|
233
|
+
"size":"63",
|
|
234
|
+
"referer":"-",
|
|
235
|
+
"agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
then output bocomes as belows:
|
|
240
|
+
|
|
241
|
+
```javascript
|
|
242
|
+
{
|
|
243
|
+
"host":"180.195.25.228",
|
|
244
|
+
"user":"-",
|
|
245
|
+
"method":"GET",
|
|
246
|
+
"path":"/category/giftcards?from=20",
|
|
247
|
+
"code":"200",
|
|
248
|
+
"size":"63",
|
|
249
|
+
"referer":"-",
|
|
250
|
+
"agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1",
|
|
251
|
+
"geoip.continent.code":"AS",
|
|
252
|
+
"geoip.continent.geoname_id":6255147,
|
|
253
|
+
"geoip.continent.name":"Asia",
|
|
254
|
+
"geoip.country.geoname_id":1694008,
|
|
255
|
+
"geoip.country.iso_code":"PH",
|
|
256
|
+
"geoip.country.name":"Philippines",
|
|
257
|
+
"geoip.city.geoname_id":1728893,
|
|
258
|
+
"geoip.city.name":"Bagumbayan",
|
|
259
|
+
"geoip.location.latitude":13.45,
|
|
260
|
+
"geoip.location.longitude":123.6667,
|
|
261
|
+
"geoip.location.time_zone":"Asia/Manila",
|
|
262
|
+
"geoip.postal.code":"4513",
|
|
263
|
+
"geoip.registered_country.geoname_id":1694008,
|
|
264
|
+
"geoip.registered_country.iso_code":"PH",
|
|
265
|
+
"geoip.registered_country.name":"Philippines",
|
|
266
|
+
"geoip.subdivisions.0.geoname_id":7521310,
|
|
267
|
+
"geoip.subdivisions.0.iso_code":"05",
|
|
268
|
+
"geoip.subdivisions.0.name":"Bicol",
|
|
269
|
+
"geoip.subdivisions.1.geoname_id":1731616,
|
|
270
|
+
"geoip.subdivisions.1.iso_code":"ALB",
|
|
271
|
+
"geoip.subdivisions.1.name":"Province of Albay",
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
then output bocomes as belows (geohidden false):
|
|
276
|
+
|
|
277
|
+
```javascript
|
|
278
|
+
{
|
|
279
|
+
"host":"180.195.25.228",
|
|
280
|
+
"user":"-",
|
|
281
|
+
"method":"GET",
|
|
282
|
+
"path":"/category/giftcards?from=20",
|
|
283
|
+
"code":"200",
|
|
284
|
+
"size":"63",
|
|
285
|
+
"referer":"-",
|
|
286
|
+
"agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1",
|
|
287
|
+
"geoip.continent.code":"AS",
|
|
288
|
+
"geoip.continent.name":"Asia",
|
|
289
|
+
"geoip.country.iso_code":"PH",
|
|
290
|
+
"geoip.country.name":"Philippines",
|
|
291
|
+
"geoip.city.name":"Bagumbayan",
|
|
292
|
+
"geoip.location.latitude":13.45,
|
|
293
|
+
"geoip.location.longitude":123.6667,
|
|
294
|
+
"geoip.location.time_zone":"Asia/Manila",
|
|
295
|
+
"geoip.postal.code":"4513",
|
|
296
|
+
"geoip.registered_country.iso_code":"PH",
|
|
297
|
+
"geoip.registered_country.name":"Philippines",
|
|
298
|
+
"geoip.subdivisions.0.iso_code":"05",
|
|
299
|
+
"geoip.subdivisions.0.name":"Bicol",
|
|
300
|
+
"geoip.subdivisions.1.iso_code":"ALB",
|
|
301
|
+
"geoip.subdivisions.1.name":"Province of Albay",
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
## Development
|
|
307
|
+
|
|
308
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `rake test` to run the tests.
|
|
309
|
+
|
|
310
|
+
## Contributing
|
|
311
|
+
|
|
312
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mosuka/fluent-plugin-filter-geoip.
|
|
313
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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-filter-geo"
|
|
7
|
+
spec.version = "0.0.1"
|
|
8
|
+
spec.authors = ["Tokyo Home SOC"]
|
|
9
|
+
spec.email = ["github@homesoc.tokyo"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "Fluent filter plugin for adding GeoIP data to record."
|
|
12
|
+
spec.description = "Fluent filter plugin for adding GeoIP data to record. Supports the new Maxmind v2 database formats."
|
|
13
|
+
spec.homepage = "https://github.com/airforon/fluent-plugin-filter-geo"
|
|
14
|
+
|
|
15
|
+
spec.license = "Apache-2.0"
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
18
|
+
spec.bindir = "exe"
|
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
20
|
+
spec.require_paths = ["lib"]
|
|
21
|
+
|
|
22
|
+
spec.add_runtime_dependency 'fluentd', '~> 0.12.19'
|
|
23
|
+
spec.add_runtime_dependency 'maxminddb', '~> 0.1.8'
|
|
24
|
+
|
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.11.2'
|
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.5.0'
|
|
27
|
+
spec.add_development_dependency 'test-unit', '~> 3.1.5'
|
|
28
|
+
spec.add_development_dependency 'minitest', '~> 5.8.3'
|
|
29
|
+
end
|
|
Binary file
|
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
require 'maxminddb'
|
|
2
|
+
require 'json'
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
require 'open-uri'
|
|
5
|
+
|
|
6
|
+
module Fluent
|
|
7
|
+
class GeoFilter < Filter
|
|
8
|
+
Fluent::Plugin.register_filter('geo', self)
|
|
9
|
+
|
|
10
|
+
DEFAULT_ENABLE_DOWNLOAD = true
|
|
11
|
+
DEFAULT_MD5_URL = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5'
|
|
12
|
+
DEFAULT_DOWNLOAD_URL = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz'
|
|
13
|
+
DEFAULT_MD5_PATH = './geoip/database/GeoLite2-City.md5'
|
|
14
|
+
DEFAULT_DATABASE_PATH = './geoip/database/GeoLite2-City.mmdb'
|
|
15
|
+
|
|
16
|
+
DEFAULT_LOOKUP_FIELD = 'ip'
|
|
17
|
+
DEFAULT_OUTPU_FIELD = 'geoip'
|
|
18
|
+
DEFAULT_FIELD_DELIMITER = '.'
|
|
19
|
+
DEFAULT_FLATTEN = false
|
|
20
|
+
DEFAULT_GEOHIDDEN = false
|
|
21
|
+
|
|
22
|
+
DEFAULT_LOCALE = 'en'
|
|
23
|
+
|
|
24
|
+
DEFAULT_CITY = true
|
|
25
|
+
DEFAULT_CONTINENT = true
|
|
26
|
+
DEFAULT_COUNTRY = true
|
|
27
|
+
DEFAULT_LOCATION = true
|
|
28
|
+
DEFAULT_POSTAL = true
|
|
29
|
+
DEFAULT_REGISTERED_COUNTRY = true
|
|
30
|
+
DEFAULT_REPRESENTED_COUNTRY = true
|
|
31
|
+
DEFAULT_SUBDIVISIONS = true
|
|
32
|
+
DEFAULT_TRAITS = true
|
|
33
|
+
DEFAULT_CONNECTION_TYPE = true
|
|
34
|
+
|
|
35
|
+
config_param :enable_auto_download, :bool, :default => DEFAULT_ENABLE_DOWNLOAD,
|
|
36
|
+
:desc => 'If true, enable to download GeoIP2 database autometically (default: %s).' % DEFAULT_ENABLE_DOWNLOAD
|
|
37
|
+
|
|
38
|
+
config_param :md5_url, :string, :default => DEFAULT_MD5_URL,
|
|
39
|
+
:desc => 'GeoIP2 MD5 checksum URL (default: %s)' % DEFAULT_MD5_URL
|
|
40
|
+
|
|
41
|
+
config_param :download_url, :string, :default => DEFAULT_DOWNLOAD_URL,
|
|
42
|
+
:desc => 'GeoIP2 database download URL (default: %s).' % DEFAULT_DOWNLOAD_URL
|
|
43
|
+
|
|
44
|
+
config_param :md5_path, :string, :default => DEFAULT_MD5_PATH,
|
|
45
|
+
:desc => 'GeoIP2 MD5 checksum path. (default: %s)' % DEFAULT_MD5_PATH
|
|
46
|
+
|
|
47
|
+
config_param :database_path, :string, :default => DEFAULT_DATABASE_PATH,
|
|
48
|
+
:desc => 'GeoIP2 database path. (default: %s)' % DEFAULT_DATABASE_PATH
|
|
49
|
+
|
|
50
|
+
config_param :lookup_field, :string, :default => DEFAULT_LOOKUP_FIELD,
|
|
51
|
+
:desc => 'Specify the field name that IP address is stored (default: %s).' % DEFAULT_LOOKUP_FIELD
|
|
52
|
+
|
|
53
|
+
config_param :output_field, :string, :default => DEFAULT_OUTPU_FIELD,
|
|
54
|
+
:desc => 'Specify the field name that store the result (default: %s).' % DEFAULT_OUTPU_FIELD
|
|
55
|
+
|
|
56
|
+
config_param :field_delimiter, :string, :default => DEFAULT_FIELD_DELIMITER,
|
|
57
|
+
:desc => 'Specify the field delimiter (default %s).' % DEFAULT_FIELD_DELIMITER
|
|
58
|
+
|
|
59
|
+
config_param :flatten, :bool, :default => DEFAULT_FLATTEN,
|
|
60
|
+
:desc => 'If true, to flatten the result using field_delimiter (default: %s).' % DEFAULT_FLATTEN
|
|
61
|
+
|
|
62
|
+
config_param :geohidden, :bool, :default => DEFAULT_GEOHIDDEN,
|
|
63
|
+
:desc => 'If true, to geohidden geoname_id,metro_code the result using geohidden (default: %s).' % DEFAULT_GEOHIDDEN
|
|
64
|
+
|
|
65
|
+
config_param :locale, :string, :default => DEFAULT_LOCALE,
|
|
66
|
+
:desc => 'Get the data for the specified locale (default: %s).' % DEFAULT_LOCALE
|
|
67
|
+
|
|
68
|
+
config_param :continent, :bool, :default => DEFAULT_CONTINENT,
|
|
69
|
+
:desc => 'If true, to get continent information (default: %s).' % DEFAULT_CONTINENT
|
|
70
|
+
|
|
71
|
+
config_param :country, :bool, :default => DEFAULT_COUNTRY,
|
|
72
|
+
:desc => 'If true, to get country information (default: %s).' % DEFAULT_COUNTRY
|
|
73
|
+
|
|
74
|
+
config_param :city, :bool, :default => DEFAULT_CITY,
|
|
75
|
+
:desc => 'If true, to get city information (default: %s).' % DEFAULT_CITY
|
|
76
|
+
|
|
77
|
+
config_param :location, :bool, :default => DEFAULT_LOCATION,
|
|
78
|
+
:desc => 'If true, to get location information (default: %s).' % DEFAULT_LOCATION
|
|
79
|
+
|
|
80
|
+
config_param :postal, :bool, :default => DEFAULT_POSTAL,
|
|
81
|
+
:desc => 'If true, to get postal information (default: %s).' % DEFAULT_POSTAL
|
|
82
|
+
|
|
83
|
+
config_param :registered_country, :bool, :default => DEFAULT_REGISTERED_COUNTRY,
|
|
84
|
+
:desc => 'If true, to get registered country information (default: %s).' % DEFAULT_REGISTERED_COUNTRY
|
|
85
|
+
|
|
86
|
+
config_param :represented_country, :bool, :default => DEFAULT_REPRESENTED_COUNTRY,
|
|
87
|
+
:desc => 'If true, to get represented country information (default: %s).' % DEFAULT_REPRESENTED_COUNTRY
|
|
88
|
+
|
|
89
|
+
config_param :subdivisions, :bool, :default => DEFAULT_SUBDIVISIONS,
|
|
90
|
+
:desc => 'If true, to get subdivisions information (default: %s).' % DEFAULT_SUBDIVISIONS
|
|
91
|
+
|
|
92
|
+
config_param :traits, :bool, :default => DEFAULT_TRAITS,
|
|
93
|
+
:desc => 'If true, to get traits information (default: %s).' % DEFAULT_TRAITS
|
|
94
|
+
|
|
95
|
+
config_param :connection_type, :bool, :default => DEFAULT_CONNECTION_TYPE,
|
|
96
|
+
:desc => 'If true, to get connection type information (default: %s).' % DEFAULT_CONNECTION_TYPE
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def initialize
|
|
100
|
+
super
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def configure(conf)
|
|
104
|
+
super
|
|
105
|
+
|
|
106
|
+
if enable_auto_download then
|
|
107
|
+
download_database @download_url, @md5_url, @database_path, @md5_path
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
@database = MaxMindDB.new(@database_path)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def filter(tag, time, record)
|
|
114
|
+
ip = record[@lookup_field]
|
|
115
|
+
|
|
116
|
+
unless ip.nil? then
|
|
117
|
+
geoip = @database.lookup(ip)
|
|
118
|
+
|
|
119
|
+
if geoip.found? then
|
|
120
|
+
if @continent then
|
|
121
|
+
continent_hash = {}
|
|
122
|
+
|
|
123
|
+
unless geoip.continent.code.nil? then
|
|
124
|
+
continent_hash['code'] = geoip.continent.code
|
|
125
|
+
end
|
|
126
|
+
if @geohidden == true then
|
|
127
|
+
unless geoip.continent.geoname_id.nil? then
|
|
128
|
+
continent_hash['geoname_id'] = geoip.continent.geoname_id
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
unless geoip.continent.iso_code.nil? then
|
|
132
|
+
continent_hash['iso_code'] = geoip.continent.iso_code
|
|
133
|
+
end
|
|
134
|
+
unless geoip.continent.name(@locale).nil? then
|
|
135
|
+
continent_hash['name'] = geoip.continent.name(@locale)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
unless continent_hash.empty? then
|
|
139
|
+
if @flatten then
|
|
140
|
+
record.merge!(to_flatten(continent_hash, [@output_field, 'continent'], @field_delimiter))
|
|
141
|
+
else
|
|
142
|
+
record[[@output_field, 'continent'].join(@field_delimiter)] = continent_hash.to_json
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
if @country then
|
|
148
|
+
country_hash = {}
|
|
149
|
+
|
|
150
|
+
unless geoip.country.code.nil? then
|
|
151
|
+
country_hash['code'] = geoip.country.code
|
|
152
|
+
end
|
|
153
|
+
if @geohidden == true then
|
|
154
|
+
unless geoip.country.geoname_id.nil? then
|
|
155
|
+
country_hash['geoname_id'] = geoip.country.geoname_id
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
unless geoip.country.iso_code.nil? then
|
|
159
|
+
country_hash['iso_code'] = geoip.country.iso_code
|
|
160
|
+
end
|
|
161
|
+
unless geoip.country.name(@locale).nil? then
|
|
162
|
+
country_hash['name'] = geoip.country.name(@locale)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
unless country_hash.empty? then
|
|
166
|
+
if @flatten then
|
|
167
|
+
record.merge!(to_flatten(country_hash, [@output_field, 'country'], @field_delimiter))
|
|
168
|
+
else
|
|
169
|
+
record[[@output_field, 'country'].join(@field_delimiter)] = country_hash.to_json
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
if @city then
|
|
175
|
+
city_hash = {}
|
|
176
|
+
|
|
177
|
+
unless geoip.city.code.nil? then
|
|
178
|
+
city_hash['code'] = geoip.city.code
|
|
179
|
+
end
|
|
180
|
+
if @geohidden == true then
|
|
181
|
+
unless geoip.city.geoname_id.nil? then
|
|
182
|
+
city_hash['geoname_id'] = geoip.city.geoname_id
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
unless geoip.city.iso_code.nil? then
|
|
186
|
+
city_hash['iso_code'] = geoip.city.iso_code
|
|
187
|
+
end
|
|
188
|
+
unless geoip.city.name(@locale).nil? then
|
|
189
|
+
city_hash['name'] = geoip.city.name(@locale)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
unless city_hash.empty? then
|
|
193
|
+
if @flatten then
|
|
194
|
+
record.merge!(to_flatten(city_hash, [@output_field, 'city'], @field_delimiter))
|
|
195
|
+
else
|
|
196
|
+
record[[@output_field, 'city'].join(@field_delimiter)] = city_hash.to_json
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
if @location then
|
|
202
|
+
location_hash = {}
|
|
203
|
+
|
|
204
|
+
unless geoip.location.latitude.nil? | geoip.location.longitude.nil? then
|
|
205
|
+
location_hash['location'] = geoip.location.longitude, geoip.location.latitude
|
|
206
|
+
end
|
|
207
|
+
if @geohidden == true then
|
|
208
|
+
unless geoip.location.metro_code.nil? then
|
|
209
|
+
location_hash['metro_code'] = geoip.location.metro_code
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
unless geoip.location.time_zone.nil? then
|
|
213
|
+
location_hash['time_zone'] = geoip.location.time_zone
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
unless location_hash.empty? then
|
|
217
|
+
if @flatten then
|
|
218
|
+
record.merge!(to_flatten(location_hash, [@output_field, 'location'], @field_delimiter))
|
|
219
|
+
else
|
|
220
|
+
record[[@output_field, 'location'].join(@field_delimiter)] = location_hash.to_json
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
if @postal then
|
|
226
|
+
postal_hash = {}
|
|
227
|
+
|
|
228
|
+
unless geoip.postal.code.nil? then
|
|
229
|
+
postal_hash['code'] = geoip.postal.code
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
unless postal_hash.empty? then
|
|
233
|
+
if @flatten then
|
|
234
|
+
record.merge!(to_flatten(postal_hash, [@output_field, 'postal'], @field_delimiter))
|
|
235
|
+
else
|
|
236
|
+
record[[@output_field, 'postal'].join(@field_delimiter)] = postal_hash.to_json
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
if @registered_country then
|
|
242
|
+
registered_country_hash = {}
|
|
243
|
+
|
|
244
|
+
unless geoip.registered_country.code.nil? then
|
|
245
|
+
registered_country_hash['code'] = geoip.registered_country.code
|
|
246
|
+
end
|
|
247
|
+
if @geohidden == true then
|
|
248
|
+
unless geoip.registered_country.geoname_id.nil? then
|
|
249
|
+
registered_country_hash['geoname_id'] = geoip.registered_country.geoname_id
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
unless geoip.registered_country.iso_code.nil? then
|
|
253
|
+
registered_country_hash['iso_code'] = geoip.registered_country.iso_code
|
|
254
|
+
end
|
|
255
|
+
unless geoip.registered_country.name(@locale).nil? then
|
|
256
|
+
registered_country_hash['name'] = geoip.registered_country.name(@locale)
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
unless registered_country_hash.empty? then
|
|
260
|
+
if @flatten then
|
|
261
|
+
record.merge!(to_flatten(registered_country_hash, [@output_field, 'registered_country'], @field_delimiter))
|
|
262
|
+
else
|
|
263
|
+
record[[@output_field, 'registered_country'].join(@field_delimiter)] = registered_country_hash.to_json
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
if @represented_country then
|
|
269
|
+
represented_country_hash = {}
|
|
270
|
+
|
|
271
|
+
unless geoip.represented_country.code.nil? then
|
|
272
|
+
represented_country_hash['code'] = geoip.represented_country.code
|
|
273
|
+
end
|
|
274
|
+
if @geohidden == true then
|
|
275
|
+
unless geoip.represented_country.geoname_id.nil? then
|
|
276
|
+
represented_country_hash['geoname_id'] = geoip.represented_country.geoname_id
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
unless geoip.represented_country.iso_code.nil? then
|
|
280
|
+
represented_country_hash['iso_code'] = geoip.represented_country.iso_code
|
|
281
|
+
end
|
|
282
|
+
unless geoip.represented_country.name(@locale).nil? then
|
|
283
|
+
represented_country_hash['name'] = geoip.represented_country.name(@locale)
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
unless represented_country_hash.empty? then
|
|
287
|
+
if @flatten then
|
|
288
|
+
record.merge!(to_flatten(represented_country_hash, [@output_field, 'represented_country'], @field_delimiter))
|
|
289
|
+
else
|
|
290
|
+
record[[@output_field, 'represented_country'].join(@field_delimiter)] = represented_country_hash.to_json
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
if @subdivisions then
|
|
296
|
+
subdivision_arry = []
|
|
297
|
+
|
|
298
|
+
i = 0
|
|
299
|
+
geoip.subdivisions.each do |subdivision|
|
|
300
|
+
subdivision_hash = {}
|
|
301
|
+
|
|
302
|
+
unless subdivision.code.nil? then
|
|
303
|
+
subdivision_hash['code'] = subdivision.code
|
|
304
|
+
end
|
|
305
|
+
if @geohidden == true then
|
|
306
|
+
unless subdivision.geoname_id.nil? then
|
|
307
|
+
subdivision_hash['geoname_id'] = subdivision.geoname_id
|
|
308
|
+
end
|
|
309
|
+
end
|
|
310
|
+
unless subdivision.iso_code.nil? then
|
|
311
|
+
subdivision_hash['iso_code'] = subdivision.iso_code
|
|
312
|
+
end
|
|
313
|
+
unless subdivision.name(@locale).nil? then
|
|
314
|
+
subdivision_hash['name'] = subdivision.name(@locale)
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
unless subdivision_hash.empty? then
|
|
318
|
+
subdivision_arry.push(subdivision_hash)
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
i = i + 1
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
unless subdivision_arry.empty? then
|
|
325
|
+
if @flatten then
|
|
326
|
+
i = 0
|
|
327
|
+
subdivision_arry.each do |subdivision|
|
|
328
|
+
record.merge!(to_flatten(subdivision, [@output_field, 'subdivisions', i.to_s], @field_delimiter))
|
|
329
|
+
i = i + 1
|
|
330
|
+
end
|
|
331
|
+
else
|
|
332
|
+
record[[@output_field, 'subdivisions'].join(@field_delimiter)] = subdivision_arry.to_json
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
if @traits then
|
|
338
|
+
traits_hash = {}
|
|
339
|
+
|
|
340
|
+
unless geoip.traits.is_anonymous_proxy.nil? then
|
|
341
|
+
traits_hash['is_anonymous_proxy'] = geoip.traits.is_anonymous_proxy
|
|
342
|
+
end
|
|
343
|
+
unless geoip.traits.is_satellite_provider.nil? then
|
|
344
|
+
traits_hash['is_satellite_provider'] = geoip.traits.is_satellite_provider
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
unless traits_hash.empty? then
|
|
348
|
+
if @flatten then
|
|
349
|
+
record.merge!(to_flatten(traits_hash, [@output_field, 'traits'], @field_delimiter))
|
|
350
|
+
else
|
|
351
|
+
record[[@output_field, 'traits'].join(@field_delimiter)] = traits_hash.to_json
|
|
352
|
+
end
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
if @connection_type then
|
|
357
|
+
unless geoip.connection_type.nil? then
|
|
358
|
+
record[[@output_field, 'connection_type'].join(@field_delimiter)] = geoip.connection_type
|
|
359
|
+
end
|
|
360
|
+
end
|
|
361
|
+
else
|
|
362
|
+
log.warn "It was not possible to look up the #{ip}."
|
|
363
|
+
end
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
return record
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
def to_flatten(hash, stack=[], delimiter='/')
|
|
370
|
+
output = {}
|
|
371
|
+
|
|
372
|
+
hash.keys.each do |key|
|
|
373
|
+
stack.push key
|
|
374
|
+
|
|
375
|
+
if hash[key].instance_of?(Hash) then
|
|
376
|
+
output.merge!(to_flatten(hash[key], stack, delimiter))
|
|
377
|
+
else
|
|
378
|
+
output[stack.join(delimiter)] = hash[key]
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
stack.pop
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
return output
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
def download_database(download_url, md5_url, database_path, md5_path)
|
|
388
|
+
# database directory
|
|
389
|
+
database_dir = File.dirname database_path
|
|
390
|
+
md5_dir = File.dirname md5_path
|
|
391
|
+
|
|
392
|
+
# create database directory if directory does not exist.
|
|
393
|
+
FileUtils.mkdir_p(database_dir) unless File.exist?(database_dir)
|
|
394
|
+
FileUtils.mkdir_p(md5_dir) unless File.exist?(md5_dir)
|
|
395
|
+
|
|
396
|
+
# create empty md5 file if file does not exist.
|
|
397
|
+
File.open(md5_path, 'wb').close() unless File.exist?(md5_path)
|
|
398
|
+
|
|
399
|
+
# read saved md5
|
|
400
|
+
current_md5 = nil
|
|
401
|
+
begin
|
|
402
|
+
open(md5_path, 'rb') do |data|
|
|
403
|
+
current_md5 = data.read
|
|
404
|
+
end
|
|
405
|
+
log.info "Current MD5: %s" % current_md5
|
|
406
|
+
rescue => e
|
|
407
|
+
log.warn e.message
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
# fetch md5
|
|
411
|
+
fetched_md5 = nil
|
|
412
|
+
begin
|
|
413
|
+
open(md5_url, 'rb') do |data|
|
|
414
|
+
fetched_md5 = data.read
|
|
415
|
+
end
|
|
416
|
+
log.info "Fetched MD5: %s" % fetched_md5
|
|
417
|
+
rescue => e
|
|
418
|
+
log.warn e.message
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
# check md5
|
|
422
|
+
unless current_md5 == fetched_md5 then
|
|
423
|
+
# download new database
|
|
424
|
+
download_path = database_dir + '/' + File.basename(download_url)
|
|
425
|
+
begin
|
|
426
|
+
log.info "Download: %s" % download_url
|
|
427
|
+
open(download_path, 'wb') do |output|
|
|
428
|
+
open(download_url, 'rb') do |data|
|
|
429
|
+
output.write(data.read)
|
|
430
|
+
end
|
|
431
|
+
end
|
|
432
|
+
log.info "Download done: %s" % download_path
|
|
433
|
+
rescue => e
|
|
434
|
+
log.warn e.message
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
# unzip new database temporaly
|
|
438
|
+
tmp_database_path = database_dir + '/tmp_' + File.basename(database_path)
|
|
439
|
+
begin
|
|
440
|
+
log.info "Unzip: %s" % download_path
|
|
441
|
+
open(tmp_database_path, 'wb') do |output|
|
|
442
|
+
Zlib::GzipReader.open(download_path) do |gz|
|
|
443
|
+
output.write(gz.read)
|
|
444
|
+
end
|
|
445
|
+
end
|
|
446
|
+
log.info "Unzip done: %s" % tmp_database_path
|
|
447
|
+
rescue => e
|
|
448
|
+
puts e.message
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
# check mkd5
|
|
452
|
+
temp_md5 = Digest::MD5.hexdigest(File.open(tmp_database_path, 'rb').read)
|
|
453
|
+
log.info "New MD5: %s" % temp_md5
|
|
454
|
+
if fetched_md5 == temp_md5 then
|
|
455
|
+
log.info "Rename: %s to %s" % [tmp_database_path, database_path]
|
|
456
|
+
FileUtils.mv(tmp_database_path, database_path)
|
|
457
|
+
log.info "Rename done: %s to %s" % [tmp_database_path, database_path]
|
|
458
|
+
|
|
459
|
+
# record new md5
|
|
460
|
+
log.info "Save: %s" % md5_path
|
|
461
|
+
File.write(md5_path, fetched_md5)
|
|
462
|
+
log.info "Save done: %s" % md5_path
|
|
463
|
+
else
|
|
464
|
+
log.info "MD5 missmatch: Fetched MD5 (%s) != New MD5 (%s) ; " % [fetched_md5, temp_md5]
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
end
|
|
468
|
+
end
|
|
469
|
+
end
|
data/maxminddb.rb
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
require 'maxminddb'
|
|
2
|
+
require 'json'
|
|
3
|
+
|
|
4
|
+
db = MaxMindDB.new('./../GeoLite2-City.mmdb')
|
|
5
|
+
ret = db.lookup('74.125.225.224')
|
|
6
|
+
|
|
7
|
+
data = {
|
|
8
|
+
:geoip => {
|
|
9
|
+
:city => {
|
|
10
|
+
:geoname_id => ret.city.geoname_id,
|
|
11
|
+
:name => ret.city.name
|
|
12
|
+
},
|
|
13
|
+
:continent => {
|
|
14
|
+
:code => ret.continent.code,
|
|
15
|
+
:geoname_id => ret.continent.geoname_id,
|
|
16
|
+
:name => ret.continent.name
|
|
17
|
+
},
|
|
18
|
+
:country => {
|
|
19
|
+
:code => ret.country.code,
|
|
20
|
+
:geoname_id => ret.country.geoname_id,
|
|
21
|
+
:name => ret.country.name
|
|
22
|
+
},
|
|
23
|
+
:location => {
|
|
24
|
+
# :accuracy_radius => "ret.location.accuracy_radius",
|
|
25
|
+
:location => [
|
|
26
|
+
ret.location.latitude,
|
|
27
|
+
ret.location.longitude
|
|
28
|
+
],
|
|
29
|
+
:metro_code => ret.location.metro_code,
|
|
30
|
+
:time_zone => ret.location.time_zone
|
|
31
|
+
},
|
|
32
|
+
:postal => {
|
|
33
|
+
:code => ret.postal.code
|
|
34
|
+
},
|
|
35
|
+
:registered_country => {
|
|
36
|
+
:geoname_id => ret.registered_country.geoname_id,
|
|
37
|
+
:iso_code => ret.registered_country.iso_code,
|
|
38
|
+
:name => ret.registered_country.name
|
|
39
|
+
},
|
|
40
|
+
:subdivisions => {
|
|
41
|
+
# :geoname_id => ret.subdivisions.geoname_id,
|
|
42
|
+
# :iso_code => ret.subdivisions.iso_code,
|
|
43
|
+
# :name => ret.subdivisions.name
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
puts JSON.pretty_generate(data)
|
|
50
|
+
|
|
51
|
+
=begin
|
|
52
|
+
puts ret.city.geoname_id
|
|
53
|
+
puts ret.city.name
|
|
54
|
+
|
|
55
|
+
puts ret.continent.code
|
|
56
|
+
puts ret.continent.geoname_id
|
|
57
|
+
puts ret.continent.name
|
|
58
|
+
|
|
59
|
+
puts ret.country.geoname_id
|
|
60
|
+
puts ret.country.iso_code
|
|
61
|
+
puts ret.country.name
|
|
62
|
+
|
|
63
|
+
puts ret.location.accuracy_radius
|
|
64
|
+
puts ret.location.latitude
|
|
65
|
+
puts ret.location.longitude
|
|
66
|
+
puts ret.location.metro_code
|
|
67
|
+
puts ret.location.time_zone
|
|
68
|
+
|
|
69
|
+
puts ret.postal.code
|
|
70
|
+
|
|
71
|
+
puts ret.registered_country.geoname_id
|
|
72
|
+
puts ret.registered_country.iso_code
|
|
73
|
+
puts ret.registered_country.name
|
|
74
|
+
|
|
75
|
+
puts ret.subdivisions.geoname_id
|
|
76
|
+
puts ret.subdivisions.iso_code
|
|
77
|
+
puts ret.subdivisions.name
|
|
78
|
+
=end
|
metadata
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: fluent-plugin-filter-geo
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Tokyo Home SOC
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-11-25 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.19
|
|
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.19
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: maxminddb
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 0.1.8
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 0.1.8
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 1.11.2
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 1.11.2
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 10.5.0
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 10.5.0
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: test-unit
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 3.1.5
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 3.1.5
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: minitest
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 5.8.3
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 5.8.3
|
|
97
|
+
description: Fluent filter plugin for adding GeoIP data to record. Supports the new
|
|
98
|
+
Maxmind v2 database formats.
|
|
99
|
+
email:
|
|
100
|
+
- github@homesoc.tokyo
|
|
101
|
+
executables: []
|
|
102
|
+
extensions: []
|
|
103
|
+
extra_rdoc_files: []
|
|
104
|
+
files:
|
|
105
|
+
- ".gitignore"
|
|
106
|
+
- Gemfile
|
|
107
|
+
- LICENSE
|
|
108
|
+
- README.md
|
|
109
|
+
- Rakefile
|
|
110
|
+
- fluent-plugin-filter-geo.gemspec
|
|
111
|
+
- fluent-plugin-filter-geo.zip
|
|
112
|
+
- lib/fluent/plugin/filter_geo.rb
|
|
113
|
+
- maxminddb.rb
|
|
114
|
+
homepage: https://github.com/airforon/fluent-plugin-filter-geo
|
|
115
|
+
licenses:
|
|
116
|
+
- Apache-2.0
|
|
117
|
+
metadata: {}
|
|
118
|
+
post_install_message:
|
|
119
|
+
rdoc_options: []
|
|
120
|
+
require_paths:
|
|
121
|
+
- lib
|
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
123
|
+
requirements:
|
|
124
|
+
- - ">="
|
|
125
|
+
- !ruby/object:Gem::Version
|
|
126
|
+
version: '0'
|
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0'
|
|
132
|
+
requirements: []
|
|
133
|
+
rubyforge_project:
|
|
134
|
+
rubygems_version: 2.5.1
|
|
135
|
+
signing_key:
|
|
136
|
+
specification_version: 4
|
|
137
|
+
summary: Fluent filter plugin for adding GeoIP data to record.
|
|
138
|
+
test_files: []
|