Class: RemoteSyslog::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_syslog/cli.rb

Constant Summary collapse

FIELD_REGEXES =
{
  'syslog' => /^(\w+ +\d+ \S+) (\S+) ([^: ]+):? (.*)$/,
  'rfc3339' => /^(\S+) (\S+) ([^: ]+):? (.*)$/
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/remote_syslog/cli.rb', line 20

def initialize(argv)
  @argv = argv

  @app_name = File.basename($0) || 'remote_syslog'

  @configfile  = '/etc/log_files.yml'
  @strip_color = false

  @daemonize_options = {
    :ARGV         => %w(start),
    :dir_mode     => :system,
    :backtrace    => false,
    :monitor      => false,
  }
end

Class Method Details

.process!(argv) ⇒ Object



14
15
16
17
18
# File 'lib/remote_syslog/cli.rb', line 14

def self.process!(argv)
  c = new(argv)
  c.parse
  c.run
end

Instance Method Details

#parseObject



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

def parse
  op = OptionParser.new do |opts|
    opts.banner = "Usage:   remote_syslog [options] <path to add'l log 1> .. <path to add'l log n>"
    opts.separator ''
    opts.separator "Example: remote_syslog -c configs/logs.yml -p 12345 /var/log/mysqld.log"
    opts.separator ''
    opts.separator "Options:"

    opts.on("-c", "--configfile PATH", "Path to config (/etc/log_files.yml)") do |v|
      @configfile = File.expand_path(v)
    end
    opts.on("-d", "--dest-host HOSTNAME", "Destination syslog hostname or IP (logs.papertrailapp.com)") do |v|
      @dest_host = v
    end
    opts.on("-p", "--dest-port PORT", "Destination syslog port (514)") do |v|
      @dest_port = v
    end
    opts.on("-D", "--no-detach", "Don't daemonize and detach from the terminal") do
      @no_detach = true
    end
    opts.on("-f", "--facility FACILITY", "Facility (user)") do |v|
      @facility = v
    end
    opts.on("--hostname HOST", "Local hostname to send from") do |v|
      @hostname = v
    end
    opts.on("-P", "--pid-dir DIRECTORY", "Directory to write .pid file in (/var/run/)") do |v|
      @daemonize_options[:dir_mode] = :normal
      @daemonize_options[:dir] = v
    end
    opts.on("--pid-file FILENAME", "PID filename (<program name>.pid)") do |v|
      self.pid_file = v
    end
    opts.on("--parse-syslog", "Parse file as syslog-formatted file") do
      @parse_fields = FIELD_REGEXES['syslog']
    end
    opts.on("-s", "--severity SEVERITY", "Severity (notice)") do |v|
      @severity = v
    end
    opts.on("--strip-color", "Strip color codes") do
      @strip_color = true
    end
    opts.on("--tls", "Connect via TCP with TLS") do
      @tls = true
    end
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end

  op.parse!(@argv)

  @files = @argv

  parse_config

  @dest_host ||= 'logs.papertrailapp.com'
  @dest_port ||= 514

  if @files.empty?
    puts "No filenames provided and #{@configfile} not found."
    puts ''
    puts op
    exit
  end

  # handle relative paths before Daemonize changes the wd to / and expand wildcards
  @files = @files.map { |f| Dir.glob(f) }.flatten.map { |f| File.expand_path(f) }.uniq

end

#parse_configObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/remote_syslog/cli.rb', line 118

def parse_config
  if File.exist?(@configfile)
    config = open(@configfile) do |f|
      YAML.load(f)
    end

    @files += Array(config['files'])

    if config['destination'] && config['destination']['host']
      @dest_host ||= config['destination']['host']
    end

    if config['destination'] && config['destination']['port']
      @dest_port ||= config['destination']['port']
    end

    if config['hostname']
      @hostname = config['hostname']
    end

    if config['parse_fields']
      @parse_fields = FIELD_REGEXES[config['parse_fields']] || Regexp.new(config['parse_fields'])
    end
  end
end

#pid_file=(v) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/remote_syslog/cli.rb', line 36

def pid_file=(v)
  m = v.match(%r{^(.+/)?([^/]+?)(\.pid)?$})
  if m[1]
    @daemonize_options[:dir_mode] = :normal
    @daemonize_options[:dir] = m[1]
  end

  @app_name = m[2]
end

#runObject



144
145
146
147
148
149
150
151
152
# File 'lib/remote_syslog/cli.rb', line 144

def run
  if @no_detach
    start
  else
    Daemons.run_proc(@app_name, @daemonize_options) do
      start
    end
  end
end

#startObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/remote_syslog/cli.rb', line 154

def start
  EventMachine.run do
    if @tls
      connection = TlsEndpoint.new(@dest_host, @dest_port)
    else
      connection = UdpEndpoint.new(@dest_host, @dest_port)
    end

    @files.each do |path|
      begin
        EventMachine::file_tail(path, RemoteSyslog::Reader,
          @dest_host, @dest_port,
          :socket => connection, :facility => @facility,
          :severity => @severity, :strip_color => @strip_color,
          :hostname => @hostname, :parse_fields => @parse_fields)
      rescue Errno::ENOENT => e
        puts "#{path} not found, continuing. (#{e.message})"
      end
    end
  end
end