示例#1
0
int main(int argc, char* argv[]) {
	Plugin::QueryResponseMessage response_message;
	std::vector<std::string> args;
	for (int i = 1; i < argc; i++) {
		args.push_back(argv[i]);
	}
	Plugin::QueryRequestMessage request_message;
	Plugin::QueryRequestMessage::Request *request = request_message.add_payload();
	request->set_command("check_nrpe");
	for (int i = 1; i < argc; i++) {
		request->add_arguments(argv[i]);
	}

	check_nrpe client;
	client.query(request_message, response_message);
	NSCAPI::nagiosReturn ret = NSCAPI::query_return_codes::returnOK;
	BOOST_FOREACH(const ::Plugin::QueryResponseMessage_Response &response, response_message.payload()) {
		ret = nscapi::plugin_helper::maxState(ret, nscapi::protobuf::functions::gbp_to_nagios_status(response.result()));
		BOOST_FOREACH(const ::Plugin::QueryResponseMessage_Response_Line &line, response.lines()) {
			std::cout << line.message();
			std::string tmp = nscapi::protobuf::functions::build_performance_data(line);
			if (!tmp.empty())
				std::cout << '|' << tmp;
		}
	}
	return ret;
}
示例#2
0
文件: compat.cpp 项目: borgified/nscp
	void matchShowAll(const boost::program_options::variables_map &vm, Plugin::QueryRequestMessage::Request &request, std::string prefix) {
		if (vm.count("ShowAll")) {
			request.add_arguments("top-syntax=" + prefix + "${list}");
		} else {
			request.add_arguments("top-syntax=" + prefix + "${problem_list}");
		}

	}
示例#3
0
void log_args(const Plugin::QueryRequestMessage::Request &request) {
	std::stringstream ss;
	for (int i = 0; i < request.arguments_size(); i++) {
		if (i > 0)
			ss << " ";
		ss << request.arguments(i);
	}
	NSC_DEBUG_MSG("Created command: " + ss.str());
}
示例#4
0
void LUAScript::query_fallback(const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response, const Plugin::QueryRequestMessage &request_message) {
	std::string response_buffer;
	boost::optional<scripts::command_definition<lua::lua_traits> > cmd = scripts_->find_command(scripts::nscp::tags::query_tag, request.command());
	if (!cmd) {
		cmd = scripts_->find_command(scripts::nscp::tags::simple_query_tag, request.command());
		if (!cmd)
			return nscapi::protobuf::functions::set_response_bad(*response, "Failed to find command: " + request.command());
		return lua_runtime_->on_query(request.command(), cmd->information, cmd->function, true, request, response, request_message);
	}
	return lua_runtime_->on_query(request.command(), cmd->information, cmd->function, false, request, response, request_message);
}
示例#5
0
void NRPEClient::query_fallback(const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response, const Plugin::QueryRequestMessage &request_message) {
	client::configuration config(nrpe_client::command_prefix, 
		boost::shared_ptr<nrpe_client::clp_handler_impl>(new nrpe_client::clp_handler_impl(boost::shared_ptr<socket_helpers::client::client_handler>(new client_handler()))), 
		boost::shared_ptr<nrpe_client::target_handler>(new nrpe_client::target_handler(targets)));
	nrpe_client::setup(config, request_message.header());
	commands.parse_query(nrpe_client::command_prefix, nrpe_client::default_command, request.command(), config, request, *response, request_message);
}
示例#6
0
void lua::lua_runtime::on_query(std::string command, script_information *information, lua::lua_traits::function_type function, bool simple, const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response, const Plugin::QueryRequestMessage &request_message)
{
	lua_wrapper lua(prep_function(information, function));
	int args = 2;
	if (function.object_ref != 0)
		args = 3;
	if (simple) {
		std::list<std::string> argslist;
		for (int i=0;i<request.arguments_size();i++)
			argslist.push_back(request.arguments(i));
		lua.push_string(command);
		lua.push_array(argslist);
		if (lua.pcall(args, 3, 0) != 0)
			return nscapi::protobuf::functions::set_response_bad(*response, "Failed to handle command: " + command + ": " + lua.pop_string());
		NSCAPI::nagiosReturn ret = NSCAPI::returnUNKNOWN;
		if (lua.size() < 3) {
			NSC_LOG_ERROR_STD("Invalid return: " + lua.dump_stack());
			nscapi::protobuf::functions::append_simple_query_response_payload(response, command, NSCAPI::returnUNKNOWN, "Invalid return", "");
			return;
		}
		std::string msg, perf;
		perf = lua.pop_string();
		msg = lua.pop_string();
		ret = lua.pop_code();
		lua.gc(LUA_GCCOLLECT, 0);
		nscapi::protobuf::functions::append_simple_query_response_payload(response, command, ret, msg, perf);
	} else {
		lua.push_string(command);
		lua.push_raw_string(request.SerializeAsString());
		lua.push_raw_string(request_message.SerializeAsString());
		args++;
		if (lua.pcall(args, 1, 0) != 0)
			return nscapi::protobuf::functions::set_response_bad(*response, "Failed to handle command: " + command + ": " + lua.pop_string());
		if (lua.size() < 1) {
			NSC_LOG_ERROR_STD("Invalid return: " + lua.dump_stack());
			nscapi::protobuf::functions::append_simple_query_response_payload(response, command, NSCAPI::returnUNKNOWN, "Invalid return data", "");
			return;
		}
		Plugin::QueryResponseMessage local_response;
		std::string data = lua.pop_raw_string();
		response->ParseFromString(data);
		lua.gc(LUA_GCCOLLECT, 0);
	}
}
示例#7
0
void CheckExternalScripts::query_fallback(const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response, const Plugin::QueryRequestMessage &) {
		//nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request);

		commands::optional_command_object command_def = commands_.find_object(request.command());

		std::list<std::string> args;
		for (int i=0;i<request.arguments_size();++i) {
			args.push_back(request.arguments(i));
		}
		if (command_def) {
			handle_command(*command_def, args, response);
			return;
		}
		alias::optional_command_object alias_def = aliases_.find_object(request.command());
		if (alias_def) {
			handle_alias(*alias_def, args, response);
			return;
		}
		NSC_LOG_ERROR_STD("No command or alias found matching: " + request.command());
		nscapi::protobuf::functions::set_response_bad(*response, "No command or alias found matching: " + request.command());
	}
