Class: LogStash::Inputs::Syslog

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/inputs/syslog.rb

Defined Under Namespace

Classes: TCPInput, UDPInput

Instance Attribute Summary

Attributes inherited from Base

#logger

Instance Method Summary collapse

Methods inherited from Base

#initialize, #tag

Constructor Details

This class inherits a constructor from LogStash::Inputs::Base

Instance Method Details

#receive(host, port, message) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/logstash/inputs/syslog.rb', line 31

def receive(host, port, message)
  url = @url.clone
  url.host = host
  url.port = port

  # Do some syslog relay-like behavior.
  # * Add syslog headers if there are none
  event = LogStash::Event.new({
    "@message" => message,
    "@type" => @type,
    "@tags" => @tags.clone,
  })
  syslog_relay(event, url)
  @logger.debug(["Got event", event.class, event.to_hash])
  @callback.call(event)
end

#registerObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/logstash/inputs/syslog.rb', line 10

def register
  if !@url.host or !@url.port
    @logger.fatal("No host or port given in #{self.class}: #{@url}")
    # TODO(sissel): Make this an actual exception class
    raise "configuration error"
  end

  @logger.info("Starting tcp listener for #{@url}")
  EventMachine::start_server(@url.host, @url.port, TCPInput, self, @logger)

  @logger.info("Starting udp listener for #{@url}")
  EventMachine::open_datagram_socket(@url.host, @url.port, UDPInput, self,
                                     @logger)

  # This comes from RFC3164, mostly.
  @@syslog_re ||= \
    /(?:<([0-9]{1,3})>)?([A-z]{3}  ?[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}) (?:(\S+[^:]) )?(.*)/
    #<priority>      timestamp          Mmm dd hh:mm:ss             host  msg
end

#syslog_relay(event, url) ⇒ Object



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
# File 'lib/logstash/inputs/syslog.rb', line 54

def syslog_relay(event, url)
  match = @@syslog_re.match(event.message)
  if match
    # match[1,2,3,4] = {pri, timestamp, hostname, message}
    # Per RFC3164, priority = (facility * 8) + severity
    #                       = (facility << 3) & (severity)
    priority = match[1].to_i
    severity = priority & 7   # 7 is 111 (3 bits)
    facility = priority >> 3
    event.fields["priority"] = priority
    event.fields["severity"] = severity
    event.fields["facility"] = facility
   
    # TODO(sissel): Use the date filter, somehow.
    event.timestamp = LogStash::Time.to_iso8601(
      DateTime.strptime(match[2], "%b %d %H:%M:%S"))

    # At least the hostname is simple...
    url.host = match[3]
    url.port = nil
    event.source = url

    event.message = match[4]
  else
    @logger.info(["NOT SYSLOG", event.message])
    url.host = Socket.gethostname if url.host == "127.0.0.1"

    # RFC3164 says unknown messages get pri=13
    priority = 13
    severity = priority & 7   # 7 is 111 (3 bits)
    facility = priority >> 3
    event.fields["priority"] = 13
    event.fields["severity"] = 5   # 13 & 7 == 5
    event.fields["facility"] = 1   # 13 >> 3 == 1

    # Don't need to modify the message, here.
    # event.message = ...

    event.source = url
  end
end