Changeset 39c73cd in nscp


Ignore:
Timestamp:
08/13/11 22:38:21 (22 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
65a2940
Parents:
f881d47
Message:

2011-08-13 MickeM

  • Added support for command line execution to PythonScript module
  • Readded support for specifying module on command line with --client mode
  • Fixed some issues with the NRPEClient module


2011-08-12 MickeM

  • Finnished (rough) adding back command line exec (with modern API)
  • Fixed so installer uses correct name for dll:s (now Server not Listsener)
Files:
2 added
24 edited

Legend:

Unmodified
Added
Removed
  • changelog

    r54ac968 r39c73cd  
    77 * Fix RtlStringFromGUID problem on NT4 
    88 
    9 2011-08-10 
     92011-08-13 MickeM 
     10 * Added support for command line execution to PythonScript module 
     11 * Readded support for specifying module on command line with --client mode 
     12 * Fixed some issues with the NRPEClient module 
     13  
     142011-08-12 MickeM 
     15  * Finnished (rough) adding back command line exec (with modern API) 
     16  * Fixed so installer uses correct name for dll:s (now Server not Listsener) 
     17   
     182011-08-11 MickeM 
     19  * Started to add back command line exec (with modern API) 
     20   
     212011-08-10 MickeM 
    1022 * Fixed linus issues and defect 
    1123 * Fixed some issues in the test python script regarding how prefix is handled 
     
    1527 * Started to merge som features from PythonScript back to Lua script 
    1628  
    17 2011-08-07 
     292011-08-07 MickeM 
    1830 * Fixed a lot of issues with PythonScript module adding suport for alias and "raw command processing" 
    1931 * Fixed issue with loading plugins and aliases as well as duplicate plugin detection 
    2032  
    21 2011-08-01 
     332011-08-01 MickeM 
    2234 * Added new module: PythonScript 
    2335 
    24 2011-07-25 
     362011-07-25 MickeM 
    2537 * merged in all 0.3.9 changes 
    2638 * refactored where filter to be "non template" to drastically reduce compile time (as well as potentially size if I ever go dll instead of static link) 
  • helpers/installers/installer/Product.wxs

    r04ef932 r39c73cd  
    111111          <Directory Id="INSTALLLOCATION_MODS" Name="modules"> 
    112112            <Component Id="NRPEServer" Guid="5A0246F8-5167-45db-B246-$(var.Postfix.GUID)" Win64="$(var.Win64)"> 
    113               <File Id="NRPEListenerDLL" Name="NRPEListener.dll" DiskId="1" Source="$(var.Source)/modules/NRPEServer.dll" Vital="no" /> 
     113              <File Id="NRPEServerDLL" Name="NRPEServer.dll" DiskId="1" Source="$(var.Source)/modules/NRPEServer.dll" Vital="no" /> 
    114114              <File Id="NRPEClientDLL" Name="NRPEClient.dll" DiskId="1" Source="$(var.Source)/modules/NRPEClient.dll" Vital="no" /> 
    115115            </Component> 
    116             <Component Id="NSCListener" Guid="6DAF8BB9-9A56-48f5-B2C5-$(var.Postfix.GUID)" Win64="$(var.Win64)"> 
    117               <File Id="NSCListenerDLL" Name="NSClientListener.dll" DiskId="1" Source="$(var.Source)/modules/NSClientServer.dll" Vital="no" /> 
     116            <Component Id="NSCServer" Guid="6DAF8BB9-9A56-48f5-B2C5-$(var.Postfix.GUID)" Win64="$(var.Win64)"> 
     117              <File Id="NSCServerDLL" Name="NSClientServer.dll" DiskId="1" Source="$(var.Source)/modules/NSClientServer.dll" Vital="no" /> 
    118118            </Component> 
    119119            <Component Id="NSCA" Guid="8820A304-C596-4393-A72F-$(var.Postfix.GUID)" Win64="$(var.Win64)"> 
     
    199199          <ComponentRef Id="Plugins" /> 
    200200        </Feature> 
    201         <Feature Id="NRPEPlugins" Title="NRPE Support" Description="NRPE Listener Plugin. Support for the more vercitile NRPE protocol (check_nrpe)" Level="1" Absent="disallow"> 
     201        <Feature Id="NRPEPlugins" Title="NRPE Support" Description="NRPE Server Plugin. Support for the more vercitile NRPE protocol (check_nrpe)" Level="1" Absent="disallow"> 
    202202          <ComponentRef Id="NRPEServer" /> 
    203203          <ComponentRef Id="NRPEServerCert" /> 
    204204        </Feature> 
    205         <Feature Id="NSCPlugins" Title="NSClient support" Description="NSClient Listener Plugin. Support for the old NSClient protocol (check_nt)" Level="1" Absent="disallow"> 
    206           <ComponentRef Id="NSCListener" /> 
     205        <Feature Id="NSCPlugins" Title="NSClient support" Description="NSClient Server Plugin. Support for the old NSClient protocol (check_nt)" Level="1" Absent="disallow"> 
     206          <ComponentRef Id="NSCServer" /> 
    207207        </Feature> 
    208208        <Feature Id="NSCAPlugin" Title="NSCA plugin" Description="Plugin to submit passive results to an NSCA server" Level="1" Absent="disallow"> 
  • include/NSCAPI.h

    re11d494 r39c73cd  
    166166    typedef NSCAPI::nagiosReturn (*lpHandleCommand)(const wchar_t*,const char*,const unsigned int,char**,unsigned int*); 
    167167    typedef int (*lpDeleteBuffer)(char**); 
    168     typedef int (*lpCommandLineExec)(const unsigned int,wchar_t**); 
     168    typedef int (*lpCommandLineExec)(const wchar_t*,const char*,const unsigned int,char**,unsigned int*); 
    169169    typedef int (*lpHandleMessage)(const char*); 
    170170    typedef int (*lpUnLoadModule)(); 
  • include/dll/impl_w32.hpp

    r1ecd26f r39c73cd  
    2222          return module; 
    2323        boost::filesystem::wpath mod = module / std::wstring(_T(".dll")); 
     24        if (boost::filesystem::is_regular(mod)) 
     25          return mod; 
     26        mod = boost::filesystem::wpath(module.string() + std::wstring(_T(".dll"))); 
    2427        if (boost::filesystem::is_regular(mod)) 
    2528          return mod; 
  • include/nrpe/client/socket.hpp

    r294b37b r39c73cd  
    2020    socket(boost::asio::io_service &io_service, std::wstring host, int port) { 
    2121      socket_.reset(new tcp::socket(io_service)); 
     22      connect(host, port); 
    2223    } 
    23     socket() {} 
     24    socket() { 
     25    } 
     26 
     27    virtual ~socket() { 
     28      socket_.reset(); 
     29      //get_socket().close(); 
     30    } 
     31 
    2432 
    2533    virtual boost::asio::io_service& get_io_service() { 
     
    4957    } 
    5058 
    51     ~socket() { 
    52       get_socket().close(); 
    53     } 
    54  
    5559    virtual void send(nrpe::packet &packet, boost::posix_time::seconds timeout) { 
    5660      std::vector<char> buf = packet.get_buffer(); 
    5761      write_with_timeout(buf, timeout); 
     62      get_socket().shutdown(boost::asio::ip::tcp::socket::shutdown_send); 
    5863    } 
    5964    virtual nrpe::packet recv(const nrpe::packet &packet, boost::posix_time::seconds timeout) { 
    6065      std::vector<char> buf(packet.get_packet_length()); 
    6166      read_with_timeout(buf, timeout); 
     67      get_socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both); 
     68      get_socket().close(); 
    6269      return nrpe::packet(&buf[0], buf.size(), packet.get_payload_length()); 
    6370    } 
     
    7380 
    7481#ifdef USE_SSL 
    75   class ssl_socket : public socket { 
     82  class ssl_socket : public socket, public boost::noncopyable { 
    7683  private: 
    7784    boost::shared_ptr<boost::asio::ssl::stream<tcp::socket> > ssl_socket_; 
     
    8087    ssl_socket(boost::asio::io_service &io_service, boost::asio::ssl::context &ctx, std::wstring host, int port) : socket() { 
    8188      ssl_socket_.reset(new boost::asio::ssl::stream<tcp::socket>(io_service, ctx)); 
     89      connect(host, port); 
    8290    } 
     91    virtual ~ssl_socket() { 
     92      ssl_socket_.reset(); 
     93    } 
     94 
    8395 
    8496    virtual void connect(std::wstring host, int port) { 
  • include/nscapi/functions.hpp

    rafd42f1 r39c73cd  
    3838#include <protobuf/plugin.pb.h> 
    3939#include <protobuf/log.pb.h> 
     40#include <protobuf/exec.pb.h> 
    4041 
    4142using namespace nscp::helpers; 
     
    6566      return PluginCommand::Response_Code_UNKNOWN; 
    6667    } 
     68    static ExecuteCommand::Response_Code exec_nagios_to_gpb(int ret) { 
     69      if (ret == NSCAPI::returnOK) 
     70        return ExecuteCommand::Response_Code_OK; 
     71      if (ret == NSCAPI::returnWARN) 
     72        return ExecuteCommand::Response_Code_WARNING; 
     73      if (ret == NSCAPI::returnCRIT) 
     74        return ExecuteCommand::Response_Code_CRITCAL; 
     75      return ExecuteCommand::Response_Code_UNKNOWN; 
     76    } 
    6777    static LogMessage::Message_Level log_to_gpb(NSCAPI::messageTypes ret) { 
    6878      if (ret == NSCAPI::critical) 
     
    103113      std::wstring command; 
    104114      std::list<std::wstring> args; 
     115      std::vector<std::wstring> args_vector; 
    105116    }; 
     117 
     118     
     119    static decoded_simple_command_data process_simple_command_line_exec_request(const wchar_t* char_command, const std::string &request) { 
     120      decoded_simple_command_data data; 
     121 
     122      data.command = char_command; 
     123      ExecuteCommand::RequestMessage request_message; 
     124      request_message.ParseFromString(request); 
     125 
     126      if (request_message.payload_size() != 1) { 
     127        throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
     128      } 
     129      ::ExecuteCommand::Request payload = request_message.payload().Get(0); 
     130      for (int i=0;i<payload.arguments_size();i++) { 
     131        data.args_vector.push_back(to_wstring(payload.arguments(i))); 
     132      } 
     133      return data; 
     134    } 
     135    static NSCAPI::nagiosReturn process_simple_command_line_exec_result(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring result, std::string &response) { 
     136      ExecuteCommand::ResponseMessage response_message; 
     137      ::ExecuteCommand::Header* hdr = response_message.mutable_header(); 
     138 
     139      hdr->set_type(ExecuteCommand::Header_Type_RESPONSE); 
     140      hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 
     141 
     142      ExecuteCommand::Response *resp = response_message.add_payload(); 
     143      resp->set_command(to_string(command)); 
     144      resp->set_message(to_string(result)); 
     145 
     146      resp->set_version(ExecuteCommand::Response_Version_VERSION_1); 
     147      resp->set_result(exec_nagios_to_gpb(ret)); 
     148      response_message.SerializeToString(&response); 
     149      return ret; 
     150    } 
     151 
    106152    static decoded_simple_command_data process_simple_command_request(const wchar_t* char_command, const std::string &request) { 
    107153      decoded_simple_command_data data; 
  • include/nscapi/macros.hpp

    r54ac968 r39c73cd  
    210210 
    211211#define NSC_WRAPPERS_CLI_DEF(toObject) \ 
    212   extern int NSCommandLineExec(const unsigned int argLen,wchar_t** args) { \ 
    213     try { \ 
    214       return toObject.commandLineExec(argLen, args); \ 
     212  extern int NSCommandLineExec(const wchar_t* command,const char* request_buffer,const unsigned int request_len,char** response_buffer,unsigned int* response_len) { \ 
     213    try { \ 
     214    std::string request = std::string(request_buffer, request_len); \ 
     215    std::string response; \ 
     216    NSCAPI::nagiosReturn retCode = (&toObject)->commandRAWLineExec(command, request, response); \ 
     217    return GET_PLUGIN()->wrapCommandLineExec(retCode, response, response_buffer, response_len); \ 
    215218    } catch (...) { \ 
    216219      NSC_LOG_CRITICAL(_T("Unknown exception in: commandLineExec(...)")); \ 
  • include/nscapi/nscapi_plugin_wrapper.cpp

    rafd42f1 r39c73cd  
    165165  return retResult; 
    166166} 
     167NSCAPI::nagiosReturn nscapi::plugin_wrapper::wrapCommandLineExec(NSCAPI::nagiosReturn retResult, const std::string &reply, char **reply_buffer, unsigned int *size) { 
     168  // TODO: Make this global to allow remote deletion!!! 
     169  unsigned int buf_len = reply.size(); 
     170  *reply_buffer = new char[buf_len + 10]; 
     171  memcpy(*reply_buffer, reply.c_str(), buf_len+1); 
     172  (*reply_buffer)[buf_len] = 0; 
     173  (*reply_buffer)[buf_len+1] = 0; 
     174  *size = buf_len; 
     175  return retResult; 
     176} 
    167177 
    168178/** 
     
    246256} 
    247257 
    248  
    249  
     258NSCAPI::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); 
     260  std::wstring result; 
     261  NSCAPI::nagiosReturn ret = commandLineExec(data.command, data.args_vector, result); 
     262  return nscapi::functions::process_simple_command_line_exec_result(data.command, ret, result, response); 
     263} 
    250264 
    251265NSCAPI::nagiosReturn nscapi::impl::SimpleNotificationHandler::handleRAWNotification(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, std::string result) { 
  • include/nscapi/nscapi_plugin_wrapper.hpp

    rd66ccee r39c73cd  
    2323#include <string> 
    2424#include <list> 
     25#include <vector> 
    2526 
    2627#include <boost/make_shared.hpp> 
     
    6970    int wrapUnloadModule(bool success); 
    7071    NSCAPI::nagiosReturn wrapHandleCommand(NSCAPI::nagiosReturn retResult, const std::string &reply, char **reply_buffer, unsigned int *size); 
     72    NSCAPI::nagiosReturn wrapCommandLineExec(NSCAPI::nagiosReturn retResult, const std::string &reply, char **reply_buffer, unsigned int *size); 
     73     
    7174    void wrapDeleteBuffer(char**buffer); 
    7275 
     
    114117      virtual NSCAPI::nagiosReturn handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) = 0; 
    115118    }; 
     119    class simple_command_line_exec { 
     120    public: 
     121      NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 
     122      virtual NSCAPI::nagiosReturn commandLineExec(const std::wstring &command, std::vector<std::wstring> &arguments, std::wstring &result) = 0; 
     123    }; 
    116124 
    117125 
  • include/program_options_ex.hpp

    rdcd90b2 r39c73cd  
    33#include <boost/program_options.hpp> 
    44#include <boost/function/function1.hpp> 
     5#include <list> 
    56 
    67template<class charT> 
     
    3738    {} 
    3839 
     40    basic_command_line_parser_ex(std::list<std::basic_string<charT> > &v)  
     41      : boost::program_options::basic_command_line_parser<charT>( 
     42      make_vector<std::list<std::basic_string<charT> >::iterator, std::list<std::basic_string<charT> >::iterator>(v.begin(), v.end()) 
     43      ) 
     44    {} 
     45 
    3946}; 
  • libs/protobuf/CMakeLists.txt

    r3bdaf18 r39c73cd  
    33WRAP_PROTO(PROTO_PLUGIN_SRC plugin.proto) 
    44WRAP_PROTO(PROTO_LOG_SRC log.proto) 
     5WRAP_PROTO(PROTO_EXEC_SRC exec.proto) 
     6WRAP_PROTO(PROTO_SETTINGS_SRC settings.proto) 
    57 
    68SET(TARGET protobuf) 
     
    911  plugin.proto 
    1012  log.proto 
     13  exec.proto 
     14  settings.proto 
    1115   
    1216  ${PROTO_PLUGIN_SRC} 
    1317  ${PROTO_LOG_SRC} 
     18  ${PROTO_EXEC_SRC} 
     19  ${PROTO_SETTINGS_SRC} 
    1420) 
    1521 
  • modules/NRPEClient/NRPEClient.cpp

    r7ec3dd1 r39c73cd  
    110110 
    111111    settings.alias().add_path_to_settings() 
    112       (_T("EXTERNAL SCRIPT SECTION"), _T("Section for external scripts configuration options (CheckExternalScripts).")) 
    113  
    114       (_T("handlers"), sh::fun_values_path(boost::bind(&NRPEClient::addCommand, this, _1, _2)),  
     112//      (_T("EXTERNAL SCRIPT SECTION"), _T("Section for external scripts configuration options (CheckExternalScripts).")) 
     113 
     114      (_T("handlers"), sh::fun_values_path(boost::bind(&NRPEClient::add_command, this, _1, _2)),  
    115115      _T("CLIENT HANDLER SECTION"), _T("")) 
     116 
     117      (_T("servers"), sh::fun_values_path(boost::bind(&NRPEClient::add_server, this, _1, _2)),  
     118      _T("REMOTE SERVER DEFINITIONS"), _T("")) 
    116119 
    117120      ; 
     
    132135    return false; 
    133136  } 
    134   return true; 
    135137 
    136138  boost::filesystem::wpath p = GET_CORE()->getBasePath() + std::wstring(_T("/security/nrpe_dh_512.pem")); 
     
    147149void NRPEClient::add_options(po::options_description &desc, nrpe_connection_data &command_data) { 
    148150  desc.add_options() 
    149     ("help,h", "Show this help message.") 
    150151    ("host,H", po::wvalue<std::wstring>(&command_data.host), "The address of the host running the NRPE daemon") 
    151152    ("port,p", po::value<int>(&command_data.port), "The port on which the daemon is running (default=5666)") 
     
    158159} 
    159160 
    160  
    161 void NRPEClient::addCommand(std::wstring key, std::wstring args) { 
     161void NRPEClient::add_server(std::wstring key, std::wstring args) { 
     162} 
     163 
     164void NRPEClient::add_command(std::wstring key, std::wstring args) { 
    162165  try { 
    163166 
     
    235238} 
    236239 
    237 int NRPEClient::commandLineExec(const unsigned int argLen, wchar_t** args) { 
     240int NRPEClient::commandLineExec(const std::wstring &command, std::vector<std::wstring> &arguments, std::wstring &result) { 
     241  if (command != _T("query") && command != _T("help")) 
     242    return NSCAPI::returnUNKNOWN; 
    238243  try { 
    239  
    240244    NRPEClient::nrpe_connection_data command_data; 
    241245    boost::program_options::variables_map vm; 
     
    247251    po::positional_options_description p; 
    248252    p.add("arguments", -1); 
    249     po::wparsed_options parsed = basic_command_line_parser_ex<wchar_t>(argLen, args).options(desc).positional(p).run(); 
     253    po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(arguments).options(desc).positional(p).run(); 
    250254    po::store(parsed, vm); 
    251255    po::notify(vm); 
    252256    command_data.parse_arguments(); 
    253  
    254     if (vm.count("help")) { 
    255       std::cout << desc << "\n"; 
    256       return 1; 
     257    if (command == _T("help")) { 
     258      std::stringstream ss; 
     259      ss << "NRPEClient Command line syntax for command: query" << std::endl;; 
     260      ss << desc; 
     261      result = utf8::cvt<std::wstring>(ss.str()); 
     262      return NSCAPI::returnOK; 
    257263    } 
    258     nrpe_result_data result = execute_nrpe_command(command_data, command_data.arguments); 
    259     std::wcout << result.text << std::endl; 
    260     return result.result; 
     264 
     265    nrpe_result_data res = execute_nrpe_command(command_data, command_data.arguments); 
     266    result = res.text; 
     267    return res.result; 
    261268  } catch (boost::program_options::validation_error &e) { 
    262     std::cout << e.what() << std::endl; 
     269    result = _T("Error: ") + utf8::cvt<std::wstring>(e.what()); 
     270    return NSCAPI::returnUNKNOWN; 
    263271  } catch (...) { 
    264     std::cout << "Unknown exception parsing command line" << std::endl; 
     272    result = _T("Unknown exception parsing command line"); 
     273    return NSCAPI::returnUNKNOWN; 
    265274  } 
    266275  return NSCAPI::returnUNKNOWN; 
     
    282291    return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("NRPE Packet errro: ") + e.getMessage()); 
    283292  } catch (std::runtime_error &e) { 
    284     return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("Socket error: ") + boost::lexical_cast<std::wstring>(e.what())); 
     293    NSC_LOG_ERROR_STD(_T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 
     294    return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 
    285295  } catch (...) { 
    286296    return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("Unknown error -- REPORT THIS!")); 
     
    298308  nrpe::client::ssl_socket socket(io_service, ctx, host, port); 
    299309  socket.send(packet, boost::posix_time::seconds(timeout)); 
    300   return socket.recv(packet, boost::posix_time::seconds(timeout)); 
     310  nrpe::packet ret = socket.recv(packet, boost::posix_time::seconds(timeout)); 
     311  return ret; 
    301312} 
    302313#endif 
     
    309320} 
    310321 
    311 /* 
    312 NRPEPacket NRPEClient::send_nossl(std::wstring host, int port, int timeout, NRPEPacket packet) 
    313 { 
    314   unsigned char dh512_p[] = { 
    315     0xCF, 0xFF, 0x65, 0xC2, 0xC8, 0xB4, 0xD2, 0x68, 0x8C, 0xC1, 0x80, 0xB1, 
    316     0x7B, 0xD6, 0xE8, 0xB3, 0x62, 0x59, 0x62, 0xED, 0xA7, 0x45, 0x6A, 0xF8, 
    317     0xE9, 0xD8, 0xBE, 0x3F, 0x38, 0x42, 0x5F, 0xB2, 0xA5, 0x36, 0x03, 0xD3, 
    318     0x06, 0x27, 0x81, 0xC8, 0x9B, 0x88, 0x50, 0x3B, 0x82, 0x3D, 0x31, 0x45, 
    319     0x2C, 0xB4, 0xC5, 0xA5, 0xBE, 0x6A, 0xE3, 0x2E, 0xA6, 0x86, 0xFD, 0x6A, 
    320     0x7E, 0x1E, 0x6A, 0x73, 
    321   }; 
    322   unsigned char dh512_g[] = { 0x02, }; 
    323  
    324   DH *dh_2 = DH_new(); 
    325   dh_2->p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL); 
    326   dh_2->g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL); 
    327  
    328   FILE *outfile = fopen("d:\\nrpe_512.pem", "w"); 
    329   PEM_write_DHparams(outfile, dh_2); 
    330   PEM_write_DHparams(stdout, dh_2); 
    331   fclose(outfile); 
    332  
    333   nrpe_socket socket(host, port); 
    334   socket.send(packet, boost::posix_time::seconds(timeout)); 
    335   return socket.recv(packet, boost::posix_time::seconds(timeout)); 
    336 } 
    337 */ 
    338  
    339  
    340  
    341  
    342  
    343322NSC_WRAP_DLL(); 
    344323NSC_WRAPPERS_MAIN_DEF(gNRPEClient); 
  • modules/NRPEClient/NRPEClient.h

    r3bdaf18 r39c73cd  
    2525 
    2626 
    27 class NRPEClient : public nscapi::impl::SimpleCommand, nscapi::impl::simple_plugin { 
     27class NRPEClient : public nscapi::impl::SimpleCommand, nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 
    2828private: 
    2929  typedef enum { 
     
    123123  bool hasMessageHandler(); 
    124124  NSCAPI::nagiosReturn handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf); 
    125   int commandLineExec(const unsigned int argLen,wchar_t** args); 
     125  int commandLineExec(const std::wstring &command, std::vector<std::wstring> &arguments, std::wstring &result); 
    126126  std::wstring getConfigurationMeta(); 
    127127 
     
    133133 
    134134private: 
    135   void addCommand(std::wstring key, std::wstring args); 
     135  void add_command(std::wstring key, std::wstring args); 
     136  void add_server(std::wstring key, std::wstring args); 
    136137 
    137138}; 
  • modules/PythonScript/PythonScript.cpp

    rb8c44b4 r39c73cd  
    7474    .def("register", &script_wrapper::function_wrapper::register_function) 
    7575    .def("register_simple", &script_wrapper::function_wrapper::register_simple_function) 
     76    .def("cmdline", &script_wrapper::function_wrapper::register_cmdline) 
     77    .def("simple_cmdline", &script_wrapper::function_wrapper::register_simple_cmdline) 
    7678    .def("subscribe", &script_wrapper::function_wrapper::subscribe_function) 
    7779    .def("subscribe_simple", &script_wrapper::function_wrapper::subscribe_simple_function) 
     
    129131    dict globalDict = extract<dict>(main_module.attr("__dict__")); 
    130132    localDict = globalDict.copy(); 
     133 
     134    PyRun_SimpleString("import cStringIO"); 
     135    PyRun_SimpleString("import sys"); 
     136    PyRun_SimpleString("sys.stderr = cStringIO.StringIO()"); 
     137 
    131138    object ignored = exec_file(scriptfile.c_str(), localDict, localDict);  
    132139  } catch( error_already_set e) { 
     
    136143 
    137144bool PythonScript::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
     145  NSC_DEBUG_MSG_STD(_T("LoadEx in PythonScript as ") + alias); 
    138146  try { 
    139147    root_ = get_core()->getBasePath(); 
     
    214222bool PythonScript::unloadModule() { 
    215223  instances_.clear(); 
    216   Py_Finalize(); 
     224  //Py_Finalize(); 
    217225  return true; 
    218226} 
     
    250258} 
    251259 
     260NSCAPI::nagiosReturn PythonScript::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response) { 
     261  boost::shared_ptr<script_wrapper::function_wrapper> inst = script_wrapper::function_wrapper::create(); 
     262  std::string cmd = utf8::cvt<std::string>(char_command); 
     263  if (inst->has_cmdline(cmd)) { 
     264    return inst->exec_cmdline(cmd, request, response); 
     265  } 
     266  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); 
     268    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); 
     271  } 
     272  return NSCAPI::returnIgnored; 
     273} 
     274 
    252275 
    253276NSCAPI::nagiosReturn PythonScript::handleRAWCommand(const wchar_t* command, const std::string &request, std::string &response) { 
     
    276299NSC_WRAPPERS_IGNORE_MSG_DEF(); 
    277300NSC_WRAPPERS_HANDLE_CMD_DEF(gPythonScript); 
     301NSC_WRAPPERS_CLI_DEF(gPythonScript); 
  • modules/PythonScript/PythonScript.def

    rb8c44b4 r39c73cd  
    1313  NSUnloadModule 
    1414  NSGetModuleDescription 
     15  NSCommandLineExec 
    1516  NSDeleteBuffer 
  • modules/PythonScript/PythonScript.h

    rb8c44b4 r39c73cd  
    3030#include <scripts/functions.hpp> 
    3131 
    32 struct python_script { 
     32struct python_script : public boost::noncopyable { 
    3333  boost::python::dict localDict; 
    3434  python_script(const script_container& script); 
     
    7575 
    7676  NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 
     77  NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 
    7778 
    7879  //NSCAPI::nagiosReturn RunLUA(const unsigned int argLen, wchar_t **char_args, std::wstring &message, std::wstring &perf); 
  • modules/PythonScript/script_wrapper.cpp

    rf881d47 r39c73cd  
    2525    core->registerCommand(utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc)); 
    2626    functions::get()->simple_functions[name] = callable; 
    27     NSC_LOG_MESSAGE_STD(_T("Added simple command: ") + utf8::cvt<std::wstring>(name) + _T(" to list of: ") + get_commands()); 
    28   } catch (...) { 
    29     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name)); 
     27  } catch (...) { 
     28    NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name)); 
    3029  } 
    3130} 
     
    3433  core->registerCommand(utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc)); 
    3534  functions::get()->normal_functions[name] = callable; 
    36   NSC_LOG_MESSAGE_STD(_T("Added simple command: ") + utf8::cvt<std::wstring>(name) + _T(" to list of: ") + get_commands()); 
     35  } catch (...) { 
     36    NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name)); 
     37  } 
     38} 
     39void script_wrapper::function_wrapper::register_simple_cmdline(std::string name, PyObject* callable) { 
     40  try { 
     41    functions::get()->simple_cmdline[name] = callable; 
     42  } catch (...) { 
     43    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name)); 
     44  } 
     45} 
     46void script_wrapper::function_wrapper::register_cmdline(std::string name, PyObject* callable) { 
     47  try { 
     48    functions::get()->normal_cmdline[name] = callable; 
    3749  } catch (...) { 
    3850    NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name)); 
     
    100112  return functions::get()->simple_functions.find(command) != functions::get()->simple_functions.end(); 
    101113} 
     114 
     115int script_wrapper::function_wrapper::exec_cmdline(const std::string cmd, const std::string &request, std::string &response) const { 
     116  try { 
     117    functions::function_map_type::iterator it = functions::get()->normal_cmdline.find(cmd); 
     118    if (it == functions::get()->normal_cmdline.end()) { 
     119      NSC_LOG_ERROR_STD(_T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd)); 
     120      return NSCAPI::returnIgnored; 
     121    } 
     122    tuple ret = boost::python::call<tuple>(it->second, cmd, request); 
     123    if (ret.ptr() == Py_None) { 
     124      return NSCAPI::returnUNKNOWN; 
     125    } 
     126    int ret_code = NSCAPI::returnUNKNOWN; 
     127    if (len(ret) > 0) 
     128      ret_code = extract<int>(ret[0]); 
     129    if (len(ret) > 1) 
     130      response = extract<std::string>(ret[1]); 
     131    return ret_code; 
     132  } catch( error_already_set e) { 
     133    log_exception(); 
     134    return NSCAPI::returnUNKNOWN; 
     135  } 
     136} 
     137 
     138int script_wrapper::function_wrapper::exec_simple_cmdline(const std::string cmd, std::list<std::wstring> arguments, std::wstring &result) const { 
     139  try { 
     140    functions::function_map_type::iterator it = functions::get()->simple_cmdline.find(cmd); 
     141    if (it == functions::get()->simple_cmdline.end()) { 
     142      result = _T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd); 
     143      NSC_LOG_ERROR_STD(result); 
     144      return NSCAPI::returnIgnored; 
     145    } 
     146 
     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); 
     152    if (ret.ptr() == Py_None) { 
     153      result = _T("None"); 
     154      return NSCAPI::returnUNKNOWN; 
     155    } 
     156    int ret_code = NSCAPI::returnUNKNOWN; 
     157    if (len(ret) > 0) 
     158      ret_code = extract<int>(ret[0]); 
     159    if (len(ret) > 1) 
     160      result = utf8::cvt<std::wstring>(extract<std::string>(ret[1])); 
     161    return ret_code; 
     162  } catch( error_already_set e) { 
     163    log_exception(); 
     164    result = _T("Exception in: ") + utf8::cvt<std::wstring>(cmd); 
     165    return NSCAPI::returnUNKNOWN; 
     166  } 
     167} 
     168 
     169bool script_wrapper::function_wrapper::has_cmdline(const std::string command) { 
     170  return functions::get()->normal_cmdline.find(command) != functions::get()->normal_cmdline.end(); 
     171} 
     172bool script_wrapper::function_wrapper::has_simple_cmdline(const std::string command) { 
     173  return functions::get()->simple_cmdline.find(command) != functions::get()->simple_cmdline.end(); 
     174} 
     175 
    102176std::wstring script_wrapper::function_wrapper::get_commands() { 
    103177  std::wstring str; 
  • modules/PythonScript/script_wrapper.hpp

    rb8c44b4 r39c73cd  
    2121    function_map_type simple_functions; 
    2222    function_map_type normal_functions; 
     23 
     24    function_map_type simple_cmdline; 
     25    function_map_type normal_cmdline; 
     26 
    2327 
    2428    static boost::shared_ptr<functions> instance; 
     
    5458    } 
    5559 
     60    void register_simple_cmdline(std::string name, PyObject* callable); 
     61    void register_cmdline(std::string name, PyObject* callable); 
    5662    void register_simple_function(std::string name, PyObject* callable, std::string desc); 
    5763    void register_function(std::string name, PyObject* callable, std::string desc); 
     
    6268    bool has_function(const std::string command); 
    6369    bool has_simple(const std::string command); 
     70 
     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; 
     73    bool has_cmdline(const std::string command); 
     74    bool has_simple_cmdline(const std::string command); 
     75 
    6476    std::wstring get_commands(); 
    6577  }; 
  • scripts/python/test.py

    r54ac968 r39c73cd  
    22 
    33core = Core.get() 
     4 
     5 
     6def get_help(arguments): 
     7  return (status.OK, 'help: Get help') 
    48 
    59prefix = 'py_' 
     
    6367  fun.register_simple('%snor'%prefix, no_ret, 'No performance data') 
    6468 
     69  fun.simple_cmdline('help', get_help) 
     70 
    6571  log('Testing to register settings keys') 
    6672  conf.register_path('hello', 'PYTHON SETTINGS', 'This is stuff for python') 
  • service/NSCPlugin.cpp

    r04ef932 r39c73cd  
    435435 
    436436 
    437 int NSCPlugin::commandLineExec(const unsigned int argLen, wchar_t **arguments) { 
    438   if (fCommandLineExec== NULL) 
    439     throw NSPluginException(module_, _T("Module does not support CommandLineExec")); 
    440   try { 
    441     return fCommandLineExec(argLen, arguments); 
    442   } catch (...) { 
    443     throw NSPluginException(module_, _T("Unhandled exception in commandLineExec.")); 
    444   } 
    445 } 
    446  
    447 bool NSCPlugin::is_duplicate( boost::filesystem::wpath file, std::wstring alias ) { 
     437int NSCPlugin::commandLineExec(const wchar_t* command, std::string &request, std::string &reply) { 
     438  char *buffer = NULL; 
     439  unsigned int len = 0; 
     440  NSCAPI::nagiosReturn ret = commandLineExec(command, request.c_str(), request.size(), &buffer, &len); 
     441  if (buffer != NULL) { 
     442    reply = std::string(buffer, len); 
     443    deleteBuffer(&buffer); 
     444  } 
     445  return ret; 
     446} 
     447 
     448bool NSCPlugin::has_command_line_exec() { 
     449  return isLoaded() && fCommandLineExec != NULL; 
     450} 
     451 
     452int NSCPlugin::commandLineExec(const wchar_t* command, const char* request, const unsigned int request_len, char** reply, unsigned int *reply_len) { 
     453  if (!has_command_line_exec()) 
     454    throw NSPluginException(module_, _T("Library is not loaded or modules does not support command line")); 
     455  try { 
     456    return fCommandLineExec(command, request, request_len, reply, reply_len); 
     457  } catch (...) { 
     458    throw NSPluginException(module_, _T("Unhandled exception in handleCommand.")); 
     459  } 
     460 
     461} 
     462boost::filesystem::wpath NSCPlugin::get_filename(boost::filesystem::wpath folder, std::wstring module) { 
     463  return dll::dll::fix_module_name(folder / module); 
     464} 
     465bool NSCPlugin::is_duplicate(boost::filesystem::wpath file, std::wstring alias) { 
    448466  if (alias.empty() && alias_.empty()) 
    449467    return module_.get_file() == dll::dll::fix_module_name(file); 
  • service/NSCPlugin.h

    r1ecd26f r39c73cd  
    145145  void unload(void); 
    146146  std::wstring getCongifurationMeta(); 
    147   int commandLineExec(const unsigned int argLen, wchar_t **arguments); 
     147  int commandLineExec(const wchar_t* command, std::string &request, std::string &reply); 
     148  int commandLineExec(const wchar_t* command, const char* request, const unsigned int request_len, char** reply, unsigned int *reply_len); 
     149  bool has_command_line_exec(); 
    148150  void showTray(); 
    149151  void hideTray(); 
    150152  bool is_duplicate( boost::filesystem::wpath file, std::wstring alias ); 
    151153 
     154  std::wstring get_alias() { 
     155    return alias_; 
     156  } 
    152157  std::wstring getFilename() { 
    153158    return module_.get_filename(); 
     
    177182  } 
    178183  unsigned int get_id() const { return plugin_id_; } 
     184  static boost::filesystem::wpath get_filename(boost::filesystem::wpath folder, std::wstring module); 
    179185 
    180186private: 
  • service/NSClient++.cpp

    rafd42f1 r39c73cd  
    4545#include "../version.hpp" 
    4646 
    47 #include "../libs/protobuf/plugin.pb.h" 
     47#include <protobuf/plugin.pb.h> 
     48#include <protobuf/exec.pb.h> 
    4849 
    4950#ifdef USE_BREAKPAD 
     
    327328//      } 
    328329//      return false; 
    329 //    } else if ( wcscasecmp( _T("version"), argv[1]+1 ) == 0 ) { 
    330 //      LOG_INFO_CORE(APPLICATION_NAME _T(", Version: ") CURRENT_SERVICE_VERSION _T(", Platform: ") SZARCH); 
    331330//    } else if ( wcscasecmp( _T("d"), argv[1]+1 ) == 0 ) { 
    332331//      // Run command from command line (like NRPE) but with debug enabled 
    333 //    } else if ( wcscasecmp( _T("noboot"), argv[1]+1 ) == 0 ) { 
    334 //      mainClient.enableDebug(false); 
    335 //      mainClient.initCore(false); 
    336 //      if (argc>=3) 
    337 //        nRetCode = mainClient.commandLineExec(argv[2], argc-3, &argv[3]); 
    338 //      else 
    339 //        nRetCode = mainClient.commandLineExec(argv[2], 0, NULL); 
    340 //      mainClient.exitCore(true); 
    341 //      return nRetCode; 
    342332//    } else if ( wcscasecmp( _T("c"), argv[1]+1 ) == 0 ) { 
    343333//      // Run command from command line (like NRPE) 
     
    361351//    } 
    362352//    return nRetCode; 
    363 //  } else if (argc > 2) { 
    364 //    std::wcout << _T(" * * * * * * * ") << std::endl; 
    365 //    mainClient.initCore(true); 
    366 //    if (argc>=3) 
    367 //      nRetCode = mainClient.commandLineExec(argv[1], argc-2, &argv[2]); 
    368 //    else 
    369 //      nRetCode = mainClient.commandLineExec(argv[1], 0, NULL); 
    370 //    mainClient.exitCore(true); 
    371 //    return nRetCode; 
    372 //  } 
    373353//  return nRetCode; 
    374354} 
     
    635615      LOG_ERROR_CORE(_T("No crash handling configured")); 
    636616    } else { 
    637       g_exception_manager->StartMonitoring(); 
     617      //g_exception_manager->StartMonitoring(); 
    638618    } 
    639619  } 
     
    762742bool NSClientT::exitCore(bool boot) { 
    763743  LOG_DEBUG_CORE(_T("Attempting to stop")); 
    764   if (boot) { 
    765     try { 
    766       LOG_DEBUG_CORE(_T("Stopping: NON Message Handling Plugins")); 
    767       mainClient.unloadPlugins(false); 
    768     } catch(NSPluginException e) { 
    769       LOG_ERROR_CORE_STD(_T("Exception raised when unloading non msg plguins: ") + e.error_ + _T(" in module: ") + e.file_); 
    770     } catch(...) { 
    771       LOG_ERROR_CORE_STD(_T("Unknown exception raised when unloading non msg plugins")); 
    772     } 
     744  try { 
     745    LOG_DEBUG_CORE(_T("Stopping: NON Message Handling Plugins")); 
     746    mainClient.unloadPlugins(false); 
     747  } catch(NSPluginException e) { 
     748    LOG_ERROR_CORE_STD(_T("Exception raised when unloading non msg plguins: ") + e.error_ + _T(" in module: ") + e.file_); 
     749  } catch(...) { 
     750    LOG_ERROR_CORE_STD(_T("Unknown exception raised when unloading non msg plugins")); 
    773751  } 
    774752#ifdef WIN32 
     
    814792    LOG_ERROR_CORE_STD(_T("UNknown exception raised: When closing shared session")); 
    815793  } 
    816   if (boot) { 
    817     try { 
    818       LOG_DEBUG_CORE(_T("Stopping: Message handling Plugins")); 
    819       mainClient.unloadPlugins(true); 
    820     } catch(NSPluginException e) { 
    821       LOG_ERROR_CORE_STD(_T("Exception raised when unloading msg plugins: ") + e.error_ + _T(" in module: ") + e.file_); 
    822     } catch(...) { 
    823       LOG_ERROR_CORE_STD(_T("UNknown exception raised: When stopping message plguins")); 
    824     } 
     794  try { 
     795    LOG_DEBUG_CORE(_T("Stopping: Message handling Plugins")); 
     796    mainClient.unloadPlugins(true); 
     797  } catch(NSPluginException e) { 
     798    LOG_ERROR_CORE_STD(_T("Exception raised when unloading msg plugins: ") + e.error_ + _T(" in module: ") + e.file_); 
     799  } catch(...) { 
     800    LOG_ERROR_CORE_STD(_T("UNknown exception raised: When stopping message plguins")); 
    825801  } 
    826802  LOG_INFO_CORE(_T("Stopped succcessfully")); 
     
    844820// Member functions 
    845821 
    846 int NSClientT::commandLineExec(const wchar_t* module, const unsigned int argLen, wchar_t** args) { 
    847   std::wstring sModule = module; 
    848   std::wstring moduleList = _T(""); 
     822 
     823int NSClientT::command_line_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp) { 
     824  bool found = false; 
     825  std::vector<std::string> responses; 
     826  ExecuteCommand::RequestMessage message; 
     827  ExecuteCommand::Header *hdr = message.mutable_header(); 
     828  hdr->set_type(ExecuteCommand::Header_Type_REQUEST); 
     829  hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 
     830 
     831  ExecuteCommand::Request *req = message.add_payload(); 
     832  req->set_command(to_string(command)); 
     833  req->set_version(ExecuteCommand::Request_Version_VERSION_1); 
     834 
     835  BOOST_FOREACH(std::wstring s, arguments) 
     836    req->add_arguments(utf8::cvt<std::string>(s)); 
     837 
     838  std::string request; 
     839  message.SerializeToString(&request); 
     840  int ret = 0; 
    849841  { 
    850842    boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(5)); 
     
    853845      return -1; 
    854846    } 
    855     for (pluginList::size_type i=0;i<plugins_.size();++i) { 
    856       plugin_type p = plugins_[i]; 
    857       if (!moduleList.empty()) 
    858         moduleList += _T(", "); 
     847    BOOST_FOREACH(plugin_type p, plugins_) { 
    859848      if (p) { 
    860         moduleList += p->getModule(); 
    861         if (p->getModule() == sModule) { 
     849        if ((module.empty() || p->getModule() == module)&&(p->has_command_line_exec())) { 
    862850          LOG_DEBUG_CORE_STD(_T("Found module: ") + p->getName() + _T("...")); 
    863851          try { 
    864             return p->commandLineExec(argLen, args); 
     852            std::string response; 
     853            ret = p->commandLineExec(command.c_str(), request, response); 
     854            found = true; 
     855            if (ret != NSCAPI::returnIgnored && !response.empty()) 
     856              responses.push_back(response); 
    865857          } catch (NSPluginException e) { 
    866858            LOG_ERROR_CORE_STD(_T("Could not execute command: ") + e.error_ + _T(" in ") + e.file_); 
     
    871863    } 
    872864  } 
    873   try { 
    874     plugin_type plugin = addPlugin(getBasePath() / boost::filesystem::wpath(_T("modules")) / boost::filesystem::wpath(module), _T("")); 
    875     if (plugin) { 
    876       LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + plugin->getName() + _T("...")); 
    877       plugin->load_plugin(NSCAPI::dontStart); 
    878       return plugin->commandLineExec(argLen, args); 
    879     } else { 
    880       LOG_ERROR_CORE_STD(_T("Failed to load: ") + std::wstring(module) + _T(" available modules are: ") + moduleList); 
     865  if (!found && !module.empty()) { 
     866    try { 
     867      //boost::filesystem::wpath root = ; 
     868      boost::filesystem::wpath file = NSCPlugin::get_filename(getBasePath() / boost::filesystem::wpath(_T("modules")), module); 
     869      if (boost::filesystem::is_regular(file)) { 
     870        plugin_type plugin = addPlugin(file, _T("")); 
     871        if (plugin) { 
     872          LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + plugin->getName() + _T("...")); 
     873          plugin->load_plugin(NSCAPI::dontStart); 
     874          std::string response; 
     875          ret = plugin->commandLineExec(command.c_str(), request, response); 
     876          if (ret != NSCAPI::returnIgnored && !response.empty()) 
     877            responses.push_back(response); 
     878        } else { 
     879          LOG_ERROR_CORE_STD(_T("Failed to load: ") + std::wstring(module)); 
     880          return 1; 
     881        } 
     882      } else { 
     883        LOG_ERROR_CORE_STD(_T("Failed to load: ") + std::wstring(module)); 
     884        return 1; 
     885      } 
     886    } catch (const NSPluginException &e) { 
     887      resp.push_back(_T("Module (") + e.file_ + _T(") was not found: ") + e.error_); 
     888      LOG_INFO_CORE_STD(_T("Module (") + e.file_ + _T(") was not found: ") + e.error_); 
     889    } catch(const std::exception &e) { 
     890      resp.push_back(_T("Module (") + module + _T(") was not found: ") + utf8::cvt<std::wstring>(e.what())); 
     891      LOG_INFO_CORE_STD(_T("Module (") + module + _T(") was not found: ") + utf8::cvt<std::wstring>(e.what())); 
    881892      return 1; 
    882     } 
    883   } catch (NSPluginException e) { 
    884     LOG_INFO_CORE_STD(_T("Module (") + e.file_ + _T(") was not found: ") + e.error_); 
    885   } 
    886   LOG_ERROR_CORE_STD(_T("Module not found: ") + std::wstring(module) + _T(" available modules are: ") + moduleList); 
    887   return 0; 
     893    } catch(...) { 
     894      resp.push_back(_T("Module (") + module + _T(") was not found...")); 
     895      LOG_INFO_CORE_STD(_T("Module (") + module + _T(") was not found...")); 
     896      return 1; 
     897    } 
     898  } 
     899  BOOST_FOREACH(std::string &r, responses) { 
     900    ExecuteCommand::ResponseMessage rsp_msg; 
     901 
     902    rsp_msg.ParseFromString(r); 
     903    if (rsp_msg.payload_size() != 1) { 
     904      resp.push_back(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 
     905      LOG_ERROR_CORE_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 
     906      return NSCAPI::returnUNKNOWN; 
     907    } 
     908    resp.push_back(utf8::cvt<std::wstring>(rsp_msg.payload(0).message())); 
     909  } 
     910  return ret; 
    888911} 
    889912 
     
    960983    } 
    961984    for (pluginList::iterator it=plugins_.begin(); it != plugins_.end();) { 
    962       LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + (*it)->getName() + _T("...")); 
     985      LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + (*it)->getName() + _T(" as ") + (*it)->get_alias()); 
    963986      try { 
    964987        if (!(*it)->load_plugin(mode)) { 
     
    10191042    if (plugin->hasMessageHandler()) 
    10201043      logger_master_.add_plugin(plugin); 
    1021     settings_manager::get_core()->register_key(_T("/modules"), plugin->getModule(), settings::settings_core::key_string, plugin->getName(), plugin->getDescription(), _T(""), false); 
     1044    //settings_manager::get_core()->register_key(_T("/modules"), plugin->getModule(), settings::settings_core::key_string, plugin->getName(), plugin->getDescription(), _T(""), false); 
     1045    // TODO add comments elsewhere to the settings store for all loaded modules... 
    10221046  } 
    10231047  return plugin; 
  • service/NSClient++.h

    r7ec3dd1 r39c73cd  
    166166  std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args); 
    167167  void reportMessage(std::string data); 
    168   int commandLineExec(const wchar_t* module, const unsigned int argLen, wchar_t** args); 
     168  int command_line_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp); 
    169169 
    170170  struct service_controller { 
  • service/cli_parser.hpp

    r7ec3dd1 r39c73cd  
    1212  po::options_description settings; 
    1313  po::options_description service; 
     14  po::options_description client; 
    1415 
    1516public: 
     
    1920    , settings("Settings options") 
    2021    , service("Service Options") 
     22    , client("Client Options") 
    2123  { 
    2224    desc.add_options() 
    2325      ("help", "produce help message") 
    24       ("settings-help", "produce help message for the various settings related options") 
    25       ("service-help", "produce help message for the various settings related options") 
     26      ("settings-help", "Produce help message for the various settings related options") 
     27      ("service-help", "Produce help message for the various settings related service management") 
     28      ("client-help", "Produce help message for the various settings related client") 
    2629      ("settings", "Enter settings mode and handle settings related commands") 
    2730      ("service", "Enter service mode and handle service related commands") 
     31      ("client", "Enter client mode and handle client related commands") 
    2832      ("test", "Start test and debug mode") 
    2933      ("debug", "Show debug information") 
     34      ("version", "Show version information") 
    3035      ; 
    3136 
     
    5459      ; 
    5560 
     61    client.add_options() 
     62      ("command,C", po::value<std::wstring>(), "Name of command to start") 
     63      ("module,M", po::value<std::wstring>(), "Name of module to load (if not specified all modules in ini file will be loaded)") 
     64      ("arguments,A", po::value< std::vector<std::wstring> >()->multitoken(), "List of arguments")      ; 
     65 
    5666  } 
    5767  int parse(int argc, wchar_t* argv[]) { 
    5868    try { 
    5969      po::options_description all("Allowed options"); 
    60       all.add(desc).add(service).add(settings); 
     70      all.add(desc).add(service).add(settings).add(client); 
    6171 
    6272      po::variables_map vm; 
     
    7383      } 
    7484 
     85      if (vm.count("help")) { 
     86        mainClient.set_console_log(); 
     87        std::cout << all << "\n"; 
     88        return 1; 
     89      } 
     90      if (vm.count("version")) { 
     91        mainClient.set_console_log(); 
     92        std::cout << APPLICATION_NAME << _T(", Version: ") << CURRENT_SERVICE_VERSION << _T(", Platform: ") << SZARCH << "\n"; 
     93        return 1; 
     94      } 
     95      if (vm.count("settings-help")) { 
     96        mainClient.set_console_log(); 
     97        std::cout << settings << "\n"; 
     98        return 1; 
     99      } 
     100      if (vm.count("service-help")) { 
     101        mainClient.set_console_log(); 
     102        std::cout << service << "\n"; 
     103        return 1; 
     104      } 
     105      if (vm.count("client-help")) { 
     106        mainClient.set_console_log(); 
     107        std::cout << client << "\n"; 
     108        return 1; 
     109      } 
    75110 
    76111      if (vm.count("settings")) { 
     
    82117        return parse_service(argc, argv); 
    83118      } 
     119      if (vm.count("client")) { 
     120        return parse_client(argc, argv); 
     121      } 
    84122      if (vm.count("test")) { 
    85123        mainClient.set_console_log(); 
    86124        return parse_test(argc, argv); 
    87       } 
    88       if (vm.count("help")) { 
    89         mainClient.set_console_log(); 
    90         std::cout << all << "\n"; 
    91         return 1; 
    92       } 
    93       if (vm.count("settings-help")) { 
    94         mainClient.set_console_log(); 
    95         std::cout << settings << "\n"; 
    96         return 1; 
    97       } 
    98       if (vm.count("service-help")) { 
    99         mainClient.set_console_log(); 
    100         std::cout << service << "\n"; 
    101         return 1; 
    102125      } 
    103126    } catch(std::exception & e) { 
     
    244267    } 
    245268  } 
     269 
     270  int parse_client(int argc, wchar_t* argv[]) { 
     271    try { 
     272      po::options_description all("Allowed options (client)"); 
     273      all.add(desc).add(client); 
     274 
     275 
     276      po::variables_map vm; 
     277      po::wparsed_options parsed =  
     278        po::wcommand_line_parser(argc, argv).options(all).allow_unregistered().run(); 
     279      po::store(parsed, vm); 
     280      po::notify(vm); 
     281 
     282      bool debug = false; 
     283      if (vm.count("debug")) { 
     284        debug = true; 
     285      } 
     286 
     287      std::wstring command; 
     288      if (vm.count("command")) { 
     289        command = vm["command"].as<std::wstring>(); 
     290      } 
     291      std::wstring module; 
     292      if (vm.count("module")) { 
     293        module = vm["module"].as<std::wstring>(); 
     294      } 
     295      std::vector<std::wstring> arguments = po::collect_unrecognized(parsed.options, po::include_positional); 
     296 
     297      if (debug) { 
     298        mainClient.set_console_log(); 
     299        mainClient.enableDebug(true); 
     300        mainClient.log_info(__FILE__, __LINE__, _T("Module: ") + module); 
     301        mainClient.log_info(__FILE__, __LINE__, _T("Command: ") + command); 
     302        std::wstring args; 
     303        BOOST_FOREACH(std::wstring s, arguments) 
     304          strEx::append_list(args, s, _T(", ")); 
     305        mainClient.log_info(__FILE__, __LINE__, _T("Arguments: ") + args); 
     306      } 
     307      if (module.empty()) { 
     308        core_->initCore(false); 
     309      } 
     310      std::vector<std::wstring> resp; 
     311      mainClient.command_line_exec(module, command, arguments, resp); 
     312      mainClient.exitCore(false); 
     313 
     314      BOOST_FOREACH(std::wstring r, resp) { 
     315        std::wcout << r << std::endl; 
     316      } 
     317      return 0; 
     318    } catch(std::exception & e) { 
     319      mainClient.log_error(__FILE__, __LINE__, std::string("Unable to parse command line (settings): ") + e.what()); 
     320      return 1; 
     321    } 
     322  } 
    246323}; 
    247324 
Note: See TracChangeset for help on using the changeset viewer.