8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/papertrail/cli.rb', line 8
def run
options = {
:configfile => File.expand_path('~/.papertrail.yml'),
:delay => 2,
:follow => false
}
OptionParser.new do |opts|
opts.banner = "papertrail - command-line tail and search for Papertrail log management service"
opts.on("-h", "--help", "Show usage") do |v|
puts opts
exit
end
opts.on("-f", "--follow", "Continue running and print new events (off)") do |v|
options[:follow] = true
end
opts.on("-d", "--delay SECONDS", "Delay between refresh (2)") do |v|
options[:delay] = v.to_i
end
opts.on("-c", "--configfile PATH", "Path to config (~/.papertrail.yml)") do |v|
options[:configfile] = File.expand_path(v)
end
opts.on("-s", "--system SYSTEM", "System to search") do |v|
options[:system] = v
end
opts.on("-g", "--group GROUP", "Group to search") do |v|
options[:group] = v
end
opts.separator usage
end.parse!
credentials = open(options[:configfile]) do |f|
YAML.load(f)
end
if credentials['token']
connection = Papertrail::Connection.new(:token => credentials['token'])
else
connection = Papertrail::Connection.new(:username => credentials['username'], :password => credentials['password'])
end
query_options = {}
if options[:system]
query_options[:system_id] = connection.find_id_for_source(options[:system])
end
if options[:group]
query_options[:group_id] = connection.find_id_for_group(options[:group])
end
search_query = connection.query(ARGV[0], query_options)
if options[:follow]
loop do
search_query.search.events.each do |event|
$stdout.puts event
end
$stdout.flush
sleep options[:delay]
end
else
search_query.search.events.each do |event|
$stdout.puts event
end
end
end
|