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"); } }
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; } }
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; } }
void CheckExternalScripts::addAllScriptsFrom(std::string str_path) { std::string pattern = "*.*"; boost::filesystem::path path(str_path); if (!boost::filesystem::is_directory(path)) { if (path.has_relative_path()) path = get_base_path() / path; if (!boost::filesystem::is_directory(path)) { file_helpers::patterns::pattern_type split_path = file_helpers::patterns::split_pattern(path); if (!boost::filesystem::is_directory(split_path.first)) { NSC_LOG_ERROR_STD("Path was not found: " + split_path.first.string()); return; } path = split_path.first; pattern = split_path.second.string(); } } NSC_DEBUG_MSG("Using script path: " + path.string()); boost::regex re; try { std::string pre = file_helpers::patterns::glob_to_regexp(pattern); NSC_DEBUG_MSG("Using regexp: " + pre); re = pre; } catch (std::exception &e) { NSC_LOG_ERROR_EXR("Invalid pattern: " + pattern, e); return; } boost::filesystem::directory_iterator end_itr; for (boost::filesystem::directory_iterator itr(path); itr != end_itr; ++itr) { if (!is_directory(itr->status())) { std::string name = file_helpers::meta::get_filename(itr->path()); if (regex_match(name, re)) add_command(name, itr->path().string()); } } }
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; }
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); } }
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); } }
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; }
int NSLoadModuleEx(unsigned int id, char* alias, int mode) { try { return NSLoadModuleExNoExcept(id, alias, mode); } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("NSLoadModuleEx", e); } catch (...) { NSC_LOG_CRITICAL("Unknown exception in: NSLoadModuleEx"); } return NSCAPI::hasFailed; }
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 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 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; }
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; }
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 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; }
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 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; }
void check::check_pdh(boost::shared_ptr<pdh_thread> &collector, const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response) { typedef filter filter_type; modern_filter::data_container data; modern_filter::cli_helper<filter_type> filter_helper(request, response, data); std::vector<std::string> counters; bool expand_index = false; bool reload = false; bool check_average = false; bool expand_instance = false; std::string flags; std::string type; std::string time; filter_type filter; filter_helper.add_options("", "", "", filter.get_filter_syntax(), "Everything looks good"); filter_helper.add_syntax("${status}: ${problem_list}", filter.get_format_syntax(), "${counter} = ${value}", "${alias}"); filter_helper.get_desc().add_options() ("counter", po::value<std::vector<std::string>>(&counters), "Performance counter to check") ("expand-index", po::bool_switch(&expand_index), "Expand indexes in counter strings") ("instances", po::bool_switch(&expand_instance), "Expand wildcards and fetch all instances") ("reload", po::bool_switch(&reload), "Reload counters on errors (useful to check counters which are not added at boot)") ("averages", po::bool_switch(&check_average), "Check average values (ie. wait for 1 second to collecting two samples)") ("time", po::value<std::string>(&time), "Timeframe to use for named rrd counters") ("flags", po::value<std::string>(&flags), "Extra flags to configure the counter (nocap100, 1000, noscale)") ("type", po::value<std::string>(&type)->default_value("large"), "Format of value (double, long, large)") ; std::vector<std::string> extra; if (!filter_helper.parse_options(extra)) return; if (filter_helper.empty() && data.syntax_top == "${problem_list}") data.syntax_top = "${list}"; if (counters.empty() && extra.empty()) return nscapi::protobuf::functions::set_response_bad(*response, "No counters specified: add counter=<name of counter>"); if (!filter_helper.build_filter(filter)) return; if (filter_helper.empty()) { filter.add_manual_perf("value"); } PDH::PDHQuery pdh; std::list<PDH::pdh_instance> free_counters; typedef std::map<std::string,std::string> counter_list; counter_list named_counters; bool has_counter = false; std::list<std::wstring> to_check; BOOST_FOREACH(std::string &counter, counters) { try { if (counter.find('\\') == std::string::npos) { named_counters[counter] = counter; } else { if (expand_index) { PDH::PDHResolver::expand_index(counter); } PDH::pdh_object obj; if (expand_instance) obj.set_instances("*"); obj.set_flags(flags); obj.set_counter(counter); obj.set_alias(counter); obj.set_strategy_static(); obj.set_type(type); PDH::pdh_instance instance = PDH::factory::create(obj); pdh.addCounter(instance); free_counters.push_back(instance); has_counter = true; } } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("Failed to poll counter", e); return nscapi::protobuf::functions::set_response_bad(*response, "Failed to add counter: " + utf8::utf8_from_native(e.what())); } } BOOST_FOREACH(const std::string &s, extra) { try { std::string counter, alias; if ((s.size() > 8) && (s.substr(0,8) == "counter:")) { std::string::size_type pos = s.find('='); if (pos != std::string::npos) { alias = s.substr(8,pos-8); counter = s.substr(pos+1); } else return nscapi::protobuf::functions::set_response_bad(*response, "Invalid option: " + s); } else return nscapi::protobuf::functions::set_response_bad(*response, "Invalid option: " + s); if (counter.find('\\') == std::string::npos) { named_counters[counter] = counter; } else { if (expand_index) { PDH::PDHResolver::expand_index(counter); } PDH::pdh_object obj; obj.set_flags(flags); obj.set_counter(counter); obj.set_strategy_static(); obj.set_type(type); obj.set_alias(alias); PDH::pdh_instance instance = PDH::factory::create(obj); pdh.addCounter(instance); free_counters.push_back(instance); has_counter = true; } } catch (const std::exception &e) { NSC_LOG_ERROR_EXR("Failed to poll counter", e); return nscapi::protobuf::functions::set_response_bad(*response, "Failed to add counter: " + utf8::utf8_from_native(e.what())); } } if (!free_counters.empty()) { try { pdh.open(); if (check_average) { pdh.collect(); Sleep(1000); } //pdh.collect(); pdh.gatherData(expand_instance); pdh.close(); } catch (const PDH::pdh_exception &e) { NSC_LOG_ERROR_EXR("Failed to poll counter", e); return nscapi::protobuf::functions::set_response_bad(*response, "Failed to poll counter: " + utf8::utf8_from_native(e.what())); } } BOOST_FOREACH(const counter_list::value_type &vc, named_counters) { try { typedef std::map<std::string,double> value_list_type; value_list_type values; if (time.empty()) { values = collector->get_value(vc.second); } else { values = collector->get_average(vc.second, strEx::stoui_as_time(time)/1000); } if (values.empty()) return nscapi::protobuf::functions::set_response_bad(*response, "Failed to get value"); BOOST_FOREACH(const value_list_type::value_type &v, values) { boost::shared_ptr<filter_obj> record(new filter_obj(vc.first, v.first, v.second)); modern_filter::match_result ret = filter.match(record); if (ret.is_done) { break; } } } catch (const PDH::pdh_exception &e) { NSC_LOG_ERROR_EXR("ERROR", e); return nscapi::protobuf::functions::set_response_bad(*response, "Failed to get value: " + utf8::utf8_from_native(e.what())); } } BOOST_FOREACH(PDH::pdh_instance &instance, free_counters) { try { if (expand_instance) { BOOST_FOREACH(const PDH::pdh_instance &child, instance->get_instances()) { boost::shared_ptr<filter_obj> record(new filter_obj(child->get_name(), child->get_counter(), child->get_int_value())); modern_filter::match_result ret = filter.match(record); if (ret.is_done) break; } } else { boost::shared_ptr<filter_obj> record(new filter_obj(instance->get_name(), instance->get_counter(), instance->get_int_value())); modern_filter::match_result ret = filter.match(record); if (ret.is_done) break; } } catch (const PDH::pdh_exception &e) {