Set up outputs listed in configuration object

This commit is contained in:
Henri DF 2016-04-12 16:59:53 -07:00
parent 179e5519ce
commit b2698f9d20
3 changed files with 34 additions and 17 deletions

View File

@ -5,12 +5,12 @@ rules_file: /etc/digwatch.conf
priority_level: warning priority_level: warning
syslog_output: syslog_output:
enabled: true enabled: false
file_output: file_output:
enabled: true enabled: false
filename: "bla.bla" filename: "bla.bla"
stdout_output: stdout_output:
enabled: false enabled: true

View File

@ -156,24 +156,38 @@ void add_lua_path(lua_State *ls, string path)
lua_pop(ls, 1); 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()); lua_getglobal(ls, lua_add_output.c_str());
if(lua_isfunction(ls, -1)) 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. "); 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); inspector->set_hostname_and_port_resolution_mode(false);
add_output(ls, output_name); for(std::vector<output_config>::iterator it = config.m_outputs.begin(); it != config.m_outputs.end(); ++it)
{
add_output(ls, *it);
}
if (infile.size()) if (infile.size())
{ {

View File

@ -165,7 +165,7 @@ outputs = {}
function add_output(output_name, config) function add_output(output_name, config)
if not (type(output_functions[output_name]) == 'function') then 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 end
table.insert(outputs, {output = output_functions[output_name], config=config}) table.insert(outputs, {output = output_functions[output_name], config=config})