Class: Fluent::GeoIPFilter
- Inherits:
-
Filter
- Object
- Filter
- Fluent::GeoIPFilter
- Defined in:
- lib/fluent/plugin/filter_geoip.rb
Constant Summary collapse
- DEFAULT_LOOKUP_FIELD =
'ip'- DEFAULT_FIELD_PREFIX =
'geoip'- DEFAULT_FIELD_DELIMITER =
'_'- DEFAULT_FLATTEN =
false- DEFAULT_CITY =
true- DEFAULT_CONTINENT =
true- DEFAULT_COUNTRY =
true- DEFAULT_LOCATION =
true- DEFAULT_POSTAL =
true- DEFAULT_REGISTERED_COUNTRY =
true- DEFAULT_REPRESENTED_COUNTRY =
true- DEFAULT_SUBDIVISIONS =
true- DEFAULT_TRAITS =
true- DEFAULT_CONNECTION_TYPE =
true
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #filter(tag, time, record) ⇒ Object
-
#initialize ⇒ GeoIPFilter
constructor
A new instance of GeoIPFilter.
- #to_boolean(string) ⇒ Object
- #to_flatten(hash, stack = [], delimiter = '/') ⇒ Object
Constructor Details
#initialize ⇒ GeoIPFilter
Returns a new instance of GeoIPFilter.
69 70 71 |
# File 'lib/fluent/plugin/filter_geoip.rb', line 69 def initialize super end |
Instance Method Details
#configure(conf) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/fluent/plugin/filter_geoip.rb', line 73 def configure(conf) super @database_path = conf['database_path'] @lookup_field = conf.has_key?('lookup_field') ? conf['lookup_field'] : DEFAULT_LOOKUP_FIELD @field_prefix = conf.has_key?('field_prefix') ? conf['field_prefix'] : DEFAULT_FIELD_PREFIX @field_delimiter = conf.has_key?('field_delimiter') ? conf['field_delimiter'] : DEFAULT_FIELD_DELIMITER @flatten = conf.has_key?('flatten') ? to_boolean(conf['flatten']) : DEFAULT_FLATTEN @continent = conf.has_key?('continent') ? to_boolean(conf['continent']) : DEFAULT_CONTINENT @country = conf.has_key?('country') ? to_boolean(conf['country']) : DEFAULT_COUNTRY @city = conf.has_key?('city') ? to_boolean(conf['city']) : DEFAULT_CITY @location = conf.has_key?('location') ? to_boolean(conf['location']) : DEFAULT_LOCATION @postal = conf.has_key?('postal') ? to_boolean(conf['postal']) : DEFAULT_POSTAL @registered_country = conf.has_key?('registered_country') ? to_boolean(conf['registered_country']) : DEFAULT_REGISTERED_COUNTRY @represented_country = conf.has_key?('represented_country') ? to_boolean(conf['represented_country']) : DEFAULT_REPRESENTED_COUNTRY @subdivisions = conf.has_key?('subdivisions') ? to_boolean(conf['subdivisions']) : DEFAULT_SUBDIVISIONS @traits = conf.has_key?('traits') ? to_boolean(conf['traits']) : DEFAULT_TRAITS @connection_type = conf.has_key?('connection_type') ? to_boolean(conf['connection_type']) : DEFAULT_CONNECTION_TYPE @database = MaxMindDB.new(@database_path) end |
#filter(tag, time, record) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/fluent/plugin/filter_geoip.rb', line 109 def filter(tag, time, record) ip = record[@lookup_field] unless ip.nil? then geoip = @database.lookup(ip) if geoip.found? then geoip_hash = geoip.to_hash if @continent && geoip_hash.has_key?('continent') then if @flatten then record.merge!(to_flatten(geoip_hash['continent'], [@field_prefix, 'continent'], @field_delimiter)) else record[[@field_prefix, 'continent'].join(@field_delimiter)] = geoip_hash['continent'].to_json end end if @country && geoip_hash.has_key?('country') then if @flatten then record.merge!(to_flatten(geoip_hash['country'], [@field_prefix, 'country'], @field_delimiter)) else record[[@field_prefix, 'country'].join(@field_delimiter)] = geoip_hash['country'].to_json end end if @city && geoip_hash.has_key?('city') then if @flatten then record.merge!(to_flatten(geoip_hash['city'], [@field_prefix, 'city'], @field_delimiter)) else record[[@field_prefix, 'city'].join(@field_delimiter)] = geoip_hash['city'].to_json end end if @location && geoip_hash.has_key?('location') then if @flatten then record.merge!(to_flatten(geoip_hash['location'], [@field_prefix, 'location'], @field_delimiter)) else record[[@field_prefix, 'location'].join(@field_delimiter)] = geoip_hash['location'].to_json end end if @postal && geoip_hash.has_key?('postal') then if @flatten then record.merge!(to_flatten(geoip_hash['postal'], [@field_prefix, 'postal'], @field_delimiter)) else record[[@field_prefix, 'postal'].join(@field_delimiter)] = geoip_hash['postal'].to_json end end if @registered_country && geoip_hash.has_key?('registered_country') then if @flatten then record.merge!(to_flatten(geoip_hash['registered_country'], [@field_prefix, 'registered_country'], @field_delimiter)) else record[[@field_prefix, 'registered_country'].join(@field_delimiter)] = geoip_hash['registered_country'].to_json end end if @represented_country && geoip_hash.has_key?('represented_country') then if @flatten then record.merge!(to_flatten(geoip_hash['represented_country'], [@field_prefix, 'represented_country'], @field_delimiter)) else record[[@field_prefix, 'represented_country'].join(@field_delimiter)] = geoip_hash['represented_country'].to_json end end if @subdivisions && geoip_hash.has_key?('subdivisions') then if @flatten then record.merge!(to_flatten(geoip_hash['subdivisions'], [@field_prefix, 'subdivisions'], @field_delimiter)) else record[[@field_prefix, 'subdivisions'].join(@field_delimiter)] = geoip_hash['subdivisions'].to_json end end if @traits && geoip_hash.has_key?('traits') then if @flatten then record.merge!(to_flatten(geoip_hash['traits'], [@field_prefix, 'traits'], @field_delimiter)) else record[[@field_prefix, 'traits'].join(@field_delimiter)] = geoip_hash['traits'].to_json end end if @connection_type && geoip_hash.has_key?('connection_type') then if @flatten then record.merge!(to_flatten(geoip_hash['connection_type'], [@field_prefix, 'connection_type'], @field_delimiter)) else record[[@field_prefix, 'connection_type'].join(@field_delimiter)] = geoip_hash['connection_type'].to_json end end log.info "Record: %s" % record.inspect else log.warn "It was not possible to look up the #{ip}." end end return record end |
#to_boolean(string) ⇒ Object
235 236 237 238 239 240 241 242 243 |
# File 'lib/fluent/plugin/filter_geoip.rb', line 235 def to_boolean(string) if string== true || string =~ (/(true|t|yes|y|1)$/i) then return true elsif string== false || string.nil? || string =~ (/(false|f|no|n|0)$/i) return false else return false end end |
#to_flatten(hash, stack = [], delimiter = '/') ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/fluent/plugin/filter_geoip.rb', line 207 def to_flatten(hash, stack=[], delimiter='/') output = {} hash.keys.each do |key| stack.push key if hash[key].instance_of?(Hash) then output.merge!(to_flatten(hash[key], stack, delimiter)) elsif hash[key].instance_of?(Array) then i = 0 hash[key].each do |data| stack.push i if data.instance_of?(Hash) then output.merge!(to_flatten(data, stack, delimiter)) end i = i + 1 stack.pop end else output[stack.join(delimiter)] = hash[key] end stack.pop end return output end |