From 8fa9bb0344acbd671c6ad016fb48861f666bc790 Mon Sep 17 00:00:00 2001 From: CryTime Date: Fri, 26 Jun 2020 20:28:18 +0200 Subject: [PATCH] enh(centengine) : adding DeleteDowntimByStartTimeComment --- include/com/centreon/engine/engine_impl.hh | 3 ++ src/cce_rpc/engine.proto | 57 +++++++++++++--------- src/centengine/engine_impl.cc | 45 ++++++++++++++--- src/external_command/commands.cc | 1 - tests/engine/enginerpc/client.cc | 25 +++++++++- tests/engine/enginerpc/enginerpc.cc | 36 ++++++++++++++ 6 files changed, 135 insertions(+), 32 deletions(-) diff --git a/include/com/centreon/engine/engine_impl.hh b/include/com/centreon/engine/engine_impl.hh index 97dc25af518..8b8a3ad47c7 100644 --- a/include/com/centreon/engine/engine_impl.hh +++ b/include/com/centreon/engine/engine_impl.hh @@ -109,6 +109,9 @@ class engine_impl final : public Engine::Service { grpc::Status DeleteDowntimeByHostName(grpc::ServerContext* context, const DowntimeHostIdentifier* request, CommandSuccess* response) override; + grpc::Status DeleteDowntimeByStartTimeComment(grpc::ServerContext* context, + const DowntimeStartTimeIdentifier* request, + CommandSuccess* response) override; grpc::Status DelayHostNotification(grpc::ServerContext* context, const HostDelayIdentifier* request, CommandSuccess* response) override; diff --git a/src/cce_rpc/engine.proto b/src/cce_rpc/engine.proto index 1c8d845a378..00970c6ae08 100644 --- a/src/cce_rpc/engine.proto +++ b/src/cce_rpc/engine.proto @@ -39,7 +39,8 @@ service Engine { rpc GetServiceGroupsCount(google.protobuf.Empty) returns (GenericValue) {} rpc GetContactGroupsCount(google.protobuf.Empty) returns (GenericValue) {} rpc GetHostGroupsCount(google.protobuf.Empty) returns (GenericValue) {} - rpc GetServiceDependenciesCount(google.protobuf.Empty) returns (GenericValue) {} + rpc GetServiceDependenciesCount(google.protobuf.Empty) + returns (GenericValue) {} rpc GetHostDependenciesCount(google.protobuf.Empty) returns (GenericValue) {} rpc ProcessServiceCheckResult(Check) returns (CommandSuccess) {} rpc ProcessHostCheckResult(Check) returns (CommandSuccess) {} @@ -50,14 +51,19 @@ service Engine { rpc DeleteAllHostComments(HostIdentifier) returns (CommandSuccess) {} rpc DeleteAllServiceComments(ServiceIdentifier) returns (CommandSuccess) {} rpc RemoveHostAcknowledgement(HostIdentifier) returns (CommandSuccess) {} - rpc RemoveServiceAcknowledgement(ServiceIdentifier) returns (CommandSuccess) {} + rpc RemoveServiceAcknowledgement(ServiceIdentifier) + returns (CommandSuccess) {} rpc DeleteHostDowntime(GenericValue) returns (CommandSuccess) {} rpc DeleteServiceDowntime(GenericValue) returns (CommandSuccess) {} rpc DeleteHostDowntimeFull(DowntimeCriterias) returns (CommandSuccess) {} rpc DeleteServiceDowntimeFull(DowntimeCriterias) returns (CommandSuccess) {} - rpc DeleteDowntimeByHostName(DowntimeHostIdentifier) returns (CommandSuccess) {} + rpc DeleteDowntimeByHostName(DowntimeHostIdentifier) + returns (CommandSuccess) {} + rpc DeleteDowntimeByStartTimeComment(DowntimeStartTimeIdentifier) + returns (CommandSuccess) {} rpc DelayHostNotification(HostDelayIdentifier) returns (CommandSuccess) {} - rpc DelayServiceNotification(ServiceDelayIdentifier) returns (CommandSuccess) {} + rpc DelayServiceNotification(ServiceDelayIdentifier) + returns (CommandSuccess) {} } message GenericString { @@ -249,8 +255,8 @@ message ThresholdsFile { message HostIdentifier { oneof identifier { - string name = 1; - uint32 id = 2; + string name = 1; + uint32 id = 2; } } @@ -261,9 +267,9 @@ message EngineHost { string check_period = 4; uint32 id = 5; enum State { - UP = 0; + UP = 0; DOWN = 1; - UNREACHABLE = 2; + UNREACHABLE = 2; } State current_state = 6; } @@ -302,29 +308,29 @@ message EngineService { string description = 4; string check_period = 5; enum State { - OK = 0; - WARNING = 1; - CRITICAL = 2; - UNKNOWN = 3; - } + OK = 0; + WARNING = 1; + CRITICAL = 2; + UNKNOWN = 3; + } State current_state = 6; } message EngineComment { - string host_name = 1; - string svc_desc = 2; - string user = 3; - string comment_data = 4; - bool persistent = 5; - uint32 entry_time = 6; + string host_name = 1; + string svc_desc = 2; + string user = 3; + string comment_data = 4; + bool persistent = 5; + uint32 entry_time = 6; } message HostDelayIdentifier { oneof identifier { - string name = 1; - uint32 id = 2; + string name = 1; + uint32 id = 2; } - uint32 delay_time = 3; + uint32 delay_time = 3; } message ServiceDelayIdentifier { @@ -332,7 +338,7 @@ message ServiceDelayIdentifier { NameIdentifier names = 1; IdIdentifier ids = 2; } - uint32 delay_time = 3; + uint32 delay_time = 3; } message DowntimeCriterias { @@ -353,3 +359,8 @@ message DowntimeHostIdentifier { google.protobuf.UInt32Value start = 3; string comment_data = 4; } + +message DowntimeStartTimeIdentifier { + google.protobuf.UInt32Value start = 1; + string comment_data = 2; +} diff --git a/src/centengine/engine_impl.cc b/src/centengine/engine_impl.cc index ca7c6ff3de2..9465edf9793 100644 --- a/src/centengine/engine_impl.cc +++ b/src/centengine/engine_impl.cc @@ -900,9 +900,9 @@ grpc::Status engine_impl::DeleteServiceDowntimeFull( * * @param context gRPC context * @param request DowntimeHostIdentifier (it's a hostname and optionally other - * filter arguments like service description, start time and downtime's - * comment - * @param Command response + * filter arguments like service description, start time and downtime's + * comment + * @param Command response * * @return Status::OK */ @@ -910,7 +910,6 @@ grpc::Status engine_impl::DeleteDowntimeByHostName( grpc::ServerContext* context __attribute__((unused)), const DowntimeHostIdentifier* request, CommandSuccess* response) { - /*hostname must be defined to delete the downtime but not others arguments*/ std::string const& host_name = request->host_name(); if (host_name.empty()) @@ -919,7 +918,6 @@ grpc::Status engine_impl::DeleteDowntimeByHostName( auto fn = std::packaged_task([&host_name, request]() -> int32_t { - std::pair start_time; std::string service_desc; std::string comment_data; @@ -935,8 +933,7 @@ grpc::Status engine_impl::DeleteDowntimeByHostName( uint32_t deleted = downtime_manager::instance() .delete_downtime_by_hostname_service_description_start_time_comment( - host_name, service_desc, start_time, - comment_data); + host_name, service_desc, start_time, comment_data); if (deleted == 0) return 1; return 0; @@ -949,6 +946,40 @@ grpc::Status engine_impl::DeleteDowntimeByHostName( return grpc::Status::OK; } +grpc::Status engine_impl::DeleteDowntimeByStartTimeComment( + grpc::ServerContext* context __attribute__((unused)), + const DowntimeStartTimeIdentifier* request, + CommandSuccess* response) { + time_t start_time; + /*hostname must be defined to delete the downtime but not others arguments*/ + if (!(request->has_start())) + return grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, + "start_time must not be empty"); + else + start_time = request->start().value(); + + std::string const& comment_data = request->comment_data(); + if (comment_data.empty()) + return grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, + "comment_data must not be empty"); + + auto fn = std::packaged_task([&comment_data, + &start_time]() -> int32_t { + uint32_t deleted = + downtime_manager::instance() + .delete_downtime_by_hostname_service_description_start_time_comment( + "", "", {true, start_time}, comment_data); + if (0 == deleted) + return 1; + return 0; + }); + std::future result = fn.get_future(); + command_manager::instance().enqueue(std::move(fn)); + + response->set_value(!result.get()); + return grpc::Status::OK; +} + grpc::Status engine_impl::DelayHostNotification( grpc::ServerContext* context __attribute__((unused)), const HostDelayIdentifier* request, diff --git a/src/external_command/commands.cc b/src/external_command/commands.cc index 65071acaf38..b62505eeaa7 100644 --- a/src/external_command/commands.cc +++ b/src/external_command/commands.cc @@ -45,7 +45,6 @@ #include "com/centreon/engine/string.hh" #include "compatibility/mmap.h" - using namespace com::centreon::engine; using namespace com::centreon::engine::configuration::applier; using namespace com::centreon::engine::downtimes; diff --git a/tests/engine/enginerpc/client.cc b/tests/engine/enginerpc/client.cc index 829e0d97d12..6e2284e941e 100644 --- a/tests/engine/enginerpc/client.cc +++ b/tests/engine/enginerpc/client.cc @@ -558,6 +558,23 @@ class EngineRPCClient { return true; } + bool DeleteDowntimeByStartTimeComment(uint32_t const start, + std::string const& commentdata, + CommandSuccess* response) { + DowntimeStartTimeIdentifier request; + grpc::ClientContext context; + request.set_comment_data(commentdata); + request.mutable_start()->set_value(start); + + grpc::Status status = + _stub->DeleteDowntimeByStartTimeComment(&context, request, response); + if (!status.ok()) { + std::cout << "DeleteDowntimeByStartTimeComment rpc engine failed" << std::endl; + return false; + } + return true; + } + bool DelayHostNotificationByName(std::string const& hostname, uint32_t& delaytime, CommandSuccess* response) { @@ -1117,7 +1134,13 @@ int main(int argc, char** argv) { status = client.DeleteDowntimeByHostName(hostname, svcdsc, start, commentdata, &response); std::cout << "DeleteDowntimeByHostName" << std::endl; + } else if (strcmp(argv[1], "DeleteDowntimeByStartTimeComment") == 0) { + CommandSuccess response; + uint32_t start = atoi(argv[2]); + std::string commentdata = argv[3]; + + status = client.DeleteDowntimeByStartTimeComment(start, commentdata, &response); + std::cout << "DeleteDowntimeByStartTimeComment" << std::endl; } - exit(status); } diff --git a/tests/engine/enginerpc/enginerpc.cc b/tests/engine/enginerpc/enginerpc.cc index 94c6b01a36f..95142cb3e57 100644 --- a/tests/engine/enginerpc/enginerpc.cc +++ b/tests/engine/enginerpc/enginerpc.cc @@ -897,6 +897,42 @@ TEST_F(EngineRpc, DeleteDowntimeByHostName) { erpc.shutdown(); } +TEST_F(EngineRpc, DeleteDowntimeByStartTimeComment) { + enginerpc erpc("0.0.0.0", 40001); + std::unique_ptr th; + std::condition_variable condvar; + std::mutex mutex; + std::ostringstream oss; + bool continuerunning = false; + + set_time(20000); + + time_t now = time(nullptr); + std::stringstream s; + s << "test_host;test_svc;" << now << ";" << now + 1 << ";1;0;1;admin;host"; + ASSERT_EQ(0u, downtime_manager::instance().get_scheduled_downtimes().size()); + + ASSERT_EQ(cmd_schedule_downtime(CMD_SCHEDULE_SVC_DOWNTIME, now, + const_cast(s.str().c_str())), + OK); + ASSERT_EQ(1u, downtime_manager::instance().get_scheduled_downtimes().size()); + call_command_manager(th, &condvar, &mutex, &continuerunning); + + /*hostname must be defined to delete the downtime but not others arguments*/ + oss << "DeleteDowntimeByStartTimeComment " << now << " host"; + + auto output = execute(oss.str()); + { + std::lock_guard lock(mutex); + continuerunning = true; + } + condvar.notify_one(); + th->join(); + + ASSERT_EQ(0u, downtime_manager::instance().get_scheduled_downtimes().size()); + erpc.shutdown(); +} + TEST_F(EngineRpc, DelayHostNotification) { enginerpc erpc("0.0.0.0", 40001); std::unique_ptr th;