diff --git a/README.md b/README.md index e9c3fd7..6d366f0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,26 @@ logstash-jdbc ============= +JDBC output plugin for Logstash. +This plugin is provided as an external plugin and is not part of the Logstash project. -JDBC output for Logstash +Warning +------- +This has not yet been extensively tested with all JDBC drivers and may not yet work for you. + +Installation +------------ + - Copy logstash directory contents into your logstash installation. + - Add JDBC jar files to vendor/jar/jdbc in your logstash installation + - Configure + +Example configuration +--------------------- +```output { + jdbc { + driver_class => 'com.microsoft.sqlserver.jdbc.SQLServerDriver' + connection_string => "jdbc:sqlserver://server:1433;databaseName=databasename;user=username;password=password;autoReconnect=true;" + statement => [ "INSERT INTO filezilla (host, connection_id, timestamp, username, client, command) VALUES(?, ?, ?, ?, ?, ?)", "%{host}", "%{connection_id}", "%{timestamp}", "%{username}", "%{client}", "%{command}" ] + } +}``` + +/* vim: set ts=4 sw=4 tw=0 :*/ diff --git a/logstash/lib/logstash/outputs/jdbc.rb b/logstash/lib/logstash/outputs/jdbc.rb new file mode 100644 index 0000000..0783255 --- /dev/null +++ b/logstash/lib/logstash/outputs/jdbc.rb @@ -0,0 +1,47 @@ +# encoding: utf-8 +require "logstash/outputs/base" +require "logstash/namespace" + +class LogStash::Outputs::Jdbc < LogStash::Outputs::Base + + config_name "jdbc" + milestone 1 + + # Driver class + config :driver_class, :validate => :string + + # connection string + config :connection_string, :validate => :string, :required => true + + # [ "insert into table (message) values(?)", "%{message}" ] + config :statement, :validate => :array, :required => true + + public + def register + @logger.info("Starting up JDBC") + require 'java' + + jarpath = File.join(File.dirname(__FILE__), "../../../vendor/jar/jdbc/*.jar") + @logger.info(jarpath) + Dir[jarpath].each do |jar| + @logger.debug("JDBC loaded jar", :jar => jar) + require jar + end + + import @driver_class + + driver = Object.const_get(@driver_class[@driver_class.rindex('.') + 1, @driver_class.length]).new + @connection = driver.connect(@connection_string, java.util.Properties.new) + + @logger.debug("JDBC", :connection => @connection) + end + + def receive(event) + return unless output?(event) + statement = @connection.prepareStatement(@statement[0]) + @statement[1..-1].each_with_index { |i, idx| statement.setString(idx + 1, event.sprintf(i)) } + @logger.debug("Sending SQL to server", :event => event, :sql => statement.toString()) + statement.executeUpdate() + end + +end # class LogStash::Outputs::jdbc