Changeset a14aa07 in nscp


Ignore:
Timestamp:
09/03/11 08:17:18 (21 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
4cac39f
Parents:
a44cb15
Message:
  • Fixed several issues in PythonScript
  • PythonScript supports loading multiple instances as well as scripts
  • Added some basic channel test to test.py script (the idea is that this will become "unit tests" eventually)
Files:
22 edited

Legend:

Unmodified
Added
Removed
  • changelog

    ra44cb15 ra14aa07  
    55 * Fixa dependonservice LanManWorkStation (old win) 
    66 * Fix RtlStringFromGUID problem on NT4 
     7 
     82011-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) 
    712 
    8132011-09-01 MickeM 
  • include/nscapi/nscapi_plugin_wrapper.cpp

    ra44cb15 ra14aa07  
    3838using namespace nscp::helpers; 
    3939 
    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 
     41nscapi::helper_singleton::helper_singleton() : core_(new nscapi::core_wrapper()), plugin_(new nscapi::plugin_wrapper()) {} 
    6142 
    6243/** 
     
    9172  return GET_CORE()->load_endpoints(f)?NSCAPI::isSuccess:NSCAPI::hasFailed; 
    9273} 
    93 /** 
    94 * Wrap the GetModuleName function call 
    95 * @param buf Buffer to store the module name 
    96 * @param bufLen Length of buffer 
    97 * @param str String to store inside the buffer 
    98 * @  copy status 
    99 */ 
    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 call 
    106  * @param *major Major version number 
    107  * @param *minor Minor version number 
    108  * @param *revision Revision 
    109  * @param version version as a module_version 
    110  * @return NSCAPI::success 
    111  */ 
    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 call 
    120  * @param has true if this module has a command handler 
    121  * @return NSCAPI::istrue or NSCAPI::isfalse 
    122  */ 
    123 NSCAPI::boolReturn nscapi::plugin_wrapper::wrapHasCommandHandler(bool has) { 
    124   return has?NSCAPI::istrue:NSCAPI::isfalse; 
    125 } 
    126 /** 
    127  * Wrap the HasMessageHandler function call 
    128  * @param has true if this module has a message handler 
    129  * @return NSCAPI::istrue or NSCAPI::isfalse 
    130  */ 
    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 call 
    150  * @param retResult The returned result 
    151  * @param retMessage The returned message 
    152  * @param retPerformance The returned performance data 
    153  * @param *returnBufferMessage The return message buffer 
    154  * @param returnBufferMessageLen The return message buffer length 
    155  * @param *returnBufferPerf The return performance data buffer 
    156  * @param returnBufferPerfLen The return performance data buffer length 
    157  * @return the return code 
    158  */ 
    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 call 
    185  * @param success true if module load was successfully 
    186  * @return NSCAPI::success or NSCAPI::failed 
    187  */ 
    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 call 
    195  * @param success true if module load was successfully 
    196  * @return NSCAPI::success or NSCAPI::failed 
    197  */ 
    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 } 
    20674 
    20775 
    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 */ 
    24176void nscapi::impl::simple_log_handler::handleMessageRAW(std::string data) { 
    24277  try { 
     
    25590} 
    25691 
    257 NSCAPI::nagiosReturn nscapi::impl::simple_command::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) { 
     92NSCAPI::nagiosReturn nscapi::impl::simple_command_handler::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) { 
    25893  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
    25994  std::wstring msg, perf; 
     
    273108} 
    274109 
    275 NSCAPI::nagiosReturn nscapi::impl::SimpleNotificationHandler::handleRAWNotification(const wchar_t* channel, const wchar_t* command, std::string result) { 
     110NSCAPI::nagiosReturn nscapi::impl::simple_submission_handler::handleRAWNotification(const wchar_t* channel, const wchar_t* command, std::string result) { 
    276111  try { 
    277112    std::wstring msg, perf; 
  • include/nscapi/nscapi_plugin_wrapper.hpp

    ra44cb15 ra14aa07  
    120120    }; 
    121121 
    122     class SimpleNotificationHandler { 
     122    class simple_submission_handler { 
    123123    public: 
    124124      NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, const wchar_t* command, std::string result); 
     
    127127    }; 
    128128 
    129     class simple_command { 
     129    class simple_command_handler { 
    130130    public: 
    131131      NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 
     
    145145    }; 
    146146 
    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     */ 
    154147  }; 
    155148 
  • modules/CheckDisk/CheckDisk.h

    ra44cb15 ra14aa07  
    2525#include <checkHelpers.hpp> 
    2626 
    27 class CheckDisk : public nscapi::impl::simple_command, public nscapi::impl::simple_plugin  { 
     27class CheckDisk : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin  { 
    2828private: 
    2929  bool show_errors_; 
  • modules/CheckEventLog/CheckEventLog.h

    ra44cb15 ra14aa07  
    2727 
    2828 
    29 class CheckEventLog : public nscapi::impl::simple_command, public nscapi::impl::simple_plugin { 
     29class CheckEventLog : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 
    3030private: 
    3131  bool debug_; 
  • modules/CheckHelpers/CheckHelpers.h

    ra44cb15 ra14aa07  
    2323#include <strEx.h> 
    2424 
    25 class CheckHelpers : public nscapi::impl::simple_command, public nscapi::impl::simple_plugin { 
     25class CheckHelpers : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 
    2626private: 
    2727 
  • modules/CheckNSCP/CheckNSCP.h

    r81e420c ra14aa07  
    3333NSC_WRAPPERS_MAIN(); 
    3434 
    35 class CheckNSCP : public nscapi::impl::simple_command, public nscapi::impl::simple_plugin, public nscapi::impl::simple_log_handler { 
     35class CheckNSCP : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_log_handler { 
    3636private: 
    3737  boost::timed_mutex mutex_; 
  • modules/CheckSystem/CheckSystem.h

    r81e420c ra14aa07  
    2828NSC_WRAPPERS_CLI(); 
    2929 
    30 class CheckSystem : public nscapi::impl::simple_command, public nscapi::impl::simple_plugin { 
     30class CheckSystem : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 
    3131private: 
    3232  CheckMemory memoryChecker; 
  • modules/CheckTaskSched/CheckTaskSched.h

    r81e420c ra14aa07  
    2828#include "TaskSched.h" 
    2929 
    30 class CheckTaskSched : public nscapi::impl::simple_command, public nscapi::impl::simple_plugin { 
     30class CheckTaskSched : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 
    3131private: 
    3232  std::wstring syntax; 
  • modules/CheckTaskSched2/CheckTaskSched2.h

    r81e420c ra14aa07  
    2828#include "TaskSched.h" 
    2929 
    30 class CheckTaskSched2 : public nscapi::impl::simple_command, public nscapi::impl::simple_plugin { 
     30class CheckTaskSched2 : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 
    3131private: 
    3232  std::wstring syntax; 
  • modules/CheckWMI/CheckWMI.h

    r81e420c ra14aa07  
    8888 
    8989 
    90 class CheckWMI : public nscapi::impl::simple_command, public nscapi::impl::simple_plugin { 
     90class CheckWMI : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin { 
    9191public: 
    9292  CheckWMI(); 
  • modules/LUAScript/LUAScript.h

    r81e420c ra14aa07  
    3131 
    3232 
    33 class LUAScript : public nscapi::impl::simple_command, public script_wrapper::lua_handler, public nscapi::impl::simple_plugin { 
     33class LUAScript : public nscapi::impl::simple_command_handler, public script_wrapper::lua_handler, public nscapi::impl::simple_plugin { 
    3434private: 
    3535 
  • modules/NRPEClient/NRPEClient.h

    ra44cb15 ra14aa07  
    2828 
    2929 
    30 class NRPEClient : public nscapi::impl::simple_command, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 
     30class NRPEClient : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 
    3131private: 
    3232  typedef enum { 
  • modules/NSCAAgent/NSCAAgent.h

    ra44cb15 ra14aa07  
    2424NSC_WRAPPERS_CHANNELS(); 
    2525 
    26 class NSCAAgent : public nscapi::impl::SimpleNotificationHandler, public nscapi::impl::simple_plugin { 
     26class NSCAAgent : public nscapi::impl::simple_submission_handler, public nscapi::impl::simple_plugin { 
    2727private: 
    2828 
  • modules/NSCPClient/NSCPClient.h

    ra44cb15 ra14aa07  
    2828 
    2929 
    30 class NSCPClient : public nscapi::impl::simple_command, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 
     30class NSCPClient : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 
    3131private: 
    3232  struct nscp_connection_data { 
  • modules/PythonScript/PythonScript.cpp

    ra44cb15 ra14aa07  
    7777    .def("simple_subscription", &script_wrapper::function_wrapper::subscribe_simple_function) 
    7878    ; 
    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<>()) 
    8080    .def("get",&script_wrapper::command_wrapper::create) 
    8181    .staticmethod("get") 
     
    147147  } 
    148148} 
    149  
     149static bool has_init = false; 
    150150bool PythonScript::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
    151151  alias_ = alias; 
     
    167167    settings.notify(); 
    168168 
    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 
    170176    try { 
    171177 
     
    174180      PyRun_SimpleString("sys.stderr = cStringIO.StringIO()"); 
    175181 
    176       initNSCP(); 
     182      if (do_init) 
     183        initNSCP(); 
    177184 
    178185      BOOST_FOREACH(script_container &script, scripts_) { 
     
    288295  std::string cmd = utf8::cvt<std::string>(command); 
    289296  if (inst->has_function(cmd)) { 
    290     return inst->exec(cmd, request, response); 
     297    return inst->handle_query(cmd, request, response); 
    291298  } 
    292299  if (inst->has_simple(cmd)) { 
    293300    nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(command, request); 
    294301    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); 
    296303    nscapi::functions::create_simple_query_response(data.command, ret, msg, perf, response); 
    297304    return ret; 
  • modules/PythonScript/script_wrapper.cpp

    ra44cb15 ra14aa07  
    2222 
    2323void 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  } 
    3035} 
    3136 
     
    3338  try { 
    3439    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; 
    3643  } catch (const std::exception &e) { 
    3744    NSC_LOG_ERROR_STD(_T("Failed to subscribe to channel ") + utf8::cvt<std::wstring>(channel) + _T(": ") + utf8::cvt<std::wstring>(e.what())); 
     
    4350  try { 
    4451    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; 
    4654  } catch (const std::exception &e) { 
    4755    NSC_LOG_ERROR_STD(_T("Failed to subscribe to channel ") + utf8::cvt<std::wstring>(channel) + _T(": ") + utf8::cvt<std::wstring>(e.what())); 
     
    5563  try { 
    5664    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; 
    5867  } catch (...) { 
    5968    NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name)); 
     
    6372  try { 
    6473  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; 
    6676  } catch (...) { 
    6777    NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name)); 
     
    7080void script_wrapper::function_wrapper::register_simple_cmdline(std::string name, PyObject* callable) { 
    7181  try { 
    72     functions::get()->simple_cmdline[name] = callable; 
     82    boost::python::handle<> h(boost::python::borrowed(callable)); 
     83    functions::get()->simple_cmdline[name] = h; 
    7384  } catch (...) { 
    7485    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name)); 
     
    7788void script_wrapper::function_wrapper::register_cmdline(std::string name, PyObject* callable) { 
    7889  try { 
    79     functions::get()->normal_cmdline[name] = callable; 
     90    boost::python::handle<> h(boost::python::borrowed(callable)); 
     91    functions::get()->normal_cmdline[name] = h; 
    8092  } catch (...) { 
    8193    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name)); 
    8294  } 
    8395} 
    84 int script_wrapper::function_wrapper::exec(const std::string cmd, const std::string &request, std::string &response) const { 
     96int script_wrapper::function_wrapper::handle_query(const std::string cmd, const std::string &request, std::string &response) const { 
    8597  try { 
    8698    functions::function_map_type::iterator it = functions::get()->normal_functions.find(cmd); 
     
    89101      return NSCAPI::returnIgnored; 
    90102    } 
    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); 
    92104    if (ret.ptr() == Py_None) { 
    93105      return NSCAPI::returnUNKNOWN; 
     
    105117} 
    106118 
    107 int script_wrapper::function_wrapper::exec_simple(const std::string cmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const { 
     119int script_wrapper::function_wrapper::handle_simple_query(const std::string cmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const { 
    108120  try { 
    109121    functions::function_map_type::iterator it = functions::get()->simple_functions.find(cmd); 
     
    117129      l.append(utf8::cvt<std::string>(a)); 
    118130    } 
    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     
    120140    if (ret.ptr() == Py_None) { 
    121141      msg = _T("None"); 
     
    125145    if (len(ret) > 0) 
    126146      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    } 
    131165    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()); 
    135172    return NSCAPI::returnUNKNOWN; 
    136173  } 
     
    151188      return NSCAPI::returnIgnored; 
    152189    } 
    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); 
    154191    if (ret.ptr() == Py_None) { 
    155192      return NSCAPI::returnUNKNOWN; 
     
    176213    } 
    177214 
    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)); 
    179216    if (ret.ptr() == Py_None) { 
    180217      result = _T("None"); 
     
    209246      return NSCAPI::returnIgnored; 
    210247    } 
    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); 
    212249    if (ret.ptr() == Py_None) { 
    213250      return NSCAPI::returnUNKNOWN; 
     
    218255    return NSCAPI::returnUNKNOWN; 
    219256  } 
     257} 
     258script_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} 
     270int 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; 
    220282} 
    221283 
     
    227289      return NSCAPI::returnIgnored; 
    228290    } 
    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)); 
    231292    if (ret.ptr() == Py_None) { 
    232293      return NSCAPI::returnUNKNOWN; 
     
    279340  return ret; 
    280341} 
    281  
    282342void script_wrapper::command_wrapper::simple_submit(std::string channel, std::string command, status code, std::string message, std::string perf) { 
    283343  NSCAPI::nagiosReturn c = NSCAPI::returnUNKNOWN; 
     
    290350  std::wstring wmessage = utf8::cvt<std::wstring>(message); 
    291351  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} 
     356void 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); 
    293360} 
    294361 
     
    297364  std::wstring msg, perf; 
    298365  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)); 
    300367} 
    301368tuple script_wrapper::command_wrapper::query(std::string command, std::string request) { 
  • modules/PythonScript/script_wrapper.hpp

    ra44cb15 ra14aa07  
    66  using namespace boost::python; 
    77   
    8  
    98  enum status { 
    109    OK = NSCAPI::returnOK,  
     
    1312    UNKNOWN = NSCAPI::returnUNKNOWN,  
    1413  }; 
     14 
     15  status nagios_return_to_py(int code); 
     16  int py_to_nagios_return(status code); 
    1517 
    1618  void log_exception(); 
     
    2325 
    2426  struct functions { 
    25     typedef std::map<std::string,PyObject*> function_map_type; 
     27    typedef std::map<std::string,boost::python::handle<> > function_map_type; 
    2628    function_map_type simple_functions; 
    2729    function_map_type normal_functions; 
     
    7072    void subscribe_function(std::string channel, PyObject* callable); 
    7173    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; 
    7476    bool has_function(const std::string command); 
    7577    bool has_simple(const std::string command); 
     
    102104  public: 
    103105    static boost::shared_ptr<command_wrapper> create() { 
     106      NSC_DEBUG_MSG_STD(_T("<<<CREATING NEW>>>")); 
    104107      return boost::shared_ptr<command_wrapper>(new command_wrapper(nscapi::plugin_singleton->get_core())); 
    105108    } 
     
    110113    tuple exec(std::string command, std::string request); 
    111114    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); 
    113116  }; 
    114117 
  • scripts/python/test.py

    ra44cb15 ra14aa07  
    11from NSCP import Settings, Registry, Core, log, status 
     2from types import * 
    23#import sys 
    34#sys.path.append('D:/source/nscp/build/x64/scripts/python/include') 
     
    89 
    910prefix = 'py_' 
     11plugin_id = 0 
    1012 
    1113def get_help(arguments): 
    1214  return (status.OK, 'help: Get help') 
    1315 
     16class Callable: 
     17  def __init__(self, anycallable): 
     18    self.__call__ = anycallable 
     19 
     20class 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     
    14105   
    15106def test_cmd(arguments): 
     
    23114  log('Data: %d %s %s'%(code, message, perf)) 
    24115 
     116def 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 
     124def 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 
    25133def test(arguments): 
    26134  global prefix 
    27   log('inside test') 
     135  global plugin_id 
     136 
     137  run_tests([ChannelTest]) 
     138 
    28139  for a in arguments: 
    29140    log('Got argument: %s'%a) 
     
    99210  return (status.OK, response.SerializeToString()) 
    100211 
    101 def init(plugin_id, plugin_alias, script_alias): 
    102   global prefix 
     212def init(pid, plugin_alias, script_alias): 
     213  global prefix 
     214  global plugin_id 
     215  plugin_id = pid 
    103216  if script_alias: 
    104217    prefix = '%s_'%script_alias 
     
    128241 
    129242  core.simple_submit('%stest'%prefix, 'test.py', status.WARNING, 'hello', '') 
     243  core.simple_submit('test', 'test.py', status.WARNING, 'hello', '') 
    130244   
    131245  (ret, list) = core.simple_exec('%stest'%prefix, ['a', 'b', 'c']) 
  • service/logger.hpp

    r81e420c ra14aa07  
    6666 
    6767    namespace ip = boost::interprocess; 
    68     const int max_message_size = 1024; 
     68    const int max_message_size = 4096; 
    6969    const std::string queue_name = "logging_queue"; 
    7070 
     
    299299        } 
    300300        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)); 
    302302          return; 
    303303        } 
  • version.hpp

    r3b11e65 ra14aa07  
    11#ifndef VERSION_HPP 
    22#define VERSION_HPP 
    3 #define PRODUCTVER     0,4,0,85 
    4 #define STRPRODUCTVER  "0,4,0,85" 
    5 #define STRPRODUCTDATE "2011-08-16" 
     3#define PRODUCTVER     0,4,0,86 
     4#define STRPRODUCTVER  "0,4,0,86" 
     5#define STRPRODUCTDATE "2011-09-03" 
    66#endif // VERSION_HPP 
  • version.txt

    rfe75eff ra14aa07  
    11version=0.4.0 
    2 build=85 
    3 date=2011-08-16 
     2build=86 
     3date=2011-09-03 
Note: See TracChangeset for help on using the changeset viewer.