fluent-plugin-mobile-carrier 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 211678f21ba539d46ac9c1ac103d04f0030ef9a0
4
+ data.tar.gz: d0f28df60642ae53bba779798a6ba1e53092a18e
5
+ SHA512:
6
+ metadata.gz: 9c08cc8c43d7be6abc8ad4ca7cc65cb00296abc0d9a118900d9d89e72970808e817c6120b7df0dfa3e0f6db9cc85d0e2de8d703a925c730b1f5b8ba178b5d5bf
7
+ data.tar.gz: b4457d79c197f4bcd2327d2c9522b3108bbeac8247e1373d936f69897a4e342c40450d96ad33649763fcd3d1989c0ab28a589eacb45bb604af512f1edcb5f21b
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ tags
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) HARUYAMA Seigo
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.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # fluent-plugin-mobile-carrier
2
+
3
+ ## MobileCarrierOutput
4
+
5
+ 'fluent-plugin-mobile-carrier' is a Fluentd plugin to judge mobile carriers from IP address.
6
+ 'fluent-plugin-mobile-carrier' needs external config yaml file, which includes Carrier-IPAddress settings.
7
+
8
+
9
+ ## Configuration
10
+
11
+ To add mobile_carrier result into matched messages:
12
+
13
+ <match input.**>
14
+ type mobile_carrier
15
+ key_name ip_address
16
+ config_yaml config.yaml
17
+ remove_prefix input
18
+ add_prefix merged
19
+ </match>
20
+
21
+ ## Copyright
22
+
23
+ * Copyright (c) HARUYAMA Seigo
24
+ * License
25
+ * Apache License, Version 2.0
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'fluent-plugin-mobile-carrier'
5
+ gem.version = '0.0.1'
6
+ gem.authors = ['HARUYAMA Seigo']
7
+ gem.email = ['haruyama@unixuser.org']
8
+ gem.description = %q{judge mobile carrier by ip address.}
9
+ gem.summary = %q{Fluentd plugin to judge mobile carrier by ip address.}
10
+ gem.homepage = 'http://github.com/haruyama/fluent-plugin-mobile-carrier'
11
+ gem.license = 'APLv2'
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.require_paths = ['lib']
17
+
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_runtime_dependency 'fluentd'
20
+ end
@@ -0,0 +1,12 @@
1
+ LineLength:
2
+ Enabled: false
3
+
4
+ CyclomaticComplexity:
5
+ Enabled: false
6
+
7
+ MethodLength:
8
+ CountComments: false # count full line comments?
9
+ Max: 50
10
+
11
+ GlobalVars:
12
+ Enabled: false
@@ -0,0 +1,87 @@
1
+
2
+ # mobile carrieroutput
3
+ class Fluent::MobileCarrierOutput < Fluent::Output
4
+ Fluent::Plugin.register_output('mobile_carrier', self)
5
+
6
+ config_param :config_yaml, :string
7
+ config_param :remove_prefix, :string, default: nil
8
+ config_param :add_prefix, :string, default: nil
9
+
10
+ config_param :key_name, :string
11
+ config_param :unknown_carrier, :string, default: 'UNKNOWN'
12
+ config_param :out_key_mobile_carrier, :string, default: 'mobile_carrier'
13
+
14
+ def initialize
15
+ super
16
+ require 'ipaddr'
17
+ require 'yaml'
18
+ end
19
+
20
+ def configure(conf)
21
+ super
22
+
23
+ raise 'config_yaml is not set!' unless (@config_yaml)
24
+ @config = prepare_config(YAML.load_file(@config_yaml))
25
+
26
+ if !@tag && !@remove_prefix && !@add_prefix
27
+ fail Fluent::ConfigError, 'missing both of remove_prefix and add_prefix'
28
+ end
29
+ if @tag && (@remove_prefix || @add_prefix)
30
+ fail Fluent::ConfigError, 'both of tag and remove_prefix/add_prefix must not be specified'
31
+ end
32
+ if @remove_prefix
33
+ @removed_prefix_string = @remove_prefix + '.'
34
+ @removed_length = @removed_prefix_string.length
35
+ end
36
+ @added_prefix_string = @add_prefix + '.' if @add_prefix
37
+ end
38
+
39
+ def tag_mangle(tag)
40
+ if @tag
41
+ @tag
42
+ else
43
+ if @remove_prefix &&
44
+ ( (tag.start_with?(@removed_prefix_string) && tag.length > @removed_length) || tag == @remove_prefix)
45
+ tag = tag[@removed_length..-1]
46
+ end
47
+ if @add_prefix
48
+ tag = if tag && tag.length > 0
49
+ @added_prefix_string + tag
50
+ else
51
+ @add_prefix
52
+ end
53
+ end
54
+ tag
55
+ end
56
+ end
57
+
58
+ def prepare_config(raw_config)
59
+ config = {}
60
+ raw_config.each { |carrier, ip_address_list|
61
+ config[carrier] = ip_address_list.map { |ip_address|
62
+ IPAddr.new(ip_address)
63
+ }
64
+ }
65
+ config
66
+ end
67
+
68
+ def judge_mobile_carrier(ip_address, config)
69
+ config.each { |carrier, ipaddr_list|
70
+ if ipaddr_list.any? { |ipaddr| ipaddr.include? ip_address }
71
+ return carrier
72
+ end
73
+ }
74
+ @unknown_carrier
75
+ end
76
+
77
+ def emit(tag, es, chain)
78
+ tag = tag_mangle(tag)
79
+ es.each do |time, record|
80
+ record.merge!(
81
+ @out_key_mobile_carrier => judge_mobile_carrier(record[@key_name], @config)
82
+ )
83
+ Fluent::Engine.emit(tag, time, record)
84
+ end
85
+ chain.next
86
+ end
87
+ end
@@ -0,0 +1,7 @@
1
+ ---
2
+ D:
3
+ - 192.168.0.1/24
4
+ - 192.168.2.1/24
5
+ E:
6
+ - 192.168.1.1/24
7
+ - 192.168.3.1/24
data/test/helper.rb ADDED
@@ -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.key?('VERBOSE')
16
+ nulllogger = Object.new
17
+ nulllogger.instance_eval do |obj|
18
+ def method_missing(method, *args)
19
+ # pass
20
+ end
21
+ end
22
+ $log = nulllogger
23
+ end
24
+
25
+ require 'fluent/plugin/out_mobile_carrier'
26
+
27
+ class Test::Unit::TestCase
28
+ end
@@ -0,0 +1,25 @@
1
+ LineLength:
2
+ Enabled: false
3
+
4
+ MethodLength:
5
+ Enabled: true
6
+ CountComments: false # count full line comments?
7
+ Max: 50
8
+
9
+ FavorSprintf:
10
+ Enabled: false
11
+
12
+ MultilineBlocks:
13
+ Enabled: false
14
+
15
+ AvoidPerlBackrefs:
16
+ Enabled: false
17
+
18
+ PercentLiterals:
19
+ Enabled: false
20
+
21
+ BraceAfterPercent:
22
+ Enabled: false
23
+
24
+ ClassLength:
25
+ Enabled: false
@@ -0,0 +1,98 @@
1
+ require 'helper'
2
+
3
+ # MobileCarrierOutput test
4
+ class Fluent::MobileCarrierOutputTest < Test::Unit::TestCase
5
+ # through & merge
6
+ CONFIG1 = %[
7
+ type mobile_carrier
8
+ remove_prefix test
9
+ add_prefix merged
10
+ config_yaml test/data/config1.yaml
11
+ key_name ip_address
12
+ ]
13
+
14
+ CONFIG2 = %[
15
+ type mobile_carrier
16
+ remove_prefix test
17
+ add_prefix merged
18
+ config_yaml test/data/config1.yaml
19
+ key_name ip
20
+ unknown_carrier unknown
21
+ out_key_mobile_carrier mc
22
+ ]
23
+
24
+ def create_driver(conf = CONFIG1, tag = 'test')
25
+ Fluent::Test::OutputTestDriver.new(Fluent::MobileCarrierOutput, tag).configure(conf)
26
+ end
27
+
28
+ def test_configure
29
+ # through & merge
30
+ d = create_driver CONFIG1
31
+ assert_equal 'ip_address', d.instance.key_name
32
+ assert_equal 'test', d.instance.remove_prefix
33
+ assert_equal 'merged', d.instance.add_prefix
34
+
35
+ assert_equal 'UNKNOWN', d.instance.unknown_carrier
36
+ assert_equal 'mobile_carrier', d.instance.out_key_mobile_carrier
37
+
38
+ # filter & merge
39
+ d = create_driver CONFIG2
40
+ assert_equal 'ip', d.instance.key_name
41
+ assert_equal 'test', d.instance.remove_prefix
42
+ assert_equal 'merged', d.instance.add_prefix
43
+
44
+ assert_equal 'unknown', d.instance.unknown_carrier
45
+ assert_equal 'mc', d.instance.out_key_mobile_carrier
46
+ end
47
+
48
+ def test_tag_mangle
49
+ p = create_driver(CONFIG1).instance
50
+ assert_equal 'merged.data', p.tag_mangle('data')
51
+ assert_equal 'merged.data', p.tag_mangle('test.data')
52
+ assert_equal 'merged.test.data', p.tag_mangle('test.test.data')
53
+ assert_equal 'merged', p.tag_mangle('test')
54
+ end
55
+
56
+ def test_emit1
57
+ d = create_driver(CONFIG1, 'test.message')
58
+ time = Time.parse('2012-07-20 16:40:30').to_i
59
+ d.run do
60
+ d.emit({ 'value' => 0 }, time)
61
+ d.emit({ 'value' => 1, 'ip_address' => '192.168.0.5' }, time)
62
+ d.emit({ 'value' => 2, 'ip_address' => '192.168.1.10' }, time)
63
+ d.emit({ 'value' => 3, 'ip_address' => '192.168.2.100' }, time)
64
+ d.emit({ 'value' => 4, 'ip_address' => '192.168.3.200' }, time)
65
+ d.emit({ 'value' => 5, 'ip_address' => '192.168.4.1' }, time)
66
+ end
67
+
68
+ emits = d.emits
69
+ assert_equal 6, emits.size
70
+ assert_equal 'merged.message', emits[0][0]
71
+ assert_equal time, emits[0][1]
72
+
73
+ m = emits[0][2]
74
+ assert_equal 0, m['value']
75
+ assert_equal 'UNKNOWN', m['mobile_carrier']
76
+
77
+ m = emits[1][2]
78
+ assert_equal 1, m['value']
79
+ assert_equal 'D', m['mobile_carrier']
80
+
81
+ m = emits[2][2]
82
+ assert_equal 2, m['value']
83
+ assert_equal 'E', m['mobile_carrier']
84
+
85
+ m = emits[3][2]
86
+ assert_equal 3, m['value']
87
+ assert_equal 'D', m['mobile_carrier']
88
+
89
+ m = emits[4][2]
90
+ assert_equal 4, m['value']
91
+ assert_equal 'E', m['mobile_carrier']
92
+
93
+ m = emits[5][2]
94
+ assert_equal 5, m['value']
95
+ assert_equal 'UNKNOWN', m['mobile_carrier']
96
+ end
97
+
98
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-mobile-carrier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - HARUYAMA Seigo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fluentd
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: judge mobile carrier by ip address.
42
+ email:
43
+ - haruyama@unixuser.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - fluent-plugin-mobile-carrier.gemspec
55
+ - lib/fluent/plugin/.rubocop.yml
56
+ - lib/fluent/plugin/out_mobile_carrier.rb
57
+ - test/data/config1.yaml
58
+ - test/helper.rb
59
+ - test/plugin/.rubocop.yml
60
+ - test/plugin/test_out_mobile_carrier.rb
61
+ homepage: http://github.com/haruyama/fluent-plugin-mobile-carrier
62
+ licenses:
63
+ - APLv2
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.0
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Fluentd plugin to judge mobile carrier by ip address.
85
+ test_files:
86
+ - test/data/config1.yaml
87
+ - test/helper.rb
88
+ - test/plugin/.rubocop.yml
89
+ - test/plugin/test_out_mobile_carrier.rb