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
-
#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
-
#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.
86 87 88 89 90 91 92 93 |
# File 'lib/logstash/agent.rb', line 86 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
#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
415 416 417 |
# File 'lib/logstash/agent.rb', line 415 def clean_state? @pipelines.empty? end |
#configure ⇒ Object
Do any start-time configuration.
Log file stuff, plugin path checking, etc.
237 238 239 240 |
# File 'lib/logstash/agent.rb', line 237 def configure configure_logging(log_file) configure_plugin_paths(plugin_paths) end |
#configure_logging(path) ⇒ Object
Point logging at a specific path.
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 |
# File 'lib/logstash/agent.rb', line 243 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
291 292 293 294 295 296 |
# File 'lib/logstash/agent.rb', line 291 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
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/logstash/agent.rb', line 362 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 |
#execute ⇒ Object
Run the agent. This method is invoked after clamp parses the flags given to this program.
128 129 130 131 132 133 134 135 136 137 138 139 140 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 |
# File 'lib/logstash/agent.rb', line 128 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) config_error = LogStash::Pipeline.config_valid?(config_str) if config_error == true @logger.terminal "Configuration OK" return 0 else @logger.fatal I18n.t("logstash.error", :error => config_error) return 1 end end register_pipeline("main", @pipeline_settings.merge({ :config_string => config_string, :config_path => config_path })) 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
122 123 124 |
# File 'lib/logstash/agent.rb', line 122 def fail() raise LogStash::ConfigurationError, end |
#fetch_config(settings) ⇒ Object
Pipeline Aux methods ##
457 458 459 |
# File 'lib/logstash/agent.rb', line 457 def fetch_config(settings) @config_loader.format_config(settings[:config_path], settings[:config_string]) end |
#pipeline_batch_delay=(pipeline_batch_delay_value) ⇒ Object
103 104 105 |
# File 'lib/logstash/agent.rb', line 103 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
99 100 101 |
# File 'lib/logstash/agent.rb', line 99 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
95 96 97 |
# File 'lib/logstash/agent.rb', line 95 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
338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/logstash/agent.rb', line 338 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!`
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/logstash/agent.rb', line 421 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
350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/logstash/agent.rb', line 350 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
404 405 406 407 |
# File 'lib/logstash/agent.rb', line 404 def running_pipeline?(pipeline_id) thread = @pipelines[pipeline_id].thread thread.is_a?(Thread) && thread.alive? end |
#running_pipelines? ⇒ Boolean
398 399 400 401 402 |
# File 'lib/logstash/agent.rb', line 398 def running_pipelines? @upgrade_mutex.synchronize do @pipelines.select {|pipeline_id, _| running_pipeline?(pipeline_id) }.any? end end |
#shutdown ⇒ Object
Pipeline CRUD ##
328 329 330 331 332 |
# File 'lib/logstash/agent.rb', line 328 def shutdown(pipeline) pipeline.shutdown do ::LogStash::ShutdownWatcher.start(pipeline) end end |
#shutdown_pipelines ⇒ Object
386 387 388 |
# File 'lib/logstash/agent.rb', line 386 def shutdown_pipelines @pipelines.each { |id, _| stop_pipeline(id) } end |
#start_pipeline(id) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/logstash/agent.rb', line 440 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
378 379 380 |
# File 'lib/logstash/agent.rb', line 378 def start_pipelines @pipelines.each { |id, _| start_pipeline(id) } end |
#stop_pipeline(id) ⇒ Object
390 391 392 393 394 395 396 |
# File 'lib/logstash/agent.rb', line 390 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
320 321 322 323 324 325 |
# File 'lib/logstash/agent.rb', line 320 def trap_sighup Stud::trap("HUP") do @logger.warn(I18n.t("logstash.agent.sighup")) reload_state! end end |
#trap_sigint ⇒ Object
Signal Trapping ##
299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/logstash/agent.rb', line 299 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
313 314 315 316 317 318 |
# File 'lib/logstash/agent.rb', line 313 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
409 410 411 412 413 |
# File 'lib/logstash/agent.rb', line 409 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
107 108 109 110 111 112 113 114 |
# File 'lib/logstash/agent.rb', line 107 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.
117 118 119 120 |
# File 'lib/logstash/agent.rb', line 117 def warn() # For now, all warnings are fatal. raise LogStash::ConfigurationError, end |