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) ⇒ 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.
94 95 96 97 98 99 100 101 |
# File 'lib/logstash/agent.rb', line 94 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
432 433 434 |
# File 'lib/logstash/agent.rb', line 432 def clean_state? @pipelines.empty? end |
#configure ⇒ Object
Do any start-time configuration.
Log file stuff, plugin path checking, etc.
254 255 256 257 |
# File 'lib/logstash/agent.rb', line 254 def configure configure_logging(log_file) configure_plugin_paths(plugin_paths) end |
#configure_logging(path) ⇒ Object
Point logging at a specific path.
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 299 300 301 302 303 304 |
# File 'lib/logstash/agent.rb', line 260 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 @logger_subscription = @logger.subscribe(@log_fd) else @logger.subscribe(STDOUT) 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
308 309 310 311 312 313 |
# File 'lib/logstash/agent.rb', line 308 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) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/logstash/agent.rb', line 379 def create_pipeline(settings) begin config = fetch_config(settings) rescue => e @logger.error("failed to fetch pipeline configuration", :message => e.) return 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
115 116 117 118 |
# File 'lib/logstash/agent.rb', line 115 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.
141 142 143 144 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 |
# File 'lib/logstash/agent.rb', line 141 def execute require "logstash/pipeline" require "cabin" # gem 'cabin' require "logstash/plugin" 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_loader = LogStash::Config::Loader.new(@logger) 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? })) 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? @thread = Thread.current # this var is implicilty used by Stud.stop? 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 @logger.unsubscribe(stdout_logs) if show_startup_errors @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
135 136 137 |
# File 'lib/logstash/agent.rb', line 135 def fail() raise LogStash::ConfigurationError, end |
#fetch_config(settings) ⇒ Object
Pipeline Aux methods ##
474 475 476 |
# File 'lib/logstash/agent.rb', line 474 def fetch_config(settings) @config_loader.format_config(settings[:config_path], settings[:config_string]) end |
#pipeline_batch_delay=(pipeline_batch_delay_value) ⇒ Object
111 112 113 |
# File 'lib/logstash/agent.rb', line 111 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
107 108 109 |
# File 'lib/logstash/agent.rb', line 107 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
103 104 105 |
# File 'lib/logstash/agent.rb', line 103 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
355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/logstash/agent.rb', line 355 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!`
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/logstash/agent.rb', line 438 def reload_pipeline!(id) old_pipeline = @pipelines[id] new_pipeline = create_pipeline(old_pipeline.original_settings) return if new_pipeline.nil? if old_pipeline.config_str == new_pipeline.config_str @logger.debug("no configuration change for pipeline", :pipeline => id, :config => old_pipeline.config_str) elsif 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)) 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
367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/logstash/agent.rb', line 367 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
421 422 423 424 |
# File 'lib/logstash/agent.rb', line 421 def running_pipeline?(pipeline_id) thread = @pipelines[pipeline_id].thread thread.is_a?(Thread) && thread.alive? end |
#running_pipelines? ⇒ Boolean
415 416 417 418 419 |
# File 'lib/logstash/agent.rb', line 415 def running_pipelines? @upgrade_mutex.synchronize do @pipelines.select {|pipeline_id, _| running_pipeline?(pipeline_id) }.any? end end |
#shutdown ⇒ Object
Pipeline CRUD ##
345 346 347 348 349 |
# File 'lib/logstash/agent.rb', line 345 def shutdown(pipeline) pipeline.shutdown do ::LogStash::ShutdownWatcher.start(pipeline) end end |
#shutdown_pipelines ⇒ Object
403 404 405 |
# File 'lib/logstash/agent.rb', line 403 def shutdown_pipelines @pipelines.each { |id, _| stop_pipeline(id) } end |
#start_pipeline(id) ⇒ Object
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/logstash/agent.rb', line 457 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, :backtrace => e.backtrace) end end sleep 0.01 until pipeline.ready? end |
#start_pipelines ⇒ Object
395 396 397 |
# File 'lib/logstash/agent.rb', line 395 def start_pipelines @pipelines.each { |id, _| start_pipeline(id) } end |
#stop_pipeline(id) ⇒ Object
407 408 409 410 411 412 413 |
# File 'lib/logstash/agent.rb', line 407 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
337 338 339 340 341 342 |
# File 'lib/logstash/agent.rb', line 337 def trap_sighup Stud::trap("HUP") do @logger.warn(I18n.t("logstash.agent.sighup")) reload_state! end end |
#trap_sigint ⇒ Object
Signal Trapping ##
316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/logstash/agent.rb', line 316 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
330 331 332 333 334 335 |
# File 'lib/logstash/agent.rb', line 330 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
426 427 428 429 430 |
# File 'lib/logstash/agent.rb', line 426 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
120 121 122 123 124 125 126 127 |
# File 'lib/logstash/agent.rb', line 120 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.
130 131 132 133 |
# File 'lib/logstash/agent.rb', line 130 def warn() # For now, all warnings are fatal. raise LogStash::ConfigurationError, end |