Class: Syslogstash::SyslogReader
- Inherits:
-
Object
- Object
- Syslogstash::SyslogReader
- Includes:
- Worker
- Defined in:
- lib/syslogstash/syslog_reader.rb
Overview
A single socket reader.
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
-
#initialize(file, tags, logstash, metrics) ⇒ SyslogReader
constructor
A new instance of SyslogReader.
-
#run ⇒ Object
Start reading from the socket file, parsing entries, and flinging them at logstash.
Methods included from Worker
Constructor Details
#initialize(file, tags, logstash, metrics) ⇒ SyslogReader
Returns a new instance of SyslogReader.
10 11 12 13 14 |
# File 'lib/syslogstash/syslog_reader.rb', line 10 def initialize(file, , logstash, metrics) @file, , @logstash, @metrics = file, , logstash, metrics log { "initializing syslog socket #{file} with tags #{tags.inspect}" } end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
8 9 10 |
# File 'lib/syslogstash/syslog_reader.rb', line 8 def file @file end |
Instance Method Details
#run ⇒ Object
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.
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 20 def run debug { "#run called" } begin socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0) socket.bind(Socket.pack_sockaddr_un(@file)) rescue Errno::EEXIST, Errno::EADDRINUSE log { "socket file #{@file} already exists; deleting" } File.unlink(@file) rescue nil retry rescue SystemCallError $stderr.puts "Error while trying to bind to #{@file}" raise end @worker = Thread.new do begin loop do msg = socket.recvmsg debug { "Message received: #{msg.inspect}" } @metrics.received(@file, Time.now) msg.first.chomp end ensure socket.close log { "removing socket file #{@file}" } File.unlink(@file) rescue nil end end end |