mirror of
https://github.com/falcosecurity/falco.git
synced 2025-07-02 17:42:18 +00:00
Cache formatters.
Instead of creating a formatter for each event, cache them and create them only when needed. A new function output_cleanup cleans up the cached formatters, and is called in the destructor if init() was called.
This commit is contained in:
parent
2a2dcaf25d
commit
db67034338
@ -27,13 +27,28 @@ along with falco. If not, see <http://www.gnu.org/licenses/>.
|
||||
using namespace std;
|
||||
|
||||
falco_outputs::falco_outputs()
|
||||
: m_initialized(false)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
falco_outputs::~falco_outputs()
|
||||
{
|
||||
if(m_initialized)
|
||||
{
|
||||
lua_getglobal(m_ls, m_lua_output_cleanup.c_str());
|
||||
|
||||
if(!lua_isfunction(m_ls, -1))
|
||||
{
|
||||
throw falco_exception("No function " + m_lua_output_cleanup + " found. ");
|
||||
}
|
||||
|
||||
if(lua_pcall(m_ls, 0, 0, 0) != 0)
|
||||
{
|
||||
const char* lerr = lua_tostring(m_ls, -1);
|
||||
throw falco_exception(string(lerr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void falco_outputs::init(bool json_output)
|
||||
@ -52,6 +67,8 @@ void falco_outputs::init(bool json_output)
|
||||
falco_formats::init(m_inspector, m_ls, json_output);
|
||||
|
||||
falco_logger::init(m_ls);
|
||||
|
||||
m_initialized = true;
|
||||
}
|
||||
|
||||
void falco_outputs::add_output(output_config oc)
|
||||
|
@ -51,7 +51,10 @@ public:
|
||||
void handle_event(sinsp_evt *ev, std::string &level, std::string &priority, std::string &format);
|
||||
|
||||
private:
|
||||
bool m_initialized;
|
||||
|
||||
std::string m_lua_add_output = "add_output";
|
||||
std::string m_lua_output_event = "output_event";
|
||||
std::string m_lua_output_cleanup = "output_cleanup";
|
||||
std::string m_lua_main_filename = "output.lua";
|
||||
};
|
||||
|
@ -24,6 +24,8 @@ mod.levels = levels
|
||||
|
||||
local outputs = {}
|
||||
|
||||
local formatters = {}
|
||||
|
||||
function mod.stdout(level, msg)
|
||||
print (msg)
|
||||
end
|
||||
@ -75,14 +77,26 @@ end
|
||||
function output_event(event, rule, priority, format)
|
||||
local level = level_of(priority)
|
||||
format = "*%evt.time: "..levels[level+1].." "..format
|
||||
formatter = formats.formatter(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)
|
||||
|
||||
for index,o in ipairs(outputs) do
|
||||
o.output(level, msg, o.config)
|
||||
end
|
||||
end
|
||||
|
||||
formats.free_formatter(formatter)
|
||||
function output_cleanup()
|
||||
for rule, formatter in pairs(formatters) do
|
||||
formats.free_formatter(formatter)
|
||||
end
|
||||
|
||||
formatters = {}
|
||||
end
|
||||
|
||||
function add_output(output_name, config)
|
||||
|
Loading…
Reference in New Issue
Block a user