From e9ce6a2fc580fcd4337ca02f6dbee9da72ca5a08 Mon Sep 17 00:00:00 2001 From: CryTime Date: Fri, 31 Jul 2020 15:37:28 +0200 Subject: [PATCH] enh(centengine) : add ChangeObjectCharVar into grpc --- src/centengine/engine_impl.cc | 275 ++++++++++++++++++++++++++-- tests/engine/enginerpc/client.cc | 90 +++++++++ tests/engine/enginerpc/enginerpc.cc | 112 +++++++++++ 3 files changed, 462 insertions(+), 15 deletions(-) diff --git a/src/centengine/engine_impl.cc b/src/centengine/engine_impl.cc index 95ee283ce82..bf776201612 100644 --- a/src/centengine/engine_impl.cc +++ b/src/centengine/engine_impl.cc @@ -25,6 +25,7 @@ #include #include +#include "com/centreon/engine/statusdata.hh" #include "com/centreon/engine/anomalydetection.hh" #include "com/centreon/engine/broker.hh" #include "com/centreon/engine/command_manager.hh" @@ -1704,7 +1705,6 @@ grpc::Status engine_impl::DeleteHostDowntimeFull( .end(); it != end; ++it) { auto dt = it->second; - std::cout << dt->get_hostname() << std::endl; if (!(request->host_name().empty()) && dt->get_hostname() != request->host_name()) continue; @@ -2140,9 +2140,7 @@ grpc::Status engine_impl::ChangeHostObjectIntVar(grpc::ServerContext* context temp_host->set_current_attempt(temp_host->get_max_attempts()); } else if (ChangeObjectInt::Mode_Name(request->mode()) == "MODATTR") { attr = request->intval(); - } else { - return 1; - } + } else return 1; if (ChangeObjectInt::Mode_Name(request->mode()) == "MODATTR") temp_host->set_modified_attributes(attr); @@ -2229,9 +2227,7 @@ grpc::Status engine_impl::ChangeServiceObjectIntVar( temp_service->set_current_attempt(temp_service->get_max_attempts()); } else if (ChangeObjectInt::Mode_Name(request->mode()) == "MODATTR") attr = request->intval(); - else { - return 1; - } + else return 1; if (ChangeObjectInt::Mode_Name(request->mode()) == "MODATTR") temp_service->set_modified_attributes(attr); @@ -2284,9 +2280,7 @@ grpc::Status engine_impl::ChangeContactObjectIntVar( "MODSATTR") { sattr = request->intval(); temp_contact->set_modified_service_attributes(sattr); - } else { - return 1; - } + } else return 1; /* send data to event broker */ broker_adaptive_contact_data( @@ -2312,8 +2306,106 @@ grpc::Status engine_impl::ChangeHostObjectCharVar( const ChangeObjectChar* request, CommandSuccess* response) { auto fn = std::packaged_task([request]() -> int32_t { - if (ChangeObjectChar::Mode_Name(request->mode()) == "") { + std::shared_ptr temp_host; + timeperiod* temp_timeperiod{nullptr}; + command_map::iterator cmd_found; + unsigned long attr{MODATTR_NONE}; + + /* For these cases, we verify that the host is valid */ + if (ChangeObjectChar::Mode_Name(request->mode()) + == "CHANGE_EVENT_HANDLER" || + ChangeObjectChar::Mode_Name(request->mode()) + == "CHANGE_CHECK_COMMAND" || + ChangeObjectChar::Mode_Name(request->mode()) + == "CHANGE_CHECK_TIMEPERIOD" || + ChangeObjectChar::Mode_Name(request->mode()) + == "CHANGE_NOTIFICATION_TIMEPERIOD") { + auto it = host::hosts.find(request->host_name()); + if (it != host::hosts.end()) + temp_host = it->second; + if (temp_host == nullptr) + return 1; + } + + /* make sure the timeperiod is valid */ + if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_CHECK_TIMEPERIOD" || + ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_NOTIFICATION_TIMEPERIOD") { + auto found = timeperiod::timeperiods.find(request->charval()); + if (found != timeperiod::timeperiods.end()) + temp_timeperiod = found->second.get(); + if (temp_timeperiod == nullptr) + return 1; } + /* make sure the command exists */ + else { + cmd_found = commands::command::commands.find(request->charval()); + if (cmd_found == commands::command::commands.end() || + !cmd_found->second) + return 1; + } + + /* update the variable */ + if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_GLOBAL_EVENT_HANDLER") { + config->global_host_event_handler(request->charval()); + global_host_event_handler_ptr = cmd_found->second.get(); + attr = MODATTR_EVENT_HANDLER_COMMAND; + } + else if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_EVENT_HANDLER") { + temp_host->set_event_handler(request->charval()); + temp_host->set_event_handler_ptr(cmd_found->second.get()); + attr = MODATTR_EVENT_HANDLER_COMMAND; + } + else if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_CHECK_COMMAND") { + temp_host->set_check_command(request->charval()); + temp_host->set_check_command_ptr(cmd_found->second.get()); + attr = MODATTR_CHECK_COMMAND; + } + else if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_CHECK_TIMEPERIOD") { + temp_host->set_check_period(request->charval()); + temp_host->check_period_ptr = temp_timeperiod; + attr = MODATTR_CHECK_TIMEPERIOD; + } + else if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_NOTIFICATION_TIMEPERIOD") { + temp_host->set_notification_period(request->charval()); + temp_host->set_notification_period_ptr(temp_timeperiod); + attr = MODATTR_NOTIFICATION_TIMEPERIOD; + } + else return 1; + + /* send data to event broker and update status file */ + if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_GLOBAL_EVENT_HANDLER") { + /* set the modified host attribute */ + modified_host_process_attributes |= attr; + + /* send data to event broker */ + broker_adaptive_program_data( + NEBTYPE_ADAPTIVEPROGRAM_UPDATE, NEBFLAG_NONE, NEBATTR_NONE, + CMD_NONE, attr, modified_host_process_attributes, MODATTR_NONE, + modified_service_process_attributes, nullptr); + /* update program status */ + update_program_status(false); + } + else { + /* set the modified host attribute */ + temp_host->add_modified_attributes(attr); + + /* send data to event broker */ + broker_adaptive_host_data(NEBTYPE_ADAPTIVEHOST_UPDATE, NEBFLAG_NONE, + NEBATTR_NONE, temp_host.get(), CMD_NONE, attr, + temp_host->get_modified_attributes(), nullptr); + + /* update the status log with the host info */ + temp_host->update_status(false); + } + return 0; }); @@ -2329,7 +2421,108 @@ grpc::Status engine_impl::ChangeServiceObjectCharVar( const ChangeObjectChar* request, CommandSuccess* response) { auto fn = std::packaged_task([request]() -> int32_t { - if (ChangeObjectChar::Mode_Name(request->mode()) == "") { + std::shared_ptr temp_service; + timeperiod* temp_timeperiod{nullptr}; + command_map::iterator cmd_found; + unsigned long attr{MODATTR_NONE}; + + /* For these cases, we verify that the host is valid */ + if (ChangeObjectChar::Mode_Name(request->mode()) + == "CHANGE_EVENT_HANDLER" || + ChangeObjectChar::Mode_Name(request->mode()) + == "CHANGE_CHECK_COMMAND" || + ChangeObjectChar::Mode_Name(request->mode()) + == "CHANGE_CHECK_TIMEPERIOD" || + ChangeObjectChar::Mode_Name(request->mode()) + == "CHANGE_NOTIFICATION_TIMEPERIOD") { + /* verify that the service is valid */ + auto it = service::services.find({request->host_name(), + request->service_desc()}); + if (it != service::services.end()) + temp_service = it->second; + if (temp_service == nullptr) + return 1; + } + /* make sure the timeperiod is valid */ + if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_CHECK_TIMEPERIOD" || + ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_NOTIFICATION_TIMEPERIOD") { + auto found = timeperiod::timeperiods.find(request->charval()); + if (found != timeperiod::timeperiods.end()) + temp_timeperiod = found->second.get(); + if (temp_timeperiod == nullptr) + return 1; + } + /* make sure the command exists */ + else { + cmd_found = commands::command::commands.find(request->charval()); + if (cmd_found == commands::command::commands.end() || + !cmd_found->second) { + return 1; + } + } + + /* update the variable */ + if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_GLOBAL_EVENT_HANDLER") { + config->global_service_event_handler(request->charval()); + global_service_event_handler_ptr = cmd_found->second.get(); + attr = MODATTR_EVENT_HANDLER_COMMAND; + } + else if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_EVENT_HANDLER") { + temp_service->set_event_handler(request->charval()); + temp_service->set_event_handler_ptr(cmd_found->second.get()); + attr = MODATTR_EVENT_HANDLER_COMMAND; + } + else if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_CHECK_COMMAND") { + temp_service->set_check_command(request->charval()); + temp_service->set_check_command_ptr(cmd_found->second.get()); + attr = MODATTR_CHECK_COMMAND; + } + else if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_CHECK_TIMEPERIOD") { + temp_service->set_check_period(request->charval()); + temp_service->check_period_ptr = temp_timeperiod; + attr = MODATTR_CHECK_TIMEPERIOD; + } + else if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_NOTIFICATION_TIMEPERIOD") { + temp_service->set_notification_period(request->charval()); + temp_service->set_notification_period_ptr(temp_timeperiod); + attr = MODATTR_NOTIFICATION_TIMEPERIOD; + } + else return 1; + + /* send data to event broker and update status file */ + if (ChangeObjectChar::Mode_Name(request->mode()) == + "CHANGE_GLOBAL_EVENT_HANDLER") { + /* set the modified service attribute */ + modified_service_process_attributes |= attr; + + /* send data to event broker */ + broker_adaptive_program_data( + NEBTYPE_ADAPTIVEPROGRAM_UPDATE, NEBFLAG_NONE, NEBATTR_NONE, CMD_NONE, + MODATTR_NONE, modified_host_process_attributes, attr, + modified_service_process_attributes, nullptr); + + /* update program status */ + update_program_status(false); + } + else { + /* set the modified service attribute */ + temp_service->add_modified_attributes(attr); + + /* send data to event broker */ + broker_adaptive_service_data( + NEBTYPE_ADAPTIVESERVICE_UPDATE, NEBFLAG_NONE, NEBATTR_NONE, + temp_service.get(), CMD_NONE, attr, + temp_service->get_modified_attributes(), nullptr); + + /* update the status log with the service info */ + temp_service->update_status(false); } return 0; }); @@ -2345,8 +2538,62 @@ grpc::Status engine_impl::ChangeContactObjectCharVar( grpc::ServerContext* context __attribute__((unused)), const ChangeContactObjectChar* request, CommandSuccess* response) { + if (request->contact().empty()) + return grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, + "contact must not be empty"); + auto fn = - std::packaged_task([request]() -> int32_t { return 0; }); + std::packaged_task([request](void) -> int32_t { + std::shared_ptr temp_contact; + timeperiod* temp_timeperiod{nullptr}; + unsigned long hattr{MODATTR_NONE}; + unsigned long sattr{MODATTR_NONE}; + + auto it = contact::contacts.find(request->contact()); + if (it != contact::contacts.end()) + temp_contact= it->second; + if (temp_contact == nullptr) + return 1; + + auto found = timeperiod::timeperiods.find(request->charval()); + if (found != timeperiod::timeperiods.end()) + temp_timeperiod = found->second.get(); + if (temp_timeperiod == nullptr) + return 1; + if (ChangeContactObjectChar::Mode_Name(request->mode()) == + "CHANGE_HOST_NOTIFICATION_TIMEPERIOD") { + temp_contact->set_host_notification_period(request->charval()); + temp_contact->set_host_notification_period_ptr(temp_timeperiod); + hattr = MODATTR_NOTIFICATION_TIMEPERIOD; + } + else if (ChangeContactObjectChar::Mode_Name(request->mode()) == + "CHANGE_CONTACT_SVC_NOTIFICATION_TIMEPERIOD"){ + temp_contact->set_service_notification_period(request->charval()); + temp_contact->set_service_notification_period_ptr(temp_timeperiod); + hattr = MODATTR_NOTIFICATION_TIMEPERIOD; + } + else return 1; + + /* set the modified attributes */ + temp_contact->set_modified_host_attributes( + temp_contact->get_modified_host_attributes() | hattr); + temp_contact->set_modified_service_attributes( + temp_contact->get_modified_service_attributes() | sattr); + + /* send data to event broker */ + broker_adaptive_contact_data( + NEBTYPE_ADAPTIVECONTACT_UPDATE, NEBFLAG_NONE, NEBATTR_NONE, + temp_contact.get(), CMD_NONE, MODATTR_NONE, + temp_contact->get_modified_attributes(), hattr, + temp_contact->get_modified_host_attributes(), sattr, + temp_contact->get_modified_service_attributes(), nullptr); + + /* update the status log with the contact info */ + temp_contact->update_status_info(false); + + + return 0; + }); std::future result = fn.get_future(); command_manager::instance().enqueue(std::move(fn)); @@ -2381,7 +2628,6 @@ grpc::Status engine_impl::ChangeHostObjectCustomVar( /* set the modified attributes and update the status of the object */ temp_host->add_modified_attributes(MODATTR_CUSTOM_VARIABLE); temp_host->update_status(false); - return 0; }); @@ -2422,7 +2668,6 @@ grpc::Status engine_impl::ChangeServiceObjectCustomVar( it->second.update(request->varvalue()); temp_service->add_modified_attributes(MODATTR_CUSTOM_VARIABLE); temp_service->update_status(false); - std::cout << "aaa" << std::endl; return 0; }); diff --git a/tests/engine/enginerpc/client.cc b/tests/engine/enginerpc/client.cc index 1d4aa040b0d..4aa40f248dd 100644 --- a/tests/engine/enginerpc/client.cc +++ b/tests/engine/enginerpc/client.cc @@ -1159,6 +1159,68 @@ class EngineRPCClient { return true; } + bool ChangeHostObjectCharVar(std::string const& hostname, + uint32_t mode, + std::string charval, + CommandSuccess* response) { + ChangeObjectChar request; + grpc::ClientContext context; + + request.set_host_name(hostname); + request.set_mode(static_cast(mode)); + request.set_charval(charval); + + grpc::Status status = + _stub->ChangeHostObjectCharVar(&context, request, response); + if (!status.ok()) { + std::cout << "ChangeHostObjectCharVar rpc engine failed" << std::endl; + return false; + } + return true; + } + + bool ChangeServiceObjectCharVar(std::string const& hostname, + std::string const& servicedesc, + uint32_t mode, + std::string charval, + CommandSuccess* response) { + ChangeObjectChar request; + grpc::ClientContext context; + + request.set_host_name(hostname); + request.set_service_desc(servicedesc); + request.set_mode(static_cast(mode)); + request.set_charval(charval); + + grpc::Status status = + _stub->ChangeServiceObjectCharVar(&context, request, response); + if (!status.ok()) { + std::cout << "ChangeServiceObjectCharVar rpc engine failed" << std::endl; + return false; + } + return true; + } + + bool ChangeContactObjectCharVar(std::string const& contact, + uint32_t mode, + std::string charval, + CommandSuccess* response) { + ChangeContactObjectChar request; + grpc::ClientContext context; + + request.set_contact(contact); + request.set_mode(static_cast(mode)); + request.set_charval(charval); + + grpc::Status status = + _stub->ChangeContactObjectCharVar(&context, request, response); + if (!status.ok()) { + std::cout << "ChangeContactObjectCharVar rpc engine failed" << std::endl; + return false; + } + return true; + } + bool ChangeHostObjectCustomVar(std::string const& hostname, std::string const& varname, std::string const& varvalue, @@ -1999,6 +2061,34 @@ int main(int argc, char** argv) { status = client.ChangeContactObjectCustomVar(contact, varname, varvalue, &response); std::cout << "ChangeContactObjectCustomVar" << std::endl; + } else if (strcmp(argv[1], "ChangeHostObjectCharVar") == 0) { + CommandSuccess response; + std::string hostname(argv[2]); + uint32_t mode = atoi(argv[3]); + std::string charval(argv[4]); + + status = client.ChangeHostObjectCharVar(hostname, mode, charval, + &response); + std::cout << "ChangeHostObjectCharVar " << status << std::endl; + } else if (strcmp(argv[1], "ChangeServiceObjectCharVar") == 0) { + CommandSuccess response; + std::string hostname(argv[2]); + std::string servicedesc(argv[3]); + uint32_t mode = atoi(argv[4]); + std::string charval(argv[5]); + + status = client.ChangeServiceObjectCharVar(hostname, servicedesc, + mode, charval, &response); + std::cout << "ChangeServiceObjectCharVar " << status << std::endl; + } else if (strcmp(argv[1], "ChangeContactObjectCharVar") == 0) { + CommandSuccess response; + std::string contact(argv[2]); + uint32_t mode = atoi(argv[3]); + std::string charval(argv[4]); + + status = client.ChangeContactObjectCharVar(contact, + mode, charval, &response); + std::cout << "ChangeContactObjectCharVar " << status << std::endl; } else { std::cout << "unknown command" << std::endl; status = EXIT_FAILURE; diff --git a/tests/engine/enginerpc/enginerpc.cc b/tests/engine/enginerpc/enginerpc.cc index 5d0100652e5..512c3ad9dd2 100644 --- a/tests/engine/enginerpc/enginerpc.cc +++ b/tests/engine/enginerpc/enginerpc.cc @@ -906,6 +906,118 @@ TEST_F(EngineRpc, ChangeContactObjectIntVar) { erpc.shutdown(); } +TEST_F(EngineRpc, ChangeHostObjectCharVar) { + enginerpc erpc("0.0.0.0", 40001); + std::unique_ptr th; + std::condition_variable condvar; + std::mutex mutex; + bool continuerunning = false; + + ASSERT_EQ(engine::timeperiod::timeperiods.size(), 1u); + + call_command_manager(th, &condvar, &mutex, &continuerunning); + + auto output = execute( + "ChangeHostObjectCharVar" + " null 0 cmd"); + ASSERT_EQ(output.back(), "ChangeHostObjectCharVar 1"); + output = execute( + "ChangeHostObjectCharVar" + " test_host 1 cmd"); + ASSERT_EQ(_host->get_event_handler(), "cmd"); + output = execute( + "ChangeHostObjectCharVar" + " test_host 2 cmd"); + ASSERT_EQ(_host->get_check_command(), "cmd"); + output = execute( + "ChangeHostObjectCharVar" + " test_host 3 24x7"); + ASSERT_EQ(_host->get_check_period(), "24x7"); + output = execute( + "ChangeHostObjectCharVar" + " test_host 4 24x7"); + ASSERT_EQ(_host->get_notification_period(), "24x7"); + { + std::lock_guard lock(mutex); + continuerunning = true; + } + condvar.notify_one(); + th->join(); + + erpc.shutdown(); +} + +TEST_F(EngineRpc, ChangeServiceObjectCharVar) { + enginerpc erpc("0.0.0.0", 40001); + std::unique_ptr th; + std::condition_variable condvar; + std::mutex mutex; + bool continuerunning = false; + + ASSERT_EQ(engine::timeperiod::timeperiods.size(), 1u); + + call_command_manager(th, &condvar, &mutex, &continuerunning); + + auto output = execute( + "ChangeServiceObjectCharVar" + " null null 0 cmd"); + ASSERT_EQ(output.back(), "ChangeServiceObjectCharVar 1"); + output = execute( + "ChangeServiceObjectCharVar" + " test_host test_svc 1 cmd"); + ASSERT_EQ(_svc->get_event_handler(), "cmd"); + output = execute( + "ChangeServiceObjectCharVar" + " test_host test_svc 2 cmd"); + ASSERT_EQ(_svc->get_check_command(), "cmd"); + output = execute( + "ChangeServiceObjectCharVar" + " test_host test_svc 3 24x7"); + ASSERT_EQ(_svc->get_check_period(), "24x7"); + output = execute( + "ChangeServiceObjectCharVar" + " test_host test_svc 4 24x7"); + ASSERT_EQ(_svc->get_notification_period(), "24x7"); + { + std::lock_guard lock(mutex); + continuerunning = true; + } + condvar.notify_one(); + th->join(); + + erpc.shutdown(); +} + +TEST_F(EngineRpc, ChangeContactObjectCharVar) { + enginerpc erpc("0.0.0.0", 40001); + std::unique_ptr th; + std::condition_variable condvar; + std::mutex mutex; + bool continuerunning = false; + + ASSERT_EQ(engine::timeperiod::timeperiods.size(), 1u); + + call_command_manager(th, &condvar, &mutex, &continuerunning); + + auto output = execute( + "ChangeContactObjectCharVar" + " admin 0 24x7"); + ASSERT_EQ(_contact->get_host_notification_period(), "24x7"); + output = execute( + "ChangeContactObjectCharVar" + " admin 1 24x7"); + ASSERT_EQ(_contact->get_service_notification_period(), "24x7"); + + { + std::lock_guard lock(mutex); + continuerunning = true; + } + condvar.notify_one(); + th->join(); + + erpc.shutdown(); +} + TEST_F(EngineRpc, ChangeHostObjectCustomVar) { enginerpc erpc("0.0.0.0", 40001); std::unique_ptr th;