Class: LogStash::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/agent.rb

Overview

Collect logs, ship them out.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Agent

Returns a new instance of Agent.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/logstash/agent.rb', line 17

def initialize(config)
  log_to(STDERR)

  @config = config
  @outputs = []
  @inputs = []
  @filters = []
  # Config should have:
  # - list of logs to monitor
  #   - log config
  # - where to ship to
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/logstash/agent.rb', line 11

def config
  @config
end

#filtersObject (readonly)

Returns the value of attribute filters.



14
15
16
# File 'lib/logstash/agent.rb', line 14

def filters
  @filters
end

#inputsObject (readonly)

Returns the value of attribute inputs.



12
13
14
# File 'lib/logstash/agent.rb', line 12

def inputs
  @inputs
end

#outputsObject (readonly)

Returns the value of attribute outputs.



13
14
15
# File 'lib/logstash/agent.rb', line 13

def outputs
  @outputs
end

Instance Method Details

#log_to(target) ⇒ Object



31
32
33
# File 'lib/logstash/agent.rb', line 31

def log_to(target)
  @logger = LogStash::Logger.new(target)
end

#registerObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/logstash/agent.rb', line 39

def register
  # TODO(sissel): warn when no inputs and no outputs are defined.
  # TODO(sissel): Refactor this madness into a config lib
  
  if (["inputs", "outputs"] & @config.keys).length == 0
    $stderr.puts "No inputs or no outputs configured. This probably isn't what you want."
  end

  # Register input and output stuff
  inputs = @config["inputs"]
  inputs.each do |value|
    # If 'url' is an array, then inputs is a hash and the key is the type
    if inputs.is_a?(Hash)
      type, urls = value
    else
      raise "config error, no type for url #{urls.inspect}"
    end

    # url could be a string or an array.
    urls = [urls] if !urls.is_a?(Array)

    urls.each do |url|
      @logger.debug("Using input #{url} of type #{type}")
      input = LogStash::Inputs.from_url(url, type) { |event| receive(event) }
      input.logger = @logger
      input.register
      @inputs << input
    end
  end # each input

  if @config.include?("filters")
    filters = @config["filters"]
    filters.collect { |x| x.to_a[0] }.each do |filter|
      name, value = filter
      @logger.debug("Using filter #{name} => #{value.inspect}")
      filter = LogStash::Filters.from_name(name, value)
      filter.logger = @logger
      filter.register
      @filters << filter
    end # each filter
  end # if we have filters

  @config["outputs"].each do |url|
    @logger.debug("Using output #{url}")
    output = LogStash::Outputs.from_url(url)
    output.logger = @logger
    output.register
    @outputs << output
  end # each output

  # Register any signal handlers
  register_signal_handler
end

#register_signal_handlerObject



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
# File 'lib/logstash/agent.rb', line 137

def register_signal_handler
  @sigchannel = EventMachine::Channel.new
  Signal.trap("USR1") do
    @sigchannel.push(:USR1)
  end

  Signal.trap("INT") do
    @sigchannel.push(:INT)
  end

  @sigchannel.subscribe do |msg|
    case msg
    when :USR1
      counts = Hash.new { |h,k| h[k] = 0 }
      ObjectSpace.each_object do |obj|
        counts[obj.class] += 1
      end

      @logger.info("SIGUSR1 received. Dumping state")
      @logger.info("#{self.class.name} config")
      @logger.info(["  Inputs:", @inputs])
      @logger.info(["  Filters:", @filters])
      @logger.info(["  Outputs:", @outputs])

      @logger.info("Dumping counts of objects by class")
      counts.sort { |a,b| a[1] <=> b[1] or a[0] <=> b[0] }.each do |key, value|
        @logger.info("Class: [#{value}] #{key}")
      end
    when :INT
      @logger.warn("SIGINT received. Shutting down.")
      EventMachine::stop_event_loop
      # TODO(sissel): Should have input/output/filter register shutdown
      # hooks.
    end # case msg
  end # @sigchannel.subscribe
end

#run(&block) ⇒ Object



94
95
96
97
98
99
# File 'lib/logstash/agent.rb', line 94

def run(&block)
  EventMachine.run do
    self.register
    yield if block_given?
  end # EventMachine.run
end

#stopObject



102
103
104
105
106
107
108
109
# File 'lib/logstash/agent.rb', line 102

def stop
  # TODO(sissel): Stop inputs, fluch outputs, wait for finish,
  # then stop the event loop
  EventMachine.stop_event_loop

  # EventMachine has no default way to indicate a 'stopping' state.
  $EVENTMACHINE_STOPPING = true
end