void CheckMKClient::send(connection_data con) { try { NSC_DEBUG_MSG_STD("Connection details: " + con.to_string()); if (con.ssl.enabled) { #ifndef USE_SSL NSC_LOG_ERROR_STD(_T("SSL not avalible (compiled without USE_SSL)")); return response; #endif } socket_helpers::client::client<check_mk::client::protocol> client(con, boost::shared_ptr<client_handler>(new client_handler())); client.connect(); std::string dummy; check_mk::packet packet = client.process_request(dummy); boost::optional<scripts::command_definition<lua::lua_traits> > cmd = scripts_->find_command("check_mk", "c_callback"); if (cmd) { parse_data(cmd->information, cmd->function, packet); } else { NSC_LOG_ERROR_STD("No check_mk callback found!"); } //lua_runtime_->on_query() client.shutdown(); } catch (std::runtime_error &e) { NSC_LOG_ERROR_EXR("Failed to send", e); } catch (std::exception &e) { NSC_LOG_ERROR_EXR("Failed to send", e); } catch (...) { NSC_LOG_ERROR_EX("Failed to send"); } }
boost::tuple<int,std::wstring> SyslogClient::send(connection_data con, std::list<std::string> messages) { try { NSC_DEBUG_MSG_STD(_T("NRPE Connection details: ") + con.to_wstring()); boost::asio::io_service io_service; ip::udp::resolver resolver(io_service); ip::udp::resolver::query query(ip::udp::v4(), con.host, strEx::s::itos(con.port)); ip::udp::endpoint receiver_endpoint = *resolver.resolve(query); ip::udp::socket socket(io_service); socket.open(ip::udp::v4()); BOOST_FOREACH(const std::string msg, messages) { NSC_DEBUG_MSG_STD(_T("Sending data: ") + utf8::cvt<std::wstring>(msg)); socket.send_to(boost::asio::buffer(msg), receiver_endpoint); } return boost::make_tuple(NSCAPI::returnOK, _T("OK")); } catch (std::runtime_error &e) {
bool CheckNSCP::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode) { start_ = boost::posix_time::microsec_clock::local_time(); std::string path; sh::settings_registry settings(get_settings_proxy()); crashFolder = get_core()->expand_path(CRASH_ARCHIVE_FOLDER); NSC_DEBUG_MSG_STD("Crash folder is: " + crashFolder.string()); return true; }
boost::optional<boost::filesystem::wpath> LUAScript::find_file(std::wstring file) { std::list<boost::filesystem::wpath> checks; checks.push_back(file); checks.push_back(root_ / _T("scripts") / _T("lua") / file); checks.push_back(root_ / _T("scripts") / file); checks.push_back(root_ / _T("lua") / file); checks.push_back(root_ / file); BOOST_FOREACH(boost::filesystem::wpath c, checks) { NSC_DEBUG_MSG_STD(_T("Looking for: ") + c.string()); if (boost::filesystem::exists(c)) return boost::optional<boost::filesystem::wpath>(c); }
boost::tuple<int,std::string> NRDPClient::clp_handler_impl::send(connection_data data, const nrdp::data &nrdp_data) { try { NSC_DEBUG_MSG_STD("Connection details: " + data.to_string()); http::client c; http::client::request_type request; request.add_default_headers(); http::client::request_type::post_map_type post; post["token"] = data.token; post["XMLDATA"] = nrdp_data.render_request(); post["cmd"] = "submitcheck"; request.add_post_payload(post); NSC_DEBUG_MSG_STD(nrdp_data.render_request()); NSC_DEBUG_MSG_STD(request.payload); http::client::response_type response = c.execute(data.host, data.port, "/nrdp/server/", request); NSC_DEBUG_MSG_STD(response.payload); return boost::make_tuple(NSCAPI::returnUNKNOWN, ""); } catch (const std::exception &e) { return boost::make_tuple(NSCAPI::returnUNKNOWN, "Error: " + utf8::utf8_from_native(e.what())); } catch (...) { return boost::make_tuple(NSCAPI::returnUNKNOWN, "Unknown error -- REPORT THIS!"); } }
bool NSCPServer::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode mode) { sh::settings_registry settings(get_settings_proxy()); settings.set_alias("nscp", alias, "server"); settings.alias().add_path_to_settings() ("NSCP SERVER SECTION", "Section for NSCP (NSCPListener.dll) (check_nscp) protocol options.") ; settings.alias().add_key_to_settings() ("port", sh::string_key(&info_.port_, "5668"), "PORT NUMBER", "Port to use for NSCP.") ("allow arguments", sh::bool_fun_key<bool>(boost::bind(&handler_impl::set_allow_arguments, handler_, _1), false), "COMMAND ARGUMENT PROCESSING", "This option determines whether or not the we will allow clients to specify arguments to commands that are executed.") ; socket_helpers::settings_helper::add_core_server_opts(settings, info_); socket_helpers::settings_helper::add_ssl_server_opts(settings, info_, true); settings.register_all(); settings.notify(); #ifndef USE_SSL if (info_.use_ssl) { NSC_LOG_ERROR_STD(_T("SSL not avalible! (not compiled with openssl support)")); return false; } #endif boost::asio::io_service io_service_; if (mode == NSCAPI::normalStart) { NSC_LOG_ERROR_LISTS(info_.validate()); std::list<std::string> errors; info_.allowed_hosts.refresh(errors); NSC_LOG_ERROR_LISTS(errors); NSC_DEBUG_MSG_STD("Allowed hosts definition: " + info_.allowed_hosts.to_string()); server_.reset(new nscp::server::server(info_, handler_)); if (!server_) { NSC_LOG_ERROR_STD("Failed to create server instance!"); return false; } server_->start(); } return true; }
void CheckExternalScripts::add_command(std::string key, std::string arg) { try { if (!provider_) { NSC_LOG_ERROR("Failed to add (no provider): " + key); return; } provider_->add_command(key, arg); if (arg.find("$ARG") != std::string::npos) { if (!allowArgs_) { NSC_DEBUG_MSG_STD("Detected a $ARG??$ expression with allowed arguments flag set to false (perhaps this is not the intent)"); } } if (arg.find("%ARG") != std::string::npos) { if (!allowArgs_) { NSC_DEBUG_MSG_STD("Detected a %ARG??% expression with allowed arguments flag set to false (perhaps this is not the intent)"); } } } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("Failed to add: " + key, e); } catch (...) { NSC_LOG_ERROR_EX("Failed to add: " + key); } }
void CheckExternalScripts::add_command(std::string key, std::string arg) { try { commands_.add(get_settings_proxy(), commands_path, key, arg, key == "default"); if (arg.find("$ARG") != std::string::npos) { if (!allowArgs_) { NSC_DEBUG_MSG_STD("Detected a $ARG??$ expression with allowed arguments flag set to false (perhaps this is not the intent)"); } } } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("Failed to add: " + key, e); } catch (...) { NSC_LOG_ERROR_EX("Failed to add: " + key); } }
void NSClientSocket::onAccept(simpleSocket::Socket client) { if (!inAllowedHosts(client.getAddrString())) { NSC_LOG_ERROR("Unothorized access from: " + client.getAddrString()); client.close(); return; } simpleSocket::DataBuffer db; client.readAll(db); if (db.getLength() > 0) { std::string incoming(db.getBuffer(), db.getLength()); NSC_DEBUG_MSG_STD("Incoming data: " + incoming); std::string response = parseRequest(incoming); NSC_DEBUG_MSG("Outgoing data: " + response); client.send(response.c_str(), static_cast<int>(response.length()), 0); } client.close(); }
bool LUAScript::loadScript(std::string alias, std::string file) { try { if (file.empty()) { file = alias; alias = ""; } boost::optional<boost::filesystem::path> ofile = lua::lua_script::find_script(root_, file); if (!ofile) return false; NSC_DEBUG_MSG_STD("Adding script: " + ofile->string()); scripts_->add(alias, ofile->string()); return true; } catch (...) { NSC_LOG_ERROR_EX("load script"); } return false; }
std::list<nscp::packet> DistributedClient::send_nossl(std::string host, int timeout, const std::list<nscp::packet> &chunks) { zmq::context_t context(1); zeromq::zeromq_client_handshake_reader handshaker("0987654321", nscp::factory::create_message_envelope_request(0)); zeromq::zeromq_client client(zeromq::zeromq_client::connection_info(host, timeout), &handshaker, &context); client.connect(); std::list<nscp::packet> responses; NSC_DEBUG_MSG_STD(_T("Sending data: ") + to_wstring(chunks.size())); BOOST_FOREACH(const nscp::packet packet, chunks) { zeromq::zeromq_client_message_reader reader(&handshaker, packet); if (client.send(&reader)) { NSC_DEBUG_MSG(_T("Got response: ") + to_wstring(reader.response.signature.payload_type)); responses.push_back(reader.response); } else { NSC_LOG_ERROR(_T("Failed to read response!")); responses.push_back(nscp::factory::create_error(_T("Failed to send data to server."))); } }
check_nt::packet handler_impl::handle(check_nt::packet p) { std::wstring buffer = p.get_payload(); NSC_DEBUG_MSG_STD(_T("Data: ") + buffer); std::wstring::size_type pos = buffer.find_first_of(_T("\n\r")); if (pos != std::wstring::npos) { std::wstring::size_type pos2 = buffer.find_first_not_of(_T("\n\r"), pos); if (pos2 != std::wstring::npos) { std::wstring rest = buffer.substr(pos2); NSC_DEBUG_MSG_STD(_T("Ignoring data: ") + rest); } buffer = buffer.substr(0, pos); } strEx::token pwd = strEx::getToken(buffer, '&'); if (!isPasswordOk(pwd.first)) { NSC_LOG_ERROR_STD(_T("Invalid password (") + pwd.first + _T(").")); return check_nt::packet("ERROR: Invalid password."); } if (pwd.second.empty()) return check_nt::packet("ERROR: No command specified."); strEx::token cmd = strEx::getToken(pwd.second, '&'); if (cmd.first.empty()) return check_nt::packet("ERROR: No command specified."); int c = boost::lexical_cast<int>(cmd.first.c_str()); NSC_DEBUG_MSG_STD(_T("Data: ") + cmd.second); std::list<std::wstring> args; // prefix various commands switch (c) { case REQ_CPULOAD: cmd.first = _T("checkCPU"); split_to_list(args, cmd.second); args.push_back(_T("nsclient")); break; case REQ_UPTIME: cmd.first = _T("checkUpTime"); args.push_back(_T("nsclient")); break; case REQ_USEDDISKSPACE: cmd.first = _T("CheckDriveSize"); split_to_list(args, cmd.second); args.push_back(_T("nsclient")); break; case REQ_CLIENTVERSION: { //std::wstring v = SETTINGS_GET_STRING(nsclient::VERSION); //if (v == _T("auto")) std::wstring v = nscapi::plugin_singleton->get_core()->getApplicationName() + _T(" ") + nscapi::plugin_singleton->get_core()->getApplicationVersionString(); return strEx::wstring_to_string(v); } case REQ_SERVICESTATE: cmd.first = _T("checkServiceState"); split_to_list(args, cmd.second); args.push_back(_T("nsclient")); break; case REQ_PROCSTATE: cmd.first = _T("checkProcState"); split_to_list(args, cmd.second); args.push_back(_T("nsclient")); break; case REQ_MEMUSE: cmd.first = _T("checkMem"); args.push_back(_T("nsclient")); break; case REQ_COUNTER: cmd.first = _T("checkCounter"); args.push_back(_T("Counter=") + cmd.second); args.push_back(_T("nsclient")); break; case REQ_FILEAGE: cmd.first = _T("getFileAge"); args.push_back(_T("path=") + cmd.second); break; case REQ_INSTANCES: cmd.first = _T("listCounterInstances"); args.push_back(cmd.second); break; default: split_to_list(args, cmd.second); } std::wstring message, perf; NSCAPI::nagiosReturn ret = nscapi::core_helper::simple_query(cmd.first.c_str(), args, message, perf); if (!nscapi::plugin_helper::isNagiosReturnCode(ret)) { if (message.empty()) return check_nt::packet("ERROR: Could not complete the request check log file for more information."); return check_nt::packet("ERROR: " + strEx::wstring_to_string(message)); } switch (c) { case REQ_UPTIME: // Some check_nt commands has no return code syntax case REQ_MEMUSE: case REQ_CPULOAD: case REQ_CLIENTVERSION: case REQ_USEDDISKSPACE: case REQ_COUNTER: case REQ_FILEAGE: return check_nt::packet(message); case REQ_SERVICESTATE: // Some check_nt commands return the return code (coded as a string) case REQ_PROCSTATE: return check_nt::packet(strEx::itos(nscapi::plugin_helper::nagios2int(ret)) + _T("& ") + message); default: // "New" check_nscp also returns performance data if (perf.empty()) return check_nt::packet(nscapi::plugin_helper::translateReturn(ret) + _T("&") + message); return check_nt::packet(nscapi::plugin_helper::translateReturn(ret) + _T("&") + message + _T("&") + perf); } return check_nt::packet("FOO"); }
bool NSCAServer::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode mode) { try { if (server_) { server_->stop(); server_.reset(); } } catch (...) { NSC_LOG_ERROR_STD("Failed to stop server"); return false; } sh::settings_registry settings(get_settings_proxy()); settings.set_alias("NSCA", alias, "server"); settings.alias().add_path_to_settings() ("NSCA SERVER SECTION", "Section for NSCA (NSCAServer) (check_nsca) protocol options.") ; settings.alias().add_key_to_settings() ("port", sh::string_key(&info_.port_, "5667"), "PORT NUMBER", "Port to use for NSCA.") ("payload length", sh::uint_key(&payload_length_, 512), "PAYLOAD LENGTH", "Length of payload to/from the NSCA agent. This is a hard specific value so you have to \"configure\" (read recompile) your NSCA agent to use the same value for it to work.") ("performance data", sh::bool_fun_key<bool>(boost::bind(&NSCAServer::set_perf_data, this, _1), true), "PERFORMANCE DATA", "Send performance data back to nagios (set this to false to remove all performance data).") ("encryption", sh::string_fun_key<std::string>(boost::bind(&NSCAServer::set_encryption, this, _1), "aes"), "ENCRYPTION", std::string("Name of encryption algorithm to use.\nHas to be the same as your agent i using or it wont work at all." "This is also independent of SSL and generally used instead of SSL.\nAvailable encryption algorithms are:\n") + nscp::encryption::helpers::get_crypto_string("\n")) ; socket_helpers::settings_helper::add_core_server_opts(settings, info_); socket_helpers::settings_helper::add_ssl_server_opts(settings, info_, false); settings.alias().add_parent("/settings/default").add_key_to_settings() ("password", sh::string_key(&password_, ""), "PASSWORD", "Password to use") ("inbox", sh::string_key(&channel_, "inbox"), "INBOX", "The default channel to post incoming messages on") ; settings.register_all(); settings.notify(); #ifndef USE_SSL if (info_.ssl.enabled) { NSC_LOG_ERROR_STD(_T("SSL not avalible! (not compiled with openssl support)")); return false; } #endif if (payload_length_ != 512) NSC_DEBUG_MSG_STD("Non-standard buffer length (hope you have recompiled check_nsca changing #define MAX_PACKETBUFFER_LENGTH = " + strEx::s::xtos(payload_length_)); NSC_LOG_ERROR_LISTS(info_.validate()); std::list<std::string> errors; info_.allowed_hosts.refresh(errors); NSC_LOG_ERROR_LISTS(errors); NSC_DEBUG_MSG_STD("Allowed hosts definition: " + info_.allowed_hosts.to_string()); if (mode == NSCAPI::normalStart || mode == NSCAPI::reloadStart) { server_.reset(new nsca::server::server(info_, this)); if (!server_) { NSC_LOG_ERROR_STD("Failed to create server instance!"); return false; } server_->start(); } return true; }
void error_handler_impl::log_error(const std::string error_) { NSC_DEBUG_MSG_STD(error_); error = error_; }
void error_handler_impl::log_debug(const std::string error) { NSC_DEBUG_MSG_STD(error); }
bool CheckMKServer::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode mode) { root_ = get_base_path(); nscp_runtime_.reset(new scripts::nscp::nscp_runtime_impl(get_id(), get_core())); lua_runtime_.reset(new lua::lua_runtime(utf8::cvt<std::string>(root_.string()))); lua_runtime_->register_plugin(boost::shared_ptr<check_mk::check_mk_plugin>(new check_mk::check_mk_plugin())); scripts_.reset(new scripts::script_manager<lua::lua_traits>(lua_runtime_, nscp_runtime_, get_id(), utf8::cvt<std::string>(alias))); handler_.reset(new handler_impl(scripts_)); sh::settings_registry settings(get_settings_proxy()); settings.set_alias("check_mk", alias, "server"); settings.alias().add_path_to_settings() ("CHECK MK SERVER SECTION", "Section for check_mk (CheckMKServer.dll) protocol options.") ("scripts", sh::fun_values_path(boost::bind(&CheckMKServer::add_script, this, _1, _2)), "REMOTE TARGET DEFINITIONS", "", "TARGET", "For more configuration options add a dedicated section") ; settings.alias().add_key_to_settings() ("port", sh::string_key(&info_.port_, "6556"), "PORT NUMBER", "Port to use for check_mk.") ; socket_helpers::settings_helper::add_core_server_opts(settings, info_); socket_helpers::settings_helper::add_ssl_server_opts(settings, info_, false); settings.register_all(); settings.notify(); if (scripts_->empty()) { add_script("default", "default_check_mk.lua"); } #ifndef USE_SSL if (info_.use_ssl) { NSC_LOG_ERROR_STD(_T("SSL not avalible! (not compiled with openssl support)")); return false; } #endif NSC_LOG_ERROR_LISTS(info_.validate()); std::list<std::string> errors; info_.allowed_hosts.refresh(errors); NSC_LOG_ERROR_LISTS(errors); NSC_DEBUG_MSG_STD("Allowed hosts definition: " + info_.allowed_hosts.to_string()); boost::asio::io_service io_service_; scripts_->load_all(); if (mode == NSCAPI::normalStart) { server_.reset(new check_mk::server::server(info_, handler_)); if (!server_) { NSC_LOG_ERROR_STD("Failed to create server instance!"); return false; } server_->start(); } return true; }