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+) ([^: ]+):? (.*)$/
}
DEFAULT_PID_FILES =
[
  "/var/run/remote_syslog.pid",
  "#{ENV['HOME']}/run/remote_syslog.pid",
  "#{ENV['HOME']}/tmp/remote_syslog.pid",
  "#{ENV['HOME']}/remote_syslog.pid",
  "#{ENV['TMPDIR']}/remote_syslog.pid",
  "/tmp/remote_syslog.pid"
]
DEFAULT_CONFIG_FILE =
'/etc/log_files.yml'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/remote_syslog/cli.rb', line 34

def initialize(argv)
  @argv = argv
  @program_name = File.basename($0)

  @strip_color = false
  @exclude_pattern = nil

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

  @agent = RemoteSyslog::Agent.new(:pid_file => default_pid_file)
end

Instance Attribute Details

#program_nameObject (readonly)

Returns the value of attribute program_name.



32
33
34
# File 'lib/remote_syslog/cli.rb', line 32

def program_name
  @program_name
end

Class Method Details

.process!(argv) ⇒ Object



26
27
28
29
30
# File 'lib/remote_syslog/cli.rb', line 26

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

Instance Method Details

#default_pid_fileObject



57
58
59
60
61
# File 'lib/remote_syslog/cli.rb', line 57

def default_pid_file
  DEFAULT_PID_FILES.each do |file|
    return file if is_file_writable?(file)
  end
end

#error(message, try_help = false) ⇒ Object



284
285
286
287
288
289
290
# File 'lib/remote_syslog/cli.rb', line 284

def error(message, try_help = false)
  puts "#{program_name}: #{message}"
  if try_help
    puts "Try `#{program_name} --help' for more information."
  end
  exit(1)
end

#is_file_writable?(file) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/remote_syslog/cli.rb', line 51

def is_file_writable?(file)
  directory = File.dirname(file)

  (File.directory?(directory) && File.writable?(directory) && !File.exists?(file)) || File.writable?(file)
end

#parseObject



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
123
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/remote_syslog/cli.rb', line 63

