fluent-plugin-mysql-query 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-munin.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,57 @@
1
+ fluent-plugin-mysql-query
2
+ ===========================
3
+
4
+ Fluentd Input plugin to execute mysql command intervaled.
5
+
6
+ ## Installation
7
+
8
+ ### native gem
9
+
10
+ `````
11
+ gem install fluent-plugin-mysql-query
12
+ `````
13
+
14
+ ### td-agent gem
15
+ `````
16
+ /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-mysql-query
17
+ `````
18
+
19
+ ## Configuration
20
+
21
+ ### Config Sample
22
+ `````
23
+ <source>
24
+ type mysql_query
25
+ server localhost # Optional (default: localhost)
26
+ port 3306 # Optional (default: 3306)
27
+ username nagios # Optional (default: root)
28
+ password passw0rd # Optional (default nopassword)
29
+ interval 30s # Optional (default: 1m)
30
+ tag input.mysql # Required
31
+ query SHOW VARIABLES LIKE 'Thread_%' # Required
32
+ record_hostname yes # Optional (yes/no)
33
+ </source>
34
+
35
+ <match input.mysql>
36
+ type stdout
37
+ </match>
38
+ `````
39
+
40
+ ### Output Sample
41
+ `````
42
+ input.mysql: {"hostname":"myhost.example.com","Variable_name":"thread_cache_size","Value":"16"}
43
+ input.mysql: {"hostname":"myhost.example.com","Variable_name":"thread_stack","Value":"262144"}
44
+ `````
45
+
46
+ ## TODO
47
+ patches welcome!
48
+ * support results into array option
49
+ * support result_key_downcase option
50
+
51
+ ## Copyright
52
+
53
+ Copyright © 2012- Kentaro Yoshida (@yoshi_ken)
54
+
55
+ ## License
56
+
57
+ Apache License, Version 2.0
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,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "fluent-plugin-mysql-query"
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-mysql-query"
10
+ s.summary = %q{Fluentd Input plugin to execute mysql query with custom intervals.}
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 "mysql2"
21
+ end
@@ -0,0 +1,78 @@
1
+ module Fluent
2
+ class MysqlQueryInput < Fluent::Input
3
+ Plugin.register_input('mysql_query', self)
4
+
5
+ def initialize
6
+ require 'mysql2'
7
+ super
8
+ end
9
+
10
+ config_param :host, :string, :default => 'localhost'
11
+ config_param :port, :integer, :default => 3306
12
+ config_param :username, :string, :default => 'root'
13
+ config_param :password, :string, :default => nil
14
+ config_param :database, :string, :default => nil
15
+ config_param :encoding, :string, :default => 'utf8'
16
+ config_param :interval, :string, :default => '1m'
17
+ config_param :record_hostname, :string, :default => nil
18
+ config_param :tag, :string
19
+ config_param :query, :string
20
+
21
+ def configure(conf)
22
+ super
23
+ @interval = Config.time_value(@interval)
24
+ @record_hostname = @record_hostname || false
25
+ @hostname = get_mysql_hostname
26
+ $log.info "adding mysql_query job: [#{@query}] interval: #{@interval}sec"
27
+ end
28
+
29
+ def start
30
+ @thread = Thread.new(&method(:run))
31
+ end
32
+
33
+ def shutdown
34
+ Thread.kill(@thread)
35
+ end
36
+
37
+ 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)
45
+ Engine.emit(tag, Engine.now, record)
46
+ end
47
+ sleep @interval
48
+ end
49
+ end
50
+
51
+ def get_connection
52
+ return Mysql2::Client.new({
53
+ :host => @host,
54
+ :port => @port,
55
+ :username => @username,
56
+ :password => @password,
57
+ :database => @database,
58
+ :encoding => @encoding,
59
+ :reconnect => true
60
+ })
61
+ end
62
+
63
+ def query(query)
64
+ @mysql ||= get_connection
65
+ begin
66
+ return @mysql.query(query)
67
+ rescue Exception => e
68
+ $log.info "#{e.inspect}"
69
+ end
70
+ end
71
+
72
+ def get_mysql_hostname
73
+ query("SHOW VARIABLES LIKE 'hostname'").each do |row|
74
+ return row.fetch('Value')
75
+ end
76
+ end
77
+ end
78
+ 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/in_mysql_query'
26
+
27
+ class Test::Unit::TestCase
28
+ end
@@ -0,0 +1,41 @@
1
+ require 'helper'
2
+
3
+ class MysqlQueryInputTest < Test::Unit::TestCase
4
+ def setup
5
+ Fluent::Test.setup
6
+ end
7
+
8
+ CONFIG = %[
9
+ host localhost
10
+ port 3306
11
+ interval 30
12
+ tag input.mysql
13
+ query SHOW VARIABLES LIKE 'Thread_%'
14
+ record_hostname yes
15
+ ]
16
+
17
+ def create_driver(conf=CONFIG,tag='test')
18
+ Fluent::Test::OutputTestDriver.new(Fluent::MysqlQueryInput, tag).configure(conf)
19
+ end
20
+
21
+ def test_configure
22
+ assert_raise(Fluent::ConfigError) {
23
+ d = create_driver('')
24
+ }
25
+ d = create_driver %[
26
+ host localhost
27
+ port 3306
28
+ interval 30
29
+ tag input.mysql
30
+ query SHOW VARIABLES LIKE 'Thread_%'
31
+ record_hostname yes
32
+ ]
33
+ d.instance.inspect
34
+ assert_equal 'localhost', d.instance.host
35
+ assert_equal 3306, d.instance.port
36
+ assert_equal 30, d.instance.interval
37
+ assert_equal 'input.mysql', d.instance.tag
38
+ assert_equal 'yes', d.instance.record_hostname
39
+ end
40
+ end
41
+
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-mysql-query
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-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fluentd
16
+ requirement: &8375040 !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: *8375040
25
+ - !ruby/object:Gem::Dependency
26
+ name: fluentd
27
+ requirement: &8373740 !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: *8373740
36
+ - !ruby/object:Gem::Dependency
37
+ name: mysql2
38
+ requirement: &8372580 !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: *8372580
47
+ description:
48
+ email:
49
+ - y.ken.studio@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - fluent-plugin-mysql-query.gemspec
60
+ - lib/fluent/plugin/in_mysql_query.rb
61
+ - test/helper.rb
62
+ - test/plugin/test_in_mysql_query.rb
63
+ homepage: https://github.com/y-ken/fluent-plugin-mysql-query
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.11
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Fluentd Input plugin to execute mysql query with custom intervals.
87
+ test_files:
88
+ - test/helper.rb
89
+ - test/plugin/test_in_mysql_query.rb