Skip to content

Commit

Permalink
enh(engine): customvariables have their own configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
bouda1 committed Jun 5, 2024
1 parent 8b3022d commit 4a8cb65
Show file tree
Hide file tree
Showing 24 changed files with 132 additions and 134 deletions.
4 changes: 2 additions & 2 deletions broker/neb/src/initial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void send_custom_variables_list(
nscvd.type = NEBTYPE_HOSTCUSTOMVARIABLE_ADD;
nscvd.timestamp.tv_sec = time(nullptr);
nscvd.var_name = const_cast<char*>(name.c_str());
nscvd.var_value = const_cast<char*>(cit->second.get_value().c_str());
nscvd.var_value = const_cast<char*>(cit->second.value().c_str());
nscvd.object_ptr = it->second.get();

// Callback.
Expand All @@ -103,7 +103,7 @@ static void send_custom_variables_list(
nscvd.type = NEBTYPE_SERVICECUSTOMVARIABLE_ADD;
nscvd.timestamp.tv_sec = time(nullptr);
nscvd.var_name = const_cast<char*>(name.c_str());
nscvd.var_value = const_cast<char*>(cit->second.get_value().c_str());
nscvd.var_value = const_cast<char*>(cit->second.value().c_str());
nscvd.object_ptr = it->second.get();

// Callback.
Expand Down
2 changes: 1 addition & 1 deletion engine/inc/com/centreon/engine/configuration/service.hh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class service : public object {
set_string const& contacts() const noexcept;
bool contacts_defined() const noexcept;
map_customvar const& customvariables() const noexcept;
map_customvar& customvariables() noexcept;
map_customvar& mut_customvariables() noexcept;
std::string const& display_name() const noexcept;
std::string const& event_handler() const noexcept;
bool event_handler_enabled() const noexcept;
Expand Down
2 changes: 1 addition & 1 deletion engine/inc/com/centreon/engine/configuration/state.hh
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ class state {
// const set_anomalydetection& anomalydetections() const noexcept;
set_anomalydetection& anomalydetections() noexcept;
set_service const& services() const noexcept;
set_service& services() noexcept;
set_service& mut_services() noexcept;
set_anomalydetection::iterator anomalydetections_find(
anomalydetection::key_type const& k);
set_service::iterator services_find(service::key_type const& k);
Expand Down
4 changes: 2 additions & 2 deletions engine/inc/com/centreon/engine/customvariable.hh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class customvariable {
void set_sent(bool sent);
bool is_sent() const;
void set_value(std::string const& value);
std::string const& get_value() const;
const std::string& value() const;
bool has_been_modified() const;
void update(std::string const& value);

Expand All @@ -55,6 +55,6 @@ class customvariable {

typedef std::unordered_map<std::string, customvariable> map_customvar;

}
} // namespace com::centreon::engine

#endif // !CCE_OBJECTS_CUSTOMVARIABLE_HH
15 changes: 6 additions & 9 deletions engine/src/commands/raw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,8 @@ void raw::_build_custom_contact_macro_environment(nagios_macros& macros,
// Set custom contact variable into the environement
for (auto const& cv : macros.custom_contact_vars) {
if (!cv.first.empty()) {
std::string value(
clean_macro_chars(cv.second.get_value(),
STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string value(clean_macro_chars(
cv.second.value(), STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string line;
line.append(MACRO_ENV_VAR_PREFIX);
line.append(cv.first);
Expand Down Expand Up @@ -429,9 +428,8 @@ void raw::_build_custom_host_macro_environment(nagios_macros& macros,
// Set custom host variable into the environement
for (auto const& cv : macros.custom_host_vars) {
if (!cv.first.empty()) {
std::string value(
clean_macro_chars(cv.second.get_value(),
STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string value(clean_macro_chars(
cv.second.value(), STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string line;
line.append(MACRO_ENV_VAR_PREFIX);
line.append(cv.first);
Expand Down Expand Up @@ -464,9 +462,8 @@ void raw::_build_custom_service_macro_environment(nagios_macros& macros,
// Set custom service variable into the environement
for (auto const& cv : macros.custom_service_vars) {
if (!cv.first.empty()) {
std::string value(
clean_macro_chars(cv.second.get_value(),
STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string value(clean_macro_chars(
cv.second.value(), STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS));
std::string line;
line.append(MACRO_ENV_VAR_PREFIX);
line.append(cv.first);
Expand Down
1 change: 1 addition & 0 deletions engine/src/configuration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(FILES
"${SRC_DIR}/connector.cc"
"${SRC_DIR}/contact.cc"
"${SRC_DIR}/contactgroup.cc"
"${SRC_DIR}/customvariable.cc"
"${SRC_DIR}/extended_conf.cc"
"${SRC_DIR}/group.cc"
"${SRC_DIR}/host.cc"
Expand Down
41 changes: 20 additions & 21 deletions engine/src/configuration/applier/anomalydetection.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2020-2024 Centreon
* Copyright 2020,2023-2024 Centreon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -141,21 +141,22 @@ void applier::anomalydetection::add_object(
ad->mut_contacts().insert({*it, nullptr});

// Add contactgroups.
for (set_string::const_iterator it(obj.contactgroups().begin()),
end(obj.contactgroups().end());
for (set_string::const_iterator it = obj.contactgroups().begin(),
end = obj.contactgroups().end();
it != end; ++it)
ad->get_contactgroups().insert({*it, nullptr});

// Add custom variables.
for (map_customvar::const_iterator it(obj.customvariables().begin()),
end(obj.customvariables().end());
for (auto it = obj.customvariables().begin(),
end = obj.customvariables().end();
it != end; ++it) {
ad->custom_variables[it->first] = it->second;
ad->custom_variables[it->first] =
engine::customvariable(it->second.value(), it->second.is_sent());

if (it->second.is_sent()) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_SERVICECUSTOMVARIABLE_ADD, ad,
it->first.c_str(), it->second.get_value().c_str(),
it->first.c_str(), it->second.value().c_str(),
&tv);
}
}
Expand All @@ -172,21 +173,22 @@ void applier::anomalydetection::add_object(
*/
void applier::anomalydetection::expand_objects(configuration::state& s) {
// Browse all anomalydetections.
for (configuration::set_anomalydetection::iterator
it_ad = s.anomalydetections().begin(),
end_ad = s.anomalydetections().end();
it_ad != end_ad; ++it_ad) {
configuration::set_anomalydetection new_ads;
// In a set, we cannot change items as it would change their order. So we
// create a new set and replace the old one with the new one at the end.
for (auto ad : s.anomalydetections()) {
// Should custom variables be sent to broker ?
for (map_customvar::iterator
it = const_cast<map_customvar&>(it_ad->customvariables()).begin(),
end = const_cast<map_customvar&>(it_ad->customvariables()).end();
for (auto it = ad.customvariables().begin(),
end = ad.customvariables().end();
it != end; ++it) {
if (!s.enable_macros_filter() ||
s.macros_filter().find(it->first) != s.macros_filter().end()) {
it->second.set_sent(true);
}
}
new_ads.insert(std::move(ad));
}
s.anomalydetections() = new_ads;
}

/**
Expand Down Expand Up @@ -373,20 +375,19 @@ void applier::anomalydetection::modify_object(
if (c.second.is_sent()) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_SERVICECUSTOMVARIABLE_DELETE, s.get(),
c.first.c_str(), c.second.get_value().c_str(),
&tv);
c.first.c_str(), c.second.value().c_str(), &tv);
}
}
s->custom_variables.clear();

for (auto& c : obj.customvariables()) {
s->custom_variables[c.first] = c.second;
s->custom_variables[c.first] =
engine::customvariable(c.second.value(), c.second.is_sent());

if (c.second.is_sent()) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_SERVICECUSTOMVARIABLE_ADD, s.get(),
c.first.c_str(), c.second.get_value().c_str(),
&tv);
c.first.c_str(), c.second.value().c_str(), &tv);
}
}
}
Expand Down Expand Up @@ -524,8 +525,6 @@ void applier::anomalydetection::_expand_service_memberships(
// Reinsert anomalydetection group.
s.servicegroups().insert(backup);
}

return;
}

/**
Expand Down
22 changes: 13 additions & 9 deletions engine/src/configuration/applier/contact.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,17 @@ void applier::contact::add_object(configuration::contact const& obj) {
engine::contact::contacts.insert({c->get_name(), c});

// Add all custom variables.
for (map_customvar::const_iterator it(obj.customvariables().begin()),
end(obj.customvariables().end());
for (auto it = obj.customvariables().begin(),
end = obj.customvariables().end();
it != end; ++it) {
c->get_custom_variables()[it->first] = it->second;
auto& cv = c->get_custom_variables()[it->first];
cv.set_value(it->second.value());
cv.set_sent(it->second.is_sent());

if (it->second.is_sent()) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_CONTACTCUSTOMVARIABLE_ADD, c.get(),
it->first.c_str(), it->second.get_value().c_str(),
it->first.c_str(), it->second.value().c_str(),
&tv);
}
}
Expand Down Expand Up @@ -298,20 +300,22 @@ void applier::contact::modify_object(configuration::contact const& obj) {
if (cus.second.is_sent()) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_CONTACTCUSTOMVARIABLE_DELETE, c,
cus.first.c_str(),
cus.second.get_value().c_str(), &tv);
cus.first.c_str(), cus.second.value().c_str(),
&tv);
}
}
c->get_custom_variables().clear();

for (auto& cus : obj.customvariables()) {
c->get_custom_variables()[cus.first] = cus.second;
auto& cv = c->get_custom_variables()[cus.first];
cv.set_value(cus.second.value());
cv.set_sent(cus.second.is_sent());

if (cus.second.is_sent()) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_CONTACTCUSTOMVARIABLE_ADD, c,
cus.first.c_str(),
cus.second.get_value().c_str(), &tv);
cus.first.c_str(), cus.second.value().c_str(),
&tv);
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions engine/src/configuration/applier/host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,16 @@ void applier::host::add_object(configuration::host const& obj) {
h->get_contactgroups().insert({*it, nullptr});

// Custom variables.
for (map_customvar::const_iterator it(obj.customvariables().begin()),
end(obj.customvariables().end());
for (auto it = obj.customvariables().begin(),
end = obj.customvariables().end();
it != end; ++it) {
h->custom_variables[it->first] = it->second;
h->custom_variables[it->first] =
engine::customvariable(it->second.value(), it->second.is_sent());

if (it->second.is_sent()) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_HOSTCUSTOMVARIABLE_ADD, h.get(),
it->first.c_str(), it->second.get_value().c_str(),
it->first.c_str(), it->second.value().c_str(),
&tv);
}
}
Expand Down Expand Up @@ -381,19 +382,20 @@ void applier::host::modify_object(configuration::host const& obj) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_HOSTCUSTOMVARIABLE_DELETE,
it_obj->second.get(), c.first.c_str(),
c.second.get_value().c_str(), &tv);
c.second.value().c_str(), &tv);
}
}
it_obj->second->custom_variables.clear();

for (auto& c : obj.customvariables()) {
it_obj->second->custom_variables[c.first] = c.second;
it_obj->second->custom_variables[c.first] =
engine::customvariable(c.second.value(), c.second.is_sent());

if (c.second.is_sent()) {
timeval tv(get_broker_timestamp(nullptr));
broker_custom_variable(NEBTYPE_HOSTCUSTOMVARIABLE_ADD,
it_obj->second.get(), c.first.c_str(),
c.second.get_value().c_str(), &tv);
c.second.value().c_str(), &tv);
}
}
}
Expand Down
Loading

1 comment on commit 4a8cb65

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
22 2 0 24 91.67 20m13.754387s

Failed Tests

Name Message ⏱️ Duration Suite
not16 The notification is not sent for service1 117.997 s Notifications
not17 The down notification of host_3 is sent dependency not working 115.974 s Notifications

Please sign in to comment.