示例#8
0
void CheckDisk::checkDriveSize(Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response) {
	boost::program_options::options_description desc;

	std::vector<std::string> times;
	std::vector<std::string> types;
	std::string perf_unit;
	nscapi::program_options::add_help(desc);
	desc.add_options()
		("CheckAll", po::value<std::string>()->implicit_value("true"), "Checks all drives.")
		("CheckAllOthers", po::value<std::string>()->implicit_value("true"), "Checks all drives turns the drive option into an exclude option.")
		("Drive", po::value<std::vector<std::string>>(&times), "The drives to check")
		("FilterType", po::value<std::vector<std::string>>(&types), "The type of drives to check fixed, remote, cdrom, ramdisk, removable")
		("perf-unit", po::value<std::string>(&perf_unit), "Force performance data to use a given unit prevents scaling which can cause problems over time in some graphing solutions.")
		;
	compat::addShowAll(desc);
	compat::addAllNumeric(desc);
	compat::addAllNumeric(desc, "Free");
	compat::addAllNumeric(desc, "Used");

	boost::program_options::variables_map vm;
	std::vector<std::string> extra;
	if (!nscapi::program_options::process_arguments_from_request(vm, desc, request, *response, true, extra))
		return;
	std::string warn, crit;

	request.clear_arguments();
	compat::matchFirstNumeric(vm, "used", "free", warn, crit);
	compat::matchFirstNumeric(vm, "used", "used", warn, crit, "Used");
	compat::matchFirstNumeric(vm, "free", "free", warn, crit, "Free");
	compat::inline_addarg(request, warn);
	compat::inline_addarg(request, crit);
	if (vm.count("CheckAll"))
		request.add_arguments("drive=*");
	bool exclude = false;
	if (vm.count("CheckAllOthers")) {
		request.add_arguments("drive=*");
		exclude = true;
	}
	if (!perf_unit.empty())
		request.add_arguments("perf-config=free(unit:" + perf_unit + ")used(unit:" + perf_unit + ")");
	request.add_arguments("detail-syntax=%(drive): Total: %(size) - Used: %(used) (%(used_pct)%) - Free: %(free) (%(free_pct)%)");
	compat::matchShowAll(vm, request);
	std::string keyword = exclude ? "exclude=" : "drive=";
	BOOST_FOREACH(const std::string &t, times) {
		request.add_arguments(keyword + t);
	}
示例#9
0
void CheckExternalScripts::query_fallback(const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response, const Plugin::QueryRequestMessage &) {
	if (!provider_) {
		NSC_LOG_ERROR_STD("No provider found: " + request.command());
		nscapi::protobuf::functions::set_response_bad(*response, "No command or alias found matching: " + request.command());
		return;
	}
	commands::command_object_instance command_def = provider_->find_command(request.command());

	std::list<std::string> args;
	for (int i = 0; i < request.arguments_size(); ++i) {
		args.push_back(request.arguments(i));
	}
	if (command_def) {
		handle_command(*command_def, args, response);
		return;
	}
	alias::command_object_instance alias_def = aliases_.find_object(request.command());
	if (alias_def) {
		handle_alias(*alias_def, args, response);
		return;
	}
	NSC_LOG_ERROR_STD("No command or alias found matching: " + request.command());
	nscapi::protobuf::functions::set_response_bad(*response, "No command or alias found matching: " + request.command());
}
示例#10
0
void CheckMKClient::query_fallback(const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response, const Plugin::QueryRequestMessage &request_message) {
	client::configuration config(command_prefix, boost::shared_ptr<clp_handler_impl>(new clp_handler_impl(this)), boost::shared_ptr<target_handler>(new target_handler(targets)));
	setup(config, request_message.header());
	commands.parse_query(command_prefix, default_command, request.command(), config, request, *response, request_message);
}
示例#11
0
void CheckTaskSched::CheckTaskSched_(Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response) {
	boost::program_options::options_description desc;

	std::vector<std::string> counters;
	std::string syntax, topSyntax, filter, arg_warn, arg_crit;
	bool debug = false;
	nscapi::program_options::add_help(desc);
	desc.add_options()
		("warn", po::value<std::string>(&arg_warn), "Warning bounds.")
		("crit", po::value<std::string>(&arg_crit), "Critical bounds.")
		("MaxWarn", po::value<std::string>(), "Maximum value before a warning is returned.")
		("MaxCrit", po::value<std::string>(), "Maximum value before a critical is returned.")
		("MinWarn", po::value<std::string>(), "Minimum value before a warning is returned.")
		("MinCrit", po::value<std::string>(), "Minimum value before a critical is returned.")
		("Counter", po::value<std::vector<std::string>>(&counters), "The time to check")
		("truncate", po::value<std::string>(), "Deprecated option")
		("syntax", po::value<std::string>(&syntax), "Syntax (same as detail-syntax in the check_tasksched check)")
		("master-syntax", po::value<std::string>(&topSyntax), "Master Syntax (same as top-syntax in the check_tasksched check)")
		("filter", po::value<std::string>(&filter), "Filter (same as filter in the check_tasksched check)")
		("debug", po::bool_switch(&debug), "Filter (same as filter in the check_tasksched check)")
		;

	boost::program_options::variables_map vm;
	std::vector<std::string> extra;
	if (!nscapi::program_options::process_arguments_from_request(vm, desc, request, *response, true, extra))
		return;
	std::string warn, crit;

	request.clear_arguments();
	if (vm.count("MaxWarn"))
		warn = "warn=count > " + vm["MaxWarn"].as<std::string>();
	if (vm.count("MaxCrit"))
		crit = "crit=count > " + vm["MaxCrit"].as<std::string>();
	if (vm.count("MinWarn"))
		warn = "warn=count < " + vm["MinWarn"].as<std::string>();
	if (vm.count("MinCrit"))
		crit = "crit=count > " + vm["MinCrit"].as<std::string>();
	if (!arg_warn.empty())
		warn = "warn=count " + arg_warn;
	if (!arg_crit.empty())
		crit = "crit=count " + arg_crit;
	if (!warn.empty())
		request.add_arguments(warn);
	if (!crit.empty())
		request.add_arguments(crit);
	if (debug)
		request.add_arguments("debug");
	if (!filter.empty())
		request.add_arguments("filter=" + filter);
	if (!topSyntax.empty()) {
		boost::replace_all(topSyntax, "%status%", "${task_status}");
		request.add_arguments("top-syntax=" + topSyntax);
	}
	if (!syntax.empty()) {
		boost::replace_all(syntax, "%title%", "${title}");
		boost::replace_all(syntax, "%status%", "${task_status}");
		boost::replace_all(syntax, "%exit_code%", "${exit_code}");
		boost::replace_all(syntax, "%most_recent_run_time%", "${most_recent_run_time}");

		request.add_arguments("detail-syntax=" + syntax);
	}

	log_args(request);
	check_tasksched(request, response);
}
示例#12
0
		cli_helper(const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response, data_container &data)
			: data(data)
			, desc("Allowed options for " + request.command())
			, request(request)
			, response(response)
			, show_all(false) {}
示例#13
0
void SMTPClient::query_fallback(const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response, const Plugin::QueryRequestMessage &request_message) {
	client::configuration config(command_prefix);
	setup(config, request_message.header());
	commands.parse_query(command_prefix, default_command, request.command(), config, request, *response, request_message);
}
示例#14
0
void SamplePluginSimple::sample_raw_command(const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response *response) {
	response->set_command(request.command());
	response->set_message("Yaaay it works");
	response->set_result(Plugin::Common_ResultCode_OK);
}
示例#15
0
文件: compat.cpp 项目: Fox-Alpha/nscp
	void matchShowAll(const boost::program_options::variables_map &vm, Plugin::QueryRequestMessage::Request &request, std::string prefix) {
		if (vm.count("ShowAll")) {
			request.add_arguments("show-all");
		}
	}