diff --git a/include/com/centreon/engine/engine_impl.hh b/include/com/centreon/engine/engine_impl.hh index 264481a0ce7..7d56c7b434e 100644 --- a/include/com/centreon/engine/engine_impl.hh +++ b/include/com/centreon/engine/engine_impl.hh @@ -95,11 +95,12 @@ class engine_impl final : public Engine::Service { const ServiceIdentifier* request, CommandSuccess* response) override; grpc::Status AcknowledgementHostProblem(grpc::ServerContext* context, - const EngineAcknowledgement* request, - CommandSuccess* response) override; - grpc::Status AcknowledgementServiceProblem(grpc::ServerContext* context, - const EngineAcknowledgement* request, - CommandSuccess* response) override; + const EngineAcknowledgement* request, + CommandSuccess* response) override; + grpc::Status AcknowledgementServiceProblem( + grpc::ServerContext* context, + const EngineAcknowledgement* request, + CommandSuccess* response) override; grpc::Status ScheduleHostDowntime(grpc::ServerContext* context, const ScheduleDowntimeIdentifier* request, CommandSuccess* response) override; @@ -176,6 +177,37 @@ class engine_impl final : public Engine::Service { grpc::Status DelayServiceNotification(grpc::ServerContext* context, const ServiceDelayIdentifier* request, CommandSuccess* response) override; + grpc::Status ChangeHostObjectIntVar(grpc::ServerContext* context, + const ChangeObjectInt* request, + CommandSuccess* response) override; + grpc::Status ChangeServiceObjectIntVar(grpc::ServerContext* context, + const ChangeObjectInt* request, + CommandSuccess* response) override; + grpc::Status ChangeContactObjectIntVar(grpc::ServerContext* context, + const ChangeContactObjectInt* request, + CommandSuccess* response) override; + grpc::Status ChangeHostObjectCharVar(grpc::ServerContext* context, + const ChangeObjectChar* request, + CommandSuccess* response) override; + grpc::Status ChangeServiceObjectCharVar(grpc::ServerContext* context, + const ChangeObjectChar* request, + CommandSuccess* response) override; + grpc::Status ChangeContactObjectCharVar( + grpc::ServerContext* context, + const ChangeContactObjectChar* request, + CommandSuccess* response) override; + grpc::Status ChangeHostObjectCustomVar(grpc::ServerContext* context + __attribute__((unused)), + const ChangeObjectCustomVar* request, + CommandSuccess* response) override; + grpc::Status ChangeServiceObjectCustomVar( + grpc::ServerContext* context __attribute__((unused)), + const ChangeObjectCustomVar* request, + CommandSuccess* response) override; + grpc::Status ChangeContactObjectCustomVar( + grpc::ServerContext* context __attribute__((unused)), + const ChangeObjectCustomVar* request, + CommandSuccess* response) override; }; CCE_END() diff --git a/src/cce_rpc/engine.proto b/src/cce_rpc/engine.proto index f661292ba2c..d7b42448831 100644 --- a/src/cce_rpc/engine.proto +++ b/src/cce_rpc/engine.proto @@ -29,7 +29,6 @@ package com.centreon.engine; service Engine { rpc GetVersion(google.protobuf.Empty) returns (Version) {} rpc GetStats(GenericString) returns (Stats) {} - rpc GetHost(HostIdentifier) returns (EngineHost) {} rpc GetContact(ContactIdentifier) returns (EngineContact) {} rpc GetService(ServiceIdentifier) returns (EngineService) {} @@ -96,6 +95,25 @@ service Engine { returns (CommandSuccess) {} rpc SignalProcess(EngineSignalProcess) returns (CommandSuccess) {} + rpc ChangeHostObjectIntVar(ChangeObjectInt) + returns (CommandSuccess) {} + rpc ChangeServiceObjectIntVar(ChangeObjectInt) + returns (CommandSuccess) {} + rpc ChangeContactObjectIntVar(ChangeContactObjectInt) + returns (CommandSuccess) {} + rpc ChangeHostObjectCharVar(ChangeObjectChar) + returns (CommandSuccess) {} + rpc ChangeServiceObjectCharVar(ChangeObjectChar) + returns (CommandSuccess) {} + rpc ChangeContactObjectCharVar(ChangeContactObjectChar) + returns (CommandSuccess) {} + rpc ChangeHostObjectCustomVar(ChangeObjectCustomVar) + returns (CommandSuccess) {} + rpc ChangeServiceObjectCustomVar(ChangeObjectCustomVar) + returns (CommandSuccess) {} + rpc ChangeContactObjectCustomVar(ChangeObjectCustomVar) + returns (CommandSuccess) {} + } message GenericString { @@ -455,3 +473,65 @@ message EngineAcknowledgement { bool notify = 6; bool persistent = 7; } + +/* used for a host or a service */ +message ChangeObjectInt { + string host_name = 1; + string service_desc = 2; + enum Mode { + NORMAL_CHECK_INTERVAL = 0; + RETRY_CHECK_INTERVAL = 1; + MAX_ATTEMPTS = 2; + MODATTR = 3; + } + Mode mode = 3; + uint32 intval = 4; + double dval = 5; +} + +/* used for a contact */ +message ChangeContactObjectInt { + string contact_name = 1; + enum Mode { + MODATTR = 0; + MODHATTR = 1; + MODSATTR = 2; + } + Mode mode = 2; + uint32 intval = 3; + double dval = 4; +} + +/* used for a host or a service */ +message ChangeObjectChar { + string host_name = 1; + string service_desc = 2; + enum Mode { + CHANGE_GLOBAL_EVENT_HANDLER = 0; + CHANGE_EVENT_HANDLER = 1; + CHANGE_CHECK_COMMAND = 2; + CHANGE_CHECK_TIMEPERIOD = 3; + CHANGE_NOTIFICATION_TIMEPERIOD = 4; + } + Mode mode = 3; + string charval = 4; +} + +/* used for a contact */ +message ChangeContactObjectChar { + string contact = 1; + enum Mode { + CHANGE_CONTACT_HOST_NOTIFICATION_TIMEPERIOD = 0; + CHANGE_CONTACT_SVC_NOTIFICATION_TIMEPERIOD = 1; + } + Mode mode = 2; + string charval = 3; +} + +message ChangeObjectCustomVar { + string host_name = 1; + string service_desc = 2; + string contact = 3; + string varname = 4; + string varvalue = 5; +} diff --git a/src/centengine/engine_impl.cc b/src/centengine/engine_impl.cc index e763fe380a5..bf776201612 100644 --- a/src/centengine/engine_impl.cc +++ b/src/centengine/engine_impl.cc @@ -21,10 +21,11 @@ #include #include #include - +#include #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; @@ -2080,6 +2080,639 @@ grpc::Status engine_impl::DelayServiceNotification( return grpc::Status::OK; } +grpc::Status engine_impl::ChangeHostObjectIntVar(grpc::ServerContext* context + __attribute__((unused)), + const ChangeObjectInt* request, + CommandSuccess* response) { + auto fn = std::packaged_task([request]() -> int32_t { + std::shared_ptr temp_host; + unsigned long attr = MODATTR_NONE; + + auto it = host::hosts.find(request->host_name()); + if (it != host::hosts.end()) + temp_host = it->second; + if (temp_host == nullptr) + return 1; + if (ChangeObjectInt::Mode_Name(request->mode()) == + "NORMAL_CHECK_INTERVAL") { + /* save the old check interval */ + double old_dval = temp_host->get_check_interval(); + + /* modify the check interval */ + temp_host->set_check_interval(request->dval()); + attr = MODATTR_NORMAL_CHECK_INTERVAL; + + /* schedule a host check if previous interval was 0 (checks were not + * regularly scheduled) */ + if (old_dval == 0 && temp_host->get_checks_enabled()) { + time_t preferred_time(0); + time_t next_valid_time(0); + /* set the host check flag */ + temp_host->set_should_be_scheduled(true); + + /* schedule a check for right now (or as soon as possible) */ + time(&preferred_time); + if (!check_time_against_period(preferred_time, + temp_host->check_period_ptr)) { + get_next_valid_time(preferred_time, &next_valid_time, + temp_host->check_period_ptr); + temp_host->set_next_check(next_valid_time); + } else + temp_host->set_next_check(preferred_time); + + /* schedule a check if we should */ + if (temp_host->get_should_be_scheduled()) + temp_host->schedule_check(temp_host->get_next_check(), + CHECK_OPTION_NONE); + } + } else if (ChangeObjectInt::Mode_Name(request->mode()) == + "RETRY_CHECK_INTERVAL") { + temp_host->set_retry_interval(request->dval()); + attr = MODATTR_RETRY_CHECK_INTERVAL; + } else if (ChangeObjectInt::Mode_Name(request->mode()) == "MAX_ATTEMPTS") { + temp_host->set_max_attempts(request->intval()); + attr = MODATTR_MAX_CHECK_ATTEMPTS; + + /* adjust current attempt number if in a hard state */ + if (temp_host->get_state_type() == notifier::hard && + temp_host->get_current_state() != host::state_up && + temp_host->get_current_attempt() > 1) + temp_host->set_current_attempt(temp_host->get_max_attempts()); + } else if (ChangeObjectInt::Mode_Name(request->mode()) == "MODATTR") { + attr = request->intval(); + } else return 1; + + if (ChangeObjectInt::Mode_Name(request->mode()) == "MODATTR") + temp_host->set_modified_attributes(attr); + else + temp_host->set_modified_attributes(temp_host->get_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; + }); + + 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::ChangeServiceObjectIntVar( + grpc::ServerContext* context __attribute__((unused)), + const ChangeObjectInt* request, + CommandSuccess* response) { + auto fn = std::packaged_task([request]() -> int32_t { + std::shared_ptr temp_service; + unsigned long attr = MODATTR_NONE; + + 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; + if (ChangeObjectInt::Mode_Name(request->mode()) == + "NORMAL_CHECK_INTERVAL") { + /* save the old check interval */ + double old_dval = temp_service->get_check_interval(); + + /* modify the check interval */ + temp_service->set_check_interval(request->dval()); + attr = MODATTR_NORMAL_CHECK_INTERVAL; + + /* schedule a service check if previous interval was 0 (checks were not + * regularly scheduled) */ + if (old_dval == 0 && temp_service->get_checks_enabled() && + temp_service->get_check_interval() != 0) { + time_t preferred_time(0); + time_t next_valid_time(0); + /* set the service check flag */ + temp_service->set_should_be_scheduled(true); + + /* schedule a check for right now (or as soon as possible) */ + time(&preferred_time); + if (!check_time_against_period(preferred_time, + temp_service->check_period_ptr)) { + get_next_valid_time(preferred_time, &next_valid_time, + temp_service->check_period_ptr); + temp_service->set_next_check(next_valid_time); + } else + temp_service->set_next_check(preferred_time); + + /* schedule a check if we should */ + if (temp_service->get_should_be_scheduled()) + temp_service->schedule_check(temp_service->get_next_check(), + CHECK_OPTION_NONE); + } + } else if (ChangeObjectInt::Mode_Name(request->mode()) == + "RETRY_CHECK_INTERVAL") { + temp_service->set_retry_interval(request->dval()); + attr = MODATTR_RETRY_CHECK_INTERVAL; + } else if (ChangeObjectInt::Mode_Name(request->mode()) == "MAX_ATTEMPTS") { + temp_service->set_max_attempts(request->intval()); + attr = MODATTR_MAX_CHECK_ATTEMPTS; + + /* adjust current attempt number if in a hard state */ + if (temp_service->get_state_type() == notifier::hard && + temp_service->get_current_state() != service::state_ok && + temp_service->get_current_attempt() > 1) + temp_service->set_current_attempt(temp_service->get_max_attempts()); + } else if (ChangeObjectInt::Mode_Name(request->mode()) == "MODATTR") + attr = request->intval(); + else return 1; + + if (ChangeObjectInt::Mode_Name(request->mode()) == "MODATTR") + temp_service->set_modified_attributes(attr); + else + temp_service->set_modified_attributes( + temp_service->get_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; + }); + + 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::ChangeContactObjectIntVar( + grpc::ServerContext* context __attribute__((unused)), + const ChangeContactObjectInt* request, + CommandSuccess* response) { + auto fn = std::packaged_task([request]() -> int32_t { + std::shared_ptr temp_contact; + unsigned long attr = MODATTR_NONE; + unsigned long hattr = MODATTR_NONE; + unsigned long sattr = MODATTR_NONE; + + auto itcontactname = contact::contacts.find(request->contact_name()); + if (itcontactname != contact::contacts.end()) + temp_contact = itcontactname->second; + else { + return 1; + } + + if (ChangeContactObjectInt::Mode_Name(request->mode()) == "MODATTR") { + attr = request->intval(); + temp_contact->set_modified_attributes(attr); + } else if (ChangeContactObjectInt::Mode_Name(request->mode()) == + "MODHATTR") { + hattr = request->intval(); + temp_contact->set_modified_host_attributes(hattr); + } else if (ChangeContactObjectInt::Mode_Name(request->mode()) == + "MODSATTR") { + sattr = request->intval(); + temp_contact->set_modified_service_attributes(sattr); + } else return 1; + + /* send data to event broker */ + broker_adaptive_contact_data( + NEBTYPE_ADAPTIVECONTACT_UPDATE, NEBFLAG_NONE, NEBATTR_NONE, + temp_contact.get(), CMD_NONE, attr, + 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)); + + response->set_value(!result.get()); + return grpc::Status::OK; +} + +grpc::Status engine_impl::ChangeHostObjectCharVar( + grpc::ServerContext* context __attribute__((unused)), + const ChangeObjectChar* request, + CommandSuccess* response) { + auto fn = std::packaged_task([request]() -> int32_t { + 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; + }); + + 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::ChangeServiceObjectCharVar( + grpc::ServerContext* context __attribute__((unused)), + const ChangeObjectChar* request, + CommandSuccess* response) { + auto fn = std::packaged_task([request]() -> int32_t { + 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; + }); + + 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::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](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)); + response->set_value(!result.get()); + return grpc::Status::OK; +} + +grpc::Status engine_impl::ChangeHostObjectCustomVar( + grpc::ServerContext* context __attribute__((unused)), + const ChangeObjectCustomVar* request, + CommandSuccess* response) { + if (request->host_name().empty()) + return grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, + "host_name must not be empty"); + auto fn = std::packaged_task([request]() -> int32_t { + // std::shared_ptr temp_host; + host* temp_host{nullptr}; + std::string varname(request->varname()); + + std::transform(varname.begin(), varname.end(), varname.begin(), ::toupper); + host_map::const_iterator it_h(host::hosts.find(request->host_name())); + if (it_h != host::hosts.end()) + temp_host = it_h->second.get(); + if (temp_host == nullptr) + return 1; + map_customvar::iterator it(temp_host->custom_variables.find(varname)); + if (it == temp_host->custom_variables.end()) + temp_host->custom_variables[varname] = + customvariable(request->varvalue()); + else + it->second.update(request->varvalue()); + /* 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; + }); + + 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::ChangeServiceObjectCustomVar( + grpc::ServerContext* context __attribute__((unused)), + const ChangeObjectCustomVar* request, + CommandSuccess* response) { + if (request->host_name().empty()) + return grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, + "host_name must not be empty"); + if (request->service_desc().empty()) + return grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, + "service description must not be empty"); + + auto fn = std::packaged_task([request]() -> int32_t { + service* temp_service{nullptr}; + std::string varname(request->varname()); + + std::transform(varname.begin(), varname.end(), varname.begin(), ::toupper); + service_map::const_iterator it_s(service::services.find( + {request->host_name(), request->service_desc()})); + if (it_s != service::services.end()) + temp_service = it_s->second.get(); + if (temp_service == nullptr) + return 1; + map_customvar::iterator it(temp_service->custom_variables.find(varname)); + if (it == temp_service->custom_variables.end()) + temp_service->custom_variables[varname] = + customvariable(request->varvalue()); + else + it->second.update(request->varvalue()); + temp_service->add_modified_attributes(MODATTR_CUSTOM_VARIABLE); + temp_service->update_status(false); + 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::ChangeContactObjectCustomVar( + grpc::ServerContext* context __attribute__((unused)), + const ChangeObjectCustomVar* 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 { + contact* temp_contact{nullptr}; + std::string varname(request->varname()); + + std::transform(varname.begin(), varname.end(), varname.begin(), ::toupper); + contact_map::iterator cnct_it = contact::contacts.find(request->contact()); + if (cnct_it != contact::contacts.end()) + temp_contact = cnct_it->second.get(); + if (temp_contact == nullptr) + return 1; + map_customvar::iterator it( + temp_contact->get_custom_variables().find(varname)); + if (it == temp_contact->get_custom_variables().end()) + temp_contact->get_custom_variables()[varname] = + customvariable(request->varvalue()); + else + it->second.update(request->varvalue()); + 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::ProcessServiceCheckResult(grpc::ServerContext* context __attribute__((unused)), const Check* request, diff --git a/tests/engine/enginerpc/client.cc b/tests/engine/enginerpc/client.cc index a0fda10ec2c..4aa40f248dd 100644 --- a/tests/engine/enginerpc/client.cc +++ b/tests/engine/enginerpc/client.cc @@ -464,21 +464,20 @@ class EngineRPCClient { grpc::Status status = _stub->AcknowledgementHostProblem(&context, request, response); if (!status.ok()) { - std::cout << "AcknowledgementHostProblem rpc engine failed" - << std::endl; + std::cout << "AcknowledgementHostProblem rpc engine failed" << std::endl; return false; } return true; } - + bool AcknowledgementServiceProblem(std::string const& hostname, - std::string const& servicedesc, - std::string const& ackauthor, - std::string const& ackdata, - int type, - bool notify, - bool persistent, - CommandSuccess* response) { + std::string const& servicedesc, + std::string const& ackauthor, + std::string const& ackdata, + int type, + bool notify, + bool persistent, + CommandSuccess* response) { EngineAcknowledgement request; grpc::ClientContext context; request.set_host_name(hostname); @@ -1093,6 +1092,196 @@ class EngineRPCClient { return true; } + bool ChangeHostObjectIntVar(std::string const& hostname, + uint32_t& mode, + uint32_t& intval, + double& dval, + CommandSuccess* response) { + ChangeObjectInt request; + grpc::ClientContext context; + + request.set_host_name(hostname); + request.set_mode(static_cast(mode)); + request.set_intval(intval); + request.set_dval(dval); + + grpc::Status status = + _stub->ChangeHostObjectIntVar(&context, request, response); + if (!status.ok()) { + std::cout << "ChangeHostObjectIntVa rpc engine failed" << std::endl; + return false; + } + return true; + } + + bool ChangeServiceObjectIntVar(std::string const& hostname, + std::string const& servicedesc, + uint32_t& mode, + uint32_t& intval, + double& dval, + CommandSuccess* response) { + ChangeObjectInt request; + grpc::ClientContext context; + + request.set_host_name(hostname); + request.set_service_desc(servicedesc); + request.set_mode(static_cast(mode)); + request.set_intval(intval); + request.set_dval(dval); + + grpc::Status status = + _stub->ChangeServiceObjectIntVar(&context, request, response); + if (!status.ok()) { + std::cout << "ChangeServiceObjectIntVa rpc engine failed" << std::endl; + return false; + } + return true; + } + + bool ChangeContactObjectIntVar(std::string const& contactname, + uint32_t& mode, + uint32_t& intval, + double& dval, + CommandSuccess* response) { + ChangeContactObjectInt request; + grpc::ClientContext context; + + request.set_contact_name(contactname); + request.set_mode(static_cast(mode)); + request.set_intval(intval); + request.set_dval(dval); + grpc::Status status = + _stub->ChangeContactObjectIntVar(&context, request, response); + if (!status.ok()) { + std::cout << "ChangeContactObjectIntVar rpc engine failed" << std::endl; + return false; + } + 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, + CommandSuccess* response) { + ChangeObjectCustomVar request; + grpc::ClientContext context; + + request.set_host_name(hostname); + request.set_varname(varname); + request.set_varvalue(varvalue); + grpc::Status status = + _stub->ChangeHostObjectCustomVar(&context, request, response); + if (!status.ok()) { + std::cout << "ChangeHostObjectCustomVar rpc engine failed" << std::endl; + return false; + } + return true; + } + + bool ChangeServiceObjectCustomVar(std::string const& hostname, + std::string const& servicedesc, + std::string const& varname, + std::string const& varvalue, + CommandSuccess* response) { + ChangeObjectCustomVar request; + grpc::ClientContext context; + + request.set_host_name(hostname); + request.set_service_desc(servicedesc); + request.set_varname(varname); + request.set_varvalue(varvalue); + grpc::Status status = + _stub->ChangeServiceObjectCustomVar(&context, request, response); + if (!status.ok()) { + std::cout << "ChangeServiceObjectCustomVar rpc engine failed" + << std::endl; + return false; + } + return true; + } + + bool ChangeContactObjectCustomVar(std::string const& contact, + std::string const& varname, + std::string const& varvalue, + CommandSuccess* response) { + ChangeObjectCustomVar request; + grpc::ClientContext context; + + request.set_contact(contact); + request.set_varname(varname); + request.set_varvalue(varvalue); + grpc::Status status = + _stub->ChangeContactObjectCustomVar(&context, request, response); + if (!status.ok()) { + std::cout << "ChangeContactObjectCustomVar rpc engine failed" + << std::endl; + return false; + } + return true; + } + bool ProcessServiceCheckResult(Check const& sc) { grpc::ClientContext context; CommandSuccess response; @@ -1795,9 +1984,9 @@ int main(int argc, char** argv) { int type = atoi(argv[5]); bool notify = atoi(argv[6]); bool persistent = atoi(argv[7]); - - status = client.AcknowledgementHostProblem(hostname, ackauthor, - ackdata, type, notify, persistent, &response); + + status = client.AcknowledgementHostProblem( + hostname, ackauthor, ackdata, type, notify, persistent, &response); std::cout << "AcknowledgementHostProblem" << std::endl; } else if (strcmp(argv[1], "AcknowledgementServiceProblem") == 0) { CommandSuccess response; @@ -1809,12 +1998,98 @@ int main(int argc, char** argv) { bool notify = atoi(argv[7]); bool persistent = atoi(argv[8]); - status = client.AcknowledgementServiceProblem(hostname, servicedesc, ackauthor, - ackdata, type, notify, persistent, &response); + status = client.AcknowledgementServiceProblem( + hostname, servicedesc, ackauthor, ackdata, type, notify, persistent, + &response); std::cout << "AcknowledgementServiceProblem" << std::endl; - } + } else if (strcmp(argv[1], "ChangeHostObjectIntVar") == 0) { + CommandSuccess response; + std::string hostname(argv[2]); + uint32_t mode = atoi(argv[3]); + uint32_t intval = atoi(argv[4]); + double dval = atof(argv[5]); + + status = + client.ChangeHostObjectIntVar(hostname, mode, intval, dval, &response); + std::cout << "ChangeHostObjectIntVar" << std::endl; + } else if (strcmp(argv[1], "ChangeServiceObjectIntVar") == 0) { + CommandSuccess response; + std::string hostname(argv[2]); + std::string servicedesc(argv[3]); + uint32_t mode = atoi(argv[4]); + uint32_t intval = atoi(argv[5]); + double dval = atof(argv[6]); + + status = client.ChangeServiceObjectIntVar(hostname, servicedesc, mode, + intval, dval, &response); + std::cout << "ChangeServiceObjectIntVar" << std::endl; + } else if (strcmp(argv[1], "ChangeContactObjectIntVar") == 0) { + CommandSuccess response; + std::string contactname(argv[2]); + uint32_t mode = atoi(argv[3]); + uint32_t intval = atoi(argv[4]); + double dval = atof(argv[5]); + + status = client.ChangeContactObjectIntVar(contactname, mode, intval, dval, + &response); + std::cout << "ChangeContactObjectIntVar" << std::endl; + } else if (strcmp(argv[1], "ChangeHostObjectCustomVar") == 0) { + CommandSuccess response; + std::string hostname(argv[2]); + std::string varname(argv[3]); + std::string varvalue(argv[4]); + + status = client.ChangeHostObjectCustomVar(hostname, varname, varvalue, + &response); + std::cout << "ChangeHostObjectCustomVar" << std::endl; + } else if (strcmp(argv[1], "ChangeServiceObjectCustomVar") == 0) { + CommandSuccess response; + std::string hostname(argv[2]); + std::string servicedesc(argv[3]); + std::string varname(argv[4]); + std::string varvalue(argv[5]); - else { + status = client.ChangeServiceObjectCustomVar(hostname, servicedesc, varname, + varvalue, &response); + std::cout << "ChangeServiceObjectCustomVar" << std::endl; + } else if (strcmp(argv[1], "ChangeContactObjectCustomVar") == 0) { + CommandSuccess response; + std::string contact(argv[2]); + std::string varname(argv[3]); + std::string varvalue(argv[4]); + + 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 bbfa399c195..512c3ad9dd2 100644 --- a/tests/engine/enginerpc/enginerpc.cc +++ b/tests/engine/enginerpc/enginerpc.cc @@ -174,7 +174,6 @@ class EngineRpc : public TestEngine { char path[1024]; std::ostringstream oss; oss << "bin/engine_rpc_client " << command; - FILE* fp = popen(oss.str().c_str(), "r"); while (fgets(path, sizeof(path), fp) != nullptr) { size_t count = strlen(path); @@ -784,7 +783,8 @@ TEST_F(EngineRpc, AcknowledgementHostProblem) { ASSERT_EQ(_host->get_problem_has_been_acknowledged(), false); call_command_manager(th, &condvar, &mutex, &continuerunning); - auto output = execute("AcknowledgementHostProblem test_host admin test 1 0 0"); + auto output = + execute("AcknowledgementHostProblem test_host admin test 1 0 0"); { std::lock_guard lock(mutex); continuerunning = true; @@ -806,8 +806,9 @@ TEST_F(EngineRpc, AcknowledgementServiceProblem) { ASSERT_EQ(_svc->get_problem_has_been_acknowledged(), false); call_command_manager(th, &condvar, &mutex, &continuerunning); - auto output = execute("AcknowledgementServiceProblem test_host test_svc admin test 1 0 0"); -; + auto output = execute( + "AcknowledgementServiceProblem test_host test_svc admin test 1 0 0"); + ; { std::lock_guard lock(mutex); continuerunning = true; @@ -819,6 +820,281 @@ TEST_F(EngineRpc, AcknowledgementServiceProblem) { erpc.shutdown(); } +TEST_F(EngineRpc, ChangeHostObjectIntVar) { + enginerpc erpc("0.0.0.0", 40001); + std::unique_ptr th; + std::condition_variable condvar; + std::mutex mutex; + bool continuerunning = false; + + call_command_manager(th, &condvar, &mutex, &continuerunning); + + auto output = execute("ChangeHostObjectIntVar test_host 0 1 1.0"); + ASSERT_EQ(_host->get_check_interval(), 1); + output = execute("ChangeHostObjectIntVar test_host 1 1 2.0"); + ASSERT_EQ(_host->get_retry_interval(), 2); + output = execute("ChangeHostObjectIntVar test_host 2 1 1.0"); + ASSERT_EQ(_host->get_max_attempts(), 1); + { + std::lock_guard lock(mutex); + continuerunning = true; + } + condvar.notify_one(); + th->join(); + erpc.shutdown(); +} + +TEST_F(EngineRpc, ChangeServiceObjectIntVar) { + enginerpc erpc("0.0.0.0", 40001); + std::unique_ptr th; + std::condition_variable condvar; + std::mutex mutex; + bool continuerunning = false; + + call_command_manager(th, &condvar, &mutex, &continuerunning); + + auto output = execute( + "ChangeServiceObjectIntVar" + " test_host test_svc 0 1 1.0"); + ASSERT_EQ(_svc->get_check_interval(), 1); + output = execute( + "ChangeServiceObjectIntVar" + " test_host test_svc 1 1 2.0"); + ASSERT_EQ(_svc->get_retry_interval(), 2); + output = execute( + "ChangeServiceObjectIntVar" + " test_host test_svc 2 1 1.0"); + ASSERT_EQ(_svc->get_max_attempts(), 1); + { + std::lock_guard lock(mutex); + continuerunning = true; + } + condvar.notify_one(); + th->join(); + + erpc.shutdown(); +} + +TEST_F(EngineRpc, ChangeContactObjectIntVar) { + enginerpc erpc("0.0.0.0", 40001); + std::unique_ptr th; + std::condition_variable condvar; + std::mutex mutex; + bool continuerunning = false; + + call_command_manager(th, &condvar, &mutex, &continuerunning); + + auto output = execute( + "ChangeContactObjectIntVar" + " admin 0 1 1.0"); + ASSERT_EQ(_contact->get_modified_attributes(), 1); + output = execute( + "ChangeContactObjectIntVar" + " admin 1 2 1.0"); + ASSERT_EQ(_contact->get_modified_host_attributes(), 2); + output = execute( + "ChangeContactObjectIntVar" + " admin 2 3 1.0"); + ASSERT_EQ(_contact->get_modified_service_attributes(), 3); + { + std::lock_guard lock(mutex); + continuerunning = true; + } + condvar.notify_one(); + th->join(); + + 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; + std::condition_variable condvar; + std::mutex mutex; + bool continuerunning = false; + + ASSERT_EQ(_host->custom_variables.size(), 0); + call_command_manager(th, &condvar, &mutex, &continuerunning); + auto output = execute( + "ChangeHostObjectCustomVar" + " test_host test_var test_val"); + { + std::lock_guard lock(mutex); + continuerunning = true; + } + condvar.notify_one(); + th->join(); + + ASSERT_EQ(_host->custom_variables.size(), 1u); + ASSERT_EQ(_host->custom_variables["TEST_VAR"].get_value(), "test_val"); + _host->custom_variables.clear(); + ASSERT_EQ(_host->custom_variables.size(), 0); + erpc.shutdown(); +} + +TEST_F(EngineRpc, ChangeServiceObjectCustomVar) { + enginerpc erpc("0.0.0.0", 40001); + std::unique_ptr th; + std::condition_variable condvar; + std::mutex mutex; + bool continuerunning = false; + + _svc->custom_variables.clear(); + ASSERT_EQ(_svc->custom_variables.size(), 0); + call_command_manager(th, &condvar, &mutex, &continuerunning); + auto output = execute( + "ChangeServiceObjectCustomVar" + " test_host test_svc test_var test_val"); + { + std::lock_guard lock(mutex); + continuerunning = true; + } + condvar.notify_one(); + th->join(); + + ASSERT_EQ(_svc->custom_variables.size(), 1u); + ASSERT_EQ(_svc->custom_variables["TEST_VAR"].get_value(), "test_val"); + _svc->custom_variables.clear(); + ASSERT_EQ(_svc->custom_variables.size(), 0); + erpc.shutdown(); +} + +TEST_F(EngineRpc, ChangeContactObjectCustomVar) { + enginerpc erpc("0.0.0.0", 40001); + std::unique_ptr th; + std::condition_variable condvar; + std::mutex mutex; + bool continuerunning = false; + ASSERT_EQ(_contact->get_custom_variables().size(), 0); + + call_command_manager(th, &condvar, &mutex, &continuerunning); + auto output = execute( + "ChangeContactObjectCustomVar" + " admin test_var test_val"); + { + std::lock_guard lock(mutex); + continuerunning = true; + } + condvar.notify_one(); + th->join(); + ASSERT_EQ(_contact->get_custom_variables().size(), 1u); + ASSERT_EQ(_contact->get_custom_variables()["TEST_VAR"].get_value(), + "test_val"); + + erpc.shutdown(); +} TEST_F(EngineRpc, ScheduleHostDowntime) { enginerpc erpc("0.0.0.0", 40001);