Class: Syslogstash::SyslogReader

Inherits:
Object
  • Object
show all
Defined in:
lib/syslogstash/syslog_reader.rb

Overview

A single socket reader.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg, logstash, stats) ⇒ SyslogReader

Returns a new instance of SyslogReader.



6
7
8
9
10
11
12
13
# File 'lib/syslogstash/syslog_reader.rb', line 6

def initialize(cfg, logstash, stats)
	@file, @logstash, @stats = cfg.syslog_socket, logstash, stats

	@add_fields = cfg.add_fields
	@relay_to   = cfg.relay_sockets
	@cfg        = cfg
	@logger     = cfg.logger
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



4
5
6
# File 'lib/syslogstash/syslog_reader.rb', line 4

def thread
  @thread
end

Instance Method Details

#runObject

Start reading from the socket file, parsing entries, and flinging them at logstash. This method will return, with the operation continuing in a separate thread.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/syslogstash/syslog_reader.rb', line 19

def run
	@logger.debug("reader") { "#run called" }

	begin
		socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
		socket.bind(Socket.pack_sockaddr_un(@file))
		File.chmod(0666, @file)
	rescue Errno::EEXIST, Errno::EADDRINUSE
		@logger.info("reader") { "socket file #{@file} already exists; deleting" }
		File.unlink(@file) rescue nil
		retry
	rescue StandardError => ex
		raise ex.class, "Error while trying to bind to #{@file}: #{ex.message}", ex.backtrace
	end

	@thread = Thread.new do
		begin
			loop do
				msg = socket.recvmsg
				@logger.debug("reader") { "Message received: #{msg.inspect}" }
				@stats.received(@file)
				relay_message msg.first
				process_message msg.first.chomp
			end
		ensure
			socket.close
			@logger.debug("reader") { "removing socket file #{@file}" }
			File.unlink(@file) rescue nil
		end
	end
end