Changeset f33c12f in nscp
- Timestamp:
- 12/05/11 08:05:14 (18 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 9c06054
- Parents:
- 9853bc3
- Files:
-
- 20 edited
-
include/client/command_line_parser.cpp (modified) (7 diffs)
-
include/client/command_line_parser.hpp (modified) (3 diffs)
-
include/nscapi/functions.hpp (modified) (12 diffs)
-
include/strEx.h (modified) (4 diffs)
-
include/utils.h (modified) (2 diffs)
-
modules/CheckHelpers/CheckHelpers.cpp (modified) (1 diff)
-
modules/DistributedClient/DistributedClient.cpp (modified) (14 diffs)
-
modules/DistributedClient/DistributedClient.h (modified) (5 diffs)
-
modules/DistributedServer/handler_impl.cpp (modified) (1 diff)
-
modules/NRPEClient/NRPEClient.cpp (modified) (3 diffs)
-
modules/NRPEClient/NRPEClient.h (modified) (2 diffs)
-
modules/NSCAClient/NSCAClient.cpp (modified) (3 diffs)
-
modules/NSCAClient/NSCAClient.h (modified) (2 diffs)
-
modules/NSCPClient/NSCPClient.cpp (modified) (3 diffs)
-
modules/NSCPClient/NSCPClient.h (modified) (2 diffs)
-
modules/SMTPClient/SMTPClient.cpp (modified) (12 diffs)
-
modules/SMTPClient/SMTPClient.h (modified) (3 diffs)
-
modules/Scheduler/Scheduler.cpp (modified) (1 diff)
-
modules/SyslogClient/SyslogClient.cpp (modified) (2 diffs)
-
modules/SyslogClient/SyslogClient.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
include/client/command_line_parser.cpp
r96c1461 rf33c12f 53 53 54 54 55 int client::command_line_parser:: commandLineExec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) {55 int client::command_line_parser::do_execute_command_as_exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result) { 56 56 if (!config.validate()) 57 57 throw cli_exception("Invalid data: " + config.to_string()); 58 58 if (command == _T("help")) { 59 result = build_help(config); 60 return NSCAPI::returnUNKNOWN; 59 return nscapi::functions::create_simple_exec_response_unknown(command, build_help(config), result); 61 60 } else if (command == _T("query")) { 62 61 std::wstring msg, perf; 63 int ret = query(config, command, arguments, msg, perf); 64 if (perf.empty()) 65 result = msg; 66 else 67 result = msg + _T("|") + perf; 62 int ret = do_query(config, command, arguments, result); 63 nscapi::functions::make_exec_from_query(result); 68 64 return ret; 69 65 } else if (command == _T("exec")) { 70 return exec(config, command, arguments, result);66 return do_exec(config, command, arguments, result); 71 67 } else if (command == _T("submit")) { 72 boost::tuple<int,std::wstring> ret = simple_submit(config, command, arguments); 73 result = ret.get<1>(); 74 return ret.get<0>(); 68 int ret = do_submit(config, command, arguments, result); 69 nscapi::functions::make_exec_from_submit(result); 70 return ret; 71 } 72 return NSCAPI::returnIgnored; 73 } 74 75 int client::command_line_parser::do_execute_command_as_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result) { 76 if (!config.validate()) 77 throw cli_exception("Invalid data: " + config.to_string()); 78 if (command == _T("help")) { 79 return nscapi::functions::create_simple_query_response_unknown(command, build_help(config), _T(""), result); 80 } else if (command == _T("query")) { 81 return do_query(config, command, arguments, result); 82 } else if (command == _T("exec")) { 83 int ret = do_exec(config, command, arguments, result); 84 nscapi::functions::make_query_from_exec(result); 85 return ret; 86 } else if (command == _T("submit")) { 87 int ret = do_submit(config, command, arguments, result); 88 nscapi::functions::make_query_from_submit(result); 89 return ret; 75 90 } 76 91 return NSCAPI::returnIgnored; … … 97 112 } 98 113 99 int client::command_manager::exec_simple(configuration &config, const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std:: wstring &message, std::wstring &perf) {114 int client::command_manager::exec_simple(configuration &config, const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::string &response) { 100 115 command_type::const_iterator cit = commands.find(command); 101 116 if (cit == commands.end()) … … 112 127 } 113 128 // TODO: Add support for target here! 114 return client::command_line_parser:: commandLineExec(config, ci.command, rendered_arguments, message);115 } 116 int client::command_line_parser:: query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &msg, std::wstring &perf) {129 return client::command_line_parser::do_execute_command_as_exec(config, ci.command, rendered_arguments, response); 130 } 131 int client::command_line_parser::do_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &response) { 117 132 boost::program_options::variables_map vm; 118 133 … … 141 156 nscapi::functions::append_simple_query_request_payload(message.add_payload(), config.data->command, config.data->arguments); 142 157 std::string result; 143 int ret = config.handler->query(config.data, message.mutable_header(), message.SerializeAsString(), result); 144 nscapi::functions::parse_simple_query_response(result, msg, perf); 145 return ret; 146 } 147 148 int client::command_line_parser::exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 158 return config.handler->query(config.data, message.mutable_header(), message.SerializeAsString(), result); 159 } 160 161 int client::command_line_parser::do_exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result) { 149 162 boost::program_options::variables_map vm; 150 163 … … 173 186 std::string response; 174 187 nscapi::functions::append_simple_exec_request_payload(message.add_payload(), config.data->command, config.data->arguments); 175 int ret = config.handler->exec(config.data, message.mutable_header(), message.SerializeAsString(), response); 176 nscapi::functions::parse_simple_exec_result(response, result); 177 return ret; 178 } 179 180 boost::tuple<int,std::wstring> client::command_line_parser::simple_submit(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments) { 188 return config.handler->exec(config.data, message.mutable_header(), message.SerializeAsString(), response); 189 } 190 191 int client::command_line_parser::do_submit(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result) { 181 192 boost::program_options::variables_map vm; 182 193 po::options_description common("Common options"); … … 203 214 nscapi::functions::append_simple_submit_request_payload(message.add_payload(), config.data->command, config.data->result, config.data->message); 204 215 205 std::string response; 206 if (config.handler->submit(config.data, message.mutable_header(), message.SerializeAsString(), response) != NSCAPI::isSuccess) 207 return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("Failed to submit command")); 208 209 std::wstring messages; 210 int ret = nscapi::functions::parse_simple_submit_response(response, messages); 211 return boost::make_tuple(ret, messages); 216 return config.handler->submit(config.data, message.mutable_header(), message.SerializeAsString(), result); 212 217 } 213 218 void client::command_line_parser::modify_header(configuration &config, ::Plugin::Common_Header* header, nscapi::functions::destination_container &recipient) { … … 225 230 } 226 231 227 int client::command_line_parser:: relay_submit(configuration &config, const std::string &request, std::string &response) {232 int client::command_line_parser::do_relay_submit(configuration &config, const std::string &request, std::string &response) { 228 233 Plugin::SubmitRequestMessage message; 229 234 message.ParseFromString(request); -
include/client/command_line_parser.hpp
r96c1461 rf33c12f 120 120 121 121 std::wstring add_command(std::wstring name, std::wstring args); 122 int exec_simple(configuration &config, const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std:: wstring &message, std::wstring &perf);122 int exec_simple(configuration &config, const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::string &response); 123 123 124 124 static std::wstring make_key(std::wstring key) { … … 137 137 static std::wstring build_help(configuration &config); 138 138 139 static int commandLineExec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 140 static int relay_submit(configuration &config, const std::string &request, std::string &response); 141 142 static boost::tuple<int,std::wstring> simple_submit(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments); 139 static int do_execute_command_as_exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 140 static int do_execute_command_as_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 141 static int do_relay_submit(configuration &config, const std::string &request, std::string &response); 143 142 144 143 static std::wstring parse_command(std::wstring command, std::wstring prefix) { … … 163 162 } 164 163 165 static int query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &msg, std::wstring &perf);166 //static std::list<std::string> submit(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments);167 static int exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result);164 static int do_query(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 165 static int do_exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 166 static int do_submit(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result); 168 167 169 168 private: -
include/nscapi/functions.hpp
r96c1461 rf33c12f 41 41 42 42 namespace nscapi { 43 44 namespace traits { 45 46 template<class T> 47 struct perf_data_consts { 48 static const T get_valid_perf_numbers(); 49 static const T get_replace_perf_coma_src(); 50 static const T get_replace_perf_coma_tgt(); 51 }; 52 53 template<> 54 struct perf_data_consts<std::wstring> { 55 static const std::wstring get_valid_perf_numbers() { 56 return _T("0123456789,."); 57 } 58 static const std::wstring get_replace_perf_coma_src() { 59 return _T(","); 60 } 61 static const std::wstring get_replace_perf_coma_tgt() { 62 return _T("."); 63 } 64 }; 65 template<> 66 struct perf_data_consts<std::string> { 67 static const std::string get_valid_perf_numbers() { 68 return "0123456789,."; 69 } 70 static const std::string get_replace_perf_coma_src() { 71 return ","; 72 } 73 static const std::string get_replace_perf_coma_tgt() { 74 return "."; 75 } 76 }; 77 } 43 78 class functions { 44 79 public: … … 71 106 return NSCAPI::hasFailed; 72 107 } 108 static Plugin::Common::ResultCode gbp_status_to_gbp_nagios(Plugin::Common::Status::StatusType ret) { 109 if (ret == Plugin::Common_Status_StatusType_OK) 110 return Plugin::Common_ResultCode_OK; 111 return Plugin::Common_ResultCode_UNKNOWN; 112 } 113 static Plugin::Common::Status::StatusType gbp_to_nagios_gbp_status(Plugin::Common::ResultCode ret) { 114 if (ret == Plugin::Common_ResultCode_UNKNOWN||ret == Plugin::Common_ResultCode_WARNING||ret == Plugin::Common_ResultCode_CRITCAL) 115 return Plugin::Common_Status_StatusType_CRITICAL; 116 return Plugin::Common_Status_StatusType_OK; 117 } 118 73 119 static Plugin::LogEntry::Entry::Level log_to_gpb(NSCAPI::messageTypes ret) { 74 120 if (ret == NSCAPI::critical) … … 98 144 } 99 145 100 static double trim_to_double(std::wstring s) { 101 std::wstring::size_type pend = s.find_first_not_of(_T("0123456789,.")); 102 if (pend != std::wstring::npos) 146 147 template<class T> 148 static double trim_to_double(T s) { 149 typename T::size_type pend = s.find_first_not_of(nscapi::traits::perf_data_consts<T>::get_valid_perf_numbers()); 150 if (pend != T::npos) 103 151 s = s.substr(0,pend); 104 strEx::replace(s, _T(","), _T("."));152 strEx::replace(s, nscapi::traits::perf_data_consts<T>::get_replace_perf_coma_src(), nscapi::traits::perf_data_consts<T>::get_replace_perf_coma_tgt()); 105 153 return strEx::stod(s); 106 154 } 155 156 107 157 108 158 struct decoded_simple_command_data { … … 280 330 Plugin::SubmitRequestMessage request; 281 331 request.mutable_header()->CopyFrom(response.header()); 332 request.mutable_header()->set_source_id(request.mutable_header()->recipient_id()); 282 333 request.set_channel(to_string(channel)); 283 334 for (int i=0;i<response.payload_size();++i) { … … 287 338 } 288 339 message = request.SerializeAsString(); 340 } 341 342 static void make_query_from_exec(std::string &data) { 343 Plugin::ExecuteResponseMessage exec_response_message; 344 exec_response_message.ParseFromString(data); 345 Plugin::QueryResponseMessage query_response_message; 346 query_response_message.mutable_header()->CopyFrom(exec_response_message); 347 for (int i=0;i<exec_response_message.payload_size();++i) { 348 Plugin::ExecuteResponseMessage::Response p = exec_response_message.payload(i); 349 append_simple_query_response_payload(query_response_message.add_payload(), p.command(), p.result(), p.message()); 350 } 351 data = query_response_message.SerializeAsString(); 352 } 353 static void make_query_from_submit(std::string &data) { 354 Plugin::SubmitResponseMessage submit_response_message; 355 submit_response_message.ParseFromString(data); 356 Plugin::QueryResponseMessage query_response_message; 357 query_response_message.mutable_header()->CopyFrom(submit_response_message); 358 for (int i=0;i<submit_response_message.payload_size();++i) { 359 Plugin::SubmitResponseMessage::Response p = submit_response_message.payload(i); 360 append_simple_query_response_payload(query_response_message.add_payload(), p.command(), gbp_status_to_gbp_nagios(p.status().status()), p.status().message(), ""); 361 } 362 data = query_response_message.SerializeAsString(); 363 } 364 365 static void make_exec_from_submit(std::string &data) { 366 Plugin::SubmitResponseMessage submit_response_message; 367 submit_response_message.ParseFromString(data); 368 Plugin::ExecuteResponseMessage exec_response_message; 369 exec_response_message.mutable_header()->CopyFrom(submit_response_message); 370 for (int i=0;i<submit_response_message.payload_size();++i) { 371 Plugin::SubmitResponseMessage::Response p = submit_response_message.payload(i); 372 append_simple_exec_response_payload(exec_response_message.add_payload(), p.command(), gbp_status_to_gbp_nagios(p.status().status()), p.status().message()); 373 } 374 data = exec_response_message.SerializeAsString(); 375 } 376 static void make_exec_from_query(std::string &data) { 377 Plugin::QueryResponseMessage query_response_message; 378 query_response_message.ParseFromString(data); 379 Plugin::ExecuteResponseMessage exec_response_message; 380 exec_response_message.mutable_header()->CopyFrom(query_response_message); 381 for (int i=0;i<query_response_message.payload_size();++i) { 382 Plugin::QueryResponseMessage::Response p = query_response_message.payload(i); 383 std::string s = build_performance_data(p); 384 if (!s.empty()) 385 s = p.message() + "|" + s; 386 else 387 s = p.message(); 388 append_simple_exec_response_payload(exec_response_message.add_payload(), p.command(), p.result(), s); 389 } 390 data = exec_response_message.SerializeAsString(); 391 } 392 393 394 static void make_return_header(::Plugin::Common_Header *target, const ::Plugin::Common_Header &source) { 395 target->CopyFrom(source); 396 target->set_source_id(target->recipient_id()); 289 397 } 290 398 … … 409 517 return NSCAPI::returnUNKNOWN; 410 518 } 519 static NSCAPI::nagiosReturn create_simple_query_response_unknown(std::wstring command, std::wstring msg, std::string &buffer) { 520 create_simple_query_response(command, NSCAPI::returnUNKNOWN, msg, _T(""), buffer); 521 return NSCAPI::returnUNKNOWN; 522 } 411 523 412 524 static void create_simple_query_response(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg, std::wstring perf, std::string &buffer) { … … 436 548 } 437 549 438 static void append_simple_exec_response_payload(Plugin::ExecuteResponseMessage::Response *payload, std::wstring command, NSCAPI::nagiosReturn ret, std::wstring msg) { 550 template<class T> 551 static void append_response_payloads(T &target_message, std::string &payload) { 552 T source_message; 553 source_message.ParseFromString(payload); 554 for (int i=0;i<source_message.payload_size();++i) 555 target_message.add_payload()->CopyFrom(source_message.payload(i)); 556 } 557 558 static void append_simple_query_response_payload(Plugin::QueryResponseMessage::Response *payload, std::string command, NSCAPI::nagiosReturn ret, std::string msg, std::string perf = "") { 439 559 payload->set_command(to_string(command)); 440 560 payload->set_message(to_string(msg)); 441 561 payload->set_result(nagios_status_to_gpb(ret)); 562 if (!perf.empty()) 563 parse_performance_data(payload, perf); 564 } 565 /* 566 static void append_simple_exec_response_payload(Plugin::ExecuteResponseMessage::Response *payload, std::string command, Plugin::Common::ResultCode ret, std::string msg) { 567 payload->set_command(command); 568 payload->set_message(msg); 569 payload->set_result(ret); 570 } 571 */ 572 static void append_simple_exec_response_payload(Plugin::ExecuteResponseMessage::Response *payload, std::string command, int ret, std::string msg) { 573 payload->set_command(command); 574 payload->set_message(msg); 575 payload->set_result(nagios_status_to_gpb(ret)); 576 } 577 /* 578 static void append_simple_submit_response_payload(Plugin::SubmitResponseMessage::Response *payload, std::string command, Plugin::Common::ResultCode ret, std::string msg) { 579 payload->set_command(command); 580 payload->mutable_status()->set_status(gbp_to_nagios_gbp_status(ret)); 581 payload->mutable_status()->set_message(msg); 582 } 583 */ 584 static void append_simple_submit_response_payload(Plugin::SubmitResponseMessage::Response *payload, std::string command, int ret, std::string msg) { 585 payload->set_command(command); 586 payload->mutable_status()->set_status(status_to_gpb(ret)); 587 payload->mutable_status()->set_message(msg); 442 588 } 443 589 … … 546 692 } 547 693 548 static void create_simple_exec_response(std::wstring command, NSCAPI::nagiosReturn ret, std::wstring result, std::string &response) { 694 template<class T> 695 static int create_simple_exec_response(T command, NSCAPI::nagiosReturn ret, T result, std::string &response) { 549 696 Plugin::ExecuteResponseMessage message; 550 697 create_simple_header(message.mutable_header()); … … 556 703 payload->set_result(nagios_status_to_gpb(ret)); 557 704 message.SerializeToString(&response); 705 return ret; 706 } 707 template<class T> 708 static int create_simple_exec_response_unknown(T command, T result, std::string &response) { 709 Plugin::ExecuteResponseMessage message; 710 create_simple_header(message.mutable_header()); 711 712 Plugin::ExecuteResponseMessage::Response *payload = message.add_payload(); 713 payload->set_command(to_string(command)); 714 payload->set_message(to_string(result)); 715 716 payload->set_result(nagios_status_to_gpb(NSCAPI::returnUNKNOWN)); 717 message.SerializeToString(&response); 718 return NSCAPI::returnUNKNOWN; 558 719 } 559 720 static decoded_simple_command_data parse_simple_exec_request(const wchar_t* char_command, const std::string &request) { … … 587 748 ////////////////////////////////////////////////////////////////////////// 588 749 589 static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::wstring &perf) { 590 boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring> tok(perf, boost::escaped_list_separator<wchar_t>(L'\\', L' ', L'\'')); 591 BOOST_FOREACH(std::wstring s, tok) { 750 template<class T, class U> 751 struct tokenizer_data { 752 boost::escaped_list_separator<U> separator; // \\, ' ', \' 753 T perf_item_splitter; // ; 754 T perf_equal_sign; // = 755 T perf_valid_number; // 0123456789., 756 757 }; 758 759 template<class T, class U> 760 static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, T &perf, tokenizer_data<T, U> tokenizer_data) { 761 boost::tokenizer<boost::escaped_list_separator<U>, typename T::const_iterator, T> tok(perf, tokenizer_data.separator); 762 BOOST_FOREACH(const T s, tok) { 592 763 if (s.size() == 0) 593 764 break; 594 st rEx::splitVector items = strEx::splitV(s, _T(";"));765 std::vector<T> items = strEx::splitV(s, tokenizer_data.perf_item_splitter); 595 766 if (items.size() < 1) { 596 767 Plugin::Common::PerformanceData* perfData = payload->add_perf(); 597 768 perfData->set_type(Plugin::Common_DataType_STRING); 598 std::pair< std::wstring,std::wstring> fitem = strEx::split(_T(""), _T("="));769 std::pair<T,T> fitem = strEx::split(T(), tokenizer_data.perf_equal_sign); 599 770 perfData->set_alias("invalid"); 600 771 Plugin::Common_PerformanceData_StringValue* stringPerfData = perfData->mutable_string_value(); … … 605 776 Plugin::Common::PerformanceData* perfData = payload->add_perf(); 606 777 perfData->set_type(Plugin::Common_DataType_FLOAT); 607 std::pair< std::wstring,std::wstring> fitem = strEx::split(items[0], _T("="));778 std::pair<T,T> fitem = strEx::split(items[0], tokenizer_data.perf_equal_sign); 608 779 perfData->set_alias(to_string(fitem.first)); 609 780 Plugin::Common_PerformanceData_FloatValue* floatPerfData = perfData->mutable_float_value(); 610 781 611 std::wstring::size_type pend = fitem.second.find_first_not_of(_T("0123456789,."));612 if (pend == std::wstring::npos) {613 floatPerfData->set_value(trim_to_double(fitem.second .c_str()));782 typename T::size_type pend = fitem.second.find_first_not_of(tokenizer_data.perf_valid_number); 783 if (pend == T::npos) { 784 floatPerfData->set_value(trim_to_double(fitem.second)); 614 785 } else { 615 floatPerfData->set_value(trim_to_double(fitem.second.substr(0,pend) .c_str()));786 floatPerfData->set_value(trim_to_double(fitem.second.substr(0,pend))); 616 787 floatPerfData->set_unit(to_string(fitem.second.substr(pend))); 617 788 } … … 625 796 } 626 797 } 627 // std::wcout << _T("Converting performance data") << perf << _T(" -- ") << utf8::cvt<std::wstring>(build_performance_data(*resp)) << std::endl; 628 } 798 } 799 static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::wstring &perf) { 800 typedef std::wstring t_string; 801 typedef wchar_t t_char; 802 tokenizer_data<t_string, t_char> data; 803 data.separator = boost::escaped_list_separator<t_char>(L'\\', L' ', L'\''); 804 data.perf_equal_sign = _T("="); 805 data.perf_item_splitter = _T(";"); 806 data.perf_valid_number = _T("0123456789,."); 807 parse_performance_data<t_string, t_char>(payload, perf, data); 808 } 809 static void parse_performance_data(Plugin::QueryResponseMessage::Response *payload, std::string &perf) { 810 typedef std::string t_string; 811 typedef char t_char; 812 tokenizer_data<t_string, t_char> data; 813 data.separator = boost::escaped_list_separator<t_char>('\\', ' ', '\''); 814 data.perf_equal_sign = "="; 815 data.perf_item_splitter = ";"; 816 data.perf_valid_number = "0123456789,."; 817 parse_performance_data<t_string, t_char>(payload, perf, data); 818 } 819 629 820 static std::string build_performance_data(Plugin::QueryResponseMessage::Response const &payload) { 630 821 std::stringstream ss; -
include/strEx.h
r96c1461 rf33c12f 548 548 return boost::lexical_cast<int>(s.c_str()); 549 549 } 550 inline double stod(std::wstring s) { 550 template<class T> 551 inline double stod(T s) { 551 552 return boost::lexical_cast<double>(s.c_str()); 552 553 } … … 749 750 } 750 751 typedef std::vector<std::wstring> splitVector; 751 inline splitVector splitV(const std::wstring str, const std::wstring key) { 752 splitVector ret; 753 std::wstring::size_type pos = 0, lpos = 0; 754 while ((pos = str.find(key, pos)) != std::wstring::npos) { 752 template<class T> 753 inline std::vector<T> splitV(const T str, const T key) { 754 std::vector<T> ret; 755 typename T::size_type pos = 0, lpos = 0; 756 while ((pos = str.find(key, pos)) != T::npos) { 755 757 ret.push_back(str.substr(lpos, pos-lpos)); 756 758 lpos = ++pos; … … 789 791 return trim_left( trim_right( str , t) , t ); 790 792 } 791 inline std::pair<std::wstring,std::wstring> split(std::wstring str, std::wstring key) { 792 std::wstring::size_type pos = str.find(key); 793 if (pos == std::wstring::npos) 794 return std::pair<std::wstring,std::wstring>(str, _T("")); 795 return std::pair<std::wstring,std::wstring>(str.substr(0, pos), str.substr(pos+key.length())); 793 template<class T> 794 inline std::pair<T,T> split(T str, T key) { 795 typename T::size_type pos = str.find(key); 796 if (pos == T::npos) 797 return std::pair<T,T>(str, T()); 798 return std::pair<T,T>(str.substr(0, pos), str.substr(pos+key.length())); 796 799 } 797 800 typedef std::pair<std::wstring,std::wstring> token; … … 1056 1059 return utf8::cvt<std::string>(arg); 1057 1060 } 1061 template <typename T> std::string to_string(const wchar_t* arg) { 1062 return utf8::cvt<std::string>(std::wstring(arg)); 1063 } 1058 1064 template <typename T> std::wstring to_wstring(const T& arg) { 1059 1065 try { -
include/utils.h
r7443b58 rf33c12f 31 31 #define MAP_OPTIONS_BEGIN(args) \ 32 32 for (std::list<std::wstring>::const_iterator cit__=args.begin();cit__!=args.end();++cit__) { \ 33 std::pair<std::wstring,std::wstring> p__ = strEx::split(*cit__, _T("=")); if (false) {}33 std::pair<std::wstring,std::wstring> p__ = strEx::split(*cit__,std::wstring(_T("="))); if (false) {} 34 34 35 35 #define MAP_OPTIONS_SHOWALL(obj) \ … … 108 108 #define MAP_OPTIONS_SECONDARY_BEGIN(splt, arg) \ 109 109 else if (p__.first.find(splt) != std::wstring::npos) { \ 110 std::pair<std::wstring,std::wstring> arg = strEx::split(p__.first,s plt); if (false) {}110 std::pair<std::wstring,std::wstring> arg = strEx::split(p__.first,std::wstring(splt)); if (false) {} 111 111 112 112 #define MAP_OPTIONS_SECONDARY_STR_AND(opt, value, objfirst, objsecond, extra) \ -
modules/CheckHelpers/CheckHelpers.cpp
ra44cb15 rf33c12f 145 145 for (cit=arguments.begin();cit!=arguments.end();++cit) { 146 146 std::wstring arg = *cit; 147 std::pair<std::wstring,std::wstring> p = strEx::split(arg, _T("="));147 std::pair<std::wstring,std::wstring> p = strEx::split(arg,std::wstring(_T("="))); 148 148 if (p.first == _T("command")) { 149 149 if (!currentCommand.first.empty()) -
modules/DistributedClient/DistributedClient.cpp
r96c1461 rf33c12f 22 22 #include "DistributedClient.h" 23 23 #include <time.h> 24 #include < strEx.h>24 #include <boost/filesystem.hpp> 25 25 26 26 #include <strEx.h> … … 36 36 37 37 /** 38 * Default c-tor39 * @return40 */38 * Default c-tor 39 * @return 40 */ 41 41 DistributedClient::DistributedClient() {} 42 42 43 43 /** 44 * Default d-tor45 * @return46 */44 * Default d-tor 45 * @return 46 */ 47 47 DistributedClient::~DistributedClient() {} 48 48 49 49 /** 50 * Load (initiate) module.51 * Start the background collector thread and let it run until unloadModule() is called.52 * @return true53 */50 * Load (initiate) module. 51 * Start the background collector thread and let it run until unloadModule() is called. 52 * @return true 53 */ 54 54 bool DistributedClient::loadModule() { 55 55 return false; … … 70 70 sh::settings_registry settings(get_settings_proxy()); 71 71 settings.set_alias(_T("distributed"), alias, _T("client")); 72 73 72 target_path = settings.alias().get_settings_path(_T("targets")); 74 73 … … 81 80 (_T("targets"), sh::fun_values_path(boost::bind(&DistributedClient::add_target, this, _1, _2)), 82 81 _T("REMOTE TARGET DEFINITIONS"), _T("")) 82 83 83 ; 84 84 … … 89 89 ; 90 90 91 settings.alias( _T("/targets/default")).add_key_to_settings()91 settings.alias().add_key_to_settings(_T("targets/default")) 92 92 93 93 (_T("timeout"), sh::uint_key(&timeout, 30), … … 140 140 return true; 141 141 } 142 std::string get_command(std::string alias, std::string command = "") { 143 if (!alias.empty()) 144 return alias; 145 if (!command.empty()) 146 return command; 147 return "_NRPE_CHECK"; 148 } 142 149 143 150 ////////////////////////////////////////////////////////////////////////// … … 147 154 void DistributedClient::add_target(std::wstring key, std::wstring arg) { 148 155 try { 149 targets.add(get_settings_proxy(), target_path , key, arg); 156 nscapi::target_handler::target t = targets.add(get_settings_proxy(), target_path , key, arg); 157 if (t.has_option(_T("certificate"))) { 158 boost::filesystem::wpath p = t.options[_T("certificate")]; 159 if (!boost::filesystem::is_regular(p)) { 160 p = get_core()->getBasePath() / p; 161 t.options[_T("certificate")] = utf8::cvt<std::wstring>(p.string()); 162 targets.add(t); 163 } 164 if (boost::filesystem::is_regular(p)) { 165 NSC_DEBUG_MSG_STD(_T("Using certificate: ") + p.string()); 166 } else { 167 NSC_LOG_ERROR_STD(_T("Certificate not found: ") + p.string()); 168 } 169 } 150 170 } catch (...) { 151 171 NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); … … 157 177 std::wstring key = commands.add_command(name, args); 158 178 if (!key.empty()) 159 register_command(key.c_str(), _T(" Custom commandfor: ") + name);179 register_command(key.c_str(), _T("DNSCP relay for: ") + name); 160 180 } catch (boost::program_options::validation_error &e) { 161 181 NSC_LOG_ERROR_STD(_T("Could not add command ") + name + _T(": ") + utf8::to_unicode(e.what())); … … 166 186 167 187 /** 168 * Unload (terminate) module.169 * Attempt to stop the background processing thread.170 * @return true if successfully, false if not (if not things might be bad)171 */188 * Unload (terminate) module. 189 * Attempt to stop the background processing thread. 190 * @return true if successfully, false if not (if not things might be bad) 191 */ 172 192 bool DistributedClient::unloadModule() { 173 193 return true; 174 194 } 175 195 176 NSCAPI::nagiosReturn DistributedClient::handle Command(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) {177 std::wstring cmd = client::command_line_parser::parse_command(command, _T("dist"));178 196 NSCAPI::nagiosReturn DistributedClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 197 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 198 std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 179 199 client::configuration config; 180 200 setup(config); 181 if (cmd == _T("query")) 182 return client::command_line_parser::query(config, cmd, arguments, message, perf); 183 if (cmd == _T("submit")) { 184 boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 185 message = result.get<1>(); 186 return result.get<0>(); 187 } 188 if (cmd == _T("exec")) { 189 return client::command_line_parser::exec(config, cmd, arguments, message); 190 } 191 return commands.exec_simple(config, target, command, arguments, message, perf); 192 } 193 194 int DistributedClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 195 std::wstring cmd = client::command_line_parser::parse_command(command, _T("nrpe")); 201 if (!client::command_line_parser::is_command(cmd)) 202 return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 203 return commands.exec_simple(config, data.target, char_command, data.args, result); 204 } 205 206 int DistributedClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 207 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 208 std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 196 209 if (!client::command_line_parser::is_command(cmd)) 197 210 return NSCAPI::returnIgnored; 198 199 211 client::configuration config; 200 212 setup(config); 201 return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 202 } 203 204 NSCAPI::nagiosReturn DistributedClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 205 try { 206 client::configuration config; 207 setup(config); 208 209 if (!client::command_line_parser::relay_submit(config, request, response)) { 210 NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 211 return NSCAPI::hasFailed; 212 } 213 return NSCAPI::isSuccess; 214 } catch (std::exception &e) { 215 NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::to_unicode(e.what())); 216 return NSCAPI::hasFailed; 217 } catch (...) { 218 NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 219 return NSCAPI::hasFailed; 220 } 213 return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 214 } 215 216 NSCAPI::nagiosReturn DistributedClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 217 client::configuration config; 218 setup(config); 219 return client::command_line_parser::do_relay_submit(config, request, result); 221 220 } 222 221 … … 227 226 void DistributedClient::add_local_options(po::options_description &desc, client::configuration::data_type data) { 228 227 desc.add_options() 228 ("certificate,c", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "certificate", _1)), 229 "Length of payload (has to be same as on the server)") 230 /* 231 ("no-ssl,n", po::value<bool>(&command_data.no_ssl)->zero_tokens()->default_value(false), "Do not initial an ssl handshake with the server, talk in plain text.") 232 233 ("cert,c", po::value<std::wstring>(&command_data.cert)->default_value(cert_), "Certificate to use.") 234 */ 229 235 ; 230 236 } … … 235 241 236 242 net::wurl url; 237 url.protocol = _T(" nrpe");238 url.port = 566 6;243 url.protocol = _T("dnscp"); 244 url.port = 5669; 239 245 nscapi::target_handler::optarget opt = targets.find_target(_T("default")); 240 246 if (opt) { … … 245 251 url.port = strEx::stoi(t.options[_T("port")]); 246 252 } catch (...) {} 247 }253 } 248 254 std::string keys[] = {"certificate", "timeout", "payload length", "ssl"}; 249 255 BOOST_FOREACH(std::string s, keys) { … … 270 276 // 271 277 272 278 std::string gather_and_log_errors(std::string &payload) { 279 NSCPIPC::ErrorMessage message; 280 message.ParseFromString(payload); 281 std::string ret; 282 for (int i=0;i<message.error_size();i++) { 283 ret += message.error(i).message(); 284 NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 285 } 286 return ret; 287 } 273 288 int DistributedClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 274 NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 275 try { 276 277 Plugin::QueryRequestMessage request_message; 278 request_message.ParseFromString(request); 279 connection_data con = parse_header(*header); 280 281 std::list<nscp::packet> chunks; 282 chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request, 0)); 283 chunks = instance->send(con, chunks); 284 BOOST_FOREACH(nscp::packet &packet, chunks) { 285 if (nscp::checks::is_query_response(packet)) { 286 reply = packet.payload; 287 } else if (nscp::checks::is_error(packet)) { 288 NSCPIPC::ErrorMessage message; 289 message.ParseFromString(packet.payload); 290 for (int i=0;i<message.error_size();i++) { 291 NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 292 } 293 ret = NSCAPI::returnUNKNOWN; 294 } else { 295 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(packet.signature.payload_type)); 296 ret = NSCAPI::returnUNKNOWN; 297 } 298 } 299 return ret; 300 } catch (std::exception &e) { 301 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 302 return NSCAPI::returnUNKNOWN; 303 } 289 int ret = NSCAPI::returnUNKNOWN; 290 Plugin::QueryRequestMessage request_message; 291 request_message.ParseFromString(request); 292 connection_data con = parse_header(*header); 293 294 Plugin::QueryResponseMessage response_message; 295 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 296 297 std::list<nscp::packet> chunks; 298 chunks.push_back(nscp::factory::create_envelope_request(1)); 299 chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request, 0)); 300 chunks = instance->send(con, chunks); 301 BOOST_FOREACH(nscp::packet &chunk, chunks) { 302 if (nscp::checks::is_query_response(chunk)) { 303 nscapi::functions::append_response_payloads(response_message, chunk.payload); 304 } else if (nscp::checks::is_error(chunk)) { 305 std::string error = gather_and_log_errors(chunk.payload); 306 nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 307 ret = NSCAPI::returnUNKNOWN; 308 } else { 309 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 310 nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 311 ret = NSCAPI::returnUNKNOWN; 312 } 313 } 314 response_message.SerializeToString(&reply); 315 return ret; 304 316 } 305 317 306 318 int DistributedClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 307 std::list<std::string> result; 308 std::wstring channel; 309 try { 310 311 Plugin::SubmitRequestMessage message; 312 message.ParseFromString(request); 313 connection_data con = parse_header(*header); 314 channel = utf8::cvt<std::wstring>(message.channel()); 315 316 std::list<nscp::packet> chunks; 317 chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request, 0)); 318 chunks = instance->send(con, chunks); 319 BOOST_FOREACH(nscp::packet &chunk, chunks) { 320 if (nscp::checks::is_query_response(chunk)) { 321 result.push_back(chunk.payload); 322 } else if (nscp::checks::is_error(chunk)) { 323 NSCPIPC::ErrorMessage message; 324 message.ParseFromString(chunk.payload); 325 for (int i=0;i<message.error_size();i++) { 326 result.push_back("Error: " + message.error(i).message()); 327 } 328 } else { 329 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 330 result.push_back("Invalid payload"); 331 } 332 } 333 if (result.empty()) { 334 std::wstring msg; 335 BOOST_FOREACH(std::string &e, result) { 336 msg += utf8::cvt<std::wstring>(e); 337 } 338 nscapi::functions::create_simple_submit_response(channel, _T(""), result.empty()?NSCAPI::isSuccess:NSCAPI::hasFailed, msg, reply); 339 } 340 341 return result.empty()?NSCAPI::isSuccess:NSCAPI::hasFailed; 342 } catch (std::exception &e) { 343 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 344 nscapi::functions::create_simple_submit_response(channel, _T(""), NSCAPI::hasFailed, utf8::cvt<std::wstring>(e.what()), reply); 345 return NSCAPI::hasFailed; 346 } 319 int ret = NSCAPI::returnUNKNOWN; 320 Plugin::SubmitRequestMessage request_message; 321 request_message.ParseFromString(request); 322 connection_data con = parse_header(*header); 323 Plugin::SubmitResponseMessage response_message; 324 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 325 326 std::list<nscp::packet> chunks; 327 chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request, 0)); 328 chunks = instance->send(con, chunks); 329 BOOST_FOREACH(nscp::packet &chunk, chunks) { 330 if (nscp::checks::is_submit_response(chunk)) { 331 nscapi::functions::append_response_payloads(response_message, chunk.payload); 332 } else if (nscp::checks::is_error(chunk)) { 333 std::string error = gather_and_log_errors(chunk.payload); 334 nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 335 ret = NSCAPI::returnUNKNOWN; 336 } else { 337 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 338 nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 339 ret = NSCAPI::returnUNKNOWN; 340 } 341 } 342 response_message.SerializeToString(&reply); 343 return ret; 347 344 } 348 345 349 346 int DistributedClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 350 347 int ret = NSCAPI::returnOK; 351 try { 352 Plugin::ExecuteRequestMessage request_message; 353 request_message.ParseFromString(request); 354 connection_data con = parse_header(*header); 355 356 std::list<nscp::packet> chunks; 357 chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request, 0)); 358 chunks = instance->send(con, chunks); 359 BOOST_FOREACH(nscp::packet &chunk, chunks) { 360 if (nscp::checks::is_exec_response(chunk)) { 361 reply = chunk.payload; 362 } else if (nscp::checks::is_error(chunk)) { 363 NSCPIPC::ErrorMessage message; 364 message.ParseFromString(chunk.payload); 365 for (int i=0;i<message.error_size();i++) { 366 NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 367 ret = NSCAPI::returnUNKNOWN; 368 } 369 } else { 370 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 371 ret = NSCAPI::returnUNKNOWN; 372 } 373 } 374 return ret; 375 } catch (std::exception &e) { 376 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 377 return NSCAPI::returnUNKNOWN; 378 } 348 Plugin::ExecuteRequestMessage request_message; 349 request_message.ParseFromString(request); 350 connection_data con = parse_header(*header); 351 352 Plugin::ExecuteResponseMessage response_message; 353 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 354 355 std::list<nscp::packet> chunks; 356 chunks.push_back(nscp::factory::create_envelope_request(1)); 357 chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request, 0)); 358 chunks = instance->send(con, chunks); 359 BOOST_FOREACH(nscp::packet &chunk, chunks) { 360 if (nscp::checks::is_exec_response(chunk)) { 361 nscapi::functions::append_response_payloads(response_message, chunk.payload); 362 } else if (nscp::checks::is_error(chunk)) { 363 std::string error = gather_and_log_errors(chunk.payload); 364 nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 365 ret = NSCAPI::returnUNKNOWN; 366 } else { 367 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 368 nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 369 ret = NSCAPI::returnUNKNOWN; 370 } 371 } 372 response_message.SerializeToString(&reply); 373 return ret; 379 374 } 380 375 … … 389 384 std::wstring host = generic_data->host; 390 385 if (host.empty() && !generic_data->target.empty()) { 391 nscapi::target_handler::optarget t = targets.find_target(generic_data->target);392 if (t)393 host = (*t).host;386 nscapi::target_handler::optarget t = targets.find_target(generic_data->target); 387 if (t) 388 host = (*t).host; 394 389 } 395 390 */ -
modules/DistributedClient/DistributedClient.h
r96c1461 rf33c12f 39 39 40 40 41 class DistributedClient : public nscapi::impl::simple_ command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec{41 class DistributedClient : public nscapi::impl::simple_plugin { 42 42 private: 43 43 … … 49 49 50 50 struct connection_data { 51 std::string cert;52 51 std::string address; 53 52 int timeout; … … 56 55 57 56 connection_data(nscapi::functions::destination_container recipient) { 58 cert = recipient.get_string_data("certificate");59 57 timeout = recipient.get_int_data("timeout", 30); 60 buffer_length = recipient.get_int_data("payload length", 1024); 61 use_ssl = recipient.get_bool_data("ssl"); 62 if (recipient.has_data("no ssl")) 63 use_ssl = !recipient.get_bool_data("no ssl"); 58 64 59 address = recipient.address; 65 60 } … … 69 64 ss << _T("address: ") << utf8::cvt<std::wstring>(address); 70 65 ss << _T(", timeout: ") << timeout; 71 ss << _T(", buffer_length: ") << buffer_length;72 ss << _T(", use_ssl: ") << use_ssl;73 ss << _T(", certificate: ") << utf8::cvt<std::wstring>(cert);74 66 return ss.str(); 75 67 } … … 137 129 bool hasNotificationHandler() { return true; }; 138 130 NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 139 NSCAPI::nagiosReturn handle Command(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf);140 int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result);131 NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 132 NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 141 133 142 134 private: -
modules/DistributedServer/handler_impl.cpp
r98113da rf33c12f 145 145 std::string outBuffer; 146 146 if (command.empty() || command == _T("_NSCP_CHECK")) { 147 nscapi::functions::create_simple_exec_response (_T("_NSCP_CHECK"), NSCAPI::returnOK, _T("I (") + nscapi::plugin_singleton->get_core()->getApplicationVersionString() + _T(") seem to be doing fine..."), outBuffer);147 nscapi::functions::create_simple_exec_response<std::string>("_NSCP_CHECK", NSCAPI::returnOK, "I (" + utf8::cvt<std::string>(nscapi::plugin_singleton->get_core()->getApplicationVersionString()) + ") seem to be doing fine...", outBuffer); 148 148 } else if (!allowArgs_ && payload.arguments_size() > 0) { 149 149 nscapi::functions::create_simple_exec_response(command, NSCAPI::returnUNKNOWN, _T("Arguments not allowed for command: ") + command, outBuffer); -
modules/NRPEClient/NRPEClient.cpp
r9853bc3 rf33c12f 54 54 55 55 bool NRPEClient::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 56 std::map<std::wstring,std::wstring> commands;57 56 58 57 std::wstring certificate; … … 187 186 } 188 187 189 NSCAPI::nagiosReturn NRPEClient::handle Command(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) {190 std::wstring cmd = client::command_line_parser::parse_command(command, _T("nrpe"));191 188 NSCAPI::nagiosReturn NRPEClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 189 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 190 std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 192 191 client::configuration config; 193 192 setup(config); 194 if (cmd == _T("query")) 195 return client::command_line_parser::query(config, cmd, arguments, message, perf); 196 if (cmd == _T("submit")) { 197 boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 198 message = result.get<1>(); 199 return result.get<0>(); 200 } 201 if (cmd == _T("exec")) { 202 return client::command_line_parser::exec(config, cmd, arguments, message); 203 } 204 return commands.exec_simple(config, target, command, arguments, message, perf); 205 } 206 207 int NRPEClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 208 std::wstring cmd = client::command_line_parser::parse_command(command, _T("nrpe")); 193 if (!client::command_line_parser::is_command(cmd)) 194 return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 195 return commands.exec_simple(config, data.target, char_command, data.args, result); 196 } 197 198 NSCAPI::nagiosReturn NRPEClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 199 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 200 std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 209 201 if (!client::command_line_parser::is_command(cmd)) 210 202 return NSCAPI::returnIgnored; 211 212 203 client::configuration config; 213 204 setup(config); 214 return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 215 } 216 217 NSCAPI::nagiosReturn NRPEClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 218 try { 219 client::configuration config; 220 setup(config); 221 222 if (!client::command_line_parser::relay_submit(config, request, response)) { 223 NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 224 return NSCAPI::hasFailed; 225 } 226 return NSCAPI::isSuccess; 227 } catch (std::exception &e) { 228 NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::to_unicode(e.what())); 229 return NSCAPI::hasFailed; 230 } catch (...) { 231 NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 232 return NSCAPI::hasFailed; 233 } 205 return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 206 } 207 208 NSCAPI::nagiosReturn NRPEClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 209 client::configuration config; 210 setup(config); 211 return client::command_line_parser::do_relay_submit(config, request, result); 234 212 } 235 213 … … 292 270 293 271 int NRPEClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 294 NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 295 try { 296 Plugin::QueryRequestMessage request_message; 297 request_message.ParseFromString(request); 298 connection_data con = parse_header(*header); 299 300 Plugin::QueryResponseMessage response_message; 301 nscapi::functions::create_simple_header(response_message.mutable_header()); // TODO copy request header (inverted) 302 303 for (int i=0;i<request_message.payload_size();i++) { 304 std::string command = get_command(request_message.payload(i).alias(), request_message.payload(i).command()); 305 std::string data = command; 306 for (int a=0;a<request_message.payload(i).arguments_size();a++) { 307 data += "!" + request_message.payload(i).arguments(a); 308 } 309 boost::tuple<int,std::wstring> ret = instance->send(con, data); 310 std::pair<std::wstring,std::wstring> rdata = strEx::split(ret.get<1>(), _T("|")); 311 nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), utf8::cvt<std::wstring>(command), ret.get<0>(), rdata.first, rdata.second); 312 } 313 response_message.SerializeToString(&reply); 314 return NSCAPI::isSuccess; 315 } catch (std::exception &e) { 316 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 317 nscapi::functions::create_simple_query_response(_T("command"), NSCAPI::returnUNKNOWN, _T("Exception: ") + utf8::to_unicode(e.what()), _T(""), reply); 318 return NSCAPI::returnUNKNOWN; 319 } 272 Plugin::QueryRequestMessage request_message; 273 request_message.ParseFromString(request); 274 connection_data con = parse_header(*header); 275 276 Plugin::QueryResponseMessage response_message; 277 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 278 279 for (int i=0;i<request_message.payload_size();i++) { 280 std::string command = get_command(request_message.payload(i).alias(), request_message.payload(i).command()); 281 std::string data = command; 282 for (int a=0;a<request_message.payload(i).arguments_size();a++) { 283 data += "!" + request_message.payload(i).arguments(a); 284 } 285 boost::tuple<int,std::wstring> ret = instance->send(con, data); 286 std::pair<std::wstring,std::wstring> rdata = strEx::split(ret.get<1>(), std::wstring(_T("|"))); 287 nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), utf8::cvt<std::wstring>(command), ret.get<0>(), rdata.first, rdata.second); 288 } 289 response_message.SerializeToString(&reply); 290 return NSCAPI::isSuccess; 320 291 } 321 292 322 293 int NRPEClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 323 std::wstring channel; 324 try { 325 Plugin::SubmitRequestMessage message; 326 message.ParseFromString(request); 327 connection_data con = parse_header(*header); 328 channel = utf8::cvt<std::wstring>(message.channel()); 329 330 for (int i=0;i<message.payload_size();++i) { 331 std::string command = get_command(message.payload(i).alias(), message.payload(i).command()); 332 std::string data = command; 333 for (int a=0;a<message.payload(i).arguments_size();a++) { 334 data += "!" + message.payload(i).arguments(i); 335 } 336 boost::tuple<int,std::wstring> ret = instance->send(con, data); 337 // TODO: Change this to append! 338 nscapi::functions::create_simple_submit_response(channel, utf8::cvt<std::wstring>(command), ret.get<0>(), _T("Message submitted successfully: ") + ret.get<1>(), reply); 339 return NSCAPI::isSuccess; 340 } 341 nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), NSCAPI::returnUNKNOWN, _T("Empty message was submitted"), reply); 342 return NSCAPI::isSuccess; 343 } catch (std::exception &e) { 344 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 345 nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), NSCAPI::returnUNKNOWN, utf8::to_unicode(e.what()), reply); 346 return NSCAPI::hasFailed; 347 } 294 Plugin::SubmitRequestMessage request_message; 295 request_message.ParseFromString(request); 296 connection_data con = parse_header(*header); 297 std::wstring channel = utf8::cvt<std::wstring>(request_message.channel()); 298 299 Plugin::SubmitResponseMessage response_message; 300 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 301 302 for (int i=0;i<request_message.payload_size();++i) { 303 std::string command = get_command(request_message.payload(i).alias(), request_message.payload(i).command()); 304 std::string data = command; 305 for (int a=0;a<request_message.payload(i).arguments_size();a++) { 306 data += "!" + request_message.payload(i).arguments(i); 307 } 308 boost::tuple<int,std::wstring> ret = instance->send(con, data); 309 nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), command, ret.get<0>(), utf8::cvt<std::string>(ret.get<1>())); 310 } 311 response_message.SerializeToString(&reply); 312 return NSCAPI::isSuccess; 348 313 } 349 314 350 315 int NRPEClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 351 NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 352 try { 353 Plugin::ExecuteRequestMessage request_message; 354 request_message.ParseFromString(request); 355 connection_data con = parse_header(*header); 356 357 for (int i=0;i<request_message.payload_size();i++) { 358 std::string command = get_command(request_message.payload(i).command()); 359 std::string data = command; 360 for (int a=0;a<request_message.payload(i).arguments_size();a++) { 361 data += "!" + request_message.payload(i).arguments(a); 362 } 363 boost::tuple<int,std::wstring> ret = instance->send(con, data); 364 nscapi::functions::create_simple_exec_response(utf8::cvt<std::wstring>(command), ret.get<0>(), ret.get<1>(), reply); 365 } 366 return NSCAPI::isSuccess; 367 } catch (std::exception &e) { 368 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 369 nscapi::functions::create_simple_exec_response(_T("command"), NSCAPI::returnUNKNOWN, _T("Exception: ") + utf8::to_unicode(e.what()), reply); 370 return NSCAPI::hasFailed; 371 } 316 Plugin::ExecuteRequestMessage request_message; 317 request_message.ParseFromString(request); 318 connection_data con = parse_header(*header); 319 320 Plugin::ExecuteResponseMessage response_message; 321 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 322 323 for (int i=0;i<request_message.payload_size();i++) { 324 std::string command = get_command(request_message.payload(i).command()); 325 std::string data = command; 326 for (int a=0;a<request_message.payload(i).arguments_size();a++) 327 data += "!" + request_message.payload(i).arguments(a); 328 boost::tuple<int,std::wstring> ret = instance->send(con, data); 329 nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), command, ret.get<0>(), utf8::cvt<std::string>(ret.get<1>())); 330 } 331 response_message.SerializeToString(&reply); 332 return NSCAPI::isSuccess; 372 333 } 373 334 -
modules/NRPEClient/NRPEClient.h
r96c1461 rf33c12f 34 34 namespace po = boost::program_options; 35 35 36 class NRPEClient : public nscapi::impl::simple_ command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec{36 class NRPEClient : public nscapi::impl::simple_plugin { 37 37 private: 38 38 … … 145 145 bool hasNotificationHandler() { return true; }; 146 146 NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 147 NSCAPI::nagiosReturn handle Command(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf);148 int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result);147 NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 148 NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 149 149 150 150 private: -
modules/NSCAClient/NSCAClient.cpp
r9853bc3 rf33c12f 222 222 } 223 223 224 NSCAPI::nagiosReturn NSCAAgent::handle Command(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) {225 std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca"));226 224 NSCAPI::nagiosReturn NSCAAgent::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 225 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 226 std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 227 227 client::configuration config; 228 228 setup(config); 229 if (cmd == _T("query")) 230 return client::command_line_parser::query(config, cmd, arguments, message, perf); 231 if (cmd == _T("submit")) { 232 boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 233 message = result.get<1>(); 234 return result.get<0>(); 235 } 236 if (cmd == _T("exec")) { 237 return client::command_line_parser::exec(config, cmd, arguments, message); 238 } 239 return commands.exec_simple(config, target, command, arguments, message, perf); 240 } 241 242 int NSCAAgent::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 243 std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca")); 229 if (!client::command_line_parser::is_command(cmd)) 230 return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 231 return commands.exec_simple(config, data.target, char_command, data.args, result); 232 } 233 234 NSCAPI::nagiosReturn NSCAAgent::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 235 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 236 std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 244 237 if (!client::command_line_parser::is_command(cmd)) 245 238 return NSCAPI::returnIgnored; 246 247 239 client::configuration config; 248 240 setup(config); 249 return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 250 } 251 252 NSCAPI::nagiosReturn NSCAAgent::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 253 try { 254 client::configuration config; 255 setup(config); 256 257 if (!client::command_line_parser::relay_submit(config, request, response)) { 258 NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 259 return NSCAPI::hasFailed; 260 } 261 return NSCAPI::isSuccess; 262 } catch (nsca::nsca_encrypt::encryption_exception &e) { 263 NSC_LOG_ERROR_STD(_T("Failed to encrypt data: ") + utf8::to_unicode(e.what())); 264 return NSCAPI::hasFailed; 265 } catch (std::exception &e) { 266 NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::to_unicode(e.what())); 267 return NSCAPI::hasFailed; 268 } catch (...) { 269 NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 270 return NSCAPI::hasFailed; 271 } 241 return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 242 } 243 244 NSCAPI::nagiosReturn NSCAAgent::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 245 client::configuration config; 246 setup(config); 247 return client::command_line_parser::do_relay_submit(config, request, result); 272 248 } 273 249 … … 337 313 338 314 int NSCAAgent::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 339 NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 340 try { 341 Plugin::QueryRequestMessage request_message; 342 request_message.ParseFromString(request); 343 connection_data con = parse_header(*header); 344 345 std::list<nsca::packet> list; 346 for (int i=0;i < request_message.payload_size(); ++i) { 347 nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 348 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(request_message.payload(i)); 349 packet.code = 0; 350 packet.result = utf8::cvt<std::string>(data.command); 351 list.push_back(packet); 352 } 353 354 boost::tuple<int,std::wstring> ret = instance->send(con, list); 355 nscapi::functions::create_simple_query_response(_T("UNKNOWN"), ret.get<0>(), ret.get<1>(), _T(""), reply); 356 return NSCAPI::isSuccess; 357 } catch (std::exception &e) { 358 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 359 nscapi::functions::create_simple_query_response(_T("command"), NSCAPI::returnUNKNOWN, _T("Exception: ") + utf8::to_unicode(e.what()), _T(""), reply); 360 return NSCAPI::returnUNKNOWN; 361 } 315 Plugin::QueryRequestMessage request_message; 316 request_message.ParseFromString(request); 317 connection_data con = parse_header(*header); 318 319 Plugin::QueryResponseMessage response_message; 320 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 321 322 std::list<nsca::packet> list; 323 for (int i=0;i < request_message.payload_size(); ++i) { 324 nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 325 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(request_message.payload(i)); 326 packet.code = 0; 327 packet.result = utf8::cvt<std::string>(data.command); 328 list.push_back(packet); 329 } 330 331 boost::tuple<int,std::wstring> ret = instance->send(con, list); 332 333 nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "TODO", ret.get<0>(), utf8::cvt<std::string>(ret.get<1>()), ""); 334 response_message.SerializeToString(&reply); 335 return NSCAPI::isSuccess; 362 336 } 363 337 364 338 int NSCAAgent::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 365 std::wstring channel; 366 try { 367 Plugin::SubmitRequestMessage message; 368 message.ParseFromString(request); 369 370 connection_data con = parse_header(*header); 371 channel = utf8::cvt<std::wstring>(message.channel()); 372 373 std::list<nsca::packet> list; 374 375 for (int i=0;i < message.payload_size(); ++i) { 376 nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 377 std::wstring alias, msg; 378 packet.code = nscapi::functions::parse_simple_submit_request_payload(message.payload(i), alias, msg); 379 if (alias != _T("host_check")) 380 packet.service = utf8::cvt<std::string>(alias); 381 packet.result = utf8::cvt<std::string>(msg); 382 list.push_back(packet); 383 } 384 385 boost::tuple<int,std::wstring> ret = instance->send(con, list); 386 nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), ret.get<0>(), ret.get<1>(), reply); 387 return NSCAPI::isSuccess; 388 } catch (std::exception &e) { 389 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 390 nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), NSCAPI::returnUNKNOWN, utf8::to_unicode(e.what()), reply); 391 return NSCAPI::hasFailed; 392 } 339 Plugin::SubmitRequestMessage message; 340 message.ParseFromString(request); 341 connection_data con = parse_header(*header); 342 std::wstring channel = utf8::cvt<std::wstring>(message.channel()); 343 344 Plugin::SubmitResponseMessage response_message; 345 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 346 347 std::list<nsca::packet> list; 348 349 for (int i=0;i < message.payload_size(); ++i) { 350 nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 351 std::wstring alias, msg; 352 packet.code = nscapi::functions::parse_simple_submit_request_payload(message.payload(i), alias, msg); 353 if (alias != _T("host_check")) 354 packet.service = utf8::cvt<std::string>(alias); 355 packet.result = utf8::cvt<std::string>(msg); 356 list.push_back(packet); 357 } 358 359 boost::tuple<int,std::wstring> ret = instance->send(con, list); 360 nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "TODO", ret.get<0>(), utf8::cvt<std::string>(ret.get<1>())); 361 response_message.SerializeToString(&reply); 362 return NSCAPI::isSuccess; 393 363 } 394 364 395 365 int NSCAAgent::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 396 NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 397 try { 398 Plugin::ExecuteRequestMessage request_message; 399 request_message.ParseFromString(request); 400 connection_data con = parse_header(*header); 401 402 std::list<nsca::packet> list; 403 for (int i=0;i < request_message.payload_size(); ++i) { 404 nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 405 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request_payload(request_message.payload(i)); 406 packet.code = 0; 407 if (data.command != _T("host_check")) 408 packet.service = utf8::cvt<std::string>(data.command); 409 //packet.result = data.; 410 list.push_back(packet); 411 } 412 boost::tuple<int,std::wstring> ret = instance->send(con, list); 413 nscapi::functions::create_simple_exec_response(_T("UNKNOWN"), ret.get<0>(), ret.get<1>(), reply); 414 return NSCAPI::isSuccess; 415 } catch (std::exception &e) { 416 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 417 nscapi::functions::create_simple_exec_response(_T("command"), NSCAPI::returnUNKNOWN, _T("Exception: ") + utf8::to_unicode(e.what()), reply); 418 return NSCAPI::hasFailed; 419 } 366 Plugin::ExecuteRequestMessage request_message; 367 request_message.ParseFromString(request); 368 connection_data con = parse_header(*header); 369 370 Plugin::ExecuteResponseMessage response_message; 371 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 372 373 std::list<nsca::packet> list; 374 for (int i=0;i < request_message.payload_size(); ++i) { 375 nsca::packet packet(con.sender_hostname, con.buffer_length, con.time_delta); 376 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request_payload(request_message.payload(i)); 377 packet.code = 0; 378 if (data.command != _T("host_check")) 379 packet.service = utf8::cvt<std::string>(data.command); 380 //packet.result = data.; 381 list.push_back(packet); 382 } 383 boost::tuple<int,std::wstring> ret = instance->send(con, list); 384 nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), "TODO", ret.get<0>(), utf8::cvt<std::string>(ret.get<1>())); 385 response_message.SerializeToString(&reply); 386 return NSCAPI::isSuccess; 420 387 } 421 388 422 389 ////////////////////////////////////////////////////////////////////////// 423 390 // Protocol implementations 391 // 424 392 425 393 boost::tuple<int,std::wstring> NSCAAgent::send(connection_data data, const std::list<nsca::packet> packets) { … … 438 406 socket.send_nsca(packet, boost::posix_time::seconds(data.timeout)); 439 407 } 440 return NSCAPI::isSuccess;408 return boost::make_tuple(NSCAPI::returnUNKNOWN, _T("")); 441 409 } catch (nsca::nsca_encrypt::encryption_exception &e) { 442 410 NSC_LOG_ERROR_STD(_T("NSCA Error: ") + utf8::to_unicode(e.what())); -
modules/NSCAClient/NSCAClient.h
r9853bc3 rf33c12f 34 34 namespace po = boost::program_options; 35 35 36 class NSCAAgent : public nscapi::impl::simple_ command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec{36 class NSCAAgent : public nscapi::impl::simple_plugin { 37 37 private: 38 38 … … 148 148 bool hasNotificationHandler() { return true; }; 149 149 NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 150 NSCAPI::nagiosReturn handle Command(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf);151 int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result);150 NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 151 NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 152 152 153 153 private: -
modules/NSCPClient/NSCPClient.cpp
r9853bc3 rf33c12f 72 72 73 73 settings.alias().add_path_to_settings() 74 (_T("NSCP CLIENT SECTION"), _T("Section for NSCP active/passive check module.")) 74 75 75 76 (_T("handlers"), sh::fun_values_path(boost::bind(&NSCPClient::add_command, this, _1, _2)), … … 192 193 } 193 194 194 NSCAPI::nagiosReturn NSCPClient::handle Command(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) {195 std::wstring cmd = client::command_line_parser::parse_command(command, _T("nscp"));196 195 NSCAPI::nagiosReturn NSCPClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 196 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 197 std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 197 198 client::configuration config; 198 199 setup(config); 199 if (cmd == _T("query")) 200 return client::command_line_parser::query(config, cmd, arguments, message, perf); 201 if (cmd == _T("submit")) { 202 boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 203 message = result.get<1>(); 204 return result.get<0>(); 205 } 206 if (cmd == _T("exec")) { 207 return client::command_line_parser::exec(config, cmd, arguments, message); 208 } 209 return commands.exec_simple(config, target, command, arguments, message, perf); 210 } 211 212 int NSCPClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 213 std::wstring cmd = client::command_line_parser::parse_command(command, _T("nscp")); 200 if (!client::command_line_parser::is_command(cmd)) 201 return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 202 return commands.exec_simple(config, data.target, char_command, data.args, result); 203 } 204 205 NSCAPI::nagiosReturn NSCPClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 206 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 207 std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 214 208 if (!client::command_line_parser::is_command(cmd)) 215 209 return NSCAPI::returnIgnored; 216 217 210 client::configuration config; 218 211 setup(config); 219 return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 220 } 221 222 NSCAPI::nagiosReturn NSCPClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 223 try { 224 client::configuration config; 225 setup(config); 226 227 if (!client::command_line_parser::relay_submit(config, request, response)) { 228 NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 229 return NSCAPI::hasFailed; 230 } 231 return NSCAPI::isSuccess; 232 } catch (std::exception &e) { 233 NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::to_unicode(e.what())); 234 return NSCAPI::hasFailed; 235 } catch (...) { 236 NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 237 return NSCAPI::hasFailed; 238 } 212 return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 213 } 214 215 NSCAPI::nagiosReturn NSCPClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 216 client::configuration config; 217 setup(config); 218 return client::command_line_parser::do_relay_submit(config, request, result); 239 219 } 240 220 … … 295 275 // 296 276 277 std::string gather_and_log_errors(std::string &payload) { 278 NSCPIPC::ErrorMessage message; 279 message.ParseFromString(payload); 280 std::string ret; 281 for (int i=0;i<message.error_size();i++) { 282 ret += message.error(i).message(); 283 NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 284 } 285 return ret; 286 } 297 287 int NSCPClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 298 NSCAPI::nagiosReturn ret = NSCAPI::returnOK; 299 try { 300 301 Plugin::QueryRequestMessage request_message; 302 request_message.ParseFromString(request); 303 connection_data con = parse_header(*header); 304 305 // TODO: Humm, this cant be right?!?! 306 307 std::list<nscp::packet> chunks; 308 chunks.push_back(nscp::factory::create_envelope_request(1)); 309 chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request, 0)); 310 chunks = instance->send(con, chunks); 311 BOOST_FOREACH(nscp::packet &chunk, chunks) { 312 if (nscp::checks::is_query_response(chunk)) { 313 reply = chunk.payload; 314 } else if (nscp::checks::is_error(chunk)) { 315 NSCPIPC::ErrorMessage message; 316 message.ParseFromString(chunk.payload); 317 for (int i=0;i<message.error_size();i++) { 318 NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 319 } 320 ret = NSCAPI::returnUNKNOWN; 321 } else { 322 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 323 ret = NSCAPI::returnUNKNOWN; 324 } 325 } 326 return ret; 327 } catch (std::exception &e) { 328 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 329 return NSCAPI::returnUNKNOWN; 330 } 331 } 288 int ret = NSCAPI::returnUNKNOWN; 289 Plugin::QueryRequestMessage request_message; 290 request_message.ParseFromString(request); 291 connection_data con = parse_header(*header); 292 293 Plugin::QueryResponseMessage response_message; 294 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 295 296 std::list<nscp::packet> chunks; 297 chunks.push_back(nscp::factory::create_envelope_request(1)); 298 chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request, 0)); 299 chunks = instance->send(con, chunks); 300 BOOST_FOREACH(nscp::packet &chunk, chunks) { 301 if (nscp::checks::is_query_response(chunk)) { 302 nscapi::functions::append_response_payloads(response_message, chunk.payload); 303 } else if (nscp::checks::is_error(chunk)) { 304 std::string error = gather_and_log_errors(chunk.payload); 305 nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 306 ret = NSCAPI::returnUNKNOWN; 307 } else { 308 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 309 nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 310 ret = NSCAPI::returnUNKNOWN; 311 } 312 } 313 response_message.SerializeToString(&reply); 314 return ret; 315 } 316 332 317 int NSCPClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 333 std::list<std::string> result; 334 try { 335 336 Plugin::QueryRequestMessage request_message; 337 request_message.ParseFromString(request); 338 connection_data con = parse_header(*header); 339 340 // TODO: Humm, this cant be right?!?! 341 342 std::list<nscp::packet> chunks; 343 chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request, 0)); 344 chunks = instance->send(con, chunks); 345 BOOST_FOREACH(nscp::packet &chunk, chunks) { 346 if (nscp::checks::is_query_response(chunk)) { 347 result.push_back(chunk.payload); 348 } else if (nscp::checks::is_error(chunk)) { 349 NSCPIPC::ErrorMessage message; 350 message.ParseFromString(chunk.payload); 351 for (int i=0;i<message.error_size();i++) { 352 result.push_back("Error: " + message.error(i).message()); 353 } 354 } else { 355 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 356 result.push_back("Invalid payload"); 357 } 358 } 359 360 if (result.empty()) { 361 std::wstring msg; 362 BOOST_FOREACH(std::string &e, result) { 363 msg += utf8::cvt<std::wstring>(e); 364 } 365 nscapi::functions::create_simple_submit_response(_T(""), _T(""), result.empty()?NSCAPI::isSuccess:NSCAPI::hasFailed, msg, reply); 366 } 367 return result.empty()?NSCAPI::isSuccess:NSCAPI::hasFailed; 368 } catch (std::exception &e) { 369 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 370 nscapi::functions::create_simple_submit_response(_T(""), _T(""), NSCAPI::hasFailed, utf8::cvt<std::wstring>(e.what()), reply); 371 return NSCAPI::hasFailed; 372 } 373 } 318 int ret = NSCAPI::returnUNKNOWN; 319 Plugin::SubmitRequestMessage request_message; 320 request_message.ParseFromString(request); 321 connection_data con = parse_header(*header); 322 Plugin::SubmitResponseMessage response_message; 323 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 324 325 std::list<nscp::packet> chunks; 326 chunks.push_back(nscp::factory::create_payload(nscp::data::command_response, request, 0)); 327 chunks = instance->send(con, chunks); 328 BOOST_FOREACH(nscp::packet &chunk, chunks) { 329 if (nscp::checks::is_submit_response(chunk)) { 330 nscapi::functions::append_response_payloads(response_message, chunk.payload); 331 } else if (nscp::checks::is_error(chunk)) { 332 std::string error = gather_and_log_errors(chunk.payload); 333 nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 334 ret = NSCAPI::returnUNKNOWN; 335 } else { 336 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 337 nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 338 ret = NSCAPI::returnUNKNOWN; 339 } 340 } 341 response_message.SerializeToString(&reply); 342 return ret; 343 } 344 374 345 int NSCPClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 375 346 int ret = NSCAPI::returnOK; 376 try { 377 378 379 Plugin::QueryRequestMessage request_message; 380 request_message.ParseFromString(request); 381 connection_data con = parse_header(*header); 382 383 // TODO: Humm, this cant be right?!?! 384 385 std::list<nscp::packet> chunks; 386 chunks.push_back(nscp::factory::create_envelope_request(1)); 387 chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request, 0)); 388 chunks = instance->send(con, chunks); 389 BOOST_FOREACH(nscp::packet &chunk, chunks) { 390 if (nscp::checks::is_exec_response(chunk)) { 391 reply = chunk.payload; 392 } else if (nscp::checks::is_error(chunk)) { 393 NSCPIPC::ErrorMessage message; 394 message.ParseFromString(chunk.payload); 395 for (int i=0;i<message.error_size();i++) { 396 NSC_LOG_ERROR_STD(_T("Error: ") + utf8::cvt<std::wstring>(message.error(i).message())); 397 ret = NSCAPI::returnUNKNOWN; 398 } 399 } else { 400 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 401 ret = NSCAPI::returnUNKNOWN; 402 } 403 } 404 return ret; 405 } catch (std::exception &e) { 406 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 407 return NSCAPI::returnUNKNOWN; 408 } 347 Plugin::ExecuteRequestMessage request_message; 348 request_message.ParseFromString(request); 349 connection_data con = parse_header(*header); 350 351 Plugin::ExecuteResponseMessage response_message; 352 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 353 354 std::list<nscp::packet> chunks; 355 chunks.push_back(nscp::factory::create_envelope_request(1)); 356 chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request, 0)); 357 chunks = instance->send(con, chunks); 358 BOOST_FOREACH(nscp::packet &chunk, chunks) { 359 if (nscp::checks::is_exec_response(chunk)) { 360 nscapi::functions::append_response_payloads(response_message, chunk.payload); 361 } else if (nscp::checks::is_error(chunk)) { 362 std::string error = gather_and_log_errors(chunk.payload); 363 nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error); 364 ret = NSCAPI::returnUNKNOWN; 365 } else { 366 NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type)); 367 nscapi::functions::append_simple_exec_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type"); 368 ret = NSCAPI::returnUNKNOWN; 369 } 370 } 371 response_message.SerializeToString(&reply); 372 return ret; 409 373 } 410 374 -
modules/NSCPClient/NSCPClient.h
r96c1461 rf33c12f 35 35 36 36 37 class NSCPClient : public nscapi::impl::simple_ command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec{37 class NSCPClient : public nscapi::impl::simple_plugin { 38 38 private: 39 39 … … 142 142 bool hasNotificationHandler() { return true; }; 143 143 NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 144 NSCAPI::nagiosReturn handle Command(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf);145 int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result);144 NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 145 NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 146 146 147 147 private: -
modules/SMTPClient/SMTPClient.cpp
r96c1461 rf33c12f 19 19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 20 20 ***************************************************************************/ 21 22 21 #include "stdafx.h" 22 #include "SMTPClient.h" 23 23 24 24 #include <utils.h> … … 28 28 #include <strEx.h> 29 29 30 #include "SMTPClient.h"31 30 32 31 #include <settings/client/settings_client.hpp> … … 35 34 36 35 namespace sh = nscapi::settings_helper; 37 namespace str = nscp::helpers;38 namespace po = boost::program_options;39 36 40 37 /** … … 43 40 */ 44 41 SMTPClient::SMTPClient() {} 42 45 43 /** 46 44 * Default d-tor … … 48 46 */ 49 47 SMTPClient::~SMTPClient() {} 48 50 49 /** 51 50 * Load (initiate) module. … … 56 55 return false; 57 56 } 57 58 58 bool SMTPClient::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 59 59 60 60 std::wstring template_string, sender, recipient; 61 unsigned int timeout; 61 62 try { 62 63 sh::settings_registry settings(get_settings_proxy()); … … 66 67 settings.alias().add_path_to_settings() 67 68 (_T("SMTP CLIENT SECTION"), _T("Section for SMTP passive check module.")) 69 (_T("handlers"), sh::fun_values_path(boost::bind(&SMTPClient::add_command, this, _1, _2)), 70 _T("CLIENT HANDLER SECTION"), _T("")) 68 71 69 72 (_T("targets"), sh::fun_values_path(boost::bind(&SMTPClient::add_target, this, _1, _2)), 70 73 _T("REMOTE TARGET DEFINITIONS"), _T("")) 71 72 74 ; 73 75 74 76 settings.alias().add_key_to_settings() 75 (_T("hostname"), sh::string_key(&hostname_),76 _T("HOSTNAME"), _T("The host name of this host if set to blank (default) the windows name of the computer will be used."))77 78 77 (_T("channel"), sh::wstring_key(&channel_, _T("SMTP")), 79 78 _T("CHANNEL"), _T("The channel to listen to.")) … … 81 80 ; 82 81 82 settings.alias().add_key_to_settings(_T("targets/default")) 83 84 (_T("timeout"), sh::uint_key(&timeout, 30), 85 _T("TIMEOUT"), _T("Timeout when reading/writing packets to/from sockets.")) 86 87 (_T("sender"), sh::wpath_key(&sender, _T("nscp@localhost")), 88 _T("SENDER"), _T("Sender of email message")) 89 90 (_T("recipient"), sh::wpath_key(&recipient, _T("nscp@localhost")), 91 _T("RECIPIENT"), _T("Recipient of email message")) 92 93 (_T("template"), sh::wpath_key(&template_string, _T("Hello, this is %source% reporting %message%!")), 94 _T("TEMPLATE"), _T("Template for message data")) 95 96 ; 97 83 98 settings.register_all(); 84 99 settings.notify(); … … 86 101 get_core()->registerSubmissionListener(get_id(), channel_); 87 102 103 if (!targets.has_target(_T("default"))) { 104 add_target(_T("default"), _T("default")); 105 targets.rebuild(); 106 } 107 nscapi::target_handler::optarget t = targets.find_target(_T("default")); 108 if (t) { 109 if (!t->has_option("template")) 110 t->options[_T("template")] = template_string; 111 if (!t->has_option("timeout")) 112 t->options[_T("timeout")] = strEx::itos(timeout); 113 if (!t->has_option("recipient")) 114 t->options[_T("recipient")] = recipient; 115 if (!t->has_option("sender")) 116 t->options[_T("sender")] = sender; 117 targets.add(*t); 118 } else { 119 NSC_LOG_ERROR(_T("Default target not found!")); 120 } 121 88 122 } catch (nscapi::nscapi_exception &e) { 89 NSC_LOG_ERROR_STD(_T(" Failed to register command: ") + utf8::cvt<std::wstring>(e.what()));123 NSC_LOG_ERROR_STD(_T("NSClient API exception: ") + utf8::to_unicode(e.what())); 90 124 return false; 91 125 } catch (std::exception &e) { 92 NSC_LOG_ERROR_STD(_T("Exception caught: ") + utf8:: cvt<std::wstring>(e.what()));126 NSC_LOG_ERROR_STD(_T("Exception caught: ") + utf8::to_unicode(e.what())); 93 127 return false; 94 128 } catch (...) { 95 NSC_LOG_ERROR_STD(_T(" Failed to register command."));129 NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN EXCEPTION>")); 96 130 return false; 97 131 } 98 132 return true; 99 133 } 134 std::string get_command(std::string alias, std::string command = "") { 135 if (!alias.empty()) 136 return alias; 137 if (!command.empty()) 138 return command; 139 return "TODO"; 140 } 141 142 ////////////////////////////////////////////////////////////////////////// 143 // Settings helpers 144 // 145 146 void SMTPClient::add_target(std::wstring key, std::wstring arg) { 147 try { 148 targets.add(get_settings_proxy(), target_path , key, arg); 149 } catch (...) { 150 NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 151 } 152 } 153 154 void SMTPClient::add_command(std::wstring name, std::wstring args) { 155 try { 156 std::wstring key = commands.add_command(name, args); 157 if (!key.empty()) 158 register_command(key.c_str(), _T("NRPE relay for: ") + name); 159 } catch (boost::program_options::validation_error &e) { 160 NSC_LOG_ERROR_STD(_T("Could not add command ") + name + _T(": ") + utf8::to_unicode(e.what())); 161 } catch (...) { 162 NSC_LOG_ERROR_STD(_T("Could not add command ") + name); 163 } 164 } 165 100 166 /** 101 167 * Unload (terminate) module. … … 107 173 } 108 174 109 void SMTPClient::add_target(std::wstring key, std::wstring arg) { 110 try { 111 targets.add(get_settings_proxy(), target_path , key, arg); 112 } catch (...) { 113 NSC_LOG_ERROR_STD(_T("Failed to add target: ") + key); 114 } 115 } 116 void SMTPClient::add_local_options(po::options_description &desc, connection_data &command_data) { 117 /* 118 desc.add_options() 119 ("payload-length,l", po::value<unsigned int>(&command_data.payload_length)->zero_tokens()->default_value(512), "The payload length to use in the NSCA packet.") 120 ("encryption,e", po::value<std::string>(&command_data.encryption)->default_value("ASE"), "Encryption algorithm to use.") 121 ; 122 */ 123 } 124 125 void SMTPClient::setup(client::configuration &config) { 126 clp_handler_impl *handler = new clp_handler_impl(this); 127 add_local_options(config.local, handler->local_data); 128 config.handler = client::configuration::handler_type(handler); 129 } 130 131 NSCAPI::nagiosReturn SMTPClient::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) { 132 std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca")); 175 176 177 NSCAPI::nagiosReturn SMTPClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 178 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 179 std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 180 client::configuration config; 181 setup(config); 182 if (!client::command_line_parser::is_command(cmd)) 183 return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 184 return commands.exec_simple(config, data.target, char_command, data.args, result); 185 } 186 187 NSCAPI::nagiosReturn SMTPClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 188 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 189 std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 133 190 if (!client::command_line_parser::is_command(cmd)) 134 191 return NSCAPI::returnIgnored; 135 136 192 client::configuration config; 137 193 setup(config); 138 boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 139 message = result.get<1>(); 140 return result.get<0>(); 141 } 142 143 144 int SMTPClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 145 std::wstring cmd = client::command_line_parser::parse_command(command, _T("smtp")); 146 if (!client::command_line_parser::is_command(cmd)) 147 return NSCAPI::returnIgnored; 148 194 return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 195 } 196 197 NSCAPI::nagiosReturn SMTPClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 149 198 client::configuration config; 150 199 setup(config); 151 return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 152 } 153 154 155 NSCAPI::nagiosReturn SMTPClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 156 try { 157 client::configuration config; 158 net::wurl url; 159 url.protocol = _T("smtp"); 160 nscapi::target_handler::optarget target = targets.find_target(_T("default")); 161 if (target) { 162 url.host = target->host; 163 url.port = strEx::stoi(target->options[_T("port")]); 164 config.data->recipient.data["template"] = utf8::cvt<std::string>(target->options[_T("template")]); 165 config.data->recipient.data["recipient"] = utf8::cvt<std::string>(target->options[_T("recipient")]); 166 config.data->recipient.data["sender"] = utf8::cvt<std::string>(target->options[_T("sender")]); 200 return client::command_line_parser::do_relay_submit(config, request, result); 201 } 202 203 ////////////////////////////////////////////////////////////////////////// 204 // Parser setup/Helpers 205 // 206 207 void SMTPClient::add_local_options(po::options_description &desc, client::configuration::data_type data) { 208 209 desc.add_options() 210 ("sender", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "sender", _1)), 211 "Length of payload (has to be same as on the server)") 212 213 ("recipient", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "recipient", _1)), 214 "Length of payload (has to be same as on the server)") 215 216 ("template", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "template", _1)), 217 "Do not initial an ssl handshake with the server, talk in plaintext.") 218 ; 219 } 220 221 void SMTPClient::setup(client::configuration &config) { 222 boost::shared_ptr<clp_handler_impl> handler = boost::shared_ptr<clp_handler_impl>(new clp_handler_impl(this)); 223 add_local_options(config.local, config.data); 224 225 net::wurl url; 226 url.protocol = _T("smtp"); 227 url.port = 25; 228 nscapi::target_handler::optarget opt = targets.find_target(_T("default")); 229 if (opt) { 230 nscapi::target_handler::target t = *opt; 231 url.host = t.host; 232 if (t.has_option("port")) { 233 try { 234 url.port = strEx::stoi(t.options[_T("port")]); 235 } catch (...) {} 167 236 } 168 config.data->recipient.id = "default"; 169 config.data->recipient.address = utf8::cvt<std::string>(url.to_string()); 170 config.data->host_self.id = "self"; 171 config.data->host_self.host = hostname_; 172 173 setup(config); 174 if (!client::command_line_parser::relay_submit(config, request, response)) { 175 NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 176 return NSCAPI::hasFailed; 237 std::string keys[] = {"sender", "timeout", "recipient", "template"}; 238 BOOST_FOREACH(std::string s, keys) { 239 config.data->recipient.data[s] = utf8::cvt<std::string>(t.options[utf8::cvt<std::wstring>(s)]); 177 240 } 178 return NSCAPI::isSuccess; 179 } catch (std::exception &e) { 180 NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::cvt<std::wstring>(e.what())); 181 return NSCAPI::hasFailed; 182 } catch (...) { 183 NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 184 return NSCAPI::hasFailed; 185 } 241 } 242 config.data->recipient.id = "default"; 243 config.data->recipient.address = utf8::cvt<std::string>(url.to_string()); 244 config.data->host_self.id = "self"; 245 //config.data->host_self.host = hostname_; 246 247 config.target_lookup = handler; 248 config.handler = handler; 249 } 250 251 SMTPClient::connection_data SMTPClient::parse_header(const ::Plugin::Common_Header &header) { 252 nscapi::functions::destination_container recipient; 253 nscapi::functions::parse_destination(header, header.recipient_id(), recipient, true); 254 return connection_data(recipient); 186 255 } 187 256 … … 192 261 return NSCAPI::hasFailed; 193 262 } 263 264 int SMTPClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 265 Plugin::SubmitRequestMessage request_message; 266 request_message.ParseFromString(request); 267 connection_data con = parse_header(*header); 268 std::wstring channel = utf8::cvt<std::wstring>(request_message.channel()); 269 270 Plugin::SubmitResponseMessage response_message; 271 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 272 273 for (int i=0;i < request_message.payload_size(); ++i) { 274 const ::Plugin::QueryResponseMessage::Response& payload = request_message.payload(i); 275 boost::asio::io_service io_service; 276 boost::shared_ptr<smtp::client::smtp_client> client(new smtp::client::smtp_client(io_service)); 277 std::list<std::string> recipients; 278 std::string message = con.template_string; 279 strEx::replace(message, "%message%", payload.message()); 280 recipients.push_back(con.recipient_str); 281 client->send_mail(con.sender, recipients, "Hello world\n"); 282 io_service.run(); 283 nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "TODO", NSCAPI::returnOK, "Message send successfully"); 284 } 285 286 response_message.SerializeToString(&reply); 287 return NSCAPI::isSuccess; 288 289 } 290 194 291 int SMTPClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 195 292 NSC_LOG_ERROR_STD(_T("SMTP does not support exec patterns")); … … 197 294 } 198 295 199 int SMTPClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 200 try { 201 202 Plugin::SubmitRequestMessage message; 203 message.ParseFromString(request); 204 205 std::wstring alias, command, msg, perf; 206 nscapi::functions::destination_container recipient; // = data->host_default_recipient; 207 nscapi::functions::parse_destination(*header, header->recipient_id(), recipient, true); 208 nscapi::functions::destination_container source; 209 nscapi::functions::parse_destination(*header, header->source_id(), source); 210 211 for (int i=0;i < message.payload_size(); ++i) { 212 boost::asio::io_service io_service; 213 boost::shared_ptr<smtp::client::smtp_client> client(new smtp::client::smtp_client(io_service)); 214 std::list<std::string> recipients; 215 recipients.push_back("michael@medin.name"); 216 client->send_mail("test@test.medin.name", recipients, "Hello world\n"); 217 io_service.run(); 218 } 219 220 nscapi::functions::create_simple_submit_response(_T(""), _T(""), NSCAPI::isSuccess, _T(""), reply); 221 return NSCAPI::isSuccess; 222 } catch (std::exception &e) { 223 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 224 nscapi::functions::create_simple_submit_response(_T(""), _T(""), NSCAPI::hasFailed, utf8::cvt<std::wstring>(e.what()), reply); 225 return NSCAPI::hasFailed; 226 } 227 } 296 ////////////////////////////////////////////////////////////////////////// 297 // Protocol implementations 298 // 228 299 229 300 NSC_WRAP_DLL(); 230 301 NSC_WRAPPERS_MAIN_DEF(SMTPClient); 231 302 NSC_WRAPPERS_IGNORE_MSG_DEF(); 232 NSC_WRAPPERS_IGNORE_CMD_DEF(); 303 NSC_WRAPPERS_HANDLE_CMD_DEF(); 304 NSC_WRAPPERS_CLI_DEF(); 233 305 NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF(); 306 -
modules/SMTPClient/SMTPClient.h
r96c1461 rf33c12f 23 23 24 24 #include <client/command_line_parser.hpp> 25 #include <boost/program_options.hpp>26 27 25 #include <nscapi/targets.hpp> 28 26 29 27 NSC_WRAPPERS_MAIN(); 28 NSC_WRAPPERS_CLI(); 30 29 NSC_WRAPPERS_CHANNELS(); 30 31 namespace po = boost::program_options; 31 32 32 33 class SMTPClient : public nscapi::impl::simple_plugin { 33 34 private: 34 35 35 std::string hostname_;36 36 std::wstring channel_; 37 nscapi::target_handler targets;38 37 std::wstring target_path; 39 38 40 struct connection_data : public client::nscp_cli_data {}; 41 struct clp_handler_impl : public client::clp_handler { 39 nscapi::target_handler targets; 40 client::command_manager commands; 41 42 struct connection_data { 43 std::string recipient_str; 44 std::string sender; 45 std::string template_string; 46 std::string host; 47 std::string port; 48 int timeout; 49 50 connection_data(nscapi::functions::destination_container recipient) { 51 recipient_str = recipient.get_string_data("recipient"); 52 timeout = recipient.get_int_data("timeout", 30); 53 sender = recipient.get_string_data("sender"); 54 template_string = recipient.get_string_data("template"); 55 56 net::url url = recipient.get_url(25); 57 host = url.host; 58 port = url.get_port(); 59 } 60 61 std::wstring to_wstring() const { 62 std::wstringstream ss; 63 ss << _T("host: ") << utf8::cvt<std::wstring>(host); 64 ss << _T(", port: ") << utf8::cvt<std::wstring>(port); 65 ss << _T(", timeout: ") << timeout; 66 ss << _T(", recipient: ") << utf8::cvt<std::wstring>(recipient_str); 67 ss << _T(", sender: ") << utf8::cvt<std::wstring>(sender); 68 ss << _T(", template: ") << utf8::cvt<std::wstring>(template_string); 69 return ss.str(); 70 } 71 }; 72 73 struct clp_handler_impl : public client::clp_handler, client::target_lookup_interface { 42 74 43 75 SMTPClient *instance; 44 76 clp_handler_impl(SMTPClient *instance) : instance(instance) {} 45 connection_data local_data;46 77 47 78 int query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 48 79 int submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 49 80 int exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply); 81 82 virtual nscapi::functions::destination_container lookup_target(std::wstring &id) { 83 nscapi::functions::destination_container ret; 84 nscapi::target_handler::optarget t = instance->targets.find_target(id); 85 if (t) { 86 if (!t->alias.empty()) 87 ret.id = utf8::cvt<std::string>(t->alias); 88 if (!t->host.empty()) 89 ret.host = utf8::cvt<std::string>(t->host); 90 if (t->has_option("address")) 91 ret.address = utf8::cvt<std::string>(t->options[_T("address")]); 92 else 93 ret.address = utf8::cvt<std::string>(t->host); 94 BOOST_FOREACH(const nscapi::target_handler::target::options_type::value_type &kvp, t->options) { 95 ret.data[utf8::cvt<std::string>(kvp.first)] = utf8::cvt<std::string>(kvp.second); 96 } 97 } 98 return ret; 99 } 50 100 }; 101 51 102 52 103 public: … … 70 121 */ 71 122 static nscapi::plugin_wrapper::module_version getModuleVersion() { 72 nscapi::plugin_wrapper::module_version version = {0, 3, 0 };123 nscapi::plugin_wrapper::module_version version = {0, 4, 0 }; 73 124 return version; 74 125 } … … 76 127 return _T("Passive check support via SMTP"); 77 128 } 78 bool hasNotificationHandler() { return true; }79 129 130 bool hasCommandHandler() { return true; }; 131 bool hasMessageHandler() { return true; }; 132 bool hasNotificationHandler() { return true; }; 80 133 NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 81 NSCAPI::nagiosReturn handle Command(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf);82 int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result);134 NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 135 NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 83 136 137 private: 138 static connection_data parse_header(const ::Plugin::Common_Header &header); 139 140 private: 141 void add_local_options(po::options_description &desc, client::configuration::data_type data); 84 142 void setup(client::configuration &config); 85 void add_ local_options(boost::program_options::options_description &desc, connection_data &command_data);143 void add_command(std::wstring key, std::wstring args); 86 144 void add_target(std::wstring key, std::wstring args); 87 145 88 146 }; 147 -
modules/Scheduler/Scheduler.cpp
r9853bc3 rf33c12f 160 160 if (code == NSCAPI::returnIgnored) { 161 161 NSC_LOG_ERROR_STD(_T("Command was not found: ") + item.command.c_str()); 162 //make_submit_from_query(response, item.channel, item.alias); 162 163 nscapi::functions::create_simple_submit_request(item.channel, item.command, NSCAPI::returnUNKNOWN, _T("Command was not found: ") + item.command, _T(""), response); 163 164 std::string result; -
modules/SyslogClient/SyslogClient.cpp
r9853bc3 rf33c12f 222 222 } 223 223 224 NSCAPI::nagiosReturn SyslogClient::handle Command(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) {225 std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca"));226 224 NSCAPI::nagiosReturn SyslogClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 225 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 226 std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 227 227 client::configuration config; 228 228 setup(config); 229 if (cmd == _T("query")) 230 return client::command_line_parser::query(config, cmd, arguments, message, perf); 231 if (cmd == _T("submit")) { 232 boost::tuple<int,std::wstring> result = client::command_line_parser::simple_submit(config, cmd, arguments); 233 message = result.get<1>(); 234 return result.get<0>(); 235 } 236 if (cmd == _T("exec")) { 237 return client::command_line_parser::exec(config, cmd, arguments, message); 238 } 239 return commands.exec_simple(config, target, command, arguments, message, perf); 240 } 241 242 int SyslogClient::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { 243 std::wstring cmd = client::command_line_parser::parse_command(command, _T("nsca")); 229 if (!client::command_line_parser::is_command(cmd)) 230 return client::command_line_parser::do_execute_command_as_query(config, cmd, data.args, result); 231 return commands.exec_simple(config, data.target, char_command, data.args, result); 232 } 233 234 NSCAPI::nagiosReturn SyslogClient::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 235 nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 236 std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 244 237 if (!client::command_line_parser::is_command(cmd)) 245 238 return NSCAPI::returnIgnored; 246 247 239 client::configuration config; 248 240 setup(config); 249 return client::command_line_parser::commandLineExec(config, cmd, arguments, result); 250 } 251 252 NSCAPI::nagiosReturn SyslogClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &response) { 253 try { 254 client::configuration config; 255 setup(config); 256 257 if (!client::command_line_parser::relay_submit(config, request, response)) { 258 NSC_LOG_ERROR_STD(_T("Failed to submit message...")); 259 return NSCAPI::hasFailed; 260 } 261 return NSCAPI::isSuccess; 262 } catch (std::exception &e) { 263 NSC_LOG_ERROR_STD(_T("Failed to send data: ") + utf8::to_unicode(e.what())); 264 return NSCAPI::hasFailed; 265 } catch (...) { 266 NSC_LOG_ERROR_STD(_T("Failed to send data: UNKNOWN")); 267 return NSCAPI::hasFailed; 268 } 241 return client::command_line_parser::do_execute_command_as_exec(config, cmd, data.args, result); 242 } 243 244 NSCAPI::nagiosReturn SyslogClient::handleRAWNotification(const wchar_t* channel, std::string request, std::string &result) { 245 client::configuration config; 246 setup(config); 247 return client::command_line_parser::do_relay_submit(config, request, result); 269 248 } 270 249 … … 343 322 int SyslogClient::clp_handler_impl::query(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 344 323 NSC_LOG_ERROR_STD(_T("SYSLOG does not support query patterns")); 324 nscapi::functions::create_simple_query_response_unknown(_T("UNKNOWN"), _T("SYSLOG does not support query patterns"), reply); 345 325 return NSCAPI::hasFailed; 346 326 } 347 327 348 328 int SyslogClient::clp_handler_impl::submit(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 349 std::wstring channel; 350 try { 351 Plugin::SubmitRequestMessage message; 352 message.ParseFromString(request); 353 connection_data con = parse_header(*header); 354 channel = utf8::cvt<std::wstring>(message.channel()); 355 356 //TODO: Map seveity! 357 358 std::list<std::string> messages; 359 for (int i=0;i < message.payload_size(); ++i) { 360 const ::Plugin::QueryResponseMessage::Response& payload = message.payload(i); 361 std::string date = "Nov 10 00:12:00"; // TODO is this actually used? 362 std::string tag = con.tag_syntax; 363 std::string message = con.message_syntax; 364 strEx::replace(message, "%message%", payload.message()); 365 strEx::replace(tag, "%message%", payload.message()); 366 367 std::string severity = con.severity; 368 if (payload.result() == ::Plugin::Common_ResultCode_OK) 369 severity = con.ok_severity; 370 if (payload.result() == ::Plugin::Common_ResultCode_WARNING) 371 severity = con.warn_severity; 372 if (payload.result() == ::Plugin::Common_ResultCode_CRITCAL) 373 severity = con.crit_severity; 374 if (payload.result() == ::Plugin::Common_ResultCode_UNKNOWN) 375 severity = con.unknown_severity; 376 377 messages.push_back(instance->parse_priority(severity, con.facility) + date + " " + tag + " " + message); 378 } 379 boost::tuple<int,std::wstring> ret = instance->send(con, messages); 380 nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), ret.get<0>(), _T("Message submitted successfully: ") + ret.get<1>(), reply); 381 return NSCAPI::isSuccess; 382 } catch (std::exception &e) { 383 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::to_unicode(e.what())); 384 nscapi::functions::create_simple_submit_response(channel, _T("UNKNOWN"), NSCAPI::returnUNKNOWN, utf8::to_unicode(e.what()), reply); 385 return NSCAPI::hasFailed; 386 } 329 Plugin::SubmitRequestMessage request_message; 330 request_message.ParseFromString(request); 331 connection_data con = parse_header(*header); 332 333 Plugin::SubmitResponseMessage response_message; 334 nscapi::functions::make_return_header(response_message.mutable_header(), *header); 335 336 //TODO: Map seveity! 337 338 std::list<std::string> messages; 339 for (int i=0;i < request_message.payload_size(); ++i) { 340 const ::Plugin::QueryResponseMessage::Response& payload = request_message.payload(i); 341 std::string date = "Nov 10 00:12:00"; // TODO is this actually used? 342 std::string tag = con.tag_syntax; 343 std::string message = con.message_syntax; 344 strEx::replace(message, "%message%", payload.message()); 345 strEx::replace(tag, "%message%", payload.message()); 346 347 std::string severity = con.severity; 348 if (payload.result() == ::Plugin::Common_ResultCode_OK) 349 severity = con.ok_severity; 350 if (payload.result() == ::Plugin::Common_ResultCode_WARNING) 351 severity = con.warn_severity; 352 if (payload.result() == ::Plugin::Common_ResultCode_CRITCAL) 353 severity = con.crit_severity; 354 if (payload.result() == ::Plugin::Common_ResultCode_UNKNOWN) 355 severity = con.unknown_severity; 356 357 messages.push_back(instance->parse_priority(severity, con.facility) + date + " " + tag + " " + message); 358 } 359 boost::tuple<int,std::wstring> ret = instance->send(con, messages); 360 nscapi::functions::append_simple_submit_response_payload(response_message.add_payload(), "UNKNOWN", ret.get<0>(), utf8::cvt<std::string>(ret.get<1>())); 361 response_message.SerializeToString(&reply); 362 return NSCAPI::isSuccess; 387 363 } 388 364 389 365 int SyslogClient::clp_handler_impl::exec(client::configuration::data_type data, ::Plugin::Common_Header* header, const std::string &request, std::string &reply) { 390 366 NSC_LOG_ERROR_STD(_T("SYSLOG does not support exec patterns")); 367 nscapi::functions::create_simple_exec_response_unknown("UNKNOWN", "SYSLOG does not support exec patterns", reply); 391 368 return NSCAPI::hasFailed; 392 369 } -
modules/SyslogClient/SyslogClient.h
r96c1461 rf33c12f 32 32 namespace po = boost::program_options; 33 33 34 class SyslogClient : public nscapi::impl::simple_ command_handler, public nscapi::impl::simple_plugin, public nscapi::impl::simple_command_line_exec{34 class SyslogClient : public nscapi::impl::simple_plugin { 35 35 private: 36 36 … … 144 144 bool hasNotificationHandler() { return true; }; 145 145 NSCAPI::nagiosReturn handleRAWNotification(const wchar_t* channel, std::string request, std::string &response); 146 NSCAPI::nagiosReturn handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf); 147 int commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result); 146 NSCAPI::nagiosReturn handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &response); 147 NSCAPI::nagiosReturn commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response); 148 148 149 149 150 private:
Note: See TracChangeset
for help on using the changeset viewer.








