Changeset 53be5c8 in nscp
- Timestamp:
- 07/12/12 07:43:42 (10 months ago)
- Branches:
- master, 0.4.1, 0.4.2
- Children:
- c327ce5
- Parents:
- 6090c98
- Files:
-
- 5 added
- 1 deleted
- 19 edited
-
CMakeLists.txt (modified) (5 diffs)
-
build.cmake (modified) (1 diff)
-
build/cmake/FindGoogleBreakpad.cmake (modified) (2 diffs)
-
build/cmake/FindGoogleProtoBuf.cmake (added)
-
build/cmake/GoogleProtoBuf.cmake (added)
-
build/cmake/NSCPPBuffer.cmake (deleted)
-
changelog (modified) (1 diff)
-
include/nscapi/nscapi_plugin_impl.cpp (modified) (2 diffs)
-
include/nscapi/nscapi_plugin_impl.hpp (modified) (2 diffs)
-
libs/dotnet-plugin-api/CMakeLists.txt (modified) (1 diff)
-
libs/lua/CMakeLists.txt (modified) (3 diffs)
-
libs/lua_pb/CMakeLists.txt (added)
-
libs/protobuf/plugin.proto (modified) (5 diffs)
-
modules/CheckEventLog/CheckEventLog.cpp (modified) (1 diff)
-
modules/CheckSystem/CheckSystem.cpp (modified) (7 diffs)
-
modules/LUAScript/CMakeLists.txt (modified) (2 diffs)
-
modules/LUAScript/LUAScript.cpp (modified) (3 diffs)
-
modules/LUAScript/LUAScript.h (modified) (2 diffs)
-
modules/LUAScript/lua_wrappers.cpp (added)
-
modules/LUAScript/lua_wrappers.hpp (modified) (11 diffs)
-
modules/LUAScript/script_wrapper.hpp (modified) (6 diffs)
-
scripts/lua/test_nrpe.lua (added)
-
service/NSClient++.cpp (modified) (1 diff)
-
service/NSClient++.h (modified) (1 diff)
-
service/cli_parser.hpp (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
CMakeLists.txt
rc6a974c r53be5c8 58 58 /usr/include/lua 59 59 ) 60 IF (LUA_INCLUE_DIR) 61 message(STATUS "Found lua in ${LUA_INCLUE_DIR}") 62 SET(HAVE_LUA TRUE PARENT_SCOPE) 63 SET(HAVE_LUA TRUE) 64 SET(LUA_SOURCE_DIR ${LUA_INCLUE_DIR}) 65 ELSE (LUA_INCLUE_DIR) 66 message(STATUS "Lua was not found in LUA_INCLUE_DIR=${LUA_INCLUE_DIR}") 67 ENDIF (LUA_INCLUE_DIR) 68 60 69 # ### ### ### ### 61 70 # Google breakpad … … 87 96 88 97 INCLUDE(${BUILD_CMAKE_FOLDER}/SetMultiThread.cmake) 89 INCLUDE(${BUILD_CMAKE_FOLDER}/ NSCPPBuffer.cmake)98 INCLUDE(${BUILD_CMAKE_FOLDER}/GoogleProtoBuf.cmake) 90 99 INCLUDE(${BUILD_CMAKE_FOLDER}/functions.cmake) 91 100 … … 107 116 ENDIF (TINYXML2_FOUND) 108 117 109 110 118 FIND_PACKAGE(ZeroMQ) 111 119 IF (ZEROMQ_FOUND) … … 113 121 ENDIF (ZEROMQ_FOUND) 114 122 123 124 FIND_PACKAGE(GoogleProtoBuf) 125 if(PROTOBUF_FOUND) 126 message(STATUS "Found proto in: ${PROTOBUF_INCLUDE_DIR} / ${PROTOBUF_LIBRARY} (${PROTOBUF_PROTOC_EXECUTABLE})") 127 INCLUDE_DIRECTORIES("${PROTOBUF_INCLUDE_DIR}") 128 IF(PROTOC_GEN_LUA_FOUND) 129 message(STATUS "Found lua protocol buffers (ie. advanced script support)") 130 ENDIF(PROTOC_GEN_LUA_FOUND) 131 else(PROTOBUF_FOUND) 132 message(FATAL_ERROR "Protocol buffers NOT found (${PROTOBUF_LIBRARYDIR})") 133 endif(PROTOBUF_FOUND) 115 134 116 135 SET(VERSION_TXT ${CMAKE_CURRENT_SOURCE_DIR}/version.txt) … … 254 273 MESSAGE(FATAL_ERROR "boost NOT fround: ${Boost_ERROR_REASON}") 255 274 endif(Boost_FOUND) 256 257 if(PROTOBUF_FOUND)258 message(STATUS "Found proto in: ${PROTOBUF_INCLUDE_DIR} / ${PROTOBUF_LIBRARY} (${PROTOBUF_PROTOC_EXECUTABLE})")259 INCLUDE_DIRECTORIES("${PROTOBUF_INCLUDE_DIR}")260 else(PROTOBUF_FOUND)261 message(FATAL_ERROR "Protocol buffers NOT found (${PROTOBUF_LIBRARYDIR})")262 endif(PROTOBUF_FOUND)263 275 264 276 IF(WIN32) -
build.cmake
rc6a974c r53be5c8 19 19 ENDIF() 20 20 21 SET(PROTOC_GEN_LUA_CMD "C:/Python/27x64/Scripts/protoc-gen-lua.cmd") 22 21 23 SET(INC_GOOGLE_BREAKPAD_DIR "D:/source/libraries/google-breakpad-svn") 22 24 -
build/cmake/FindGoogleBreakpad.cmake
r7ec3dd1 r53be5c8 28 28 ENDIF(NOT BREAKPAD_LIBRARY_SUFFIX_DEBUG) 29 29 30 MESSAGE( "Breakpad config: ${BREAKPAD_LIBRARY_PREFIX}...${BREAKPAD_LIBRARY_SUFFIX}, ${BREAKPAD_LIBRARY_PREFIX_DEBUG}...${BREAKPAD_LIBRARY_SUFFIX_DEBUG}" )30 MESSAGE(STATUS "Breakpad config: ${BREAKPAD_LIBRARY_PREFIX}...${BREAKPAD_LIBRARY_SUFFIX}, ${BREAKPAD_LIBRARY_PREFIX_DEBUG}...${BREAKPAD_LIBRARY_SUFFIX_DEBUG}" ) 31 31 SET(BREAKPAD_FOUND TRUE) 32 32 FOREACH(COMPONENT ${GoogleBreakpad_FIND_COMPONENTS}) … … 47 47 ENDFOREACH(COMPONENT) 48 48 IF(BREAKPAD_FOUND) 49 MESSAGE( "Looking for dump-symbols in: ${BREAKPAD_INCLUDE_DIR}/tools/windows/binaries" )49 MESSAGE(STATUS "Looking for dump-symbols in: ${BREAKPAD_INCLUDE_DIR}/tools/windows/binaries" ) 50 50 FIND_PROGRAM(BREAKPAD_DUMPSYMS_EXE 51 51 dump_syms.exe NAMES dump_syms dumpsyms 52 52 PATHS ENV PATH ${BREAKPAD_INCLUDE_DIR}/tools/windows/binaries 53 53 ) 54 MESSAGE( "Looking for dump-symbols in: ${BREAKPAD_INCLUDE_DIR}/tools/windows/binaries: ${BREAKPAD_DUMPSYMS_EXE}" )54 MESSAGE(STATUS "Looking for dump-symbols in: ${BREAKPAD_INCLUDE_DIR}/tools/windows/binaries: ${BREAKPAD_DUMPSYMS_EXE}" ) 55 55 IF(BREAKPAD_DUMPSYMS_EXE) 56 56 SET(BREAKPAD_DUMPSYMS_EXE_FOUND TRUE) -
changelog
r6090c98 r53be5c8 4 4 * Fix dependonservice LanManWorkStation (old win) 5 5 * Fix RtlStringFromGUID problem on NT4 6 7 2012-07-11 MickeM 8 * LUAScript: Improved lua scripting module a lot 9 * LUAScript: Added protocol buffer support to lua scripts 10 * tests: Rewrote (halfway there) test_nrpe python script as a lua script. 11 * CheckEventLog: Fixed command name when submitting real-time "no action checks" 6 12 7 13 2012-06-18 MickeM -
include/nscapi/nscapi_plugin_impl.cpp
rc3f233d r53be5c8 15 15 nscapi::core_wrapper* nscapi::impl::simple_plugin::get_core() { 16 16 return plugin_singleton->get_core(); 17 } 18 19 20 void nscapi::impl::simple_plugin::register_command(std::wstring command, std::wstring description, std::list<std::wstring> aliases) { 21 BOOST_FOREACH(const std::wstring alias, aliases) { 22 register_command(alias, description); 23 } 24 register_command(command, description); 17 25 } 18 26 … … 42 50 } 43 51 52 53 44 54 NSCAPI::nagiosReturn nscapi::impl::simple_command_line_exec::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response) { 45 55 nscapi::protobuf::types::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); -
include/nscapi/nscapi_plugin_impl.hpp
rc3f233d r53be5c8 23 23 return boost::shared_ptr<nscapi::settings_proxy>(new nscapi::settings_proxy(get_core())); 24 24 } 25 25 26 void register_command(std::wstring command, std::wstring description) { 26 27 get_core()->registerCommand(get_id(), command, description); 27 28 } 29 void register_command(std::wstring command, std::wstring description, std::list<std::wstring> aliases); 28 30 29 31 }; … … 33 35 NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 34 36 virtual NSCAPI::nagiosReturn handleSimpleNotification(const std::wstring channel, const std::wstring source, const std::wstring command, NSCAPI::nagiosReturn code, std::wstring msg, std::wstring perf) = 0; 35 36 37 }; 37 38 -
libs/dotnet-plugin-api/CMakeLists.txt
r74e060a r53be5c8 21 21 STRING(REPLACE "/GZ" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) 22 22 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /clr") 23 MESSAGE(STATUS "---> DOT NET FLAGS: ${CMAKE_CXX_FLAGS}")24 MESSAGE(STATUS "---> DOT NET FLAGS: ${CMAKE_CXX_FLAGS_DEBUG}")25 MESSAGE(STATUS "---> DOT NET FLAGS: ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")26 MESSAGE(STATUS "---> DOT NET FLAGS: ${CMAKE_CXX_FLAGS_MINSIZEREL}")27 23 28 24 ADD_LIBRARY(NSCPDOTNET MODULE ${SRCS}) -
libs/lua/CMakeLists.txt
rd66ccee r53be5c8 2 2 3 3 IF (HAVE_LUA) 4 message(STATUS "Found CACHED lua in ${LUA_SOURCE_DIR}") 5 ELSE (HAVE_LUA) 6 FIND_PATH(LUA_SOURCE_DIR lua.h ${LUA_INCLUE_DIR}/src) 7 8 IF (LUA_INCLUE_DIR) 9 message(STATUS "Found lua in ${LUA_INCLUE_DIR}") 10 SET(HAVE_LUA TRUE PARENT_SCOPE) 11 SET(HAVE_LUA TRUE) 12 SET(LUA_SOURCE_DIR ${LUA_INCLUE_DIR}) 13 ELSE (LUA_INCLUE_DIR) 14 message(STATUS "Lua was not found in ${LUA_SOURCE_DIR} (LUA_SOURCE_DIR)") 15 ENDIF (LUA_INCLUE_DIR) 16 ENDIF (HAVE_LUA) 17 18 19 IF (HAVE_LUA) 4 SET(LUA_SOURCE_DIR ${LUA_INCLUE_DIR}) 20 5 #FILE (GLOB lua_SOURCES "${LUA_SOURCE_DIR}/*.c") 21 6 … … 51 36 ${LUA_SOURCE_DIR}/lzio.c 52 37 ) 53 54 38 55 39 FILE (GLOB lua_HEADERS "${LUA_SOURCE_DIR}/*.h") 56 message(STATUS "Using lua from ${LUA_SOURCE_DIR}")57 40 IF (MSVC) 58 41 CMAKE_POLICY(SET CMP0008 NEW) … … 70 53 SET_TARGET_PROPERTIES(lua_static PROPERTIES FOLDER "libraries") 71 54 72 # IF (WIN32)73 # TARGET_LINK_LIBRARIES (cryptopp Ws2_32.lib)74 # TARGET_LINK_LIBRARIES (cryptopp_static Ws2_32.lib)75 # ENDIF (WIN32)76 77 55 IF(APPLE) 78 56 SET_TARGET_PROPERTIES(lua PROPERTIES COMPILE_FLAGS "-fPIC -w -DOSX -DNDEBUG -finline-functions -O3 -DCRYPTOPP_DISABLE_ASM") -
libs/protobuf/plugin.proto
r8d89d7a r53be5c8 126 126 optional string data = 3; 127 127 } 128 message ResponseData { 129 message ResponseDataCell { 130 required Common.AnyDataType data = 1; 131 optional string tag = 2; 132 } 133 message ResponseDataRow { 134 repeated ResponseDataCell cells = 1; 135 } 136 required string alias = 1; 137 optional ResponseDataRow headers = 2; 138 repeated ResponseDataRow rows = 3; 139 140 } 128 141 129 142 }; … … 193 206 message ExecuteResponseMessage { 194 207 message Response { 195 196 message ResponseData {197 message ResponseDataCell {198 required Common.AnyDataType data = 1;199 optional string tag = 2;200 }201 message ResponseDataRow {202 repeated ResponseDataCell cells = 1;203 }204 required string alias = 1;205 optional ResponseDataRow headers = 2;206 repeated ResponseDataRow rows = 3;207 208 }209 210 208 optional int32 id = 2; 211 209 required string command = 5; … … 215 213 required string message = 10; 216 214 217 optional ResponseData data = 11;215 optional Common.ResponseData data = 11; 218 216 repeated Common.Attachment attachments = 17; 219 217 … … 272 270 required Settings node = 2; 273 271 274 optional string default = 3;272 //optional string default = 3; 275 273 optional bool fetch_descriptions = 4; 276 274 … … 283 281 message QuerySettingsResponseMessage { 284 282 message Response { 285 message SectionKeys {286 repeated string key = 1;287 }288 283 optional int64 id = 1; 289 284 required Settings node = 2; 290 285 291 286 optional Common.AnyDataType value = 4; 292 optional SectionKeyskeys = 5;287 repeated string keys = 5; 293 288 294 289 optional string title = 6; -
modules/CheckEventLog/CheckEventLog.cpp
rc3f233d r53be5c8 71 71 void real_time_thread::process_no_events(const filters::filter_config_object &object) { 72 72 std::wstring response; 73 if (!nscapi::core_helper::submit_simple_message(object.target, object.alias, NSCAPI::returnOK, object.ok_msg, object.perf_msg, response)) { 73 std::wstring command = object.alias; 74 if (!object.command.empty()) 75 command = object.command; 76 if (!nscapi::core_helper::submit_simple_message(object.target, command, NSCAPI::returnOK, object.ok_msg, object.perf_msg, response)) { 74 77 NSC_LOG_ERROR(_T("Failed to submit evenhtlog result: ") + response); 75 78 } -
modules/CheckSystem/CheckSystem.cpp
rc3f233d r53be5c8 21 21 22 22 #include "stdafx.h" 23 #include "CheckSystem.h" 24 25 #include <map> 26 #include <set> 23 27 24 28 #include <boost/regex.hpp> 25 #include <boost/lexical_cast.hpp> 26 27 #include "CheckSystem.h" 29 #include <boost/assign/list_of.hpp> 30 31 #include <tlhelp32.h> 32 33 28 34 #include <utils.h> 29 #include <tlhelp32.h>30 35 #include <EnumNtSrv.h> 31 36 #include <EnumProcess.h> 32 37 #include <checkHelpers.hpp> 33 #include <map>34 #include <set>35 38 #include <sysinfo.h> 36 39 #include <filter_framework.hpp> 37 40 #include <simple_registry.hpp> 38 41 #include <settings/client/settings_client.hpp> 39 #include <arrayBuffer.h>40 41 42 #include <config.h> 42 43 … … 69 70 */ 70 71 72 bool missing_system_counters(std::map<std::wstring,std::wstring> &counters) 73 { 74 wchar_t *keys[] = {PDH_SYSTEM_KEY_UPT, PDH_SYSTEM_KEY_MCL, PDH_SYSTEM_KEY_MCB, PDH_SYSTEM_KEY_CPU}; 75 BOOST_FOREACH(const wchar_t *cnt, keys) { 76 if (counters.find(cnt) == counters.end()) 77 return true; 78 } 79 return false; 80 } 81 71 82 bool CheckSystem::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 72 83 PDHCollector::system_counter_data *data = new PDHCollector::system_counter_data; … … 74 85 try { 75 86 typedef std::map<std::wstring,std::wstring> counter_map_type; 76 std::map<std::wstring,std::wstring> counters; 77 bool default_counters = true; 87 counter_map_type counters; 78 88 79 89 sh::settings_registry settings(get_settings_proxy()); … … 81 91 82 92 settings.alias().add_path_to_settings() 93 (_T("pdh/counters"), sh::wstring_map_path(&counters) 94 , _T("PDH COUNTERS"), _T("Define various PDH counters to check.")) 95 ; 96 97 settings.register_all(); 98 settings.notify(); 99 settings.clear(); 100 101 if (counters.empty() || missing_system_counters(counters)) { 102 std::wstring path = settings.alias().get_settings_path(_T("pdh/counters")); 103 104 get_core()->settings_register_key(path, PDH_SYSTEM_KEY_UPT, NSCAPI::key_string, _T("UPTIME"), _T("PDH Key for system uptime."), _T("\\2\\674"), false); 105 get_core()->settings_register_key(path, PDH_SYSTEM_KEY_MCL, NSCAPI::key_string, _T("Commit limit"), _T("PDH key for memory commit limit"), _T("\\4\\30"), false); 106 get_core()->settings_register_key(path, PDH_SYSTEM_KEY_MCB, NSCAPI::key_string, _T("Commit bytes"), _T("PDH Key for system CPU load."), _T("\\4\\26"), false); 107 get_core()->settings_register_key(path, PDH_SYSTEM_KEY_CPU, NSCAPI::key_string, _T("CPU Load"), _T("PDH Key for system CPU load."), _T("\\238(_total)\\6"), false); 108 get_core()->settings_register_key(path + _T("/") + PDH_SYSTEM_KEY_CPU, _T("collection strategy"), NSCAPI::key_string, _T("Collection Strategy"), _T("Collection strategy for CPP is usually round robin."), _T("round robin"), false); 109 } 110 111 settings.alias().add_path_to_settings() 83 112 (_T("WINDOWS CHECK SYSTEM"), _T("Section for system checks and system settings")) 84 113 … … 87 116 (_T("pdh"), _T("PDH COUNTER INFORMATION"), _T("")) 88 117 89 (_T("pdh/counters"), sh::wstring_map_path(&counters)90 , _T("PDH COUNTERS"), _T(""))91 92 118 ; 93 119 94 120 settings.alias().add_key_to_settings() 95 (_T("default"), sh::bool_key(&default_counters, true),96 _T("DEFAULT COUNTERS"), _T("Load the default counters: ") PDH_SYSTEM_KEY_CPU _T(", ") PDH_SYSTEM_KEY_MCB _T(", ") PDH_SYSTEM_KEY_MCL _T(" and ") PDH_SYSTEM_KEY_UPT _T(" If not you need to specify these manually. ") )97 98 121 (_T("default buffer length"), sh::wstring_key(&data->buffer_length, _T("1h")), 99 122 _T("DEFAULT LENGTH"), _T("Used to define the default intervall for range buffer checks (ie. CPU).")) … … 129 152 settings.notify(); 130 153 154 131 155 typedef PDHCollector::system_counter_data::counter cnt; 156 /* 132 157 if (default_counters) { 133 158 data->counters.push_back(cnt(PDH_SYSTEM_KEY_MCL, _T("\\4\\30"), cnt::type_int64, cnt::format_large, cnt::value)); … … 136 161 data->counters.push_back(cnt(PDH_SYSTEM_KEY_UPT, _T("\\2\\674"), cnt::type_int64, cnt::format_large, cnt::value)); 137 162 } 163 */ 138 164 BOOST_FOREACH(counter_map_type::value_type c, counters) { 139 165 data->counters.push_back(cnt(c.first, c.second, cnt::type_int64, cnt::format_large, cnt::value)); 140 166 } 141 167 142 register_command(_T("checkCPU"), _T("Check the CPU load of the computer.")); 143 register_command(_T("checkUpTime"), _T("Check the up-time of the computer.")); 144 register_command(_T("checkServiceState"), _T("Check the state of one or more of the computer services.")); 145 register_command(_T("checkProcState"), _T("Check the state of one or more of the processes running on the computer.")); 146 register_command(_T("checkMem"), _T("Check free/used memory on the system.")); 147 register_command(_T("checkCounter"), _T("Check a PDH counter.")); 148 register_command(_T("listCounterInstances"), _T("List all instances for a counter.")); 149 register_command(_T("checkSingleRegEntry"), _T("Check registry key")); 168 register_command(_T("check_CPU"), _T("Check that the load of the CPU(s) are within bounds."), 169 boost::assign::list_of(_T("checkCPU"))); 170 register_command(_T("check_uptime"), _T("Check time since last server re-boot."), 171 boost::assign::list_of(_T("checkUpTime"))); 172 register_command(_T("check_service"), _T("Check the state of one or more of the computer services."), 173 boost::assign::list_of(_T("checkServiceState"))); 174 register_command(_T("check_process"), _T("Check the state of one or more of the processes running on the computer."), 175 boost::assign::list_of(_T("checkProcState"))); 176 register_command(_T("check_memory"), _T("Check free/used memory on the system."), 177 boost::assign::list_of(_T("checkMem"))); 178 register_command(_T("check_pdh"), _T("Check a PDH counter."), 179 boost::assign::list_of(_T("checkCounter"))); 180 register_command(_T("check_registry"), _T("Check values in the registry."), 181 boost::assign::list_of(_T("checkSingleRegEntry"))); 182 183 register_command(_T("listCounterInstances"), _T("*DEPRECATED* List all instances for a counter.")); 184 150 185 } catch (nscapi::nscapi_exception &e) { 151 186 NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); -
modules/LUAScript/CMakeLists.txt
ra06e6af r53be5c8 8 8 stdafx.cpp 9 9 "${TARGET}.cpp" 10 lua_wrappers.cpp 11 10 12 ${NSCP_DEF_PLUGIN_CPP} 11 13 ) … … 28 30 add_library(${TARGET} MODULE ${SRCS}) 29 31 32 SET(LUA_PB) 33 IF (PROTOC_GEN_LUA_FOUND) 34 MESSAGE(STATUS "Lua has PB support") 35 SET(LUA_PB lua_pb_static) 36 ADD_DEFINITIONS(-DHAVE_LUA_PB) 37 INCLUDE_DIRECTORIES(${NSCP_PROJECT_BINARY_DIR}/libs/lua_pb) 38 INCLUDE_DIRECTORIES(${NSCP_PROJECT_BINARY_DIR}/libs/protobuf) 39 ENDIF() 40 41 30 42 target_link_libraries(${TARGET} 31 43 ${Boost_FILESYSTEM_LIBRARY} 44 ${Boost_PROGRAM_OPTIONS_LIBRARY} 32 45 ${NSCP_DEF_PLUGIN_LIB} 33 46 lua_static 47 ${LUA_PB} 34 48 ) 35 49 INCLUDE(${BUILD_CMAKE_FOLDER}/module.cmake) -
modules/LUAScript/LUAScript.cpp
ra8c6e93 r53be5c8 21 21 #include "stdafx.h" 22 22 #include "LUAScript.h" 23 24 #include <boost/program_options.hpp> 25 23 26 #include <strEx.h> 24 27 #include <time.h> … … 36 39 37 40 namespace sh = nscapi::settings_helper; 41 namespace po = boost::program_options; 38 42 39 43 bool LUAScript::loadModule() { … … 167 171 168 172 169 NSCAPI::nagiosReturn LUAScript::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) { 170 if (command == _T("luareload")) { 171 return reload(message)?NSCAPI::returnOK:NSCAPI::returnCRIT; 172 } 173 if (!registry->has_command(command)) 173 NSCAPI::nagiosReturn LUAScript::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) { 174 if (!registry->has_command(char_command)) 174 175 return NSCAPI::returnIgnored; 175 return registry->on_query(target, command, arguments, message, perf); 176 // const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf 176 return registry->on_query(char_command, request, response); 177 } 178 179 180 181 NSCAPI::nagiosReturn LUAScript::execute_and_load(std::list<std::wstring> args, std::wstring &message) { 182 try { 183 po::options_description desc("Options for the following commands: (exec, execute)"); 184 boost::program_options::variables_map vm; 185 std::wstring file; 186 desc.add_options() 187 ("help", "Display help") 188 ("script", po::wvalue<std::wstring>(&file), "The script to run") 189 ("file", po::wvalue<std::wstring>(&file), "The script to run") 190 ; 191 192 std::vector<std::wstring> vargs(args.begin(), args.end()); 193 po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(vargs).options(desc).run(); 194 po::store(parsed, vm); 195 po::notify(vm); 196 197 if (vm.count("help") > 0) { 198 std::stringstream ss; 199 ss << desc; 200 message = utf8::to_unicode(ss.str()); 201 return NSCAPI::returnUNKNOWN; 202 } 203 204 boost::optional<boost::filesystem::wpath> ofile = find_file(file); 205 if (!ofile) { 206 message = _T("Script not found: ") + file; 207 NSC_LOG_ERROR_STD(message); 208 return NSCAPI::returnUNKNOWN; 209 } 210 211 try { 212 instances_.push_back(script_wrapper::lua_script::create_instance(get_core(), get_id(), registry, _T("cmdline"), (*ofile).string())); 213 } catch (const lua_wrappers::LUAException &e) { 214 NSC_LOG_ERROR_STD(_T("Could not load script ") + _T(": ") + e.getMessage()); 215 } catch (const std::exception &e) { 216 NSC_LOG_ERROR_STD(_T("Could not load script ") + _T(": ") + utf8::to_unicode(e.what())); 217 } 218 return NSCAPI::returnOK; 219 } catch (const std::exception &e) { 220 message = _T("Failed to execute script ") + utf8::to_unicode(e.what()); 221 NSC_LOG_ERROR_STD(message); 222 return NSCAPI::returnUNKNOWN; 223 } catch (...) { 224 message = _T("Failed to execute script "); 225 NSC_LOG_ERROR_STD(message); 226 return NSCAPI::returnUNKNOWN; 227 } 177 228 } 178 229 179 230 NSCAPI::nagiosReturn LUAScript::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 231 if (command == _T("help")) { 232 std::list<std::wstring> args; 233 args.push_back(_T("--help")); 234 std::wstring result; 235 int ret = execute_and_load(args, result); 236 return ret; 237 } else if (command == _T("lua-execute") || command == _T("lua-run") 238 || command == _T("run") || command == _T("execute") || command == _T("exec") || command == _T("")) { 239 return execute_and_load(arguments, result); 240 } 180 241 if (!registry->has_exec(command)) 181 242 return NSCAPI::returnIgnored; -
modules/LUAScript/LUAScript.h
ra8c6e93 r53be5c8 34 34 35 35 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 {36 class LUAScript : public nscapi::impl::simple_command_line_exec, public nscapi::impl::simple_submission_handler, public nscapi::impl::simple_plugin { 37 37 private: 38 38 … … 70 70 boost::optional<boost::filesystem::wpath> find_file(std::wstring file); 71 71 bool loadScript(std::wstring alias, std::wstring file); 72 NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 72 NSCAPI::nagiosReturn execute_and_load(std::list<std::wstring> args, std::wstring &message); 73 NSCAPI::nagiosReturn handleSimpleNotification(const std::wstring channel, const std::wstring source, const std::wstring command, NSCAPI::nagiosReturn code, std::wstring msg, std::wstring perf); 74 NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 73 75 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 76 77 -
modules/LUAScript/lua_wrappers.hpp
ra8c6e93 r53be5c8 8 8 #include "luna.h" 9 9 10 #include <string> 11 #include <list> 12 13 #include <NSCAPI.h> 14 #include <nscapi/nscapi_core_wrapper.hpp> 15 16 #include <strEx.h> 10 17 11 18 namespace lua_wrappers { 12 13 14 15 19 class Lua_State { 16 20 lua_State *L; … … 55 59 return _T("<NOT_A_STRING>"); 56 60 } 61 bool get_string(std::wstring &str, int pos = -1) { 62 if (pos == -1) 63 pos = lua_gettop(L); 64 if (pos == 0) 65 return false; 66 if (is_string(pos)) 67 str = utf8::cvt<std::wstring>(lua_tostring(L, pos)); 68 else if (is_number(pos)) 69 str = strEx::itos(lua_tonumber(L, pos)); 70 else 71 return false; 72 return true; 73 } 57 74 int get_int(int pos = -1) { 58 75 if (pos == -1) … … 66 83 return 0; 67 84 } 68 bool eanget_boolean(int pos = -1) {85 bool get_boolean(int pos = -1) { 69 86 if (pos == -1) 70 87 pos = lua_gettop(L); … … 77 94 return false; 78 95 } 79 NSCAPI::nagiosReturn get_code(int pos = -1) { 80 std::string str; 81 if (pos == -1) 82 pos = lua_gettop(L); 83 if (pos == 0) 84 return NSCAPI::returnUNKNOWN; 85 switch (lua_type(L, pos)) { 86 case LUA_TNUMBER: 87 return static_cast<int>(lua_tonumber(L, pos)); 88 case LUA_TSTRING: 89 return string_to_code(lua_tostring(L, pos)); 90 case LUA_TBOOLEAN: 91 return lua_toboolean(L, pos)?NSCAPI::returnOK:NSCAPI::returnCRIT; 92 } 93 NSC_LOG_ERROR_STD(_T("Incorrect return from script: should be error, ok, warning or unknown")); 94 return NSCAPI::returnUNKNOWN; 95 } 96 NSCAPI::nagiosReturn get_code(int pos = -1); 96 97 std::list<std::wstring> get_array(const int pos = -1) { 97 98 std::list<std::wstring> ret; … … 125 126 return ret; 126 127 } 128 bool pop_string(std::wstring &str) { 129 int top = lua_gettop(L); 130 if (top == 0) 131 return false; 132 if (!get_string(str, top)) 133 return false; 134 pop(); 135 return true; 136 } 137 bool pop_function(int &funref) { 138 int top = lua_gettop(L); 139 if (top == 0) 140 return false; 141 if (!is_function(top)) 142 return false; 143 funref = luaL_ref(L, LUA_REGISTRYINDEX); 144 if (funref == 0) 145 return false; 146 return true; 147 } 127 148 int pop_int() { 128 149 int ret; … … 151 172 return ret; 152 173 } 174 175 void getglobal(const std::wstring &name) { 176 lua_getglobal(L, utf8::cvt<std::string>(name).c_str()); 177 } 178 179 153 180 ////////////////////////////////////////////////////////////////////////// 154 181 // Converters 155 NSCAPI::nagiosReturn string_to_code(std::string str) { 156 if ((str == "critical")||(str == "crit")||(str == "error")) { 157 return NSCAPI::returnCRIT; 158 } else if ((str == "warning")||(str == "warn")) { 159 return NSCAPI::returnWARN; 160 } else if (str == "ok") { 161 return NSCAPI::returnOK; 162 } else if (str == "unknown") { 163 return NSCAPI::returnUNKNOWN; 164 } 165 NSC_LOG_ERROR_STD(_T("Invalid code: ") + utf8::to_unicode(str)); 166 return NSCAPI::returnUNKNOWN; 167 } 182 NSCAPI::nagiosReturn string_to_code(std::string str); 168 183 169 184 … … 245 260 lua_pushstring(L, s.c_str()); 246 261 } 262 void push_raw_string(std::string s) { 263 lua_pushlstring(L, s.c_str(), s.size()); 264 } 247 265 void push_array(std::list<std::wstring> &arr) { 248 266 lua_createtable(L, 0, arr.size()); … … 260 278 return size() == 0; 261 279 } 262 void log_stack() { 263 int args = size(); 264 NSC_DEBUG_MSG_STD(_T("Invalid lua stack state, dumping stack")); 265 for (int i=1;i<args+1;i++) { 266 NSC_DEBUG_MSG_STD(get_type_as_string(i) +_T(": ") + get_string(i)); 267 } 268 } 280 void log_stack(); 269 281 270 282 int error(std::string s) { … … 404 416 boost::shared_ptr<lua_script_instance> instance; 405 417 int func_ref; 418 bool simple; 406 419 }; 407 420 … … 418 431 public: 419 432 420 NSCAPI::nagiosReturn on_query(const std::wstring & target, const std::wstring & command, std::list<std::wstring> & arguments, std::wstring & message, std::wstring & perf) { 421 function_map::iterator it = functions.find(command); 422 if (it == functions.end()) 423 throw LUAException(_T("Invalid function: ") + command); 424 function_container c = it->second; 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()); 430 return NSCAPI::returnUNKNOWN; 431 } 432 int arg_count = lua.size(); 433 if (arg_count > 2) 434 perf = lua.pop_string(); 435 if (arg_count > 1) 436 message = lua.pop_string(); 437 if (arg_count > 0) 438 return lua.pop_code(); 439 NSC_LOG_ERROR_STD(_T("No arguments returned from script.")); 440 return NSCAPI::returnUNKNOWN; 441 } 442 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 484 void register_query(const std::wstring &command, boost::shared_ptr<lua_script_instance> instance, int func_ref) { 433 NSCAPI::nagiosReturn on_query(const wchar_t* command, const std::string &request, std::string &response); 434 NSCAPI::nagiosReturn on_exec(const std::wstring & command, std::list<std::wstring> & arguments, std::wstring & result); 435 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); 436 437 void register_query(const std::wstring &command, boost::shared_ptr<lua_script_instance> instance, int func_ref, bool simple = true) { 485 438 function_container c; 486 439 c.func_ref = func_ref; 487 440 c.instance = instance; 441 c.simple = simple; 488 442 functions[command] = c; 489 443 } … … 562 516 }; 563 517 564 lua_instance_manager::script_map_type lua_instance_manager::scripts;565 518 566 519 -
modules/LUAScript/script_wrapper.hpp
ree52cdd r53be5c8 1 1 #pragma once 2 3 #include <map> 2 4 3 5 #include <boost/enable_shared_from_this.hpp> 4 6 #include <boost/shared_ptr.hpp> 5 #include < map>7 #include <boost/optional.hpp> 6 8 7 9 #include "lua_wrappers.hpp" … … 10 12 #include <nscapi/nscapi_core_helper.hpp> 11 13 14 #ifdef HAVE_LUA_PB 15 #include <Plugin.pb-lua.h> 16 #endif 12 17 namespace script_wrapper { 13 18 … … 71 76 int query(lua_State *L) { 72 77 lua_wrappers::lua_wrapper lua(L); 73 NSC_LOG_ERROR_STD(_T("Unsupported API called: query")); 74 return lua.error("Unsupported API called: query"); 78 try { 79 int nargs = lua.size(); 80 if (nargs != 2) 81 return lua.error("nscp.query requires 2 arguments!"); 82 std::string data = utf8::cvt<std::string>(lua.pop_string()); 83 std::wstring command = lua.pop_string(); 84 std::string response; 85 NSCAPI::nagiosReturn ret = nscapi::plugin_singleton->get_core()->query(command, data, response); 86 lua.push_code(ret); 87 lua.push_raw_string(response); 88 return lua.size(); 89 } catch (...) { 90 return lua.error("Unknown exception in: simple_query"); 91 } 75 92 } 76 93 int simple_exec(lua_State *L) { … … 152 169 static const Luna<registry_wrapper>::RegType methods[]; 153 170 171 boost::optional<int> read_registration(lua_wrapper &lua, std::wstring &command, int &funref, std::wstring &description) { 172 std::wstring funname; 173 if (lua.size() != 2 && lua.size() != 3) 174 return lua.error("Invalid number of arguments: " + strEx::s::xtos(lua.size()) + " expected 2 or 3"); 175 if (lua.size() > 2 && !lua.pop_string(description)) 176 return lua.error("Invalid description"); 177 if (lua.pop_string(funname)) { 178 lua.getglobal(funname); 179 } 180 if (!lua.pop_function(funref)) 181 return lua.error("Invalid function"); 182 if (!lua.pop_string(command)) 183 return lua.error("Invalid command"); 184 return boost::optional<int>(); 185 } 154 186 int register_function(lua_State *L) { 155 lua_wrappers::lua_wrapper lua(L); 156 NSC_LOG_ERROR_STD(_T("Unsupported API called: exec")); 157 return lua.error("Unsupported API called: exec"); 187 // void = (cmd, function, desc) 188 std::wstring command, description; 189 int funref = 0; 190 lua_wrapper lua(L); 191 boost::optional<int> error = read_registration(lua, command, funref, description); 192 if (error) 193 return *error; 194 195 if (description.empty()) 196 description = _T("Lua script: ") + command; 197 get_instance()->get_core()->registerCommand(get_instance()->get_plugin_id(), command, description); 198 get_instance()->get_registry()->register_query(command, get_instance(), funref, false); 199 return 0; 158 200 } 159 201 int register_simple_function(lua_State *L) { 160 lua_wrapper lua(L); 161 std::wstring description; 162 if (lua.size() > 2) 163 description = lua.pop_string(); 164 std::wstring name; 165 if (lua.is_string()) { 166 name = lua.pop_string(); 167 lua_getglobal(L, utf8::cvt<std::string>(name).c_str()); 168 } 169 if (!lua.is_function()) 170 return lua.error("Invalid argument not a function: " + utf8::cvt<std::string>(name)); 171 172 int func_ref = luaL_ref(L, LUA_REGISTRYINDEX); 173 174 if (func_ref == 0) 175 return lua.error("Invalid function: " + utf8::cvt<std::string>(name)); 176 std::wstring script = lua.pop_string(); 202 // void = (cmd, function, desc) 203 std::wstring command, description; 204 int funref = 0; 205 lua_wrapper lua(L); 206 boost::optional<int> error = read_registration(lua, command, funref, description); 207 if (error) 208 return *error; 209 177 210 if (description.empty()) 178 description = _T("Lua script: ") + script;179 get_instance()->get_core()->registerCommand(get_instance()->get_plugin_id(), script, description);180 get_instance()->get_registry()->register_query( script, get_instance(), func_ref);211 description = _T("Lua script: ") + command; 212 get_instance()->get_core()->registerCommand(get_instance()->get_plugin_id(), command, description); 213 get_instance()->get_registry()->register_query(command, get_instance(), funref, true); 181 214 return 0; 182 215 } … … 232 265 const char registry_wrapper::className[] = "Registry"; 233 266 const Luna<registry_wrapper>::RegType registry_wrapper::methods[] = { 234 { " function", ®istry_wrapper::register_function },235 { "simple_ function", ®istry_wrapper::register_simple_function },267 { "query", ®istry_wrapper::register_function }, 268 { "simple_query", ®istry_wrapper::register_simple_function }, 236 269 { "cmdline", ®istry_wrapper::register_cmdline }, 237 270 { "simple_cmdline", ®istry_wrapper::register_simple_cmdline }, … … 411 444 Luna<registry_wrapper>::Register(L); 412 445 Luna<settings_wrapper>::Register(L); 446 #ifdef HAVE_LUA_PB 447 GET_CORE()->log(NSCAPI::log_level::error, "test", 123, "Loading lua pb"); 448 lua_protobuf_Plugin_open(L); 449 #endif 413 450 } 414 451 -
service/NSClient++.cpp
r52c5bf2 r53be5c8 1023 1023 } 1024 1024 1025 int NSClientT::simple_exec(std::wstring module, std::wstringcommand, std::vector<std::wstring> arguments, std::list<std::wstring> &resp) {1025 int NSClientT::simple_exec(std::wstring command, std::vector<std::wstring> arguments, std::list<std::wstring> &resp) { 1026 1026 std::string request; 1027 1027 std::list<std::string> responses; 1028 1028 std::list<std::wstring> errors; 1029 1029 nscapi::functions::create_simple_exec_request(command, arguments, request); 1030 std::wstring module; 1031 std::wstring::size_type pos = command.find(L'.'); 1032 if (pos != std::wstring::npos) { 1033 module = command.substr(0, pos); 1034 command = command.substr(pos+1); 1035 } 1030 1036 int ret = load_and_run(module, boost::bind(&exec_helper, _1, command, arguments, request, &responses), errors); 1031 1037 -
service/NSClient++.h
r440c0cb r53be5c8 135 135 NSCAPI::nagiosReturn inject(std::wstring command, std::wstring arguments, std::wstring &msg, std::wstring & perf); 136 136 std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args); 137 int simple_exec(std::wstring module, std::wstringcommand, std::vector<std::wstring> arguments, std::list<std::wstring> &resp);137 int simple_exec(std::wstring command, std::vector<std::wstring> arguments, std::list<std::wstring> &resp); 138 138 int simple_query(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::list<std::wstring> &resp); 139 139 NSCAPI::nagiosReturn exec_command(const wchar_t* target, const wchar_t* raw_command, std::string &request, std::string &response); -
service/cli_parser.hpp
rc3f233d r53be5c8 88 88 ("argument,a", po::wvalue<std::vector<std::wstring> >(), "List of arguments (gets -- prefixed automatically)") 89 89 ("raw-argument", po::wvalue<std::vector<std::wstring> >(), "List of arguments (does not get -- prefixed)") 90 ("load-all", "Load all plugins.") 90 91 ; 91 92 … … 344 345 modes mode; 345 346 bool boot; 346 client_arguments() : mode(none), boot(false) {} 347 bool load_all; 348 client_arguments() : mode(none), boot(false), load_all(false) {} 347 349 348 350 void debug() { … … 398 400 args.mode = client_arguments::submit; 399 401 } 402 args.load_all = vm.count("load-all")==1; 400 403 401 404 if (vm.count("module")) … … 469 472 args.mode = client_arguments::combined; 470 473 args.module = _T("PythonScript"); 474 } else if (lang == _T("lua")) { 475 args.command = _T("LUAScript.run"); 476 args.combined_query = _T("lua_unittest"); 477 args.mode = client_arguments::combined; 478 args.module = _T("LuaScript"); 471 479 } else { 472 480 std::wcerr << _T("Unknown language: ") << lang << std::endl; … … 522 530 523 531 core_->boot_init(log_level); 532 if (args.load_all) 533 core_->preboot_load_all_plugin_files(); 524 534 if (args.module.empty()) 525 535 core_->boot_load_all_plugins(); … … 536 546 ret = mainClient.simple_query(args.module, args.command, args.arguments, resp); 537 547 } else if (args.mode == client_arguments::exec || args.mode == client_arguments::combined) { 538 ret = mainClient.simple_exec(args. module, args.command, args.arguments, resp);548 ret = mainClient.simple_exec(args.command, args.arguments, resp); 539 549 if (ret == NSCAPI::returnIgnored) { 540 550 ret = 1; 541 551 std::wcout << _T("Command not found (by module): ") << args.command << std::endl; 542 552 resp.push_back(_T("Command not found: ") + args.command); 543 mainClient.simple_exec( args.module,_T("help"), args.arguments, resp);553 mainClient.simple_exec(_T("help"), args.arguments, resp); 544 554 } else if (args.mode == client_arguments::combined) { 545 555 if (ret == NSCAPI::returnOK) {
Note: See TracChangeset
for help on using the changeset viewer.








