Example #1
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);
}
Example #2
0
void NRPEClient::nrpe_forward(const std::string &command, Plugin::QueryRequestMessage &request, Plugin::QueryResponseMessage *response) {
	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.header());
	commands.forward_query(config, request, *response);
}
Example #3
0
int DistributedClient::clp_handler_impl::query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, std::string &reply) {
	const ::Plugin::Common_Header& request_header = request_message.header();
	int ret = NSCAPI::returnUNKNOWN;
	connection_data con = parse_header(request_header, data);

	Plugin::QueryResponseMessage response_message;
	nscapi::functions::make_return_header(response_message.mutable_header(), request_header);

	std::list<nscp::packet> chunks;
	chunks.push_back(nscp::factory::create_envelope_request(1));
	chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request_message.SerializeAsString(), 0));
	chunks = instance->send(con, chunks);
	BOOST_FOREACH(nscp::packet &chunk, chunks) {
		if (nscp::checks::is_query_response(chunk)) {
			nscapi::functions::append_response_payloads(response_message, chunk.payload);
		} else if (nscp::checks::is_error(chunk)) {
			std::string error = gather_and_log_errors(chunk.payload);
			nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, error);
			ret = NSCAPI::returnUNKNOWN;
		} else {
			NSC_LOG_ERROR_STD(_T("Unsupported message type: ") + strEx::itos(chunk.signature.payload_type));
			nscapi::functions::append_simple_query_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type");
			ret = NSCAPI::returnUNKNOWN;
		}
	}
	response_message.SerializeToString(&reply);
	return ret;
}
Example #4
0
int NSCPClient::clp_handler_impl::query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, Plugin::QueryResponseMessage &response_message) {
	const ::Plugin::Common_Header& request_header = request_message.header();
	connection_data con = parse_header(request_header, data);

	std::string tmp;
	int ret = send(con, NSCPIPC::Common_MessageTypes_QUERY_REQUEST, request_message.SerializeAsString(), tmp);
	response_message.ParseFromString(tmp);
	return ret;
}
Example #5
0
int CheckMKClient::clp_handler_impl::query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, Plugin::QueryResponseMessage &response_message) {
	const ::Plugin::Common_Header& request_header = request_message.header();
	int ret = NSCAPI::returnUNKNOWN;
	connection_data con = parse_header(request_header, data);

	nscapi::protobuf::functions::make_return_header(response_message.mutable_header(), request_header);

	instance->send(con);
	return ret;
}
Example #6
0
void client::configuration::do_query(const Plugin::QueryRequestMessage &request, Plugin::QueryResponseMessage &response) {
	Plugin::QueryResponseMessage local_response;

	std::string target = "default";
	if (request.header().has_recipient_id())
		target = request.header().recipient_id();
	else if (request.header().has_destination_id())
		target = request.header().destination_id();

	BOOST_FOREACH(const std::string t, strEx::s::splitEx(target, std::string(","))) {
		destination_container d;
		destination_container s;

		// If we have a target, apply it
		object_handler_type::object_instance op = targets.find_object(t);
		if (op)
			d.apply(op);
		else {
			object_handler_type::object_instance op = targets.find_object("default");
			if (op)
				d.apply(op);
		}
		// Next apply the header object
		d.apply(t, request.header());
		s.apply(request.header().sender_id(), request.header());
		std::string command = request.header().command();

		if (!command.empty()) {
			// If we have a header command treat the data as a batch
			i_do_query(s, d, command, request, response, true);
		} else {
			// Parse each objects command and execute them
			for (int i=0;i<request.payload_size();i++) {
				::Plugin::QueryRequestMessage local_request_message;
				const ::Plugin::QueryRequestMessage::Request &local_request = request.payload(i);
				local_request_message.mutable_header()->CopyFrom(request.header());
				local_request_message.add_payload()->CopyFrom(local_request);
				std::string command = local_request.command();
				::Plugin::QueryResponseMessage local_response_message;
				i_do_query(s, d, command, local_request_message, local_response_message, false);
				for (int j=0;j<local_response_message.payload_size();j++) {
					response.add_payload()->CopyFrom(local_response_message.payload(j));
				}
			}
		}
	}
}
Example #7
0
NSCAPI::nagiosReturn SyslogClient::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) {
	std::wstring cmd = client::command_line_parser::parse_command(char_command, command_prefix);

	Plugin::QueryRequestMessage message;
	message.ParseFromString(request);

	client::configuration config;
	setup(config, message.header());

	return commands.process_query(cmd, config, message, result);
}
/*
void client::command_manager::parse_query(const std::string &prefix, const std::string &default_command, const std::string &cmd, client::configuration &config, const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response &response, const Plugin::QueryRequestMessage &request_message) {
	boost::program_options::variables_map vm;
	std::string real_command;
	try {
		command_type::const_iterator cit = commands.find(cmd);
		boost::program_options::variables_map vm;
		if (cit == commands.end()) {
			real_command = parse_command(cmd, prefix);
			if (real_command == "forward") {
				for (int i=0;i<request_message.header().metadata_size();i++) {
					if (request_message.header().metadata(i).key() == "command")
						config.data->command = request_message.header().metadata(i).value();
				}
				for (int i=0;i<request.arguments_size();i++) {
					config.data->arguments.push_back(request.arguments(i));
				}
			} else {
				po::options_description desc = create_descriptor(real_command, default_command, config);
				if (!nscapi::program_options::process_arguments_from_request(vm, desc, request, response)) 
					return;
			}
		} else {
			std::vector<std::string> args;
			real_command = parse_command(cit->second.command, prefix);
			po::options_description desc = create_descriptor(real_command, default_command, config);
			BOOST_FOREACH(std::string argument, cit->second.arguments) {
				for (int i=0;i<request.arguments_size();i++) {
					strEx::replace(argument, "$ARG" + strEx::s::xtos(i+1) + "$", request.arguments(i));
				}
				args.push_back(argument);
			}
			if (!nscapi::program_options::process_arguments_from_vector(vm, desc, request.command(), args, response)) 
				return;
		}
	} catch (const std::exception &e) {
		return nscapi::protobuf::functions::set_response_bad(response, "Exception processing command line: " + utf8::utf8_from_native(e.what()));
	}

	if (!config.data->target_id.empty()) {
		if (!config.targets.has_object(config.data->target_id))
			return nscapi::protobuf::functions::set_response_bad(response, "Target not found: " + config.data->target_id);
		//TODO: config.data->recipient.import(config.target_lookup->lookup_target(config.data->target_id));
	}
	if (real_command == "query" || (real_command.empty() && default_command == "query")) {
		do_query(config, request_message.header(), response);
	} else if (real_command == "exec" || (real_command.empty() && default_command == "exec")) {
		return nscapi::protobuf::functions::set_response_bad(response, "Paradigm shift currently not supported");
	} else if (real_command == "submit" || (real_command.empty() && default_command == "submit")) {
		return nscapi::protobuf::functions::set_response_bad(response, "Paradigm shift currently not supported");
	} else {
		return nscapi::protobuf::functions::set_response_bad(response, "Invalid command: "  +real_command);
	}
}
*/
void client::configuration::parse_query(const Plugin::QueryRequestMessage &request, Plugin::QueryResponseMessage &response) {

	std::string command = "";

	for (int i = 0; i<request.header().metadata_size(); i++) {
		if (request.header().metadata(i).key() == "command")
			command = request.header().metadata(i).value();
	}
	for (int i = 0; i < request.payload_size(); i++) {
		::Plugin::QueryRequestMessage::Request *reqp = request.mutable_payload(0);
		if (!command.empty()) {
			command = reqp->command();
		}
		// Check pre populated commands
		if (reqp->arguments_size() > 0) {

			boost::program_options::variables_map vm;
			std::string real_command = "query";
			try {
				po::options_description desc = create_descriptor(real_command, real_command, config);
				if (!nscapi::program_options::process_arguments_from_vector(vm, desc, real_command, args, response))
					return;
			}
			catch (const std::exception &e) {
				return nscapi::protobuf::functions::set_response_bad(response, "Exception processing command line: " + utf8::utf8_from_native(e.what()));
			}

			if (real_command == "query") {
				const ::Plugin::Common::Header header;
				do_query(config, header, response);
			}
			else {
				return nscapi::protobuf::functions::set_response_bad(response, "Invalid command: " + real_command);
			}
		}
	}
}
void client::configuration::forward_query(const Plugin::QueryRequestMessage &request, Plugin::QueryResponseMessage &response) {
	Plugin::QueryResponseMessage local_response;

	std::string target = "default";
	if (request.header().has_destination_id())
		target = request.header().destination_id();
	BOOST_FOREACH(const std::string t, strEx::s::splitEx(target, std::string(","))) {
		nscapi::settings_objects::object_handler::optional_object op = targets.find_object(t);
		if (op) {
			destination_container d(*op);
			d.apply(t, request.header());
			destination_container s;
			s.apply(request.header().sender_id(), request.header());
			::Plugin::QueryResponseMessage local_response_message;
			if (!handler->query(s, d, request, local_response_message)) {
				nscapi::protobuf::functions::set_response_bad(*response.add_payload(), "Failed to submit");
				return;
			}
			for (int i = 0; i < local_response_message.payload_size(); i++) {
				response.add_payload()->CopyFrom(local_response.payload(i));
			}
		}
	}
}
Example #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);
}
Example #11
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);
}