fluent-plugin-nata2 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: 5e2a1e16f95544b74c8d97de9684ae6a77ffd538
4
+ data.tar.gz: 5ce25e1b61ccb5a1530705ab6b11c5125b6aeb1d
5
+ SHA512:
6
+ metadata.gz: deb2302a54d1b79627cb2735fd47e5b136c7304b40c2cdb02131204e2134933de987a8d3ad0a993777f8b314fcc60fcbbefeb1e61170cf65212faaa0c0d97cd8
7
+ data.tar.gz: 911d28e1c507b7975ee444bf3d6ffe3a3498fb30cd43c8aeca23dd76f212c8027f88e45820fe58cae3738fbc45c95982bd73150e4689d608bf21007d9c4116df
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ *.db
7
+ config.toml
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.1
6
+
7
+ before_install:
8
+ - gem update bundler
9
+
10
+ script:
11
+ - bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 studio3104
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # fluent-plugin-nata2 [![Build Status](https://travis-ci.org/studio3104/fluent-plugin-nata2.png)](https://travis-ci.org/studio3104/fluent-plugin-nata2) [![Code Climate](https://codeclimate.com/github/studio3104/fluent-plugin-nata2.png)](https://codeclimate.com/github/studio3104/fluent-plugin-nata2)
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fluent-plugin-nata2'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fluent-plugin-nata2
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/studio3104/fluent-plugin-nata2/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
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
+ task :default => :test
@@ -0,0 +1,26 @@
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-nata2'
7
+ spec.version = '0.0.1'
8
+ spec.authors = ['studio3104']
9
+ spec.email = ['studio3104.com@gmail.com']
10
+ spec.summary = %q{fluent-plugin to post slow query logs to Nata2 server}
11
+ spec.description = %q{fluent-plugin to post slow query logs to Nata2 server}
12
+ spec.homepage = 'https://github.com/studio3104/fluent-plugin-nata2'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'fluentd', '~> 0.10.45'
21
+ spec.add_runtime_dependency 'fluent-mixin-rewrite-tag-name'
22
+ spec.add_runtime_dependency 'mysql-slowquery-parser'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.5'
25
+ spec.add_development_dependency 'rake'
26
+ end
@@ -0,0 +1,109 @@
1
+ class Fluent::MySQLSlowQueryExInput < Fluent::NewTailInput
2
+ Fluent::Plugin.register_input('mysqlslowquery_ex', self)
3
+
4
+ config_param :dbname_if_missing_dbname_in_log, :string, default: nil
5
+ config_param :last_dbname_file, :string, defautl: nil
6
+
7
+ def initialize
8
+ super
9
+ require 'mysql-slowquery-parser'
10
+ end
11
+
12
+ def configure(conf)
13
+ conf['format'] = 'none'
14
+ super
15
+ if conf['pos_file'] == @last_dbname_file
16
+ raise Fluet::ConfigError, ''
17
+ end
18
+ end
19
+
20
+ def start
21
+ @last_dbname_of = if @last_dbname_file
22
+ @last_dbname_file_handle = File.open(@last_dbname_file, File::RDWR|File::CREAT, Fluent::DEFAULT_FILE_PERMISSION)
23
+ @last_dbname_file_handle.sync = true
24
+ get_last_dbname()
25
+ else
26
+ {}
27
+ end
28
+ super
29
+ end
30
+
31
+ def shutdown
32
+ save_last_dbname()
33
+ @last_dbname_file_handle.close if @last_dbname_file_handle
34
+ super
35
+ end
36
+
37
+ def get_last_dbname
38
+ return unless @last_dbname_file_handle
39
+ @last_dbname_file_handle.pos = 0
40
+ last_db = @last_dbname_file_handle.read.chomp
41
+ begin
42
+ JSON.parse(last_db, symbolize_names: true)
43
+ rescue JSON::ParserError
44
+ {}
45
+ end
46
+ end
47
+
48
+ def save_last_dbname
49
+ return unless @last_dbname_file_handle
50
+ current = get_last_dbname()
51
+ unless current == @last_dbname_of
52
+ @last_dbname_file_handle.pos = 0
53
+ @last_dbname_file_handle.truncate(0)
54
+ @last_dbname_file_handle.write(JSON.generate(current.merge(@last_dbname_of)))
55
+ end
56
+ end
57
+
58
+ def parser
59
+ MySQLSlowQueryParser
60
+ end
61
+
62
+ def receive_lines(lines, tail_watcher)
63
+ es = Fluent::MultiEventStream.new
64
+
65
+ prepare_lines_to_parse(lines).each do |query_unit|
66
+ begin
67
+ parsed_query_unit = parser.parse_slow_log(query_unit)
68
+ rescue
69
+ log.warn %Q{in_mysqlslowquery_ex: parse error: #{$!.message}, (#{query_unit.to_s})}
70
+ next
71
+ end
72
+ parsed_query = apply_dbname_to_record(parsed_query_unit)
73
+ es.add(Time.now.to_i, parsed_query)
74
+ save_last_dbname()
75
+ end
76
+
77
+ if !es.empty?
78
+ begin
79
+ Fluent::Engine.emit_stream(@tag, es)
80
+ rescue
81
+ # ignore errors. Engine shows logs and backtraces.
82
+ end
83
+ end
84
+ end
85
+
86
+ def prepare_lines_to_parse(lines, slow_queries = [])
87
+ @query_unit = [] unless @query_unit
88
+ while !lines.empty?
89
+ line = lines.shift
90
+ @query_unit << line
91
+ if line.end_with?(';', ";\n") && !line.start_with?('use ', 'SET timestamp=')
92
+ slow_queries << @query_unit
93
+ @query_unit = nil
94
+ prepare_lines_to_parse(lines, slow_queries)
95
+ break # For when refactoring. Just in case.
96
+ end
97
+ end
98
+ slow_queries
99
+ end
100
+
101
+ def apply_dbname_to_record(parsed_query)
102
+ database_name = parsed_query[:db] || parsed_query[:schema] || @last_dbname_of[@path.to_sym] || @dbname_if_missing_dbname_in_log
103
+ @last_dbname_of[@path.to_sym] = database_name
104
+ parsed_query[:database] = database_name
105
+ parsed_query.delete(:db)
106
+ parsed_query.delete(:schema)
107
+ parsed_query
108
+ end
109
+ end
@@ -0,0 +1,55 @@
1
+ require 'fluent/mixin/rewrite_tag_name'
2
+
3
+ class Fluent::Nata2Output < Fluent::Output
4
+ Fluent::Plugin.register_output('nata2', self)
5
+
6
+ include Fluent::HandleTagNameMixin
7
+ include Fluent::Mixin::RewriteTagName
8
+
9
+ config_param :server, :string
10
+ config_param :port, :integer
11
+
12
+ def initialize
13
+ super
14
+ require 'net/http'
15
+ require 'uri'
16
+ end
17
+
18
+ def configure(conf)
19
+ super
20
+ @url = 'http://' + @server + ':' + @port.to_s
21
+ end
22
+
23
+ def emit(tag, es, chain)
24
+ es.each do |time, record|
25
+ emit_tag = tag.clone
26
+ filter_record(emit_tag, time, record)
27
+ service_name, host_name, database_name = prepare_data_to_post(emit_tag, record)
28
+ post_to_nata2(service_name, host_name, database_name, record)
29
+ end
30
+ chain.next
31
+ end
32
+
33
+ def prepare_data_to_post(tag, record)
34
+ tag = tag.split('.')
35
+ service_name = tag.shift
36
+ host_name = tag.join('.')
37
+ database_name = record[:database] || record['database']
38
+ [ service_name, host_name, database_name ]
39
+ end
40
+
41
+ def post_to_nata2(service_name, host_name, database_name, record)
42
+ api = URI.parse(@url + %Q{/api/1/#{service_name}/#{host_name}/#{database_name}})
43
+ begin
44
+ request = Net::HTTP::Post.new(api.path)
45
+ request.set_form_data(record)
46
+ http = Net::HTTP.new(api.host, api.port)
47
+ response = http.start.request(request)
48
+ rescue IOError, EOFError, SystemCallError => e
49
+ log.warn %Q{net/http POST raises exception: #{e.class}, '#{e.message}'}
50
+ end
51
+ if !response || !response.is_a?(Net::HTTPSuccess)
52
+ log.warn %Q{failed to post to nata2: #{api}, sql: #{record[:sql]}, code: #{response && response.code}}
53
+ end
54
+ end
55
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts 'Run `bundle install` to install missing gems'
9
+ exit e.status_code
10
+ end
11
+ require 'test/unit'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'fluent/test'
16
+ require 'fluent/plugin/in_tail'
17
+ unless ENV.has_key?('VERBOSE')
18
+ nulllogger = Object.new
19
+ nulllogger.instance_eval {|obj|
20
+ def method_missing(method, *args)
21
+ # pass
22
+ end
23
+ }
24
+ $log = nulllogger
25
+ end
26
+
27
+ require 'fluent/plugin/in_mysqlslowquery_ex'
28
+
29
+ class Test::Unit::TestCase
30
+ end
@@ -0,0 +1,13 @@
1
+ require 'helper'
2
+
3
+ class MySQLSlowQueryInputTest < Test::Unit::TestCase
4
+ CONFIG = %[
5
+ ]
6
+
7
+ def create_driver(conf = CONFIG_NAME, tag='test')
8
+ Fluent::Test::InputTestDriver.new(Fluent::MySQLSlowQueryExInput, tag).configure(conf)
9
+ end
10
+
11
+ def test_configure
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'helper'
2
+
3
+ class Nata2OutputTest < Test::Unit::TestCase
4
+ CONFIG = %[
5
+ ]
6
+
7
+ def create_driver(conf = CONFIG_NAME, tag='test')
8
+ Fluent::Test::OutputTestDriver.new(Fluent::Nata2put, tag).configure(conf)
9
+ end
10
+
11
+ def test_configure
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-nata2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - studio3104
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-15 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.10.45
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.45
27
+ - !ruby/object:Gem::Dependency
28
+ name: fluent-mixin-rewrite-tag-name
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
+ - !ruby/object:Gem::Dependency
42
+ name: mysql-slowquery-parser
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: fluent-plugin to post slow query logs to Nata2 server
84
+ email:
85
+ - studio3104.com@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - fluent-plugin-nata2.gemspec
97
+ - lib/fluent/plugin/in_mysqlslowquery_ex.rb
98
+ - lib/fluent/plugin/out_nata2.rb
99
+ - test/helper.rb
100
+ - test/plugin/test_in_mysqlslowquery_ex.rb
101
+ - test/plugin/test_out_nata2.rb
102
+ homepage: https://github.com/studio3104/fluent-plugin-nata2
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: fluent-plugin to post slow query logs to Nata2 server
126
+ test_files:
127
+ - test/helper.rb
128
+ - test/plugin/test_in_mysqlslowquery_ex.rb
129
+ - test/plugin/test_out_nata2.rb