Changeset 438998b in nscp


Ignore:
Timestamp:
08/22/11 10:30:36 (21 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
bd18eb2
Parents:
fe75eff
Message:

Initial version of the NSCP protocol (very crude) also might not build on *nix as I haven't verified that yet...
Will improve this this week and hopefully have something better in a bit... But this works so thought Id commit it anyways if I break something :)

Files:
32 added
2 deleted
12 edited

Legend:

Unmodified
Added
Removed
  • include/nrpe/server/tcp_connection.hpp

    r294b37b r438998b  
    4141      boost::asio::ip::tcp::socket socket_; 
    4242    }; 
    43  
    44 /* 
    45     namespace socket_handlers { 
    46  
    47       class socket { 
    48       public: 
    49         typedef boost::asio::basic_socket<tcp,boost::asio::stream_socket_service<tcp> >  basic_socket_type; 
    50         virtual basic_socket_type& get() = 0; 
    51       }; 
    52  
    53       class normal_socket : public socket { 
    54       public: 
    55         socket::basic_socket_type& get() { 
    56           return socket_; 
    57         } 
    58         normal_socket(boost::asio::io_service& io_service)  
    59           : socket_(io_service) 
    60         {} 
    61       private: 
    62         basic_socket_type socket_; 
    63       }; 
    64       class ssl_socket : public socket { 
    65       public: 
    66         socket::basic_socket_type& get() { 
    67           return socket_.lowest_layer(); 
    68         } 
    69          
    70         ssl_socket(boost::asio::io_service& io_service, boost::asio::ssl::context &context)  
    71           : socket_(io_service, context) 
    72         {} 
    73       private: 
    74         typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_type; 
    75         socket_type socket_; 
    76       }; 
    77  
    78     } 
    79     /// Represents a single connection from a client. 
    80     class connection_base : public boost::enable_shared_from_this<connection_base>, private boost::noncopyable { 
    81     public: 
    82       /// Construct a connection with the given io_service. 
    83       explicit connection_base(boost::asio::io_service& io_service, nrpe::server::socket_handlers::socket* socket, nrpe::server::handler& handler); 
    84       virtual ~connection_base() { 
    85         handler_.log_debug(__FILEW__, __LINE__, _T("Destroying socket...")); 
    86       } 
    87  
    88       /// Get the socket associated with the connection. 
    89       nrpe::server::socket_handlers::socket::basic_socket_type& socket(); 
    90  
    91       /// Start the first asynchronous operation for the connection. 
    92       void start(); 
    93       void start_ssl(); 
    94  
    95     private: 
    96       /// Handle completion of a read operation. 
    97       void handle_read(const boost::system::error_code& e, std::size_t bytes_transferred); 
    98  
    99       /// Handle completion of a write operation. 
    100       void handle_write(const boost::system::error_code& e); 
    101  
    102       void handle_handshake(const boost::system::error_code& error); 
    103  
    104       /// Strand to ensure the connection's handlers are not called concurrently. 
    105       boost::asio::io_service::strand strand_; 
    106  
    107       /// Socket for the connection. 
    108       //socket_handler socket_; 
    109       boost::shared_ptr<nrpe::server::socket_handlers::socket> socket_; 
    110  
    111       /// The handler used to process the incoming request. 
    112       //request_handler& request_handler_; 
    113  
    114       typedef boost::array<char, 8192> buffer_type; 
    115       /// Buffer for incoming data. 
    116       buffer_type buffer_; 
    117  
    118       /// The incoming request. 
    119       //request request_; 
    120  
    121       /// The parser for the incoming request. 
    122       nrpe::server::handler &handler_; 
    123       nrpe::server::parser parser_; 
    124       //request_parser request_parser_; 
    125  
    126       /// The reply to be sent back to the client. 
    127       //reply reply_; 
    128  
    129     }; 
    130 */ 
    131  
    132 /* 
    133     class ssl_connection : public boost::enable_shared_from_this<ssl_connection>, private boost::noncopyable { 
    134     private: 
    135       typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket; 
    136       connection_ptr connection_; 
    137     public: 
    138  
    139       explicit ssl_connection(boost::asio::io_service& io_service, boost::asio::ssl::context &context, nrpe::server::handler& handler); 
    140       virtual ~ssl_connection() { 
    141         handler_.log_debug(__FILEW__, __LINE__, _T("Destroying SSL socket...")); 
    142       } 
    143  
    144       ssl_socket::lowest_layer_type& socket(); 
    145       void start(); 
    146  
    147     private: 
    148       ssl_socket socket_; 
    149       nrpe::server::handler &handler_; 
    150  
    151     }; 
    152  
    153     typedef boost::shared_ptr<ssl_connection> ssl_connection_ptr; 
    154 */ 
    155  
    15643  } // namespace server 
    15744} // namespace nrpe 
  • include/nscapi/functions.hpp

    rfe75eff r438998b  
    153153    } 
    154154 
     155    static void create_simple_query_request(std::wstring command, std::vector<std::wstring> arguments, std::string &buffer) { 
     156      PluginCommand::RequestMessage message; 
     157      ::PluginCommand::Header* header = message.mutable_header(); 
     158 
     159      header->set_type(PluginCommand::Header_Type_REQUEST); 
     160      header->set_version(PluginCommand::Header_Version_VERSION_1); 
     161 
     162      PluginCommand::Request *payload = message.add_payload(); 
     163      payload->set_command(to_string(command)); 
     164 
     165      BOOST_FOREACH(std::wstring s, arguments) 
     166        payload->add_arguments(to_string(s)); 
     167 
     168      payload->set_version(PluginCommand::Request_Version_VERSION_1); 
     169      message.SerializeToString(&buffer); 
     170    } 
     171 
     172    static void create_simple_query_result(NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer) { 
     173      PluginCommand::ResponseMessage message; 
     174      ::PluginCommand::Header* header = message.mutable_header(); 
     175 
     176      header->set_type(PluginCommand::Header_Type_RESPONSE); 
     177      header->set_version(PluginCommand::Header_Version_VERSION_1); 
     178 
     179      PluginCommand::Response *payload = message.add_payload(); 
     180      payload->set_message(to_string(msg)); 
     181      if (!perf.empty()) 
     182        parse_performance_data(payload, perf); 
     183 
     184      payload->set_version(PluginCommand::Response_Version_VERSION_1); 
     185      message.SerializeToString(&buffer); 
     186    } 
     187 
     188 
    155189    static decoded_simple_command_data process_simple_command_request(const wchar_t* char_command, const std::string &request) { 
    156190      decoded_simple_command_data data; 
  • include/socket/socket_helpers.cpp

    rb9498ef r438998b  
    7878 
    7979void socket_helpers::io::set_result(boost::optional<boost::system::error_code>* a, boost::system::error_code b) { 
    80   a->reset(b); 
     80  if (!b) { 
     81    a->reset(b); 
     82  } else { 
     83    std::cout << "timer aborted incorrectly: " << b.message() << std::endl; 
     84  } 
    8185} 
  • include/socket/socket_helpers.hpp

    rb9498ef r438998b  
    117117    void set_result(boost::optional<boost::system::error_code>* a, boost::system::error_code b); 
    118118 
    119     template <typename AsyncReadStream, typename RawSocket, typename MutableBufferSequence> 
    120     void read_with_timeout(AsyncReadStream& sock, RawSocket& rawSocket, const MutableBufferSequence& buffers, boost::posix_time::time_duration duration) { 
     119    struct timed_writer : boost::noncopyable { 
     120      boost::asio::io_service &io_service; 
     121      boost::posix_time::time_duration duration; 
     122      boost::asio::deadline_timer timer; 
     123 
     124      boost::optional<boost::system::error_code> timer_result; 
     125      boost::optional<boost::system::error_code> read_result; 
     126 
     127      timed_writer(boost::asio::io_service &io_service, boost::posix_time::time_duration duration) 
     128        : io_service(io_service)  
     129        , timer(io_service) 
     130      { 
     131        timer.expires_from_now(duration); 
     132        timer.async_wait(boost::bind(set_result, &timer_result, _1)); 
     133      } 
     134      ~timed_writer() { 
     135        timer.cancel(); 
     136      } 
     137 
     138      template <typename AsyncWriteStream, typename MutableBufferSequence> 
     139      void write(AsyncWriteStream& socket, MutableBufferSequence &buffer) { 
     140        async_write(socket, buffer, boost::bind(set_result, &read_result, _1)); 
     141      } 
     142 
     143      template <typename AsyncWriteStream, typename RawSocket, typename MutableBufferSequence> 
     144      bool write_and_wait(AsyncWriteStream& sock, RawSocket& rawSocket, const MutableBufferSequence& buffer) { 
     145        write(sock, buffer); 
     146        return wait(rawSocket); 
     147      } 
     148 
     149      template <typename RawSocket> 
     150      bool wait(RawSocket& socket) { 
     151        io_service.reset(); 
     152        while (io_service.run_one()) { 
     153          if (read_result) { 
     154            return true; 
     155          } 
     156          else if (timer_result) { 
     157            socket.close(); 
     158            return false; 
     159          } 
     160        } 
     161      } 
     162    }; 
     163 
     164 
     165    template <typename AsyncWriteStream, typename RawSocket, typename MutableBufferSequence> 
     166    void write_with_timeout(AsyncWriteStream& sock, RawSocket& rawSocket, const MutableBufferSequence& buffers, boost::posix_time::time_duration duration) { 
    121167      boost::optional<boost::system::error_code> timer_result; 
    122168      boost::asio::deadline_timer timer(sock.get_io_service()); 
     
    125171 
    126172      boost::optional<boost::system::error_code> read_result; 
    127       async_read(sock, buffers, boost::bind(set_result, &read_result, _1)); 
     173      async_write(sock, buffers, boost::bind(set_result, &read_result, _1)); 
    128174 
    129175      sock.get_io_service().reset(); 
     
    137183      if (*read_result) 
    138184        throw boost::system::system_error(*read_result); 
    139     }  
    140  
    141     template <typename AsyncWriteStream, typename RawSocket, typename MutableBufferSequence> 
    142     void write_with_timeout(AsyncWriteStream& sock, RawSocket& rawSocket, const MutableBufferSequence& buffers, boost::posix_time::time_duration duration) { 
     185    } 
     186 
     187 
     188    struct timed_reader : boost::noncopyable { 
     189      boost::asio::io_service &io_service; 
     190      boost::posix_time::time_duration duration; 
     191      boost::asio::deadline_timer timer; 
     192 
     193      boost::optional<boost::system::error_code> timer_result; 
     194      boost::optional<boost::system::error_code> write_result; 
     195 
     196      timed_reader(boost::asio::io_service &io_service, boost::posix_time::time_duration duration) 
     197        : io_service(io_service)  
     198        , timer(io_service) 
     199      { 
     200        timer.expires_from_now(duration); 
     201        timer.async_wait(boost::bind(set_result, &timer_result, _1)); 
     202      } 
     203      ~timed_reader() { 
     204        timer.cancel(); 
     205      } 
     206 
     207      template <typename AsyncWriteStream, typename MutableBufferSequence> 
     208      void read(AsyncWriteStream& socket, MutableBufferSequence &buffers) { 
     209        async_read(socket, buffers, boost::bind(set_result, &write_result, _1)); 
     210      } 
     211 
     212      template <typename AsyncWriteStream, typename MutableBufferSequence> 
     213      bool read_and_wait(AsyncWriteStream& sock, MutableBufferSequence& buffers) { 
     214        read(sock, buffers); 
     215        return wait(); 
     216      } 
     217      bool wait() { 
     218        io_service.reset(); 
     219        while (io_service.run_one()) { 
     220          if (write_result) { 
     221            std::cout << "---read---" << std::endl; 
     222            //timer.cancel(); 
     223            return true; 
     224          } 
     225          else if (timer_result) { 
     226            std::cout << "---timer (read)---" << std::endl; 
     227            //socket.close(); 
     228            return false; 
     229          } 
     230        } 
     231      } 
     232    }; 
     233 
     234 
     235    template <typename AsyncReadStream, typename RawSocket, typename MutableBufferSequence> 
     236    void read_with_timeout(AsyncReadStream& sock, RawSocket& rawSocket, const MutableBufferSequence& buffers, boost::posix_time::time_duration duration) { 
    143237      boost::optional<boost::system::error_code> timer_result; 
    144238      boost::asio::deadline_timer timer(sock.get_io_service()); 
     
    147241 
    148242      boost::optional<boost::system::error_code> read_result; 
    149       async_write(sock, buffers, boost::bind(set_result, &read_result, _1)); 
     243      async_read(sock, buffers, boost::bind(set_result, &read_result, _1)); 
    150244 
    151245      sock.get_io_service().reset(); 
     
    160254        throw boost::system::system_error(*read_result); 
    161255    } 
    162  
    163256  } 
    164257} 
  • include/strEx.h

    rb9498ef r438998b  
    136136  } 
    137137 
     138  inline std::string strip_hex(std::vector<char> str) { 
     139    std::string ret; ret.reserve(str.size()); 
     140    BOOST_FOREACH(char c, str) 
     141    { 
     142      if (c==0||c==7||c==10||c==11||c==12||c==13||c==127) 
     143        ret.push_back('?'); 
     144      else 
     145        ret.push_back(c); 
     146    } 
     147    return ret; 
     148  } 
     149 
    138150  inline void append_list(std::wstring &lst, std::wstring &append, std::wstring sep = _T(", ")) { 
    139151    if (append.empty()) 
     
    179191    std::string chars; 
    180192    for (unsigned int i=0;i<len;i++) { 
     193      if (i%32==0) { 
     194        if (i > 0) { 
     195          ss << chars; 
     196          ss << "\n"; 
     197        } 
     198        chars = ""; 
     199        ss << std::hex << std::setw(8) << std::setfill('0') << i; 
     200        ss << ": "; 
     201      } 
     202      ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(static_cast<unsigned char>(buf[i])); 
     203      ss << ", "; 
     204      if (buf[i] < 30 || buf[i] == 127) 
     205        chars += '?'; 
     206      else 
     207        chars += buf[i]; 
     208    } 
     209    return ss.str(); 
     210  } 
     211  inline std::string format_buffer(const std::vector<char> &buf) { 
     212    std::stringstream ss; 
     213    std::string chars; 
     214    for (unsigned int i=0;i<buf.size();i++) { 
    181215      if (i%32==0) { 
    182216        if (i > 0) { 
  • libs/protobuf/CMakeLists.txt

    r39c73cd r438998b  
    55WRAP_PROTO(PROTO_EXEC_SRC exec.proto) 
    66WRAP_PROTO(PROTO_SETTINGS_SRC settings.proto) 
     7WRAP_PROTO(PROTO_ENVELOPE_SRC envelope.proto) 
    78 
    89SET(TARGET protobuf) 
     
    1314  exec.proto 
    1415  settings.proto 
     16  envelope.proto 
    1517   
    1618  ${PROTO_PLUGIN_SRC} 
     
    1820  ${PROTO_EXEC_SRC} 
    1921  ${PROTO_SETTINGS_SRC} 
     22  ${PROTO_ENVELOPE_SRC} 
    2023) 
    2124 
  • libs/protobuf/settings.proto

    r39c73cd r438998b  
    1 package PluginCommand; 
     1package SettingsCommand; 
    22 
    33message Header { 
  • modules/NRPEClient/CMakeLists.txt

    rb9498ef r438998b  
    3939) 
    4040INCLUDE(${BUILD_CMAKE_FOLDER}/module.cmake) 
     41SOURCE_GROUP("Server" REGULAR_EXPRESSION .*include/nrpe/.*) 
  • modules/NRPEClient/NRPEClient.cpp

    rfe75eff r438998b  
    110110 
    111111    settings.alias().add_path_to_settings() 
    112 //      (_T("EXTERNAL SCRIPT SECTION"), _T("Section for external scripts configuration options (CheckExternalScripts).")) 
    113112 
    114113      (_T("handlers"), sh::fun_values_path(boost::bind(&NRPEClient::add_command, this, _1, _2)),  
     
    238237 
    239238int NRPEClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 
    240   if (command != _T("query") && command != _T("help")) 
     239  if (command != _T("query_nrpe") && command != _T("help")) 
    241240    return NSCAPI::returnIgnored; 
    242241  try { 
  • modules/NRPEServer/CMakeLists.txt

    rb9498ef r438998b  
    5353) 
    5454INCLUDE(${BUILD_CMAKE_FOLDER}/module.cmake) 
     55SOURCE_GROUP("Server" REGULAR_EXPRESSION .*include/nrpe/.*) 
  • modules/NRPEServer/handler_impl.cpp

    r2c95d22 r438998b  
    5656  } 
    5757  if (data.length() >= p.get_payload_length()-1) { 
    58     NSC_LOG_ERROR(_T("Truncating returndata as it is bigger then NRPE allowes :(")); 
     58    //NSC_LOG_ERROR(_T("Truncating returndata as it is bigger then NRPE allowes :(")); 
    5959    data = data.substr(0,p.get_payload_length()-2); 
    6060  } 
  • service/cli_parser.hpp

    r2c95d22 r438998b  
    308308        core_->initCore(false); 
    309309      } 
     310      int ret = 0; 
    310311      std::vector<std::wstring> resp; 
    311       mainClient.simple_exec(module, command, arguments, resp); 
     312      if (mainClient.simple_exec(module, command, arguments, resp) == NSCAPI::returnIgnored) { 
     313        ret = 1; 
     314        std::wcout << _T("No handler for that command: ") << command << std::endl; 
     315      } 
    312316      mainClient.exitCore(false); 
    313317 
     
    315319        std::wcout << r << std::endl; 
    316320      } 
    317       return 0; 
     321      return ret; 
    318322    } catch(std::exception & e) { 
    319323      mainClient.log_error(__FILE__, __LINE__, std::string("Unable to parse command line (settings): ") + e.what()); 
Note: See TracChangeset for help on using the changeset viewer.