Changeset 2c95d22 in nscp


Ignore:
Timestamp:
08/15/11 00:02:39 (21 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
b9498ef
Parents:
65a2940
Message:

2011-08-14 MickeM

  • Rename Function to Registry in PythonScript API as well as some other function renames
  • Started to clean up the helpers around the API
  • Added support for execute to PythonScripts? to execute commands
  • BUG: just realised that static plugin instances prevent multiple instances :) Will fix but not now as it is not important (for me)...
  • Added initial support for channels to PythonScript Core still lacks support for subscribing to arbitrary channel
Files:
26 edited

Legend:

Unmodified
Added
Removed
  • changelog

    r65a2940 r2c95d22  
    1010 * NRPEClient now works on linux 
    1111 * 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 
    1219 
    13202011-08-13 MickeM 
  • include/NSCAPI.h

    r39c73cd r2c95d22  
    2424#include <unicode_char.hpp> 
    2525#include <string> 
     26#include <strEx.h> 
    2627 
    2728namespace NSCAPI { 
     
    111112 
    112113 
    113   class nscapi_exception { 
     114  class nscapi_exception : public std::exception { 
    114115  public: 
    115116    std::wstring msg_; 
    116117    nscapi_exception(std::wstring msg) : msg_(msg) {} 
     118    std::string what() { 
     119      return utf8::cvt<std::string>(msg_); 
     120    } 
    117121  }; 
    118122 
     
    133137    typedef NSCAPI::errorReturn (*lpNSAPIExit)(void); 
    134138    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 *); 
    135140    typedef void (*lpNSAPIDestroyBuffer)(char**); 
    136141 
  • include/nscapi/functions.hpp

    r39c73cd r2c95d22  
    113113      std::wstring command; 
    114114      std::list<std::wstring> args; 
    115       std::vector<std::wstring> args_vector; 
     115      //std::vector<std::wstring> args_vector; 
    116116    }; 
    117117 
    118118     
    119     static decoded_simple_command_data process_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) { 
    120120      decoded_simple_command_data data; 
    121121 
     
    129129      ::ExecuteCommand::Request payload = request_message.payload().Get(0); 
    130130      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))); 
    132132      } 
    133133      return data; 
    134134    } 
    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) { 
    136136      ExecuteCommand::ResponseMessage response_message; 
    137137      ::ExecuteCommand::Header* hdr = response_message.mutable_header(); 
     
    166166      return data; 
    167167    } 
     168 
    168169    static NSCAPI::nagiosReturn process_simple_command_result(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &response) { 
    169170      PluginCommand::ResponseMessage response_message; 
     
    183184      return ret; 
    184185    } 
     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 
    185243    static void parse_performance_data(PluginCommand::Response *resp, std::wstring &perf) { 
    186  
    187244      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'\'')); 
    188245      BOOST_FOREACH(std::wstring s, tok) { 
  • include/nscapi/nscapi_core_wrapper.cpp

    rafd42f1 r2c95d22  
    106106 * @return The returned status of the command 
    107107 */ 
    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)  
     108NSCAPI::nagiosReturn nscapi::core_wrapper::query(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)  
    109109{ 
    110110  if (!fNSAPIInject) 
     
    113113} 
    114114 
     115NSCAPI::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 
    115122void nscapi::core_wrapper::DestroyBuffer(char**buffer) { 
    116123  if (!fNSAPIDestroyBuffer) 
     
    118125  return fNSAPIDestroyBuffer(buffer); 
    119126} 
     127 
     128 
     129void 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 
    120135 
    121136NSCAPI::errorReturn nscapi::core_wrapper::NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::string result) { 
     
    134149* @return The return of the command 
    135150*/ 
    136 NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSimpleCommand(const std::wstring command, const std::list<std::wstring> argument, std::wstring & msg, std::wstring & perf)  
     151NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & argument, std::wstring & msg, std::wstring & perf)  
    137152{ 
    138153  if (!fNSAPIInject) 
    139154    throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); 
    140  
    141155  std::string response; 
    142   NSCAPI::nagiosReturn ret = InjectCommand(command, argument, response); 
     156  NSCAPI::nagiosReturn ret = simple_query(command, argument, response); 
    143157  if (!response.empty()) { 
    144158    PluginCommand::ResponseMessage rsp_msg; 
     
    163177* @return The return of the command 
    164178*/ 
    165 NSCAPI::nagiosReturn nscapi::core_wrapper::InjectCommand(const std::wstring command, const std::list<std::wstring> argument, std::string & result)  
     179NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & argument, std::string & result)  
    166180{ 
    167181  if (!fNSAPIInject) 
    168182    throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); 
    169  
    170183 
    171184  PluginCommand::RequestMessage message; 
     
    184197  message.SerializeToString(&request); 
    185198 
    186   return InjectCommand(command.c_str(), request, result); 
    187 } 
    188  
    189 NSCAPI::nagiosReturn nscapi::core_wrapper::InjectCommand(const std::wstring command, std::string request, std::string & result)  
     199  return query(command.c_str(), request, result); 
     200} 
     201 
     202NSCAPI::nagiosReturn nscapi::core_wrapper::query(const std::wstring & command, const std::string & request, std::string & result)  
    190203{ 
    191204  if (!fNSAPIInject) 
     
    193206  char *buffer = NULL; 
    194207  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); 
    196209 
    197210  if (buffer_size > 0 && buffer != NULL) { 
     
    215228  return retC; 
    216229} 
    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 
     232NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query_from_nrpe(const std::wstring command, const std::wstring & buffer, std::wstring & message, std::wstring & perf) { 
    267233  if (!fNSAPIInject) 
    268234    throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); 
     
    271237  BOOST_FOREACH(std::wstring s, tok) 
    272238    arglist.push_back(s); 
    273   return InjectSimpleCommand(command.c_str(), arglist, message, perf); 
    274 } 
     239  return simple_query(command, arglist, message, perf); 
     240} 
     241 
     242NSCAPI::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} 
     266NSCAPI::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 
    275274 
    276275 
     
    609608  //fNSAPIExit = (nscapi::core_api::lpNSAPIExit)f(_T("NSAPIExit")); 
    610609  fNSAPIInject = (nscapi::core_api::lpNSAPIInject)f(_T("NSAPIInject")); 
     610  fNSAPIExecCommand = (nscapi::core_api::lpNSAPIExecCommand)f(_T("NSAPIExecCommand")); 
    611611  fNSAPIDestroyBuffer = (nscapi::core_api::lpNSAPIDestroyBuffer)f(_T("NSAPIDestroyBuffer")); 
    612612  fNSAPINotify = (nscapi::core_api::lpNSAPINotify)f(_T("NSAPINotify")); 
  • include/nscapi/nscapi_core_wrapper.hpp

    r04ef932 r2c95d22  
    4949    nscapi::core_api::lpNSAPIExit fNSAPIExit; 
    5050    nscapi::core_api::lpNSAPIInject fNSAPIInject; 
     51    nscapi::core_api::lpNSAPIExecCommand fNSAPIExecCommand; 
    5152    nscapi::core_api::lpNSAPIDestroyBuffer fNSAPIDestroyBuffer; 
    5253    nscapi::core_api::lpNSAPINotify fNSAPINotify; 
     
    132133 
    133134    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); 
    135135    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); 
    139147    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); 
    143151    void StopService(void); 
    144152    void Exit(void); 
  • include/nscapi/nscapi_plugin_wrapper.cpp

    r39c73cd r2c95d22  
    257257 
    258258NSCAPI::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::process_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); 
    260260  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); 
    263265} 
    264266 
  • include/nscapi/nscapi_plugin_wrapper.hpp

    r39c73cd r2c95d22  
    120120    public: 
    121121      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; 
    123123    }; 
    124124 
  • libs/protobuf/exec.proto

    r39c73cd r2c95d22  
    6868  required Code result = 9; 
    6969  required string message = 10; 
    70   repeated ResponseData perf = 12; 
    71    
    7270} 
    7371 
  • modules/CheckExternalScripts/CheckExternalScripts.cpp

    rd66ccee r2c95d22  
    180180    std::wstring message; 
    181181    try { 
    182       return GET_CORE()->InjectCommand(cd.command, args, response); 
     182      return GET_CORE()->simple_query(cd.command, args, response); 
    183183    } catch (boost::escaped_list_error &e) { 
    184184      NSC_LOG_MESSAGE(_T("Failed to parse alias expression: ") + strEx::string_to_wstring(e.what())); 
    185185      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); 
    187187    } 
    188188  } else { 
  • modules/CheckHelpers/CheckHelpers.cpp

    re11d494 r2c95d22  
    9595    } 
    9696    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); 
    9898    return NSCAPI::returnOK; 
    9999  } else if (command == _T("checkalwayscritical")) { 
     
    103103    } 
    104104    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); 
    106106    return NSCAPI::returnCRIT; 
    107107  } else if (command == _T("checkalwayswarning")) { 
     
    111111    } 
    112112    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); 
    114114    return NSCAPI::returnWARN; 
    115115  } else if (command == _T("checkok")) { 
     
    159159    std::list<std::wstring> sub_args; 
    160160    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); 
    162162    returnCode = nscapi::plugin_helper::maxState(returnCode, tRet); 
    163163    if (!message.empty()) 
     
    233233  std::list<std::wstring> cmd_args_l(cmd_args.begin(), cmd_args.end()); 
    234234 
    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); 
    236236  switch (tRet) { 
    237237    case NSCAPI::returnOK: 
     
    252252public: 
    253253  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); 
    255255  } 
    256256  std::wstring msg; 
  • modules/LUAScript/script_wrapper.hpp

    r04ef932 r2c95d22  
    207207        std::wstring message; 
    208208        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); 
    210210        push_code(L, ret); 
    211211        lua_pushstring(L, strEx::wstring_to_string(message).c_str()); 
  • modules/NRPEClient/NRPEClient.cpp

    r65a2940 r2c95d22  
    238238} 
    239239 
    240 int NRPEClient::commandLineExec(const std::wstring &command, std::vector<std::wstring> &arguments, std::wstring &result) { 
    241   NSC_DEBUG_MSG_STD(_T("===> ") + command); 
     240int NRPEClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 
    242241  if (command != _T("query") && command != _T("help")) 
    243     return NSCAPI::returnUNKNOWN; 
     242    return NSCAPI::returnIgnored; 
    244243  try { 
    245244    NRPEClient::nrpe_connection_data command_data; 
     
    250249    add_options(desc, command_data); 
    251250 
     251    std::vector<std::wstring> vargs(arguments.begin(), arguments.end()); 
    252252    po::positional_options_description p; 
    253253    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(); 
    255255    po::store(parsed, vm); 
    256256    po::notify(vm); 
  • modules/NRPEClient/NRPEClient.h

    r65a2940 r2c95d22  
    126126  bool hasMessageHandler(); 
    127127  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); 
    129129  std::wstring getConfigurationMeta(); 
    130130 
  • modules/NRPEServer/handler_impl.cpp

    r7443b58 r2c95d22  
    3333  try { 
    3434    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); 
    3636    NSC_DEBUG_MSG_STD(_T("Running command: ") + cmd.first + _T(" = ") + msg); 
    3737  } catch (...) { 
  • modules/NSClientServer/handler_impl.cpp

    rb8c44b4 r2c95d22  
    122122 
    123123  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); 
    125125  if (!nscapi::plugin_helper::isNagiosReturnCode(ret)) { 
    126126    if (message.empty()) 
  • modules/PythonScript/PythonScript.cpp

    r39c73cd r2c95d22  
    6767    .def("register_key", &script_wrapper::settings_wrapper::settings_register_key) 
    6868    ; 
    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) 
    7070    .def("get",&script_wrapper::function_wrapper::create) 
    7171    .staticmethod("get") 
    7272    .def("create",&script_wrapper::function_wrapper::create) 
    7373    .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) 
    7676    .def("cmdline", &script_wrapper::function_wrapper::register_cmdline) 
    7777    .def("simple_cmdline", &script_wrapper::function_wrapper::register_simple_cmdline) 
    78     .def("subscribe", &script_wrapper::function_wrapper::subscribe_function) 
    79     .def("subscribe_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) 
    8080    ; 
    8181  class_<script_wrapper::command_wrapper, boost::shared_ptr<script_wrapper::command_wrapper> >("Core", no_init) 
     
    9999    ; 
    100100  def("log", script_wrapper::log_msg); 
     101  def("get_alias", script_wrapper::get_alias); 
    101102} 
    102103 
     
    143144 
    144145bool PythonScript::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
     146  alias_ = alias; 
    145147  NSC_DEBUG_MSG_STD(_T("LoadEx in PythonScript as ") + alias); 
    146148  try { 
     
    230232} 
    231233bool PythonScript::hasMessageHandler() { 
    232   return false; 
    233 } 
     234  return true; 
     235} 
     236bool PythonScript::hasNotificationHandler() { 
     237  return true; 
     238} 
     239 
    234240 
    235241 
     
    262268  std::string cmd = utf8::cvt<std::string>(char_command); 
    263269  if (inst->has_cmdline(cmd)) { 
    264     return inst->exec_cmdline(cmd, request, response); 
     270    return inst->handle_exec(cmd, request, response); 
    265271  } 
    266272  if (inst->has_simple_cmdline(cmd)) { 
    267     nscapi::functions::decoded_simple_command_data data = nscapi::functions::process_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); 
    268274    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); 
    271277  } 
    272278  return NSCAPI::returnIgnored; 
     
    295301} 
    296302 
     303 
     304NSCAPI::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 
    297319NSC_WRAP_DLL(); 
    298320NSC_WRAPPERS_MAIN_DEF(gPythonScript); 
     
    300322NSC_WRAPPERS_HANDLE_CMD_DEF(gPythonScript); 
    301323NSC_WRAPPERS_CLI_DEF(gPythonScript); 
     324NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF(gPythonScript); 
  • modules/PythonScript/PythonScript.h

    r65a2940 r2c95d22  
    2121NSC_WRAPPERS_MAIN(); 
    2222NSC_WRAPPERS_CLI(); 
     23NSC_WRAPPERS_CHANNELS(); 
    2324 
    2425#include <config.h> 
     
    4950  typedef std::list<boost::shared_ptr<python_script> > instance_list_type; 
    5051  instance_list_type instances_; 
     52  std::wstring alias_; 
    5153 
    5254public: 
     
    5961  bool unloadModule(); 
    6062  bool reload(std::wstring &msg); 
     63  std::wstring get_alias() { 
     64    return alias_; 
     65  } 
    6166 
    6267  std::wstring getModuleName() { 
     
    7378  bool hasCommandHandler(); 
    7479  bool hasMessageHandler(); 
     80  bool hasNotificationHandler(); 
    7581  bool loadScript(std::wstring alias, std::wstring script); 
    7682  //NSCAPI::nagiosReturn handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf); 
     
    7884  NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 
    7985  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); 
    8087 
    8188  //NSCAPI::nagiosReturn RunLUA(const unsigned int argLen, wchar_t **char_args, std::wstring &message, std::wstring &perf); 
  • modules/PythonScript/script_wrapper.cpp

    r39c73cd r2c95d22  
    33#include <strEx.h> 
    44#include "script_wrapper.hpp" 
     5#include "PythonScript.h" 
    56 
    67using namespace boost::python; 
    78 
    89boost::shared_ptr<script_wrapper::functions> script_wrapper::functions::instance; 
     10 
     11extern PythonScript gPythonScript; 
    912 
    1013 
    1114void script_wrapper::log_msg(std::wstring x) { 
    1215  NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(x)); 
     16} 
     17std::string script_wrapper::get_alias() { 
     18  return utf8::cvt<std::string>(gPythonScript.get_alias()); 
    1319} 
    1420 
     
    2127  PyErr_Clear(); 
    2228} 
     29 
     30void script_wrapper::function_wrapper::subscribe_simple_function(std::string channel, PyObject* callable) { 
     31  functions::get()->simple_handler[channel] = callable; 
     32} 
     33void script_wrapper::function_wrapper::subscribe_function(std::string channel, PyObject* callable) { 
     34  functions::get()->normal_handler[channel] = callable; 
     35} 
     36 
     37 
    2338void script_wrapper::function_wrapper::register_simple_function(std::string name, PyObject* callable, std::string desc) { 
    2439  try { 
     
    113128} 
    114129 
    115 int script_wrapper::function_wrapper::exec_cmdline(const std::string cmd, const std::string &request, std::string &response) const { 
     130int script_wrapper::function_wrapper::handle_exec(const std::string cmd, const std::string &request, std::string &response) const { 
    116131  try { 
    117132    functions::function_map_type::iterator it = functions::get()->normal_cmdline.find(cmd); 
     
    136151} 
    137152 
    138 int script_wrapper::function_wrapper::exec_simple_cmdline(const std::string cmd, std::list<std::wstring> arguments, std::wstring &result) const { 
     153int script_wrapper::function_wrapper::handle_simple_exec(const std::string cmd, std::list<std::wstring> arguments, std::wstring &result) const { 
    139154  try { 
    140155    functions::function_map_type::iterator it = functions::get()->simple_cmdline.find(cmd); 
     
    145160    } 
    146161 
    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)); 
    152163    if (ret.ptr() == Py_None) { 
    153164      result = _T("None"); 
     
    167178} 
    168179 
     180 
     181bool 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} 
     184bool 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 
     188int 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 
     206int 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 
    169230bool script_wrapper::function_wrapper::has_cmdline(const std::string command) { 
    170231  return functions::get()->normal_cmdline.find(command) != functions::get()->normal_cmdline.end(); 
     
    189250 
    190251 
    191 std::list<std::wstring> script_wrapper::command_wrapper::convert(list lst) { 
     252std::list<std::wstring> script_wrapper::convert(list lst) { 
    192253  std::list<std::wstring> ret; 
    193254  for (int i = 0;i<len(lst);i++) 
     
    195256  return ret; 
    196257} 
     258list 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 
     266void 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 
    197271tuple script_wrapper::command_wrapper::simple_query(std::string command, list args) { 
    198272  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); 
    200274  return make_tuple(ret,utf8::cvt<std::string>(msg), utf8::cvt<std::string>(perf)); 
    201275} 
    202276tuple script_wrapper::command_wrapper::query(std::string command, std::string request) { 
    203277  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); 
    205279  return make_tuple(ret,response); 
     280} 
     281 
     282object 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} 
     295tuple 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); 
    206299} 
    207300 
  • modules/PythonScript/script_wrapper.hpp

    r39c73cd r2c95d22  
    1616  void log_exception(); 
    1717  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 
    1823 
    1924  struct functions { 
     
    2530    function_map_type normal_cmdline; 
    2631 
     32    function_map_type simple_handler; 
     33    function_map_type normal_handler; 
    2734 
    2835    static boost::shared_ptr<functions> instance; 
     
    4956    function_wrapper(nscapi::core_wrapper* core) : core(core) {} 
    5057    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; 
    5459 
    5560 
     
    6267    void register_simple_function(std::string name, PyObject* callable, std::string desc); 
    6368    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); 
    6671    int exec_simple(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const; 
    6772    int exec(const std::string wcmd, const std::string &request, std::string &response) const; 
     
    6974    bool has_simple(const std::string command); 
    7075 
    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; 
    7378    bool has_cmdline(const std::string command); 
    7479    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); 
    7586 
    7687    std::wstring get_commands(); 
     
    93104    } 
    94105 
    95     std::list<std::wstring> convert(boost::python::list lst); 
    96106    tuple simple_query(std::string command, boost::python::list args); 
    97107    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); 
    101111    void submit() {} 
    102112  }; 
  • modules/Scheduler/Scheduler.cpp

    re11d494 r2c95d22  
    156156  try { 
    157157    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); 
    159159    if (nscapi::report::matches(item.report, code)) { 
    160160      GET_CORE()->NotifyChannel(item.channel, item.alias, code, response); 
  • scripts/python/test.py

    r39c73cd r2c95d22  
    1 from NSCP import Settings, Functions, Core, log, status 
     1from NSCP import Settings, Registry, Core, log, status, get_alias 
    22 
    33core = Core.get() 
    4  
    54 
    65def get_help(arguments): 
    76  return (status.OK, 'help: Get help') 
    87 
     8   
     9def test_cmd(arguments): 
     10  global prefix 
     11  log('inside test_cmd') 
     12  return (status.OK, 'The command works: %s (%d)'%(prefix, len(arguments))) 
     13 
     14def test_channel(channel, command, code, message, perf): 
     15  log('inside test_channel: %s'%channel) 
     16  log('Data: %d %s %s'%(code, message, perf)) 
     17 
    918prefix = 'py_' 
    1019def test(arguments): 
     20  global prefix 
    1121  log('inside test') 
    1222  for a in arguments: 
     
    4959   
    5060def init(alias): 
     61  global prefix 
    5162  if alias: 
    5263    prefix = '%s_'%alias 
    5364 
    54   log('Hello World') 
     65  log('Script: test.py with alias: %s from %s'%(alias, get_alias())) 
    5566 
    5667  conf = Settings.get() 
     
    6071   
    6172  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') 
    6879 
    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) 
    7090 
    7191  log('Testing to register settings keys') 
  • service/NSClient++.cpp

    r65a2940 r2c95d22  
    825825 
    826826 
    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 */ 
     831void 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 
     890void 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 */ 
     921NSClientT::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 
     965std::wstring NSClientT::describeCommand(std::wstring command) { 
     966  return commands_.describe(command); 
     967} 
     968std::list<std::wstring> NSClientT::getAllCommandNames() { 
     969  return commands_.list(); 
     970} 
     971void NSClientT::registerCommand(unsigned int id, std::wstring cmd, std::wstring desc) { 
     972  return commands_.register_command(id, cmd, desc); 
     973} 
     974 
     975NSCAPI::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 */ 
     1041NSCAPI::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 
     1087int NSClientT::simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp) { 
    8281088  bool found = false; 
    8291089  std::vector<std::string> responses; 
     
    9151175 
    9161176 
    917 /** 
    918  * Unload all plug-ins (in reversed order) 
    919  */ 
    920 void NSClientT::unloadPlugins(bool unloadLoggers) { 
     1177NSCAPI::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; 
    9211180  { 
    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)); 
    9321182    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_); 
    9461198        } 
    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")); 
    9511199      } 
    9521200    } 
    9531201  } 
    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 
    11741223 
    11751224 
  • service/NSClient++.h

    r39c73cd r2c95d22  
    166166  std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args); 
    167167  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); 
    169170 
    170171  struct service_controller { 
  • service/cli_parser.hpp

    r39c73cd r2c95d22  
    309309      } 
    310310      std::vector<std::wstring> resp; 
    311       mainClient.command_line_exec(module, command, arguments, resp); 
     311      mainClient.simple_exec(module, command, arguments, resp); 
    312312      mainClient.exitCore(false); 
    313313 
  • service/core_api.cpp

    r54ac968 r2c95d22  
    119119  return ret; 
    120120} 
     121 
     122NSCAPI::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 
    121137NSCAPI::errorReturn NSAPIGetSettingsSection(const wchar_t* section, wchar_t*** aBuffer, unsigned int * bufLen) { 
    122138  try { 
     
    418434  if (wcscasecmp(buffer, _T("NSAPIInject")) == 0) 
    419435    return reinterpret_cast<LPVOID>(&NSAPIInject); 
     436  if (wcscasecmp(buffer, _T("NSAPIExecCommand")) == 0) 
     437    return reinterpret_cast<LPVOID>(&NSAPIExecCommand); 
    420438  if (wcscasecmp(buffer, _T("NSAPIGetBasePath")) == 0) 
    421439    return reinterpret_cast<LPVOID>(&NSAPIGetBasePath); 
  • service/core_api.h

    r1ecd26f r2c95d22  
    3939void NSAPIStopServer(void); 
    4040NSCAPI::nagiosReturn NSAPIInject(const wchar_t* command, const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len); 
     41NSCAPI::nagiosReturn NSAPIExecCommand(const wchar_t* command, const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len); 
    4142NSCAPI::errorReturn NSAPIGetSettingsSection(const wchar_t*, wchar_t***, unsigned int *); 
    4243NSCAPI::errorReturn NSAPIReleaseSettingsSectionBuffer(wchar_t*** aBuffer, unsigned int * bufLen); 
Note: See TracChangeset for help on using the changeset viewer.