From b2698f9d20b6c6bc67245ad8637b53c3b1ca670c Mon Sep 17 00:00:00 2001 From: Henri DF Date: Tue, 12 Apr 2016 16:59:53 -0700 Subject: [PATCH] Set up outputs listed in configuration object --- digwatch.yaml | 6 ++-- userspace/digwatch/digwatch.cpp | 43 ++++++++++++++++++-------- userspace/digwatch/lua/rule_loader.lua | 2 +- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/digwatch.yaml b/digwatch.yaml index 0c452e78..5a5e8df7 100644 --- a/digwatch.yaml +++ b/digwatch.yaml @@ -5,12 +5,12 @@ rules_file: /etc/digwatch.conf priority_level: warning syslog_output: - enabled: true + enabled: false file_output: - enabled: true + enabled: false filename: "bla.bla" stdout_output: - enabled: false + enabled: true diff --git a/userspace/digwatch/digwatch.cpp b/userspace/digwatch/digwatch.cpp index 8c74508a..86aa8de9 100644 --- a/userspace/digwatch/digwatch.cpp +++ b/userspace/digwatch/digwatch.cpp @@ -156,24 +156,38 @@ void add_lua_path(lua_State *ls, string path) lua_pop(ls, 1); } -void add_output(lua_State *ls, string output_name) +void add_output(lua_State *ls, output_config oc) { + + uint8_t nargs = 1; 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 + if(!lua_isfunction(ls, -1)) { throw sinsp_exception("No function " + lua_add_output + " found. "); } + lua_pushstring(ls, oc.name.c_str()); + + // If we have options, build up a lua table containing them + if (oc.options.size()) + { + nargs = 2; + lua_createtable(ls, 0, oc.options.size()); + + for (auto it = oc.options.cbegin(); it != oc.options.cend(); ++it) + { + lua_pushstring(ls, (*it).second.c_str()); + lua_setfield(ls, -2, (*it).first.c_str()); + } + } + + if(lua_pcall(ls, nargs, 0, 0) != 0) + { + const char* lerr = lua_tostring(ls, -1); + string err = "Error invoking add_output: " + string(lerr); + throw sinsp_exception(err); + } + } @@ -363,7 +377,10 @@ int digwatch_init(int argc, char **argv) inspector->set_hostname_and_port_resolution_mode(false); - add_output(ls, output_name); + for(std::vector::iterator it = config.m_outputs.begin(); it != config.m_outputs.end(); ++it) + { + add_output(ls, *it); + } if (infile.size()) { diff --git a/userspace/digwatch/lua/rule_loader.lua b/userspace/digwatch/lua/rule_loader.lua index 9a465fe1..5eb87cce 100644 --- a/userspace/digwatch/lua/rule_loader.lua +++ b/userspace/digwatch/lua/rule_loader.lua @@ -165,7 +165,7 @@ 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) + error("rule_loader.add_output(): invalid output_name: "..output_name) end table.insert(outputs, {output = output_functions[output_name], config=config})