def parse
  op = OptionParser.new do |opts|
    opts.banner = "Usage: #{program_name} [OPTION]... <FILE>..."
    opts.separator ''

    opts.separator "Options:"

    opts.on("-c", "--configfile PATH", "Path to config (/etc/log_files.yml)") do |v|
      @configfile = v
    end
    opts.on("-d", "--dest-host HOSTNAME", "Destination syslog hostname or IP (logs.papertrailapp.com)") do |v|
      @agent.destination_host = v
    end
    opts.on("-p", "--dest-port PORT", "Destination syslog port (514)") do |v|
      @agent.destination_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|
      @agent.facility = v
    end
    opts.on("--hostname HOST", "Local hostname to send from") do |v|
      @agent.hostname = v
    end
    opts.on("-P", "--pid-dir DIRECTORY", "DEPRECATED: Directory to write .pid file in") do |v|
      puts "Warning: --pid-dir is deprecated. Please use --pid-file FILENAME instead"
      @pid_directory = v
    end
    opts.on("--pid-file FILENAME", "Location of the PID file (default #{@agent.pid_file})") do |v|
      @agent.pid_file = v
    end
    opts.on("--parse-syslog", "Parse file as syslog-formatted file") do
      @agent.parse_fields = FIELD_REGEXES['syslog']
    end
    opts.on("-s", "--severity SEVERITY", "Severity (notice)") do |v|
      @agent.severity = v
    end
    opts.on("--strip-color", "Strip color codes") do
      @agent.strip_color = true
    end
    opts.on("--tcp", "Connect via TCP (no TLS)") do
      @agent.tcp = true
    end
    opts.on("--tls", "Connect via TCP with TLS") do
      @agent.tls = true
    end


    opts.on("--new-file-check-interval INTERVAL", OptionParser::DecimalInteger,
      "Time between checks for new files") do |v|
      @agent.glob_check_interval = v
    end

    opts.separator ''
    opts.separator 'Advanced options:'

    opts.on("--[no-]eventmachine-tail", "Enable or disable using eventmachine-tail") do |v|
      @agent.eventmachine_tail = v
    end
    opts.on("--debug-log FILE", "Log internal debug messages") do |v|
      @agent.log_file = v
    end

    severities = Logger::Severity.constants + Logger::Severity.constants.map { |s| s.downcase }
    opts.on("--debug-level LEVEL", severities, "Log internal debug messages at level") do |v|
      @agent.logger.level = Logger::Severity.const_get(v.upcase)
    end

    opts.separator ""
    opts.separator "Common options:"

    opts.on("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    opts.on("--version", "Show version") do
      puts RemoteSyslog::VERSION
      exit(0)
    end

    opts.separator ''
    opts.separator "Example:"
    opts.separator "    $ #{program_name} -c configs/logs.yml -p 12345 /var/log/mysqld.log"
  end

  op.parse!(@argv)

  @files = @argv.dup.delete_if { |a| a.empty? }

  if @configfile
    if File.exists?(@configfile)
      parse_config(@configfile)
    else
      error "The config file specified could not be found: #{@configfile}"
    end
  elsif File.exists?(DEFAULT_CONFIG_FILE)
    parse_config(DEFAULT_CONFIG_FILE)
  end

  if @files.empty?
    error "You must specify at least one file to watch"
  end

  @agent.destination_host ||= 'logs.papertrailapp.com'
  @agent.destination_port ||= 514

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

  @agent.files = @files

  if @pid_directory
    if @agent.pid_file
      @agent.pid_file = File.expand_path("#{@pid_directory}/#{File.basename(@agent.pid_file)}")
    else
      @agent.pid_file = File.expand_path("#{@pid_directory}/remote_syslog.pid")
    end
  end

  # We're dealing with an old-style pid_file
  if @agent.pid_file && File.basename(@agent.pid_file) == @agent.pid_file
    default_pid_dir = File.dirname(default_pid_file)

    @agent.pid_file = File.join(default_pid_dir, @agent.pid_file)

    if File.extname(@agent.pid_file) == ''
      @agent.pid_file << '.pid'
    end
  end

  @agent.pid_file ||= default_pid_file

  if !@no_detach && !::Servolux.fork?
    @no_detach = true

    puts "Fork is not supported in this Ruby environment. Running in foreground."
  end
rescue OptionParser::ParseError => e
  error e.message, true
end

#parse_config(file) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/remote_syslog/cli.rb', line 206

def parse_config(file)
  config = YAML.load_file(file)

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

  if config['destination'] && config['destination']['host']
    @agent.destination_host ||= config['destination']['host']
  end

  if config['destination'] && config['destination']['port']
    @agent.destination_port ||= config['destination']['port']
  end

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

  @agent.server_cert        = config['ssl_server_cert']
  @agent.client_cert_chain  = config['ssl_client_cert_chain']
  @agent.client_private_key = config['ssl_client_private_key']

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

  if config['exclude_patterns']
    @agent.exclude_pattern = Regexp.new(config['exclude_patterns'].map { |r| "(#{r})" }.join('|'))
  end

  if config['exclude_files']
    @agent.exclude_file_pattern = Regexp.new(config['exclude_files'].map { |r| "(#{r})" }.join('|'))
  end

  if config['new_file_check_interval']
    @agent.glob_check_interval = config['new_file_check_interval']
  end

  if config['prepend']
    @agent.prepend = config['prepend']
  end
end

#redirect_ioObject



279
280
281
# File 'lib/remote_syslog/cli.rb', line 279

def redirect_io
  @agent.redirect_io!
end

#runObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/remote_syslog/cli.rb', line 248

def run
  Thread.abort_on_exception = true

  if @agent.tls && !EventMachine.ssl?
    error "TLS is not supported by eventmachine installed on this system.\nThe openssl-devel/openssl-dev package must be installed before installing eventmachine."
  end

  if @no_detach
    puts "Watching #{@agent.files.length} files/globs. Sending to #{@agent.destination_host}:#{@agent.destination_port} (#{@agent.endpoint_mode})."
    @agent.run
  else
    daemon = Servolux::Daemon.new(:server => @agent, :after_fork => method(:redirect_io))

    if daemon.alive?
      error "Already running at #{@agent.pid_file}. To run another instance, specify a different `--pid-file`.", true
    end

    puts "Watching #{@agent.files.length} files/globs. Sending to #{@agent.destination_host}:#{@agent.destination_port} (#{@agent.endpoint_mode})."
    daemon.startup
  end
rescue Servolux::Daemon::StartupError => e
  case message = e.message[/^(Child raised error: )?(.*)$/, 2]
  when /#<Errno::EACCES: (.*)>$/
    error $1
  else
    error message
  end
rescue Interrupt
  exit(0)
end