Module: MogilefsdLogTailer

Defined in:
lib/mogilefsd_log_tailer.rb,
lib/mogilefsd_log_tailer/version.rb,
lib/mogilefsd_log_tailer/tail_handler.rb

Defined Under Namespace

Classes: TailHandler

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.parse_options!Object



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

def self.parse_options!
  hosts = []
  filename = nil
  daemonize = false

  OptionParser.new do |o|
    script_name = File.basename($0)
    o.set_summary_indent('  ')
    o.banner = "#{script_name} version #{VERSION}\nUsage: #{script_name} [options] tracker1:port1 tracker2:port2 ..."
    o.separator ""
    o.on("-d", "--[no-]daemonize", "Run as a daemon.") { |d| daemonize = d }
    o.on("-f", "--file FILE", "The log file to which the output should go.") { |n| filename = File.expand_path(n) }
    o.on("-h", "--help", "Show this help message.") { puts o; exit }
  end.parse!

  while (h = ARGV.shift)
    hosts << h
  end

  if hosts.empty?
    STDERR.puts("No hosts to tail.")
    exit(1)
  end

  [
    hosts,
    {
      :daemonize => daemonize,
      :filename => filename,
    },
  ]
end

.runObject



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
# File 'lib/mogilefsd_log_tailer.rb', line 40

def self.run
  hosts, options = parse_options!

  log_file = $stdout
  log_dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))

  if options[:filename]
    log_dir = File.dirname(options[:filename])
  end

  if options[:daemonize]
    Daemons.daemonize({
        :app_name => 'mogilefsd_tail',
        :dir_mode => :normal,
        :dir => log_dir,
        :log_output => true,
      })
  end

  if options[:filename]
    log_file = File.open(options[:filename], 'ab')
  end

  EventMachine.run do
    hosts.each do |hp|
      host, port = hp.split(':')
      EventMachine.connect(host, port.to_i,
        TailHandler, host, port.to_i, log_file)
    end
  end
end