Changeset a8c6e93 in nscp


Ignore:
Timestamp:
02/02/12 21:23:24 (16 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
e396b2f
Parents:
a06e6af
Message:

Now "everything" (ish) works including channels, exec and query (via moderna API)
Still no protocol buffer support but not sure how to play that yet so will not be avalible in 0.4.0.

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • changelog

    ra06e6af ra8c6e93  
    55 * Fixa dependonservice LanManWorkStation (old win) 
    66 * Fix RtlStringFromGUID problem on NT4 
     7 
     82012-02-02 MickeM 
     9 * Implemented full API for LuaScript 
     10  Now "everything" (ish) works including channels, exec and query (via moderna API) 
     11  Still no protocol buffer support but not sure how to play that yet so will not be avalible in 0.4.0. 
    712 
    8132012-02-01 MickeM 
  • modules/LUAScript/LUAScript.cpp

    ra06e6af ra8c6e93  
    177177} 
    178178 
     179NSCAPI::nagiosReturn LUAScript::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 
     180  if (!registry->has_exec(command)) 
     181    return NSCAPI::returnIgnored; 
     182  return registry->on_exec(command, arguments, result); 
     183} 
     184 
     185NSCAPI::nagiosReturn LUAScript::handleSimpleNotification(const std::wstring channel, const std::wstring source, const std::wstring command, NSCAPI::nagiosReturn code, std::wstring msg, std::wstring perf) { 
     186  if (!registry->has_submit(channel)) 
     187    return NSCAPI::returnIgnored; 
     188  return registry->on_submission(channel, source, command, code, msg, perf); 
     189} 
     190 
     191 
     192 
    179193 
    180194NSC_WRAP_DLL(); 
     
    182196NSC_WRAPPERS_IGNORE_MSG_DEF(); 
    183197NSC_WRAPPERS_HANDLE_CMD_DEF(); 
     198NSC_WRAPPERS_CLI_DEF(); 
     199NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF(); 
  • modules/LUAScript/LUAScript.def

    rc5ec0c8 ra8c6e93  
    1313  NSUnloadModule 
    1414  NSGetModuleDescription 
     15  NSCommandLineExec 
    1516  NSDeleteBuffer 
    16  
     17  NSHasNotificationHandler 
     18  NSHandleNotification 
  • modules/LUAScript/LUAScript.h

    ra06e6af ra8c6e93  
    2020***************************************************************************/ 
    2121NSC_WRAPPERS_MAIN(); 
     22NSC_WRAPPERS_CLI(); 
     23NSC_WRAPPERS_CHANNELS(); 
     24 
    2225//#include <config.h> 
    2326#include <strEx.h> 
     
    3134 
    3235 
    33 class LUAScript : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 
     36class LUAScript : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_command_line_exec, public nscapi::impl::simple_submission_handler, public nscapi::impl::simple_plugin { 
    3437private: 
    3538 
     
    6467  bool hasCommandHandler(); 
    6568  bool hasMessageHandler(); 
     69  bool hasNotificationHandler() { return true; } 
    6670  boost::optional<boost::filesystem::wpath> find_file(std::wstring file); 
    6771  bool loadScript(std::wstring alias, std::wstring file); 
    6872  NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 
     73  NSCAPI::nagiosReturn commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 
     74  NSCAPI::nagiosReturn handleSimpleNotification(const std::wstring channel, const std::wstring source, const std::wstring command, NSCAPI::nagiosReturn code, std::wstring msg, std::wstring perf); 
     75 
     76 
    6977  //NSCAPI::nagiosReturn RunLUA(const unsigned int argLen, wchar_t **char_args, std::wstring &message, std::wstring &perf); 
    7078  //NSCAPI::nagiosReturn extract_return(Lua_State &L, int arg_count,  std::wstring &message, std::wstring &perf); 
  • modules/LUAScript/lua_wrappers.hpp

    ra06e6af ra8c6e93  
    323323    } 
    324324 
     325    std::list<std::wstring> inline checkarray(int pos) { 
     326      luaL_checktype(L, pos, LUA_TTABLE); 
     327      return get_array(pos); 
     328    } 
     329 
    325330    boolean inline checkbool(int pos) { 
    326331      return lua_toboolean(L, pos); 
     
    404409    function_map functions; 
    405410    function_map channels; 
    406   public: 
    407     NSCAPI::nagiosReturn on_query(const std::wstring & target, const std::wstring & command, std::list<std::wstring> & arguments, std::wstring & message, std::wstring & perf)  
    408     { 
    409       NSC_DEBUG_MSG_STD(_T("Running command: ") + command); 
     411    function_map execs; 
     412 
     413    inline lua_State * prep_function(const function_container &c) { 
     414      lua_State *L = c.instance->get_lua_state(); 
     415      lua_rawgeti(L, LUA_REGISTRYINDEX, c.func_ref); 
     416      return L; 
     417    } 
     418  public: 
     419 
     420    NSCAPI::nagiosReturn on_query(const std::wstring & target, const std::wstring & command, std::list<std::wstring> & arguments, std::wstring & message, std::wstring & perf) { 
    410421      function_map::iterator it = functions.find(command); 
    411422      if (it == functions.end()) 
    412423        throw LUAException(_T("Invalid function: ") + command); 
    413424      function_container c = it->second; 
    414  
    415       lua_State *L = c.instance->get_lua_state(); 
    416       lua_rawgeti(L, LUA_REGISTRYINDEX, c.func_ref); 
    417       //lua_pushnumber(L, 1); 
    418       lua_pushstring(L, utf8::cvt<std::string>(command).c_str());  
    419  
    420       lua_createtable(L, 0, arguments.size()); 
    421       int i=0; 
    422       BOOST_FOREACH(std::wstring arg, arguments) { 
    423         lua_pushnumber(L,i++); 
    424         lua_pushstring(L,strEx::wstring_to_string(arg).c_str()); 
    425         lua_settable(L,-3); 
    426       } 
    427  
    428       if (lua_pcall(L, 2, LUA_MULTRET, 0) != 0) { 
    429         std::wstring err = utf8::cvt<std::wstring>(lua_tostring(L, -1)); 
    430         NSC_LOG_ERROR_STD(_T("Failed to handle command: ") + command + _T(": ") + err); 
    431         lua_pop(L, 1); 
     425      lua_wrapper lua(prep_function(c)); 
     426      lua.push_string(command); 
     427      lua.push_array(arguments); 
     428      if (lua.pcall(2, LUA_MULTRET, 0) != 0) { 
     429        NSC_LOG_ERROR_STD(_T("Failed to handle command: ") + command + _T(": ") + lua.pop_string()); 
    432430        return NSCAPI::returnUNKNOWN; 
    433431      } 
    434       return extract_return(L, lua_gettop(L), message, perf); 
    435     } 
    436  
    437     NSCAPI::nagiosReturn extract_return(lua_State *L, int arg_count, std::wstring &message, std::wstring &perf) { 
    438       lua_wrapper lua(L); 
    439       // code, message, performance data 
    440       if (arg_count > 3) { 
    441         NSC_LOG_ERROR_STD(_T("Too many arguments return from script (only using last 3)")); 
    442         lua_pop(L, arg_count-3); 
    443       } 
     432      int arg_count = lua.size(); 
    444433      if (arg_count > 2) 
    445434        perf = lua.pop_string(); 
     
    452441    } 
    453442 
    454     bool has_command(const std::wstring & command) { 
    455       return functions.find(command) != functions.end(); 
    456     } 
     443    NSCAPI::nagiosReturn on_exec(const std::wstring & command, std::list<std::wstring> & arguments, std::wstring & result) { 
     444      function_map::iterator it = execs.find(command); 
     445      if (it == execs.end()) 
     446        throw LUAException(_T("Invalid function: ") + command); 
     447      function_container c = it->second; 
     448      lua_wrapper lua(prep_function(c)); 
     449      lua.push_string(command); 
     450      lua.push_array(arguments); 
     451      if (lua.pcall(2, LUA_MULTRET, 0) != 0) { 
     452        NSC_LOG_ERROR_STD(_T("Failed to handle command: ") + command + _T(": ") + lua.pop_string()); 
     453        return NSCAPI::returnUNKNOWN; 
     454      } 
     455      int arg_count = lua.size(); 
     456      if (arg_count > 1) 
     457        result = lua.pop_string(); 
     458      if (arg_count > 0) 
     459        return lua.pop_code(); 
     460      NSC_LOG_ERROR_STD(_T("No arguments returned from script.")); 
     461      return NSCAPI::returnUNKNOWN; 
     462    } 
     463    NSCAPI::nagiosReturn on_submission(const std::wstring channel, const std::wstring source, const std::wstring command, NSCAPI::nagiosReturn code, std::wstring msg, std::wstring perf) { 
     464      function_map::iterator it = channels.find(channel); 
     465      if (it == channels.end()) 
     466        throw LUAException(_T("Invalid function: ") + channel); 
     467      function_container c = it->second; 
     468      lua_wrapper lua(prep_function(c)); 
     469      lua.push_string(source); 
     470      lua.push_string(command); 
     471      lua.push_code(code); 
     472      lua.push_string(msg); 
     473      lua.push_string(perf); 
     474      if (lua.pcall(5, LUA_MULTRET, 0) != 0) { 
     475        NSC_LOG_ERROR_STD(_T("Failed to handle command: ") + command + _T(": ") + lua.pop_string()); 
     476        return NSCAPI::returnUNKNOWN; 
     477      } 
     478      if (lua.size() > 0) 
     479        return lua.pop_code(); 
     480      NSC_LOG_ERROR_STD(_T("No arguments returned from script.")); 
     481      return NSCAPI::returnUNKNOWN; 
     482    } 
     483 
    457484    void register_query(const std::wstring &command, boost::shared_ptr<lua_script_instance> instance, int func_ref) { 
    458485      function_container c; 
     
    461488      functions[command] = c; 
    462489    } 
    463  
    464     void clear() { 
    465       functions.clear(); 
    466       // DO we need to release reference here? 
    467     } 
    468  
    469490    void register_subscription(const std::wstring &channel, boost::shared_ptr<lua_script_instance> instance, int func_ref) { 
    470491      function_container c; 
     
    474495    } 
    475496 
    476     void register_cmdline(const std::wstring &command, boost::shared_ptr<lua_script_instance> instance, int func_ref) { 
     497    void register_exec(const std::wstring &command, boost::shared_ptr<lua_script_instance> instance, int func_ref) { 
    477498      function_container c; 
    478499      c.func_ref = func_ref; 
    479500      c.instance = instance; 
    480       channels[command] = c; 
    481     } 
    482  
    483  
    484  
    485  
     501      execs[command] = c; 
     502    } 
     503 
     504    void clear() { 
     505      functions.clear(); 
     506      execs.clear(); 
     507      channels.clear(); 
     508      // DO we need to release reference here? 
     509    } 
     510 
     511    bool has_command(const std::wstring & command) { 
     512      return functions.find(command) != functions.end(); 
     513    } 
     514    bool has_exec(const std::wstring & command) { 
     515      return execs.find(command) != execs.end(); 
     516    } 
     517    bool has_submit(const std::wstring &command) { 
     518      return channels.find(command) != channels.end(); 
     519    } 
    486520  }; 
    487521 
  • modules/LUAScript/script_wrapper.hpp

    ra06e6af ra8c6e93  
    7474    } 
    7575    int simple_exec(lua_State *L) { 
    76       NSC_DEBUG_MSG(_T("s_q")); 
    77       return 0; 
     76      lua_wrappers::lua_wrapper lua(L); 
     77      try { 
     78        int nargs = lua.size(); 
     79        std::wstring target = lua.wstring(1); 
     80        std::wstring command = lua.wstring(2); 
     81        std::list<std::wstring> arguments = lua.checkarray(3); 
     82        std::list<std::wstring> result; 
     83        NSCAPI::nagiosReturn ret = get_instance()->get_core()->exec_simple_command(target, command, arguments, result); 
     84        lua.push_code(ret); 
     85        lua.push_array(result); 
     86        return 2; 
     87      } catch (...) { 
     88        return lua.error("Unknown exception in: simple_query"); 
     89      } 
    7890    } 
    7991    int exec(lua_State *L) { 
     
    8395    } 
    8496    int simple_submit(lua_State *L) { 
    85       NSC_DEBUG_MSG(_T("s_q")); 
    86       return 0; 
     97      lua_wrappers::lua_wrapper lua(L); 
     98      try { 
     99        int nargs = lua.size(); 
     100        std::wstring channel = lua.wstring(1); 
     101        std::wstring command = lua.wstring(2); 
     102        NSCAPI::nagiosReturn code = lua.get_code(3); 
     103        std::wstring message = lua.wstring(4); 
     104        std::wstring perf = lua.wstring(5); 
     105        std::wstring result; 
     106        NSCAPI::nagiosReturn ret = get_instance()->get_core()->submit_simple_message(channel, command, code, message, perf, result); 
     107        lua.push_code(ret); 
     108        lua.push_string(result); 
     109        return 2; 
     110      } catch (...) { 
     111        return lua.error("Unknown exception in: simple_query"); 
     112      } 
    87113    } 
    88114    int submit(lua_State *L) { 
     
    174200        return lua.error("Invalid function: " + utf8::cvt<std::string>(name)); 
    175201      std::wstring script = lua.pop_string(); 
    176       get_instance()->get_registry()->register_cmdline(script, get_instance(), func_ref); 
     202      get_instance()->get_registry()->register_exec(script, get_instance(), func_ref); 
    177203      return 0; 
    178204    } 
     
    311337      std::wstring path = lua.wstring(1); 
    312338      std::wstring key = lua.wstring(1); 
    313       std::wstring stype = lua.wstring(1); 
     339      std::string stype = lua.string(1); 
    314340      NSCAPI::settings_type type = get_type(stype); 
    315341      std::wstring title = lua.wstring(1); 
     
    341367 
    342368    static int execute (lua_State *L) { 
    343       try { 
    344         lua_wrapper lua(L); 
    345         int nargs = lua.size(); 
    346         if (nargs == 0) 
    347           return lua.error("nscp.execute requires at least 1 argument!"); 
    348         unsigned int argLen = nargs-1; 
    349         std::list<std::wstring> arguments; 
    350         for (unsigned int i=argLen; i>0; i--) 
    351           arguments.push_front(lua.pop_string()); 
    352         std::wstring command = lua.pop_string(); 
    353         std::wstring message; 
    354         std::wstring perf; 
    355         NSCAPI::nagiosReturn ret = GET_CORE()->simple_query(command, arguments, message, perf); 
    356         lua.push_code(ret); 
    357         lua.push_string(message); 
    358         lua.push_string(perf); 
    359         return 3; 
    360       } catch (...) { 
    361         return luaL_error(L, "Unknown exception in: nscp.execute"); 
    362       } 
     369      core_wrapper core(L); 
     370      return core.simple_query(L); 
    363371    } 
    364372 
  • scripts/lua/test.lua

    r04ef932 ra8c6e93  
     1nscp.print('Loading test script...') 
     2 
     3v = nscp.getSetting('NSCA Agent', 'interval', 'broken') 
     4nscp.print('value: ' .. v) 
     5 
     6function test_func_query(command, args) 
     7  nscp.print('Inside function (query): ' .. command) 
     8  return 'ok', 'whoops 001', '' 
     9end 
     10 
     11function test_func_exec(command, args) 
     12  nscp.print('Inside function (exec): ' .. command) 
     13  return 'ok', 'whoops 002' 
     14end 
     15 
     16function test_func_submission(command, args) 
     17  nscp.print('Inside function (exec): ' .. command) 
     18  return 'ok' 
     19end 
    120 
    221 
     22nscp.execute('version') 
    323 
     24local reg = Registry() 
     25reg:simple_function('lua_test', test_func_query, 'this is a command') 
     26reg:simple_cmdline('lua_test', test_func_exec) 
     27reg:simple_subscription('lua_test', test_func_submission) 
     28 
     29local settings = Settings() 
     30 
     31str = settings:get_string('/settings/lua/scripts', 'testar', 'FOO BAR') 
     32nscp.print('Value: (FOO BAR): ' .. str) 
     33settings:set_string('/settings/lua/scripts', 'testar', 'BAR FOO') 
     34str = settings:get_string('/settings/lua/scripts', 'testar', 'FOO BAR') 
     35nscp.print('Value: (BAR FOO): ' .. str) 
     36i = settings:get_int('/settings/lua/scripts', 'testar', 123) 
     37nscp.print('Value: (123): ' .. i) 
     38settings:set_int('/settings/lua/scripts', 'testar', 456) 
     39i = settings:get_int('/settings/lua/scripts', 'testar', 789) 
     40nscp.print('Value: (456): ' .. i) 
     41 
     42local core = Core() 
     43code, msg, perf = core:simple_query('lua_test', {'a', 'b', 'c'}) 
     44nscp.print('Value: (query): ' .. code) 
     45nscp.print('Value: (query): ' .. msg) 
     46nscp.print('Value: (query): ' .. perf) 
     47code, msgs = core:simple_exec('*', 'lua_test', {'a', 'b', 'c'}) 
     48nscp.print('Value: (exec): ' .. code) 
     49for msg in pairs(msgs) do 
     50  nscp.print('Value: (exec): ' .. msg) 
     51end 
     52code, msg = core:simple_submit('lua_test', 'test_lua', 'ok', 'foo', '') 
     53nscp.print('Value: (submit): ' .. code) 
     54nscp.print('Value: (submit): ' .. msg) 
     55 
Note: See TracChangeset for help on using the changeset viewer.