Changeset f33c12f in nscp


Ignore:
Timestamp:
12/05/11 08:05:14 (18 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
9c06054
Parents:
9853bc3
Message:
  • Changed so Client interfaces no longer use "simple" interface meaning correct targets are now propagated (ie. calling NRPE -> NSCA will get correct host set automatically) (havent tested this yet, but now it builds at least, will test to night)
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • include/client/command_line_parser.cpp

    r96c1461 rf33c12f  
    5353 
    5454 
    55 int client::command_line_parser::commandLineExec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 
     55int client::command_line_parser::do_execute_command_as_exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result) { 
    5656  if (!config.validate()) 
    5757    throw cli_exception("Invalid data: " + config.to_string()); 
    5858  if (command == _T("help")) { 
    59     result = build_help(config); 
    60     return NSCAPI::returnUNKNOWN; 
     59    return nscapi::functions::create_simple_exec_response_unknown(command, build_help(config), result); 
    6160  } else if (command == _T("query")) { 
    6261    std::wstring msg, perf; 
    63     int ret = query(config, command, arguments, msg, perf); 
    64     if (perf.empty()) 
    65       result = msg; 
    66     else 
    67       result = msg + _T("|") + perf; 
     62    int ret = do_query(config, command, arguments, result); 
     63    nscapi::functions::make_exec_from_query(result); 
    6864    return ret; 
    6965  } else if (command == _T("exec")) { 
    70     return exec(config, command, arguments, result); 
     66    return do_exec(config, command, arguments, result); 
    7167  } else if (command == _T("submit")) { 
    72     boost::tuple<int,std::wstring> ret = simple_submit(config, command, arguments); 
    73     result = ret.get<1>(); 
    74     return ret.get<0>(); 
     68    int ret = do_submit(config, command, arguments, result); 
     69    nscapi::functions::make_exec_from_submit(result); 
     70    return ret; 
     71  } 
     72  return NSCAPI::returnIgnored; 
     73} 
     74 
     75int client::command_line_parser::do_execute_command_as_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result) { 
     76  if (!config.validate()) 
     77    throw cli_exception("Invalid data: " + config.to_string()); 
     78  if (command == _T("help")) { 
     79    return nscapi::functions::create_simple_query_response_unknown(command, build_help(config), _T(""), result); 
     80  } else if (command == _T("query")) { 
     81    return do_query(config, command, arguments, result); 
     82  } else if (command == _T("exec")) { 
     83    int ret = do_exec(config, command, arguments, result); 
     84    nscapi::functions::make_query_from_exec(result); 
     85    return ret; 
     86  } else if (command == _T("submit")) { 
     87    int ret = do_submit(config, command, arguments, result); 
     88    nscapi::functions::make_query_from_submit(result); 
     89    return ret; 
    7590  } 
    7691  return NSCAPI::returnIgnored; 
     
    97112} 
    98113 
    99 int client::command_manager::exec_simple(configuration &config, const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) { 
     114int client::command_manager::exec_simple(configuration &config, const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::string &response) { 
    100115  command_type::const_iterator cit = commands.find(command); 
    101116  if (cit == commands.end()) 
     
    112127  } 
    113128  // TODO: Add support for target here! 
    114   return client::command_line_parser::commandLineExec(config, ci.command, rendered_arguments, message); 
    115 } 
    116 int client::command_line_parser::query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &msg, std::wstring &perf) { 
     129  return client::command_line_parser::do_execute_command_as_exec(config, ci.command, rendered_arguments, response); 
     130} 
     131int client::command_line_parser::do_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &response) { 
    117132  boost::program_options::variables_map vm; 
    118133 
     
    141156  nscapi::functions::append_simple_query_request_payload(message.add_payload(), config.data->command, config.data->arguments); 
    142157  std::string result; 
    143   int ret = config.handler->query(config.data, message.mutable_header(), message.SerializeAsString(), result); 
    144   nscapi::functions::parse_simple_query_response(result, msg, perf); 
    145   return ret; 
    146 } 
    147  
    148 int client::command_line_parser::exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 
     158  return config.handler->query(config.data, message.mutable_header(), message.SerializeAsString(), result); 
     159} 
     160 
     161int client::command_line_parser::do_exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result) { 
    149162  boost::program_options::variables_map vm; 
    150163 
     
    173186  std::string response; 
    174187  nscapi::functions::append_simple_exec_request_payload(message.add_payload(), config.data->command, config.data->arguments); 
    175   int ret = config.handler->exec(config.data, message.mutable_header(), message.SerializeAsString(), response); 
    176   nscapi::functions::parse_simple_exec_result(response, result); 
    177   return ret; 
    178 } 
    179  
    180 boost::tuple<int,std::wstring> client::command_line_parser::simple_submit(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments) { 
     188  return config.handler->exec(config.data, message.mutable_header(), message.SerializeAsString(), response); 
     189} 
     190 
     191int client::command_line_parser::do_submit(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result) { 
    181192  boost::program_options::variables_map vm; 
    182193  po::options_description common("Common options"); 
     
    203214  nscapi::functions::append_simple_submit_request_payload(message.add_payload(), config.data->command, config.data->result, config.data->message); 
    204215 
    205   std::string response; 
    206   if (config.handler->submit(config.data, message.mutable_header(), message.SerializeAsString(), response) != NSCAPI::isSuccess) 
    207     return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("Failed to submit command")); 
    208  
    209   std::wstring messages; 
    210   int ret = nscapi::functions::parse_simple_submit_response(response, messages); 
    211   return boost::make_tuple(ret, messages); 
     216  return config.handler->submit(config.data, message.mutable_header(), message.SerializeAsString(), result); 
    212217} 
    213218void client::command_line_parser::modify_header(configuration &config, ::Plugin::Common_Header* header, nscapi::functions::destination_container &recipient) { 
     
    225230} 
    226231 
    227 int client::command_line_parser::relay_submit(configuration &config, const std::string &request, std::string &response) { 
     232int client::command_line_parser::do_relay_submit(configuration &config, const std::string &request, std::string &response) { 
    228233  Plugin::SubmitRequestMessage message; 
    229234  message.ParseFromString(request); 
  • include/client/command_line_parser.hpp

    r96c1461 rf33c12f  
    120120 
    121121    std::wstring add_command(std::wstring name, std::wstring args); 
    122     int exec_simple(configuration &config, const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 
     122    int exec_simple(configuration &config, const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::string &response); 
    123123 
    124124    static std::wstring make_key(std::wstring key) { 
     
    137137    static std::wstring build_help(configuration &config); 
    138138 
    139     static int commandLineExec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 
    140     static int relay_submit(configuration &config, const std::string &request, std::string &response); 
    141  
    142     static boost::tuple<int,std::wstring> simple_submit(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments); 
     139    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, std::string &result); 
     141    static int do_relay_submit(configuration &config, const std::string &request, std::string &response); 
    143142 
    144143    static std::wstring parse_command(std::wstring command, std::wstring prefix) { 
     
    163162    } 
    164163 
    165     static int query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &msg, std::wstring &perf); 
    166     //static std::list<std::string> submit(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments); 
    167     static int exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 
     164    static int do_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 
     165    static int do_exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 
     166    static int do_submit(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 
    168167 
    169168  private: 
  • include/nscapi/functions.hpp

    r96c1461 rf33c12f  
    4141 
    4242namespace nscapi { 
     43 
     44  namespace traits { 
     45 
     46    template<class T> 
     47    struct perf_data_consts { 
     48      static const T get_valid_perf_numbers(); 
     49      static const T get_replace_perf_coma_src(); 
     50      static const T get_replace_perf_coma_tgt(); 
     51    }; 
     52 
     53    template<> 
     54    struct perf_data_consts<std::wstring> { 
     55      static const std::wstring get_valid_perf_numbers() { 
     56        return _T("0123456789,."); 
     57      } 
     58      static const std::wstring get_replace_perf_coma_src() { 
     59        return _T(","); 
     60      } 
     61      static const std::wstring get_replace_perf_coma_tgt() { 
     62        return _T("."); 
     63      } 
     64    }; 
     65    template<> 
     66    struct perf_data_consts<std::string> { 
     67      static const std::string get_valid_perf_numbers() { 
     68        return "0123456789,."; 
     69      } 
     70      static const std::string get_replace_perf_coma_src() { 
     71        return ","; 
     72      } 
     73      static const std::string get_replace_perf_coma_tgt() { 
     74        return "."; 
     75      } 
     76    }; 
     77  } 
    4378  class functions { 
    4479  public: 
     
    71106      return NSCAPI::hasFailed; 
    72107    } 
     108    static Plugin::Common::ResultCode gbp_status_to_gbp_nagios(Plugin::Common::Status::StatusType ret) { 
     109      if (ret == Plugin::Common_Status_StatusType_OK) 
     110        return Plugin::Common_ResultCode_OK; 
     111      return Plugin::Common_ResultCode_UNKNOWN; 
     112    } 
     113    static Plugin::Common::Status::StatusType gbp_to_nagios_gbp_status(Plugin::Common::ResultCode ret) { 
     114      if (ret == Plugin::Common_ResultCode_UNKNOWN||ret == Plugin::Common_ResultCode_WARNING||ret == Plugin::Common_ResultCode_CRITCAL) 
     115        return Plugin::Common_Status_StatusType_CRITICAL; 
     116      return Plugin::Common_Status_StatusType_OK; 
     117    } 
     118     
    73119    static Plugin::LogEntry::Entry::Level log_to_gpb(NSCAPI::messageTypes ret) { 
    74120      if (ret == NSCAPI::critical) 
     
    98144    } 
    99145 
    100     static double trim_to_double(std::wstring s) { 
    101       std::wstring::size_type pend = s.find_first_not_of(_T("0123456789,.")); 
    102       if (pend != std::wstring::npos) 
     146 
     147    template<class T> 
     148    static double trim_to_double(T s) { 
     149      typename T::size_type pend = s.find_first_not_of(nscapi::traits::perf_data_consts<T>::get_valid_perf_numbers()); 
     150      if (pend != T::npos) 
    103151        s = s.substr(0,pend); 
    104       strEx::replace(s, _T(","), _T(".")); 
     152      strEx::replace(s, nscapi::traits::perf_data_consts<T>::get_replace_perf_coma_src(), nscapi::traits::perf_data_consts<T>::get_replace_perf_coma_tgt()); 
    105153      return strEx::stod(s); 
    106154    } 
     155 
     156     
    107157 
    108158    struct decoded_simple_command_data { 
     
    280330      Plugin::SubmitRequestMessage request; 
    281331      request.mutable_header()->CopyFrom(response.header()); 
     332      request.mutable_header()->set_source_id(request.mutable_header()->recipient_id()); 
    282333      request.set_channel(to_string(channel)); 
    283334      for (int i=0;i<response.payload_size();++i) { 
     
    287338      } 
    288339      message = request.SerializeAsString(); 
     340    } 
     341 
     342    static void make_query_from_exec(std::string &data) { 
     343      Plugin::ExecuteResponseMessage exec_response_message; 
     344      exec_response_message.ParseFromString(data); 
     345      Plugin::QueryResponseMessage query_response_message; 
     346      query_response_message.mutable_header()->CopyFrom(exec_response_message); 
     347      for (int i=0;i<exec_response_message.payload_size();++i) { 
     348        Plugin::ExecuteResponseMessage::Response p = exec_response_message.payload(i); 
     349        append_simple_query_response_payload(query_response_message.add_payload(), p.command(), p.result(), p.message()); 
     350      } 
     351      data = query_response_message.SerializeAsString(); 
     352    } 
     353    static void make_query_from_submit(std::string &data) { 
     354      Plugin::SubmitResponseMessage submit_response_message; 
     355      submit_response_message.ParseFromString(data); 
     356      Plugin::QueryResponseMessage query_response_message; 
     357      query_response_message.mutable_header()->CopyFrom(submit_response_message); 
     358      for (int i=0;i<submit_response_message.payload_size();++i) { 
     359        Plugin::SubmitResponseMessage::Response p = submit_response_message.payload(i); 
     360        append_simple_query_response_payload(query_response_message.add_payload(), p.command(), gbp_status_to_gbp_nagios(p.status().status()), p.status().message(), ""); 
     361      } 
     362      data = query_response_message.SerializeAsString(); 
     363    } 
     364 
     365    static void make_exec_from_submit(std::string &data) { 
     366      Plugin::SubmitResponseMessage submit_response_message; 
     367      submit_response_message.ParseFromString(data); 
     368      Plugin::ExecuteResponseMessage exec_response_message; 
     369      exec_response_message.mutable_header()->CopyFrom(submit_response_message); 
     370      for (int i=0;i<submit_response_message.payload_size();++i) { 
     371        Plugin::SubmitResponseMessage::Response p = submit_response_message.payload(i); 
     372        append_simple_exec_response_payload(exec_response_message.add_payload(), p.command(), gbp_status_to_gbp_nagios(p.status().status()), p.status().message()); 
     373      } 
     374      data = exec_response_message.SerializeAsString(); 
     375    } 
     376    static void make_exec_from_query(std::string &data) { 
     377      Plugin::QueryResponseMessage query_response_message; 
     378      query_response_message.ParseFromString(data); 
     379      Plugin::ExecuteResponseMessage exec_response_message; 
     380      exec_response_message.mutable_header()->CopyFrom(query_response_message); 
     381      for (int i=0;i<query_response_message.payload_size();++i) { 
     382        Plugin::QueryResponseMessage::Response p = query_response_message.payload(i); 
     383        std::string s = build_performance_data(p); 
     384        if (!s.empty()) 
     385          s = p.message() + "|" + s; 
     386        else 
     387          s = p.message(); 
     388        append_simple_exec_response_payload(exec_response_message.add_payload(), p.command(), p.result(), s); 
     389      } 
     390      data = exec_response_message.SerializeAsString(); 
     391    } 
     392 
     393 
     394    static void make_return_header(::Plugin::Common_Header *target, const ::Plugin::Common_Header &source) { 
     395      target->CopyFrom(source); 
     396      target->set_source_id(target->recipient_id()); 
    289397    } 
    290398 
     
    409517      return NSCAPI::returnUNKNOWN; 
    410518    } 
     519    static NSCAPI::nagiosReturn create_simple_query_response_unknown(std::wstring command, std::wstring msg, std::string &buffer) { 
     520      create_simple_query_response(command, NSCAPI::returnUNKNOWN, msg, _T(""), buffer); 
     521      return NSCAPI::returnUNKNOWN; 
     522    } 
    411523 
    412524    static void create_simple_query_response(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer) { 
     
    436548    } 
    437549 
    438     static void append_simple_exec_response_payload(Plugin::ExecuteResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg) { 
     550    template<class T> 
     551    static void append_response_payloads(T &target_message, std::string &payload) { 
     552      T source_message; 
     553      source_message.ParseFromString(payload); 
     554      for (int i=0;i<source_message.payload_size();++i) 
     555        target_message.add_payload()->CopyFrom(source_message.payload(i)); 
     556    } 
     557 
     558    static void append_simple_query_response_payload(Plugin::QueryResponseMessage::Response *payload, std::string command, NSCAPI::nagiosReturn ret, std::string msg, std::string perf = "") { 
    439559      payload->set_command(to_string(command)); 
    440560      payload->set_message(to_string(msg)); 
    441561      payload->set_result(nagios_status_to_gpb(ret)); 
     562      if (!perf.empty()) 
     563        parse_performance_data(payload, perf); 
     564    } 
     565/* 
     566    static void append_simple_exec_response_payload(Plugin::ExecuteResponseMessage::Response *payload, std::string command, Plugin::Common::ResultCode ret, std::string msg) { 
     567      payload->set_command(command); 
     568      payload->set_message(msg); 
     569      payload->set_result(ret); 
     570    } 
     571    */ 
     572    static void append_simple_exec_response_payload(Plugin::ExecuteResponseMessage::Response *payload, std::string command, int ret, std::string msg) { 
     573      payload->set_command(command); 
     574      payload->set_message(msg); 
     575      payload->set_result(nagios_status_to_gpb(ret)); 
     576    } 
     577    /* 
     578    static void append_simple_submit_response_payload(Plugin::SubmitResponseMessage::Response *payload, std::string command, Plugin::Common::ResultCode ret, std::string msg) { 
     579      payload->set_command(command); 
     580      payload->mutable_status()->set_status(gbp_to_nagios_gbp_status(ret)); 
     581      payload->mutable_status()->set_message(msg); 
     582    } 
     583    */ 
     584    static void append_simple_submit_response_payload(Plugin::SubmitResponseMessage::Response *payload, std::string command, int ret, std::string msg) { 
     585      payload->set_command(command); 
     586      payload->mutable_status()->set_status(status_to_gpb(ret)); 
     587      payload->mutable_status()->set_message(msg); 
    442588    } 
    443589 
     
    546692    } 
    547693 
    548     static void create_simple_exec_response(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring result, std::string &response) { 
     694    template<class T> 
     695    static int create_simple_exec_response(T command, NSCAPI::nagiosReturn ret, T result, std::string &response) { 
    549696      Plugin::ExecuteResponseMessage message; 
    550697      create_simple_header(message.mutable_header()); 
     
    556703      payload->set_result(nagios_status_to_gpb(ret)); 
    557704      message.SerializeToString(&response); 
     705      return ret; 
     706    } 
     707    template<class T> 
     708    static int create_simple_exec_response_unknown(T command, T result, std::string &response) { 
     709      Plugin::ExecuteResponseMessage message; 
     710      create_simple_header(message.mutable_header()); 
     711 
     712      Plugin::ExecuteResponseMessage::Response *payload = message.add_payload(); 
     713      payload->set_command(to_string(command)); 
     714      payload->set_message(to_string(result)); 
     715 
     716      payload->set_result(nagios_status_to_gpb(NSCAPI::returnUNKNOWN)); 
     717      message.SerializeToString(&response); 
     718      return NSCAPI::returnUNKNOWN; 
    558719    } 
    559720    static decoded_simple_command_data parse_simple_exec_request(const wchar_t* char_command, const std::string &request) { 
     
    587748    ////////////////////////////////////////////////////////////////////////// 
    588749 
    589     static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::wstring &perf) { 
    590       boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring> tok(perf, boost::escaped_list_separator<wchar_t>(L'\\', L' ', L'\'')); 
    591       BOOST_FOREACH(std::wstring s, tok) { 
     750    template<class T, class U> 
     751    struct tokenizer_data { 
     752      boost::escaped_list_separator<U> separator; // \\, ' ', \' 
     753      T perf_item_splitter;           // ;  
     754      T perf_equal_sign;              // = 
     755      T perf_valid_number;            // 0123456789., 
     756 
     757    }; 
     758 
     759    template<class T, class U> 
     760    static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, T &perf, tokenizer_data<T, U> tokenizer_data) { 
     761      boost::tokenizer<boost::escaped_list_separator<U>, typename T::const_iterator, T> tok(perf, tokenizer_data.separator); 
     762      BOOST_FOREACH(const T s, tok) { 
    592763        if (s.size() == 0) 
    593764          break; 
    594         strEx::splitVector items = strEx::splitV(s, _T(";")); 
     765        std::vector<T> items = strEx::splitV(s, tokenizer_data.perf_item_splitter); 
    595766        if (items.size() < 1) { 
    596767          Plugin::Common::PerformanceData* perfData = payload->add_perf(); 
    597768          perfData->set_type(Plugin::Common_DataType_STRING); 
    598           std::pair<std::wstring,std::wstring> fitem = strEx::split(_T(""), _T("=")); 
     769          std::pair<T,T> fitem = strEx::split(T(), tokenizer_data.perf_equal_sign); 
    599770          perfData->set_alias("invalid"); 
    600771          Plugin::Common_PerformanceData_StringValue* stringPerfData = perfData->mutable_string_value(); 
     
    605776        Plugin::Common::PerformanceData* perfData = payload->add_perf(); 
    606777        perfData->set_type(Plugin::Common_DataType_FLOAT); 
    607         std::pair<std::wstring,std::wstring> fitem = strEx::split(items[0], _T("=")); 
     778        std::pair<T,T> fitem = strEx::split(items[0], tokenizer_data.perf_equal_sign); 
    608779        perfData->set_alias(to_string(fitem.first)); 
    609780        Plugin::Common_PerformanceData_FloatValue* floatPerfData = perfData->mutable_float_value(); 
    610781 
    611         std::wstring::size_type pend = fitem.second.find_first_not_of(_T("0123456789,.")); 
    612         if (pend == std::wstring::npos) { 
    613           floatPerfData->set_value(trim_to_double(fitem.second.c_str())); 
     782        typename T::size_type pend = fitem.second.find_first_not_of(tokenizer_data.perf_valid_number); 
     783        if (pend == T::npos) { 
     784          floatPerfData->set_value(trim_to_double(fitem.second)); 
    614785        } else { 
    615           floatPerfData->set_value(trim_to_double(fitem.second.substr(0,pend).c_str())); 
     786          floatPerfData->set_value(trim_to_double(fitem.second.substr(0,pend))); 
    616787          floatPerfData->set_unit(to_string(fitem.second.substr(pend))); 
    617788        } 
     
    625796        } 
    626797      } 
    627 //      std::wcout << _T("Converting performance data") << perf << _T(" -- ") << utf8::cvt<std::wstring>(build_performance_data(*resp)) << std::endl; 
    628     } 
     798    } 
     799    static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::wstring &perf) { 
     800      typedef std::wstring t_string; 
     801      typedef wchar_t t_char; 
     802      tokenizer_data<t_string, t_char> data; 
     803      data.separator = boost::escaped_list_separator<t_char>(L'\\', L' ', L'\''); 
     804      data.perf_equal_sign = _T("="); 
     805      data.perf_item_splitter = _T(";"); 
     806      data.perf_valid_number = _T("0123456789,."); 
     807      parse_performance_data<t_string, t_char>(payload, perf, data); 
     808    } 
     809    static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::string &perf) { 
     810      typedef std::string t_string; 
     811      typedef char t_char; 
     812      tokenizer_data<t_string, t_char> data; 
     813      data.separator = boost::escaped_list_separator<t_char>('\\', ' ', '\''); 
     814      data.perf_equal_sign = "="; 
     815      data.perf_item_splitter = ";"; 
     816      data.perf_valid_number = "0123456789,."; 
     817      parse_performance_data<t_string, t_char>(payload, perf, data); 
     818    } 
     819 
    629820    static std::string build_performance_data(Plugin::QueryResponseMessage::Response const &payload) { 
    630821      std::stringstream ss; 
  • include/strEx.h

    r96c1461 rf33c12f  
    548548    return boost::lexical_cast<int>(s.c_str()); 
    549549  } 
    550   inline double stod(std::wstring s) { 
     550  template<class T> 
     551  inline double stod(T s) { 
    551552    return boost::lexical_cast<double>(s.c_str()); 
    552553  } 
     
    749750  } 
    750751  typedef std::vector<std::wstring> splitVector; 
    751   inline splitVector splitV(const std::wstring str, const std::wstring key) { 
    752     splitVector ret; 
    753     std::wstring::size_type pos = 0, lpos = 0; 
    754     while ((pos = str.find(key, pos)) !=  std::wstring::npos) { 
     752  template<class T> 
     753  inline std::vector<T> splitV(const T str, const T key) { 
     754    std::vector<T> ret; 
     755    typename T::size_type pos = 0, lpos = 0; 
     756    while ((pos = str.find(key, pos)) !=  T::npos) { 
    755757      ret.push_back(str.substr(lpos, pos-lpos)); 
    756758      lpos = ++pos; 
     
    789791    return trim_left( trim_right( str , t) , t ); 
    790792  }  
    791   inline std::pair<std::wstring,std::wstring> split(std::wstring str, std::wstring key) { 
    792     std::wstring::size_type pos = str.find(key); 
    793     if (pos == std::wstring::npos) 
    794       return std::pair<std::wstring,std::wstring>(str, _T("")); 
    795     return std::pair<std::wstring,std::wstring>(str.substr(0, pos), str.substr(pos+key.length())); 
     793  template<class T> 
     794  inline std::pair<T,T> split(T str, T key) { 
     795    typename T::size_type pos = str.find(key); 
     796    if (pos == T::npos) 
     797      return std::pair<T,T>(str, T()); 
     798    return std::pair<T,T>(str.substr(0, pos), str.substr(pos+key.length())); 
    796799  } 
    797800  typedef std::pair<std::wstring,std::wstring> token; 
     
    10561059      return utf8::cvt<std::string>(arg); 
    10571060    } 
     1061    template <typename T> std::string to_string(const wchar_t* arg) { 
     1062      return utf8::cvt<std::string>(std::wstring(arg)); 
     1063    } 
    10581064    template <typename T> std::wstring to_wstring(const T& arg) { 
    10591065      try { 
  • include/utils.h

    r7443b58 rf33c12f  
    3131#define MAP_OPTIONS_BEGIN(args) \ 
    3232  for (std::list<std::wstring>::const_iterator cit__=args.begin();cit__!=args.end();++cit__) { \ 
    33   std::pair<std::wstring,std::wstring> p__ = strEx::split(*cit__,_T("=")); if (false) {} 
     33  std::pair<std::wstring,std::wstring> p__ = strEx::split(*cit__,std::wstring(_T("="))); if (false) {} 
    3434 
    3535#define MAP_OPTIONS_SHOWALL(obj) \ 
     
    108108#define MAP_OPTIONS_SECONDARY_BEGIN(splt, arg) \ 
    109109  else if (p__.first.find(splt) != std::wstring::npos) { \ 
    110   std::pair<std::wstring,std::wstring> arg = strEx::split(p__.first,splt); if (false) {} 
     110  std::pair<std::wstring,std::wstring> arg = strEx::split(p__.first,std::wstring(splt)); if (false) {} 
    111111 
    112112#define MAP_OPTIONS_SECONDARY_STR_AND(opt, value, objfirst, objsecond, extra) \ 
  • modules/CheckHelpers/CheckHelpers.cpp

    ra44cb15 rf33c12f  
    145145  for (cit=arguments.begin();cit!=arguments.end();++cit) { 
    146146    std::wstring arg = *cit; 
    147     std::pair<std::wstring,std::wstring> p = strEx::split(arg,_T("=")); 
     147    std::pair<std::wstring,std::wstring> p = strEx::split(arg,std::wstring(_T("="))); 
    148148    if (p.first == _T("command")) { 
    149149      if (!currentCommand.first.empty()) 
  • modules/DistributedClient/DistributedClient.cpp

    r96c1461 rf33c12f  
    2222#include "DistributedClient.h" 
    2323#include <time.h> 
    24 #include <strEx.h> 
     24#include <boost/filesystem.hpp> 
    2525 
    2626#include <strEx.h> 
     
    3636 
    3737/** 
    38  * Default c-tor 
    39  * @return  
    40  */ 
     38* Default c-tor 
     39* @return  
     40*/ 
    4141DistributedClient::DistributedClient() {} 
    4242 
    4343/** 
    44  * Default d-tor 
    45  * @return  
    46  */ 
     44* Default d-tor 
     45* @return  
     46*/ 
    4747DistributedClient::~DistributedClient() {} 
    4848 
    4949/** 
    50  * Load (initiate) module. 
    51  * Start the background collector thread and let it run until unloadModule() is called. 
    52  * @return true 
    53  */ 
     50* Load (initiate) module. 
     51* Start the background collector thread and let it run until unloadModule() is called. 
     52* @return true 
     53*/ 
    5454bool DistributedClient::loadModule() { 
    5555  return false; 
     
    7070    sh::settings_registry settings(get_settings_proxy()); 
    7171    settings.set_alias(_T("distributed"), alias, _T("client")); 
    72  
    7372    target_path = settings.alias().get_settings_path(_T("targets")); 
    7473 
     
    8180      (_T("targets"), sh::fun_values_path(boost::bind(&DistributedClient::add_target, this, _1, _2)),  
    8281      _T("REMOTE TARGET DEFINITIONS"), _T("")) 
     82 
    8383      ; 
    8484 
     
    8989      ; 
    9090 
    91     settings.alias(_T("/targets/default")).add_key_to_settings() 
     91    settings.alias().add_key_to_settings(_T("targets/default")) 
    9292 
    9393      (_T("timeout"), sh::uint_key(&timeout, 30), 
     
    140140  return true; 
    141141} 
     142std::string get_command(std::string alias, std::string command = "") { 
     143  if (!alias.empty()) 
     144    return alias;  
     145  if (!command.empty()) 
     146    return command;  
     147  return "_NRPE_CHECK"; 
     148} 
    142149 
    143150////////////////////////////////////////////////////////////////////////// 
     
    147154void DistributedClient::add_target(std::wstring key, std::wstring arg) { 
    148155  try { 
    149     targets.add(get_settings_proxy(), target_path , key, arg); 
     156    nscapi::target_handler::target t = targets.add(get_settings_proxy(), target_path , key, arg); 
     157    if (t.has_option(_T("certificate"))) { 
     158      boost::filesystem::wpath p = t.options[_T("certificate")]; 
     159      if (!boost::filesystem::is_regular(p)) { 
     160        p = get_core()->getBasePath() / p; 
     161        t.options[_T("certificate")] = utf8::cvt<std::wstring>(p.string()); 
     162        targets.add(t); 
     163      } 
     164      if (boost::filesystem::is_regular(p)) { 
     165        NSC_DEBUG_MSG_STD(_T("Using certificate: ") + p.string()); 
     166      } else { 
     167        NSC_LOG_ERROR_STD(_T("Certificate not found: ") + p.string()); 
     168      } 
     169    } 
    150170  } catch (...) { 
    151171    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 
     
    157177    std::wstring key = commands.add_command(name, args); 
    158178    if (!key.empty()) 
    159       register_command(key.c_str(), _T("Custom command for: ") + name); 
     179      register_command(key.c_str(), _T("DNSCP relay for: ") + name); 
    160180  } catch (boost::program_options::validation_error &e) { 
    161181    NSC_LOG_ERROR_STD(_T("Could not add command ") + name + _T(": ") + utf8::to_unicode(e.what())); 
     
    166186 
    167187/** 
    168  * Unload (terminate) module. 
    169  * Attempt to stop the background processing thread. 
    170  * @return true if successfully, false if not (if not things might be bad) 
    171  */ 
     188* Unload (terminate) module. 
     189* Attempt to stop the background processing thread. 
     190* @return true if successfully, false if not (if not things might be bad) 
     191*/ 
    172192bool DistributedClient::unloadModule() { 
    173193  return true; 
    174194} 
    175195 
    176 NSCAPI::nagiosReturn DistributedClient::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) { 
    177   std::wstring cmd = client::command_line_parser::parse_command(command, _T("dist")); 
    178  
     196NSCAPI::nagiosReturn DistributedClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
     197  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
     198  std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 
    179199  client::configuration config; 
    180200  setup(config); 
    181   if (cmd == _T("query")) 
    182     return client::command_line_parser::query(config, cmd, arguments, message, perf); 
    183   if (cmd == _T("submit")) { 
    184     boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 
    185     message = result.get<1>(); 
    186     return result.get<0>(); 
    187   } 
    188   if (cmd == _T("exec")) { 
    189     return client::command_line_parser::exec(config, cmd, arguments, message); 
    190   } 
    191   return commands.exec_simple(config, target, command, arguments, message, perf); 
    192 } 
    193  
    194 int DistributedClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 
    195   std::wstring cmd = client::command_line_parser::parse_command(command, _T("nrpe")); 
     201  if (!client::command_line_parser::is_command(cmd)) 
     202    return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 
     203  return commands.exec_simple(config, data.target, char_command, data.args, result); 
     204} 
     205 
     206int DistributedClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
     207  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
     208  std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
    196209  if (!client::command_line_parser::is_command(cmd)) 
    197210    return NSCAPI::returnIgnored; 
    198  
    199211  client::configuration config; 
    200212  setup(config); 
    201   return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 
    202 } 
    203  
    204 NSCAPI::nagiosReturn DistributedClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 
    205   try { 
    206     client::configuration config; 
    207     setup(config); 
    208  
    209     if (!client::command_line_parser::relay_submit(config, request, response)) { 
    210       NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 
    211       return NSCAPI::hasFailed; 
    212     } 
    213     return NSCAPI::isSuccess; 
    214   } catch (std::exception &e) { 
    215     NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::to_unicode(e.what())); 
    216     return NSCAPI::hasFailed; 
    217   } catch (...) { 
    218     NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 
    219     return NSCAPI::hasFailed; 
    220   } 
     213  return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     214} 
     215 
     216NSCAPI::nagiosReturn DistributedClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
     217  client::configuration config; 
     218  setup(config); 
     219  return client::command_line_parser::do_relay_submit(config, request, result); 
    221220} 
    222221 
     
    227226void DistributedClient::add_local_options(po::options_description &desc, client::configuration::data_type data) { 
    228227  desc.add_options() 
     228    ("certificate,c", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "certificate", _1)),  
     229    "Length of payload (has to be same as on the server)") 
     230    /* 
     231    ("no-ssl,n", po::value<bool>(&command_data.no_ssl)->zero_tokens()->default_value(false), "Do not initial an ssl handshake with the server, talk in plain text.") 
     232 
     233    ("cert,c", po::value<std::wstring>(&command_data.cert)->default_value(cert_), "Certificate to use.") 
     234    */ 
    229235    ; 
    230236} 
     
    235241 
    236242  net::wurl url; 
    237   url.protocol = _T("nrpe"); 
    238   url.port = 5666; 
     243  url.protocol = _T("dnscp"); 
     244  url.port = 5669; 
    239245  nscapi::target_handler::optarget opt = targets.find_target(_T("default")); 
    240246  if (opt) { 
     
    245251        url.port = strEx::stoi(t.options[_T("port")]); 
    246252      } catch (...) {} 
    247 } 
     253    } 
    248254    std::string keys[] = {"certificate", "timeout", "payload length", "ssl"}; 
    249255    BOOST_FOREACH(std::string s, keys) { 
     
    270276// 
    271277 
    272  
     278std::string gather_and_log_errors(std::string  &payload) { 
     279  NSCPIPC::ErrorMessage message; 
     280  message.ParseFromString(payload); 
     281  std::string ret; 
     282  for (int i=0;i<message.error_size();i++) { 
     283    ret += message.error(i).message(); 
     284    NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 
     285  } 
     286  return ret; 
     287} 
    273288int DistributedClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    274   NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 
    275   try { 
    276  
    277     Plugin::QueryRequestMessage request_message; 
    278     request_message.ParseFromString(request); 
    279     connection_data con = parse_header(*header); 
    280  
    281     std::list<nscp::packet> chunks; 
    282     chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request, 0)); 
    283     chunks = instance->send(con, chunks); 
    284     BOOST_FOREACH(nscp::packet &packet, chunks) { 
    285       if (nscp::checks::is_query_response(packet)) { 
    286         reply = packet.payload; 
    287       } else if (nscp::checks::is_error(packet)) { 
    288         NSCPIPC::ErrorMessage message; 
    289         message.ParseFromString(packet.payload); 
    290         for (int i=0;i<message.error_size();i++) { 
    291           NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 
    292         } 
    293         ret = NSCAPI::returnUNKNOWN; 
    294       } else { 
    295         NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(packet.signature.payload_type)); 
    296         ret = NSCAPI::returnUNKNOWN; 
    297       } 
    298     } 
    299     return ret; 
    300   } catch (std::exception &e) { 
    301     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    302     return NSCAPI::returnUNKNOWN; 
    303   } 
     289  int ret = NSCAPI::returnUNKNOWN; 
     290  Plugin::QueryRequestMessage request_message; 
     291  request_message.ParseFromString(request); 
     292  connection_data con = parse_header(*header); 
     293 
     294  Plugin::QueryResponseMessage response_message; 
     295  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     296 
     297  std::list<nscp::packet> chunks; 
     298  chunks.push_back(nscp::factory::create_envelope_request(1)); 
     299  chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request, 0)); 
     300  chunks = instance->send(con, chunks); 
     301  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
     302    if (nscp::checks::is_query_response(chunk)) { 
     303      nscapi::functions::append_response_payloads(response_message, chunk.payload); 
     304    } else if (nscp::checks::is_error(chunk)) { 
     305      std::string error = gather_and_log_errors(chunk.payload); 
     306      nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 
     307      ret = NSCAPI::returnUNKNOWN; 
     308    } else { 
     309      NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 
     310      nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 
     311      ret = NSCAPI::returnUNKNOWN; 
     312    } 
     313  } 
     314  response_message.SerializeToString(&reply); 
     315  return ret; 
    304316} 
    305317 
    306318int DistributedClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    307   std::list<std::string> result; 
    308   std::wstring channel; 
    309   try { 
    310  
    311     Plugin::SubmitRequestMessage message; 
    312     message.ParseFromString(request); 
    313     connection_data con = parse_header(*header); 
    314     channel = utf8::cvt<std::wstring>(message.channel()); 
    315  
    316     std::list<nscp::packet> chunks; 
    317     chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request, 0)); 
    318     chunks = instance->send(con, chunks); 
    319     BOOST_FOREACH(nscp::packet &chunk, chunks) { 
    320       if (nscp::checks::is_query_response(chunk)) { 
    321         result.push_back(chunk.payload); 
    322       } else if (nscp::checks::is_error(chunk)) { 
    323         NSCPIPC::ErrorMessage message; 
    324         message.ParseFromString(chunk.payload); 
    325         for (int i=0;i<message.error_size();i++) { 
    326           result.push_back("Error: " + message.error(i).message()); 
    327         } 
    328       } else { 
    329         NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 
    330         result.push_back("Invalid payload"); 
    331       } 
    332     } 
    333     if (result.empty()) { 
    334       std::wstring msg; 
    335       BOOST_FOREACH(std::string &e, result) { 
    336         msg += utf8::cvt<std::wstring>(e); 
    337       } 
    338       nscapi::functions::create_simple_submit_response(channel, _T(""), result.empty()?NSCAPI::isSuccess:NSCAPI::hasFailed, msg, reply); 
    339     } 
    340  
    341     return result.empty()?NSCAPI::isSuccess:NSCAPI::hasFailed; 
    342   } catch (std::exception &e) { 
    343     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    344     nscapi::functions::create_simple_submit_response(channel, _T(""), NSCAPI::hasFailed, utf8::cvt<std::wstring>(e.what()), reply); 
    345     return NSCAPI::hasFailed; 
    346   } 
     319  int ret = NSCAPI::returnUNKNOWN; 
     320  Plugin::SubmitRequestMessage request_message; 
     321  request_message.ParseFromString(request); 
     322  connection_data con = parse_header(*header); 
     323  Plugin::SubmitResponseMessage response_message; 
     324  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     325 
     326  std::list<nscp::packet> chunks; 
     327  chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request, 0)); 
     328  chunks = instance->send(con, chunks); 
     329  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
     330    if (nscp::checks::is_submit_response(chunk)) { 
     331      nscapi::functions::append_response_payloads(response_message, chunk.payload); 
     332    } else if (nscp::checks::is_error(chunk)) { 
     333      std::string error = gather_and_log_errors(chunk.payload); 
     334      nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 
     335      ret = NSCAPI::returnUNKNOWN; 
     336    } else { 
     337      NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 
     338      nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 
     339      ret = NSCAPI::returnUNKNOWN; 
     340    } 
     341  } 
     342  response_message.SerializeToString(&reply); 
     343  return ret; 
    347344} 
    348345 
    349346int DistributedClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    350347  int ret = NSCAPI::returnOK; 
    351   try { 
    352     Plugin::ExecuteRequestMessage request_message; 
    353     request_message.ParseFromString(request); 
    354     connection_data con = parse_header(*header); 
    355  
    356     std::list<nscp::packet> chunks; 
    357     chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request, 0)); 
    358     chunks = instance->send(con, chunks); 
    359     BOOST_FOREACH(nscp::packet &chunk, chunks) { 
    360       if (nscp::checks::is_exec_response(chunk)) { 
    361         reply = chunk.payload; 
    362       } else if (nscp::checks::is_error(chunk)) { 
    363         NSCPIPC::ErrorMessage message; 
    364         message.ParseFromString(chunk.payload); 
    365         for (int i=0;i<message.error_size();i++) { 
    366           NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 
    367           ret = NSCAPI::returnUNKNOWN; 
    368         } 
    369       } else { 
    370         NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 
    371         ret = NSCAPI::returnUNKNOWN; 
    372       } 
    373     } 
    374     return ret; 
    375   } catch (std::exception &e) { 
    376     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    377     return NSCAPI::returnUNKNOWN; 
    378   } 
     348  Plugin::ExecuteRequestMessage request_message; 
     349  request_message.ParseFromString(request); 
     350  connection_data con = parse_header(*header); 
     351 
     352  Plugin::ExecuteResponseMessage response_message; 
     353  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     354 
     355  std::list<nscp::packet> chunks; 
     356  chunks.push_back(nscp::factory::create_envelope_request(1)); 
     357  chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request, 0)); 
     358  chunks = instance->send(con, chunks); 
     359  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
     360    if (nscp::checks::is_exec_response(chunk)) { 
     361      nscapi::functions::append_response_payloads(response_message, chunk.payload); 
     362    } else if (nscp::checks::is_error(chunk)) { 
     363      std::string error = gather_and_log_errors(chunk.payload); 
     364      nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 
     365      ret = NSCAPI::returnUNKNOWN; 
     366    } else { 
     367      NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 
     368      nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 
     369      ret = NSCAPI::returnUNKNOWN; 
     370    } 
     371  } 
     372  response_message.SerializeToString(&reply); 
     373  return ret; 
    379374} 
    380375 
     
    389384  std::wstring host = generic_data->host; 
    390385  if (host.empty() && !generic_data->target.empty()) { 
    391     nscapi::target_handler::optarget t = targets.find_target(generic_data->target); 
    392     if (t) 
    393       host = (*t).host; 
     386  nscapi::target_handler::optarget t = targets.find_target(generic_data->target); 
     387  if (t) 
     388  host = (*t).host; 
    394389  } 
    395390  */ 
  • modules/DistributedClient/DistributedClient.h

    r96c1461 rf33c12f  
    3939 
    4040 
    41 class DistributedClient : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 
     41class DistributedClient : public nscapi::impl::simple_plugin { 
    4242private: 
    4343 
     
    4949 
    5050  struct connection_data { 
    51     std::string cert; 
    5251    std::string address; 
    5352    int timeout; 
     
    5655 
    5756    connection_data(nscapi::functions::destination_container recipient) { 
    58       cert = recipient.get_string_data("certificate"); 
    5957      timeout = recipient.get_int_data("timeout", 30); 
    60       buffer_length = recipient.get_int_data("payload length", 1024); 
    61       use_ssl = recipient.get_bool_data("ssl"); 
    62       if (recipient.has_data("no ssl")) 
    63         use_ssl = !recipient.get_bool_data("no ssl"); 
     58 
    6459      address = recipient.address; 
    6560    } 
     
    6964      ss << _T("address: ") << utf8::cvt<std::wstring>(address); 
    7065      ss << _T(", timeout: ") << timeout; 
    71       ss << _T(", buffer_length: ") << buffer_length; 
    72       ss << _T(", use_ssl: ") << use_ssl; 
    73       ss << _T(", certificate: ") << utf8::cvt<std::wstring>(cert); 
    7466      return ss.str(); 
    7567    } 
     
    137129  bool hasNotificationHandler() { return true; }; 
    138130  NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 
    139   NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 
    140   int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 
     131  NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 
     132  NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 
    141133 
    142134private: 
  • modules/DistributedServer/handler_impl.cpp

    r98113da rf33c12f  
    145145  std::string outBuffer; 
    146146  if (command.empty() || command == _T("_NSCP_CHECK")) { 
    147     nscapi::functions::create_simple_exec_response(_T("_NSCP_CHECK"), NSCAPI::returnOK, _T("I (") + nscapi::plugin_singleton->get_core()->getApplicationVersionString() + _T(") seem to be doing fine..."), outBuffer); 
     147    nscapi::functions::create_simple_exec_response<std::string>("_NSCP_CHECK", NSCAPI::returnOK, "I (" + utf8::cvt<std::string>(nscapi::plugin_singleton->get_core()->getApplicationVersionString()) + ") seem to be doing fine...", outBuffer); 
    148148  } else if (!allowArgs_ && payload.arguments_size() > 0) { 
    149149    nscapi::functions::create_simple_exec_response(command, NSCAPI::returnUNKNOWN, _T("Arguments not allowed for command: ") + command, outBuffer); 
  • modules/NRPEClient/NRPEClient.cpp

    r9853bc3 rf33c12f  
    5454 
    5555bool NRPEClient::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
    56   std::map<std::wstring,std::wstring> commands; 
    5756 
    5857  std::wstring certificate; 
     
    187186} 
    188187 
    189 NSCAPI::nagiosReturn NRPEClient::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) { 
    190   std::wstring cmd = client::command_line_parser::parse_command(command, _T("nrpe")); 
    191  
     188NSCAPI::nagiosReturn NRPEClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
     189  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
     190  std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 
    192191  client::configuration config; 
    193192  setup(config); 
    194   if (cmd == _T("query")) 
    195     return client::command_line_parser::query(config, cmd, arguments, message, perf); 
    196   if (cmd == _T("submit")) { 
    197     boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 
    198     message = result.get<1>(); 
    199     return result.get<0>(); 
    200   } 
    201   if (cmd == _T("exec")) { 
    202     return client::command_line_parser::exec(config, cmd, arguments, message); 
    203   } 
    204   return commands.exec_simple(config, target, command, arguments, message, perf); 
    205 } 
    206  
    207 int NRPEClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 
    208   std::wstring cmd = client::command_line_parser::parse_command(command, _T("nrpe")); 
     193  if (!client::command_line_parser::is_command(cmd)) 
     194    return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 
     195  return commands.exec_simple(config, data.target, char_command, data.args, result); 
     196} 
     197 
     198NSCAPI::nagiosReturn NRPEClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
     199  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
     200  std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
    209201  if (!client::command_line_parser::is_command(cmd)) 
    210202    return NSCAPI::returnIgnored; 
    211  
    212203  client::configuration config; 
    213204  setup(config); 
    214   return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 
    215 } 
    216  
    217 NSCAPI::nagiosReturn NRPEClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 
    218   try { 
    219     client::configuration config; 
    220     setup(config); 
    221  
    222     if (!client::command_line_parser::relay_submit(config, request, response)) { 
    223       NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 
    224       return NSCAPI::hasFailed; 
    225     } 
    226     return NSCAPI::isSuccess; 
    227   } catch (std::exception &e) { 
    228     NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::to_unicode(e.what())); 
    229     return NSCAPI::hasFailed; 
    230   } catch (...) { 
    231     NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 
    232     return NSCAPI::hasFailed; 
    233   } 
     205  return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     206} 
     207 
     208NSCAPI::nagiosReturn NRPEClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
     209  client::configuration config; 
     210  setup(config); 
     211  return client::command_line_parser::do_relay_submit(config, request, result); 
    234212} 
    235213 
     
    292270 
    293271int NRPEClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    294   NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 
    295   try { 
    296     Plugin::QueryRequestMessage request_message; 
    297     request_message.ParseFromString(request); 
    298     connection_data con = parse_header(*header); 
    299  
    300     Plugin::QueryResponseMessage response_message; 
    301     nscapi::functions::create_simple_header(response_message.mutable_header()); // TODO copy request header (inverted) 
    302  
    303     for (int i=0;i<request_message.payload_size();i++) { 
    304       std::string command = get_command(request_message.payload(i).alias(), request_message.payload(i).command()); 
    305       std::string data = command; 
    306       for (int a=0;a<request_message.payload(i).arguments_size();a++) { 
    307         data += "!" + request_message.payload(i).arguments(a); 
    308       } 
    309       boost::tuple<int,std::wstring> ret = instance->send(con, data); 
    310       std::pair<std::wstring,std::wstring> rdata = strEx::split(ret.get<1>(), _T("|")); 
    311       nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), utf8::cvt<std::wstring>(command), ret.get<0>(), rdata.first, rdata.second); 
    312     } 
    313     response_message.SerializeToString(&reply); 
    314     return NSCAPI::isSuccess; 
    315   } catch (std::exception &e) { 
    316     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 
    317     nscapi::functions::create_simple_query_response(_T("command"), NSCAPI::returnUNKNOWN, _T("Exception: ") + utf8::to_unicode(e.what()), _T(""), reply); 
    318     return NSCAPI::returnUNKNOWN; 
    319   } 
     272  Plugin::QueryRequestMessage request_message; 
     273  request_message.ParseFromString(request); 
     274  connection_data con = parse_header(*header); 
     275 
     276  Plugin::QueryResponseMessage response_message; 
     277  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     278 
     279  for (int i=0;i<request_message.payload_size();i++) { 
     280    std::string command = get_command(request_message.payload(i).alias(), request_message.payload(i).command()); 
     281    std::string data = command; 
     282    for (int a=0;a<request_message.payload(i).arguments_size();a++) { 
     283      data += "!" + request_message.payload(i).arguments(a); 
     284    } 
     285    boost::tuple<int,std::wstring> ret = instance->send(con, data); 
     286    std::pair<std::wstring,std::wstring> rdata = strEx::split(ret.get<1>(), std::wstring(_T("|"))); 
     287    nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), utf8::cvt<std::wstring>(command), ret.get<0>(), rdata.first, rdata.second); 
     288  } 
     289  response_message.SerializeToString(&reply); 
     290  return NSCAPI::isSuccess; 
    320291} 
    321292 
    322293int NRPEClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    323   std::wstring channel; 
    324   try { 
    325     Plugin::SubmitRequestMessage message; 
    326     message.ParseFromString(request); 
    327     connection_data con = parse_header(*header); 
    328     channel = utf8::cvt<std::wstring>(message.channel()); 
    329      
    330     for (int i=0;i<message.payload_size();++i) { 
    331       std::string command = get_command(message.payload(i).alias(), message.payload(i).command()); 
    332       std::string data = command; 
    333       for (int a=0;a<message.payload(i).arguments_size();a++) { 
    334         data += "!" + message.payload(i).arguments(i); 
    335       } 
    336       boost::tuple<int,std::wstring> ret = instance->send(con, data); 
    337       // TODO: Change this to append! 
    338       nscapi::functions::create_simple_submit_response(channel, utf8::cvt<std::wstring>(command), ret.get<0>(), _T("Message submitted successfully: ") + ret.get<1>(), reply); 
    339       return NSCAPI::isSuccess; 
    340     } 
    341     nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), NSCAPI::returnUNKNOWN, _T("Empty message was submitted"), reply); 
    342     return NSCAPI::isSuccess; 
    343   } catch (std::exception &e) { 
    344     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 
    345     nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), NSCAPI::returnUNKNOWN, utf8::to_unicode(e.what()), reply); 
    346     return NSCAPI::hasFailed; 
    347   }  
     294  Plugin::SubmitRequestMessage request_message; 
     295  request_message.ParseFromString(request); 
     296  connection_data con = parse_header(*header); 
     297  std::wstring channel = utf8::cvt<std::wstring>(request_message.channel()); 
     298   
     299  Plugin::SubmitResponseMessage response_message; 
     300  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     301 
     302  for (int i=0;i<request_message.payload_size();++i) { 
     303    std::string command = get_command(request_message.payload(i).alias(), request_message.payload(i).command()); 
     304    std::string data = command; 
     305    for (int a=0;a<request_message.payload(i).arguments_size();a++) { 
     306      data += "!" + request_message.payload(i).arguments(i); 
     307    } 
     308    boost::tuple<int,std::wstring> ret = instance->send(con, data); 
     309    nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), command, ret.get<0>(), utf8::cvt<std::string>(ret.get<1>())); 
     310  } 
     311  response_message.SerializeToString(&reply); 
     312  return NSCAPI::isSuccess; 
    348313} 
    349314 
    350315int NRPEClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    351   NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 
    352   try { 
    353     Plugin::ExecuteRequestMessage request_message; 
    354     request_message.ParseFromString(request); 
    355     connection_data con = parse_header(*header); 
    356  
    357     for (int i=0;i<request_message.payload_size();i++) { 
    358       std::string command = get_command(request_message.payload(i).command()); 
    359       std::string data = command; 
    360       for (int a=0;a<request_message.payload(i).arguments_size();a++) { 
    361         data += "!" + request_message.payload(i).arguments(a); 
    362       } 
    363       boost::tuple<int,std::wstring> ret = instance->send(con, data); 
    364       nscapi::functions::create_simple_exec_response(utf8::cvt<std::wstring>(command), ret.get<0>(), ret.get<1>(), reply); 
    365     } 
    366     return NSCAPI::isSuccess; 
    367   } catch (std::exception &e) { 
    368     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 
    369     nscapi::functions::create_simple_exec_response(_T("command"), NSCAPI::returnUNKNOWN, _T("Exception: ") + utf8::to_unicode(e.what()), reply); 
    370     return NSCAPI::hasFailed; 
    371   } 
     316  Plugin::ExecuteRequestMessage request_message; 
     317  request_message.ParseFromString(request); 
     318  connection_data con = parse_header(*header); 
     319 
     320  Plugin::ExecuteResponseMessage response_message; 
     321  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     322 
     323  for (int i=0;i<request_message.payload_size();i++) { 
     324    std::string command = get_command(request_message.payload(i).command()); 
     325    std::string data = command; 
     326    for (int a=0;a<request_message.payload(i).arguments_size();a++) 
     327      data += "!" + request_message.payload(i).arguments(a); 
     328    boost::tuple<int,std::wstring> ret = instance->send(con, data); 
     329    nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), command, ret.get<0>(), utf8::cvt<std::string>(ret.get<1>())); 
     330  } 
     331  response_message.SerializeToString(&reply); 
     332  return NSCAPI::isSuccess; 
    372333} 
    373334 
  • modules/NRPEClient/NRPEClient.h

    r96c1461 rf33c12f  
    3434namespace po = boost::program_options; 
    3535 
    36 class NRPEClient : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 
     36class NRPEClient : public nscapi::impl::simple_plugin { 
    3737private: 
    3838 
     
    145145  bool hasNotificationHandler() { return true; }; 
    146146  NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 
    147   NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 
    148   int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 
     147  NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 
     148  NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 
    149149 
    150150private: 
  • modules/NSCAClient/NSCAClient.cpp

    r9853bc3 rf33c12f  
    222222} 
    223223 
    224 NSCAPI::nagiosReturn NSCAAgent::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) { 
    225   std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca")); 
    226  
     224NSCAPI::nagiosReturn NSCAAgent::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
     225  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
     226  std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 
    227227  client::configuration config; 
    228228  setup(config); 
    229   if (cmd == _T("query")) 
    230     return client::command_line_parser::query(config, cmd, arguments, message, perf); 
    231   if (cmd == _T("submit")) { 
    232     boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 
    233     message = result.get<1>(); 
    234     return result.get<0>(); 
    235   } 
    236   if (cmd == _T("exec")) { 
    237     return client::command_line_parser::exec(config, cmd, arguments, message); 
    238   } 
    239   return commands.exec_simple(config, target, command, arguments, message, perf); 
    240 } 
    241  
    242 int NSCAAgent::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 
    243   std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca")); 
     229  if (!client::command_line_parser::is_command(cmd)) 
     230    return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 
     231  return commands.exec_simple(config, data.target, char_command, data.args, result); 
     232} 
     233 
     234NSCAPI::nagiosReturn NSCAAgent::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
     235  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
     236  std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
    244237  if (!client::command_line_parser::is_command(cmd)) 
    245238    return NSCAPI::returnIgnored; 
    246  
    247239  client::configuration config; 
    248240  setup(config); 
    249   return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 
    250 } 
    251  
    252 NSCAPI::nagiosReturn NSCAAgent::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 
    253   try { 
    254     client::configuration config; 
    255     setup(config); 
    256  
    257     if (!client::command_line_parser::relay_submit(config, request, response)) { 
    258       NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 
    259       return NSCAPI::hasFailed; 
    260     } 
    261     return NSCAPI::isSuccess; 
    262   } catch (nsca::nsca_encrypt::encryption_exception &e) { 
    263     NSC_LOG_ERROR_STD(_T("Failed to encrypt data: ") + utf8::to_unicode(e.what())); 
    264     return NSCAPI::hasFailed; 
    265   } catch (std::exception &e) { 
    266     NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::to_unicode(e.what())); 
    267     return NSCAPI::hasFailed; 
    268   } catch (...) { 
    269     NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 
    270     return NSCAPI::hasFailed; 
    271   } 
     241  return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     242} 
     243 
     244NSCAPI::nagiosReturn NSCAAgent::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
     245  client::configuration config; 
     246  setup(config); 
     247  return client::command_line_parser::do_relay_submit(config, request, result); 
    272248} 
    273249 
     
    337313 
    338314int NSCAAgent::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    339   NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 
    340   try { 
    341     Plugin::QueryRequestMessage request_message; 
    342     request_message.ParseFromString(request); 
    343     connection_data con = parse_header(*header); 
    344  
    345     std::list<nsca::packet> list; 
    346     for (int i=0;i < request_message.payload_size(); ++i) { 
    347       nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 
    348       nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(request_message.payload(i)); 
    349       packet.code = 0; 
    350       packet.result = utf8::cvt<std::string>(data.command); 
    351       list.push_back(packet); 
    352     } 
    353  
    354     boost::tuple<int,std::wstring> ret = instance->send(con, list); 
    355     nscapi::functions::create_simple_query_response(_T("UNKNOWN"), ret.get<0>(), ret.get<1>(), _T(""), reply); 
    356     return NSCAPI::isSuccess; 
    357   } catch (std::exception &e) { 
    358     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 
    359     nscapi::functions::create_simple_query_response(_T("command"), NSCAPI::returnUNKNOWN, _T("Exception: ") + utf8::to_unicode(e.what()), _T(""), reply); 
    360     return NSCAPI::returnUNKNOWN; 
    361   } 
     315  Plugin::QueryRequestMessage request_message; 
     316  request_message.ParseFromString(request); 
     317  connection_data con = parse_header(*header); 
     318 
     319  Plugin::QueryResponseMessage response_message; 
     320  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     321 
     322  std::list<nsca::packet> list; 
     323  for (int i=0;i < request_message.payload_size(); ++i) { 
     324    nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 
     325    nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(request_message.payload(i)); 
     326    packet.code = 0; 
     327    packet.result = utf8::cvt<std::string>(data.command); 
     328    list.push_back(packet); 
     329  } 
     330 
     331  boost::tuple<int,std::wstring> ret = instance->send(con, list); 
     332 
     333  nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "TODO", ret.get<0>(), utf8::cvt<std::string>(ret.get<1>()), ""); 
     334  response_message.SerializeToString(&reply); 
     335  return NSCAPI::isSuccess; 
    362336} 
    363337 
    364338int NSCAAgent::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    365   std::wstring channel; 
    366   try { 
    367     Plugin::SubmitRequestMessage message; 
    368     message.ParseFromString(request); 
    369  
    370     connection_data con = parse_header(*header); 
    371     channel = utf8::cvt<std::wstring>(message.channel()); 
    372  
    373     std::list<nsca::packet> list; 
    374  
    375     for (int i=0;i < message.payload_size(); ++i) { 
    376       nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 
    377       std::wstring alias, msg; 
    378       packet.code = nscapi::functions::parse_simple_submit_request_payload(message.payload(i), alias, msg); 
    379       if (alias != _T("host_check")) 
    380         packet.service = utf8::cvt<std::string>(alias); 
    381       packet.result = utf8::cvt<std::string>(msg); 
    382       list.push_back(packet); 
    383     } 
    384  
    385     boost::tuple<int,std::wstring> ret = instance->send(con, list); 
    386     nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), ret.get<0>(), ret.get<1>(), reply); 
    387     return NSCAPI::isSuccess; 
    388   } catch (std::exception &e) { 
    389     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 
    390     nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), NSCAPI::returnUNKNOWN, utf8::to_unicode(e.what()), reply); 
    391     return NSCAPI::hasFailed; 
    392   } 
     339  Plugin::SubmitRequestMessage message; 
     340  message.ParseFromString(request); 
     341  connection_data con = parse_header(*header); 
     342  std::wstring channel = utf8::cvt<std::wstring>(message.channel()); 
     343 
     344  Plugin::SubmitResponseMessage response_message; 
     345  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     346 
     347  std::list<nsca::packet> list; 
     348 
     349  for (int i=0;i < message.payload_size(); ++i) { 
     350    nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 
     351    std::wstring alias, msg; 
     352    packet.code = nscapi::functions::parse_simple_submit_request_payload(message.payload(i), alias, msg); 
     353    if (alias != _T("host_check")) 
     354      packet.service = utf8::cvt<std::string>(alias); 
     355    packet.result = utf8::cvt<std::string>(msg); 
     356    list.push_back(packet); 
     357  } 
     358 
     359  boost::tuple<int,std::wstring> ret = instance->send(con, list); 
     360  nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "TODO", ret.get<0>(), utf8::cvt<std::string>(ret.get<1>())); 
     361  response_message.SerializeToString(&reply); 
     362  return NSCAPI::isSuccess; 
    393363} 
    394364 
    395365int NSCAAgent::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    396   NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 
    397   try { 
    398     Plugin::ExecuteRequestMessage request_message; 
    399     request_message.ParseFromString(request); 
    400     connection_data con = parse_header(*header); 
    401  
    402     std::list<nsca::packet> list; 
    403     for (int i=0;i < request_message.payload_size(); ++i) { 
    404       nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 
    405       nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request_payload(request_message.payload(i)); 
    406       packet.code = 0; 
    407       if (data.command != _T("host_check")) 
    408         packet.service = utf8::cvt<std::string>(data.command); 
    409       //packet.result = data.; 
    410       list.push_back(packet); 
    411     } 
    412     boost::tuple<int,std::wstring> ret = instance->send(con, list); 
    413     nscapi::functions::create_simple_exec_response(_T("UNKNOWN"), ret.get<0>(), ret.get<1>(), reply); 
    414     return NSCAPI::isSuccess; 
    415   } catch (std::exception &e) { 
    416     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 
    417     nscapi::functions::create_simple_exec_response(_T("command"), NSCAPI::returnUNKNOWN, _T("Exception: ") + utf8::to_unicode(e.what()), reply); 
    418     return NSCAPI::hasFailed; 
    419   } 
     366  Plugin::ExecuteRequestMessage request_message; 
     367  request_message.ParseFromString(request); 
     368  connection_data con = parse_header(*header); 
     369 
     370  Plugin::ExecuteResponseMessage response_message; 
     371  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     372 
     373  std::list<nsca::packet> list; 
     374  for (int i=0;i < request_message.payload_size(); ++i) { 
     375    nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 
     376    nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request_payload(request_message.payload(i)); 
     377    packet.code = 0; 
     378    if (data.command != _T("host_check")) 
     379      packet.service = utf8::cvt<std::string>(data.command); 
     380    //packet.result = data.; 
     381    list.push_back(packet); 
     382  } 
     383  boost::tuple<int,std::wstring> ret = instance->send(con, list); 
     384  nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), "TODO", ret.get<0>(), utf8::cvt<std::string>(ret.get<1>())); 
     385  response_message.SerializeToString(&reply); 
     386  return NSCAPI::isSuccess; 
    420387} 
    421388 
    422389////////////////////////////////////////////////////////////////////////// 
    423390// Protocol implementations 
     391// 
    424392 
    425393boost::tuple<int,std::wstring> NSCAAgent::send(connection_data data, const std::list<nsca::packet> packets) { 
     
    438406      socket.send_nsca(packet, boost::posix_time::seconds(data.timeout)); 
    439407    } 
    440     return NSCAPI::isSuccess; 
     408    return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("")); 
    441409  } catch (nsca::nsca_encrypt::encryption_exception &e) { 
    442410    NSC_LOG_ERROR_STD(_T("NSCA Error: ") + utf8::to_unicode(e.what())); 
  • modules/NSCAClient/NSCAClient.h

    r9853bc3 rf33c12f  
    3434namespace po = boost::program_options; 
    3535 
    36 class NSCAAgent : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 
     36class NSCAAgent : public nscapi::impl::simple_plugin { 
    3737private: 
    3838 
     
    148148  bool hasNotificationHandler() { return true; }; 
    149149  NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 
    150   NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 
    151   int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 
     150  NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 
     151  NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 
    152152 
    153153private: 
  • modules/NSCPClient/NSCPClient.cpp

    r9853bc3 rf33c12f  
    7272 
    7373    settings.alias().add_path_to_settings() 
     74      (_T("NSCP CLIENT SECTION"), _T("Section for NSCP active/passive check module.")) 
    7475 
    7576      (_T("handlers"), sh::fun_values_path(boost::bind(&NSCPClient::add_command, this, _1, _2)),  
     
    192193} 
    193194 
    194 NSCAPI::nagiosReturn NSCPClient::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) { 
    195   std::wstring cmd = client::command_line_parser::parse_command(command, _T("nscp")); 
    196  
     195NSCAPI::nagiosReturn NSCPClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
     196  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
     197  std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 
    197198  client::configuration config; 
    198199  setup(config); 
    199   if (cmd == _T("query")) 
    200     return client::command_line_parser::query(config, cmd, arguments, message, perf); 
    201   if (cmd == _T("submit")) { 
    202     boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 
    203     message = result.get<1>(); 
    204     return result.get<0>(); 
    205   } 
    206   if (cmd == _T("exec")) { 
    207     return client::command_line_parser::exec(config, cmd, arguments, message); 
    208   } 
    209   return commands.exec_simple(config, target, command, arguments, message, perf); 
    210 } 
    211  
    212 int NSCPClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 
    213   std::wstring cmd = client::command_line_parser::parse_command(command, _T("nscp")); 
     200  if (!client::command_line_parser::is_command(cmd)) 
     201    return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 
     202  return commands.exec_simple(config, data.target, char_command, data.args, result); 
     203} 
     204 
     205NSCAPI::nagiosReturn NSCPClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
     206  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
     207  std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
    214208  if (!client::command_line_parser::is_command(cmd)) 
    215209    return NSCAPI::returnIgnored; 
    216  
    217210  client::configuration config; 
    218211  setup(config); 
    219   return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 
    220 } 
    221  
    222 NSCAPI::nagiosReturn NSCPClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 
    223   try { 
    224     client::configuration config; 
    225     setup(config); 
    226  
    227     if (!client::command_line_parser::relay_submit(config, request, response)) { 
    228       NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 
    229       return NSCAPI::hasFailed; 
    230     } 
    231     return NSCAPI::isSuccess; 
    232   } catch (std::exception &e) { 
    233     NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::to_unicode(e.what())); 
    234     return NSCAPI::hasFailed; 
    235   } catch (...) { 
    236     NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 
    237     return NSCAPI::hasFailed; 
    238   } 
     212  return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     213} 
     214 
     215NSCAPI::nagiosReturn NSCPClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
     216  client::configuration config; 
     217  setup(config); 
     218  return client::command_line_parser::do_relay_submit(config, request, result); 
    239219} 
    240220 
     
    295275// 
    296276 
     277std::string gather_and_log_errors(std::string  &payload) { 
     278  NSCPIPC::ErrorMessage message; 
     279  message.ParseFromString(payload); 
     280  std::string ret; 
     281  for (int i=0;i<message.error_size();i++) { 
     282    ret += message.error(i).message(); 
     283    NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 
     284  } 
     285  return ret; 
     286} 
    297287int NSCPClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    298   NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 
    299   try { 
    300  
    301     Plugin::QueryRequestMessage request_message; 
    302     request_message.ParseFromString(request); 
    303     connection_data con = parse_header(*header); 
    304  
    305     // TODO: Humm, this cant be right?!?! 
    306  
    307     std::list<nscp::packet> chunks; 
    308     chunks.push_back(nscp::factory::create_envelope_request(1)); 
    309     chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request, 0)); 
    310     chunks = instance->send(con, chunks); 
    311     BOOST_FOREACH(nscp::packet &chunk, chunks) { 
    312       if (nscp::checks::is_query_response(chunk)) { 
    313         reply = chunk.payload; 
    314       } else if (nscp::checks::is_error(chunk)) { 
    315         NSCPIPC::ErrorMessage message; 
    316         message.ParseFromString(chunk.payload); 
    317         for (int i=0;i<message.error_size();i++) { 
    318           NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 
    319         } 
    320         ret = NSCAPI::returnUNKNOWN; 
    321       } else { 
    322         NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 
    323         ret = NSCAPI::returnUNKNOWN; 
    324       } 
    325     } 
    326     return ret; 
    327   } catch (std::exception &e) { 
    328     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    329     return NSCAPI::returnUNKNOWN; 
    330   } 
    331 } 
     288  int ret = NSCAPI::returnUNKNOWN; 
     289  Plugin::QueryRequestMessage request_message; 
     290  request_message.ParseFromString(request); 
     291  connection_data con = parse_header(*header); 
     292 
     293  Plugin::QueryResponseMessage response_message; 
     294  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     295 
     296  std::list<nscp::packet> chunks; 
     297  chunks.push_back(nscp::factory::create_envelope_request(1)); 
     298  chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request, 0)); 
     299  chunks = instance->send(con, chunks); 
     300  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
     301    if (nscp::checks::is_query_response(chunk)) { 
     302      nscapi::functions::append_response_payloads(response_message, chunk.payload); 
     303    } else if (nscp::checks::is_error(chunk)) { 
     304      std::string error = gather_and_log_errors(chunk.payload); 
     305      nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 
     306      ret = NSCAPI::returnUNKNOWN; 
     307    } else { 
     308      NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 
     309      nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 
     310      ret = NSCAPI::returnUNKNOWN; 
     311    } 
     312  } 
     313  response_message.SerializeToString(&reply); 
     314  return ret; 
     315} 
     316 
    332317int NSCPClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    333   std::list<std::string> result; 
    334   try { 
    335  
    336     Plugin::QueryRequestMessage request_message; 
    337     request_message.ParseFromString(request); 
    338     connection_data con = parse_header(*header); 
    339  
    340     // TODO: Humm, this cant be right?!?! 
    341  
    342     std::list<nscp::packet> chunks; 
    343     chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request, 0)); 
    344     chunks = instance->send(con, chunks); 
    345     BOOST_FOREACH(nscp::packet &chunk, chunks) { 
    346       if (nscp::checks::is_query_response(chunk)) { 
    347         result.push_back(chunk.payload); 
    348       } else if (nscp::checks::is_error(chunk)) { 
    349         NSCPIPC::ErrorMessage message; 
    350         message.ParseFromString(chunk.payload); 
    351         for (int i=0;i<message.error_size();i++) { 
    352           result.push_back("Error: " + message.error(i).message()); 
    353         } 
    354       } else { 
    355         NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 
    356         result.push_back("Invalid payload"); 
    357       } 
    358     } 
    359  
    360     if (result.empty()) { 
    361       std::wstring msg; 
    362       BOOST_FOREACH(std::string &e, result) { 
    363         msg += utf8::cvt<std::wstring>(e); 
    364       } 
    365       nscapi::functions::create_simple_submit_response(_T(""), _T(""), result.empty()?NSCAPI::isSuccess:NSCAPI::hasFailed, msg, reply); 
    366     } 
    367     return result.empty()?NSCAPI::isSuccess:NSCAPI::hasFailed; 
    368   } catch (std::exception &e) { 
    369     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    370     nscapi::functions::create_simple_submit_response(_T(""), _T(""), NSCAPI::hasFailed, utf8::cvt<std::wstring>(e.what()), reply); 
    371     return NSCAPI::hasFailed; 
    372   } 
    373 } 
     318  int ret = NSCAPI::returnUNKNOWN; 
     319  Plugin::SubmitRequestMessage request_message; 
     320  request_message.ParseFromString(request); 
     321  connection_data con = parse_header(*header); 
     322  Plugin::SubmitResponseMessage response_message; 
     323  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     324 
     325  std::list<nscp::packet> chunks; 
     326  chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request, 0)); 
     327  chunks = instance->send(con, chunks); 
     328  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
     329    if (nscp::checks::is_submit_response(chunk)) { 
     330      nscapi::functions::append_response_payloads(response_message, chunk.payload); 
     331    } else if (nscp::checks::is_error(chunk)) { 
     332      std::string error = gather_and_log_errors(chunk.payload); 
     333      nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 
     334      ret = NSCAPI::returnUNKNOWN; 
     335    } else { 
     336      NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 
     337      nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 
     338      ret = NSCAPI::returnUNKNOWN; 
     339    } 
     340  } 
     341  response_message.SerializeToString(&reply); 
     342  return ret; 
     343} 
     344 
    374345int NSCPClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    375346  int ret = NSCAPI::returnOK; 
    376   try { 
    377  
    378  
    379     Plugin::QueryRequestMessage request_message; 
    380     request_message.ParseFromString(request); 
    381     connection_data con = parse_header(*header); 
    382  
    383     // TODO: Humm, this cant be right?!?! 
    384  
    385     std::list<nscp::packet> chunks; 
    386     chunks.push_back(nscp::factory::create_envelope_request(1)); 
    387     chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request, 0)); 
    388     chunks = instance->send(con, chunks); 
    389     BOOST_FOREACH(nscp::packet &chunk, chunks) { 
    390       if (nscp::checks::is_exec_response(chunk)) { 
    391         reply = chunk.payload; 
    392       } else if (nscp::checks::is_error(chunk)) { 
    393         NSCPIPC::ErrorMessage message; 
    394         message.ParseFromString(chunk.payload); 
    395         for (int i=0;i<message.error_size();i++) { 
    396           NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 
    397           ret = NSCAPI::returnUNKNOWN; 
    398         } 
    399       } else { 
    400         NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 
    401         ret = NSCAPI::returnUNKNOWN; 
    402       } 
    403     } 
    404     return ret; 
    405   } catch (std::exception &e) { 
    406     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    407     return NSCAPI::returnUNKNOWN; 
    408   } 
     347  Plugin::ExecuteRequestMessage request_message; 
     348  request_message.ParseFromString(request); 
     349  connection_data con = parse_header(*header); 
     350 
     351  Plugin::ExecuteResponseMessage response_message; 
     352  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     353 
     354  std::list<nscp::packet> chunks; 
     355  chunks.push_back(nscp::factory::create_envelope_request(1)); 
     356  chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request, 0)); 
     357  chunks = instance->send(con, chunks); 
     358  BOOST_FOREACH(nscp::packet &chunk, chunks) { 
     359    if (nscp::checks::is_exec_response(chunk)) { 
     360      nscapi::functions::append_response_payloads(response_message, chunk.payload); 
     361    } else if (nscp::checks::is_error(chunk)) { 
     362      std::string error = gather_and_log_errors(chunk.payload); 
     363      nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 
     364      ret = NSCAPI::returnUNKNOWN; 
     365    } else { 
     366      NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 
     367      nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 
     368      ret = NSCAPI::returnUNKNOWN; 
     369    } 
     370  } 
     371  response_message.SerializeToString(&reply); 
     372  return ret; 
    409373} 
    410374 
  • modules/NSCPClient/NSCPClient.h

    r96c1461 rf33c12f  
    3535 
    3636 
    37 class NSCPClient : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 
     37class NSCPClient : public nscapi::impl::simple_plugin { 
    3838private: 
    3939 
     
    142142  bool hasNotificationHandler() { return true; }; 
    143143  NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 
    144   NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 
    145   int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 
     144  NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 
     145  NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 
    146146 
    147147private: 
  • modules/SMTPClient/SMTPClient.cpp

    r96c1461 rf33c12f  
    1919*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * 
    2020***************************************************************************/ 
    21  
    2221#include "stdafx.h" 
     22#include "SMTPClient.h" 
    2323 
    2424#include <utils.h> 
     
    2828#include <strEx.h> 
    2929 
    30 #include "SMTPClient.h" 
    3130 
    3231#include <settings/client/settings_client.hpp> 
     
    3534 
    3635namespace sh = nscapi::settings_helper; 
    37 namespace str = nscp::helpers; 
    38 namespace po = boost::program_options; 
    3936 
    4037/** 
     
    4340 */ 
    4441SMTPClient::SMTPClient() {} 
     42 
    4543/** 
    4644 * Default d-tor 
     
    4846 */ 
    4947SMTPClient::~SMTPClient() {} 
     48 
    5049/** 
    5150 * Load (initiate) module. 
     
    5655  return false; 
    5756} 
     57 
    5858bool SMTPClient::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
    5959 
    60  
     60  std::wstring template_string, sender, recipient; 
     61  unsigned int timeout; 
    6162  try { 
    6263    sh::settings_registry settings(get_settings_proxy()); 
     
    6667    settings.alias().add_path_to_settings() 
    6768      (_T("SMTP CLIENT SECTION"), _T("Section for SMTP passive check module.")) 
     69      (_T("handlers"), sh::fun_values_path(boost::bind(&SMTPClient::add_command, this, _1, _2)),  
     70      _T("CLIENT HANDLER SECTION"), _T("")) 
    6871 
    6972      (_T("targets"), sh::fun_values_path(boost::bind(&SMTPClient::add_target, this, _1, _2)),  
    7073      _T("REMOTE TARGET DEFINITIONS"), _T("")) 
    71  
    7274      ; 
    7375 
    7476    settings.alias().add_key_to_settings() 
    75       (_T("hostname"), sh::string_key(&hostname_), 
    76       _T("HOSTNAME"), _T("The host name of this host if set to blank (default) the windows name of the computer will be used.")) 
    77  
    7877      (_T("channel"), sh::wstring_key(&channel_, _T("SMTP")), 
    7978      _T("CHANNEL"), _T("The channel to listen to.")) 
     
    8180      ; 
    8281 
     82    settings.alias().add_key_to_settings(_T("targets/default")) 
     83 
     84      (_T("timeout"), sh::uint_key(&timeout, 30), 
     85      _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets.")) 
     86 
     87      (_T("sender"), sh::wpath_key(&sender, _T("nscp@localhost")), 
     88      _T("SENDER"), _T("Sender of email message")) 
     89 
     90      (_T("recipient"), sh::wpath_key(&recipient, _T("nscp@localhost")), 
     91      _T("RECIPIENT"), _T("Recipient of email message")) 
     92 
     93      (_T("template"), sh::wpath_key(&template_string, _T("Hello, this is %source% reporting %message%!")), 
     94      _T("TEMPLATE"), _T("Template for message data")) 
     95 
     96      ; 
     97 
    8398    settings.register_all(); 
    8499    settings.notify(); 
     
    86101    get_core()->registerSubmissionListener(get_id(), channel_); 
    87102 
     103    if (!targets.has_target(_T("default"))) { 
     104      add_target(_T("default"), _T("default")); 
     105      targets.rebuild(); 
     106    } 
     107    nscapi::target_handler::optarget t = targets.find_target(_T("default")); 
     108    if (t) { 
     109      if (!t->has_option("template")) 
     110        t->options[_T("template")] = template_string; 
     111      if (!t->has_option("timeout")) 
     112        t->options[_T("timeout")] = strEx::itos(timeout); 
     113      if (!t->has_option("recipient")) 
     114        t->options[_T("recipient")] = recipient; 
     115      if (!t->has_option("sender")) 
     116        t->options[_T("sender")] = sender; 
     117      targets.add(*t); 
     118    } else { 
     119      NSC_LOG_ERROR(_T("Default target not found!")); 
     120    } 
     121 
    88122  } catch (nscapi::nscapi_exception &e) { 
    89     NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); 
     123    NSC_LOG_ERROR_STD(_T("NSClient API exception: ") + utf8::to_unicode(e.what())); 
    90124    return false; 
    91125  } catch (std::exception &e) { 
    92     NSC_LOG_ERROR_STD(_T("Exception caught: ") + utf8::cvt<std::wstring>(e.what())); 
     126    NSC_LOG_ERROR_STD(_T("Exception caught: ") + utf8::to_unicode(e.what())); 
    93127    return false; 
    94128  } catch (...) { 
    95     NSC_LOG_ERROR_STD(_T("Failed to register command.")); 
     129    NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN EXCEPTION>")); 
    96130    return false; 
    97131  } 
    98132  return true; 
    99133} 
     134std::string get_command(std::string alias, std::string command = "") { 
     135  if (!alias.empty()) 
     136    return alias;  
     137  if (!command.empty()) 
     138    return command;  
     139  return "TODO"; 
     140} 
     141 
     142////////////////////////////////////////////////////////////////////////// 
     143// Settings helpers 
     144// 
     145 
     146void SMTPClient::add_target(std::wstring key, std::wstring arg) { 
     147  try { 
     148    targets.add(get_settings_proxy(), target_path , key, arg); 
     149  } catch (...) { 
     150    NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 
     151  } 
     152} 
     153 
     154void SMTPClient::add_command(std::wstring name, std::wstring args) { 
     155  try { 
     156    std::wstring key = commands.add_command(name, args); 
     157    if (!key.empty()) 
     158      register_command(key.c_str(), _T("NRPE relay for: ") + name); 
     159  } catch (boost::program_options::validation_error &e) { 
     160    NSC_LOG_ERROR_STD(_T("Could not add command ") + name + _T(": ") + utf8::to_unicode(e.what())); 
     161  } catch (...) { 
     162    NSC_LOG_ERROR_STD(_T("Could not add command ") + name); 
     163  } 
     164} 
     165 
    100166/** 
    101167 * Unload (terminate) module. 
     
    107173} 
    108174 
    109 void SMTPClient::add_target(std::wstring key, std::wstring arg) { 
    110   try { 
    111     targets.add(get_settings_proxy(), target_path , key, arg); 
    112   } catch (...) { 
    113     NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 
    114   } 
    115 } 
    116 void SMTPClient::add_local_options(po::options_description &desc, connection_data &command_data) { 
    117   /* 
    118   desc.add_options() 
    119     ("payload-length,l", po::value<unsigned int>(&command_data.payload_length)->zero_tokens()->default_value(512), "The payload length to use in the NSCA packet.") 
    120     ("encryption,e", po::value<std::string>(&command_data.encryption)->default_value("ASE"), "Encryption algorithm to use.") 
    121     ; 
    122     */ 
    123 } 
    124  
    125 void SMTPClient::setup(client::configuration &config) { 
    126   clp_handler_impl *handler = new clp_handler_impl(this); 
    127   add_local_options(config.local, handler->local_data); 
    128   config.handler = client::configuration::handler_type(handler); 
    129 } 
    130  
    131 NSCAPI::nagiosReturn SMTPClient::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) { 
    132   std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca")); 
     175 
     176 
     177NSCAPI::nagiosReturn SMTPClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
     178  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
     179  std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 
     180  client::configuration config; 
     181  setup(config); 
     182  if (!client::command_line_parser::is_command(cmd)) 
     183    return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 
     184  return commands.exec_simple(config, data.target, char_command, data.args, result); 
     185} 
     186 
     187NSCAPI::nagiosReturn SMTPClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
     188  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
     189  std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
    133190  if (!client::command_line_parser::is_command(cmd)) 
    134191    return NSCAPI::returnIgnored; 
    135  
    136192  client::configuration config; 
    137193  setup(config); 
    138   boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 
    139   message = result.get<1>(); 
    140   return result.get<0>(); 
    141 } 
    142  
    143  
    144 int SMTPClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 
    145   std::wstring cmd = client::command_line_parser::parse_command(command, _T("smtp")); 
    146   if (!client::command_line_parser::is_command(cmd)) 
    147     return NSCAPI::returnIgnored; 
    148  
     194  return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     195} 
     196 
     197NSCAPI::nagiosReturn SMTPClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
    149198  client::configuration config; 
    150199  setup(config); 
    151   return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 
    152 } 
    153  
    154  
    155 NSCAPI::nagiosReturn SMTPClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 
    156   try { 
    157     client::configuration config; 
    158     net::wurl url; 
    159     url.protocol = _T("smtp"); 
    160     nscapi::target_handler::optarget target = targets.find_target(_T("default")); 
    161     if (target) { 
    162       url.host = target->host; 
    163       url.port = strEx::stoi(target->options[_T("port")]); 
    164       config.data->recipient.data["template"] = utf8::cvt<std::string>(target->options[_T("template")]); 
    165       config.data->recipient.data["recipient"] = utf8::cvt<std::string>(target->options[_T("recipient")]); 
    166       config.data->recipient.data["sender"] = utf8::cvt<std::string>(target->options[_T("sender")]); 
     200  return client::command_line_parser::do_relay_submit(config, request, result); 
     201} 
     202 
     203////////////////////////////////////////////////////////////////////////// 
     204// Parser setup/Helpers 
     205// 
     206 
     207void SMTPClient::add_local_options(po::options_description &desc, client::configuration::data_type data) { 
     208 
     209  desc.add_options() 
     210    ("sender", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "sender", _1)),  
     211      "Length of payload (has to be same as on the server)") 
     212 
     213    ("recipient", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "recipient", _1)),  
     214      "Length of payload (has to be same as on the server)") 
     215 
     216    ("template", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "template", _1)),  
     217    "Do not initial an ssl handshake with the server, talk in plaintext.") 
     218    ; 
     219} 
     220 
     221void SMTPClient::setup(client::configuration &config) { 
     222  boost::shared_ptr<clp_handler_impl> handler = boost::shared_ptr<clp_handler_impl>(new clp_handler_impl(this)); 
     223  add_local_options(config.local, config.data); 
     224 
     225  net::wurl url; 
     226  url.protocol = _T("smtp"); 
     227  url.port = 25; 
     228  nscapi::target_handler::optarget opt = targets.find_target(_T("default")); 
     229  if (opt) { 
     230    nscapi::target_handler::target t = *opt; 
     231    url.host = t.host; 
     232    if (t.has_option("port")) { 
     233      try { 
     234        url.port = strEx::stoi(t.options[_T("port")]); 
     235      } catch (...) {} 
    167236    } 
    168     config.data->recipient.id = "default"; 
    169     config.data->recipient.address = utf8::cvt<std::string>(url.to_string()); 
    170     config.data->host_self.id = "self"; 
    171     config.data->host_self.host = hostname_; 
    172  
    173     setup(config); 
    174     if (!client::command_line_parser::relay_submit(config, request, response)) { 
    175       NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 
    176       return NSCAPI::hasFailed; 
     237    std::string keys[] = {"sender", "timeout", "recipient", "template"}; 
     238    BOOST_FOREACH(std::string s, keys) { 
     239      config.data->recipient.data[s] = utf8::cvt<std::string>(t.options[utf8::cvt<std::wstring>(s)]); 
    177240    } 
    178     return NSCAPI::isSuccess; 
    179   } catch (std::exception &e) { 
    180     NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::cvt<std::wstring>(e.what())); 
    181     return NSCAPI::hasFailed; 
    182   } catch (...) { 
    183     NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 
    184     return NSCAPI::hasFailed; 
    185   } 
     241  } 
     242  config.data->recipient.id = "default"; 
     243  config.data->recipient.address = utf8::cvt<std::string>(url.to_string()); 
     244  config.data->host_self.id = "self"; 
     245  //config.data->host_self.host = hostname_; 
     246 
     247  config.target_lookup = handler; 
     248  config.handler = handler; 
     249} 
     250 
     251SMTPClient::connection_data SMTPClient::parse_header(const ::Plugin::Common_Header &header) { 
     252  nscapi::functions::destination_container recipient; 
     253  nscapi::functions::parse_destination(header, header.recipient_id(), recipient, true); 
     254  return connection_data(recipient); 
    186255} 
    187256 
     
    192261  return NSCAPI::hasFailed; 
    193262} 
     263 
     264int SMTPClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
     265  Plugin::SubmitRequestMessage request_message; 
     266  request_message.ParseFromString(request); 
     267  connection_data con = parse_header(*header); 
     268  std::wstring channel = utf8::cvt<std::wstring>(request_message.channel()); 
     269 
     270  Plugin::SubmitResponseMessage response_message; 
     271  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     272 
     273  for (int i=0;i < request_message.payload_size(); ++i) { 
     274    const ::Plugin::QueryResponseMessage::Response& payload = request_message.payload(i); 
     275    boost::asio::io_service io_service; 
     276    boost::shared_ptr<smtp::client::smtp_client> client(new smtp::client::smtp_client(io_service)); 
     277    std::list<std::string> recipients; 
     278    std::string message = con.template_string; 
     279    strEx::replace(message, "%message%", payload.message()); 
     280    recipients.push_back(con.recipient_str); 
     281    client->send_mail(con.sender, recipients, "Hello world\n"); 
     282    io_service.run(); 
     283    nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "TODO", NSCAPI::returnOK, "Message send successfully"); 
     284  } 
     285 
     286  response_message.SerializeToString(&reply); 
     287  return NSCAPI::isSuccess; 
     288 
     289} 
     290 
    194291int SMTPClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    195292  NSC_LOG_ERROR_STD(_T("SMTP does not support exec patterns")); 
     
    197294} 
    198295 
    199 int SMTPClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    200   try { 
    201  
    202     Plugin::SubmitRequestMessage message; 
    203     message.ParseFromString(request); 
    204  
    205     std::wstring alias, command, msg, perf; 
    206     nscapi::functions::destination_container recipient; // = data->host_default_recipient; 
    207     nscapi::functions::parse_destination(*header, header->recipient_id(), recipient, true); 
    208     nscapi::functions::destination_container source; 
    209     nscapi::functions::parse_destination(*header, header->source_id(), source); 
    210  
    211     for (int i=0;i < message.payload_size(); ++i) { 
    212       boost::asio::io_service io_service; 
    213       boost::shared_ptr<smtp::client::smtp_client> client(new smtp::client::smtp_client(io_service)); 
    214       std::list<std::string> recipients; 
    215       recipients.push_back("michael@medin.name"); 
    216       client->send_mail("test@test.medin.name", recipients, "Hello world\n"); 
    217       io_service.run(); 
    218     } 
    219  
    220     nscapi::functions::create_simple_submit_response(_T(""), _T(""), NSCAPI::isSuccess, _T(""), reply); 
    221     return NSCAPI::isSuccess; 
    222   } catch (std::exception &e) { 
    223     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 
    224     nscapi::functions::create_simple_submit_response(_T(""), _T(""), NSCAPI::hasFailed, utf8::cvt<std::wstring>(e.what()), reply); 
    225     return NSCAPI::hasFailed; 
    226   } 
    227 } 
     296////////////////////////////////////////////////////////////////////////// 
     297// Protocol implementations 
     298// 
    228299 
    229300NSC_WRAP_DLL(); 
    230301NSC_WRAPPERS_MAIN_DEF(SMTPClient); 
    231302NSC_WRAPPERS_IGNORE_MSG_DEF(); 
    232 NSC_WRAPPERS_IGNORE_CMD_DEF(); 
     303NSC_WRAPPERS_HANDLE_CMD_DEF(); 
     304NSC_WRAPPERS_CLI_DEF(); 
    233305NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF(); 
     306 
  • modules/SMTPClient/SMTPClient.h

    r96c1461 rf33c12f  
    2323 
    2424#include <client/command_line_parser.hpp> 
    25 #include <boost/program_options.hpp> 
    26  
    2725#include <nscapi/targets.hpp> 
    2826 
    2927NSC_WRAPPERS_MAIN(); 
     28NSC_WRAPPERS_CLI(); 
    3029NSC_WRAPPERS_CHANNELS(); 
     30 
     31namespace po = boost::program_options; 
    3132 
    3233class SMTPClient : public nscapi::impl::simple_plugin { 
    3334private: 
    3435 
    35   std::string hostname_; 
    3636  std::wstring channel_; 
    37   nscapi::target_handler targets; 
    3837  std::wstring target_path; 
    3938 
    40   struct connection_data : public client::nscp_cli_data {}; 
    41   struct clp_handler_impl : public client::clp_handler { 
     39  nscapi::target_handler targets; 
     40  client::command_manager commands; 
     41 
     42  struct connection_data { 
     43    std::string recipient_str; 
     44    std::string sender; 
     45    std::string template_string; 
     46    std::string host; 
     47    std::string port; 
     48    int timeout; 
     49 
     50    connection_data(nscapi::functions::destination_container recipient) { 
     51      recipient_str = recipient.get_string_data("recipient"); 
     52      timeout = recipient.get_int_data("timeout", 30); 
     53      sender = recipient.get_string_data("sender"); 
     54      template_string = recipient.get_string_data("template"); 
     55 
     56      net::url url = recipient.get_url(25); 
     57      host = url.host; 
     58      port = url.get_port(); 
     59    } 
     60 
     61    std::wstring to_wstring() const { 
     62      std::wstringstream ss; 
     63      ss << _T("host: ") << utf8::cvt<std::wstring>(host); 
     64      ss << _T(", port: ") << utf8::cvt<std::wstring>(port); 
     65      ss << _T(", timeout: ") << timeout; 
     66      ss << _T(", recipient: ") << utf8::cvt<std::wstring>(recipient_str); 
     67      ss << _T(", sender: ") << utf8::cvt<std::wstring>(sender); 
     68      ss << _T(", template: ") << utf8::cvt<std::wstring>(template_string); 
     69      return ss.str(); 
     70    } 
     71  }; 
     72 
     73  struct clp_handler_impl : public client::clp_handler, client::target_lookup_interface { 
    4274 
    4375    SMTPClient *instance; 
    4476    clp_handler_impl(SMTPClient *instance) : instance(instance) {} 
    45     connection_data local_data; 
    4677 
    4778    int query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    4879    int submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
    4980    int exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 
     81 
     82    virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 
     83      nscapi::functions::destination_container ret; 
     84      nscapi::target_handler::optarget t = instance->targets.find_target(id); 
     85      if (t) { 
     86        if (!t->alias.empty()) 
     87          ret.id = utf8::cvt<std::string>(t->alias); 
     88        if (!t->host.empty()) 
     89          ret.host = utf8::cvt<std::string>(t->host); 
     90        if (t->has_option("address")) 
     91          ret.address = utf8::cvt<std::string>(t->options[_T("address")]); 
     92        else  
     93          ret.address = utf8::cvt<std::string>(t->host); 
     94        BOOST_FOREACH(const nscapi::target_handler::target::options_type::value_type &kvp, t->options) { 
     95          ret.data[utf8::cvt<std::string>(kvp.first)] = utf8::cvt<std::string>(kvp.second); 
     96        } 
     97      } 
     98      return ret; 
     99    } 
    50100  }; 
     101 
    51102 
    52103public: 
     
    70121  */ 
    71122  static nscapi::plugin_wrapper::module_version getModuleVersion() { 
    72     nscapi::plugin_wrapper::module_version version = {0, 3, 0 }; 
     123    nscapi::plugin_wrapper::module_version version = {0, 4, 0 }; 
    73124    return version; 
    74125  } 
     
    76127    return _T("Passive check support via SMTP"); 
    77128  } 
    78   bool hasNotificationHandler() { return true; } 
    79129 
     130  bool hasCommandHandler() { return true; }; 
     131  bool hasMessageHandler() { return true; }; 
     132  bool hasNotificationHandler() { return true; }; 
    80133  NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 
    81   NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 
    82   int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 
     134  NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 
     135  NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 
    83136 
     137private: 
     138  static connection_data parse_header(const ::Plugin::Common_Header &header); 
     139 
     140private: 
     141  void add_local_options(po::options_description &desc, client::configuration::data_type data); 
    84142  void setup(client::configuration &config); 
    85   void add_local_options(boost::program_options::options_description &desc, connection_data &command_data); 
     143  void add_command(std::wstring key, std::wstring args); 
    86144  void add_target(std::wstring key, std::wstring args); 
    87145 
    88146}; 
     147 
  • modules/Scheduler/Scheduler.cpp

    r9853bc3 rf33c12f  
    160160    if (code == NSCAPI::returnIgnored) { 
    161161      NSC_LOG_ERROR_STD(_T("Command was not found: ") + item.command.c_str()); 
     162      //make_submit_from_query(response, item.channel, item.alias); 
    162163      nscapi::functions::create_simple_submit_request(item.channel, item.command, NSCAPI::returnUNKNOWN, _T("Command was not found: ") + item.command, _T(""), response); 
    163164      std::string result; 
  • modules/SyslogClient/SyslogClient.cpp

    r9853bc3 rf33c12f  
    222222} 
    223223 
    224 NSCAPI::nagiosReturn SyslogClient::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) { 
    225   std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca")); 
    226  
     224NSCAPI::nagiosReturn SyslogClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
     225  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
     226  std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 
    227227  client::configuration config; 
    228228  setup(config); 
    229   if (cmd == _T("query")) 
    230     return client::command_line_parser::query(config, cmd, arguments, message, perf); 
    231   if (cmd == _T("submit")) { 
    232     boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 
    233     message = result.get<1>(); 
    234     return result.get<0>(); 
    235   } 
    236   if (cmd == _T("exec")) { 
    237     return client::command_line_parser::exec(config, cmd, arguments, message); 
    238   } 
    239   return commands.exec_simple(config, target, command, arguments, message, perf); 
    240 } 
    241  
    242 int SyslogClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 
    243   std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca")); 
     229  if (!client::command_line_parser::is_command(cmd)) 
     230    return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 
     231  return commands.exec_simple(config, data.target, char_command, data.args, result); 
     232} 
     233 
     234NSCAPI::nagiosReturn SyslogClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
     235  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
     236  std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
    244237  if (!client::command_line_parser::is_command(cmd)) 
    245238    return NSCAPI::returnIgnored; 
    246  
    247239  client::configuration config; 
    248240  setup(config); 
    249   return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 
    250 } 
    251  
    252 NSCAPI::nagiosReturn SyslogClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 
    253   try { 
    254     client::configuration config; 
    255     setup(config); 
    256  
    257     if (!client::command_line_parser::relay_submit(config, request, response)) { 
    258       NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 
    259       return NSCAPI::hasFailed; 
    260     } 
    261     return NSCAPI::isSuccess; 
    262   } catch (std::exception &e) { 
    263     NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::to_unicode(e.what())); 
    264     return NSCAPI::hasFailed; 
    265   } catch (...) { 
    266     NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 
    267     return NSCAPI::hasFailed; 
    268   } 
     241  return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 
     242} 
     243 
     244NSCAPI::nagiosReturn SyslogClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 
     245  client::configuration config; 
     246  setup(config); 
     247  return client::command_line_parser::do_relay_submit(config, request, result); 
    269248} 
    270249 
     
    343322int SyslogClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    344323  NSC_LOG_ERROR_STD(_T("SYSLOG does not support query patterns")); 
     324  nscapi::functions::create_simple_query_response_unknown(_T("UNKNOWN"), _T("SYSLOG does not support query patterns"), reply); 
    345325  return NSCAPI::hasFailed; 
    346326} 
    347327 
    348328int SyslogClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    349   std::wstring channel; 
    350   try { 
    351     Plugin::SubmitRequestMessage message; 
    352     message.ParseFromString(request); 
    353     connection_data con = parse_header(*header); 
    354     channel = utf8::cvt<std::wstring>(message.channel()); 
    355  
    356     //TODO: Map seveity! 
    357  
    358     std::list<std::string> messages; 
    359     for (int i=0;i < message.payload_size(); ++i) { 
    360       const ::Plugin::QueryResponseMessage::Response& payload = message.payload(i); 
    361       std::string date = "Nov 10 00:12:00"; // TODO is this actually used? 
    362       std::string tag = con.tag_syntax; 
    363       std::string message = con.message_syntax; 
    364       strEx::replace(message, "%message%", payload.message()); 
    365       strEx::replace(tag, "%message%", payload.message()); 
    366  
    367       std::string severity = con.severity; 
    368       if (payload.result() == ::Plugin::Common_ResultCode_OK) 
    369         severity = con.ok_severity; 
    370       if (payload.result() == ::Plugin::Common_ResultCode_WARNING) 
    371         severity = con.warn_severity; 
    372       if (payload.result() == ::Plugin::Common_ResultCode_CRITCAL) 
    373         severity = con.crit_severity; 
    374       if (payload.result() == ::Plugin::Common_ResultCode_UNKNOWN) 
    375         severity = con.unknown_severity; 
    376  
    377       messages.push_back(instance->parse_priority(severity, con.facility) + date + " " + tag + " " + message); 
    378     } 
    379     boost::tuple<int,std::wstring> ret = instance->send(con, messages); 
    380     nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), ret.get<0>(), _T("Message submitted successfully: ") + ret.get<1>(), reply); 
    381     return NSCAPI::isSuccess; 
    382   } catch (std::exception &e) { 
    383     NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 
    384     nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), NSCAPI::returnUNKNOWN, utf8::to_unicode(e.what()), reply); 
    385     return NSCAPI::hasFailed; 
    386   } 
     329  Plugin::SubmitRequestMessage request_message; 
     330  request_message.ParseFromString(request); 
     331  connection_data con = parse_header(*header); 
     332 
     333  Plugin::SubmitResponseMessage response_message; 
     334  nscapi::functions::make_return_header(response_message.mutable_header(), *header); 
     335 
     336  //TODO: Map seveity! 
     337 
     338  std::list<std::string> messages; 
     339  for (int i=0;i < request_message.payload_size(); ++i) { 
     340    const ::Plugin::QueryResponseMessage::Response& payload = request_message.payload(i); 
     341    std::string date = "Nov 10 00:12:00"; // TODO is this actually used? 
     342    std::string tag = con.tag_syntax; 
     343    std::string message = con.message_syntax; 
     344    strEx::replace(message, "%message%", payload.message()); 
     345    strEx::replace(tag, "%message%", payload.message()); 
     346 
     347    std::string severity = con.severity; 
     348    if (payload.result() == ::Plugin::Common_ResultCode_OK) 
     349      severity = con.ok_severity; 
     350    if (payload.result() == ::Plugin::Common_ResultCode_WARNING) 
     351      severity = con.warn_severity; 
     352    if (payload.result() == ::Plugin::Common_ResultCode_CRITCAL) 
     353      severity = con.crit_severity; 
     354    if (payload.result() == ::Plugin::Common_ResultCode_UNKNOWN) 
     355      severity = con.unknown_severity; 
     356 
     357    messages.push_back(instance->parse_priority(severity, con.facility) + date + " " + tag + " " + message); 
     358  } 
     359  boost::tuple<int,std::wstring> ret = instance->send(con, messages); 
     360  nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "UNKNOWN", ret.get<0>(), utf8::cvt<std::string>(ret.get<1>())); 
     361  response_message.SerializeToString(&reply); 
     362  return NSCAPI::isSuccess; 
    387363} 
    388364 
    389365int SyslogClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 
    390366  NSC_LOG_ERROR_STD(_T("SYSLOG does not support exec patterns")); 
     367  nscapi::functions::create_simple_exec_response_unknown("UNKNOWN", "SYSLOG does not support exec patterns", reply); 
    391368  return NSCAPI::hasFailed; 
    392369} 
  • modules/SyslogClient/SyslogClient.h

    r96c1461 rf33c12f  
    3232namespace po = boost::program_options; 
    3333 
    34 class SyslogClient : public nscapi::impl::simple_command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 
     34class SyslogClient : public nscapi::impl::simple_plugin { 
    3535private: 
    3636 
     
    144144  bool hasNotificationHandler() { return true; }; 
    145145  NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 
    146   NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 
    147   int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 
     146  NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 
     147  NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 
     148 
    148149 
    149150private: 
Note: See TracChangeset for help on using the changeset viewer.