Class: LogStash::Runner
- Inherits:
-
Clamp::StrictCommand
- Object
- Command
- Clamp::StrictCommand
- LogStash::Runner
- Includes:
- Util::Loggable
- Defined in:
- lib/logstash/runner.rb
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
Instance Method Summary collapse
-
#cli_help?(args) ⇒ Boolean
is the user asking for CLI help subcommand?.
-
#configure_plugin_paths(paths) ⇒ Object
add the given paths for ungemified/bare plugins lookups.
- #create_agent(*args) ⇒ Object
- #execute ⇒ Object
-
#fail(message) ⇒ Object
Emit a failure message and abort.
-
#fetch_settings_path(cli_args) ⇒ Object
where can I find the logstash.yml file? 1.
-
#initialize(*args) ⇒ Runner
constructor
A new instance of Runner.
- #run(args) ⇒ Object
- #setting(key) ⇒ Object
-
#show_gems ⇒ Object
def show_version_java.
-
#show_short_help ⇒ Object
def fail.
-
#show_version ⇒ Object
def self.main.
-
#show_version_java ⇒ Object
def show_version_ruby.
-
#show_version_logstash ⇒ Object
def show_version.
-
#show_version_ruby ⇒ Object
def show_version_logstash.
- #start_shell(shell, start_binding) ⇒ Object
- #trap_sighup ⇒ Object
- #trap_sigint ⇒ Object
- #trap_sigterm ⇒ Object
Methods included from Util::Loggable
included, #logger, #slow_logger
Methods inherited from Clamp::StrictCommand
Methods included from Clamp::Option::StrictDeclaration
#define_deprecated_accessors_for, #define_deprecated_writer_for, #define_reader_for, #define_simple_writer_for
Constructor Details
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
154 155 156 |
# File 'lib/logstash/runner.rb', line 154 def agent @agent end |
Instance Method Details
#cli_help?(args) ⇒ Boolean
is the user asking for CLI help subcommand?
420 421 422 423 |
# File 'lib/logstash/runner.rb', line 420 def cli_help?(args) # I know, double negative !(["--help", "-h"] & args).empty? end |
#configure_plugin_paths(paths) ⇒ Object
add the given paths for ungemified/bare plugins lookups
333 334 335 336 337 338 |
# File 'lib/logstash/runner.rb', line 333 def configure_plugin_paths(paths) Array(paths).each do |path| fail(I18n.t("logstash.runner.configuration.plugin_path_missing", :path => path)) unless File.directory?(path) LogStash::Environment.add_plugin_path(path) end end |
#create_agent(*args) ⇒ Object
340 341 342 |
# File 'lib/logstash/runner.rb', line 340 def create_agent(*args) LogStash::Agent.new(*args) end |
#execute ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/logstash/runner.rb', line 181 def execute require "logstash/util" require "logstash/util/java_version" require "stud/task" # Configure Logstash logging facility, this need to be done before everything else to # make sure the logger has the correct settings and the log level is correctly defined. java.lang.System.setProperty("ls.logs", setting("path.logs")) java.lang.System.setProperty("ls.log.format", setting("log.format")) java.lang.System.setProperty("ls.log.level", setting("log.level")) unless java.lang.System.getProperty("log4j.configurationFile") log4j_config_location = ::File.join(setting("path.settings"), "log4j2.properties") LogStash::Logging::Logger::initialize("file://" + log4j_config_location) end # override log level that may have been introduced from a custom log4j config file LogStash::Logging::Logger::configure_logging(setting("log.level")) if setting("config.debug") && logger.debug? logger.warn("--config.debug was specified, but log.level was not set to \'debug\'! No config info will be logged.") end # We configure the registry and load any plugin that can register hooks # with logstash, this need to be done before any operation. LogStash::PLUGIN_REGISTRY.setup! @settings.validate_all LogStash::Util::set_thread_name(self.class.name) if RUBY_VERSION < "1.9.2" logger.fatal "Ruby 1.9.2 or later is required. (You are running: " + RUBY_VERSION + ")" return 1 end # Exit on bad java versions java_version = LogStash::Util::JavaVersion.version if LogStash::Util::JavaVersion.bad_java_version?(java_version) logger.fatal "Java version 1.8.0 or later is required. (You are running: #{java_version})" return 1 end LogStash::ShutdownWatcher.unsafe_shutdown = setting("pipeline.unsafe_shutdown") configure_plugin_paths(setting("path.plugins")) if version? show_version return 0 end return start_shell(setting("interactive"), binding) if setting("interactive") @settings.format_settings.each {|line| logger.debug(line) } if setting("config.string").nil? && setting("path.config").nil? fail(I18n.t("logstash.runner.missing-configuration")) end if setting("config.reload.automatic") && setting("path.config").nil? # there's nothing to reload signal_usage_error(I18n.t("logstash.runner.reload-without-config-path")) end if setting("config.test_and_exit") config_loader = LogStash::Config::Loader.new(logger) config_str = config_loader.format_config(setting("path.config"), setting("config.string")) begin LogStash::Pipeline.new(config_str) puts "Configuration OK" logger.info "Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash" return 0 rescue => e logger.fatal I18n.t("logstash.runner.invalid-configuration", :error => e.) return 1 end end @agent = create_agent(@settings) @agent.register_pipeline("main", @settings) # enable sigint/sigterm before starting the agent # to properly handle a stalled agent sigint_id = trap_sigint() sigterm_id = trap_sigterm() @agent_task = Stud::Task.new { @agent.execute } # no point in enabling config reloading before the agent starts # also windows doesn't have SIGHUP. we can skip it sighup_id = LogStash::Environment.windows? ? nil : trap_sighup() agent_return = @agent_task.wait @agent.shutdown # flush any outstanding log messages during shutdown org.apache.logging.log4j.LogManager.shutdown agent_return rescue Clamp::UsageError => e $stderr.puts "ERROR: #{e.}" show_short_help return 1 rescue => e # if logger itself is not initialized if LogStash::Logging::Logger.get_logging_context.nil? $stderr.puts "#{I18n.t("oops")} :error => #{e}, :backtrace => #{e.backtrace}" else logger.fatal(I18n.t("oops"), :error => e, :backtrace => e.backtrace) end return 1 ensure Stud::untrap("INT", sigint_id) unless sigint_id.nil? Stud::untrap("TERM", sigterm_id) unless sigterm_id.nil? Stud::untrap("HUP", sighup_id) unless sighup_id.nil? @log_fd.close if @log_fd end |
#fail(message) ⇒ Object
Emit a failure message and abort.
345 346 347 |
# File 'lib/logstash/runner.rb', line 345 def fail() signal_usage_error() end |
#fetch_settings_path(cli_args) ⇒ Object
where can I find the logstash.yml file?
-
look for a “–path.settings path”
-
look for a “–path.settings=path”
-
check if the LS_SETTINGS_DIR environment variable is set
-
return nil if not found
406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/logstash/runner.rb', line 406 def fetch_settings_path(cli_args) if i=cli_args.find_index("--path.settings") cli_args[i+1] elsif settings_arg = cli_args.find {|v| v.match(/--path.settings=/) } match = settings_arg.match(/--path.settings=(.*)/) match[1] elsif ENV['LS_SETTINGS_DIR'] ENV['LS_SETTINGS_DIR'] else nil end end |
#run(args) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/logstash/runner.rb', line 161 def run(args) settings_path = fetch_settings_path(args) @settings.set("path.settings", settings_path) if settings_path begin LogStash::SETTINGS.from_yaml(LogStash::SETTINGS.get("path.settings")) rescue Errno::ENOENT $stderr.puts "WARNING: Could not find logstash.yml which is typically located in $LS_HOME/config or /etc/logstash. You can specify the path using --path.settings. Continuing using the defaults" rescue => e # abort unless we're just looking for the help unless cli_help?(args) $stderr.puts "ERROR: Failed to load settings file from \"path.settings\". Aborting... path.setting=#{LogStash::SETTINGS.get("path.settings")}, exception=#{e.class}, message=>#{e.}" return 1 end end super(*[args]) end |
#setting(key) ⇒ Object
397 398 399 |
# File 'lib/logstash/runner.rb', line 397 def setting(key) @settings.get_value(key) end |
#show_gems ⇒ Object
def show_version_java
324 325 326 327 328 329 |
# File 'lib/logstash/runner.rb', line 324 def show_gems require "rubygems" Gem::Specification.each do |spec| puts "gem #{spec.name} #{spec.version}" end end |
#show_short_help ⇒ Object
def fail
349 350 351 |
# File 'lib/logstash/runner.rb', line 349 def show_short_help puts I18n.t("logstash.runner.short-help") end |
#show_version ⇒ Object
def self.main
300 301 302 303 304 305 306 307 308 |
# File 'lib/logstash/runner.rb', line 300 def show_version show_version_logstash if logger.info? show_version_ruby show_version_java if LogStash::Environment.jruby? show_gems if logger.debug? end end |
#show_version_java ⇒ Object
def show_version_ruby
318 319 320 321 322 |
# File 'lib/logstash/runner.rb', line 318 def show_version_java properties = java.lang.System.getProperties puts "java #{properties["java.version"]} (#{properties["java.vendor"]})" puts "jvm #{properties["java.vm.name"]} / #{properties["java.vm.version"]}" end |
#show_version_logstash ⇒ Object
def show_version
310 311 312 |
# File 'lib/logstash/runner.rb', line 310 def show_version_logstash puts "logstash #{LOGSTASH_VERSION}" end |
#show_version_ruby ⇒ Object
def show_version_logstash
314 315 316 |
# File 'lib/logstash/runner.rb', line 314 def show_version_ruby puts RUBY_DESCRIPTION end |
#start_shell(shell, start_binding) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/logstash/runner.rb', line 353 def start_shell(shell, start_binding) case shell when "pry" require 'pry' start_binding.pry when "irb" require 'irb' ARGV.clear # TODO: set binding to this instance of Runner # currently bugged as per https://github.com/jruby/jruby/issues/384 IRB.start(__FILE__) else fail(I18n.t("logstash.runner.invalid-shell")) end end |
#trap_sighup ⇒ Object
369 370 371 372 373 374 |
# File 'lib/logstash/runner.rb', line 369 def trap_sighup Stud::trap("HUP") do logger.warn(I18n.t("logstash.agent.sighup")) @agent.reload_state! end end |
#trap_sigint ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/logstash/runner.rb', line 383 def trap_sigint Stud::trap("INT") do if @interrupted_once logger.fatal(I18n.t("logstash.agent.forced_sigint")) exit else logger.warn(I18n.t("logstash.agent.sigint")) Thread.new(logger) {|lg| sleep 5; lg.warn(I18n.t("logstash.agent.slow_shutdown")) } @interrupted_once = true @agent_task.stop! end end end |
#trap_sigterm ⇒ Object
376 377 378 379 380 381 |
# File 'lib/logstash/runner.rb', line 376 def trap_sigterm Stud::trap("TERM") do logger.warn(I18n.t("logstash.agent.sigterm")) @agent_task.stop! end end |