Class: Fluent::Syslog
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::Syslog
show all
- Defined in:
- lib/fluent/plugin/out_syslog.rb
Defined Under Namespace
Classes: SocketFailureError
Constant Summary
collapse
- DISCARD_STRING =
declare const string for nullifying token if we decide to discard records
'DISCARD'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#sockets ⇒ Object
Returns the value of attribute sockets.
6
7
8
|
# File 'lib/fluent/plugin/out_syslog.rb', line 6
def sockets
@sockets
end
|
Instance Method Details
31
32
33
34
|
# File 'lib/fluent/plugin/out_syslog.rb', line 31
def configure(conf)
super
end
|
#create_packet(tag, time, record) ⇒ Object
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
|
# File 'lib/fluent/plugin/out_syslog.rb', line 75
def create_packet(tag, time, record)
if record.dig('log')
begin
record['message'] = record['log']
record.delete('log')
end
end
if record.dig('levelname')
begin
record['level'] = record['levelname']
record.delete('levelname')
end
end
if @parse_json
begin
parser = Yajl::Parser.new
parsed_message = parser.parse(record['message'])
if parsed_message.is_a?(Hash)
record = record.merge(parsed_message)
end
rescue Yajl::ParseError
end
end
pri = 134
version = 1
record_time = time ? Time.at(time) : Time.now
timestamp = record_time.to_datetime.rfc3339
hostname = @syslog_hostname || '-'
app_name = @syslog_app || tag || '-'
procid = '-'
msgid = '-'
pen = @syslog_pen
tag = @syslog_tag ? " tag=\"#{@syslog_tag},fluentd\"" : ''
token = @syslog_token ? " key=\"#{@syslog_token}\"" : ''
structured_data = "[#{pen} #{token} #{tag}]"
msg = Yajl.dump(record)
"<#{pri}>#{version} #{timestamp} #{hostname} #{app_name} #{procid} #{msgid} #{structured_data} #{msg}\n"
end
|
#create_socket(host, port) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/fluent/plugin/out_syslog.rb', line 58
def create_socket(host, port)
log.info "initializing tcp socket for #{host}:#{port}"
begin
socket = TCPSocket.new(host, port)
log.debug "enabling ssl for socket #{host}:#{port}"
ssl = OpenSSL::SSL::SSLSocket.new(socket)
ssl.sync_close = true
ssl.connect
rescue => e
log.warn "failed to create tcp socket #{host}:#{port}: #{e}"
ssl = nil
end
ssl
end
|
47
48
49
|
# File 'lib/fluent/plugin/out_syslog.rb', line 47
def format(tag, time, record)
[tag, time, record].to_msgpack
end
|
#send_to_syslog(packet) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/fluent/plugin/out_syslog.rb', line 125
def send_to_syslog(packet)
@socket ||= create_socket(@syslog_host, @syslog_port)
if @socket.nil?
err_msg = "Unable to create socket with #{@syslog_host}:#{@syslog_port}"
raise SocketFailureError, err_msg
else
begin
@socket.write packet
rescue => e
@socket = nil
err_msg = "Closing socket. #{e.class} writing to '#{@syslog_host}:#{@syslog_port}': #{e}"
raise SocketFailureError, err_msg, e.backtrace
end
end
end
|
#shutdown ⇒ Object
42
43
44
45
|
# File 'lib/fluent/plugin/out_syslog.rb', line 42
def shutdown
super
@socket.close
end
|
#start ⇒ Object
36
37
38
39
40
|
# File 'lib/fluent/plugin/out_syslog.rb', line 36
def start
super
@socket = create_socket(@syslog_host, @syslog_port)
end
|
#write(chunk) ⇒ Object
51
52
53
54
55
56
|
# File 'lib/fluent/plugin/out_syslog.rb', line 51
def write(chunk)
chunk.msgpack_each { |(tag, time, record)|
packet = create_packet(tag, time, record)
send_to_syslog(packet)
}
end
|