logstash-input-jdbc 0.1.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: 7fc1b073511bb9288a73ba28f953cf5cda3574e3
4
+ data.tar.gz: c477bdcba05546244e43827fc95a577cb3fe5872
5
+ SHA512:
6
+ metadata.gz: eba2db8acf799e38e3b9efc8106ce33577923412d4128265e1a8a7fbf1dab194f3fa54012d507818e2385a81ff00dc7990d486638ae49fc2d7b5e8a50c460e94
7
+ data.tar.gz: 849eb469d3a605883e4f1ea6fdfac1c6de614235f7fe51b953119ee24148014af2cb21e15fd74efda3e86e2077c86b88070cb2e0a1d5d0a09c510f930663b8c3
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ .bundle
4
+ vendor
5
+ derby.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+ gem "logstash", :github => "elasticsearch/logstash", :branch => "1.5"
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co>
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.
@@ -0,0 +1,97 @@
1
+ # Logstash JDBC Input Plugin
2
+
3
+ ## WIP: Under Development, NOT FOR PRODUCTION
4
+
5
+ This is a plugin for [Logstash](https://github.com/elasticsearch/logstash).
6
+
7
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
8
+
9
+ ## Documentation
10
+
11
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elasticsearch.org/guide/en/logstash/current/).
12
+
13
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
14
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elasticsearch/docs#asciidoc-guide
15
+
16
+ ## Need Help?
17
+
18
+ Need help? Try #logstash on freenode IRC or the logstash-users@googlegroups.com mailing list.
19
+
20
+ ## Developing
21
+
22
+ ### 1. Plugin Developement and Testing
23
+
24
+ #### Code
25
+ - To get started, you'll need JRuby with the Bundler gem installed.
26
+
27
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization.
28
+
29
+ - Install dependencies
30
+ ```sh
31
+ bundle install
32
+ ```
33
+
34
+ #### Test
35
+
36
+ ```sh
37
+ bundle exec rspec
38
+ ```
39
+
40
+ The Logstash code required to run the tests/specs is specified in the `Gemfile` by the line similar to:
41
+ ```ruby
42
+ gem "logstash", :github => "elasticsearch/logstash", :branch => "1.5"
43
+ ```
44
+ To test against another version or a local Logstash, edit the `Gemfile` to specify an alternative location, for example:
45
+ ```ruby
46
+ gem "logstash", :github => "elasticsearch/logstash", :ref => "master"
47
+ ```
48
+ ```ruby
49
+ gem "logstash", :path => "/your/local/logstash"
50
+ ```
51
+
52
+ Then update your dependencies and run your tests:
53
+
54
+ ```sh
55
+ bundle install
56
+ bundle exec rspec
57
+ ```
58
+
59
+ ### 2. Running your unpublished Plugin in Logstash
60
+
61
+ #### 2.1 Run in a local Logstash clone
62
+
63
+ - Edit Logstash `tools/Gemfile` and add the local plugin path, for example:
64
+ ```ruby
65
+ gem "logstash-input-jdbc", :path => "/your/local/logstash-input-jdbc"
66
+ ```
67
+ - Update Logstash dependencies
68
+ ```sh
69
+ rake vendor:gems
70
+ ```
71
+ - Run Logstash with your plugin
72
+ ```sh
73
+ bin/logstash -e 'input {jdbc {..}}'
74
+ ```
75
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
76
+
77
+ #### 2.2 Run in an installed Logstash
78
+
79
+ - Build your plugin gem
80
+ ```sh
81
+ gem build logstash-input-jdbc.gemspec
82
+ ```
83
+ - Install the plugin from the Logstash home
84
+ ```sh
85
+ bin/plugin install /your/local/plugin/logstash-input-jdbc.gem
86
+ ```
87
+ - Start Logstash and proceed to test the plugin
88
+
89
+ ## Contributing
90
+
91
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
92
+
93
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
94
+
95
+ It is more important to me that you are able to contribute.
96
+
97
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elasticsearch/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1 @@
1
+ require "logstash/devutils/rake"
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+ require "logstash/inputs/base"
3
+ require "logstash/namespace"
4
+ require "logstash/plugin_mixins/jdbc"
5
+
6
+ # INFORMATION
7
+ #
8
+ # This plugin was created as a way to iteratively ingest any database
9
+ # with a JDBC interface into Logstash.
10
+ #
11
+ # #### JDBC Mixin
12
+ #
13
+ # This plugin utilizes a mixin that helps Logstash plugins manage JDBC connections.
14
+ # The mixin provides its own set of configurations (some are required) to properly
15
+ # set up the connection to the appropriate database.
16
+ #
17
+ # #### Predefined Parameters
18
+ #
19
+ # Some parameters are built-in and can be used from within your queries.
20
+ # Here is the list:
21
+ #
22
+ # |==========================================================
23
+ # |sql_last_start |The time the last query executed in plugin
24
+ # |==========================================================
25
+ #
26
+ # #### Usage:
27
+ # This is an example logstash config
28
+ # [source,ruby]
29
+ # input {
30
+ # jdbc {
31
+ # jdbc_driver_class => "org.apache.derby.jdbc.EmbeddedDriver" (required; from mixin)
32
+ # jdbc_connection_string => "jdbc:derby:memory:testdb;create=true" (required; from mixin)
33
+ # jdbc_user => "username" (from mixin)
34
+ # jdbc_password => "mypass" (from mixin)
35
+ # statement => "SELECT * from table where created_at > :sql_last_start and id = :my_id" (required)
36
+ # parameters => { "my_id" => "231" }
37
+ # schedule => "* * * * *"
38
+ # }
39
+ # }
40
+ class LogStash::Inputs::Jdbc < LogStash::Inputs::Base
41
+ include LogStash::PluginMixins::Jdbc
42
+ config_name "jdbc"
43
+
44
+ # If undefined, Logstash will complain, even if codec is unused.
45
+ default :codec, "plain"
46
+
47
+ # Statement to execute
48
+ # To use parameters, use named parameter syntax.
49
+ # For example:
50
+ # "SELECT * FROM MYTABLE WHERE id = :target_id"
51
+ # here ":target_id" is a named parameter
52
+ #
53
+ config :statement, :validate => :string, :required => true
54
+
55
+ # Hash of query parameter, for example `{ "target_id" => "321" }`
56
+ config :parameters, :validate => :hash, :default => {}
57
+
58
+ # Schedule of when to periodically run statement, in Cron format
59
+ # for example: "* * * * *" (execute query every minute, on the minute)
60
+ config :schedule, :validate => :string
61
+
62
+ public
63
+
64
+ def register
65
+ require "rufus/scheduler"
66
+ prepare_jdbc_connection()
67
+ end # def register
68
+
69
+ def run(queue)
70
+ if @schedule
71
+ @scheduler = Rufus::Scheduler.new
72
+ @scheduler.cron @schedule do
73
+ execute_query(queue)
74
+ end
75
+ @scheduler.join
76
+ else
77
+ execute_query(queue)
78
+ end
79
+ end # def run
80
+
81
+ def teardown
82
+ if @scheduler
83
+ @scheduler.stop
84
+ end
85
+ close_jdbc_connection()
86
+ end # def teardown
87
+
88
+ private
89
+ def execute_query(queue)
90
+ # update default parameters
91
+ @parameters['sql_last_start'] = @sql_last_start
92
+ execute_statement(@statement, @parameters) do |row|
93
+ event = LogStash::Event.new(row)
94
+ decorate(event)
95
+ queue << event
96
+ end
97
+ end
98
+ end # class LogStash::Inputs::Jdbc
@@ -0,0 +1,104 @@
1
+ # encoding: utf-8
2
+ # TAKEN FROM WIIBAA
3
+ require "logstash/config/mixin"
4
+
5
+ # Tentative of abstracting JDBC logic to a mixin
6
+ # for potential reuse in other plugins (input/output)
7
+ module LogStash::PluginMixins::Jdbc
8
+
9
+ @logger = Cabin::Channel.get(LogStash)
10
+
11
+ # This method is called when someone includes this module
12
+ def self.included(base)
13
+ # Add these methods to the 'base' given.
14
+ base.extend(self)
15
+ base.setup_jdbc_config
16
+
17
+ @sql_last_start = Time.at(0).utc
18
+ end
19
+
20
+
21
+ public
22
+ def setup_jdbc_config
23
+ # JDBC driver library path to third party driver library.
24
+ config :jdbc_driver_library, :validate => :path
25
+
26
+ # JDBC driver class to load, for example "oracle.jdbc.OracleDriver" or "org.apache.derby.jdbc.ClientDriver"
27
+ config :jdbc_driver_class, :validate => :string, :required => true
28
+
29
+ # JDBC connection string
30
+ config :jdbc_connection_string, :validate => :string, :required => true
31
+
32
+ # JDBC user
33
+ config :jdbc_user, :validate => :string, :default => "tal"
34
+
35
+ # JDBC password
36
+ config :jdbc_password, :validate => :password
37
+
38
+ # Connection pool configuration.
39
+ # Validate connection before use.
40
+ config :jdbc_validate_connection, :validate => :boolean, :default => false
41
+
42
+ # Connection pool configuration.
43
+ # How often to validate a connection (in seconds)
44
+ config :jdcb_validation_timeout, :validate => :number, :default => 3600
45
+ end
46
+
47
+ public
48
+ def prepare_jdbc_connection
49
+ require "java"
50
+ require "sequel"
51
+ require "sequel/adapters/jdbc"
52
+ require @jdbc_driver_library if @jdbc_driver_library
53
+ Sequel::JDBC.load_driver(@jdbc_driver_class)
54
+ @database = Sequel.connect(@jdbc_connection_string, :user=> @jdbc_user, :password=> @jdbc_password.nil? ? nil : @jdbc_password.value)
55
+ if @jdbc_validate_connection
56
+ @database.extension(:connection_validator)
57
+ @database.pool.connection_validation_timeout = @jdcb_validation_timeout
58
+ end
59
+ begin
60
+ @database.test_connection
61
+ rescue Sequel::DatabaseConnectionError => e
62
+ #TODO return false and let the plugin raise a LogStash::ConfigurationError
63
+ raise e
64
+ end
65
+ end # def prepare_jdbc_connection
66
+
67
+ public
68
+ def close_jdbc_connection
69
+ @database.disconnect if @database
70
+ end
71
+
72
+ public
73
+ def execute_statement(statement, parameters)
74
+ success = false
75
+ begin
76
+ parameters = symbolized_params(parameters)
77
+ query = @database[statement, parameters]
78
+ @logger.debug? and @logger.debug("Executing JDBC query", :statement => statement, :parameters => parameters)
79
+ @sql_last_start = Time.now.utc
80
+ query.all do |row|
81
+ #Stringify row keys
82
+ yield Hash[row.map { |k, v| [k.to_s, v] }]
83
+ end
84
+ success = true
85
+ rescue Sequel::DatabaseConnectionError, Sequel::DatabaseError => e
86
+ @logger.warn("Exception when executing JDBC query", :exception => e)
87
+ end
88
+ return success
89
+ end
90
+
91
+ # Symbolize parameters keys to use with Sequel
92
+ private
93
+ def symbolized_params(parameters)
94
+ parameters.inject({}) do |hash,(k,v)|
95
+ case v
96
+ when LogStash::Timestamp
97
+ hash[k.to_sym] = v.time
98
+ else
99
+ hash[k.to_sym] = v
100
+ end
101
+ hash
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,29 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-input-jdbc'
3
+ s.version = '0.1.1'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = "This example input streams a string at a definable interval."
6
+ s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
7
+ s.authors = ["Elastic"]
8
+ s.email = 'info@elastic.co'
9
+ s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = `git ls-files`.split($\)
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "input" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency 'logstash', '>= 1.4.0', '< 2.0.0'
22
+ s.add_runtime_dependency 'logstash-codec-plain'
23
+ s.add_runtime_dependency 'sequel'
24
+
25
+ s.add_development_dependency 'logstash-devutils'
26
+ s.add_development_dependency 'timecop'
27
+ s.add_development_dependency 'rufus-scheduler'
28
+ s.add_development_dependency 'jdbc-derby'
29
+ end
@@ -0,0 +1,84 @@
1
+ require "logstash/devutils/rspec/spec_helper"
2
+ require "logstash/inputs/jdbc"
3
+ require "jdbc/derby"
4
+ require "timecop"
5
+
6
+
7
+ describe "jdbc" do
8
+ let(:mixin_settings) { {"jdbc_driver_class" => "org.apache.derby.jdbc.EmbeddedDriver", "jdbc_connection_string" => "jdbc:derby:memory:testdb;create=true"} }
9
+
10
+ before :each do
11
+ Jdbc::Derby.load_driver
12
+ end
13
+
14
+ it "should register and tear down" do
15
+ settings = {"statement" => "SELECT 1 as col1 FROM SYSIBM.SYSDUMMY1"}
16
+ plugin = LogStash::Plugin.lookup("input", "jdbc").new(mixin_settings.merge(settings))
17
+ expect { plugin.register }.to_not raise_error
18
+ expect { plugin.teardown }.to_not raise_error
19
+ end
20
+
21
+ it "should retrieve params correctly from Event" do
22
+ settings = {"statement" => "SELECT :num_param as num_param FROM SYSIBM.SYSDUMMY1", "parameters" => {"num_param" => 10} }
23
+ plugin = LogStash::Inputs::Jdbc.new(mixin_settings.merge(settings))
24
+ plugin.register
25
+ q = Queue.new
26
+ plugin.run(q)
27
+ insist { q.size } == 1
28
+ insist { q.pop['num_param'] } == settings['parameters']['num_param']
29
+ plugin.teardown
30
+ end
31
+
32
+ it "should properly schedule" do
33
+ settings = {"statement" => "SELECT 1 as num_param FROM SYSIBM.SYSDUMMY1", "schedule" => "* * * * *"}
34
+ plugin = LogStash::Inputs::Jdbc.new(mixin_settings.merge(settings))
35
+ plugin.register
36
+ q = Queue.new
37
+ Timecop.travel(Time.new(2000))
38
+ Timecop.scale(60)
39
+ runner = Thread.new do
40
+ plugin.run(q)
41
+ end
42
+ sleep 3
43
+ plugin.teardown
44
+ runner.kill
45
+ runner.join
46
+ insist { q.size } == 2
47
+ Timecop.return
48
+ end
49
+
50
+ it "should successfully iterate table with respect to field values" do
51
+ require "sequel"
52
+ require "sequel/adapters/jdbc"
53
+ Jdbc::Derby.load_driver
54
+ @database = Sequel.connect(mixin_settings['jdbc_connection_string'], :user=> nil, :password=> nil)
55
+ @database.create_table :test_table do
56
+ DateTime :created_at
57
+ Integer :num
58
+ end
59
+ test_table = @database[:test_table]
60
+ settings = {"statement" => "SELECT num, created_at FROM test_table WHERE created_at > :sql_last_start"}
61
+ plugin = LogStash::Inputs::Jdbc.new(mixin_settings.merge(settings))
62
+ plugin.register
63
+ q = Queue.new
64
+
65
+ nums = [10, 20, 30, 40, 50]
66
+ plugin.run(q)
67
+ test_table.insert(:num => nums[0], :created_at => Time.now.utc)
68
+ test_table.insert(:num => nums[1], :created_at => Time.now.utc)
69
+ plugin.run(q)
70
+ test_table.insert(:num => nums[2], :created_at => Time.now.utc)
71
+ test_table.insert(:num => nums[3], :created_at => Time.now.utc)
72
+ test_table.insert(:num => nums[4], :created_at => Time.now.utc)
73
+ plugin.run(q)
74
+
75
+ actual_sum = 0
76
+ until q.empty? do
77
+ actual_sum += q.pop['num']
78
+ end
79
+
80
+ plugin.teardown
81
+
82
+ insist { actual_sum } == nums.inject{|sum,x| sum + x }
83
+ end
84
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-input-jdbc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Elastic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '>='
17
+ - !ruby/object:Gem::Version
18
+ version: 1.4.0
19
+ - - <
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ name: logstash
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.0
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ name: logstash-codec-plain
40
+ prerelease: false
41
+ type: :runtime
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ name: sequel
54
+ prerelease: false
55
+ type: :runtime
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ name: logstash-devutils
68
+ prerelease: false
69
+ type: :development
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ name: timecop
82
+ prerelease: false
83
+ type: :development
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ name: rufus-scheduler
96
+ prerelease: false
97
+ type: :development
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ name: jdbc-derby
110
+ prerelease: false
111
+ type: :development
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program
118
+ email: info@elastic.co
119
+ executables: []
120
+ extensions: []
121
+ extra_rdoc_files: []
122
+ files:
123
+ - .gitignore
124
+ - Gemfile
125
+ - LICENSE
126
+ - README.md
127
+ - Rakefile
128
+ - lib/logstash/inputs/jdbc.rb
129
+ - lib/logstash/plugin_mixins/jdbc.rb
130
+ - logstash-input-jdbc.gemspec
131
+ - spec/inputs/jdbc_spec.rb
132
+ homepage: http://www.elastic.co/guide/en/logstash/current/index.html
133
+ licenses:
134
+ - Apache License (2.0)
135
+ metadata:
136
+ logstash_plugin: 'true'
137
+ logstash_group: input
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.1.9
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: This example input streams a string at a definable interval.
158
+ test_files:
159
+ - spec/inputs/jdbc_spec.rb