Class: Papertrail::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/papertrail/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



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
# File 'lib/papertrail/connection.rb', line 15

def initialize(options)
  ssl_options = {
    :verify => options.fetch(:verify_ssl) { OpenSSL::SSL::VERIFY_PEER }
  }

  unless (options[:username] && options[:password]) || options[:token]
    raise ArgumentError, "Must provide a username and password or a token"
  end

  # Make Ubuntu OpenSSL work
  #
  # From: https://bugs.launchpad.net/ubuntu/+source/openssl/+bug/396818
  # "[OpenSSL] does not presume to select a set of CAs by default."
  if File.file?('/etc/ssl/certs/ca-certificates.crt')
    ssl_options[:ca_file] = '/etc/ssl/certs/ca-certificates.crt'
  end

  @connection = Papertrail::HttpClient.new(ssl_options).tap do |conn|
    if options[:username] && options[:password]
      conn.basic_auth(options[:username], options[:password])
    else
      conn.token_auth(options[:token])
    end
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



11
12
13
# File 'lib/papertrail/connection.rb', line 11

def connection
  @connection
end

Instance Method Details

#create_group(name, system_wildcard = nil) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/papertrail/connection.rb', line 107

def create_group(name, system_wildcard = nil)
  group = { :group => { :name => name } }
  if system_wildcard
    group[:group][:system_wildcard] = system_wildcard
  end
  @connection.post("groups.json", group)
end

#each_event(query_term = nil, options = {}, &block) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/papertrail/connection.rb', line 181

def each_event(query_term = nil, options = {}, &block)
  # If there was no query but there were options, shuffle around
  # the parameters
  if query_term.is_a?(Hash)
    options, query_term = query_term, nil
  end

  # Remove all of the options that shouldn't be in each query
  options  = { :tail => false }.merge(options)
  min_id   = options.delete(:min_id)
  max_id   = options.delete(:max_id)
  min_time = options.delete(:min_time)
  max_time = options.delete(:max_time)

  # Figure out where to start querying
  if min_id
    search = search_query(query_term, options.merge(:min_id => min_id))
  elsif min_time
    search = search_query(query_term, options.merge(:min_time => min_time.to_i))
  else
    raise ArgumentError, "Either :min_id or :min_time must be specified"
  end

  # Start processing events
  loop do
    search_results = search.next_results_page
    search_results.events.each do |event|
      # If we've found an event beyond what we were looking for, we're done
      break if max_time && event.received_at > max_time
      break if max_id && event.id > max_id

      block.call(event)
    end

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

    # If we've reached the most current log message, we're done
    break if search_results.reached_end?
  end

  nil
end

#find_id_for_group(name) ⇒ Object



60
61
62
63
# File 'lib/papertrail/connection.rb', line 60

def find_id_for_group(name)
  response = @connection.get('groups.json', :group_name => name)
  find_id_for_item(response.body, name)
end

#find_id_for_item(items, name_wanted) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/papertrail/connection.rb', line 98

def find_id_for_item(items, name_wanted)
  item = find_item_by_name(items, name_wanted)
  if item
    return item['id']
  end

  return nil
end

#find_id_for_source(name) ⇒ Object



54
55
56
57
58
# File 'lib/papertrail/connection.rb', line 54

def find_id_for_source(name)
  response = @connection.get('systems.json', :system_name => name)

  find_id_for_item(response.body, name)
end

#find_item_by_name(items, name_wanted) ⇒ Object



94
95
96
# File 'lib/papertrail/connection.rb', line 94

def find_item_by_name(items, name_wanted)
  find_items_by_name(items, name_wanted).first
end

#find_items_by_name(items, name_wanted) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/papertrail/connection.rb', line 80

def find_items_by_name(items, name_wanted)
  results = []

  items.each do |item|
    results << item if item['name'] == name_wanted
  end

  items.each do |item|
    results << item if item['name'] =~ /#{Regexp.escape(name_wanted)}/i
  end

  results
end

#find_search(name, group_id = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/papertrail/connection.rb', line 65

def find_search(name, group_id = nil)
  response = @connection.get('searches.json', :search_name => name)

  candidates = find_items_by_name(response.body, name)
  return nil if candidates.empty?

  candidates.each do |item|
    if !group_id || group_id == item['group_id']
      return item
    end
  end

  return candidates.first
end

#finishObject



50
51
52
# File 'lib/papertrail/connection.rb', line 50

def finish
  @connection.finish
end

#join_group(source_name, group_name) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/papertrail/connection.rb', line 127

def join_group(source_name, group_name)
  source_id = find_id_for_source(source_name)
  group_id = find_id_for_group(group_name)
  if source_id && group_id
    @connection.post("systems/#{source_id}/join.json", :group_id => group_id)
  end
end

#leave_group(source_name, group_name) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/papertrail/connection.rb', line 135

def leave_group(source_name, group_name)
  source_id = find_id_for_source(source_name)
  group_id = find_id_for_group(group_name)
  if source_id && group_id
    @connection.post("systems/#{source_id}/leave.json", :group_id => group_id)
  end
end

#register_source(name, *args) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/papertrail/connection.rb', line 143

def register_source(name, *args)
  options = args.last.is_a?(Hash) ? args.pop.dup : {}

  if ip_address = args.shift
    options[:ip_address] = ip_address
  end

  if hostname = args.shift
    options[:hostname] = hostname
  end

  request = {
    :system => {
      :name => name
    }
  }

  if options[:ip_address]
    request[:system][:ip_address] = options[:ip_address]
  end

  if options[:hostname]
    request[:system][:hostname] = options[:hostname]
  end

  if options[:destination_port]
    request[:destination_port] = options[:destination_port]
  end

  @connection.post("systems.json", request)
end

#show_group(name) ⇒ Object



115
116
117
118
119
# File 'lib/papertrail/connection.rb', line 115

def show_group(name)
  if id = find_id_for_group(name)
    @connection.get("groups/#{id}.json").body
  end
end

#show_source(name) ⇒ Object



121
122
123
124
125
# File 'lib/papertrail/connection.rb', line 121

def show_source(name)
  if id = find_id_for_source(name)
    @connection.get("systems/#{id}.json").body
  end
end

#startObject



41
42
43
44
45
46
47
48
# File 'lib/papertrail/connection.rb', line 41

def start
  if block_given?
    @connection.start { yield self }
  else
    @connection.start
    self
  end
end

#unregister_source(name) ⇒ Object



175
176
177
178
179
# File 'lib/papertrail/connection.rb', line 175

def unregister_source(name)
  if source_id = find_id_for_source(name)
    @connection.delete("systems/#{source_id}.json")
  end
end