-
Notifications
You must be signed in to change notification settings - Fork 875
/
output.lua
271 lines (220 loc) · 6.93 KB
/
output.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
50
51
52
53
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
-- Copyright (C) 2019 The Falco Authors.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local mod = {}
local outputs = {}
function mod.stdout(event, rule, source, priority, priority_num, msg, format, hostname, options)
mod.stdout_message(priority, priority_num, msg, outputs)
end
function mod.stdout_message(priority, priority_num, msg, options)
if options.buffered == 0 then
io.stdout:setvbuf "no"
end
print(msg)
end
function mod.stdout_cleanup()
io.stdout:flush()
end
-- Note: not actually closing/reopening stdout
function mod.stdout_reopen(options)
end
function mod.file_validate(options)
if (not type(options.filename) == "string") then
error("File output needs to be configured with a valid filename")
end
local file, err = io.open(options.filename, "a+")
if file == nil then
error("Error with file output: " .. err)
end
file:close()
end
function mod.file_open(options)
if ffile == nil then
ffile = io.open(options.filename, "a+")
if options.buffered == 0 then
ffile:setvbuf "no"
end
end
end
function mod.file(event, rule, source, priority, priority_num, msg, format, hostname, options)
mod.file_message(priority, priority_num, msg, options)
end
function mod.file_message(priority, priority_num, msg, options)
if options.keep_alive == "true" then
mod.file_open(options)
else
ffile = io.open(options.filename, "a+")
end
ffile:write(msg, "\n")
if options.keep_alive == nil or options.keep_alive ~= "true" then
ffile:close()
ffile = nil
end
end
function mod.file_cleanup()
if ffile ~= nil then
ffile:flush()
ffile:close()
ffile = nil
end
end
function mod.file_reopen(options)
if options.keep_alive == "true" then
mod.file_cleanup()
mod.file_open(options)
end
end
function mod.syslog(event, rule, source, priority, priority_num, msg, format, hostname, options)
mod.syslog_message(priority, priority_num, msg, options)
end
function mod.syslog_message(priority, priority_num, msg, options)
falco.syslog(priority_num, msg)
end
function mod.syslog_cleanup()
end
function mod.syslog_reopen()
end
function mod.program_open(options)
if pfile == nil then
pfile = io.popen(options.program, "w")
if options.buffered == 0 then
pfile:setvbuf "no"
end
end
end
function mod.program(event, rule, source, priority, priority_num, msg, format, hostname, options)
mod.program_message(priority, priority_num, msg, options)
end
function mod.program_message(priority, priority_num, msg, options)
-- XXX Ideally we'd check that the program ran
-- successfully. However, the luajit we're using returns true even
-- when the shell can't run the program.
-- Note: options are all strings
if options.keep_alive == "true" then
mod.program_open(options)
else
pfile = io.popen(options.program, "w")
end
pfile:write(msg, "\n")
if options.keep_alive == nil or options.keep_alive ~= "true" then
pfile:close()
pfile = nil
end
end
function mod.program_cleanup()
if pfile ~= nil then
pfile:flush()
pfile:close()
pfile = nil
end
end
function mod.program_reopen(options)
if options.keep_alive == "true" then
mod.program_cleanup()
mod.program_open(options)
end
end
function mod.http(event, rule, source, priority, priority_num, msg, format, hostname, options)
mod.http_message(priority, priority_num, msg, options)
end
function mod.http_message(priority, priority_num, msg, options)
c_outputs.handle_http(options.url, msg)
end
function mod.http_cleanup()
end
function mod.http_reopen()
end
function mod.grpc(event, rule, source, priority, priority_num, msg, format, hostname, options)
fields = formats.resolve_tokens(event, source, format)
c_outputs.handle_grpc(event, rule, source, priority, msg, fields, hostname, options)
end
function mod.grpc_message(priority, priority_num, msg, options)
-- todo(fntlnz, leodido) > gRPC does not support subscribing to dropped events yet
end
function mod.grpc_cleanup()
end
function mod.grpc_reopen()
end
function output_event(event, rule, source, priority, priority_num, format, hostname)
-- If format starts with a *, remove it, as we're adding our own
-- prefix here.
if format:sub(1, 1) == "*" then
format = format:sub(2)
end
-- time_format_iso_8601 will be the same for all output channels
time_format_iso_8601 = 0
for index, o in ipairs(outputs) do
time_format_iso_8601 = o.options.time_format_iso_8601
break
end
if source == "syscall" then
if time_format_iso_8601 == 1 then
format = "*%evt.time.iso8601: " .. priority .. " " .. format
else
format = "*%evt.time: " .. priority .. " " .. format
end
else
if time_format_iso_8601 == 1 then
format = "*%jevt.time.iso8601: " .. priority .. " " .. format
else
format = "*%jevt.time: " .. priority .. " " .. format
end
end
msg = formats.format_event(event, rule, source, priority, format)
for index, o in ipairs(outputs) do
o.output(event, rule, source, priority, priority_num, msg, format, hostname, o.options)
end
end
function output_msg(msg, priority, priority_num)
for index, o in ipairs(outputs) do
o.message(priority, priority_num, msg, o.options)
end
end
function output_cleanup()
formats.free_formatters()
for index, o in ipairs(outputs) do
o.cleanup()
end
end
function output_reopen()
for index, o in ipairs(outputs) do
o.reopen(o.options)
end
end
function add_output(output_name, buffered, time_format_iso_8601, options)
if not (type(mod[output_name]) == "function") then
error("rule_loader.add_output(): invalid output_name: " .. output_name)
end
-- outputs can optionally define a validation function so that we don't
-- find out at runtime (when an event finally matches a rule!) that the options are invalid
if (type(mod[output_name .. "_validate"]) == "function") then
mod[output_name .. "_validate"](options)
end
if options == nil then
options = {}
end
options.buffered = buffered
options.time_format_iso_8601 = time_format_iso_8601
table.insert(
outputs,
{
output = mod[output_name],
cleanup = mod[output_name .. "_cleanup"],
reopen = mod[output_name .. "_reopen"],
message = mod[output_name .. "_message"],
options = options
}
)
end
return mod