fluent-plugin-cloudfront-log 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e41573e85cd6b33115e5f1525475c09c8668c099
4
+ data.tar.gz: ade5e4dd5a8ba5bdcc52de52396840ea4fd8ef0e
5
+ SHA512:
6
+ metadata.gz: 65fcc533f4fb58e998a60a2b6bbc9e9e48088b1b7c8d5268b25fd0aec426ed3a0e512100a7abe9255816c3e4b49544a0db015ef85e6ce36c9c36fcc7a7b8d43a
7
+ data.tar.gz: 353fb1744667a60477e2cb03b91d7007442766823922afeac18eca682b7bad2ac6fce969205adf7b0edaddaa5c52e554f4fea7e145e21fa2e12881e8dec50f91
@@ -0,0 +1,40 @@
1
+ # Created by https://www.gitignore.io/api/ruby
2
+
3
+ ### Ruby ###
4
+ *.gem
5
+ *.rbc
6
+ /.config
7
+ /coverage/
8
+ /InstalledFiles
9
+ /pkg/
10
+ /spec/reports/
11
+ /spec/examples.txt
12
+ /test/tmp/
13
+ /test/version_tmp/
14
+ /tmp/
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+
21
+ ## Documentation cache and generated files:
22
+ /.yardoc/
23
+ /_yardoc/
24
+ /doc/
25
+ /rdoc/
26
+
27
+ ## Environment normalization:
28
+ /.bundle/
29
+ /vendor/bundle
30
+ /lib/bundler/man/
31
+
32
+ # for a library or gem, you might want to ignore these files since the code is
33
+ # intended to run in multiple environments; otherwise, check them in:
34
+ Gemfile.lock
35
+ .ruby-version
36
+ .ruby-gemset
37
+
38
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
39
+ .rvmrc
40
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-cloudfront-log.gemspec
4
+ gemspec
@@ -0,0 +1,36 @@
1
+ # Fluent::Plugin::Cloudfront::Log
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fluent/plugin/cloudfront/log`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fluent-plugin-cloudfront-log'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fluent-plugin-cloudfront-log
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fluent-plugin-cloudfront-log.
36
+
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rake/testtask"
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
8
+ end
9
+
10
+
11
+ task :default => :test
@@ -0,0 +1,25 @@
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-cloudfront-log"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["kubihie"]
9
+ spec.email = ["kubihie@gmail.com"]
10
+
11
+ spec.summary = %q{AWS CloudFront log input plugin.}
12
+ spec.description = %q{AWS CloudFront log input plugin for fluentd.}
13
+ spec.homepage = "https://github.com/kubihie/fluent-plugin-cloudfront-log"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "fluentd", "~> 0"
21
+ spec.add_dependency "aws-sdk", "~> 2.1"
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency 'test-unit', "~> 2"
25
+ end
@@ -0,0 +1,129 @@
1
+ class Fluent::Cloudfront_LogInput < Fluent::Input
2
+ Fluent::Plugin.register_input('cloudfront_log', self)
3
+
4
+ config_param :aws_key_id, :string, :default => nil, :secret => true
5
+ config_param :aws_sec_key, :string, :default => nil, :secret => true
6
+ config_param :log_bucket, :string
7
+ config_param :log_prefix, :string, :default => nil
8
+ config_param :moved_log_bucket, :string, :default => @log_bucket
9
+ config_param :moved_log_prefix, :string, :default => '_moved'
10
+ config_param :region, :string
11
+ config_param :tag, :string, :default => 'cloudfront.access'
12
+ config_param :interval, :integer, :default => 300
13
+
14
+ def initialize
15
+ super
16
+ require 'logger'
17
+ require 'aws-sdk'
18
+ require 'zlib'
19
+ require 'time'
20
+ require 'uri'
21
+ end
22
+
23
+ def configure(conf)
24
+ super
25
+
26
+ raise Fluent::ConfigError.new unless @log_bucket
27
+ raise Fluent::ConfigError.new unless @region
28
+
29
+ @moved_log_bucket = @log_bucket unless @moved_log_bucket
30
+ @moved_log_prefix = @log_prefix + '_moved' unless @moved_log_prefix
31
+
32
+ log.info('@log_bucket:' + @log_bucket)
33
+ log.info('@moved_log_bucket:' + @moved_log_bucket)
34
+ log.info('@log_prefix:' + @log_prefix)
35
+ log.info('@moved_log_prefix:' + @moved_log_prefix)
36
+ end
37
+
38
+ def start
39
+ super
40
+ client
41
+
42
+ @loop = Coolio::Loop.new
43
+ #timer = TimerWatcher.new(@interval, true, log) do
44
+ # input
45
+ #end
46
+ timer = TimerWatcher.new(@interval, true, log, &method(:input))
47
+
48
+ @loop.attach(timer)
49
+ @thread = Thread.new(&method(:run))
50
+ end
51
+
52
+ def shutdown
53
+ @loop.stop
54
+ @thread.join
55
+ end
56
+
57
+ def run
58
+ @loop.run
59
+ end
60
+
61
+ def client
62
+ begin
63
+ options = {:region => @region}
64
+ if @aws_key_id and @aws_sec_key
65
+ options[:access_key_id] = @aws_key_id
66
+ options[:secret_access_key] = @aws_sec_key
67
+ end
68
+ @client = Aws::S3::Client.new(options)
69
+ rescue => e
70
+ log.warn("S3 client error. #{e.message}")
71
+ end
72
+ end
73
+
74
+ def parse_header(line)
75
+ case line
76
+ when /^#Version:.+/i then
77
+ @version = line.sub(/^#Version:/i, '').strip
78
+ when /^#Fields:.+/i then
79
+ @fields = line.sub(/^#Fields:/i, '').strip.split("\s")
80
+ end
81
+ end
82
+
83
+ def purge(filename)
84
+ log.info('copy_src:' + @log_bucket + '/' + @log_prefix + filename)
85
+ log.info('copy_dst:' + @moved_log_bucket + '/' + @moved_log_prefix + '/' + filename)
86
+ client.copy_object(:bucket => @moved_log_bucket, :copy_source => @log_bucket + '/' + @log_prefix + filename, :key => @moved_log_prefix + '/' + filename)
87
+ client.delete_object(:bucket => @log_bucket, :key => @log_prefix + filename)
88
+ end
89
+
90
+ def input
91
+ client.list_objects(:bucket => @log_bucket, :prefix => @log_prefix , :delimiter => '/').each do |list|
92
+ list.contents.each do |content|
93
+ filename = content.key.sub(/#{@log_prefix}/, '')
94
+ #log.info('filename:' + filename)
95
+ next if filename[-1] == '/' #skip directory/
96
+ next unless filename[-2, 2] == 'gz' #skip without gz file
97
+
98
+ access_log_gz = client.get_object(:bucket => @log_bucket, :key => content.key).body
99
+ access_log = Zlib::GzipReader.new(access_log_gz).read
100
+
101
+ access_log.split("\n").each do |line|
102
+ if line[0.1] == '#'
103
+ parse_header(line)
104
+ next
105
+ end
106
+ line = URI.unescape(line) #hoge%2520fuga -> hoge%20fuga
107
+ line = URI.unescape(line) #hoge%20fuga -> hoge fuga
108
+ line = line.split("\t")
109
+ record = Hash[@fields.collect.zip(line)]
110
+ timestamp = Time.parse("#{record['date']} #{record['time']} +0900").to_i
111
+ router.emit(@tag, timestamp, record)
112
+ end
113
+ purge(filename)
114
+ end
115
+ end
116
+ end
117
+
118
+ class TimerWatcher < Coolio::TimerWatcher
119
+ def initialize(interval, repeat, log, &callback)
120
+ @callback = callback
121
+ @log = log
122
+ super(interval, repeat)
123
+ end
124
+
125
+ def on_timer
126
+ @callback.call
127
+ end
128
+ end
129
+ end
@@ -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/in_cloudfront_log'
26
+
27
+ class Test::Unit::TestCase
28
+ end
@@ -0,0 +1,57 @@
1
+ require_relative '../helper'
2
+ require 'fluent/test'
3
+
4
+ class Cloudfront_LogInputTest < Test::Unit::TestCase
5
+ def setup
6
+ Fluent::Test.setup
7
+ end
8
+
9
+ DEFAULT_CONFIG = {
10
+ :aws_key_id => 'AKIAZZZZZZZZZZZZZZZZ',
11
+ :aws_sec_key => '1234567890qwertyuiopasdfghjklzxcvbnm',
12
+ :log_bucket => 'bucket-name',
13
+ :log_prefix => 'a/b/c',
14
+ :moved_log_bucket => 'bucket-name-moved',
15
+ :moved_log_prefix => 'a/b/c_moved',
16
+ :region => 'ap-northeast-1',
17
+ :tag => 'cloudfront',
18
+ :interval => '500'
19
+ }
20
+
21
+ def parse_config(conf = {})
22
+ ''.tap{|s| conf.each { |k, v| s << "#{k} #{v}\n" } }
23
+ end
24
+
25
+ def create_driver(conf = DEFAULT_CONFIG)
26
+ Fluent::Test::InputTestDriver.new(Fluent::Cloudfront_LogInput).configure(parse_config conf)
27
+ end
28
+
29
+ def test_configure
30
+ assert_nothing_raised { driver = create_driver }
31
+
32
+ exception = assert_raise(Fluent::ConfigError) {
33
+ conf = DEFAULT_CONFIG.clone
34
+ conf.delete(:log_bucket)
35
+ driver = create_driver(conf)
36
+ }
37
+ assert_equal("'log_bucket' parameter is required", exception.message)
38
+
39
+ exception = assert_raise(Fluent::ConfigError) {
40
+ conf = DEFAULT_CONFIG.clone
41
+ conf.delete(:region)
42
+ driver = create_driver(conf)
43
+ }
44
+ assert_equal("'region' parameter is required", exception.message)
45
+
46
+ conf = DEFAULT_CONFIG.clone
47
+ conf.delete(:moved_log_bucket)
48
+ driver = create_driver(conf)
49
+ assert_equal(driver.instance.log_bucket, driver.instance.moved_log_bucket)
50
+
51
+ conf = DEFAULT_CONFIG.clone
52
+ conf.delete(:moved_log_prefix)
53
+ driver = create_driver(conf)
54
+ assert_equal('_moved', driver.instance.moved_log_prefix)
55
+ end
56
+
57
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-cloudfront-log
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kubihie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-01 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'
20
+ type: :runtime
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: aws-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '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: '2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2'
83
+ description: AWS CloudFront log input plugin for fluentd.
84
+ email:
85
+ - kubihie@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - README.md
93
+ - Rakefile
94
+ - fluent-plugin-cloudfront-log.gemspec
95
+ - lib/fluent/plugin/in_cloudfront_log.rb
96
+ - test/helper.rb
97
+ - test/plugin/test_in_cloudfrontlog.rb
98
+ homepage: https://github.com/kubihie/fluent-plugin-cloudfront-log
99
+ licenses: []
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.2.0
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: AWS CloudFront log input plugin.
121
+ test_files:
122
+ - test/helper.rb
123
+ - test/plugin/test_in_cloudfrontlog.rb