fluent-plugin-mysql-query 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  fluent-plugin-mysql-query
2
2
  ===========================
3
3
 
4
- Fluentd Input plugin to execute mysql command intervaled.
4
+ Fluentd Input plugin to execute mysql query for stationary measurement.
5
5
 
6
6
  ## Installation
7
7
 
@@ -29,7 +29,11 @@ gem install fluent-plugin-mysql-query
29
29
  interval 30s # Optional (default: 1m)
30
30
  tag input.mysql # Required
31
31
  query SHOW VARIABLES LIKE 'Thread_%' # Required
32
+ # inserting hostname into record.
32
33
  record_hostname yes # Optional (yes/no)
34
+ # multi row results to be nested or separated record.
35
+ nest_result no # Optional (yes/no)
36
+ nest_keyname data # Optional (default: result)
33
37
  </source>
34
38
 
35
39
  <match input.mysql>
@@ -38,14 +42,24 @@ gem install fluent-plugin-mysql-query
38
42
  `````
39
43
 
40
44
  ### Output Sample
45
+ record_hostname: yes, nest_result: no
41
46
  `````
42
47
  input.mysql: {"hostname":"myhost.example.com","Variable_name":"thread_cache_size","Value":"16"}
43
48
  input.mysql: {"hostname":"myhost.example.com","Variable_name":"thread_stack","Value":"262144"}
44
49
  `````
50
+ record_hostname: yes, nest_result: yes, nest_keyname: data
51
+ `````
52
+ input.mysql: {"hostname":"myhost.example.com","data":[{"Variable_name":"thread_cache_size","Value":"16"},{"Variable_name":"thread_stack","Value":"262144"}]}
53
+ `````
54
+
55
+ ### Example Query
56
+ * SHOW VARIABLES LIKE 'Thread_%';
57
+ * SELECT MAX(id) AS max_foo_id FROM foo_table;
58
+ * SHOW FULL PROCESSLIST;
59
+ * INSERT INTO log (data, created_at) VALUES((SELECT MAX(id) FROM foo_table), NOW());
45
60
 
46
61
  ## TODO
47
62
  patches welcome!
48
- * support results into array option
49
63
  * support result_key_downcase option
50
64
 
51
65
  ## Copyright
@@ -3,11 +3,11 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fluent-plugin-mysql-query"
6
- s.version = "0.0.1"
6
+ s.version = "0.1.1"
7
7
  s.authors = ["Kentaro Yoshida"]
8
8
  s.email = ["y.ken.studio@gmail.com"]
9
9
  s.homepage = "https://github.com/y-ken/fluent-plugin-mysql-query"
10
- s.summary = %q{Fluentd Input plugin to execute mysql query with custom intervals.}
10
+ s.summary = %q{Fluentd Input plugin to execute mysql query for stationary measurement.}
11
11
 
12
12
  s.files = `git ls-files`.split("\n")
13
13
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -14,15 +14,18 @@ module Fluent
14
14
  config_param :database, :string, :default => nil
15
15
  config_param :encoding, :string, :default => 'utf8'
16
16
  config_param :interval, :string, :default => '1m'
17
- config_param :record_hostname, :string, :default => nil
18
17
  config_param :tag, :string
19
18
  config_param :query, :string
19
+ config_param :nest_result, :string, :default => nil
20
+ config_param :nest_key, :string, :default => 'result'
21
+ config_param :record_hostname, :string, :default => nil
20
22
 
21
23
  def configure(conf)
22
24
  super
23
- @interval = Config.time_value(@interval)
24
- @record_hostname = @record_hostname || false
25
25
  @hostname = get_mysql_hostname
26
+ @interval = Config.time_value(@interval)
27
+ @nest_result = Config.bool_value(@nest_result) || false
28
+ @record_hostname = Config.bool_value(@record_hostname) || false
26
29
  $log.info "adding mysql_query job: [#{@query}] interval: #{@interval}sec"
27
30
  end
28
31
 
@@ -35,14 +38,18 @@ module Fluent
35
38
  end
36
39
 
37
40
  def run
38
- loop do
39
- con = query(@query)
40
- con.each do |row|
41
- tag = "#{@tag}".gsub('__HOSTNAME__', @hostname).gsub('${hostname}', @hostname)
42
- record = Hash.new
43
- record.store('hostname', @hostname) if @record_hostname
44
- record.merge!(row)
41
+ loop do
42
+ tag = "#{@tag}".gsub('__HOSTNAME__', @hostname).gsub('${hostname}', @hostname)
43
+ record = Hash.new
44
+ record.store('hostname', @hostname) if @record_hostname
45
+ result = get_exec_result
46
+ if (@nest_result)
47
+ record.store(@nest_key, result)
45
48
  Engine.emit(tag, Engine.now, record)
49
+ else
50
+ result.each do |data|
51
+ Engine.emit(tag, Engine.now, record.merge(data))
52
+ end
46
53
  end
47
54
  sleep @interval
48
55
  end
@@ -63,7 +70,7 @@ module Fluent
63
70
  def query(query)
64
71
  @mysql ||= get_connection
65
72
  begin
66
- return @mysql.query(query)
73
+ return @mysql.query(query, :cast => false, :cache_rows => false)
67
74
  rescue Exception => e
68
75
  $log.info "#{e.inspect}"
69
76
  end
@@ -74,5 +81,14 @@ module Fluent
74
81
  return row.fetch('Value')
75
82
  end
76
83
  end
84
+
85
+ def get_exec_result
86
+ result = Array.new
87
+ stmt = query(@query)
88
+ stmt.each do |row|
89
+ result.push(row)
90
+ end
91
+ return result
92
+ end
77
93
  end
78
94
  end
@@ -35,7 +35,7 @@ class MysqlQueryInputTest < Test::Unit::TestCase
35
35
  assert_equal 3306, d.instance.port
36
36
  assert_equal 30, d.instance.interval
37
37
  assert_equal 'input.mysql', d.instance.tag
38
- assert_equal 'yes', d.instance.record_hostname
38
+ assert_equal true, d.instance.record_hostname
39
39
  end
40
40
  end
41
41
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-mysql-query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-10-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fluentd
16
- requirement: &8375040 !ruby/object:Gem::Requirement
16
+ requirement: &11556900 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *8375040
24
+ version_requirements: *11556900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: fluentd
27
- requirement: &8373740 !ruby/object:Gem::Requirement
27
+ requirement: &11555420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *8373740
35
+ version_requirements: *11555420
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: mysql2
38
- requirement: &8372580 !ruby/object:Gem::Requirement
38
+ requirement: &11554560 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *8372580
46
+ version_requirements: *11554560
47
47
  description:
48
48
  email:
49
49
  - y.ken.studio@gmail.com
@@ -83,7 +83,7 @@ rubyforge_project:
83
83
  rubygems_version: 1.8.11
84
84
  signing_key:
85
85
  specification_version: 3
86
- summary: Fluentd Input plugin to execute mysql query with custom intervals.
86
+ summary: Fluentd Input plugin to execute mysql query for stationary measurement.
87
87
  test_files:
88
88
  - test/helper.rb
89
89
  - test/plugin/test_in_mysql_query.rb