Changeset 4c8d44d in nscp
- Timestamp:
- 02/20/08 21:04:27 (5 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2, stable
- Children:
- 140dd49
- Parents:
- 5b9d247
- Files:
-
- 6 edited
-
NSC.dist (modified) (2 diffs)
-
changelog (modified) (2 diffs)
-
include/config.h (modified) (1 diff)
-
modules/NRPEClient/NRPEClient.cpp (modified) (3 diffs)
-
modules/NRPEClient/NRPEClient.h (modified) (4 diffs)
-
modules/NSCAAgent/NSCAAgent.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
NSC.dist
r394f7a1 r4c8d44d 26 26 ; Script to check external scripts and/or internal aliases, early beta. 27 27 ;CheckExternalScripts.dll 28 ; Check other hosts through NRPE extreme beta and probably a bit dangerous! :) 29 ;NRPEClient.dll 30 28 31 29 32 … … 274 277 ;check_nok=scripts\xlong.bat 275 278 ;check_vbs=cscript.exe //T:30 //NoLogo scripts\check_vb.vbs 279 280 ;# REMOTE NRPE PROXY COMMANDS 281 ; A list of commands that check other hosts. 282 ; Used by the NRPECLient module 283 [NRPE Client Handlers] 284 check_other=-H 192.168.0.1 -p 5666 -c remote_command -a arguments 285 -
changelog
r2f01f93 r4c8d44d 12 12 Usage: nsclient++ -noboot NRPEClient -H 192.168.0.1 -p 5666 -c check_something -a foo bar 13 13 This is an early concept so don't expect much... 14 * Fixed a bug in NSCA module (now it works again :) 15 + Added a command wrapper for the NRPECLient module so now it can act as a check command. 16 (No argument handling yet though), For a sample check out the [NRPE Client Handlers] section in NSC.ini 14 17 15 18 2008-02-19 MickeM … … 17 20 18 21 2008-02-18 MickeM 19 + Added proper output handling to process subsystem (now you can execute programs t at return "much" data.22 + Added proper output handling to process subsystem (now you can execute programs that return "much" data. 20 23 + Added select support for SSL_write (now you can send "any amount of data" to the (SSL) socket. 21 24 Since check_nrpe doesn't do this it wont work in that end, but still... -
include/config.h
r394f7a1 r4c8d44d 83 83 // NRPE Settings headlines 84 84 #define NRPE_SECTION_TITLE _T("NRPE") 85 #define NRPE_CLIENT_HANDLER_SECTION_TITLE _T("NRPE Client Handlers") 85 86 #define NRPE_HANDLER_SECTION_TITLE _T("NRPE Handlers") 86 87 #define NRPE_SETTINGS_TIMEOUT _T("command_timeout") -
modules/NRPEClient/NRPEClient.cpp
r2f01f93 r4c8d44d 43 43 44 44 bool NRPEClient::loadModule() { 45 timeout = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_TIMEOUT ,NRPE_SETTINGS_TIMEOUT_DEFAULT);46 socketTimeout_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_READ_TIMEOUT ,NRPE_SETTINGS_READ_TIMEOUT_DEFAULT);47 45 buffer_length_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_STRLEN, NRPE_SETTINGS_STRLEN_DEFAULT); 46 47 std::list<std::wstring> commands = NSCModuleHelper::getSettingsSection(NRPE_CLIENT_HANDLER_SECTION_TITLE); 48 NSC_DEBUG_MSG_STD(_T("humm...")); 49 for (std::list<std::wstring>::const_iterator it = commands.begin(); it != commands.end(); ++it) { 50 NSC_DEBUG_MSG_STD(*it); 51 std::wstring s = NSCModuleHelper::getSettingsString(NRPE_CLIENT_HANDLER_SECTION_TITLE, (*it), _T("")); 52 if (s.empty()) { 53 NSC_LOG_ERROR_STD(_T("Invalid NRPE-client entry: ") + (*it)); 54 } else { 55 addCommand((*it).c_str(), s); 56 } 57 } 48 58 return true; 49 59 } 60 61 void NRPEClient::addCommand(strEx::blindstr key, std::wstring args) { 62 NSC_LOG_ERROR_STD(_T("Parsing: ") + args); 63 try { 64 boost::program_options::options_description desc = get_optionDesc(); 65 boost::program_options::positional_options_description p = get_optionsPositional(); 66 67 boost::program_options::variables_map vm; 68 boost::program_options::store( 69 basic_command_line_parser_ex<TCHAR>(args).options(desc).positional(p).run() 70 , vm); 71 boost::program_options::notify(vm); 72 nrpe_connection_data cd = get_ConectionData(vm); 73 NSC_DEBUG_MSG_STD(_T("Parsed CLI for: ") + key.c_str() + _T(" = ") + cd.toString()); 74 commands[key] = cd; 75 } catch (boost::program_options::validation_error &e) { 76 NSC_LOG_ERROR_STD(_T("Could not parse: ") + key.c_str() + strEx::string_to_wstring(e.what())); 77 } catch (...) { 78 NSC_LOG_ERROR_STD(_T("Could not parse: ") + key.c_str()); 79 } 80 } 81 50 82 bool NRPEClient::unloadModule() { 51 83 return true; … … 60 92 NSCAPI::nagiosReturn NRPEClient::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf) 61 93 { 62 return 0; 63 } 64 65 66 94 command_list::const_iterator cit = commands.find(command); 95 if (cit == commands.end()) 96 return NSCAPI::returnIgnored; 97 nrpe_result_data r = execute_nrpe_command((*cit).second); 98 message = r.text; 99 return r.result; 100 } 101 102 103 boost::program_options::options_description NRPEClient::get_optionDesc() { 104 boost::program_options::options_description desc("Allowed options"); 105 buffer_length_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_STRLEN, NRPE_SETTINGS_STRLEN_DEFAULT); 106 desc.add_options() 107 ("help,h", "Show this help message.") 108 ("host,H", boost::program_options::wvalue<std::wstring>(), "The address of the host running the NRPE daemon") 109 ("port,p", boost::program_options::value<int>(), "The port on which the daemon is running (default=5666)") 110 ("command,c", boost::program_options::wvalue<std::wstring>(), "The name of the command that the remote daemon should run") 111 ("timeout,t", boost::program_options::value<int>(), "Number of seconds before connection times out (default=10)") 112 ("buffer-length,l", boost::program_options::value<int>(), std::string("Length of payload (has to be same as on the server (default=" + strEx::s::itos(buffer_length_) + ")").c_str()) 113 ("no-ssl,n", "Do not initial an ssl handshake with the server, talk in plaintext.") 114 ("arguments,a", boost::program_options::wvalue<std::vector<std::wstring>>(), "list of arguments") 115 ; 116 return desc; 117 } 118 boost::program_options::positional_options_description NRPEClient::get_optionsPositional() { 119 boost::program_options::positional_options_description p; 120 p.add("arguments", -1); 121 return p; 122 } 123 NRPEClient::nrpe_connection_data NRPEClient::get_ConectionData(boost::program_options::variables_map &vm) { 124 nrpe_connection_data ret(buffer_length_); 125 if (vm.count("host")) 126 ret.host = vm["host"].as<std::wstring>(); 127 if (vm.count("port")) 128 ret.port = vm["port"].as<int>(); 129 if (vm.count("timeout")) 130 ret.timeout = vm["timeout"].as<int>(); 131 if (vm.count("buffer-length")) 132 ret.buffer_length = vm["buffer-length"].as<int>(); 133 if (vm.count("command")) 134 ret.command = vm["command"].as<std::wstring>(); 135 if (vm.count("arguments")) { 136 std::vector<std::wstring> v = vm["arguments"].as<std::vector<std::wstring>>(); 137 for (std::vector<std::wstring>::const_iterator cit = v.begin(); cit != v.end(); ++cit) { 138 if (!ret.arguments.empty()) 139 ret.arguments += _T("!"); 140 ret.arguments += *cit; 141 } 142 } 143 if (vm.count("no-ssl")) 144 ret.ssl = false; 145 return ret; 146 } 67 147 int NRPEClient::commandLineExec(const TCHAR* command, const unsigned int argLen, TCHAR** args) { 68 try { 69 boost::program_options::options_description desc("Allowed options"); 70 buffer_length_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_STRLEN, NRPE_SETTINGS_STRLEN_DEFAULT); 71 desc.add_options() 72 ("help,h", "Show this help message.") 73 ("host,H", boost::program_options::wvalue<std::wstring>(), "The address of the host running the NRPE daemon") 74 ("port,p", boost::program_options::value<int>(), "The port on which the daemon is running (default=5666)") 75 ("command,c", boost::program_options::wvalue<std::wstring>(), "The name of the command that the remote daemon should run") 76 ("timeout,t", boost::program_options::value<int>(), "Number of seconds before connection times out (default=10)") 77 ("buffer-length,l", boost::program_options::value<int>(), std::string("Length of payload (has to be same as on the server (default=" + strEx::s::itos(buffer_length_) + ")").c_str()) 78 ("no-ssl,n", "Do not initial an ssl handshake with the server, talk in plaintext.") 79 ("arguments,a", boost::program_options::wvalue<std::vector<std::wstring>>(), "list of arguments") 80 ; 81 boost::program_options::positional_options_description p; 82 p.add("arguments", -1); 83 84 boost::program_options::variables_map vm; 85 boost::program_options::store( 86 basic_command_line_parser_ex<TCHAR>(command, argLen, args).options(desc).positional(p).run() 87 , vm); 88 boost::program_options::notify(vm); 89 90 if (vm.count("help")) { 91 std::cout << desc << "\n"; 92 return 1; 93 } 94 std::wstring host = _T("localhost"); 95 std::wstring command; 96 std::wstring arguments; 97 int port = 5666; 98 int timeout = 10; 99 bool ssl = true; 100 101 if (vm.count("host")) 102 host = vm["host"].as<std::wstring>(); 103 if (vm.count("port")) 104 port = vm["port"].as<int>(); 105 if (vm.count("timeout")) 106 timeout = vm["timeout"].as<int>(); 107 if (vm.count("buffer-length")) 108 buffer_length_ = vm["buffer-length"].as<int>(); 109 if (vm.count("command")) 110 command = vm["command"].as<std::wstring>(); 111 if (vm.count("arguments")) { 112 std::vector<std::wstring> v = vm["arguments"].as<std::vector<std::wstring>>(); 113 for (std::vector<std::wstring>::const_iterator cit = v.begin(); cit != v.end(); ++cit) { 114 if (!arguments.empty()) 115 arguments += _T("!"); 116 arguments += *cit; 117 } 118 } 119 if (vm.count("no-ssl")) 120 ssl = false; 121 return execute_nrpe_command(host, port, ssl, timeout, command, arguments); 122 } catch (boost::program_options::validation_error &e) { 123 std::cout << e.what() << std::endl; 124 } catch (...) { 125 std::cout << "Unknown exception parsing command line" << std::endl; 148 try { 149 boost::program_options::options_description desc = get_optionDesc(); 150 boost::program_options::positional_options_description p = get_optionsPositional(); 151 152 boost::program_options::variables_map vm; 153 boost::program_options::store( 154 basic_command_line_parser_ex<TCHAR>(command, argLen, args).options(desc).positional(p).run() 155 , vm); 156 boost::program_options::notify(vm); 157 158 if (vm.count("help")) { 159 std::cout << desc << "\n"; 160 return 1; 126 161 } 127 return 0; 128 } 129 int NRPEClient::execute_nrpe_command(std::wstring host, int port, bool ssl, int timeout, std::wstring command, std::wstring arguments) { 162 163 nrpe_result_data result = execute_nrpe_command(get_ConectionData(vm)); 164 std::wcout << result.text << std::endl; 165 return result.result; 166 } catch (boost::program_options::validation_error &e) { 167 std::cout << e.what() << std::endl; 168 } catch (...) { 169 std::cout << "Unknown exception parsing command line" << std::endl; 170 } 171 return NSCAPI::returnUNKNOWN; 172 } 173 NRPEClient::nrpe_result_data NRPEClient::execute_nrpe_command(nrpe_connection_data con) { 130 174 try { 131 std::wstring cmd = command;132 if (cmd.empty())133 cmd = _T("_NRPE_CHECK");134 if (!arguments.empty())135 cmd += _T("!") + arguments;136 175 NRPEPacket packet; 137 if ( ssl)138 packet = send_ssl( host, port, timeout, NRPEPacket::make_request(cmd, buffer_length_));176 if (con.ssl) 177 packet = send_ssl(con.host, con.port, con.timeout, NRPEPacket::make_request(con.get_cli(), con.buffer_length)); 139 178 else 140 packet = send_nossl(host, port, timeout, NRPEPacket::make_request(cmd, buffer_length_)); 141 std::wcout << packet.getPayload() << std::endl; 142 return packet.getResult(); 179 packet = send_nossl(con.host, con.port, con.timeout, NRPEPacket::make_request(con.get_cli(), con.buffer_length)); 180 return nrpe_result_data(packet.getResult(), packet.getPayload()); 143 181 } catch (simpleSocket::SocketException &e) { 144 std::wcout << _T("whoops...") << e.getMessage() << std::endl;182 return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("Socket error: ") + e.getMessage()); 145 183 } catch (simpleSSL::SSLException &e) { 146 std::wcout << _T("whoops...") << e.getMessage() << std::endl;184 return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("SSL Socket error: ") + e.getMessage()); 147 185 } catch (...) { 148 std::cout << "whoops..." << std::endl; 149 } 150 return NSCAPI::returnUNKNOWN; 186 return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("Unknown error")); 187 } 151 188 } 152 189 NRPEPacket NRPEClient::send_ssl(std::wstring host, int port, int timeout, NRPEPacket packet) … … 174 211 175 212 176 NRPEPacket NRPEClient::handlePacket(NRPEPacket p) { 177 if (p.getType() != NRPEPacket::queryPacket) { 178 NSC_LOG_ERROR(_T("Request is not a query.")); 179 throw NRPEException(_T("Invalid query type")); 180 } 181 if (p.getVersion() != NRPEPacket::version2) { 182 NSC_LOG_ERROR(_T("Request had unsupported version.")); 183 throw NRPEException(_T("Invalid version")); 184 } 185 if (!p.verifyCRC()) { 186 NSC_LOG_ERROR(_T("Request had invalid checksum.")); 187 throw NRPEException(_T("Invalid checksum")); 188 } 189 strEx::token cmd = strEx::getToken(p.getPayload(), '!'); 190 if (cmd.first == _T("_NRPE_CHECK")) { 191 return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnOK, _T("I (") SZVERSION _T(") seem to be doing fine..."), buffer_length_); 192 } 193 std::wstring msg, perf; 194 195 if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_ARGUMENTS, NRPE_SETTINGS_ALLOW_ARGUMENTS_DEFAULT) == 0) { 196 if (!cmd.second.empty()) { 197 NSC_LOG_ERROR(_T("Request contained arguments (not currently allowed, check the allow_arguments option).")); 198 throw NRPEException(_T("Request contained arguments (not currently allowed, check the allow_arguments option).")); 199 } 200 } 201 if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_NASTY_META, NRPE_SETTINGS_ALLOW_NASTY_META_DEFAULT) == 0) { 202 if (cmd.first.find_first_of(NASTY_METACHARS) != std::wstring::npos) { 203 NSC_LOG_ERROR(_T("Request command contained illegal metachars!")); 204 throw NRPEException(_T("Request command contained illegal metachars!")); 205 } 206 if (cmd.second.find_first_of(NASTY_METACHARS) != std::wstring::npos) { 207 NSC_LOG_ERROR(_T("Request arguments contained illegal metachars!")); 208 throw NRPEException(_T("Request command contained illegal metachars!")); 209 } 210 } 211 212 NSCAPI::nagiosReturn ret = -3; 213 try { 214 ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first, cmd.second, '!', msg, perf); 215 } catch (...) { 216 return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnUNKNOWN, _T("UNKNOWN: Internal exception"), buffer_length_); 217 } 218 switch (ret) { 219 case NSCAPI::returnInvalidBufferLen: 220 msg = _T("UNKNOWN: Return buffer to small to handle this command."); 221 ret = NSCAPI::returnUNKNOWN; 222 break; 223 case NSCAPI::returnIgnored: 224 msg = _T("UNKNOWN: No handler for that command"); 225 ret = NSCAPI::returnUNKNOWN; 226 break; 227 case NSCAPI::returnOK: 228 case NSCAPI::returnWARN: 229 case NSCAPI::returnCRIT: 230 case NSCAPI::returnUNKNOWN: 231 break; 232 default: 233 msg = _T("UNKNOWN: Internal error."); 234 ret = NSCAPI::returnUNKNOWN; 235 } 236 if (msg.length() > buffer_length_) { 237 NSC_LOG_ERROR(_T("Truncating returndata as it is bigger then NRPE allowes :(")); 238 msg = msg.substr(0,buffer_length_-1); 239 } 240 if (perf.empty()||noPerfData_) { 241 return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg, buffer_length_); 242 } else { 243 return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg + _T("|") + perf, buffer_length_); 244 } 245 } 213 246 214 247 215 NSC_WRAPPERS_MAIN_DEF(gNRPEClient); -
modules/NRPEClient/NRPEClient.h
r2f01f93 r4c8d44d 25 25 #include <map> 26 26 #include <nrpe/NRPEPacket.hpp> 27 #include <boost/program_options.hpp> 28 27 29 28 30 class NRPEClient { … … 31 33 inject, script, script_dir, 32 34 } command_type; 33 struct command_data { 34 command_data() : type(inject) {} 35 command_data(command_type type_, std::wstring arguments_) : type(type_), arguments(arguments_) {} 36 command_type type; 35 struct nrpe_connection_data { 36 std::wstring host; 37 std::wstring command; 37 38 std::wstring arguments; 39 std::wstring command_line; 40 int port; 41 int timeout; 42 unsigned int buffer_length; 43 bool ssl; 44 nrpe_connection_data(unsigned int buffer_length_ = 1024) 45 : host(_T("localhost")), 46 port(5666), 47 timeout(10), 48 ssl(true), 49 buffer_length(buffer_length_) 50 {} 51 std::wstring get_cli() { 52 if (command_line.empty()) { 53 command_line = command; 54 if (command_line.empty()) 55 command_line = _T("_NRPE_CHECK"); 56 if (!arguments.empty()) 57 command_line += _T("!") + arguments; 58 } 59 return command_line; 60 } 61 std::wstring toString() { 62 std::wstringstream ss; 63 ss << _T("host: ") << host; 64 ss << _T(", port: ") << port; 65 ss << _T(", timeout: ") << timeout; 66 ss << _T(", ssl: ") << ssl; 67 ss << _T(", buffer_length: ") << buffer_length; 68 return ss.str(); 69 } 38 70 }; 39 bool bUseSSL_; 40 simpleSSL::Listener socket_ssl_; 41 simpleSocket::Listener<> socket_; 42 typedef std::map<strEx::blindstr, command_data> command_list; 71 struct nrpe_result_data { 72 nrpe_result_data() {} 73 nrpe_result_data(int result_, std::wstring text_) : result(result_), text(text_) {} 74 std::wstring text; 75 int result; 76 }; 77 typedef std::map<strEx::blindstr, nrpe_connection_data> command_list; 43 78 command_list commands; 44 unsigned int timeout;45 unsigned int socketTimeout_;46 socketHelpers::allowedHosts allowedHosts;47 bool noPerfData_;48 std::wstring scriptDirectory_;49 79 unsigned int buffer_length_; 50 80 … … 65 95 } 66 96 std::wstring getModuleDescription() { 67 return _T("A simple server that listens for incoming NRPE connection and handles them.\nNRPE is preferred over NSClient as it is more flexible. You can of cource use both NSClient andNRPE.");97 return _T("A simple client for NRPE."); 68 98 } 69 99 … … 75 105 76 106 private: 77 int execute_nrpe_command(std::wstring host, int port, bool ssl, int timeout, std::wstring command, std::wstring arguments);107 nrpe_result_data execute_nrpe_command(nrpe_connection_data con); 78 108 NRPEPacket send_nossl(std::wstring host, int port, int timeout, NRPEPacket packet); 79 109 NRPEPacket send_ssl(std::wstring host, int port, int timeout, NRPEPacket packet); 80 class NRPEException { 81 std::wstring error_; 82 public: 83 /* NRPESocketException(simpleSSL::SSLException e) { 84 error_ = e.getMessage(); 85 } 86 NRPEException(NRPEPacket::NRPEPacketException e) { 87 error_ = e.getMessage(); 88 } 89 */ 90 NRPEException(std::wstring s) { 91 error_ = s; 92 } 93 std::wstring getMessage() { 94 return error_; 95 } 96 }; 110 111 boost::program_options::options_description NRPEClient::get_optionDesc(); 112 boost::program_options::positional_options_description NRPEClient::get_optionsPositional(); 113 nrpe_connection_data NRPEClient::get_ConectionData(boost::program_options::variables_map &vm); 97 114 98 115 99 116 private: 100 101 102 NRPEPacket handlePacket(NRPEPacket p); 103 void addCommand(command_type type, strEx::blindstr key, std::wstring args = _T("")) { 104 addCommand(key, command_data(type, args)); 105 } 106 void addCommand(strEx::blindstr key, command_data args) { 107 commands[key] = args; 108 } 117 void addCommand(strEx::blindstr key, std::wstring args); 109 118 110 119 }; -
modules/NSCAAgent/NSCAAgent.cpp
r394f7a1 r4c8d44d 57 57 */ 58 58 bool NSCAAgent::loadModule() { 59 //pdhThread.createThread();60 //std::list<std::wstring> checks = NSCModuleHelper::getSettingsSection(NSCA_CMD_SECTION_TITLE);61 //int interval = NSCModuleHelper::getSettingsInt(NSCA_AGENT_SECTION_TITLE, NSCA_INTERVAL, NSCA_INTERVAL_DEFAULT);62 59 int e_threads = NSCModuleHelper::getSettingsInt(NSCA_AGENT_SECTION_TITLE, NSCA_DEBUG_THREADS, NSCA_DEBUG_THREADS_DEFAULT); 63 60 64 for (int i= 1;i<e_threads;i++) {61 for (int i=0;i<e_threads;i++) { 65 62 std::wstring id = _T("nsca_t_") + strEx::itos(i); 66 63 NSCAThreadImpl *thread = new NSCAThreadImpl(id);
Note: See TracChangeset
for help on using the changeset viewer.








