Class: Fluent::MonitorAgentInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_monitor_agent.rb

Defined Under Namespace

Classes: ConfigMonitorServlet, JSONConfigMonitorServlet, JSONMonitorServlet, LTSVConfigMonitorServlet, LTSVMonitorServlet, MonitorServlet, TimerWatcher

Constant Summary collapse

MONITOR_INFO =
{
  'output_plugin' => 'is_a?(::Fluent::Output)', # deprecated. Use plugin_category instead
  'buffer_queue_length' => '@buffer.queue_size',
  'buffer_total_queued_size' => '@buffer.total_queued_chunk_size',
  'retry_count' => '@num_errors',
}
IGNORE_ATTRIBUTES =

TODO: use %i() after drop ruby v1.9.3 support.

%W(@config_root_section @config @masked_config).map(&:to_sym)

Constants included from Configurable

Configurable::CONFIG_TYPE_REGISTRY

Instance Attribute Summary

Attributes inherited from Input

#router

Attributes included from PluginLoggerMixin

#log

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Input

#configure, #initialize

Methods included from PluginLoggerMixin

#configure, included, #initialize

Methods included from PluginId

#configure, #plugin_id

Methods included from Configurable

#config, #configure, included, #initialize, lookup_type, register_type

Constructor Details

This class inherits a constructor from Fluent::Input

Class Method Details

.collect_children(pe, array = []) ⇒ Object

get nexted plugins (such as <store> of the copy plugin) from the plugin ‘pe` recursively



328
329
330
331
332
333
334
335
336
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 328

def self.collect_children(pe, array=[])
  array << pe
  if pe.is_a?(MultiOutput) && pe.respond_to?(:outputs)
    pe.outputs.each {|nop|
      collect_children(nop, array)
    }
  end
  array
end

Instance Method Details

#all_pluginsObject



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 303

def all_plugins
  array = []

  # get all input plugins
  array.concat Engine.root_agent.inputs

  # get all output plugins
  Engine.root_agent.outputs.each { |o|
    MonitorAgentInput.collect_children(o, array)
  }
  # get all filter plugins
  Engine.root_agent.filters.each { |f|
    MonitorAgentInput.collect_children(f, array)
  }
  Engine.root_agent.labels.each { |name, l|
    # TODO: Add label name to outputs / filters for identifing plugins
    l.outputs.each { |o| MonitorAgentInput.collect_children(o, array) }
    l.filters.each { |f| MonitorAgentInput.collect_children(f, array) }
  }

  array
end

#fluentd_optsObject



431
432
433
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 431

def fluentd_opts
  @fluentd_opts ||= get_fluentd_opts
end

#get_fluentd_optsObject



435
436
437
438
439
440
441
442
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 435

def get_fluentd_opts
  opts = {}
  ObjectSpace.each_object(Fluent::Supervisor) { |obj|
    opts.merge!(obj.options)
    break
  }
  opts
end

#get_monitor_info(pe, opts = {}) ⇒ Object

get monitor info from the plugin ‘pe` and return a hash object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 385

def get_monitor_info(pe, opts={})
  obj = {}

  # Common plugin information
  obj['plugin_id'] = pe.plugin_id
  obj['plugin_category'] = plugin_category(pe)
  obj['type'] = pe.config['@type'] || pe.config['type']
  obj['config'] = pe.config if !opts.has_key?(:with_config) || opts[:with_config]

  # run MONITOR_INFO in plugins' instance context and store the info to obj
  MONITOR_INFO.each_pair {|key,code|
    begin
      obj[key] = pe.instance_eval(code)
    rescue
    end
  }

  # include all instance variables if :with_debug_info is set
  if opts[:with_debug_info]
    iv = {}
    pe.instance_eval do
      instance_variables.each {|sym|
        next if IGNORE_ATTRIBUTES.include?(sym)
        key = sym.to_s[1..-1]  # removes first '@'
        iv[key] = instance_variable_get(sym)
      }
    end
    obj['instance_variables'] = iv
  end

  obj
end

#plugin_category(pe) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 418

def plugin_category(pe)
  case pe
  when Fluent::Input
    'input'.freeze
  when Fluent::Output
    'output'.freeze
  when Fluent::Filter
    'filter'.freeze
  else
    'unknown'.freeze
  end
end

#plugin_info_by_id(plugin_id, opts = {}) ⇒ Object

search a plugin by plugin_id



353
354
355
356
357
358
359
360
361
362
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 353

def plugin_info_by_id(plugin_id, opts={})
  found = all_plugins.find {|pe|
    pe.respond_to?(:plugin_id) && pe.plugin_id.to_s == plugin_id
  }
  if found
    get_monitor_info(found, opts)
  else
    nil
  end
end

#plugin_info_by_tag(tag, opts = {}) ⇒ Object

try to match the tag and get the info from the matched output plugin TODO: Support output in label



340
341
342
343
344
345
346
347
348
349
350
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 340

def plugin_info_by_tag(tag, opts={})
  matches = Engine.root_agent.event_router.instance_variable_get(:@match_rules)
  matches.each { |rule|
    if rule.match?(tag)
      if rule.collector.is_a?(Output)
        return get_monitor_info(rule.collector, opts)
      end
    end
  }
  nil
end

#plugins_info_all(opts = {}) ⇒ Object



375
376
377
378
379
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 375

def plugins_info_all(opts={})
  all_plugins.map {|pe|
    get_monitor_info(pe, opts)
  }
end

#plugins_info_by_type(type, opts = {}) ⇒ Object

This method returns an array because multiple plugins could have the same type



366
367
368
369
370
371
372
373
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 366

def plugins_info_by_type(type, opts={})
  array = all_plugins.select {|pe|
    (pe.config['@type'] == type || pe.config['type'] == type) rescue nil
  }
  array.map {|pe|
    get_monitor_info(pe, opts)
  }
end

#runObject



271
272
273
274
275
276
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 271

def run
  @loop.run
rescue => e
  log.error "unexpected error", error: e.to_s
  log.error_backtrace
end

#shutdownObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 278

def shutdown
  if @srv
    @srv.shutdown
    @srv = nil
  end
  if @thread
    @thread.join
    @thread = nil
  end
  if @tag
    @loop.watchers.each { |w| w.detach }
    @loop.stop
    @loop = nil
    @thread_for_emit.join
    @thread_for_emit = nil
  end
end

#startObject



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
# File 'lib/fluent/plugin/in_monitor_agent.rb', line 238

def start
  log.debug "listening monitoring http server on http://#{@bind}:#{@port}/api/plugins"
  @srv = WEBrick::HTTPServer.new({
      BindAddress: @bind,
      Port: @port,
      Logger: WEBrick::Log.new(STDERR, WEBrick::Log::FATAL),
      AccessLog: [],
    })
  @srv.mount('/api/plugins', LTSVMonitorServlet, self)
  @srv.mount('/api/plugins.json', JSONMonitorServlet, self)
  @srv.mount('/api/config', LTSVConfigMonitorServlet, self)
  @srv.mount('/api/config.json', JSONConfigMonitorServlet, self)
  @thread = Thread.new {
    @srv.start
  }
  if @tag
    log.debug "tag parameter is specified. Emit plugins info to '#{@tag}'"

    @loop = Coolio::Loop.new
    opts = {with_config: @emit_config}
    timer = TimerWatcher.new(@emit_interval, log) {
      es = MultiEventStream.new
      now = Engine.now
      plugins_info_all(opts).each { |record|
        es.add(now, record)
      }
      router.emit_stream(@tag, es)
    }
    @loop.attach(timer)
    @thread_for_emit = Thread.new(&method(:run))
  end
end