Class: LogStash::Agent
- Inherits:
-
Clamp::Command
- Object
- Clamp::Command
- LogStash::Agent
- Defined in:
- lib/logstash/agent.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_INPUT =
"input { stdin { type => stdin } }"- DEFAULT_OUTPUT =
"output { stdout { codec => rubydebug } }"
Instance Attribute Summary collapse
-
#config_loader ⇒ Object
readonly
Returns the value of attribute config_loader.
-
#pipelines ⇒ Object
readonly
Returns the value of attribute pipelines.
Instance Method Summary collapse
- #clean_state? ⇒ Boolean
-
#configure ⇒ Object
Do any start-time configuration.
-
#configure_logging(path) ⇒ Object
Point logging at a specific path.
-
#configure_plugin_paths(paths) ⇒ Object
add the given paths for ungemified/bare plugins lookups.
- #create_pipeline(settings, config = nil) ⇒ Object
- #debug_config=(debug_config) ⇒ Object
-
#execute ⇒ Object
Run the agent.
-
#fail(message) ⇒ Object
def warn.
-
#fetch_config(settings) ⇒ Object
Pipeline Aux methods ##.
-
#initialize(*params) ⇒ Agent
constructor
A new instance of Agent.
- #pipeline_batch_delay=(pipeline_batch_delay_value) ⇒ Object
- #pipeline_batch_size=(pipeline_batch_size_value) ⇒ Object
- #pipeline_workers=(pipeline_workers_value) ⇒ Object
-
#register_pipeline(pipeline_id, settings) ⇒ Object
register_pipeline - adds a pipeline to the agent’s state.
-
#reload_pipeline!(id) ⇒ Object
since this method modifies the @pipelines hash it is wrapped in @upgrade_mutex in the parent call ‘reload_state!`.
- #reload_state! ⇒ Object
- #running_pipeline?(pipeline_id) ⇒ Boolean
- #running_pipelines? ⇒ Boolean
-
#shutdown ⇒ Object
Pipeline CRUD ##.
- #shutdown_pipelines ⇒ Object
- #start_pipeline(id) ⇒ Object
- #start_pipelines ⇒ Object
- #stop_pipeline(id) ⇒ Object
- #trap_sighup ⇒ Object
-
#trap_sigint ⇒ Object
Signal Trapping ##.
- #trap_sigterm ⇒ Object
- #upgrade_pipeline(pipeline_id, new_pipeline) ⇒ Object
- #validate_positive_integer(str_arg) ⇒ Object
-
#warn(message) ⇒ Object
Emit a warning message.
Constructor Details
#initialize(*params) ⇒ Agent
Returns a new instance of Agent.
98 99 100 101 102 103 104 105 |
# File 'lib/logstash/agent.rb', line 98 def initialize(*params) super(*params) @logger = Cabin::Channel.get(LogStash) @pipelines = {} @pipeline_settings ||= { :pipeline_id => "main" } @upgrade_mutex = Mutex.new @config_loader = LogStash::Config::Loader.new(@logger) end |
Instance Attribute Details
#config_loader ⇒ Object (readonly)
Returns the value of attribute config_loader.
14 15 16 |
# File 'lib/logstash/agent.rb', line 14 def config_loader @config_loader end |
#pipelines ⇒ Object (readonly)
Returns the value of attribute pipelines.
14 15 16 |
# File 'lib/logstash/agent.rb', line 14 def pipelines @pipelines end |
Instance Method Details
#clean_state? ⇒ Boolean
453 454 455 |
# File 'lib/logstash/agent.rb', line 453 def clean_state? @pipelines.empty? end |
#configure ⇒ Object
Do any start-time configuration.
Log file stuff, plugin path checking, etc.
261 262 263 264 |
# File 'lib/logstash/agent.rb', line 261 def configure configure_logging(log_file) configure_plugin_paths(plugin_paths) end |
#configure_logging(path) ⇒ Object
Point logging at a specific path.
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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/logstash/agent.rb', line 267 def configure_logging(path) # Set with the -v (or -vv...) flag if quiet? @logger.level = :error elsif verbose? @logger.level = :info elsif debug? @logger.level = :debug else # Old support for the -v and -vv stuff. if verbosity? && verbosity?.any? # this is an array with length of how many times the flag is given if verbosity?.length == 1 @logger.warn("The -v flag is deprecated and will be removed in a future release. You should use --verbose instead.") @logger.level = :info else @logger.warn("The -vv flag is deprecated and will be removed in a future release. You should use --debug instead.") @logger.level = :debug end else @logger.level = :warn end end if log_file # TODO(sissel): Implement file output/rotation in Cabin. # TODO(sissel): Catch exceptions, report sane errors. begin @log_fd.close if @log_fd @log_fd = File.new(path, "a") rescue => e fail(I18n.t("logstash.agent.configuration.log_file_failed", :path => path, :error => e)) end puts "Sending logstash logs to #{path}." @logger.unsubscribe(@logger_subscription) if @logger_subscription if log_in_json? @logger_subscription = @logger.subscribe(LogStash::Logging::JSON.new(@log_fd)) @logger.subscribe(LogStash::Logging::JSON.new(STDOUT), :level => :fatal) else @logger_subscription = @logger.subscribe(@log_fd) @logger.subscribe(STDOUT, :level => :fatal) end else if log_in_json? @logger.subscribe(LogStash::Logging::JSON.new(STDOUT)) else @logger.subscribe(STDOUT) end end # TODO(sissel): redirect stdout/stderr to the log as well # http://jira.codehaus.org/browse/JRUBY-7003 end |
#configure_plugin_paths(paths) ⇒ Object
add the given paths for ungemified/bare plugins lookups
326 327 328 329 330 331 |
# File 'lib/logstash/agent.rb', line 326 def configure_plugin_paths(paths) Array(paths).each do |path| fail(I18n.t("logstash.agent.configuration.plugin_path_missing", :path => path)) unless File.directory?(path) LogStash::Environment.add_plugin_path(path) end end |
#create_pipeline(settings, config = nil) ⇒ Object
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/logstash/agent.rb', line 434 def create_pipeline(settings, config=nil) if config.nil? begin config = fetch_config(settings) rescue => e @logger.error("failed to fetch pipeline configuration", :message => e.) return end end begin LogStash::Pipeline.new(config, settings) rescue => e @logger.error("fetched an invalid config", :config => config, :reason => e.) return end end |
#debug_config=(debug_config) ⇒ Object
119 120 121 122 |
# File 'lib/logstash/agent.rb', line 119 def debug_config=(debug_config) @config_loader.debug_config = debug_config @debug_config = true end |
#execute ⇒ Object
Run the agent. This method is invoked after clamp parses the flags given to this program.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 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 |
# File 'lib/logstash/agent.rb', line 145 def execute require "logstash/pipeline" require "cabin" # gem 'cabin' require "logstash/plugin" require "logstash/logging/json" LogStash::ShutdownWatcher.unsafe_shutdown = unsafe_shutdown? LogStash::ShutdownWatcher.logger = @logger if version? show_version return 0 end # temporarily send logs to stdout as well if a --log is specified # and stdout appears to be a tty show_startup_errors = log_file && STDOUT.tty? if show_startup_errors stdout_logs = @logger.subscribe(STDOUT) end configure if filter_workers @logger.warn("--filter-workers is deprecated! Please use --pipeline-workers or -w. This setting will be removed in the next major version!") self.pipeline_workers = filter_workers end # You must specify a config_string or config_path if config_string.nil? && config_path.nil? fail(I18n.t("logstash.agent.missing-configuration")) end if auto_reload? && config_path.nil? # there's nothing to reload fail(I18n.t("logstash.agent.reload-without-config-path")) end if config_test? config_str = @config_loader.format_config(config_path, config_string) begin # currently the best strategy to validate the configuration # is creating a pipeline instance and checking for exceptions LogStash::Pipeline.new(config_str) @logger.terminal "Configuration OK" return 0 rescue => e @logger.fatal I18n.t("logstash.agent.invalid-configuration", :error => e.) return 1 end end register_pipeline("main", @pipeline_settings.merge({ :config_string => config_string, :config_path => config_path, :debug_config => debug_config?, :allow_env => allow_env? })) @thread = Thread.current # this var is implicilty used by Stud.stop? sigint_id = trap_sigint() sigterm_id = trap_sigterm() sighup_id = trap_sighup() @logger.unsubscribe(stdout_logs) if show_startup_errors @logger.info("starting agent") start_pipelines return 1 if clean_state? Stud.stoppable_sleep(reload_interval) # sleep before looping if auto_reload? Stud.interval(reload_interval) { reload_state! } else while !Stud.stop? if clean_state? || running_pipelines? sleep 0.5 else break end end end shutdown return 0 rescue LogStash::ConfigurationError => e @logger.unsubscribe(stdout_logs) if show_startup_errors @logger.error I18n.t("logstash.agent.error", :error => e) if !config_test? @logger.info I18n.t("logstash.agent.configtest-flag-information") end return 1 rescue => e if show_startup_errors @logger.terminal(e.) @logger.unsubscribe(stdout_logs) end @logger.warn(I18n.t("oops"), :error => e., :class => e.class.name, :backtrace => e.backtrace) return 1 ensure @log_fd.close if @log_fd 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? end |
#fail(message) ⇒ Object
def warn
139 140 141 |
# File 'lib/logstash/agent.rb', line 139 def fail() signal_usage_error() end |
#fetch_config(settings) ⇒ Object
Pipeline Aux methods ##
500 501 502 |
# File 'lib/logstash/agent.rb', line 500 def fetch_config(settings) @config_loader.format_config(settings[:config_path], settings[:config_string]) end |
#pipeline_batch_delay=(pipeline_batch_delay_value) ⇒ Object
115 116 117 |
# File 'lib/logstash/agent.rb', line 115 def pipeline_batch_delay=(pipeline_batch_delay_value) @pipeline_settings[:pipeline_batch_delay] = validate_positive_integer(pipeline_batch_delay_value) end |
#pipeline_batch_size=(pipeline_batch_size_value) ⇒ Object
111 112 113 |
# File 'lib/logstash/agent.rb', line 111 def pipeline_batch_size=(pipeline_batch_size_value) @pipeline_settings[:pipeline_batch_size] = validate_positive_integer(pipeline_batch_size_value) end |
#pipeline_workers=(pipeline_workers_value) ⇒ Object
107 108 109 |
# File 'lib/logstash/agent.rb', line 107 def pipeline_workers=(pipeline_workers_value) @pipeline_settings[:pipeline_workers] = validate_positive_integer(pipeline_workers_value) end |
#register_pipeline(pipeline_id, settings) ⇒ Object
register_pipeline - adds a pipeline to the agent’s state
373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/logstash/agent.rb', line 373 def register_pipeline(pipeline_id, settings) pipeline = create_pipeline(settings.merge(:pipeline_id => pipeline_id)) return unless pipeline.is_a?(LogStash::Pipeline) if @auto_reload && pipeline.non_reloadable_plugins.any? @logger.error(I18n.t("logstash.agent.non_reloadable_config_register"), :pipeline_id => pipeline_id, :plugins => pipeline.non_reloadable_plugins.map(&:class)) return end @pipelines[pipeline_id] = pipeline end |
#reload_pipeline!(id) ⇒ Object
since this method modifies the @pipelines hash it is wrapped in @upgrade_mutex in the parent call ‘reload_state!`
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/logstash/agent.rb', line 459 def reload_pipeline!(id) old_pipeline = @pipelines[id] new_config = fetch_config(old_pipeline.original_settings) if old_pipeline.config_str == new_config @logger.debug("no configuration change for pipeline", :pipeline => id, :config => new_config) return end new_pipeline = create_pipeline(old_pipeline.original_settings, new_config) return if new_pipeline.nil? if new_pipeline.non_reloadable_plugins.any? @logger.error(I18n.t("logstash.agent.non_reloadable_config_reload"), :pipeline_id => id, :plugins => new_pipeline.non_reloadable_plugins.map(&:class)) return else @logger.log("fetched new config for pipeline. upgrading..", :pipeline => id, :config => new_pipeline.config_str) upgrade_pipeline(id, new_pipeline) end end |
#reload_state! ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/logstash/agent.rb', line 385 def reload_state! @upgrade_mutex.synchronize do @pipelines.each do |pipeline_id, _| begin reload_pipeline!(pipeline_id) rescue => e @logger.error(I18n.t("oops"), :error => e, :backtrace => e.backtrace) end end end end |
#running_pipeline?(pipeline_id) ⇒ Boolean
423 424 425 426 |
# File 'lib/logstash/agent.rb', line 423 def running_pipeline?(pipeline_id) thread = @pipelines[pipeline_id].thread thread.is_a?(Thread) && thread.alive? end |
#running_pipelines? ⇒ Boolean
417 418 419 420 421 |
# File 'lib/logstash/agent.rb', line 417 def running_pipelines? @upgrade_mutex.synchronize do @pipelines.select {|pipeline_id, _| running_pipeline?(pipeline_id) }.any? end end |
#shutdown ⇒ Object
Pipeline CRUD ##
363 364 365 366 367 |
# File 'lib/logstash/agent.rb', line 363 def shutdown(pipeline) pipeline.shutdown do ::LogStash::ShutdownWatcher.start(pipeline) end end |
#shutdown_pipelines ⇒ Object
405 406 407 |
# File 'lib/logstash/agent.rb', line 405 def shutdown_pipelines @pipelines.each { |id, _| stop_pipeline(id) } end |
#start_pipeline(id) ⇒ Object
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/logstash/agent.rb', line 483 def start_pipeline(id) pipeline = @pipelines[id] return unless pipeline.is_a?(LogStash::Pipeline) return if pipeline.ready? @logger.info("starting pipeline", :id => id) Thread.new do LogStash::Util.set_thread_name("pipeline.#{id}") begin pipeline.run rescue => e @logger.error("Pipeline aborted due to error", :exception => e.class.name, :error => e., :backtrace => e.backtrace) end end sleep 0.01 until pipeline.ready? end |
#start_pipelines ⇒ Object
397 398 399 |
# File 'lib/logstash/agent.rb', line 397 def start_pipelines @pipelines.each { |id, _| start_pipeline(id) } end |
#stop_pipeline(id) ⇒ Object
409 410 411 412 413 414 415 |
# File 'lib/logstash/agent.rb', line 409 def stop_pipeline(id) pipeline = @pipelines[id] return unless pipeline @logger.log("stopping pipeline", :id => id) pipeline.shutdown { LogStash::ShutdownWatcher.start(pipeline) } @pipelines[id].thread.join end |
#trap_sighup ⇒ Object
355 356 357 358 359 360 |
# File 'lib/logstash/agent.rb', line 355 def trap_sighup Stud::trap("HUP") do @logger.warn(I18n.t("logstash.agent.sighup")) reload_state! end end |
#trap_sigint ⇒ Object
Signal Trapping ##
334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/logstash/agent.rb', line 334 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) {|logger| sleep 5; logger.warn(I18n.t("logstash.agent.slow_shutdown")) } @interrupted_once = true Stud.stop!(@thread) end end end |
#trap_sigterm ⇒ Object
348 349 350 351 352 353 |
# File 'lib/logstash/agent.rb', line 348 def trap_sigterm Stud::trap("TERM") do @logger.warn(I18n.t("logstash.agent.sigterm")) Stud.stop!(@thread) end end |
#upgrade_pipeline(pipeline_id, new_pipeline) ⇒ Object
428 429 430 431 432 |
# File 'lib/logstash/agent.rb', line 428 def upgrade_pipeline(pipeline_id, new_pipeline) stop_pipeline(pipeline_id) @pipelines[pipeline_id] = new_pipeline start_pipeline(pipeline_id) end |
#validate_positive_integer(str_arg) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/logstash/agent.rb', line 124 def validate_positive_integer(str_arg) int_arg = str_arg.to_i if str_arg !~ /^\d+$/ || int_arg < 1 raise ArgumentError, "Expected a positive integer, got '#{str_arg}'" end int_arg end |
#warn(message) ⇒ Object
Emit a warning message.
134 135 136 137 |
# File 'lib/logstash/agent.rb', line 134 def warn() # For now, all warnings are fatal. signal_usage_error() end |