From 179e5519ceed13712b61aaa38d2a99fc19f9e099 Mon Sep 17 00:00:00 2001 From: Henri DF Date: Mon, 11 Apr 2016 15:16:01 -0700 Subject: [PATCH] Small refactoring of output config This is a step towards being able to support multiple outputs of different types (including file outputs which require their own config). --- userspace/digwatch/digwatch.cpp | 27 ++++++++++++++++++++++---- userspace/digwatch/lua/rule_loader.lua | 20 ++++++++++++++----- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/userspace/digwatch/digwatch.cpp b/userspace/digwatch/digwatch.cpp index 43de8178..8c74508a 100644 --- a/userspace/digwatch/digwatch.cpp +++ b/userspace/digwatch/digwatch.cpp @@ -54,13 +54,13 @@ static void usage() } string lua_on_event = "on_event"; +string lua_add_output = "add_output"; // // Event processing loop // void do_inspect(sinsp* inspector, digwatch_rules* rules, - string output_name, lua_State* ls) { int32_t res; @@ -110,9 +110,8 @@ void do_inspect(sinsp* inspector, { lua_pushlightuserdata(ls, ev); lua_pushnumber(ls, ev->get_check_id()); - lua_pushstring(ls, output_name.c_str()); - if(lua_pcall(ls, 3, 0, 0) != 0) + if(lua_pcall(ls, 2, 0, 0) != 0) { const char* lerr = lua_tostring(ls, -1); string err = "Error invoking function output: " + string(lerr); @@ -157,6 +156,25 @@ void add_lua_path(lua_State *ls, string path) lua_pop(ls, 1); } +void add_output(lua_State *ls, string output_name) +{ + lua_getglobal(ls, lua_add_output.c_str()); + + if(lua_isfunction(ls, -1)) + { + lua_pushstring(ls, output_name.c_str()); + if(lua_pcall(ls, 1, 0, 0) != 0) + { + const char* lerr = lua_tostring(ls, -1); + string err = "Error invoking add_output: " + string(lerr); + throw sinsp_exception(err); + } + } + else + { + throw sinsp_exception("No function " + lua_add_output + " found. "); + } +} // @@ -345,6 +363,8 @@ int digwatch_init(int argc, char **argv) inspector->set_hostname_and_port_resolution_mode(false); + add_output(ls, output_name); + if (infile.size()) { inspector->open(infile); @@ -366,7 +386,6 @@ int digwatch_init(int argc, char **argv) } do_inspect(inspector, rules, - output_name, ls); inspector->close(); diff --git a/userspace/digwatch/lua/rule_loader.lua b/userspace/digwatch/lua/rule_loader.lua index 773328e2..9a465fe1 100644 --- a/userspace/digwatch/lua/rule_loader.lua +++ b/userspace/digwatch/lua/rule_loader.lua @@ -159,17 +159,27 @@ function on_done() io.flush() end -local outputs = require('output') +local output_functions = require('output') -function on_event(evt_, rule_id, output_name) - if not (type(outputs[output_name]) == 'function') then - error("rule_loader.on_event(): invalid output_name: ", output_name) +outputs = {} + +function add_output(output_name, config) + if not (type(output_functions[output_name]) == 'function') then + error("rule_loader.add_output(): invalid output_name: ", output_name) end + table.insert(outputs, {output = output_functions[output_name], config=config}) +end + +function on_event(evt_, rule_id) + if state.outputs[rule_id] == nil then error ("rule_loader.on_event(): event with invalid rule_id: ", rule_id) end - outputs[output_name](evt_, state.outputs[rule_id].level, state.outputs[rule_id].format) + for index,o in ipairs(outputs) do + o.output(evt_, state.outputs[rule_id].level, state.outputs[rule_id].format, o.config) + end + end