Changeset b38e845 in nscp for modules


Ignore:
Timestamp:
08/24/11 18:37:39 (21 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
d7e265d
Parents:
0b8df3e
Message:
  • Sever refactoring of the new API (there is now two pb files ipc for NSCP protocol and plugin for plugin communication)
  • Cleaned up API helper functions
Location:
modules
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • modules/CheckExternalScripts/CheckExternalScripts.cpp

    r2c95d22 rb38e845  
    138138 
    139139NSCAPI::nagiosReturn CheckExternalScripts::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) { 
    140   nscapi::functions::decoded_simple_command_data data = nscapi::functions::process_simple_command_request(char_command, request); 
     140  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
    141141 
    142142  command_list::const_iterator cit = commands.find(data.command); 
     
    158158        if (first && !isAlias && !allowNasty_) { 
    159159          if (str.find_first_of(NASTY_METACHARS) != std::wstring::npos) { 
    160             return nscapi::functions::process_simple_command_result(data.command, NSCAPI::returnUNKNOWN, _T("Request contained illegal characters!"), _T(""), response); 
     160            return nscapi::functions::create_simple_query_response_unknown(_T("Request contained illegal characters!"), _T(""), response, data.command); 
     161 
    161162          } 
    162163        } 
     
    190191    int result = process::executeProcess(process::exec_arguments(root_, cd.command + _T(" ") + xargs, timeout), message, perf); 
    191192    if (!nscapi::plugin_helper::isNagiosReturnCode(result)) { 
    192       return nscapi::functions::process_simple_command_result(data.command, NSCAPI::returnUNKNOWN, _T("The command (") + cd.command + _T(") returned an invalid return code: ") + strEx::itos(result), _T(""), response); 
    193     } 
    194     return nscapi::functions::process_simple_command_result(data.command, nscapi::plugin_helper::int2nagios(result), message, perf, response); 
     193      nscapi::functions::create_simple_query_response(NSCAPI::returnUNKNOWN, _T("The command (") + cd.command + _T(") returned an invalid return code: ") + strEx::itos(result), _T(""), response, data.command); 
     194      return NSCAPI::returnUNKNOWN; 
     195    } 
     196    nscapi::functions::create_simple_query_response(nscapi::plugin_helper::int2nagios(result), message, perf, response, data.command); 
     197    return result; 
    195198  } 
    196199} 
  • modules/NSCPClient/NSCPClient.cpp

    r4632ff7 rb38e845  
    240240NSCPClient::nscp_result_data NSCPClient::execute_nscp_command(nscp_connection_data con, std::string buffer) { 
    241241  try { 
    242     std::list<nscp::packet::nscp_chunk> chunks; 
     242    std::list<nscp::packet> chunks; 
    243243    chunks.push_back(nscp::packet::build_envelope_request(1)); 
    244     chunks.push_back(nscp::packet::build_payload(nscp::data::command_request, buffer, 0)); 
     244    chunks.push_back(nscp::packet::create_payload(nscp::data::command_request, buffer, 0)); 
    245245    if (!con.no_ssl) { 
    246246#ifdef USE_SSL 
     
    253253      chunks = send_nossl(con.host, con.port, con.timeout, chunks); 
    254254    } 
    255     BOOST_FOREACH(nscp::packet::nscp_chunk &chunk, chunks) { 
     255    BOOST_FOREACH(nscp::packet &chunk, chunks) { 
    256256      NSC_DEBUG_MSG_STD(_T("Found chunk: ") + utf8::cvt<std::wstring>(strEx::format_buffer(chunk.payload.c_str(), chunk.payload.size()))); 
    257257    } 
    258258    return nscp_result_data(NSCAPI::returnUNKNOWN, _T("Hello")); 
    259259  } catch (nscp::nscp_exception &e) { 
    260     NSC_LOG_ERROR_STD(_T("Socket error: ") + e.getMessage()); 
    261     return nscp_result_data(NSCAPI::returnUNKNOWN, _T("NSCP Packet error: ") + e.getMessage()); 
     260    NSC_LOG_ERROR_STD(_T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 
     261    return nscp_result_data(NSCAPI::returnUNKNOWN, _T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 
    262262  } catch (std::runtime_error &e) { 
    263263    NSC_LOG_ERROR_STD(_T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 
     
    274274 
    275275#ifdef USE_SSL 
    276 std::list<nscp::packet::nscp_chunk> NSCPClient::send_ssl(std::wstring host, int port, int timeout, std::list<nscp::packet::nscp_chunk> &chunks) { 
     276std::list<nscp::packet> NSCPClient::send_ssl(std::wstring host, int port, int timeout, std::list<nscp::packet> &chunks) { 
    277277  boost::asio::io_service io_service; 
    278278  boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::sslv23); 
     
    286286#endif 
    287287 
    288 std::list<nscp::packet::nscp_chunk> NSCPClient::send_nossl(std::wstring host, int port, int timeout, std::list<nscp::packet::nscp_chunk> &chunks) { 
     288std::list<nscp::packet> NSCPClient::send_nossl(std::wstring host, int port, int timeout, std::list<nscp::packet> &chunks) { 
    289289  boost::asio::io_service io_service; 
    290290  nscp::client::socket socket(io_service, host, port); 
  • modules/NSCPClient/NSCPClient.h

    r438998b rb38e845  
    105105private: 
    106106  nscp_result_data  execute_nscp_command(nscp_connection_data con, std::string buffer); 
    107   std::list<nscp::packet::nscp_chunk> send_nossl(std::wstring host, int port, int timeout, std::list<nscp::packet::nscp_chunk> &chunks); 
    108   std::list<nscp::packet::nscp_chunk> send_ssl(std::wstring host, int port, int timeout, std::list<nscp::packet::nscp_chunk> &chunks); 
     107  std::list<nscp::packet> send_nossl(std::wstring host, int port, int timeout, std::list<nscp::packet> &chunks); 
     108  std::list<nscp::packet> send_ssl(std::wstring host, int port, int timeout, std::list<nscp::packet> &chunks); 
    109109  void add_options(po::options_description &desc, nscp_connection_data &command_data); 
    110110 
  • modules/NSCPServer/handler_impl.cpp

    r438998b rb38e845  
    77#include "handler_impl.hpp" 
    88 
    9 std::string handler_impl::process(std::string &buffer) { 
    10   std::string ret; 
    11   PluginCommand::RequestMessage msg; 
    12   msg.ParseFromString(buffer); 
    13   if (msg.payload_size() != 1) { 
    14     NSC_LOG_ERROR(_T("Multiple payloads not currently supported...")); 
    15     nscapi::functions::create_simple_query_result(NSCAPI::returnUNKNOWN, _T("Multiple payloads not currently supported..."), _T(""), ret); 
    16     return ret; 
    17   } 
    18   std::wstring command = utf8::cvt<std::wstring>(msg.payload(0).command()); 
    19   if (command.empty() || command == _T("_NSCP_CHECK")) { 
    20     nscapi::functions::create_simple_query_result(NSCAPI::returnOK, _T("I (") + nscapi::plugin_singleton->get_core()->getApplicationVersionString() + _T(") seem to be doing fine..."), _T(""), ret); 
    21     return ret; 
    22   } 
     9std::list<nscp::packet> handler_impl::process(nscp::packet &packet) { 
     10  std::list<nscp::packet> result; 
    2311 
    24   // @todo re-add support for nasty arguments / arguments passing 
     12  Plugin::Common::Header hdr; 
     13 
     14  if (packet.is_command_request()) { 
     15    Plugin::QueryRequestMessage msg; 
     16    msg.ParseFromString(packet.payload); 
     17    hdr.CopyFrom(msg.header()); 
     18 
     19    // @todo: Make split optional 
     20 
     21    for (int i=0;i<msg.payload_size();i++) { 
     22 
     23        // @todo re-add support for nasty arguments / arguments passing 
    2524  /* 
    2625  if (!allowNasty_) { 
     
    3534  } 
    3635  */ 
    37   try { 
    38     NSC_DEBUG_MSG_STD(_T("Running command: ") + command); 
    39     NSCAPI::nagiosReturn returncode = nscapi::plugin_singleton->get_core()->query(command, buffer, ret); 
    40   } catch (...) { 
    41     NSC_LOG_ERROR(_T("Internal exception processing request: ") + command); 
    42     nscapi::functions::create_simple_query_result(NSCAPI::returnUNKNOWN, _T("Internal exception processing request: ") + command, _T(""), ret); 
    43     return ret; 
     36 
     37      std::string outBuffer; 
     38      std::wstring command = utf8::cvt<std::wstring>(msg.payload(i).command()); 
     39      if (command.empty() || command == _T("_NSCP_CHECK")) { 
     40        nscapi::functions::create_simple_query_response(NSCAPI::returnOK, _T("I (") + nscapi::plugin_singleton->get_core()->getApplicationVersionString() + _T(") seem to be doing fine..."), _T(""), outBuffer); 
     41      } else { 
     42        std::string tmpBuffer; 
     43        Plugin::QueryRequestMessage tmp; 
     44        tmp.mutable_header()->CopyFrom(hdr); 
     45        tmp.add_payload()->CopyFrom(msg.payload(i)); 
     46        tmp.SerializeToString(&tmpBuffer); 
     47        NSCAPI::nagiosReturn returncode = nscapi::plugin_singleton->get_core()->query(command, tmpBuffer, outBuffer); 
     48      } 
     49      result.push_back(nscp::packet::create_query_response(outBuffer)); 
     50    } 
     51  } else { 
     52    NSC_LOG_ERROR(_T("Unknown packet: ") + packet.to_wstring()); 
     53    result.push_back(create_error(_T("Unknown packet: ") + packet.to_wstring())); 
    4454  } 
    45   return ret; 
     55  return result; 
    4656} 
    4757 
  • modules/NSCPServer/handler_impl.hpp

    r438998b rb38e845  
    2020  } 
    2121 
    22   std::string process(std::string &buffer); 
     22  std::list<nscp::packet> process(nscp::packet &buffer); 
    2323 
    2424  nscp::packet create_error(std::wstring msg) { 
    25     return nscp::packet::create_response(4, msg, payload_length_); 
     25    return nscp::packet::create_error(msg); 
    2626  } 
    2727 
  • modules/PythonScript/PythonScript.cpp

    r2c95d22 rb38e845  
    274274    std::wstring result; 
    275275    NSCAPI::nagiosReturn ret = inst->handle_simple_exec(cmd, data.args, result); 
    276     return nscapi::functions::create_simple_exec_result(data.command, ret, result, response); 
     276    nscapi::functions::create_simple_exec_response(data.command, ret, result, response); 
     277    return ret; 
    277278  } 
    278279  return NSCAPI::returnIgnored; 
     
    287288  } 
    288289  if (inst->has_simple(cmd)) { 
    289     nscapi::functions::decoded_simple_command_data data = nscapi::functions::process_simple_command_request(command, request); 
     290    nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(command, request); 
    290291    std::wstring msg, perf; 
    291292    NSCAPI::nagiosReturn ret = inst->exec_simple(cmd, data.args, msg, perf); 
    292     return nscapi::functions::process_simple_command_result(data.command, ret, msg, perf, response); 
     293    nscapi::functions::create_simple_query_response(ret, msg, perf, response, data.command); 
     294    return ret; 
    293295  } 
    294296  NSC_LOG_ERROR_STD(_T("Could not find python commands for: ") + command + _T(" (avalible python commands are: ") + inst->get_commands() + _T(")")); 
     
    311313  if (inst->has_simple_message_handler(chnl)) { 
    312314    std::wstring msg, perf; 
    313     nscapi::functions::parse_simple_message(request, msg, perf); 
     315    nscapi::functions::parse_simple_query_response(request, msg, perf); 
    314316    return inst->handle_simple_message(chnl, cmd, code, msg, perf); 
    315317  } 
Note: See TracChangeset for help on using the changeset viewer.