Class: Papertrail::Cli

Inherits:
Object
  • Object
show all
Includes:
CliHelpers
Defined in:
lib/papertrail/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CliHelpers

#find_configfile, #load_configfile, #output_http_error, #parse_time, #set_min_max_time!, #symbolize_keys

Constructor Details

#initializeCli

Returns a new instance of Cli.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/papertrail/cli.rb', line 15

def initialize
  @options = {
    :configfile => nil,
    :delay  => 2,
    :follow => false,
    :token  => ENV['PAPERTRAIL_API_TOKEN']
  }

  @query_options = {}
  @query = nil
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



13
14
15
# File 'lib/papertrail/cli.rb', line 13

def connection
  @connection
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/papertrail/cli.rb', line 13

def options
  @options
end

#query_optionsObject (readonly)

Returns the value of attribute query_options.



13
14
15
# File 'lib/papertrail/cli.rb', line 13

def query_options
  @query_options
end

Instance Method Details

#display_results(results) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/papertrail/cli.rb', line 161

def display_results(results)
  if options[:json]
    $stdout.puts Papertrail::OkJson.encode(results.data)
  else
    results.events.each do |event|
      $stdout.puts event
    end
  end

  $stdout.flush
end

#query_time_rangeObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/papertrail/cli.rb', line 124

def query_time_range
  min_time = parse_time(options[:min_time])

  if options[:max_time]
    max_time = parse_time(options[:max_time])
  end

  search_results = connection.query(@query, query_options.merge(:min_time => min_time.to_i, :tail => false)).search

  loop do
    search_results.events.each do |event|
      # If we've found an event beyond what we were looking for, we're done
      if max_time && event.received_at > max_time
        break
      end

      if options[:json]
        $stdout.puts Papertrail::OkJson.encode(event.data)
      else
        $stdout.puts event
      end
    end

    # If we've found the end of what we're looking for, we're done
    if max_time && search_results.max_time_at > max_time
      break
    end

    if search_results.reached_end?
      break
    end

    # Perform the next search
    search_results = connection.query(@query, query_options.merge(:min_id => search_results.max_id, :tail => false)).search
  end
end

#runObject



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/papertrail/cli.rb', line 27

def run
  if configfile = find_configfile
    configfile_options = load_configfile(configfile)
    options.merge!(configfile_options)
  end

  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.on("-S", "--search SEARCH", "Saved search to search") do |v|
      options[:search] = v
    end
    opts.on("-j", "--json", "Output raw json data") do |v|
      options[:json] = true
    end
    opts.on("--min-time MIN", "Earliest time to search from.") do |v|
      options[:min_time] = v
    end
    opts.on("--max-time MAX", "Latest time to search from.") do |v|
      options[:max_time] = v
    end

    opts.separator usage
  end.parse!

  if options[:configfile]
    configfile_options = load_configfile(options[:configfile])
    options.merge!(configfile_options)
  end
  
  unless options[:token]
    abort 'Authentication token not found. Set config file "token" attribute or PAPERTRAIL_API_TOKEN.'
  end

  @connection = Papertrail::Connection.new(options)

  if options[:system]
    query_options[:system_id] = connection.find_id_for_source(options[:system])
    unless query_options[:system_id]
      abort "System \"#{options[:system]}\" not found"
    end
  end

  if options[:group]
    query_options[:group_id] = connection.find_id_for_group(options[:group])
    unless query_options[:group_id]
      abort "Group \"#{options[:group]}\" not found"
    end
  end

  if options[:search]
    search = connection.find_search(options[:search], query_options[:group_id])
    unless search
      abort "Search \"#{options[:search]}\" not found"
    end

    query_options[:group_id] ||= search['group_id']
    @query = search['query']
  end

  @query ||= ARGV[0]

  if options[:follow]
    search_query = connection.query(@query, query_options)

    loop do
      display_results(search_query.search)
      sleep options[:delay]
    end
  elsif options[:min_time]
    query_time_range
  else
    set_min_max_time!(options, query_options)
    search_query = connection.query(@query, query_options)
    display_results(search_query.search)
  end
end

#usageObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/papertrail/cli.rb', line 174

def usage
  <<-EOF

  Usage:
papertrail [-f] [-s system] [-g group] [-S search] [-d seconds] \
  [-c papertrail.yml] [-j] [--min-time time] [--max-time time] [query]

  Examples:
papertrail -f
papertrail something
papertrail 1.2.3 Failure
papertrail -s ns1 "connection refused"
papertrail -f "(www OR db) (nginx OR pgsql) -accepted"
papertrail -f -g Production "(nginx OR pgsql) -accepted"
papertrail -g Production --min-time 'yesterday at noon' --max-time 'today at 4am'

  More: https://papertrailapp.com/

  EOF
end