Skip to content

Instantly share code, notes, and snippets.

@andsel
Created October 25, 2022 12:47
Show Gist options
  • Save andsel/b77b001783e01fa61ebcbb5cdcb66599 to your computer and use it in GitHub Desktop.
Save andsel/b77b001783e01fa61ebcbb5cdcb66599 to your computer and use it in GitHub Desktop.
Direct instantiation of Logstash input plugin without a running Logstash
# encoding: utf-8
# Script to test JDBC input plugin in Logstash environment
#
# To run, copy script to Logstash folder and run:
#
# bin/ruby instantiate_jdbc_input.rb
################### Setup Logstash and Log4j ##################
require_relative "lib/bootstrap/environment"
LogStash::Bundler.setup!({:without => [:build, :development]})
require "logstash-core"
require "logstash/environment"
require "socket"
#port = 2000
log4j_test_path = "/tmp/log4j.test.log"
java.lang.System.setProperty("ls.logs", log4j_test_path)
java.lang.System.setProperty("ls.log.format", "plain")
java.lang.System.setProperty("ls.log.level", "info")
java.lang.System.setProperty("ls.pipeline.separate_logs", "false")
LogStash::Logging::Logger::reconfigure(URI.encode(::File.join(LogStash::Environment::LOGSTASH_HOME, "config", "log4j2.properties")))
include LogStash::Util::Loggable
################### Start JDBC input plugin #################
require "logstash/inputs/jdbc.rb"
# warn, keys MUST be string else it triggers a "modification while iterating" error
config = {
"jdbc_driver_library" => "/home/andrea/dev/oracle/ojdbc11-full/ojdbc11_g.jar",
"jdbc_driver_class" => "Java::oracle.jdbc.driver.OracleDriver",
"jdbc_connection_string" => "jdbc:oracle:thin:@//192.168.1.100:1521/ORCLCDB",
"jdbc_user" => "logstash",
"jdbc_password" => "oracle",
#"schedule" => "*/10 * * * * * UTC",
"statement" => "SELECT * from SYSTEM_PRIVILEGE_MAP",
"sequel_opts" => {"max_connections" => "1"},
"jdbc_validate_connection" => true,
"jdbc_validation_timeout" => 60,
"connection_retry_attempts" => 3,
"connection_retry_attempts_wait_time" => 3,
"clean_run" => false,
"record_last_run" => true,
"use_column_value" => true,
"tracking_column" => "id",
"tracking_column_type" => "numeric",
"prepared_statement_name" => "LOGSTASH.PS",
"use_prepared_statements" => true,
"last_run_metadata_path" => "/home/andrea/test/oracle/.jdbc_last_run",
}
(1..5).each do |i|
logger.info("DNADBG>> instantiating and registering JDBC input plugin...loop #{i}")
plugin = LogStash::Inputs::Jdbc.new(config)
plugin.register
logger.info("DNADBG>> running the input plugin")
worker = Thread.new do
logger.info("Running from plugin runner")
queue = []
#plugin.load_driver
#plugin.execute_query(queue)
#plugin.execute_query(queue)
plugin.run(queue)
sleep 5
plugin.run(queue)
end
sleep_secs = 30
logger.info("Run for a #{sleep_secs} seconds then exit")
sleep sleep_secs
logger.info("Shutting down")
plugin.do_stop
worker.join
logger.info("Shut down\n\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment