class Fluent::Plugin::MonitorAgentInput::MonitorServlet

Public Class Methods

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

Public Instance Methods

build_object(req, res) click to toggle source
# File lib/fluent/plugin/in_monitor_agent.rb, line 64
def build_object(req, res)
  unless req.path_info == ""
    return render_json_error(404, "Not found")
  end

  # parse ?=query string
  if req.query_string
    begin
      qs = CGI.parse(req.query_string)
    rescue
      return render_json_error(400, "Invalid query string")
    end
  else
    qs = Hash.new {|h,k| [] }
  end

  # if ?debug=1 is set, set :with_debug_info for get_monitor_info
  # and :pretty_json for render_json_error
  opts = {with_config: @agent.include_config, with_retry: @agent.include_retry}
  if s = qs['debug'] and s[0]
    opts[:with_debug_info] = true
    opts[:pretty_json] = true
  end

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

  if with_config = get_search_parameter(qs, 'with_config'.freeze)
    opts[:with_config] = Fluent::Config.bool_value(with_config)
  end

  if with_retry = get_search_parameter(qs, 'with_retry'.freeze)
    opts[:with_retry] = Fluent::Config.bool_value(with_retry)
  end

  if tag = get_search_parameter(qs, 'tag'.freeze)
    # ?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 = get_search_parameter(qs, '@id'.freeze)
    # ?@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_id = get_search_parameter(qs, 'id'.freeze)
    # Without @ version of ?@id= for backward compatibility
    if obj = @agent.plugin_info_by_id(plugin_id, opts)
      list = [obj]
    else
      list = []
    end

  elsif plugin_type = get_search_parameter(qs, '@type'.freeze)
    # ?@type= to search plugins by 'type <type>' config param
    list = @agent.plugins_info_by_type(plugin_type, opts)

  elsif plugin_type = get_search_parameter(qs, 'type'.freeze)
    # Without @ version of ?@type= for backward compatibility
    list = @agent.plugins_info_by_type(plugin_type, opts)

  else
    # otherwise show all plugins
    list = @agent.plugins_info_all(opts)
  end

  return list, opts
end
do_GET(req, res) click to toggle source
# File lib/fluent/plugin/in_monitor_agent.rb, line 45
def do_GET(req, res)
  begin
    code, header, body = process(req, res)
  rescue
    code, header, body = render_json_error(500, {
        'message '=> 'Internal Server Error',
        'error' => "#{$!}",
        'backgrace'=> $!.backtrace,
      })
  end

  # set response code, header and body
  res.status = code
  header.each_pair {|k,v|
    res[k] = v
  }
  res.body = body
end
get_search_parameter(qs, param_name) click to toggle source
# File lib/fluent/plugin/in_monitor_agent.rb, line 140
def get_search_parameter(qs, param_name)
  return nil unless qs.has_key?(param_name)
  qs[param_name].first
end
render_json(obj, opts={}) click to toggle source
# File lib/fluent/plugin/in_monitor_agent.rb, line 145
def render_json(obj, opts={})
  render_json_error(200, obj, opts)
end
render_json_error(code, obj, opts={}) click to toggle source
# File lib/fluent/plugin/in_monitor_agent.rb, line 149
def render_json_error(code, obj, opts={})
  if opts[:pretty_json]
    js = JSON.pretty_generate(obj)
  else
    js = obj.to_json
  end
  [code, {'Content-Type'=>'application/json'}, js]
end