Changeset 254


Ignore:
Timestamp:
02/11/10 19:44:30 (2 years ago)
Author:
mickem
Message:

Preliminary NSCA support (much is hard coded right now so only a PoC not a finished concept)

Location:
trunk
Files:
21 added
6 deleted
30 edited

Legend:

Unmodified
Added
Removed
  • trunk/CMakeLists.txt

    r242 r254  
    2626 
    2727SET(NSCP_INCLUDE_PATH "${NSCP_SOURCE_DIR}/include" CACHE PATH "directory containing NSCP specific includes") 
    28 SET(PB_PATH "${NSCP_SOURCE_DIR}/proto" CACHE PATH "directory containing NSCP specific protocol buffer files") 
     28#SET(PB_PATH "${NSCP_SOURCE_DIR}/proto" CACHE PATH "directory containing NSCP specific protocol buffer files") 
    2929INCLUDE_DIRECTORIES(${NSCP_INCLUDE_PATH}) 
    30 INCLUDE_DIRECTORIES(${PB_PATH}) 
     30#INCLUDE_DIRECTORIES(${PB_PATH}) 
    3131 
    3232SET(BOOST_LIBRARYDIR ${BOOST_LIBRARYDIR} CACHE PATH "") 
     
    169169  ${EXTRA_LIBS} 
    170170  ${PROTOBUF_LIBRARY} 
    171   proto 
    172 ) 
    173   message(STATUS, "LIB: ${NSCP_DEF_PLUGIN_LIB}") 
    174  
     171  protobuf 
     172) 
     173message(STATUS, "LIB: ${NSCP_DEF_PLUGIN_LIB}") 
     174 
     175   
     176SET(ALL_LIB_NAMES) 
     177FILE(GLOB ALL_LIB_PROJECTS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "libs/*/CMakeLists.txt") 
     178foreach(CURRENT_LIB ${ALL_LIB_PROJECTS}) 
     179  get_filename_component(CURRENT_LIB_PATH ${CURRENT_LIB} PATH) 
     180  get_filename_component(CURRENT_LIB_NAME ${CURRENT_LIB_PATH} NAME) 
     181  message(STATUS "Adding library: ${CURRENT_LIB_PATH} (${CURRENT_LIB_NAME})") 
     182  ADD_SUBDIRECTORY("${CURRENT_LIB_PATH}") 
     183  SET(ALL_LIB_NAMES ${ALL_LIB_NAMES} ${CURRENT_LIB_NAME}) 
     184endforeach(CURRENT_LIB ${ALL_LIB_PROJECTS}) 
     185   
    175186 
    176187# Compile time constants & make sure our build finds it 
     
    189200MESSAGE(STATUS "Build path: ${PROJECT_BINARY_DIR}") 
    190201ADD_SUBDIRECTORY("service") 
    191 ADD_SUBDIRECTORY("proto") 
    192202 
    193203SET(ALL_MODULE_NAMES) 
  • trunk/include/NSCHelper.cpp

    r245 r254  
    358358} 
    359359 
    360 NSCAPI::errorReturn NSCModuleHelper::NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring message, std::wstring perf) { 
     360NSCAPI::errorReturn NSCModuleHelper::NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::string result) { 
    361361  if (!fNSAPINotify) 
    362362    throw NSCMHExcpetion(_T("NSCore has not been initiated...")); 
    363   return fNSAPINotify(channel.c_str(), command.c_str(), code, message.c_str(), perf.c_str()); 
     363  return fNSAPINotify(channel.c_str(), command.c_str(), code, result.c_str(), result.size()); 
    364364} 
    365365 
     
    422422    throw NSCMHExcpetion(_T("NSCore has not been initiated...")); 
    423423 
    424  
    425   PluginCommand::RequestMessage message; 
    426   PluginCommand::Header *hdr = message.mutable_header(); 
    427   hdr->set_type(PluginCommand::Header_Type_REQUEST); 
    428   hdr->set_version(PluginCommand::Header_Version_VERSION_1); 
    429  
    430   PluginCommand::Request *req = message.add_payload(); 
    431   req->set_command(to_string(command)); 
    432   req->set_version(PluginCommand::Request_Version_VERSION_1); 
    433  
    434   BOOST_FOREACH(std::wstring s, argument) 
    435     req->add_arguments(to_string(s)); 
    436  
    437   std::string request; 
    438   message.SerializeToString(&request); 
    439  
    440   char *buffer = NULL; 
    441   unsigned int buffer_size = 0; 
    442  
    443   NSCAPI::nagiosReturn retC = InjectCommandRAW(command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size); 
    444  
    445   if (buffer_size > 0 && buffer != NULL) { 
     424  std::string response; 
     425  NSCAPI::nagiosReturn ret = InjectCommand(command, argument, response); 
     426  if (!response.empty()) { 
    446427    PluginCommand::ResponseMessage rsp_msg; 
    447     std::string response(buffer, buffer_size); 
    448428    rsp_msg.ParseFromString(response); 
    449429    if (rsp_msg.payload_size() != 1) { 
     
    452432    } 
    453433    msg = to_wstring(rsp_msg.payload(0).message()); 
     434  } 
     435  return ret; 
     436} 
     437/** 
     438* Inject a request command in the core (this will then be sent to the plug-in stack for processing) 
     439* @param command Command to inject (password should not be included. 
     440* @param argLen The length of the argument buffer 
     441* @param **argument The argument buffer 
     442* @param message The return message buffer 
     443* @param perf The return performance data buffer 
     444* @return The return of the command 
     445*/ 
     446NSCAPI::nagiosReturn NSCModuleHelper::InjectCommand(const std::wstring command, const std::list<std::wstring> argument, std::string & result)  
     447{ 
     448  if (!fNSAPIInject) 
     449    throw NSCMHExcpetion(_T("NSCore has not been initiated...")); 
     450 
     451 
     452  PluginCommand::RequestMessage message; 
     453  PluginCommand::Header *hdr = message.mutable_header(); 
     454  hdr->set_type(PluginCommand::Header_Type_REQUEST); 
     455  hdr->set_version(PluginCommand::Header_Version_VERSION_1); 
     456 
     457  PluginCommand::Request *req = message.add_payload(); 
     458  req->set_command(to_string(command)); 
     459  req->set_version(PluginCommand::Request_Version_VERSION_1); 
     460 
     461  BOOST_FOREACH(std::wstring s, argument) 
     462    req->add_arguments(to_string(s)); 
     463 
     464  std::string request; 
     465  message.SerializeToString(&request); 
     466 
     467  char *buffer = NULL; 
     468  unsigned int buffer_size = 0; 
     469 
     470  NSCAPI::nagiosReturn retC = InjectCommandRAW(command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size); 
     471 
     472  if (buffer_size > 0 && buffer != NULL) { 
     473    PluginCommand::ResponseMessage rsp_msg; 
     474    result = std::string(buffer, buffer_size); 
    454475  } 
    455476 
     
    944965 */ 
    945966NSCAPI::boolReturn NSCModuleWrapper::wrapHasCommandHandler(bool has) { 
    946   if (has) 
    947     return NSCAPI::istrue; 
    948   return NSCAPI::isfalse; 
     967  return has?NSCAPI::istrue:NSCAPI::isfalse; 
    949968} 
    950969/** 
     
    954973 */ 
    955974NSCAPI::boolReturn NSCModuleWrapper::wrapHasMessageHandler(bool has) { 
    956   if (has) 
    957     return NSCAPI::istrue; 
    958   return NSCAPI::isfalse; 
    959 } 
     975  return has?NSCAPI::istrue:NSCAPI::isfalse; 
     976} 
     977NSCAPI::boolReturn NSCModuleWrapper::wrapHasNotificationHandler(bool has) { 
     978  return has?NSCAPI::istrue:NSCAPI::isfalse; 
     979} 
     980 
     981NSCAPI::nagiosReturn NSCModuleWrapper::wrapHandleNotification(NSCAPI::nagiosReturn retResult) { 
     982  return retResult; 
     983} 
     984 
    960985/** 
    961986 * Wrap the HandleCommand call 
  • trunk/include/NSCHelper.h

    r245 r254  
    140140  typedef NSCAPI::errorReturn (*lpNSAPIStopServer)(void); 
    141141  typedef NSCAPI::errorReturn (*lpNSAPIExit)(void); 
    142   typedef NSCAPI::nagiosReturn (*lpNSAPIInject)(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len); 
     142  typedef NSCAPI::nagiosReturn (*lpNSAPIInject)(const wchar_t*, const char *, const unsigned int, char **, unsigned int *); 
    143143  typedef void (*lpNSAPIDestroyBuffer)(char**); 
    144144 
    145   typedef NSCAPI::errorReturn (*lpNSAPINotify)(const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn, const wchar_t*, const wchar_t*); 
     145  typedef NSCAPI::errorReturn (*lpNSAPINotify)(const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn, const char*, unsigned int); 
    146146 
    147147  typedef NSCAPI::boolReturn (*lpNSAPICheckLogMessages)(int); 
     
    177177  NSCAPI::nagiosReturn InjectCommandRAW(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len); 
    178178  void DestroyBuffer(char**buffer); 
     179  NSCAPI::nagiosReturn InjectCommand(const std::wstring command, const std::list<std::wstring> argument, std::string & result); 
    179180  NSCAPI::nagiosReturn InjectSimpleCommand(const std::wstring command, const std::list<std::wstring> argument, std::wstring & message, std::wstring & perf); 
    180   NSCAPI::errorReturn NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring message, std::wstring perf); 
     181  NSCAPI::errorReturn NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::string result); 
    181182  NSCAPI::nagiosReturn InjectSplitAndCommand(const wchar_t* command, wchar_t* buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf); 
    182183  NSCAPI::nagiosReturn InjectSplitAndCommand(const std::wstring command, const std::wstring buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf, bool escape = false); 
     
    201202 
    202203 
     204  class SimpleNotificationHandler { 
     205  public: 
     206    NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, std::string result) { 
     207      try { 
     208        PluginCommand::ResponseMessage message; 
     209        message.ParseFromString(result); 
     210        if (message.payload_size() != 1) { 
     211          //NSC_LOG_ERROR_STD(_T("Unsupported payload size: ") + to_wstring(request_message.payload_size())); 
     212          return NSCAPI::returnIgnored; 
     213        } 
     214 
     215        ::PluginCommand::Response payload = message.payload().Get(0); 
     216        std::list<std::wstring> args; 
     217        for (int i=0;i<payload.arguments_size();i++) { 
     218          args.push_back(to_wstring(payload.arguments(i))); 
     219        } 
     220        std::wstring msg = to_wstring(payload.message()), perf; 
     221        NSCAPI::nagiosReturn ret = handleSimpleNotification(channel, command, code, msg, perf); 
     222      } catch (std::exception &e) { 
     223        std::cout << "Failed to parse data from: " << strEx::strip_hex(result) << e.what() <<  std::endl;; 
     224      } catch (...) { 
     225        std::cout << "Failed to parse data from: " << strEx::strip_hex(result) << std::endl;; 
     226      } 
     227 
     228      return -1; 
     229    } 
     230    virtual NSCAPI::nagiosReturn handleSimpleNotification(const std::wstring channel, const std::wstring command, NSCAPI::nagiosReturn code, std::wstring msg, std::wstring perf) = 0; 
     231 
     232  }; 
     233 
    203234  class SimpleCommand { 
    204235 
  • trunk/include/filter_framework.hpp

    r246 r254  
    5151      static bool filter(std::wstring filter, std::wstring str) { 
    5252        return str == filter; 
     53      } 
     54    }; 
     55    struct not_string_filter { 
     56      static bool filter(std::wstring filter, std::wstring str) { 
     57        return !(str == filter); 
    5358      } 
    5459    }; 
  • trunk/include/nsc_module_wrapper.hpp

    r241 r254  
    2323  NSCAPI::boolReturn wrapHasCommandHandler(bool has); 
    2424  NSCAPI::boolReturn wrapHasMessageHandler(bool has); 
     25  NSCAPI::boolReturn wrapHasNotificationHandler(bool has); 
     26  NSCAPI::nagiosReturn wrapHandleNotification(NSCAPI::nagiosReturn retResult); 
    2527  int wrapUnloadModule(bool success); 
    2628  NSCAPI::nagiosReturn wrapHandleCommand(NSCAPI::nagiosReturn retResult, const std::string &reply, char **reply_buffer, unsigned int *size); 
     
    170172  extern NSCAPI::nagiosReturn NSHandleCommand(const wchar_t* command, const char* request_buffer, const unsigned int request_buffer_len, char** reply_buffer, unsigned int *reply_buffer_len) \ 
    171173  { \ 
    172     try { \ 
    173       std::string request(request_buffer, request_buffer_len), reply; \ 
    174       NSCAPI::nagiosReturn retCode = (&toObject)->handleRAWCommand(command, request, reply); \ 
    175       return NSCModuleWrapper::wrapHandleCommand(retCode, reply, reply_buffer, reply_buffer_len); \ 
    176     } catch (...) { \ 
    177       NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHandleCommand(...)")); \ 
    178       return NSCAPI::returnIgnored; \ 
     174  try { \ 
     175  std::string request(request_buffer, request_buffer_len), reply; \ 
     176  NSCAPI::nagiosReturn retCode = (&toObject)->handleRAWCommand(command, request, reply); \ 
     177  return NSCModuleWrapper::wrapHandleCommand(retCode, reply, reply_buffer, reply_buffer_len); \ 
     178    } catch (...) { \ 
     179    NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHandleCommand(...)")); \ 
     180    return NSCAPI::returnIgnored; \ 
    179181    } \ 
    180182  } \ 
    181183  extern NSCAPI::boolReturn NSHasCommandHandler() { \ 
    182     try { \ 
    183       return NSCModuleWrapper::wrapHasCommandHandler(toObject.hasCommandHandler()); \ 
    184     } catch (...) { \ 
    185       NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasCommandHandler(...)")); \ 
    186       return NSCAPI::isfalse; \ 
     184  try { \ 
     185  return NSCModuleWrapper::wrapHasCommandHandler(toObject.hasCommandHandler()); \ 
     186    } catch (...) { \ 
     187    NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasCommandHandler(...)")); \ 
     188    return NSCAPI::isfalse; \ 
    187189    } \ 
    188190  } 
    189191#define NSC_WRAPPERS_IGNORE_CMD_DEF() \ 
    190192  extern NSCAPI::nagiosReturn NSHandleCommand(const wchar_t* IN_cmd, const unsigned int IN_argsLen, wchar_t **IN_args, \ 
    191                   wchar_t *OUT_retBufMessage, unsigned int IN_retBufMessageLen, wchar_t *OUT_retBufPerf, unsigned int IN_retBufPerfLen) { \ 
    192     return NSCAPI::returnIgnored; \ 
     193  wchar_t *OUT_retBufMessage, unsigned int IN_retBufMessageLen, wchar_t *OUT_retBufPerf, unsigned int IN_retBufPerfLen) { \ 
     194  return NSCAPI::returnIgnored; \ 
    193195  } \ 
    194196  extern NSCAPI::boolReturn NSHasCommandHandler() { return NSCAPI::isfalse; } 
     197#define NSC_WRAPPERS_IGNORE_NOTIFICATION_DEF() \ 
     198  extern void NSHandleNotification(const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn, const char*, unsigned int) {} \ 
     199  extern NSCAPI::boolReturn NSHasNotificationHandler() { return NSCAPI::isfalse; } 
     200#define NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF(toObject) \ 
     201  extern NSCAPI::nagiosReturn NSHandleNotification(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, const char* result_buffer, unsigned int result_buffer_len) \ 
     202  { \ 
     203    try { \ 
     204      std::string result(result_buffer, result_buffer_len); \ 
     205      return NSCModuleWrapper::wrapHandleNotification((&toObject)->handleRAWNotification(channel, command, code, result)); \ 
     206    } catch (...) { \ 
     207      NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasNotificationHandler(...)")); \ 
     208      return NSCAPI::returnIgnored; \ 
     209    } \ 
     210  } \ 
     211  extern NSCAPI::boolReturn NSHasNotificationHandler() { \ 
     212    try { \ 
     213      return NSCModuleWrapper::wrapHasNotificationHandler(toObject.hasNotificationHandler()); \ 
     214    } catch (...) { \ 
     215      NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasNotificationHandler(...)")); \ 
     216      return NSCAPI::isfalse; \ 
     217    } \ 
     218  } 
    195219 
    196220 
  • trunk/include/socket_helpers.hpp

    r226 r254  
    115115    } 
    116116  }; 
     117 
     118  namespace io { 
     119    void set_result(boost::optional<boost::system::error_code>* a, boost::system::error_code b) { 
     120      a->reset(b); 
     121    }  
     122 
     123    template <typename AsyncReadStream, typename RawSocket, typename MutableBufferSequence> 
     124    void read_with_timeout(AsyncReadStream& sock, RawSocket& rawSocket, const MutableBufferSequence& buffers, boost::posix_time::time_duration duration) { 
     125      boost::optional<boost::system::error_code> timer_result; 
     126      boost::asio::deadline_timer timer(sock.io_service()); 
     127      timer.expires_from_now(duration); 
     128      timer.async_wait(boost::bind(set_result, &timer_result, _1)); 
     129 
     130      boost::optional<boost::system::error_code> read_result; 
     131      async_read(sock, buffers, boost::bind(set_result, &read_result, _1)); 
     132 
     133      sock.io_service().reset(); 
     134      while (sock.io_service().run_one()) { 
     135        if (read_result) 
     136          timer.cancel(); 
     137        else if (timer_result) 
     138          rawSocket.close(); 
     139      } 
     140 
     141      if (*read_result) 
     142        throw boost::system::system_error(*read_result); 
     143    }  
     144 
     145    template <typename AsyncWriteStream, typename RawSocket, typename MutableBufferSequence> 
     146    void write_with_timeout(AsyncWriteStream& sock, RawSocket& rawSocket, const MutableBufferSequence& buffers, boost::posix_time::time_duration duration) { 
     147      boost::optional<boost::system::error_code> timer_result; 
     148      boost::asio::deadline_timer timer(sock.io_service()); 
     149      timer.expires_from_now(duration); 
     150      timer.async_wait(boost::bind(set_result, &timer_result, _1)); 
     151 
     152      boost::optional<boost::system::error_code> read_result; 
     153      async_write(sock, buffers, boost::bind(set_result, &read_result, _1)); 
     154 
     155      sock.io_service().reset(); 
     156      while (sock.io_service().run_one()) { 
     157        if (read_result) 
     158          timer.cancel(); 
     159        else if (timer_result) 
     160          rawSocket.close(); 
     161      } 
     162 
     163      if (*read_result) 
     164        throw boost::system::system_error(*read_result); 
     165    } 
     166 
     167  } 
    117168} 
     169 
  • trunk/include/strEx.h

    r246 r254  
    7474    std::locale loc; 
    7575    for(unsigned int i= 0; i < arg.size(); ++i) 
    76       result += std::use_facet<std::ctype<char> >(loc).narrow(arg[i], 0); 
     76      result += std::use_facet<std::ctype<wchar_t> >(loc).narrow(arg[i], 0); 
    7777    return result; 
    7878  } 
     
    112112      if (c==0||c==7||c==10||c==11||c==12||c==13||c==127) 
    113113        ret.push_back(L'?'); 
     114      else 
     115        ret.push_back(c); 
     116    } 
     117    return ret; 
     118  } 
     119  inline std::string strip_hex(std::string str) { 
     120    std::string ret; ret.reserve(str.size()); 
     121    BOOST_FOREACH(char c, str) 
     122    { 
     123      if (c==0||c==7||c==10||c==11||c==12||c==13||c==127) 
     124        ret.push_back('?'); 
    114125      else 
    115126        ret.push_back(c); 
     
    404415    return value * smallest_unit; 
    405416  } 
     417  inline long stol_as_time_sec(std::wstring time, unsigned int smallest_unit = 1) { 
     418    long neg = 1; 
     419    if (time.length() > 1 && time[0] == L'-') 
     420      return -stoui_as_time_sec(time.substr(1), smallest_unit); 
     421    return stoui_as_time_sec(time, smallest_unit); 
     422  } 
    406423 
    407424  inline unsigned long long stoi64_as_time(std::wstring time, unsigned int smallest_unit = 1000) { 
     
    809826    return boost::lexical_cast<std::string>(arg) ; 
    810827  } 
    811   catch(boost::bad_lexical_cast& e) { 
     828  catch(...) { 
    812829    return ""; 
    813830  } 
     
    817834    return boost::lexical_cast<std::wstring>(arg) ; 
    818835  } 
    819   catch(boost::bad_lexical_cast& e) { 
     836  catch(...) { 
    820837    return _T(""); 
    821838  } 
  • trunk/modules/CheckExternalScripts/CheckExternalScripts.cpp

    r250 r254  
    3535 
    3636CheckExternalScripts gCheckExternalScripts; 
    37 #ifdef _WIN32 
    38 BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) 
    39 { 
    40   NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); 
    41   return TRUE; 
    42 } 
    43 #endif 
    4437 
    4538CheckExternalScripts::CheckExternalScripts() {} 
     
    174167 
    175168 
     169NSC_WRAP_DLL(); 
    176170NSC_WRAPPERS_MAIN_DEF(gCheckExternalScripts); 
    177171NSC_WRAPPERS_IGNORE_MSG_DEF(); 
  • trunk/modules/NRPEClient/CMakeLists.txt

    r244 r254  
    88  stdafx.cpp 
    99  "${TARGET}.cpp" 
    10   ${NSCP_INCLUDE_PATH}/nrpe/nrpepacket.cpp 
     10  ${NSCP_INCLUDE_PATH}/nrpe/nrpe_packet.cpp 
    1111 
    1212  ${NSCP_DEF_PLUGIN_CPP} 
     
    2020    "${TARGET}.h" 
    2121    "${TARGET}.def" 
    22     ${NSCP_INCLUDE_PATH}/nrpe/nrpepacket.hpp 
    2322    ${NSCP_INCLUDE_PATH}/socket_helpers.hpp 
     23    ${NSCP_INCLUDE_PATH}/nrpe/nrpe_packet.hpp 
     24    ${NSCP_INCLUDE_PATH}/nrpe/nrpe_socket.hpp 
     25    ${NSCP_INCLUDE_PATH}/swap_bytes.hpp 
    2426 
    2527    ${NSCP_DEF_PLUGIN_HPP} 
  • trunk/modules/NRPEClient/NRPEClient.cpp

    r250 r254  
    2929#include <boost/filesystem.hpp> 
    3030#include <strEx.h> 
     31#include <nrpe/nrpe_socket.hpp> 
    3132 
    3233 
     
    217218  } 
    218219} 
    219 /* 
    220 NRPEPacket NRPEClient::send_ssl(std::wstring host, int port, int timeout, NRPEPacket packet) 
    221 { 
    222 #ifndef USE_SSL 
    223   return send_nossl(host, port, timeout, packet); 
    224 #else 
    225    
    226   simpleSSL::Socket socket(true); 
    227   socket.connect(host, port); 
    228   NSC_DEBUG_MSG_STD(_T(">>>length: ") + strEx::itos(packet.getBufferLength())); 
    229   socket.sendAll(packet.getBuffer(), packet.getBufferLength()); 
    230   simpleSocket::DataBuffer buffer; 
    231   socket.readAll(buffer, packet.getBufferLength()); 
    232   NSC_DEBUG_MSG_STD(_T("<<<length: ") + strEx::itos(buffer.getLength())); 
    233   packet.readFrom(buffer.getBuffer(), buffer.getLength()); 
    234   return packet; 
    235    
    236  
    237   boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::sslv23); 
    238   ctx.use_tmp_dh_file("d:\\nrpe_512.pem"); 
    239   ctx.set_verify_mode(boost::asio::ssl::context::verify_peer); 
    240   nrpe_ssl_socket socket(io_service, ctx, host, port); 
    241   //socket. 
    242  
    243 #endif 
    244 } 
    245 */ 
    246 using boost::asio::ip::tcp; 
    247  
    248 void set_result(boost::optional<boost::system::error_code>* a, boost::system::error_code b) 
    249 { 
    250   a->reset(b); 
    251 }  
    252 template <typename AsyncReadStream, typename RawSocket, typename MutableBufferSequence> 
    253 void read_with_timeout(AsyncReadStream& sock, RawSocket& rawSocket, const MutableBufferSequence& buffers, boost::posix_time::time_duration duration) 
    254 { 
    255   boost::optional<boost::system::error_code> timer_result; 
    256   boost::asio::deadline_timer timer(sock.io_service()); 
    257   timer.expires_from_now(duration); 
    258   timer.async_wait(boost::bind(set_result, &timer_result, _1)); 
    259  
    260   boost::optional<boost::system::error_code> read_result; 
    261   async_read(sock, buffers, boost::bind(set_result, &read_result, _1)); 
    262  
    263   sock.io_service().reset(); 
    264   while (sock.io_service().run_one()) 
    265   { 
    266     if (read_result) 
    267       timer.cancel(); 
    268     else if (timer_result) 
    269       rawSocket.close(); 
    270   } 
    271  
    272   if (*read_result) 
    273     throw boost::system::system_error(*read_result); 
    274 }  
    275  
    276 template <typename AsyncWriteStream, typename RawSocket, typename MutableBufferSequence> 
    277 void write_with_timeout(AsyncWriteStream& sock, RawSocket& rawSocket, const MutableBufferSequence& buffers, boost::posix_time::time_duration duration) 
    278 { 
    279   boost::optional<boost::system::error_code> timer_result; 
    280   boost::asio::deadline_timer timer(sock.io_service()); 
    281   timer.expires_from_now(duration); 
    282   timer.async_wait(boost::bind(set_result, &timer_result, _1)); 
    283  
    284   boost::optional<boost::system::error_code> read_result; 
    285   async_write(sock, buffers, boost::bind(set_result, &read_result, _1)); 
    286  
    287   sock.io_service().reset(); 
    288   while (sock.io_service().run_one()) 
    289   { 
    290     if (read_result) 
    291       timer.cancel(); 
    292     else if (timer_result) 
    293       rawSocket.close(); 
    294   } 
    295  
    296   if (*read_result) 
    297     throw boost::system::system_error(*read_result); 
    298 } 
    299  
    300 class nrpe_socket : public boost::noncopyable { 
    301 public: 
    302   tcp::socket socket_; 
    303  
    304 public: 
    305   nrpe_socket(boost::asio::io_service &io_service, std::wstring host, int port) : socket_(io_service) { 
    306     tcp::resolver resolver(io_service); 
    307     tcp::resolver::query query(to_string(host), to_string(port)); 
    308     //tcp::resolver::query query("www.medin.name", "80"); 
    309     //tcp::resolver::query query("test_server", "80"); 
    310  
    311     tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); 
    312     tcp::resolver::iterator end; 
    313  
    314     boost::system::error_code error = boost::asio::error::host_not_found; 
    315     while (error && endpoint_iterator != end) 
    316     { 
    317       tcp::resolver::endpoint_type ep = *endpoint_iterator; 
    318       NSC_DEBUG_MSG_STD(_T("Attempting to connect to: ") + to_wstring(ep.address().to_string()) + _T(":") + to_wstring(ep.port())); 
    319       socket_.close(); 
    320       socket_.connect(*endpoint_iterator++, error); 
    321     } 
    322     if (error) 
    323       throw boost::system::system_error(error); 
    324   } 
    325   ~nrpe_socket() { 
    326     socket_.close(); 
    327   } 
    328  
    329   void send(nrpe::packet &packet, boost::posix_time::seconds timeout) { 
    330     std::vector<char> buf(packet.get_packet_length()); 
    331     write_with_timeout(socket_, socket_, boost::asio::buffer(packet.create_buffer(), packet.get_packet_length()), timeout); 
    332   } 
    333   nrpe::packet recv(const nrpe::packet &packet, boost::posix_time::seconds timeout) { 
    334     std::vector<char> buf(packet.get_packet_length()); 
    335     std::cout <<  "About to read: " << buf.size() << std::endl; 
    336     read_with_timeout(socket_, socket_, boost::asio::buffer(buf), timeout); 
    337     std::cout <<  "Read data: " << buf.size() << std::endl; 
    338     return nrpe::packet(&buf[0], buf.size(), packet.get_payload_length()); 
    339   } 
    340 }; 
     220 
     221 
    341222#ifdef USE_SSL 
    342  
    343 class nrpe_ssl_socket { 
    344  
    345 private: 
    346   boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_; 
    347 public: 
    348   nrpe_ssl_socket(boost::asio::io_service &io_service, boost::asio::ssl::context &ctx, std::wstring host, int port) : socket_(io_service, ctx) { 
    349     NSC_LOG_CRITICAL(_T("Looking up...")); 
    350     tcp::resolver resolver(io_service); 
    351     tcp::resolver::query query(to_string(host), to_string(port)); 
    352     //tcp::resolver::query query("www.medin.name", "80"); 
    353     //tcp::resolver::query query("test_server", "80"); 
    354  
    355     tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); 
    356     tcp::resolver::iterator end; 
    357  
    358     boost::system::error_code error = boost::asio::error::host_not_found; 
    359     NSC_LOG_CRITICAL(_T("Connecting...")); 
    360     while (error && endpoint_iterator != end) 
    361     { 
    362       tcp::resolver::endpoint_type ep = *endpoint_iterator; 
    363       NSC_DEBUG_MSG_STD(_T("Attempting to connect to: ") + to_wstring(ep.address().to_string()) + _T(":") + to_wstring(ep.port())); 
    364       socket_.lowest_layer().close(); 
    365       socket_.lowest_layer().connect(*endpoint_iterator++, error); 
    366     } 
    367     if (error) 
    368       throw boost::system::system_error(error); 
    369     NSC_LOG_CRITICAL(_T("Connected...")); 
    370  
    371     NSC_LOG_CRITICAL(_T("Handshaking...")); 
    372     //socket_.handshake(boost::asio::ssl::stream_base::client); 
    373     socket_.handshake(boost::asio::ssl::stream_base::client); 
    374     NSC_LOG_CRITICAL(_T("Handshook...") + strEx::itos(error.value())); 
    375  
    376   } 
    377  
    378   void send(nrpe::packet &packet, boost::posix_time::seconds timeout) { 
    379     std::vector<char> buf(packet.get_packet_length()); 
    380     write_with_timeout(socket_, socket_.lowest_layer(), boost::asio::buffer(packet.create_buffer(), packet.get_packet_length()), timeout); 
    381   } 
    382   nrpe::packet recv(const nrpe::packet &packet, boost::posix_time::seconds timeout) { 
    383     std::vector<char> buf(packet.get_packet_length()); 
    384     read_with_timeout(socket_, socket_.lowest_layer(), boost::asio::buffer(buf), timeout); 
    385     return nrpe::packet(&buf[0], buf.size(), packet.get_payload_length()); 
    386   } 
    387 }; 
    388 nrpe::packet NRPEClient::send_ssl(std::wstring host, int port, int timeout, nrpe::packet packet) 
    389 { 
     223nrpe::packet NRPEClient::send_ssl(std::wstring host, int port, int timeout, nrpe::packet packet) { 
    390224  boost::asio::io_service io_service; 
    391225  boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::sslv23); 
     
    393227  ctx.use_tmp_dh_file(to_string(cert_)); 
    394228  ctx.set_verify_mode(boost::asio::ssl::context::verify_none); 
    395   nrpe_ssl_socket socket(io_service, ctx, host, port); 
     229  nrpe::ssl_socket socket(io_service, ctx, host, port); 
    396230  socket.send(packet, boost::posix_time::seconds(timeout)); 
    397231  return socket.recv(packet, boost::posix_time::seconds(timeout)); 
     
    399233#endif 
    400234 
    401 nrpe::packet NRPEClient::send_nossl(std::wstring host, int port, int timeout, nrpe::packet packet) 
    402 { 
     235nrpe::packet NRPEClient::send_nossl(std::wstring host, int port, int timeout, nrpe::packet packet) { 
    403236  boost::asio::io_service io_service; 
    404   nrpe_socket socket(io_service, host, port); 
     237  nrpe::socket socket(io_service, host, port); 
    405238  socket.send(packet, boost::posix_time::seconds(timeout)); 
    406239  return socket.recv(packet, boost::posix_time::seconds(timeout)); 
  • trunk/modules/NRPEClient/NRPEClient.h

    r250 r254  
    2222NSC_WRAPPERS_MAIN(); 
    2323#include <map> 
    24 #include <nrpe/nrpepacket.hpp> 
     24#include <nrpe/nrpe_packet.hpp> 
    2525 
    2626 
     
    127127  nrpe::packet send_ssl(std::wstring host, int port, int timeout, nrpe::packet packet); 
    128128  void add_options(po::options_description &desc, nrpe_connection_data &command_data); 
    129 #ifdef USE_BOOST 
    130 #endif 
    131129 
    132130private: 
  • trunk/modules/NRPEServer/CMakeLists.txt

    r244 r254  
    1414  nrpe_connection.cpp 
    1515  nrpe_handler.cpp 
    16   ${NSCP_INCLUDE_PATH}/nrpe/nrpepacket.cpp 
     16  ${NSCP_INCLUDE_PATH}/nrpe/nrpe_packet.cpp 
    1717 
    1818  ${NSCP_DEF_PLUGIN_CPP} 
     
    3030    nrpe_handler.hpp 
    3131    nrpe_parser.hpp 
    32     ${NSCP_INCLUDE_PATH}/nrpe/nrpepacket.hpp 
    3332    ${NSCP_INCLUDE_PATH}/socket_helpers.hpp 
     33    ${NSCP_INCLUDE_PATH}/nrpe/nrpe_packet.hpp 
     34    ${NSCP_INCLUDE_PATH}/swap_bytes.hpp 
    3435 
    3536    ${NSCP_DEF_PLUGIN_HPP} 
  • trunk/modules/NRPEServer/nrpe_connection.cpp

    r226 r254  
    4141          boost::tie(result, begin) = parser_.digest(begin, end); 
    4242          if (begin == old_begin) { 
    43             std::cout << "Whoops, this is wrong..."; 
     43            handler_.create_error(_T("Something strange happened...")); 
    4444            return; 
    4545          } 
  • trunk/modules/NRPEServer/nrpe_handler.hpp

    r226 r254  
    1 #include <nrpe/nrpepacket.hpp> 
     1#include <nrpe/nrpe_packet.hpp> 
    22#include <boost/tuple/tuple.hpp> 
    33 
  • trunk/modules/NRPEServer/nrpe_parser.hpp

    r224 r254  
    1 #include <nrpe/nrpepacket.hpp> 
     1#include <nrpe/nrpe_packet.hpp> 
    22#include <boost/tuple/tuple.hpp> 
    33#include <boost/noncopyable.hpp> 
     
    1313        : payload_length_(payload_length) 
    1414        , packet_length_(nrpe::length::get_packet_length(payload_length)) 
    15       { 
    16         std::cout << "== ctor length: " << payload_length_ << "/" << packet_length_ << std::endl; 
    17       } 
     15      {} 
    1816 
    1917      template <typename InputIterator> 
    2018      boost::tuple<bool, InputIterator> digest(InputIterator begin, InputIterator end) { 
    21         std::cout << "== Parsing: " << payload_length_ << "/" << packet_length_ << std::endl; 
    2219        int count = packet_length_ - buffer_.size(); 
    23         std::cout << "Bytes already: " << buffer_.size() << ", need more: " << count << " got: " << (end-begin) << std::endl; 
    2420        for (; count > 0&& begin != end; ++begin, --count) 
    2521          buffer_.push_back(*begin); 
     
    3531        payload_length_ = length; 
    3632        packet_length_ = nrpe::length::get_packet_length(length); 
    37         std::cout << "== Setting length: " << payload_length_ << "/" << packet_length_ << std::endl; 
    3833      } 
    3934    }; 
  • trunk/modules/NRPEServer/stdafx.h

    r238 r254  
    4848#include <NSCHelper.h> 
    4949#include <nsc_module_wrapper.hpp> 
    50 #include <nrpe/nrpepacket.hpp> 
     50#include <nrpe/nrpe_packet.hpp> 
    5151 
  • trunk/modules/NSCAAgent/NSCAAgent.cpp

    r202 r254  
    2525#include <list> 
    2626#include <string> 
     27#include <nsca/nsca_enrypt.hpp> 
     28#include <nsca/nsca_packet.hpp> 
     29#include <nsca/nsca_socket.hpp> 
    2730 
    2831NSCAAgent gNSCAAgent; 
     
    6366    SETTINGS_REG_PATH(nsca::CMD_SECTION); 
    6467 
    65     SETTINGS_REG_KEY_I(nsca::INTERVAL); 
    6668    SETTINGS_REG_KEY_S(nsca::HOSTNAME); 
    6769    SETTINGS_REG_KEY_S(nsca::SERVER_HOST); 
     
    6971    SETTINGS_REG_KEY_I(nsca::ENCRYPTION); 
    7072    SETTINGS_REG_KEY_S(nsca::PASSWORD); 
    71     SETTINGS_REG_KEY_I(nsca::THREADS); 
    7273    SETTINGS_REG_KEY_B(nsca::CACHE_HOST); 
     74 
     75    hostname_ = to_string(SETTINGS_GET_STRING(nsca::HOSTNAME)); 
     76    nscahost_ = SETTINGS_GET_STRING(nsca::SERVER_HOST); 
     77    nscaport_ = SETTINGS_GET_INT(nsca::SERVER_PORT); 
     78 
     79    encryption_method_ = SETTINGS_GET_INT(nsca::ENCRYPTION); 
     80    password_ = strEx::wstring_to_string(SETTINGS_GET_STRING(nsca::PASSWORD)); 
     81    cacheNscaHost_ = SETTINGS_GET_INT(nsca::CACHE_HOST); 
     82    timeout_ = SETTINGS_GET_INT(nsca::READ_TIMEOUT); 
     83    payload_length_ = SETTINGS_GET_INT(nsca::PAYLOAD_LENGTH); 
     84    time_delta_ = strEx::stol_as_time_sec(SETTINGS_GET_STRING(nsca::TIME_DELTA_DEFAULT), 1); 
    7385 
    7486 
     
    7991  } 
    8092 
    81   if (mode == NSCAPI::normalStart) { 
    82     int e_threads = SETTINGS_GET_INT(nsca::THREADS); 
    8393 
    84     for (int i=0;i<e_threads;i++) { 
    85       std::wstring id = _T("nsca_t_") + strEx::itos(i); 
    86       NSCAThreadImpl *thread = new NSCAThreadImpl(id); 
    87       extra_threads.push_back(thread); 
    88     } 
    89     for (std::list<NSCAThreadImpl*>::const_iterator cit=extra_threads.begin();cit != extra_threads.end(); ++cit) { 
    90       (*cit)->createThread(reinterpret_cast<LPVOID>(rand())); 
    91     } 
    92   } 
    93    
    9494  return true; 
    9595} 
     
    100100 */ 
    101101bool NSCAAgent::unloadModule() { 
    102   /* 
    103   if (!pdhThread.exitThread(20000)) { 
    104     std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl; 
    105     NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result...")); 
    106   } 
    107   */ 
    108   for (std::list<NSCAThreadImpl*>::iterator it=extra_threads.begin();it != extra_threads.end(); ++it) { 
    109     if (!(*it)->exitThread(20000)) { 
    110       std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl; 
    111       NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result...")); 
    112     } 
    113   } 
    114102  return true; 
    115 } 
    116 /** 
    117  * Check if we have a command handler. 
    118  * @return true (as we have a command handler) 
    119  */ 
    120 bool NSCAAgent::hasCommandHandler() { 
    121   return false; 
    122 } 
    123 /** 
    124  * Check if we have a message handler. 
    125  * @return false as we have no message handler 
    126  */ 
    127 bool NSCAAgent::hasMessageHandler() { 
    128   return false; 
    129 } 
    130  
    131 int NSCAAgent::commandLineExec(const TCHAR* command, const unsigned int argLen, TCHAR** args) { 
    132   return -1; 
    133 } 
    134  
    135  
    136 /** 
    137  * Main command parser and delegator. 
    138  * This also handles a lot of the simpler responses (though some are deferred to other helper functions) 
    139  * 
    140  * 
    141  * @param command  
    142  * @param argLen  
    143  * @param **args  
    144  * @return  
    145  */ 
    146 NSCAPI::nagiosReturn NSCAAgent::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) { 
    147   return NSCAPI::returnIgnored; 
    148103} 
    149104std::wstring NSCAAgent::getCryptos() { 
    150105  std::wstring ret = _T("{"); 
    151106  for (int i=0;i<LAST_ENCRYPTION_ID;i++) { 
    152     if (nsca_encrypt::hasEncryption(i)) { 
     107    if (nsca::nsca_encrypt::hasEncryption(i)) { 
    153108      std::wstring name; 
    154109      try { 
    155         nsca_encrypt::any_encryption *core = nsca_encrypt::get_encryption_core(i); 
     110        nsca::nsca_encrypt::any_encryption *core = nsca::nsca_encrypt::get_encryption_core(i); 
    156111        if (core == NULL) 
    157112          name = _T("Broken<NULL>"); 
    158113        else 
    159114          name = core->getName(); 
    160       } catch (nsca_encrypt::encryption_exception &e) { 
     115      } catch (nsca::nsca_encrypt::encryption_exception &e) { 
    161116        name = e.getMessage(); 
    162117      } 
     
    169124} 
    170125 
     126NSCAPI::nagiosReturn NSCAAgent::handleSimpleNotification(const std::wstring channel, const std::wstring command, NSCAPI::nagiosReturn code, std::wstring msg, std::wstring perf) { 
     127  try { 
     128    NSC_DEBUG_MSG_STD(_T("* * *NSCA * * * Handling command: ") + command); 
     129    boost::asio::io_service io_service; 
     130    NSC_DEBUG_MSG_STD(_T("* * *NSCA * * * message: ") + msg); 
     131    nsca::socket socket(io_service); 
     132    socket.connect(nscahost_, nscaport_); 
     133    nsca::packet packet(hostname_, payload_length_, time_delta_); 
     134    packet.code = code; 
     135    packet.host = "hello"; 
     136    packet.result = to_string(msg); 
     137    socket.recv_iv(password_, encryption_method_, boost::posix_time::seconds(timeout_)); 
     138    socket.send_nsca(packet, boost::posix_time::seconds(timeout_)); 
     139    return 1; 
     140  } catch (std::exception &e) { 
     141    NSC_LOG_ERROR_STD(_T("Failed to send data: ") + to_wstring(e.what())); 
     142  } catch (...) { 
     143    NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 
     144  } 
     145} 
     146 
    171147 
    172148NSC_WRAPPERS_MAIN_DEF(gNSCAAgent); 
    173149NSC_WRAPPERS_IGNORE_MSG_DEF(); 
    174 NSC_WRAPPERS_HANDLE_CMD_DEF(gNSCAAgent); 
    175 NSC_WRAPPERS_HANDLE_CONFIGURATION(gNSCAAgent); 
    176 NSC_WRAPPERS_CLI_DEF(gNSCAAgent); 
    177  
    178  
    179  
    180 MODULE_SETTINGS_START(NSCAAgent, _T("NRPE Listener configuration"), _T("..."))  
    181  
    182 PAGE(_T("NRPE Listsner configuration"))  
    183  
    184 ITEM_EDIT_TEXT(_T("port"), _T("This is the port the NRPEListener.dll will listen to."))  
    185 ITEM_MAP_TO(_T("basic_ini_text_mapper"))  
    186 OPTION(_T("section"), _T("NRPE"))  
    187 OPTION(_T("key"), _T("port"))  
    188 OPTION(_T("default"), _T("5666"))  
    189 ITEM_END() 
    190  
    191 ITEM_CHECK_BOOL(_T("allow_arguments"), _T("This option determines whether or not the NRPE daemon will allow clients to specify arguments to commands that are executed."))  
    192 ITEM_MAP_TO(_T("basic_ini_bool_mapper"))  
    193 OPTION(_T("section"), _T("NRPE"))  
    194 OPTION(_T("key"), _T("allow_arguments"))  
    195 OPTION(_T("default"), _T("false"))  
    196 OPTION(_T("true_value"), _T("1"))  
    197 OPTION(_T("false_value"), _T("0"))  
    198 ITEM_END() 
    199  
    200 ITEM_CHECK_BOOL(_T("allow_nasty_meta_chars"), _T("This might have security implications (depending on what you do with the options)"))  
    201 ITEM_MAP_TO(_T("basic_ini_bool_mapper"))  
    202 OPTION(_T("section"), _T("NRPE"))  
    203 OPTION(_T("key"), _T("allow_nasty_meta_chars"))  
    204 OPTION(_T("default"), _T("false"))  
    205 OPTION(_T("true_value"), _T("1"))  
    206 OPTION(_T("false_value"), _T("0"))  
    207 ITEM_END() 
    208  
    209 ITEM_CHECK_BOOL(_T("use_ssl"), _T("This option will enable SSL encryption on the NRPE data socket (this increases security somwhat."))  
    210 ITEM_MAP_TO(_T("basic_ini_bool_mapper"))  
    211 OPTION(_T("section"), _T("NRPE"))  
    212 OPTION(_T("key"), _T("use_ssl"))  
    213 OPTION(_T("default"), _T("true"))  
    214 OPTION(_T("true_value"), _T("1"))  
    215 OPTION(_T("false_value"), _T("0"))  
    216 ITEM_END() 
    217  
    218 PAGE_END() 
    219 ADVANCED_PAGE(_T("Access configuration"))  
    220  
    221 ITEM_EDIT_OPTIONAL_LIST(_T("Allow connection from:"), _T("This is the hosts that will be allowed to poll performance data from the NRPE server."))  
    222 OPTION(_T("disabledCaption"), _T("Use global settings (defined previously)"))  
    223 OPTION(_T("enabledCaption"), _T("Specify hosts for NRPE server"))  
    224 OPTION(_T("listCaption"), _T("Add all IP addresses (not hosts) which should be able to connect:"))  
    225 OPTION(_T("separator"), _T(","))  
    226 OPTION(_T("disabled"), _T(""))  
    227 ITEM_MAP_TO(_T("basic_ini_text_mapper"))  
    228 OPTION(_T("section"), _T("NRPE"))  
    229 OPTION(_T("key"), _T("allowed_hosts"))  
    230 OPTION(_T("default"), _T(""))  
    231 ITEM_END() 
    232  
    233 PAGE_END() 
    234 MODULE_SETTINGS_END() 
     150NSC_WRAPPERS_IGNORE_CMD_DEF(); 
     151NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF(gNSCAAgent); 
  • trunk/modules/NSCAAgent/NSCAAgent.def

    r87 r254  
    1111  NSHandleCommand 
    1212  NSUnloadModule 
    13   NSGetConfigurationMeta 
    1413  NSGetModuleDescription 
    15   NSCommandLineExec 
     14  NSDeleteBuffer 
     15  NSHandleNotification 
     16  NSHasNotificationHandler 
  • trunk/modules/NSCAAgent/NSCAAgent.h

    r201 r254  
    2121#pragma once 
    2222 
    23 #include "NSCAThread.h" 
    2423#include <CheckMemory.h> 
    2524 
    2625NSC_WRAPPERS_MAIN(); 
    27 NSC_WRAPPERS_CLI(); 
    2826 
    29 class NSCAAgent { 
     27class NSCAAgent : public NSCModuleHelper::SimpleNotificationHandler { 
    3028private: 
    31   CheckMemory memoryChecker; 
    32   int processMethod_; 
    33 //  NSCAThreadImpl pdhThread; 
    34   std::list<NSCAThreadImpl*> extra_threads; 
    3529 
    36 public: 
     30  std::string hostname_; 
     31  std::wstring nscahost_; 
     32  unsigned int nscaport_; 
     33  unsigned int payload_length_; 
     34  bool cacheNscaHost_; 
     35  std::string password_; 
     36  int encryption_method_; 
     37  unsigned int timeout_; 
     38  int time_delta_; 
    3739 
    3840public: 
     
    6668    return std::wstring(_T("Passive check support (needs NSCA on nagios server).\nAvalible crypto are: ")) + getCryptos(); 
    6769  } 
    68  
    69   bool hasCommandHandler(); 
    70   bool hasMessageHandler(); 
    71   NSCAPI::nagiosReturn handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf); 
    72   int commandLineExec(const TCHAR* command, const unsigned int argLen, TCHAR** args); 
     70  bool hasNotificationHandler() { return true; } 
    7371 
    7472  std::wstring getCryptos(); 
    7573 
     74  NSCAPI::nagiosReturn handleSimpleNotification(const std::wstring channel, const std::wstring command, NSCAPI::nagiosReturn code, std::wstring msg, std::wstring perf); 
     75 
     76 
    7677}; 
  • trunk/modules/NSCAAgent/NSCAThread.cpp

    r202 r254  
    9090  read_timeout_ = SETTINGS_GET_INT(nsca::READ_TIMEOUT); 
    9191 
    92   std::list<std::wstring> items = NSCModuleHelper::getSettingsSection(settings::nsca::CMD_SECTION_TITLE); 
     92  std::list<std::wstring> items = NSCModuleHelper::getSettingsSection(setting_keys::nsca::CMD_SECTION_TITLE); 
    9393  for (std::list<std::wstring>::const_iterator cit = items.begin(); cit != items.end(); ++cit) { 
    9494    addCommand(*cit); 
     
    118118  std::wstring msg; 
    119119  std::wstring perf; 
    120   NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectCommand(cmd_.c_str(), args_.getLen(), args_.get(), msg, perf); 
     120  NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectSimpleCommand(cmd_.c_str(), args_, msg, perf); 
    121121  result.service = alias_; 
    122122  if (ret == NSCAPI::returnIgnored) { 
     
    139139 
    140140void NSCAThread::addCommand(std::wstring key) { 
    141   std::wstring value = NSCModuleHelper::getSettingsString(settings::nsca::CMD_SECTION_PATH, key, _T("")); 
     141  std::wstring value = NSCModuleHelper::getSettingsString(setting_keys::nsca::CMD_SECTION_PATH, key, _T("")); 
    142142  if ((key.length() > 4) && (key.substr(0,4) == _T("host"))) 
    143143    commands_.push_back(Command(_T(""), value)); 
  • trunk/modules/NSCAAgent/NSCAThread.h

    r201 r254  
    2424#include <Mutex.h> 
    2525#include <arrayBuffer.h> 
    26 #include <Socket.h> 
    2726#include "nsca_enrypt.hpp" 
    2827 
     
    9392 
    9493 
    95 class Arguments { 
    96   arrayBuffer::arrayBuffer arglist_; 
    97   unsigned int arglen_; 
    98 public: 
    99   Arguments() : arglist_(NULL), arglen_(0) {} 
    100   ~Arguments() { 
    101     arrayBuffer::destroyArrayBuffer(arglist_, arglen_); 
    102     arglist_ = NULL; 
    103   } 
    104   Arguments(const Arguments& other) : arglist_(NULL), arglen_(0) { 
    105     arglen_ = other.getLen(); 
    106     arglist_ = arrayBuffer::copy(other.get(), other.getLen()); 
    107   } 
    108   Arguments& operator=(Arguments& other) { 
    109     if (this != &other){ 
    110       arglen_ = other.getLen(); 
    111       arglist_ = other.detach(); 
    112     } 
    113     return *this; 
    114   } 
    115   Arguments& operator=(const Arguments& other) { 
    116     if (this != &other){ 
    117       arglen_ = other.getLen(); 
    118       arglist_ = arrayBuffer::copy(other.get(), other.getLen()); 
    119     } 
    120     return *this; 
    121   } 
    122   Arguments(const std::wstring &other) : arglist_(NULL), arglen_(0)  { 
    123     arglist_ = arrayBuffer::split2arrayBuffer(other, ' ', arglen_, true); 
    124   } 
    125   arrayBuffer::arrayBuffer detach() { 
    126     arrayBuffer::arrayBuffer ret = arglist_; 
    127     arglist_ = NULL; 
    128     arglen_ = 0; 
    129     return ret; 
    130   } 
    131   const arrayBuffer::arrayBuffer get() const { 
    132     return arglist_; 
    133   } 
    134   unsigned int getLen() const { 
    135     return arglen_; 
    136   } 
    137 }; 
     94 
    13895class Command { 
    13996  std::wstring cmd_; 
    14097  std::wstring alias_; 
    141   Arguments args_; 
     98  std::list<std::wstring> args_; 
    14299public: 
    143100 
     
    160117    } 
    161118 
    162     simpleSocket::DataBuffer getBuffer(nsca_encrypt &crypt_inst, __time32_t time_delta, unsigned int pluginoutput_length) const { 
     119    std::string getBuffer(nsca_encrypt &crypt_inst, __time32_t time_delta, unsigned int pluginoutput_length) const { 
     120      std::string ret; ret.resize(buffer_len); 
    163121      std::string s = strEx::wstring_to_string(service); 
    164122      std::string r = strEx::wstring_to_string(result); 
     
    203161    strEx::token token = strEx::getToken(raw, ' '); 
    204162    cmd_ = token.first; 
    205     args_ = token.second; 
     163    args_.push_back(token.second); 
    206164  } 
    207165  static unsigned int conv_code(NSCAPI::nagiosReturn ret) { 
  • trunk/modules/NSCAAgent/stdafx.h

    r207 r254  
    2121#pragma once 
    2222 
    23 #define WIN32_LEAN_AND_MEAN   // Exclude rarely-used stuff from Windows headers 
    24 // Windows Header Files: 
    25 #include <windows.h> 
    26  
    27 #define COMPILE_NEWAPIS_STUBS 
    2823#define WANT_GETLONGPATHNAME_WRAPPER 
    2924//#include <NewAPIs.h> 
     
    3429#include <hash_map> 
    3530#include <list> 
     31 
     32 
     33#include <boost/asio.hpp> 
     34#ifdef USE_SSL 
     35#include <boost/asio/ssl.hpp> 
     36#endif 
     37 
    3638#include <NSCAPI.h> 
    3739#include <NSCHelper.h> 
     
    4143#include <error.hpp> 
    4244 
     45 
    4346#ifdef MEMCHECK 
    4447#include <vld.h> 
  • trunk/modules/Scheduler/Scheduler.cpp

    r245 r254  
    123123void Scheduler::handle_schedule(scheduler::target item) { 
    124124  try { 
    125     std::wstring msg, perf; 
    126     NSCAPI::nagiosReturn code = NSCModuleHelper::InjectSimpleCommand(item.command.c_str(), item.arguments, msg, perf); 
     125    std::string response; 
     126    NSCAPI::nagiosReturn code = NSCModuleHelper::InjectCommand(item.command.c_str(), item.arguments, response); 
    127127    if (NSCHelper::report::matches(item.report, code)) { 
    128       NSCModuleHelper::NotifyChannel(item.channel, item.alias, code, msg, perf); 
     128      NSCModuleHelper::NotifyChannel(item.channel, item.alias, code, response); 
    129129    } 
    130130  } catch (NSCModuleHelper::NSCMHExcpetion &e) { 
  • trunk/service/CMakeLists.txt

    r242 r254  
    4040     
    4141    commands.hpp 
     42    channels.hpp 
    4243    logger.hpp 
    4344    settings_logger_impl.hpp 
  • trunk/service/NSCPlugin.cpp

    r244 r254  
    4848  ,fShowTray(NULL) 
    4949  ,fHideTray(NULL) 
     50  ,fHasNotificationHandler(NULL) 
     51  ,fHandleNotification(NULL) 
    5052  ,bLoaded_(false) 
    5153  ,lastIsMsgPlugin_(false) 
     
    206208  } 
    207209} 
     210bool NSCPlugin::hasNotificationHandler() { 
     211  if (!isLoaded()) 
     212    throw NSPluginException(module_, _T("Module not loaded")); 
     213  if (!fHasNotificationHandler) 
     214    return false; 
     215  try { 
     216    if (fHasNotificationHandler()) { 
     217      return true; 
     218    } 
     219    return false; 
     220  } catch (...) { 
     221    throw NSPluginException(module_, _T("Unhandled exception in hasMessageHandler.")); 
     222  } 
     223} 
    208224/** 
    209225 * Allow for the plug in to handle a command from the input core. 
     
    231247} 
    232248 
     249bool NSCPlugin::handleNotification(const wchar_t *channel, const wchar_t* command, NSCAPI::nagiosReturn code, char* result, unsigned int result_len) { 
     250  if (!isLoaded() || fHandleNotification == NULL) 
     251    throw NSPluginException(module_, _T("Library is not loaded")); 
     252  try { 
     253    return fHandleNotification(channel, command, code, result, result_len); 
     254  } catch (...) { 
     255    throw NSPluginException(module_, _T("Unhandled exception in handleCommand.")); 
     256  } 
     257} 
     258 
     259 
    233260 
    234261void NSCPlugin::deleteBuffer(char** buffer) { 
     
    386413    fGetConfigurationMeta = (lpGetConfigurationMeta)module_.load_proc("NSGetConfigurationMeta"); 
    387414    fCommandLineExec = (lpCommandLineExec)module_.load_proc("NSCommandLineExec"); 
    388  
     415    fHandleNotification = (lpHandleNotification)module_.load_proc("NSHandleNotification"); 
     416    fHasNotificationHandler = (lpHasNotificationHandler)module_.load_proc("NSHasNotificationHandler"); 
     417     
    389418    fShowTray = (lpShowTray)module_.load_proc("ShowIcon"); 
    390419    fHideTray = (lpHideTray)module_.load_proc("HideIcon"); 
  • trunk/service/NSCPlugin.h

    r241 r254  
    126126  typedef void (*lpShowTray)(); 
    127127  typedef void (*lpHideTray)(); 
     128  typedef int (*lpHasNotificationHandler)(); 
     129  typedef int (*lpHandleNotification)(const wchar_t *channel, const wchar_t* command, NSCAPI::nagiosReturn code, char* result, unsigned int result_len); 
    128130 
    129131 
     
    143145  lpShowTray fShowTray; 
    144146  lpHideTray fHideTray; 
     147  lpHasNotificationHandler fHasNotificationHandler; 
     148  lpHandleNotification fHandleNotification; 
    145149 
    146150public: 
     
    157161  bool getVersion(int *major, int *minor, int *revision); 
    158162  bool hasCommandHandler(void); 
     163  bool hasNotificationHandler(void); 
    159164  bool hasMessageHandler(void); 
    160165  NSCAPI::nagiosReturn handleCommand(const wchar_t *command, const char* dataBuffer, const unsigned int dataBuffer_len, char** returnBuffer, unsigned int *returnBuffer_len); 
    161166  NSCAPI::nagiosReturn handleCommand(const wchar_t* command, std::string &request, std::string &reply); 
     167  bool handleNotification(const wchar_t *channel, const wchar_t* command, NSCAPI::nagiosReturn code, char* result, unsigned int result_len); 
    162168  void deleteBuffer(char**buffer); 
    163169  void handleMessage(int msgType, const wchar_t* file, const int line, const wchar_t *message); 
  • trunk/service/NSClient++.cpp

    r245 r254  
    872872      return; 
    873873    } 
    874     commandHandlers_.clear(); 
    875874    if (unloadLoggers) 
    876875      messageHandlers_.clear(); 
     
    989988    plugins_.insert(plugins_.end(), plugin); 
    990989    commands_.add_plugin(plugin); 
    991     if (plugin->hasCommandHandler()) 
    992       commandHandlers_.insert(commandHandlers_.end(), plugin); 
     990    channels_.add_plugin(plugin); 
     991    if (plugin->hasNotificationHandler()) { 
     992      channels_.register_listener(plugin->get_id(), _T("NSCA")); 
     993    } 
    993994    if (plugin->hasMessageHandler()) 
    994995      messageHandlers_.insert(messageHandlers_.end(), plugin); 
     
    11091110    } 
    11101111  } else */{ 
    1111     /* 
    11121112    boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 
    11131113    if (!readLock.owns_lock()) { 
     
    11151115      return NSCAPI::returnUNKNOWN; 
    11161116    } 
    1117     */ 
    11181117    try { 
    11191118      nsclient::commands::plugin_type plugin = commands_.get(command); 
     
    11321131      return NSCAPI::returnIgnored; 
    11331132    } 
     1133  } 
     1134} 
     1135 
     1136 
     1137NSCAPI::errorReturn NSClientT::send_notification(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code,  char* result, unsigned int result_len) { 
     1138  boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 
     1139  if (!readLock.owns_lock()) { 
     1140    LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex.")); 
     1141    return NSCAPI::hasFailed; 
     1142  } 
     1143  try { 
     1144    LOG_ERROR_CORE_STD(_T("Notifying: ") + strEx::strip_hex(to_wstring(std::string(result,result_len)))); 
     1145    bool found = false; 
     1146    BOOST_FOREACH(nsclient::channels::plugin_type p, channels_.get(channel)) { 
     1147      p->handleNotification(channel, command, code, result, result_len); 
     1148      found = true; 
     1149    } 
     1150    if (!found) { 
     1151      LOG_ERROR_CORE_STD(_T("Noone listens for events from: ") + std::wstring(channel)); 
     1152    } 
     1153    return NSCAPI::isSuccess; 
     1154  } catch (nsclient::channels::channel_exception &e) { 
     1155    LOG_ERROR_CORE(_T("No handler for channel: ") + std::wstring(channel) + _T(": ") + to_wstring(e.what())); 
     1156    return NSCAPI::hasFailed; 
     1157  } catch (...) { 
     1158    LOG_ERROR_CORE(_T("Error handling channel: ") + std::wstring(channel)); 
     1159    return NSCAPI::hasFailed; 
    11341160  } 
    11351161} 
  • trunk/service/NSClient++.h

    r241 r254  
    3030#include "NSCPlugin.h" 
    3131#include "commands.hpp" 
     32#include "channels.hpp" 
    3233#include "logger.hpp" 
    3334 
     
    101102  typedef std::list<cached_log_entry> log_cache_type; 
    102103  pluginList plugins_; 
    103   pluginList commandHandlers_; 
    104104  pluginList messageHandlers_; 
    105105  boost::filesystem::wpath  basePath; 
     
    122122  bool enable_shared_session_; 
    123123  nsclient::commands commands_; 
     124  nsclient::channels channels_; 
    124125 
    125126 
    126127public: 
    127128  // c-tor, d-tor 
    128   NSClientT(void) : debug_(log_unknown), plugins_loaded_(false), enable_shared_session_(false), commands_(this) {} 
     129  NSClientT(void) : debug_(log_unknown), plugins_loaded_(false), enable_shared_session_(false), commands_(this), channels_(this) {} 
    129130  virtual ~NSClientT(void) {} 
    130131  void enableDebug(bool debug = true) { 
     
    163164  // Member functions 
    164165  boost::filesystem::wpath getBasePath(void); 
     166  NSCAPI::errorReturn send_notification(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, char* result, unsigned int result_len); 
    165167  NSCAPI::nagiosReturn injectRAW(const wchar_t* command, std::string &request, std::string &response); 
    166168  NSCAPI::nagiosReturn inject(std::wstring command, std::wstring arguments, std::wstring &msg, std::wstring & perf); 
  • trunk/service/core_api.cpp

    r245 r254  
    433433} 
    434434 
    435 NSCAPI::errorReturn NSAPINotify(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, const wchar_t* message, const wchar_t* perf) { 
    436   LOG_ERROR_STD(_T("TODO: implment channels: ") + std::wstring(command)); 
    437   return NSCAPI::hasFailed; 
     435NSCAPI::errorReturn NSAPINotify(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code,  char* result, unsigned int result_len) { 
     436  return mainClient.send_notification(channel, command, code, result, result_len); 
    438437} 
    439438 
  • trunk/service/core_api.h

    r241 r254  
    5858NSCAPI::errorReturn NSAPIReleasePluginList(int,NSCAPI::plugin_info*[]); 
    5959NSCAPI::errorReturn NSAPISettingsSave(void); 
    60 NSCAPI::errorReturn NSAPINotify(const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn, const wchar_t*, const wchar_t*); 
     60NSCAPI::errorReturn NSAPINotify(const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn, char*, unsigned int); 
    6161void NSAPIDestroyBuffer(char**); 
Note: See TracChangeset for help on using the changeset viewer.