diff --git a/userspace/digwatch/lua/rule_loader.lua b/userspace/digwatch/lua/rule_loader.lua index 284c1be0..f1132d87 100644 --- a/userspace/digwatch/lua/rule_loader.lua +++ b/userspace/digwatch/lua/rule_loader.lua @@ -56,7 +56,7 @@ local state to the line-oriented compiler. --]] local function init() - return {macros={}, filter_ast=nil} + return {macros={}, filter_ast=nil, n_rules=0} end @@ -66,12 +66,17 @@ function load_rule(r) end local line_ast = compiler.compile_line(r, state.macros) - if (line_ast.type == "MacroDef") then + if (line_ast.type == nil) then -- blank line + return + elseif (line_ast.type == "MacroDef") then return elseif (not (line_ast.type == "Rule")) then error ("Unexpected type in load_rule: "..line_ast.type) end + digwatch.set_formatter(state.n_rules, line_ast.output.value) + state.n_rules = state.n_rules + 1 + if (state.filter_ast == nil) then state.filter_ast = line_ast.filter.value else diff --git a/userspace/digwatch/rules.cpp b/userspace/digwatch/rules.cpp index d8c3ecac..39f883ce 100644 --- a/userspace/digwatch/rules.cpp +++ b/userspace/digwatch/rules.cpp @@ -6,18 +6,57 @@ extern "C" { #include "lauxlib.h" } +std::map g_format_map; +sinsp* g_inspector; + +const static struct luaL_reg ll_digwatch [] = +{ + {"set_formatter", &digwatch_rules::set_formatter}, + {NULL,NULL} +}; + digwatch_rules::digwatch_rules(sinsp* inspector, string lua_main_filename, string lua_dir) { + g_inspector = inspector; + // Initialize Lua interpreter m_ls = lua_open(); luaL_openlibs(m_ls); m_lua_parser = new lua_parser(inspector, m_ls); + luaL_openlib(m_ls, "digwatch", ll_digwatch, 0); + add_lua_path(lua_dir); load_compiler(lua_main_filename); } +int digwatch_rules::set_formatter (lua_State *ls) { + uint32_t index = luaL_checkinteger(ls, 1); + string format = luaL_checkstring(ls, 2); + + try + { + if(format == "" || format == "default") + { + g_format_map[index] = new sinsp_evt_formatter(g_inspector, DEFAULT_OUTPUT_STR); + } + else + { + g_format_map[index] = new sinsp_evt_formatter(g_inspector, format); + } + } + catch(sinsp_exception& e) + { + string err = "invalid output format " + format; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("set_formatter error"); + } + + return 0; +} + + void digwatch_rules::add_lua_path(string path) { path += "?.lua"; @@ -84,7 +123,7 @@ void digwatch_rules::load_rules(string rules_filename) if(lua_pcall(m_ls, 1, 0, 0) != 0) { const char* lerr = lua_tostring(m_ls, -1); - string err = "Error loading rule: " + string(lerr); + string err = "Error loading rule '" + line + "':" + string(lerr); throw sinsp_exception(err); } } diff --git a/userspace/digwatch/rules.h b/userspace/digwatch/rules.h index c46ea97f..7f007152 100644 --- a/userspace/digwatch/rules.h +++ b/userspace/digwatch/rules.h @@ -3,6 +3,8 @@ #include "sinsp.h" #include "lua_parser.h" +class sinsp_evt_formatter; + class digwatch_rules { public: @@ -11,12 +13,16 @@ class digwatch_rules void load_rules(string rules_filename); sinsp_filter* get_filter(); + // set_formatter(index, format_string) + static int set_formatter(lua_State *ls); private: void add_lua_path(string path); void load_compiler(string lua_main_filename); lua_parser* m_lua_parser; lua_State* m_ls; + string m_lua_load_rule = "load_rule"; string m_lua_on_done = "on_done"; + string m_lua_on_event = "on_event"; };