Example #1
0
int DistributedClient::clp_handler_impl::exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, std::string &reply) {
	const ::Plugin::Common_Header& request_header = request_message.header();
	int ret = NSCAPI::returnOK;
	connection_data con = parse_header(request_header, data);

	Plugin::ExecuteResponseMessage 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::exec_request, request_message.SerializeAsString(), 0));
	chunks = instance->send(con, chunks);
	BOOST_FOREACH(nscp::packet &chunk, chunks) {
		if (nscp::checks::is_exec_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_exec_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_exec_response_payload(response_message.add_payload(), "", NSCAPI::returnUNKNOWN, "Unsupported response type");
			ret = NSCAPI::returnUNKNOWN;
		}
	}
	response_message.SerializeToString(&reply);
	return ret;
}
Example #2
0
int NSCPClient::clp_handler_impl::exec(client::configuration::data_type data, const Plugin::ExecuteRequestMessage &request_message, Plugin::ExecuteResponseMessage &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_EXEC_REQUEST, request_message.SerializeAsString(), tmp);
	response_message.ParseFromString(tmp);
	return ret;
}
Example #3
0
void lua::lua_runtime::on_exec(std::string command, script_information *information, lua::lua_traits::function_type function, bool simple, const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response, const Plugin::ExecuteRequestMessage &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_exec_response_payload(response, command, NSCAPI::returnUNKNOWN, "Invalid return");
			return;
		}
		std::string msg, perf;
		msg = lua.pop_string();
		ret = lua.pop_code();
		lua.gc(LUA_GCCOLLECT, 0);
		nscapi::protobuf::functions::append_simple_exec_response_payload(response, command, ret, msg);
	} 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_exec_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);
	}
}