Changeset 9bd40e2 in nscp


Ignore:
Timestamp:
05/06/12 21:25:45 (13 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.1, 0.4.2
Children:
ef5832c
Parents:
53473f7
Message:
  • Refactored server internals to be more uniform (This is the first step to adding more protocols like NRDP and unit tests for check_nt)
Files:
4 added
20 deleted
22 edited

Legend:

Unmodified
Added
Removed
  • changelog

    r53473f7 r9bd40e2  
    55 * Fix RtlStringFromGUID problem on NT4 
    66 
     72012-05-06 MickeM 
     8 * Refactored server internals to be more uniform (This is the first step to adding more protocols like NRDP and unittests for check_nt) 
     9  
    7102012-05-03 MickeM 
    811 * Fixed issue with parsing size-type (usch as MinCritFree=10G) 
  • include/check_nt/server/handler.hpp

    re1d9a58 r9bd40e2  
    66namespace check_nt { 
    77  namespace server { 
    8     class handler { 
     8    class handler : public boost::noncopyable { 
    99    public: 
    10       handler() {} 
    11       handler(const handler &other) {} 
    12       handler& operator= (const handler &other) { 
    13         return *this; 
    14       } 
    1510      virtual check_nt::packet handle(check_nt::packet packet) = 0; 
    16       virtual void log_debug(std::string file, int line, std::wstring msg) = 0; 
    17       virtual void log_error(std::string file, int line, std::wstring msg) = 0; 
     11      virtual void log_debug(std::string module, std::string file, int line, std::string msg) const = 0; 
     12      virtual void log_error(std::string module, std::string file, int line, std::string msg) const = 0; 
    1813      virtual check_nt::packet create_error(std::wstring msg) = 0; 
    1914 
  • include/check_nt/server/parser.hpp

    r682ccd2 r9bd40e2  
    99namespace check_nt { 
    1010  namespace server { 
    11     class parser : public boost::noncopyable{ 
     11    class parser : public boost::noncopyable { 
    1212      std::vector<char> buffer_; 
    13       boost::shared_ptr<check_nt::server::handler> handler_; 
    1413    public: 
    15       parser(boost::shared_ptr<check_nt::server::handler> handler) : handler_(handler) {} 
     14      parser() {} 
    1615 
    1716      template <typename InputIterator> 
  • include/nrpe/server/handler.hpp

    r1ecd26f r9bd40e2  
    66namespace nrpe { 
    77  namespace server { 
    8     class handler { 
     8    class handler : boost::noncopyable { 
    99    public: 
    10       handler() {} 
    11       handler(const handler &other) {} 
    12       handler& operator= (const handler &other) { 
    13         return *this; 
    14       } 
    1510      virtual nrpe::packet handle(nrpe::packet packet) = 0; 
    16       virtual void log_debug(std::string file, int line, std::wstring msg) = 0; 
    17       virtual void log_error(std::string file, int line, std::wstring msg) = 0; 
     11      virtual void log_debug(std::string module, std::string file, int line, std::string msg) const = 0; 
     12      virtual void log_error(std::string module, std::string file, int line, std::string msg) const = 0; 
    1813      virtual nrpe::packet create_error(std::wstring msg) = 0; 
    1914      virtual unsigned int get_payload_length() = 0; 
  • include/nrpe/server/parser.hpp

    re396b2f r9bd40e2  
    1313      unsigned int packet_length_; 
    1414      unsigned int payload_length_; 
    15       boost::shared_ptr<nrpe::server::handler> handler_; 
    1615    public: 
    17       parser(boost::shared_ptr<nrpe::server::handler> handler) : handler_(handler) { 
    18         set_payload_length(handler->get_payload_length()); 
     16      parser(unsigned int payload_length) : payload_length_(payload_length), packet_length_(nrpe::length::get_packet_length(payload_length)) { 
    1917      } 
    2018 
     
    3230        return packet; 
    3331      } 
     32      /* 
    3433      void set_payload_length(unsigned int length) { 
    3534        payload_length_ = length; 
     
    3938        return payload_length_; 
    4039      } 
     40      */ 
    4141    }; 
    4242 
  • include/nsca/nsca_socket.hpp

    r84cdb9b r9bd40e2  
    9292      std::string iv = iv_packet.get_iv(); 
    9393      time = iv_packet.get_time(); 
    94       NSC_DEBUG_MSG(_T("Encrypting using when sending: ") + utf8::cvt<std::wstring>(nsca::nsca_encrypt::helpers::encryption_to_string(encryption_method)) + _T(" and ") + utf8::cvt<std::wstring>(password)); 
     94      NSC_DEBUG_MSG(_T("Encrypting using: ") + utf8::cvt<std::wstring>(nsca::nsca_encrypt::helpers::encryption_to_string(encryption_method)) + _T(", password '") + utf8::cvt<std::wstring>(password) + _T("'")); 
    9595      crypt_inst.encrypt_init(password, encryption_method, iv); 
    9696      return true; 
  • include/nsca/server/handler.hpp

    r9b9be81 r9bd40e2  
    55namespace nsca { 
    66  namespace server { 
    7     class handler { 
     7    class handler : boost::noncopyable { 
    88    public: 
    9       handler() {} 
    10       handler(const handler &other) {} 
    11       handler& operator= (const handler &other) { 
    12         return *this; 
    13       } 
    149      virtual void handle(nsca::packet packet) = 0; 
    15       virtual void log_debug(std::string file, int line, std::wstring msg) = 0; 
    16       virtual void log_error(std::string file, int line, std::wstring msg) = 0; 
     10      virtual void log_debug(std::string module, std::string file, int line, std::string msg) const = 0; 
     11      virtual void log_error(std::string module, std::string file, int line, std::string msg) const = 0; 
    1712      virtual unsigned int get_payload_length() = 0; 
    1813      virtual void set_payload_length(unsigned int payload) = 0; 
  • include/nsca/server/parser.hpp

    r0f7b655 r9bd40e2  
    1717      boost::shared_ptr<nsca::server::handler> handler_; 
    1818    public: 
    19       parser(boost::shared_ptr<nsca::server::handler> handler) : handler_(handler) { 
    20         set_payload_length(handler->get_payload_length()); 
     19      parser(unsigned int payload_length) : payload_length_(payload_length), packet_length_(nsca::length::get_packet_length(payload_length)) { 
    2120      } 
    2221 
     
    4140        return buffer_; 
    4241      } 
    43       void set_payload_length(unsigned int length) { 
    44         payload_length_ = length; 
    45         packet_length_ = nsca::length::get_packet_length(length); 
    46       } 
    47       int get_payload_lenght() const { return payload_length_; } 
    4842      std::string::size_type size() { 
    4943        return buffer_.size(); 
  • include/socket/server.hpp

    ra629015 r9bd40e2  
    1010 
    1111#include <socket/socket_helpers.hpp> 
     12#include <socket/connection.hpp> 
    1213#include <strEx.h> 
    1314 
     
    1617  namespace server { 
    1718    namespace str = nscp::helpers; 
    18     /* 
    19     struct dummy_handler { 
    20     handler() {} 
    21     handler(const handler &other) {} 
    22     handler& operator= (const handler &other) { 
    23     return *this; 
    24     } 
    25     virtual std::string handle(std::string packet) = 0; 
    26     virtual void log_debug(std::string file, int line, std::wstring msg) = 0; 
    27     virtual void log_error(std::string file, int line, std::wstring msg) = 0; 
    28     virtual std::string create_error(std::wstring msg) = 0; 
    29     }; 
    30     struct dummy_connection_info : public socket_helpers::connection_info { 
    31     dummy_connection_info(boost::shared_ptr<dummy_handler> request_handler) : request_handler(request_handler) {} 
    32     dummy_connection_info(const connection_info &other)  
    33     : socket_helpers::connection_info(other) 
    34     , request_handler(other.request_handler) 
    35     {} 
    36     connection_info& operator=(const connection_info &other) { 
    37     socket_helpers::connection_info::operator=(other); 
    38     request_handler = other.request_handler; 
    39     return *this; 
    40     } 
    41     boost::shared_ptr<dummy_handler> request_handler; 
    42     }; 
    43  
    44  
    45     struct dummy_server_definition { 
    46     typedef boost::shared_ptr<dummy_handler> handler_type; 
    47     typedef dummy_connection_info connection_info_type; 
    48  
    49     static connection_info_type create(boost::asio::io_service& io_service, boost::asio::ssl::context &context, handler_type handler, connection_info_type info); 
    50     }; 
    51  
    52     */ 
     19     
    5320    class server_exception : public std::exception { 
    5421      std::string error; 
     
    7441    namespace ip = boost::asio::ip; 
    7542 
    76     template<class ServerDefinition> 
    77     class server : boost::noncopyable { 
     43    template<class protocol_type, std::size_t N> 
     44    class server : private boost::noncopyable { 
     45 
     46      typedef socket_helpers::server::connection<protocol_type, N> connection_type; 
     47#ifdef USE_SSL 
     48      typedef socket_helpers::server::ssl_connection<protocol_type, N> ssl_connection_type; 
     49#endif 
    7850    public: 
    79       server(typename ServerDefinition::connection_info_type info)  
    80         : acceptor_(io_service_) 
     51      server(boost::shared_ptr<protocol_type> protocol) 
     52        : protocol_(protocol) 
     53        , acceptor_(io_service_) 
    8154        , accept_strand_(io_service_) 
    82         , request_handler_(info.request_handler) 
    8355#ifdef USE_SSL 
    8456        , context_(io_service_, boost::asio::ssl::context::sslv23) 
    8557#endif 
    86         , info_(info) {} 
     58      { 
     59        if (!protocol_) 
     60          throw server_exception("Invalid protocol instance"); 
     61      } 
     62      ~server() { 
    8763 
    88       void setup() { 
    89         if (!request_handler_) 
    90           throw server_exception("Invalid handler when creating server"); 
     64      } 
    9165 
    92         // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). 
     66      void start() { 
    9367        ip::tcp::resolver resolver(io_service_); 
    9468        ip::tcp::resolver::iterator endpoint_iterator; 
    95         if (info_.address.empty()) { 
    96           endpoint_iterator = resolver.resolve(ip::tcp::resolver::query(info_.get_port())); 
     69        if (protocol_->get_info().address.empty()) { 
     70          endpoint_iterator = resolver.resolve(ip::tcp::resolver::query(protocol_->get_info().get_port())); 
    9771        } else { 
    98           endpoint_iterator = resolver.resolve(ip::tcp::resolver::query(info_.get_address(), info_.get_port())); 
     72          endpoint_iterator = resolver.resolve(ip::tcp::resolver::query(protocol_->get_info().get_address(), protocol_->get_info().get_port())); 
    9973        } 
    10074        ip::tcp::resolver::iterator end; 
    101         if (endpoint_iterator == end) 
    102           throw server_exception("Failed to lookup: " + info_.get_endpoint_string()); 
     75        if (endpoint_iterator == end) { 
     76          protocol_->log_error(__FILE__, __LINE__, "Failed to lookup: " + protocol_->get_info().get_endpoint_string()); 
     77          return; 
     78        } 
     79        if (protocol_->get_info().use_ssl) { 
    10380#ifdef USE_SSL 
    104         if (info_.use_ssl) { 
    10581          SSL_CTX_set_cipher_list(context_.impl(), "ADH"); 
    106           request_handler_->log_debug(__FILE__, __LINE__, _T("Using cert: ") + str::to_wstring(info_.certificate)); 
    107           context_.use_tmp_dh_file(to_string(info_.certificate)); 
     82          protocol_->log_debug(__FILE__, __LINE__, "Using certificate: " + utf8::cvt<std::string>(protocol_->get_info().certificate)); 
     83          context_.use_tmp_dh_file(to_string(protocol_->get_info().certificate)); 
    10884          context_.set_verify_mode(boost::asio::ssl::context::verify_none); 
     85#else 
     86          protocol_->log_error(__FILE__, __LINE__, "Not compiled with SSL"); 
     87#endif 
    10988        } 
    110 #endif 
    11189        new_connection_.reset(create_connection()); 
    11290 
     
    11492        acceptor_.open(endpoint.protocol()); 
    11593        acceptor_.set_option(ip::tcp::acceptor::reuse_address(true)); 
    116         request_handler_->log_debug(__FILE__, __LINE__, _T("Attempting to bind to: ") + info_.get_endpoint_wstring()); 
     94        protocol_->log_debug(__FILE__, __LINE__, "Attempting to bind to: " + protocol_->get_info().get_endpoint_string()); 
    11795        acceptor_.bind(endpoint); 
    118         if (info_.back_log == connection_info::backlog_default) 
     96        if (protocol_->get_info().back_log == connection_info::backlog_default) 
    11997          acceptor_.listen(); 
    12098        else 
    121           acceptor_.listen(info_.back_log); 
     99          acceptor_.listen(protocol_->get_info().back_log); 
    122100 
    123         acceptor_.async_accept(new_connection_->socket(), 
    124           accept_strand_.wrap( 
    125           boost::bind(&server<ServerDefinition>::handle_accept, this, boost::asio::placeholders::error) 
    126           ) 
    127           ); 
    128         request_handler_->log_debug(__FILE__, __LINE__, _T("Bound to: ") + info_.get_endpoint_wstring()); 
    129       } 
     101        acceptor_.async_accept(new_connection_->socket(),accept_strand_.wrap( 
     102          boost::bind(&server::handle_accept, this, boost::asio::placeholders::error) 
     103          )); 
     104        protocol_->log_debug(__FILE__, __LINE__, "Bound to: " + protocol_->get_info().get_endpoint_string()); 
    130105 
    131       typename ServerDefinition::connection_type* create_connection() { 
    132 #ifdef USE_SSL 
    133         return ServerDefinition::create(io_service_, context_, request_handler_, info_); 
    134 #else 
    135         return ServerDefinition::create(io_service_, request_handler_, info_); 
    136 #endif 
    137       } 
    138  
    139  
    140       virtual ~server() {} 
    141  
    142       /// Run the server's io_service loop. 
    143       void start() { 
    144         // Create a pool of threads to run all of the io_services. 
    145         for (std::size_t i = 0; i < info_.thread_pool_size; ++i) { 
    146           boost::shared_ptr<boost::thread> thread( 
    147             new boost::thread( boost::bind(&boost::asio::io_service::run, &io_service_) )); 
    148           threads_.push_back(thread); 
     106        for (std::size_t i = 0; i < protocol_->get_info().thread_pool_size; ++i) { 
     107          thread_group_.create_thread(boost::bind(&boost::asio::io_service::run, &io_service_)); 
    149108        } 
    150         request_handler_->log_debug(__FILE__, __LINE__, _T("Thredpool containes: ") + str::to_wstring(info_.thread_pool_size)); 
    151109      } 
    152110 
    153111      void stop() { 
    154112        io_service_.stop(); 
    155         for (std::size_t i = 0; i < threads_.size(); ++i) 
    156           threads_[i]->join(); 
     113        thread_group_.join_all(); 
    157114      } 
    158115 
     116    private: 
    159117      void handle_accept(const boost::system::error_code& e) { 
    160118        if (!e) { 
    161119          std::list<std::string> errors; 
    162           try { 
    163             if (info_.allowed_hosts.is_allowed(new_connection_->socket().remote_endpoint().address(), errors)) { 
    164               std::string s = new_connection_->socket().remote_endpoint().address().to_string(); 
    165               request_handler_->log_debug(__FILE__, __LINE__, _T("Accepting connection from: ") + str::to_wstring(s)); 
    166               new_connection_->start(); 
    167             } else { 
    168               BOOST_FOREACH(const std::string &e, errors) { 
    169                 request_handler_->log_error(__FILE__, __LINE__, utf8::cvt<std::wstring>(e)); 
    170               } 
    171               std::string s = new_connection_->socket().remote_endpoint().address().to_string(); 
    172               request_handler_->log_error(__FILE__, __LINE__, _T("Rejcted connection from: ") + str::to_wstring(s)); 
    173               new_connection_->stop(); 
    174             } 
    175           } catch(const std::exception &e) { 
     120          if (protocol_->on_accept(new_connection_->socket())) { 
     121            new_connection_->start(); 
     122          } else { 
    176123            new_connection_->stop(); 
    177             request_handler_->log_error(__FILE__, __LINE__, utf8::cvt<std::wstring>(e.what())); 
    178124          } 
     125 
    179126          new_connection_.reset(create_connection()); 
     127 
    180128          acceptor_.async_accept(new_connection_->socket(), 
    181             accept_strand_.wrap(boost::bind(&server<ServerDefinition>::handle_accept, this, boost::asio::placeholders::error)) 
    182           ); 
     129            accept_strand_.wrap( 
     130            boost::bind(&server::handle_accept, this, boost::asio::placeholders::error) 
     131            ) 
     132            ); 
    183133        } else { 
    184           request_handler_->log_error(__FILE__, __LINE__, _T("Socket ERROR: ") + str::to_wstring(e.message())); 
     134          protocol_->log_error(__FILE__, __LINE__, "Socket ERROR: " + e.message()); 
    185135        } 
    186136      } 
    187137 
    188  
    189     private: 
     138      typename connection_type* create_connection() { 
     139#ifdef USE_SSL 
     140        if (protocol_->get_info().use_ssl) { 
     141          return new ssl_connection_type(io_service_, context_, protocol_); 
     142        } 
     143#endif 
     144        return new connection_type(io_service_, protocol_); 
     145      } 
    190146 
    191147      boost::asio::io_service io_service_; 
    192  
    193148      boost::asio::ip::tcp::acceptor acceptor_; 
    194  
    195       boost::shared_ptr<typename ServerDefinition::connection_type> new_connection_; 
    196  
    197       std::vector<boost::shared_ptr<boost::thread> > threads_; 
    198  
    199       typename ServerDefinition::handler_type request_handler_; 
    200  
     149      boost::shared_ptr<connection_type> new_connection_; 
     150      boost::thread_group thread_group_; 
    201151#ifdef USE_SSL 
    202152      boost::asio::ssl::context context_; 
    203153#endif 
     154      boost::asio::strand accept_strand_; 
     155      boost::shared_ptr<protocol_type> protocol_; 
    204156 
    205       boost::asio::strand accept_strand_; 
    206  
    207       typename ServerDefinition::connection_info_type info_; 
    208157    }; 
    209158 
  • modules/NRPEServer/CMakeLists.txt

    r8d89d7a r9bd40e2  
    1212  "${TARGET}.cpp" 
    1313  "handler_impl.cpp" 
    14   ${NSCP_INCLUDEDIR}/nrpe/server/server.cpp 
    15   ${NSCP_INCLUDEDIR}/nrpe/server/connection.cpp 
    16   ${NSCP_INCLUDEDIR}/nrpe/server/tcp_connection.cpp 
    17   ${NSCP_INCLUDEDIR}/nrpe/server/ssl_connection.cpp 
    1814  ${NSCP_INCLUDEDIR}/nrpe/packet.cpp 
    1915  ${NSCP_INCLUDEDIR}/socket/socket_helpers.cpp 
     
    3329    "${TARGET}.def" 
    3430    "handler_impl.hpp" 
    35     ${NSCP_INCLUDEDIR}/nrpe/server/server.hpp 
    36     ${NSCP_INCLUDEDIR}/nrpe/server/connection.hpp 
    37     ${NSCP_INCLUDEDIR}/nrpe/server/tcp_connection.hpp 
    38     ${NSCP_INCLUDEDIR}/nrpe/server/ssl_connection.hpp 
     31    ${NSCP_INCLUDEDIR}/nrpe/server/protocol.hpp 
    3932    ${NSCP_INCLUDEDIR}/nrpe/server/handler.hpp 
    4033    ${NSCP_INCLUDEDIR}/nrpe/server/parser.hpp 
     
    4235    ${NSCP_INCLUDEDIR}/swap_bytes.hpp 
    4336    ${NSCP_INCLUDEDIR}/socket/socket_helpers.hpp 
     37    ${NSCP_INCLUDEDIR}/socket/server.hpp 
     38    ${NSCP_INCLUDEDIR}/socket/connection.hpp 
    4439 
    4540    ${NSCP_DEF_PLUGIN_HPP} 
     
    5853INCLUDE(${BUILD_CMAKE_FOLDER}/module.cmake) 
    5954SOURCE_GROUP("Server" REGULAR_EXPRESSION .*include/nrpe/.*) 
     55SOURCE_GROUP("Socket" REGULAR_EXPRESSION .*include/socket/.*) 
  • modules/NRPEServer/NRPEServer.cpp

    r83c2453 r9bd40e2  
    3131 
    3232 
    33 NRPEListener::NRPEListener() : info_(boost::shared_ptr<nrpe::server::handler>(new handler_impl(1024))) { 
     33NRPEServer::NRPEServer() : handler_(new handler_impl(1024)) { 
    3434} 
    35 NRPEListener::~NRPEListener() {} 
     35NRPEServer::~NRPEServer() {} 
    3636 
    37 bool NRPEListener::loadModule() { 
     37bool NRPEServer::loadModule() { 
    3838  return false; 
    3939} 
    4040 
    41 bool NRPEListener::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
     41bool NRPEServer::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
    4242  try { 
    4343 
     
    4646 
    4747    settings.alias().add_path_to_settings() 
    48       (_T("NRPE SERVER SECTION"), _T("Section for NRPE (NRPEListener.dll) (check_nrpe) protocol options.")) 
     48      (_T("NRPE SERVER SECTION"), _T("Section for NRPE (NRPEServer.dll) (check_nrpe) protocol options.")) 
    4949      ; 
    5050 
     
    5353      _T("PORT NUMBER"), _T("Port to use for NRPE.")) 
    5454 
    55       (_T("payload length"), sh::int_fun_key<unsigned int>(boost::bind(&nrpe::server::handler::set_payload_length, info_.request_handler, _1), 1024), 
     55      (_T("payload length"), sh::int_fun_key<unsigned int>(boost::bind(&nrpe::server::handler::set_payload_length, handler_, _1), 1024), 
    5656      _T("PAYLOAD LENGTH"), _T("Length of payload to/from the NRPE agent. This is a hard specific value so you have to \"configure\" (read recompile) your NRPE agent to use the same value for it to work."), true) 
    5757 
    58       (_T("allow arguments"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_allow_arguments, info_.request_handler, _1), false), 
     58      (_T("allow arguments"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_allow_arguments, handler_, _1), false), 
    5959      _T("COMMAND ARGUMENT PROCESSING"), _T("This option determines whether or not the we will allow clients to specify arguments to commands that are executed.")) 
    6060 
    61       (_T("allow nasty characters"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_allow_nasty_arguments, info_.request_handler, _1), false), 
     61      (_T("allow nasty characters"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_allow_nasty_arguments, handler_, _1), false), 
    6262      _T("COMMAND ALLOW NASTY META CHARS"), _T("This option determines whether or not the we will allow clients to specify nasty (as in |`&><'\"\\[]{}) characters in arguments.")) 
    6363 
    64       (_T("performance data"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_perf_data, info_.request_handler, _1), true), 
     64      (_T("performance data"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_perf_data, handler_, _1), true), 
    6565      _T("PERFORMANCE DATA"), _T("Send performance data back to nagios (set this to 0 to remove all performance data)."), true) 
    6666 
     
    104104    } 
    105105#endif 
    106     if (info_.request_handler->get_payload_length() != 1024) 
    107       NSC_DEBUG_MSG_STD(_T("Non-standard buffer length (hope you have recompiled check_nrpe changing #define MAX_PACKETBUFFER_LENGTH = ") + strEx::itos(info_.request_handler->get_payload_length())); 
     106    if (handler_->get_payload_length() != 1024) 
     107      NSC_DEBUG_MSG_STD(_T("Non-standard buffer length (hope you have recompiled check_nrpe changing #define MAX_PACKETBUFFER_LENGTH = ") + strEx::itos(handler_->get_payload_length())); 
    108108    if (!boost::filesystem::is_regular(info_.certificate)) 
    109109      NSC_LOG_ERROR_STD(_T("Certificate not found: ") + info_.certificate); 
     
    120120 
    121121    if (mode == NSCAPI::normalStart) { 
     122#ifndef USE_SSL 
    122123      if (info_.use_ssl) { 
    123 #ifdef USE_SSL 
    124         server_.reset(new nrpe::server::server(info_)); 
    125 #else 
    126124        NSC_LOG_ERROR_STD(_T("SSL is not supported (not compiled with openssl)")); 
    127125        return false; 
     126      } 
    128127#endif 
    129       } else { 
    130         server_.reset(new nrpe::server::server(info_)); 
    131       } 
     128      server_.reset(new nrpe::server::server(boost::shared_ptr<nrpe::read_protocol>(new nrpe::read_protocol(info_, handler_)))); 
    132129      if (!server_) { 
    133130        NSC_LOG_ERROR_STD(_T("Failed to create server instance!")); 
     
    136133      server_->start(); 
    137134    } 
    138   } catch (nrpe::server::nrpe_exception &e) { 
    139     NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.what()); 
    140     return false; 
    141135  } catch (std::exception &e) { 
    142136    NSC_LOG_ERROR_STD(_T("Exception caught: ") + to_wstring(e.what())); 
     
    151145} 
    152146 
    153 bool NRPEListener::unloadModule() { 
     147bool NRPEServer::unloadModule() { 
    154148  try { 
    155149    if (server_) { 
     
    165159 
    166160 
    167 bool NRPEListener::hasCommandHandler() { 
     161bool NRPEServer::hasCommandHandler() { 
    168162  return false; 
    169163} 
    170 bool NRPEListener::hasMessageHandler() { 
     164bool NRPEServer::hasMessageHandler() { 
    171165  return false; 
    172166} 
    173167 
    174168NSC_WRAP_DLL(); 
    175 NSC_WRAPPERS_MAIN_DEF(NRPEListener); 
     169NSC_WRAPPERS_MAIN_DEF(NRPEServer); 
    176170NSC_WRAPPERS_IGNORE_MSG_DEF(); 
    177171NSC_WRAPPERS_IGNORE_CMD_DEF(); 
  • modules/NRPEServer/NRPEServer.h

    r81e420c r9bd40e2  
    2121 
    2222#include <socket_helpers.hpp> 
    23 #include <nrpe/server/server.hpp> 
     23#include <nrpe/server/protocol.hpp> 
     24#include "handler_impl.hpp" 
    2425 
    2526NSC_WRAPPERS_MAIN(); 
    2627 
    27 class NRPEListener : public nscapi::impl::simple_plugin { 
    28 private: 
    29   typedef enum { 
    30     inject, script, script_dir, 
    31   } command_type; 
    32   struct command_data { 
    33     command_data() : type(inject) {} 
    34     command_data(command_type type_, std::wstring arguments_) : type(type_), arguments(arguments_) {} 
    35     command_type type; 
    36     std::wstring arguments; 
    37   }; 
    38  
    39   nrpe::server::server::connection_info info_; 
    40  
     28class NRPEServer : public nscapi::impl::simple_plugin { 
    4129public: 
    42   NRPEListener(); 
    43   virtual ~NRPEListener(); 
     30  NRPEServer(); 
     31  virtual ~NRPEServer(); 
    4432  // Module calls 
    4533  bool loadModule(); 
     
    6755  NSCAPI::nagiosReturn handleCommand(const strEx::blindstr command, const unsigned int argLen, wchar_t **char_args, std::wstring &message, std::wstring &perf); 
    6856  std::wstring getConfigurationMeta(); 
     57   
     58private: 
     59  socket_helpers::connection_info info_; 
    6960  boost::shared_ptr<nrpe::server::server> server_; 
     61  boost::shared_ptr<handler_impl> handler_; 
    7062 
    71 private: 
     63 
    7264  class NRPEException { 
    7365    std::wstring error_; 
  • modules/NRPEServer/handler_impl.hpp

    r8013c0c r9bd40e2  
    3535    noPerfData_ = !v; 
    3636    if (noPerfData_) 
    37       log_debug(__FILE__, __LINE__, _T("Performance data disabled!")); 
     37      log_debug("nrpe", __FILE__, __LINE__, "Performance data disabled!"); 
    3838  } 
    3939 
    40   void log_debug(std::string file, int line, std::wstring msg) { 
     40  void log_debug(std::string module, std::string file, int line, std::string msg) const { 
    4141    if (GET_CORE()->should_log(NSCAPI::log_level::debug)) { 
    4242      GET_CORE()->log(NSCAPI::log_level::debug, file, line, msg); 
    4343    } 
    4444  } 
    45   void log_error(std::string file, int line, std::wstring msg) { 
     45  void log_error(std::string module, std::string file, int line, std::string msg) const { 
    4646    if (GET_CORE()->should_log(NSCAPI::log_level::error)) { 
    4747      GET_CORE()->log(NSCAPI::log_level::error, file, line, msg); 
  • modules/NSCAServer/CMakeLists.txt

    r8d89d7a r9bd40e2  
    99  "${TARGET}.cpp" 
    1010  "handler_impl.cpp" 
    11   ${NSCP_INCLUDEDIR}/nsca/server/server.cpp 
    12   ${NSCP_INCLUDEDIR}/nsca/server/connection.cpp 
     11  ${NSCP_INCLUDEDIR}/nsca/nsca_packet.cpp 
    1312  ${NSCP_INCLUDEDIR}/socket/socket_helpers.cpp 
    14   ${NSCP_INCLUDEDIR}/nsca/nsca_packet.cpp 
    1513 
    1614  ${NSCP_DEF_PLUGIN_CPP} 
     
    2523ENDIF(HAVE_CRYPTOPP) 
    2624 
     25#IF(OPENSSL_FOUND) 
     26# ADD_DEFINITIONS(-DUSE_SSL) 
     27#ENDIF(OPENSSL_FOUND) 
     28 
    2729ADD_DEFINITIONS(${NSCP_GLOBAL_DEFINES}) 
    2830 
     
    3335    "${TARGET}.def" 
    3436    "handler_impl.hpp" 
    35     ${NSCP_INCLUDEDIR}/nsca/server/server.hpp 
    36     ${NSCP_INCLUDEDIR}/nsca/server/connection.hpp 
     37    ${NSCP_INCLUDEDIR}/nsca/server/protocol.hpp 
    3738    ${NSCP_INCLUDEDIR}/nsca/server/handler.hpp 
    3839    ${NSCP_INCLUDEDIR}/nsca/server/parser.hpp 
    3940    ${NSCP_INCLUDEDIR}/nsca/nsca_packet.hpp 
    4041    ${NSCP_INCLUDEDIR}/swap_bytes.hpp 
     42    ${NSCP_INCLUDEDIR}/socket/socket_helpers.hpp 
    4143    ${NSCP_INCLUDEDIR}/socket/server.hpp 
    42     ${NSCP_INCLUDEDIR}/socket/socket_helpers.hpp 
     44    ${NSCP_INCLUDEDIR}/socket/connection.hpp 
    4345 
    4446    ${NSCP_DEF_PLUGIN_HPP} 
     
    5456  ${Boost_THREAD_LIBRARY} 
    5557  ${NSCP_DEF_PLUGIN_LIB} 
     58# ${OPENSSL_LIBRARIES} 
    5659  ${CRYPTOPP_LIB} 
    5760) 
    5861INCLUDE(${BUILD_CMAKE_FOLDER}/module.cmake) 
    5962SOURCE_GROUP("Server" REGULAR_EXPRESSION .*include/nsca/.*) 
     63SOURCE_GROUP("Socket" REGULAR_EXPRESSION .*include/socket/.*) 
  • modules/NSCAServer/NSCAServer.cpp

    r9b9be81 r9bd40e2  
    2929namespace str = nscp::helpers; 
    3030 
    31 NSCAServer::NSCAServer() : info_(boost::shared_ptr<nsca::server::handler>(new handler_impl(1024))) {} 
     31NSCAServer::NSCAServer() : handler_(new nsca_handler_impl(1024)) {} 
    3232 
    3333bool NSCAServer::loadModule() { 
     
    5050      _T("PORT NUMBER"), _T("Port to use for NSCA.")) 
    5151 
    52       (_T("payload length"), sh::int_fun_key<unsigned int>(boost::bind(&nsca::server::handler::set_payload_length, info_.request_handler, _1), 512), 
     52      (_T("payload length"), sh::int_fun_key<unsigned int>(boost::bind(&nsca::server::handler::set_payload_length, handler_, _1), 512), 
    5353      _T("PAYLOAD LENGTH"), _T("Length of payload to/from the NSCA agent. This is a hard specific value so you have to \"configure\" (read recompile) your NSCA agent to use the same value for it to work.")) 
    5454 
    55       (_T("performance data"), sh::bool_fun_key<bool>(boost::bind(&nsca::server::handler::set_perf_data, info_.request_handler, _1), true), 
     55      (_T("performance data"), sh::bool_fun_key<bool>(boost::bind(&nsca::server::handler::set_perf_data, handler_, _1), true), 
    5656      _T("PERFORMANCE DATA"), _T("Send performance data back to nagios (set this to 0 to remove all performance data).")) 
    5757 
    58       (_T("encryption"), sh::string_fun_key<std::string>(boost::bind(&nsca::server::handler::set_encryption, info_.request_handler, _1), "aes"), 
     58      (_T("encryption"), sh::string_fun_key<std::string>(boost::bind(&nsca::server::handler::set_encryption, handler_, _1), "aes"), 
    5959      _T("ENCRYPTION"), _T("Encryption to use")) 
    6060 
    61       (_T("password"), sh::string_fun_key<std::string>(boost::bind(&nsca::server::handler::set_password, info_.request_handler, _1), ""), 
     61      (_T("password"), sh::string_fun_key<std::string>(boost::bind(&nsca::server::handler::set_password, handler_, _1), ""), 
    6262      _T("PASSWORD"), _T("Password to use")) 
    6363 
     
    8585      _T("TIMEOUT"), _T("Timeout when reading packets on incoming sockets. If the data has not arrived within this time we will bail out.")) 
    8686 
    87       (_T("inbox"), sh::string_fun_key<std::wstring>(boost::bind(&nsca::server::handler::set_channel, info_.request_handler, _1), _T("inbox")), 
     87      (_T("inbox"), sh::string_fun_key<std::wstring>(boost::bind(&nsca::server::handler::set_channel, handler_, _1), _T("inbox")), 
    8888      _T("INBOX"), _T("The default channel to post incoming messages on")) 
    8989 
     
    9393    settings.notify(); 
    9494 
    95     if (info_.request_handler->get_payload_length() != 512) 
    96       NSC_DEBUG_MSG_STD(_T("Non-standard buffer length (hope you have recompiled check_nsca changing #define MAX_PACKETBUFFER_LENGTH = ") + strEx::itos(info_.request_handler->get_payload_length())); 
     95    if (handler_->get_payload_length() != 512) 
     96      NSC_DEBUG_MSG_STD(_T("Non-standard buffer length (hope you have recompiled check_nsca changing #define MAX_PACKETBUFFER_LENGTH = ") + strEx::itos(handler_->get_payload_length())); 
    9797 
    9898    std::list<std::string> errors; 
     
    104104 
    105105    if (mode == NSCAPI::normalStart) { 
    106       server_.reset(new nsca::server::nsca_server(info_)); 
     106#ifndef USE_SSL 
     107      if (info_.use_ssl) { 
     108        NSC_LOG_ERROR_STD(_T("SSL is not supported (not compiled with openssl)")); 
     109        return false; 
     110      } 
     111#endif 
     112      server_.reset(new nsca::server::server(boost::shared_ptr<nsca::read_protocol>(new nsca::read_protocol(info_, handler_)))); 
    107113      if (!server_) { 
    108114        NSC_LOG_ERROR_STD(_T("Failed to create server instance!")); 
    109115        return false; 
    110116      } 
    111       server_->setup(); 
     117      //server_->setup(); 
    112118      server_->start(); 
    113119    } 
  • modules/NSCAServer/NSCAServer.h

    ra629015 r9bd40e2  
    2121 
    2222#include <socket_helpers.hpp> 
    23 #include <nsca/server/server.hpp> 
     23#include <nsca/server/protocol.hpp> 
    2424 
    2525NSC_WRAPPERS_MAIN(); 
     
    2727class NSCAServer : public nscapi::impl::simple_plugin { 
    2828private: 
    29   nsca::server::nsca_connection_info info_; 
     29  socket_helpers::connection_info info_; 
    3030 
    3131public: 
     
    5555  static std::wstring getCryptos(); 
    5656 
    57   boost::shared_ptr<nsca::server::nsca_server> server_; 
     57  boost::shared_ptr<nsca::server::server> server_; 
     58  boost::shared_ptr<nsca::server::handler> handler_; 
    5859}; 
    5960 
  • modules/NSCAServer/handler_impl.cpp

    ree52cdd r9bd40e2  
    66#include <config.h> 
    77 
    8 void handler_impl::handle(nsca::packet p) { 
     8void nsca_handler_impl::handle(nsca::packet p) { 
    99  std::wstring response; 
    1010  std::string::size_type pos = p.result.find('|'); 
  • modules/NSCAServer/handler_impl.hpp

    r8013c0c r9bd40e2  
    99#include <unicode_char.hpp> 
    1010 
    11 class handler_impl : public nsca::server::handler, private boost::noncopyable { 
     11class nsca_handler_impl : public nsca::server::handler, private boost::noncopyable { 
    1212  unsigned int payload_length_; 
    1313  bool allowArgs_; 
     
    1818  std::string password_; 
    1919public: 
    20   handler_impl(unsigned int payload_length) : payload_length_(payload_length), noPerfData_(false), allowNasty_(false), allowArgs_(false) {} 
     20  nsca_handler_impl(unsigned int payload_length) : payload_length_(payload_length), noPerfData_(false), allowNasty_(false), allowArgs_(false) {} 
    2121 
    2222  unsigned int get_payload_length() { 
     
    5050    noPerfData_ = !v; 
    5151    if (noPerfData_) 
    52       log_debug(__FILE__, __LINE__, _T("Performance data disabled!")); 
     52      log_debug("nsca", __FILE__, __LINE__, "Performance data disabled!"); 
    5353  } 
    5454 
    55   void log_debug(std::string file, int line, std::wstring msg) { 
     55  void log_debug(std::string module, std::string file, int line, std::string msg) const { 
    5656    if (GET_CORE()->should_log(NSCAPI::log_level::debug)) { 
    5757      GET_CORE()->log(NSCAPI::log_level::debug, file, line, msg); 
    5858    } 
    5959  } 
    60   void log_error(std::string file, int line, std::wstring msg) { 
     60  void log_error(std::string module, std::string file, int line, std::string msg) const { 
    6161    if (GET_CORE()->should_log(NSCAPI::log_level::error)) { 
    6262      GET_CORE()->log(NSCAPI::log_level::error, file, line, msg); 
  • modules/NSClientServer/CMakeLists.txt

    r440c0cb r9bd40e2  
    1111  stdafx.cpp 
    1212  "${TARGET}.cpp" 
    13   "handler_impl.cpp" 
    14   ${NSCP_INCLUDEDIR}/check_nt/server/server.cpp 
    15   ${NSCP_INCLUDEDIR}/check_nt/server/connection.cpp 
    16   ${NSCP_INCLUDEDIR}/check_nt/server/tcp_connection.cpp 
    17   ${NSCP_INCLUDEDIR}/check_nt/server/ssl_connection.cpp 
    1813  ${NSCP_INCLUDEDIR}/check_nt/packet.cpp 
    1914  ${NSCP_INCLUDEDIR}/socket/socket_helpers.cpp 
     
    3328    "${TARGET}.def" 
    3429    "handler_impl.hpp" 
    35     ${NSCP_INCLUDEDIR}/check_nt/server/server.hpp 
    36     ${NSCP_INCLUDEDIR}/check_nt/server/connection.hpp 
    37     ${NSCP_INCLUDEDIR}/check_nt/server/tcp_connection.hpp 
    38     ${NSCP_INCLUDEDIR}/check_nt/server/ssl_connection.hpp 
    39     ${NSCP_INCLUDEDIR}/check_nt/server/handler.hpp 
     30    ${NSCP_INCLUDEDIR}/check_nt/server/protocol.hpp 
    4031    ${NSCP_INCLUDEDIR}/check_nt/server/parser.hpp 
    4132    ${NSCP_INCLUDEDIR}/check_nt/packet.hpp 
    4233    ${NSCP_INCLUDEDIR}/swap_bytes.hpp 
     34 
    4335    ${NSCP_INCLUDEDIR}/socket/socket_helpers.hpp 
     36    ${NSCP_INCLUDEDIR}/socket/connection.hpp 
     37    ${NSCP_INCLUDEDIR}/socket/server.hpp 
    4438 
    4539    ${NSCP_DEF_PLUGIN_HPP} 
     
    5751) 
    5852INCLUDE(${BUILD_CMAKE_FOLDER}/module.cmake) 
     53SOURCE_GROUP("Server" REGULAR_EXPRESSION .*include/check_nt/.*) 
     54SOURCE_GROUP("Socket" REGULAR_EXPRESSION .*include/socket/.*) 
  • modules/NSClientServer/NSClientServer.cpp

    r6533c1a r9bd40e2  
    2626#include "handler_impl.hpp" 
    2727#include <settings/client/settings_client.hpp> 
     28#include <nscapi/nscapi_core_helper.hpp> 
    2829 
    2930namespace sh = nscapi::settings_helper; 
    3031 
    31 NSClientListener::NSClientListener() : info_(boost::shared_ptr<check_nt::server::handler>(new handler_impl())) { 
    32 } 
    33 NSClientListener::~NSClientListener() { 
    34 } 
    35  
    36 bool NSClientListener::loadModule() { 
     32NSClientServer::NSClientServer()  
     33  : noPerfData_(false) 
     34  , allowNasty_(false) 
     35  , allowArgs_(false) 
     36{ 
     37} 
     38NSClientServer::~NSClientServer() { 
     39} 
     40 
     41bool NSClientServer::loadModule() { 
    3742  return false; 
    3843} 
    3944 
    40 bool NSClientListener::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
     45bool NSClientServer::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
    4146 
    4247  try { 
     
    5358      _T("PORT NUMBER"), _T("Port to use for check_nt.")) 
    5459 
    55       (_T("performance data"), sh::bool_fun_key<bool>(boost::bind(&check_nt::server::handler::set_perf_data, info_.request_handler, _1), true), 
     60      (_T("performance data"), sh::bool_fun_key<bool>(boost::bind(&NSClientServer::set_perf_data, this, _1), true), 
    5661      _T("PERFORMANCE DATA"), _T("Send performance data back to nagios (set this to 0 to remove all performance data).")) 
    5762 
     
    9095      _T("TIMEOUT"), _T("Timeout when reading packets on incoming sockets. If the data has not arrived within this time we will bail out.")) 
    9196 
    92       (_T("password"), sh::string_fun_key<std::wstring>(boost::bind(&check_nt::server::handler::set_password, info_.request_handler, _1), _T("")), 
     97      (_T("password"), sh::string_fun_key<std::wstring>(boost::bind(&NSClientServer::set_password, this, _1), _T("")), 
    9398      _T("PASSWORD"), _T("Password used to authenticate againast server")) 
    9499 
     
    119124  if (mode == NSCAPI::normalStart) { 
    120125    try { 
     126#ifndef USE_SSL 
    121127          if (info_.use_ssl) { 
    122 #ifdef USE_SSL 
    123             server_.reset(new check_nt::server::server(info_)); 
    124 #else 
    125128            NSC_LOG_ERROR_STD(_T("SSL is not supported (not compiled with openssl)")); 
    126129            return false; 
     130          } 
    127131#endif 
    128           } else { 
    129             server_.reset(new check_nt::server::server(info_)); 
    130           } 
     132          server_.reset(new check_nt::server::server(boost::shared_ptr<check_nt::read_protocol>(new check_nt::read_protocol(info_, this)))); 
    131133          if (!server_) { 
    132134            NSC_LOG_ERROR_STD(_T("Failed to create server instance!")); 
     
    134136          } 
    135137          server_->start(); 
    136  
    137     } catch (check_nt::server::check_nt_exception &e) { 
    138       NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.what()); 
    139       return false; 
    140138    } catch (std::exception &e) { 
    141139      NSC_LOG_ERROR_STD(_T("Exception caught: ") + to_wstring(e.what())); 
     
    148146  return true; 
    149147} 
    150 bool NSClientListener::unloadModule() { 
     148bool NSClientServer::unloadModule() { 
    151149  try { 
    152150    if (server_) { 
     
    161159} 
    162160 
     161 
     162 
     163#define REQ_CLIENTVERSION 1 // Works fine! 
     164#define REQ_CPULOAD     2 // Quirks 
     165#define REQ_UPTIME      3 // Works fine! 
     166#define REQ_USEDDISKSPACE 4 // Works fine! 
     167#define REQ_SERVICESTATE  5 // Works fine! 
     168#define REQ_PROCSTATE   6 // Works fine! 
     169#define REQ_MEMUSE      7 // Works fine! 
     170#define REQ_COUNTER     8 // Works fine! 
     171#define REQ_FILEAGE     9 // Works fine! (i hope) 
     172#define REQ_INSTANCES   10  // Works fine! (i hope) 
     173 
     174bool NSClientServer::isPasswordOk(std::wstring remotePassword)  { 
     175  std::wstring localPassword = get_password(); 
     176  if (localPassword == remotePassword) { 
     177    return true; 
     178  } 
     179  if ((remotePassword == _T("None")) && (localPassword.empty())) { 
     180    return true; 
     181  } 
     182  return false; 
     183} 
     184 
     185void split_to_list(std::list<std::wstring> &list, std::wstring str) { 
     186  strEx::splitList add = strEx::splitEx(str, _T("&")); 
     187  list.insert(list.begin(), add.begin(), add.end()); 
     188} 
     189 
     190check_nt::packet NSClientServer::handle(check_nt::packet p) { 
     191  std::wstring buffer = p.get_payload(); 
     192  NSC_DEBUG_MSG_STD(_T("Data: ") + buffer); 
     193 
     194  std::wstring::size_type pos = buffer.find_first_of(_T("\n\r")); 
     195  if (pos != std::wstring::npos) { 
     196    std::wstring::size_type pos2 = buffer.find_first_not_of(_T("\n\r"), pos); 
     197    if (pos2 != std::wstring::npos) { 
     198      std::wstring rest = buffer.substr(pos2); 
     199      NSC_DEBUG_MSG_STD(_T("Ignoring data: ") + rest); 
     200    } 
     201    buffer = buffer.substr(0, pos); 
     202  } 
     203 
     204  strEx::token pwd = strEx::getToken(buffer, '&'); 
     205  if (!isPasswordOk(pwd.first)) { 
     206    NSC_LOG_ERROR_STD(_T("Invalid password (") + pwd.first + _T(").")); 
     207    return check_nt::packet("ERROR: Invalid password."); 
     208  } 
     209  if (pwd.second.empty()) 
     210    return check_nt::packet("ERROR: No command specified."); 
     211  strEx::token cmd = strEx::getToken(pwd.second, '&'); 
     212  if (cmd.first.empty()) 
     213    return check_nt::packet("ERROR: No command specified."); 
     214 
     215  int c = boost::lexical_cast<int>(cmd.first.c_str()); 
     216 
     217  NSC_DEBUG_MSG_STD(_T("Data: ") + cmd.second); 
     218 
     219  std::list<std::wstring> args; 
     220 
     221  // prefix various commands 
     222  switch (c) { 
     223    case REQ_CPULOAD: 
     224      cmd.first = _T("checkCPU"); 
     225      split_to_list(args, cmd.second); 
     226      args.push_back(_T("nsclient")); 
     227      break; 
     228    case REQ_UPTIME: 
     229      cmd.first = _T("checkUpTime"); 
     230      args.push_back(_T("nsclient")); 
     231      break; 
     232    case REQ_USEDDISKSPACE: 
     233      cmd.first = _T("CheckDriveSize"); 
     234      split_to_list(args, cmd.second); 
     235      args.push_back(_T("nsclient")); 
     236      break; 
     237    case REQ_CLIENTVERSION: 
     238      { 
     239        //std::wstring v = SETTINGS_GET_STRING(nsclient::VERSION); 
     240        //if (v == _T("auto")) 
     241        std::wstring v = nscapi::plugin_singleton->get_core()->getApplicationName() + _T(" ") + nscapi::plugin_singleton->get_core()->getApplicationVersionString(); 
     242        return strEx::wstring_to_string(v); 
     243      } 
     244    case REQ_SERVICESTATE: 
     245      cmd.first = _T("checkServiceState"); 
     246      split_to_list(args, cmd.second); 
     247      args.push_back(_T("nsclient")); 
     248      break; 
     249    case REQ_PROCSTATE: 
     250      cmd.first = _T("checkProcState"); 
     251      split_to_list(args, cmd.second); 
     252      args.push_back(_T("nsclient")); 
     253      break; 
     254    case REQ_MEMUSE: 
     255      cmd.first = _T("checkMem"); 
     256      args.push_back(_T("nsclient")); 
     257      break; 
     258    case REQ_COUNTER: 
     259      cmd.first = _T("checkCounter"); 
     260      args.push_back(_T("Counter=") + cmd.second); 
     261      args.push_back(_T("nsclient")); 
     262      break; 
     263    case REQ_FILEAGE: 
     264      cmd.first = _T("getFileAge"); 
     265      args.push_back(_T("path=") + cmd.second); 
     266      break; 
     267    case REQ_INSTANCES: 
     268      cmd.first = _T("listCounterInstances"); 
     269      args.push_back(cmd.second); 
     270      break; 
     271 
     272 
     273    default: 
     274      split_to_list(args, cmd.second); 
     275  } 
     276 
     277  std::wstring message, perf; 
     278  NSCAPI::nagiosReturn ret = nscapi::core_helper::simple_query(cmd.first.c_str(), args, message, perf); 
     279  if (!nscapi::plugin_helper::isNagiosReturnCode(ret)) { 
     280    if (message.empty()) 
     281      return check_nt::packet("ERROR: Could not complete the request check log file for more information."); 
     282    return check_nt::packet("ERROR: " + strEx::wstring_to_string(message)); 
     283  } 
     284  switch (c) { 
     285    case REQ_UPTIME:    // Some check_nt commands has no return code syntax 
     286    case REQ_MEMUSE: 
     287    case REQ_CPULOAD: 
     288    case REQ_CLIENTVERSION: 
     289    case REQ_USEDDISKSPACE: 
     290    case REQ_COUNTER: 
     291    case REQ_FILEAGE: 
     292      return check_nt::packet(message); 
     293 
     294    case REQ_SERVICESTATE:  // Some check_nt commands return the return code (coded as a string) 
     295    case REQ_PROCSTATE: 
     296      return check_nt::packet(strEx::itos(nscapi::plugin_helper::nagios2int(ret)) + _T("& ") + message); 
     297 
     298    default:        // "New" check_nscp also returns performance data 
     299      if (perf.empty()) 
     300        return check_nt::packet(nscapi::plugin_helper::translateReturn(ret) + _T("&") + message); 
     301      return check_nt::packet(nscapi::plugin_helper::translateReturn(ret) + _T("&") + message + _T("&") + perf); 
     302  } 
     303 
     304  return check_nt::packet("FOO"); 
     305} 
     306 
     307 
     308 
    163309NSC_WRAP_DLL(); 
    164 NSC_WRAPPERS_MAIN_DEF(NSClientListener); 
     310NSC_WRAPPERS_MAIN_DEF(NSClientServer); 
    165311NSC_WRAPPERS_IGNORE_MSG_DEF(); 
    166312NSC_WRAPPERS_IGNORE_CMD_DEF(); 
  • modules/NSClientServer/NSClientServer.h

    r81e420c r9bd40e2  
    1919*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * 
    2020***************************************************************************/ 
    21 //#include <Socket.h> 
    2221#include <socket_helpers.hpp> 
    23 #include <check_nt/server/server.hpp> 
     22#include <check_nt/server/protocol.hpp> 
    2423 
    2524NSC_WRAPPERS_MAIN(); 
    2625 
    27 class NSClientListener : public nscapi::impl::simple_plugin { 
    28 private: 
    29  
    30   check_nt::server::server::connection_info info_; 
    31   boost::shared_ptr<check_nt::server::server> server_; 
    32  
     26class NSClientServer : public nscapi::impl::simple_plugin, public check_nt::server::handler { 
    3327public: 
    34   NSClientListener(); 
    35   virtual ~NSClientListener(); 
     28  NSClientServer(); 
     29  virtual ~NSClientServer(); 
    3630  // Module calls 
    3731  bool loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode); 
     
    5044    return _T("A simple server that listens for incoming NSClient (check_nt) connection and handles them.\nAlthough NRPE is the preferred method NSClient is fully supported and can be used for simplicity or for compatibility."); 
    5145  } 
     46 
     47 
     48  check_nt::packet handle(check_nt::packet packet); 
     49 
     50  check_nt::packet create_error(std::wstring msg) { 
     51    return check_nt::packet("ERROR: Failed to parse"); 
     52  } 
     53 
     54  void log_debug(std::string module, std::string file, int line, std::string msg) const { 
     55    if (GET_CORE()->should_log(NSCAPI::log_level::debug)) { 
     56      GET_CORE()->log(NSCAPI::log_level::debug, file, line, msg); 
     57    } 
     58  } 
     59  void log_error(std::string module, std::string file, int line, std::string msg) const { 
     60    if (GET_CORE()->should_log(NSCAPI::log_level::error)) { 
     61      GET_CORE()->log(NSCAPI::log_level::error, file, line, msg); 
     62    } 
     63  } 
     64 
     65  std::wstring get_password() const { 
     66    return password_; 
     67  } 
     68 
     69private: 
     70  void set_password(std::wstring password) { 
     71    password_ = password; 
     72  } 
     73  virtual void set_allow_arguments(bool v)  { 
     74    allowArgs_ = v; 
     75  } 
     76  virtual void set_allow_nasty_arguments(bool v) { 
     77    allowNasty_ = v; 
     78  } 
     79  virtual void set_perf_data(bool v) { 
     80    noPerfData_ = !v; 
     81  } 
     82  bool isPasswordOk(std::wstring remotePassword); 
     83 
     84private: 
     85  socket_helpers::connection_info info_; 
     86  boost::shared_ptr<check_nt::server::server> server_; 
     87 
     88  bool allowArgs_; 
     89  bool allowNasty_; 
     90  bool noPerfData_; 
     91  std::wstring password_; 
     92 
    5293}; 
  • modules/NSClientServer/handler_impl.hpp

    r8013c0c r9bd40e2  
    3030 
    3131 
    32   void log_debug(std::string file, int line, std::wstring msg) { 
     32  void log_debug(std::string module, std::string file, int line, std::string msg) { 
    3333    if (GET_CORE()->should_log(NSCAPI::log_level::debug)) { 
    3434      GET_CORE()->log(NSCAPI::log_level::debug, file, line, msg); 
    3535    } 
    3636  } 
    37   void log_error(std::string file, int line, std::wstring msg) { 
     37  void log_error(std::string module, std::string file, int line, std::string msg) { 
    3838    if (GET_CORE()->should_log(NSCAPI::log_level::error)) { 
    3939      GET_CORE()->log(NSCAPI::log_level::error, file, line, msg); 
Note: See TracChangeset for help on using the changeset viewer.