Skip to content

Commit

Permalink
Merge pull request #2422 from ganmacs/refactor-in-monitor
Browse files Browse the repository at this point in the history
Refactor in monitor
  • Loading branch information
repeatedly authored May 31, 2019
2 parents 85ad8d7 + e7d90df commit 241a37f
Showing 1 changed file with 27 additions and 43 deletions.
70 changes: 27 additions & 43 deletions lib/fluent/plugin/in_monitor_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def initialize(server, agent)

def do_GET(req, res)
begin
code, header, body = process(req, res)
code, header, body = process(req)
rescue
code, header, body = render_json_error(500, {
'message '=> 'Internal Server Error',
Expand All @@ -67,85 +67,69 @@ def do_GET(req, res)
res.body = body
end

def build_object(req, res)
def build_object(req)
unless req.path_info == ""
return render_json_error(404, "Not found")
end

qs = Hash.new { |_, _| [] }
# parse ?=query string
if req.query_string
begin
qs = CGI.parse(req.query_string)
qs.merge!(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 = {}

if qs['debug'.freeze].first
opts[:with_debug_info] = true
opts[:pretty_json] = true
end

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

if with_config = get_search_parameter(qs, 'with_config'.freeze)
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 = get_search_parameter(qs, 'with_retry'.freeze)
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

if tag = get_search_parameter(qs, 'tag'.freeze)
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 = get_search_parameter(qs, '@id'.freeze)
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_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)
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)

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

def get_search_parameter(qs, param_name)
return nil unless qs.has_key?(param_name)
qs[param_name].first
[list, opts]
end

def render_json(obj, opts={})
Expand All @@ -163,8 +147,8 @@ def render_json_error(code, obj, opts={})
end

class LTSVMonitorServlet < MonitorServlet
def process(req, res)
list, _opts = build_object(req, res)
def process(req)
list, _opts = build_object(req)
return unless list

normalized = JSON.parse(list.to_json)
Expand All @@ -186,8 +170,8 @@ def process(req, res)
end

class JSONMonitorServlet < MonitorServlet
def process(req, res)
list, opts = build_object(req, res)
def process(req)
list, opts = build_object(req)
return unless list

render_json({
Expand All @@ -197,7 +181,7 @@ def process(req, res)
end

class ConfigMonitorServlet < MonitorServlet
def build_object(req, res)
def build_object(req)
{
'pid' => Process.pid,
'ppid' => Process.ppid
Expand All @@ -206,8 +190,8 @@ def build_object(req, res)
end

class LTSVConfigMonitorServlet < ConfigMonitorServlet
def process(req, res)
result = build_object(req, res)
def process(req)
result = build_object(req)

row = []
JSON.parse(result.to_json).each_pair { |k, v|
Expand All @@ -220,8 +204,8 @@ def process(req, res)
end

class JSONConfigMonitorServlet < ConfigMonitorServlet
def process(req, res)
result = build_object(req, res)
def process(req)
result = build_object(req)
render_json(result)
end
end
Expand Down

0 comments on commit 241a37f

Please sign in to comment.