def list(args)
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[search]")
opts.on('--hosts HOSTS', String, "Filter logs to specific Host ID(s)") do |val|
params['servers'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
end
opts.on('--servers HOSTS', String, "alias for --hosts") do |val|
params['servers'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
end
opts.on('--vms HOSTS', String, "alias for --hosts") do |val|
params['servers'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
end
opts.on('--container CONTAINER', String, "Filter logs to specific Container ID(s)") do |val|
params['containers'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
end
opts.on('--cluster ID', String, "Filter logs to specific Cluster ID") do |val|
params['clusters'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
end
opts.on('--start TIMESTAMP','--start TIMESTAMP', "Start timestamp. Default is 30 days ago.") do |val|
options[:start] = parse_time(val)
end
opts.on('--end TIMESTAMP','--end TIMESTAMP', "End timestamp. Default is now.") do |val|
options[:end] = parse_time(val)
end
opts.on('--level VALUE', String, "Log Level. DEBUG,INFO,WARN,ERROR") do |val|
params['level'] = params['level'] ? [params['level'], val].flatten : [val]
end
opts.on('-t', '--table', "Format ouput as a table.") do
options[:table] = true
end
opts.on('-a', '--all', "Display all details: entire message." ) do
options[:details] = true
end
build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
opts.footer = "List logs for all hosts and containers."
end
optparse.parse!(args)
if args.count > 0
options[:phrase] = args.join(" ")
end
connect(options)
begin
params['level'] = params['level'].collect {|it| it.to_s.upcase }.join('|') if params['level']
params.merge!(parse_list_options(options))
if params['phrase']
options.delete(:phrase)
search_phrase = params.delete('phrase')
params['query'] = search_phrase
end
params['order'] = params['direction'] unless params['direction'].nil?
params['startMs'] = (options[:start].to_i * 1000) if options[:start]
params['endMs'] = (options[:end].to_i * 1000) if options[:end]
params['interval'] = options[:interval].to_s if options[:interval]
@logs_interface.setopts(options)
if options[:dry_run]
print_dry_run @logs_interface.dry.list(params)
return
end
json_response = @logs_interface.list(params)
render_result = json_response['logs'] ? render_with_format(json_response, options, 'logs') : render_with_format(json_response, options, 'data')
return 0 if render_result
title = "Morpheus Logs"
subtitles = parse_list_subtitles(options)
if options[:start]
subtitles << "Start: #{options[:start]}".strip
end
if options[:end]
subtitles << "End: #{options[:end]}".strip
end
if params['query']
subtitles << "Search: #{params['query']}".strip
end
if params['servers']
subtitles << "Servers: #{params['servers']}".strip
end
if params['containers']
subtitles << "Containers: #{params['containers']}".strip
end
if params['clusters']
subtitles << "Clusters: #{params['clusters']}".strip
end
if params['level']
subtitles << "Level: #{params['level']}"
end
print_h1 title, subtitles, options
logs = json_response['data'] || json_response['logs']
if logs.empty?
print "#{cyan}No logs found.#{reset}\n"
else
print format_log_records(logs, options)
print_results_pagination({'meta'=>{'total'=>(json_response['total']['value'] rescue json_response['total']),'size'=>logs.size,'max'=>(json_response['max'] || options[:max]),'offset'=>(json_response['offset'] || options[:offset] || 0)}})
end
print reset,"\n"
return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end