Changeset 0f7b655 in nscp


Ignore:
Timestamp:
02/08/12 23:42:00 (16 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
76540c3
Parents:
e396b2f
Message:
  • Added test cases for targets to NRPE
  • Added test cases for targets to NSCA
  • Added test cases for lenghts to NSCA
  • Changed to python API sleep uses milliseconds (not seconds as before) Makes NSCA unittests go much faster (as I can wait much less)
  • Fixed a lot of bugs related to target handling.
  • implemented target refactoring on all Client modules.
Files:
24 edited

Legend:

Unmodified
Added
Removed
  • changelog

    re396b2f r0f7b655  
    66 * Fix RtlStringFromGUID problem on NT4 
    77 
     82012-02-05 MickeM 
     9 * Added test cases for targets to NRPE 
     10 * Added test cases for targets to NSCA 
     11 * Added test cases for lenghts to NSCA 
     12 * Changed to python API sleep uses milliseconds (not seconds as before) 
     13   Makes NSCA unittests go much faster (as I can wait much less) 
     14 * Fixed a lot of bugs related to target handling. 
     15 * implemented target refactoring on all Client modules. 
    816 
    9172012-02-05 MickeM 
     
    2028 * Implemented full settings API for LuaScript (next RC will have to wait till next weekend) 
    2129 
    22 2012-01-31 MickeM 
     302012-01-31 MickeM1 
    2331 * Fixed issue with parsing "invalid external commands". 
    2432   If parsing falies it will notify you but use the legacy split string method instead. 
  • include/client/command_line_parser.cpp

    re396b2f r0f7b655  
    9595} 
    9696 
    97 int client::command_line_parser::do_execute_command_as_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, const std::string &request, std::string &result) { 
     97int client::command_line_parser::do_execute_command_as_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result) { 
    9898  if (!config.validate()) 
    9999    throw cli_exception("Invalid data: " + config.to_string()); 
     
    102102  } else if (command == _T("query")) { 
    103103    return do_query(config, command, arguments, result); 
    104   } else if (command == _T("forward")) { 
    105     return do_forward(config, request, result); 
    106104  } else if (command == _T("exec")) { 
    107105    int ret = do_exec(config, command, arguments, result); 
     
    136134} 
    137135 
     136 
     137int client::command_manager::process_query(std::wstring cmd, client::configuration &config, const Plugin::QueryRequestMessage &message, std::string &result) { 
     138  if (cmd == _T("forward")) 
     139    return config.handler->query(config.data, message, result); 
     140 
     141  // TODO: Loop though commands here! 
     142  if (message.payload_size() != 1) { 
     143    return nscapi::functions::create_simple_query_response_unknown(cmd, _T("Multiple payloads currently not supported"), result); 
     144  } 
     145  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(message.payload(0)); 
     146  if (client::command_line_parser::is_command(cmd)) 
     147    return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 
     148  return exec_simple(config, data.target, cmd, data.args, result); 
     149} 
     150 
     151int client::command_manager::process_exec(std::wstring cmd, client::configuration &config, const Plugin::ExecuteRequestMessage &message, std::string &result) { 
     152  if (cmd == _T("forward")) 
     153    return config.handler->exec(config.data, message, result); 
     154 
     155  // TODO: Loop though commands here! 
     156  if (message.payload_size() != 1) { 
     157    return nscapi::functions::create_simple_exec_response_unknown(cmd, std::wstring(_T("Multiple payloads currently not supported")), result); 
     158  } 
     159  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(cmd, message); 
     160  if (client::command_line_parser::is_command(cmd)) 
     161    return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     162  return exec_simple(config, data.target, cmd, data.args, result); 
     163} 
     164 
    138165int client::command_manager::exec_simple(configuration &config, const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::string &response) { 
    139166  command_type::const_iterator cit = commands.find(command); 
     
    179206  modify_header(config, message.mutable_header(), config.data->recipient); 
    180207  nscapi::functions::append_simple_query_request_payload(message.add_payload(), config.data->command, config.data->arguments); 
    181   return config.handler->query(config.data, message.mutable_header(), message.SerializeAsString(), response); 
    182 } 
    183  
    184 int client::command_line_parser::do_forward(configuration &config, const std::string &request, std::string &response) { 
    185   /* 
    186   if (!config.data->target_id.empty()) { 
    187     if (!config.target_lookup) 
    188       throw cli_exception("No target interface given when looking for targets"); 
    189     config.data->recipient.import(config.target_lookup->lookup_target(config.data->target_id)); 
    190   } 
    191   */ 
    192   Plugin::QueryRequestMessage message; 
    193   message.ParseFromString(request); 
    194   return config.handler->query(config.data, message.mutable_header(), request, response); 
     208  return config.handler->query(config.data, message, response); 
    195209} 
    196210 
     
    222236  std::string response; 
    223237  nscapi::functions::append_simple_exec_request_payload(message.add_payload(), config.data->command, config.data->arguments); 
    224   return config.handler->exec(config.data, message.mutable_header(), message.SerializeAsString(), response); 
     238  return config.handler->exec(config.data, message, response); 
    225239} 
    226240 
     
    241255    if (!config.target_lookup) 
    242256      throw cli_exception("No target interface given when looking for targets"); 
    243     config.data->recipient.import(config.target_lookup->lookup_target(config.data->target_id)); 
     257    config.data->recipient.apply(config.target_lookup->lookup_target(config.data->target_id)); 
     258    //po::store(parsed, vm); 
     259    po::notify(vm); 
    244260  } 
    245261 
     
    250266  nscapi::functions::append_simple_submit_request_payload(message.add_payload(), config.data->command, parse_result(config.data->result), config.data->message); 
    251267 
    252   return config.handler->submit(config.data, message.mutable_header(), message.SerializeAsString(), result); 
     268  return config.handler->submit(config.data, message, result); 
    253269} 
    254270void client::command_line_parser::modify_header(configuration &config, ::Plugin::Common_Header* header, nscapi::functions::destination_container &recipient) { 
     
    266282} 
    267283 
    268 int client::command_line_parser::do_relay_submit(configuration &config, const std::string &request, std::string &response) { 
    269   Plugin::SubmitRequestMessage message; 
    270   message.ParseFromString(request); 
    271   modify_header(config, message.mutable_header(), config.data->recipient); 
    272   return config.handler->submit(config.data, message.mutable_header(), message.SerializeAsString(), response); 
    273 } 
    274  
     284int client::command_line_parser::do_relay_submit(configuration &config, Plugin::SubmitRequestMessage &request_message, std::string &response) { 
     285  modify_header(config, request_message.mutable_header(), config.data->recipient); 
     286  return config.handler->submit(config.data, request_message, response); 
     287} 
     288 
  • include/client/command_line_parser.hpp

    r7354aa1 r0f7b655  
    111111 
    112112  struct clp_handler { 
    113     virtual int query(configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) = 0; 
    114     virtual int submit(configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &response) = 0; 
    115     virtual int exec(configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) = 0; 
     113    virtual int query(configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply) = 0; 
     114    virtual int submit(configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &response) = 0; 
     115    virtual int exec(configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply) = 0; 
    116116  }; 
    117117  struct command_manager { 
     
    126126    } 
    127127 
     128    int process_query(std::wstring cmd, client::configuration &config, const Plugin::QueryRequestMessage &message, std::string &result); 
     129    int process_exec(std::wstring cmd, client::configuration &config, const Plugin::ExecuteRequestMessage &message, std::string &result); 
     130 
     131     
    128132  }; 
    129133 
     
    138142 
    139143    static int do_execute_command_as_exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 
    140     static int do_execute_command_as_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, const std::string &request, std::string &result); 
    141     static int do_relay_submit(configuration &config, const std::string &request, std::string &response); 
     144    static int do_execute_command_as_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 
     145    static int do_relay_submit(configuration &config, Plugin::SubmitRequestMessage &request_message, std::string &response); 
    142146 
    143147    static std::wstring parse_command(std::wstring command, std::wstring prefix) { 
     
    164168 
    165169    static int do_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 
    166     static int do_forward(configuration &config, const std::string &request, std::string &result); 
    167170    static int do_exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 
    168171    static int do_submit(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 
  • include/nsca/server/connection.cpp

    r2ec2eb6 r0f7b655  
    5858    void connection::timeout(const boost::system::error_code& e) { 
    5959      if (e != boost::asio::error::operation_aborted) { 
    60         handler_->log_debug(__FILE__, __LINE__, _T("Timeout <<<-")); 
     60        handler_->log_debug(__FILE__, __LINE__, _T("Timeout reading:") + strEx::itos(parser_.get_payload_lenght())); 
    6161        boost::system::error_code ignored_ec; 
    6262        socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec); 
  • include/nsca/server/parser.hpp

    r2ec2eb6 r0f7b655  
    4545        packet_length_ = nsca::length::get_packet_length(length); 
    4646      } 
     47      int get_payload_lenght() const { return payload_length_; } 
    4748      std::string::size_type size() { 
    4849        return buffer_.size(); 
  • include/nscapi/functions.hpp

    re396b2f r0f7b655  
    326326        if (host.id() == tag) { 
    327327          data.id = tag; 
    328           data.address.import(net::parse(host.address())); 
     328          if (!host.address().empty()) 
     329            data.address.import(net::parse(host.address())); 
    329330          if (!host.comment().empty()) 
    330331            data.comment = host.comment(); 
     
    744745    } 
    745746    static decoded_simple_command_data parse_simple_exec_request(const wchar_t* char_command, const std::string &request) { 
    746       decoded_simple_command_data data; 
    747  
    748       data.command = char_command; 
    749747      Plugin::ExecuteRequestMessage message; 
    750748      message.ParseFromString(request); 
     749 
     750      return parse_simple_exec_request(char_command, message); 
     751    } 
     752    static decoded_simple_command_data parse_simple_exec_request(const std::wstring cmd, const Plugin::ExecuteRequestMessage &message) { 
     753      decoded_simple_command_data data; 
     754      data.command = cmd; 
    751755      if (message.has_header()) 
    752756        data.target = utf8::cvt<std::wstring>(message.header().recipient_id()); 
  • modules/NRPEClient/NRPEClient.cpp

    re396b2f r0f7b655  
    3232namespace sh = nscapi::settings_helper; 
    3333 
     34const std::wstring NRPEClient::command_prefix = _T("nrpe"); 
    3435/** 
    3536 * Default c-tor 
     
    5556bool NRPEClient::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
    5657 
    57   std::wstring certificate; 
    58   unsigned int timeout = 30, buffer_length = 1024; 
    59   bool use_ssl = true; 
    6058  try { 
    6159 
     
    119117void NRPEClient::add_target(std::wstring key, std::wstring arg) { 
    120118  try { 
    121     nscapi::targets::target_object t = targets.add(get_settings_proxy(), target_path , key, arg); 
     119    targets.add(get_settings_proxy(), target_path , key, arg); 
    122120  } catch (const std::exception &e) { 
    123121    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key + _T(", ") + utf8::to_unicode(e.what())); 
     
    149147 
    150148NSCAPI::nagiosReturn NRPEClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
     149  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     150 
    151151  Plugin::QueryRequestMessage message; 
    152152  message.ParseFromString(request); 
    153   if (message.payload_size() != 1) { 
    154     return nscapi::functions::create_simple_query_response_unknown(char_command, _T("Multiple payloads currently not supported"), result); 
    155   } 
    156   std::string recipient = message.header().recipient_id(); 
    157   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(message.payload(0)); 
    158   std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("nrpe")); 
     153 
    159154  client::configuration config; 
    160   config.data->recipient.id = recipient; 
    161   setup(config); 
    162   if (client::command_line_parser::is_command(cmd)) 
    163     return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, request, result); 
    164   return commands.exec_simple(config, data.target, cmd, data.args, result); 
     155  setup(config, message.header()); 
     156 
     157  return commands.process_query(cmd, config, message, result); 
    165158} 
    166159 
    167160NSCAPI::nagiosReturn NRPEClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
    168   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
    169   std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
    170   if (!client::command_line_parser::is_command(cmd)) 
    171     return NSCAPI::returnIgnored; 
     161  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     162 
     163  Plugin::ExecuteRequestMessage message; 
     164  message.ParseFromString(request); 
     165 
    172166  client::configuration config; 
    173   setup(config); 
    174   return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     167  setup(config, message.header()); 
     168 
     169  return commands.process_exec(cmd, config, message, result); 
    175170} 
    176171 
    177172NSCAPI::nagiosReturn NRPEClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
     173  Plugin::SubmitRequestMessage message; 
     174  message.ParseFromString(request); 
     175 
    178176  client::configuration config; 
    179   setup(config); 
    180   return client::command_line_parser::do_relay_submit(config, request, result); 
     177  setup(config, message.header()); 
     178 
     179  return client::command_line_parser::do_relay_submit(config, message, result); 
    181180} 
    182181 
     
    198197} 
    199198 
    200 void NRPEClient::setup(client::configuration &config) { 
     199void NRPEClient::setup(client::configuration &config, const ::Plugin::Common_Header& header) { 
    201200  boost::shared_ptr<clp_handler_impl> handler = boost::shared_ptr<clp_handler_impl>(new clp_handler_impl(this)); 
    202201  add_local_options(config.local, config.data); 
    203202 
    204   config.data->recipient.address = net::parse("nrpe://localhost:5666"); 
     203  config.data->recipient.id = header.recipient_id(); 
    205204  std::wstring recipient = utf8::cvt<std::wstring>(config.data->recipient.id); 
    206205  if (!targets.has_object(recipient)) { 
     
    215214    config.data->recipient.apply(def); 
    216215  } 
    217   config.data->recipient.id = "default"; 
    218216  config.data->host_self.id = "self"; 
    219217  //config.data->host_self.host = hostname_; 
     
    233231// 
    234232 
    235 int NRPEClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    236   Plugin::QueryRequestMessage request_message; 
    237   request_message.ParseFromString(request); 
    238   connection_data con = this->instance->parse_header(*header, data); 
     233int NRPEClient::clp_handler_impl::query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply) { 
     234  const ::Plugin::Common_Header& request_header = request_message.header(); 
     235  connection_data con = parse_header(request_header, data); 
    239236 
    240237  Plugin::QueryResponseMessage response_message; 
    241   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     238  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    242239 
    243240  for (int i=0;i<request_message.payload_size();i++) { 
     
    255252} 
    256253 
    257 int NRPEClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    258   Plugin::SubmitRequestMessage request_message; 
    259   request_message.ParseFromString(request); 
    260   connection_data con = this->instance->parse_header(*header, data); 
     254int NRPEClient::clp_handler_impl::submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply) { 
     255  const ::Plugin::Common_Header& request_header = request_message.header(); 
     256  connection_data con = parse_header(request_header, data); 
    261257  std::wstring channel = utf8::cvt<std::wstring>(request_message.channel()); 
    262258   
    263259  Plugin::SubmitResponseMessage response_message; 
    264   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     260  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    265261 
    266262  for (int i=0;i<request_message.payload_size();++i) { 
     
    277273} 
    278274 
    279 int NRPEClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    280   Plugin::ExecuteRequestMessage request_message; 
    281   request_message.ParseFromString(request); 
    282   connection_data con = this->instance->parse_header(*header, data); 
     275int NRPEClient::clp_handler_impl::exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply) { 
     276  const ::Plugin::Common_Header& request_header = request_message.header(); 
     277  connection_data con = parse_header(request_header, data); 
    283278 
    284279  Plugin::ExecuteResponseMessage response_message; 
    285   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     280  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    286281 
    287282  for (int i=0;i<request_message.payload_size();i++) { 
  • modules/NRPEClient/NRPEClient.h

    re396b2f r0f7b655  
    4040  std::wstring channel_; 
    4141  std::wstring target_path; 
     42  const static std::wstring command_prefix; 
    4243 
    4344  struct custom_reader { 
     
    110111    connection_data(nscapi::functions::destination_container arguments, nscapi::functions::destination_container target) { 
    111112      arguments.import(target); 
    112       cert = arguments.get_string_data("certificate", ""); 
     113      cert = arguments.get_string_data("certificate"); 
    113114      timeout = arguments.get_int_data("timeout", 30); 
    114115      buffer_length = arguments.get_int_data("payload length", 1024); 
     
    140141    clp_handler_impl(NRPEClient *instance) : instance(instance) {} 
    141142 
    142     int query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    143     int submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    144     int exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
     143    int query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply); 
     144    int submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply); 
     145    int exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply); 
    145146 
    146147    virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 
    147       nscapi::targets::optional_target_object t = instance->targets.find_object(id); 
    148       if (t) 
    149         return t->to_destination_container(); 
     148      nscapi::targets::optional_target_object opt = instance->targets.find_object(id); 
     149      if (opt) 
     150        return opt->to_destination_container(); 
    150151      nscapi::functions::destination_container ret; 
    151152      return ret; 
     
    201202  static nrpe::packet send_ssl(std::string cert, std::string host, std::string port, int timeout, nrpe::packet packet); 
    202203 
    203   connection_data parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data); 
     204  static connection_data parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data); 
    204205 
    205206private: 
    206207  void add_local_options(po::options_description &desc, client::configuration::data_type data); 
    207   void setup(client::configuration &config); 
     208  void setup(client::configuration &config, const ::Plugin::Common_Header& header); 
    208209  void add_command(std::wstring key, std::wstring args); 
    209210  void add_target(std::wstring key, std::wstring args); 
  • modules/NSCAClient/NSCAClient.cpp

    re396b2f r0f7b655  
    3333namespace sh = nscapi::settings_helper; 
    3434 
     35const std::wstring NSCAAgent::command_prefix = _T("nsca"); 
    3536/** 
    3637 * Default c-tor 
     
    5354  return false; 
    5455} 
    55 /* 
    56 DEFINE_SETTING_S(REPORT_MODE, NSCA_SERVER_SECTION, "report", "all"); 
    57 DESCRIBE_SETTING(REPORT_MODE, "REPORT MODE", "What to report to the server (any of the following: all, critical, warning, unknown, ok)"); 
    58 */ 
    5956 
    6057bool NSCAAgent::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
    6158 
    62  
    63   std::wstring encryption, password, nscahost; 
    64   std::string delay; 
    65   unsigned int timeout = 30, payload_length = 512, nscaport = 5666; 
    6659  try { 
     60 
    6761    sh::settings_registry settings(get_settings_proxy()); 
    6862    settings.set_alias(_T("NSCA"), alias, _T("client")); 
     
    9084      ; 
    9185 
    92     settings.alias().add_path_to_settings(_T("server")) 
    93       (_T("NSCA SERVER"), _T("Configure the NSCA server to report to.")) 
    94       ; 
    95  
    9686    settings.register_all(); 
    9787    settings.notify(); 
    9888 
     89    targets.add_missing(get_settings_proxy(), target_path, _T("default"), _T(""), true); 
     90 
     91 
    9992    get_core()->registerSubmissionListener(get_id(), channel_); 
     93 
     94    register_command(_T("nsca_query"), _T("Check remote NRPE host")); 
     95    register_command(_T("nsca_submit"), _T("Submit (via query) remote NRPE host")); 
     96    register_command(_T("nsca_forward"), _T("Forward query to remote NRPE host")); 
     97    register_command(_T("nsca_exec"), _T("Execute (via query) remote NRPE host")); 
     98    register_command(_T("nsca_help"), _T("Help on using NRPE Client")); 
    10099 
    101100    if (hostname_ == "auto") { 
     
    151150void NSCAAgent::add_target(std::wstring key, std::wstring arg) { 
    152151  try { 
    153     nscapi::targets::target_object t = targets.add(get_settings_proxy(), target_path , key, arg); 
     152    targets.add(get_settings_proxy(), target_path , key, arg); 
     153  } catch (const std::exception &e) { 
     154    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key + _T(", ") + utf8::to_unicode(e.what())); 
    154155  } catch (...) { 
    155156    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 
     
    179180 
    180181NSCAPI::nagiosReturn NSCAAgent::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
    181   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
    182   std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("nsca")); 
     182  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     183 
     184  Plugin::QueryRequestMessage message; 
     185  message.ParseFromString(request); 
     186 
    183187  client::configuration config; 
    184   setup(config); 
    185   if (!client::command_line_parser::is_command(cmd)) 
    186     return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, request, result); 
    187   return commands.exec_simple(config, data.target, char_command, data.args, result); 
     188  setup(config, message.header()); 
     189 
     190  return commands.process_query(cmd, config, message, result); 
    188191} 
    189192 
    190193NSCAPI::nagiosReturn NSCAAgent::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
    191   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
    192   std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("nsca")); 
    193   if (!client::command_line_parser::is_command(cmd)) 
    194     return NSCAPI::returnIgnored; 
     194  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     195 
     196  Plugin::ExecuteRequestMessage message; 
     197  message.ParseFromString(request); 
     198 
    195199  client::configuration config; 
    196   setup(config); 
    197   return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     200  setup(config, message.header()); 
     201 
     202  return commands.process_exec(cmd, config, message, result); 
    198203} 
    199204 
    200205NSCAPI::nagiosReturn NSCAAgent::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
     206  Plugin::SubmitRequestMessage message; 
     207  message.ParseFromString(request); 
     208 
    201209  client::configuration config; 
    202   setup(config); 
    203   return client::command_line_parser::do_relay_submit(config, request, result); 
     210  setup(config, message.header()); 
     211 
     212  return client::command_line_parser::do_relay_submit(config, message, result); 
    204213} 
    205214 
     
    213222    "Length of payload (has to be same as on the server)") 
    214223 
    215     ("buffer-length,l", po::value<unsigned int>()->notifier(boost::bind(&nscapi::functions::destination_container::set_int_data, &data->recipient, "payload length", _1)),  
     224    ("payload-length,l", po::value<unsigned int>()->notifier(boost::bind(&nscapi::functions::destination_container::set_int_data, &data->recipient, "payload length", _1)),  
    216225    "Length of payload (has to be same as on the server)") 
    217226 
     
    233242} 
    234243 
    235 void NSCAAgent::setup(client::configuration &config) { 
     244void NSCAAgent::setup(client::configuration &config, const ::Plugin::Common_Header& header) { 
    236245  boost::shared_ptr<clp_handler_impl> handler(new clp_handler_impl(this)); 
    237246  add_local_options(config.local, config.data); 
    238247 
    239   config.data->recipient.id = "default"; 
    240   config.data->recipient.address = net::parse("nsca://localhost:5667"); 
    241  
    242   nscapi::targets::optional_target_object opt = targets.find_object(_T("default")); 
     248  config.data->recipient.id = header.recipient_id(); 
     249  std::wstring recipient = utf8::cvt<std::wstring>(config.data->recipient.id); 
     250  if (!targets.has_object(recipient)) { 
     251    NSC_LOG_ERROR(_T("Target not found (using default): ") + recipient); 
     252    recipient = _T("default"); 
     253  } 
     254  nscapi::targets::optional_target_object opt = targets.find_object(recipient); 
     255 
    243256  if (opt) { 
    244257    nscapi::targets::target_object t = *opt; 
    245258    nscapi::functions::destination_container def = t.to_destination_container(); 
    246     config.data->recipient.import(def); 
     259    config.data->recipient.apply(def); 
    247260  } 
    248261  config.data->host_self.id = "self"; 
     
    253266} 
    254267 
    255 NSCAAgent::connection_data NSCAAgent::parse_header(const ::Plugin::Common_Header &header) { 
     268NSCAAgent::connection_data NSCAAgent::parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data) { 
    256269  nscapi::functions::destination_container recipient, sender; 
    257270  nscapi::functions::parse_destination(header, header.recipient_id(), recipient, true); 
    258271  nscapi::functions::parse_destination(header, header.sender_id(), sender, true); 
    259   return connection_data(recipient, sender); 
     272  return connection_data(recipient, data->recipient, sender); 
    260273} 
    261274 
     
    264277// 
    265278 
    266 int NSCAAgent::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    267   Plugin::QueryRequestMessage request_message; 
    268   request_message.ParseFromString(request); 
    269   connection_data con = parse_header(*header); 
     279int NSCAAgent::clp_handler_impl::query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply) { 
     280  const ::Plugin::Common_Header& request_header = request_message.header(); 
     281  connection_data con = parse_header(request_header, data); 
    270282 
    271283  Plugin::QueryResponseMessage response_message; 
    272   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     284  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    273285 
    274286  std::list<nsca::packet> list; 
     
    288300} 
    289301 
    290 int NSCAAgent::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    291   Plugin::SubmitRequestMessage message; 
    292   message.ParseFromString(request); 
    293   connection_data con = parse_header(*header); 
    294   std::wstring channel = utf8::cvt<std::wstring>(message.channel()); 
     302int NSCAAgent::clp_handler_impl::submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply) { 
     303  const ::Plugin::Common_Header& request_header = request_message.header(); 
     304  connection_data con = parse_header(request_header, data); 
     305  std::wstring channel = utf8::cvt<std::wstring>(request_message.channel()); 
    295306 
    296307  Plugin::SubmitResponseMessage response_message; 
    297   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     308  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    298309 
    299310  std::list<nsca::packet> list; 
    300311 
    301   for (int i=0;i < message.payload_size(); ++i) { 
     312  for (int i=0;i < request_message.payload_size(); ++i) { 
    302313    nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 
    303314    std::wstring alias, msg; 
    304     packet.code = nscapi::functions::parse_simple_submit_request_payload(message.payload(i), alias, msg); 
     315    packet.code = nscapi::functions::parse_simple_submit_request_payload(request_message.payload(i), alias, msg); 
    305316    if (alias != _T("host_check")) 
    306317      packet.service = utf8::cvt<std::string>(alias); 
     
    315326} 
    316327 
    317 int NSCAAgent::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    318   Plugin::ExecuteRequestMessage request_message; 
    319   request_message.ParseFromString(request); 
    320   connection_data con = parse_header(*header); 
     328int NSCAAgent::clp_handler_impl::exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply) { 
     329  const ::Plugin::Common_Header& request_header = request_message.header(); 
     330  connection_data con = parse_header(request_header, data); 
    321331 
    322332  Plugin::ExecuteResponseMessage response_message; 
    323   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     333  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    324334 
    325335  std::list<nsca::packet> list; 
  • modules/NSCAClient/NSCAClient.h

    re396b2f r0f7b655  
    4040  std::wstring channel_; 
    4141  std::wstring target_path; 
     42  const static std::wstring command_prefix; 
    4243  std::string hostname_; 
    4344  bool cacheNscaHost_; 
     
    5354      target.set_property_int(_T("payload length"), 512); 
    5455    } 
    55     static void post_process_target(target_object &target) {} 
    5656 
    5757    static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 
     
    7474        ; 
    7575    } 
     76    static void post_process_target(target_object &target) { 
     77    } 
    7678  }; 
    7779 
     
    8890    int time_delta; 
    8991 
    90     connection_data(nscapi::functions::destination_container recipient, nscapi::functions::destination_container sender) { 
     92    connection_data(nscapi::functions::destination_container recipient, nscapi::functions::destination_container target, nscapi::functions::destination_container sender) { 
     93      recipient.import(target); 
    9194      timeout = recipient.get_int_data("timeout", 30); 
    9295      buffer_length = recipient.get_int_data("payload length", 512); 
     
    126129    clp_handler_impl(NSCAAgent *instance) : instance(instance) {} 
    127130 
    128     int query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    129     int submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    130     int exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
     131    int query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply); 
     132    int submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply); 
     133    int exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply); 
    131134 
    132135    virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 
     
    181184  boost::tuple<int,std::wstring> send(connection_data data, const std::list<nsca::packet> packets); 
    182185  void add_options(po::options_description &desc, connection_data &command_data); 
    183   static connection_data parse_header(const ::Plugin::Common_Header &header); 
     186  static connection_data parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data); 
    184187 
    185188private: 
    186189  void add_local_options(po::options_description &desc, client::configuration::data_type data); 
    187   void setup(client::configuration &config); 
     190  void setup(client::configuration &config, const ::Plugin::Common_Header& header); 
    188191  void add_command(std::wstring key, std::wstring args); 
    189192  void add_target(std::wstring key, std::wstring args); 
  • modules/NSCPClient/NSCPClient.cpp

    re396b2f r0f7b655  
    3434namespace sh = nscapi::settings_helper; 
    3535 
     36const std::wstring NSCPClient::command_prefix = _T("nscp"); 
    3637/** 
    3738 * Default c-tor 
     
    5859  std::map<std::wstring,std::wstring> commands; 
    5960 
    60   std::wstring certificate; 
    61   unsigned int timeout = 30, buffer_length = 1024; 
    62   bool use_ssl = true; 
    6361  try { 
    6462 
    65     register_command(_T("query_nscp"), _T("Submit a query to a remote host via NSCP")); 
    66     register_command(_T("submit_nscp"), _T("Submit a query to a remote host via NSCP")); 
    67     register_command(_T("exec_nscp"), _T("Execute remote command on a remote host via NSCP")); 
    6863 
    6964    sh::settings_registry settings(get_settings_proxy()); 
     
    9186    settings.notify(); 
    9287 
     88    targets.add_missing(get_settings_proxy(), target_path, _T("default"), _T(""), true); 
     89 
     90 
    9391    get_core()->registerSubmissionListener(get_id(), channel_); 
     92    register_command(_T("nscp_query"), _T("Submit a query to a remote host via NSCP")); 
     93    register_command(_T("nscp_forward"), _T("Forward query to remote NSCP host")); 
     94    register_command(_T("nscp_submit"), _T("Submit a query to a remote host via NSCP")); 
     95    register_command(_T("nscp_exec"), _T("Execute remote command on a remote host via NSCP")); 
     96    register_command(_T("nscp_help"), _T("Help on using NSCP Client")); 
    9497 
    9598  } catch (nscapi::nscapi_exception &e) { 
     
    120123  try { 
    121124    targets.add(get_settings_proxy(), target_path , key, arg); 
     125  } catch (const std::exception &e) { 
     126    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key + _T(", ") + utf8::to_unicode(e.what())); 
    122127  } catch (...) { 
    123128    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 
     
    147152 
    148153NSCAPI::nagiosReturn NSCPClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
    149   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
    150   std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 
     154  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     155 
     156  Plugin::QueryRequestMessage message; 
     157  message.ParseFromString(request); 
     158 
    151159  client::configuration config; 
    152   setup(config); 
    153   if (!client::command_line_parser::is_command(cmd)) 
    154     return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, request, result); 
    155   return commands.exec_simple(config, data.target, char_command, data.args, result); 
     160  setup(config, message.header()); 
     161 
     162  return commands.process_query(cmd, config, message, result); 
    156163} 
    157164 
    158165NSCAPI::nagiosReturn NSCPClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
    159   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
    160   std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
    161   if (!client::command_line_parser::is_command(cmd)) 
    162     return NSCAPI::returnIgnored; 
     166  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     167 
     168  Plugin::ExecuteRequestMessage message; 
     169  message.ParseFromString(request); 
     170 
    163171  client::configuration config; 
    164   setup(config); 
    165   return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     172  setup(config, message.header()); 
     173 
     174  return commands.process_exec(cmd, config, message, result); 
    166175} 
    167176 
    168177NSCAPI::nagiosReturn NSCPClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
     178  Plugin::SubmitRequestMessage message; 
     179  message.ParseFromString(request); 
     180 
    169181  client::configuration config; 
    170   setup(config); 
    171   return client::command_line_parser::do_relay_submit(config, request, result); 
     182  setup(config, message.header()); 
     183 
     184  return client::command_line_parser::do_relay_submit(config, message, result); 
    172185} 
    173186 
     
    188201} 
    189202 
    190 void NSCPClient::setup(client::configuration &config) { 
     203void NSCPClient::setup(client::configuration &config, const ::Plugin::Common_Header& header) { 
    191204  boost::shared_ptr<clp_handler_impl> handler = boost::shared_ptr<clp_handler_impl>(new clp_handler_impl(this)); 
    192205  add_local_options(config.local, config.data); 
    193206 
    194   config.data->recipient.id = "default"; 
    195   config.data->recipient.address = net::parse("nscp://localhost:5668"); 
    196   nscapi::targets::optional_target_object opt = targets.find_object(_T("default")); 
     207  config.data->recipient.id = header.recipient_id(); 
     208  std::wstring recipient = utf8::cvt<std::wstring>(config.data->recipient.id); 
     209  if (!targets.has_object(recipient)) { 
     210    NSC_LOG_ERROR(_T("Target not found (using default): ") + recipient); 
     211    recipient = _T("default"); 
     212  } 
     213  nscapi::targets::optional_target_object opt = targets.find_object(recipient); 
     214 
    197215  if (opt) { 
    198     nscapi::functions::destination_container def = opt->to_destination_container(); 
    199     config.data->recipient.import(def); 
     216    nscapi::targets::target_object t = *opt; 
     217    nscapi::functions::destination_container def = t.to_destination_container(); 
     218    config.data->recipient.apply(def); 
    200219  } 
    201220  config.data->host_self.id = "self"; 
     
    206225} 
    207226 
    208 NSCPClient::connection_data NSCPClient::parse_header(const ::Plugin::Common_Header &header) { 
     227NSCPClient::connection_data NSCPClient::parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data) { 
    209228  nscapi::functions::destination_container recipient; 
    210229  nscapi::functions::parse_destination(header, header.recipient_id(), recipient, true); 
    211   return connection_data(recipient); 
     230  return connection_data(recipient, data->recipient); 
    212231} 
    213232 
     
    226245  return ret; 
    227246} 
    228 int NSCPClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
     247int NSCPClient::clp_handler_impl::query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply) { 
     248  const ::Plugin::Common_Header& request_header = request_message.header(); 
    229249  int ret = NSCAPI::returnUNKNOWN; 
    230   Plugin::QueryRequestMessage request_message; 
    231   request_message.ParseFromString(request); 
    232   connection_data con = parse_header(*header); 
     250  connection_data con = parse_header(request_header, data); 
    233251 
    234252  Plugin::QueryResponseMessage response_message; 
    235   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     253  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    236254 
    237255  std::list<nscp::packet> chunks; 
    238256  chunks.push_back(nscp::factory::create_envelope_request(1)); 
    239   chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request, 0)); 
     257  chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request_message.SerializeAsString(), 0)); 
    240258  chunks = instance->send(con, chunks); 
    241259  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
     
    256274} 
    257275 
    258 int NSCPClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
     276int NSCPClient::clp_handler_impl::submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply) { 
     277  const ::Plugin::Common_Header& request_header = request_message.header(); 
    259278  int ret = NSCAPI::returnUNKNOWN; 
    260   Plugin::SubmitRequestMessage request_message; 
    261   request_message.ParseFromString(request); 
    262   connection_data con = parse_header(*header); 
     279  connection_data con = parse_header(request_header, data); 
    263280  Plugin::SubmitResponseMessage response_message; 
    264   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     281  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    265282 
    266283  std::list<nscp::packet> chunks; 
    267   chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request, 0)); 
     284  chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request_message.SerializeAsString(), 0)); 
    268285  chunks = instance->send(con, chunks); 
    269286  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
     
    284301} 
    285302 
    286 int NSCPClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
     303int NSCPClient::clp_handler_impl::exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply) { 
     304  const ::Plugin::Common_Header& request_header = request_message.header(); 
    287305  int ret = NSCAPI::returnOK; 
    288   Plugin::ExecuteRequestMessage request_message; 
    289   request_message.ParseFromString(request); 
    290   connection_data con = parse_header(*header); 
     306  connection_data con = parse_header(request_header, data); 
    291307 
    292308  Plugin::ExecuteResponseMessage response_message; 
    293   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     309  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    294310 
    295311  std::list<nscp::packet> chunks; 
    296312  chunks.push_back(nscp::factory::create_envelope_request(1)); 
    297   chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request, 0)); 
     313  chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request_message.SerializeAsString(), 0)); 
    298314  chunks = instance->send(con, chunks); 
    299315  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
  • modules/NSCPClient/NSCPClient.h

    re396b2f r0f7b655  
    3636namespace sh = nscapi::settings_helper; 
    3737 
    38  
    3938class NSCPClient : public nscapi::impl::simple_plugin { 
    4039private: 
     
    4241  std::wstring channel_; 
    4342  std::wstring target_path; 
     43  const static std::wstring command_prefix; 
    4444 
    4545  struct custom_reader { 
     
    5353      target.set_property_int(_T("payload length"), 1024); 
    5454    } 
    55  
    56     //static void post_process_target(target_object &target) {} 
    5755 
    5856    static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 
     
    111109    int timeout; 
    112110 
    113     connection_data(nscapi::functions::destination_container recipient) { 
    114       cert = recipient.get_string_data("certificate"); 
    115       timeout = recipient.get_int_data("timeout", 30); 
    116       use_ssl = recipient.get_bool_data("ssl"); 
    117       if (recipient.has_data("no ssl")) 
    118         use_ssl = !recipient.get_bool_data("no ssl"); 
    119       host = recipient.address.host; 
    120       port = recipient.address.get_port(5668); 
     111    connection_data(nscapi::functions::destination_container arguments, nscapi::functions::destination_container target) { 
     112      arguments.import(target); 
     113      cert = arguments.get_string_data("certificate"); 
     114      timeout = arguments.get_int_data("timeout", 30); 
     115      use_ssl = arguments.get_bool_data("ssl"); 
     116      if (arguments.has_data("no ssl")) 
     117        use_ssl = !arguments.get_bool_data("no ssl"); 
     118      if (arguments.has_data("use ssl")) 
     119        use_ssl = arguments.get_bool_data("use ssl"); 
     120 
     121      host = arguments.address.host; 
     122      port = arguments.address.get_port(5668); 
    121123    } 
    122124 
     
    126128      ss << _T(", port: ") << utf8::cvt<std::wstring>(port); 
    127129      ss << _T(", timeout: ") << timeout; 
     130      ss << _T(", use_ssl: ") << use_ssl; 
    128131      ss << _T(", certificate: ") << utf8::cvt<std::wstring>(cert); 
    129       ss << _T(", use_ssl: ") << use_ssl; 
    130132      return ss.str(); 
    131133    } 
     
    137139    clp_handler_impl(NSCPClient *instance) : instance(instance) {} 
    138140 
    139     int query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    140     int submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    141     int exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
     141    int query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply); 
     142    int submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply); 
     143    int exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply); 
    142144 
    143145    virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 
    144       nscapi::functions::destination_container ret; 
    145146      nscapi::targets::optional_target_object opt = instance->targets.find_object(id); 
    146147      if (opt) 
    147148        return opt->to_destination_container(); 
     149      nscapi::functions::destination_container ret; 
    148150      return ret; 
    149151    } 
     
    202204  bool submit_nscp(std::list<std::wstring> &arguments, std::wstring &result); 
    203205 
    204   static connection_data parse_header(const ::Plugin::Common_Header &header); 
     206  static connection_data parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data); 
    205207 
    206208private: 
    207209  void add_local_options(po::options_description &desc, client::configuration::data_type data); 
    208   void setup(client::configuration &config); 
     210  void setup(client::configuration &config, const ::Plugin::Common_Header& header); 
    209211  void add_command(std::wstring key, std::wstring args); 
    210212  void add_target(std::wstring key, std::wstring args); 
  • modules/PythonScript/script_wrapper.cpp

    ra87ce04 r0f7b655  
    128128  } 
    129129} 
    130 void script_wrapper::sleep(unsigned int seconds) { 
    131   { 
    132     thread_unlocker unlocker; 
    133     { 
    134       boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); 
     130void script_wrapper::sleep(unsigned int ms) { 
     131  { 
     132    thread_unlocker unlocker; 
     133    { 
     134      boost::this_thread::sleep(boost::posix_time::milliseconds(ms)); 
    135135 
    136136    } 
  • modules/SMTPClient/SMTPClient.cpp

    re396b2f r0f7b655  
    3535namespace sh = nscapi::settings_helper; 
    3636 
     37const std::wstring SMTPClient::command_prefix = _T("smtp"); 
    3738/** 
    3839 * Default c-tor 
     
    7980 
    8081      ; 
     82 
    8183    settings.register_all(); 
    8284    settings.notify(); 
    8385 
     86    targets.add_missing(get_settings_proxy(), target_path, _T("default"), _T(""), true); 
     87 
     88 
    8489    get_core()->registerSubmissionListener(get_id(), channel_); 
     90    register_command(_T("smtp_query"), _T("Check remote SMTP host")); 
     91    register_command(_T("smtp_submit"), _T("Submit (via query) remote SMTP host")); 
     92    register_command(_T("smtp_forward"), _T("Forward query to remote SMTP host")); 
     93    register_command(_T("smtp_exec"), _T("Execute (via query) remote SMTP host")); 
     94    register_command(_T("smtp_help"), _T("Help on using SMTP Client")); 
    8595  } catch (nscapi::nscapi_exception &e) { 
    8696    NSC_LOG_ERROR_STD(_T("NSClient API exception: ") + utf8::to_unicode(e.what())); 
     
    110120  try { 
    111121    targets.add(get_settings_proxy(), target_path , key, arg); 
     122  } catch (const std::exception &e) { 
     123    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key + _T(", ") + utf8::to_unicode(e.what())); 
    112124  } catch (...) { 
    113125    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 
     
    139151 
    140152NSCAPI::nagiosReturn SMTPClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
    141   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
    142   std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 
     153  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     154 
     155  Plugin::QueryRequestMessage message; 
     156  message.ParseFromString(request); 
     157 
    143158  client::configuration config; 
    144   setup(config); 
    145   if (!client::command_line_parser::is_command(cmd)) 
    146     return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, request, result); 
    147   return commands.exec_simple(config, data.target, char_command, data.args, result); 
     159  setup(config, message.header()); 
     160 
     161  return commands.process_query(cmd, config, message, result); 
    148162} 
    149163 
    150164NSCAPI::nagiosReturn SMTPClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
    151   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
    152   std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
    153   if (!client::command_line_parser::is_command(cmd)) 
    154     return NSCAPI::returnIgnored; 
     165  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     166 
     167  Plugin::ExecuteRequestMessage message; 
     168  message.ParseFromString(request); 
     169 
    155170  client::configuration config; 
    156   setup(config); 
    157   return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     171  setup(config, message.header()); 
     172 
     173  return commands.process_exec(cmd, config, message, result); 
    158174} 
    159175 
    160176NSCAPI::nagiosReturn SMTPClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
     177  Plugin::SubmitRequestMessage message; 
     178  message.ParseFromString(request); 
     179 
    161180  client::configuration config; 
    162   setup(config); 
    163   return client::command_line_parser::do_relay_submit(config, request, result); 
     181  setup(config, message.header()); 
     182 
     183  return client::command_line_parser::do_relay_submit(config, message, result); 
    164184} 
    165185 
     
    182202} 
    183203 
    184 void SMTPClient::setup(client::configuration &config) { 
     204void SMTPClient::setup(client::configuration &config, const ::Plugin::Common_Header& header) { 
    185205  boost::shared_ptr<clp_handler_impl> handler = boost::shared_ptr<clp_handler_impl>(new clp_handler_impl(this)); 
    186206  add_local_options(config.local, config.data); 
    187207 
    188   config.data->recipient.id = "default"; 
    189   config.data->recipient.address = net::parse("smtp://localhost:25"); 
    190   nscapi::targets::optional_target_object opt = targets.find_object(_T("default")); 
     208  config.data->recipient.id = header.recipient_id(); 
     209  std::wstring recipient = utf8::cvt<std::wstring>(config.data->recipient.id); 
     210  if (!targets.has_object(recipient)) { 
     211    NSC_LOG_ERROR(_T("Target not found (using default): ") + recipient); 
     212    recipient = _T("default"); 
     213  } 
     214  nscapi::targets::optional_target_object opt = targets.find_object(recipient); 
     215 
    191216  if (opt) { 
    192     nscapi::functions::destination_container def = opt->to_destination_container(); 
    193     config.data->recipient.import(def); 
     217    nscapi::targets::target_object t = *opt; 
     218    nscapi::functions::destination_container def = t.to_destination_container(); 
     219    config.data->recipient.apply(def); 
    194220  } 
    195221  config.data->host_self.id = "self"; 
     
    200226} 
    201227 
    202 SMTPClient::connection_data SMTPClient::parse_header(const ::Plugin::Common_Header &header) { 
     228SMTPClient::connection_data SMTPClient::parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data) { 
    203229  nscapi::functions::destination_container recipient; 
    204230  nscapi::functions::parse_destination(header, header.recipient_id(), recipient, true); 
    205   return connection_data(recipient); 
     231  return connection_data(recipient, data->recipient); 
    206232} 
    207233 
    208234////////////////////////////////////////////////////////////////////////// 
    209235// Parser implementations 
    210 int SMTPClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
     236int SMTPClient::clp_handler_impl::query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply) { 
    211237  NSC_LOG_ERROR_STD(_T("SMTP does not support query patterns")); 
    212238  return NSCAPI::hasFailed; 
    213239} 
    214240 
    215 int SMTPClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    216   Plugin::SubmitRequestMessage request_message; 
    217   request_message.ParseFromString(request); 
    218   connection_data con = parse_header(*header); 
     241int SMTPClient::clp_handler_impl::submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply) { 
     242  const ::Plugin::Common_Header& request_header = request_message.header(); 
     243  connection_data con = parse_header(request_header, data); 
    219244  std::wstring channel = utf8::cvt<std::wstring>(request_message.channel()); 
    220245 
    221246  Plugin::SubmitResponseMessage response_message; 
    222   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     247  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    223248 
    224249  for (int i=0;i < request_message.payload_size(); ++i) { 
     
    234259    nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "TODO", NSCAPI::returnOK, "Message send successfully"); 
    235260  } 
    236  
    237261  response_message.SerializeToString(&reply); 
    238262  return NSCAPI::isSuccess; 
     
    240264} 
    241265 
    242 int SMTPClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
     266int SMTPClient::clp_handler_impl::exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply) { 
    243267  NSC_LOG_ERROR_STD(_T("SMTP does not support exec patterns")); 
    244268  return NSCAPI::hasFailed; 
  • modules/SMTPClient/SMTPClient.h

    re396b2f r0f7b655  
    3737  std::wstring channel_; 
    3838  std::wstring target_path; 
     39  const static std::wstring command_prefix; 
    3940 
    4041  struct custom_reader { 
    4142    typedef nscapi::targets::target_object object_type; 
    4243    typedef nscapi::targets::target_object target_object; 
    43  
    4444 
    4545    static void init_default(target_object &target) { 
     
    4949      target.set_property_string(_T("template"), _T("Hello, this is %source% reporting %message%!")); 
    5050    } 
    51     static void post_process_target(target_object &target) {} 
    5251 
    5352    static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 
     
    6766      ; 
    6867    } 
     68    static void post_process_target(target_object &target) { 
     69    } 
    6970  }; 
    7071 
     
    8081    int timeout; 
    8182 
    82     connection_data(nscapi::functions::destination_container recipient) { 
    83       recipient_str = recipient.get_string_data("recipient"); 
    84       timeout = recipient.get_int_data("timeout", 30); 
    85       sender = recipient.get_string_data("sender"); 
    86       template_string = recipient.get_string_data("template"); 
     83    connection_data(nscapi::functions::destination_container arguments, nscapi::functions::destination_container target) { 
     84      arguments.import(target); 
     85      recipient_str = arguments.get_string_data("recipient"); 
     86      timeout = arguments.get_int_data("timeout", 30); 
     87      sender = arguments.get_string_data("sender"); 
     88      template_string = arguments.get_string_data("template"); 
    8789 
    88       host = recipient.address.host; 
    89       port = recipient.address.get_port(25); 
     90      host = arguments.address.host; 
     91      port = arguments.address.get_port(25); 
    9092    } 
    9193 
     
    107109    clp_handler_impl(SMTPClient *instance) : instance(instance) {} 
    108110 
    109     int query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    110     int submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    111     int exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
     111    int query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply); 
     112    int submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply); 
     113    int exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply); 
    112114 
    113115    virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 
    114       nscapi::functions::destination_container ret; 
    115116      nscapi::targets::optional_target_object opt = instance->targets.find_object(id); 
    116117      if (opt) 
    117118        return opt->to_destination_container(); 
     119      nscapi::functions::destination_container ret; 
    118120      return ret; 
    119121    } 
     
    156158 
    157159private: 
    158   static connection_data parse_header(const ::Plugin::Common_Header &header); 
     160  static connection_data parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data); 
    159161 
    160162private: 
    161163  void add_local_options(po::options_description &desc, client::configuration::data_type data); 
    162   void setup(client::configuration &config); 
     164  void setup(client::configuration &config, const ::Plugin::Common_Header& header); 
    163165  void add_command(std::wstring key, std::wstring args); 
    164166  void add_target(std::wstring key, std::wstring args); 
  • modules/SyslogClient/SyslogClient.cpp

    re396b2f r0f7b655  
    3535namespace ip = boost::asio::ip; 
    3636 
     37const std::wstring SyslogClient::command_prefix = _T("syslog"); 
    3738/** 
    3839 * Default c-tor 
     
    9091  severities["debug"] = 7; 
    9192 
    92   std::wstring severity, facility, tag_syntax, message_syntax; 
    93   std::wstring ok_severity, warn_severity, crit_severity, unknown_severity; 
    9493  try { 
    9594    sh::settings_registry settings(get_settings_proxy()); 
     
    117116    settings.notify(); 
    118117 
     118    targets.add_missing(get_settings_proxy(), target_path, _T("default"), _T(""), true); 
     119 
     120 
    119121    get_core()->registerSubmissionListener(get_id(), channel_); 
    120  
     122    register_command(_T("syslog_query"), _T("Check remote NRPE host")); 
     123    register_command(_T("syslog_submit"), _T("Submit (via query) remote NRPE host")); 
     124    register_command(_T("syslog_forward"), _T("Forward query to remote NRPE host")); 
     125    register_command(_T("syslog_exec"), _T("Execute (via query) remote NRPE host")); 
     126    register_command(_T("syslog_help"), _T("Help on using NRPE Client")); 
    121127  } catch (nscapi::nscapi_exception &e) { 
    122128    NSC_LOG_ERROR_STD(_T("NSClient API exception: ") + utf8::to_unicode(e.what())); 
     
    140146  try { 
    141147    targets.add(get_settings_proxy(), target_path , key, arg); 
     148  } catch (const std::exception &e) { 
     149    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key + _T(", ") + utf8::to_unicode(e.what())); 
    142150  } catch (...) { 
    143151    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 
     
    167175 
    168176NSCAPI::nagiosReturn SyslogClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
    169   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
    170   std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 
     177  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     178 
     179  Plugin::QueryRequestMessage message; 
     180  message.ParseFromString(request); 
     181 
    171182  client::configuration config; 
    172   setup(config); 
    173   if (!client::command_line_parser::is_command(cmd)) 
    174     return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, request, result); 
    175   return commands.exec_simple(config, data.target, char_command, data.args, result); 
     183  setup(config, message.header()); 
     184 
     185  return commands.process_query(cmd, config, message, result); 
    176186} 
    177187 
    178188NSCAPI::nagiosReturn SyslogClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
    179   nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
    180   std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
    181   if (!client::command_line_parser::is_command(cmd)) 
    182     return NSCAPI::returnIgnored; 
     189  std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix); 
     190 
     191  Plugin::ExecuteRequestMessage message; 
     192  message.ParseFromString(request); 
     193 
    183194  client::configuration config; 
    184   setup(config); 
    185   return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     195  setup(config, message.header()); 
     196 
     197  return commands.process_exec(cmd, config, message, result); 
    186198} 
    187199 
    188200NSCAPI::nagiosReturn SyslogClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
     201  Plugin::SubmitRequestMessage message; 
     202  message.ParseFromString(request); 
     203 
    189204  client::configuration config; 
    190   setup(config); 
    191   return client::command_line_parser::do_relay_submit(config, request, result); 
     205  setup(config, message.header()); 
     206 
     207  return client::command_line_parser::do_relay_submit(config, message, result); 
    192208} 
    193209 
     
    224240} 
    225241 
    226 void SyslogClient::setup(client::configuration &config) { 
     242void SyslogClient::setup(client::configuration &config, const ::Plugin::Common_Header& header) { 
    227243  boost::shared_ptr<clp_handler_impl> handler = boost::shared_ptr<clp_handler_impl>(new clp_handler_impl(this)); 
    228244  add_local_options(config.local, config.data); 
    229245 
    230   config.data->recipient.id = "default"; 
    231   config.data->recipient.address = net::parse("syslog://localhost:514"); 
    232   nscapi::targets::optional_target_object opt = targets.find_object(_T("default")); 
     246  config.data->recipient.id = header.recipient_id(); 
     247  std::wstring recipient = utf8::cvt<std::wstring>(config.data->recipient.id); 
     248  if (!targets.has_object(recipient)) { 
     249    NSC_LOG_ERROR(_T("Target not found (using default): ") + recipient); 
     250    recipient = _T("default"); 
     251  } 
     252  nscapi::targets::optional_target_object opt = targets.find_object(recipient); 
     253 
    233254  if (opt) { 
    234     nscapi::functions::destination_container def = opt->to_destination_container(); 
    235     config.data->recipient.import(def); 
     255    nscapi::targets::target_object t = *opt; 
     256    nscapi::functions::destination_container def = t.to_destination_container(); 
     257    config.data->recipient.apply(def); 
    236258  } 
    237259  config.data->host_self.id = "self"; 
     
    242264} 
    243265 
    244 SyslogClient::connection_data SyslogClient::parse_header(const ::Plugin::Common_Header &header) { 
     266SyslogClient::connection_data SyslogClient::parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data) { 
    245267  nscapi::functions::destination_container recipient; 
    246268  nscapi::functions::parse_destination(header, header.recipient_id(), recipient, true); 
    247   return connection_data(recipient); 
     269  return connection_data(recipient, data->recipient); 
    248270} 
    249271 
     
    252274// 
    253275 
    254 int SyslogClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
     276int SyslogClient::clp_handler_impl::query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply) { 
    255277  NSC_LOG_ERROR_STD(_T("SYSLOG does not support query patterns")); 
    256278  nscapi::functions::create_simple_query_response_unknown(_T("UNKNOWN"), _T("SYSLOG does not support query patterns"), reply); 
     
    258280} 
    259281 
    260 int SyslogClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    261   Plugin::SubmitRequestMessage request_message; 
    262   request_message.ParseFromString(request); 
    263   connection_data con = parse_header(*header); 
     282int SyslogClient::clp_handler_impl::submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply) { 
     283  const ::Plugin::Common_Header& request_header = request_message.header(); 
     284  connection_data con = parse_header(request_header, data); 
    264285 
    265286  Plugin::SubmitResponseMessage response_message; 
    266   nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     287  nscapi::functions::make_return_header(response_message.mutable_header(), request_header); 
    267288 
    268289  //TODO: Map seveity! 
     
    295316} 
    296317 
    297 int SyslogClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
     318int SyslogClient::clp_handler_impl::exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply) { 
    298319  NSC_LOG_ERROR_STD(_T("SYSLOG does not support exec patterns")); 
    299320  nscapi::functions::create_simple_exec_response_unknown("UNKNOWN", "SYSLOG does not support exec patterns", reply); 
  • modules/SyslogClient/SyslogClient.h

    re396b2f r0f7b655  
    3838  std::wstring channel_; 
    3939  std::wstring target_path; 
     40  const static std::wstring command_prefix; 
    4041 
    4142  struct custom_reader { 
     
    5455    } 
    5556 
    56     static void post_process_target(target_object &target) {} 
    5757 
    5858    static void add_custom_keys(sh::settings_registry &settings, boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object) { 
     
    8383        _T("TODO"), _T("")) 
    8484        ; 
     85    } 
     86    static void post_process_target(target_object &target) { 
    8587    } 
    8688  }; 
     
    103105    std::string ok_severity, warn_severity, crit_severity, unknown_severity; 
    104106 
    105     connection_data(nscapi::functions::destination_container recipient) { 
     107    connection_data(nscapi::functions::destination_container recipient, nscapi::functions::destination_container target) { 
     108      recipient.import(target); 
    106109      severity = recipient.data["severity"]; 
    107110      facility = recipient.data["facility"]; 
     
    135138    clp_handler_impl(SyslogClient *instance) : instance(instance) {} 
    136139 
    137     int query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    138     int submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    139     int exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
     140    int query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply); 
     141    int submit(client::configuration::data_type data, const Plugin::SubmitRequestMessage &request_message, std::string &reply); 
     142    int exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply); 
    140143 
    141144    virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 
    142       nscapi::functions::destination_container ret; 
    143145      nscapi::targets::optional_target_object opt = instance->targets.find_object(id); 
    144146      if (opt) 
    145147        return opt->to_destination_container(); 
     148      nscapi::functions::destination_container ret; 
    146149      return ret; 
    147150    } 
     
    186189private: 
    187190  boost::tuple<int,std::wstring> send(connection_data con, std::list<std::string> messages); 
    188   static connection_data parse_header(const ::Plugin::Common_Header &header); 
     191  static connection_data parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data); 
    189192 
    190193private: 
    191194  void add_local_options(po::options_description &desc, client::configuration::data_type data); 
    192   void setup(client::configuration &config); 
     195  void setup(client::configuration &config, const ::Plugin::Common_Header& header); 
    193196  void add_command(std::wstring key, std::wstring args); 
    194197  void add_target(std::wstring key, std::wstring args); 
  • scripts/python/test_all.py

    r441a022 r0f7b655  
    1 from NSCP import Settings, Registry, Core, log, status, log_error, sleep 
     1from NSCP import Settings, Registry, Core, log, status, log_error 
    22from test_helper import BasicTest, TestResult, Callable, setup_singleton, install_testcases, init_testcases, shutdown_testcases 
    33 
  • scripts/python/test_eventlog.py

    rf7a074d r0f7b655  
    7373    result = TestResult() 
    7474    result.add_message(self.test_create('Application Error', 1000, '0', 'error', ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']), 'Testing to create a log message') 
    75     sleep(1) 
     75    sleep(500) 
    7676    log(self.last_message) 
    7777    log('%s'%self.last_message) 
  • scripts/python/test_nrpe.py

    re396b2f r0f7b655  
    187187      else: 
    188188        log('Waiting for %s (%s/%s)'%(uid,alias,target)) 
    189         sleep(1) 
     189        sleep(500) 
    190190    if found: 
    191191      return result 
     
    194194  def test_one(self, ssl=True, length=1024, state = status.UNKNOWN, tag = 'TODO'): 
    195195    result = TestResult('Testing NRPE: %s/%s/%s with various targets'%(ssl, length, tag)) 
    196     result.add(self.submit_payload('%s/%s/%s'%(ssl, length, tag), ssl, length, '%ssrc%s'%(tag, tag), state, '%smsg%s'%(tag, tag), '', "valid")) 
    197     result.add(self.submit_payload('%s/%s/%s'%(ssl, length, tag), ssl, length, '%ssrc%s'%(tag, tag), state, '%smsg%s'%(tag, tag), '', "test_rp")) 
    198     result.add(self.submit_payload('%s/%s/%s'%(ssl, length, tag), ssl, length, '%ssrc%s'%(tag, tag), state, '%smsg%s'%(tag, tag), '', "invalid")) 
     196    for t in ['valid', 'test_rp', 'invalid']: 
     197      result.add(self.submit_payload('%s/%s/%s'%(ssl, length, tag), ssl, length, '%ssrc%s'%(tag, tag), state, '%smsg%s'%(tag, tag), '', t)) 
    199198    return result 
    200199 
     
    207206    core.reload('test_nrpe_server') 
    208207 
    209     #                /settings/NRPE/test_nrpe_client/targets/default 
    210208    conf.set_string('/settings/NRPE/test_nrpe_client/targets/default', 'address', 'nrpe://127.0.0.1:35666') 
    211209    conf.set_bool('/settings/NRPE/test_nrpe_client/targets/default', 'use ssl', not ssl) 
  • scripts/python/test_nsca.py

    re396b2f r0f7b655  
    151151      if not self.has_response(uuid): 
    152152        log('Waiting for %s (%d/10)'%(uuid, i+1)) 
    153         sleep(1) 
     153        sleep(200) 
    154154      else: 
    155155        log('Got response %s'%uuid) 
     
    164164      if not rmsg.got_simple_response or not rmsg.got_response: 
    165165        log('Waiting for delayed response %s s/m: %s/%s - (%d/10)'%(uuid, rmsg.got_simple_response, rmsg.got_response, i+1)) 
    166         sleep(1) 
     166        sleep(500) 
    167167      else: 
    168168        log('Got delayed response %s'%uuid) 
     
    179179    return True 
    180180 
    181   def submit_payload(self, encryption, source, status, msg, perf, tag): 
     181  def submit_payload(self, encryption, target, length, source, status, msg, perf, tag): 
    182182    message = plugin_pb2.SubmitRequestMessage() 
    183183     
    184184    message.header.version = plugin_pb2.Common.VERSION_1 
    185     message.header.recipient_id = "test_rp" 
     185    message.header.recipient_id = target 
    186186    message.channel = 'nsca_test_outbox' 
    187187    host = message.header.hosts.add() 
    188     host.address = "127.0.0.1:15667" 
    189     host.id = "test_rp" 
    190     enc = host.metadata.add() 
    191     enc.key = "encryption" 
    192     enc.value = encryption 
    193     enc = host.metadata.add() 
    194     enc.key = "password" 
    195     enc.value = 'pwd-%s'%encryption 
     188    host.id = target 
     189    if (target == 'valid'): 
     190      pass 
     191    else: 
     192      host.address = "127.0.0.1:15667" 
     193      enc = host.metadata.add() 
     194      enc.key = "encryption" 
     195      enc.value = encryption 
     196      enc = host.metadata.add() 
     197      enc.key = "password" 
     198      enc.value = 'pwd-%s'%encryption 
     199      enc = host.metadata.add() 
     200      enc.key = "payload length" 
     201      enc.value = '%d'%length 
    196202 
    197203    uid = str(uuid.uuid4()) 
     
    208214    return result 
    209215     
    210   def submit_via_exec(self, encryption, source, status, msg, perf, tag): 
     216  def submit_via_exec(self, encryption, target, length, source, status, msg, perf, tag): 
    211217    uid = str(uuid.uuid4()) 
    212218   
     
    216222      '--result', '%d'%status,  
    217223      '--message', '%s - %s'%(uid, msg),  
    218       '--encryption', encryption, 
    219       '--password', 'pwd-%s'%encryption, 
    220       '--address', '127.0.0.1:15667' 
     224      '--target', target, 
    221225      ] 
     226    if (target == 'valid'): 
     227      pass 
     228    else: 
     229      args.extend([ 
     230        '--address', '127.0.0.1:15667', 
     231        '--encryption', encryption, 
     232        '--password', 'pwd-%s'%encryption, 
     233        '--payload-length', '%d'%length, 
     234      ]) 
    222235    (result_code, result_message) = core.simple_exec('any', 'nsca_submit', args) 
    223236    result = TestResult('Testing payload submission (via command line exec): %s'%tag) 
     
    232245    return result 
    233246 
    234   def test_one_crypto_full(self, encryption, state, key): 
     247  def test_one_crypto_full(self, encryption, state, key, target, length): 
    235248    result = TestResult('Testing %s/%s'%(encryption, key)) 
    236     result.add(self.submit_payload(encryption, '%ssrc%s'%(key, key), state, '%smsg%s'%(key, key), '', '%s/%s'%(state, encryption))) 
    237     result.add(self.submit_via_exec(encryption, '%ssrc%s'%(key, key), state, '%smsg%s'%(key, key), '', '%s/%s'%(state, encryption))) 
    238     return result 
    239  
    240   def test_one_crypto(self, crypto): 
     249    result.add(self.submit_payload(encryption, target, length, '%ssrc%s'%(key, key), state, '%smsg%s'%(key, key), '', '%s/%s/%d/%s'%(state, encryption, length, target))) 
     250    result.add(self.submit_via_exec(encryption, target, length, '%ssrc%s'%(key, key), state, '%smsg%s'%(key, key), '', '%s/%s/%d/%s'%(state, encryption, length, target))) 
     251    return result 
     252 
     253  def test_one_crypto(self, crypto, length=512): 
    241254    conf = Settings.get() 
    242255    conf.set_string('/settings/NSCA/test_nsca_server', 'encryption', '%s'%crypto) 
    243256    conf.set_string('/settings/NSCA/test_nsca_server', 'password', 'pwd-%s'%crypto) 
     257    conf.set_int('/settings/NSCA/test_nsca_server', 'payload length', length) 
    244258    core.reload('test_nsca_server') 
    245     result = TestResult('Testing encryption algorith: %s'%crypto) 
     259     
     260    conf.set_string('/settings/NSCA/test_nsca_client/targets/default', 'address', 'nsca://127.0.0.1:35667') 
     261    conf.set_string('/settings/NSCA/test_nsca_client/targets/default', 'encryption', '%s'%crypto) 
     262    conf.set_string('/settings/NSCA/test_nsca_client/targets/default', 'password', 'default-%s'%crypto) 
     263    conf.set_int('/settings/NSCA/test_nsca_client/targets/default', 'payload length', length*3) 
     264 
     265    conf.set_string('/settings/NSCA/test_nsca_client/targets/invalid', 'address', 'nsca://127.0.0.1:25667') 
     266    conf.set_string('/settings/NSCA/test_nsca_client/targets/invalid', 'encryption', 'none') 
     267    conf.set_string('/settings/NSCA/test_nsca_client/targets/invalid', 'password', 'invalid-%s'%crypto) 
     268    conf.set_int('/settings/NSCA/test_nsca_client/targets/invalid', 'payload length', length*2) 
     269 
     270    conf.set_string('/settings/NSCA/test_nsca_client/targets/valid', 'address', 'nsca://127.0.0.1:15667') 
     271    conf.set_string('/settings/NSCA/test_nsca_client/targets/valid', 'encryption', '%s'%crypto) 
     272    conf.set_string('/settings/NSCA/test_nsca_client/targets/valid', 'password', 'pwd-%s'%crypto) 
     273    conf.set_int('/settings/NSCA/test_nsca_client/targets/valid', 'payload length', length) 
     274    core.reload('test_nsca_client') 
     275 
     276     
     277     
     278    result = TestResult('Testing: %s/%d'%(crypto, length)) 
    246279    result.add_message(isOpen('localhost', 15667), 'Checking that port is open') 
    247     result.add(self.test_one_crypto_full(crypto, status.UNKNOWN, 'unknown')) 
    248     result.add(self.test_one_crypto_full(crypto, status.OK, 'ok')) 
    249     result.add(self.test_one_crypto_full(crypto, status.WARNING, 'warn')) 
    250     result.add(self.test_one_crypto_full(crypto, status.CRITICAL, 'crit')) 
     280    for target in ['valid', 'test_rp', 'invalid']: 
     281      result.add(self.test_one_crypto_full(crypto, status.UNKNOWN, 'unknown', target, length)) 
     282      result.add(self.test_one_crypto_full(crypto, status.OK, 'ok', target, length)) 
     283      result.add(self.test_one_crypto_full(crypto, status.WARNING, 'warn', target, length)) 
     284      result.add(self.test_one_crypto_full(crypto, status.CRITICAL, 'crit', target, length)) 
    251285    return result 
    252286 
     
    255289    cryptos = ["none", "xor", "des", "3des", "cast128", "xtea", "blowfish", "twofish", "rc2", "aes", "aes256", "aes192", "aes128", "serpent", "gost", "3way"] 
    256290    for c in cryptos: 
    257       result.add_message(True, 'Testing crypto: %s'%c) 
    258       result.add(self.test_one_crypto(c)) 
     291      for l in [128, 512, 1024, 4096]: 
     292        result.add(self.test_one_crypto(c, l)) 
     293      #result.add(self.test_one_crypto(c)) 
    259294     
    260295    return result 
  • scripts/python/test_python.py

    r441a022 r0f7b655  
    5858      old_stress_count = self.stress_count 
    5959      old_noop_count = self.noop_count 
    60       sleep(5) 
     60      sleep(5000) 
    6161      result.add_message(True, 'Commands/second: %d/%d'%( (self.stress_count-old_stress_count)/5, (self.noop_count-old_noop_count)/5 ) ) 
    6262    elapsed = (time() - start) 
  • scripts/python/test_stress.py

    r6a30f44 r0f7b655  
    113113        old_stress_count = self.results_count 
    114114        old_noop_count = self.check_count 
    115         sleep(5) 
     115        sleep(5000) 
    116116        result.add_message(True, 'Commands/second: %d/%d'%( (self.results_count-old_stress_count)/5, (self.check_count-old_noop_count)/5 ) ) 
    117117        if (self.results_count*100/total_count) > last_major + 10: 
  • scripts/python/test_w32_system.py

    rffa6a59 r0f7b655  
    5757      log('Starting notepad...') 
    5858      handle = subprocess.Popen('notepad.exe', shell=False) 
    59       sleep(1) 
     59      sleep(500) 
    6060      pids.append(handle.pid) 
    6161      for j in range(0,3): 
Note: See TracChangeset for help on using the changeset viewer.