class Fluent::Plugin::MonitorAgentInput::APIHandler

Public Class Methods

new(agent) click to toggle source
# File lib/fluent/plugin/in_monitor_agent_modified.rb, line 47
def initialize(agent)
  @agent = agent
end

Public Instance Methods

config_json(req) click to toggle source
# File lib/fluent/plugin/in_monitor_agent_modified.rb, line 73
def config_json(req)
  obj = {
    'pid' => Process.pid,
    'ppid' => Process.ppid
  }.merge(@agent.fluentd_opts)
  opts = build_option(req)

  render_json(obj, pretty_json: opts[:pretty_json])
end
config_ltsv(_req) click to toggle source
# File lib/fluent/plugin/in_monitor_agent_modified.rb, line 64
def config_ltsv(_req)
  obj = {
    'pid' => Process.pid,
    'ppid' => Process.ppid
  }.merge(@agent.fluentd_opts)

  render_ltsv([obj])
end
plugins_json(req) click to toggle source
# File lib/fluent/plugin/in_monitor_agent_modified.rb, line 57
def plugins_json(req)
  opts = build_option(req)
  obj = build_object(opts)

  render_json({ 'plugins' => obj }, pretty_json: opts[:pretty_json])
end
plugins_ltsv(req) click to toggle source
# File lib/fluent/plugin/in_monitor_agent_modified.rb, line 51
def plugins_ltsv(req)
  list = build_object(build_option(req))

  render_ltsv(list)
end

Private Instance Methods

build_object(opts) click to toggle source
# File lib/fluent/plugin/in_monitor_agent_modified.rb, line 122
def build_object(opts)
  qs = opts[:query]
  if tag = qs['tag'.freeze].first
    # ?tag= to search an output plugin by match pattern
    if obj = @agent.plugin_info_by_tag(tag, opts)
      list = [obj]
    else
      list = []
    end
  elsif plugin_id = (qs['@id'.freeze].first || qs['id'.freeze].first)
    # ?@id= to search a plugin by 'id <plugin_id>' config param
    if obj = @agent.plugin_info_by_id(plugin_id, opts)
      list = [obj]
    else
      list = []
    end
  elsif plugin_type = (qs['@type'.freeze].first || qs['type'.freeze].first)
    # ?@type= to search plugins by 'type <type>' config param
    list = @agent.plugins_info_by_type(plugin_type, opts)
  else
    # otherwise show all plugins
    list = @agent.plugins_info_all(opts)
  end

  list
end
build_option(req) click to toggle source
# File lib/fluent/plugin/in_monitor_agent_modified.rb, line 149
def build_option(req)
  qs = Hash.new { |_, _| [] }
  # parse ?=query string
  if req.query_string
    qs.merge!(CGI.parse(req.query_string))
  end

  # if ?debug=1 is set, set :with_debug_info for get_monitor_info
  # and :pretty_json for render_json_error
  opts = { query: qs }
  if qs['debug'.freeze].first
    opts[:with_debug_info] = true
    opts[:pretty_json] = true
  end

  if ivars = qs['with_ivars'.freeze].first
    opts[:ivars] = ivars.split(',')
  end

  if with_config = qs['with_config'.freeze].first
    opts[:with_config] = Fluent::Config.bool_value(with_config)
  else
    opts[:with_config] = @agent.include_config
  end

  if with_retry = qs['with_retry'.freeze].first
    opts[:with_retry] = Fluent::Config.bool_value(with_retry)
  else
    opts[:with_retry] = @agent.include_retry
  end

  opts
end
render_error_json(code:, msg:, pretty_json: nil, **additional_params) click to toggle source
# File lib/fluent/plugin/in_monitor_agent_modified.rb, line 85
def render_error_json(code:, msg:, pretty_json: nil, **additional_params)
  resp = additional_params.merge('message' => msg)
  render_json(resp, code: code, pretty_json: pretty_json)
end
render_json(obj, code: 200, pretty_json: nil) click to toggle source
# File lib/fluent/plugin/in_monitor_agent_modified.rb, line 90
def render_json(obj, code: 200, pretty_json: nil)
  body =
    if pretty_json
      JSON.pretty_generate(obj)
    else
      obj.to_json
    end

  [code, { 'Content-Type' => 'application/json' }, body]
end
render_ltsv(obj, code: 200) click to toggle source
# File lib/fluent/plugin/in_monitor_agent_modified.rb, line 101
def render_ltsv(obj, code: 200)
  normalized = JSON.parse(obj.to_json)
  text = ''
  normalized.each do |hash|
    row = []
    hash.each do |k, v|
      if v.is_a?(Array)
        row << "#{k}:#{v.join(',')}"
      elsif v.is_a?(Hash)
        next
      else
        row << "#{k}:#{v}"
      end
    end

    text << row.join("\t") << "\n"
  end

  [code, { 'Content-Type' => 'text/plain' }, text]
end