-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enh(centengine) : add Acknowledge Problem #54
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
#include <future> | ||
|
||
#include "com/centreon/engine/anomalydetection.hh" | ||
#include "com/centreon/engine/broker.hh" | ||
#include "com/centreon/engine/command_manager.hh" | ||
#include "com/centreon/engine/comment.hh" | ||
#include "com/centreon/engine/common.hh" | ||
|
@@ -133,7 +134,7 @@ grpc::Status engine_impl::GetHost(grpc::ServerContext* context | |
host->set_address(selectedhost->get_address()); | ||
host->set_check_period(selectedhost->get_check_period()); | ||
host->set_current_state( | ||
static_cast<EngineHost::tate>(selectedhost->get_current_state())); | ||
static_cast<EngineHost::State>(selectedhost->get_current_state())); | ||
host->set_id(selectedhost->get_host_id()); | ||
return 0; | ||
}); | ||
|
@@ -792,6 +793,130 @@ grpc::Status engine_impl::RemoveServiceAcknowledgement( | |
return grpc::Status::OK; | ||
} | ||
|
||
grpc::Status engine_impl::AcknowledgementHostProblem( | ||
grpc::ServerContext* context __attribute__((unused)), | ||
const EngineAcknowledgement* request, | ||
CommandSuccess* response) { | ||
auto fn = std::packaged_task<int32_t(void)>([request]() -> int32_t { | ||
std::shared_ptr<engine::host> temp_host; | ||
/* get the host */ | ||
auto it = host::hosts.find(request->host_name()); | ||
if (it != host::hosts.end()) | ||
temp_host = it->second; | ||
if (temp_host == nullptr) | ||
return 1; | ||
/* cannot acknowledge a non-existent problem */ | ||
if (temp_host->get_current_state() == host::state_up) | ||
return 1; | ||
/* set the acknowledgement flag */ | ||
temp_host->set_problem_has_been_acknowledged(true); | ||
/* set the acknowledgement type */ | ||
if (EngineAcknowledgement::Type_Name(request->type()) == "STICKY") | ||
temp_host->set_acknowledgement_type(ACKNOWLEDGEMENT_STICKY); | ||
else | ||
temp_host->set_acknowledgement_type(ACKNOWLEDGEMENT_NORMAL); | ||
/* schedule acknowledgement expiration */ | ||
time_t current_time(time(nullptr)); | ||
temp_host->set_last_acknowledgement(current_time); | ||
temp_host->schedule_acknowledgement_expiration(); | ||
/* send data to event broker */ | ||
char* ack_author = strdup(request->ack_author().c_str()); | ||
char* ack_data = strdup(request->ack_data().c_str()); | ||
broker_acknowledgement_data(NEBTYPE_ACKNOWLEDGEMENT_ADD, NEBFLAG_NONE, | ||
NEBATTR_NONE, HOST_ACKNOWLEDGEMENT, | ||
static_cast<void*>(temp_host.get()), ack_author, | ||
ack_data, request->type(), request->notify(), | ||
request->persistent(), nullptr); | ||
/* send out an acknowledgement notification */ | ||
if (request->notify()) | ||
temp_host->notify(notifier::reason_acknowledgement, ack_author, ack_data, | ||
notifier::notification_option_none); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here, notify() needs a std::string on ack_author and ack_data. |
||
/* update the status log with the host info */ | ||
temp_host->update_status(false); | ||
/* add a comment for the acknowledgement */ | ||
std::shared_ptr<comment> com{new comment( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. std::shared_ptr com{new comment(... should be replaced with |
||
comment::host, comment::acknowledgment, temp_host->get_host_id(), 0, | ||
current_time, ack_author, ack_data, request->persistent(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here again ack_author and ack_data should be std::string... So here is again two new copies of your strings. |
||
comment::internal, false, (time_t)0)}; | ||
comment::comments.insert({com->get_comment_id(), com}); | ||
|
||
delete ack_author; | ||
delete ack_data; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you do not strdup() at the first line, those two lines are no more needed. |
||
return 0; | ||
}); | ||
|
||
std::future<int32_t> result = fn.get_future(); | ||
command_manager::instance().enqueue(std::move(fn)); | ||
|
||
response->set_value(!result.get()); | ||
return grpc::Status::OK; | ||
} | ||
|
||
grpc::Status engine_impl::AcknowledgementServiceProblem( | ||
grpc::ServerContext* context __attribute__((unused)), | ||
const EngineAcknowledgement* request, | ||
CommandSuccess* response) { | ||
auto fn = std::packaged_task<int32_t(void)>([request]() -> int32_t { | ||
std::shared_ptr<engine::service> temp_service; | ||
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; | ||
/* cannot acknowledge a non-existent problem */ | ||
if (temp_service->get_current_state() == service::state_ok) | ||
return 1; | ||
/* set the acknowledgement flag */ | ||
temp_service->set_problem_has_been_acknowledged(true); | ||
/* set the acknowledgement type */ | ||
if (EngineAcknowledgement::Type_Name(request->type()) == "STICKY") | ||
temp_service->set_acknowledgement_type(ACKNOWLEDGEMENT_STICKY); | ||
else | ||
temp_service->set_acknowledgement_type(ACKNOWLEDGEMENT_NORMAL); | ||
/* schedule acknowledgement expiration */ | ||
time_t current_time(time(nullptr)); | ||
temp_service->set_last_acknowledgement(current_time); | ||
temp_service->schedule_acknowledgement_expiration(); | ||
/* send data to event broker */ | ||
char* ack_author = strdup(request->ack_author().c_str()); | ||
char* ack_data = strdup(request->ack_data().c_str()); | ||
|
||
/* send data to event broker */ | ||
broker_acknowledgement_data(NEBTYPE_ACKNOWLEDGEMENT_ADD, NEBFLAG_NONE, | ||
NEBATTR_NONE, SERVICE_ACKNOWLEDGEMENT, | ||
static_cast<void*> (temp_service.get()), | ||
ack_author, ack_data, request->type(), | ||
request->notify(), request->persistent(), | ||
nullptr); | ||
/* send out an acknowledgement notification */ | ||
if (request->notify()) | ||
temp_service->notify(notifier::reason_acknowledgement, ack_author, | ||
ack_data, notifier::notification_option_none); | ||
/* update the status log with the service info */ | ||
temp_service->update_status(false); | ||
|
||
/* add a comment for the acknowledgement */ | ||
std::shared_ptr<comment> com{new comment( | ||
comment::service, comment::acknowledgment, temp_service->get_host_id(), | ||
temp_service->get_service_id(), current_time, ack_author, ack_data, | ||
request->persistent(), comment::internal, false, (time_t)0)}; | ||
comment::comments.insert({com->get_comment_id(), com}); | ||
|
||
delete ack_author; | ||
delete ack_data; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we have exactly the same problem here. |
||
|
||
std::cout << "ahah" << std::endl; | ||
return 0; | ||
}); | ||
|
||
std::future<int32_t> result = fn.get_future(); | ||
command_manager::instance().enqueue(std::move(fn)); | ||
|
||
response->set_value(!result.get()); | ||
return grpc::Status::OK; | ||
} | ||
|
||
/** | ||
* @brief Schedules downtime for a specific host. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,7 +131,7 @@ class EngineRpc : public TestEngine { | |
|
||
host_map const& hm{engine::host::hosts}; | ||
_host = hm.begin()->second; | ||
_host->set_current_state(engine::host::state_up); | ||
_host->set_current_state(engine::host::state_down); | ||
_host->set_state_type(checkable::hard); | ||
_host->set_problem_has_been_acknowledged(false); | ||
_host->set_notify_on(static_cast<uint32_t>(-1)); | ||
|
@@ -144,6 +144,10 @@ class EngineRpc : public TestEngine { | |
else | ||
_svc = svc; | ||
} | ||
_svc->set_current_state(engine::service::state_critical); | ||
_svc->set_state_type(checkable::hard); | ||
_svc->set_problem_has_been_acknowledged(false); | ||
_svc->set_notify_on(static_cast<uint32_t>(-1)); | ||
|
||
contact_map const& cm{engine::contact::contacts}; | ||
_contact = cm.begin()->second; | ||
|
@@ -770,6 +774,55 @@ TEST_F(EngineRpc, RemoveServiceAcknowledgement) { | |
erpc.shutdown(); | ||
} | ||
|
||
TEST_F(EngineRpc, AcknowledgementHostProblem) { | ||
enginerpc erpc("0.0.0.0", 40001); | ||
std::unique_ptr<std::thread> th; | ||
std::condition_variable condvar; | ||
std::mutex mutex; | ||
std::ostringstream oss; | ||
bool continuerunning = false; | ||
|
||
ASSERT_EQ(_host->get_problem_has_been_acknowledged(), false); | ||
oss << "AcknowledgementHostProblem test_host admin test 1 0 0"; | ||
call_command_manager(th, &condvar, &mutex, &continuerunning); | ||
|
||
auto output = execute(oss.str()); | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex); | ||
continuerunning = true; | ||
} | ||
condvar.notify_one(); | ||
th->join(); | ||
|
||
ASSERT_EQ(_host->get_problem_has_been_acknowledged(), true); | ||
erpc.shutdown(); | ||
} | ||
|
||
TEST_F(EngineRpc, AcknowledgementServiceProblem) { | ||
enginerpc erpc("0.0.0.0", 40001); | ||
std::unique_ptr<std::thread> th; | ||
std::condition_variable condvar; | ||
std::mutex mutex; | ||
std::ostringstream oss; | ||
bool continuerunning = false; | ||
|
||
ASSERT_EQ(_svc->get_problem_has_been_acknowledged(), false); | ||
oss << "AcknowledgementServiceProblem test_host test_svc admin test 1 0 0"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer a string instead of an ostringstream... |
||
call_command_manager(th, &condvar, &mutex, &continuerunning); | ||
|
||
auto output = execute(oss.str()); | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex); | ||
continuerunning = true; | ||
} | ||
condvar.notify_one(); | ||
th->join(); | ||
|
||
ASSERT_EQ(_svc->get_problem_has_been_acknowledged(), true); | ||
erpc.shutdown(); | ||
} | ||
|
||
|
||
TEST_F(EngineRpc, ScheduleHostDowntime) { | ||
enginerpc erpc("0.0.0.0", 40001); | ||
std::unique_ptr<std::thread> th; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why a strdup ? Why not just taking the pointer ?