Changeset a14aa07 in nscp
- Timestamp:
- 09/03/11 08:17:18 (21 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 4cac39f
- Parents:
- a44cb15
- Files:
-
- 22 edited
-
changelog (modified) (1 diff)
-
include/nscapi/nscapi_plugin_wrapper.cpp (modified) (4 diffs)
-
include/nscapi/nscapi_plugin_wrapper.hpp (modified) (3 diffs)
-
modules/CheckDisk/CheckDisk.h (modified) (1 diff)
-
modules/CheckEventLog/CheckEventLog.h (modified) (1 diff)
-
modules/CheckHelpers/CheckHelpers.h (modified) (1 diff)
-
modules/CheckNSCP/CheckNSCP.h (modified) (1 diff)
-
modules/CheckSystem/CheckSystem.h (modified) (1 diff)
-
modules/CheckTaskSched/CheckTaskSched.h (modified) (1 diff)
-
modules/CheckTaskSched2/CheckTaskSched2.h (modified) (1 diff)
-
modules/CheckWMI/CheckWMI.h (modified) (1 diff)
-
modules/LUAScript/LUAScript.h (modified) (1 diff)
-
modules/NRPEClient/NRPEClient.h (modified) (1 diff)
-
modules/NSCAAgent/NSCAAgent.h (modified) (1 diff)
-
modules/NSCPClient/NSCPClient.h (modified) (1 diff)
-
modules/PythonScript/PythonScript.cpp (modified) (5 diffs)
-
modules/PythonScript/script_wrapper.cpp (modified) (19 diffs)
-
modules/PythonScript/script_wrapper.hpp (modified) (6 diffs)
-
scripts/python/test.py (modified) (5 diffs)
-
service/logger.hpp (modified) (2 diffs)
-
version.hpp (modified) (1 diff)
-
version.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
changelog
ra44cb15 ra14aa07 5 5 * Fixa dependonservice LanManWorkStation (old win) 6 6 * Fix RtlStringFromGUID problem on NT4 7 8 2011-09-02 MickeM 9 * Fixed several issues in PythonScript 10 * PythonScript supports loading multiple instances as well as scripts 11 * Added some basic channel test to test.py script (the idea is that this will become "unit tests" eventually) 7 12 8 13 2011-09-01 MickeM -
include/nscapi/nscapi_plugin_wrapper.cpp
ra44cb15 ra14aa07 38 38 using namespace nscp::helpers; 39 39 40 //extern nscapi::helper_singleton* nscapi::plugin_singleton; 41 /** 42 * Wrap a return string. 43 * This function copies a string to a char buffer making sure the buffer has the correct length. 44 * 45 * @param *buffer Buffer to copy the string to. 46 * @param bufLen Length of the buffer 47 * @param str Th string to copy 48 * @param defaultReturnCode The default return code 49 * @return NSCAPI::success unless the buffer is to short then it will be NSCAPI::invalidBufferLen 50 */ 51 int nscapi::plugin_wrapper::wrapReturnString(wchar_t *buffer, unsigned int bufLen, std::wstring str, int defaultReturnCode ) { 52 // @todo deprecate this 53 if (str.length() >= bufLen) { 54 std::wstring sstr = str.substr(0, bufLen-2); 55 NSC_DEBUG_MSG_STD(_T("String (") + strEx::itos(str.length()) + _T(") to long to fit inside buffer(") + strEx::itos(bufLen) + _T(") : ") + sstr); 56 return NSCAPI::isInvalidBufferLen; 57 } 58 wcsncpy(buffer, str.c_str(), bufLen); 59 return defaultReturnCode; 60 } 40 41 nscapi::helper_singleton::helper_singleton() : core_(new nscapi::core_wrapper()), plugin_(new nscapi::plugin_wrapper()) {} 61 42 62 43 /** … … 91 72 return GET_CORE()->load_endpoints(f)?NSCAPI::isSuccess:NSCAPI::hasFailed; 92 73 } 93 /**94 * Wrap the GetModuleName function call95 * @param buf Buffer to store the module name96 * @param bufLen Length of buffer97 * @param str String to store inside the buffer98 * @ copy status99 */100 NSCAPI::errorReturn nscapi::plugin_wrapper::wrapGetModuleName(wchar_t* buf, unsigned int bufLen, std::wstring str) {101 return nscapi::plugin_wrapper::wrapReturnString(buf, bufLen, str, NSCAPI::isSuccess);102 }103 104 /**105 * Wrap the GetModuleVersion function call106 * @param *major Major version number107 * @param *minor Minor version number108 * @param *revision Revision109 * @param version version as a module_version110 * @return NSCAPI::success111 */112 NSCAPI::errorReturn nscapi::plugin_wrapper::wrapGetModuleVersion(int *major, int *minor, int *revision, module_version version) {113 *major = version.major;114 *minor = version.minor;115 *revision = version.revision;116 return NSCAPI::isSuccess;117 }118 /**119 * Wrap the HasCommandHandler function call120 * @param has true if this module has a command handler121 * @return NSCAPI::istrue or NSCAPI::isfalse122 */123 NSCAPI::boolReturn nscapi::plugin_wrapper::wrapHasCommandHandler(bool has) {124 return has?NSCAPI::istrue:NSCAPI::isfalse;125 }126 /**127 * Wrap the HasMessageHandler function call128 * @param has true if this module has a message handler129 * @return NSCAPI::istrue or NSCAPI::isfalse130 */131 NSCAPI::boolReturn nscapi::plugin_wrapper::wrapHasMessageHandler(bool has) {132 return has?NSCAPI::istrue:NSCAPI::isfalse;133 }134 NSCAPI::boolReturn nscapi::plugin_wrapper::wrapHasNotificationHandler(bool has) {135 return has?NSCAPI::istrue:NSCAPI::isfalse;136 }137 NSCAPI::boolReturn nscapi::plugin_wrapper::wrapHasRoutingHandler(bool has) {138 return has?NSCAPI::istrue:NSCAPI::isfalse;139 }140 141 NSCAPI::nagiosReturn nscapi::plugin_wrapper::wrapHandleNotification(NSCAPI::nagiosReturn retResult) {142 return retResult;143 }144 NSCAPI::nagiosReturn nscapi::plugin_wrapper::wrapRouteMessage(NSCAPI::nagiosReturn retResult) {145 return retResult;146 }147 148 /**149 * Wrap the HandleCommand call150 * @param retResult The returned result151 * @param retMessage The returned message152 * @param retPerformance The returned performance data153 * @param *returnBufferMessage The return message buffer154 * @param returnBufferMessageLen The return message buffer length155 * @param *returnBufferPerf The return performance data buffer156 * @param returnBufferPerfLen The return performance data buffer length157 * @return the return code158 */159 NSCAPI::nagiosReturn nscapi::plugin_wrapper::wrapHandleCommand(NSCAPI::nagiosReturn retResult, const std::string &reply, char **reply_buffer, unsigned int *size) {160 // TODO: Make this global to allow remote deletion!!!161 unsigned int buf_len = reply.size();162 *reply_buffer = new char[buf_len + 10];163 memcpy(*reply_buffer, reply.c_str(), buf_len+1);164 (*reply_buffer)[buf_len] = 0;165 (*reply_buffer)[buf_len+1] = 0;166 *size = buf_len;167 if (!nscapi::plugin_helper::isMyNagiosReturn(retResult)) {168 NSC_LOG_ERROR(_T("A module returned an invalid return code"));169 }170 return retResult;171 }172 NSCAPI::nagiosReturn nscapi::plugin_wrapper::wrapCommandLineExec(NSCAPI::nagiosReturn retResult, const std::string &reply, char **reply_buffer, unsigned int *size) {173 // TODO: Make this global to allow remote deletion!!!174 unsigned int buf_len = reply.size();175 *reply_buffer = new char[buf_len + 10];176 memcpy(*reply_buffer, reply.c_str(), buf_len+1);177 (*reply_buffer)[buf_len] = 0;178 (*reply_buffer)[buf_len+1] = 0;179 *size = buf_len;180 return retResult;181 }182 183 /**184 * Wrap the NSLoadModule call185 * @param success true if module load was successfully186 * @return NSCAPI::success or NSCAPI::failed187 */188 int nscapi::plugin_wrapper::wrapLoadModule(bool success) {189 if (success)190 return NSCAPI::isSuccess;191 return NSCAPI::hasFailed;192 }193 /**194 * Wrap the NSUnloadModule call195 * @param success true if module load was successfully196 * @return NSCAPI::success or NSCAPI::failed197 */198 int nscapi::plugin_wrapper::wrapUnloadModule(bool success) {199 if (success)200 return NSCAPI::isSuccess;201 return NSCAPI::hasFailed;202 }203 void nscapi::plugin_wrapper::wrapDeleteBuffer(char**buffer) {204 delete [] *buffer;205 }206 74 207 75 208 nscapi::helper_singleton::helper_singleton() : core_(new nscapi::core_wrapper()), plugin_(new nscapi::plugin_wrapper()) {}209 210 211 /*212 NSCAPI::nagiosReturn nscapi::impl::CommandImpl::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) {213 214 nscapi::functions::parse_simple_query_request(char_command, request);215 std::wstring command = char_command;216 PluginCommand::RequestMessage request_message;217 request_message.ParseFromString(request);218 219 if (request_message.payload_size() != 1) {220 return NSCAPI::returnIgnored;221 }222 ::PluginCommand::Request req_payload = request_message.payload().Get(0);223 224 PluginCommand::ResponseMessage response_message;225 ::PluginCommand::Header* hdr = response_message.mutable_header();226 227 hdr->set_type(PluginCommand::Header_Type_RESPONSE);228 hdr->set_version(PluginCommand::Header_Version_VERSION_1);229 230 PluginCommand::Response *resp_payload = response_message.add_payload();231 232 handleCommand(command, &req_payload, resp_payload);233 234 235 resp_payload->set_version(PluginCommand::Response_Version_VERSION_1);236 response_message.SerializeToString(&response);237 238 return NSCAPI::returnOK;239 }240 */241 76 void nscapi::impl::simple_log_handler::handleMessageRAW(std::string data) { 242 77 try { … … 255 90 } 256 91 257 NSCAPI::nagiosReturn nscapi::impl::simple_command ::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) {92 NSCAPI::nagiosReturn nscapi::impl::simple_command_handler::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) { 258 93 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 259 94 std::wstring msg, perf; … … 273 108 } 274 109 275 NSCAPI::nagiosReturn nscapi::impl:: SimpleNotificationHandler::handleRAWNotification(const wchar_t* channel, const wchar_t* command, std::string result) {110 NSCAPI::nagiosReturn nscapi::impl::simple_submission_handler::handleRAWNotification(const wchar_t* channel, const wchar_t* command, std::string result) { 276 111 try { 277 112 std::wstring msg, perf; -
include/nscapi/nscapi_plugin_wrapper.hpp
ra44cb15 ra14aa07 120 120 }; 121 121 122 class SimpleNotificationHandler {122 class simple_submission_handler { 123 123 public: 124 124 NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, const wchar_t* command, std::string result); … … 127 127 }; 128 128 129 class simple_command {129 class simple_command_handler { 130 130 public: 131 131 NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); … … 145 145 }; 146 146 147 /*148 class CommandImpl {149 public:150 NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response);151 virtual void handleCommand(std::wstring command, PluginCommand::Request *request, PluginCommand::Response *response) = 0;152 };153 */154 147 }; 155 148 -
modules/CheckDisk/CheckDisk.h
ra44cb15 ra14aa07 25 25 #include <checkHelpers.hpp> 26 26 27 class CheckDisk : public nscapi::impl::simple_command , public nscapi::impl::simple_plugin {27 class CheckDisk : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 28 28 private: 29 29 bool show_errors_; -
modules/CheckEventLog/CheckEventLog.h
ra44cb15 ra14aa07 27 27 28 28 29 class CheckEventLog : public nscapi::impl::simple_command , public nscapi::impl::simple_plugin {29 class CheckEventLog : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 30 30 private: 31 31 bool debug_; -
modules/CheckHelpers/CheckHelpers.h
ra44cb15 ra14aa07 23 23 #include <strEx.h> 24 24 25 class CheckHelpers : public nscapi::impl::simple_command , public nscapi::impl::simple_plugin {25 class CheckHelpers : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 26 26 private: 27 27 -
modules/CheckNSCP/CheckNSCP.h
r81e420c ra14aa07 33 33 NSC_WRAPPERS_MAIN(); 34 34 35 class CheckNSCP : public nscapi::impl::simple_command , public nscapi::impl::simple_plugin, public nscapi::impl::simple_log_handler {35 class CheckNSCP : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_log_handler { 36 36 private: 37 37 boost::timed_mutex mutex_; -
modules/CheckSystem/CheckSystem.h
r81e420c ra14aa07 28 28 NSC_WRAPPERS_CLI(); 29 29 30 class CheckSystem : public nscapi::impl::simple_command , public nscapi::impl::simple_plugin {30 class CheckSystem : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 31 31 private: 32 32 CheckMemory memoryChecker; -
modules/CheckTaskSched/CheckTaskSched.h
r81e420c ra14aa07 28 28 #include "TaskSched.h" 29 29 30 class CheckTaskSched : public nscapi::impl::simple_command , public nscapi::impl::simple_plugin {30 class CheckTaskSched : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 31 31 private: 32 32 std::wstring syntax; -
modules/CheckTaskSched2/CheckTaskSched2.h
r81e420c ra14aa07 28 28 #include "TaskSched.h" 29 29 30 class CheckTaskSched2 : public nscapi::impl::simple_command , public nscapi::impl::simple_plugin {30 class CheckTaskSched2 : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 31 31 private: 32 32 std::wstring syntax; -
modules/CheckWMI/CheckWMI.h
r81e420c ra14aa07 88 88 89 89 90 class CheckWMI : public nscapi::impl::simple_command , public nscapi::impl::simple_plugin {90 class CheckWMI : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 91 91 public: 92 92 CheckWMI(); -
modules/LUAScript/LUAScript.h
r81e420c ra14aa07 31 31 32 32 33 class LUAScript : public nscapi::impl::simple_command , public script_wrapper::lua_handler, public nscapi::impl::simple_plugin {33 class LUAScript : public nscapi::impl::simple_command_handler, public script_wrapper::lua_handler, public nscapi::impl::simple_plugin { 34 34 private: 35 35 -
modules/NRPEClient/NRPEClient.h
ra44cb15 ra14aa07 28 28 29 29 30 class NRPEClient : public nscapi::impl::simple_command , public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec {30 class NRPEClient : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 31 31 private: 32 32 typedef enum { -
modules/NSCAAgent/NSCAAgent.h
ra44cb15 ra14aa07 24 24 NSC_WRAPPERS_CHANNELS(); 25 25 26 class NSCAAgent : public nscapi::impl:: SimpleNotificationHandler, public nscapi::impl::simple_plugin {26 class NSCAAgent : public nscapi::impl::simple_submission_handler, public nscapi::impl::simple_plugin { 27 27 private: 28 28 -
modules/NSCPClient/NSCPClient.h
ra44cb15 ra14aa07 28 28 29 29 30 class NSCPClient : public nscapi::impl::simple_command , public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec {30 class NSCPClient : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 31 31 private: 32 32 struct nscp_connection_data { -
modules/PythonScript/PythonScript.cpp
ra44cb15 ra14aa07 77 77 .def("simple_subscription", &script_wrapper::function_wrapper::subscribe_simple_function) 78 78 ; 79 class_<script_wrapper::command_wrapper, boost::shared_ptr<script_wrapper::command_wrapper> >("Core", no_init)79 class_<script_wrapper::command_wrapper, boost::shared_ptr<script_wrapper::command_wrapper> >("Core", init<>()) 80 80 .def("get",&script_wrapper::command_wrapper::create) 81 81 .staticmethod("get") … … 147 147 } 148 148 } 149 149 static bool has_init = false; 150 150 bool PythonScript::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 151 151 alias_ = alias; … … 167 167 settings.notify(); 168 168 169 Py_Initialize(); 169 bool do_init = false; 170 if (!has_init) { 171 has_init = true; 172 Py_Initialize(); 173 do_init = true; 174 } 175 170 176 try { 171 177 … … 174 180 PyRun_SimpleString("sys.stderr = cStringIO.StringIO()"); 175 181 176 initNSCP(); 182 if (do_init) 183 initNSCP(); 177 184 178 185 BOOST_FOREACH(script_container &script, scripts_) { … … 288 295 std::string cmd = utf8::cvt<std::string>(command); 289 296 if (inst->has_function(cmd)) { 290 return inst-> exec(cmd, request, response);297 return inst->handle_query(cmd, request, response); 291 298 } 292 299 if (inst->has_simple(cmd)) { 293 300 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(command, request); 294 301 std::wstring msg, perf; 295 NSCAPI::nagiosReturn ret = inst-> exec_simple(cmd, data.args, msg, perf);302 NSCAPI::nagiosReturn ret = inst->handle_simple_query(cmd, data.args, msg, perf); 296 303 nscapi::functions::create_simple_query_response(data.command, ret, msg, perf, response); 297 304 return ret; -
modules/PythonScript/script_wrapper.cpp
ra44cb15 ra14aa07 22 22 23 23 void script_wrapper::log_exception() { 24 PyErr_Print(); 25 boost::python::object sys(boost::python::handle<>(PyImport_ImportModule("sys"))); 26 boost::python::object err = sys.attr("stderr"); 27 std::string err_text = boost::python::extract<std::string>(err.attr("getvalue")()); 28 NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(err_text)); 29 PyErr_Clear(); 24 try { 25 PyErr_Print(); 26 boost::python::object sys(boost::python::handle<>(PyImport_ImportModule("sys"))); 27 boost::python::object err = sys.attr("stderr"); 28 std::string err_text = boost::python::extract<std::string>(err.attr("getvalue")()); 29 NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(err_text)); 30 PyErr_Clear(); 31 } catch (const std::exception &e) { 32 NSC_LOG_ERROR_STD(_T("Failed to parse error: ") + utf8::cvt<std::wstring>(e.what())); 33 PyErr_Clear(); 34 } 30 35 } 31 36 … … 33 38 try { 34 39 core->registerSubmissionListener(plugin_id, utf8::cvt<std::wstring>(channel)); 35 functions::get()->simple_handler[channel] = callable; 40 boost::python::handle<> h(boost::python::borrowed(callable)); 41 //return boost::python::object o(h); 42 functions::get()->simple_handler[channel] = h; 36 43 } catch (const std::exception &e) { 37 44 NSC_LOG_ERROR_STD(_T("Failed to subscribe to channel ") + utf8::cvt<std::wstring>(channel) + _T(": ") + utf8::cvt<std::wstring>(e.what())); … … 43 50 try { 44 51 core->registerSubmissionListener(plugin_id, utf8::cvt<std::wstring>(channel)); 45 functions::get()->normal_handler[channel] = callable; 52 boost::python::handle<> h(boost::python::borrowed(callable)); 53 functions::get()->normal_handler[channel] = h; 46 54 } catch (const std::exception &e) { 47 55 NSC_LOG_ERROR_STD(_T("Failed to subscribe to channel ") + utf8::cvt<std::wstring>(channel) + _T(": ") + utf8::cvt<std::wstring>(e.what())); … … 55 63 try { 56 64 core->registerCommand(plugin_id, utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc)); 57 functions::get()->simple_functions[name] = callable; 65 boost::python::handle<> h(boost::python::borrowed(callable)); 66 functions::get()->simple_functions[name] = h; 58 67 } catch (...) { 59 68 NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name)); … … 63 72 try { 64 73 core->registerCommand(plugin_id, utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc)); 65 functions::get()->normal_functions[name] = callable; 74 boost::python::handle<> h(boost::python::borrowed(callable)); 75 functions::get()->normal_functions[name] = h; 66 76 } catch (...) { 67 77 NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name)); … … 70 80 void script_wrapper::function_wrapper::register_simple_cmdline(std::string name, PyObject* callable) { 71 81 try { 72 functions::get()->simple_cmdline[name] = callable; 82 boost::python::handle<> h(boost::python::borrowed(callable)); 83 functions::get()->simple_cmdline[name] = h; 73 84 } catch (...) { 74 85 NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name)); … … 77 88 void script_wrapper::function_wrapper::register_cmdline(std::string name, PyObject* callable) { 78 89 try { 79 functions::get()->normal_cmdline[name] = callable; 90 boost::python::handle<> h(boost::python::borrowed(callable)); 91 functions::get()->normal_cmdline[name] = h; 80 92 } catch (...) { 81 93 NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name)); 82 94 } 83 95 } 84 int script_wrapper::function_wrapper:: exec(const std::string cmd, const std::string &request, std::string &response) const {96 int script_wrapper::function_wrapper::handle_query(const std::string cmd, const std::string &request, std::string &response) const { 85 97 try { 86 98 functions::function_map_type::iterator it = functions::get()->normal_functions.find(cmd); … … 89 101 return NSCAPI::returnIgnored; 90 102 } 91 tuple ret = boost::python::call<tuple>( it->second, cmd, request);103 tuple ret = boost::python::call<tuple>(boost::python::object(it->second).ptr(), cmd, request); 92 104 if (ret.ptr() == Py_None) { 93 105 return NSCAPI::returnUNKNOWN; … … 105 117 } 106 118 107 int script_wrapper::function_wrapper:: exec_simple(const std::string cmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const {119 int script_wrapper::function_wrapper::handle_simple_query(const std::string cmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const { 108 120 try { 109 121 functions::function_map_type::iterator it = functions::get()->simple_functions.find(cmd); … … 117 129 l.append(utf8::cvt<std::string>(a)); 118 130 } 119 tuple ret = boost::python::call<tuple>(it->second, l); 131 tuple ret; 132 try { 133 ret = boost::python::call<tuple>(boost::python::object(it->second).ptr(), l); 134 } catch( error_already_set e) { 135 log_exception(); 136 msg = _T("Exception in: ") + utf8::cvt<std::wstring>(cmd); 137 return NSCAPI::returnUNKNOWN; 138 } 139 120 140 if (ret.ptr() == Py_None) { 121 141 msg = _T("None"); … … 125 145 if (len(ret) > 0) 126 146 ret_code = extract<int>(ret[0]); 127 if (len(ret) > 1) 128 msg = utf8::cvt<std::wstring>(extract<std::string>(ret[1])); 129 if (len(ret) > 2) 130 perf = utf8::cvt<std::wstring>(extract<std::string>(ret[2])); 147 if (len(ret) > 1) { 148 try { 149 //boost::python::object o = ret[1]; 150 msg = utf8::cvt<std::wstring>(extract<std::string>(ret[1])); 151 } catch(const error_already_set &e) { 152 msg = _T("Failed to convert message"); 153 ret_code = NSCAPI::returnUNKNOWN; 154 } 155 } 156 if (len(ret) > 2) { 157 try { 158 //boost::python::object o = ret[2]; 159 perf = utf8::cvt<std::wstring>(extract<std::string>(ret[2])); 160 } catch(const error_already_set &e) { 161 msg = _T("Failed to convert performance data"); 162 ret_code = NSCAPI::returnUNKNOWN; 163 } 164 } 131 165 return ret_code; 132 } catch( error_already_set e) { 133 log_exception(); 134 msg = _T("Exception in: ") + utf8::cvt<std::wstring>(cmd); 166 } catch(const error_already_set &e) { 167 log_exception(); 168 msg = _T("Failed to convert data for: ") + utf8::cvt<std::wstring>(cmd); 169 return NSCAPI::returnUNKNOWN; 170 } catch(const std::exception &e) { 171 msg = _T("Exception in ") + utf8::cvt<std::wstring>(cmd) + _T(": ") + utf8::cvt<std::wstring>(e.what()); 135 172 return NSCAPI::returnUNKNOWN; 136 173 } … … 151 188 return NSCAPI::returnIgnored; 152 189 } 153 tuple ret = boost::python::call<tuple>( it->second, cmd, request);190 tuple ret = boost::python::call<tuple>(boost::python::object(it->second).ptr(), cmd, request); 154 191 if (ret.ptr() == Py_None) { 155 192 return NSCAPI::returnUNKNOWN; … … 176 213 } 177 214 178 tuple ret = boost::python::call<tuple>( it->second, convert(arguments));215 tuple ret = boost::python::call<tuple>(boost::python::object(it->second).ptr(), convert(arguments)); 179 216 if (ret.ptr() == Py_None) { 180 217 result = _T("None"); … … 209 246 return NSCAPI::returnIgnored; 210 247 } 211 object ret = boost::python::call<object>( it->second, channel, command, message);248 object ret = boost::python::call<object>(boost::python::object(it->second).ptr(), channel, command, message); 212 249 if (ret.ptr() == Py_None) { 213 250 return NSCAPI::returnUNKNOWN; … … 218 255 return NSCAPI::returnUNKNOWN; 219 256 } 257 } 258 script_wrapper::status script_wrapper::nagios_return_to_py(int code) { 259 if (code == NSCAPI::returnOK) 260 return OK; 261 if (code == NSCAPI::returnWARN) 262 return WARN; 263 if (code == NSCAPI::returnCRIT) 264 return CRIT; 265 if (code == NSCAPI::returnUNKNOWN) 266 return UNKNOWN; 267 NSC_LOG_ERROR_STD(_T("Invalid return code: ") + strEx::itos(code)); 268 return UNKNOWN; 269 } 270 int script_wrapper::py_to_nagios_return(status code) { 271 NSCAPI::nagiosReturn c = NSCAPI::returnUNKNOWN; 272 if (code == OK) 273 return NSCAPI::returnOK; 274 if (code == WARN) 275 return NSCAPI::returnWARN; 276 if (code == CRIT) 277 return NSCAPI::returnCRIT; 278 if (code == UNKNOWN) 279 return NSCAPI::returnUNKNOWN; 280 NSC_LOG_ERROR_STD(_T("Invalid return code: ") + strEx::itos(c)); 281 return NSCAPI::returnUNKNOWN; 220 282 } 221 283 … … 227 289 return NSCAPI::returnIgnored; 228 290 } 229 230 object ret = boost::python::call<object>(it->second, channel, command, code, msg, perf); 291 object ret = boost::python::call<object>(boost::python::object(it->second).ptr(), channel, command, nagios_return_to_py(code), utf8::cvt<std::string>(msg), utf8::cvt<std::string>(perf)); 231 292 if (ret.ptr() == Py_None) { 232 293 return NSCAPI::returnUNKNOWN; … … 279 340 return ret; 280 341 } 281 282 342 void script_wrapper::command_wrapper::simple_submit(std::string channel, std::string command, status code, std::string message, std::string perf) { 283 343 NSCAPI::nagiosReturn c = NSCAPI::returnUNKNOWN; … … 290 350 std::wstring wmessage = utf8::cvt<std::wstring>(message); 291 351 std::wstring wperf = utf8::cvt<std::wstring>(perf); 292 core->submit_simple_message(utf8::cvt<std::wstring>(channel), utf8::cvt<std::wstring>(command), c, wmessage, wperf); 352 std::wstring wchannel = utf8::cvt<std::wstring>(channel); 353 std::wstring wcommand = utf8::cvt<std::wstring>(command); 354 core->submit_simple_message(wchannel, wcommand, c, wmessage, wperf); 355 } 356 void script_wrapper::command_wrapper::submit(std::string channel, std::string command, std::string request) { 357 std::wstring wchannel = utf8::cvt<std::wstring>(channel); 358 std::wstring wcommand = utf8::cvt<std::wstring>(command); 359 core->submit_message(wchannel, wcommand, request); 293 360 } 294 361 … … 297 364 std::wstring msg, perf; 298 365 int ret = core->simple_query(utf8::cvt<std::wstring>(command), convert(args), msg, perf); 299 return make_tuple( ret,utf8::cvt<std::string>(msg), utf8::cvt<std::string>(perf));366 return make_tuple(nagios_return_to_py(ret),utf8::cvt<std::string>(msg), utf8::cvt<std::string>(perf)); 300 367 } 301 368 tuple script_wrapper::command_wrapper::query(std::string command, std::string request) { -
modules/PythonScript/script_wrapper.hpp
ra44cb15 ra14aa07 6 6 using namespace boost::python; 7 7 8 9 8 enum status { 10 9 OK = NSCAPI::returnOK, … … 13 12 UNKNOWN = NSCAPI::returnUNKNOWN, 14 13 }; 14 15 status nagios_return_to_py(int code); 16 int py_to_nagios_return(status code); 15 17 16 18 void log_exception(); … … 23 25 24 26 struct functions { 25 typedef std::map<std::string, PyObject*> function_map_type;27 typedef std::map<std::string,boost::python::handle<> > function_map_type; 26 28 function_map_type simple_functions; 27 29 function_map_type normal_functions; … … 70 72 void subscribe_function(std::string channel, PyObject* callable); 71 73 void subscribe_simple_function(std::string channel, PyObject* callable); 72 int exec_simple(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const;73 int exec(const std::string wcmd, const std::string &request, std::string &response) const;74 int handle_simple_query(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const; 75 int handle_query(const std::string wcmd, const std::string &request, std::string &response) const; 74 76 bool has_function(const std::string command); 75 77 bool has_simple(const std::string command); … … 102 104 public: 103 105 static boost::shared_ptr<command_wrapper> create() { 106 NSC_DEBUG_MSG_STD(_T("<<<CREATING NEW>>>")); 104 107 return boost::shared_ptr<command_wrapper>(new command_wrapper(nscapi::plugin_singleton->get_core())); 105 108 } … … 110 113 tuple exec(std::string command, std::string request); 111 114 void simple_submit(std::string channel, std::string command, status code, std::string message, std::string perf); 112 void submit( ) {}115 void submit(std::string channel, std::string command, std::string request); 113 116 }; 114 117 -
scripts/python/test.py
ra44cb15 ra14aa07 1 1 from NSCP import Settings, Registry, Core, log, status 2 from types import * 2 3 #import sys 3 4 #sys.path.append('D:/source/nscp/build/x64/scripts/python/include') … … 8 9 9 10 prefix = 'py_' 11 plugin_id = 0 10 12 11 13 def get_help(arguments): 12 14 return (status.OK, 'help: Get help') 13 15 16 class Callable: 17 def __init__(self, anycallable): 18 self.__call__ = anycallable 19 20 class ChannelTest: 21 instance = None 22 channel = '' 23 reg = None 24 25 last_channel = '' 26 last_command = '' 27 last_status = status.UNKNOWN 28 last_message = '' 29 last_perf = '' 30 31 instance = None 32 class SingletonHelper: 33 def __call__( self, *args, **kw ) : 34 if ChannelTest.instance is None : 35 object = ChannelTest() 36 ChannelTest.instance = object 37 return ChannelTest.instance 38 39 getInstance = SingletonHelper() 40 41 def desc(self): 42 return 'Testing that channels work' 43 44 def test_submission_handler_001(channel, command, code, message, perf): 45 instance = ChannelTest.getInstance() 46 instance.last_channel = channel 47 instance.last_command = command 48 instance.last_status = code 49 instance.last_message = message 50 instance.last_perf = perf 51 test_submission_handler_001 = Callable(test_submission_handler_001) 52 53 def test_command_handler_001(arguments): 54 instance = ChannelTest.getInstance() 55 return (instance.last_status, '%s'%instance.last_message, '%s'%instance.last_perf) 56 test_command_handler_001 = Callable(test_command_handler_001) 57 58 def setup(self, plugin_id, prefix): 59 self.channel = '_%stest_channel'%prefix 60 self.reg = Registry.get(plugin_id) 61 self.reg.simple_subscription('%s_001'%self.channel, ChannelTest.test_submission_handler_001) 62 self.reg.simple_function('%s_001'%self.channel, ChannelTest.test_command_handler_001, 'This is a sample command') 63 64 def teardown(self): 65 None 66 #self.reg.unregister_simple_subscription('%s_001'%self.channel) 67 #self.reg.unregister_simple_function('%s_001'%self.channel) 68 69 def test_simple(self, channel, command, code, message, perf, tag): 70 core.simple_submit('%s'%channel, '%s'%command, code, '%s'%message, '%s'%perf) 71 (retcode, retmessage, retperf) = core.simple_query(channel, []) 72 isok = True 73 if retcode != code: 74 log('FAILED - Test did not get the correct retuirn code: %s = %s (%s)'%(retcode, code, retmessage)) 75 isok = False 76 if retmessage != message: 77 log('FAILED - Test did not get the correct retuirn code: %s = %s'%(retmessage, message)) 78 isok = False 79 if retperf != perf: 80 log('FAILED - Test did not get the correct retuirn code: %s = %s'%(retperf, perf)) 81 isok = False 82 if isok: 83 log('OK - Test successfull: %s'%tag) 84 return 0 85 return 1 86 87 88 def run_test(self): 89 count = 0 90 count += self.test_simple('%s_001'%self.channel, 'foobar', status.OK, 'qwerty', '', 'simple ok') 91 count += self.test_simple('%s_001'%self.channel, 'foobar', status.WARNING, 'foobar', '', 'simple warning') 92 count += self.test_simple('%s_001'%self.channel, 'foobar', status.CRITICAL, 'test', '', 'simple critical') 93 count += self.test_simple('%s_001'%self.channel, 'foobar', status.UNKNOWN, '1234567890', '', 'simple unknown') 94 count += self.test_simple('%s_001'%self.channel, 'foobar', status.OK, 'qwerty', "'foo'=5%", 'simple performance data 001') 95 count += self.test_simple('%s_001'%self.channel, 'foobar', status.OK, 'qwerty', "'foo'=5%;10", 'simple performance data 002') 96 count += self.test_simple('%s_001'%self.channel, 'foobar', status.OK, 'qwerty', "'foo'=5%;10;23", 'simple performance data 003') 97 count += self.test_simple('%s_001'%self.channel, 'foobar', status.OK, 'qwerty', "'foo'=5%;10;23;10;78", 'simple performance data 004') 98 count += self.test_simple('%s_001'%self.channel, 'foobar', status.OK, 'qwerty', "'foo'=5%;10;23;10;78 'bar'=1k;2;3", 'simple performance data 005') 99 if count > 0: 100 log("ERROR: %d tests failed"%count) 101 else: 102 log("OK: all tests successfull") 103 return (count, 9) 104 14 105 15 106 def test_cmd(arguments): … … 23 114 log('Data: %d %s %s'%(code, message, perf)) 24 115 116 def run_test(cls): 117 instance = cls.getInstance() 118 instance.setup(plugin_id, prefix) 119 ret = instance.run_test() 120 instance.teardown() 121 log('Tested %s (%s of %s)'%(instance.desc(), ret[0], ret[1])) 122 return ret 123 124 def run_tests(list): 125 all_failed = 0 126 all_count = 0 127 for c in list: 128 (failed, count) = run_test(c) 129 all_failed += failed 130 all_count += count 131 return (all_failed, all_count) 132 25 133 def test(arguments): 26 134 global prefix 27 log('inside test') 135 global plugin_id 136 137 run_tests([ChannelTest]) 138 28 139 for a in arguments: 29 140 log('Got argument: %s'%a) … … 99 210 return (status.OK, response.SerializeToString()) 100 211 101 def init(plugin_id, plugin_alias, script_alias): 102 global prefix 212 def init(pid, plugin_alias, script_alias): 213 global prefix 214 global plugin_id 215 plugin_id = pid 103 216 if script_alias: 104 217 prefix = '%s_'%script_alias … … 128 241 129 242 core.simple_submit('%stest'%prefix, 'test.py', status.WARNING, 'hello', '') 243 core.simple_submit('test', 'test.py', status.WARNING, 'hello', '') 130 244 131 245 (ret, list) = core.simple_exec('%stest'%prefix, ['a', 'b', 'c']) -
service/logger.hpp
r81e420c ra14aa07 66 66 67 67 namespace ip = boost::interprocess; 68 const int max_message_size = 1024;68 const int max_message_size = 4096; 69 69 const std::string queue_name = "logging_queue"; 70 70 … … 299 299 } 300 300 if (data.size() >= max_message_size) { 301 log_fatal_error("Message to large to fit buffer: " + data);301 log_fatal_error("Message to large to fit buffer: " + to_string(data.size()) + " > " + to_string(max_message_size)); 302 302 return; 303 303 } -
version.hpp
r3b11e65 ra14aa07 1 1 #ifndef VERSION_HPP 2 2 #define VERSION_HPP 3 #define PRODUCTVER 0,4,0,8 54 #define STRPRODUCTVER "0,4,0,8 5"5 #define STRPRODUCTDATE "2011-0 8-16"3 #define PRODUCTVER 0,4,0,86 4 #define STRPRODUCTVER "0,4,0,86" 5 #define STRPRODUCTDATE "2011-09-03" 6 6 #endif // VERSION_HPP -
version.txt
rfe75eff ra14aa07 1 1 version=0.4.0 2 build=8 53 date=2011-0 8-162 build=86 3 date=2011-09-03
Note: See TracChangeset
for help on using the changeset viewer.








