Changeset 2c95d22 in nscp
- Timestamp:
- 08/15/11 00:02:39 (21 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- b9498ef
- Parents:
- 65a2940
- Files:
-
- 26 edited
-
changelog (modified) (1 diff)
-
include/NSCAPI.h (modified) (3 diffs)
-
include/nscapi/functions.hpp (modified) (4 diffs)
-
include/nscapi/nscapi_core_wrapper.cpp (modified) (10 diffs)
-
include/nscapi/nscapi_core_wrapper.hpp (modified) (2 diffs)
-
include/nscapi/nscapi_plugin_wrapper.cpp (modified) (1 diff)
-
include/nscapi/nscapi_plugin_wrapper.hpp (modified) (1 diff)
-
libs/protobuf/exec.proto (modified) (1 diff)
-
modules/CheckExternalScripts/CheckExternalScripts.cpp (modified) (1 diff)
-
modules/CheckHelpers/CheckHelpers.cpp (modified) (6 diffs)
-
modules/LUAScript/script_wrapper.hpp (modified) (1 diff)
-
modules/NRPEClient/NRPEClient.cpp (modified) (2 diffs)
-
modules/NRPEClient/NRPEClient.h (modified) (1 diff)
-
modules/NRPEServer/handler_impl.cpp (modified) (1 diff)
-
modules/NSClientServer/handler_impl.cpp (modified) (1 diff)
-
modules/PythonScript/PythonScript.cpp (modified) (7 diffs)
-
modules/PythonScript/PythonScript.h (modified) (5 diffs)
-
modules/PythonScript/script_wrapper.cpp (modified) (8 diffs)
-
modules/PythonScript/script_wrapper.hpp (modified) (6 diffs)
-
modules/Scheduler/Scheduler.cpp (modified) (1 diff)
-
scripts/python/test.py (modified) (3 diffs)
-
service/NSClient++.cpp (modified) (2 diffs)
-
service/NSClient++.h (modified) (1 diff)
-
service/cli_parser.hpp (modified) (1 diff)
-
service/core_api.cpp (modified) (2 diffs)
-
service/core_api.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
changelog
r65a2940 r2c95d22 10 10 * NRPEClient now works on linux 11 11 * Added "portable" settings map file to installer (so it will work with old installed versions) 12 * Rename Function to Registry in PythonScript API as well as some other function renames 13 * Started to clean up the helpers around the API 14 * Added support for execute to PythonScripts to execute commands 15 * BUG: just realised that static plugin instances prevent multiple instances :) 16 Will fix but not now as it is not important (for me)... 17 * Added initial support for channels to PythonScript 18 Core still lacks support for subscribing to arbitrary channels 12 19 13 20 2011-08-13 MickeM -
include/NSCAPI.h
r39c73cd r2c95d22 24 24 #include <unicode_char.hpp> 25 25 #include <string> 26 #include <strEx.h> 26 27 27 28 namespace NSCAPI { … … 111 112 112 113 113 class nscapi_exception {114 class nscapi_exception : public std::exception { 114 115 public: 115 116 std::wstring msg_; 116 117 nscapi_exception(std::wstring msg) : msg_(msg) {} 118 std::string what() { 119 return utf8::cvt<std::string>(msg_); 120 } 117 121 }; 118 122 … … 133 137 typedef NSCAPI::errorReturn (*lpNSAPIExit)(void); 134 138 typedef NSCAPI::nagiosReturn (*lpNSAPIInject)(const wchar_t*, const char *, const unsigned int, char **, unsigned int *); 139 typedef NSCAPI::nagiosReturn (*lpNSAPIExecCommand)(const wchar_t*, const char *, const unsigned int, char **, unsigned int *); 135 140 typedef void (*lpNSAPIDestroyBuffer)(char**); 136 141 -
include/nscapi/functions.hpp
r39c73cd r2c95d22 113 113 std::wstring command; 114 114 std::list<std::wstring> args; 115 std::vector<std::wstring> args_vector;115 //std::vector<std::wstring> args_vector; 116 116 }; 117 117 118 118 119 static decoded_simple_command_data p rocess_simple_command_line_exec_request(const wchar_t* char_command, const std::string &request) {119 static decoded_simple_command_data parse_simple_exec_request(const wchar_t* char_command, const std::string &request) { 120 120 decoded_simple_command_data data; 121 121 … … 129 129 ::ExecuteCommand::Request payload = request_message.payload().Get(0); 130 130 for (int i=0;i<payload.arguments_size();i++) { 131 data.args _vector.push_back(to_wstring(payload.arguments(i)));131 data.args.push_back(to_wstring(payload.arguments(i))); 132 132 } 133 133 return data; 134 134 } 135 static NSCAPI::nagiosReturn process_simple_command_line_exec_result(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring result, std::string &response) {135 static NSCAPI::nagiosReturn create_simple_exec_result(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring result, std::string &response) { 136 136 ExecuteCommand::ResponseMessage response_message; 137 137 ::ExecuteCommand::Header* hdr = response_message.mutable_header(); … … 166 166 return data; 167 167 } 168 168 169 static NSCAPI::nagiosReturn process_simple_command_result(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &response) { 169 170 PluginCommand::ResponseMessage response_message; … … 183 184 return ret; 184 185 } 186 187 static void create_simple_exec_request(const std::wstring &command, const std::list<std::wstring> & args, std::string &request) { 188 189 ExecuteCommand::RequestMessage message; 190 ExecuteCommand::Header *hdr = message.mutable_header(); 191 hdr->set_type(ExecuteCommand::Header_Type_REQUEST); 192 hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 193 194 ExecuteCommand::Request *req = message.add_payload(); 195 req->set_command(to_string(command)); 196 req->set_version(ExecuteCommand::Request_Version_VERSION_1); 197 198 BOOST_FOREACH(std::wstring s, args) 199 req->add_arguments(to_string(s)); 200 201 message.SerializeToString(&request); 202 } 203 static void parse_simple_exec_result(const std::string &response, std::list<std::wstring> &result) { 204 ExecuteCommand::ResponseMessage response_message; 205 response_message.ParseFromString(response); 206 207 for (int i=0;i<response_message.payload_size(); i++) { 208 result.push_back(utf8::cvt<std::wstring>(response_message.payload(i).message())); 209 } 210 } 211 212 213 static void create_simple_message_request(std::wstring command, NSCAPI::nagiosReturn code, std::wstring &msg, std::wstring &perf, std::string &request) { 214 PluginCommand::ResponseMessage message; 215 PluginCommand::Header *hdr = message.mutable_header(); 216 hdr->set_type(PluginCommand::Header_Type_RESPONSE); 217 hdr->set_version(PluginCommand::Header_Version_VERSION_1); 218 219 PluginCommand::Response *resp = message.add_payload(); 220 resp->set_command(to_string(command)); 221 resp->set_result(nagios_to_gpb(code)); 222 resp->set_version(PluginCommand::Response_Version_VERSION_1); 223 resp->set_message(to_string(msg)); 224 parse_performance_data(resp, perf); 225 226 message.SerializeToString(&request); 227 } 228 229 230 static void parse_simple_message(std::string &response, std::wstring &msg, std::wstring &perf) { 231 PluginCommand::ResponseMessage response_message; 232 response_message.ParseFromString(response); 233 234 235 if (response_message.payload_size() != 1) { 236 throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 237 } 238 PluginCommand::Response payload = response_message.payload().Get(0); 239 msg = utf8::cvt<std::wstring>(payload.message()); 240 perf = utf8::cvt<std::wstring>(build_performance_data(payload)); 241 } 242 185 243 static void parse_performance_data(PluginCommand::Response *resp, std::wstring &perf) { 186 187 244 boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring> tok(perf, boost::escaped_list_separator<wchar_t>(L'\\', L' ', L'\'')); 188 245 BOOST_FOREACH(std::wstring s, tok) { -
include/nscapi/nscapi_core_wrapper.cpp
rafd42f1 r2c95d22 106 106 * @return The returned status of the command 107 107 */ 108 NSCAPI::nagiosReturn nscapi::core_wrapper:: InjectCommandRAW(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)108 NSCAPI::nagiosReturn nscapi::core_wrapper::query(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len) 109 109 { 110 110 if (!fNSAPIInject) … … 113 113 } 114 114 115 NSCAPI::nagiosReturn nscapi::core_wrapper::exec_command(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len) 116 { 117 if (!fNSAPIExecCommand) 118 throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); 119 return fNSAPIExecCommand(command, request, request_len, response, response_len); 120 } 121 115 122 void nscapi::core_wrapper::DestroyBuffer(char**buffer) { 116 123 if (!fNSAPIDestroyBuffer) … … 118 125 return fNSAPIDestroyBuffer(buffer); 119 126 } 127 128 129 void nscapi::core_wrapper::submit_simple_message(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring & message, std::wstring & perf) { 130 std::string request; 131 nscapi::functions::create_simple_message_request(command, code, message, perf, request); 132 NSCAPI::nagiosReturn ret = NotifyChannel(channel, command, code, request); 133 } 134 120 135 121 136 NSCAPI::errorReturn nscapi::core_wrapper::NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::string result) { … … 134 149 * @return The return of the command 135 150 */ 136 NSCAPI::nagiosReturn nscapi::core_wrapper:: InjectSimpleCommand(const std::wstring command, const std::list<std::wstring>argument, std::wstring & msg, std::wstring & perf)151 NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & argument, std::wstring & msg, std::wstring & perf) 137 152 { 138 153 if (!fNSAPIInject) 139 154 throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); 140 141 155 std::string response; 142 NSCAPI::nagiosReturn ret = InjectCommand(command, argument, response);156 NSCAPI::nagiosReturn ret = simple_query(command, argument, response); 143 157 if (!response.empty()) { 144 158 PluginCommand::ResponseMessage rsp_msg; … … 163 177 * @return The return of the command 164 178 */ 165 NSCAPI::nagiosReturn nscapi::core_wrapper:: InjectCommand(const std::wstring command, const std::list<std::wstring>argument, std::string & result)179 NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & argument, std::string & result) 166 180 { 167 181 if (!fNSAPIInject) 168 182 throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); 169 170 183 171 184 PluginCommand::RequestMessage message; … … 184 197 message.SerializeToString(&request); 185 198 186 return InjectCommand(command.c_str(), request, result);187 } 188 189 NSCAPI::nagiosReturn nscapi::core_wrapper:: InjectCommand(const std::wstring command, std::stringrequest, std::string & result)199 return query(command.c_str(), request, result); 200 } 201 202 NSCAPI::nagiosReturn nscapi::core_wrapper::query(const std::wstring & command, const std::string & request, std::string & result) 190 203 { 191 204 if (!fNSAPIInject) … … 193 206 char *buffer = NULL; 194 207 unsigned int buffer_size = 0; 195 NSCAPI::nagiosReturn retC = InjectCommandRAW(command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);208 NSCAPI::nagiosReturn retC = query(command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size); 196 209 197 210 if (buffer_size > 0 && buffer != NULL) { … … 215 228 return retC; 216 229 } 217 /** 218 * A wrapper around the InjetCommand that is simpler to use. 219 * Parses a string by splitting and makes the array and also manages return buffers and such. 220 * @param command The command to execute 221 * @param buffer The buffer to split 222 * @param spliwchar_t The char to use as splitter 223 * @param message The return message buffer 224 * @param perf The return performance data buffer 225 * @return The result of the command 226 */ 227 NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSplitAndCommand(const wchar_t* command, wchar_t* buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf) 228 { 229 if (!fNSAPIInject) 230 throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); 231 232 std::wstring args = std::wstring(buffer); 233 boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(args, boost::escaped_list_separator<wchar_t>(L'\\', splitChar, L'\"')); 234 std::list<std::wstring> arglist; 235 BOOST_FOREACH(std::wstring s, tok) 236 arglist.push_back(s); 237 return InjectSimpleCommand(command, arglist, message, perf); 238 } 239 /** 240 * A wrapper around the InjetCommand that is simpler to use. 241 * @param command The command to execute 242 * @param buffer The buffer to split 243 * @param spliwchar_t The char to use as splitter 244 * @param message The return message buffer 245 * @param perf The return performance data buffer 246 * @return The result of the command 247 */ 248 NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSplitAndCommand(const std::wstring command, const std::wstring buffer, wchar_t spliwchar_t, std::wstring & message, std::wstring & perf, bool escape) { 249 if (!fNSAPIInject) 250 throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); 251 std::list<std::wstring> arglist; 252 if (escape) { 253 boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(buffer, boost::escaped_list_separator<wchar_t>(L'\\', spliwchar_t, L'\"')); 254 BOOST_FOREACH(std::wstring s, tok) 255 arglist.push_back(s); 256 } else { 257 std::wstring split; 258 split.push_back(spliwchar_t); 259 boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(buffer, boost::escaped_list_separator<wchar_t>(_T(""), split, _T("\""))); 260 BOOST_FOREACH(std::wstring s, tok) 261 arglist.push_back(s); 262 } 263 return InjectSimpleCommand(command.c_str(), arglist, message, perf); 264 } 265 266 NSCAPI::nagiosReturn nscapi::core_wrapper::InjectNRPECommand(const std::wstring command, const std::wstring buffer, std::wstring & message, std::wstring & perf) { 230 231 232 NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query_from_nrpe(const std::wstring command, const std::wstring & buffer, std::wstring & message, std::wstring & perf) { 267 233 if (!fNSAPIInject) 268 234 throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); … … 271 237 BOOST_FOREACH(std::wstring s, tok) 272 238 arglist.push_back(s); 273 return InjectSimpleCommand(command.c_str(), arglist, message, perf); 274 } 239 return simple_query(command, arglist, message, perf); 240 } 241 242 NSCAPI::nagiosReturn nscapi::core_wrapper::exec_command(const std::wstring command, std::string request, std::string & result) { 243 char *buffer = NULL; 244 unsigned int buffer_size = 0; 245 NSCAPI::nagiosReturn retC = exec_command(command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size); 246 247 if (buffer_size > 0 && buffer != NULL) { 248 result = std::string(buffer, buffer_size); 249 } 250 251 DestroyBuffer(&buffer); 252 switch (retC) { 253 case NSCAPI::returnIgnored: 254 CORE_LOG_MESSAGE_STD(_T("No handler for command '") + command + _T("'.")); 255 break; 256 case NSCAPI::returnOK: 257 case NSCAPI::returnCRIT: 258 case NSCAPI::returnWARN: 259 case NSCAPI::returnUNKNOWN: 260 break; 261 default: 262 throw nscapi::nscapi_exception(_T("Unknown return code when injecting: ") + std::wstring(command)); 263 } 264 return retC; 265 } 266 NSCAPI::nagiosReturn nscapi::core_wrapper::exec_simple_command(const std::wstring command, const std::list<std::wstring> &argument, std::list<std::wstring> & result) { 267 std::string request, response; 268 nscapi::functions::create_simple_exec_request(command, argument, request); 269 NSCAPI::nagiosReturn ret = exec_command(command, request, response); 270 nscapi::functions::parse_simple_exec_result(response, result); 271 return ret; 272 } 273 275 274 276 275 … … 609 608 //fNSAPIExit = (nscapi::core_api::lpNSAPIExit)f(_T("NSAPIExit")); 610 609 fNSAPIInject = (nscapi::core_api::lpNSAPIInject)f(_T("NSAPIInject")); 610 fNSAPIExecCommand = (nscapi::core_api::lpNSAPIExecCommand)f(_T("NSAPIExecCommand")); 611 611 fNSAPIDestroyBuffer = (nscapi::core_api::lpNSAPIDestroyBuffer)f(_T("NSAPIDestroyBuffer")); 612 612 fNSAPINotify = (nscapi::core_api::lpNSAPINotify)f(_T("NSAPINotify")); -
include/nscapi/nscapi_core_wrapper.hpp
r04ef932 r2c95d22 49 49 nscapi::core_api::lpNSAPIExit fNSAPIExit; 50 50 nscapi::core_api::lpNSAPIInject fNSAPIInject; 51 nscapi::core_api::lpNSAPIExecCommand fNSAPIExecCommand; 51 52 nscapi::core_api::lpNSAPIDestroyBuffer fNSAPIDestroyBuffer; 52 53 nscapi::core_api::lpNSAPINotify fNSAPINotify; … … 132 133 133 134 void Message(int msgType, std::string file, int line, std::wstring message); 134 NSCAPI::nagiosReturn InjectCommandRAW(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len);135 135 void DestroyBuffer(char**buffer); 136 NSCAPI::nagiosReturn InjectCommand(const std::wstring command, std::string request, std::string & result); 137 NSCAPI::nagiosReturn InjectCommand(const std::wstring command, const std::list<std::wstring> argument, std::string & result); 138 NSCAPI::nagiosReturn InjectSimpleCommand(const std::wstring command, const std::list<std::wstring> argument, std::wstring & message, std::wstring & perf); 136 NSCAPI::nagiosReturn query(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len); 137 NSCAPI::nagiosReturn query(const std::wstring & command, const std::string & request, std::string & result); 138 NSCAPI::nagiosReturn simple_query(const std::wstring command, const std::list<std::wstring> & argument, std::wstring & message, std::wstring & perf); 139 NSCAPI::nagiosReturn simple_query(const std::wstring command, const std::list<std::wstring> & argument, std::string & result); 140 NSCAPI::nagiosReturn simple_query_from_nrpe(const std::wstring command, const std::wstring & buffer, std::wstring & message, std::wstring & perf); 141 142 NSCAPI::nagiosReturn exec_command(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len); 143 NSCAPI::nagiosReturn exec_command(const std::wstring command, std::string request, std::string & result); 144 NSCAPI::nagiosReturn exec_simple_command(const std::wstring command, const std::list<std::wstring> &argument, std::list<std::wstring> & result); 145 146 void submit_simple_message(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring & message, std::wstring & perf); 139 147 NSCAPI::errorReturn NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::string result); 140 NSCAPI::nagiosReturn InjectSplitAndCommand(const wchar_t* command, wchar_t* buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf);141 NSCAPI::nagiosReturn InjectSplitAndCommand(const std::wstring command, const std::wstring buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf, bool escape = false);142 NSCAPI::nagiosReturn InjectNRPECommand(const std::wstring command, const std::wstring buffer, std::wstring & message, std::wstring & perf);148 //NSCAPI::nagiosReturn InjectCommand(const std::wstring command, const std::list<std::wstring> argument, std::string & result); 149 //NSCAPI::nagiosReturn InjectSplitAndCommand(const wchar_t* command, wchar_t* buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf); 150 //NSCAPI::nagiosReturn InjectSplitAndCommand(const std::wstring command, const std::wstring buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf, bool escape = false); 143 151 void StopService(void); 144 152 void Exit(void); -
include/nscapi/nscapi_plugin_wrapper.cpp
r39c73cd r2c95d22 257 257 258 258 NSCAPI::nagiosReturn nscapi::impl::simple_command_line_exec::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response) { 259 nscapi::functions::decoded_simple_command_data data = nscapi::functions::p rocess_simple_command_line_exec_request(char_command, request);259 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 260 260 std::wstring result; 261 NSCAPI::nagiosReturn ret = commandLineExec(data.command, data.args_vector, result); 262 return nscapi::functions::process_simple_command_line_exec_result(data.command, ret, result, response); 261 NSCAPI::nagiosReturn ret = commandLineExec(data.command, data.args, result); 262 if (ret == NSCAPI::returnIgnored) 263 return NSCAPI::returnIgnored; 264 return nscapi::functions::create_simple_exec_result(data.command, ret, result, response); 263 265 } 264 266 -
include/nscapi/nscapi_plugin_wrapper.hpp
r39c73cd r2c95d22 120 120 public: 121 121 NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 122 virtual NSCAPI::nagiosReturn commandLineExec(const std::wstring &command, std:: vector<std::wstring> &arguments, std::wstring &result) = 0;122 virtual NSCAPI::nagiosReturn commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) = 0; 123 123 }; 124 124 -
libs/protobuf/exec.proto
r39c73cd r2c95d22 68 68 required Code result = 9; 69 69 required string message = 10; 70 repeated ResponseData perf = 12;71 72 70 } 73 71 -
modules/CheckExternalScripts/CheckExternalScripts.cpp
rd66ccee r2c95d22 180 180 std::wstring message; 181 181 try { 182 return GET_CORE()-> InjectCommand(cd.command, args, response);182 return GET_CORE()->simple_query(cd.command, args, response); 183 183 } catch (boost::escaped_list_error &e) { 184 184 NSC_LOG_MESSAGE(_T("Failed to parse alias expression: ") + strEx::string_to_wstring(e.what())); 185 185 NSC_LOG_MESSAGE(_T("We will now try parsing the old syntax instead...")); 186 return GET_CORE()-> InjectCommand(cd.command, args, response);186 return GET_CORE()->simple_query(cd.command, args, response); 187 187 } 188 188 } else { -
modules/CheckHelpers/CheckHelpers.cpp
re11d494 r2c95d22 95 95 } 96 96 std::wstring new_command = arguments.front(); arguments.pop_front(); 97 GET_CORE()-> InjectSimpleCommand(new_command, arguments, message, perf);97 GET_CORE()->simple_query(new_command, arguments, message, perf); 98 98 return NSCAPI::returnOK; 99 99 } else if (command == _T("checkalwayscritical")) { … … 103 103 } 104 104 std::wstring new_command = arguments.front(); arguments.pop_front(); 105 GET_CORE()-> InjectSimpleCommand(new_command, arguments, message, perf);105 GET_CORE()->simple_query(new_command, arguments, message, perf); 106 106 return NSCAPI::returnCRIT; 107 107 } else if (command == _T("checkalwayswarning")) { … … 111 111 } 112 112 std::wstring new_command = arguments.front(); arguments.pop_front(); 113 GET_CORE()-> InjectSimpleCommand(new_command, arguments, message, perf);113 GET_CORE()->simple_query(new_command, arguments, message, perf); 114 114 return NSCAPI::returnWARN; 115 115 } else if (command == _T("checkok")) { … … 159 159 std::list<std::wstring> sub_args; 160 160 std::wstring tMsg, tPerf; 161 NSCAPI::nagiosReturn tRet = GET_CORE()-> InjectSimpleCommand((*cit2).first.c_str(), (*cit2).second, tMsg, tPerf);161 NSCAPI::nagiosReturn tRet = GET_CORE()->simple_query((*cit2).first.c_str(), (*cit2).second, tMsg, tPerf); 162 162 returnCode = nscapi::plugin_helper::maxState(returnCode, tRet); 163 163 if (!message.empty()) … … 233 233 std::list<std::wstring> cmd_args_l(cmd_args.begin(), cmd_args.end()); 234 234 235 NSCAPI::nagiosReturn tRet = GET_CORE()-> InjectSimpleCommand(command, cmd_args_l, msg, perf);235 NSCAPI::nagiosReturn tRet = GET_CORE()->simple_query(command, cmd_args_l, msg, perf); 236 236 switch (tRet) { 237 237 case NSCAPI::returnOK: … … 252 252 public: 253 253 void proc(std::wstring command, std::list<std::wstring> arguments) { 254 code = GET_CORE()-> InjectSimpleCommand(command, arguments, msg, perf);254 code = GET_CORE()->simple_query(command, arguments, msg, perf); 255 255 } 256 256 std::wstring msg; -
modules/LUAScript/script_wrapper.hpp
r04ef932 r2c95d22 207 207 std::wstring message; 208 208 std::wstring perf; 209 NSCAPI::nagiosReturn ret = GET_CORE()-> InjectSimpleCommand(command, arguments, message, perf);209 NSCAPI::nagiosReturn ret = GET_CORE()->simple_query(command, arguments, message, perf); 210 210 push_code(L, ret); 211 211 lua_pushstring(L, strEx::wstring_to_string(message).c_str()); -
modules/NRPEClient/NRPEClient.cpp
r65a2940 r2c95d22 238 238 } 239 239 240 int NRPEClient::commandLineExec(const std::wstring &command, std::vector<std::wstring> &arguments, std::wstring &result) { 241 NSC_DEBUG_MSG_STD(_T("===> ") + command); 240 int NRPEClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 242 241 if (command != _T("query") && command != _T("help")) 243 return NSCAPI::return UNKNOWN;242 return NSCAPI::returnIgnored; 244 243 try { 245 244 NRPEClient::nrpe_connection_data command_data; … … 250 249 add_options(desc, command_data); 251 250 251 std::vector<std::wstring> vargs(arguments.begin(), arguments.end()); 252 252 po::positional_options_description p; 253 253 p.add("arguments", -1); 254 po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>( arguments).options(desc).positional(p).run();254 po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(vargs).options(desc).positional(p).run(); 255 255 po::store(parsed, vm); 256 256 po::notify(vm); -
modules/NRPEClient/NRPEClient.h
r65a2940 r2c95d22 126 126 bool hasMessageHandler(); 127 127 NSCAPI::nagiosReturn handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf); 128 int commandLineExec(const std::wstring &command, std:: vector<std::wstring> &arguments, std::wstring &result);128 int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 129 129 std::wstring getConfigurationMeta(); 130 130 -
modules/NRPEServer/handler_impl.cpp
r7443b58 r2c95d22 33 33 try { 34 34 NSC_DEBUG_MSG_STD(_T("Running command: ") + cmd.first); 35 ret = nscapi::plugin_singleton->get_core()-> InjectNRPECommand(cmd.first, cmd.second, msg, perf);35 ret = nscapi::plugin_singleton->get_core()->simple_query_from_nrpe(cmd.first, cmd.second, msg, perf); 36 36 NSC_DEBUG_MSG_STD(_T("Running command: ") + cmd.first + _T(" = ") + msg); 37 37 } catch (...) { -
modules/NSClientServer/handler_impl.cpp
rb8c44b4 r2c95d22 122 122 123 123 std::wstring message, perf; 124 NSCAPI::nagiosReturn ret = nscapi::plugin_singleton->get_core()-> InjectSimpleCommand(cmd.first.c_str(), args, message, perf);124 NSCAPI::nagiosReturn ret = nscapi::plugin_singleton->get_core()->simple_query(cmd.first.c_str(), args, message, perf); 125 125 if (!nscapi::plugin_helper::isNagiosReturnCode(ret)) { 126 126 if (message.empty()) -
modules/PythonScript/PythonScript.cpp
r39c73cd r2c95d22 67 67 .def("register_key", &script_wrapper::settings_wrapper::settings_register_key) 68 68 ; 69 class_<script_wrapper::function_wrapper, boost::shared_ptr<script_wrapper::function_wrapper> >(" Functions", no_init)69 class_<script_wrapper::function_wrapper, boost::shared_ptr<script_wrapper::function_wrapper> >("Registry", no_init) 70 70 .def("get",&script_wrapper::function_wrapper::create) 71 71 .staticmethod("get") 72 72 .def("create",&script_wrapper::function_wrapper::create) 73 73 .staticmethod("create") 74 .def(" register", &script_wrapper::function_wrapper::register_function)75 .def(" register_simple", &script_wrapper::function_wrapper::register_simple_function)74 .def("function", &script_wrapper::function_wrapper::register_function) 75 .def("simple_function", &script_wrapper::function_wrapper::register_simple_function) 76 76 .def("cmdline", &script_wrapper::function_wrapper::register_cmdline) 77 77 .def("simple_cmdline", &script_wrapper::function_wrapper::register_simple_cmdline) 78 .def("subscri be", &script_wrapper::function_wrapper::subscribe_function)79 .def("s ubscribe_simple", &script_wrapper::function_wrapper::subscribe_simple_function)78 .def("subscription", &script_wrapper::function_wrapper::subscribe_function) 79 .def("simple_subscription", &script_wrapper::function_wrapper::subscribe_simple_function) 80 80 ; 81 81 class_<script_wrapper::command_wrapper, boost::shared_ptr<script_wrapper::command_wrapper> >("Core", no_init) … … 99 99 ; 100 100 def("log", script_wrapper::log_msg); 101 def("get_alias", script_wrapper::get_alias); 101 102 } 102 103 … … 143 144 144 145 bool PythonScript::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 146 alias_ = alias; 145 147 NSC_DEBUG_MSG_STD(_T("LoadEx in PythonScript as ") + alias); 146 148 try { … … 230 232 } 231 233 bool PythonScript::hasMessageHandler() { 232 return false; 233 } 234 return true; 235 } 236 bool PythonScript::hasNotificationHandler() { 237 return true; 238 } 239 234 240 235 241 … … 262 268 std::string cmd = utf8::cvt<std::string>(char_command); 263 269 if (inst->has_cmdline(cmd)) { 264 return inst-> exec_cmdline(cmd, request, response);270 return inst->handle_exec(cmd, request, response); 265 271 } 266 272 if (inst->has_simple_cmdline(cmd)) { 267 nscapi::functions::decoded_simple_command_data data = nscapi::functions::p rocess_simple_command_line_exec_request(char_command, request);273 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 268 274 std::wstring result; 269 NSCAPI::nagiosReturn ret = inst-> exec_simple_cmdline(cmd, data.args, result);270 return nscapi::functions:: process_simple_command_line_exec_result(data.command, ret, result, response);275 NSCAPI::nagiosReturn ret = inst->handle_simple_exec(cmd, data.args, result); 276 return nscapi::functions::create_simple_exec_result(data.command, ret, result, response); 271 277 } 272 278 return NSCAPI::returnIgnored; … … 295 301 } 296 302 303 304 NSCAPI::nagiosReturn PythonScript::handleRAWNotification(const std::wstring &channel, const std::wstring &command, NSCAPI::nagiosReturn code, std::string &request) { 305 boost::shared_ptr<script_wrapper::function_wrapper> inst = script_wrapper::function_wrapper::create(); 306 std::string cmd = utf8::cvt<std::string>(command); 307 std::string chnl = utf8::cvt<std::string>(channel); 308 if (inst->has_message_handler(chnl)) { 309 return inst->handle_message(chnl, cmd, request); 310 } 311 if (inst->has_simple_message_handler(chnl)) { 312 std::wstring msg, perf; 313 nscapi::functions::parse_simple_message(request, msg, perf); 314 return inst->handle_simple_message(chnl, cmd, code, msg, perf); 315 } 316 return NSCAPI::returnIgnored; 317 } 318 297 319 NSC_WRAP_DLL(); 298 320 NSC_WRAPPERS_MAIN_DEF(gPythonScript); … … 300 322 NSC_WRAPPERS_HANDLE_CMD_DEF(gPythonScript); 301 323 NSC_WRAPPERS_CLI_DEF(gPythonScript); 324 NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF(gPythonScript); -
modules/PythonScript/PythonScript.h
r65a2940 r2c95d22 21 21 NSC_WRAPPERS_MAIN(); 22 22 NSC_WRAPPERS_CLI(); 23 NSC_WRAPPERS_CHANNELS(); 23 24 24 25 #include <config.h> … … 49 50 typedef std::list<boost::shared_ptr<python_script> > instance_list_type; 50 51 instance_list_type instances_; 52 std::wstring alias_; 51 53 52 54 public: … … 59 61 bool unloadModule(); 60 62 bool reload(std::wstring &msg); 63 std::wstring get_alias() { 64 return alias_; 65 } 61 66 62 67 std::wstring getModuleName() { … … 73 78 bool hasCommandHandler(); 74 79 bool hasMessageHandler(); 80 bool hasNotificationHandler(); 75 81 bool loadScript(std::wstring alias, std::wstring script); 76 82 //NSCAPI::nagiosReturn handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf); … … 78 84 NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 79 85 NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 86 NSCAPI::nagiosReturn handleRAWNotification(const std::wstring &channel, const std::wstring &command, NSCAPI::nagiosReturn code, std::string &request); 80 87 81 88 //NSCAPI::nagiosReturn RunLUA(const unsigned int argLen, wchar_t **char_args, std::wstring &message, std::wstring &perf); -
modules/PythonScript/script_wrapper.cpp
r39c73cd r2c95d22 3 3 #include <strEx.h> 4 4 #include "script_wrapper.hpp" 5 #include "PythonScript.h" 5 6 6 7 using namespace boost::python; 7 8 8 9 boost::shared_ptr<script_wrapper::functions> script_wrapper::functions::instance; 10 11 extern PythonScript gPythonScript; 9 12 10 13 11 14 void script_wrapper::log_msg(std::wstring x) { 12 15 NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(x)); 16 } 17 std::string script_wrapper::get_alias() { 18 return utf8::cvt<std::string>(gPythonScript.get_alias()); 13 19 } 14 20 … … 21 27 PyErr_Clear(); 22 28 } 29 30 void script_wrapper::function_wrapper::subscribe_simple_function(std::string channel, PyObject* callable) { 31 functions::get()->simple_handler[channel] = callable; 32 } 33 void script_wrapper::function_wrapper::subscribe_function(std::string channel, PyObject* callable) { 34 functions::get()->normal_handler[channel] = callable; 35 } 36 37 23 38 void script_wrapper::function_wrapper::register_simple_function(std::string name, PyObject* callable, std::string desc) { 24 39 try { … … 113 128 } 114 129 115 int script_wrapper::function_wrapper:: exec_cmdline(const std::string cmd, const std::string &request, std::string &response) const {130 int script_wrapper::function_wrapper::handle_exec(const std::string cmd, const std::string &request, std::string &response) const { 116 131 try { 117 132 functions::function_map_type::iterator it = functions::get()->normal_cmdline.find(cmd); … … 136 151 } 137 152 138 int script_wrapper::function_wrapper:: exec_simple_cmdline(const std::string cmd, std::list<std::wstring> arguments, std::wstring &result) const {153 int script_wrapper::function_wrapper::handle_simple_exec(const std::string cmd, std::list<std::wstring> arguments, std::wstring &result) const { 139 154 try { 140 155 functions::function_map_type::iterator it = functions::get()->simple_cmdline.find(cmd); … … 145 160 } 146 161 147 boost::python::list l; 148 BOOST_FOREACH(std::wstring a, arguments) { 149 l.append(utf8::cvt<std::string>(a)); 150 } 151 tuple ret = boost::python::call<tuple>(it->second, l); 162 tuple ret = boost::python::call<tuple>(it->second, convert(arguments)); 152 163 if (ret.ptr() == Py_None) { 153 164 result = _T("None"); … … 167 178 } 168 179 180 181 bool script_wrapper::function_wrapper::has_message_handler(const std::string channel) { 182 return functions::get()->normal_handler.find(channel) != functions::get()->normal_handler.end(); 183 } 184 bool script_wrapper::function_wrapper::has_simple_message_handler(const std::string channel) { 185 return functions::get()->simple_handler.find(channel) != functions::get()->simple_handler.end(); 186 } 187 188 int script_wrapper::function_wrapper::handle_message(const std::string channel, const std::string command, std::string &message) const { 189 try { 190 functions::function_map_type::iterator it = functions::get()->normal_handler.find(channel); 191 if (it == functions::get()->normal_handler.end()) { 192 NSC_LOG_ERROR_STD(_T("Failed to find python handler: ") + utf8::cvt<std::wstring>(channel)); 193 return NSCAPI::returnIgnored; 194 } 195 object ret = boost::python::call<object>(it->second, channel, command, message); 196 if (ret.ptr() == Py_None) { 197 return NSCAPI::returnUNKNOWN; 198 } 199 return extract<int>(ret); 200 } catch( error_already_set e) { 201 log_exception(); 202 return NSCAPI::returnUNKNOWN; 203 } 204 } 205 206 int script_wrapper::function_wrapper::handle_simple_message(const std::string channel, const std::string command, int code, std::wstring &msg, std::wstring &perf) const { 207 try { 208 functions::function_map_type::iterator it = functions::get()->simple_handler.find(channel); 209 if (it == functions::get()->simple_handler.end()) { 210 NSC_LOG_ERROR_STD(_T("Failed to find python handler: ") + utf8::cvt<std::wstring>(channel)); 211 return NSCAPI::returnIgnored; 212 } 213 214 object ret = boost::python::call<object>(it->second, channel, command, code, msg, perf); 215 if (ret.ptr() == Py_None) { 216 return NSCAPI::returnUNKNOWN; 217 } 218 return extract<int>(ret); 219 } catch( error_already_set e) { 220 log_exception(); 221 return NSCAPI::returnUNKNOWN; 222 } 223 } 224 225 226 227 228 229 169 230 bool script_wrapper::function_wrapper::has_cmdline(const std::string command) { 170 231 return functions::get()->normal_cmdline.find(command) != functions::get()->normal_cmdline.end(); … … 189 250 190 251 191 std::list<std::wstring> script_wrapper::co mmand_wrapper::convert(list lst) {252 std::list<std::wstring> script_wrapper::convert(list lst) { 192 253 std::list<std::wstring> ret; 193 254 for (int i = 0;i<len(lst);i++) … … 195 256 return ret; 196 257 } 258 list script_wrapper::convert(std::list<std::wstring> lst) { 259 list ret; 260 BOOST_FOREACH(std::wstring s, lst) { 261 ret.append(utf8::cvt<std::string>(s)); 262 } 263 return ret; 264 } 265 266 void script_wrapper::command_wrapper::simple_submit(std::string channel, std::string command, status code, std::string message, std::string perf) { 267 core->submit_simple_message(utf8::cvt<std::wstring>(channel), utf8::cvt<std::wstring>(command), code, utf8::cvt<std::wstring>(message), utf8::cvt<std::wstring>(perf)); 268 } 269 270 197 271 tuple script_wrapper::command_wrapper::simple_query(std::string command, list args) { 198 272 std::wstring msg, perf; 199 int ret = core-> InjectSimpleCommand(utf8::cvt<std::wstring>(command), convert(args), msg, perf);273 int ret = core->simple_query(utf8::cvt<std::wstring>(command), convert(args), msg, perf); 200 274 return make_tuple(ret,utf8::cvt<std::string>(msg), utf8::cvt<std::string>(perf)); 201 275 } 202 276 tuple script_wrapper::command_wrapper::query(std::string command, std::string request) { 203 277 std::string response; 204 int ret = core-> InjectCommand(utf8::cvt<std::wstring>(command), request, response);278 int ret = core->query(utf8::cvt<std::wstring>(command), request, response); 205 279 return make_tuple(ret,response); 280 } 281 282 object script_wrapper::command_wrapper::simple_exec(std::string command, list args) { 283 try { 284 std::list<std::wstring> result; 285 int ret = core->exec_simple_command(utf8::cvt<std::wstring>(command), convert(args), result); 286 return make_tuple(ret, convert(result)); 287 } catch (const std::exception &e) { 288 NSC_LOG_ERROR_STD(_T("Failed to execute ") + utf8::cvt<std::wstring>(command) + _T(": ") + utf8::cvt<std::wstring>(e.what())); 289 return object(); 290 } catch (...) { 291 NSC_LOG_ERROR_STD(_T("Failed to execute ") + utf8::cvt<std::wstring>(command)); 292 return object(); 293 } 294 } 295 tuple script_wrapper::command_wrapper::exec(std::string command, std::string request) { 296 std::string response; 297 int ret = core->exec_command(utf8::cvt<std::wstring>(command), request, response); 298 return make_tuple(ret, response); 206 299 } 207 300 -
modules/PythonScript/script_wrapper.hpp
r39c73cd r2c95d22 16 16 void log_exception(); 17 17 void log_msg(std::wstring x); 18 std::string get_alias(); 19 20 std::list<std::wstring> convert(boost::python::list lst); 21 boost::python::list convert(std::list<std::wstring> lst); 22 18 23 19 24 struct functions { … … 25 30 function_map_type normal_cmdline; 26 31 32 function_map_type simple_handler; 33 function_map_type normal_handler; 27 34 28 35 static boost::shared_ptr<functions> instance; … … 49 56 function_wrapper(nscapi::core_wrapper* core) : core(core) {} 50 57 typedef std::map<std::string,PyObject*> function_map_type; 51 //function_map_type simple_functions; 52 //function_map_type functions; 53 typedef boost::python::tuple simple_return; 58 //typedef boost::python::tuple simple_return; 54 59 55 60 … … 62 67 void register_simple_function(std::string name, PyObject* callable, std::string desc); 63 68 void register_function(std::string name, PyObject* callable, std::string desc); 64 void subscribe_function( ) {}65 void subscribe_simple_function( ) {}69 void subscribe_function(std::string channel, PyObject* callable); 70 void subscribe_simple_function(std::string channel, PyObject* callable); 66 71 int exec_simple(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const; 67 72 int exec(const std::string wcmd, const std::string &request, std::string &response) const; … … 69 74 bool has_simple(const std::string command); 70 75 71 int exec_simple_cmdline(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &result) const;72 int exec_cmdline(const std::string wcmd, const std::string &request, std::string &response) const;76 int handle_simple_exec(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &result) const; 77 int handle_exec(const std::string wcmd, const std::string &request, std::string &response) const; 73 78 bool has_cmdline(const std::string command); 74 79 bool has_simple_cmdline(const std::string command); 80 81 82 int handle_simple_message(const std::string channel, const std::string wcmd, int code, std::wstring &msg, std::wstring &perf) const; 83 int handle_message(const std::string channel, const std::string wcmd, std::string &message) const; 84 bool has_message_handler(const std::string command); 85 bool has_simple_message_handler(const std::string command); 75 86 76 87 std::wstring get_commands(); … … 93 104 } 94 105 95 std::list<std::wstring> convert(boost::python::list lst);96 106 tuple simple_query(std::string command, boost::python::list args); 97 107 tuple query(std::string command, std::string request); 98 void simple_exec() {}99 void exec() {}100 void simple_submit( ) {}108 object simple_exec(std::string command, boost::python::list args); 109 tuple exec(std::string command, std::string request); 110 void simple_submit(std::string channel, std::string command, status code, std::string message, std::string perf); 101 111 void submit() {} 102 112 }; -
modules/Scheduler/Scheduler.cpp
re11d494 r2c95d22 156 156 try { 157 157 std::string response; 158 NSCAPI::nagiosReturn code = GET_CORE()-> InjectCommand(item.command.c_str(), item.arguments, response);158 NSCAPI::nagiosReturn code = GET_CORE()->simple_query(item.command.c_str(), item.arguments, response); 159 159 if (nscapi::report::matches(item.report, code)) { 160 160 GET_CORE()->NotifyChannel(item.channel, item.alias, code, response); -
scripts/python/test.py
r39c73cd r2c95d22 1 from NSCP import Settings, Functions, Core, log, status1 from NSCP import Settings, Registry, Core, log, status, get_alias 2 2 3 3 core = Core.get() 4 5 4 6 5 def get_help(arguments): 7 6 return (status.OK, 'help: Get help') 8 7 8 9 def test_cmd(arguments): 10 global prefix 11 log('inside test_cmd') 12 return (status.OK, 'The command works: %s (%d)'%(prefix, len(arguments))) 13 14 def test_channel(channel, command, code, message, perf): 15 log('inside test_channel: %s'%channel) 16 log('Data: %d %s %s'%(code, message, perf)) 17 9 18 prefix = 'py_' 10 19 def test(arguments): 20 global prefix 11 21 log('inside test') 12 22 for a in arguments: … … 49 59 50 60 def init(alias): 61 global prefix 51 62 if alias: 52 63 prefix = '%s_'%alias 53 64 54 log(' Hello World')65 log('Script: test.py with alias: %s from %s'%(alias, get_alias())) 55 66 56 67 conf = Settings.get() … … 60 71 61 72 log('Testing to register a function') 62 fun = Functions.get()63 fun.register_simple('%stest'%prefix, test, 'This is a sample command')64 fun.register_simple('%snormal'%prefix, normal, 'This is a sample command')65 fun.register_simple('%snop'%prefix, no_perf, 'No performance data')66 fun.register_simple('%snom'%prefix, no_msg, 'No performance data')67 fun.register_simple('%snor'%prefix, no_ret, 'No performance data')73 reg = Registry.get() 74 reg.simple_function('%stest'%prefix, test, 'This is a sample command') 75 reg.simple_function('%snormal'%prefix, normal, 'This is a sample command') 76 reg.simple_function('%snop'%prefix, no_perf, 'No performance data') 77 reg.simple_function('%snom'%prefix, no_msg, 'No performance data') 78 reg.simple_function('%snor'%prefix, no_ret, 'No performance data') 68 79 69 fun.simple_cmdline('help', get_help) 80 reg.simple_cmdline('help', get_help) 81 reg.simple_cmdline('%stest'%prefix, test_cmd) 82 83 reg.simple_subscription('%stest'%prefix, test_channel) 84 85 (ret, list) = core.simple_submit('%stest'%prefix, 'test.py', status.WARNING, 'hello', '') 86 87 (ret, list) = core.simple_exec('%stest'%prefix, ['a', 'b', 'c']) 88 for l in list: 89 log('-- %s --'%l) 70 90 71 91 log('Testing to register settings keys') -
service/NSClient++.cpp
r65a2940 r2c95d22 825 825 826 826 827 int NSClientT::command_line_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp) { 827 828 /** 829 * Unload all plug-ins (in reversed order) 830 */ 831 void NSClientT::unloadPlugins(bool unloadLoggers) { 832 { 833 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 834 if (!writeLock.owns_lock()) { 835 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (003).")); 836 return; 837 } 838 if (unloadLoggers) 839 logger_master_.remove_all_plugins(); 840 } 841 { 842 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 843 if (!readLock.owns_lock()) { 844 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (004).")); 845 return; 846 } 847 for (pluginList::reverse_iterator it = plugins_.rbegin(); it != plugins_.rend(); ++it) { 848 plugin_type p = *it; 849 if (!p) 850 continue; 851 try { 852 if (unloadLoggers || !p->hasMessageHandler()) { 853 LOG_DEBUG_CORE_STD(_T("Unloading plugin: ") + p->getModule() + _T("...")); 854 p->unload(); 855 } else { 856 LOG_DEBUG_CORE_STD(_T("Skipping log plugin: ") + p->getModule() + _T("...")); 857 } 858 } catch(NSPluginException e) { 859 LOG_ERROR_CORE_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); 860 } catch(...) { 861 LOG_ERROR_CORE_STD(_T("Unknown exception raised when unloading plugin")); 862 } 863 } 864 } 865 { 866 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 867 if (!writeLock.owns_lock()) { 868 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (005).")); 869 return; 870 } 871 commands_.remove_all(); 872 for (pluginList::iterator it = plugins_.begin(); it != plugins_.end();) { 873 plugin_type p = (*it); 874 try { 875 if (!p && (unloadLoggers|| !p->isLoaded())) { 876 it = plugins_.erase(it); 877 //delete p; 878 continue; 879 } 880 } catch(NSPluginException e) { 881 LOG_ERROR_CORE_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); 882 } catch(...) { 883 LOG_ERROR_CORE(_T("Unknown exception raised when unloading plugin")); 884 } 885 it++; 886 } 887 } 888 } 889 890 void NSClientT::loadPlugins(NSCAPI::moduleLoadMode mode) { 891 bool hasBroken = false; 892 { 893 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 894 if (!readLock.owns_lock()) { 895 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (006).")); 896 return; 897 } 898 for (pluginList::iterator it=plugins_.begin(); it != plugins_.end();) { 899 LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + (*it)->getName() + _T(" as ") + (*it)->get_alias()); 900 try { 901 if (!(*it)->load_plugin(mode)) { 902 LOG_ERROR_CORE_STD(_T("Plugin refused to load: ") + (*it)->getModule()); 903 it = plugins_.erase(it); 904 } else 905 ++it; 906 } catch (NSPluginException e) { 907 it = plugins_.erase(it); 908 LOG_ERROR_CORE_STD(_T("Could not load plugin: ") + e.file_ + _T(": ") + e.error_); 909 } catch (...) { 910 it = plugins_.erase(it); 911 LOG_ERROR_CORE_STD(_T("Could not load plugin: ") + (*it)->getModule()); 912 } 913 } 914 } 915 logger_master_.all_plugins_loaded(); 916 } 917 /** 918 * Load and add a plugin to various internal structures 919 * @param plugin The plug-in instance to load. The pointer is managed by the 920 */ 921 NSClientT::plugin_type NSClientT::addPlugin(boost::filesystem::wpath file, std::wstring alias) { 922 { 923 LOG_DEBUG_CORE_STD(_T("addPlugin(") + file.string() + _T(" as ") + alias + _T(")")); 924 // Check if this is a duplicate plugin (if so return that instance) 925 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 926 if (!writeLock.owns_lock()) { 927 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (007a).")); 928 return plugin_type(); 929 } 930 931 BOOST_FOREACH(plugin_type plug, plugins_) { 932 if (plug->is_duplicate(file, alias)) { 933 LOG_DEBUG_CORE_STD(_T("Found duplicate plugin returning old ") + to_wstring(plug->get_id())); 934 return plug; 935 } 936 } 937 938 } 939 940 941 plugin_type plugin(new NSCPlugin(next_plugin_id_++, file, alias)); 942 plugin->load_dll(); 943 { 944 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 945 if (!writeLock.owns_lock()) { 946 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (007b).")); 947 return plugin; 948 } 949 950 plugins_.insert(plugins_.end(), plugin); 951 commands_.add_plugin(plugin); 952 channels_.add_plugin(plugin); 953 if (plugin->hasNotificationHandler()) { 954 channels_.register_listener(plugin->get_id(), _T("NSCA")); 955 } 956 if (plugin->hasMessageHandler()) 957 logger_master_.add_plugin(plugin); 958 //settings_manager::get_core()->register_key(_T("/modules"), plugin->getModule(), settings::settings_core::key_string, plugin->getName(), plugin->getDescription(), _T(""), false); 959 // TODO add comments elsewhere to the settings store for all loaded modules... 960 } 961 return plugin; 962 } 963 964 965 std::wstring NSClientT::describeCommand(std::wstring command) { 966 return commands_.describe(command); 967 } 968 std::list<std::wstring> NSClientT::getAllCommandNames() { 969 return commands_.list(); 970 } 971 void NSClientT::registerCommand(unsigned int id, std::wstring cmd, std::wstring desc) { 972 return commands_.register_command(id, cmd, desc); 973 } 974 975 NSCAPI::nagiosReturn NSClientT::inject(std::wstring command, std::wstring arguments, std::wstring &msg, std::wstring & perf) { 976 /*if (shared_client_.get() != NULL && shared_client_->hasMaster()) { 977 try { 978 return shared_client_->inject(command, arguments, splitter, escape, msg, perf); 979 } catch (nsclient_session::session_exception &e) { 980 LOG_ERROR_STD(_T("Failed to inject remote command: ") + e.what()); 981 return NSCAPI::returnCRIT; 982 } catch (...) { 983 LOG_ERROR_STD(_T("Failed to inject remote command: Unknown exception")); 984 return NSCAPI::returnCRIT; 985 } 986 } else */{ 987 PluginCommand::RequestMessage message; 988 PluginCommand::Header *hdr = message.mutable_header(); 989 hdr->set_type(PluginCommand::Header_Type_REQUEST); 990 hdr->set_version(PluginCommand::Header_Version_VERSION_1); 991 992 PluginCommand::Request *req = message.add_payload(); 993 req->set_command(to_string(command)); 994 req->set_version(PluginCommand::Request_Version_VERSION_1); 995 996 std::string args = to_string(arguments); 997 998 boost::tokenizer<boost::escaped_list_separator<char> > tok(args, boost::escaped_list_separator<char>('\\', ' ', '\"')); 999 BOOST_FOREACH(std::string s, tok) 1000 req->add_arguments(s); 1001 1002 std::string request, response; 1003 message.SerializeToString(&request); 1004 1005 1006 1007 NSCAPI::nagiosReturn ret = injectRAW(command.c_str(), request, response); 1008 if (response.empty()) { 1009 LOG_ERROR_CORE(_T("No data retutned from command")); 1010 return NSCAPI::returnUNKNOWN; 1011 } 1012 1013 PluginCommand::ResponseMessage rsp_msg; 1014 1015 rsp_msg.ParseFromString(response); 1016 if (rsp_msg.payload_size() != 1) { 1017 LOG_ERROR_CORE_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 1018 return NSCAPI::returnUNKNOWN; 1019 } 1020 msg = utf8::cvt<std::wstring>(rsp_msg.payload(0).message()); 1021 perf = utf8::cvt<std::wstring>(nscapi::functions::build_performance_data(rsp_msg.payload(0))); 1022 if ( (ret == NSCAPI::returnInvalidBufferLen) || (ret == NSCAPI::returnIgnored) ) { 1023 return ret; 1024 } 1025 return ret; 1026 } 1027 } 1028 1029 /** 1030 * Inject a command into the plug-in stack. 1031 * 1032 * @param command Command to inject 1033 * @param argLen Length of argument buffer 1034 * @param **argument Argument buffer 1035 * @param *returnMessageBuffer Message buffer 1036 * @param returnMessageBufferLen Length of returnMessageBuffer 1037 * @param *returnPerfBuffer Performance data buffer 1038 * @param returnPerfBufferLen Length of returnPerfBuffer 1039 * @return The command status 1040 */ 1041 NSCAPI::nagiosReturn NSClientT::injectRAW(const wchar_t* raw_command, std::string &request, std::string &response) { 1042 std::wstring cmd = nsclient::commands::make_key(raw_command); 1043 if (logDebug()) { 1044 LOG_DEBUG_CORE_STD(_T("Injecting: ") + cmd + _T("...")); 1045 } 1046 /*if (shared_client_.get() != NULL && shared_client_->hasMaster()) { 1047 try { 1048 std::wstring msg, perf; 1049 int returnCode = shared_client_->inject(command, arrayBuffer::arrayBuffer2string(argument, argLen, _T(" ")), L' ', true, msg, perf); 1050 NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, msg, returnCode); 1051 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, perf, returnCode); 1052 } catch (nsclient_session::session_exception &e) { 1053 LOG_ERROR_STD(_T("Failed to inject remote command: ") + e.what()); 1054 int returnCode = NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, _T("Failed to inject remote command: ") + e.what(), NSCAPI::returnCRIT); 1055 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, _T(""), returnCode); 1056 } catch (...) { 1057 LOG_ERROR_STD(_T("Failed to inject remote command: Unknown exception")); 1058 int returnCode = NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, _T("Failed to inject remote command: + e.what()"), NSCAPI::returnCRIT); 1059 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, _T(""), returnCode); 1060 } 1061 } else */{ 1062 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1063 if (!readLock.owns_lock()) { 1064 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (008).")); 1065 return NSCAPI::returnUNKNOWN; 1066 } 1067 try { 1068 nsclient::commands::plugin_type plugin = commands_.get(cmd); 1069 if (!plugin) { 1070 LOG_ERROR_CORE(_T("No handler for command: ") + cmd + _T(" avalible commands: ") + commands_.to_wstring()); 1071 return NSCAPI::returnIgnored; 1072 } 1073 NSCAPI::nagiosReturn c = plugin->handleCommand(cmd.c_str(), request, response); 1074 LOG_DEBUG_CORE_STD(_T("Result ") + cmd + _T(": ") + nscapi::plugin_helper::translateReturn(c)); 1075 return c; 1076 } catch (nsclient::commands::command_exception &e) { 1077 LOG_ERROR_CORE(_T("No handler for command: ") + cmd + _T(": ") + to_wstring(e.what())); 1078 return NSCAPI::returnIgnored; 1079 } catch (...) { 1080 LOG_ERROR_CORE(_T("Error handling command: ") + cmd); 1081 return NSCAPI::returnIgnored; 1082 } 1083 } 1084 } 1085 1086 1087 int NSClientT::simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp) { 828 1088 bool found = false; 829 1089 std::vector<std::string> responses; … … 915 1175 916 1176 917 /** 918 * Unload all plug-ins (in reversed order) 919 */ 920 void NSClientT::unloadPlugins(bool unloadLoggers) { 1177 NSCAPI::nagiosReturn NSClientT::exec_command(const wchar_t* raw_command, std::string &request, std::string &response) { 1178 std::list<std::string> responses; 1179 bool found = false; 921 1180 { 922 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 923 if (!writeLock.owns_lock()) { 924 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (003).")); 925 return; 926 } 927 if (unloadLoggers) 928 logger_master_.remove_all_plugins(); 929 } 930 { 931 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1181 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(5)); 932 1182 if (!readLock.owns_lock()) { 933 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (004).")); 934 return; 935 } 936 for (pluginList::reverse_iterator it = plugins_.rbegin(); it != plugins_.rend(); ++it) { 937 plugin_type p = *it; 938 if (!p) 939 continue; 940 try { 941 if (unloadLoggers || !p->hasMessageHandler()) { 942 LOG_DEBUG_CORE_STD(_T("Unloading plugin: ") + p->getModule() + _T("...")); 943 p->unload(); 944 } else { 945 LOG_DEBUG_CORE_STD(_T("Skipping log plugin: ") + p->getModule() + _T("...")); 1183 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (001).")); 1184 return -1; 1185 } 1186 BOOST_FOREACH(plugin_type p, plugins_) { 1187 if (p && p->has_command_line_exec()) { 1188 try { 1189 std::string response; 1190 NSCAPI::nagiosReturn r = p->commandLineExec(raw_command, request, response); 1191 if (r != NSCAPI::returnIgnored && !response.empty()) { 1192 LOG_DEBUG_CORE_STD(_T("Got response from: ") + p->getName()); 1193 found = true; 1194 responses.push_back(response); 1195 } 1196 } catch (NSPluginException e) { 1197 LOG_ERROR_CORE_STD(_T("Could not execute command: ") + e.error_ + _T(" in ") + e.file_); 946 1198 } 947 } catch(NSPluginException e) {948 LOG_ERROR_CORE_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_);949 } catch(...) {950 LOG_ERROR_CORE_STD(_T("Unknown exception raised when unloading plugin"));951 1199 } 952 1200 } 953 1201 } 954 { 955 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 956 if (!writeLock.owns_lock()) { 957 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (005).")); 958 return; 959 } 960 commands_.remove_all(); 961 for (pluginList::iterator it = plugins_.begin(); it != plugins_.end();) { 962 plugin_type p = (*it); 963 try { 964 if (!p && (unloadLoggers|| !p->isLoaded())) { 965 it = plugins_.erase(it); 966 //delete p; 967 continue; 968 } 969 } catch(NSPluginException e) { 970 LOG_ERROR_CORE_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); 971 } catch(...) { 972 LOG_ERROR_CORE(_T("Unknown exception raised when unloading plugin")); 973 } 974 it++; 975 } 976 } 977 } 978 979 void NSClientT::loadPlugins(NSCAPI::moduleLoadMode mode) { 980 bool hasBroken = false; 981 { 982 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 983 if (!readLock.owns_lock()) { 984 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (006).")); 985 return; 986 } 987 for (pluginList::iterator it=plugins_.begin(); it != plugins_.end();) { 988 LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + (*it)->getName() + _T(" as ") + (*it)->get_alias()); 989 try { 990 if (!(*it)->load_plugin(mode)) { 991 LOG_ERROR_CORE_STD(_T("Plugin refused to load: ") + (*it)->getModule()); 992 it = plugins_.erase(it); 993 } else 994 ++it; 995 } catch (NSPluginException e) { 996 it = plugins_.erase(it); 997 LOG_ERROR_CORE_STD(_T("Could not load plugin: ") + e.file_ + _T(": ") + e.error_); 998 } catch (...) { 999 it = plugins_.erase(it); 1000 LOG_ERROR_CORE_STD(_T("Could not load plugin: ") + (*it)->getModule()); 1001 } 1002 } 1003 } 1004 logger_master_.all_plugins_loaded(); 1005 } 1006 /** 1007 * Load and add a plugin to various internal structures 1008 * @param plugin The plug-in instance to load. The pointer is managed by the 1009 */ 1010 NSClientT::plugin_type NSClientT::addPlugin(boost::filesystem::wpath file, std::wstring alias) { 1011 { 1012 LOG_DEBUG_CORE_STD(_T("addPlugin(") + file.string() + _T(" as ") + alias + _T(")")); 1013 // Check if this is a duplicate plugin (if so return that instance) 1014 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 1015 if (!writeLock.owns_lock()) { 1016 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (007a).")); 1017 return plugin_type(); 1018 } 1019 1020 BOOST_FOREACH(plugin_type plug, plugins_) { 1021 if (plug->is_duplicate(file, alias)) { 1022 LOG_DEBUG_CORE_STD(_T("Found duplicate plugin returning old ") + to_wstring(plug->get_id())); 1023 return plug; 1024 } 1025 } 1026 1027 } 1028 1029 1030 plugin_type plugin(new NSCPlugin(next_plugin_id_++, file, alias)); 1031 plugin->load_dll(); 1032 { 1033 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 1034 if (!writeLock.owns_lock()) { 1035 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (007b).")); 1036 return plugin; 1037 } 1038 1039 plugins_.insert(plugins_.end(), plugin); 1040 commands_.add_plugin(plugin); 1041 channels_.add_plugin(plugin); 1042 if (plugin->hasNotificationHandler()) { 1043 channels_.register_listener(plugin->get_id(), _T("NSCA")); 1044 } 1045 if (plugin->hasMessageHandler()) 1046 logger_master_.add_plugin(plugin); 1047 //settings_manager::get_core()->register_key(_T("/modules"), plugin->getModule(), settings::settings_core::key_string, plugin->getName(), plugin->getDescription(), _T(""), false); 1048 // TODO add comments elsewhere to the settings store for all loaded modules... 1049 } 1050 return plugin; 1051 } 1052 1053 1054 std::wstring NSClientT::describeCommand(std::wstring command) { 1055 return commands_.describe(command); 1056 } 1057 std::list<std::wstring> NSClientT::getAllCommandNames() { 1058 return commands_.list(); 1059 } 1060 void NSClientT::registerCommand(unsigned int id, std::wstring cmd, std::wstring desc) { 1061 return commands_.register_command(id, cmd, desc); 1062 } 1063 1064 NSCAPI::nagiosReturn NSClientT::inject(std::wstring command, std::wstring arguments, std::wstring &msg, std::wstring & perf) { 1065 /*if (shared_client_.get() != NULL && shared_client_->hasMaster()) { 1066 try { 1067 return shared_client_->inject(command, arguments, splitter, escape, msg, perf); 1068 } catch (nsclient_session::session_exception &e) { 1069 LOG_ERROR_STD(_T("Failed to inject remote command: ") + e.what()); 1070 return NSCAPI::returnCRIT; 1071 } catch (...) { 1072 LOG_ERROR_STD(_T("Failed to inject remote command: Unknown exception")); 1073 return NSCAPI::returnCRIT; 1074 } 1075 } else */{ 1076 PluginCommand::RequestMessage message; 1077 PluginCommand::Header *hdr = message.mutable_header(); 1078 hdr->set_type(PluginCommand::Header_Type_REQUEST); 1079 hdr->set_version(PluginCommand::Header_Version_VERSION_1); 1080 1081 PluginCommand::Request *req = message.add_payload(); 1082 req->set_command(to_string(command)); 1083 req->set_version(PluginCommand::Request_Version_VERSION_1); 1084 1085 std::string args = to_string(arguments); 1086 1087 boost::tokenizer<boost::escaped_list_separator<char> > tok(args, boost::escaped_list_separator<char>('\\', ' ', '\"')); 1088 BOOST_FOREACH(std::string s, tok) 1089 req->add_arguments(s); 1090 1091 std::string request, response; 1092 message.SerializeToString(&request); 1093 1094 1095 1096 NSCAPI::nagiosReturn ret = injectRAW(command.c_str(), request, response); 1097 if (response.empty()) { 1098 LOG_ERROR_CORE(_T("No data retutned from command")); 1099 return NSCAPI::returnUNKNOWN; 1100 } 1101 1102 PluginCommand::ResponseMessage rsp_msg; 1103 1104 rsp_msg.ParseFromString(response); 1105 if (rsp_msg.payload_size() != 1) { 1106 LOG_ERROR_CORE_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 1107 return NSCAPI::returnUNKNOWN; 1108 } 1109 msg = utf8::cvt<std::wstring>(rsp_msg.payload(0).message()); 1110 perf = utf8::cvt<std::wstring>(nscapi::functions::build_performance_data(rsp_msg.payload(0))); 1111 if ( (ret == NSCAPI::returnInvalidBufferLen) || (ret == NSCAPI::returnIgnored) ) { 1112 return ret; 1113 } 1114 return ret; 1115 } 1116 } 1117 1118 /** 1119 * Inject a command into the plug-in stack. 1120 * 1121 * @param command Command to inject 1122 * @param argLen Length of argument buffer 1123 * @param **argument Argument buffer 1124 * @param *returnMessageBuffer Message buffer 1125 * @param returnMessageBufferLen Length of returnMessageBuffer 1126 * @param *returnPerfBuffer Performance data buffer 1127 * @param returnPerfBufferLen Length of returnPerfBuffer 1128 * @return The command status 1129 */ 1130 NSCAPI::nagiosReturn NSClientT::injectRAW(const wchar_t* raw_command, std::string &request, std::string &response) { 1131 std::wstring cmd = nsclient::commands::make_key(raw_command); 1132 if (logDebug()) { 1133 LOG_DEBUG_CORE_STD(_T("Injecting: ") + cmd + _T("...")); 1134 } 1135 /*if (shared_client_.get() != NULL && shared_client_->hasMaster()) { 1136 try { 1137 std::wstring msg, perf; 1138 int returnCode = shared_client_->inject(command, arrayBuffer::arrayBuffer2string(argument, argLen, _T(" ")), L' ', true, msg, perf); 1139 NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, msg, returnCode); 1140 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, perf, returnCode); 1141 } catch (nsclient_session::session_exception &e) { 1142 LOG_ERROR_STD(_T("Failed to inject remote command: ") + e.what()); 1143 int returnCode = NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, _T("Failed to inject remote command: ") + e.what(), NSCAPI::returnCRIT); 1144 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, _T(""), returnCode); 1145 } catch (...) { 1146 LOG_ERROR_STD(_T("Failed to inject remote command: Unknown exception")); 1147 int returnCode = NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, _T("Failed to inject remote command: + e.what()"), NSCAPI::returnCRIT); 1148 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, _T(""), returnCode); 1149 } 1150 } else */{ 1151 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1152 if (!readLock.owns_lock()) { 1153 LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex (008).")); 1154 return NSCAPI::returnUNKNOWN; 1155 } 1156 try { 1157 nsclient::commands::plugin_type plugin = commands_.get(cmd); 1158 if (!plugin) { 1159 LOG_ERROR_CORE(_T("No handler for command: ") + cmd + _T(" avalible commands: ") + commands_.to_wstring()); 1160 return NSCAPI::returnIgnored; 1161 } 1162 NSCAPI::nagiosReturn c = plugin->handleCommand(cmd.c_str(), request, response); 1163 LOG_DEBUG_CORE_STD(_T("Result ") + cmd + _T(": ") + nscapi::plugin_helper::translateReturn(c)); 1164 return c; 1165 } catch (nsclient::commands::command_exception &e) { 1166 LOG_ERROR_CORE(_T("No handler for command: ") + cmd + _T(": ") + to_wstring(e.what())); 1167 return NSCAPI::returnIgnored; 1168 } catch (...) { 1169 LOG_ERROR_CORE(_T("Error handling command: ") + cmd); 1170 return NSCAPI::returnIgnored; 1171 } 1172 } 1173 } 1202 1203 ExecuteCommand::ResponseMessage response_message; 1204 ::ExecuteCommand::Header* hdr = response_message.mutable_header(); 1205 1206 hdr->set_type(ExecuteCommand::Header_Type_RESPONSE); 1207 hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 1208 1209 BOOST_FOREACH(std::string r, responses) { 1210 ExecuteCommand::ResponseMessage tmp; 1211 tmp.ParseFromString(r); 1212 for (int i=0;i<tmp.payload_size();i++) { 1213 ExecuteCommand::Response *r = response_message.add_payload(); 1214 r->CopyFrom(tmp.payload(i)); 1215 } 1216 } 1217 response_message.SerializeToString(&response); 1218 if (found) 1219 return NSCAPI::returnOK; 1220 return NSCAPI::returnIgnored; 1221 } 1222 1174 1223 1175 1224 -
service/NSClient++.h
r39c73cd r2c95d22 166 166 std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args); 167 167 void reportMessage(std::string data); 168 int command_line_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp); 168 int simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp); 169 NSCAPI::nagiosReturn exec_command(const wchar_t* raw_command, std::string &request, std::string &response); 169 170 170 171 struct service_controller { -
service/cli_parser.hpp
r39c73cd r2c95d22 309 309 } 310 310 std::vector<std::wstring> resp; 311 mainClient. command_line_exec(module, command, arguments, resp);311 mainClient.simple_exec(module, command, arguments, resp); 312 312 mainClient.exitCore(false); 313 313 -
service/core_api.cpp
r54ac968 r2c95d22 119 119 return ret; 120 120 } 121 122 NSCAPI::nagiosReturn NSAPIExecCommand(const wchar_t* command, const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len) { 123 std::string request (request_buffer, request_buffer_len), response; 124 NSCAPI::nagiosReturn ret = mainClient.exec_command(command, request, response); 125 *response_buffer_len = response.size(); 126 if (response.empty()) 127 *response_buffer = NULL; 128 else { 129 *response_buffer = new char[*response_buffer_len + 10]; 130 memcpy(*response_buffer, response.c_str(), *response_buffer_len); 131 } 132 return ret; 133 } 134 135 136 121 137 NSCAPI::errorReturn NSAPIGetSettingsSection(const wchar_t* section, wchar_t*** aBuffer, unsigned int * bufLen) { 122 138 try { … … 418 434 if (wcscasecmp(buffer, _T("NSAPIInject")) == 0) 419 435 return reinterpret_cast<LPVOID>(&NSAPIInject); 436 if (wcscasecmp(buffer, _T("NSAPIExecCommand")) == 0) 437 return reinterpret_cast<LPVOID>(&NSAPIExecCommand); 420 438 if (wcscasecmp(buffer, _T("NSAPIGetBasePath")) == 0) 421 439 return reinterpret_cast<LPVOID>(&NSAPIGetBasePath); -
service/core_api.h
r1ecd26f r2c95d22 39 39 void NSAPIStopServer(void); 40 40 NSCAPI::nagiosReturn NSAPIInject(const wchar_t* command, const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len); 41 NSCAPI::nagiosReturn NSAPIExecCommand(const wchar_t* command, const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len); 41 42 NSCAPI::errorReturn NSAPIGetSettingsSection(const wchar_t*, wchar_t***, unsigned int *); 42 43 NSCAPI::errorReturn NSAPIReleaseSettingsSectionBuffer(wchar_t*** aBuffer, unsigned int * bufLen);
Note: See TracChangeset
for help on using the changeset viewer.








