Changeset b38e845 in nscp


Ignore:
Timestamp:
08/24/11 18:37:39 (22 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
Files:
4 deleted
25 edited

Legend:

Unmodified
Added
Removed
  • changelog

    r0b8df3e rb38e845  
    66 * Fix RtlStringFromGUID problem on NT4 
    77 
    8 2011-08-22 MickeM 
     82011-08-24 MickeM 
     9 * Sever refactoring of the new API (there is now two pb files ipc for NSCP protocol and plugin for plugin communication) 
     10 * Cleaned up API helper functions 
     11 
     122011-08-23 MickeM 
    913 * Fixed issue in the NSCP server 
    1014 
  • include/nscapi/functions.hpp

    r438998b rb38e845  
    3737 
    3838#include <protobuf/plugin.pb.h> 
    39 #include <protobuf/log.pb.h> 
    40 #include <protobuf/exec.pb.h> 
    4139 
    4240using namespace nscp::helpers; 
    4341 
    4442namespace nscapi { 
    45  
    46 /* 
    47   class nscapi_exception : public exception { 
    48     std::string what_; 
    49   public: 
    50     nscapi_exception() {} 
    51     nscapi_exception(std::string what) : what_(what) {} 
    52     virtual const char* what() const throw() { 
    53       return what_; 
    54     } 
    55   }; 
    56 */ 
    5743  class functions { 
    5844  public: 
    59     static PluginCommand::Response_Code nagios_to_gpb(int ret) { 
     45    static Plugin::Common::ResultCode nagios_status_to_gpb(int ret) { 
    6046      if (ret == NSCAPI::returnOK) 
    61         return PluginCommand::Response_Code_OK; 
     47        return Plugin::Common_ResultCode_OK; 
    6248      if (ret == NSCAPI::returnWARN) 
    63         return PluginCommand::Response_Code_WARNING; 
     49        return Plugin::Common_ResultCode_WARNING; 
    6450      if (ret == NSCAPI::returnCRIT) 
    65         return PluginCommand::Response_Code_CRITCAL; 
    66       return PluginCommand::Response_Code_UNKNOWN; 
    67     } 
    68     static ExecuteCommand::Response_Code exec_nagios_to_gpb(int ret) { 
    69       if (ret == NSCAPI::returnOK) 
    70         return ExecuteCommand::Response_Code_OK; 
    71       if (ret == NSCAPI::returnWARN) 
    72         return ExecuteCommand::Response_Code_WARNING; 
    73       if (ret == NSCAPI::returnCRIT) 
    74         return ExecuteCommand::Response_Code_CRITCAL; 
    75       return ExecuteCommand::Response_Code_UNKNOWN; 
    76     } 
    77     static LogMessage::Message_Level log_to_gpb(NSCAPI::messageTypes ret) { 
     51        return Plugin::Common_ResultCode_CRITCAL; 
     52      return Plugin::Common_ResultCode_UNKNOWN; 
     53    } 
     54    static int gbp_to_nagios_status(Plugin::Common::ResultCode ret) { 
     55      if (ret == Plugin::Common_ResultCode_OK) 
     56        return NSCAPI::returnOK; 
     57      if (ret == Plugin::Common_ResultCode_WARNING) 
     58        return NSCAPI::returnWARN; 
     59      if (ret == Plugin::Common_ResultCode_CRITCAL) 
     60        return NSCAPI::returnCRIT; 
     61      return NSCAPI::returnUNKNOWN; 
     62    } 
     63    static Plugin::LogEntry::Entry::Level log_to_gpb(NSCAPI::messageTypes ret) { 
    7864      if (ret == NSCAPI::critical) 
    79         return ::LogMessage::Message_Level_LOG_CRITICAL; 
     65        return Plugin::LogEntry_Entry_Level_LOG_CRITICAL; 
    8066      if (ret == NSCAPI::debug) 
    81         return ::LogMessage::Message_Level_LOG_DEBUG; 
     67        return Plugin::LogEntry_Entry_Level_LOG_DEBUG; 
    8268      if (ret == NSCAPI::error) 
    83         return ::LogMessage::Message_Level_LOG_ERROR; 
     69        return Plugin::LogEntry_Entry_Level_LOG_ERROR; 
    8470      if (ret == NSCAPI::log) 
    85         return ::LogMessage::Message_Level_LOG_INFO; 
     71        return Plugin::LogEntry_Entry_Level_LOG_INFO; 
    8672      if (ret == NSCAPI::warning) 
    87         return ::LogMessage::Message_Level_LOG_WARNING; 
    88       return ::LogMessage::Message_Level_LOG_ERROR; 
    89     } 
    90     static NSCAPI::messageTypes gpb_to_log(LogMessage::Message_Level ret) { 
    91       if (ret == ::LogMessage::Message_Level_LOG_CRITICAL) 
     73        return Plugin::LogEntry_Entry_Level_LOG_WARNING; 
     74      return Plugin::LogEntry_Entry_Level_LOG_ERROR; 
     75    } 
     76    static NSCAPI::messageTypes gpb_to_log(Plugin::LogEntry::Entry::Level ret) { 
     77      if (ret == Plugin::LogEntry_Entry_Level_LOG_CRITICAL) 
    9278        return NSCAPI::critical; 
    93       if (ret == ::LogMessage::Message_Level_LOG_DEBUG) 
     79      if (ret == Plugin::LogEntry_Entry_Level_LOG_DEBUG) 
    9480        return NSCAPI::debug; 
    95       if (ret == ::LogMessage::Message_Level_LOG_ERROR) 
     81      if (ret == Plugin::LogEntry_Entry_Level_LOG_ERROR) 
    9682        return NSCAPI::error; 
    97       if (ret == ::LogMessage::Message_Level_LOG_INFO) 
     83      if (ret == Plugin::LogEntry_Entry_Level_LOG_INFO) 
    9884        return NSCAPI::log; 
    99       if (ret == ::LogMessage::Message_Level_LOG_WARNING) 
     85      if (ret == Plugin::LogEntry_Entry_Level_LOG_WARNING) 
    10086        return NSCAPI::warning; 
    10187      return NSCAPI::error; 
     
    117103    }; 
    118104 
    119      
     105 
     106 
     107    static void create_simple_header(Plugin::Common::Header* hdr, Plugin::Common_Header_Type type)  { 
     108      hdr->set_type(type); 
     109      hdr->set_version(Plugin::Common_Version_VERSION_1); 
     110      hdr->set_max_supported_version(Plugin::Common_Version_VERSION_1); 
     111      // @todo add additional fields here! 
     112    } 
     113 
     114 
     115    ////////////////////////////////////////////////////////////////////////// 
     116 
     117    static void create_simple_query_request(std::wstring command, std::vector<std::wstring> arguments, std::string &buffer) { 
     118      Plugin::QueryRequestMessage message; 
     119      create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_QUERY_REQUEST); 
     120 
     121      Plugin::QueryRequestMessage::Request *payload = message.add_payload(); 
     122      payload->set_command(to_string(command)); 
     123 
     124      BOOST_FOREACH(std::wstring s, arguments) 
     125        payload->add_arguments(to_string(s)); 
     126 
     127      message.SerializeToString(&buffer); 
     128    } 
     129    static void create_simple_query_request(std::wstring command, std::list<std::wstring> arguments, std::string &buffer) { 
     130      Plugin::QueryRequestMessage message; 
     131      create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_QUERY_REQUEST); 
     132 
     133      Plugin::QueryRequestMessage::Request *payload = message.add_payload(); 
     134      payload->set_command(to_string(command)); 
     135 
     136      BOOST_FOREACH(std::wstring s, arguments) 
     137        payload->add_arguments(to_string(s)); 
     138 
     139      message.SerializeToString(&buffer); 
     140    } 
     141    static NSCAPI::nagiosReturn create_simple_query_response_unknown(std::wstring msg, std::wstring perf, std::string &buffer, std::wstring command = _T("")) { 
     142      create_simple_query_response(NSCAPI::returnUNKNOWN, msg, perf, buffer, command); 
     143      return NSCAPI::returnUNKNOWN; 
     144    } 
     145 
     146    static void create_simple_query_response(NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer, std::wstring command = _T("")) { 
     147      Plugin::QueryResponseMessage message; 
     148      create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_QUERY_RESPONSE); 
     149 
     150      Plugin::QueryResponseMessage::Response *payload = message.add_payload(); 
     151      if (!command.empty()) 
     152        payload->set_command(to_string(command)); 
     153      payload->set_message(to_string(msg)); 
     154      payload->set_result(nagios_status_to_gpb(ret)); 
     155      if (!perf.empty()) 
     156        parse_performance_data(payload, perf); 
     157 
     158      message.SerializeToString(&buffer); 
     159    } 
     160 
     161    static decoded_simple_command_data parse_simple_query_request(const wchar_t* char_command, const std::string &request) { 
     162      decoded_simple_command_data data; 
     163 
     164      data.command = char_command; 
     165      Plugin::QueryRequestMessage message; 
     166      message.ParseFromString(request); 
     167 
     168      if (message.payload_size() != 1) { 
     169        throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
     170      } 
     171      ::Plugin::QueryRequestMessage::Request payload = message.payload().Get(0); 
     172      for (int i=0;i<payload.arguments_size();i++) { 
     173        data.args.push_back(to_wstring(payload.arguments(i))); 
     174      } 
     175      return data; 
     176    } 
     177 
     178    static void parse_simple_query_response(std::string &response, std::wstring &msg, std::wstring &perf) { 
     179      Plugin::QueryResponseMessage message; 
     180      message.ParseFromString(response); 
     181 
     182 
     183      if (message.payload_size() != 1) { 
     184        throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
     185      } 
     186 
     187      Plugin::QueryResponseMessage::Response payload = message.payload().Get(0); 
     188      msg = utf8::cvt<std::wstring>(payload.message()); 
     189      perf = utf8::cvt<std::wstring>(build_performance_data(payload)); 
     190    } 
     191 
     192 
     193    ////////////////////////////////////////////////////////////////////////// 
     194 
     195    static void create_simple_exec_request(const std::wstring &command, const std::list<std::wstring> & args, std::string &request) { 
     196       
     197      Plugin::ExecuteRequestMessage message; 
     198      create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_EXEC_REQUEST); 
     199 
     200      Plugin::ExecuteRequestMessage::Request *payload = message.add_payload(); 
     201      payload->set_command(to_string(command)); 
     202 
     203      BOOST_FOREACH(std::wstring s, args) 
     204        payload->add_arguments(to_string(s)); 
     205 
     206      message.SerializeToString(&request); 
     207    } 
     208    static void create_simple_exec_request(const std::wstring &command, const std::vector<std::wstring> & args, std::string &request) { 
     209 
     210      Plugin::ExecuteRequestMessage message; 
     211      create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_EXEC_REQUEST); 
     212 
     213      Plugin::ExecuteRequestMessage::Request *payload = message.add_payload(); 
     214      payload->set_command(to_string(command)); 
     215 
     216      BOOST_FOREACH(std::wstring s, args) 
     217        payload->add_arguments(to_string(s)); 
     218 
     219      message.SerializeToString(&request); 
     220    } 
     221    static void parse_simple_exec_result(const std::string &response, std::list<std::wstring> &result) { 
     222      Plugin::ExecuteResponseMessage message; 
     223      message.ParseFromString(response); 
     224 
     225      for (int i=0;i<message.payload_size(); i++) { 
     226        result.push_back(utf8::cvt<std::wstring>(message.payload(i).message())); 
     227      } 
     228    } 
     229 
     230    static void create_simple_exec_response(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring result, std::string &response) { 
     231      Plugin::ExecuteResponseMessage message; 
     232      create_simple_header(message.mutable_header(), Plugin::Common_Header_Type_EXEC_RESPONSE); 
     233 
     234      Plugin::ExecuteResponseMessage::Response *payload = message.add_payload(); 
     235      payload->set_command(to_string(command)); 
     236      payload->set_message(to_string(result)); 
     237 
     238      payload->set_result(nagios_status_to_gpb(ret)); 
     239      message.SerializeToString(&response); 
     240    } 
    120241    static decoded_simple_command_data parse_simple_exec_request(const wchar_t* char_command, const std::string &request) { 
    121242      decoded_simple_command_data data; 
    122243 
    123244      data.command = char_command; 
    124       ExecuteCommand::RequestMessage request_message; 
    125       request_message.ParseFromString(request); 
    126       if (request_message.has_header()) 
    127         data.target = utf8::cvt<std::wstring>(request_message.header().recipient()); 
    128  
    129       if (request_message.payload_size() != 1) { 
     245      Plugin::ExecuteRequestMessage message; 
     246      message.ParseFromString(request); 
     247      if (message.has_header()) 
     248        data.target = utf8::cvt<std::wstring>(message.header().recipient()); 
     249 
     250      if (message.payload_size() != 1) { 
    130251        throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
    131252      } 
    132       ::ExecuteCommand::Request payload = request_message.payload().Get(0); 
     253      Plugin::ExecuteRequestMessage::Request payload = message.payload().Get(0); 
    133254      for (int i=0;i<payload.arguments_size();i++) { 
    134255        data.args.push_back(to_wstring(payload.arguments(i))); 
     
    136257      return data; 
    137258    } 
    138     static NSCAPI::nagiosReturn create_simple_exec_result(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring result, std::string &response) { 
    139       ExecuteCommand::ResponseMessage response_message; 
    140       ::ExecuteCommand::Header* hdr = response_message.mutable_header(); 
    141  
    142       hdr->set_type(ExecuteCommand::Header_Type_RESPONSE); 
    143       hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 
    144  
    145       ExecuteCommand::Response *resp = response_message.add_payload(); 
    146       resp->set_command(to_string(command)); 
    147       resp->set_message(to_string(result)); 
    148  
    149       resp->set_version(ExecuteCommand::Response_Version_VERSION_1); 
    150       resp->set_result(exec_nagios_to_gpb(ret)); 
    151       response_message.SerializeToString(&response); 
    152       return ret; 
    153     } 
    154  
    155     static void create_simple_query_request(std::wstring command, std::vector<std::wstring> arguments, std::string &buffer) { 
    156       PluginCommand::RequestMessage message; 
    157       ::PluginCommand::Header* header = message.mutable_header(); 
    158  
    159       header->set_type(PluginCommand::Header_Type_REQUEST); 
    160       header->set_version(PluginCommand::Header_Version_VERSION_1); 
    161  
    162       PluginCommand::Request *payload = message.add_payload(); 
    163       payload->set_command(to_string(command)); 
    164  
    165       BOOST_FOREACH(std::wstring s, arguments) 
    166         payload->add_arguments(to_string(s)); 
    167  
    168       payload->set_version(PluginCommand::Request_Version_VERSION_1); 
    169       message.SerializeToString(&buffer); 
    170     } 
    171  
    172     static void create_simple_query_result(NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer) { 
    173       PluginCommand::ResponseMessage message; 
    174       ::PluginCommand::Header* header = message.mutable_header(); 
    175  
    176       header->set_type(PluginCommand::Header_Type_RESPONSE); 
    177       header->set_version(PluginCommand::Header_Version_VERSION_1); 
    178  
    179       PluginCommand::Response *payload = message.add_payload(); 
    180       payload->set_message(to_string(msg)); 
    181       if (!perf.empty()) 
    182         parse_performance_data(payload, perf); 
    183  
    184       payload->set_version(PluginCommand::Response_Version_VERSION_1); 
    185       message.SerializeToString(&buffer); 
    186     } 
    187  
    188  
    189     static decoded_simple_command_data process_simple_command_request(const wchar_t* char_command, const std::string &request) { 
    190       decoded_simple_command_data data; 
    191  
    192       data.command = char_command; 
    193       PluginCommand::RequestMessage request_message; 
    194       request_message.ParseFromString(request); 
    195  
    196       if (request_message.payload_size() != 1) { 
    197         throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
    198       } 
    199       ::PluginCommand::Request payload = request_message.payload().Get(0); 
    200       for (int i=0;i<payload.arguments_size();i++) { 
    201         data.args.push_back(to_wstring(payload.arguments(i))); 
    202       } 
    203       return data; 
    204     } 
    205  
    206     static NSCAPI::nagiosReturn process_simple_command_result(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &response) { 
    207       PluginCommand::ResponseMessage response_message; 
    208       ::PluginCommand::Header* hdr = response_message.mutable_header(); 
    209  
    210       hdr->set_type(PluginCommand::Header_Type_RESPONSE); 
    211       hdr->set_version(PluginCommand::Header_Version_VERSION_1); 
    212  
    213       PluginCommand::Response *resp = response_message.add_payload(); 
    214       resp->set_command(to_string(command)); 
    215       resp->set_message(to_string(msg)); 
    216       parse_performance_data(resp, perf); 
    217  
    218       resp->set_version(PluginCommand::Response_Version_VERSION_1); 
    219       resp->set_result(nagios_to_gpb(ret)); 
    220       response_message.SerializeToString(&response); 
    221       return ret; 
    222     } 
    223  
    224     static void create_simple_exec_request(const std::wstring &command, const std::list<std::wstring> & args, std::string &request) { 
    225        
    226       ExecuteCommand::RequestMessage message; 
    227       ExecuteCommand::Header *hdr = message.mutable_header(); 
    228       hdr->set_type(ExecuteCommand::Header_Type_REQUEST); 
    229       hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 
    230  
    231       ExecuteCommand::Request *req = message.add_payload(); 
    232       req->set_command(to_string(command)); 
    233       req->set_version(ExecuteCommand::Request_Version_VERSION_1); 
    234  
    235       BOOST_FOREACH(std::wstring s, args) 
    236         req->add_arguments(to_string(s)); 
    237  
    238       message.SerializeToString(&request); 
    239     } 
    240     static void parse_simple_exec_result(const std::string &response, std::list<std::wstring> &result) { 
    241       ExecuteCommand::ResponseMessage response_message; 
    242       response_message.ParseFromString(response); 
    243  
    244       for (int i=0;i<response_message.payload_size(); i++) { 
    245         result.push_back(utf8::cvt<std::wstring>(response_message.payload(i).message())); 
    246       } 
    247     } 
    248  
    249  
    250     static void create_simple_message_request(std::wstring command, NSCAPI::nagiosReturn code, std::wstring &msg, std::wstring &perf, std::string &request) { 
    251       PluginCommand::ResponseMessage message; 
    252       PluginCommand::Header *hdr = message.mutable_header(); 
    253       hdr->set_type(PluginCommand::Header_Type_RESPONSE); 
    254       hdr->set_version(PluginCommand::Header_Version_VERSION_1); 
    255  
    256       PluginCommand::Response *resp = message.add_payload(); 
    257       resp->set_command(to_string(command)); 
    258       resp->set_result(nagios_to_gpb(code)); 
    259       resp->set_version(PluginCommand::Response_Version_VERSION_1); 
    260       resp->set_message(to_string(msg)); 
    261       parse_performance_data(resp, perf); 
    262  
    263       message.SerializeToString(&request); 
    264     } 
    265      
    266  
    267     static void parse_simple_message(std::string &response, std::wstring &msg, std::wstring &perf) { 
    268       PluginCommand::ResponseMessage response_message; 
    269       response_message.ParseFromString(response); 
    270  
    271  
    272       if (response_message.payload_size() != 1) { 
    273         throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 
    274       } 
    275       PluginCommand::Response payload = response_message.payload().Get(0); 
    276       msg = utf8::cvt<std::wstring>(payload.message()); 
    277       perf = utf8::cvt<std::wstring>(build_performance_data(payload)); 
    278     } 
    279  
    280     static void parse_performance_data(PluginCommand::Response *resp, std::wstring &perf) { 
     259 
     260 
     261    ////////////////////////////////////////////////////////////////////////// 
     262 
     263    static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::wstring &perf) { 
    281264      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'\'')); 
    282265      BOOST_FOREACH(std::wstring s, tok) { 
     
    285268        strEx::splitVector items = strEx::splitV(s, _T(";")); 
    286269        if (items.size() < 1) { 
    287           ::PluginCommand::PerformanceData* perfData = resp->add_perf(); 
    288           perfData->set_type(PluginCommand::PerformanceData_Type_STRING); 
     270          Plugin::Common::PerformanceData* perfData = payload->add_perf(); 
     271          perfData->set_type(Plugin::Common_DataType_STRING); 
    289272          std::pair<std::wstring,std::wstring> fitem = strEx::split(_T(""), _T("=")); 
    290273          perfData->set_alias("invalid"); 
    291           ::PluginCommand::PerformanceData_StringValue* stringPerfData = perfData->mutable_string_value(); 
     274          Plugin::Common_PerformanceData_StringValue* stringPerfData = perfData->mutable_string_value(); 
    292275          stringPerfData->set_value("invalid performance data"); 
    293276          break; 
    294277        } 
    295278 
    296         ::PluginCommand::PerformanceData* perfData = resp->add_perf(); 
    297         perfData->set_type(PluginCommand::PerformanceData_Type_FLOAT); 
     279        Plugin::Common::PerformanceData* perfData = payload->add_perf(); 
     280        perfData->set_type(Plugin::Common_DataType_FLOAT); 
    298281        std::pair<std::wstring,std::wstring> fitem = strEx::split(items[0], _T("=")); 
    299282        perfData->set_alias(to_string(fitem.first)); 
    300         ::PluginCommand::PerformanceData_FloatValue* floatPerfData = perfData->mutable_float_value(); 
     283        Plugin::Common_PerformanceData_FloatValue* floatPerfData = perfData->mutable_float_value(); 
    301284 
    302285        std::wstring::size_type pend = fitem.second.find_first_not_of(_T("0123456789,.")); 
     
    318301//      std::wcout << _T("Converting performance data") << perf << _T(" -- ") << utf8::cvt<std::wstring>(build_performance_data(*resp)) << std::endl; 
    319302    } 
    320     static std::string build_performance_data(::PluginCommand::Response const &payload) { 
     303    static std::string build_performance_data(Plugin::QueryResponseMessage::Response const &payload) { 
    321304      std::stringstream ss; 
    322305      ss.precision(5); 
     
    324307      bool first = true; 
    325308      for (int i=0;i<payload.perf_size();i++) { 
    326         ::PluginCommand::PerformanceData perfData = payload.perf(i); 
     309        Plugin::Common::PerformanceData perfData = payload.perf(i); 
    327310        if (!first) 
    328311          ss << " "; 
     
    330313        ss << '\'' << perfData.alias() << "'="; 
    331314        if (perfData.has_float_value()) { 
    332           ::PluginCommand::PerformanceData_FloatValue fval = perfData.float_value(); 
     315          Plugin::Common_PerformanceData_FloatValue fval = perfData.float_value(); 
    333316 
    334317          ss << fval.value(); 
  • include/nscapi/nscapi_core_wrapper.cpp

    r2c95d22 rb38e845  
    3232 
    3333#include <protobuf/plugin.pb.h> 
    34 #include <protobuf/log.pb.h> 
    3534 
    3635using namespace nscp::helpers; 
     
    7069    std::string str; 
    7170    try { 
    72       LogMessage::LogMessage message; 
    73       LogMessage::Message *msg = message.add_message(); 
     71      Plugin::LogEntry message; 
     72      Plugin::LogEntry::Entry *msg = message.add_entry(); 
    7473      msg->set_level(nscapi::functions::log_to_gpb(msgType)); 
    7574      msg->set_file(file); 
     
    129128void nscapi::core_wrapper::submit_simple_message(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring & message, std::wstring & perf) { 
    130129  std::string request; 
    131   nscapi::functions::create_simple_message_request(command, code, message, perf, request); 
     130  nscapi::functions::create_simple_query_response(code, message, perf, request, command); 
    132131  NSCAPI::nagiosReturn ret = NotifyChannel(channel, command, code, request); 
    133132} 
     
    156155  NSCAPI::nagiosReturn ret = simple_query(command, argument, response); 
    157156  if (!response.empty()) { 
    158     PluginCommand::ResponseMessage rsp_msg; 
    159     rsp_msg.ParseFromString(response); 
    160     if (rsp_msg.payload_size() != 1) { 
    161       CORE_LOG_ERROR_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 
     157    try { 
     158      nscapi::functions::parse_simple_query_response(response, msg, perf); 
     159    } catch (std::exception &e) { 
     160      CORE_LOG_ERROR_STD(_T("Failed to extract return message: ") + utf8::cvt<std::wstring>(e.what())); 
    162161      return NSCAPI::returnUNKNOWN; 
    163162    } 
    164     ::PluginCommand::Response payload = rsp_msg.payload(0); 
    165     msg = utf8::cvt<std::wstring>(payload.message()); 
    166     perf = utf8::cvt<std::wstring>(::nscapi::functions::build_performance_data(payload)); 
    167163  } 
    168164  return ret; 
     
    177173* @return The return of the command 
    178174*/ 
    179 NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & argument, std::string & result)  
     175NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & arguments, std::string & result)  
    180176{ 
    181177  if (!fNSAPIInject) 
    182178    throw nscapi::nscapi_exception(_T("NSCore has not been initiated...")); 
    183179 
    184   PluginCommand::RequestMessage message; 
    185   PluginCommand::Header *hdr = message.mutable_header(); 
    186   hdr->set_type(PluginCommand::Header_Type_REQUEST); 
    187   hdr->set_version(PluginCommand::Header_Version_VERSION_1); 
    188  
    189   PluginCommand::Request *req = message.add_payload(); 
    190   req->set_command(to_string(command)); 
    191   req->set_version(PluginCommand::Request_Version_VERSION_1); 
    192  
    193   BOOST_FOREACH(std::wstring s, argument) 
    194     req->add_arguments(to_string(s)); 
    195  
    196180  std::string request; 
    197   message.SerializeToString(&request); 
    198  
     181  try { 
     182    nscapi::functions::create_simple_query_request(command, arguments, request); 
     183  } catch (std::exception &e) { 
     184    CORE_LOG_ERROR_STD(_T("Failed to extract return message: ") + utf8::cvt<std::wstring>(e.what())); 
     185    return NSCAPI::returnUNKNOWN; 
     186  } 
    199187  return query(command.c_str(), request, result); 
    200188} 
  • include/nscapi/nscapi_plugin_wrapper.cpp

    rfe75eff rb38e845  
    3535 
    3636#include <protobuf/plugin.pb.h> 
    37 #include <protobuf/log.pb.h> 
    3837 
    3938using namespace nscp::helpers; 
     
    204203 
    205204 
    206  
     205/* 
    207206NSCAPI::nagiosReturn nscapi::impl::CommandImpl::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) { 
    208207 
     208  nscapi::functions::parse_simple_query_request(char_command, request); 
    209209  std::wstring command = char_command; 
    210210  PluginCommand::RequestMessage request_message; 
     
    232232  return NSCAPI::returnOK; 
    233233} 
    234  
     234*/ 
    235235void nscapi::impl::simple_log_handler::handleMessageRAW(std::string data) { 
    236236  try { 
    237     LogMessage::LogMessage message; 
     237    Plugin::LogEntry message; 
    238238    message.ParseFromString(data); 
    239239 
    240     for (int i=0;i<message.message_size();i++) { 
    241       LogMessage::Message msg = message.message(i); 
     240    for (int i=0;i<message.entry_size();i++) { 
     241      Plugin::LogEntry::Entry msg = message.entry(i); 
    242242      handleMessage(msg.level(), msg.file(), msg.line(), msg.message()); 
    243243    } 
     
    250250 
    251251NSCAPI::nagiosReturn nscapi::impl::simple_command::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response) { 
    252   nscapi::functions::decoded_simple_command_data data = nscapi::functions::process_simple_command_request(char_command, request); 
     252  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
    253253  std::wstring msg, perf; 
    254254  NSCAPI::nagiosReturn ret = handleCommand(data.target, data.command, data.args, msg, perf); 
    255   return nscapi::functions::process_simple_command_result(data.command, ret, msg, perf, response); 
     255  nscapi::functions::create_simple_query_response(ret, msg, perf, response, data.command); 
     256  return ret; 
    256257} 
    257258 
     
    262263  if (ret == NSCAPI::returnIgnored) 
    263264    return NSCAPI::returnIgnored; 
    264   return nscapi::functions::create_simple_exec_result(data.command, ret, result, response); 
     265  nscapi::functions::create_simple_exec_response(data.command, ret, result, response); 
     266  return ret; 
    265267} 
    266268 
    267269NSCAPI::nagiosReturn nscapi::impl::SimpleNotificationHandler::handleRAWNotification(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, std::string result) { 
    268270  try { 
    269     PluginCommand::ResponseMessage message; 
    270     if (result.size() == 0) { 
    271       nscapi::plugin_singleton->get_core()->Message(NSCAPI::error, __FILE__, __LINE__, _T("Return data is empty cant parse response!")); 
    272       return NSCAPI::returnUNKNOWN; 
    273     } 
    274     message.ParseFromString(result); 
    275     if (message.payload_size() != 1) { 
    276       nscapi::plugin_singleton->get_core()->Message(NSCAPI::error, __FILE__, __LINE__, _T("Unsupported payload size: ") + to_wstring(message.payload_size())); 
    277       //NSC_LOG_ERROR_STD(); 
    278       return NSCAPI::returnIgnored; 
    279     } 
    280  
    281     ::PluginCommand::Response payload = message.payload().Get(0); 
    282  
    283     std::list<std::wstring> args; 
    284     for (int i=0;i<payload.arguments_size();i++) { 
    285       args.push_back(to_wstring(payload.arguments(i))); 
    286     } 
    287     std::wstring msg = utf8::cvt<std::wstring>(payload.message()); 
    288     std::wstring perf = utf8::cvt<std::wstring>(::nscapi::functions::build_performance_data(payload)); 
     271    std::wstring msg, perf; 
     272    nscapi::functions::parse_simple_query_response(result, msg, perf); 
    289273    return handleSimpleNotification(channel, command, code, msg, perf); 
    290274  } catch (std::exception &e) { 
  • include/nscapi/nscapi_plugin_wrapper.hpp

    rfe75eff rb38e845  
    130130    }; 
    131131 
    132  
     132/* 
    133133    class CommandImpl { 
    134134    public: 
     
    136136      virtual void handleCommand(std::wstring command, PluginCommand::Request *request, PluginCommand::Response *response) = 0; 
    137137    }; 
     138    */ 
    138139  } 
    139140}; 
  • include/nscp/client/socket.hpp

    r4632ff7 rb38e845  
    5858    } 
    5959 
    60     virtual void send(std::list<nscp::packet::nscp_chunk> &chunks, boost::posix_time::seconds timeout) { 
     60    virtual void send(std::list<nscp::packet> &chunks, boost::posix_time::seconds timeout) { 
    6161      boost::shared_ptr<socket_helpers::io::timed_writer> writer(new socket_helpers::io::timed_writer(get_io_service())); 
    6262      writer->start_timer(timeout); 
    63       BOOST_FOREACH(nscp::packet::nscp_chunk &chunk, chunks) { 
     63      BOOST_FOREACH(nscp::packet &chunk, chunks) { 
    6464        if (!writer->write_and_wait(*socket_, get_socket(), boost::asio::buffer(chunk.to_buffer()))) { 
    6565          std::cout << "FaILED TO SEND DATA..." << std::endl; 
     
    7070      writer.reset(); 
    7171    } 
    72     virtual std::list<nscp::packet::nscp_chunk> recv(boost::posix_time::seconds timeout) { 
     72    virtual std::list<nscp::packet> recv(boost::posix_time::seconds timeout) { 
    7373      int left = 1; 
    74       std::list<nscp::packet::nscp_chunk> chunks; 
     74      std::list<nscp::packet> chunks; 
    7575      boost::shared_ptr<socket_helpers::io::timed_reader> reader(new socket_helpers::io::timed_reader(get_io_service())); 
    7676      reader->start_timer(timeout); 
    7777      while (left > 0) { 
    78         nscp::packet::nscp_chunk chunk; 
     78        nscp::packet chunk; 
    7979        std::vector<char> buf(sizeof(nscp::data::signature_packet)); 
    8080        if (!reader->read_and_wait(*socket_, get_socket(), boost::asio::buffer(buf))) { 
     
    134134    } 
    135135 
    136     virtual void send(std::list<nscp::packet::nscp_chunk> &chunks, boost::posix_time::seconds timeout) { 
     136    virtual void send(std::list<nscp::packet> &chunks, boost::posix_time::seconds timeout) { 
    137137      boost::shared_ptr<socket_helpers::io::timed_writer> writer(new socket_helpers::io::timed_writer(get_io_service())); 
    138138      writer->start_timer(timeout); 
    139       BOOST_FOREACH(nscp::packet::nscp_chunk &chunk, chunks) { 
     139      BOOST_FOREACH(nscp::packet &chunk, chunks) { 
    140140        if (!writer->write_and_wait(*ssl_socket_, get_socket(), boost::asio::buffer(chunk.to_buffer()))) { 
    141141          std::cout << "FaILED TO SEND DATA..." << std::endl; 
     
    147147    } 
    148148 
    149     virtual std::list<nscp::packet::nscp_chunk> recv(boost::posix_time::seconds timeout) { 
     149    virtual std::list<nscp::packet> recv(boost::posix_time::seconds timeout) { 
    150150      int left = 1; 
    151       std::list<nscp::packet::nscp_chunk> chunks; 
     151      std::list<nscp::packet> chunks; 
    152152      boost::shared_ptr<socket_helpers::io::timed_reader> reader(new socket_helpers::io::timed_reader(get_io_service())); 
    153153      reader->start_timer(timeout); 
    154154      while (left > 0) { 
    155         nscp::packet::nscp_chunk chunk; 
     155        nscp::packet chunk; 
    156156        std::vector<char> buf(sizeof(nscp::data::signature_packet)); 
    157157        if (!reader->read_and_wait(*ssl_socket_, get_socket(), boost::asio::buffer(buf))) { 
  • include/nscp/packet.cpp

    r438998b rb38e845  
    2323#include <nscp/packet.hpp> 
    2424 
    25  
    26  
  • include/nscp/packet.hpp

    r438998b rb38e845  
    3030#include <utils.h> 
    3131 
    32 #include <protobuf/envelope.pb.h> 
     32#include <protobuf/ipc.pb.h> 
     33#include <protobuf/plugin.pb.h> 
    3334 
    3435using namespace nscp::helpers; 
    3536 
    3637namespace nscp { 
    37  
    38  
    39  
    4038  class data { 
    4139  public: 
    4240    static const short unknown_packet = 0; 
     41 
    4342    static const short envelope_request = 1; 
    4443    static const short envelope_response = 2; 
     
    4645    static const short command_request = 10; 
    4746    static const short command_response = 11; 
     47 
     48    static const short error = 100; 
    4849 
    4950    static const short version_1 = 1; 
     
    9192      } 
    9293    }; 
    93     struct raw_header_packet { 
    94       char buffer[]; 
    95     }; 
    96  
    9794  }; 
    9895  struct length { 
     
    109106  }; 
    110107 
    111   class nscp_exception { 
    112     std::wstring error_; 
     108  class nscp_exception : public std::exception { 
     109    std::string error_; 
    113110  public: 
    114     nscp_exception(std::wstring error) : error_(error) {} 
    115     std::wstring getMessage() { 
    116       return error_; 
    117     } 
    118   }; 
    119  
    120   class packet /*: public boost::noncopyable*/ { 
    121   private: 
    122     std::wstring payload_; 
    123     short type_; 
    124     short version_; 
    125     int result_; 
    126     unsigned int crc32_; 
    127     unsigned int calculatedCRC32_; 
    128     char *tmpBuffer; 
    129     unsigned int payload_length_; 
    130   public: 
    131  
    132     struct nscp_chunk { 
    133       nscp::data::signature_packet signature; 
    134       std::string header; 
    135       std::string payload; 
    136       std::string to_buffer() const { 
    137         std::string ret = write_signature(); 
    138         if (!header.empty()) 
    139           ret.insert(ret.end(), header.begin(), header.end()); 
    140         if (!payload.empty()) 
    141           ret.insert(ret.end(), payload.begin(), payload.end()); 
    142         return ret; 
    143       } 
    144       void read_signature(std::vector<char> &buf) { 
    145         assert(buf.size() >= sizeof(nscp::data::signature_packet)); 
    146         nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(&(*buf.begin())); 
    147         signature = *tmp; 
    148         signature.payload_type = tmp->payload_type; 
    149         signature.payload_length = tmp->payload_length; 
    150       } 
    151       void read_payload(std::vector<char> &buf) { 
    152         payload = std::string(buf.begin(), buf.end()); 
    153       } 
    154       std::string write_signature() const { 
    155         char * buffer = new char[sizeof(nscp::data::signature_packet)+1]; 
    156         nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(buffer); 
    157         *tmp = signature; 
    158         std::string str_buf(buffer, sizeof(nscp::data::signature_packet)); 
    159         delete [] buffer; 
    160         return str_buf; 
    161       } 
    162  
    163     }; 
    164  
    165     static nscp_chunk build_envelope_request(unsigned long additionl_packets) { 
    166       nscp_chunk chunk; 
    167       chunk.signature.header_length = 0; 
    168       chunk.signature.header_type = 0; 
    169  
    170       chunk.signature.additional_packet_count = additionl_packets; 
    171       chunk.signature.version = nscp::data::version_1; 
    172  
    173       NSCPEnvelope::Request request_envelope; 
    174       request_envelope.set_version(NSCPEnvelope::VERSION_1); 
    175       request_envelope.set_max_supported_version(NSCPEnvelope::VERSION_1); 
    176       request_envelope.SerializeToString(&chunk.payload); 
    177  
    178       chunk.signature.payload_length = chunk.payload.size(); 
    179       chunk.signature.payload_type = nscp::data::envelope_request; 
    180  
    181       return chunk; 
    182     } 
    183  
    184     static nscp_chunk build_payload(unsigned long payload_type, std::string buffer, unsigned long additionl_packets) { 
    185       nscp_chunk chunk; 
    186       chunk.signature.header_length = 0; 
    187       chunk.signature.header_type = 0; 
    188  
    189       chunk.signature.additional_packet_count = additionl_packets; 
    190       chunk.signature.version = nscp::data::version_1; 
    191  
    192       chunk.payload = std::string(buffer.begin(), buffer.end()); 
    193       chunk.signature.payload_length = chunk.payload.size(); 
    194       chunk.signature.payload_type = payload_type; 
    195  
    196       return chunk; 
    197     } 
    198  
    199     packet(unsigned int payload_length) : tmpBuffer(NULL), payload_length_(payload_length) {}; 
    200     packet(std::vector<char> buffer, unsigned int payload_length) : tmpBuffer(NULL), payload_length_(payload_length) { 
    201       char *tmp = new char[buffer.size()+1]; 
    202       copy( buffer.begin(), buffer.end(), tmp); 
    203       readFrom(tmp, buffer.size()); 
    204       delete [] tmp; 
    205     }; 
    206     packet(const char *buffer, unsigned int buffer_length, unsigned int payload_length) : tmpBuffer(NULL), payload_length_(payload_length) { 
    207       readFrom(buffer, buffer_length); 
    208     }; 
    209     packet(short type, short version, int result, std::wstring payLoad, unsigned int payload_length)  
    210       : tmpBuffer(NULL)  
    211       ,type_(type) 
    212       ,version_(version) 
    213       ,result_(result) 
    214       ,payload_(payLoad) 
    215       ,payload_length_(payload_length) 
    216     { 
    217     } 
    218     packet()  
    219       : tmpBuffer(NULL)  
    220       ,type_(nscp::data::unknown_packet) 
    221       ,version_(nscp::data::version_1) 
    222       ,result_(0) 
    223       ,payload_length_(0) 
    224     { 
    225     } 
    226     packet(const packet &other) : tmpBuffer(NULL) { 
    227       payload_ = other.payload_; 
    228       type_ = other.type_; 
    229       version_ = other.version_; 
    230       result_ = other.result_; 
    231       crc32_ = other.crc32_; 
    232       calculatedCRC32_ = other.calculatedCRC32_; 
    233       payload_length_ = other.payload_length_; 
    234     } 
    235     packet& operator=(packet const& other) { 
    236       tmpBuffer=NULL; 
    237       payload_ = other.payload_; 
    238       type_ = other.type_; 
    239       version_ = other.version_; 
    240       result_ = other.result_; 
    241       crc32_ = other.crc32_; 
    242       calculatedCRC32_ = other.calculatedCRC32_; 
    243       payload_length_ = other.payload_length_; 
     111    nscp_exception(std::wstring error) : error_(utf8::cvt<std::string>(error)) {} 
     112    nscp_exception(std::string error) : error_(error) {} 
     113    const char* what() const { 
     114      return error_.c_str(); 
     115    } 
     116  }; 
     117 
     118  struct packet { 
     119    nscp::data::signature_packet signature; 
     120    std::string header; 
     121    std::string payload; 
     122 
     123    packet() {} 
     124    packet(const nscp::data::signature_packet &sig) : signature(sig) {} 
     125    packet(const nscp::data::signature_packet &sig, std::string header, std::string payload) : signature(sig), header(header), payload(payload) {} 
     126    packet(const packet & other) : signature(other.signature), header(other.header), payload(other.payload) {} 
     127    const packet& operator=(const packet & other) { 
     128      signature = other.signature; 
     129      header = other.header; 
     130      payload = other.payload; 
    244131      return *this; 
    245132    } 
    246133 
    247     ~packet() { 
    248       delete [] tmpBuffer; 
    249     } 
    250     static packet make_request(std::wstring payload, unsigned int buffer_length) { 
    251       return packet(nscp::data::envelope_request, nscp::data::version_1, -1, payload, buffer_length); 
    252     } 
    253  
    254     const char* create_buffer() { 
    255       return NULL; 
    256     } 
    257  
    258     std::vector<char> get_buffer() { 
    259       const char *c = create_buffer(); 
    260       std::vector<char> buf(c, c+get_packet_length()); 
    261       return buf; 
    262     } 
    263  
    264     void readFrom(const char *buffer, unsigned int length) { 
    265     } 
    266  
    267     unsigned short getVersion() const { return version_; } 
    268     unsigned short getType() const { return type_; } 
    269     unsigned short getResult() const { return result_; } 
    270     std::wstring getPayload() const { return payload_; } 
    271     bool verifyCRC() { return calculatedCRC32_ == crc32_; } 
    272     unsigned int get_packet_length() const { return 0; } 
    273     unsigned int get_payload_length() const { return payload_length_; } 
    274  
    275     boost::asio::const_buffer to_buffers() { 
    276       return boost::asio::buffer(create_buffer(), get_packet_length()); 
    277     } 
    278     std::wstring to_string() { 
    279       std::wstringstream ss; 
    280       ss << _T("type: ") << type_; 
    281       ss << _T(", version: ") << version_; 
    282       ss << _T(", result: ") << result_; 
    283       ss << _T(", crc32: ") << crc32_; 
    284       ss << _T(", payload: ") << payload_; 
    285       return ss.str(); 
    286     } 
    287     static nscp::packet create_response(int ret, std::wstring string, int buffer_length) { 
    288       return packet(nscp::data::unknown_packet, nscp::data::version_1, ret, string, buffer_length); 
     134    std::string to_buffer() const { 
     135      std::string ret = write_signature(); 
     136      if (!header.empty()) 
     137        ret.insert(ret.end(), header.begin(), header.end()); 
     138      if (!payload.empty()) 
     139        ret.insert(ret.end(), payload.begin(), payload.end()); 
     140      return ret; 
     141    } 
     142    void read_signature(std::vector<char> &buf) { 
     143      assert(buf.size() >= sizeof(nscp::data::signature_packet)); 
     144      nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(&(*buf.begin())); 
     145      signature = *tmp; 
     146      signature.payload_type = tmp->payload_type; 
     147      signature.payload_length = tmp->payload_length; 
     148    } 
     149    void read_payload(std::vector<char> &buf) { 
     150      payload = std::string(buf.begin(), buf.end()); 
     151    } 
     152    std::string write_signature() const { 
     153      char * buffer = new char[sizeof(nscp::data::signature_packet)+1]; 
     154      nscp::data::signature_packet *tmp = reinterpret_cast<nscp::data::signature_packet*>(buffer); 
     155      *tmp = signature; 
     156      std::string str_buf(buffer, sizeof(nscp::data::signature_packet)); 
     157      delete [] buffer; 
     158      return str_buf; 
     159    } 
     160 
     161    static packet build_envelope_request(unsigned long additionl_packets) { 
     162      nscp::data::signature_packet signature; 
     163      signature.header_length = 0; 
     164      signature.header_type = 0; 
     165 
     166      signature.additional_packet_count = additionl_packets; 
     167      signature.version = nscp::data::version_1; 
     168 
     169      std::string buffer; 
     170      NSCPIPC::RequestEnvelope request_envelope; 
     171      request_envelope.set_version(NSCPIPC::Common_Version_VERSION_1); 
     172      request_envelope.set_max_supported_version(NSCPIPC::Common_Version_VERSION_1); 
     173      request_envelope.SerializeToString(&buffer); 
     174 
     175      signature.payload_length = buffer.size(); 
     176      signature.payload_type = nscp::data::envelope_request; 
     177 
     178      return packet(signature, "", buffer); 
     179    } 
     180 
     181    static nscp::data::signature_packet create_simple_sig(int payload_type, std::string::size_type size) { 
     182      nscp::data::signature_packet signature; 
     183      signature.header_length = 0; 
     184      signature.header_type = 0; 
     185 
     186      signature.additional_packet_count = 0; 
     187      signature.version = nscp::data::version_1; 
     188 
     189      signature.payload_length = size; 
     190      signature.payload_type = payload_type; 
     191      return signature; 
     192 
     193    } 
     194    static packet create_payload(unsigned long payload_type, std::string buffer, unsigned long additional_packets = 0) { 
     195      nscp::data::signature_packet signature; 
     196      signature.header_length = 0; 
     197      signature.header_type = 0; 
     198 
     199      signature.additional_packet_count = additional_packets; 
     200      signature.version = nscp::data::version_1; 
     201 
     202      signature.payload_length = buffer.size(); 
     203      signature.payload_type = payload_type; 
     204 
     205      return packet(signature, "", buffer); 
     206    } 
     207    std::wstring to_wstring() { 
     208      return signature.to_wstring(); 
     209    } 
     210    static nscp::packet create_error(std::wstring msg) { 
     211      nscp::data::signature_packet signature; 
     212      signature.header_length = 0; 
     213      signature.header_type = 0; 
     214 
     215      signature.additional_packet_count = 0; 
     216      signature.version = nscp::data::version_1; 
     217 
     218      std::string buffer; 
     219      NSCPIPC::ErrorMessage message; 
     220      NSCPIPC::ErrorMessage::Message *error = message.add_error(); 
     221      error->set_severity(NSCPIPC::ErrorMessage_Message_Severity_IS_ERRROR); 
     222      error->set_message(utf8::cvt<std::string>(msg)); 
     223      message.SerializeToString(&buffer); 
     224 
     225      signature.payload_length = buffer.size(); 
     226      signature.payload_type = nscp::data::error; 
     227 
     228      return packet(signature, "", buffer); 
     229    } 
     230    bool is_command_request() { 
     231      return signature.payload_type == nscp::data::command_request; 
     232    } 
     233    static nscp::packet create_query_response(std::string buffer) { 
     234      return create_payload(nscp::data::command_response, buffer); 
    289235    } 
    290236  }; 
  • include/nscp/server/connection.cpp

    r0b8df3e rb38e845  
    99#include <boost/asio/ssl.hpp> 
    1010 
    11 #include <protobuf/envelope.pb.h> 
     11#include <protobuf/ipc.pb.h> 
    1212#include <protobuf/plugin.pb.h> 
    1313 
     
    9292          unsigned int count = outbound_queue_.size(); 
    9393          handler_->log_debug(__FILE__, __LINE__, _T("Sending responses: ") + strEx::itos(count)); 
    94           BOOST_FOREACH(nscp::packet::nscp_chunk &chunk, outbound_queue_) { 
     94          BOOST_FOREACH(nscp::packet &chunk, outbound_queue_) { 
    9595            chunk.signature.additional_packet_count = --count; 
    9696            std::string s = chunk.to_buffer(); 
     
    125125 
    126126    boost::tuple<bool, connection::process_helper> connection::process_payload() { 
    127       std::string result; 
    128       handler_->log_debug(__FILE__, __LINE__, _T("Got payload for: ") + strEx::itos(sig.payload_type)); 
    129       parser_.parse_payload(result, sig); 
     127      nscp::packet chunk(sig); 
     128      parser_.parse_payload(chunk); 
     129      handler_->log_debug(__FILE__, __LINE__, _T("Processing: ") + chunk.to_wstring()); 
     130      std::list<nscp::packet> result = handler_->process(chunk); 
     131      outbound_queue_.insert(outbound_queue_.end(), result.begin(), result.end()); 
     132      /* 
    130133      if (sig.payload_type == nscp::data::envelope_request) { 
    131134        NSCPEnvelope::Request envelope; 
     
    140143        handler_->log_error(__FILE__, __LINE__, _T("Unhandled packet: ") + strEx::itos(sig.payload_type)); 
    141144      } 
     145      */ 
    142146      return boost::make_tuple(sig.additional_packet_count > 0, process_helper(&nscp::server::parser::digest_signature, &connection::process_signature)); 
    143147    } 
  • include/nscp/server/connection.hpp

    r438998b rb38e845  
    8686      std::list<std::string> buffers_; 
    8787      std::vector<boost::asio::const_buffer> response_buffers_; 
    88       std::list<nscp::packet::nscp_chunk> outbound_queue_; 
     88      std::list<nscp::packet> outbound_queue_; 
    8989 
    9090    }; 
  • include/nscp/server/handler.hpp

    r438998b rb38e845  
    1313        return *this; 
    1414      } 
    15       //virtual nscp::packet handle_envelope(nscp::packet packet) = 0; 
    16       virtual std::string process(std::string &buffer) = 0; 
     15      virtual std::list<nscp::packet> process(nscp::packet &packet) = 0; 
    1716 
    1817      virtual void log_debug(std::string file, int line, std::wstring msg) = 0; 
  • include/nscp/server/parser.hpp

    r438998b rb38e845  
    55#include <boost/noncopyable.hpp> 
    66 
    7 #include <protobuf/envelope.pb.h> 
     7#include <protobuf/ipc.pb.h> 
    88#include "handler.hpp" 
    99 
     
    5353        buffer_.clear(); 
    5454      } 
    55       void parse_payload(std::string &result, const nscp::data::signature_packet &signature) { 
    56         unsigned long wanted = nscp::length::get_payload_size(signature); 
     55      void parse_payload(nscp::packet &packet) { 
     56        unsigned long wanted = nscp::length::get_payload_size(packet.signature); 
    5757        assert(buffer_.size() >= wanted); 
    58  
    59         result.insert(result.begin(), buffer_.begin(), buffer_.begin()+wanted); 
     58        packet.payload.insert(packet.payload.begin(), buffer_.begin(), buffer_.begin()+wanted); 
    6059        buffer_.clear(); 
    6160      } 
  • libs/protobuf/CMakeLists.txt

    r438998b rb38e845  
    22 
    33WRAP_PROTO(PROTO_PLUGIN_SRC plugin.proto) 
    4 WRAP_PROTO(PROTO_LOG_SRC log.proto) 
    5 WRAP_PROTO(PROTO_EXEC_SRC exec.proto) 
    6 WRAP_PROTO(PROTO_SETTINGS_SRC settings.proto) 
    7 WRAP_PROTO(PROTO_ENVELOPE_SRC envelope.proto) 
     4WRAP_PROTO(PROTO_IPC_SRC ipc.proto) 
    85 
    96SET(TARGET protobuf) 
     
    118SET(SRCS 
    129  plugin.proto 
    13   log.proto 
    14   exec.proto 
    15   settings.proto 
    16   envelope.proto 
     10  ipc.proto 
    1711   
    1812  ${PROTO_PLUGIN_SRC} 
    19   ${PROTO_LOG_SRC} 
    20   ${PROTO_EXEC_SRC} 
    21   ${PROTO_SETTINGS_SRC} 
    22   ${PROTO_ENVELOPE_SRC} 
     13  ${PROTO_IPC_SRC} 
    2314) 
    2415 
  • libs/protobuf/ipc.proto

    r438998b rb38e845  
    11package NSCPIPC; 
     2 
     3message Common { 
     4 
     5  enum Version { 
     6    VERSION_1 = 1; 
     7  }; 
     8}; 
    29 
    310message RegisterSlave { 
     
    714   
    815}; 
     16 
     17message RequestEnvelope { 
     18  required Common.Version version = 1; 
     19  optional Common.Version max_supported_version = 2; 
     20   
     21  optional string sender = 17; 
     22  optional string recipient = 18; 
     23  optional int32 id = 19; 
     24}; 
     25 
     26message ResponseEnvelope { 
     27  enum Status { 
     28    IS_OK = 1; 
     29 
     30    HAS_WARNINGS = 5; 
     31    IS_WARNING = 6; 
     32    HAS_ERRRORS = 7; 
     33    IS_ERRRORS = 8; 
     34 
     35    IS_FATAL = 10; 
     36     
     37    UNSUPPORTED_VERSION = 20; 
     38  }; 
     39 
     40  required Common.Version version = 1; 
     41  required Common.Version max_supported_version = 2; 
     42   
     43  required Status request_status = 3; 
     44 
     45  optional string message = 7; 
     46 
     47  optional string sender = 17; 
     48  optional string recipient = 18; 
     49  optional int32 id = 19; 
     50}; 
     51 
     52message ErrorMessage { 
     53 
     54  message Message { 
     55    enum Severity { 
     56      IS_OK = 1; 
     57      HAS_WARNINGS = 5; 
     58      IS_WARNING = 6; 
     59      HAS_ERRRORS = 7; 
     60      IS_ERRROR = 8; 
     61      IS_FATAL = 10; 
     62    }; 
     63 
     64 
     65    optional int32 line = 1; 
     66    optional int64 date = 2; 
     67    required Severity severity = 3; 
     68    required string message = 4; 
     69    repeated string details = 5; 
     70  } 
     71 
     72  repeated Message error = 1; 
     73 
     74  optional string sender = 17; 
     75  optional string recipient = 18; 
     76  optional int32 request_id = 19; 
     77} 
  • libs/protobuf/plugin.proto

    rc391984 rb38e845  
    1 package PluginCommand; 
    2  
    3 message Header { 
    4   enum Type { 
    5     REQUEST = 1; 
    6     RESPONSE = 2; 
    7   }; 
    8   enum Version { 
    9     VERSION_1 = 1; 
    10   }; 
    11   required Type type = 1; 
    12   required Version version = 2; 
    13   optional Version max_supported_version = 3; 
    14    
    15   optional string sender = 17; 
    16   optional string recipient = 18; 
    17   optional int32 id = 19; 
    18 }; 
    19  
    20 message Request { 
    21   enum Version { 
    22     VERSION_1 = 1; 
    23   }; 
    24  
    25   required Version version = 1; 
    26   optional int32 id = 2; 
    27   required string command = 3; 
    28   repeated string arguments = 4; 
    29 }; 
    30  
    31 message PerformanceData { 
    32   enum Type { 
    33     INT = 1; 
    34     STRING = 1; 
    35     FLOAT = 1; 
    36   }; 
    37   message IntValue { 
    38     required int64 value = 1; 
    39     optional string unit = 2; 
    40     optional int64 warning = 3; 
    41     optional int64 critical = 4; 
    42     optional int64 minimum = 6; 
    43     optional int64 maximum = 7; 
    44   } 
    45   message StringValue { 
    46     required string value = 1; 
    47   } 
    48   message FloatValue { 
    49     required double value = 1; 
    50     optional string unit = 2; 
    51     optional double warning = 3; 
    52     optional double critical = 4; 
    53     optional double minimum = 6; 
    54     optional double maximum = 7; 
    55   } 
    56   required string alias = 1; 
    57   required Type type = 2; 
    58   optional IntValue int_value = 3; 
    59   optional StringValue string_value = 4; 
    60   optional FloatValue float_value = 5; 
    61    
    62   // TODO add thresholds here! 
    63  
    64 } 
    65  
    66 message Response { 
    67   enum Code { 
     1package Plugin; 
     2 
     3message Common { 
     4 
     5  enum ResultCode { 
    686    OK    = 0; 
    697    WARNING = 1; 
     
    719    UNKNOWN = 3; 
    7210  }; 
     11  enum DataType { 
     12    INT = 1; 
     13    STRING = 1; 
     14    FLOAT = 1; 
     15    BOOL = 1; 
     16  }; 
     17   
     18  message AnyDataType { 
     19    required Common.DataType type = 1; 
     20    optional string string_data = 2; 
     21    optional int64 int_data = 3; 
     22    optional double float_data = 4; 
     23    optional bool bool_data = 5; 
     24  } 
     25   
     26 
    7327  enum Version { 
    7428    VERSION_1 = 1; 
    7529  }; 
    7630 
    77   required Version version = 1; 
    78   optional int32 id = 2; 
    79   required string command = 5; 
    80   repeated string arguments = 16; 
     31  message Header { 
     32    enum Type { 
     33      QUERY_REQUEST = 1; 
     34      QUERY_RESPONSE = 2; 
     35       
     36      EXEC_REQUEST = 3; 
     37      EXEC_RESPONSE = 4; 
     38       
     39      LOG_MESSAGE = 5; 
     40    }; 
     41    required Type type = 1; 
     42    required Common.Version version = 2; 
     43    optional Common.Version max_supported_version = 3; 
     44     
     45    optional string sender = 17; 
     46    optional string recipient = 18; 
     47    optional int64 id = 19; 
     48  }; 
     49 
     50  message PerformanceData { 
     51    message IntValue { 
     52      required int64 value = 1; 
     53      optional string unit = 2; 
     54      optional int64 warning = 3; 
     55      optional int64 critical = 4; 
     56      optional int64 minimum = 6; 
     57      optional int64 maximum = 7; 
     58    } 
     59    message StringValue { 
     60      required string value = 1; 
     61    } 
     62    message FloatValue { 
     63      required double value = 1; 
     64      optional string unit = 2; 
     65      optional double warning = 3; 
     66      optional double critical = 4; 
     67      optional double minimum = 6; 
     68      optional double maximum = 7; 
     69    } 
     70    message BoolValue { 
     71      required bool value = 1; 
     72      optional string unit = 2; 
     73      optional bool warning = 3; 
     74      optional bool critical = 4; 
     75    } 
     76    required string alias = 1; 
     77    required Common.DataType type = 2; 
     78    optional IntValue int_value = 3; 
     79    optional StringValue string_value = 4; 
     80    optional FloatValue float_value = 5; 
     81    optional BoolValue bool_value = 6; 
     82     
     83    // TODO add thresholds here! 
     84  } 
     85}; 
     86 
     87// // // // // // // // // // // // // // // // // // // // // // // //  
     88// 
     89// Query request/response 
     90// Used for querying the client this is the "normal" check_nrpe thingy 
     91// 
     92// // // // // // // // // // // // // // // // // // // // // // // //  
     93 
     94message QueryRequestMessage { 
     95  message Request { 
     96    optional int32 id = 1; 
     97    required string command = 2; 
     98    repeated string arguments = 3; 
     99  }; 
     100 
     101  required Common.Header header = 1; 
     102  repeated Request payload = 2; 
     103} 
     104message QueryResponseMessage { 
     105 
     106  message Response { 
     107 
     108    optional int32 id = 1; 
     109    required string command = 2; 
     110    repeated string arguments = 16; 
     111     
     112    required Common.ResultCode result = 3; 
     113    required string message = 4; 
     114    optional string legacyPerf = 17; 
     115    repeated Common.PerformanceData perf = 5; 
     116     
     117  } 
     118 
     119  required Common.Header header = 1; 
     120  repeated Response payload = 2; 
     121} 
     122 
     123// // // // // // // // // // // // // // // // // // // // // // // //  
     124// 
     125// Log Entry 
     126// Used for sending errors and log entries to a logging module  
     127// (this is internal errors, not syslog or eventlog) 
     128// 
     129// // // // // // // // // // // // // // // // // // // // // // // //  
     130 
     131message LogEntry { 
     132  message Entry { 
     133    enum Level { 
     134      LOG_DEBUG =  0; 
     135      LOG_INFO  = 10; 
     136      LOG_WARNING = 20; 
     137      LOG_ERROR = 30; 
     138      LOG_CRITICAL  = 40; 
     139    }; 
     140    required Level  level = 1; 
     141    optional string sender  = 2; 
     142    optional string file  = 3; 
     143    optional int32  line  = 4; 
     144    optional string message = 5; 
     145    optional int32  date  = 6; 
     146  }; 
     147 
     148  repeated Entry entry = 1; 
     149} 
     150// // // // // // // // // // // // // // // // // // // // // // // //  
     151// 
     152// Execute command request and response 
     153// Used for executing commands on clients  
     154// 
     155// // // // // // // // // // // // // // // // // // // // // // // //  
     156 
     157message ExecuteRequestMessage { 
     158  message Request { 
     159    optional int32 id = 1; 
     160    required string command = 2; 
     161    repeated string arguments = 3; 
     162  }; 
     163 
     164  required Common.Header header = 1; 
     165  repeated Request payload = 2; 
     166} 
     167message ExecuteResponseMessage { 
     168  message Response { 
    81169   
    82   required Code result = 9; 
    83   required string message = 10; 
    84   optional string legacyPerf = 11; 
    85   repeated PerformanceData perf = 12; 
    86    
    87 } 
    88  
    89 message RequestMessage { 
    90   required Header header = 1; 
     170    message ResponseData { 
     171      message ResponseDataCell { 
     172        required Common.AnyDataType data = 1; 
     173        optional string tag = 2; 
     174      } 
     175      message ResponseDataRow { 
     176        repeated ResponseDataCell cells = 1; 
     177      } 
     178      required string alias = 1; 
     179      optional ResponseDataRow headers = 2; 
     180      repeated ResponseDataRow rows = 3; 
     181 
     182    } 
     183 
     184    optional int32 id = 2; 
     185    required string command = 5; 
     186    repeated string arguments = 16; 
     187     
     188    required Common.ResultCode result = 9; 
     189    required string message = 10; 
     190     
     191    optional ResponseData data = 11; 
     192     
     193  } 
     194  required Common.Header header = 1; 
     195  repeated Response payload = 2; 
     196} 
     197 
     198// // // // // // // // // // // // // // // // // // // // // // // //  
     199// 
     200// Submit result 
     201// Used for submitting a passive result 
     202// 
     203// // // // // // // // // // // // // // // // // // // // // // // //  
     204 
     205message SubmitResponseMessage { 
     206  required Common.Header header = 1; 
     207  repeated QueryResponseMessage.Response payload = 2; 
     208} 
     209 
     210// // // // // // // // // // // // // // // // // // // // // // // //  
     211// 
     212// Settings commands 
     213// Used for accessing the settings store 
     214// 
     215// // // // // // // // // // // // // // // // // // // // // // // //  
     216message Settings { 
     217  message Node { 
     218    message Path { 
     219      required string path = 1; 
     220      optional string key = 2; 
     221    } 
     222    optional Path path = 1; 
     223    optional string expression = 2; 
     224  }; 
     225}; 
     226 
     227message QuerySettingsRequestMessage { 
     228  message Request { 
     229    optional int64 id = 1; 
     230    required Settings node = 2; 
     231     
     232    optional string default = 3; 
     233    optional bool fetch_descriptions = 4; 
     234     
     235    optional Common.DataType return_type = 17; 
     236     
     237  }; 
     238  required Common.Header header = 1; 
    91239  repeated Request payload = 2; 
    92240} 
    93 message ResponseMessage { 
    94   required Header header = 1; 
     241message QuerySettingsResponseMessage { 
     242  message Response { 
     243    message SectionKeys { 
     244      repeated string key = 1; 
     245    } 
     246    optional int64 id = 1; 
     247    required Settings node = 2; 
     248 
     249    optional Common.AnyDataType value = 4; 
     250    optional SectionKeys keys = 5; 
     251     
     252    optional string title = 6; 
     253    optional string description = 7; 
     254     
     255    optional Common.DataType required_type = 17; 
     256     
     257  }; 
     258  required Common.Header header = 1; 
    95259  repeated Response payload = 2; 
    96260} 
    97261 
    98  
  • 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  } 
  • service/NSClient++.cpp

    r2c95d22 rb38e845  
    4646 
    4747#include <protobuf/plugin.pb.h> 
    48 #include <protobuf/exec.pb.h> 
    4948 
    5049#ifdef USE_BREAKPAD 
     
    985984    } 
    986985  } else */{ 
    987     PluginCommand::RequestMessage message; 
    988     PluginCommand::Header *hdr = message.mutable_header(); 
    989     hdr->set_type(PluginCommand::Header_Type_REQUEST); 
    990     hdr->set_version(PluginCommand::Header_Version_VERSION_1); 
    991  
    992     PluginCommand::Request *req = message.add_payload(); 
    993     req->set_command(to_string(command)); 
    994     req->set_version(PluginCommand::Request_Version_VERSION_1); 
    995  
    996     std::string args = to_string(arguments); 
    997  
    998     boost::tokenizer<boost::escaped_list_separator<char> > tok(args, boost::escaped_list_separator<char>('\\', ' ', '\"')); 
    999     BOOST_FOREACH(std::string s, tok) 
    1000       req->add_arguments(s); 
    1001  
     986 
     987    std::list<std::wstring> args; 
     988    strEx::parse_command(arguments, args); 
    1002989    std::string request, response; 
    1003     message.SerializeToString(&request); 
    1004  
    1005  
    1006  
     990    nscapi::functions::create_simple_query_request(command, args, request); 
    1007991    NSCAPI::nagiosReturn ret = injectRAW(command.c_str(), request, response); 
    1008992    if (response.empty()) { 
     
    1010994      return NSCAPI::returnUNKNOWN; 
    1011995    } 
    1012  
    1013     PluginCommand::ResponseMessage rsp_msg; 
    1014  
    1015     rsp_msg.ParseFromString(response); 
    1016     if (rsp_msg.payload_size() != 1) { 
    1017       LOG_ERROR_CORE_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 
    1018       return NSCAPI::returnUNKNOWN; 
    1019     } 
    1020     msg = utf8::cvt<std::wstring>(rsp_msg.payload(0).message()); 
    1021     perf = utf8::cvt<std::wstring>(nscapi::functions::build_performance_data(rsp_msg.payload(0))); 
    1022     if ( (ret == NSCAPI::returnInvalidBufferLen) || (ret == NSCAPI::returnIgnored) ) { 
    1023       return ret; 
    1024     } 
     996    nscapi::functions::parse_simple_query_response(response, msg, perf); 
    1025997    return ret; 
    1026998  } 
     
    10851057 
    10861058 
    1087 int NSClientT::simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp) { 
     1059int NSClientT::simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::list<std::wstring> &resp) { 
    10881060  bool found = false; 
    1089   std::vector<std::string> responses; 
    1090   ExecuteCommand::RequestMessage message; 
    1091   ExecuteCommand::Header *hdr = message.mutable_header(); 
    1092   hdr->set_type(ExecuteCommand::Header_Type_REQUEST); 
    1093   hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 
    1094  
    1095   ExecuteCommand::Request *req = message.add_payload(); 
    1096   req->set_command(to_string(command)); 
    1097   req->set_version(ExecuteCommand::Request_Version_VERSION_1); 
    1098  
    1099   BOOST_FOREACH(std::wstring s, arguments) 
    1100     req->add_arguments(utf8::cvt<std::string>(s)); 
    1101  
    11021061  std::string request; 
    1103   message.SerializeToString(&request); 
     1062  std::list<std::string> responses; 
     1063  nscapi::functions::create_simple_exec_request(command, arguments, request); 
    11041064  int ret = 0; 
    11051065  { 
     
    11611121  } 
    11621122  BOOST_FOREACH(std::string &r, responses) { 
    1163     ExecuteCommand::ResponseMessage rsp_msg; 
    1164  
    1165     rsp_msg.ParseFromString(r); 
    1166     if (rsp_msg.payload_size() != 1) { 
    1167       resp.push_back(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 
    1168       LOG_ERROR_CORE_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 
     1123    try { 
     1124      nscapi::functions::parse_simple_exec_result(r, resp); 
     1125    } catch (std::exception &e) { 
     1126      resp.push_back(_T("Failed to extract return message: ") + utf8::cvt<std::wstring>(e.what())); 
     1127      LOG_ERROR_CORE_STD(resp.back()); 
    11691128      return NSCAPI::returnUNKNOWN; 
    11701129    } 
    1171     resp.push_back(utf8::cvt<std::wstring>(rsp_msg.payload(0).message())); 
    11721130  } 
    11731131  return ret; 
     
    12011159  } 
    12021160 
    1203   ExecuteCommand::ResponseMessage response_message; 
    1204   ::ExecuteCommand::Header* hdr = response_message.mutable_header(); 
    1205  
    1206   hdr->set_type(ExecuteCommand::Header_Type_RESPONSE); 
    1207   hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 
     1161  Plugin::ExecuteResponseMessage response_message; 
     1162  nscapi::functions::create_simple_header(response_message.mutable_header(), Plugin::Common_Header_Type_EXEC_RESPONSE); 
    12081163 
    12091164  BOOST_FOREACH(std::string r, responses) { 
    1210     ExecuteCommand::ResponseMessage tmp; 
     1165    Plugin::ExecuteResponseMessage tmp; 
    12111166    tmp.ParseFromString(r); 
    12121167    for (int i=0;i<tmp.payload_size();i++) { 
    1213       ExecuteCommand::Response *r = response_message.add_payload(); 
     1168      Plugin::ExecuteResponseMessage::Response *r = response_message.add_payload(); 
    12141169      r->CopyFrom(tmp.payload(i)); 
    12151170    } 
  • service/NSClient++.h

    r2c95d22 rb38e845  
    166166  std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args); 
    167167  void reportMessage(std::string data); 
    168   int simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp); 
     168  int simple_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::list<std::wstring> &resp); 
    169169  NSCAPI::nagiosReturn exec_command(const wchar_t* raw_command, std::string &request, std::string &response); 
    170170 
  • service/cli_parser.hpp

    r438998b rb38e845  
    309309      } 
    310310      int ret = 0; 
    311       std::vector<std::wstring> resp; 
     311      std::list<std::wstring> resp; 
    312312      if (mainClient.simple_exec(module, command, arguments, resp) == NSCAPI::returnIgnored) { 
    313313        ret = 1; 
  • service/logger.hpp

    rafd42f1 rb38e845  
    66#include <boost/thread/mutex.hpp> 
    77 
    8 #include <protobuf/log.pb.h> 
     8#include <protobuf/plugin.pb.h> 
    99 
    1010#include <strEx.h> 
     
    2727  public: 
    2828    static inline std::string create_debug(const char* file, const int line, std::wstring message) { 
    29       return create_message(::LogMessage::Message_Level_LOG_DEBUG, file, line, message); 
     29      return create_message(Plugin::LogEntry_Entry_Level_LOG_DEBUG, file, line, message); 
    3030    } 
    3131    static inline std::string create_info(const char* file, const int line, std::wstring message) { 
    32       return create_message(::LogMessage::Message_Level_LOG_INFO, file, line, message); 
     32      return create_message(Plugin::LogEntry_Entry_Level_LOG_INFO, file, line, message); 
    3333    } 
    3434    static inline std::string create_error(const char* file, const int line, std::wstring message) { 
    35       return create_message(::LogMessage::Message_Level_LOG_ERROR, file, line, message); 
     35      return create_message(Plugin::LogEntry_Entry_Level_LOG_ERROR, file, line, message); 
    3636    } 
    3737    static inline std::string create_error(const std::string file, const int line, std::wstring message) { 
    38       return create_message(::LogMessage::Message_Level_LOG_ERROR, file.c_str(), line, message); 
     38      return create_message(Plugin::LogEntry_Entry_Level_LOG_ERROR, file.c_str(), line, message); 
    3939    } 
    4040    static inline std::string create_warning(const char* file, const int line, std::wstring message) { 
    41       return create_message(::LogMessage::Message_Level_LOG_WARNING, file, line, message); 
    42     } 
    43  
    44     static std::string create_message(LogMessage::Message_Level msgType, const char* file, const int line, std::wstring logMessage) { 
     41      return create_message(Plugin::LogEntry_Entry_Level_LOG_WARNING, file, line, message); 
     42    } 
     43 
     44    static std::string create_message(Plugin::LogEntry::Entry::Level msgType, const char* file, const int line, std::wstring logMessage) { 
    4545      std::string str; 
    4646      try { 
    47         LogMessage::LogMessage message; 
    48         LogMessage::Message *msg = message.add_message(); 
     47        Plugin::LogEntry message; 
     48        Plugin::LogEntry::Entry *msg = message.add_entry(); 
    4949        msg->set_level(msgType); 
    5050        msg->set_file(file); 
     
    142142      } 
    143143 
    144       std::wstring render_log_level(::LogMessage::Message_Level l) { 
     144      std::wstring render_log_level(Plugin::LogEntry::Entry::Level l) { 
    145145        int code = nscapi::functions::gpb_to_log(l); 
    146146        switch (code) { 
     
    173173        std::wstring str; 
    174174        try { 
    175           LogMessage::LogMessage message; 
     175          Plugin::LogEntry message; 
    176176          if (!message.ParseFromString(data)) { 
    177177            return _T("Failed to parse message: ") + to_wstring(strEx::strip_hex(data)); 
    178178          } 
    179179 
    180           for (int i=0;i<message.message_size();i++) { 
    181             LogMessage::Message msg = message.message(i); 
     180          for (int i=0;i<message.entry_size();i++) { 
     181            Plugin::LogEntry::Entry msg = message.entry(i); 
    182182            if (!str.empty()) 
    183183              str += _T(" -- "); 
Note: See TracChangeset for help on using the changeset viewer.