fluent-plugin-gamobile 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-boundio.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (c) 2012- Kentaro Yoshida
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.
14
+
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ fluent-plugin-gamobile
2
+ =====================
3
+
4
+ ## Component
5
+ Fluentd Output plugin to send access report with "Google Analytics for mobile".
6
+
7
+ ## Installation
8
+
9
+ ### native gem
10
+ `````
11
+ gem install fluent-plugin-gamobile
12
+ `````
13
+
14
+ ### td-agent gem
15
+ `````
16
+ /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-gamobile
17
+ `````
18
+
19
+ ## Configuration
20
+
21
+ ### Sample
22
+ Please setup "gem install fluent-plugin-rewrite-tag-filter" before trying this sample.
23
+ `````
24
+ <source>
25
+ type tail
26
+ path /var/log/httpd/access_log
27
+ format /^(?<domain>[^ ]*) (?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<status>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" (?<response_time>[^ ]*))?$/
28
+ time_format %d/%b/%Y:%H:%M:%S %z
29
+ tag td.apache.access
30
+ pos_file /var/log/td-agent/apache_access.pos
31
+ </source>
32
+
33
+ <match td.apache.access>
34
+ type copy
35
+ <store>
36
+ type rewrite_tag_filter
37
+ rewriterule1 agent (spider|bot|crawler|\+http\:) apache.access.robot
38
+ </store>
39
+ </match>
40
+
41
+ <match apache.access.robot>
42
+ type gamobile
43
+ ga_account MO-12345678-1
44
+ # set UserVar from record
45
+ set_var agent # Optional (default none)
46
+ # mapping internal name with record
47
+ map_domain domain # Optional (default: domain)
48
+ map_remoteaddr host # Optional (default: host)
49
+ map_path path # Optional (default: path)
50
+ map_referer referer # Optional (default: referer)
51
+ map_useragent agent # Optional (default: agent)
52
+ map_guid guid # Optional (default: guid)
53
+ map_acceptlang lang # Optional (default: lang)
54
+ </match>
55
+ `````
56
+
57
+ ## Use Case
58
+ * track crawler access activity
59
+ * track internal api access activity
60
+
61
+ ## Backend Service
62
+ http://www.google.com/intl/ja/analytics/
63
+
64
+ ## TODO
65
+ patches welcome!
66
+
67
+ ## Copyright
68
+ Copyright © 2012- Kentaro Yoshida (@yoshi_ken)
69
+
70
+ ## License
71
+ Apache License, Version 2.0
72
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ Rake::TestTask.new(:test) do |test|
4
+ test.libs << 'lib' << 'test'
5
+ test.pattern = 'test/**/test_*.rb'
6
+ test.verbose = true
7
+ end
8
+
9
+ task :default => :test
10
+
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "fluent-plugin-gamobile"
6
+ s.version = "0.0.1"
7
+ s.authors = ["Kentaro Yoshida"]
8
+ s.email = ["y.ken.studio@gmail.com"]
9
+ s.homepage = "https://github.com/y-ken/fluent-plugin-gamobile"
10
+ s.summary = %q{Fluentd Output plugin to send access report with "Google Analytics for mobile".}
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ s.require_paths = ["lib"]
16
+
17
+ # specify any dependencies here; for example:
18
+ s.add_development_dependency "fluentd"
19
+ s.add_runtime_dependency "fluentd"
20
+ s.add_runtime_dependency "active_support"
21
+ s.add_runtime_dependency "i18n"
22
+ end
@@ -0,0 +1,93 @@
1
+ class Fluent::GamobileOutput < Fluent::Output
2
+ Fluent::Plugin.register_output('gamobile', self)
3
+
4
+ config_param :ga_account, :string
5
+ config_param :set_var, :string, :default => nil
6
+ config_param :map_domain, :string, :default => 'domain'
7
+ config_param :map_remoteaddr, :string, :default => 'host'
8
+ config_param :map_path, :string, :default => 'path'
9
+ config_param :map_referer, :string, :default => 'referer'
10
+ config_param :map_useragent, :string, :default => 'agent'
11
+ config_param :map_guid, :string, :default => 'guid'
12
+ config_param :map_acceptlang, :string, :default => 'lang'
13
+
14
+ def initialize
15
+ super
16
+ require 'net/http'
17
+ require 'active_support/core_ext'
18
+ Net::HTTP.version_1_2
19
+ end
20
+
21
+ def configure(conf)
22
+ super
23
+ end
24
+
25
+ def emit(tag, es, chain)
26
+ es.each do |time,record|
27
+ report(record)
28
+ end
29
+
30
+ chain.next
31
+ end
32
+
33
+ def set_record(record)
34
+ @record = record
35
+ end
36
+
37
+ def get_record(key)
38
+ return @record[key] unless @record[key].blank?
39
+ end
40
+
41
+ def get_remote_address
42
+ if get_record(@map_remoteaddr) =~ /^([^.]+\.[^.]+\.[^.]+\.).*/
43
+ return "#{$1}0"
44
+ else
45
+ return ''
46
+ end
47
+ end
48
+
49
+ def get_visitor_id
50
+ if get_record(@map_guid).blank?
51
+ message = "#{get_record(@map_useragent)}#{Digest::SHA1.hexdigest(rand.to_s)}#{Time.now.to_i}"
52
+ else
53
+ message = "#{get_record(@map_guid)}#{@ga_account}"
54
+ end
55
+ md5string = Digest::MD5.hexdigest(message)
56
+ return "0x#{md5string[0,16]}"
57
+ end
58
+
59
+ def get_utmv
60
+ return ERB::Util.u("+__utmv=999#{get_record(@set_var)};") unless get_record(@set_var).blank?
61
+ end
62
+
63
+ def build_query
64
+ utm_gif_location = 'http://www.google-analytics.com/__utm.gif'
65
+ queries = Array.new
66
+ queries << "utmwv=4.4sh"
67
+ queries << "utmn=#{rand(1000000*1000000)}"
68
+ queries << "utmhn=#{ERB::Util.u(get_record(@map_domain))}"
69
+ queries << "utmr=#{ERB::Util.u(get_record(@map_referer))}"
70
+ queries << "utmp=#{ERB::Util.u(get_record(@map_path))}"
71
+ queries << "utmac=#{@ga_account}"
72
+ queries << "utmcc=__utma%3D999.999.999.999.999.1%3B#{get_utmv}"
73
+ queries << "utmvid=#{get_visitor_id}"
74
+ queries << "utmip=#{get_remote_address}"
75
+ return URI.parse(utm_gif_location + '?' + queries.join('&'))
76
+ end
77
+
78
+ def report(record)
79
+ set_record(record)
80
+ begin
81
+ uri = build_query
82
+ $log.info "gamobile sending report: #{uri.to_s}"
83
+ Net::HTTP.start(uri.host, uri.port) do |http|
84
+ http.get(uri.request_uri, {
85
+ "user_agent" => get_record(@map_useragent).to_s,
86
+ "Accepts-Language" => get_record(@map_acceptlang).to_s
87
+ })
88
+ end
89
+ rescue => e
90
+ $log.error("gamobile Error: #{e.message}")
91
+ end
92
+ end
93
+ end
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.has_key?('VERBOSE')
16
+ nulllogger = Object.new
17
+ nulllogger.instance_eval {|obj|
18
+ def method_missing(method, *args)
19
+ # pass
20
+ end
21
+ }
22
+ $log = nulllogger
23
+ end
24
+
25
+ require 'fluent/plugin/out_gamobile'
26
+
27
+ class Test::Unit::TestCase
28
+ end
@@ -0,0 +1,40 @@
1
+ require 'helper'
2
+
3
+ class GamobileOutputTest < Test::Unit::TestCase
4
+ def setup
5
+ Fluent::Test.setup
6
+ end
7
+
8
+ CONFIG = %[
9
+ ga_account MO-1234567-1
10
+ set_var agent
11
+ ]
12
+
13
+ def create_driver(conf=CONFIG,tag='test')
14
+ Fluent::Test::OutputTestDriver.new(Fluent::GamobileOutput, tag).configure(conf)
15
+ end
16
+
17
+ def test_configure
18
+ assert_raise(Fluent::ConfigError) {
19
+ d = create_driver('')
20
+ }
21
+ d = create_driver %[
22
+ ga_account MO-1234567-1
23
+ set_var agent
24
+ ]
25
+ d.instance.inspect
26
+ assert_equal 'MO-1234567-1', d.instance.ga_account
27
+ assert_equal 'agent', d.instance.set_var
28
+ end
29
+
30
+ def test_emit
31
+ d1 = create_driver(CONFIG, 'input.access')
32
+ time = Time.parse("2012-01-02 13:14:15").to_i
33
+ d1.run do
34
+ d1.emit({'message' => 'sample message'})
35
+ end
36
+ emits = d1.emits
37
+ assert_equal 0, emits.length
38
+ end
39
+ end
40
+
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-gamobile
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kentaro Yoshida
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fluentd
16
+ requirement: &19257400 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *19257400
25
+ - !ruby/object:Gem::Dependency
26
+ name: fluentd
27
+ requirement: &19256960 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *19256960
36
+ - !ruby/object:Gem::Dependency
37
+ name: active_support
38
+ requirement: &19256540 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *19256540
47
+ - !ruby/object:Gem::Dependency
48
+ name: i18n
49
+ requirement: &19256120 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *19256120
58
+ description:
59
+ email:
60
+ - y.ken.studio@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - fluent-plugin-gamobile.gemspec
71
+ - lib/fluent/plugin/out_gamobile.rb
72
+ - test/helper.rb
73
+ - test/plugin/test_out_gamobile.rb
74
+ homepage: https://github.com/y-ken/fluent-plugin-gamobile
75
+ licenses: []
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.11
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Fluentd Output plugin to send access report with "Google Analytics for mobile".
98
+ test_files:
99
+ - test/helper.rb
100
+ - test/plugin/test_out_gamobile.rb