Skip to content

Commit

Permalink
enh(centengine) : add ChangeObjectCharVar into grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpaccount01 committed Jul 31, 2020
1 parent c289ba6 commit e9ce6a2
Show file tree
Hide file tree
Showing 3 changed files with 462 additions and 15 deletions.
275 changes: 260 additions & 15 deletions src/centengine/engine_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <functional>
#include <future>

#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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand All @@ -2312,8 +2306,106 @@ grpc::Status engine_impl::ChangeHostObjectCharVar(
const ChangeObjectChar* request,
CommandSuccess* response) {
auto fn = std::packaged_task<int32_t(void)>([request]() -> int32_t {
if (ChangeObjectChar::Mode_Name(request->mode()) == "") {
std::shared_ptr<engine::host> 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;
});

Expand All @@ -2329,7 +2421,108 @@ grpc::Status engine_impl::ChangeServiceObjectCharVar(
const ChangeObjectChar* request,
CommandSuccess* response) {
auto fn = std::packaged_task<int32_t(void)>([request]() -> int32_t {
if (ChangeObjectChar::Mode_Name(request->mode()) == "") {
std::shared_ptr<engine::service> 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;
});
Expand All @@ -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<int32_t(void)>([request]() -> int32_t { return 0; });
std::packaged_task<int32_t(void)>([request](void) -> int32_t {
std::shared_ptr<engine::contact> 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<int32_t> result = fn.get_future();
command_manager::instance().enqueue(std::move(fn));
Expand Down Expand Up @@ -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;
});

Expand Down Expand Up @@ -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;
});

Expand Down
Loading

0 comments on commit e9ce6a2

Please sign in to comment.