Changeset a8c6e93 in nscp
- Timestamp:
- 02/02/12 21:23:24 (16 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- e396b2f
- Parents:
- a06e6af
- Files:
-
- 7 edited
-
changelog (modified) (1 diff)
-
modules/LUAScript/LUAScript.cpp (modified) (2 diffs)
-
modules/LUAScript/LUAScript.def (modified) (1 diff)
-
modules/LUAScript/LUAScript.h (modified) (3 diffs)
-
modules/LUAScript/lua_wrappers.hpp (modified) (5 diffs)
-
modules/LUAScript/script_wrapper.hpp (modified) (5 diffs)
-
scripts/lua/test.lua (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
changelog
ra06e6af ra8c6e93 5 5 * Fixa dependonservice LanManWorkStation (old win) 6 6 * Fix RtlStringFromGUID problem on NT4 7 8 2012-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. 7 12 8 13 2012-02-01 MickeM -
modules/LUAScript/LUAScript.cpp
ra06e6af ra8c6e93 177 177 } 178 178 179 NSCAPI::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 185 NSCAPI::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 179 193 180 194 NSC_WRAP_DLL(); … … 182 196 NSC_WRAPPERS_IGNORE_MSG_DEF(); 183 197 NSC_WRAPPERS_HANDLE_CMD_DEF(); 198 NSC_WRAPPERS_CLI_DEF(); 199 NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF(); -
modules/LUAScript/LUAScript.def
rc5ec0c8 ra8c6e93 13 13 NSUnloadModule 14 14 NSGetModuleDescription 15 NSCommandLineExec 15 16 NSDeleteBuffer 16 17 NSHasNotificationHandler 18 NSHandleNotification -
modules/LUAScript/LUAScript.h
ra06e6af ra8c6e93 20 20 ***************************************************************************/ 21 21 NSC_WRAPPERS_MAIN(); 22 NSC_WRAPPERS_CLI(); 23 NSC_WRAPPERS_CHANNELS(); 24 22 25 //#include <config.h> 23 26 #include <strEx.h> … … 31 34 32 35 33 class LUAScript : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_ plugin {36 class 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 { 34 37 private: 35 38 … … 64 67 bool hasCommandHandler(); 65 68 bool hasMessageHandler(); 69 bool hasNotificationHandler() { return true; } 66 70 boost::optional<boost::filesystem::wpath> find_file(std::wstring file); 67 71 bool loadScript(std::wstring alias, std::wstring file); 68 72 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 69 77 //NSCAPI::nagiosReturn RunLUA(const unsigned int argLen, wchar_t **char_args, std::wstring &message, std::wstring &perf); 70 78 //NSCAPI::nagiosReturn extract_return(Lua_State &L, int arg_count, std::wstring &message, std::wstring &perf); -
modules/LUAScript/lua_wrappers.hpp
ra06e6af ra8c6e93 323 323 } 324 324 325 std::list<std::wstring> inline checkarray(int pos) { 326 luaL_checktype(L, pos, LUA_TTABLE); 327 return get_array(pos); 328 } 329 325 330 boolean inline checkbool(int pos) { 326 331 return lua_toboolean(L, pos); … … 404 409 function_map functions; 405 410 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) { 410 421 function_map::iterator it = functions.find(command); 411 422 if (it == functions.end()) 412 423 throw LUAException(_T("Invalid function: ") + command); 413 424 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()); 432 430 return NSCAPI::returnUNKNOWN; 433 431 } 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(); 444 433 if (arg_count > 2) 445 434 perf = lua.pop_string(); … … 452 441 } 453 442 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 457 484 void register_query(const std::wstring &command, boost::shared_ptr<lua_script_instance> instance, int func_ref) { 458 485 function_container c; … … 461 488 functions[command] = c; 462 489 } 463 464 void clear() {465 functions.clear();466 // DO we need to release reference here?467 }468 469 490 void register_subscription(const std::wstring &channel, boost::shared_ptr<lua_script_instance> instance, int func_ref) { 470 491 function_container c; … … 474 495 } 475 496 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) { 477 498 function_container c; 478 499 c.func_ref = func_ref; 479 500 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 } 486 520 }; 487 521 -
modules/LUAScript/script_wrapper.hpp
ra06e6af ra8c6e93 74 74 } 75 75 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 } 78 90 } 79 91 int exec(lua_State *L) { … … 83 95 } 84 96 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 } 87 113 } 88 114 int submit(lua_State *L) { … … 174 200 return lua.error("Invalid function: " + utf8::cvt<std::string>(name)); 175 201 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); 177 203 return 0; 178 204 } … … 311 337 std::wstring path = lua.wstring(1); 312 338 std::wstring key = lua.wstring(1); 313 std:: wstring stype = lua.wstring(1);339 std::string stype = lua.string(1); 314 340 NSCAPI::settings_type type = get_type(stype); 315 341 std::wstring title = lua.wstring(1); … … 341 367 342 368 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); 363 371 } 364 372 -
scripts/lua/test.lua
r04ef932 ra8c6e93 1 nscp.print('Loading test script...') 2 3 v = nscp.getSetting('NSCA Agent', 'interval', 'broken') 4 nscp.print('value: ' .. v) 5 6 function test_func_query(command, args) 7 nscp.print('Inside function (query): ' .. command) 8 return 'ok', 'whoops 001', '' 9 end 10 11 function test_func_exec(command, args) 12 nscp.print('Inside function (exec): ' .. command) 13 return 'ok', 'whoops 002' 14 end 15 16 function test_func_submission(command, args) 17 nscp.print('Inside function (exec): ' .. command) 18 return 'ok' 19 end 1 20 2 21 22 nscp.execute('version') 3 23 24 local reg = Registry() 25 reg:simple_function('lua_test', test_func_query, 'this is a command') 26 reg:simple_cmdline('lua_test', test_func_exec) 27 reg:simple_subscription('lua_test', test_func_submission) 28 29 local settings = Settings() 30 31 str = settings:get_string('/settings/lua/scripts', 'testar', 'FOO BAR') 32 nscp.print('Value: (FOO BAR): ' .. str) 33 settings:set_string('/settings/lua/scripts', 'testar', 'BAR FOO') 34 str = settings:get_string('/settings/lua/scripts', 'testar', 'FOO BAR') 35 nscp.print('Value: (BAR FOO): ' .. str) 36 i = settings:get_int('/settings/lua/scripts', 'testar', 123) 37 nscp.print('Value: (123): ' .. i) 38 settings:set_int('/settings/lua/scripts', 'testar', 456) 39 i = settings:get_int('/settings/lua/scripts', 'testar', 789) 40 nscp.print('Value: (456): ' .. i) 41 42 local core = Core() 43 code, msg, perf = core:simple_query('lua_test', {'a', 'b', 'c'}) 44 nscp.print('Value: (query): ' .. code) 45 nscp.print('Value: (query): ' .. msg) 46 nscp.print('Value: (query): ' .. perf) 47 code, msgs = core:simple_exec('*', 'lua_test', {'a', 'b', 'c'}) 48 nscp.print('Value: (exec): ' .. code) 49 for msg in pairs(msgs) do 50 nscp.print('Value: (exec): ' .. msg) 51 end 52 code, msg = core:simple_submit('lua_test', 'test_lua', 'ok', 'foo', '') 53 nscp.print('Value: (submit): ' .. code) 54 nscp.print('Value: (submit): ' .. msg) 55
Note: See TracChangeset
for help on using the changeset viewer.








