Class: OAS::LogCollector::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/oas/log_collector/manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Manager

Returns a new instance of Manager.



9
10
11
12
13
14
# File 'lib/oas/log_collector/manager.rb', line 9

def initialize(opts = {})
  sources = opts.delete(:sources) || []
  sources.each { |host, user, port| (opts[:sources] ||= []) << OAS::LogCollector::Source.new(host, user, port || 22) }

  options.merge!(opts)
end

Instance Method Details

#collect(source) ⇒ Object



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
# File 'lib/oas/log_collector/manager.rb', line 34

def collect(source)
  hostname = source.host

  result = source.find(options[:source_logs_path], {
    :mtime => "-#{options[:days_to_search]}",
    :type  => 'f',
    :regex => '.*\.log\.[0-9]+\.bz2'
  })

  files = Hash.new
  total = 0
  last_timestamp = last_run = get_last_run(hostname)
  result.sort!.each do |path|
    log_file  = OAS::LogCollector::LogFile.new(path)
    if log_file.timestamp > Integer(last_run) && total < options[:files_to_copy]
      files[path] = File.join(options[:logs_path], hostname, log_file.year, log_file.month, log_file.day, log_file.name)
      last_timestamp = log_file.timestamp
      total += 1
    end
  end

  if files.any?
    source.download(files)
    set_last_run(hostname, last_timestamp)
  end
end

#get_last_run(hostname) ⇒ Object



61
62
63
64
65
# File 'lib/oas/log_collector/manager.rb', line 61

def get_last_run(hostname)
  OAS::LogCollector.redis do |conn|
    conn.hget("timestamps", hostname) || 0
  end
end

#optionsObject



30
31
32
# File 'lib/oas/log_collector/manager.rb', line 30

def options
  OAS::LogCollector.options
end

#set_last_run(hostname, timestamp) ⇒ Object



67
68
69
70
71
# File 'lib/oas/log_collector/manager.rb', line 67

def set_last_run(hostname, timestamp)
  OAS::LogCollector.redis do |conn|
    conn.hset("timestamps", hostname, timestamp)
  end
end

#start!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/oas/log_collector/manager.rb', line 16

def start!
  loop do
    threads = []
    options[:sources].each do |source|
      threads << Thread.new do
        collect(source)
      end
    end
    threads.each(&:join)

    sleep options[:sleep_seconds]
  end
end