diff --git a/broker/neb/src/initial.cc b/broker/neb/src/initial.cc index 887141cd7a3..06cb56c2213 100644 --- a/broker/neb/src/initial.cc +++ b/broker/neb/src/initial.cc @@ -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(name.c_str()); - nscvd.var_value = const_cast(cit->second.get_value().c_str()); + nscvd.var_value = const_cast(cit->second.value().c_str()); nscvd.object_ptr = it->second.get(); // Callback. @@ -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(name.c_str()); - nscvd.var_value = const_cast(cit->second.get_value().c_str()); + nscvd.var_value = const_cast(cit->second.value().c_str()); nscvd.object_ptr = it->second.get(); // Callback. diff --git a/engine/inc/com/centreon/engine/configuration/anomalydetection.hh b/engine/inc/com/centreon/engine/configuration/anomalydetection.hh index f36255c6cdc..87df2042b8c 100644 --- a/engine/inc/com/centreon/engine/configuration/anomalydetection.hh +++ b/engine/inc/com/centreon/engine/configuration/anomalydetection.hh @@ -1,5 +1,5 @@ /* -** Copyright 2011-2013,2015-2017, 2022 Centreon +** Copyright 2011-2013,2015-2017, 2022-2023 Centreon ** ** This file is part of Centreon Engine. ** @@ -21,9 +21,9 @@ #define CCE_CONFIGURATION_ANOMALYDETECTION_HH #include "com/centreon/engine/common.hh" +#include "com/centreon/engine/configuration/customvariable.hh" #include "com/centreon/engine/configuration/group.hh" #include "com/centreon/engine/configuration/object.hh" -#include "com/centreon/engine/customvariable.hh" #include "com/centreon/engine/opt.hh" namespace com::centreon::engine { @@ -69,8 +69,9 @@ class anomalydetection : public object { set_string& contacts() noexcept; set_string const& contacts() const noexcept; bool contacts_defined() const noexcept; - map_customvar const& customvariables() const noexcept; - map_customvar& customvariables() noexcept; + const std::unordered_map& customvariables() + const noexcept; + std::unordered_map& customvariables() noexcept; std::string const& display_name() const noexcept; std::string const& event_handler() const noexcept; bool event_handler_enabled() const noexcept; @@ -190,7 +191,7 @@ class anomalydetection : public object { opt _check_interval; group _contactgroups; group _contacts; - map_customvar _customvariables; + std::unordered_map _customvariables; std::string _display_name; std::string _event_handler; opt _event_handler_enabled; diff --git a/engine/inc/com/centreon/engine/configuration/contact.hh b/engine/inc/com/centreon/engine/configuration/contact.hh index de028c053a1..56b1df50155 100644 --- a/engine/inc/com/centreon/engine/configuration/contact.hh +++ b/engine/inc/com/centreon/engine/configuration/contact.hh @@ -22,9 +22,9 @@ #include +#include "com/centreon/engine/configuration/customvariable.hh" #include "com/centreon/engine/configuration/group.hh" #include "com/centreon/engine/configuration/object.hh" -#include "com/centreon/engine/customvariable.hh" #include "com/centreon/engine/opt.hh" typedef std::vector tab_string; diff --git a/engine/inc/com/centreon/engine/configuration/customvariable.hh b/engine/inc/com/centreon/engine/configuration/customvariable.hh new file mode 100644 index 00000000000..337c1830bf3 --- /dev/null +++ b/engine/inc/com/centreon/engine/configuration/customvariable.hh @@ -0,0 +1,48 @@ +/* + * Copyright 2023 Centreon (https://www.centreon.com/) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * For more information : contact@centreon.com + * + */ +#ifndef CCE_CONFIGURATION_CUSTOMVARIABLE_HH +#define CCE_CONFIGURATION_CUSTOMVARIABLE_HH +#include + +namespace com::centreon::engine::configuration { +/** + * @class customvariable customvariable.hh + * "com/centreon/engine/configuration/customvariable.hh" + * @brief This class represents a customvariable configuration. + * + * It is lighter than the engine::customvariable class and also it is + * separated from the engine core because the configuration module must be + * loadable from cbd. + */ +class customvariable { + std::string _value; + bool _is_sent; + + public: + customvariable(std::string const& value = "", bool is_sent = false); + bool operator==(const customvariable& other) const; + ~customvariable() noexcept = default; + void set_sent(bool sent); + bool is_sent() const; + void set_value(const std::string& value); + const std::string& value() const; +}; +} // namespace com::centreon::engine::configuration + +#endif /* !CCE_CONFIGURATION_CUSTOMVARIABLE_HH */ diff --git a/engine/inc/com/centreon/engine/configuration/host.hh b/engine/inc/com/centreon/engine/configuration/host.hh index 1db64c940a8..6bb32c67dcb 100644 --- a/engine/inc/com/centreon/engine/configuration/host.hh +++ b/engine/inc/com/centreon/engine/configuration/host.hh @@ -21,11 +21,11 @@ #define CCE_CONFIGURATION_HOST_HH #include "com/centreon/engine/common.hh" +#include "com/centreon/engine/configuration/customvariable.hh" #include "com/centreon/engine/configuration/group.hh" #include "com/centreon/engine/configuration/object.hh" #include "com/centreon/engine/configuration/point_2d.hh" #include "com/centreon/engine/configuration/point_3d.hh" -#include "com/centreon/engine/customvariable.hh" #include "com/centreon/engine/opt.hh" namespace com::centreon::engine { @@ -70,7 +70,8 @@ class host : public object { point_3d const& coords_3d() const noexcept; const std::unordered_map& customvariables() const noexcept; - std::unordered_map& customvariables() noexcept; + std::unordered_map& + mut_customvariables() noexcept; std::string const& display_name() const noexcept; std::string const& event_handler() const noexcept; bool event_handler_enabled() const noexcept; diff --git a/engine/inc/com/centreon/engine/configuration/service.hh b/engine/inc/com/centreon/engine/configuration/service.hh index 49fc7bd5853..5831b2efa12 100644 --- a/engine/inc/com/centreon/engine/configuration/service.hh +++ b/engine/inc/com/centreon/engine/configuration/service.hh @@ -1,34 +1,32 @@ -/* -** Copyright 2011-2013,2015-2017-2022 Centreon -** -** This file is part of Centreon Engine. -** -** Centreon Engine is free software: you can redistribute it and/or -** modify it under the terms of the GNU General Public License version 2 -** as published by the Free Software Foundation. -** -** Centreon Engine is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -** General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with Centreon Engine. If not, see -** . -*/ +/** + * Copyright 2011-2013,2015-2017-2022 Centreon + * + * This file is part of Centreon Engine. + * + * Centreon Engine is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * Centreon Engine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Centreon Engine. If not, see + * . + */ #ifndef CCE_CONFIGURATION_SERVICE_HH #define CCE_CONFIGURATION_SERVICE_HH #include "com/centreon/engine/common.hh" +#include "com/centreon/engine/configuration/customvariable.hh" #include "com/centreon/engine/configuration/group.hh" #include "com/centreon/engine/configuration/object.hh" -#include "com/centreon/engine/customvariable.hh" #include "com/centreon/engine/opt.hh" -namespace com::centreon::engine { - -namespace configuration { +namespace com::centreon::engine::configuration { class service : public object { public: @@ -68,8 +66,10 @@ class service : public object { set_string& contacts() noexcept; set_string const& contacts() const noexcept; bool contacts_defined() const noexcept; - map_customvar const& customvariables() const noexcept; - map_customvar& customvariables() noexcept; + const std::unordered_map& customvariables() + const noexcept; + std::unordered_map& + mut_customvariables() noexcept; std::string const& display_name() const noexcept; std::string const& event_handler() const noexcept; bool event_handler_enabled() const noexcept; @@ -185,7 +185,7 @@ class service : public object { std::string _check_period; group _contactgroups; group _contacts; - map_customvar _customvariables; + std::unordered_map _customvariables; std::string _display_name; std::string _event_handler; opt _event_handler_enabled; @@ -231,8 +231,7 @@ typedef std::list list_service; typedef std::set set_service; typedef std::unordered_map, service_ptr> map_service; -} // namespace configuration -} // namespace com::centreon::engine +} // namespace com::centreon::engine::configuration #endif // !CCE_CONFIGURATION_SERVICE_HH diff --git a/engine/inc/com/centreon/engine/customvariable.hh b/engine/inc/com/centreon/engine/customvariable.hh index cf1ad73e40a..3b2e7fefc2e 100644 --- a/engine/inc/com/centreon/engine/customvariable.hh +++ b/engine/inc/com/centreon/engine/customvariable.hh @@ -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); @@ -55,6 +55,6 @@ class customvariable { typedef std::unordered_map map_customvar; -} +} // namespace com::centreon::engine #endif // !CCE_OBJECTS_CUSTOMVARIABLE_HH diff --git a/engine/src/anomalydetection.cc b/engine/src/anomalydetection.cc index 08dcaadcd50..8fa79965660 100644 --- a/engine/src/anomalydetection.cc +++ b/engine/src/anomalydetection.cc @@ -1095,8 +1095,6 @@ bool anomalydetection::parse_perfdata(std::string const& perfdata, /* We should master this string, so no need to check if it is utf-8 */ calculated_result.set_output(oss.str()); - timestamp now(timestamp::now()); - // Update check result. timeval tv; gettimeofday(&tv, nullptr); diff --git a/engine/src/commands/raw.cc b/engine/src/commands/raw.cc index 50e195545a1..21774f804d9 100644 --- a/engine/src/commands/raw.cc +++ b/engine/src/commands/raw.cc @@ -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); @@ -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); @@ -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); diff --git a/engine/src/configuration/CMakeLists.txt b/engine/src/configuration/CMakeLists.txt index b0bd8910f26..ff0c4fcd7a0 100644 --- a/engine/src/configuration/CMakeLists.txt +++ b/engine/src/configuration/CMakeLists.txt @@ -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" diff --git a/engine/src/configuration/anomalydetection.cc b/engine/src/configuration/anomalydetection.cc index f6598b379e2..cd59aadc1ba 100644 --- a/engine/src/configuration/anomalydetection.cc +++ b/engine/src/configuration/anomalydetection.cc @@ -1012,8 +1012,8 @@ bool anomalydetection::contacts_defined() const noexcept { * * @return The customvariables. */ -com::centreon::engine::map_customvar const& anomalydetection::customvariables() - const noexcept { +const std::unordered_map& +anomalydetection::customvariables() const noexcept { return _customvariables; } @@ -1022,7 +1022,7 @@ com::centreon::engine::map_customvar const& anomalydetection::customvariables() * * @return The customvariables. */ -com::centreon::engine::map_customvar& +std::unordered_map& anomalydetection::customvariables() noexcept { return _customvariables; } diff --git a/engine/src/configuration/applier/anomalydetection.cc b/engine/src/configuration/applier/anomalydetection.cc index 42153fbb4f9..fd5be1e8ca0 100644 --- a/engine/src/configuration/applier/anomalydetection.cc +++ b/engine/src/configuration/applier/anomalydetection.cc @@ -147,12 +147,13 @@ void applier::anomalydetection::add_object( 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); } } @@ -368,20 +369,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); } } } diff --git a/engine/src/configuration/applier/contact.cc b/engine/src/configuration/applier/contact.cc index 48b716dcaae..8a4289c5036 100644 --- a/engine/src/configuration/applier/contact.cc +++ b/engine/src/configuration/applier/contact.cc @@ -105,12 +105,14 @@ void applier::contact::add_object(configuration::contact const& obj) { 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); } } @@ -250,20 +252,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); } } } diff --git a/engine/src/configuration/applier/host.cc b/engine/src/configuration/applier/host.cc index 4a8ff8c752e..6577915df86 100644 --- a/engine/src/configuration/applier/host.cc +++ b/engine/src/configuration/applier/host.cc @@ -120,15 +120,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); } } @@ -170,53 +171,6 @@ void applier::host::add_object(configuration::host const& obj) { h.get(), MODATTR_ALL); } -/** - * @brief Expand a host. - * - * During expansion, the host will be added to its host groups. These - * will be modified in the state. - * - * @param[int,out] s Configuration state. - */ -void applier::host::expand_objects(configuration::state& s) { - // Browse all hosts. - for (auto& host_cfg : s.hosts()) { - // Should custom variables be sent to broker ? - for (map_customvar::iterator - it(const_cast(host_cfg.customvariables()).begin()), - end(const_cast(host_cfg.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); - } - } - - // Browse current host's groups. - for (set_string::const_iterator it_group(host_cfg.hostgroups().begin()), - end_group(host_cfg.hostgroups().end()); - it_group != end_group; ++it_group) { - // Find host group. - configuration::set_hostgroup::iterator group( - s.hostgroups_find(*it_group)); - if (group == s.hostgroups().end()) - throw(engine_error() - << "Could not add host '" << host_cfg.host_name() - << "' to non-existing host group '" << *it_group << "'"); - - // Remove host group from state. - configuration::hostgroup backup(*group); - s.hostgroups().erase(group); - - // Add host to group members. - backup.members().insert(host_cfg.host_name()); - - // Reinsert host group. - s.hostgroups().insert(backup); - } - } -} - /** * Modified host. * @@ -382,19 +336,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); } } } @@ -541,3 +496,52 @@ void applier::host::resolve_object(configuration::host const& obj) { // Resolve host. it->second->resolve(config_warnings, config_errors); } + +/** + * @brief Expand a host. + * + * During expansion, the host will be added to its host groups. These + * will be modified in the state. + * + * @param[int,out] s Configuration state. + */ +void applier::host::expand_objects(configuration::state& s) { + // Browse all hosts. + set_host new_hosts; + for (auto host_cfg : s.hosts()) { + // Should custom variables be sent to broker ? + for (auto it = host_cfg.mut_customvariables().begin(), + end = host_cfg.mut_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); + } + } + + // Browse current host's groups. + for (set_string::const_iterator it_group(host_cfg.hostgroups().begin()), + end_group(host_cfg.hostgroups().end()); + it_group != end_group; ++it_group) { + // Find host group. + configuration::set_hostgroup::iterator group( + s.hostgroups_find(*it_group)); + if (group == s.hostgroups().end()) + throw(engine_error() + << "Could not add host '" << host_cfg.host_name() + << "' to non-existing host group '" << *it_group << "'"); + + // Remove host group from state. + configuration::hostgroup backup(*group); + s.hostgroups().erase(group); + + // Add host to group members. + backup.members().insert(host_cfg.host_name()); + + // Reinsert host group. + s.hostgroups().insert(backup); + } + new_hosts.insert(host_cfg); + } + s.hosts() = std::move(new_hosts); +} diff --git a/engine/src/configuration/applier/service.cc b/engine/src/configuration/applier/service.cc index 3914fcb85ac..f7e7ff1fca6 100644 --- a/engine/src/configuration/applier/service.cc +++ b/engine/src/configuration/applier/service.cc @@ -186,15 +186,16 @@ void applier::service::add_object(configuration::service const& obj) { svc->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) { - svc->custom_variables[it->first] = it->second; + svc->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, svc, - it->first.c_str(), it->second.get_value().c_str(), + it->first.c_str(), it->second.value().c_str(), &tv); } } @@ -239,13 +240,10 @@ void applier::service::add_object(configuration::service const& obj) { void applier::service::expand_objects(configuration::state& s) { // Browse all services. configuration::set_service expanded; - for (configuration::set_service::iterator it_svc(s.services().begin()), - end_svc(s.services().end()); - it_svc != end_svc; ++it_svc) { + for (auto svc_cfg : s.services()) { // Should custom variables be sent to broker ? - for (map_customvar::iterator - it(const_cast(it_svc->customvariables()).begin()), - end(const_cast(it_svc->customvariables()).end()); + for (auto it = svc_cfg.mut_customvariables().begin(), + end = svc_cfg.mut_customvariables().end(); it != end; ++it) { if (!s.enable_macros_filter() || s.macros_filter().find(it->first) != s.macros_filter().end()) { @@ -257,24 +255,24 @@ void applier::service::expand_objects(configuration::state& s) { std::set target_hosts; // Hosts members. - target_hosts = it_svc->hosts(); + target_hosts = svc_cfg.hosts(); // Host group members. - for (set_string::const_iterator it(it_svc->hostgroups().begin()), - end(it_svc->hostgroups().end()); + for (set_string::const_iterator it(svc_cfg.hostgroups().begin()), + end(svc_cfg.hostgroups().end()); it != end; ++it) { // Find host group. set_hostgroup::iterator it2(s.hostgroups_find(*it)); if (it2 == s.hostgroups().end()) throw(engine_error() << "Could not find host group '" << *it << "' on which to apply service '" - << it_svc->service_description() << "'"); + << svc_cfg.service_description() << "'"); // Check host group and user configuration. if (it2->members().empty() && !s.allow_empty_hostgroup_assignment()) throw(engine_error() << "Could not expand host group '" << *it << "' specified in service '" - << it_svc->service_description() << "'"); + << svc_cfg.service_description() << "'"); // Add host group members. target_hosts.insert(it2->members().begin(), it2->members().end()); @@ -285,7 +283,7 @@ void applier::service::expand_objects(configuration::state& s) { end(target_hosts.end()); it != end; ++it) { // Create service instance. - configuration::service svc(*it_svc); + configuration::service svc(svc_cfg); svc.hostgroups().clear(); svc.hosts().clear(); svc.hosts().insert(*it); @@ -483,20 +481,19 @@ void applier::service::modify_object(configuration::service const& obj) { 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); } } } diff --git a/engine/src/configuration/customvariable.cc b/engine/src/configuration/customvariable.cc new file mode 100644 index 00000000000..d41dd32df75 --- /dev/null +++ b/engine/src/configuration/customvariable.cc @@ -0,0 +1,56 @@ +/* + * Copyright 2023 Centreon (https://www.centreon.com/) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * For more information : contact@centreon.com + * + */ + +#include "com/centreon/engine/configuration/customvariable.hh" + +using namespace com::centreon::engine::configuration; + +customvariable::customvariable(const std::string& value, bool is_sent) + : _value{value}, _is_sent{is_sent} {} + +// customvariable::customvariable(customvariable const& other) +// : _value{other._value}, _is_sent{other._is_sent} {} + +// customvariable& customvariable::operator=(const customvariable& other) { +// if (this != &other) { +// _value = other.value; +// _is_sent = other._is_sent; +// } +// return *this; +// } + +void customvariable::set_sent(bool sent) { + _is_sent = sent; +} + +bool customvariable::is_sent() const { + return _is_sent; +} + +void customvariable::set_value(const std::string& value) { + _value = value; +} + +const std::string& customvariable::value() const { + return _value; +} + +bool customvariable::operator==(const customvariable& other) const { + return _value == other._value && _is_sent == other._is_sent; +} diff --git a/engine/src/configuration/host.cc b/engine/src/configuration/host.cc index 2cd983842a6..ed7864a5547 100644 --- a/engine/src/configuration/host.cc +++ b/engine/src/configuration/host.cc @@ -658,8 +658,8 @@ point_3d const& host::coords_3d() const noexcept { * * @return The customvariables. */ -const std::unordered_map& -host::customvariables() const noexcept { +const std::unordered_map& host::customvariables() + const noexcept { return _customvariables; } @@ -668,8 +668,8 @@ host::customvariables() const noexcept { * * @return The customvariables. */ -std::unordered_map& -host::customvariables() noexcept { +std::unordered_map& +host::mut_customvariables() noexcept { return _customvariables; } diff --git a/engine/src/configuration/service.cc b/engine/src/configuration/service.cc index e9d8ebabe6c..769d7025e00 100644 --- a/engine/src/configuration/service.cc +++ b/engine/src/configuration/service.cc @@ -937,7 +937,7 @@ bool service::contacts_defined() const noexcept { * * @return The customvariables. */ -const std::unordered_map& +const std::unordered_map& service::customvariables() const noexcept { return _customvariables; } @@ -947,8 +947,8 @@ service::customvariables() const noexcept { * * @return The customvariables. */ -std::unordered_map& -service::customvariables() noexcept { +std::unordered_map& +service::mut_customvariables() noexcept { return _customvariables; } diff --git a/engine/src/customvariable.cc b/engine/src/customvariable.cc index 99be377b54f..077a437048d 100644 --- a/engine/src/customvariable.cc +++ b/engine/src/customvariable.cc @@ -83,7 +83,7 @@ bool customvariable::is_sent() const { * * @return The value of the customvariable */ -std::string const& customvariable::get_value() const { +const std::string& customvariable::value() const { return _value; } diff --git a/engine/src/macros.cc b/engine/src/macros.cc index 451c3756464..673b76b8856 100644 --- a/engine/src/macros.cc +++ b/engine/src/macros.cc @@ -644,7 +644,7 @@ int grab_custom_object_macro_r(nagios_macros* mac, /* get the custom variable */ for (auto const& cv : vars) { if (macro_name == cv.first) { - output = cv.second.get_value(); + output = cv.second.value(); result = OK; break; } diff --git a/engine/src/retention/applier/contact.cc b/engine/src/retention/applier/contact.cc index 69f9c1fbca3..5ffce899e14 100644 --- a/engine/src/retention/applier/contact.cc +++ b/engine/src/retention/applier/contact.cc @@ -1,21 +1,21 @@ /** -* Copyright 2011-2013,2016 Merethis -* -* This file is part of Centreon Engine. -* -* Centreon Engine is free software: you can redistribute it and/or -* modify it under the terms of the GNU General Public License version 2 -* as published by the Free Software Foundation. -* -* Centreon Engine is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Centreon Engine. If not, see -* . -*/ + * Copyright 2011-2013,2016 Merethis + * + * This file is part of Centreon Engine. + * + * Centreon Engine is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * Centreon Engine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Centreon Engine. If not, see + * . + */ #include "com/centreon/engine/retention/applier/contact.hh" #include "com/centreon/engine/configuration/applier/state.hh" @@ -129,7 +129,7 @@ void applier::contact::_update(configuration::state const& config, if (!state.customvariables().empty() && (obj->get_modified_attributes() & MODATTR_CUSTOM_VARIABLE)) { for (auto const& cv : state.customvariables()) { - obj->get_custom_variables()[cv.first].update(cv.second.get_value()); + obj->get_custom_variables()[cv.first].update(cv.second.value()); } } } diff --git a/engine/src/retention/applier/host.cc b/engine/src/retention/applier/host.cc index c4c3057d216..c1f6e94f6a9 100644 --- a/engine/src/retention/applier/host.cc +++ b/engine/src/retention/applier/host.cc @@ -1,21 +1,21 @@ /** -* Copyright 2011-2013,2015-2016,2022 Centreon -* -* This file is part of Centreon Engine. -* -* Centreon Engine is free software: you can redistribute it and/or -* modify it under the terms of the GNU General Public License version 2 -* as published by the Free Software Foundation. -* -* Centreon Engine is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Centreon Engine. If not, see -* . -*/ + * Copyright 2011-2013,2015-2016,2022 Centreon + * + * This file is part of Centreon Engine. + * + * Centreon Engine is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * Centreon Engine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Centreon Engine. If not, see + * . + */ #include "com/centreon/engine/retention/applier/host.hh" #include "com/centreon/engine/configuration/applier/state.hh" @@ -255,7 +255,7 @@ void applier::host::_update(configuration::state const& config, for (map_customvar::const_iterator it(state.customvariables().begin()), end(state.customvariables().end()); it != end; ++it) - obj.custom_variables[it->first].update(it->second.get_value()); + obj.custom_variables[it->first].update(it->second.value()); } } // Adjust modified attributes if necessary. diff --git a/engine/src/retention/applier/service.cc b/engine/src/retention/applier/service.cc index d8e060eec3b..13783948e2d 100644 --- a/engine/src/retention/applier/service.cc +++ b/engine/src/retention/applier/service.cc @@ -264,7 +264,7 @@ void applier::service::update(configuration::state const& config, for (map_customvar::const_iterator it(state.customvariables().begin()), end(state.customvariables().end()); it != end; ++it) - obj.custom_variables[it->first].update(it->second.get_value()); + obj.custom_variables[it->first].update(it->second.value()); } } // Adjust modified attributes if necessary. diff --git a/engine/src/retention/dump.cc b/engine/src/retention/dump.cc index f619c857937..cc129497c71 100644 --- a/engine/src/retention/dump.cc +++ b/engine/src/retention/dump.cc @@ -192,7 +192,7 @@ std::ostream& dump::customvariables(std::ostream& os, map_customvar const& obj) { for (auto const& cv : obj) os << "_" << cv.first << "=" << cv.second.has_been_modified() << "," - << cv.second.get_value() << "\n"; + << cv.second.value() << "\n"; return os; } diff --git a/engine/src/xsddefault.cc b/engine/src/xsddefault.cc index 9ae791a047d..93a219b608d 100644 --- a/engine/src/xsddefault.cc +++ b/engine/src/xsddefault.cc @@ -449,7 +449,7 @@ int xsddefault_save_status_data() { for (auto const& cv : it->second->custom_variables) { if (!cv.first.empty()) stream << "\t_" << cv.first << "=" << cv.second.has_been_modified() - << ";" << cv.second.get_value() << "\n"; + << ";" << cv.second.value() << "\n"; } stream << "\t}\n\n"; } @@ -625,7 +625,7 @@ int xsddefault_save_status_data() { for (auto const& cv : it->second->custom_variables) { if (!cv.first.empty()) stream << "\t_" << cv.first << "=" << cv.second.has_been_modified() - << ";" << cv.second.get_value() << "\n"; + << ";" << cv.second.value() << "\n"; } stream << "\t}\n\n"; } @@ -669,7 +669,7 @@ int xsddefault_save_status_data() { for (auto const& cv : cntct->get_custom_variables()) { if (!cv.first.empty()) stream << "\t_" << cv.first << "=" << cv.second.has_been_modified() - << ";" << cv.second.get_value() << "\n"; + << ";" << cv.second.value() << "\n"; } stream << "\t}\n\n"; } diff --git a/engine/tests/configuration/applier/applier-contact.cc b/engine/tests/configuration/applier/applier-contact.cc index d55dc5ab9ad..cf7752674be 100644 --- a/engine/tests/configuration/applier/applier-contact.cc +++ b/engine/tests/configuration/applier/applier-contact.cc @@ -133,8 +133,7 @@ TEST_F(ApplierContact, ModifyContactFromConfig) { ASSERT_TRUE(ctct.parse("service_notification_commands", "svc1,svc2")); ASSERT_TRUE(ctct.parse("_superVar", "superValue")); ASSERT_TRUE(ctct.customvariables().size() == 1); - ASSERT_TRUE(ctct.customvariables().at("superVar").get_value() == - "superValue"); + ASSERT_TRUE(ctct.customvariables().at("superVar").value() == "superValue"); configuration::applier::command cmd_aply; configuration::applier::connector cnn_aply; @@ -159,9 +158,9 @@ TEST_F(ApplierContact, ModifyContactFromConfig) { contact_map::const_iterator ct_it{engine::contact::contacts.find("test")}; ASSERT_TRUE(ct_it != engine::contact::contacts.end()); ASSERT_EQ(ct_it->second->get_custom_variables().size(), 2u); - ASSERT_TRUE(ct_it->second->get_custom_variables()["superVar"].get_value() == + ASSERT_TRUE(ct_it->second->get_custom_variables()["superVar"].value() == "Super"); - ASSERT_TRUE(ct_it->second->get_custom_variables()["superVar1"].get_value() == + ASSERT_TRUE(ct_it->second->get_custom_variables()["superVar1"].value() == "Super1"); ASSERT_TRUE(ct_it->second->get_alias() == "newAlias"); ASSERT_FALSE(ct_it->second->notify_on(notifier::service_notification, diff --git a/engine/tests/configuration/applier/applier-state.cc b/engine/tests/configuration/applier/applier-state.cc index cd6feb66e0a..cf5251b391a 100644 --- a/engine/tests/configuration/applier/applier-state.cc +++ b/engine/tests/configuration/applier/applier-state.cc @@ -891,7 +891,7 @@ TEST_F(ApplierState, StateLegacyParsing) { ASSERT_EQ(adit->dependent_service_id(), 1); ASSERT_TRUE(adit->metric_name() == "metric2"); ASSERT_EQ(adit->customvariables().size(), 1); - ASSERT_EQ(adit->customvariables().at("ad_cv").get_value(), + ASSERT_EQ(adit->customvariables().at("ad_cv").value(), std::string("this_is_a_test")); ASSERT_EQ(adit->contactgroups().size(), 2); ASSERT_EQ(adit->contacts().size(), 1); diff --git a/engine/tests/enginerpc/enginerpc.cc b/engine/tests/enginerpc/enginerpc.cc index 762e1886436..2cc77c5d18e 100644 --- a/engine/tests/enginerpc/enginerpc.cc +++ b/engine/tests/enginerpc/enginerpc.cc @@ -1688,7 +1688,7 @@ TEST_F(EngineRpc, ChangeHostObjectCustomVar) { th->join(); ASSERT_EQ(_host->custom_variables.size(), 1u); - ASSERT_EQ(_host->custom_variables["TEST_VAR"].get_value(), "test_val"); + ASSERT_EQ(_host->custom_variables["TEST_VAR"].value(), "test_val"); _host->custom_variables.clear(); ASSERT_EQ(_host->custom_variables.size(), 0u); erpc.shutdown(); @@ -1715,7 +1715,7 @@ TEST_F(EngineRpc, ChangeServiceObjectCustomVar) { th->join(); ASSERT_EQ(_svc->custom_variables.size(), 1u); - ASSERT_EQ(_svc->custom_variables["TEST_VAR"].get_value(), "test_val"); + ASSERT_EQ(_svc->custom_variables["TEST_VAR"].value(), "test_val"); _svc->custom_variables.clear(); ASSERT_EQ(_svc->custom_variables.size(), 0u); erpc.shutdown(); @@ -1740,8 +1740,7 @@ TEST_F(EngineRpc, ChangeContactObjectCustomVar) { 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"); + ASSERT_EQ(_contact->get_custom_variables()["TEST_VAR"].value(), "test_val"); erpc.shutdown(); }