nscp::packet NSCPClient::clp_handler_impl::send(connection_data con, nscp::packet &packet) { nscp::packet response; try { if (con.ssl.enabled) { #ifndef USE_SSL NSC_LOG_ERROR_STD("SSL not avalible (compiled without USE_SSL)"); return response; #endif } socket_helpers::client::client<nscp::client::protocol> client(con, boost::shared_ptr<client_handler>(new client_handler())); client.connect(); response = client.process_request(packet); client.shutdown(); return response; } catch (std::runtime_error &e) { NSC_LOG_ERROR_EXR("Failed to send", e); return response; } catch (std::exception &e) { NSC_LOG_ERROR_EXR("Failed to send", e); return response; } catch (...) { NSC_LOG_ERROR_EX("Failed to send"); return response; } }
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"); } }
void Scheduler::add_schedule(std::string key, std::string arg) { try { schedules_.add(get_settings_proxy(), key, arg, key == "default"); } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("Failed to add target: " + key, e); } catch (...) { NSC_LOG_ERROR_EX("Failed to add target: " + key); } }
void check::add_counter(boost::shared_ptr<nscapi::settings_proxy> proxy, std::string path, std::string key, std::string query) { try { counters_.add(proxy, path, key, query, key == "default"); } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("Failed to add counter: " + key, e); } catch (...) { NSC_LOG_ERROR_EX("Failed to add counter: " + key); } }
void CheckExternalScripts::add_alias(std::string key, std::string arg) { try { aliases_.add(get_settings_proxy(), key, arg); } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("Failed to add: " + key, e); } catch (...) { NSC_LOG_ERROR_EX("Failed to add: " + key); } }
void SyslogClient::add_target(std::string key, std::string arg) { try { client_.add_target(get_settings_proxy(), key, arg); } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("Failed to add target: " + key, e); } catch (...) { NSC_LOG_ERROR_EX("Failed to add target: " + key); } }
void CheckMKClient::add_target(std::string key, std::string arg) { try { targets.add(get_settings_proxy(), target_path , key, arg); } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("Failed to add target: " + key, e); } catch (...) { NSC_LOG_ERROR_EX("Failed to add target: " + key); } }
NSCAPI::boolReturn NSHasNotificationHandler() { try { if (instance->hasNotificationHandler()) return NSCAPI::istrue; } catch (...) { NSC_LOG_ERROR_EX("NSHasNotificationHandler"); } return NSCAPI::isfalse; }
NSCAPI::nagiosReturn NSRouteMessage(const wchar_t* channel, const wchar_t* command, const char* request_buffer, const unsigned int request_buffer_len, char** reply_buffer, unsigned int *reply_buffer_len) { try { std::string request(request_buffer, request_buffer_len), reply; NSCAPI::nagiosReturn retCode = instance->RAWRouteMessage(channel, command, request, reply); helpers::wrap_string(reply, reply_buffer, reply_buffer_len); return retCode; } catch (...) { NSC_LOG_ERROR_EX("NSRouteMessage"); } return NSCAPI::returnIgnored; }
bool Scheduler::handle_schedule(schedules::target_object item) { try { std::string response; nscapi::core_helper ch(get_core(), get_id()); if (!ch.simple_query(item->command.c_str(), item->arguments, response)) { NSC_LOG_ERROR("Failed to execute: " + item->command); if (item->channel.empty()) { NSC_LOG_ERROR_WA("No channel specified for ", item->get_alias()); return true; } nscapi::protobuf::functions::create_simple_submit_request(item->channel, item->command, NSCAPI::query_return_codes::returnUNKNOWN, "Command was not found: " + item->command, "", response); std::string result; get_core()->submit_message(item->channel, response, result); return true; } Plugin::QueryResponseMessage resp_msg; resp_msg.ParseFromString(response); Plugin::QueryResponseMessage resp_msg_send; resp_msg_send.mutable_header()->CopyFrom(resp_msg.header()); BOOST_FOREACH(const Plugin::QueryResponseMessage::Response &p, resp_msg.payload()) { if (nscapi::report::matches(item->report, nscapi::protobuf::functions::gbp_to_nagios_status(p.result()))) resp_msg_send.add_payload()->CopyFrom(p); } if (resp_msg_send.payload_size() > 0) { if (item->channel.empty()) { NSC_LOG_ERROR_STD("No channel specified for " + item->get_alias() + " mssage will not be sent."); return true; } nscapi::protobuf::functions::make_submit_from_query(response, item->channel, item->get_alias(), item->target_id, item->source_id); std::string result; if (!get_core()->submit_message(item->channel, response, result)) { NSC_LOG_ERROR_STD("Failed to submit: " + item->get_alias()); return true; } std::string error; if (!nscapi::protobuf::functions::parse_simple_submit_response(result, error)) { NSC_LOG_ERROR_STD("Failed to submit " + item->get_alias() + ": " + error); return true; } } else { NSC_DEBUG_MSG("Filter not matched for: " + item->get_alias() + " so nothing is reported"); } return true; } catch (nscapi::nscapi_exception &e) { NSC_LOG_ERROR_EXR("Failed to register command: ", e); return false; } catch (std::exception &e) { NSC_LOG_ERROR_EXR("Exception: ", e); return false; } catch (...) { NSC_LOG_ERROR_EX(item->get_alias()); return false; } }
bool NSCPServer::unloadModule() { try { if (server_) { server_->stop(); server_.reset(); } } catch (...) { NSC_LOG_ERROR_EX("unload"); return false; } return true; }
void SyslogClient::add_command(std::string key, std::string arg) { try { nscapi::core_helper core(get_core(), get_id()); std::string k = client_.add_command(key, arg); if (!k.empty()) core.register_command(k.c_str(), "Syslog relay for: " + key); } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("Failed to add command: " + key, e); } catch (...) { NSC_LOG_ERROR_EX("Failed to add command: " + key); } }
void NRDPClient::add_command(std::string name, std::string args) { nscapi::core_helper core(get_core(), get_id()); try { std::string key = commands.add_command(name, args); if (!key.empty()) core.register_command(key, "NRPE relay for: " + name); } catch (boost::program_options::validation_error &e) { NSC_LOG_ERROR_EXR("Failed to add command: " + name, e); } catch (...) { NSC_LOG_ERROR_EX("Failed to add target: " + name); } }
void CheckMKClient::add_command(std::string name, std::string args) { try { nscapi::core_helper core(get_core(), get_id()); std::string key = commands.add_command(name, args); if (!key.empty()) core.register_command(key.c_str(), "check_mk relay for: " + name); } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("Failed to add command: " + name, e); } catch (...) { NSC_LOG_ERROR_EX("Failed to add command: " + name); } }
int NSCommandLineExec(char *request_buffer, unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len) { try { std::string request(request_buffer, request_buffer_len), reply; NSCAPI::nagiosReturn retCode = instance->commandRAWLineExec(request, reply); helpers::wrap_string(reply, response_buffer, response_buffer_len); return retCode; } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("NSCommandLineExec", e); } catch (...) { NSC_LOG_ERROR_EX("NSCommandLineExec"); } return NSCAPI::hasFailed; }
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); } }
NSCAPI::nagiosReturn NSHandleNotification(const char* channel, const char* buffer, unsigned int buffer_len, char** response_buffer, unsigned int *response_buffer_len) { try { std::string request(buffer, buffer_len), reply; NSCAPI::nagiosReturn retCode = instance->handleRAWNotification(channel, request, reply); helpers::wrap_string(reply, response_buffer, response_buffer_len); //return helpers::wrap_string(reply, response_buffer, response_buffer_len, retCode); return retCode; } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("NSHandleNotification", e); } catch (...) { NSC_LOG_ERROR_EX("NSHandleNotification"); } return NSCAPI::returnIgnored; }
bool CheckMKServer::unloadModule() { try { if (server_) { server_->stop(); server_.reset(); } scripts_.reset(); lua_runtime_.reset(); nscp_runtime_.reset(); handler_.reset(); } catch (...) { NSC_LOG_ERROR_EX("unload"); return false; } return true; }
bool NSCPClient::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode ) { std::map<std::wstring,std::wstring> commands; try { sh::settings_registry settings(get_settings_proxy()); settings.set_alias("nscp", alias, "client"); target_path = settings.alias().get_settings_path("targets"); settings.alias().add_path_to_settings() ("NSCP CLIENT SECTION", "Section for NSCP active/passive check module.") ("handlers", sh::fun_values_path(boost::bind(&NSCPClient::add_command, this, _1, _2)), "CLIENT HANDLER SECTION", "", "CLIENT HANDLER", "For more configuration options add a dedicated section") ("targets", sh::fun_values_path(boost::bind(&NSCPClient::add_target, this, _1, _2)), "REMOTE TARGET DEFINITIONS", "", "TARGET", "For more configuration options add a dedicated section") ; settings.alias().add_key_to_settings() ("channel", sh::string_key(&channel_, "NSCP"), "CHANNEL", "The channel to listen to.") ; settings.register_all(); settings.notify(); targets.add_samples(get_settings_proxy(), target_path); targets.add_missing(get_settings_proxy(), target_path, "default", "", true); nscapi::core_helper::core_proxy core(get_core(), get_id()); core.register_channel(channel_); } catch (nscapi::nscapi_exception &e) { NSC_LOG_ERROR_EXR("load", e); return false; } catch (std::exception &e) { NSC_LOG_ERROR_EXR("load", e); return false; } catch (...) { NSC_LOG_ERROR_EX("load"); return false; } return true; }
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; }
NSCAPI::nagiosReturn NSHandleCommand(const char* request_buffer, const unsigned int request_buffer_len, char** reply_buffer, unsigned int *reply_buffer_len) { try { std::string request(request_buffer, request_buffer_len), reply; NSCAPI::nagiosReturn retCode = instance->handleRAWCommand(request, reply); helpers::wrap_string(reply, reply_buffer, reply_buffer_len); if (!nscapi::plugin_helper::isMyNagiosReturn(retCode)) { NSC_LOG_ERROR("A module returned an invalid return code"); } return retCode; } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("NSHandleCommand", e); return NSCAPI::returnUNKNOWN; } catch (...) { NSC_LOG_ERROR_EX("NSHandleCommand"); return NSCAPI::returnUNKNOWN; } return NSCAPI::returnIgnored; }
bool SMTPClient::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode) { std::wstring template_string, sender, recipient; try { sh::settings_registry settings(get_settings_proxy()); settings.set_alias("SMTP", alias, "client"); client_.set_path(settings.alias().get_settings_path("targets")); settings.alias().add_path_to_settings() ("SMTP CLIENT SECTION", "Section for SMTP passive check module.") ("handlers", sh::fun_values_path(boost::bind(&SMTPClient::add_command, this, _1, _2)), "CLIENT HANDLER SECTION", "", "CLIENT HANDLER", "For more configuration options add a dedicated section") ("targets", sh::fun_values_path(boost::bind(&SMTPClient::add_target, this, _1, _2)), "REMOTE TARGET DEFINITIONS", "", "TARGET", "For more configuration options add a dedicated section") ; settings.alias().add_key_to_settings() ("channel", sh::string_key(&channel_, "SMTP"), "CHANNEL", "The channel to listen to.") ; settings.register_all(); settings.notify(); client_.finalize(get_settings_proxy()); nscapi::core_helper core(get_core(), get_id()); core.register_channel(channel_); } catch (const nsclient::nsclient_exception &e) { NSC_LOG_ERROR_EXR("load", e); return false; } catch (std::exception &e) { NSC_LOG_ERROR_EXR("NSClient API exception: ", e); return false; } catch (...) { NSC_LOG_ERROR_EX("NSClient API exception: "); return false; } return true; }
void target_helper::add_target(nscapi::settings_helper::settings_impl_interface_ptr core, std::string key, std::string val) { std::string alias = key; target_info target; try { sh::settings_registry settings(core); target.hostname = val; if (val.empty()) target.hostname = alias; settings.add_path_to_settings() (target.hostname, "TARGET LIST SECTION", "A list of available remote target systems") ; settings.add_key_to_settings("targets/" + target.hostname) ("hostname", sh::string_key(&target.hostname), "TARGET HOSTNAME", "Hostname or ip address of target") ("username", sh::string_key(&target.username), "TARGET USERNAME", "Username used to authenticate with") ("password", sh::string_key(&target.password), "TARGET PASSWORD", "Password used to authenticate with") ("protocol", sh::string_key(&target.protocol), "TARGET PROTOCOL", "Protocol identifier used to route requests") ; settings.register_all(); settings.notify(); } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("loading: ", e); } catch (...) { NSC_LOG_ERROR_EX("loading: "); } targets[alias] = target; }
bool NRPEClient::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode mode) { try { sh::settings_registry settings(get_settings_proxy()); settings.set_alias("NRPE", alias, "client"); target_path = settings.alias().get_settings_path("targets"); settings.alias().add_path_to_settings() ("NRPE CLIENT SECTION", "Section for NRPE active/passive check module.") ("handlers", sh::fun_values_path(boost::bind(&NRPEClient::add_command, this, _1, _2)), "CLIENT HANDLER SECTION", "") ("targets", sh::fun_values_path(boost::bind(&NRPEClient::add_target, this, _1, _2)), "REMOTE TARGET DEFINITIONS", "") ; settings.alias().add_key_to_settings() ("channel", sh::string_key(&channel_, "NRPE"), "CHANNEL", "The channel to listen to.") ; settings.register_all(); settings.notify(); nscapi::core_helper::core_proxy core(get_core(), get_id()); targets.add_samples(get_settings_proxy(), target_path); targets.ensure_default(get_settings_proxy(), target_path); core.register_channel(channel_); } catch (std::exception &e) { NSC_LOG_ERROR_EXR("loading", e); return false; } catch (...) { NSC_LOG_ERROR_EX("loading"); return false; } 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); } }
bool LUAScript::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode) { try { 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()))); scripts_.reset(new scripts::script_manager<lua::lua_traits>(lua_runtime_, nscp_runtime_, get_id(), utf8::cvt<std::string>(alias))); sh::settings_registry settings(get_settings_proxy()); settings.set_alias(alias, "lua"); settings.alias().add_path_to_settings() ("LUA SCRIPT SECTION", "Section for the LUAScripts module.") ("scripts", sh::fun_values_path(boost::bind(&LUAScript::loadScript, this, _1, _2)), "LUA SCRIPTS SECTION", "A list of scripts available to run from the LuaSCript module.", "SCRIPT DEFENTION", "For more configuration options add a dedicated section") ; settings.register_all(); settings.notify(); // if (!scriptDirectory_.empty()) { // addAllScriptsFrom(scriptDirectory_); // } scripts_->load_all(); } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("load", e); return false; } catch (...) { NSC_LOG_ERROR_EX("load"); return false; } return true; }
bool CheckMKClient::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode) { std::map<std::wstring,std::wstring> commands; try { 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))); sh::settings_registry settings(get_settings_proxy()); settings.set_alias("check_mk", alias, "client"); target_path = settings.alias().get_settings_path("targets"); settings.alias().add_path_to_settings() ("CHECK MK CLIENT SECTION", "Section for NSCP active/passive check module.") ("handlers", sh::fun_values_path(boost::bind(&CheckMKClient::add_command, this, _1, _2)), "CLIENT HANDLER SECTION", "", "CLIENT", "For more configuration options add a dedicated section") ("targets", sh::fun_values_path(boost::bind(&CheckMKClient::add_target, this, _1, _2)), "REMOTE TARGET DEFINITIONS", "", "TARGET", "For more configuration options add a dedicated section") ("scripts", sh::fun_values_path(boost::bind(&CheckMKClient::add_script, this, _1, _2)), "REMOTE TARGET DEFINITIONS", "", "SCRIPT", "For more configuration options add a dedicated section") ; settings.alias().add_key_to_settings() ("channel", sh::string_key(&channel_, "CheckMK"), "CHANNEL", "The channel to listen to.") ; settings.register_all(); settings.notify(); targets.add_samples(get_settings_proxy(), target_path); targets.add_missing(get_settings_proxy(), target_path, "default", "", true); if (scripts_->empty()) { add_script("default", "default_check_mk.lua"); } nscapi::core_helper core(get_core(), get_id()); core.register_channel(channel_); scripts_->load_all(); } catch (nscapi::nscapi_exception &e) { NSC_LOG_ERROR_EXR("Load", e); return false; } catch (std::exception &e) { NSC_LOG_ERROR_EXR("Load", e); return false; } catch (...) { NSC_LOG_ERROR_EX("Load"); return false; } return true; }
bool SyslogClient::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode) { try { sh::settings_registry settings(get_settings_proxy()); settings.set_alias("syslog", alias, "client"); target_path = settings.alias().get_settings_path("targets"); settings.alias().add_path_to_settings() ("SYSLOG CLIENT SECTION", "Section for SYSLOG passive check module.") ("handlers", sh::fun_values_path(boost::bind(&SyslogClient::add_command, this, _1, _2)), "CLIENT HANDLER SECTION", "", "CLIENT", "For more configuration options add a dedicated section") ("targets", sh::fun_values_path(boost::bind(&SyslogClient::add_target, this, _1, _2)), "REMOTE TARGET DEFINITIONS", "", "TARGET", "For more configuration options add a dedicated section") ; settings.alias().add_key_to_settings() ("hostname", sh::string_key(&hostname_, "auto"), "HOSTNAME", "The host name of the monitored computer.\nSet this to auto (default) to use the windows name of the computer.\n\n" "auto\tHostname\n" "${host}\tHostname\n" "${host_lc}\nHostname in lowercase\n" "${host_uc}\tHostname in uppercase\n" "${domain}\tDomainname\n" "${domain_lc}\tDomainname in lowercase\n" "${domain_uc}\tDomainname in uppercase\n" ) ("channel", sh::string_key(&channel_, "syslog"), "CHANNEL", "The channel to listen to.") ; settings.register_all(); settings.notify(); client_.finalize(get_settings_proxy()); nscapi::core_helper core(get_core(), get_id()); core.register_channel(channel_); if (hostname_ == "auto") { hostname_ = boost::asio::ip::host_name(); } else if (hostname_ == "auto-lc") { hostname_ = boost::asio::ip::host_name(); std::transform(hostname_.begin(), hostname_.end(), hostname_.begin(), ::tolower); } else if (hostname_ == "auto-uc") { hostname_ = boost::asio::ip::host_name(); std::transform(hostname_.begin(), hostname_.end(), hostname_.begin(), ::toupper); } else { strEx::s::token dn = strEx::s::getToken(boost::asio::ip::host_name(), '.'); try { boost::asio::io_service svc; boost::asio::ip::tcp::resolver resolver (svc); boost::asio::ip::tcp::resolver::query query (boost::asio::ip::host_name(), ""); boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve (query), end; std::string s; while (iter != end) { s += iter->host_name(); s += " - "; s += iter->endpoint().address().to_string(); iter++; } } catch (const std::exception& e) { NSC_LOG_ERROR_EXR("Failed to resolve: ", e); } strEx::replace(hostname_, "${host}", dn.first); strEx::replace(hostname_, "${domain}", dn.second); std::transform(dn.first.begin(), dn.first.end(), dn.first.begin(), ::toupper); std::transform(dn.second.begin(), dn.second.end(), dn.second.begin(), ::toupper); strEx::replace(hostname_, "${host_uc}", dn.first); strEx::replace(hostname_, "${domain_uc}", dn.second); std::transform(dn.first.begin(), dn.first.end(), dn.first.begin(), ::tolower); std::transform(dn.second.begin(), dn.second.end(), dn.second.begin(), ::tolower); strEx::replace(hostname_, "${host_lc}", dn.first); strEx::replace(hostname_, "${domain_lc}", dn.second); } } catch (nscapi::nscapi_exception &e) { NSC_LOG_ERROR_EXR("NSClient API exception: ", e); return false; } catch (std::exception &e) { NSC_LOG_ERROR_EXR("NSClient API exception: ", e); return false; } catch (...) { NSC_LOG_ERROR_EX("NSClient API exception: "); return false; } return true; }
bool CheckExternalScripts::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode) { try { sh::settings_registry settings(get_settings_proxy()); settings.set_alias(alias, "external scripts"); aliases_.set_path(settings.alias().get_settings_path("alias")); std::string wrappings_path = settings.alias().get_settings_path("wrappings"); boost::filesystem::path scriptRoot; std::string scriptDirectory; std::map<std::string, std::string> wrappings; settings.alias().add_path_to_settings() ("wrappings", sh::string_map_path(&wrappings) , "Script wrappings", "A list of templates for defining script commands.\nEnter any command line here and they will be expanded by scripts placed under the wrapped scripts section. %SCRIPT% will be replaced by the actual script an %ARGS% will be replaced by any given arguments.", "WRAPPING", "An external script wrapping") ("alias", sh::fun_values_path(boost::bind(&CheckExternalScripts::add_alias, this, _1, _2)), "Command aliases", "A list of aliases for already defined commands (with arguments).\n" "An alias is an internal command that has been predefined to provide a single command without arguments. Be careful so you don't create loops (ie check_loop=check_a, check_a=check_loop)", "ALIAS", "Query alias") ; settings.register_all(); settings.notify(); settings.clear(); if (wrappings.find("ps1") == wrappings.end()) { wrappings["ps1"] = "cmd /c echo If (-Not (Test-Path \"scripts\\%SCRIPT%\") ) { Write-Host \"UNKNOWN: Script `\"%SCRIPT%`\" not found.\"; exit(3) }; scripts\\%SCRIPT% $ARGS$; exit($lastexitcode) | powershell.exe /noprofile -command -"; settings.register_key(wrappings_path, "ps1", NSCAPI::key_string, "POWERSHELL WRAPPING", "Command line used for executing wrapped ps1 (powershell) scripts", "cmd /c echo If (-Not (Test-Path \"scripts\\%SCRIPT%\") ) { Write-Host \"UNKNOWN: Script `\"%SCRIPT%`\" not found.\"; exit(3) }; scripts\\%SCRIPT% $ARGS$; exit($lastexitcode) | powershell.exe /noprofile -command -", false); settings.set_static_key(wrappings_path, "ps1", wrappings["ps1"]); } if (wrappings.find("vbs") == wrappings.end()) { wrappings["vbs"] = "cscript.exe //T:30 //NoLogo scripts\\\\lib\\\\wrapper.vbs %SCRIPT% %ARGS%"; settings.register_key(wrappings_path, "vbs", NSCAPI::key_string, "Visual basic script", "Command line used for wrapped vbs scripts", "cscript.exe //T:30 //NoLogo scripts\\\\lib\\\\wrapper.vbs %SCRIPT% %ARGS%", false); settings.set_static_key(wrappings_path, "vbs", wrappings["vbs"]); } if (wrappings.find("bat") == wrappings.end()) { wrappings["bat"] = "scripts\\\\%SCRIPT% %ARGS%"; settings.register_key(wrappings_path, "bat", NSCAPI::key_string, "Batch file", "Command used for executing wrapped batch files", "scripts\\\\%SCRIPT% %ARGS%", false); settings.set_static_key(wrappings_path, "bat", wrappings["bat"]); } if (aliases_.empty()) { NSC_DEBUG_MSG("No aliases found (adding default)"); add_alias("alias_cpu", "check_cpu"); add_alias("alias_cpu_ex", "check_cpu \"warn=load > $ARG1$\" \"crit=load > $ARG2$\" time=5m time=1m time=30s"); add_alias("alias_mem", "check_memory"); add_alias("alias_up", "check_uptime"); add_alias("alias_disk", "check_drivesize"); add_alias("alias_disk_loose", "check_drivesize"); add_alias("alias_volumes", "check_drivesize"); add_alias("alias_volumes_loose", "check_drivesize"); add_alias("alias_service", "check_service"); add_alias("alias_service_ex", "check_service \"exclude=Net Driver HPZ12\" \"exclude=Pml Driver HPZ12\" exclude=stisvc"); add_alias("alias_process", "check_process \"process=$ARG1$\" \"crit=state != 'started'\""); add_alias("alias_process_stopped", "check_process \"process=$ARG1$\" \"crit=state != 'stopped'\""); add_alias("alias_process_count", "check_process \"process=$ARG1$\" \"warn=count > $ARG2$\" \"crit=count > $ARG3$\""); add_alias("alias_process_hung", "check_process \"filter=is_hung\" \"crit=count>0\""); add_alias("alias_event_log", "check_eventlog"); add_alias("alias_file_size", "check_files \"path=$ARG1$\" \"crit=size > $ARG2$\" \"top-syntax=${list}\" \"detail-syntax=${filename] ${size}\" max-dir-depth=10"); add_alias("alias_file_age", "check_files \"path=$ARG1$\" \"crit=written > $ARG2$\" \"top-syntax=${list}\" \"detail-syntax=${filename] ${written}\" max-dir-depth=10"); add_alias("alias_sched_all", "check_tasksched show-all \"syntax=${title}: ${exit_code}\" \"crit=exit_code ne 0\""); add_alias("alias_sched_long", "check_tasksched \"filter=status = 'running'\" \"detail-syntax=${title} (${most_recent_run_time})\" \"crit=most_recent_run_time < -$ARG1$\""); add_alias("alias_sched_task", "check_tasksched show-all \"filter=title eq '$ARG1$'\" \"detail-syntax=${title} (${exit_code})\" \"crit=exit_code ne 0\""); // add_alias("alias_updates", "check_updates -warning 0 -critical 0"); } settings.alias().add_key_to_settings() ("timeout", sh::uint_key(&timeout, 60), "Command timeout", "The maximum time in seconds that a command can execute. (if more then this execution will be aborted). NOTICE this only affects external commands not internal ones.") ("allow arguments", sh::bool_key(&allowArgs_, false), "Allow arguments when executing external scripts", "This option determines whether or not the we will allow clients to specify arguments to commands that are executed.") ("allow nasty characters", sh::bool_key(&allowNasty_, false), "Allow certain potentially dangerous characters in arguments", "This option determines whether or not the we will allow clients to specify nasty (as in |`&><'\"\\[]{}) characters in arguments.") ("script path", sh::string_key(&scriptDirectory), "Load all scripts in a given folder", "Load all scripts in a given directory and use them as commands.") ("script root", sh::path_key(&scriptRoot, "${scripts}"), "Script root folder", "Root path where all scripts are contained (You can not upload/download scripts outside this folder).") ; settings.register_all(); settings.notify(); settings.clear(); provider_.reset(new script_provider(get_id(), get_core(), settings.alias().get_settings_path("scripts"), scriptRoot, wrappings)); settings.alias().add_path_to_settings() ("External script settings", "General settings for the external scripts module (CheckExternalScripts).") ("scripts", sh::fun_values_path(boost::bind(&CheckExternalScripts::add_command, this, _1, _2)), "External scripts", "A list of scripts available to run from the CheckExternalScripts module. Syntax is: `command=script arguments`", "SCRIPT", "For more configuration options add a dedicated section (if you add a new section you can customize the user and various other advanced features)") ("wrapped scripts", sh::fun_values_path(boost::bind(&CheckExternalScripts::add_wrapping, this, _1, _2)), "Wrapped scripts", "A list of wrapped scripts (ie. script using a template mechanism).\nThe template used will be defined by the extension of the script. Thus a foo.ps1 will use the ps1 wrapping from the wrappings section.", "WRAPPED SCRIPT", "A wrapped script definitions") ; settings.alias().add_templates() ("scripts", "plus", "Add a simple script", "Add binding for a simple script", "{" "\"fields\": [ " " { \"id\": \"alias\", \"title\" : \"Alias\", \"type\" : \"input\", \"desc\" : \"This will identify the command\"} , " " { \"id\": \"script\", \"title\" : \"Script\", \"type\" : \"data-choice\", \"desc\" : \"The name of the script\",\"exec\" : \"CheckExternalScripts list --json\" } , " " { \"id\": \"args\", \"title\" : \"Arguments\", \"type\" : \"input\", \"desc\" : \"Command line arguments for the script use $ARG1$ to specify arguments\" } , " " { \"id\": \"cmd\", \"key\" : \"command\", \"title\" : \"A\", \"type\" : \"hidden\", \"desc\" : \"A\" } " " ], " "\"events\": { " "\"onSave\": \"(function (node) { node.save_path = self.path; var f = node.get_field('cmd'); f.key = node.get_field('alias').value(); var val = node.get_field('script').value(); if (node.get_field('args').value()) { val += ' ' + node.get_field('args').value(); }; f.value(val)})\"" "}" "}") ("alias", "plus", "Add an alias", "Add binding for an alias", "{" "\"fields\": [ " " { \"id\": \"alias\", \"title\" : \"Alias\", \"type\" : \"input\", \"desc\" : \"This will identify the command\"} , " " { \"id\": \"command\", \"title\" : \"Command\", \"type\" : \"data-choice\", \"desc\" : \"The name of the command to execute\",\"exec\" : \"CheckExternalScripts list --json --query\" } , " " { \"id\": \"args\", \"title\" : \"Arguments\", \"type\" : \"input\", \"desc\" : \"Command line arguments for the command. use $ARG1$ to specify arguments\" } , " " { \"id\": \"cmd\", \"key\" : \"command\", \"title\" : \"A\", \"type\" : \"hidden\", \"desc\" : \"A\" } " " ], " "\"events\": { " "\"onSave\": \"(function (node) { node.save_path = self.path; \nvar f = node.get_field('cmd'); \nf.key = node.get_field('alias').value(); \nvar val = node.get_field('command').value(); \nif (node.get_field('args').value()) { \nval += ' ' + node.get_field('args').value(); }; \nf.value(val)})\"" "}" "}") ; settings.register_all(); settings.notify(); if (!scriptDirectory.empty()) { addAllScriptsFrom(scriptDirectory); } aliases_.add_samples(get_settings_proxy()); aliases_.add_missing(get_settings_proxy(), "default", ""); root_ = get_base_path(); nscapi::core_helper core(get_core(), get_id()); BOOST_FOREACH(const boost::shared_ptr<alias::command_object> &o, aliases_.get_object_list()) { core.register_alias(o->get_alias(), "Alias for: " + o->command); } } catch (...) { NSC_LOG_ERROR_EX("loading"); return false; } return true; }
bool SimpleFileWriter::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode mode) { std::string primary_key; std::string channel; try { sh::settings_registry settings(get_settings_proxy()); settings.set_alias(alias, "writers/file"); settings.alias().add_path_to_settings() ("FILE WRITER", "Section for simple file writer module (SimpleFileWriter.dll).") ; settings.alias().add_key_to_settings() ("syntax", sh::string_key(&primary_key, "${alias-or-command} ${result} ${message}"), "MESSAGE SYNTAX", "The syntax of the message to write to the line.\nCan be any arbitrary string as well as include any of the following special keywords:" "${command} = The command name, ${host} the host, ${channel} the recieving channel, ${alias} the alias for the command, ${alias-or-command} = alias if set otherweise command, ${message} = the message data (no escape), ${result} = The result status (number).") ("file", sh::path_key(&filename_, "output.txt"), "FILE TO WRITE TO", "The filename to write output to.") ("channel", sh::string_key(&channel, "FILE"), "CHANNEL", "The channel to listen to.") ; settings.register_all(); settings.notify(); nscapi::core_helper::core_proxy core(get_core(), get_id()); core.register_channel(channel); parsers::simple_expression parser; parsers::simple_expression::result_type result; if (!parser.parse(primary_key, result)) { NSC_LOG_ERROR_STD("Failed to parse primary key: " + primary_key) } BOOST_FOREACH(parsers::simple_expression::entry &e, result) { if (!e.is_variable) { index_lookup_.push_back(simple_string_functor(e.name)); } else if (e.name == "command") { index_lookup_.push_back(payload_command_functor()); } else if (e.name == "host") { index_lookup_.push_back(header_host_functor()); } else if (e.name == "channel") { index_lookup_.push_back(channel_functor()); } else if (e.name == "alias") { index_lookup_.push_back(payload_alias_functor()); } else if (e.name == "alias-or-command") { index_lookup_.push_back(payload_alias_or_command_functor()); } else if (e.name == "message") { index_lookup_.push_back(payload_message_functor()); } else if (e.name == "result") { index_lookup_.push_back(payload_result_functor()); } else { NSC_LOG_ERROR_STD("Invalid index: " + e.name); } } } catch (nscapi::nscapi_exception &e) { NSC_LOG_ERROR_EXR("Failed to register command: ", e); return false; } catch (std::exception &e) { NSC_LOG_ERROR_EXR("load", e); return false; } catch (...) { NSC_LOG_ERROR_EX("load"); return false; } return true; }