fluent-plugin-mysqlslowquerylog-ippen-digital 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ # For TextMate, emacs, vim
6
+ *.tmproj
7
+ tmtags
8
+ *~
9
+ \#*
10
+ .\#*
11
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-mysqlslowquerylog.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,2 @@
1
+ Copyright (c) 2012 Satoshi SUZUKI
2
+ Apache License, Version 2.0
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # fluent-plugin-mysqlslowquerylog
2
+
3
+ ## Component
4
+
5
+ ### MySQL Slow Query Log Output
6
+
7
+ Fluentd plugin to concat MySQL slowquerylog.
8
+
9
+ ## Configuration
10
+
11
+ Input Messages (Slow Query Log)
12
+ ```
13
+ # Time: 130107 11:36:21
14
+ # User@Host: root[root] @ localhost []
15
+ # Query_time: 0.000378 Lock_time: 0.000111 Rows_sent: 7 Rows_examined: 7
16
+ SET timestamp=1357526181;
17
+ select * from user;
18
+ # Time: 130107 11:38:47
19
+ # User@Host: root[root] @ localhost []
20
+ # Query_time: 0.002142 Lock_time: 0.000166 Rows_sent: 142 Rows_examined: 142
21
+ use information_schema;
22
+ SET timestamp=1357526327;
23
+ select * from INNODB_BUFFER_PAGE_LRU;
24
+ ```
25
+
26
+ Output Messages
27
+ ```
28
+ 2013-01-07T11:36:21+09:00 cocatenated.mysql.slowlog {"user":"root[root]","host":"localhost","query_time":0.000378,"lock_time":0.000111,"rows_sent":7,"rows_examined":7,"sql":"SET timestamp=1357526181; select * from user;"}
29
+ 2013-01-07T11:38:47+09:00 cocatenated.mysql.slowlog {"user":"root[root]","host":"localhost","query_time":0.002142,"lock_time":0.000166,"rows_sent":142,"rows_examined":142,"sql":"use information_schema; SET timestamp=1357526327; select * from INNODB_BUFFER_PAGE_LRU;"}
30
+ ```
31
+
32
+ ### Example Settings
33
+ sender (fluent-agent-lite)
34
+ ```
35
+ TAG_PREFIX="mysql"
36
+ LOGS=$(cat <<"EOF"
37
+ slowlog.db01 /var/log/mysql/mysql-slow.log
38
+ EOF
39
+ )
40
+ PRIMARY_SERVER="log_server:24224"
41
+ ```
42
+
43
+ sender (td-agent)
44
+ ```
45
+ <source>
46
+ type tail
47
+ path /var/log/mysql/mysql-slow.log
48
+ format /^(?<message>.+)$/
49
+ tag mysql.slowlog.db01
50
+ </source>
51
+ <match>
52
+ type forward
53
+ host log_server
54
+ </match>
55
+ ```
56
+
57
+ reciever
58
+ ```
59
+ <source>
60
+ type forward
61
+ </source>
62
+ <match mysql.slowlog.*>
63
+ type mysqlslowquerylog
64
+ add_tag_prefix cocatenated.
65
+ </match>
66
+ <match cocatenated.mysql.slowlog.*>
67
+ type file
68
+ path /tmp/slowtest
69
+ </match>
70
+ ```
71
+
72
+ ## Installation
73
+
74
+ Add this line to your application's Gemfile:
75
+
76
+ gem 'fluent-plugin-mysqlslowquerylog'
77
+
78
+ And then execute:
79
+
80
+ $ bundle
81
+
82
+ Or install it yourself as:
83
+
84
+ $ gem install fluent-plugin-mysqlslowquerylog
85
+
86
+ ## Contributing
87
+
88
+ 1. Fork it
89
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
90
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
91
+ 4. Push to the branch (`git push origin my-new-feature`)
92
+ 5. Create new Pull Request
93
+
94
+ ## Copyright
95
+
96
+ ### Copyright
97
+ Copyright (c) 2012- Satoshi SUZUKI (@studio3104)
98
+
99
+ ### License
100
+ Apache License, Version 2.0
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,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "fluent-plugin-mysqlslowquerylog-ippen-digital"
5
+ gem.version = "0.0.3"
6
+ gem.authors = ["Satoshi SUZUKI", "Gerhard Strauss"]
7
+ gem.email = ["studio3104.com@gmail.com", "gerhard.strauss@ippen-digital.de"]
8
+ gem.description = %q{Fluentd plugin to concat MySQL slowquerylog.}
9
+ gem.summary = %q{Fluentd plugin to concat MySQL slowquerylog.}
10
+ gem.homepage = "https://github.com/gerhard-strauss/fluent-plugin-mysqlslowquerylog"
11
+
12
+ gem.files = `git ls-files`.split($/)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.require_paths = ["lib"]
16
+
17
+ gem.add_development_dependency "fluentd"
18
+ gem.add_runtime_dependency "fluentd"
19
+ end
@@ -0,0 +1,86 @@
1
+ class Fluent::MySQLSlowQueryLogOutput < Fluent::Output
2
+ Fluent::Plugin.register_output('mysqlslowquerylog', self)
3
+ include Fluent::HandleTagNameMixin
4
+
5
+ def configure(conf)
6
+ super
7
+ @slowlogs = {}
8
+
9
+ if !@remove_tag_prefix && !@remove_tag_suffix && !@add_tag_prefix && !@add_tag_suffix
10
+ raise ConfigError, "out_myslowquerylog: At least one of option, remove_tag_prefix, remove_tag_suffix, add_tag_prefix or add_tag_suffix is required to be set."
11
+ end
12
+ end
13
+
14
+ def start
15
+ super
16
+ end
17
+
18
+ def shutdown
19
+ super
20
+ end
21
+
22
+ def emit(tag, es, chain)
23
+ if !@slowlogs[:"#{tag}"].instance_of?(Array)
24
+ @slowlogs[:"#{tag}"] = []
25
+ end
26
+ es.each do |time, record|
27
+ concat_messages(tag, time, record)
28
+ end
29
+
30
+ chain.next
31
+ end
32
+
33
+ def concat_messages(tag, time, record)
34
+ record.each do |key, value|
35
+ @slowlogs[:"#{tag}"] << value
36
+ if value.end_with?(';') && !value.upcase.start_with?('USE ', 'SET TIMESTAMP=')
37
+ parse_message(tag, time)
38
+ end
39
+ end
40
+ end
41
+
42
+ REGEX1 = /^#? User\@Host:\s+(\S+)\s+\@\s+(\S+).*/
43
+ REGEX2 = /^# Query_time: ([0-9.]+)\s+Lock_time: ([0-9.]+)\s+Rows_sent: ([0-9.]+)\s+Rows_examined: ([0-9.]+).*/
44
+ def parse_message(tag, time)
45
+ record = {}
46
+ date = nil
47
+
48
+ # Skip the message that is output when after flush-logs or restart mysqld.
49
+ # e.g.) /usr/sbin/mysqld, Version: 5.5.28-0ubuntu0.12.04.2-log ((Ubuntu)). started with:
50
+ begin
51
+ message = @slowlogs[:"#{tag}"].shift
52
+ end while !message.start_with?('#')
53
+
54
+ if message.start_with?('# Time: ')
55
+ date = Time.parse(message[8..-1].strip)
56
+ message = @slowlogs[:"#{tag}"].shift
57
+ end
58
+
59
+ message =~ REGEX1
60
+ record['user'] = $1
61
+ record['host'] = $2
62
+ message = @slowlogs[:"#{tag}"].shift
63
+
64
+ message =~ REGEX2
65
+ record['query_time'] = $1.to_f
66
+ record['lock_time'] = $2.to_f
67
+ record['rows_sent'] = $3.to_i
68
+ record['rows_examined'] = $4.to_i
69
+
70
+ record['sql'] = @slowlogs[:"#{tag}"].map {|m| m.strip}.join(' ')
71
+
72
+ time = date.to_i if date
73
+ flush_emit(tag, time, record)
74
+ end
75
+
76
+ def flush_emit(tag, time, record)
77
+ @slowlogs[:"#{tag}"].clear
78
+ _tag = tag.clone
79
+ filter_record(_tag, time, record)
80
+ if tag != _tag
81
+ router.emit(_tag, time, record)
82
+ else
83
+ $log.warn "Can not emit message because the tag has not changed. Dropped record #{record}"
84
+ end
85
+ end
86
+ 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_mysqlslowquerylog'
26
+
27
+ class Test::Unit::TestCase
28
+ end
@@ -0,0 +1,129 @@
1
+ require 'helper'
2
+
3
+ class MySQLSlowQueryLogOutputTest < Test::Unit::TestCase
4
+ def setup
5
+ Fluent::Test.setup
6
+ end
7
+
8
+ CONFIG = %[
9
+ add_tag_prefix cocatenated.
10
+ ]
11
+
12
+ def create_driver(conf = CONFIG, tag='test')
13
+ Fluent::Test::OutputTestDriver.new(Fluent::MySQLSlowQueryLogOutput, tag).configure(conf)
14
+ end
15
+
16
+ def test_emit
17
+ 1.times do
18
+
19
+ d1 = create_driver
20
+ d2 = create_driver(CONFIG,'test2')
21
+ d3 = create_driver(CONFIG,'test3')
22
+ d4 = create_driver(CONFIG,'test4')
23
+
24
+ d1.run do
25
+ d1.emit('message' => "/usr/sbin/mysqld, Version: 5.5.28-0ubuntu0.12.04.2-log ((Ubuntu)). started with:")
26
+ d1.emit('message' => "Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock")
27
+ d1.emit('message' => "Time Id Command Argument")
28
+ d1.emit('message' => "# Time: 130105 16:43:42")
29
+ d1.emit('message' => "# User@Host: debian-sys-maint[debian-sys-maint] @ localhost []")
30
+ d1.emit('message' => "# Query_time: 0.000167 Lock_time: 0.000057 Rows_sent: 1 Rows_examined: 7")
31
+ d1.emit('message' => "SET timestamp=1357371822;")
32
+ d1.emit('message' => "SELECT count(*) FROM mysql.user WHERE user='root' and password='';")
33
+ end
34
+
35
+ d2.run do
36
+ d2.emit('message' => "# User@Host: debian-sys-maint[debian-sys-maint] @ localhost []")
37
+ d2.emit('message' => "# Query_time: 0.002998 Lock_time: 0.000078 Rows_sent: 31 Rows_examined: 81")
38
+ d2.emit('message' => "SET timestamp=61357371822;")
39
+ d2.emit('message' => "select concat('select count(*) into @discard from `',")
40
+ d2.emit('message' => " TABLE_SCHEMA, '`.`', TABLE_NAME, '`')")
41
+ d2.emit('message' => " from information_schema.TABLES where ENGINE='MyISAM';")
42
+
43
+ d2.emit('message' => "# Time: 130105 18:04:21")
44
+ d2.emit('message' => "# User@Host: root[root] @ localhost []")
45
+ d2.emit('message' => "# Query_time: 0.000398 Lock_time: 0.000117 Rows_sent: 7 Rows_examined: 7")
46
+ d2.emit('message' => "use mysql;")
47
+ d2.emit('message' => "SET timestamp=1357376661;")
48
+ d2.emit('message' => "select * from user;")
49
+ end
50
+
51
+ d3.run do
52
+ d3.emit('message' => "# User@Host: debian-sys-maint[debian-sys-maint] @ localhost []")
53
+ d3.emit('message' => "# Query_time: 0.014260 Lock_time: 0.000182 Rows_sent: 0 Rows_examined: 808")
54
+
55
+ d4.emit('message' => "# User@Host: debian-sys-maint[debian-sys-maint] @ localhost []")
56
+ d4.emit('message' => "# Query_time: 0.000262 Lock_time: 0.000200 Rows_sent: 0 Rows_examined: 0")
57
+ d4.emit('message' => "SET timestamp=1357371822;")
58
+ d4.emit('message' => "select count(*) into @discard from `information_schema`.`EVENTS`;")
59
+
60
+ d3.emit('message' => "SET timestamp=1357371822;")
61
+ d3.emit('message' => "select count(*) into @discard from `information_schema`.`COLUMNS`;")
62
+ end
63
+
64
+ assert_equal 1, d1.emits.size
65
+ assert_equal 2, d2.emits.size
66
+ assert_equal 2, d3.emits.size
67
+
68
+ assert_equal '2013-01-05 16:43:42 +0900', Time.at(d1.emits[0][1]).to_s
69
+ assert_equal '2013-01-05 18:04:21 +0900', Time.at(d2.emits[1][1]).to_s
70
+
71
+ assert_equal 'cocatenated.test', d1.emits[0][0]
72
+ assert_equal 'cocatenated.test2', d2.emits[0][0]
73
+ assert_equal 'cocatenated.test2', d2.emits[1][0]
74
+ assert_equal 'cocatenated.test4', d3.emits[0][0]
75
+ assert_equal 'cocatenated.test3', d3.emits[1][0]
76
+
77
+ assert_equal({
78
+ "user" => "debian-sys-maint[debian-sys-maint]",
79
+ "host" => "localhost",
80
+ "query_time" => 0.000167,
81
+ "lock_time" => 5.7e-05,
82
+ "rows_sent" => 1,
83
+ "rows_examined" => 7,
84
+ "sql" => "SET timestamp=1357371822; SELECT count(*) FROM mysql.user WHERE user='root' and password='';"
85
+ }, d1.emits[0][2])
86
+
87
+ assert_equal({
88
+ "user" => "debian-sys-maint[debian-sys-maint]",
89
+ "host" => "localhost",
90
+ "query_time" => 0.002998,
91
+ "lock_time" => 7.8e-05,
92
+ "rows_sent" => 31,
93
+ "rows_examined" => 81,
94
+ "sql" => "SET timestamp=61357371822; select concat('select count(*) into @discard from `', TABLE_SCHEMA, '`.`', TABLE_NAME, '`') from information_schema.TABLES where ENGINE='MyISAM';"
95
+ }, d2.emits[0][2])
96
+
97
+ assert_equal({
98
+ "user" => "root[root]",
99
+ "host" => "localhost",
100
+ "query_time" => 0.000398,
101
+ "lock_time" => 0.000117,
102
+ "rows_sent" => 7,
103
+ "rows_examined" => 7,
104
+ "sql" => "use mysql; SET timestamp=1357376661; select * from user;"
105
+ }, d2.emits[1][2])
106
+
107
+ assert_equal({
108
+ "user" => "debian-sys-maint[debian-sys-maint]",
109
+ "host" => "localhost",
110
+ "query_time" => 0.000262,
111
+ "lock_time" => 0.0002,
112
+ "rows_sent" => 0,
113
+ "rows_examined" => 0,
114
+ "sql" => "SET timestamp=1357371822; select count(*) into @discard from `information_schema`.`EVENTS`;"
115
+ }, d3.emits[0][2])
116
+
117
+ assert_equal({
118
+ "user" => "debian-sys-maint[debian-sys-maint]",
119
+ "host" => "localhost",
120
+ "query_time" => 0.01426,
121
+ "lock_time" => 0.000182,
122
+ "rows_sent" => 0,
123
+ "rows_examined" => 808,
124
+ "sql" => "SET timestamp=1357371822; select count(*) into @discard from `information_schema`.`COLUMNS`;"
125
+ }, d3.emits[1][2])
126
+
127
+ end
128
+ end
129
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-mysqlslowquerylog-ippen-digital
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Satoshi SUZUKI
9
+ - Gerhard Strauss
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2018-01-16 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: fluentd
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: fluentd
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: Fluentd plugin to concat MySQL slowquerylog.
48
+ email:
49
+ - studio3104.com@gmail.com
50
+ - gerhard.strauss@ippen-digital.de
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - fluent-plugin-mysqlslowquerylog.gemspec
61
+ - lib/fluent/plugin/out_mysqlslowquerylog.rb
62
+ - test/helper.rb
63
+ - test/plugin/test_out_mysqlslowquerylog.rb
64
+ homepage: https://github.com/gerhard-strauss/fluent-plugin-mysqlslowquerylog
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.23
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Fluentd plugin to concat MySQL slowquerylog.
88
+ test_files:
89
+ - test/helper.rb
90
+ - test/plugin/test_out_mysqlslowquerylog.rb