mirror of
https://github.com/falcosecurity/falco.git
synced 2025-07-13 06:24:29 +00:00
Merge pull request #212 from draios/use-formatter-cache
Use formatter cache
This commit is contained in:
commit
561c388dab
@ -24,12 +24,14 @@ along with falco. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
|
|
||||||
|
|
||||||
sinsp* falco_formats::s_inspector = NULL;
|
sinsp* falco_formats::s_inspector = NULL;
|
||||||
bool s_json_output = false;
|
bool falco_formats::s_json_output = false;
|
||||||
|
sinsp_evt_formatter_cache *falco_formats::s_formatters = NULL;
|
||||||
|
|
||||||
const static struct luaL_reg ll_falco [] =
|
const static struct luaL_reg ll_falco [] =
|
||||||
{
|
{
|
||||||
{"formatter", &falco_formats::formatter},
|
{"formatter", &falco_formats::formatter},
|
||||||
{"free_formatter", &falco_formats::free_formatter},
|
{"free_formatter", &falco_formats::free_formatter},
|
||||||
|
{"free_formatters", &falco_formats::free_formatters},
|
||||||
{"format_event", &falco_formats::format_event},
|
{"format_event", &falco_formats::format_event},
|
||||||
{NULL,NULL}
|
{NULL,NULL}
|
||||||
};
|
};
|
||||||
@ -38,6 +40,10 @@ void falco_formats::init(sinsp* inspector, lua_State *ls, bool json_output)
|
|||||||
{
|
{
|
||||||
s_inspector = inspector;
|
s_inspector = inspector;
|
||||||
s_json_output = json_output;
|
s_json_output = json_output;
|
||||||
|
if(!s_formatters)
|
||||||
|
{
|
||||||
|
s_formatters = new sinsp_evt_formatter_cache(s_inspector);
|
||||||
|
}
|
||||||
|
|
||||||
luaL_openlib(ls, "formats", ll_falco, 0);
|
luaL_openlib(ls, "formats", ll_falco, 0);
|
||||||
}
|
}
|
||||||
@ -73,22 +79,43 @@ int falco_formats::free_formatter(lua_State *ls)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int falco_formats::free_formatters(lua_State *ls)
|
||||||
|
{
|
||||||
|
if(s_formatters)
|
||||||
|
{
|
||||||
|
delete(s_formatters);
|
||||||
|
s_formatters = NULL;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int falco_formats::format_event (lua_State *ls)
|
int falco_formats::format_event (lua_State *ls)
|
||||||
{
|
{
|
||||||
string line;
|
string line;
|
||||||
|
|
||||||
if (!lua_islightuserdata(ls, -1) ||
|
if (!lua_isstring(ls, -1) ||
|
||||||
!lua_isstring(ls, -2) ||
|
!lua_isstring(ls, -2) ||
|
||||||
!lua_isstring(ls, -3) ||
|
!lua_isstring(ls, -3) ||
|
||||||
!lua_islightuserdata(ls, -4)) {
|
!lua_islightuserdata(ls, -4)) {
|
||||||
throw falco_exception("Invalid arguments passed to format_event()\n");
|
lua_pushstring(ls, "Invalid arguments passed to format_event()");
|
||||||
|
lua_error(ls);
|
||||||
}
|
}
|
||||||
sinsp_evt* evt = (sinsp_evt*)lua_topointer(ls, 1);
|
sinsp_evt* evt = (sinsp_evt*)lua_topointer(ls, 1);
|
||||||
const char *rule = (char *) lua_tostring(ls, 2);
|
const char *rule = (char *) lua_tostring(ls, 2);
|
||||||
const char *level = (char *) lua_tostring(ls, 3);
|
const char *level = (char *) lua_tostring(ls, 3);
|
||||||
sinsp_evt_formatter* formatter = (sinsp_evt_formatter*)lua_topointer(ls, 4);
|
const char *format = (char *) lua_tostring(ls, 4);
|
||||||
|
|
||||||
formatter->tostring(evt, &line);
|
string sformat = format;
|
||||||
|
|
||||||
|
try {
|
||||||
|
s_formatters->tostring(evt, sformat, &line);
|
||||||
|
}
|
||||||
|
catch (sinsp_exception& e)
|
||||||
|
{
|
||||||
|
string err = "Invalid output format '" + sformat + "': '" + string(e.what()) + "'";
|
||||||
|
lua_pushstring(ls, err.c_str());
|
||||||
|
lua_error(ls);
|
||||||
|
}
|
||||||
|
|
||||||
// For JSON output, the formatter returned just the output
|
// For JSON output, the formatter returned just the output
|
||||||
// string containing the format text and values. Use this to
|
// string containing the format text and values. Use this to
|
||||||
|
@ -39,8 +39,13 @@ class falco_formats
|
|||||||
// falco.free_formatter(formatter)
|
// falco.free_formatter(formatter)
|
||||||
static int free_formatter(lua_State *ls);
|
static int free_formatter(lua_State *ls);
|
||||||
|
|
||||||
|
// falco.free_formatters()
|
||||||
|
static int free_formatters(lua_State *ls);
|
||||||
|
|
||||||
// formatted_string = falco.format_event(evt, formatter)
|
// formatted_string = falco.format_event(evt, formatter)
|
||||||
static int format_event(lua_State *ls);
|
static int format_event(lua_State *ls);
|
||||||
|
|
||||||
static sinsp* s_inspector;
|
static sinsp* s_inspector;
|
||||||
|
static sinsp_evt_formatter_cache *s_formatters;
|
||||||
|
static bool s_json_output;
|
||||||
};
|
};
|
||||||
|
@ -49,7 +49,8 @@ int falco_rules::clear_filters(lua_State *ls)
|
|||||||
{
|
{
|
||||||
if (! lua_islightuserdata(ls, -1))
|
if (! lua_islightuserdata(ls, -1))
|
||||||
{
|
{
|
||||||
throw falco_exception("Invalid arguments passed to clear_filters()\n");
|
lua_pushstring(ls, "Invalid arguments passed to clear_filters()");
|
||||||
|
lua_error(ls);
|
||||||
}
|
}
|
||||||
|
|
||||||
falco_rules *rules = (falco_rules *) lua_topointer(ls, -1);
|
falco_rules *rules = (falco_rules *) lua_topointer(ls, -1);
|
||||||
@ -70,7 +71,8 @@ int falco_rules::add_filter(lua_State *ls)
|
|||||||
! lua_istable(ls, -2) ||
|
! lua_istable(ls, -2) ||
|
||||||
! lua_istable(ls, -1))
|
! lua_istable(ls, -1))
|
||||||
{
|
{
|
||||||
throw falco_exception("Invalid arguments passed to add_filter()\n");
|
lua_pushstring(ls, "Invalid arguments passed to add_filter()");
|
||||||
|
lua_error(ls);
|
||||||
}
|
}
|
||||||
|
|
||||||
falco_rules *rules = (falco_rules *) lua_topointer(ls, -4);
|
falco_rules *rules = (falco_rules *) lua_topointer(ls, -4);
|
||||||
@ -122,7 +124,8 @@ int falco_rules::enable_rule(lua_State *ls)
|
|||||||
! lua_isstring(ls, -2) ||
|
! lua_isstring(ls, -2) ||
|
||||||
! lua_isnumber(ls, -1))
|
! lua_isnumber(ls, -1))
|
||||||
{
|
{
|
||||||
throw falco_exception("Invalid arguments passed to enable_rule()\n");
|
lua_pushstring(ls, "Invalid arguments passed to enable_rule()");
|
||||||
|
lua_error(ls);
|
||||||
}
|
}
|
||||||
|
|
||||||
falco_rules *rules = (falco_rules *) lua_topointer(ls, -3);
|
falco_rules *rules = (falco_rules *) lua_topointer(ls, -3);
|
||||||
|
@ -24,8 +24,6 @@ mod.levels = levels
|
|||||||
|
|
||||||
local outputs = {}
|
local outputs = {}
|
||||||
|
|
||||||
local formatters = {}
|
|
||||||
|
|
||||||
function mod.stdout(level, msg)
|
function mod.stdout(level, msg)
|
||||||
print (msg)
|
print (msg)
|
||||||
end
|
end
|
||||||
@ -84,14 +82,8 @@ function output_event(event, rule, priority, format)
|
|||||||
end
|
end
|
||||||
|
|
||||||
format = "*%evt.time: "..levels[level+1].." "..format
|
format = "*%evt.time: "..levels[level+1].." "..format
|
||||||
if formatters[rule] == nil then
|
|
||||||
formatter = formats.formatter(format)
|
|
||||||
formatters[rule] = formatter
|
|
||||||
else
|
|
||||||
formatter = formatters[rule]
|
|
||||||
end
|
|
||||||
|
|
||||||
msg = formats.format_event(event, rule, levels[level+1], formatter)
|
msg = formats.format_event(event, rule, levels[level+1], format)
|
||||||
|
|
||||||
for index,o in ipairs(outputs) do
|
for index,o in ipairs(outputs) do
|
||||||
o.output(level, msg, o.config)
|
o.output(level, msg, o.config)
|
||||||
@ -99,11 +91,7 @@ function output_event(event, rule, priority, format)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function output_cleanup()
|
function output_cleanup()
|
||||||
for rule, formatter in pairs(formatters) do
|
formats.free_formatters()
|
||||||
formats.free_formatter(formatter)
|
|
||||||
end
|
|
||||||
|
|
||||||
formatters = {}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function add_output(output_name, config)
|
function add_output(output_name, config)
|
||||||
|
Loading…
Reference in New Issue
Block a user