Class: Syslogstash::SyslogReader

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

Overview

A single socket reader.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Worker

#stop, #thread, #wait

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, tags, logstash, metrics)
  @file, @tags, @logstash, @metrics = file, tags, logstash, metrics

  log { "initializing syslog socket #{file} with tags #{tags.inspect}" }
end

Instance Attribute Details

#fileObject (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

#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.



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)
        process_message msg.first.chomp
      end
    ensure
      socket.close
      log { "removing socket file #{@file}" }
      File.unlink(@file) rescue nil
    end
  end
end