Changeset 39c73cd in nscp
- Timestamp:
- 08/13/11 22:38:21 (22 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 65a2940
- Parents:
- f881d47
- Files:
-
- 2 added
- 24 edited
-
changelog (modified) (2 diffs)
-
helpers/installers/installer/Product.wxs (modified) (2 diffs)
-
include/NSCAPI.h (modified) (1 diff)
-
include/dll/impl_w32.hpp (modified) (1 diff)
-
include/nrpe/client/socket.hpp (modified) (4 diffs)
-
include/nscapi/functions.hpp (modified) (3 diffs)
-
include/nscapi/macros.hpp (modified) (1 diff)
-
include/nscapi/nscapi_plugin_wrapper.cpp (modified) (2 diffs)
-
include/nscapi/nscapi_plugin_wrapper.hpp (modified) (3 diffs)
-
include/program_options_ex.hpp (modified) (2 diffs)
-
libs/protobuf/CMakeLists.txt (modified) (2 diffs)
-
libs/protobuf/exec.proto (added)
-
libs/protobuf/settings.proto (added)
-
modules/NRPEClient/NRPEClient.cpp (modified) (9 diffs)
-
modules/NRPEClient/NRPEClient.h (modified) (3 diffs)
-
modules/PythonScript/PythonScript.cpp (modified) (6 diffs)
-
modules/PythonScript/PythonScript.def (modified) (1 diff)
-
modules/PythonScript/PythonScript.h (modified) (2 diffs)
-
modules/PythonScript/script_wrapper.cpp (modified) (3 diffs)
-
modules/PythonScript/script_wrapper.hpp (modified) (3 diffs)
-
scripts/python/test.py (modified) (2 diffs)
-
service/NSCPlugin.cpp (modified) (1 diff)
-
service/NSCPlugin.h (modified) (2 diffs)
-
service/NSClient++.cpp (modified) (11 diffs)
-
service/NSClient++.h (modified) (1 diff)
-
service/cli_parser.hpp (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
changelog
r54ac968 r39c73cd 7 7 * Fix RtlStringFromGUID problem on NT4 8 8 9 2011-08-10 9 2011-08-13 MickeM 10 * Added support for command line execution to PythonScript module 11 * Readded support for specifying module on command line with --client mode 12 * Fixed some issues with the NRPEClient module 13 14 2011-08-12 MickeM 15 * Finnished (rough) adding back command line exec (with modern API) 16 * Fixed so installer uses correct name for dll:s (now Server not Listsener) 17 18 2011-08-11 MickeM 19 * Started to add back command line exec (with modern API) 20 21 2011-08-10 MickeM 10 22 * Fixed linus issues and defect 11 23 * Fixed some issues in the test python script regarding how prefix is handled … … 15 27 * Started to merge som features from PythonScript back to Lua script 16 28 17 2011-08-07 29 2011-08-07 MickeM 18 30 * Fixed a lot of issues with PythonScript module adding suport for alias and "raw command processing" 19 31 * Fixed issue with loading plugins and aliases as well as duplicate plugin detection 20 32 21 2011-08-01 33 2011-08-01 MickeM 22 34 * Added new module: PythonScript 23 35 24 2011-07-25 36 2011-07-25 MickeM 25 37 * merged in all 0.3.9 changes 26 38 * refactored where filter to be "non template" to drastically reduce compile time (as well as potentially size if I ever go dll instead of static link) -
helpers/installers/installer/Product.wxs
r04ef932 r39c73cd 111 111 <Directory Id="INSTALLLOCATION_MODS" Name="modules"> 112 112 <Component Id="NRPEServer" Guid="5A0246F8-5167-45db-B246-$(var.Postfix.GUID)" Win64="$(var.Win64)"> 113 <File Id="NRPE ListenerDLL" Name="NRPEListener.dll" DiskId="1" Source="$(var.Source)/modules/NRPEServer.dll" Vital="no" />113 <File Id="NRPEServerDLL" Name="NRPEServer.dll" DiskId="1" Source="$(var.Source)/modules/NRPEServer.dll" Vital="no" /> 114 114 <File Id="NRPEClientDLL" Name="NRPEClient.dll" DiskId="1" Source="$(var.Source)/modules/NRPEClient.dll" Vital="no" /> 115 115 </Component> 116 <Component Id="NSC Listener" Guid="6DAF8BB9-9A56-48f5-B2C5-$(var.Postfix.GUID)" Win64="$(var.Win64)">117 <File Id="NSC ListenerDLL" Name="NSClientListener.dll" DiskId="1" Source="$(var.Source)/modules/NSClientServer.dll" Vital="no" />116 <Component Id="NSCServer" Guid="6DAF8BB9-9A56-48f5-B2C5-$(var.Postfix.GUID)" Win64="$(var.Win64)"> 117 <File Id="NSCServerDLL" Name="NSClientServer.dll" DiskId="1" Source="$(var.Source)/modules/NSClientServer.dll" Vital="no" /> 118 118 </Component> 119 119 <Component Id="NSCA" Guid="8820A304-C596-4393-A72F-$(var.Postfix.GUID)" Win64="$(var.Win64)"> … … 199 199 <ComponentRef Id="Plugins" /> 200 200 </Feature> 201 <Feature Id="NRPEPlugins" Title="NRPE Support" Description="NRPE Listener Plugin. Support for the more vercitile NRPE protocol (check_nrpe)" Level="1" Absent="disallow">201 <Feature Id="NRPEPlugins" Title="NRPE Support" Description="NRPE Server Plugin. Support for the more vercitile NRPE protocol (check_nrpe)" Level="1" Absent="disallow"> 202 202 <ComponentRef Id="NRPEServer" /> 203 203 <ComponentRef Id="NRPEServerCert" /> 204 204 </Feature> 205 <Feature Id="NSCPlugins" Title="NSClient support" Description="NSClient Listener Plugin. Support for the old NSClient protocol (check_nt)" Level="1" Absent="disallow">206 <ComponentRef Id="NSC Listener" />205 <Feature Id="NSCPlugins" Title="NSClient support" Description="NSClient Server Plugin. Support for the old NSClient protocol (check_nt)" Level="1" Absent="disallow"> 206 <ComponentRef Id="NSCServer" /> 207 207 </Feature> 208 208 <Feature Id="NSCAPlugin" Title="NSCA plugin" Description="Plugin to submit passive results to an NSCA server" Level="1" Absent="disallow"> -
include/NSCAPI.h
re11d494 r39c73cd 166 166 typedef NSCAPI::nagiosReturn (*lpHandleCommand)(const wchar_t*,const char*,const unsigned int,char**,unsigned int*); 167 167 typedef int (*lpDeleteBuffer)(char**); 168 typedef int (*lpCommandLineExec)(const unsigned int,wchar_t**);168 typedef int (*lpCommandLineExec)(const wchar_t*,const char*,const unsigned int,char**,unsigned int*); 169 169 typedef int (*lpHandleMessage)(const char*); 170 170 typedef int (*lpUnLoadModule)(); -
include/dll/impl_w32.hpp
r1ecd26f r39c73cd 22 22 return module; 23 23 boost::filesystem::wpath mod = module / std::wstring(_T(".dll")); 24 if (boost::filesystem::is_regular(mod)) 25 return mod; 26 mod = boost::filesystem::wpath(module.string() + std::wstring(_T(".dll"))); 24 27 if (boost::filesystem::is_regular(mod)) 25 28 return mod; -
include/nrpe/client/socket.hpp
r294b37b r39c73cd 20 20 socket(boost::asio::io_service &io_service, std::wstring host, int port) { 21 21 socket_.reset(new tcp::socket(io_service)); 22 connect(host, port); 22 23 } 23 socket() {} 24 socket() { 25 } 26 27 virtual ~socket() { 28 socket_.reset(); 29 //get_socket().close(); 30 } 31 24 32 25 33 virtual boost::asio::io_service& get_io_service() { … … 49 57 } 50 58 51 ~socket() {52 get_socket().close();53 }54 55 59 virtual void send(nrpe::packet &packet, boost::posix_time::seconds timeout) { 56 60 std::vector<char> buf = packet.get_buffer(); 57 61 write_with_timeout(buf, timeout); 62 get_socket().shutdown(boost::asio::ip::tcp::socket::shutdown_send); 58 63 } 59 64 virtual nrpe::packet recv(const nrpe::packet &packet, boost::posix_time::seconds timeout) { 60 65 std::vector<char> buf(packet.get_packet_length()); 61 66 read_with_timeout(buf, timeout); 67 get_socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both); 68 get_socket().close(); 62 69 return nrpe::packet(&buf[0], buf.size(), packet.get_payload_length()); 63 70 } … … 73 80 74 81 #ifdef USE_SSL 75 class ssl_socket : public socket {82 class ssl_socket : public socket, public boost::noncopyable { 76 83 private: 77 84 boost::shared_ptr<boost::asio::ssl::stream<tcp::socket> > ssl_socket_; … … 80 87 ssl_socket(boost::asio::io_service &io_service, boost::asio::ssl::context &ctx, std::wstring host, int port) : socket() { 81 88 ssl_socket_.reset(new boost::asio::ssl::stream<tcp::socket>(io_service, ctx)); 89 connect(host, port); 82 90 } 91 virtual ~ssl_socket() { 92 ssl_socket_.reset(); 93 } 94 83 95 84 96 virtual void connect(std::wstring host, int port) { -
include/nscapi/functions.hpp
rafd42f1 r39c73cd 38 38 #include <protobuf/plugin.pb.h> 39 39 #include <protobuf/log.pb.h> 40 #include <protobuf/exec.pb.h> 40 41 41 42 using namespace nscp::helpers; … … 65 66 return PluginCommand::Response_Code_UNKNOWN; 66 67 } 68 static ExecuteCommand::Response_Code exec_nagios_to_gpb(int ret) { 69 if (ret == NSCAPI::returnOK) 70 return ExecuteCommand::Response_Code_OK; 71 if (ret == NSCAPI::returnWARN) 72 return ExecuteCommand::Response_Code_WARNING; 73 if (ret == NSCAPI::returnCRIT) 74 return ExecuteCommand::Response_Code_CRITCAL; 75 return ExecuteCommand::Response_Code_UNKNOWN; 76 } 67 77 static LogMessage::Message_Level log_to_gpb(NSCAPI::messageTypes ret) { 68 78 if (ret == NSCAPI::critical) … … 103 113 std::wstring command; 104 114 std::list<std::wstring> args; 115 std::vector<std::wstring> args_vector; 105 116 }; 117 118 119 static decoded_simple_command_data process_simple_command_line_exec_request(const wchar_t* char_command, const std::string &request) { 120 decoded_simple_command_data data; 121 122 data.command = char_command; 123 ExecuteCommand::RequestMessage request_message; 124 request_message.ParseFromString(request); 125 126 if (request_message.payload_size() != 1) { 127 throw nscapi_exception(_T("Whoops, invalid payload size (for now)")); 128 } 129 ::ExecuteCommand::Request payload = request_message.payload().Get(0); 130 for (int i=0;i<payload.arguments_size();i++) { 131 data.args_vector.push_back(to_wstring(payload.arguments(i))); 132 } 133 return data; 134 } 135 static NSCAPI::nagiosReturn process_simple_command_line_exec_result(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring result, std::string &response) { 136 ExecuteCommand::ResponseMessage response_message; 137 ::ExecuteCommand::Header* hdr = response_message.mutable_header(); 138 139 hdr->set_type(ExecuteCommand::Header_Type_RESPONSE); 140 hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 141 142 ExecuteCommand::Response *resp = response_message.add_payload(); 143 resp->set_command(to_string(command)); 144 resp->set_message(to_string(result)); 145 146 resp->set_version(ExecuteCommand::Response_Version_VERSION_1); 147 resp->set_result(exec_nagios_to_gpb(ret)); 148 response_message.SerializeToString(&response); 149 return ret; 150 } 151 106 152 static decoded_simple_command_data process_simple_command_request(const wchar_t* char_command, const std::string &request) { 107 153 decoded_simple_command_data data; -
include/nscapi/macros.hpp
r54ac968 r39c73cd 210 210 211 211 #define NSC_WRAPPERS_CLI_DEF(toObject) \ 212 extern int NSCommandLineExec(const unsigned int argLen,wchar_t** args) { \ 213 try { \ 214 return toObject.commandLineExec(argLen, args); \ 212 extern int NSCommandLineExec(const wchar_t* command,const char* request_buffer,const unsigned int request_len,char** response_buffer,unsigned int* response_len) { \ 213 try { \ 214 std::string request = std::string(request_buffer, request_len); \ 215 std::string response; \ 216 NSCAPI::nagiosReturn retCode = (&toObject)->commandRAWLineExec(command, request, response); \ 217 return GET_PLUGIN()->wrapCommandLineExec(retCode, response, response_buffer, response_len); \ 215 218 } catch (...) { \ 216 219 NSC_LOG_CRITICAL(_T("Unknown exception in: commandLineExec(...)")); \ -
include/nscapi/nscapi_plugin_wrapper.cpp
rafd42f1 r39c73cd 165 165 return retResult; 166 166 } 167 NSCAPI::nagiosReturn nscapi::plugin_wrapper::wrapCommandLineExec(NSCAPI::nagiosReturn retResult, const std::string &reply, char **reply_buffer, unsigned int *size) { 168 // TODO: Make this global to allow remote deletion!!! 169 unsigned int buf_len = reply.size(); 170 *reply_buffer = new char[buf_len + 10]; 171 memcpy(*reply_buffer, reply.c_str(), buf_len+1); 172 (*reply_buffer)[buf_len] = 0; 173 (*reply_buffer)[buf_len+1] = 0; 174 *size = buf_len; 175 return retResult; 176 } 167 177 168 178 /** … … 246 256 } 247 257 248 249 258 NSCAPI::nagiosReturn nscapi::impl::simple_command_line_exec::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response) { 259 nscapi::functions::decoded_simple_command_data data = nscapi::functions::process_simple_command_line_exec_request(char_command, request); 260 std::wstring result; 261 NSCAPI::nagiosReturn ret = commandLineExec(data.command, data.args_vector, result); 262 return nscapi::functions::process_simple_command_line_exec_result(data.command, ret, result, response); 263 } 250 264 251 265 NSCAPI::nagiosReturn nscapi::impl::SimpleNotificationHandler::handleRAWNotification(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, std::string result) { -
include/nscapi/nscapi_plugin_wrapper.hpp
rd66ccee r39c73cd 23 23 #include <string> 24 24 #include <list> 25 #include <vector> 25 26 26 27 #include <boost/make_shared.hpp> … … 69 70 int wrapUnloadModule(bool success); 70 71 NSCAPI::nagiosReturn wrapHandleCommand(NSCAPI::nagiosReturn retResult, const std::string &reply, char **reply_buffer, unsigned int *size); 72 NSCAPI::nagiosReturn wrapCommandLineExec(NSCAPI::nagiosReturn retResult, const std::string &reply, char **reply_buffer, unsigned int *size); 73 71 74 void wrapDeleteBuffer(char**buffer); 72 75 … … 114 117 virtual NSCAPI::nagiosReturn handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) = 0; 115 118 }; 119 class simple_command_line_exec { 120 public: 121 NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 122 virtual NSCAPI::nagiosReturn commandLineExec(const std::wstring &command, std::vector<std::wstring> &arguments, std::wstring &result) = 0; 123 }; 116 124 117 125 -
include/program_options_ex.hpp
rdcd90b2 r39c73cd 3 3 #include <boost/program_options.hpp> 4 4 #include <boost/function/function1.hpp> 5 #include <list> 5 6 6 7 template<class charT> … … 37 38 {} 38 39 40 basic_command_line_parser_ex(std::list<std::basic_string<charT> > &v) 41 : boost::program_options::basic_command_line_parser<charT>( 42 make_vector<std::list<std::basic_string<charT> >::iterator, std::list<std::basic_string<charT> >::iterator>(v.begin(), v.end()) 43 ) 44 {} 45 39 46 }; -
libs/protobuf/CMakeLists.txt
r3bdaf18 r39c73cd 3 3 WRAP_PROTO(PROTO_PLUGIN_SRC plugin.proto) 4 4 WRAP_PROTO(PROTO_LOG_SRC log.proto) 5 WRAP_PROTO(PROTO_EXEC_SRC exec.proto) 6 WRAP_PROTO(PROTO_SETTINGS_SRC settings.proto) 5 7 6 8 SET(TARGET protobuf) … … 9 11 plugin.proto 10 12 log.proto 13 exec.proto 14 settings.proto 11 15 12 16 ${PROTO_PLUGIN_SRC} 13 17 ${PROTO_LOG_SRC} 18 ${PROTO_EXEC_SRC} 19 ${PROTO_SETTINGS_SRC} 14 20 ) 15 21 -
modules/NRPEClient/NRPEClient.cpp
r7ec3dd1 r39c73cd 110 110 111 111 settings.alias().add_path_to_settings() 112 (_T("EXTERNAL SCRIPT SECTION"), _T("Section for external scripts configuration options (CheckExternalScripts)."))113 114 (_T("handlers"), sh::fun_values_path(boost::bind(&NRPEClient::add Command, this, _1, _2)),112 // (_T("EXTERNAL SCRIPT SECTION"), _T("Section for external scripts configuration options (CheckExternalScripts).")) 113 114 (_T("handlers"), sh::fun_values_path(boost::bind(&NRPEClient::add_command, this, _1, _2)), 115 115 _T("CLIENT HANDLER SECTION"), _T("")) 116 117 (_T("servers"), sh::fun_values_path(boost::bind(&NRPEClient::add_server, this, _1, _2)), 118 _T("REMOTE SERVER DEFINITIONS"), _T("")) 116 119 117 120 ; … … 132 135 return false; 133 136 } 134 return true;135 137 136 138 boost::filesystem::wpath p = GET_CORE()->getBasePath() + std::wstring(_T("/security/nrpe_dh_512.pem")); … … 147 149 void NRPEClient::add_options(po::options_description &desc, nrpe_connection_data &command_data) { 148 150 desc.add_options() 149 ("help,h", "Show this help message.")150 151 ("host,H", po::wvalue<std::wstring>(&command_data.host), "The address of the host running the NRPE daemon") 151 152 ("port,p", po::value<int>(&command_data.port), "The port on which the daemon is running (default=5666)") … … 158 159 } 159 160 160 161 void NRPEClient::addCommand(std::wstring key, std::wstring args) { 161 void NRPEClient::add_server(std::wstring key, std::wstring args) { 162 } 163 164 void NRPEClient::add_command(std::wstring key, std::wstring args) { 162 165 try { 163 166 … … 235 238 } 236 239 237 int NRPEClient::commandLineExec(const unsigned int argLen, wchar_t** args) { 240 int NRPEClient::commandLineExec(const std::wstring &command, std::vector<std::wstring> &arguments, std::wstring &result) { 241 if (command != _T("query") && command != _T("help")) 242 return NSCAPI::returnUNKNOWN; 238 243 try { 239 240 244 NRPEClient::nrpe_connection_data command_data; 241 245 boost::program_options::variables_map vm; … … 247 251 po::positional_options_description p; 248 252 p.add("arguments", -1); 249 po::wparsed_options parsed = basic_command_line_parser_ex<wchar_t>(argLen, args).options(desc).positional(p).run();253 po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(arguments).options(desc).positional(p).run(); 250 254 po::store(parsed, vm); 251 255 po::notify(vm); 252 256 command_data.parse_arguments(); 253 254 if (vm.count("help")) { 255 std::cout << desc << "\n"; 256 return 1; 257 if (command == _T("help")) { 258 std::stringstream ss; 259 ss << "NRPEClient Command line syntax for command: query" << std::endl;; 260 ss << desc; 261 result = utf8::cvt<std::wstring>(ss.str()); 262 return NSCAPI::returnOK; 257 263 } 258 nrpe_result_data result = execute_nrpe_command(command_data, command_data.arguments); 259 std::wcout << result.text << std::endl; 260 return result.result; 264 265 nrpe_result_data res = execute_nrpe_command(command_data, command_data.arguments); 266 result = res.text; 267 return res.result; 261 268 } catch (boost::program_options::validation_error &e) { 262 std::cout << e.what() << std::endl; 269 result = _T("Error: ") + utf8::cvt<std::wstring>(e.what()); 270 return NSCAPI::returnUNKNOWN; 263 271 } catch (...) { 264 std::cout << "Unknown exception parsing command line" << std::endl; 272 result = _T("Unknown exception parsing command line"); 273 return NSCAPI::returnUNKNOWN; 265 274 } 266 275 return NSCAPI::returnUNKNOWN; … … 282 291 return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("NRPE Packet errro: ") + e.getMessage()); 283 292 } catch (std::runtime_error &e) { 284 return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("Socket error: ") + boost::lexical_cast<std::wstring>(e.what())); 293 NSC_LOG_ERROR_STD(_T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 294 return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 285 295 } catch (...) { 286 296 return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("Unknown error -- REPORT THIS!")); … … 298 308 nrpe::client::ssl_socket socket(io_service, ctx, host, port); 299 309 socket.send(packet, boost::posix_time::seconds(timeout)); 300 return socket.recv(packet, boost::posix_time::seconds(timeout)); 310 nrpe::packet ret = socket.recv(packet, boost::posix_time::seconds(timeout)); 311 return ret; 301 312 } 302 313 #endif … … 309 320 } 310 321 311 /*312 NRPEPacket NRPEClient::send_nossl(std::wstring host, int port, int timeout, NRPEPacket packet)313 {314 unsigned char dh512_p[] = {315 0xCF, 0xFF, 0x65, 0xC2, 0xC8, 0xB4, 0xD2, 0x68, 0x8C, 0xC1, 0x80, 0xB1,316 0x7B, 0xD6, 0xE8, 0xB3, 0x62, 0x59, 0x62, 0xED, 0xA7, 0x45, 0x6A, 0xF8,317 0xE9, 0xD8, 0xBE, 0x3F, 0x38, 0x42, 0x5F, 0xB2, 0xA5, 0x36, 0x03, 0xD3,318 0x06, 0x27, 0x81, 0xC8, 0x9B, 0x88, 0x50, 0x3B, 0x82, 0x3D, 0x31, 0x45,319 0x2C, 0xB4, 0xC5, 0xA5, 0xBE, 0x6A, 0xE3, 0x2E, 0xA6, 0x86, 0xFD, 0x6A,320 0x7E, 0x1E, 0x6A, 0x73,321 };322 unsigned char dh512_g[] = { 0x02, };323 324 DH *dh_2 = DH_new();325 dh_2->p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL);326 dh_2->g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL);327 328 FILE *outfile = fopen("d:\\nrpe_512.pem", "w");329 PEM_write_DHparams(outfile, dh_2);330 PEM_write_DHparams(stdout, dh_2);331 fclose(outfile);332 333 nrpe_socket socket(host, port);334 socket.send(packet, boost::posix_time::seconds(timeout));335 return socket.recv(packet, boost::posix_time::seconds(timeout));336 }337 */338 339 340 341 342 343 322 NSC_WRAP_DLL(); 344 323 NSC_WRAPPERS_MAIN_DEF(gNRPEClient); -
modules/NRPEClient/NRPEClient.h
r3bdaf18 r39c73cd 25 25 26 26 27 class NRPEClient : public nscapi::impl::SimpleCommand, nscapi::impl::simple_plugin {27 class NRPEClient : public nscapi::impl::SimpleCommand, nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec { 28 28 private: 29 29 typedef enum { … … 123 123 bool hasMessageHandler(); 124 124 NSCAPI::nagiosReturn handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf); 125 int commandLineExec(const unsigned int argLen,wchar_t** args);125 int commandLineExec(const std::wstring &command, std::vector<std::wstring> &arguments, std::wstring &result); 126 126 std::wstring getConfigurationMeta(); 127 127 … … 133 133 134 134 private: 135 void addCommand(std::wstring key, std::wstring args); 135 void add_command(std::wstring key, std::wstring args); 136 void add_server(std::wstring key, std::wstring args); 136 137 137 138 }; -
modules/PythonScript/PythonScript.cpp
rb8c44b4 r39c73cd 74 74 .def("register", &script_wrapper::function_wrapper::register_function) 75 75 .def("register_simple", &script_wrapper::function_wrapper::register_simple_function) 76 .def("cmdline", &script_wrapper::function_wrapper::register_cmdline) 77 .def("simple_cmdline", &script_wrapper::function_wrapper::register_simple_cmdline) 76 78 .def("subscribe", &script_wrapper::function_wrapper::subscribe_function) 77 79 .def("subscribe_simple", &script_wrapper::function_wrapper::subscribe_simple_function) … … 129 131 dict globalDict = extract<dict>(main_module.attr("__dict__")); 130 132 localDict = globalDict.copy(); 133 134 PyRun_SimpleString("import cStringIO"); 135 PyRun_SimpleString("import sys"); 136 PyRun_SimpleString("sys.stderr = cStringIO.StringIO()"); 137 131 138 object ignored = exec_file(scriptfile.c_str(), localDict, localDict); 132 139 } catch( error_already_set e) { … … 136 143 137 144 bool PythonScript::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 145 NSC_DEBUG_MSG_STD(_T("LoadEx in PythonScript as ") + alias); 138 146 try { 139 147 root_ = get_core()->getBasePath(); … … 214 222 bool PythonScript::unloadModule() { 215 223 instances_.clear(); 216 Py_Finalize();224 //Py_Finalize(); 217 225 return true; 218 226 } … … 250 258 } 251 259 260 NSCAPI::nagiosReturn PythonScript::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response) { 261 boost::shared_ptr<script_wrapper::function_wrapper> inst = script_wrapper::function_wrapper::create(); 262 std::string cmd = utf8::cvt<std::string>(char_command); 263 if (inst->has_cmdline(cmd)) { 264 return inst->exec_cmdline(cmd, request, response); 265 } 266 if (inst->has_simple_cmdline(cmd)) { 267 nscapi::functions::decoded_simple_command_data data = nscapi::functions::process_simple_command_line_exec_request(char_command, request); 268 std::wstring result; 269 NSCAPI::nagiosReturn ret = inst->exec_simple_cmdline(cmd, data.args, result); 270 return nscapi::functions::process_simple_command_line_exec_result(data.command, ret, result, response); 271 } 272 return NSCAPI::returnIgnored; 273 } 274 252 275 253 276 NSCAPI::nagiosReturn PythonScript::handleRAWCommand(const wchar_t* command, const std::string &request, std::string &response) { … … 276 299 NSC_WRAPPERS_IGNORE_MSG_DEF(); 277 300 NSC_WRAPPERS_HANDLE_CMD_DEF(gPythonScript); 301 NSC_WRAPPERS_CLI_DEF(gPythonScript); -
modules/PythonScript/PythonScript.def
rb8c44b4 r39c73cd 13 13 NSUnloadModule 14 14 NSGetModuleDescription 15 NSCommandLineExec 15 16 NSDeleteBuffer -
modules/PythonScript/PythonScript.h
rb8c44b4 r39c73cd 30 30 #include <scripts/functions.hpp> 31 31 32 struct python_script {32 struct python_script : public boost::noncopyable { 33 33 boost::python::dict localDict; 34 34 python_script(const script_container& script); … … 75 75 76 76 NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 77 NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 77 78 78 79 //NSCAPI::nagiosReturn RunLUA(const unsigned int argLen, wchar_t **char_args, std::wstring &message, std::wstring &perf); -
modules/PythonScript/script_wrapper.cpp
rf881d47 r39c73cd 25 25 core->registerCommand(utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc)); 26 26 functions::get()->simple_functions[name] = callable; 27 NSC_LOG_MESSAGE_STD(_T("Added simple command: ") + utf8::cvt<std::wstring>(name) + _T(" to list of: ") + get_commands()); 28 } catch (...) { 29 NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name)); 27 } catch (...) { 28 NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name)); 30 29 } 31 30 } … … 34 33 core->registerCommand(utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc)); 35 34 functions::get()->normal_functions[name] = callable; 36 NSC_LOG_MESSAGE_STD(_T("Added simple command: ") + utf8::cvt<std::wstring>(name) + _T(" to list of: ") + get_commands()); 35 } catch (...) { 36 NSC_LOG_ERROR_STD(_T("Failed to register functions: ") + utf8::cvt<std::wstring>(name)); 37 } 38 } 39 void script_wrapper::function_wrapper::register_simple_cmdline(std::string name, PyObject* callable) { 40 try { 41 functions::get()->simple_cmdline[name] = callable; 42 } catch (...) { 43 NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name)); 44 } 45 } 46 void script_wrapper::function_wrapper::register_cmdline(std::string name, PyObject* callable) { 47 try { 48 functions::get()->normal_cmdline[name] = callable; 37 49 } catch (...) { 38 50 NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name)); … … 100 112 return functions::get()->simple_functions.find(command) != functions::get()->simple_functions.end(); 101 113 } 114 115 int script_wrapper::function_wrapper::exec_cmdline(const std::string cmd, const std::string &request, std::string &response) const { 116 try { 117 functions::function_map_type::iterator it = functions::get()->normal_cmdline.find(cmd); 118 if (it == functions::get()->normal_cmdline.end()) { 119 NSC_LOG_ERROR_STD(_T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd)); 120 return NSCAPI::returnIgnored; 121 } 122 tuple ret = boost::python::call<tuple>(it->second, cmd, request); 123 if (ret.ptr() == Py_None) { 124 return NSCAPI::returnUNKNOWN; 125 } 126 int ret_code = NSCAPI::returnUNKNOWN; 127 if (len(ret) > 0) 128 ret_code = extract<int>(ret[0]); 129 if (len(ret) > 1) 130 response = extract<std::string>(ret[1]); 131 return ret_code; 132 } catch( error_already_set e) { 133 log_exception(); 134 return NSCAPI::returnUNKNOWN; 135 } 136 } 137 138 int script_wrapper::function_wrapper::exec_simple_cmdline(const std::string cmd, std::list<std::wstring> arguments, std::wstring &result) const { 139 try { 140 functions::function_map_type::iterator it = functions::get()->simple_cmdline.find(cmd); 141 if (it == functions::get()->simple_cmdline.end()) { 142 result = _T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd); 143 NSC_LOG_ERROR_STD(result); 144 return NSCAPI::returnIgnored; 145 } 146 147 boost::python::list l; 148 BOOST_FOREACH(std::wstring a, arguments) { 149 l.append(utf8::cvt<std::string>(a)); 150 } 151 tuple ret = boost::python::call<tuple>(it->second, l); 152 if (ret.ptr() == Py_None) { 153 result = _T("None"); 154 return NSCAPI::returnUNKNOWN; 155 } 156 int ret_code = NSCAPI::returnUNKNOWN; 157 if (len(ret) > 0) 158 ret_code = extract<int>(ret[0]); 159 if (len(ret) > 1) 160 result = utf8::cvt<std::wstring>(extract<std::string>(ret[1])); 161 return ret_code; 162 } catch( error_already_set e) { 163 log_exception(); 164 result = _T("Exception in: ") + utf8::cvt<std::wstring>(cmd); 165 return NSCAPI::returnUNKNOWN; 166 } 167 } 168 169 bool script_wrapper::function_wrapper::has_cmdline(const std::string command) { 170 return functions::get()->normal_cmdline.find(command) != functions::get()->normal_cmdline.end(); 171 } 172 bool script_wrapper::function_wrapper::has_simple_cmdline(const std::string command) { 173 return functions::get()->simple_cmdline.find(command) != functions::get()->simple_cmdline.end(); 174 } 175 102 176 std::wstring script_wrapper::function_wrapper::get_commands() { 103 177 std::wstring str; -
modules/PythonScript/script_wrapper.hpp
rb8c44b4 r39c73cd 21 21 function_map_type simple_functions; 22 22 function_map_type normal_functions; 23 24 function_map_type simple_cmdline; 25 function_map_type normal_cmdline; 26 23 27 24 28 static boost::shared_ptr<functions> instance; … … 54 58 } 55 59 60 void register_simple_cmdline(std::string name, PyObject* callable); 61 void register_cmdline(std::string name, PyObject* callable); 56 62 void register_simple_function(std::string name, PyObject* callable, std::string desc); 57 63 void register_function(std::string name, PyObject* callable, std::string desc); … … 62 68 bool has_function(const std::string command); 63 69 bool has_simple(const std::string command); 70 71 int exec_simple_cmdline(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &result) const; 72 int exec_cmdline(const std::string wcmd, const std::string &request, std::string &response) const; 73 bool has_cmdline(const std::string command); 74 bool has_simple_cmdline(const std::string command); 75 64 76 std::wstring get_commands(); 65 77 }; -
scripts/python/test.py
r54ac968 r39c73cd 2 2 3 3 core = Core.get() 4 5 6 def get_help(arguments): 7 return (status.OK, 'help: Get help') 4 8 5 9 prefix = 'py_' … … 63 67 fun.register_simple('%snor'%prefix, no_ret, 'No performance data') 64 68 69 fun.simple_cmdline('help', get_help) 70 65 71 log('Testing to register settings keys') 66 72 conf.register_path('hello', 'PYTHON SETTINGS', 'This is stuff for python') -
service/NSCPlugin.cpp
r04ef932 r39c73cd 435 435 436 436 437 int NSCPlugin::commandLineExec(const unsigned int argLen, wchar_t **arguments) { 438 if (fCommandLineExec== NULL) 439 throw NSPluginException(module_, _T("Module does not support CommandLineExec")); 440 try { 441 return fCommandLineExec(argLen, arguments); 442 } catch (...) { 443 throw NSPluginException(module_, _T("Unhandled exception in commandLineExec.")); 444 } 445 } 446 447 bool NSCPlugin::is_duplicate( boost::filesystem::wpath file, std::wstring alias ) { 437 int NSCPlugin::commandLineExec(const wchar_t* command, std::string &request, std::string &reply) { 438 char *buffer = NULL; 439 unsigned int len = 0; 440 NSCAPI::nagiosReturn ret = commandLineExec(command, request.c_str(), request.size(), &buffer, &len); 441 if (buffer != NULL) { 442 reply = std::string(buffer, len); 443 deleteBuffer(&buffer); 444 } 445 return ret; 446 } 447 448 bool NSCPlugin::has_command_line_exec() { 449 return isLoaded() && fCommandLineExec != NULL; 450 } 451 452 int NSCPlugin::commandLineExec(const wchar_t* command, const char* request, const unsigned int request_len, char** reply, unsigned int *reply_len) { 453 if (!has_command_line_exec()) 454 throw NSPluginException(module_, _T("Library is not loaded or modules does not support command line")); 455 try { 456 return fCommandLineExec(command, request, request_len, reply, reply_len); 457 } catch (...) { 458 throw NSPluginException(module_, _T("Unhandled exception in handleCommand.")); 459 } 460 461 } 462 boost::filesystem::wpath NSCPlugin::get_filename(boost::filesystem::wpath folder, std::wstring module) { 463 return dll::dll::fix_module_name(folder / module); 464 } 465 bool NSCPlugin::is_duplicate(boost::filesystem::wpath file, std::wstring alias) { 448 466 if (alias.empty() && alias_.empty()) 449 467 return module_.get_file() == dll::dll::fix_module_name(file); -
service/NSCPlugin.h
r1ecd26f r39c73cd 145 145 void unload(void); 146 146 std::wstring getCongifurationMeta(); 147 int commandLineExec(const unsigned int argLen, wchar_t **arguments); 147 int commandLineExec(const wchar_t* command, std::string &request, std::string &reply); 148 int commandLineExec(const wchar_t* command, const char* request, const unsigned int request_len, char** reply, unsigned int *reply_len); 149 bool has_command_line_exec(); 148 150 void showTray(); 149 151 void hideTray(); 150 152 bool is_duplicate( boost::filesystem::wpath file, std::wstring alias ); 151 153 154 std::wstring get_alias() { 155 return alias_; 156 } 152 157 std::wstring getFilename() { 153 158 return module_.get_filename(); … … 177 182 } 178 183 unsigned int get_id() const { return plugin_id_; } 184 static boost::filesystem::wpath get_filename(boost::filesystem::wpath folder, std::wstring module); 179 185 180 186 private: -
service/NSClient++.cpp
rafd42f1 r39c73cd 45 45 #include "../version.hpp" 46 46 47 #include "../libs/protobuf/plugin.pb.h" 47 #include <protobuf/plugin.pb.h> 48 #include <protobuf/exec.pb.h> 48 49 49 50 #ifdef USE_BREAKPAD … … 327 328 // } 328 329 // return false; 329 // } else if ( wcscasecmp( _T("version"), argv[1]+1 ) == 0 ) {330 // LOG_INFO_CORE(APPLICATION_NAME _T(", Version: ") CURRENT_SERVICE_VERSION _T(", Platform: ") SZARCH);331 330 // } else if ( wcscasecmp( _T("d"), argv[1]+1 ) == 0 ) { 332 331 // // Run command from command line (like NRPE) but with debug enabled 333 // } else if ( wcscasecmp( _T("noboot"), argv[1]+1 ) == 0 ) {334 // mainClient.enableDebug(false);335 // mainClient.initCore(false);336 // if (argc>=3)337 // nRetCode = mainClient.commandLineExec(argv[2], argc-3, &argv[3]);338 // else339 // nRetCode = mainClient.commandLineExec(argv[2], 0, NULL);340 // mainClient.exitCore(true);341 // return nRetCode;342 332 // } else if ( wcscasecmp( _T("c"), argv[1]+1 ) == 0 ) { 343 333 // // Run command from command line (like NRPE) … … 361 351 // } 362 352 // return nRetCode; 363 // } else if (argc > 2) {364 // std::wcout << _T(" * * * * * * * ") << std::endl;365 // mainClient.initCore(true);366 // if (argc>=3)367 // nRetCode = mainClient.commandLineExec(argv[1], argc-2, &argv[2]);368 // else369 // nRetCode = mainClient.commandLineExec(argv[1], 0, NULL);370 // mainClient.exitCore(true);371 // return nRetCode;372 // }373 353 // return nRetCode; 374 354 } … … 635 615 LOG_ERROR_CORE(_T("No crash handling configured")); 636 616 } else { 637 g_exception_manager->StartMonitoring();617 //g_exception_manager->StartMonitoring(); 638 618 } 639 619 } … … 762 742 bool NSClientT::exitCore(bool boot) { 763 743 LOG_DEBUG_CORE(_T("Attempting to stop")); 764 if (boot) { 765 try { 766 LOG_DEBUG_CORE(_T("Stopping: NON Message Handling Plugins")); 767 mainClient.unloadPlugins(false); 768 } catch(NSPluginException e) { 769 LOG_ERROR_CORE_STD(_T("Exception raised when unloading non msg plguins: ") + e.error_ + _T(" in module: ") + e.file_); 770 } catch(...) { 771 LOG_ERROR_CORE_STD(_T("Unknown exception raised when unloading non msg plugins")); 772 } 744 try { 745 LOG_DEBUG_CORE(_T("Stopping: NON Message Handling Plugins")); 746 mainClient.unloadPlugins(false); 747 } catch(NSPluginException e) { 748 LOG_ERROR_CORE_STD(_T("Exception raised when unloading non msg plguins: ") + e.error_ + _T(" in module: ") + e.file_); 749 } catch(...) { 750 LOG_ERROR_CORE_STD(_T("Unknown exception raised when unloading non msg plugins")); 773 751 } 774 752 #ifdef WIN32 … … 814 792 LOG_ERROR_CORE_STD(_T("UNknown exception raised: When closing shared session")); 815 793 } 816 if (boot) { 817 try { 818 LOG_DEBUG_CORE(_T("Stopping: Message handling Plugins")); 819 mainClient.unloadPlugins(true); 820 } catch(NSPluginException e) { 821 LOG_ERROR_CORE_STD(_T("Exception raised when unloading msg plugins: ") + e.error_ + _T(" in module: ") + e.file_); 822 } catch(...) { 823 LOG_ERROR_CORE_STD(_T("UNknown exception raised: When stopping message plguins")); 824 } 794 try { 795 LOG_DEBUG_CORE(_T("Stopping: Message handling Plugins")); 796 mainClient.unloadPlugins(true); 797 } catch(NSPluginException e) { 798 LOG_ERROR_CORE_STD(_T("Exception raised when unloading msg plugins: ") + e.error_ + _T(" in module: ") + e.file_); 799 } catch(...) { 800 LOG_ERROR_CORE_STD(_T("UNknown exception raised: When stopping message plguins")); 825 801 } 826 802 LOG_INFO_CORE(_T("Stopped succcessfully")); … … 844 820 // Member functions 845 821 846 int NSClientT::commandLineExec(const wchar_t* module, const unsigned int argLen, wchar_t** args) { 847 std::wstring sModule = module; 848 std::wstring moduleList = _T(""); 822 823 int NSClientT::command_line_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp) { 824 bool found = false; 825 std::vector<std::string> responses; 826 ExecuteCommand::RequestMessage message; 827 ExecuteCommand::Header *hdr = message.mutable_header(); 828 hdr->set_type(ExecuteCommand::Header_Type_REQUEST); 829 hdr->set_version(ExecuteCommand::Header_Version_VERSION_1); 830 831 ExecuteCommand::Request *req = message.add_payload(); 832 req->set_command(to_string(command)); 833 req->set_version(ExecuteCommand::Request_Version_VERSION_1); 834 835 BOOST_FOREACH(std::wstring s, arguments) 836 req->add_arguments(utf8::cvt<std::string>(s)); 837 838 std::string request; 839 message.SerializeToString(&request); 840 int ret = 0; 849 841 { 850 842 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(5)); … … 853 845 return -1; 854 846 } 855 for (pluginList::size_type i=0;i<plugins_.size();++i) { 856 plugin_type p = plugins_[i]; 857 if (!moduleList.empty()) 858 moduleList += _T(", "); 847 BOOST_FOREACH(plugin_type p, plugins_) { 859 848 if (p) { 860 moduleList += p->getModule(); 861 if (p->getModule() == sModule) { 849 if ((module.empty() || p->getModule() == module)&&(p->has_command_line_exec())) { 862 850 LOG_DEBUG_CORE_STD(_T("Found module: ") + p->getName() + _T("...")); 863 851 try { 864 return p->commandLineExec(argLen, args); 852 std::string response; 853 ret = p->commandLineExec(command.c_str(), request, response); 854 found = true; 855 if (ret != NSCAPI::returnIgnored && !response.empty()) 856 responses.push_back(response); 865 857 } catch (NSPluginException e) { 866 858 LOG_ERROR_CORE_STD(_T("Could not execute command: ") + e.error_ + _T(" in ") + e.file_); … … 871 863 } 872 864 } 873 try { 874 plugin_type plugin = addPlugin(getBasePath() / boost::filesystem::wpath(_T("modules")) / boost::filesystem::wpath(module), _T("")); 875 if (plugin) { 876 LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + plugin->getName() + _T("...")); 877 plugin->load_plugin(NSCAPI::dontStart); 878 return plugin->commandLineExec(argLen, args); 879 } else { 880 LOG_ERROR_CORE_STD(_T("Failed to load: ") + std::wstring(module) + _T(" available modules are: ") + moduleList); 865 if (!found && !module.empty()) { 866 try { 867 //boost::filesystem::wpath root = ; 868 boost::filesystem::wpath file = NSCPlugin::get_filename(getBasePath() / boost::filesystem::wpath(_T("modules")), module); 869 if (boost::filesystem::is_regular(file)) { 870 plugin_type plugin = addPlugin(file, _T("")); 871 if (plugin) { 872 LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + plugin->getName() + _T("...")); 873 plugin->load_plugin(NSCAPI::dontStart); 874 std::string response; 875 ret = plugin->commandLineExec(command.c_str(), request, response); 876 if (ret != NSCAPI::returnIgnored && !response.empty()) 877 responses.push_back(response); 878 } else { 879 LOG_ERROR_CORE_STD(_T("Failed to load: ") + std::wstring(module)); 880 return 1; 881 } 882 } else { 883 LOG_ERROR_CORE_STD(_T("Failed to load: ") + std::wstring(module)); 884 return 1; 885 } 886 } catch (const NSPluginException &e) { 887 resp.push_back(_T("Module (") + e.file_ + _T(") was not found: ") + e.error_); 888 LOG_INFO_CORE_STD(_T("Module (") + e.file_ + _T(") was not found: ") + e.error_); 889 } catch(const std::exception &e) { 890 resp.push_back(_T("Module (") + module + _T(") was not found: ") + utf8::cvt<std::wstring>(e.what())); 891 LOG_INFO_CORE_STD(_T("Module (") + module + _T(") was not found: ") + utf8::cvt<std::wstring>(e.what())); 881 892 return 1; 882 } 883 } catch (NSPluginException e) { 884 LOG_INFO_CORE_STD(_T("Module (") + e.file_ + _T(") was not found: ") + e.error_); 885 } 886 LOG_ERROR_CORE_STD(_T("Module not found: ") + std::wstring(module) + _T(" available modules are: ") + moduleList); 887 return 0; 893 } catch(...) { 894 resp.push_back(_T("Module (") + module + _T(") was not found...")); 895 LOG_INFO_CORE_STD(_T("Module (") + module + _T(") was not found...")); 896 return 1; 897 } 898 } 899 BOOST_FOREACH(std::string &r, responses) { 900 ExecuteCommand::ResponseMessage rsp_msg; 901 902 rsp_msg.ParseFromString(r); 903 if (rsp_msg.payload_size() != 1) { 904 resp.push_back(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 905 LOG_ERROR_CORE_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size())); 906 return NSCAPI::returnUNKNOWN; 907 } 908 resp.push_back(utf8::cvt<std::wstring>(rsp_msg.payload(0).message())); 909 } 910 return ret; 888 911 } 889 912 … … 960 983 } 961 984 for (pluginList::iterator it=plugins_.begin(); it != plugins_.end();) { 962 LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + (*it)->getName() + _T(" ..."));985 LOG_DEBUG_CORE_STD(_T("Loading plugin: ") + (*it)->getName() + _T(" as ") + (*it)->get_alias()); 963 986 try { 964 987 if (!(*it)->load_plugin(mode)) { … … 1019 1042 if (plugin->hasMessageHandler()) 1020 1043 logger_master_.add_plugin(plugin); 1021 settings_manager::get_core()->register_key(_T("/modules"), plugin->getModule(), settings::settings_core::key_string, plugin->getName(), plugin->getDescription(), _T(""), false); 1044 //settings_manager::get_core()->register_key(_T("/modules"), plugin->getModule(), settings::settings_core::key_string, plugin->getName(), plugin->getDescription(), _T(""), false); 1045 // TODO add comments elsewhere to the settings store for all loaded modules... 1022 1046 } 1023 1047 return plugin; -
service/NSClient++.h
r7ec3dd1 r39c73cd 166 166 std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args); 167 167 void reportMessage(std::string data); 168 int command LineExec(const wchar_t* module, const unsigned int argLen, wchar_t** args);168 int command_line_exec(std::wstring module, std::wstring command, std::vector<std::wstring> arguments, std::vector<std::wstring> &resp); 169 169 170 170 struct service_controller { -
service/cli_parser.hpp
r7ec3dd1 r39c73cd 12 12 po::options_description settings; 13 13 po::options_description service; 14 po::options_description client; 14 15 15 16 public: … … 19 20 , settings("Settings options") 20 21 , service("Service Options") 22 , client("Client Options") 21 23 { 22 24 desc.add_options() 23 25 ("help", "produce help message") 24 ("settings-help", "produce help message for the various settings related options") 25 ("service-help", "produce help message for the various settings related options") 26 ("settings-help", "Produce help message for the various settings related options") 27 ("service-help", "Produce help message for the various settings related service management") 28 ("client-help", "Produce help message for the various settings related client") 26 29 ("settings", "Enter settings mode and handle settings related commands") 27 30 ("service", "Enter service mode and handle service related commands") 31 ("client", "Enter client mode and handle client related commands") 28 32 ("test", "Start test and debug mode") 29 33 ("debug", "Show debug information") 34 ("version", "Show version information") 30 35 ; 31 36 … … 54 59 ; 55 60 61 client.add_options() 62 ("command,C", po::value<std::wstring>(), "Name of command to start") 63 ("module,M", po::value<std::wstring>(), "Name of module to load (if not specified all modules in ini file will be loaded)") 64 ("arguments,A", po::value< std::vector<std::wstring> >()->multitoken(), "List of arguments") ; 65 56 66 } 57 67 int parse(int argc, wchar_t* argv[]) { 58 68 try { 59 69 po::options_description all("Allowed options"); 60 all.add(desc).add(service).add(settings) ;70 all.add(desc).add(service).add(settings).add(client); 61 71 62 72 po::variables_map vm; … … 73 83 } 74 84 85 if (vm.count("help")) { 86 mainClient.set_console_log(); 87 std::cout << all << "\n"; 88 return 1; 89 } 90 if (vm.count("version")) { 91 mainClient.set_console_log(); 92 std::cout << APPLICATION_NAME << _T(", Version: ") << CURRENT_SERVICE_VERSION << _T(", Platform: ") << SZARCH << "\n"; 93 return 1; 94 } 95 if (vm.count("settings-help")) { 96 mainClient.set_console_log(); 97 std::cout << settings << "\n"; 98 return 1; 99 } 100 if (vm.count("service-help")) { 101 mainClient.set_console_log(); 102 std::cout << service << "\n"; 103 return 1; 104 } 105 if (vm.count("client-help")) { 106 mainClient.set_console_log(); 107 std::cout << client << "\n"; 108 return 1; 109 } 75 110 76 111 if (vm.count("settings")) { … … 82 117 return parse_service(argc, argv); 83 118 } 119 if (vm.count("client")) { 120 return parse_client(argc, argv); 121 } 84 122 if (vm.count("test")) { 85 123 mainClient.set_console_log(); 86 124 return parse_test(argc, argv); 87 }88 if (vm.count("help")) {89 mainClient.set_console_log();90 std::cout << all << "\n";91 return 1;92 }93 if (vm.count("settings-help")) {94 mainClient.set_console_log();95 std::cout << settings << "\n";96 return 1;97 }98 if (vm.count("service-help")) {99 mainClient.set_console_log();100 std::cout << service << "\n";101 return 1;102 125 } 103 126 } catch(std::exception & e) { … … 244 267 } 245 268 } 269 270 int parse_client(int argc, wchar_t* argv[]) { 271 try { 272 po::options_description all("Allowed options (client)"); 273 all.add(desc).add(client); 274 275 276 po::variables_map vm; 277 po::wparsed_options parsed = 278 po::wcommand_line_parser(argc, argv).options(all).allow_unregistered().run(); 279 po::store(parsed, vm); 280 po::notify(vm); 281 282 bool debug = false; 283 if (vm.count("debug")) { 284 debug = true; 285 } 286 287 std::wstring command; 288 if (vm.count("command")) { 289 command = vm["command"].as<std::wstring>(); 290 } 291 std::wstring module; 292 if (vm.count("module")) { 293 module = vm["module"].as<std::wstring>(); 294 } 295 std::vector<std::wstring> arguments = po::collect_unrecognized(parsed.options, po::include_positional); 296 297 if (debug) { 298 mainClient.set_console_log(); 299 mainClient.enableDebug(true); 300 mainClient.log_info(__FILE__, __LINE__, _T("Module: ") + module); 301 mainClient.log_info(__FILE__, __LINE__, _T("Command: ") + command); 302 std::wstring args; 303 BOOST_FOREACH(std::wstring s, arguments) 304 strEx::append_list(args, s, _T(", ")); 305 mainClient.log_info(__FILE__, __LINE__, _T("Arguments: ") + args); 306 } 307 if (module.empty()) { 308 core_->initCore(false); 309 } 310 std::vector<std::wstring> resp; 311 mainClient.command_line_exec(module, command, arguments, resp); 312 mainClient.exitCore(false); 313 314 BOOST_FOREACH(std::wstring r, resp) { 315 std::wcout << r << std::endl; 316 } 317 return 0; 318 } catch(std::exception & e) { 319 mainClient.log_error(__FILE__, __LINE__, std::string("Unable to parse command line (settings): ") + e.what()); 320 return 1; 321 } 322 } 246 323 }; 247 324
Note: See TracChangeset
for help on using the changeset viewer.








