From 4a8cb6506b5b3a7e395f0ad8dd1d12f2b068ae61 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Wed, 5 Jun 2024 08:34:50 +0200 Subject: [PATCH 01/21] enh(engine): customvariables have their own configuration class --- broker/neb/src/initial.cc | 4 +- .../centreon/engine/configuration/service.hh | 2 +- .../centreon/engine/configuration/state.hh | 2 +- .../inc/com/centreon/engine/customvariable.hh | 4 +- engine/src/commands/raw.cc | 15 +++--- engine/src/configuration/CMakeLists.txt | 1 + .../configuration/applier/anomalydetection.cc | 41 ++++++++-------- engine/src/configuration/applier/contact.cc | 22 +++++---- engine/src/configuration/applier/host.cc | 16 +++--- engine/src/configuration/applier/service.cc | 49 +++++++++---------- engine/src/configuration/applier/state.cc | 2 +- engine/src/configuration/parser.cc | 2 +- engine/src/configuration/service.cc | 2 +- engine/src/configuration/state.cc | 2 +- engine/src/customvariable.cc | 2 +- engine/src/macros.cc | 2 +- engine/src/retention/applier/contact.cc | 36 +++++++------- engine/src/retention/applier/host.cc | 36 +++++++------- engine/src/retention/applier/service.cc | 2 +- engine/src/retention/dump.cc | 2 +- engine/src/xsddefault.cc | 6 +-- .../configuration/applier/applier-contact.cc | 7 ++- .../configuration/applier/applier-state.cc | 2 +- engine/tests/enginerpc/enginerpc.cc | 7 ++- 24 files changed, 132 insertions(+), 134 deletions(-) diff --git a/broker/neb/src/initial.cc b/broker/neb/src/initial.cc index 71829c4e99d..a8fad65920e 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/service.hh b/engine/inc/com/centreon/engine/configuration/service.hh index 49fc7bd5853..d72dc1fb52d 100644 --- a/engine/inc/com/centreon/engine/configuration/service.hh +++ b/engine/inc/com/centreon/engine/configuration/service.hh @@ -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; diff --git a/engine/inc/com/centreon/engine/configuration/state.hh b/engine/inc/com/centreon/engine/configuration/state.hh index e0b3cc2d9ab..76f167f8bb0 100644 --- a/engine/inc/com/centreon/engine/configuration/state.hh +++ b/engine/inc/com/centreon/engine/configuration/state.hh @@ -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); 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/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/applier/anomalydetection.cc b/engine/src/configuration/applier/anomalydetection.cc index a4cc5ef1d14..ccbd1325b63 100644 --- a/engine/src/configuration/applier/anomalydetection.cc +++ b/engine/src/configuration/applier/anomalydetection.cc @@ -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. @@ -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); } } @@ -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(it_ad->customvariables()).begin(), - end = const_cast(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; } /** @@ -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); } } } @@ -524,8 +525,6 @@ void applier::anomalydetection::_expand_service_memberships( // Reinsert anomalydetection group. s.servicegroups().insert(backup); } - - return; } /** diff --git a/engine/src/configuration/applier/contact.cc b/engine/src/configuration/applier/contact.cc index 022737ac760..c3bb3025aff 100644 --- a/engine/src/configuration/applier/contact.cc +++ b/engine/src/configuration/applier/contact.cc @@ -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); } } @@ -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); } } } diff --git a/engine/src/configuration/applier/host.cc b/engine/src/configuration/applier/host.cc index 6ca6239e0a8..c21486618e1 100644 --- a/engine/src/configuration/applier/host.cc +++ b/engine/src/configuration/applier/host.cc @@ -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); } } @@ -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); } } } diff --git a/engine/src/configuration/applier/service.cc b/engine/src/configuration/applier/service.cc index e26d4f3d955..3d83fb17eb5 100644 --- a/engine/src/configuration/applier/service.cc +++ b/engine/src/configuration/applier/service.cc @@ -113,7 +113,7 @@ void applier::service::add_object(configuration::service const& obj) { obj.service_description(), *obj.hosts().begin()); // Add service to the global configuration set. - config->services().insert(obj); + config->mut_services().insert(obj); // Create service. engine::service* svc{add_service( @@ -185,15 +185,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); } } @@ -238,13 +239,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()) { @@ -256,24 +254,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()); @@ -284,7 +282,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); @@ -301,7 +299,7 @@ void applier::service::expand_objects(configuration::state& s) { } // Set expanded services in configuration state. - s.services().swap(expanded); + s.mut_services().swap(expanded); } /** @@ -340,8 +338,8 @@ void applier::service::modify_object(configuration::service const& obj) { // Update the global configuration set. configuration::service obj_old(*it_cfg); - config->services().erase(it_cfg); - config->services().insert(obj); + config->mut_services().erase(it_cfg); + config->mut_services().insert(obj); // Modify properties. if (it_obj->second->get_hostname() != *obj.hosts().begin() || @@ -482,20 +480,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); } } } @@ -591,7 +588,7 @@ void applier::service::remove_object(configuration::service const& obj) { } // Remove service from the global configuration set. - config->services().erase(obj); + config->mut_services().erase(obj); } /** diff --git a/engine/src/configuration/applier/state.cc b/engine/src/configuration/applier/state.cc index dcf62c66ff0..e697f92bdca 100644 --- a/engine/src/configuration/applier/state.cc +++ b/engine/src/configuration/applier/state.cc @@ -1468,7 +1468,7 @@ void applier::state::_processing(configuration::state& new_cfg, config->hostgroups()); // Resolve services. - _resolve(config->services()); + _resolve(config->mut_services()); // Resolve anomalydetections. _resolve( diff --git a/engine/src/configuration/parser.cc b/engine/src/configuration/parser.cc index 5c017842d1f..112ef90061c 100644 --- a/engine/src/configuration/parser.cc +++ b/engine/src/configuration/parser.cc @@ -96,7 +96,7 @@ void parser::parse(std::string const& path, state& config) { config.servicedependencies()); _insert(_lst_objects[object::serviceescalation], config.serviceescalations()); _insert(_map_objects[object::servicegroup], config.servicegroups()); - _insert(_lst_objects[object::service], config.services()); + _insert(_lst_objects[object::service], config.mut_services()); _insert(_lst_objects[object::anomalydetection], config.anomalydetections()); _insert(_map_objects[object::timeperiod], config.timeperiods()); _insert(_lst_objects[object::severity], config.mut_severities()); diff --git a/engine/src/configuration/service.cc b/engine/src/configuration/service.cc index fdeb2d1d982..28951a26cf6 100644 --- a/engine/src/configuration/service.cc +++ b/engine/src/configuration/service.cc @@ -1052,7 +1052,7 @@ com::centreon::engine::map_customvar const& service::customvariables() * * @return The customvariables. */ -com::centreon::engine::map_customvar& service::customvariables() noexcept { +com::centreon::engine::map_customvar& service::mut_customvariables() noexcept { return _customvariables; } diff --git a/engine/src/configuration/state.cc b/engine/src/configuration/state.cc index dc4b3ee4e82..cf70aed70db 100644 --- a/engine/src/configuration/state.cc +++ b/engine/src/configuration/state.cc @@ -3213,7 +3213,7 @@ set_service const& state::services() const noexcept { * * @return All engine services. */ -set_service& state::services() noexcept { +set_service& state::mut_services() noexcept { return _services; } 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 37a345833be..5e8962e3917 100644 --- a/engine/src/retention/dump.cc +++ b/engine/src/retention/dump.cc @@ -191,7 +191,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(); } From 42e07ae5bc904740e5abc9286e29f1da19a13539 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Wed, 22 May 2024 13:44:50 +0200 Subject: [PATCH 02/21] enh(engine/string): string lib removed from configuration --- .../engine/configuration/applier/command.hh | 25 +- .../centreon/engine/configuration/contact.hh | 8 +- .../engine/configuration/contactgroup.hh | 22 +- .../centreon/engine/configuration/group.hh | 69 ++-- engine/inc/com/centreon/engine/string.hh | 42 +- engine/src/configuration/anomalydetection.cc | 389 ++++++------------ .../configuration/applier/anomalydetection.cc | 12 - engine/src/configuration/applier/command.cc | 105 ++--- engine/src/configuration/applier/connector.cc | 35 +- engine/src/configuration/applier/contact.cc | 115 +++--- .../src/configuration/applier/contactgroup.cc | 62 +-- engine/src/configuration/applier/globals.cc | 19 +- engine/src/configuration/applier/macros.cc | 1 - .../applier/servicedependency.cc | 1 - engine/src/configuration/contact.cc | 73 ++-- engine/src/configuration/contactgroup.cc | 22 +- engine/src/configuration/group.cc | 78 +++- engine/src/configuration/host.cc | 1 - engine/src/configuration/hostdependency.cc | 1 - engine/src/configuration/hostescalation.cc | 1 - engine/src/configuration/object.cc | 1 - engine/src/configuration/parser.cc | 1 - engine/src/configuration/service.cc | 1 - engine/src/configuration/servicedependency.cc | 1 - engine/src/configuration/serviceescalation.cc | 1 - engine/src/configuration/state.cc | 1 - engine/src/configuration/timeperiod.cc | 1 - 27 files changed, 478 insertions(+), 610 deletions(-) diff --git a/engine/inc/com/centreon/engine/configuration/applier/command.hh b/engine/inc/com/centreon/engine/configuration/applier/command.hh index 8df1d47a62e..b2163f07390 100644 --- a/engine/inc/com/centreon/engine/configuration/applier/command.hh +++ b/engine/inc/com/centreon/engine/configuration/applier/command.hh @@ -35,17 +35,22 @@ class state; namespace applier { class command { public: - command(); - ~command() throw(); - void add_object(configuration::command const& obj); + /** + * @brief Default constructor + */ + command() = default; + + /** + * @brief Destructor. + */ + ~command() noexcept = default; + command(const command&) = delete; + command& operator=(const command&) = delete; + void add_object(const configuration::command& obj); + void modify_object(const configuration::command& obj); + void remove_object(const configuration::command& obj); void expand_objects(configuration::state& s); - void modify_object(configuration::command const& obj); - void remove_object(configuration::command const& obj); - void resolve_object(configuration::command const& obj); - - private: - command(command const& right); - command& operator=(command const& right); + void resolve_object(const configuration::command& obj); }; } // namespace applier } // namespace configuration diff --git a/engine/inc/com/centreon/engine/configuration/contact.hh b/engine/inc/com/centreon/engine/configuration/contact.hh index 615181876f7..f6d763f9168 100644 --- a/engine/inc/com/centreon/engine/configuration/contact.hh +++ b/engine/inc/com/centreon/engine/configuration/contact.hh @@ -53,8 +53,10 @@ class contact : public object { set_string& contactgroups() noexcept; set_string const& contactgroups() const noexcept; std::string const& contact_name() const noexcept; - map_customvar const& customvariables() const noexcept; - map_customvar& customvariables() noexcept; + const std::unordered_map& customvariables() + const noexcept; + std::unordered_map& + mutable_customvariables() noexcept; std::string const& email() const noexcept; bool host_notifications_enabled() const noexcept; list_string const& host_notification_commands() const noexcept; @@ -96,7 +98,7 @@ class contact : public object { opt _can_submit_commands; group _contactgroups; std::string _contact_name; - map_customvar _customvariables; + std::unordered_map _customvariables; std::string _email; opt _host_notifications_enabled; group _host_notification_commands; diff --git a/engine/inc/com/centreon/engine/configuration/contactgroup.hh b/engine/inc/com/centreon/engine/configuration/contactgroup.hh index 2fb472697c8..86552d44da7 100644 --- a/engine/inc/com/centreon/engine/configuration/contactgroup.hh +++ b/engine/inc/com/centreon/engine/configuration/contactgroup.hh @@ -32,22 +32,22 @@ class contactgroup : public object { contactgroup(key_type const& key = ""); contactgroup(contactgroup const& right); - ~contactgroup() throw() override; + ~contactgroup() noexcept override; contactgroup& operator=(contactgroup const& right); - bool operator==(contactgroup const& right) const throw(); - bool operator!=(contactgroup const& right) const throw(); - bool operator<(contactgroup const& right) const throw(); + bool operator==(contactgroup const& right) const noexcept; + bool operator!=(contactgroup const& right) const noexcept; + bool operator<(contactgroup const& right) const noexcept; void check_validity() const override; - key_type const& key() const throw(); + key_type const& key() const noexcept; void merge(object const& obj) override; bool parse(char const* key, char const* value) override; - std::string const& alias() const throw(); - set_string& contactgroup_members() throw(); - set_string const& contactgroup_members() const throw(); - std::string const& contactgroup_name() const throw(); - set_string& members() throw(); - set_string const& members() const throw(); + std::string const& alias() const noexcept; + set_string& contactgroup_members() noexcept; + set_string const& contactgroup_members() const noexcept; + std::string const& contactgroup_name() const noexcept; + set_string& members() noexcept; + set_string const& members() const noexcept; private: typedef bool (*setter_func)(contactgroup&, char const*); diff --git a/engine/inc/com/centreon/engine/configuration/group.hh b/engine/inc/com/centreon/engine/configuration/group.hh index 45281375df3..9f873ad1469 100644 --- a/engine/inc/com/centreon/engine/configuration/group.hh +++ b/engine/inc/com/centreon/engine/configuration/group.hh @@ -1,21 +1,21 @@ -/* -** Copyright 2011-2013,2017 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,2017-2023 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_GROUP_HH #define CCE_CONFIGURATION_GROUP_HH @@ -29,30 +29,29 @@ typedef std::list list_string; typedef std::set set_string; typedef std::set > set_pair_string; -namespace com::centreon::engine { +namespace com::centreon::engine::configuration { -namespace configuration { template class group { public: group(bool inherit = false); group(group const& other); - ~group() throw(); + ~group() noexcept; group& operator=(group const& other); group& operator=(std::string const& other); group& operator+=(group const& other); - bool operator==(group const& other) const throw(); - bool operator!=(group const& other) const throw(); - bool operator<(group const& other) const throw(); - T& operator*() throw() { return (_data); } - T const& operator*() const throw() { return (_data); } - T* operator->() throw() { return (&_data); } - T const* operator->() const throw() { return (&_data); } - T& get() throw() { return (_data); } - T const& get() const throw() { return (_data); } - bool is_inherit() const throw() { return (_is_inherit); } - void is_inherit(bool enable) throw() { _is_inherit = enable; } - bool is_set() const throw() { return (_is_set); } + bool operator==(group const& other) const noexcept; + bool operator!=(group const& other) const noexcept; + bool operator<(group const& other) const noexcept; + T& operator*() noexcept { return (_data); } + T const& operator*() const noexcept { return (_data); } + T* operator->() noexcept { return (&_data); } + T const* operator->() const noexcept { return (&_data); } + T& get() noexcept { return (_data); } + T const& get() const noexcept { return (_data); } + bool is_inherit() const noexcept { return (_is_inherit); } + void is_inherit(bool enable) noexcept { _is_inherit = enable; } + bool is_set() const noexcept { return (_is_set); } void reset(); private: @@ -61,8 +60,6 @@ class group { bool _is_null; bool _is_set; }; -} // namespace configuration - -} +} // namespace com::centreon::engine::configuration #endif // !CCE_CONFIGURATION_GROUP_HH diff --git a/engine/inc/com/centreon/engine/string.hh b/engine/inc/com/centreon/engine/string.hh index 89c9bb5df44..3290bf77507 100644 --- a/engine/inc/com/centreon/engine/string.hh +++ b/engine/inc/com/centreon/engine/string.hh @@ -1,25 +1,28 @@ -/* -** Copyright 2011-2013,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,2017-2024 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_STRING_HH #define CCE_STRING_HH +#include +#include + #include #include #include @@ -29,7 +32,6 @@ #include #include - namespace com::centreon::engine { namespace string { @@ -226,6 +228,6 @@ class c_strtok { } // namespace string -} +} // namespace com::centreon::engine #endif // !CCE_MISC_STRING_HH diff --git a/engine/src/configuration/anomalydetection.cc b/engine/src/configuration/anomalydetection.cc index 7ccf1724b99..f6598b379e2 100644 --- a/engine/src/configuration/anomalydetection.cc +++ b/engine/src/configuration/anomalydetection.cc @@ -26,14 +26,15 @@ #include "com/centreon/engine/globals.hh" #include "com/centreon/engine/host.hh" #include "com/centreon/engine/logging/logger.hh" -#include "com/centreon/engine/string.hh" +#include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; extern int config_errors; using namespace com::centreon; using namespace com::centreon::engine::configuration; -using namespace com::centreon::engine::logging; +using com::centreon::common::log_v2::log_v2; +using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ &object::setter::generic @@ -323,443 +324,314 @@ anomalydetection& anomalydetection::operator=(anomalydetection const& other) { */ bool anomalydetection::operator==( anomalydetection const& other) const noexcept { + auto logger = log_v2::instance().get(log_v2::CONFIG); if (!object::operator==(other)) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => object don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => object don't match"); return false; } if (_acknowledgement_timeout != other._acknowledgement_timeout) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "acknowledgement_timeout don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "acknowledgement_timeout don't match"); return false; } if (_action_url != other._action_url) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => action_url don't match"; - config_logger->debug( - "configuration::anomalydetection::" - "equality => action_url don't match"); + logger->debug( + "configuration::anomalydetection::equality => action_url don't match"); return false; } if (_status_change != other._status_change) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => status_change don't match"; - config_logger->debug( - "configuration::anomalydetection::" - "equality => status_change don't match"); + logger->debug( + "configuration::anomalydetection::equality => status_change don't " + "match"); return false; } if (_checks_active != other._checks_active) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => checks_active don't match"; - config_logger->debug( - "configuration::anomalydetection::" - "equality => checks_active don't match"); + logger->debug( + "configuration::anomalydetection::equality => checks_active don't " + "match"); return false; } if (_checks_passive != other._checks_passive) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => checks_passive don't match"; - config_logger->debug( - "configuration::anomalydetection::" - "equality => checks_passive don't match"); + logger->debug( + "configuration::anomalydetection::equality => checks_passive don't " + "match"); return false; } if (_metric_name != other._metric_name) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => metric_name don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => metric_name don't match"); return false; } if (_thresholds_file != other._thresholds_file) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => thresholds_file don't " - "match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => thresholds_file don't " "match"); return false; } if (_check_freshness != other._check_freshness) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => check_freshness don't " - "match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => check_freshness don't " "match"); return false; } if (_check_interval != other._check_interval) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => check_interval don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => check_interval don't match"); return false; } if (_contactgroups != other._contactgroups) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => contactgroups don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => contactgroups don't match"); return false; } if (_contacts != other._contacts) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => contacts don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => contacts don't match"); return false; } if (std::operator!=(_customvariables, other._customvariables)) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => customvariables don't " - "match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => customvariables don't " "match"); return false; } if (_display_name != other._display_name) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => display_name don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => display_name don't match"); return false; } if (_event_handler != other._event_handler) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => event_handler don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => event_handler don't match"); return false; } if (_event_handler_enabled != other._event_handler_enabled) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => event_handler don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => event_handler don't match"); return false; } if (_first_notification_delay != other._first_notification_delay) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "first_notification_delay don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "first_notification_delay don't match"); return false; } if (_flap_detection_enabled != other._flap_detection_enabled) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "flap_detection_enabled don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "flap_detection_enabled don't match"); return false; } if (_flap_detection_options != other._flap_detection_options) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "flap_detection_options don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "flap_detection_options don't match"); return false; } if (_freshness_threshold != other._freshness_threshold) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "freshness_threshold don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "freshness_threshold don't match"); return false; } if (_high_flap_threshold != other._high_flap_threshold) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "high_flap_threshold don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "high_flap_threshold don't match"); return false; } if (_host_name != other._host_name) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => _host_name don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => _host_name don't match"); return false; } if (_icon_image != other._icon_image) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => icon_image don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => icon_image don't match"); return false; } if (_icon_image_alt != other._icon_image_alt) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => icon_image_alt don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => icon_image_alt don't match"); return false; } if (_initial_state != other._initial_state) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => initial_state don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => initial_state don't match"); return false; } if (_is_volatile != other._is_volatile) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => is_volatile don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => is_volatile don't match"); return false; } if (_low_flap_threshold != other._low_flap_threshold) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => low_flap_threshold " - "don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => low_flap_threshold " "don't match"); return false; } if (_max_check_attempts != other._max_check_attempts) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => max_check_attempts " - "don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => max_check_attempts " "don't match"); return false; } if (_notes != other._notes) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => notes don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => notes don't match"); return false; } if (_notes_url != other._notes_url) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => notes_url don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => notes_url don't match"); return false; } if (_notifications_enabled != other._notifications_enabled) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "notifications_enabled don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "notifications_enabled don't match"); return false; } if (_notification_interval != other._notification_interval) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "notification_interval don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "notification_interval don't match"); return false; } if (_notification_options != other._notification_options) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "notification_options don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "notification_options don't match"); return false; } if (_notification_period != other._notification_period) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "notification_period don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "notification_period don't match"); return false; } if (_obsess_over_service != other._obsess_over_service) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "obsess_over_service don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "obsess_over_service don't match"); return false; } if (_process_perf_data != other._process_perf_data) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => process_perf_data " - "don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => process_perf_data " "don't match"); return false; } if (_retain_nonstatus_information != other._retain_nonstatus_information) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "retain_nonstatus_information don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "retain_nonstatus_information don't match"); return false; } if (_retain_status_information != other._retain_status_information) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "retain_status_information don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "retain_status_information don't match"); return false; } if (_retry_interval != other._retry_interval) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => retry_interval don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => retry_interval don't match"); return false; } if (_recovery_notification_delay != other._recovery_notification_delay) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "recovery_notification_delay don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "recovery_notification_delay don't match"); return false; } if (_servicegroups != other._servicegroups) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => servicegroups don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => servicegroups don't match"); return false; } if (_service_description != other._service_description) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => " - "service_description don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => " "service_description don't match"); return false; } if (_host_id != other._host_id) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => host_id don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => host_id don't match"); return false; } if (_host_id != other._host_id) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => host_id don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => host_id don't match"); return false; } if (_service_id != other._service_id) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => service_id don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::" "equality => service_id don't match"); return false; } if (_internal_id != other._internal_id) { - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => internal_id " "don't match"); return false; } if (_dependent_service_id != other._dependent_service_id) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => dependent_service_id " - "don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => dependent_service_id " "don't match"); return false; } if (_stalking_options != other._stalking_options) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => stalking_options " - "don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => stalking_options " "don't match"); return false; } if (_timezone != other._timezone) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => timezone don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => timezone don't match"); return false; } if (_severity_id != other._severity_id) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => severity id don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => severity id don't match"); return false; } if (_icon_id != other._icon_id) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => icon id don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => icon id don't match"); return false; } if (_tags != other._tags) { - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => tags don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => tags don't match"); return false; } if (_sensitivity != other._sensitivity) { - engine_logger(dbg_config, more) << "configuration::anomalydetection::" - "equality => sensitivity don't match"; - config_logger->debug( + logger->debug( "configuration::anomalydetection::equality => sensitivity don't match"); return false; } - engine_logger(dbg_config, more) - << "configuration::anomalydetection::equality => OK"; - config_logger->debug("configuration::anomalydetection::equality => OK"); + logger->debug("configuration::anomalydetection::equality => OK"); return true; } @@ -889,24 +761,28 @@ bool anomalydetection::operator<(anomalydetection const& other) const noexcept { /** * Check if the object is valid. * - * @exception engine_error if this anomalydetection is an invalid object. + * @exception exceptions::msg_fmt if this anomalydetection is an invalid + * object. */ void anomalydetection::check_validity() const { if (_service_description.empty()) - throw engine_error() << "Service has no description (property " - << "'service_description')"; + throw msg_fmt( + "Service has no description (property " + "'service_description')"); if (_host_name.empty()) - throw engine_error() << "Service '" << _service_description - << "' is not attached to any host (property " - "'host_name')"; + throw msg_fmt( + "Service '{}' is not attached to any host (property 'host_name')", + _service_description); if (_metric_name.empty()) - throw engine_error() - << "Anomaly detection service '" << _service_description - << "' has no metric name specified (property 'metric_name')"; + throw msg_fmt( + "Anomaly detection service '{}' has no metric name specified (property " + "'metric_name')", + _service_description); if (_thresholds_file.empty()) - throw engine_error() - << "Anomaly detection service '" << _service_description - << "' has no thresholds file specified (property 'thresholds_file')"; + throw msg_fmt( + "Anomaly detection service '{}' has no thresholds file specified " + "(property 'thresholds_file')", + _service_description); } /** @@ -926,8 +802,8 @@ anomalydetection::key_type anomalydetection::key() const { */ void anomalydetection::merge(object const& obj) { if (obj.type() != _type) - throw engine_error() << "Cannot merge anomalydetection with '" << obj.type() - << "'"; + throw msg_fmt("Cannot merge anomalydetection with object of type '{}'", + static_cast(obj.type())); anomalydetection const& tmpl(static_cast(obj)); MRG_OPTION(_acknowledgement_timeout); @@ -994,7 +870,7 @@ bool anomalydetection::parse(char const* key, char const* value) { return (it->second)(*this, value); if (key[0] == '_') { - map_customvar::iterator it{_customvariables.find(key + 1)}; + auto it = _customvariables.find(key + 1); if (it == _customvariables.end()) _customvariables[key + 1] = customvariable(value); else @@ -1329,7 +1205,6 @@ bool anomalydetection::notifications_enabled() const noexcept { */ void anomalydetection::notification_interval(unsigned int interval) noexcept { _notification_interval = interval; - return; } /** @@ -1366,7 +1241,6 @@ unsigned short anomalydetection::notification_options() const noexcept { */ void anomalydetection::notification_period(std::string const& period) { _notification_period = period; - return; } /** @@ -1529,7 +1403,6 @@ unsigned short anomalydetection::stalking_options() const noexcept { */ void anomalydetection::timezone(std::string const& time_zone) { _timezone = time_zone; - return; } /** @@ -1590,7 +1463,7 @@ bool anomalydetection::set_acknowledgement_timeout(int value) { * * @return True on success, otherwise false. */ -bool anomalydetection::_set_action_url(std::string const& value) { +bool anomalydetection::_set_action_url(const std::string& value) { _action_url = value; return true; } @@ -1752,10 +1625,8 @@ bool anomalydetection::_set_event_handler_enabled(bool value) { */ bool anomalydetection::_set_failure_prediction_enabled(bool value) { (void)value; - engine_logger(log_verification_error, basic) - << "Warning: anomalydetection failure_prediction_enabled is deprecated." - << " This option will not be supported in 20.04."; - config_logger->warn( + auto logger = log_v2::instance().get(log_v2::CONFIG); + logger->warn( "Warning: anomalydetection failure_prediction_enabled is deprecated. " "This option will not be supported in 20.04."); ++config_warnings; @@ -1772,10 +1643,8 @@ bool anomalydetection::_set_failure_prediction_enabled(bool value) { bool anomalydetection::_set_failure_prediction_options( std::string const& value) { (void)value; - engine_logger(log_verification_error, basic) - << "Warning: anomalydetection failure_prediction_options is deprecated." - << " This option will not be supported in 20.04."; - config_logger->warn( + auto logger = log_v2::instance().get(log_v2::CONFIG); + logger->warn( "Warning: anomalydetection failure_prediction_options is deprecated. " "This option will not be supported in 20.04."); ++config_warnings; @@ -1815,22 +1684,20 @@ bool anomalydetection::_set_flap_detection_enabled(bool value) { */ bool anomalydetection::_set_flap_detection_options(std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "o" || *it == "ok") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "o" || v == "ok") options |= ok; - else if (*it == "w" || *it == "warning") + else if (v == "w" || v == "warning") options |= warning; - else if (*it == "u" || *it == "unknown") + else if (v == "u" || v == "unknown") options |= unknown; - else if (*it == "c" || *it == "critical") + else if (v == "c" || v == "critical") options |= critical; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = ok | warning | unknown | critical; else return false; @@ -1907,8 +1774,8 @@ bool anomalydetection::_set_icon_image_alt(std::string const& value) { * @return True on success, otherwise false. */ bool anomalydetection::_set_initial_state(std::string const& value) { - std::string data(value); - string::trim(data); + std::string_view data(value); + data = absl::StripAsciiWhitespace(data); if (data == "o" || data == "ok") _initial_state = engine::service::state_ok; else if (data == "w" || data == "warning") @@ -2005,26 +1872,24 @@ bool anomalydetection::_set_notifications_enabled(bool value) { */ bool anomalydetection::_set_notification_options(std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "u" || *it == "unknown") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "u" || v == "unknown") options |= unknown; - else if (*it == "w" || *it == "warning") + else if (v == "w" || v == "warning") options |= warning; - else if (*it == "c" || *it == "critical") + else if (v == "c" || v == "critical") options |= critical; - else if (*it == "r" || *it == "recovery") + else if (v == "r" || v == "recovery") options |= ok; - else if (*it == "f" || *it == "flapping") + else if (v == "f" || v == "flapping") options |= flapping; - else if (*it == "s" || *it == "downtime") + else if (v == "s" || v == "downtime") options |= downtime; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = unknown | warning | critical | ok | flapping | downtime; else return false; @@ -2078,10 +1943,8 @@ bool anomalydetection::_set_obsess_over_service(bool value) { */ bool anomalydetection::_set_parallelize_check(bool value) { (void)value; - engine_logger(log_verification_error, basic) - << "Warning: anomalydetection parallelize_check is deprecated" - << " This option will not be supported in 20.04."; - config_logger->warn( + auto logger = log_v2::instance().get(log_v2::CONFIG); + logger->warn( "Warning: anomalydetection parallelize_check is deprecated This option " "will not be supported in 20.04."); ++config_warnings; @@ -2219,22 +2082,20 @@ bool anomalydetection::set_dependent_service_id(uint64_t value) { */ bool anomalydetection::_set_stalking_options(std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "o" || *it == "ok") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "o" || v == "ok") options |= ok; - else if (*it == "w" || *it == "warning") + else if (v == "w" || v == "warning") options |= warning; - else if (*it == "u" || *it == "unknown") + else if (v == "u" || v == "unknown") options |= unknown; - else if (*it == "c" || *it == "critical") + else if (v == "c" || v == "critical") options |= critical; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = ok | warning | unknown | critical; else return false; @@ -2281,7 +2142,8 @@ bool anomalydetection::_set_category_tags(const std::string& value) { if (parse_ok) { _tags.emplace(id, tag::servicecategory); } else { - config_logger->warn( + auto logger = log_v2::instance().get(log_v2::CONFIG); + logger->warn( "Warning: anomalydetection ({}, {}) error for parsing tag {}", _host_id, _service_id, value); ret = false; @@ -2316,7 +2178,8 @@ bool anomalydetection::_set_group_tags(const std::string& value) { if (parse_ok) { _tags.emplace(id, tag::servicegroup); } else { - config_logger->warn( + auto logger = log_v2::instance().get(log_v2::CONFIG); + logger->warn( "Warning: anomalydetection ({}, {}) error for parsing tag {}", _host_id, _service_id, value); ret = false; diff --git a/engine/src/configuration/applier/anomalydetection.cc b/engine/src/configuration/applier/anomalydetection.cc index ccbd1325b63..fd5be1e8ca0 100644 --- a/engine/src/configuration/applier/anomalydetection.cc +++ b/engine/src/configuration/applier/anomalydetection.cc @@ -62,9 +62,6 @@ void applier::anomalydetection::add_object( << obj.service_description() << "'"; // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Creating new anomalydetection '" << obj.service_description() - << "' of host '" << obj.host_name() << "'."; SPDLOG_LOGGER_DEBUG(config_logger, "Creating new anomalydetection '{}' of host '{}'.", obj.service_description(), obj.host_name()); @@ -203,9 +200,6 @@ void applier::anomalydetection::modify_object( std::string const& service_description(obj.service_description()); // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Modifying new anomalydetection '" << service_description - << "' of host '" << host_name << "'."; SPDLOG_LOGGER_DEBUG(config_logger, "Modifying new anomalydetection '{}' of host '{}'.", service_description, host_name); @@ -410,9 +404,6 @@ void applier::anomalydetection::remove_object( assert(obj.key().first); // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Removing anomalydetection '" << service_description << "' of host '" - << host_name << "'."; SPDLOG_LOGGER_DEBUG(config_logger, "Removing anomalydetection '{}' of host '{}'.", service_description, host_name); @@ -460,9 +451,6 @@ void applier::anomalydetection::remove_object( void applier::anomalydetection::resolve_object( configuration::anomalydetection const& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Resolving anomalydetection '" << obj.service_description() - << "' of host '" << obj.host_name() << "'."; SPDLOG_LOGGER_DEBUG(config_logger, "Resolving anomalydetection '{}' of host '{}'.", obj.service_description(), obj.host_name()); diff --git a/engine/src/configuration/applier/command.cc b/engine/src/configuration/applier/command.cc index 48275d2ed3e..9bbcb97fd32 100644 --- a/engine/src/configuration/applier/command.cc +++ b/engine/src/configuration/applier/command.cc @@ -31,71 +31,13 @@ using namespace com::centreon::engine; using namespace com::centreon::engine::configuration; -/** - * Default constructor. - */ -applier::command::command() {} - -/** - * Destructor. - */ -applier::command::~command() throw() {} - -/** - * Add new command. - * - * @param[in] obj The new command to add into the monitoring engine. - */ -void applier::command::add_object(configuration::command const& obj) { - // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Creating new command '" << obj.command_name() << "'."; - config_logger->debug("Creating new command '{}'.", obj.command_name()); - - // Add command to the global configuration set. - config->commands().insert(obj); - - if (obj.connector().empty()) { - std::shared_ptr raw = std::make_shared( - obj.command_name(), obj.command_line(), &checks::checker::instance()); - commands::command::commands[raw->get_name()] = raw; - } else { - connector_map::iterator found_con{ - commands::connector::connectors.find(obj.connector())}; - if (found_con != commands::connector::connectors.end() && - found_con->second) { - std::shared_ptr forward{ - std::make_shared( - obj.command_name(), obj.command_line(), found_con->second)}; - commands::command::commands[forward->get_name()] = forward; - } else - throw engine_error() << "Could not register command '" - << obj.command_name() << "': unable to find '" - << obj.connector() << "'"; - } -} - -/** - * @brief Expand command. - * - * Command configuration objects do not need expansion. Therefore this - * method does nothing. - * - * @param[in] s Unused. - */ -void applier::command::expand_objects(configuration::state& s) { - (void)s; -} - /** * Modified command. * * @param[in] obj The new command to modify into the monitoring engine. */ -void applier::command::modify_object(configuration::command const& obj) { +void applier::command::modify_object(const configuration::command& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Modifying command '" << obj.command_name() << "'."; config_logger->debug("Modifying command '{}'.", obj.command_name()); // Find old configuration. @@ -148,6 +90,38 @@ void applier::command::modify_object(configuration::command const& obj) { &tv); } +/** + * Add new command. + * + * @param[in] obj The new command to add into the monitoring engine. + */ +void applier::command::add_object(configuration::command const& obj) { + // Logging. + config_logger->debug("Creating new command '{}'.", obj.command_name()); + + // Add command to the global configuration set. + config->commands().insert(obj); + + if (obj.connector().empty()) { + std::shared_ptr raw = std::make_shared( + obj.command_name(), obj.command_line(), &checks::checker::instance()); + commands::command::commands[raw->get_name()] = raw; + } else { + connector_map::iterator found_con{ + commands::connector::connectors.find(obj.connector())}; + if (found_con != commands::connector::connectors.end() && + found_con->second) { + std::shared_ptr forward{ + std::make_shared( + obj.command_name(), obj.command_line(), found_con->second)}; + commands::command::commands[forward->get_name()] = forward; + } else + throw engine_error() << "Could not register command '" + << obj.command_name() << "': unable to find '" + << obj.connector() << "'"; + } +} + /** * Remove old command. * @@ -155,8 +129,6 @@ void applier::command::modify_object(configuration::command const& obj) { */ void applier::command::remove_object(configuration::command const& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Removing command '" << obj.command_name() << "'."; config_logger->debug("Removing command '{}'.", obj.command_name()); // Find command. @@ -180,6 +152,17 @@ void applier::command::remove_object(configuration::command const& obj) { config->commands().erase(obj); } +/** + * @brief Expand command. + * + * Command configuration objects do not need expansion. Therefore this + * method does nothing. + * + * @param[in] s Unused. + */ +void applier::command::expand_objects(configuration::state& s + [[maybe_unused]]) {} + /** * @brief Resolve command. * diff --git a/engine/src/configuration/applier/connector.cc b/engine/src/configuration/applier/connector.cc index ca35ccefea0..ca73bf1a70d 100644 --- a/engine/src/configuration/applier/connector.cc +++ b/engine/src/configuration/applier/connector.cc @@ -34,8 +34,6 @@ using namespace com::centreon::engine::configuration; */ void applier::connector::add_object(configuration::connector const& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Creating new connector '" << obj.connector_name() << "'."; config_logger->debug("Creating new connector '{}'.", obj.connector_name()); // Expand command line. @@ -53,18 +51,6 @@ void applier::connector::add_object(configuration::connector const& obj) { commands::connector::connectors[obj.connector_name()] = cmd; } -/** - * @brief Expand connector. - * - * Connector configuration objects do not need expansion. Therefore - * this method only copy obj to expanded. - * - * @param[in] s Unused. - */ -void applier::connector::expand_objects(configuration::state& s) { - (void)s; -} - /** * Modify connector. * @@ -72,8 +58,6 @@ void applier::connector::expand_objects(configuration::state& s) { */ void applier::connector::modify_object(configuration::connector const& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Modifying connector '" << obj.connector_name() << "'."; config_logger->debug("Modifying connector '{}'.", obj.connector_name()); // Find old configuration. @@ -113,8 +97,6 @@ void applier::connector::modify_object(configuration::connector const& obj) { */ void applier::connector::remove_object(configuration::connector const& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Removing connector '" << obj.connector_name() << "'."; config_logger->debug("Removing connector '{}'.", obj.connector_name()); // Find connector. @@ -128,6 +110,18 @@ void applier::connector::remove_object(configuration::connector const& obj) { config->connectors().erase(obj); } +/** + * @brief Expand connector. + * + * Connector configuration objects do not need expansion. Therefore + * this method only copy obj to expanded. + * + * @param[in] s Unused. + */ +void applier::connector::expand_objects(configuration::state& s) { + (void)s; +} + /** * @brief Resolve a connector. * @@ -136,6 +130,5 @@ void applier::connector::remove_object(configuration::connector const& obj) { * * @param[in] obj Unused. */ -void applier::connector::resolve_object(configuration::connector const& obj) { - (void)obj; -} +void applier::connector::resolve_object(const configuration::connector& obj + [[maybe_unused]]) {} diff --git a/engine/src/configuration/applier/contact.cc b/engine/src/configuration/applier/contact.cc index c3bb3025aff..8a4289c5036 100644 --- a/engine/src/configuration/applier/contact.cc +++ b/engine/src/configuration/applier/contact.cc @@ -30,6 +30,8 @@ using namespace com::centreon; using namespace com::centreon::engine; using namespace com::centreon::engine::configuration; +using com::centreon::common::log_v2::log_v2; + /** * Check if the contact group name matches the configuration object. */ @@ -53,9 +55,11 @@ class contactgroup_name_comparator { * @param[in] obj The new contact to add into the monitoring engine. */ void applier::contact::add_object(configuration::contact const& obj) { + // Make sure we have the data we need. + if (obj.contact_name().empty()) + throw engine_error() << "Could not register contact with an empty name"; + // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Creating new contact '" << obj.contact_name() << "'."; config_logger->debug("Creating new contact '{}'.", obj.contact_name()); // Add contact to the global configuration set. @@ -114,56 +118,6 @@ void applier::contact::add_object(configuration::contact const& obj) { } } -/** - * @brief Expand a contact. - * - * During expansion, the contact will be added to its contact groups. - * These will be modified in the state. - * - * @param[in,out] s Configuration state. - */ -void applier::contact::expand_objects(configuration::state& s) { - // Browse all contacts. - for (configuration::set_contact::iterator it_contact(s.contacts().begin()), - end_contact(s.contacts().end()); - it_contact != end_contact; ++it_contact) { - // Should custom variables be sent to broker ? - map_customvar& mcv( - const_cast(it_contact->customvariables())); - for (map_customvar::iterator it{mcv.begin()}, end{mcv.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 contact's groups. - for (set_string::const_iterator - it_group(it_contact->contactgroups().begin()), - end_group(it_contact->contactgroups().end()); - it_group != end_group; ++it_group) { - // Find contact group. - configuration::set_contactgroup::iterator group( - s.contactgroups_find(*it_group)); - if (group == s.contactgroups().end()) - throw(engine_error() - << "Could not add contact '" << it_contact->contact_name() - << "' to non-existing contact group '" << *it_group << "'"); - - // Remove contact group from state. - configuration::contactgroup backup(*group); - s.contactgroups().erase(group); - - // Add contact to group members. - backup.members().insert(it_contact->contact_name()); - - // Reinsert contact group. - s.contactgroups().insert(backup); - } - } -} - /** * Modified contact. * @@ -171,8 +125,6 @@ void applier::contact::expand_objects(configuration::state& s) { */ void applier::contact::modify_object(configuration::contact const& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Modifying contact '" << obj.contact_name() << "'."; config_logger->debug("Modifying contact '{}'.", obj.contact_name()); // Find old configuration. @@ -335,8 +287,6 @@ void applier::contact::modify_object(configuration::contact const& obj) { */ void applier::contact::remove_object(configuration::contact const& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Removing contact '" << obj.contact_name() << "'."; config_logger->debug("Removing contact '{}'.", obj.contact_name()); // Find contact. @@ -362,15 +312,62 @@ void applier::contact::remove_object(configuration::contact const& obj) { config->contacts().erase(obj); } +/** + * @brief Expand a contact. + * + * During expansion, the contact will be added to its contact groups. + * These will be modified in the state. + * + * @param[in,out] s Configuration state. + */ +void applier::contact::expand_objects(configuration::state& s) { + // Browse all contacts. + configuration::set_contact new_contacts; + // We loop on s.contacts() but we make a copy of each element because the + // container is a set and changing its element has an impact on the order + // they are stored. So we copy each element, modify them and move the result + // to a new set. + for (auto contact : s.contacts()) { + // Should custom variables be sent to broker ? + auto& mcv = contact.mutable_customvariables(); + for (auto& cv : mcv) { + if (!s.enable_macros_filter() || + s.macros_filter().find(cv.first) != s.macros_filter().end()) { + cv.second.set_sent(true); + } + } + + // Browse current contact's groups. + for (auto& g : contact.contactgroups()) { + // Find contact group. + configuration::set_contactgroup::iterator group(s.contactgroups_find(g)); + if (group == s.contactgroups().end()) + throw(engine_error() + << "Could not add contact '" << contact.contact_name() + << "' to non-existing contact group '" << g << "'"); + + // Remove contact group from state. + configuration::contactgroup backup(*group); + s.contactgroups().erase(group); + + // Add contact to group members. + backup.members().insert(contact.contact_name()); + + // Reinsert contact group. + s.contactgroups().insert(backup); + } + new_contacts.insert(std::move(contact)); + } + s.contacts() = new_contacts; +} + /** * Resolve a contact. * * @param[in,out] obj Object to resolve. */ -void applier::contact::resolve_object(configuration::contact const& obj) { +void applier::contact::resolve_object(const configuration::contact& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Resolving contact '" << obj.contact_name() << "'."; config_logger->debug("Resolving contact '{}'.", obj.contact_name()); // Find contact. diff --git a/engine/src/configuration/applier/contactgroup.cc b/engine/src/configuration/applier/contactgroup.cc index ebb743a3281..4f569a6a99d 100644 --- a/engine/src/configuration/applier/contactgroup.cc +++ b/engine/src/configuration/applier/contactgroup.cc @@ -24,7 +24,6 @@ #include "com/centreon/engine/deleter/listmember.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/logging/logger.hh" using namespace com::centreon::engine::configuration; using namespace com::centreon::engine::logging; @@ -38,8 +37,6 @@ void applier::contactgroup::add_object(configuration::contactgroup const& obj) { std::string const& name(obj.contactgroup_name()); // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Creating new contactgroup '" << name << "'."; config_logger->debug("Creating new contactgroup '{}'.", name); if (engine::contactgroup::contactgroups.find(name) != @@ -57,9 +54,6 @@ void applier::contactgroup::add_object(configuration::contactgroup const& obj) { it != end; ++it) { contact_map::iterator ct_it{engine::contact::contacts.find(*it)}; if (ct_it == engine::contact::contacts.end()) { - engine_logger(log_verification_error, basic) - << "Error: Contact '" << *it << "' specified in contact group '" - << cg->get_name() << "' is not defined anywhere!"; config_logger->error( "Error: Contact '{}' specified in contact group '{}' is not defined " "anywhere!", @@ -75,26 +69,6 @@ void applier::contactgroup::add_object(configuration::contactgroup const& obj) { engine::contactgroup::contactgroups.insert({name, cg}); } -/** - * Expand all contactgroups. - * - * @param[in,out] s State being applied. - */ -void applier::contactgroup::expand_objects(configuration::state& s) { - // Resolve groups. - _resolved.clear(); - for (configuration::set_contactgroup::iterator it(s.contactgroups().begin()), - end(s.contactgroups().end()); - it != end; ++it) - _resolve_members(s, *it); - - // Save resolved groups in the configuration set. - s.contactgroups().clear(); - for (resolved_set::const_iterator it(_resolved.begin()), end(_resolved.end()); - it != end; ++it) - s.contactgroups().insert(it->second); -} - /** * Modified contactgroup. * @@ -102,10 +76,8 @@ void applier::contactgroup::expand_objects(configuration::state& s) { * engine. */ void applier::contactgroup::modify_object( - configuration::contactgroup const& obj) { + const configuration::contactgroup& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Modifying contactgroup '" << obj.contactgroup_name() << "'"; config_logger->debug("Modifying contactgroup '{}'", obj.contactgroup_name()); // Find old configuration. @@ -141,9 +113,6 @@ void applier::contactgroup::modify_object( it != end; ++it) { contact_map::const_iterator ct_it{engine::contact::contacts.find(*it)}; if (ct_it == engine::contact::contacts.end()) { - engine_logger(log_verification_error, basic) - << "Error: Contact '" << *it << "' specified in contact group '" - << it_obj->second->get_name() << "' is not defined anywhere!"; config_logger->error( "Error: Contact '{}' specified in contact group '{}' is not " "defined anywhere!", @@ -169,10 +138,8 @@ void applier::contactgroup::modify_object( * engine. */ void applier::contactgroup::remove_object( - configuration::contactgroup const& obj) { + const configuration::contactgroup& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Removing contactgroup '" << obj.contactgroup_name() << "'"; config_logger->debug("Removing contactgroup '{}'", obj.contactgroup_name()); // Find contact group. @@ -193,6 +160,26 @@ void applier::contactgroup::remove_object( config->contactgroups().erase(obj); } +/** + * Expand all contactgroups. + * + * @param[in,out] s State being applied. + */ +void applier::contactgroup::expand_objects(configuration::state& s) { + // Resolve groups. + _resolved.clear(); + for (configuration::set_contactgroup::iterator it(s.contactgroups().begin()), + end(s.contactgroups().end()); + it != end; ++it) + _resolve_members(s, *it); + + // Save resolved groups in the configuration set. + s.contactgroups().clear(); + for (resolved_set::const_iterator it(_resolved.begin()), end(_resolved.end()); + it != end; ++it) + s.contactgroups().insert(it->second); +} + /** * Resolve a contact group. * @@ -201,8 +188,6 @@ void applier::contactgroup::remove_object( void applier::contactgroup::resolve_object( configuration::contactgroup const& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Resolving contact group '" << obj.contactgroup_name() << "'"; config_logger->debug("Resolving contact group '{}'", obj.contactgroup_name()); // Find contact group. @@ -228,9 +213,6 @@ void applier::contactgroup::_resolve_members( // Only process if contactgroup has not been resolved already. if (_resolved.find(obj.key()) == _resolved.end()) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Resolving members of contact group '" << obj.contactgroup_name() - << "'"; config_logger->debug("Resolving members of contact group '{}'", obj.contactgroup_name()); diff --git a/engine/src/configuration/applier/globals.cc b/engine/src/configuration/applier/globals.cc index c85c97079c6..c6b2409e4a1 100644 --- a/engine/src/configuration/applier/globals.cc +++ b/engine/src/configuration/applier/globals.cc @@ -19,7 +19,6 @@ #include "com/centreon/engine/configuration/applier/globals.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/string.hh" using namespace com::centreon::engine; using namespace com::centreon::engine::configuration; @@ -89,15 +88,15 @@ void applier::globals::clear() { delete[] ::ocsp_command; delete[] ::use_timezone; - ::debug_file = NULL; - ::global_host_event_handler = NULL; - ::global_service_event_handler = NULL; - ::illegal_object_chars = NULL; - ::illegal_output_chars = NULL; - ::log_file = NULL; - ::ochp_command = NULL; - ::ocsp_command = NULL; - ::use_timezone = NULL; + ::debug_file = nullptr; + ::global_host_event_handler = nullptr; + ::global_service_event_handler = nullptr; + ::illegal_object_chars = nullptr; + ::illegal_output_chars = nullptr; + ::log_file = nullptr; + ::ochp_command = nullptr; + ::ocsp_command = nullptr; + ::use_timezone = nullptr; } /** diff --git a/engine/src/configuration/applier/macros.cc b/engine/src/configuration/applier/macros.cc index 18275987d59..271dd250236 100644 --- a/engine/src/configuration/applier/macros.cc +++ b/engine/src/configuration/applier/macros.cc @@ -22,7 +22,6 @@ #include "com/centreon/engine/globals.hh" #include "com/centreon/engine/macros.hh" #include "com/centreon/engine/macros/misc.hh" -#include "com/centreon/engine/string.hh" using namespace com::centreon::engine; using namespace com::centreon::engine::configuration; diff --git a/engine/src/configuration/applier/servicedependency.cc b/engine/src/configuration/applier/servicedependency.cc index f4fd873c949..72fee7b50ad 100644 --- a/engine/src/configuration/applier/servicedependency.cc +++ b/engine/src/configuration/applier/servicedependency.cc @@ -245,7 +245,6 @@ void applier::servicedependency::remove_object( engine::servicedependency::servicedependencies_find(obj.key())); if (it != engine::servicedependency::servicedependencies.end()) { // Notify event broker. - timeval tv(get_broker_timestamp(nullptr)); broker_adaptive_dependency_data(NEBTYPE_SERVICEDEPENDENCY_DELETE, it->second.get()); diff --git a/engine/src/configuration/contact.cc b/engine/src/configuration/contact.cc index ea0733d2364..550cb0e0519 100644 --- a/engine/src/configuration/contact.cc +++ b/engine/src/configuration/contact.cc @@ -1,5 +1,5 @@ /** - * Copyright 2011-2019 Centreon (https://www.centreon.com/) + * Copyright 2011-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. @@ -20,8 +20,7 @@ #include "com/centreon/engine/configuration/contact.hh" #include "com/centreon/engine/configuration/host.hh" #include "com/centreon/engine/configuration/service.hh" -#include "com/centreon/engine/exceptions/error.hh" -#include "com/centreon/engine/string.hh" +#include "com/centreon/exceptions/msg_fmt.hh" using namespace com::centreon; using namespace com::centreon::engine; @@ -233,8 +232,7 @@ bool contact::operator<(contact const& other) const noexcept { */ void contact::check_validity() const { if (_contact_name.empty()) - throw(engine_error() << "Contact has no name (property 'contact_name')"); - return; + throw exceptions::msg_fmt("Contact has no name (property 'contact_name')"); } /** @@ -253,7 +251,8 @@ contact::key_type const& contact::key() const noexcept { */ void contact::merge(object const& obj) { if (obj.type() != _type) - throw(engine_error() << "Cannot merge contact with '" << obj.type() << "'"); + throw exceptions::msg_fmt("Cannot merge contact with object of type '{}'", + static_cast(obj.type())); contact const& tmpl(static_cast(obj)); MRG_TAB(_address); @@ -293,7 +292,7 @@ bool contact::parse(char const* key, char const* value) { if (!strncmp(key, ADDRESS_PROPERTY, sizeof(ADDRESS_PROPERTY) - 1)) return _set_address(key + sizeof(ADDRESS_PROPERTY) - 1, value); else if (key[0] == '_') { - map_customvar::iterator it(_customvariables.find(key + 1)); + auto it = _customvariables.find(key + 1); if (it == _customvariables.end()) _customvariables[key + 1] = customvariable(value); else @@ -363,7 +362,8 @@ std::string const& contact::contact_name() const noexcept { * * @return The customvariables. */ -map_customvar const& contact::customvariables() const noexcept { +const std::unordered_map& +contact::customvariables() const noexcept { return _customvariables; } @@ -372,7 +372,8 @@ map_customvar const& contact::customvariables() const noexcept { * * @return The customvariables. */ -map_customvar& contact::customvariables() noexcept { +std::unordered_map& +contact::mutable_customvariables() noexcept { return _customvariables; } @@ -501,9 +502,11 @@ std::string const& contact::timezone() const noexcept { * * @return True on success, otherwise false. */ -bool contact::_set_address(std::string const& key, std::string const& value) { +bool contact::_set_address(const std::string& key, const std::string& value) { unsigned int id; - if (!string::to(key.c_str(), id) || (id < 1) || (id > MAX_ADDRESSES)) + if (!absl::SimpleAtoi(key, &id)) + return false; + if (id < 1 || id > MAX_ADDRESSES) return false; _address[id - 1] = value; return true; @@ -601,25 +604,23 @@ bool contact::_set_host_notification_commands(std::string const& value) { * @return True on success, otherwise false. */ bool contact::_set_host_notification_options(std::string const& value) { - unsigned short options(host::none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "d" || *it == "down") + unsigned short options = host::none; + auto values = absl::StrSplit(value, ','); + for (auto it = values.begin(), end = values.end(); it != end; ++it) { + std::string_view v = absl::StripAsciiWhitespace(*it); + if (v == "d" || v == "down") options |= host::down; - else if (*it == "u" || *it == "unreachable") + else if (v == "u" || v == "unreachable") options |= host::unreachable; - else if (*it == "r" || *it == "recovery") + else if (v == "r" || v == "recovery") options |= host::up; - else if (*it == "f" || *it == "flapping") + else if (v == "f" || v == "flapping") options |= host::flapping; - else if (*it == "s" || *it == "downtime") + else if (v == "s" || v == "downtime") options |= host::downtime; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = host::none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = host::down | host::unreachable | host::up | host::flapping | host::downtime; else @@ -698,26 +699,24 @@ bool contact::_set_service_notification_commands(std::string const& value) { */ bool contact::_set_service_notification_options(std::string const& value) { unsigned short options(service::none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "u" || *it == "unknown") + auto values = absl::StrSplit(value, ','); + for (auto it = values.begin(), end = values.end(); it != end; ++it) { + auto v = absl::StripAsciiWhitespace(*it); + if (v == "u" || v == "unknown") options |= service::unknown; - else if (*it == "w" || *it == "warning") + else if (v == "w" || v == "warning") options |= service::warning; - else if (*it == "c" || *it == "critical") + else if (v == "c" || v == "critical") options |= service::critical; - else if (*it == "r" || *it == "recovery") + else if (v == "r" || v == "recovery") options |= service::ok; - else if (*it == "f" || *it == "flapping") + else if (v == "f" || v == "flapping") options |= service::flapping; - else if (*it == "s" || *it == "downtime") + else if (v == "s" || v == "downtime") options |= service::downtime; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = service::none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = service::unknown | service::warning | service::critical | service::ok | service::flapping | service::downtime; else diff --git a/engine/src/configuration/contactgroup.cc b/engine/src/configuration/contactgroup.cc index 9bad91e0cf5..2348efbd749 100644 --- a/engine/src/configuration/contactgroup.cc +++ b/engine/src/configuration/contactgroup.cc @@ -17,7 +17,7 @@ * */ #include "com/centreon/engine/configuration/contactgroup.hh" -#include "com/centreon/engine/exceptions/error.hh" +#include "com/centreon/exceptions/msg_fmt.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; @@ -53,7 +53,7 @@ contactgroup::contactgroup(contactgroup const& right) : object(right) { /** * Destructor. */ -contactgroup::~contactgroup() throw() {} +contactgroup::~contactgroup() noexcept {} /** * Copy constructor. @@ -70,7 +70,7 @@ contactgroup& contactgroup::operator=(contactgroup const& right) { _contactgroup_name = right._contactgroup_name; _members = right._members; } - return (*this); + return *this; } /** @@ -122,9 +122,8 @@ bool contactgroup::operator<(contactgroup const& right) const throw() { */ void contactgroup::check_validity() const { if (_contactgroup_name.empty()) - throw(engine_error() << "Contact group has no name " - "(property 'contactgroup_name')"); - return; + throw exceptions::msg_fmt( + "Contact group has no name (property 'contactgroup_name')"); } /** @@ -143,8 +142,9 @@ contactgroup::key_type const& contactgroup::key() const throw() { */ void contactgroup::merge(object const& obj) { if (obj.type() != _type) - throw(engine_error() << "Cannot merge contact group with '" << obj.type() - << "'"); + throw exceptions::msg_fmt( + "Cannot merge contact group with object of type '{}'", + static_cast(obj.type())); contactgroup const& tmpl(static_cast(obj)); MRG_DEFAULT(_alias); @@ -175,7 +175,7 @@ bool contactgroup::parse(char const* key, char const* value) { * @return The alias. */ std::string const& contactgroup::alias() const throw() { - return (_alias); + return _alias; } /** @@ -232,7 +232,7 @@ set_string const& contactgroup::members() const throw() { */ bool contactgroup::_set_alias(std::string const& value) { _alias = value; - return (true); + return true; } /** @@ -244,7 +244,7 @@ bool contactgroup::_set_alias(std::string const& value) { */ bool contactgroup::_set_contactgroup_members(std::string const& value) { _contactgroup_members = value; - return (true); + return true; } /** diff --git a/engine/src/configuration/group.cc b/engine/src/configuration/group.cc index 03a19046b06..3f13e1a4f17 100644 --- a/engine/src/configuration/group.cc +++ b/engine/src/configuration/group.cc @@ -17,7 +17,6 @@ * */ #include "com/centreon/engine/configuration/group.hh" -#include "com/centreon/engine/string.hh" using namespace com::centreon::engine::configuration; @@ -72,17 +71,88 @@ group& group::operator=(group const& other) { * @return This object. */ template -group& group::operator=(std::string const& other) { +group& group::operator=(const std::string& other) { _data.clear(); if (!other.empty()) { if (other[0] == '+') { _is_inherit = true; - string::split(other.substr(1), _data, ','); + std::string_view sv = other; + auto split = absl::StrSplit(sv.substr(1), ','); + for (auto& s : split) { + std::string_view str = absl::StripAsciiWhitespace(s); + _data.push_back(std::string(str.data(), str.size())); + } } else if (other == "null") _is_null = true; else { _is_inherit = false; - string::split(other, _data, ','); + auto split = absl::StrSplit(other, ','); + for (auto& s : split) { + std::string_view str = absl::StripAsciiWhitespace(s); + _data.push_back(std::string(str.data(), str.size())); + } + } + } + _is_set = true; + return *this; +} + +template <> +group>& group>::operator=( + const std::string& other) { + _data.clear(); + if (!other.empty()) { + if (other[0] == '+') { + _is_inherit = true; + std::string_view sv = other; + auto split = absl::StrSplit(sv.substr(1), ','); + for (auto& s : split) { + std::string_view str = absl::StripAsciiWhitespace(s); + _data.insert(std::string(str.data(), str.size())); + } + } else if (other == "null") + _is_null = true; + else { + _is_inherit = false; + auto split = absl::StrSplit(other, ','); + for (auto& s : split) { + std::string_view str = absl::StripAsciiWhitespace(s); + _data.insert(std::string(str.data(), str.size())); + } + } + } + _is_set = true; + return *this; +} + +using set_pair_str = std::set>; +template <> +group& group::operator=(const std::string& other) { + _data.clear(); + auto spl = [this](std::string_view sv) { + auto values = absl::StrSplit(sv, ','); + for (auto it = values.begin(); it != values.end(); ++it) { + auto key = *it; + key = absl::StripAsciiWhitespace(key); + ++it; + if (it == values.end()) + break; + auto value = *it; + value = absl::StripAsciiWhitespace(value); + _data.insert({std::string(key.data(), key.size()), + std::string(value.data(), value.size())}); + } + }; + if (!other.empty()) { + if (other[0] == '+') { + _is_inherit = true; + std::string_view sv = other; + spl(sv.substr(1)); + } else if (other == "null") + _is_null = true; + else { + _is_inherit = false; + spl(other); } } _is_set = true; diff --git a/engine/src/configuration/host.cc b/engine/src/configuration/host.cc index 57870514bd5..a3b607c5859 100644 --- a/engine/src/configuration/host.cc +++ b/engine/src/configuration/host.cc @@ -26,7 +26,6 @@ #include "com/centreon/engine/globals.hh" #include "com/centreon/engine/host.hh" #include "com/centreon/engine/logging/logger.hh" -#include "com/centreon/engine/string.hh" extern int config_warnings; extern int config_errors; diff --git a/engine/src/configuration/hostdependency.cc b/engine/src/configuration/hostdependency.cc index 158b378384f..5ee1dcefb60 100644 --- a/engine/src/configuration/hostdependency.cc +++ b/engine/src/configuration/hostdependency.cc @@ -22,7 +22,6 @@ #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" #include "com/centreon/engine/logging/logger.hh" -#include "com/centreon/engine/string.hh" extern int config_warnings; extern int config_errors; diff --git a/engine/src/configuration/hostescalation.cc b/engine/src/configuration/hostescalation.cc index 602616c8245..2282c98aa66 100644 --- a/engine/src/configuration/hostescalation.cc +++ b/engine/src/configuration/hostescalation.cc @@ -19,7 +19,6 @@ */ #include "com/centreon/engine/configuration/hostescalation.hh" #include "com/centreon/engine/exceptions/error.hh" -#include "com/centreon/engine/string.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; diff --git a/engine/src/configuration/object.cc b/engine/src/configuration/object.cc index ae0db4ec86f..099799083ac 100644 --- a/engine/src/configuration/object.cc +++ b/engine/src/configuration/object.cc @@ -35,7 +35,6 @@ #include "com/centreon/engine/configuration/tag.hh" #include "com/centreon/engine/configuration/timeperiod.hh" #include "com/centreon/engine/exceptions/error.hh" -#include "com/centreon/engine/string.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; diff --git a/engine/src/configuration/parser.cc b/engine/src/configuration/parser.cc index 112ef90061c..1be2eb4a267 100644 --- a/engine/src/configuration/parser.cc +++ b/engine/src/configuration/parser.cc @@ -20,7 +20,6 @@ #include "com/centreon/engine/configuration/parser.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/string.hh" #include "com/centreon/io/directory_entry.hh" using namespace com::centreon; diff --git a/engine/src/configuration/service.cc b/engine/src/configuration/service.cc index 28951a26cf6..62559e48250 100644 --- a/engine/src/configuration/service.cc +++ b/engine/src/configuration/service.cc @@ -26,7 +26,6 @@ #include "com/centreon/engine/globals.hh" #include "com/centreon/engine/host.hh" #include "com/centreon/engine/logging/logger.hh" -#include "com/centreon/engine/string.hh" extern int config_warnings; extern int config_errors; diff --git a/engine/src/configuration/servicedependency.cc b/engine/src/configuration/servicedependency.cc index ec5d032bf11..9def6367153 100644 --- a/engine/src/configuration/servicedependency.cc +++ b/engine/src/configuration/servicedependency.cc @@ -22,7 +22,6 @@ #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" #include "com/centreon/engine/logging/logger.hh" -#include "com/centreon/engine/string.hh" extern int config_warnings; extern int config_errors; diff --git a/engine/src/configuration/serviceescalation.cc b/engine/src/configuration/serviceescalation.cc index 820af49c438..e1806cbed81 100644 --- a/engine/src/configuration/serviceescalation.cc +++ b/engine/src/configuration/serviceescalation.cc @@ -21,7 +21,6 @@ #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" #include "com/centreon/engine/logging/logger.hh" -#include "com/centreon/engine/string.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; diff --git a/engine/src/configuration/state.cc b/engine/src/configuration/state.cc index cf70aed70db..290fa163782 100644 --- a/engine/src/configuration/state.cc +++ b/engine/src/configuration/state.cc @@ -21,7 +21,6 @@ #include "com/centreon/engine/broker.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/string.hh" #include "com/centreon/io/file_entry.hh" #include "compatibility/locations.h" diff --git a/engine/src/configuration/timeperiod.cc b/engine/src/configuration/timeperiod.cc index 5d20d96030d..a2eeff46c80 100644 --- a/engine/src/configuration/timeperiod.cc +++ b/engine/src/configuration/timeperiod.cc @@ -21,7 +21,6 @@ #include "com/centreon/engine/common.hh" #include "com/centreon/engine/exceptions/error.hh" -#include "com/centreon/engine/string.hh" #include "com/centreon/engine/timerange.hh" using namespace com::centreon; From e116fb193bf2210ea443ef23ffd40d18a58cf69a Mon Sep 17 00:00:00 2001 From: David Boucher Date: Wed, 22 May 2024 15:36:12 +0200 Subject: [PATCH 03/21] fix(engine/string): string lib removed from configuration --- .../engine/configuration/file_info.hh | 38 +- .../com/centreon/engine/configuration/host.hh | 47 +- .../engine/configuration/serviceescalation.hh | 148 +++-- .../engine/configuration/timeperiod.hh | 40 +- .../applier/servicedependency.cc | 11 - engine/src/configuration/host.cc | 117 ++-- engine/src/configuration/hostdependency.cc | 75 ++- engine/src/configuration/hostescalation.cc | 505 ------------------ engine/src/configuration/object.cc | 11 +- engine/src/configuration/service.cc | 217 ++------ engine/src/configuration/servicedependency.cc | 122 ++--- engine/src/configuration/serviceescalation.cc | 144 ++--- engine/src/configuration/timeperiod.cc | 71 +-- 13 files changed, 414 insertions(+), 1132 deletions(-) delete mode 100644 engine/src/configuration/hostescalation.cc diff --git a/engine/inc/com/centreon/engine/configuration/file_info.hh b/engine/inc/com/centreon/engine/configuration/file_info.hh index a01262ead8b..79225c399d8 100644 --- a/engine/inc/com/centreon/engine/configuration/file_info.hh +++ b/engine/inc/com/centreon/engine/configuration/file_info.hh @@ -1,21 +1,21 @@ -/* -** Copyright 2011-2013 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 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 + * . + */ #ifndef CCE_CONFIGURATION_FILE_INFO_HH #define CCE_CONFIGURATION_FILE_INFO_HH @@ -59,6 +59,6 @@ class file_info { }; } // namespace configuration -} +} // namespace com::centreon::engine #endif // !CCE_CONFIGURATION_FILE_INFO_HH diff --git a/engine/inc/com/centreon/engine/configuration/host.hh b/engine/inc/com/centreon/engine/configuration/host.hh index 93f831ad357..1db64c940a8 100644 --- a/engine/inc/com/centreon/engine/configuration/host.hh +++ b/engine/inc/com/centreon/engine/configuration/host.hh @@ -1,21 +1,21 @@ -/* -** 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_HOST_HH #define CCE_CONFIGURATION_HOST_HH @@ -68,8 +68,9 @@ class host : public object { set_string const& contacts() const noexcept; point_2d const& coords_2d() const noexcept; point_3d const& coords_3d() 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; @@ -183,7 +184,7 @@ class host : public object { group _contacts; opt _coords_2d; opt _coords_3d; - map_customvar _customvariables; + std::unordered_map _customvariables; std::string _display_name; std::string _event_handler; opt _event_handler_enabled; @@ -197,7 +198,7 @@ class host : public object { std::string _host_name; std::string _icon_image; std::string _icon_image_alt; - opt _initial_state; + uint32_t _initial_state; opt _low_flap_threshold; opt _max_check_attempts; std::string _notes; @@ -225,7 +226,7 @@ class host : public object { typedef std::shared_ptr host_ptr; typedef std::list list_host; -typedef std::set set_host; +using set_host = std::set; } // namespace configuration } // namespace com::centreon::engine diff --git a/engine/inc/com/centreon/engine/configuration/serviceescalation.hh b/engine/inc/com/centreon/engine/configuration/serviceescalation.hh index 71502eeaf0c..e66e8fae8ba 100644 --- a/engine/inc/com/centreon/engine/configuration/serviceescalation.hh +++ b/engine/inc/com/centreon/engine/configuration/serviceescalation.hh @@ -1,21 +1,21 @@ -/* -** Copyright 2011-2013,2017 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,2017 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_SERVICEESCALATION_HH #define CCE_CONFIGURATION_SERVICEESCALATION_HH @@ -25,10 +25,35 @@ #include "com/centreon/engine/opt.hh" #include "com/centreon/engine/shared.hh" -namespace com::centreon::engine { +namespace com::centreon::engine::configuration { -namespace configuration { class serviceescalation : public object { + typedef bool (*setter_func)(serviceescalation&, char const*); + + group _contactgroups; + opt _escalation_options; + opt _escalation_period; + opt _first_notification; + group _hostgroups; + group _hosts; + opt _last_notification; + opt _notification_interval; + group _servicegroups; + group _service_description; + static std::unordered_map const _setters; + Uuid _uuid; + + bool _set_contactgroups(std::string const& value); + bool _set_escalation_options(std::string const& value); + bool _set_escalation_period(std::string const& value); + bool _set_first_notification(unsigned int value); + bool _set_hostgroups(std::string const& value); + bool _set_hosts(std::string const& value); + bool _set_last_notification(unsigned int value); + bool _set_notification_interval(unsigned int value); + bool _set_servicegroups(std::string const& value); + bool _set_service_description(std::string const& value); + public: enum action_on { none = 0, @@ -42,73 +67,44 @@ class serviceescalation : public object { serviceescalation(); serviceescalation(serviceescalation const& right); - ~serviceescalation() throw() override; + ~serviceescalation() noexcept override = default; serviceescalation& operator=(serviceescalation const& right); - bool operator==(serviceescalation const& right) const throw(); - bool operator!=(serviceescalation const& right) const throw(); + bool operator==(serviceescalation const& right) const noexcept; + bool operator!=(serviceescalation const& right) const noexcept; bool operator<(serviceescalation const& right) const; void check_validity() const override; - key_type const& key() const throw(); + key_type const& key() const noexcept; void merge(object const& obj) override; bool parse(char const* key, char const* value) override; - set_string& contactgroups() throw(); - set_string const& contactgroups() const throw(); - bool contactgroups_defined() const throw(); - void escalation_options(unsigned int options) throw(); - unsigned short escalation_options() const throw(); + set_string& contactgroups() noexcept; + set_string const& contactgroups() const noexcept; + bool contactgroups_defined() const noexcept; + void escalation_options(unsigned int options) noexcept; + unsigned short escalation_options() const noexcept; void escalation_period(std::string const& period); - std::string const& escalation_period() const throw(); - bool escalation_period_defined() const throw(); - void first_notification(unsigned int n) throw(); - unsigned int first_notification() const throw(); - list_string& hostgroups() throw(); - list_string const& hostgroups() const throw(); - list_string& hosts() throw(); - list_string const& hosts() const throw(); - void last_notification(unsigned int options) throw(); - unsigned int last_notification() const throw(); - void notification_interval(unsigned int interval) throw(); - unsigned int notification_interval() const throw(); - bool notification_interval_defined() const throw(); - list_string& servicegroups() throw(); - list_string const& servicegroups() const throw(); - list_string& service_description() throw(); - list_string const& service_description() const throw(); + std::string const& escalation_period() const noexcept; + bool escalation_period_defined() const noexcept; + void first_notification(unsigned int n) noexcept; + unsigned int first_notification() const noexcept; + list_string& hostgroups() noexcept; + list_string const& hostgroups() const noexcept; + list_string& hosts() noexcept; + list_string const& hosts() const noexcept; + void last_notification(unsigned int options) noexcept; + unsigned int last_notification() const noexcept; + void notification_interval(unsigned int interval) noexcept; + unsigned int notification_interval() const noexcept; + bool notification_interval_defined() const noexcept; + list_string& servicegroups() noexcept; + list_string const& servicegroups() const noexcept; + list_string& service_description() noexcept; + list_string const& service_description() const noexcept; Uuid const& uuid() const; - - private: - typedef bool (*setter_func)(serviceescalation&, char const*); - - bool _set_contactgroups(std::string const& value); - bool _set_escalation_options(std::string const& value); - bool _set_escalation_period(std::string const& value); - bool _set_first_notification(unsigned int value); - bool _set_hostgroups(std::string const& value); - bool _set_hosts(std::string const& value); - bool _set_last_notification(unsigned int value); - bool _set_notification_interval(unsigned int value); - bool _set_servicegroups(std::string const& value); - bool _set_service_description(std::string const& value); - - group _contactgroups; - opt _escalation_options; - opt _escalation_period; - opt _first_notification; - group _hostgroups; - group _hosts; - opt _last_notification; - opt _notification_interval; - group _servicegroups; - group _service_description; - static std::unordered_map const _setters; - Uuid _uuid; }; typedef std::shared_ptr serviceescalation_ptr; typedef std::set set_serviceescalation; -} // namespace configuration - -} +} // namespace com::centreon::engine::configuration #endif // !CCE_CONFIGURATION_SERVICEESCALATION_HH diff --git a/engine/inc/com/centreon/engine/configuration/timeperiod.hh b/engine/inc/com/centreon/engine/configuration/timeperiod.hh index b4aeb6ccfa9..e16117080a3 100644 --- a/engine/inc/com/centreon/engine/configuration/timeperiod.hh +++ b/engine/inc/com/centreon/engine/configuration/timeperiod.hh @@ -1,21 +1,21 @@ -/* -** Copyright 2011-2013,2017 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,2017 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_TIMEPERIOD_HH #define CCE_CONFIGURATION_TIMEPERIOD_HH @@ -64,7 +64,7 @@ class timeperiod : public object { bool _add_week_day(std::string const& key, std::string const& value); static bool _build_timeranges(std::string const& line, timerange_list& timeranges); - static bool _build_time_t(std::string const& time_str, unsigned long& ret); + static bool _build_time_t(std::string_view time_str, unsigned long& ret); static bool _has_similar_daterange(std::list const& lst, daterange const& range) throw(); static bool _get_month_id(std::string const& name, unsigned int& id); @@ -85,6 +85,6 @@ typedef std::shared_ptr timeperiod_ptr; typedef std::set set_timeperiod; } // namespace configuration -} +} // namespace com::centreon::engine #endif // !CCE_CONFIGURATION_TIMEPERIOD_HH diff --git a/engine/src/configuration/applier/servicedependency.cc b/engine/src/configuration/applier/servicedependency.cc index 72fee7b50ad..0639733eb8c 100644 --- a/engine/src/configuration/applier/servicedependency.cc +++ b/engine/src/configuration/applier/servicedependency.cc @@ -25,7 +25,6 @@ #include "com/centreon/engine/configuration/servicedependency.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/logging/logger.hh" using namespace com::centreon::engine::configuration; @@ -61,12 +60,6 @@ void applier::servicedependency::add_object( << obj.hosts().front() << "'"; // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Creating new service dependency of service '" - << obj.dependent_service_description().front() << "' of host '" - << obj.dependent_hosts().front() << "' on service '" - << obj.service_description().front() << "' of host '" - << obj.hosts().front() << "'."; config_logger->debug( "Creating new service dependency of service '{}' of host '{}' on service " "'{}' of host '{}'.", @@ -236,8 +229,6 @@ void applier::servicedependency::modify_object( void applier::servicedependency::remove_object( configuration::servicedependency const& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Removing a service dependency."; config_logger->debug("Removing a service dependency."); // Find service dependency. @@ -264,8 +255,6 @@ void applier::servicedependency::remove_object( void applier::servicedependency::resolve_object( configuration::servicedependency const& obj) { // Logging. - engine_logger(logging::dbg_config, logging::more) - << "Resolving a service dependency."; config_logger->debug("Resolving a service dependency."); // Find service dependency. diff --git a/engine/src/configuration/host.cc b/engine/src/configuration/host.cc index a3b607c5859..2cd983842a6 100644 --- a/engine/src/configuration/host.cc +++ b/engine/src/configuration/host.cc @@ -25,14 +25,14 @@ #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" #include "com/centreon/engine/host.hh" -#include "com/centreon/engine/logging/logger.hh" +#include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; extern int config_errors; -using namespace com::centreon; using namespace com::centreon::engine::configuration; -using namespace com::centreon::engine::logging; +using com::centreon::common::log_v2::log_v2; +using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) &object::setter::generic @@ -122,7 +122,8 @@ static unsigned short const default_flap_detection_options(host::up | host::unreachable); static unsigned int const default_freshness_threshold(0); static unsigned int const default_high_flap_threshold(0); -static unsigned short const default_initial_state(engine::host::state_up); +static unsigned short const default_initial_state( + com::centreon::engine::host::state_up); static unsigned int const default_low_flap_threshold(0); static unsigned int const default_max_check_attempts(3); static bool const default_notifications_enabled(true); @@ -434,10 +435,9 @@ bool host::operator<(host const& other) const noexcept { */ void host::check_validity() const { if (_host_name.empty()) - throw engine_error() << "Host has no name (property 'host_name')"; + throw msg_fmt("Host has no name (property 'host_name')"); if (_address.empty()) - throw engine_error() << "Host '" << _host_name - << "' has no address (property 'address')"; + throw msg_fmt("Host '{}' has no address (property 'address')", _host_name); } /** @@ -456,7 +456,8 @@ host::key_type host::key() const noexcept { */ void host::merge(object const& obj) { if (obj.type() != _type) - throw engine_error() << "Cannot merge host with '" << obj.type() << "'"; + throw msg_fmt("Cannot merge host with object of type '{}'", + static_cast(obj.type())); host const& tmpl(static_cast(obj)); MRG_OPTION(_acknowledgement_timeout); @@ -486,7 +487,6 @@ void host::merge(object const& obj) { MRG_DEFAULT(_host_name); MRG_DEFAULT(_icon_image); MRG_DEFAULT(_icon_image_alt); - MRG_OPTION(_initial_state); MRG_OPTION(_low_flap_threshold); MRG_OPTION(_max_check_attempts); MRG_DEFAULT(_notes); @@ -525,7 +525,7 @@ bool host::parse(char const* key, char const* value) { if (it != _setters.end()) return (it->second)(*this, value); if (key[0] == '_') { - map_customvar::iterator it(_customvariables.find(key + 1)); + auto it = _customvariables.find(key + 1); if (it == _customvariables.end()) _customvariables[key + 1] = customvariable(value); else @@ -658,7 +658,8 @@ point_3d const& host::coords_3d() const noexcept { * * @return The customvariables. */ -engine::map_customvar const& host::customvariables() const noexcept { +const std::unordered_map& +host::customvariables() const noexcept { return _customvariables; } @@ -667,7 +668,8 @@ engine::map_customvar const& host::customvariables() const noexcept { * * @return The customvariables. */ -engine::map_customvar& host::customvariables() noexcept { +std::unordered_map& +host::customvariables() noexcept { return _customvariables; } @@ -1176,18 +1178,17 @@ bool host::_set_contacts(std::string const& value) { * @return True on success, otherwise false. */ bool host::_set_coords_2d(std::string const& value) { - std::list coords; - string::split(value, coords, ','); + std::list coords = absl::StrSplit(value, ','); if (coords.size() != 2) return false; int x; - if (!string::to(string::trim(coords.front()).c_str(), x)) + if (!absl::SimpleAtoi(coords.front(), &x)) return false; coords.pop_front(); int y; - if (!string::to(string::trim(coords.front()).c_str(), y)) + if (!absl::SimpleAtoi(coords.front(), &y)) return false; _coords_2d = point_2d(x, y); @@ -1202,23 +1203,22 @@ bool host::_set_coords_2d(std::string const& value) { * @return True on success, otherwise false. */ bool host::_set_coords_3d(std::string const& value) { - std::list coords; - string::split(value, coords, ','); + std::list coords = absl::StrSplit(value, ','); if (coords.size() != 3) return false; double x; - if (!string::to(string::trim(coords.front()).c_str(), x)) + if (!absl::SimpleAtod(coords.front(), &x)) return false; coords.pop_front(); double y; - if (!string::to(string::trim(coords.front()).c_str(), y)) + if (!absl::SimpleAtod(coords.front(), &y)) return false; coords.pop_front(); double z; - if (!string::to(string::trim(coords.front()).c_str(), z)) + if (!absl::SimpleAtod(coords.front(), &z)) return false; _coords_3d = point_3d(x, y, z); @@ -1268,11 +1268,7 @@ bool host::_set_event_handler_enabled(bool value) { * * @return True on success, otherwise false. */ -bool host::_set_failure_prediction_enabled(bool value) { - (void)value; - engine_logger(log_verification_error, basic) - << "Warning: host failure_prediction_enabled is deprecated" - << " This option will not be supported in 20.04."; +bool host::_set_failure_prediction_enabled(bool value [[maybe_unused]]) { config_logger->warn( "Warning: host failure_prediction_enabled is deprecated This option will " "not be supported in 20.04."); @@ -1287,11 +1283,8 @@ bool host::_set_failure_prediction_enabled(bool value) { * * @return True on success, otherwise false. */ -bool host::_set_failure_prediction_options(std::string const& value) { - (void)value; - engine_logger(log_verification_error, basic) - << "Warning: service failure_prediction_options is deprecated" - << " This option will not be supported in 20.04."; +bool host::_set_failure_prediction_options(std::string const& value + [[maybe_unused]]) { config_logger->warn( "Warning: service failure_prediction_options is deprecated This option " "will not be supported in 20.04."); @@ -1332,20 +1325,18 @@ bool host::_set_flap_detection_enabled(bool value) { */ bool host::_set_flap_detection_options(std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "o" || *it == "up") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "o" || v == "up") options |= up; - else if (*it == "d" || *it == "down") + else if (v == "d" || v == "down") options |= down; - else if (*it == "u" || *it == "unreachable") + else if (v == "u" || v == "unreachable") options |= unreachable; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = up | down | unreachable; else return false; @@ -1446,8 +1437,8 @@ bool host::_set_icon_image_alt(std::string const& value) { * @return True on success, otherwise false. */ bool host::_set_initial_state(std::string const& value) { - std::string data(value); - string::trim(data); + std::string_view data(value); + data = absl::StripAsciiWhitespace(data); if (data == "o" || data == "up") _initial_state = engine::host::state_up; else if (data == "d" || data == "down") @@ -1542,24 +1533,22 @@ bool host::_set_notification_interval(unsigned int value) { */ bool host::_set_notification_options(std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "d" || *it == "down") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "d" || v == "down") options |= down; - else if (*it == "u" || *it == "unreachable") + else if (v == "u" || v == "unreachable") options |= unreachable; - else if (*it == "r" || *it == "recovery") + else if (v == "r" || v == "recovery") options |= up; - else if (*it == "f" || *it == "flapping") + else if (v == "f" || v == "flapping") options |= flapping; - else if (*it == "s" || *it == "downtime") + else if (v == "s" || v == "downtime") options |= downtime; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = down | unreachable | up | flapping | downtime; else return false; @@ -1673,20 +1662,18 @@ bool host::_set_recovery_notification_delay(unsigned int value) { */ bool host::_set_stalking_options(std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "o" || *it == "up") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "o" || v == "up") options |= up; - else if (*it == "d" || *it == "down") + else if (v == "d" || v == "down") options |= down; - else if (*it == "u" || *it == "unreachable") + else if (v == "u" || v == "unreachable") options |= unreachable; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = up | down | unreachable; else return false; diff --git a/engine/src/configuration/hostdependency.cc b/engine/src/configuration/hostdependency.cc index 5ee1dcefb60..9083fd21d4b 100644 --- a/engine/src/configuration/hostdependency.cc +++ b/engine/src/configuration/hostdependency.cc @@ -19,9 +19,8 @@ */ #include "com/centreon/engine/configuration/hostdependency.hh" -#include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/logging/logger.hh" +#include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; extern int config_errors; @@ -29,6 +28,7 @@ extern int config_errors; using namespace com::centreon; using namespace com::centreon::engine::configuration; using namespace com::centreon::engine::logging; +using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ &object::setter::generic @@ -179,15 +179,15 @@ bool hostdependency::operator<(hostdependency const& right) const { */ void hostdependency::check_validity() const { if (_hosts->empty() && _hostgroups->empty()) - throw(engine_error() << "Host dependency is not attached to any " - << "host or host group (properties 'host_name' or " - << "'hostgroup_name', respectively)"); + throw msg_fmt( + "Host dependency is not attached to any host or host group (properties " + "'host_name' or 'hostgroup_name', respectively)"); if (_dependent_hosts->empty() && _dependent_hostgroups->empty()) - throw(engine_error() - << "Host dependency is not attached to any " - << "dependent host or dependent host group (properties " - << "'dependent_host_name' or 'dependent_hostgroup_name', " - << "respectively)"); + throw msg_fmt( + "Host dependency is not attached to any " + "dependent host or dependent host group (properties " + "'dependent_host_name' or 'dependent_hostgroup_name', " + "respectively)"); if (!_execution_failure_options && !_notification_failure_options) { ++config_warnings; @@ -196,16 +196,11 @@ void hostdependency::check_validity() const { std::string dependend_host_name(!_dependent_hosts->empty() ? *_dependent_hosts->begin() : *_dependent_hostgroups->begin()); - engine_logger(log_config_warning, basic) - << "Warning: Ignoring lame host dependency of '" << dependend_host_name - << "' on host/hostgroups '" << host_name << "'."; config_logger->warn( "Warning: Ignoring lame host dependency of '{}' on host/hostgroups " "'{}'.", dependend_host_name, host_name); } - - return; } /** @@ -224,8 +219,9 @@ hostdependency::key_type const& hostdependency::key() const throw() { */ void hostdependency::merge(object const& obj) { if (obj.type() != _type) - throw(engine_error() << "Cannot merge host dependency with '" << obj.type() - << "'"); + throw exceptions::msg_fmt( + "Cannot merge host dependency with object of type '{}'", + static_cast(obj.type())); hostdependency const& tmpl(static_cast(obj)); MRG_DEFAULT(_dependency_period); @@ -261,7 +257,6 @@ bool hostdependency::parse(char const* key, char const* value) { */ void hostdependency::dependency_period(std::string const& period) { _dependency_period = period; - return; } /** @@ -281,7 +276,6 @@ std::string const& hostdependency::dependency_period() const throw() { void hostdependency::dependency_type( hostdependency::dependency_kind type) throw() { _dependency_type = type; - return; } /** @@ -337,7 +331,6 @@ set_string const& hostdependency::dependent_hosts() const throw() { */ void hostdependency::execution_failure_options(unsigned int options) throw() { _execution_failure_options.set(options); - return; } /** @@ -392,7 +385,6 @@ set_string const& hostdependency::hosts() const throw() { */ void hostdependency::inherits_parent(bool inherit) throw() { _inherits_parent = inherit; - return; } /** @@ -412,7 +404,6 @@ bool hostdependency::inherits_parent() const throw() { void hostdependency::notification_failure_options( unsigned int options) throw() { _notification_failure_options.set(options); - return; } /** @@ -469,22 +460,20 @@ bool hostdependency::_set_dependent_hosts(std::string const& value) { */ bool hostdependency::_set_execution_failure_options(std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "o" || *it == "up") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "o" || v == "up") options |= up; - else if (*it == "d" || *it == "down") + else if (v == "d" || v == "down") options |= down; - else if (*it == "u" || *it == "unreachable") + else if (v == "u" || v == "unreachable") options |= unreachable; - else if (*it == "p" || *it == "pending") + else if (v == "p" || v == "pending") options |= pending; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = up | down | unreachable | pending; else return false; @@ -539,22 +528,20 @@ bool hostdependency::_set_inherits_parent(bool value) { bool hostdependency::_set_notification_failure_options( std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "o" || *it == "up") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "o" || v == "up") options |= up; - else if (*it == "d" || *it == "down") + else if (v == "d" || v == "down") options |= down; - else if (*it == "u" || *it == "unreachable") + else if (v == "u" || v == "unreachable") options |= unreachable; - else if (*it == "p" || *it == "pending") + else if (v == "p" || v == "pending") options |= pending; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = up | down | unreachable | pending; else return false; diff --git a/engine/src/configuration/hostescalation.cc b/engine/src/configuration/hostescalation.cc deleted file mode 100644 index 2282c98aa66..00000000000 --- a/engine/src/configuration/hostescalation.cc +++ /dev/null @@ -1,505 +0,0 @@ -/** - * Copyright 2011-2013,2015 Merethis - * Copyright 2017-2024 Centreon - * - * 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/hostescalation.hh" -#include "com/centreon/engine/exceptions/error.hh" - -using namespace com::centreon; -using namespace com::centreon::engine::configuration; - -#define SETTER(type, method) \ - &object::setter::generic - -std::unordered_map const hostescalation::_setters{ - {"hostgroup", SETTER(std::string const&, _set_hostgroups)}, - {"hostgroups", SETTER(std::string const&, _set_hostgroups)}, - {"hostgroup_name", SETTER(std::string const&, _set_hostgroups)}, - {"host", SETTER(std::string const&, _set_hosts)}, - {"host_name", SETTER(std::string const&, _set_hosts)}, - {"contact_groups", SETTER(std::string const&, _set_contactgroups)}, - {"escalation_options", SETTER(std::string const&, _set_escalation_options)}, - {"escalation_period", SETTER(std::string const&, _set_escalation_period)}, - {"first_notification", SETTER(uint32_t, _set_first_notification)}, - {"last_notification", SETTER(uint32_t, _set_last_notification)}, - {"notification_interval", SETTER(uint32_t, _set_notification_interval)}}; - -// Default values. -static unsigned short const default_escalation_options(hostescalation::none); -static unsigned int const default_first_notification(-2); -static unsigned int const default_last_notification(-2); -static unsigned int const default_notification_interval(0); - -/** - * Default constructor. - */ -hostescalation::hostescalation() - : object(object::hostescalation), - _escalation_options(default_escalation_options), - _first_notification(default_first_notification), - _last_notification(default_last_notification), - _notification_interval(default_notification_interval) {} - -/** - * Copy constructor. - * - * @param[in] right The hostescalation to copy. - */ -hostescalation::hostescalation(hostescalation const& right) : object(right) { - operator=(right); -} - -/** - * Destructor. - */ -hostescalation::~hostescalation() throw() {} - -/** - * Copy constructor. - * - * @param[in] right The hostescalation to copy. - * - * @return This hostescalation. - */ -hostescalation& hostescalation::operator=(hostescalation const& right) { - if (this != &right) { - object::operator=(right); - _contactgroups = right._contactgroups; - _escalation_options = right._escalation_options; - _escalation_period = right._escalation_period; - _first_notification = right._first_notification; - _hostgroups = right._hostgroups; - _hosts = right._hosts; - _last_notification = right._last_notification; - _notification_interval = right._notification_interval; - _uuid = right._uuid; - } - return *this; -} - -/** - * Equal operator. - * - * @param[in] right The hostescalation to compare. - * - * @return True if is the same hostescalation, otherwise false. - */ -bool hostescalation::operator==(hostescalation const& right) const throw() { - return (object::operator==(right) && _contactgroups == right._contactgroups && - _escalation_options == right._escalation_options && - _escalation_period == right._escalation_period && - _first_notification == right._first_notification && - _hostgroups == right._hostgroups && _hosts == right._hosts && - _last_notification == right._last_notification && - _notification_interval == right._notification_interval); -} - -/** - * Equal operator. - * - * @param[in] right The hostescalation to compare. - * - * @return True if is not the same hostescalation, otherwise false. - */ -bool hostescalation::operator!=(hostescalation const& right) const throw() { - return !operator==(right); -} - -/** - * Less-than operator. - * - * @param[in] right Object to compare to. - * - * @return True if this object is less than right. - */ -bool hostescalation::operator<(hostescalation const& right) const { - if (_hosts != right._hosts) - return _hosts < right._hosts; - else if (_hostgroups != right._hostgroups) - return _hostgroups < right._hostgroups; - else if (_contactgroups != right._contactgroups) - return _contactgroups < right._contactgroups; - else if (_escalation_options != right._escalation_options) - return _escalation_options < right._escalation_options; - else if (_escalation_period != right._escalation_period) - return _escalation_period < right._escalation_period; - else if (_first_notification != right._first_notification) - return _first_notification < right._first_notification; - else if (_last_notification != right._last_notification) - return _last_notification < right._last_notification; - return _notification_interval < right._notification_interval; -} - -/** - * @brief Check if the object is valid. - * - * If the object is not valid, an exception is thrown. - */ -void hostescalation::check_validity() const { - if (_hosts->empty() && _hostgroups->empty()) - throw(engine_error() << "Host escalation is not attached to any " - << "host or host group (properties 'host_name' or " - << "'hostgroup_name', respectively)"); -} - -/** - * Get host escalation key. - * - * @return This object. - */ -hostescalation::key_type const& hostescalation::key() const throw() { - return *this; -} - -/** - * Merge object. - * - * @param[in] obj The object to merge. - */ -void hostescalation::merge(object const& obj) { - if (obj.type() != _type) - throw(engine_error() << "Cannot merge host escalation with '" << obj.type() - << "'"); - hostescalation const& tmpl(static_cast(obj)); - - MRG_INHERIT(_contactgroups); - MRG_OPTION(_escalation_options); - MRG_OPTION(_escalation_period); - MRG_OPTION(_first_notification); - MRG_INHERIT(_hostgroups); - MRG_INHERIT(_hosts); - MRG_OPTION(_last_notification); - MRG_OPTION(_notification_interval); -} - -/** - * Parse and set the hostescalation property. - * - * @param[in] key The property name. - * @param[in] value The property value. - * - * @return True on success, otherwise false. - */ -bool hostescalation::parse(char const* key, char const* value) { - std::unordered_map::const_iterator - it{_setters.find(key)}; - if (it != _setters.end()) - return (it->second)(*this, value); - return false; -} - -/** - * Get contact groups. - * - * @return The contact groups. - */ -set_string& hostescalation::contactgroups() throw() { - return *_contactgroups; -} - -/** - * Get contactgroups. - * - * @return The contactgroups. - */ -set_string const& hostescalation::contactgroups() const throw() { - return *_contactgroups; -} - -/** - * Check if contact groups were defined. - * - * @return True if contact groups were defined. - */ -bool hostescalation::contactgroups_defined() const throw() { - return _contactgroups.is_set(); -} - -/** - * Set escalation options. - * - * @param[in] options New escalation options. - */ -void hostescalation::escalation_options(unsigned short options) throw() { - _escalation_options = options; - return; -} - -/** - * Get escalation_options. - * - * @return The escalation_options. - */ -unsigned short hostescalation::escalation_options() const throw() { - return _escalation_options; -} - -/** - * Set the escalation period. - * - * @param[in] period New escalation period. - */ -void hostescalation::escalation_period(std::string const& period) { - _escalation_period = period; - return; -} - -/** - * Get escalation_period. - * - * @return The escalation_period. - */ -std::string const& hostescalation::escalation_period() const throw() { - return _escalation_period; -} - -/** - * Check if escalation period was defined. - * - * @return True if escalation period was defined. - */ -bool hostescalation::escalation_period_defined() const throw() { - return _escalation_period.is_set(); -} - -/** - * Set the first notification. - * - * @param[in] n New first notification number. - */ -void hostescalation::first_notification(unsigned int n) throw() { - _first_notification = n; - return; -} - -/** - * Get first_notification. - * - * @return The first_notification. - */ -unsigned int hostescalation::first_notification() const throw() { - return _first_notification; -} - -/** - * Get host groups. - * - * @return The host groups. - */ -set_string& hostescalation::hostgroups() throw() { - return *_hostgroups; -} - -/** - * Get hostgroups. - * - * @return The hostgroups. - */ -set_string const& hostescalation::hostgroups() const throw() { - return *_hostgroups; -} - -/** - * Get hosts. - * - * @return The hosts. - */ -set_string& hostescalation::hosts() throw() { - return *_hosts; -} - -/** - * Get hosts. - * - * @return The hosts. - */ -set_string const& hostescalation::hosts() const throw() { - return *_hosts; -} - -/** - * Set the last notification. - * - * @param[in] n New last notification number. - */ -void hostescalation::last_notification(unsigned int n) throw() { - _last_notification = n; - return; -} - -/** - * Get last_notification. - * - * @return The last_notification. - */ -unsigned int hostescalation::last_notification() const throw() { - return _last_notification; -} - -/** - * Set the notification interval. - * - * @param[in] interval New notification interval. - */ -void hostescalation::notification_interval(unsigned int interval) { - _notification_interval = interval; - return; -} - -/** - * Get notification_interval. - * - * @return The notification_interval. - */ -unsigned int hostescalation::notification_interval() const throw() { - return _notification_interval; -} - -/** - * Check if the notification interval was defined. - * - * @return True if the notification interval was defined. - */ -bool hostescalation::notification_interval_defined() const throw() { - return _notification_interval.is_set(); -} - -/** - * Set contactgroups value. - * - * @param[in] value The new contactgroups value. - * - * @return True on success, otherwise false. - */ -bool hostescalation::_set_contactgroups(std::string const& value) { - _contactgroups = value; - return true; -} - -/** - * Set escalation_options value. - * - * @param[in] value The new escalation_options value. - * - * @return True on success, otherwise false. - */ -bool hostescalation::_set_escalation_options(std::string const& value) { - unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "d" || *it == "down") - options |= down; - else if (*it == "u" || *it == "unreachable") - options |= unreachable; - else if (*it == "r" || *it == "recovery") - options |= recovery; - else if (*it == "n" || *it == "none") - options = none; - else if (*it == "a" || *it == "all") - options = down | unreachable | recovery; - else - return false; - } - _escalation_options = options; - return true; -} - -/** - * Set escalation_period value. - * - * @param[in] value The new escalation_period value. - * - * @return True on success, otherwise false. - */ -bool hostescalation::_set_escalation_period(std::string const& value) { - _escalation_period = value; - return true; -} - -/** - * Set first_notification value. - * - * @param[in] value The new first_notification value. - * - * @return True on success, otherwise false. - */ -bool hostescalation::_set_first_notification(unsigned int value) { - _first_notification = value; - return true; -} - -/** - * Set hostgroups value. - * - * @param[in] value The new hostgroups value. - * - * @return True on success, otherwise false. - */ -bool hostescalation::_set_hostgroups(std::string const& value) { - _hostgroups = value; - return true; -} - -/** - * Set hosts value. - * - * @param[in] value The new hosts value. - * - * @return True on success, otherwise false. - */ -bool hostescalation::_set_hosts(std::string const& value) { - _hosts = value; - return true; -} - -/** - * Set last_notification value. - * - * @param[in] value The new last_notification value. - * - * @return True on success, otherwise false. - */ -bool hostescalation::_set_last_notification(unsigned int value) { - _last_notification = value; - return true; -} - -/** - * Set notification_interval value. - * - * @param[in] value The new notification_interval value. - * - * @return True on success, otherwise false. - */ -bool hostescalation::_set_notification_interval(unsigned int value) { - _notification_interval = value; - return true; -} - -/** - * Get uuid value. - * - * @return uuid. - */ -Uuid const& hostescalation::uuid(void) const { - return _uuid; -} diff --git a/engine/src/configuration/object.cc b/engine/src/configuration/object.cc index 099799083ac..b5e779b25e6 100644 --- a/engine/src/configuration/object.cc +++ b/engine/src/configuration/object.cc @@ -34,10 +34,11 @@ #include "com/centreon/engine/configuration/severity.hh" #include "com/centreon/engine/configuration/tag.hh" #include "com/centreon/engine/configuration/timeperiod.hh" -#include "com/centreon/engine/exceptions/error.hh" +#include "com/centreon/exceptions/msg_fmt.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; +using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ &object::setter::generic @@ -216,7 +217,7 @@ void object::resolve_template(map_object& templates) { for (std::string& s : _templates) { map_object::iterator tmpl = templates.find(s); if (tmpl == templates.end()) - throw engine_error() << "Cannot merge object of type '" << s << "'"; + throw msg_fmt("Cannot merge object of type '{}'", s); tmpl->second->resolve_template(templates); merge(*tmpl->second); } @@ -261,7 +262,9 @@ std::string const& object::type_name() const noexcept { "serviceextinfo", "servicegroup", "timeperiod", - "anomalydetection"}; + "anomalydetection", + "severity", + "tag"}; return tab[_type]; } @@ -298,6 +301,6 @@ bool object::_set_should_register(bool value) { */ bool object::_set_templates(std::string const& value) { _templates.clear(); - string::split(value, _templates, ','); + _templates = absl::StrSplit(value, ','); return true; } diff --git a/engine/src/configuration/service.cc b/engine/src/configuration/service.cc index 62559e48250..e9d8ebabe6c 100644 --- a/engine/src/configuration/service.cc +++ b/engine/src/configuration/service.cc @@ -22,17 +22,16 @@ #include #include #include "com/centreon/engine/customvariable.hh" -#include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" #include "com/centreon/engine/host.hh" -#include "com/centreon/engine/logging/logger.hh" +#include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; extern int config_errors; using namespace com::centreon; using namespace com::centreon::engine::configuration; -using namespace com::centreon::engine::logging; +using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ &object::setter::generic @@ -306,373 +305,270 @@ service& service::operator=(service const& other) { */ bool service::operator==(service const& other) const noexcept { if (!object::operator==(other)) { - engine_logger(dbg_config, more) - << "configuration::service::equality => object don't match"; config_logger->debug( "configuration::service::equality => object don't match"); return false; } if (_acknowledgement_timeout != other._acknowledgement_timeout) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "acknowledgement_timeout don't match"; config_logger->debug( "configuration::service::equality => " "acknowledgement_timeout don't match"); return false; } if (_action_url != other._action_url) { - engine_logger(dbg_config, more) - << "configuration::service::equality => action_url don't match"; config_logger->debug( "configuration::service::equality => action_url don't match"); return false; } if (_checks_active != other._checks_active) { - engine_logger(dbg_config, more) - << "configuration::service::equality => checks_active don't match"; config_logger->debug( "configuration::service::equality => checks_active don't match"); return false; } if (_checks_passive != other._checks_passive) { - engine_logger(dbg_config, more) - << "configuration::service::equality => checks_passive don't match"; config_logger->debug( "configuration::service::equality => checks_passive don't match"); return false; } if (_check_command != other._check_command) { - engine_logger(dbg_config, more) - << "configuration::service::equality => checks_passive don't match"; config_logger->debug( "configuration::service::equality => checks_passive don't match"); return false; } if (_check_command_is_important != other._check_command_is_important) { - engine_logger(dbg_config, more) - << "configuration::service::equality => check_command don't match"; config_logger->debug( "configuration::service::equality => check_command don't match"); return false; } if (_check_freshness != other._check_freshness) { - engine_logger(dbg_config, more) - << "configuration::service::equality => check_freshness don't match"; config_logger->debug( "configuration::service::equality => check_freshness don't match"); return false; } if (_check_interval != other._check_interval) { - engine_logger(dbg_config, more) - << "configuration::service::equality => check_interval don't match"; config_logger->debug( "configuration::service::equality => check_interval don't match"); return false; } if (_check_period != other._check_period) { - engine_logger(dbg_config, more) - << "configuration::service::equality => check_period don't match"; config_logger->debug( "configuration::service::equality => check_period don't match"); return false; } if (_contactgroups != other._contactgroups) { - engine_logger(dbg_config, more) - << "configuration::service::equality => contactgroups don't match"; config_logger->debug( "configuration::service::equality => contactgroups don't match"); return false; } if (_contacts != other._contacts) { - engine_logger(dbg_config, more) - << "configuration::service::equality => contacts don't match"; config_logger->debug( "configuration::service::equality => contacts don't match"); return false; } if (std::operator!=(_customvariables, other._customvariables)) { - engine_logger(dbg_config, more) - << "configuration::service::equality => customvariables don't match"; config_logger->debug( "configuration::service::equality => customvariables don't match"); return false; } if (_display_name != other._display_name) { - engine_logger(dbg_config, more) - << "configuration::service::equality => display_name don't match"; config_logger->debug( "configuration::service::equality => display_name don't match"); return false; } if (_event_handler != other._event_handler) { - engine_logger(dbg_config, more) - << "configuration::service::equality => event_handler don't match"; config_logger->debug( "configuration::service::equality => event_handler don't match"); return false; } if (_event_handler_enabled != other._event_handler_enabled) { - engine_logger(dbg_config, more) - << "configuration::service::equality => event_handler don't match"; config_logger->debug( "configuration::service::equality => event_handler don't match"); return false; } if (_first_notification_delay != other._first_notification_delay) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "first_notification_delay don't match"; config_logger->debug( "configuration::service::equality => " "first_notification_delay don't match"); return false; } if (_flap_detection_enabled != other._flap_detection_enabled) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "flap_detection_enabled don't match"; config_logger->debug( "configuration::service::equality => " "flap_detection_enabled don't match"); return false; } if (_flap_detection_options != other._flap_detection_options) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "flap_detection_options don't match"; config_logger->debug( "configuration::service::equality => " "flap_detection_options don't match"); return false; } if (_freshness_threshold != other._freshness_threshold) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "freshness_threshold don't match"; config_logger->debug( "configuration::service::equality => " "freshness_threshold don't match"); return false; } if (_high_flap_threshold != other._high_flap_threshold) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "high_flap_threshold don't match"; config_logger->debug( "configuration::service::equality => " "high_flap_threshold don't match"); return false; } if (_hostgroups != other._hostgroups) { - engine_logger(dbg_config, more) - << "configuration::service::equality => hostgroups don't match"; config_logger->debug( "configuration::service::equality => hostgroups don't match"); return false; } if (_hosts != other._hosts) { - engine_logger(dbg_config, more) - << "configuration::service::equality => _hosts don't match"; config_logger->debug( "configuration::service::equality => _hosts don't match"); return false; } if (_icon_image != other._icon_image) { - engine_logger(dbg_config, more) - << "configuration::service::equality => icon_image don't match"; config_logger->debug( "configuration::service::equality => icon_image don't match"); return false; } if (_icon_image_alt != other._icon_image_alt) { - engine_logger(dbg_config, more) - << "configuration::service::equality => icon_image_alt don't match"; config_logger->debug( "configuration::service::equality => icon_image_alt don't match"); return false; } if (_initial_state != other._initial_state) { - engine_logger(dbg_config, more) - << "configuration::service::equality => initial_state don't match"; config_logger->debug( "configuration::service::equality => initial_state don't match"); return false; } if (_is_volatile != other._is_volatile) { - engine_logger(dbg_config, more) - << "configuration::service::equality => is_volatile don't match"; config_logger->debug( "configuration::service::equality => is_volatile don't match"); return false; } if (_low_flap_threshold != other._low_flap_threshold) { - engine_logger(dbg_config, more) - << "configuration::service::equality => low_flap_threshold don't match"; config_logger->debug( "configuration::service::equality => low_flap_threshold don't match"); return false; } if (_max_check_attempts != other._max_check_attempts) { - engine_logger(dbg_config, more) - << "configuration::service::equality => max_check_attempts don't match"; config_logger->debug( "configuration::service::equality => max_check_attempts don't match"); return false; } if (_notes != other._notes) { - engine_logger(dbg_config, more) - << "configuration::service::equality => notes don't match"; config_logger->debug( "configuration::service::equality => notes don't match"); return false; } if (_notes_url != other._notes_url) { - engine_logger(dbg_config, more) - << "configuration::service::equality => notes_url don't match"; config_logger->debug( "configuration::service::equality => notes_url don't match"); return false; } if (_notifications_enabled != other._notifications_enabled) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "notifications_enabled don't match"; config_logger->debug( "configuration::service::equality => " "notifications_enabled don't match"); return false; } if (_notification_interval != other._notification_interval) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "notification_interval don't match"; config_logger->debug( "configuration::service::equality => " "notification_interval don't match"); return false; } if (_notification_options != other._notification_options) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "notification_options don't match"; config_logger->debug( "configuration::service::equality => " "notification_options don't match"); return false; } if (_notification_period != other._notification_period) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "notification_period don't match"; config_logger->debug( "configuration::service::equality => " "notification_period don't match"); return false; } if (_obsess_over_service != other._obsess_over_service) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "obsess_over_service don't match"; config_logger->debug( "configuration::service::equality => " "obsess_over_service don't match"); return false; } if (_process_perf_data != other._process_perf_data) { - engine_logger(dbg_config, more) - << "configuration::service::equality => process_perf_data don't match"; config_logger->debug( "configuration::service::equality => process_perf_data don't match"); return false; } if (_retain_nonstatus_information != other._retain_nonstatus_information) { - engine_logger(dbg_config, more) - << "configuration::service::equality => " - "retain_nonstatus_information don't match"; config_logger->debug( "configuration::service::equality => " "retain_nonstatus_information don't match"); return false; } if (_retain_status_information != other._retain_status_information) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "retain_status_information don't match"; config_logger->debug( "configuration::service::equality => " "retain_status_information don't match"); return false; } if (_retry_interval != other._retry_interval) { - engine_logger(dbg_config, more) - << "configuration::service::equality => retry_interval don't match"; config_logger->debug( "configuration::service::equality => retry_interval don't match"); return false; } if (_recovery_notification_delay != other._recovery_notification_delay) { - engine_logger(dbg_config, more) - << "configuration::service::equality => " - "recovery_notification_delay don't match"; config_logger->debug( "configuration::service::equality => " "recovery_notification_delay don't match"); return false; } if (_servicegroups != other._servicegroups) { - engine_logger(dbg_config, more) - << "configuration::service::equality => servicegroups don't match"; config_logger->debug( "configuration::service::equality => servicegroups don't match"); return false; } if (_service_description != other._service_description) { - engine_logger(dbg_config, more) << "configuration::service::equality => " - "service_description don't match"; config_logger->debug( "configuration::service::equality => " "service_description don't match"); return false; } if (_host_id != other._host_id) { - engine_logger(dbg_config, more) - << "configuration::service::equality => host_id don't match"; config_logger->debug( "configuration::service::equality => host_id don't match"); return false; } if (_service_id != other._service_id) { - engine_logger(dbg_config, more) - << "configuration::service::equality => service_id don't match"; config_logger->debug( "configuration::service::equality => service_id don't match"); return false; } if (_stalking_options != other._stalking_options) { - engine_logger(dbg_config, more) - << "configuration::service::equality => stalking_options don't match"; config_logger->debug( "configuration::service::equality => stalking_options don't match"); return false; } if (_timezone != other._timezone) { - engine_logger(dbg_config, more) - << "configuration::service::equality => timezone don't match"; config_logger->debug( "configuration::service::equality => timezone don't match"); return false; } if (_severity_id != other._severity_id) { - engine_logger(dbg_config, more) - << "configuration::service::equality => severity id don't match"; config_logger->debug( "configuration::service::equality => severity id don't match"); return false; } if (_icon_id != other._icon_id) { - engine_logger(dbg_config, more) - << "configuration::service::equality => icon id don't match"; config_logger->debug( "configuration::service::equality => icon id don't match"); return false; } if (_tags != other._tags) { - engine_logger(dbg_config, more) - << "configuration::service::equality => tags don't match"; config_logger->debug( "configuration::service::equality => tags don't match"); return false; } - engine_logger(dbg_config, more) << "configuration::service::equality => OK"; config_logger->debug("configuration::service::equality => OK"); return true; } @@ -804,16 +700,16 @@ bool service::operator<(service const& other) const noexcept { */ void service::check_validity() const { if (_service_description.empty()) - throw engine_error() << "Service has no description (property " - << "'service_description')"; + throw msg_fmt( + "Service has no description (property 'service_description')"); if (_hosts->empty() && _hostgroups->empty()) - throw engine_error() - << "Service '" << _service_description - << "' is not attached to any host or host group (properties " - << "'host_name' or 'hostgroup_name', respectively)"; + throw msg_fmt( + "Service '{}' is not attached to any host or host group (properties " + "'host_name' or 'hostgroup_name', respectively)", + _service_description); if (_check_command.empty()) - throw engine_error() << "Service '" << _service_description - << "' has no check command (property 'check_command')"; + throw msg_fmt("Service '{}' has no check command (property 'check_command'", + _service_description); } /** @@ -833,7 +729,8 @@ service::key_type service::key() const { */ void service::merge(object const& obj) { if (obj.type() != _type) - throw(engine_error() << "Cannot merge service with '" << obj.type() << "'"); + throw msg_fmt("Cannot merge service with object of type '{}'", + static_cast(obj.type())); service const& tmpl(static_cast(obj)); MRG_OPTION(_acknowledgement_timeout); @@ -859,7 +756,6 @@ void service::merge(object const& obj) { MRG_INHERIT(_hosts); MRG_DEFAULT(_icon_image); MRG_DEFAULT(_icon_image_alt); - MRG_OPTION(_initial_state); MRG_OPTION(_is_volatile); MRG_OPTION(_low_flap_threshold); MRG_OPTION(_max_check_attempts); @@ -899,7 +795,7 @@ bool service::parse(char const* key, char const* value) { return (it->second)(*this, value); if (key[0] == '_') { - map_customvar::iterator it{_customvariables.find(key + 1)}; + auto it = _customvariables.find(key + 1); if (it == _customvariables.end()) _customvariables[key + 1] = customvariable(value); else @@ -1041,8 +937,8 @@ bool service::contacts_defined() const noexcept { * * @return The customvariables. */ -com::centreon::engine::map_customvar const& service::customvariables() - const noexcept { +const std::unordered_map& +service::customvariables() const noexcept { return _customvariables; } @@ -1051,7 +947,8 @@ com::centreon::engine::map_customvar const& service::customvariables() * * @return The customvariables. */ -com::centreon::engine::map_customvar& service::mut_customvariables() noexcept { +std::unordered_map& +service::customvariables() noexcept { return _customvariables; } @@ -1260,7 +1157,6 @@ bool service::notifications_enabled() const noexcept { */ void service::notification_interval(unsigned int interval) noexcept { _notification_interval = interval; - return; } /** @@ -1297,7 +1193,6 @@ unsigned short service::notification_options() const noexcept { */ void service::notification_period(std::string const& period) { _notification_period = period; - return; } /** @@ -1433,7 +1328,6 @@ unsigned short service::stalking_options() const noexcept { */ void service::timezone(std::string const& time_zone) { _timezone = time_zone; - return; } /** @@ -1645,9 +1539,6 @@ bool service::_set_event_handler_enabled(bool value) { */ bool service::_set_failure_prediction_enabled(bool value) { (void)value; - engine_logger(log_verification_error, basic) - << "Warning: service failure_prediction_enabled is deprecated." - << " This option will not be supported in 20.04."; config_logger->warn( "Warning: service failure_prediction_enabled is deprecated. This option " "will not be supported in 20.04."); @@ -1664,9 +1555,6 @@ bool service::_set_failure_prediction_enabled(bool value) { */ bool service::_set_failure_prediction_options(std::string const& value) { (void)value; - engine_logger(log_verification_error, basic) - << "Warning: service failure_prediction_options is deprecated." - << " This option will not be supported in 20.04."; config_logger->warn( "Warning: service failure_prediction_options is deprecated. This option " "will not be supported in 20.04."); @@ -1707,22 +1595,20 @@ bool service::_set_flap_detection_enabled(bool value) { */ bool service::_set_flap_detection_options(std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "o" || *it == "ok") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "o" || v == "ok") options |= ok; - else if (*it == "w" || *it == "warning") + else if (v == "w" || v == "warning") options |= warning; - else if (*it == "u" || *it == "unknown") + else if (v == "u" || v == "unknown") options |= unknown; - else if (*it == "c" || *it == "critical") + else if (v == "c" || v == "critical") options |= critical; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = ok | warning | unknown | critical; else return false; @@ -1811,8 +1697,8 @@ bool service::_set_icon_image_alt(std::string const& value) { * @return True on success, otherwise false. */ bool service::_set_initial_state(std::string const& value) { - std::string data(value); - string::trim(data); + std::string_view data(value); + data = absl::StripAsciiWhitespace(data); if (data == "o" || data == "ok") _initial_state = engine::service::state_ok; else if (data == "w" || data == "warning") @@ -1909,26 +1795,24 @@ bool service::_set_notifications_enabled(bool value) { */ bool service::_set_notification_options(std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "u" || *it == "unknown") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "u" || v == "unknown") options |= unknown; - else if (*it == "w" || *it == "warning") + else if (v == "w" || v == "warning") options |= warning; - else if (*it == "c" || *it == "critical") + else if (v == "c" || v == "critical") options |= critical; - else if (*it == "r" || *it == "recovery") + else if (v == "r" || v == "recovery") options |= ok; - else if (*it == "f" || *it == "flapping") + else if (v == "f" || v == "flapping") options |= flapping; - else if (*it == "s" || *it == "downtime") + else if (v == "s" || v == "downtime") options |= downtime; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = unknown | warning | critical | ok | flapping | downtime; else return false; @@ -1982,9 +1866,6 @@ bool service::_set_obsess_over_service(bool value) { */ bool service::_set_parallelize_check(bool value) { (void)value; - engine_logger(log_verification_error, basic) - << "Warning: service parallelize_check is deprecated" - << " This option will not be supported in 20.04."; config_logger->warn( "Warning: service parallelize_check is deprecated This option will not " "be supported in 20.04."); @@ -2099,22 +1980,20 @@ bool service::set_service_id(uint64_t value) { */ bool service::_set_stalking_options(std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "o" || *it == "ok") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "o" || v == "ok") options |= ok; - else if (*it == "w" || *it == "warning") + else if (v == "w" || v == "warning") options |= warning; - else if (*it == "u" || *it == "unknown") + else if (v == "u" || v == "unknown") options |= unknown; - else if (*it == "c" || *it == "critical") + else if (v == "c" || v == "critical") options |= critical; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = ok | warning | unknown | critical; else return false; diff --git a/engine/src/configuration/servicedependency.cc b/engine/src/configuration/servicedependency.cc index 9def6367153..ba5a722fae3 100644 --- a/engine/src/configuration/servicedependency.cc +++ b/engine/src/configuration/servicedependency.cc @@ -19,16 +19,15 @@ */ #include "com/centreon/engine/configuration/servicedependency.hh" -#include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/logging/logger.hh" +#include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; extern int config_errors; using namespace com::centreon; using namespace com::centreon::engine::configuration; -using namespace com::centreon::engine::logging; +using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ &object::setter::generic @@ -152,20 +151,20 @@ servicedependency& servicedependency::operator=( */ bool servicedependency::operator==(servicedependency const& right) const throw() { - return ( - object::operator==(right) && - _dependency_period == right._dependency_period && - _dependency_type == right._dependency_type && - _dependent_hostgroups == right._dependent_hostgroups && - _dependent_hosts == right._dependent_hosts && - _dependent_servicegroups == right._dependent_servicegroups && - _dependent_service_description == right._dependent_service_description && - _execution_failure_options == right._execution_failure_options && - _inherits_parent == right._inherits_parent && - _hostgroups == right._hostgroups && _hosts == right._hosts && - _notification_failure_options == right._notification_failure_options && - _servicegroups == right._servicegroups && - _service_description == right._service_description); + return object::operator==(right) && + _dependency_period == right._dependency_period && + _dependency_type == right._dependency_type && + _dependent_hostgroups == right._dependent_hostgroups && + _dependent_hosts == right._dependent_hosts && + _dependent_servicegroups == right._dependent_servicegroups && + _dependent_service_description == + right._dependent_service_description && + _execution_failure_options == right._execution_failure_options && + _inherits_parent == right._inherits_parent && + _hostgroups == right._hostgroups && _hosts == right._hosts && + _notification_failure_options == right._notification_failure_options && + _servicegroups == right._servicegroups && + _service_description == right._service_description; } /** @@ -214,7 +213,7 @@ bool servicedependency::operator<(servicedependency const& right) const { return (_execution_failure_options < right._execution_failure_options); else if (_inherits_parent != right._inherits_parent) return _inherits_parent < right._inherits_parent; - return (_notification_failure_options < right._notification_failure_options); + return _notification_failure_options < right._notification_failure_options; } /** @@ -226,31 +225,28 @@ void servicedependency::check_validity() const { // Check base service(s). if (_servicegroups->empty()) { if (_service_description->empty()) - throw(engine_error() << "Service dependency is not attached to " - << "any service or service group (properties " - << "'service_description' or 'servicegroup_name', " - << "respectively)"); + throw msg_fmt( + "Service dependency is not attached to any service or service group " + "(properties 'service_description' or 'servicegroup_name', " + "respectively)"); else if (_hosts->empty() && _hostgroups->empty()) - throw( - engine_error() << "Service dependency is not attached to " - << "any host or host group (properties 'host_name' or " - << "'hostgroup_name', respectively)"); + throw msg_fmt( + "Service dependency is not attached to any host or host group " + "(properties 'host_name' or 'hostgroup_name', respectively)"); } // Check dependent service(s). if (_dependent_servicegroups->empty()) { if (_dependent_service_description->empty()) - throw( - engine_error() << "Service dependency is not attached to " - << "any dependent service or dependent service group " - << "(properties 'dependent_service_description' or " - << "'dependent_servicegroup_name', respectively)"); + throw msg_fmt( + "Service dependency is not attached to any dependent service or " + "dependent service group (properties 'dependent_service_description' " + "or 'dependent_servicegroup_name', respectively)"); else if (_dependent_hosts->empty() && _dependent_hostgroups->empty()) - throw(engine_error() - << "Service dependency is not attached to " - << "any dependent host or dependent host group (properties " - << "'dependent_host_name' or 'dependent_hostgroup_name', " - << "respectively)"); + throw msg_fmt( + "Service dependency is not attached to any dependent host or " + "dependent host group (properties 'dependent_host_name' or " + "'dependent_hostgroup_name', respectively)"); } // With no execution or failure options this dependency is useless. @@ -277,7 +273,6 @@ void servicedependency::check_validity() const { else msg << "host '" << _hosts->front() << "'"; } - engine_logger(log_config_warning, basic) << msg.str(); config_logger->warn(msg.str()); } } @@ -298,8 +293,8 @@ servicedependency::key_type const& servicedependency::key() const throw() { */ void servicedependency::merge(object const& obj) { if (obj.type() != _type) - throw(engine_error() << "Cannot merge service dependency with '" - << obj.type() << "'"); + throw msg_fmt("Cannot merge service dependency with '{}'", + static_cast(obj.type())); servicedependency const& tmpl(static_cast(obj)); MRG_DEFAULT(_dependency_period); @@ -340,7 +335,6 @@ bool servicedependency::parse(char const* key, char const* value) { */ void servicedependency::dependency_period(std::string const& period) { _dependency_period = period; - return; } /** @@ -360,7 +354,6 @@ std::string const& servicedependency::dependency_period() const throw() { void servicedependency::dependency_type( servicedependency::dependency_kind type) throw() { _dependency_type = type; - return; } /** @@ -454,7 +447,6 @@ list_string const& servicedependency::dependent_service_description() const void servicedependency::execution_failure_options( unsigned int options) throw() { _execution_failure_options = options; - return; } /** @@ -473,7 +465,6 @@ unsigned int servicedependency::execution_failure_options() const throw() { */ void servicedependency::inherits_parent(bool inherit) throw() { _inherits_parent = inherit; - return; } /** @@ -529,7 +520,6 @@ list_string const& servicedependency::hosts() const throw() { void servicedependency::notification_failure_options( unsigned int options) throw() { _notification_failure_options = options; - return; } /** @@ -648,24 +638,22 @@ bool servicedependency::_set_dependent_service_description( bool servicedependency::_set_execution_failure_options( std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "o" || *it == "ok") + auto values = absl::StrSplit(value, ','); + for (auto& v : values) { + std::string_view sv = absl::StripAsciiWhitespace(v); + if (sv == "o" || sv == "ok") options |= ok; - else if (*it == "u" || *it == "unknown") + else if (sv == "u" || sv == "unknown") options |= unknown; - else if (*it == "w" || *it == "warning") + else if (sv == "w" || sv == "warning") options |= warning; - else if (*it == "c" || *it == "critical") + else if (sv == "c" || sv == "critical") options |= critical; - else if (*it == "p" || *it == "pending") + else if (sv == "p" || sv == "pending") options |= pending; - else if (*it == "n" || *it == "none") + else if (sv == "n" || sv == "none") options = none; - else if (*it == "a" || *it == "all") + else if (sv == "a" || sv == "all") options = ok | unknown | warning | critical | pending; else return false; @@ -720,24 +708,22 @@ bool servicedependency::_set_hosts(std::string const& value) { bool servicedependency::_set_notification_failure_options( std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "o" || *it == "ok") + auto values = absl::StrSplit(value, ','); + for (auto& v : values) { + std::string_view sv = absl::StripAsciiWhitespace(v); + if (sv == "o" || sv == "ok") options |= ok; - else if (*it == "u" || *it == "unknown") + else if (sv == "u" || sv == "unknown") options |= unknown; - else if (*it == "w" || *it == "warning") + else if (sv == "w" || sv == "warning") options |= warning; - else if (*it == "c" || *it == "critical") + else if (sv == "c" || sv == "critical") options |= critical; - else if (*it == "p" || *it == "pending") + else if (sv == "p" || sv == "pending") options |= pending; - else if (*it == "n" || *it == "none") + else if (sv == "n" || sv == "none") options = none; - else if (*it == "a" || *it == "all") + else if (sv == "a" || sv == "all") options = ok | unknown | warning | critical | pending; else return false; diff --git a/engine/src/configuration/serviceescalation.cc b/engine/src/configuration/serviceescalation.cc index e1806cbed81..c28cf5517cd 100644 --- a/engine/src/configuration/serviceescalation.cc +++ b/engine/src/configuration/serviceescalation.cc @@ -18,13 +18,12 @@ */ #include "com/centreon/engine/configuration/serviceescalation.hh" -#include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/logging/logger.hh" +#include "com/centreon/exceptions/msg_fmt.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; -using namespace com::centreon::engine::logging; +using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ &object::setter::generic @@ -78,11 +77,6 @@ serviceescalation::serviceescalation(serviceescalation const& right) operator=(right); } -/** - * Destructor. - */ -serviceescalation::~serviceescalation() throw() {} - /** * Copy constructor. * @@ -116,106 +110,76 @@ serviceescalation& serviceescalation::operator=( * * @return True if is the same serviceescalation, otherwise false. */ -bool serviceescalation::operator==(serviceescalation const& right) const - throw() { +bool serviceescalation::operator==( + serviceescalation const& right) const noexcept { /* No comparison is made on the UUID because it is used between the * configuration object and the object. Since this object is randomly * constructor in almost all cases, we can have two equal escalations * with different uuid.*/ if (!object::operator==(right)) { - engine_logger(dbg_config, more) - << "configuration::serviceescalation::equality => object don't match"; config_logger->debug( "configuration::serviceescalation::equality => object don't match"); return false; } if (_contactgroups != right._contactgroups) { - engine_logger(dbg_config, more) << "configuration::serviceescalation::" - "equality => contact groups don't match"; config_logger->debug( "configuration::serviceescalation::" "equality => contact groups don't match"); return false; } if (_escalation_options != right._escalation_options) { - engine_logger(dbg_config, more) - << "configuration::serviceescalation::equality => escalation options " - "don't match"; config_logger->debug( "configuration::serviceescalation::equality => escalation options " "don't match"); return false; } if (_escalation_period != right._escalation_period) { - engine_logger(dbg_config, more) - << "configuration::serviceescalation::equality => escalation periods " - "don't match"; config_logger->debug( "configuration::serviceescalation::equality => escalation periods " "don't match"); return false; } if (_first_notification != right._first_notification) { - engine_logger(dbg_config, more) - << "configuration::serviceescalation::equality => first notifications " - "don't match"; config_logger->debug( "configuration::serviceescalation::equality => first notifications " "don't match"); return false; } if (_hostgroups != right._hostgroups) { - engine_logger(dbg_config, more) << "configuration::serviceescalation::" - "equality => host groups don't match"; config_logger->debug( - "configuration::serviceescalation::" - "equality => host groups don't match"); + "configuration::serviceescalation::equality => host groups don't " + "match"); return false; } if (_hosts != right._hosts) { - engine_logger(dbg_config, more) - << "configuration::serviceescalation::equality => hosts don't match"; config_logger->debug( "configuration::serviceescalation::equality => hosts don't match"); return false; } if (_last_notification != right._last_notification) { - engine_logger(dbg_config, more) - << "configuration::serviceescalation::equality => last notification " - "don't match"; config_logger->debug( "configuration::serviceescalation::equality => last notification " "don't match"); return false; } if (_notification_interval != right._notification_interval) { - engine_logger(dbg_config, more) - << "configuration::serviceescalation::equality => notification " - "interval don't match"; config_logger->debug( "configuration::serviceescalation::equality => notification " "interval don't match"); return false; } if (_servicegroups != right._servicegroups) { - engine_logger(dbg_config, more) << "configuration::serviceescalation::" - "equality => service groups don't match"; config_logger->debug( "configuration::serviceescalation::" "equality => service groups don't match"); return false; } if (_service_description != right._service_description) { - engine_logger(dbg_config, more) - << "configuration::serviceescalation::equality => service descriptions " - "don't match"; config_logger->debug( "configuration::serviceescalation::equality => service descriptions " "don't match"); return false; } - engine_logger(dbg_config, more) - << "configuration::serviceescalation::equality => OK"; config_logger->debug("configuration::serviceescalation::equality => OK"); return true; } @@ -227,8 +191,8 @@ bool serviceescalation::operator==(serviceescalation const& right) const * * @return True if is not the same serviceescalation, otherwise false. */ -bool serviceescalation::operator!=(serviceescalation const& right) const - throw() { +bool serviceescalation::operator!=( + serviceescalation const& right) const noexcept { return !operator==(right); } @@ -269,17 +233,16 @@ bool serviceescalation::operator<(serviceescalation const& right) const { void serviceescalation::check_validity() const { if (_servicegroups->empty()) { if (_service_description->empty()) - throw(engine_error() << "Service escalation is not attached to " - << "any service or service group (properties " - << "'service_description' and 'servicegroup_name', " - << "respectively)"); + throw msg_fmt( + "Service escalation is not attached to " + "any service or service group (properties " + "'service_description' and 'servicegroup_name', " + "respectively)"); else if (_hosts->empty() && _hostgroups->empty()) - throw( - engine_error() << "Service escalation is not attached to " - << "any host or host group (properties 'host_name' or " - << "'hostgroup_name', respectively)"); + throw msg_fmt( + "Service escalation is not attached to any host or host group " + "(properties 'host_name' or 'hostgroup_name', respectively)"); } - return; } /** @@ -287,7 +250,7 @@ void serviceescalation::check_validity() const { * * @return This object. */ -serviceescalation::key_type const& serviceescalation::key() const throw() { +serviceescalation::key_type const& serviceescalation::key() const noexcept { return *this; } @@ -298,8 +261,8 @@ serviceescalation::key_type const& serviceescalation::key() const throw() { */ void serviceescalation::merge(object const& obj) { if (obj.type() != _type) - throw(engine_error() << "Cannot merge service escalation with '" - << obj.type() << "'"); + throw msg_fmt("Cannot merge service escalation with '{}'", + static_cast(obj.type())); serviceescalation const& tmpl(static_cast(obj)); MRG_INHERIT(_contactgroups); @@ -336,7 +299,7 @@ bool serviceescalation::parse(char const* key, char const* value) { * * @return The contact groups. */ -set_string& serviceescalation::contactgroups() throw() { +set_string& serviceescalation::contactgroups() noexcept { return *_contactgroups; } @@ -345,7 +308,7 @@ set_string& serviceescalation::contactgroups() throw() { * * @return The contactgroups. */ -set_string const& serviceescalation::contactgroups() const throw() { +set_string const& serviceescalation::contactgroups() const noexcept { return *_contactgroups; } @@ -354,7 +317,7 @@ set_string const& serviceescalation::contactgroups() const throw() { * * @return True if contact groups were defined. */ -bool serviceescalation::contactgroups_defined() const throw() { +bool serviceescalation::contactgroups_defined() const noexcept { return _contactgroups.is_set(); } @@ -363,9 +326,8 @@ bool serviceescalation::contactgroups_defined() const throw() { * * @param[in] options New escalation options. */ -void serviceescalation::escalation_options(unsigned int options) throw() { +void serviceescalation::escalation_options(unsigned int options) noexcept { _escalation_options = options; - return; } /** @@ -373,7 +335,7 @@ void serviceescalation::escalation_options(unsigned int options) throw() { * * @return The escalation_options. */ -unsigned short serviceescalation::escalation_options() const throw() { +unsigned short serviceescalation::escalation_options() const noexcept { return _escalation_options; } @@ -384,7 +346,6 @@ unsigned short serviceescalation::escalation_options() const throw() { */ void serviceescalation::escalation_period(std::string const& period) { _escalation_period = period; - return; } /** @@ -392,7 +353,7 @@ void serviceescalation::escalation_period(std::string const& period) { * * @return The escalation_period. */ -std::string const& serviceescalation::escalation_period() const throw() { +std::string const& serviceescalation::escalation_period() const noexcept { return _escalation_period; } @@ -401,7 +362,7 @@ std::string const& serviceescalation::escalation_period() const throw() { * * @return True if the escalation period was defined. */ -bool serviceescalation::escalation_period_defined() const throw() { +bool serviceescalation::escalation_period_defined() const noexcept { return _escalation_period.is_set(); } @@ -410,9 +371,8 @@ bool serviceescalation::escalation_period_defined() const throw() { * * @param[in] n First notification number. */ -void serviceescalation::first_notification(unsigned int n) throw() { +void serviceescalation::first_notification(unsigned int n) noexcept { _first_notification = n; - return; } /** @@ -420,7 +380,7 @@ void serviceescalation::first_notification(unsigned int n) throw() { * * @return The first_notification. */ -unsigned int serviceescalation::first_notification() const throw() { +unsigned int serviceescalation::first_notification() const noexcept { return _first_notification; } @@ -429,7 +389,7 @@ unsigned int serviceescalation::first_notification() const throw() { * * @return Host groups. */ -list_string& serviceescalation::hostgroups() throw() { +list_string& serviceescalation::hostgroups() noexcept { return *_hostgroups; } @@ -438,7 +398,7 @@ list_string& serviceescalation::hostgroups() throw() { * * @return The hostgroups. */ -list_string const& serviceescalation::hostgroups() const throw() { +list_string const& serviceescalation::hostgroups() const noexcept { return *_hostgroups; } @@ -447,7 +407,7 @@ list_string const& serviceescalation::hostgroups() const throw() { * * @return The hosts. */ -list_string& serviceescalation::hosts() throw() { +list_string& serviceescalation::hosts() noexcept { return *_hosts; } @@ -456,7 +416,7 @@ list_string& serviceescalation::hosts() throw() { * * @return The hosts. */ -list_string const& serviceescalation::hosts() const throw() { +list_string const& serviceescalation::hosts() const noexcept { return *_hosts; } @@ -465,9 +425,8 @@ list_string const& serviceescalation::hosts() const throw() { * * @param[in] n Last notification number. */ -void serviceescalation::last_notification(unsigned int n) throw() { +void serviceescalation::last_notification(unsigned int n) noexcept { _last_notification = n; - return; } /** @@ -475,7 +434,7 @@ void serviceescalation::last_notification(unsigned int n) throw() { * * @return The last_notification. */ -unsigned int serviceescalation::last_notification() const throw() { +unsigned int serviceescalation::last_notification() const noexcept { return _last_notification; } @@ -484,9 +443,8 @@ unsigned int serviceescalation::last_notification() const throw() { * * @param[in] interval New notification interval. */ -void serviceescalation::notification_interval(unsigned int interval) throw() { +void serviceescalation::notification_interval(unsigned int interval) noexcept { _notification_interval = interval; - return; } /** @@ -494,7 +452,7 @@ void serviceescalation::notification_interval(unsigned int interval) throw() { * * @return The notification_interval. */ -unsigned int serviceescalation::notification_interval() const throw() { +unsigned int serviceescalation::notification_interval() const noexcept { return _notification_interval; } @@ -503,7 +461,7 @@ unsigned int serviceescalation::notification_interval() const throw() { * * @return True if the notification interval was set. */ -bool serviceescalation::notification_interval_defined() const throw() { +bool serviceescalation::notification_interval_defined() const noexcept { return _notification_interval.is_set(); } @@ -512,7 +470,7 @@ bool serviceescalation::notification_interval_defined() const throw() { * * @return The service groups. */ -list_string& serviceescalation::servicegroups() throw() { +list_string& serviceescalation::servicegroups() noexcept { return *_servicegroups; } @@ -521,7 +479,7 @@ list_string& serviceescalation::servicegroups() throw() { * * @return The servicegroups. */ -list_string const& serviceescalation::servicegroups() const throw() { +list_string const& serviceescalation::servicegroups() const noexcept { return *_servicegroups; } @@ -530,7 +488,7 @@ list_string const& serviceescalation::servicegroups() const throw() { * * @return Service description. */ -list_string& serviceescalation::service_description() throw() { +list_string& serviceescalation::service_description() noexcept { return *_service_description; } @@ -539,7 +497,7 @@ list_string& serviceescalation::service_description() throw() { * * @return The service_description. */ -list_string const& serviceescalation::service_description() const throw() { +list_string const& serviceescalation::service_description() const noexcept { return *_service_description; } @@ -564,22 +522,20 @@ bool serviceescalation::_set_contactgroups(std::string const& value) { */ bool serviceescalation::_set_escalation_options(std::string const& value) { unsigned short options(none); - std::list values; - string::split(value, values, ','); - for (std::list::iterator it(values.begin()), end(values.end()); - it != end; ++it) { - string::trim(*it); - if (*it == "w" || *it == "warning") + auto values = absl::StrSplit(value, ','); + for (auto& val : values) { + auto v = absl::StripAsciiWhitespace(val); + if (v == "w" || v == "warning") options |= warning; - else if (*it == "u" || *it == "unknown") + else if (v == "u" || v == "unknown") options |= unknown; - else if (*it == "c" || *it == "critical") + else if (v == "c" || v == "critical") options |= critical; - else if (*it == "r" || *it == "recovery") + else if (v == "r" || v == "recovery") options |= recovery; - else if (*it == "n" || *it == "none") + else if (v == "n" || v == "none") options = none; - else if (*it == "a" || *it == "all") + else if (v == "a" || v == "all") options = warning | unknown | critical | recovery; else return false; diff --git a/engine/src/configuration/timeperiod.cc b/engine/src/configuration/timeperiod.cc index a2eeff46c80..1e16e61d6c9 100644 --- a/engine/src/configuration/timeperiod.cc +++ b/engine/src/configuration/timeperiod.cc @@ -20,12 +20,13 @@ #include "com/centreon/engine/configuration/timeperiod.hh" #include "com/centreon/engine/common.hh" -#include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/timerange.hh" +#include "com/centreon/exceptions/msg_fmt.hh" using namespace com::centreon; using namespace com::centreon::engine; using namespace com::centreon::engine::configuration; +using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ &object::setter::generic @@ -56,7 +57,7 @@ timeperiod::timeperiod(timeperiod const& right) : object(right) { /** * Destructor. */ -timeperiod::~timeperiod() throw() {} +timeperiod::~timeperiod() noexcept {} /** * Copy constructor. @@ -85,10 +86,10 @@ timeperiod& timeperiod::operator=(timeperiod const& right) { * @return True if is the same timeperiod, otherwise false. */ bool timeperiod::operator==(timeperiod const& right) const { - return (object::operator==(right) && _alias == right._alias && - _exceptions == right._exceptions && _exclude == right._exclude && - _timeperiod_name == right._timeperiod_name && - _timeranges == right._timeranges); + return object::operator==(right) && _alias == right._alias && + _exceptions == right._exceptions && _exclude == right._exclude && + _timeperiod_name == right._timeperiod_name && + _timeranges == right._timeranges; } /** @@ -128,9 +129,8 @@ bool timeperiod::operator<(timeperiod const& right) const { */ void timeperiod::check_validity() const { if (_timeperiod_name.empty()) - throw(engine_error() - << "Time period has no name (property 'timeperiod_name')"); - return; + throw exceptions::msg_fmt( + "Time period has no name (property 'timeperiod_name')"); } /** @@ -138,7 +138,7 @@ void timeperiod::check_validity() const { * * @return The time period name. */ -timeperiod::key_type const& timeperiod::key() const throw() { +timeperiod::key_type const& timeperiod::key() const noexcept { return _timeperiod_name; } @@ -149,8 +149,9 @@ timeperiod::key_type const& timeperiod::key() const throw() { */ void timeperiod::merge(object const& obj) { if (obj.type() != _type) - throw(engine_error() << "Cannot merge time period with '" << obj.type() - << "'"); + throw exceptions::msg_fmt( + "Cannot merge time period with object of type '{}'", + static_cast(obj.type())); timeperiod const& tmpl(static_cast(obj)); MRG_DEFAULT(_alias); @@ -198,12 +199,11 @@ bool timeperiod::parse(std::string const& line) { if (pos == std::string::npos) return false; std::string key(line.substr(0, pos)); - std::string value(line.substr(pos + 1)); - string::trim(value); + std::string value(absl::StripAsciiWhitespace(line.substr(pos + 1))); if (object::parse(key.c_str(), value.c_str()) || - parse(key.c_str(), string::trim(value).c_str()) || - _add_calendar_date(line) || _add_other_date(line)) + parse(key.c_str(), value.c_str()) || _add_calendar_date(line) || + _add_other_date(line)) return true; return false; } @@ -213,7 +213,7 @@ bool timeperiod::parse(std::string const& line) { * * @return The alias value. */ -std::string const& timeperiod::alias() const throw() { +std::string const& timeperiod::alias() const noexcept { return _alias; } @@ -222,7 +222,7 @@ std::string const& timeperiod::alias() const throw() { * * @return The exceptions value. */ -exception_array const& timeperiod::exceptions() const throw() { +exception_array const& timeperiod::exceptions() const noexcept { return _exceptions; } @@ -231,7 +231,7 @@ exception_array const& timeperiod::exceptions() const throw() { * * @return The exclude value. */ -set_string const& timeperiod::exclude() const throw() { +set_string const& timeperiod::exclude() const noexcept { return *_exclude; } @@ -240,7 +240,7 @@ set_string const& timeperiod::exclude() const throw() { * * @return The timeperiod_name value. */ -std::string const& timeperiod::timeperiod_name() const throw() { +std::string const& timeperiod::timeperiod_name() const noexcept { return _timeperiod_name; } @@ -263,19 +263,23 @@ days_array const& timeperiod::timeranges() const { */ bool timeperiod::_build_timeranges(std::string const& line, timerange_list& timeranges) { - list_string timeranges_str; - string::split(line, timeranges_str, ','); - for (list_string::const_iterator it(timeranges_str.begin()), - end(timeranges_str.end()); - it != end; ++it) { - std::size_t pos(it->find('-')); + size_t pos = line.find_first_of(';'); + std::string_view lst; + if (pos != std::string::npos) + lst = std::string_view(line.data(), pos); + else + lst = line; + auto timeranges_str = absl::StrSplit(lst, ','); + for (auto tr : timeranges_str) { + tr = absl::StripAsciiWhitespace(tr); + std::size_t pos(tr.find('-')); if (pos == std::string::npos) return false; unsigned long start_time; - if (!_build_time_t(it->substr(0, pos), start_time)) + if (!_build_time_t(tr.substr(0, pos), start_time)) return false; unsigned long end_time; - if (!_build_time_t(it->substr(pos + 1), end_time)) + if (!_build_time_t(tr.substr(pos + 1), end_time)) return false; timeranges.emplace_back(start_time, end_time); } @@ -290,16 +294,15 @@ bool timeperiod::_build_timeranges(std::string const& line, * * @return True on success, otherwise false. */ -bool timeperiod::_build_time_t(std::string const& time_str, - unsigned long& ret) { +bool timeperiod::_build_time_t(std::string_view time_str, unsigned long& ret) { std::size_t pos(time_str.find(':')); if (pos == std::string::npos) return false; unsigned long hours; - if (!string::to(time_str.substr(0, pos).c_str(), hours)) + if (!absl::SimpleAtoi(time_str.substr(0, pos), &hours)) return false; unsigned long minutes; - if (!string::to(time_str.substr(pos + 1).c_str(), minutes)) + if (!absl::SimpleAtoi(time_str.substr(pos + 1), &minutes)) return false; ret = hours * 3600 + minutes * 60; return true; @@ -315,7 +318,7 @@ bool timeperiod::_build_time_t(std::string const& time_str, * @return True on success, otherwise false. */ bool timeperiod::_has_similar_daterange(std::list const& lst, - daterange const& range) throw() { + daterange const& range) noexcept { for (std::list::const_iterator it(lst.begin()), end(lst.end()); it != end; ++it) if (it->is_date_data_equal(range)) @@ -593,7 +596,7 @@ bool timeperiod::_add_week_day(std::string const& key, * @return True on success, otherwise false. */ bool timeperiod::_get_month_id(std::string const& name, unsigned int& id) { - static std::string const months[] = { + static std::string_view const months[] = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"}; for (id = 0; id < sizeof(months) / sizeof(months[0]); ++id) From a5f095c1aa2765eed9a0efae80444e3b84361dd2 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Wed, 22 May 2024 16:59:26 +0200 Subject: [PATCH 04/21] cleanup(engine/configuration): headers updated --- engine/enginerpc/engine_impl.cc | 1 + .../centreon/engine/configuration/state.hh | 41 ++-- engine/src/checks/checker.cc | 7 +- .../src/configuration/applier/contactgroup.cc | 1 + engine/src/configuration/applier/host.cc | 1 + .../configuration/applier/hostdependency.cc | 1 + .../configuration/applier/hostescalation.cc | 1 + engine/src/configuration/applier/service.cc | 1 + .../applier/serviceescalation.cc | 1 + .../src/configuration/applier/timeperiod.cc | 1 + engine/src/configuration/hostdependency.cc | 1 + engine/src/configuration/parser.cc | 6 - engine/src/configuration/servicegroup.cc | 2 +- engine/src/configuration/state.cc | 225 ++++++++---------- engine/src/configuration/timeperiod.cc | 1 - engine/src/retention/dump.cc | 1 + 16 files changed, 126 insertions(+), 166 deletions(-) diff --git a/engine/enginerpc/engine_impl.cc b/engine/enginerpc/engine_impl.cc index 0476f9dc9d0..8c5e5b89b20 100644 --- a/engine/enginerpc/engine_impl.cc +++ b/engine/enginerpc/engine_impl.cc @@ -55,6 +55,7 @@ namespace asio = boost::asio; #include "com/centreon/engine/globals.hh" #include "com/centreon/engine/hostdependency.hh" #include "com/centreon/engine/hostgroup.hh" +#include "com/centreon/engine/logging/logger.hh" #include "com/centreon/engine/service.hh" #include "com/centreon/engine/servicedependency.hh" #include "com/centreon/engine/servicegroup.hh" diff --git a/engine/inc/com/centreon/engine/configuration/state.hh b/engine/inc/com/centreon/engine/configuration/state.hh index 76f167f8bb0..0a7f9647822 100644 --- a/engine/inc/com/centreon/engine/configuration/state.hh +++ b/engine/inc/com/centreon/engine/configuration/state.hh @@ -1,21 +1,21 @@ -/* -** Copyright 2011-2017 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-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_STATE_HH #define CCE_CONFIGURATION_STATE_HH @@ -36,7 +36,6 @@ #include "com/centreon/engine/configuration/severity.hh" #include "com/centreon/engine/configuration/tag.hh" #include "com/centreon/engine/configuration/timeperiod.hh" -#include "com/centreon/engine/logging/logger.hh" namespace com::centreon::engine::configuration { @@ -345,8 +344,8 @@ class state { servicegroup::key_type const& k); // const set_anomalydetection& anomalydetections() const noexcept; set_anomalydetection& anomalydetections() noexcept; - set_service const& services() const noexcept; set_service& mut_services() noexcept; + set_service const& services() const noexcept; set_anomalydetection::iterator anomalydetections_find( anomalydetection::key_type const& k); set_service::iterator services_find(service::key_type const& k); @@ -478,7 +477,6 @@ class state { void _set_event_broker_options(std::string const& value); void _set_free_child_process_memory(std::string const& value); void _set_host_inter_check_delay_method(std::string const& value); - void _set_host_perfdata_file_mode(std::string const& value); void _set_lock_file(std::string const& value); void _set_log_archive_path(std::string const& value); void _set_log_initial_states(std::string const& value); @@ -497,6 +495,7 @@ class state { void _set_temp_file(std::string const& value); void _set_temp_path(std::string const& value); void _set_use_embedded_perl_implicitly(std::string const& value); + void _set_host_perfdata_file_mode(std::string const& value); bool _accept_passive_host_checks; bool _accept_passive_service_checks; diff --git a/engine/src/checks/checker.cc b/engine/src/checks/checker.cc index 56c412ef5cf..aeca2d268e6 100644 --- a/engine/src/checks/checker.cc +++ b/engine/src/checks/checker.cc @@ -25,6 +25,7 @@ #include "com/centreon/engine/configuration/whitelist.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/logging/logger.hh" #include "com/centreon/engine/macros.hh" #include "com/centreon/engine/neberrors.hh" #include "com/centreon/engine/objects.hh" @@ -39,12 +40,6 @@ using namespace com::centreon::engine::checks; checker* checker::_instance = nullptr; static constexpr time_t max_check_reaper_time = 30; -/************************************** - * * - * Public Methods * - * * - **************************************/ - /** * Get instance of the checker singleton. * diff --git a/engine/src/configuration/applier/contactgroup.cc b/engine/src/configuration/applier/contactgroup.cc index 4f569a6a99d..05d144d65f3 100644 --- a/engine/src/configuration/applier/contactgroup.cc +++ b/engine/src/configuration/applier/contactgroup.cc @@ -24,6 +24,7 @@ #include "com/centreon/engine/deleter/listmember.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/logging/logger.hh" using namespace com::centreon::engine::configuration; using namespace com::centreon::engine::logging; diff --git a/engine/src/configuration/applier/host.cc b/engine/src/configuration/applier/host.cc index c21486618e1..149cb78661e 100644 --- a/engine/src/configuration/applier/host.cc +++ b/engine/src/configuration/applier/host.cc @@ -27,6 +27,7 @@ #include "com/centreon/engine/downtimes/downtime_manager.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/logging/logger.hh" #include "com/centreon/engine/severity.hh" using namespace com::centreon; diff --git a/engine/src/configuration/applier/hostdependency.cc b/engine/src/configuration/applier/hostdependency.cc index f30f8f35b6c..a6d4ff886c2 100644 --- a/engine/src/configuration/applier/hostdependency.cc +++ b/engine/src/configuration/applier/hostdependency.cc @@ -23,6 +23,7 @@ #include "com/centreon/engine/configuration/applier/state.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/logging/logger.hh" using namespace com::centreon::engine::configuration; diff --git a/engine/src/configuration/applier/hostescalation.cc b/engine/src/configuration/applier/hostescalation.cc index f0db4b1b576..e6ce5c968a5 100644 --- a/engine/src/configuration/applier/hostescalation.cc +++ b/engine/src/configuration/applier/hostescalation.cc @@ -23,6 +23,7 @@ #include "com/centreon/engine/configuration/applier/state.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/logging/logger.hh" using namespace com::centreon::engine::configuration; diff --git a/engine/src/configuration/applier/service.cc b/engine/src/configuration/applier/service.cc index 3d83fb17eb5..f7e7ff1fca6 100644 --- a/engine/src/configuration/applier/service.cc +++ b/engine/src/configuration/applier/service.cc @@ -25,6 +25,7 @@ #include "com/centreon/engine/downtimes/downtime_manager.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/logging/logger.hh" #include "com/centreon/engine/severity.hh" using namespace com::centreon; diff --git a/engine/src/configuration/applier/serviceescalation.cc b/engine/src/configuration/applier/serviceescalation.cc index 8676491c66a..e867f18f3b0 100644 --- a/engine/src/configuration/applier/serviceescalation.cc +++ b/engine/src/configuration/applier/serviceescalation.cc @@ -22,6 +22,7 @@ #include "com/centreon/engine/configuration/applier/state.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/logging/logger.hh" using namespace com::centreon::engine::configuration; diff --git a/engine/src/configuration/applier/timeperiod.cc b/engine/src/configuration/applier/timeperiod.cc index 74338ac5c39..46aa058dca2 100644 --- a/engine/src/configuration/applier/timeperiod.cc +++ b/engine/src/configuration/applier/timeperiod.cc @@ -25,6 +25,7 @@ #include "com/centreon/engine/deleter/listmember.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/logging/logger.hh" using namespace com::centreon::engine::configuration; diff --git a/engine/src/configuration/hostdependency.cc b/engine/src/configuration/hostdependency.cc index 9083fd21d4b..dd906735da4 100644 --- a/engine/src/configuration/hostdependency.cc +++ b/engine/src/configuration/hostdependency.cc @@ -20,6 +20,7 @@ #include "com/centreon/engine/configuration/hostdependency.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/logging/logger.hh" #include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; diff --git a/engine/src/configuration/parser.cc b/engine/src/configuration/parser.cc index 1be2eb4a267..220a3bce18d 100644 --- a/engine/src/configuration/parser.cc +++ b/engine/src/configuration/parser.cc @@ -285,8 +285,6 @@ void parser::_parse_directory_configuration(std::string const& path) { * @param[in] path The configuration path. */ void parser::_parse_global_configuration(std::string const& path) { - engine_logger(logging::log_info_message, logging::most) - << "Reading main configuration file '" << path << "'."; config_logger->info("Reading main configuration file '{}'.", path); std::ifstream stream(path.c_str(), std::ios::binary); @@ -318,8 +316,6 @@ void parser::_parse_global_configuration(std::string const& path) { * @param[in] path The object definitions path. */ void parser::_parse_object_definitions(std::string const& path) { - engine_logger(logging::log_info_message, logging::basic) - << "Processing object config file '" << path << "'"; config_logger->info("Processing object config file '{}'", path); std::ifstream stream(path, std::ios::binary); @@ -396,8 +392,6 @@ void parser::_parse_object_definitions(std::string const& path) { * @param[in] path The resource file path. */ void parser::_parse_resource_file(std::string const& path) { - engine_logger(logging::log_info_message, logging::most) - << "Reading resource file '" << path << "'"; config_logger->info("Reading resource file '{}'", path); std::ifstream stream(path.c_str(), std::ios::binary); diff --git a/engine/src/configuration/servicegroup.cc b/engine/src/configuration/servicegroup.cc index 3855ff80897..d580b921d45 100644 --- a/engine/src/configuration/servicegroup.cc +++ b/engine/src/configuration/servicegroup.cc @@ -1,5 +1,5 @@ /** - * Copyright 2011-2013,2017 Centreon + * Copyright 2011-2013,2017-2024 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/engine/src/configuration/state.cc b/engine/src/configuration/state.cc index 290fa163782..e0c50a5a631 100644 --- a/engine/src/configuration/state.cc +++ b/engine/src/configuration/state.cc @@ -19,15 +19,14 @@ #include "com/centreon/engine/configuration/state.hh" #include "com/centreon/common/rapidjson_helper.hh" #include "com/centreon/engine/broker.hh" -#include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/logging/logger.hh" #include "com/centreon/io/file_entry.hh" #include "compatibility/locations.h" using namespace com::centreon; using namespace com::centreon::engine; using namespace com::centreon::engine::configuration; -using namespace com::centreon::engine::logging; using com::centreon::common::log_v2::log_v2; namespace com::centreon::engine::configuration::detail { @@ -1085,7 +1084,7 @@ unsigned int state::auto_rescheduling_interval() const noexcept { */ void state::auto_rescheduling_interval(unsigned int value) { if (!value) - throw engine_error() << "auto_rescheduling_interval cannot be 0"; + throw exceptions::msg_fmt("auto_rescheduling_interval cannot be 0"); _auto_rescheduling_interval = value; } @@ -1105,7 +1104,7 @@ unsigned int state::auto_rescheduling_window() const noexcept { */ void state::auto_rescheduling_window(unsigned int value) { if (!value) - throw engine_error() << "auto_rescheduling_window cannot be 0"; + throw exceptions::msg_fmt("auto_rescheduling_window cannot be 0"); _auto_rescheduling_window = value; } @@ -1141,13 +1140,13 @@ std::string const& state::broker_module_directory() const noexcept { * * @param[in] value The new broker_module_directory value. */ -void state::broker_module_directory(std::string const& value) { +void state::broker_module_directory(const std::string& value) { if (value.empty() || value[0] == '/') _broker_module_directory = value; else { - io::file_entry fe(_cfg_main); - std::string base_name(fe.directory_name()); - _broker_module_directory = base_name + "/" + value; + std::filesystem::path p(_cfg_main); + std::string base_name = p.parent_path(); + _broker_module_directory = fmt::format("{}/{}", base_name, value); } } @@ -1311,7 +1310,7 @@ unsigned int state::check_reaper_interval() const noexcept { */ void state::check_reaper_interval(unsigned int value) { if (!value) - throw engine_error() << "check_reaper_interval cannot be 0"; + throw exceptions::msg_fmt("check_reaper_interval cannot be 0"); _check_reaper_interval = value; } @@ -1440,9 +1439,9 @@ int state::command_check_interval() const noexcept { */ void state::command_check_interval(int value) { if (value < -1 || !value) - throw engine_error() - << "command_check_interval must be either positive or -1 (" << value - << " provided)"; + throw exceptions::msg_fmt( + "command_check_interval must be either positive or -1 ({} provided)", + value); _command_check_interval = value; @@ -1708,7 +1707,7 @@ uint64_t state::debug_level() const noexcept { */ void state::debug_level(uint64_t value) { if (value == std::numeric_limits::max()) - _debug_level = static_cast(all); + _debug_level = static_cast(logging::all); else _debug_level = value; } @@ -1728,8 +1727,8 @@ unsigned int state::debug_verbosity() const noexcept { * @param[in] value The new debug_verbosity value. */ void state::debug_verbosity(unsigned int value) { - if (value > most) - _debug_verbosity = static_cast(most); + if (value > logging::most) + _debug_verbosity = static_cast(logging::most); else _debug_verbosity = value; } @@ -1877,7 +1876,7 @@ unsigned int state::event_handler_timeout() const noexcept { */ void state::event_handler_timeout(unsigned int value) { if (!value) - throw engine_error() << "event_handler_timeout cannot be 0"; + throw exceptions::msg_fmt("event_handler_timeout cannot be 0"); _event_handler_timeout = value; } @@ -1987,8 +1986,9 @@ float state::high_host_flap_threshold() const noexcept { */ void state::high_host_flap_threshold(float value) { if (value <= 0.0 || value >= 100.0) - throw engine_error() << "high_host_flap_threshold must " - << "be between 0.0 and 100.0, both excluded"; + throw exceptions::msg_fmt( + "high_host_flap_threshold must be between 0.0 and 100.0, both " + "excluded"); _high_host_flap_threshold = value; } @@ -2008,8 +2008,9 @@ float state::high_service_flap_threshold() const noexcept { */ void state::high_service_flap_threshold(float value) { if (value <= 0.0 || value >= 100.0) - throw engine_error() << "high_service_flap_threshold " - << "must be between 0.0 and 100.0, both excluded"; + throw exceptions::msg_fmt( + "high_service_flap_threshold must be between 0.0 and 100.0, both " + "excluded"); _high_service_flap_threshold = value; } @@ -2129,7 +2130,7 @@ set_host& state::hosts() noexcept { * * @return Iterator to the host if found, hosts().end() if it was not. */ -set_host::const_iterator state::hosts_find(host::key_type const& k) const { +set_host::const_iterator state::hosts_find(const host::key_type& k) const { configuration::host below_searched(k); set_host::const_iterator it(_hosts.upper_bound(below_searched)); if ((it != _hosts.end()) && (it->host_id() == k)) @@ -2139,7 +2140,7 @@ set_host::const_iterator state::hosts_find(host::key_type const& k) const { return _hosts.end(); } -set_host::const_iterator state::hosts_find(std::string const& name) const { +set_host::const_iterator state::hosts_find(const std::string& name) const { for (set_host::const_iterator it(_hosts.begin()), end(_hosts.end()); it != end; ++it) { if (it->host_name() == name) @@ -2181,7 +2182,7 @@ unsigned int state::host_check_timeout() const noexcept { */ void state::host_check_timeout(unsigned int value) { if (!value) - throw engine_error() << "host_check_timeout cannot be 0"; + throw exceptions::msg_fmt("host_check_timeout cannot be 0"); _host_check_timeout = value; } @@ -2382,7 +2383,7 @@ unsigned int state::interval_length() const noexcept { */ void state::interval_length(unsigned int value) { if (!value) - throw engine_error() << "interval_length cannot be 0"; + throw exceptions::msg_fmt("interval_length cannot be 0"); if (!_command_check_interval_is_seconds && _command_check_interval != -1) { _command_check_interval /= _interval_length; @@ -2561,8 +2562,8 @@ float state::low_host_flap_threshold() const noexcept { */ void state::low_host_flap_threshold(float value) { if (value <= 0.0 || value >= 100.0) - throw engine_error() << "low_host_flap_threshold " - << "must be between 0.0 and 100.0, both excluded"; + throw exceptions::msg_fmt( + "low_host_flap_threshold must be between 0.0 and 100.0, both excluded"); _low_host_flap_threshold = value; } @@ -2582,8 +2583,9 @@ float state::low_service_flap_threshold() const noexcept { */ void state::low_service_flap_threshold(float value) { if (value <= 0.0 || value >= 100.0) - throw engine_error() << "low_service_flap_threshold " - << "must be between 0.0 and 100.0, both excluded"; + throw exceptions::msg_fmt( + "low_service_flap_threshold must be between 0.0 and 100.0, both " + "excluded"); _low_service_flap_threshold = value; } @@ -2621,7 +2623,7 @@ unsigned int state::max_host_check_spread() const noexcept { */ void state::max_host_check_spread(unsigned int value) { if (!value) - throw engine_error() << "max_host_check_spread cannot be 0"; + throw exceptions::msg_fmt("max_host_check_spread cannot be 0"); _max_host_check_spread = value; } @@ -2695,7 +2697,7 @@ unsigned int state::max_service_check_spread() const noexcept { */ void state::max_service_check_spread(unsigned int value) { if (!value) - throw engine_error() << "max_service_check_spread cannot be 0"; + throw exceptions::msg_fmt("max_service_check_spread cannot be 0"); _max_service_check_spread = value; } @@ -2715,7 +2717,7 @@ unsigned int state::notification_timeout() const noexcept { */ void state::notification_timeout(unsigned int value) { if (!value) - throw engine_error() << "notification_timeout cannot be 0"; + throw exceptions::msg_fmt("notification_timeout cannot be 0"); _notification_timeout = value; } @@ -2789,7 +2791,7 @@ unsigned int state::ochp_timeout() const noexcept { */ void state::ochp_timeout(unsigned int value) { if (!value) - throw engine_error() << "ochp_timeout cannot be 0"; + throw exceptions::msg_fmt("ochp_timeout cannot be 0"); _ochp_timeout = value; } @@ -2827,7 +2829,7 @@ unsigned int state::ocsp_timeout() const noexcept { */ void state::ocsp_timeout(unsigned int value) { if (!value) - throw engine_error() << "ocsp_timeout cannot be 0"; + throw exceptions::msg_fmt("ocsp_timeout cannot be 0"); _ocsp_timeout = value; } @@ -3063,7 +3065,7 @@ unsigned int state::retention_scheduling_horizon() const noexcept { */ void state::retention_scheduling_horizon(unsigned int value) { if (!value) - throw engine_error() << "retention_scheduling_horizon cannot be 0"; + throw exceptions::msg_fmt("retention_scheduling_horizon cannot be 0"); _retention_scheduling_horizon = value; } @@ -3083,7 +3085,7 @@ unsigned int state::retention_update_interval() const noexcept { */ void state::retention_update_interval(unsigned int value) { if (!value) - throw engine_error() << "retention_update_interval cannot be 0"; + throw exceptions::msg_fmt("retention_update_interval cannot be 0"); _retention_update_interval = value; } @@ -3325,7 +3327,7 @@ unsigned int state::service_check_timeout() const noexcept { */ void state::service_check_timeout(unsigned int value) { if (!value) - throw engine_error() << "service_check_timeout cannot be 0"; + throw exceptions::msg_fmt("service_check_timeout cannot be 0"); _service_check_timeout = value; } @@ -3345,7 +3347,7 @@ unsigned int state::service_freshness_check_interval() const noexcept { */ void state::service_freshness_check_interval(unsigned int value) { if (!value) - throw engine_error() << "service_freshness_check_interval cannot be 0"; + throw exceptions::msg_fmt("service_freshness_check_interval cannot be 0"); _service_freshness_check_interval = value; } @@ -3420,6 +3422,9 @@ std::string const& state::service_perfdata_file() const noexcept { * @param[in] value The new service_perfdata_file value. */ void state::service_perfdata_file(std::string const& value) { + config_logger->warn( + "Warning: service_perfdata_command is no more used for a long time, you " + "should not use it anymore."); _service_perfdata_file = value; } @@ -3512,8 +3517,8 @@ float state::sleep_time() const noexcept { */ void state::sleep_time(float value) { if (value <= 0.0) - throw engine_error() << "sleep_time cannot be less or equal to 0 (" << value - << " provided)"; + throw exceptions::msg_fmt( + "sleep_time cannot be less or equal to 0 ({} provided)", value); _sleep_time = value; } @@ -3553,9 +3558,9 @@ void state::state_retention_file(std::string const& value) { if (value.empty() || value[0] == '/') _state_retention_file = value; else { - io::file_entry fe(_cfg_main); - std::string base_name(fe.directory_name()); - _state_retention_file = base_name + "/" + value; + std::filesystem::path p{_cfg_main}; + std::string base_name(p.parent_path()); + _state_retention_file = fmt::format("{}/{}", base_name, value); } } @@ -3593,8 +3598,8 @@ unsigned int state::status_update_interval() const noexcept { */ void state::status_update_interval(unsigned int value) { if (value < 2) - throw engine_error() << "status_update_interval cannot be less than 2 (" - << value << " provided)"; + throw exceptions::msg_fmt( + "status_update_interval cannot be less than 2 ({} provided)", value); _status_update_interval = value; } @@ -3612,7 +3617,6 @@ bool state::set(char const* key, char const* value) { if (it != _setters.end()) return (it->second)->apply_from_cfg(*this, value); } catch (std::exception const& e) { - engine_logger(log_config_error, basic) << e.what(); config_logger->error(e.what()); return false; } @@ -3691,8 +3695,8 @@ unsigned int state::time_change_threshold() const noexcept { */ void state::time_change_threshold(unsigned int value) { if (value < 6) - throw engine_error() << "time_change_threshold cannot be less than 6 (" - << value << " provided)"; + throw exceptions::msg_fmt( + "time_change_threshold cannot be less than 6 ({} provided)", value); _time_change_threshold = value; } @@ -3723,7 +3727,7 @@ void state::user(std::unordered_map const& value) { */ void state::user(std::string const& key, std::string const& value) { if (key.size() < 3 || key[0] != '$' || key[key.size() - 1] != '$') - throw engine_error() << "Invalid user key '" << key << "'"; + throw exceptions::msg_fmt("Invalid user key '{}'", key); std::string new_key = key; new_key.erase(new_key.begin(), new_key.begin() + 1); new_key.erase(new_key.end() - 1, new_key.end()); @@ -3737,7 +3741,7 @@ void state::user(std::string const& key, std::string const& value) { * @param[in] value The user value. */ void state::user(unsigned int key, std::string const& value) { - _users[string::from(key)] = value; + _users[fmt::format_int(key).str()] = value; } /** @@ -3747,9 +3751,6 @@ void state::user(unsigned int key, std::string const& value) { * @param[in] value The new use_aggressive_host_checking value. */ void state::use_aggressive_host_checking(bool value __attribute__((unused))) { - engine_logger(log_verification_error, basic) - << "Warning: use_aggressive_host_checking is deprecated." - " This option is no more supported since version 21.04."; config_logger->warn( "Warning: use_aggressive_host_checking is deprecated. This option is " "no " @@ -3936,7 +3937,7 @@ std::string const& state::log_v2_logger() const noexcept { */ void state::log_v2_logger(std::string const& value) { if (value.empty()) - throw engine_error() << "log_v2_logger cannot be empty"; + throw exceptions::msg_fmt("log_v2_logger cannot be empty"); _log_v2_logger = value; } @@ -3958,7 +3959,8 @@ void state::log_level_functions(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_functions = value; else - throw engine_error() << "error wrong level setted for log_level_functions"; + throw exceptions::msg_fmt( + "error wrong level setted for log_level_functions"); } /** @@ -3979,7 +3981,7 @@ void state::log_level_config(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_config = value; else - throw engine_error() << "error wrong level setted for log_level_config"; + throw exceptions::msg_fmt("error wrong level setted for log_level_config"); } /** @@ -4000,7 +4002,7 @@ void state::log_level_events(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_events = value; else - throw engine_error() << "error wrong level setted for log_level_events"; + throw exceptions::msg_fmt("error wrong level setted for log_level_events"); } /** @@ -4021,7 +4023,7 @@ void state::log_level_checks(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_checks = value; else - throw engine_error() << "error wrong level setted for log_level_checks"; + throw exceptions::msg_fmt("error wrong level setted for log_level_checks"); } /** @@ -4042,8 +4044,8 @@ void state::log_level_notifications(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_notifications = value; else - throw engine_error() - << "error wrong level setted for log_level_notifications"; + throw exceptions::msg_fmt( + "error wrong level setted for log_level_notifications"); } /** @@ -4064,8 +4066,8 @@ void state::log_level_eventbroker(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_eventbroker = value; else - throw engine_error() - << "error wrong level setted for log_level_eventbroker"; + throw exceptions::msg_fmt( + "error wrong level setted for log_level_eventbroker"); } /** @@ -4086,8 +4088,8 @@ void state::log_level_external_command(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_external_command = value; else - throw engine_error() - << "error wrong level setted for log_level_external_command"; + throw exceptions::msg_fmt( + "error wrong level setted for log_level_external_command"); } /** @@ -4108,7 +4110,8 @@ void state::log_level_commands(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_commands = value; else - throw engine_error() << "error wrong level setted for log_level_commands"; + throw exceptions::msg_fmt( + "error wrong level setted for log_level_commands"); } /** @@ -4129,7 +4132,8 @@ void state::log_level_downtimes(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_downtimes = value; else - throw engine_error() << "error wrong level setted for log_level_downtimes"; + throw exceptions::msg_fmt( + "error wrong level setted for log_level_downtimes"); } /** @@ -4150,7 +4154,8 @@ void state::log_level_comments(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_comments = value; else - throw engine_error() << "error wrong level setted for log_level_comments"; + throw exceptions::msg_fmt( + "error wrong level setted for log_level_comments"); } /** @@ -4171,7 +4176,7 @@ void state::log_level_macros(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_macros = value; else - throw engine_error() << "error wrong level setted for log_level_macros"; + throw exceptions::msg_fmt("error wrong level setted for log_level_macros"); } /** @@ -4192,7 +4197,7 @@ void state::log_level_process(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_process = value; else - throw engine_error() << "error wrong level setted for log_level_process"; + throw exceptions::msg_fmt("error wrong level setted for log_level_process"); } /** @@ -4213,7 +4218,7 @@ void state::log_level_runtime(std::string const& value) { if (log_v2::instance().contains_level(value)) _log_level_runtime = value; else - throw engine_error() << "error wrong level setted for log_level_runtime"; + throw exceptions::msg_fmt("error wrong level setted for log_level_runtime"); } /** @@ -4259,8 +4264,6 @@ void state::use_true_regexp_matching(bool value) { */ void state::_set_aggregate_status_updates(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: aggregate_status_updates variable ignored"; config_logger->warn("Warning: aggregate_status_updates variable ignored"); ++config_warnings; } @@ -4272,8 +4275,6 @@ void state::_set_aggregate_status_updates(std::string const& value) { */ void state::_set_auth_file(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: auth_file variable ignored"; config_logger->warn("Warning: auth_file variable ignored"); ++config_warnings; } @@ -4285,8 +4286,6 @@ void state::_set_auth_file(std::string const& value) { */ void state::_set_bare_update_check(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: bare_update_check variable ignored"; config_logger->warn("Warning: bare_update_check variable ignored"); ++config_warnings; } @@ -4309,9 +4308,9 @@ void state::_set_cfg_dir(std::string const& value) { if (value.empty() || value[0] == '/') _cfg_dir.push_back(value); else { - io::file_entry fe(_cfg_main); - std::string base_name(fe.directory_name()); - _cfg_dir.push_back(base_name + "/" + value); + std::filesystem::path p{_cfg_main}; + std::string base_name(p.parent_path()); + _cfg_dir.emplace_back(fmt::format("{}/{}", base_name, value)); } } @@ -4320,13 +4319,13 @@ void state::_set_cfg_dir(std::string const& value) { * * @param[in] value The new configuration file. */ -void state::_set_cfg_file(std::string const& value) { +void state::_set_cfg_file(const std::string& value) { if (value.empty() || value[0] == '/') _cfg_file.push_back(value); else { - io::file_entry fe(_cfg_main); - std::string base_name(fe.directory_name()); - _cfg_file.push_back(base_name + "/" + value); + std::filesystem::path p{_cfg_main}; + std::string base_name(p.parent_path()); + _cfg_file.emplace_back(fmt::format("{}/{}", base_name, value)); } } @@ -4337,8 +4336,6 @@ void state::_set_cfg_file(std::string const& value) { */ void state::_set_check_for_updates(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: check_for_updates variable ignored"; config_logger->warn("Warning: check_for_updates variable ignored"); ++config_warnings; } @@ -4350,8 +4347,6 @@ void state::_set_check_for_updates(std::string const& value) { */ void state::_set_child_processes_fork_twice(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: child_processes_fork_twice variable ignored"; config_logger->warn("Warning: child_processes_fork_twice variable ignored"); ++config_warnings; } @@ -4381,8 +4376,6 @@ void state::_set_command_check_interval(std::string const& value) { */ void state::_set_comment_file(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: comment_file variable ignored"; config_logger->warn("Warning: comment_file variable ignored"); ++config_warnings; } @@ -4394,8 +4387,6 @@ void state::_set_comment_file(std::string const& value) { */ void state::_set_daemon_dumps_core(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: daemon_dumps_core variable ignored"; config_logger->warn("Warning: daemon_dumps_core variable ignored"); ++config_warnings; } @@ -4423,7 +4414,7 @@ void state::_set_date_format(std::string const& value) { */ void state::_set_downtime_file(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) + engine_logger(logging::log_config_warning, logging::basic) << "Warning: downtime_file variable ignored"; config_logger->warn("Warning: downtime_file variable ignored"); ++config_warnings; @@ -4436,7 +4427,7 @@ void state::_set_downtime_file(std::string const& value) { */ void state::_set_enable_embedded_perl(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) + engine_logger(logging::log_config_warning, logging::basic) << "Warning: enable_embedded_perl variable ignored"; config_logger->warn("Warning: enable_embedded_perl variable ignored"); ++config_warnings; @@ -4449,7 +4440,7 @@ void state::_set_enable_embedded_perl(std::string const& value) { */ void state::_set_enable_failure_prediction(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) + engine_logger(logging::log_config_warning, logging::basic) << "Warning: enable_failure_prediction variable ignored"; config_logger->warn("Warning: enable_failure_prediction variable ignored"); ++config_warnings; @@ -4477,8 +4468,6 @@ void state::_set_event_broker_options(std::string const& value) { */ void state::_set_free_child_process_memory(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: free_child_process_memory variable ignored"; config_logger->warn("Warning: free_child_process_memory variable ignored"); ++config_warnings; } @@ -4499,10 +4488,11 @@ void state::_set_host_inter_check_delay_method(std::string const& value) { _host_inter_check_delay_method = icd_user; if (!string::to(value.c_str(), scheduling_info.host_inter_check_delay) || scheduling_info.host_inter_check_delay <= 0.0) - throw engine_error() - << "Invalid value for host_inter_check_delay_method, must " - << "be one of 'n' (none), 'd' (dumb), 's' (smart) or a " - << "stricly positive value (" << value << " provided)"; + throw exceptions::msg_fmt( + "Invalid value for host_inter_check_delay_method, must be one of 'n' " + "(none), 'd' (dumb), 's' (smart) or a stricly positive value ({} " + "provided)", + value); } } @@ -4527,8 +4517,6 @@ void state::_set_host_perfdata_file_mode(std::string const& value) { */ void state::_set_lock_file(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: lock_file variable ignored"; config_logger->warn("Warning: lock_file variable ignored"); ++config_warnings; } @@ -4540,8 +4528,6 @@ void state::_set_lock_file(std::string const& value) { */ void state::_set_log_archive_path(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: log_archive_path variable ignored"; config_logger->warn("Warning: log_archive_path variable ignored"); ++config_warnings; } @@ -4553,8 +4539,6 @@ void state::_set_log_archive_path(std::string const& value) { */ void state::_set_log_initial_states(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: log_initial_states variable ignored"; config_logger->warn("Warning: log_initial_states variable ignored"); ++config_warnings; return; @@ -4567,8 +4551,6 @@ void state::_set_log_initial_states(std::string const& value) { */ void state::_set_log_rotation_method(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: log_rotation_method variable ignored"; config_logger->warn("Warning: log_rotation_method variable ignored"); ++config_warnings; } @@ -4580,8 +4562,6 @@ void state::_set_log_rotation_method(std::string const& value) { */ void state::_set_nagios_group(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: nagios_group variable ignored"; config_logger->warn("Warning: nagios_group variable ignored"); ++config_warnings; } @@ -4593,8 +4573,6 @@ void state::_set_nagios_group(std::string const& value) { */ void state::_set_nagios_user(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: nagios_user variable ignored"; config_logger->warn("Warning: nagios_user variable ignored"); ++config_warnings; } @@ -4606,8 +4584,6 @@ void state::_set_nagios_user(std::string const& value) { */ void state::_set_object_cache_file(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: object_cache_file variable ignored"; config_logger->warn("Warning: object_cache_file variable ignored"); ++config_warnings; } @@ -4619,8 +4595,6 @@ void state::_set_object_cache_file(std::string const& value) { */ void state::_set_p1_file(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: p1_file variable ignored"; config_logger->warn("Warning: p1_file variable ignored"); ++config_warnings; @@ -4633,8 +4607,6 @@ void state::_set_p1_file(std::string const& value) { */ void state::_set_precached_object_file(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: precached_object_file variable ignored"; config_logger->warn("Warning: precached_object_file variable ignored"); ++config_warnings; } @@ -4662,8 +4634,6 @@ void state::_set_resource_file(std::string const& value) { void state::_set_retained_process_service_attribute_mask( std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: retained_process_service_attribute_mask variable ignored"; config_logger->warn( "Warning: retained_process_service_attribute_mask variable ignored"); ++config_warnings; @@ -4676,8 +4646,6 @@ void state::_set_retained_process_service_attribute_mask( */ void state::_set_retained_service_attribute_mask(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: retained_service_attribute_mask variable ignored"; config_logger->warn( "Warning: retained_service_attribute_mask variable ignored"); ++config_warnings; @@ -4699,10 +4667,11 @@ void state::_set_service_inter_check_delay_method(std::string const& value) { _service_inter_check_delay_method = icd_user; if (!string::to(value.c_str(), scheduling_info.service_inter_check_delay) || scheduling_info.service_inter_check_delay <= 0.0) - throw engine_error() - << "Invalid value for service_inter_check_delay_method, " - << "must be one of 'n' (none), 'd' (dumb), 's' (smart) or " - << "a strictly positive value (" << value << " provided)"; + throw exceptions::msg_fmt( + "Invalid value for service_inter_check_delay_method, must be one of " + "'n' (none), 'd' (dumb), 's' (smart) or a strictly positive value " + "({} provided)", + value); } } @@ -4743,8 +4712,6 @@ void state::_set_service_perfdata_file_mode(std::string const& value) { */ void state::_set_temp_file(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: temp_file variable ignored"; config_logger->warn("Warning: temp_file variable ignored"); ++config_warnings; } @@ -4756,8 +4723,6 @@ void state::_set_temp_file(std::string const& value) { */ void state::_set_temp_path(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: temp_path variable ignored"; config_logger->warn("Warning: temp_path variable ignored"); ++config_warnings; } @@ -4769,8 +4734,6 @@ void state::_set_temp_path(std::string const& value) { */ void state::_set_use_embedded_perl_implicitly(std::string const& value) { (void)value; - engine_logger(log_config_warning, basic) - << "Warning: use_embedded_perl_implicitly variable ignored"; config_logger->warn("Warning: use_embedded_perl_implicitly variable ignored"); ++config_warnings; } diff --git a/engine/src/configuration/timeperiod.cc b/engine/src/configuration/timeperiod.cc index 1e16e61d6c9..fa17aee4d7d 100644 --- a/engine/src/configuration/timeperiod.cc +++ b/engine/src/configuration/timeperiod.cc @@ -26,7 +26,6 @@ using namespace com::centreon; using namespace com::centreon::engine; using namespace com::centreon::engine::configuration; -using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ &object::setter::generic diff --git a/engine/src/retention/dump.cc b/engine/src/retention/dump.cc index 5e8962e3917..cc129497c71 100644 --- a/engine/src/retention/dump.cc +++ b/engine/src/retention/dump.cc @@ -29,6 +29,7 @@ #include "com/centreon/engine/downtimes/service_downtime.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/logging/logger.hh" using namespace com::centreon::engine; using namespace com::centreon::engine::configuration::applier; From 5eab0e0d0a0c1574977fd33c7629f4351534f850 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Wed, 22 May 2024 18:07:48 +0200 Subject: [PATCH 05/21] fix(engine/string): string lib a little less used in configuration module --- engine/src/configuration/applier/macros.cc | 15 +++---- engine/src/configuration/state.cc | 44 ++++++++++++++----- .../configuration/applier/applier-service.cc | 14 +++--- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/engine/src/configuration/applier/macros.cc b/engine/src/configuration/applier/macros.cc index 271dd250236..4fe1be01a90 100644 --- a/engine/src/configuration/applier/macros.cc +++ b/engine/src/configuration/applier/macros.cc @@ -37,16 +37,15 @@ using namespace com::centreon::engine::configuration; * @return True if the key is old-style and has been parsed succesfully. */ static bool is_old_style_user_macro(std::string const& key, unsigned int& val) { - if (::strncmp(key.c_str(), "USER", ::strlen("USER")) != 0) - return (false); + if (std::string_view(key.data(), 4) != "USER") + return false; - std::string rest = key.substr(4); + std::string_view rest(key.data() + 4, key.size() - 4); // Super strict validation. - for (size_t i = 0; i < rest.size(); ++i) - if (rest[i] < '0' || rest[i] > '9') - return (false); - string::to(rest.c_str(), val); - return (true); + for (auto c : rest) + if (c < '0' || c > '9') + return false; + return absl::SimpleAtoi(rest, &val); } /** diff --git a/engine/src/configuration/state.cc b/engine/src/configuration/state.cc index e0c50a5a631..51a42f064ff 100644 --- a/engine/src/configuration/state.cc +++ b/engine/src/configuration/state.cc @@ -36,13 +36,37 @@ struct setter : public setter_base { bool apply_from_cfg(state& obj, char const* value) override { try { U val(0); - if (!string::to(value, val)) - return false; - (obj.*ptr)(val); - } catch (std::exception const& e) { - SPDLOG_LOGGER_ERROR(config_logger, "fail to update {} with value {}: {}", - setter_base::_field_name, value, e.what()); - return false; + if constexpr (std::is_same_v) { + if (!absl::SimpleAtob(value, &val)) + return false; + (obj.*ptr)(val); + } else if constexpr (std::is_same_v) { + uint32_t v; + if (!absl::SimpleAtoi(value, &v)) + return false; + if (v > 0xffffu) + return false; + else + val = v; + (obj.*ptr)(val); + } else if constexpr (std::is_integral::value) { + if (!absl::SimpleAtoi(value, &val)) + return false; + (obj.*ptr)(val); + } else if constexpr (std::is_same_v) { + if (!absl::SimpleAtof(value, &val)) + return false; + (obj.*ptr)(val); + } else if constexpr (std::is_same_v) { + if (!absl::SimpleAtod(value, &val)) + return false; + (obj.*ptr)(val); + } else { + static_assert(std::is_integral_v || std::is_floating_point_v || + std::is_same_v); + } + } catch (const std::exception& e) { + engine_logger(logging::log_config_error, logging::basic) << e.what(); } return true; } @@ -4486,7 +4510,7 @@ void state::_set_host_inter_check_delay_method(std::string const& value) { _host_inter_check_delay_method = icd_smart; else { _host_inter_check_delay_method = icd_user; - if (!string::to(value.c_str(), scheduling_info.host_inter_check_delay) || + if (!absl::SimpleAtod(value, &scheduling_info.host_inter_check_delay) || scheduling_info.host_inter_check_delay <= 0.0) throw exceptions::msg_fmt( "Invalid value for host_inter_check_delay_method, must be one of 'n' " @@ -4665,7 +4689,7 @@ void state::_set_service_inter_check_delay_method(std::string const& value) { _service_inter_check_delay_method = icd_smart; else { _service_inter_check_delay_method = icd_user; - if (!string::to(value.c_str(), scheduling_info.service_inter_check_delay) || + if (!absl::SimpleAtod(value, &scheduling_info.service_inter_check_delay) || scheduling_info.service_inter_check_delay <= 0.0) throw exceptions::msg_fmt( "Invalid value for service_inter_check_delay_method, must be one of " @@ -4685,7 +4709,7 @@ void state::_set_service_interleave_factor_method(std::string const& value) { _service_interleave_factor_method = ilf_smart; else { _service_interleave_factor_method = ilf_user; - if (!string::to(value.c_str(), scheduling_info.service_interleave_factor) || + if (!absl::SimpleAtoi(value, &scheduling_info.service_interleave_factor) || scheduling_info.service_interleave_factor < 1) scheduling_info.service_interleave_factor = 1; } diff --git a/engine/tests/configuration/applier/applier-service.cc b/engine/tests/configuration/applier/applier-service.cc index 80acf18f9c1..fbbf969e7b1 100644 --- a/engine/tests/configuration/applier/applier-service.cc +++ b/engine/tests/configuration/applier/applier-service.cc @@ -31,12 +31,14 @@ #include "com/centreon/engine/configuration/service.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/service.hh" +#include "com/centreon/exceptions/msg_fmt.hh" #include "helper.hh" using namespace com::centreon; using namespace com::centreon::engine; using namespace com::centreon::engine::configuration; using namespace com::centreon::engine::configuration::applier; +using com::centreon::exceptions::msg_fmt; class ApplierService : public TestEngine { public: @@ -268,18 +270,18 @@ TEST_F(ApplierService, ServicesCheckValidity) { configuration::service csvc; // No service description - ASSERT_THROW(csvc.check_validity(), engine::exceptions::error); + ASSERT_THROW(csvc.check_validity(), msg_fmt); ASSERT_TRUE(csvc.parse("service_description", "check description")); ASSERT_TRUE(csvc.parse("service_id", "53")); // No host attached to - ASSERT_THROW(csvc.check_validity(), engine::exceptions::error); + ASSERT_THROW(csvc.check_validity(), msg_fmt); ASSERT_TRUE(csvc.parse("hosts", "test_host")); // No check command attached to - ASSERT_THROW(csvc.check_validity(), engine::exceptions::error); + ASSERT_THROW(csvc.check_validity(), msg_fmt); configuration::applier::command cmd_aply; configuration::command cmd("cmd"); @@ -764,7 +766,7 @@ TEST_F(ApplierService, ServicesCheckValidityTags) { configuration::service csvc; // No service description - ASSERT_THROW(csvc.check_validity(), engine::exceptions::error); + ASSERT_THROW(csvc.check_validity(), msg_fmt); ASSERT_TRUE(csvc.parse("service_description", "check description")); ASSERT_TRUE(csvc.parse("service_id", "53")); @@ -772,12 +774,12 @@ TEST_F(ApplierService, ServicesCheckValidityTags) { ASSERT_TRUE(csvc.parse("category_tags", "3")); // No host attached to - ASSERT_THROW(csvc.check_validity(), engine::exceptions::error); + ASSERT_THROW(csvc.check_validity(), msg_fmt); ASSERT_TRUE(csvc.parse("hosts", "test_host")); // No check command attached to - ASSERT_THROW(csvc.check_validity(), engine::exceptions::error); + ASSERT_THROW(csvc.check_validity(), msg_fmt); configuration::applier::command cmd_aply; configuration::command cmd("cmd"); From 823bb5cc2b79434e1586ca663daa943daeea7f1d Mon Sep 17 00:00:00 2001 From: David Boucher Date: Thu, 23 May 2024 11:16:01 +0200 Subject: [PATCH 06/21] enh(engine/string): always on string functions --- clib/inc/com/centreon/exceptions/error.hh | 43 ++ .../engine/configuration/file_info.hh | 41 +- .../centreon/engine/configuration/parser.hh | 8 +- engine/src/configuration/parser.cc | 528 +++++++++++------- 4 files changed, 394 insertions(+), 226 deletions(-) create mode 100644 clib/inc/com/centreon/exceptions/error.hh diff --git a/clib/inc/com/centreon/exceptions/error.hh b/clib/inc/com/centreon/exceptions/error.hh new file mode 100644 index 00000000000..c5a66dd3f3d --- /dev/null +++ b/clib/inc/com/centreon/exceptions/error.hh @@ -0,0 +1,43 @@ +/** + * Copyright 2023 Centreon + * + * 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 CCB_EXCEPTIONS_ERROR_HH +#define CCB_EXCEPTIONS_ERROR_HH + +#include "com/centreon/exceptions/msg_fmt.hh" + +namespace com::centreon::exceptions { +/** + * @class error error.hh "com/centreon/exceptions/error.hh" + * @brief Shutdown exception class. + * + * This exception is thrown when someone attemps to read from a + * stream that has been error. + */ +class error : public com::centreon::exceptions::msg_fmt { + public: + template + explicit error(std::string const& str, const Args&... args) + : msg_fmt(str, args...) {} + error() = delete; + ~error() noexcept {} + error& operator=(const error&) = delete; +}; +} // namespace com::centreon::exceptions + +#endif // !CCB_EXCEPTIONS_ERROR_HH diff --git a/engine/inc/com/centreon/engine/configuration/file_info.hh b/engine/inc/com/centreon/engine/configuration/file_info.hh index 79225c399d8..cef0b981cd2 100644 --- a/engine/inc/com/centreon/engine/configuration/file_info.hh +++ b/engine/inc/com/centreon/engine/configuration/file_info.hh @@ -20,6 +20,8 @@ #ifndef CCE_CONFIGURATION_FILE_INFO_HH #define CCE_CONFIGURATION_FILE_INFO_HH +#include + #include "com/centreon/engine/exceptions/error.hh" namespace com::centreon::engine { @@ -30,35 +32,42 @@ class file_info { std::string _path; public: - file_info(std::string const& path = "", unsigned int line = 0) + file_info(const std::string& path, uint32_t line) : _line(line), _path(path) {} - file_info(file_info const& right) { operator=(right); } - ~file_info() noexcept {} - file_info& operator=(file_info const& right) { - if (this != &right) { - _line = right._line; - _path = right._path; + file_info(file_info&& other) + : _line{other._line}, _path{std::move(other._path)} {} + ~file_info() noexcept = default; + file_info(const file_info&) = delete; + file_info& operator=(const file_info&) = delete; + file_info& operator=(file_info&& other) { + if (this != &other) { + _line = other._line; + _path = std::move(other._path); } return *this; } - bool operator==(file_info const& right) const noexcept { - return _line == right._line && _path == right._path; - } - bool operator!=(file_info const& right) const noexcept { - return !operator==(right); - } + friend exceptions::error& operator<<(exceptions::error& err, file_info const& info) { err << "in file '" << info.path() << "' on line " << info.line(); return err; } + friend std::ostream& operator<<(std::ostream& os, const file_info& info) { + os << fmt::format("in file '{}' on line {}", info.path(), info.line()); + return os; + } unsigned int line() const noexcept { return _line; } - void line(unsigned int line) noexcept { _line = line; } - std::string const& path() const noexcept { return _path; } - void path(std::string const& path) { _path = path; } + const std::string& path() const noexcept { return _path; } }; + } // namespace configuration } // namespace com::centreon::engine +namespace fmt { +template <> +struct formatter + : ostream_formatter {}; +} // namespace fmt + #endif // !CCE_CONFIGURATION_FILE_INFO_HH diff --git a/engine/inc/com/centreon/engine/configuration/parser.hh b/engine/inc/com/centreon/engine/configuration/parser.hh index be6652587cc..704043179fa 100644 --- a/engine/inc/com/centreon/engine/configuration/parser.hh +++ b/engine/inc/com/centreon/engine/configuration/parser.hh @@ -61,8 +61,8 @@ class parser { }; parser(unsigned int read_options = read_all); - ~parser() throw(); - void parse(std::string const& path, state& config); + ~parser() noexcept = default; + void parse(const std::string& path, state& config); private: typedef void (parser::*store)(object_ptr obj); @@ -88,10 +88,10 @@ class parser { static void _insert(map_object const& from, std::set& to); std::string const& _map_object_type(map_object const& objects) const throw(); void _parse_directory_configuration(std::string const& path); - void _parse_global_configuration(std::string const& path); - void _parse_object_definitions(std::string const& path); + void _parse_object_definitions(const std::string& path); void _parse_resource_file(std::string const& path); void _resolve_template(); + void _parse_global_configuration(std::string const& path); void _store_into_list(object_ptr obj); template void _store_into_map(object_ptr obj); diff --git a/engine/src/configuration/parser.cc b/engine/src/configuration/parser.cc index 220a3bce18d..df38d695c9b 100644 --- a/engine/src/configuration/parser.cc +++ b/engine/src/configuration/parser.cc @@ -20,11 +20,15 @@ #include "com/centreon/engine/configuration/parser.hh" #include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" +#include "com/centreon/exceptions/error.hh" +#include "com/centreon/exceptions/msg_fmt.hh" #include "com/centreon/io/directory_entry.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; using namespace com::centreon::io; +using com::centreon::exceptions::error; +using com::centreon::exceptions::msg_fmt; parser::store parser::_store[] = { &parser::_store_into_map, @@ -46,6 +50,30 @@ parser::store parser::_store[] = { &parser::_store_into_list, &parser::_store_into_list}; +/** + * Get the next valid line. + * + * @param[in, out] stream The current stream to read new line. + * @param[out] line The line to fill. + * @param[in, out] pos The current position. + * + * @return True if data is available, false if no data. + */ +static bool get_next_line(std::ifstream& stream, + std::string& line, + uint32_t& pos) { + while (std::getline(stream, line, '\n')) { + ++pos; + line = absl::StripAsciiWhitespace(line); + if (!line.empty()) { + char c = line[0]; + if (c != '#' && c != ';' && c != '\x0') + return true; + } + } + return false; +} + /** * Default constructor. * @@ -56,9 +84,213 @@ parser::parser(unsigned int read_options) : _config(NULL), _read_options(read_options) {} /** - * Destructor. + * Parse the object definition file. + * + * @param[in] path The object definitions path. */ -parser::~parser() throw() {} +void parser::_parse_object_definitions(std::string const& path) { + config_logger->info("Processing object config file '{}'", path); + + std::ifstream stream(path, std::ios::binary); + if (!stream.is_open()) + throw msg_fmt("Parsing of object definition failed: can't open file '{}'", + path); + + _current_line = 0; + _current_path = path; + + bool parse_object = false; + object_ptr obj; + std::string input; + while (get_next_line(stream, input, _current_line)) { + // Multi-line. + while ('\\' == input[input.size() - 1]) { + input.resize(input.size() - 1); + std::string addendum; + if (!get_next_line(stream, addendum, _current_line)) + break; + input.append(addendum); + } + + // Check if is a valid object. + if (obj == nullptr) { + if (input.find("define") || !std::isspace(input[6])) + throw msg_fmt( + "Parsing of object definition failed " + "in file '{}' on line {}: Unexpected start definition", + _current_path, _current_line); + input.erase(0, 6); + absl::StripLeadingAsciiWhitespace(&input); + std::size_t last = input.size() - 1; + if (input.empty() || input[last] != '{') + throw msg_fmt( + "Parsing of object definition failed in file '{}' on line {}: " + "Unexpected start definition", + _current_path, _current_line); + input.erase(last); + absl::StripTrailingAsciiWhitespace(&input); + obj = object::create(input); + if (obj == nullptr) + throw msg_fmt( + "Parsing of object definition failed in file '{}' on line {}: " + "Unknown object type name '{}'", + _current_path, _current_line, input); + parse_object = (_read_options & (1 << obj->type())); + _objects_info.emplace(obj.get(), file_info(path, _current_line)); + } + // Check if is the not the end of the current object. + else if (input != "}") { + if (parse_object) { + if (!obj->parse(input)) + throw msg_fmt( + "Parsing of object definition failed in file '{}' on line {}: " + "Invalid line '{}'", + _current_path, _current_line, input); + } + } + // End of the current object. + else { + if (parse_object) { + if (!obj->name().empty()) + _add_template(obj); + if (obj->should_register()) + _add_object(obj); + } + obj.reset(); + } + } +} + +/** + * Parse the directory configuration. + * + * @param[in] path The directory path. + */ +void parser::_parse_directory_configuration(std::string const& path) { + for (auto& entry : std::filesystem::directory_iterator(path)) { + if (entry.is_regular_file() && entry.path().extension() == ".cfg") + _parse_object_definitions(entry.path().string()); + } +} + +/** + * Parse the global configuration file. + * + * @param[in] path The configuration path. + */ +void parser::_parse_global_configuration(const std::string& path) { + config_logger->info("Reading main configuration file '{}'.", path); + + std::ifstream stream(path, std::ios::binary); + if (!stream.is_open()) + throw msg_fmt( + "Parsing of global configuration failed: can't open file '{}'", path); + + _config->cfg_main(path); + + _current_line = 0; + _current_path = path; + + std::string input; + while (get_next_line(stream, input, _current_line)) { + std::list values = + absl::StrSplit(input, absl::MaxSplits('=', 1)); + if (values.size() == 2) { + auto it = values.begin(); + char const* key = it->c_str(); + ++it; + char const* value = it->c_str(); + if (_config->set(key, value)) + continue; + } + throw msg_fmt( + "Parsing of global configuration failed in file '{}' on line {}: " + "Invalid line '{}'", + path, _current_line, input); + } +} + +/** + * Parse the resource file. + * + * @param[in] path The resource file path. + */ +void parser::_parse_resource_file(std::string const& path) { + config_logger->info("Reading resource file '{}'", path); + + std::ifstream stream(path.c_str(), std::ios::binary); + if (!stream.is_open()) + throw msg_fmt("Parsing of resource file failed: can't open file '{}'", + path); + + _current_line = 0; + _current_path = path; + + std::string input; + while (get_next_line(stream, input, _current_line)) { + try { + std::list key_value = + absl::StrSplit(input, absl::MaxSplits('=', 1)); + if (key_value.size() == 2) { + auto it = key_value.begin(); + std::string& key = *it; + ++it; + std::string value = *it; + _config->user(key, value); + } else + throw msg_fmt( + "Parsing of resource file '{}' failed on line {}; Invalid line " + "'{}'", + _current_path, _current_line, input); + } catch (std::exception const& e) { + (void)e; + throw msg_fmt( + "Parsing of resource file '{}' failed on line {}: Invalid line '{}'", + _current_path, _current_line, input); + } + } +} + +/** + * Resolve template for register objects. + */ +void parser::_resolve_template() { + for (map_object& templates : _templates) { + for (map_object::iterator it = templates.begin(), end = templates.end(); + it != end; ++it) + it->second->resolve_template(templates); + } + + for (uint32_t i = 0; i < _lst_objects.size(); ++i) { + map_object& templates = _templates[i]; + for (list_object::iterator it = _lst_objects[i].begin(), + end = _lst_objects[i].end(); + it != end; ++it) { + (*it)->resolve_template(templates); + try { + (*it)->check_validity(); + } catch (std::exception const& e) { + throw error("Configuration parsing failed {}: {}", + _get_file_info(it->get()), e.what()); + } + } + } + + for (uint32_t i = 0; i < _map_objects.size(); ++i) { + map_object& templates = _templates[i]; + for (map_object::iterator it = _map_objects[i].begin(), + end = _map_objects[i].end(); + it != end; ++it) { + it->second->resolve_template(templates); + try { + it->second->check_validity(); + } catch (const std::exception& e) { + throw error("Configuration parsing failed {}: {}", + _get_file_info(it->second.get()), e.what()); + } + } + } +} /** * Parse configuration file. @@ -66,7 +298,7 @@ parser::~parser() throw() {} * @param[in] path The configuration file path. * @param[in] config The state configuration to fill. */ -void parser::parse(std::string const& path, state& config) { +void parser::parse(const std::string& path, state& config) { _config = &config; // parse the global configuration file. @@ -118,7 +350,6 @@ void parser::parse(std::string const& path, state& config) { void parser::_add_object(object_ptr obj) { if (obj->should_register()) (this->*_store[obj->type()])(obj); - return; } /** @@ -127,17 +358,14 @@ void parser::_add_object(object_ptr obj) { * @param[in] obj The tempalte to add into the list. */ void parser::_add_template(object_ptr obj) { - std::string const& name(obj->name()); + const std::string& name = obj->name(); if (name.empty()) - throw engine_error() << "Parsing of " << obj->type_name() << " failed " - << _get_file_info(obj.get()) - << ": Property 'name' " - "is missing"; - map_object& tmpl(_templates[obj->type()]); + throw msg_fmt("Parsing of {} failed {}: Property 'name' is missing", + obj->type_name(), _get_file_info(obj.get())); + map_object& tmpl = _templates[obj->type()]; if (tmpl.find(name) != tmpl.end()) - throw engine_error() << "Parsing of " << obj->type_name() << " failed " - << _get_file_info(obj.get()) << ": " << name - << " already exists"; + throw msg_fmt("Parsing of {} failed {}: '{}' already exists", + obj->type_name(), _get_file_info(obj.get()), name); tmpl[name] = obj; } @@ -168,8 +396,8 @@ file_info const& parser::_get_file_info(object* obj) const { if (it != _objects_info.end()) return it->second; } - throw engine_error() << "Parsing failed: Object not " - "found into the file information cache"; + throw msg_fmt( + "Parsing failed: Object not found into the file information cache"); } /** @@ -233,7 +461,6 @@ void parser::_insert(list_object const& from, std::set& to) { for (list_object::const_iterator it(from.begin()), end(from.end()); it != end; ++it) to.insert(*static_cast(it->get())); - return; } /** @@ -247,7 +474,6 @@ void parser::_insert(map_object const& from, std::set& to) { for (map_object::const_iterator it(from.begin()), end(from.end()); it != end; ++it) to.insert(*static_cast(it->second.get())); - return; } /** @@ -267,223 +493,113 @@ std::string const& parser::_map_object_type(map_object const& objects) const } /** - * Parse the directory configuration. + * Store object into the list. * - * @param[in] path The directory path. + * @param[in] obj The object to store. */ -void parser::_parse_directory_configuration(std::string const& path) { - directory_entry dir(path); - std::list const& lst(dir.entry_list("*.cfg")); - for (std::list::const_iterator it(lst.begin()), end(lst.end()); - it != end; ++it) - _parse_object_definitions(it->path()); +void parser::_store_into_list(object_ptr obj) { + _lst_objects[obj->type()].push_back(obj); } /** - * Parse the global configuration file. + * Store object into the map. * - * @param[in] path The configuration path. + * @param[in] obj The object to store. */ -void parser::_parse_global_configuration(std::string const& path) { - config_logger->info("Reading main configuration file '{}'.", path); - - std::ifstream stream(path.c_str(), std::ios::binary); - if (!stream.is_open()) - throw engine_error() << "Parsing of global " - "configuration failed: Can't open file '" - << path << "'"; - - _config->cfg_main(path); - - _current_line = 0; - _current_path = path; - - std::string input; - while (string::get_next_line(stream, input, _current_line)) { - char const* key; - char const* value; - if (!string::split(input, &key, &value, '=') || !_config->set(key, value)) - throw engine_error() << "Parsing of global " - "configuration failed in file '" - << path << "' on line " << _current_line - << ": Invalid line '" << input << "'"; - } +template +void parser::_store_into_map(object_ptr obj) { + std::shared_ptr real(std::static_pointer_cast(obj)); + map_object::iterator it(_map_objects[obj->type()].find((real.get()->*ptr)())); + if (it != _map_objects[obj->type()].end()) + throw engine_error() << "Parsing of " << obj->type_name() << " failed " + << _get_file_info(obj.get()) << ": " << obj->name() + << " alrealdy exists"; + _map_objects[obj->type()][(real.get()->*ptr)()] = real; } /** - * Parse the object definition file. + * Apply parse method into list. * - * @param[in] path The object definitions path. + * @param[in] lst The list to apply action. + * @param[in] pfunc The method to apply. */ -void parser::_parse_object_definitions(std::string const& path) { - config_logger->info("Processing object config file '{}'", path); - - std::ifstream stream(path, std::ios::binary); - if (!stream.is_open()) - throw engine_error() << "Parsing of object definition failed: " - << "Can't open file '" << path << "'"; - - _current_line = 0; - _current_path = path; - - bool parse_object = false; - object_ptr obj; - std::string input; - while (string::get_next_line(stream, input, _current_line)) { - // Multi-line. - while ('\\' == input[input.size() - 1]) { - input.resize(input.size() - 1); - std::string addendum; - if (!string::get_next_line(stream, addendum, _current_line)) - break; - input.append(addendum); - } - - // Check if is a valid object. - if (obj == nullptr) { - if (input.find("define") || !std::isspace(input[6])) - throw engine_error() - << "Parsing of object definition failed " - << "in file '" << _current_path << "' on line " << _current_line - << ": Unexpected start definition"; - string::trim_left(input.erase(0, 6)); - std::size_t last(input.size() - 1); - if (input.empty() || input[last] != '{') - throw engine_error() - << "Parsing of object definition failed " - << "in file '" << _current_path << "' on line " << _current_line - << ": Unexpected start definition"; - std::string const& type(string::trim_right(input.erase(last))); - obj = object::create(type); - if (obj == nullptr) - throw engine_error() - << "Parsing of object definition failed " - << "in file '" << _current_path << "' on line " << _current_line - << ": Unknown object type name '" << type << "'"; - parse_object = (_read_options & (1 << obj->type())); - _objects_info[obj.get()] = file_info(path, _current_line); - } - // Check if is the not the end of the current object. - else if (input != "}") { - if (parse_object) { - if (!obj->parse(input)) - throw engine_error() - << "Parsing of object definition " - << "failed in file '" << _current_path << "' on line " - << _current_line << ": Invalid line '" << input << "'"; - } - } - // End of the current object. - else { - if (parse_object) { - if (!obj->name().empty()) - _add_template(obj); - if (obj->should_register()) - _add_object(obj); - } - obj.reset(); - } - } +void parser::_apply(std::list const& lst, + void (parser::*pfunc)(std::string const&)) { + for (std::list::const_iterator it(lst.begin()), end(lst.end()); + it != end; ++it) + (this->*pfunc)(*it); } /** - * Parse the resource file. + * Apply the host extended info. * - * @param[in] path The resource file path. + * @warning This function is for compatibility and has very + * poor performance. Didn't use extended info. If you + * want to use the generic template system. */ -void parser::_parse_resource_file(std::string const& path) { - config_logger->info("Reading resource file '{}'", path); +void parser::_apply_hostextinfo() { + map_object& gl_hosts(_map_objects[object::host]); + list_object const& hostextinfos(_lst_objects[object::hostextinfo]); + for (list_object::const_iterator it(hostextinfos.begin()), + end(hostextinfos.end()); + it != end; ++it) { + // Get the current hostextinfo to check. + hostextinfo_ptr obj(std::static_pointer_cast(*it)); - std::ifstream stream(path.c_str(), std::ios::binary); - if (!stream.is_open()) - throw engine_error() << "Parsing of resource file failed: " - << "can't open file '" << path << "'"; + list_host hosts; + _get_objects_by_list_name(obj->hosts(), gl_hosts, hosts); + _get_hosts_by_hostgroups_name(obj->hostgroups(), hosts); - _current_line = 0; - _current_path = path; - - std::string input; - while (string::get_next_line(stream, input, _current_line)) { - try { - std::string key; - std::string value; - if (!string::split(input, key, value, '=')) - throw engine_error() << "Parsing of resource file '" << _current_path - << "' failed on line " << _current_line - << ": Invalid line '" << input << "'"; - _config->user(key, value); - } catch (std::exception const& e) { - (void)e; - throw engine_error() << "Parsing of resource file '" << _current_path - << "' failed on line " << _current_line - << ": Invalid line '" << input << "'"; - } + for (list_host::iterator it(hosts.begin()), end(hosts.end()); it != end; + ++it) + it->merge(*obj); } } /** - * Resolve template for register objects. + * Apply the service extended info. + * + * @warning This function is for compatibility and has very + * poor performance. Didn't use extended info. If you + * want to use the generic template system. */ -void parser::_resolve_template() { - for (map_object& templates : _templates) { - for (map_object::iterator it = templates.begin(), end = templates.end(); - it != end; ++it) - it->second->resolve_template(templates); - } +void parser::_apply_serviceextinfo() { + map_object& gl_hosts(_map_objects[object::host]); + list_object& gl_services(_lst_objects[object::service]); + list_object const& serviceextinfos(_lst_objects[object::serviceextinfo]); + for (list_object::const_iterator it(serviceextinfos.begin()), + end(serviceextinfos.end()); + it != end; ++it) { + // Get the current serviceextinfo to check. + serviceextinfo_ptr obj(std::static_pointer_cast(*it)); - for (unsigned int i = 0; i < _lst_objects.size(); ++i) { - map_object& templates = _templates[i]; - for (list_object::iterator it = _lst_objects[i].begin(), - end = _lst_objects[i].end(); - it != end; ++it) { - (*it)->resolve_template(templates); - try { - (*it)->check_validity(); - } catch (std::exception const& e) { - throw engine_error() << "Configuration parsing failed " - << _get_file_info(it->get()) << ": " << e.what(); - } - } - } + list_host hosts; + _get_objects_by_list_name(obj->hosts(), gl_hosts, hosts); + _get_hosts_by_hostgroups_name(obj->hostgroups(), hosts); - for (unsigned int i = 0; i < _map_objects.size(); ++i) { - map_object& templates = _templates[i]; - for (map_object::iterator it = _map_objects[i].begin(), - end = _map_objects[i].end(); + for (list_object::iterator it(gl_services.begin()), end(gl_services.end()); it != end; ++it) { - it->second->resolve_template(templates); - try { - it->second->check_validity(); - } catch (std::exception const& e) { - throw engine_error() - << "Configuration parsing failed " - << _get_file_info(it->second.get()) << ": " << e.what(); + service_ptr svc(std::static_pointer_cast(*it)); + if (svc->service_description() != obj->service_description()) + continue; + + list_host svc_hosts; + _get_objects_by_list_name(svc->hosts(), gl_hosts, svc_hosts); + _get_hosts_by_hostgroups_name(svc->hostgroups(), svc_hosts); + + bool found(false); + for (list_host::const_iterator it_host(hosts.begin()), + end_host(hosts.end()); + !found && it_host != end_host; ++it_host) { + for (list_host::const_iterator it_svc_host(svc_hosts.begin()), + end_svc_host(svc_hosts.end()); + it_svc_host != end_svc_host; ++it_svc_host) { + if (it_host->host_name() == it_svc_host->host_name()) { + svc->merge(*obj); + found = true; + } + } } } } } - -/** - * Store object into the list. - * - * @param[in] obj The object to store. - */ -void parser::_store_into_list(object_ptr obj) { - _lst_objects[obj->type()].push_back(obj); -} - -/** - * Store object into the map. - * - * @param[in] obj The object to store. - */ -template -void parser::_store_into_map(object_ptr obj) { - std::shared_ptr real(std::static_pointer_cast(obj)); - map_object::iterator it(_map_objects[obj->type()].find((real.get()->*ptr)())); - if (it != _map_objects[obj->type()].end()) - throw engine_error() << "Parsing of " << obj->type_name() << " failed " - << _get_file_info(obj.get()) << ": " << obj->name() - << " alrealdy exists"; - _map_objects[obj->type()][(real.get()->*ptr)()] = real; -} From e0ea2a7e4ba65d219eab8c70ea9a0f739501658f Mon Sep 17 00:00:00 2001 From: David Boucher Date: Thu, 23 May 2024 12:32:09 +0200 Subject: [PATCH 07/21] fix(engine/conf): issue with debug_level unsigned --- .../inc/com/centreon/engine/configuration/state.hh | 6 +++--- engine/src/configuration/state.cc | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/engine/inc/com/centreon/engine/configuration/state.hh b/engine/inc/com/centreon/engine/configuration/state.hh index 0a7f9647822..80ce73c9fb4 100644 --- a/engine/inc/com/centreon/engine/configuration/state.hh +++ b/engine/inc/com/centreon/engine/configuration/state.hh @@ -180,8 +180,8 @@ class state { void date_format(date_type value); std::string const& debug_file() const noexcept; void debug_file(std::string const& value); - uint64_t debug_level() const noexcept; - void debug_level(uint64_t value); + int64_t debug_level() const noexcept; + void debug_level(int64_t value); unsigned int debug_verbosity() const noexcept; void debug_verbosity(unsigned int value); bool enable_environment_macros() const noexcept; @@ -530,7 +530,7 @@ class state { set_contact _contacts; date_type _date_format; std::string _debug_file; - uint64_t _debug_level; + int64_t _debug_level; unsigned int _debug_verbosity; bool _enable_environment_macros; bool _enable_event_handlers; diff --git a/engine/src/configuration/state.cc b/engine/src/configuration/state.cc index 51a42f064ff..e2643eaa838 100644 --- a/engine/src/configuration/state.cc +++ b/engine/src/configuration/state.cc @@ -159,7 +159,7 @@ void state::_init_setter() { SETTER(std::string const&, _set_daemon_dumps_core, "daemon_dumps_core"); SETTER(std::string const&, _set_date_format, "date_format"); SETTER(std::string const&, debug_file, "debug_file"); - SETTER(uint64_t, debug_level, "debug_level"); + SETTER(int64_t, debug_level, "debug_level"); SETTER(unsigned int, debug_verbosity, "debug_verbosity"); SETTER(std::string const&, _set_downtime_file, "downtime_file"); SETTER(std::string const&, _set_enable_embedded_perl, "enable_embedded_perl"); @@ -351,7 +351,7 @@ static int const default_command_check_interval(-1); static std::string const default_command_file(DEFAULT_COMMAND_FILE); static state::date_type const default_date_format(state::us); static std::string const default_debug_file(DEFAULT_DEBUG_FILE); -static uint64_t const default_debug_level(0); +static int64_t const default_debug_level(0); static unsigned int const default_debug_verbosity(1); static bool const default_enable_environment_macros(false); static bool const default_enable_event_handlers(true); @@ -1720,7 +1720,7 @@ void state::debug_file(std::string const& value) { * * @return The debug_level value. */ -uint64_t state::debug_level() const noexcept { +int64_t state::debug_level() const noexcept { return _debug_level; } @@ -1729,9 +1729,9 @@ uint64_t state::debug_level() const noexcept { * * @param[in] value The new debug_level value. */ -void state::debug_level(uint64_t value) { - if (value == std::numeric_limits::max()) - _debug_level = static_cast(logging::all); +void state::debug_level(int64_t value) { + if (value == -1) + _debug_level = static_cast(logging::all); else _debug_level = value; } From f5050722a4c6d72173cdfa4f3ead8f3004c17488 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Thu, 23 May 2024 13:59:31 +0200 Subject: [PATCH 08/21] fix(common/log_v2): log with grpc can crash --- common/log_v2/log_v2.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/common/log_v2/log_v2.cc b/common/log_v2/log_v2.cc index 94e857bc736..264b6e78573 100644 --- a/common/log_v2/log_v2.cc +++ b/common/log_v2/log_v2.cc @@ -146,6 +146,11 @@ log_v2::log_v2(std::string name) : _log_name{std::move(name)} { create_loggers(config::logger_type::LOGGER_STDOUT); } +log_v2::~log_v2() noexcept { + /* When log_v2 is stopped, grpc mustn't log anymore. */ + gpr_set_log_function(nullptr); +} + /** * @brief Destructor. */ From d427ff046e18f8f47df42379f056f312828a9969 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Thu, 23 May 2024 16:04:48 +0200 Subject: [PATCH 09/21] fix(engine/string): still some use of string lib in configuration fixed --- .../centreon/engine/configuration/object.hh | 75 ++++++++++++------- 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/engine/inc/com/centreon/engine/configuration/object.hh b/engine/inc/com/centreon/engine/configuration/object.hh index 4f098d2e457..0b8ddaef420 100644 --- a/engine/inc/com/centreon/engine/configuration/object.hh +++ b/engine/inc/com/centreon/engine/configuration/object.hh @@ -1,21 +1,21 @@ -/* -** Copyright 2011-2015 Merethis -** Copyright 2016-2022 Centreon -** -** 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 -*/ +/** + * Copyright 2011-2015 Merethis + * Copyright 2016-2022 Centreon + * + * 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_OBJECT_HH #define CCE_CONFIGURATION_OBJECT_HH @@ -60,7 +60,7 @@ class object { virtual void check_validity() const = 0; static std::shared_ptr create(std::string const& type_name); virtual void merge(object const& obj) = 0; - std::string const& name() const noexcept; + const std::string& name() const noexcept; virtual bool parse(char const* key, char const* value); virtual bool parse(std::string const& line); void resolve_template( @@ -72,24 +72,43 @@ class object { protected: struct setters { char const* name; - bool (*func)(object&, char const*); + bool (*func)(object&, const char*); }; template struct setter { - static bool generic(T& obj, char const* value) { + static bool generic(T& obj, const char* value) { U val(0); - if (!string::to(value, val)) - return (false); - return ((obj.*ptr)(val)); + if constexpr (std::is_same_v) { + if (absl::SimpleAtob(value, &val)) + return (obj.*ptr)(val); + else + return false; + } else if constexpr (std::is_integral::value) { + if (absl::SimpleAtoi(value, &val)) + return (obj.*ptr)(val); + else + return false; + } else if constexpr (std::is_same_v) { + if (absl::SimpleAtof(value, &val)) + return (obj.*ptr)(val); + else + return false; + } else if constexpr (std::is_same_v) { + if (absl::SimpleAtod(value, &val)) + return (obj.*ptr)(val); + else + return false; + } else { + static_assert(std::is_integral_v || std::is_floating_point_v || + std::is_same_v); + } } }; template struct setter { - static bool generic(T& obj, char const* value) { - return ((obj.*ptr)(value)); - } + static bool generic(T& obj, const char* value) { return (obj.*ptr)(value); } }; bool _set_name(std::string const& value); @@ -109,7 +128,7 @@ typedef std::list list_object; typedef std::unordered_map map_object; } // namespace configuration -} +} // namespace com::centreon::engine #define MRG_TAB(prop) \ do { \ From 37e38528db81d703b7a256e003be3b833eec4d66 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Thu, 23 May 2024 16:32:58 +0200 Subject: [PATCH 10/21] fix(engine): extinfo classes are no more used. --- .../centreon/engine/configuration/object.hh | 4 +- .../centreon/engine/configuration/parser.hh | 38 +++++---- engine/src/configuration/parser.cc | 81 +------------------ 3 files changed, 23 insertions(+), 100 deletions(-) diff --git a/engine/inc/com/centreon/engine/configuration/object.hh b/engine/inc/com/centreon/engine/configuration/object.hh index 0b8ddaef420..e43e954c060 100644 --- a/engine/inc/com/centreon/engine/configuration/object.hh +++ b/engine/inc/com/centreon/engine/configuration/object.hh @@ -38,12 +38,12 @@ class object { host = 4, hostdependency = 5, hostescalation = 6, - hostextinfo = 7, + hostextinfo = 7, // no more used. hostgroup = 8, service = 9, servicedependency = 10, serviceescalation = 11, - serviceextinfo = 12, + serviceextinfo = 12, // no more used. servicegroup = 13, timeperiod = 14, anomalydetection = 15, diff --git a/engine/inc/com/centreon/engine/configuration/parser.hh b/engine/inc/com/centreon/engine/configuration/parser.hh index 704043179fa..923147c36ca 100644 --- a/engine/inc/com/centreon/engine/configuration/parser.hh +++ b/engine/inc/com/centreon/engine/configuration/parser.hh @@ -1,21 +1,21 @@ -/* -** Copyright 2011-2013,2017 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,2017 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_PARSER_HH #define CCE_CONFIGURATION_PARSER_HH @@ -48,13 +48,11 @@ class parser { read_host = (1 << 4), read_hostdependency = (1 << 5), read_hostescalation = (1 << 6), - read_hostextinfo = (1 << 7), read_hostgroup = (1 << 8), read_hostgroupescalation = (1 << 9), read_service = (1 << 10), read_servicedependency = (1 << 11), read_serviceescalation = (1 << 12), - read_serviceextinfo = (1 << 13), read_servicegroup = (1 << 14), read_timeperiod = (1 << 15), read_all = (~0) diff --git a/engine/src/configuration/parser.cc b/engine/src/configuration/parser.cc index df38d695c9b..896adc35a9d 100644 --- a/engine/src/configuration/parser.cc +++ b/engine/src/configuration/parser.cc @@ -81,7 +81,7 @@ static bool get_next_line(std::ifstream& stream, * (use to skip some object type). */ parser::parser(unsigned int read_options) - : _config(NULL), _read_options(read_options) {} + : _config(nullptr), _read_options(read_options) {} /** * Parse the object definition file. @@ -511,9 +511,8 @@ void parser::_store_into_map(object_ptr obj) { std::shared_ptr real(std::static_pointer_cast(obj)); map_object::iterator it(_map_objects[obj->type()].find((real.get()->*ptr)())); if (it != _map_objects[obj->type()].end()) - throw engine_error() << "Parsing of " << obj->type_name() << " failed " - << _get_file_info(obj.get()) << ": " << obj->name() - << " alrealdy exists"; + throw error("Parsing of {} failed {}: {} already exists", obj->type_name(), + _get_file_info(obj.get()), obj->name()); _map_objects[obj->type()][(real.get()->*ptr)()] = real; } @@ -529,77 +528,3 @@ void parser::_apply(std::list const& lst, it != end; ++it) (this->*pfunc)(*it); } - -/** - * Apply the host extended info. - * - * @warning This function is for compatibility and has very - * poor performance. Didn't use extended info. If you - * want to use the generic template system. - */ -void parser::_apply_hostextinfo() { - map_object& gl_hosts(_map_objects[object::host]); - list_object const& hostextinfos(_lst_objects[object::hostextinfo]); - for (list_object::const_iterator it(hostextinfos.begin()), - end(hostextinfos.end()); - it != end; ++it) { - // Get the current hostextinfo to check. - hostextinfo_ptr obj(std::static_pointer_cast(*it)); - - list_host hosts; - _get_objects_by_list_name(obj->hosts(), gl_hosts, hosts); - _get_hosts_by_hostgroups_name(obj->hostgroups(), hosts); - - for (list_host::iterator it(hosts.begin()), end(hosts.end()); it != end; - ++it) - it->merge(*obj); - } -} - -/** - * Apply the service extended info. - * - * @warning This function is for compatibility and has very - * poor performance. Didn't use extended info. If you - * want to use the generic template system. - */ -void parser::_apply_serviceextinfo() { - map_object& gl_hosts(_map_objects[object::host]); - list_object& gl_services(_lst_objects[object::service]); - list_object const& serviceextinfos(_lst_objects[object::serviceextinfo]); - for (list_object::const_iterator it(serviceextinfos.begin()), - end(serviceextinfos.end()); - it != end; ++it) { - // Get the current serviceextinfo to check. - serviceextinfo_ptr obj(std::static_pointer_cast(*it)); - - list_host hosts; - _get_objects_by_list_name(obj->hosts(), gl_hosts, hosts); - _get_hosts_by_hostgroups_name(obj->hostgroups(), hosts); - - for (list_object::iterator it(gl_services.begin()), end(gl_services.end()); - it != end; ++it) { - service_ptr svc(std::static_pointer_cast(*it)); - if (svc->service_description() != obj->service_description()) - continue; - - list_host svc_hosts; - _get_objects_by_list_name(svc->hosts(), gl_hosts, svc_hosts); - _get_hosts_by_hostgroups_name(svc->hostgroups(), svc_hosts); - - bool found(false); - for (list_host::const_iterator it_host(hosts.begin()), - end_host(hosts.end()); - !found && it_host != end_host; ++it_host) { - for (list_host::const_iterator it_svc_host(svc_hosts.begin()), - end_svc_host(svc_hosts.end()); - it_svc_host != end_svc_host; ++it_svc_host) { - if (it_host->host_name() == it_svc_host->host_name()) { - svc->merge(*obj); - found = true; - } - } - } - } - } -} From 87ff6d61d74c31e33a17ccee8cf5bde2db650cb9 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Thu, 23 May 2024 19:18:47 +0200 Subject: [PATCH 11/21] enh(engine/conf): customvariable has its conf object now --- .../engine/configuration/anomalydetection.hh | 9 +- .../centreon/engine/configuration/contact.hh | 2 +- .../com/centreon/engine/configuration/host.hh | 5 +- .../centreon/engine/configuration/service.hh | 53 +++++----- engine/src/anomalydetection.cc | 2 - engine/src/configuration/anomalydetection.cc | 6 +- engine/src/configuration/applier/host.cc | 96 ++++++++++--------- engine/src/configuration/host.cc | 8 +- engine/src/configuration/service.cc | 6 +- 9 files changed, 94 insertions(+), 93 deletions(-) diff --git a/engine/inc/com/centreon/engine/configuration/anomalydetection.hh b/engine/inc/com/centreon/engine/configuration/anomalydetection.hh index c797a20b8b6..68fb0d74495 100644 --- a/engine/inc/com/centreon/engine/configuration/anomalydetection.hh +++ b/engine/inc/com/centreon/engine/configuration/anomalydetection.hh @@ -20,9 +20,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 { @@ -68,8 +68,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; @@ -189,7 +190,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 f6d763f9168..fc2651fb4b1 100644 --- a/engine/inc/com/centreon/engine/configuration/contact.hh +++ b/engine/inc/com/centreon/engine/configuration/contact.hh @@ -21,9 +21,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/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 d72dc1fb52d..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& mut_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/src/anomalydetection.cc b/engine/src/anomalydetection.cc index 3d73cf162d5..ce878ad8c2d 100644 --- a/engine/src/anomalydetection.cc +++ b/engine/src/anomalydetection.cc @@ -1099,8 +1099,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/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/host.cc b/engine/src/configuration/applier/host.cc index 149cb78661e..6577915df86 100644 --- a/engine/src/configuration/applier/host.cc +++ b/engine/src/configuration/applier/host.cc @@ -171,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. * @@ -543,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/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; } From d3f299c29c0002c87679fb6db55c0e0b2a7930a9 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Fri, 24 May 2024 11:51:41 +0200 Subject: [PATCH 12/21] fix(engine): on configuration independency --- .../centreon/engine/configuration/logging.hh | 42 +++++++++++++++++++ engine/src/configuration/anomalydetection.cc | 4 -- engine/src/configuration/command.cc | 15 +++---- engine/src/configuration/connector.cc | 18 ++++---- engine/src/configuration/host.cc | 3 -- engine/src/configuration/hostdependency.cc | 2 - engine/src/configuration/hostgroup.cc | 29 +++---------- engine/src/configuration/parser.cc | 1 - engine/src/configuration/service.cc | 2 - engine/src/configuration/servicegroup.cc | 13 +++--- engine/src/configuration/severity.cc | 13 +++--- engine/src/configuration/state.cc | 17 +++----- engine/src/configuration/tag.cc | 9 ++-- engine/src/configuration/timeperiod.cc | 3 -- 14 files changed, 86 insertions(+), 85 deletions(-) create mode 100644 engine/inc/com/centreon/engine/configuration/logging.hh diff --git a/engine/inc/com/centreon/engine/configuration/logging.hh b/engine/inc/com/centreon/engine/configuration/logging.hh new file mode 100644 index 00000000000..f67a253b4cb --- /dev/null +++ b/engine/inc/com/centreon/engine/configuration/logging.hh @@ -0,0 +1,42 @@ +/** + * Copyright 2024 Centreon + * + * 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 CCEC_LOGGING_HH +#define CCEC_LOGGING_HH + +/** + * This namespace is based on the com/centreon/engine/logging/logger.hh file. + * + * It contains the minimum needed by the configuration module about logging so + * that the legacy logger can still be configured. + * + * FIXME: We hope this legacy logger can be removed very soon. + */ +namespace com::centreon::engine::configuration::logging { +enum type_value { + log_all = 2096895ull, + dbg_all = 4095ull << 32, + all = log_all | dbg_all, +}; + +enum verbosity_level { basic = 0u, more = 1u, most = 2u }; + +} // namespace com::centreon::engine::configuration::logging + +#endif /* !CCEC_LOGGING_HH */ diff --git a/engine/src/configuration/anomalydetection.cc b/engine/src/configuration/anomalydetection.cc index cd59aadc1ba..f1d96c95305 100644 --- a/engine/src/configuration/anomalydetection.cc +++ b/engine/src/configuration/anomalydetection.cc @@ -21,11 +21,7 @@ #include #include #include -#include "com/centreon/engine/customvariable.hh" -#include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/host.hh" -#include "com/centreon/engine/logging/logger.hh" #include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; diff --git a/engine/src/configuration/command.cc b/engine/src/configuration/command.cc index 6258d974196..f12fb86c2a4 100644 --- a/engine/src/configuration/command.cc +++ b/engine/src/configuration/command.cc @@ -18,10 +18,11 @@ * */ #include "com/centreon/engine/configuration/command.hh" -#include "com/centreon/engine/exceptions/error.hh" +#include "com/centreon/exceptions/error.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; +using com::centreon::exceptions::error; #define SETTER(type, method) \ &object::setter::generic @@ -114,11 +115,10 @@ bool command::operator<(command const& right) const throw() { */ void command::check_validity() const { if (_command_name.empty()) - throw(engine_error() << "Command has no name (property 'command_name')"); + throw error("Command has no name (property 'command_name')"); if (_command_line.empty()) - throw(engine_error() << "Command '" << _command_name - << "' has no command line (property 'command_line')"); - return; + throw error("Command '{}' has no command line (property 'command_line')", + _command_name); } /** @@ -127,7 +127,7 @@ void command::check_validity() const { * @return The command name. */ command::key_type const& command::key() const throw() { - return (_command_name); + return _command_name; } /** @@ -137,7 +137,8 @@ command::key_type const& command::key() const throw() { */ void command::merge(object const& obj) { if (obj.type() != _type) - throw(engine_error() << "Cannot merge command with '" << obj.type() << "'"); + throw error("Cannot merge command with type '{}'", + static_cast(obj.type())); command const& tmpl(static_cast(obj)); MRG_DEFAULT(_command_line); diff --git a/engine/src/configuration/connector.cc b/engine/src/configuration/connector.cc index 1e3b83d920f..9d2c9ed3fa9 100644 --- a/engine/src/configuration/connector.cc +++ b/engine/src/configuration/connector.cc @@ -18,11 +18,11 @@ * */ #include "com/centreon/engine/configuration/connector.hh" -#include "com/centreon/engine/checks/checker.hh" -#include "com/centreon/engine/exceptions/error.hh" +#include "com/centreon/exceptions/error.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; +using com::centreon::exceptions::error; #define SETTER(type, method) \ &object::setter::generic @@ -114,13 +114,11 @@ bool connector::operator<(connector const& right) const throw() { */ void connector::check_validity() const { if (_connector_name.empty()) - throw( - engine_error() << "Connector has no name (property 'connector_name')"); + throw error("Connector has no name (property 'connector_name')"); if (_connector_line.empty()) - throw( - engine_error() << "Connector '" << _connector_name - << "' has no command line (property 'connector_line')"); - return; + throw error( + "Connector '{}' has no command line (property 'connector_line')", + _connector_name); } /** @@ -139,8 +137,8 @@ connector::key_type const& connector::key() const throw() { */ void connector::merge(object const& obj) { if (obj.type() != _type) - throw(engine_error() << "Cannot merge connector with '" << obj.type() - << "'"); + throw error("Cannot merge connector with type '{}'", + static_cast(obj.type())); connector const& tmpl(static_cast(obj)); MRG_DEFAULT(_connector_line); diff --git a/engine/src/configuration/host.cc b/engine/src/configuration/host.cc index ed7864a5547..a958a6e7c8c 100644 --- a/engine/src/configuration/host.cc +++ b/engine/src/configuration/host.cc @@ -22,16 +22,13 @@ #include "absl/strings/numbers.h" #include "absl/strings/str_split.h" #include "absl/strings/string_view.h" -#include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/host.hh" #include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; extern int config_errors; using namespace com::centreon::engine::configuration; -using com::centreon::common::log_v2::log_v2; using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) &object::setter::generic diff --git a/engine/src/configuration/hostdependency.cc b/engine/src/configuration/hostdependency.cc index dd906735da4..e1ab0de267d 100644 --- a/engine/src/configuration/hostdependency.cc +++ b/engine/src/configuration/hostdependency.cc @@ -20,7 +20,6 @@ #include "com/centreon/engine/configuration/hostdependency.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/logging/logger.hh" #include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; @@ -28,7 +27,6 @@ extern int config_errors; using namespace com::centreon; using namespace com::centreon::engine::configuration; -using namespace com::centreon::engine::logging; using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ diff --git a/engine/src/configuration/hostgroup.cc b/engine/src/configuration/hostgroup.cc index 0e09a0b0c24..8c1ef96cd5c 100644 --- a/engine/src/configuration/hostgroup.cc +++ b/engine/src/configuration/hostgroup.cc @@ -18,13 +18,12 @@ */ #include "com/centreon/engine/configuration/hostgroup.hh" -#include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/logging/logger.hh" +#include "com/centreon/exceptions/error.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; -using namespace com::centreon::engine::logging; +using com::centreon::exceptions::error; #define SETTER(type, method) \ &object::setter::generic @@ -91,57 +90,41 @@ hostgroup& hostgroup::operator=(hostgroup const& right) { */ bool hostgroup::operator==(hostgroup const& right) const throw() { if (!object::operator==(right)) { - engine_logger(dbg_config, more) - << "configuration::hostgroup::equality => object don't match"; config_logger->debug( "configuration::hostgroup::equality => object don't match"); return false; } if (_action_url != right._action_url) { - engine_logger(dbg_config, more) - << "configuration::hostgroup::equality => action url don't match"; config_logger->debug( "configuration::hostgroup::equality => action url don't match"); return false; } if (_alias != right._alias) { - engine_logger(dbg_config, more) - << "configuration::hostgroup::equality => alias don't match"; config_logger->debug( "configuration::hostgroup::equality => alias don't match"); return false; } if (_hostgroup_id != right._hostgroup_id) { - engine_logger(dbg_config, more) - << "configuration::hostgroup::equality => hostgroup id don't match"; config_logger->debug( "configuration::hostgroup::equality => hostgroup id don't match"); return false; } if (_hostgroup_name != right._hostgroup_name) { - engine_logger(dbg_config, more) - << "configuration::hostgroup::equality => hostgroup name don't match"; config_logger->debug( "configuration::hostgroup::equality => hostgroup name don't match"); return false; } if (_members != right._members) { - engine_logger(dbg_config, more) - << "configuration::hostgroup::equality => members don't match"; config_logger->debug( "configuration::hostgroup::equality => members don't match"); return false; } if (_notes != right._notes) { - engine_logger(dbg_config, more) - << "configuration::hostgroup::equality => notes don't match"; config_logger->debug( "configuration::hostgroup::equality => notes don't match"); return false; } if (_notes_url != right._notes_url) { - engine_logger(dbg_config, more) - << "configuration::hostgroup::equality => notes url don't match"; config_logger->debug( "configuration::hostgroup::equality => notes url don't match"); return false; @@ -192,9 +175,7 @@ bool hostgroup::operator<(hostgroup const& right) const throw() { */ void hostgroup::check_validity() const { if (_hostgroup_name.empty()) - throw(engine_error() << "Host group has no name " - "(property 'hostgroup_name')"); - return; + throw error("Host group has no name (property 'hostgroup_name')"); } /** @@ -213,8 +194,8 @@ hostgroup::key_type const& hostgroup::key() const throw() { */ void hostgroup::merge(object const& obj) { if (obj.type() != _type) - throw(engine_error() << "Cannot merge host group with '" << obj.type() - << "'"); + throw error("Cannot merge host group with type '{}'", + static_cast(obj.type())); hostgroup const& tmpl(static_cast(obj)); MRG_DEFAULT(_action_url); MRG_DEFAULT(_alias); diff --git a/engine/src/configuration/parser.cc b/engine/src/configuration/parser.cc index 896adc35a9d..64ec8772cdd 100644 --- a/engine/src/configuration/parser.cc +++ b/engine/src/configuration/parser.cc @@ -18,7 +18,6 @@ */ #include "com/centreon/engine/configuration/parser.hh" -#include "com/centreon/engine/exceptions/error.hh" #include "com/centreon/engine/globals.hh" #include "com/centreon/exceptions/error.hh" #include "com/centreon/exceptions/msg_fmt.hh" diff --git a/engine/src/configuration/service.cc b/engine/src/configuration/service.cc index 769d7025e00..618305b6863 100644 --- a/engine/src/configuration/service.cc +++ b/engine/src/configuration/service.cc @@ -21,9 +21,7 @@ #include #include #include -#include "com/centreon/engine/customvariable.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/host.hh" #include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; diff --git a/engine/src/configuration/servicegroup.cc b/engine/src/configuration/servicegroup.cc index d580b921d45..dcd621c59fb 100644 --- a/engine/src/configuration/servicegroup.cc +++ b/engine/src/configuration/servicegroup.cc @@ -17,10 +17,11 @@ * */ #include "com/centreon/engine/configuration/servicegroup.hh" -#include "com/centreon/engine/exceptions/error.hh" +#include "com/centreon/exceptions/error.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; +using com::centreon::exceptions::error; #define SETTER(type, method) \ &object::setter::generic @@ -142,9 +143,9 @@ bool servicegroup::operator<(servicegroup const& right) const throw() { */ void servicegroup::check_validity() const { if (_servicegroup_name.empty()) - throw(engine_error() << "Service group has no name " - "(property 'servicegroup_name')"); - return; + throw error( + "Service group has no name " + "(property 'servicegroup_name')"); } /** @@ -163,8 +164,8 @@ servicegroup::key_type const& servicegroup::key() const throw() { */ void servicegroup::merge(object const& obj) { if (obj.type() != _type) - throw(engine_error() << "Cannot merge service group with '" << obj.type() - << "'"); + throw error("Cannot merge service group with type '{}'", + static_cast(obj.type())); servicegroup const& tmpl(static_cast(obj)); MRG_DEFAULT(_action_url); diff --git a/engine/src/configuration/severity.cc b/engine/src/configuration/severity.cc index e31a0eb5fce..b1db1e963ac 100644 --- a/engine/src/configuration/severity.cc +++ b/engine/src/configuration/severity.cc @@ -18,11 +18,12 @@ */ #include "com/centreon/engine/configuration/severity.hh" -#include "com/centreon/engine/exceptions/error.hh" +#include "com/centreon/exceptions/error.hh" using namespace com::centreon; using namespace com::centreon::engine; using namespace com::centreon::engine::configuration; +using com::centreon::exceptions::error; #define SETTER(type, method) \ &object::setter::generic @@ -131,15 +132,13 @@ bool severity::operator<(const severity& other) const noexcept { */ void severity::check_validity() const { if (_severity_name.empty()) - throw engine_error() << "Severity has no name (property 'severity_name')"; + throw error("Severity has no name (property 'severity_name')"); if (_key.first == 0) - throw engine_error() - << "Severity id must not be less than 1 (property 'id')"; + throw error("Severity id must not be less than 1 (property 'id')"); if (_level == 0) - throw engine_error() - << "Severity level must not be less than 1 (property 'level')"; + throw error("Severity level must not be less than 1 (property 'level')"); if (_key.second == severity::none) - throw engine_error() << "Severity type must be one of 'service' or 'host'"; + throw error("Severity type must be one of 'service' or 'host'"); } /** diff --git a/engine/src/configuration/state.cc b/engine/src/configuration/state.cc index e2643eaa838..33bae55a104 100644 --- a/engine/src/configuration/state.cc +++ b/engine/src/configuration/state.cc @@ -18,9 +18,8 @@ */ #include "com/centreon/engine/configuration/state.hh" #include "com/centreon/common/rapidjson_helper.hh" -#include "com/centreon/engine/broker.hh" +#include "com/centreon/engine/configuration/logging.hh" #include "com/centreon/engine/globals.hh" -#include "com/centreon/engine/logging/logger.hh" #include "com/centreon/io/file_entry.hh" #include "compatibility/locations.h" @@ -66,7 +65,7 @@ struct setter : public setter_base { std::is_same_v); } } catch (const std::exception& e) { - engine_logger(logging::log_config_error, logging::basic) << e.what(); + config_logger->error(e.what()); } return true; } @@ -4438,8 +4437,6 @@ void state::_set_date_format(std::string const& value) { */ void state::_set_downtime_file(std::string const& value) { (void)value; - engine_logger(logging::log_config_warning, logging::basic) - << "Warning: downtime_file variable ignored"; config_logger->warn("Warning: downtime_file variable ignored"); ++config_warnings; } @@ -4451,8 +4448,6 @@ void state::_set_downtime_file(std::string const& value) { */ void state::_set_enable_embedded_perl(std::string const& value) { (void)value; - engine_logger(logging::log_config_warning, logging::basic) - << "Warning: enable_embedded_perl variable ignored"; config_logger->warn("Warning: enable_embedded_perl variable ignored"); ++config_warnings; } @@ -4464,8 +4459,6 @@ void state::_set_enable_embedded_perl(std::string const& value) { */ void state::_set_enable_failure_prediction(std::string const& value) { (void)value; - engine_logger(logging::log_config_warning, logging::basic) - << "Warning: enable_failure_prediction variable ignored"; config_logger->warn("Warning: enable_failure_prediction variable ignored"); ++config_warnings; return; @@ -4480,9 +4473,9 @@ void state::_set_event_broker_options(std::string const& value) { if (value != "-1") detail::setter("") .apply_from_cfg(*this, value.c_str()); - else { - _event_broker_options = BROKER_EVERYTHING; - } + else + _event_broker_options = + static_cast(-1); // BROKER_EVERYTHING; } /** diff --git a/engine/src/configuration/tag.cc b/engine/src/configuration/tag.cc index b1698531410..6fc8ed542f3 100644 --- a/engine/src/configuration/tag.cc +++ b/engine/src/configuration/tag.cc @@ -18,11 +18,12 @@ */ #include "com/centreon/engine/configuration/tag.hh" -#include "com/centreon/engine/exceptions/error.hh" +#include "com/centreon/exceptions/error.hh" using namespace com::centreon; using namespace com::centreon::engine; using namespace com::centreon::engine::configuration; +using com::centreon::exceptions::error; #define SETTER(type, method) &object::setter::generic @@ -112,11 +113,11 @@ bool tag::operator<(const tag& other) const noexcept { */ void tag::check_validity() const { if (_tag_name.empty()) - throw engine_error() << "Tag has no name (property 'tag_name')"; + throw error("Tag has no name (property 'tag_name')"); if (_key.first == 0) - throw engine_error() << "Tag id must not be less than 1 (property 'id')"; + throw error("Tag id must not be less than 1 (property 'id')"); if (_key.second == static_cast(-1)) - throw engine_error() << "Tag type must be defined (property 'type')"; + throw error("Tag type must be defined (property 'type')"); } /** diff --git a/engine/src/configuration/timeperiod.cc b/engine/src/configuration/timeperiod.cc index fa17aee4d7d..552c563bd53 100644 --- a/engine/src/configuration/timeperiod.cc +++ b/engine/src/configuration/timeperiod.cc @@ -18,9 +18,6 @@ * */ #include "com/centreon/engine/configuration/timeperiod.hh" - -#include "com/centreon/engine/common.hh" -#include "com/centreon/engine/timerange.hh" #include "com/centreon/exceptions/msg_fmt.hh" using namespace com::centreon; From e0ab74ee77726a143b682f3f556ccbb3934d921b Mon Sep 17 00:00:00 2001 From: David Boucher Date: Mon, 27 May 2024 16:04:12 +0200 Subject: [PATCH 13/21] enh(engine): on the road of independency --- .../engine/configuration/anomalydetection.hh | 2 +- .../centreon/engine/configuration/command.hh | 2 +- .../engine/configuration/connector.hh | 2 +- .../centreon/engine/configuration/contact.hh | 2 +- .../engine/configuration/contactgroup.hh | 2 +- .../com/centreon/engine/configuration/host.hh | 2 +- .../engine/configuration/hostdependency.hh | 4 +- .../engine/configuration/hostescalation.hh | 4 +- .../engine/configuration/hostgroup.hh | 4 +- .../centreon/engine/configuration/object.hh | 13 +- .../centreon/engine/configuration/parser.hh | 5 +- .../centreon/engine/configuration/service.hh | 8 +- .../engine/configuration/servicedependency.hh | 4 +- .../engine/configuration/serviceescalation.hh | 2 +- .../engine/configuration/servicegroup.hh | 4 +- .../centreon/engine/configuration/severity.hh | 4 +- .../centreon/engine/configuration/state.hh | 12 ++ .../com/centreon/engine/configuration/tag.hh | 38 ++-- .../engine/configuration/timeperiod.hh | 2 +- engine/src/configuration/anomalydetection.cc | 4 +- engine/src/configuration/command.cc | 2 +- engine/src/configuration/connector.cc | 4 +- engine/src/configuration/contact.cc | 3 +- engine/src/configuration/contactgroup.cc | 4 +- engine/src/configuration/host.cc | 4 +- engine/src/configuration/hostdependency.cc | 4 +- engine/src/configuration/hostgroup.cc | 28 +-- engine/src/configuration/object.cc | 8 +- engine/src/configuration/parser.cc | 24 ++- engine/src/configuration/service.cc | 153 +++++++-------- engine/src/configuration/servicedependency.cc | 13 +- engine/src/configuration/serviceescalation.cc | 32 ++-- engine/src/configuration/servicegroup.cc | 2 +- engine/src/configuration/severity.cc | 2 +- engine/src/configuration/state.cc | 178 ++++++++++-------- engine/src/configuration/tag.cc | 5 +- engine/src/configuration/timeperiod.cc | 2 +- .../configuration/applier/applier-service.cc | 18 +- engine/tests/configuration/contact.cc | 3 +- engine/tests/configuration/severity.cc | 15 +- engine/tests/configuration/tag.cc | 15 +- 41 files changed, 362 insertions(+), 277 deletions(-) diff --git a/engine/inc/com/centreon/engine/configuration/anomalydetection.hh b/engine/inc/com/centreon/engine/configuration/anomalydetection.hh index 68fb0d74495..24a9e1c8aa6 100644 --- a/engine/inc/com/centreon/engine/configuration/anomalydetection.hh +++ b/engine/inc/com/centreon/engine/configuration/anomalydetection.hh @@ -49,7 +49,7 @@ class anomalydetection : public object { bool operator==(anomalydetection const& other) const noexcept; bool operator!=(anomalydetection const& other) const noexcept; bool operator<(anomalydetection const& other) const noexcept; - void check_validity() const override; + void check_validity(error_info* err) const override; key_type key() const; void merge(object const& obj) override; bool parse(char const* key, char const* value) override; diff --git a/engine/inc/com/centreon/engine/configuration/command.hh b/engine/inc/com/centreon/engine/configuration/command.hh index 9702d7db9fe..d4faf24af3f 100644 --- a/engine/inc/com/centreon/engine/configuration/command.hh +++ b/engine/inc/com/centreon/engine/configuration/command.hh @@ -36,7 +36,7 @@ class command : public object { bool operator==(command const& right) const throw(); bool operator!=(command const& right) const throw(); bool operator<(command const& right) const throw(); - void check_validity() const override; + void check_validity(error_info* err) const override; key_type const& key() const throw(); void merge(object const& obj) override; bool parse(char const* key, char const* value) override; diff --git a/engine/inc/com/centreon/engine/configuration/connector.hh b/engine/inc/com/centreon/engine/configuration/connector.hh index 98c6129d172..8d43a5973eb 100644 --- a/engine/inc/com/centreon/engine/configuration/connector.hh +++ b/engine/inc/com/centreon/engine/configuration/connector.hh @@ -37,7 +37,7 @@ class connector : public object { bool operator==(connector const& right) const throw(); bool operator!=(connector const& right) const throw(); bool operator<(connector const& right) const throw(); - void check_validity() const override; + void check_validity(error_info* err) const override; key_type const& key() const throw(); void merge(object const& obj) override; bool parse(char const* key, char const* value) override; diff --git a/engine/inc/com/centreon/engine/configuration/contact.hh b/engine/inc/com/centreon/engine/configuration/contact.hh index fc2651fb4b1..9f2570c558a 100644 --- a/engine/inc/com/centreon/engine/configuration/contact.hh +++ b/engine/inc/com/centreon/engine/configuration/contact.hh @@ -42,7 +42,7 @@ class contact : public object { bool operator==(contact const& other) const noexcept; bool operator!=(contact const& other) const noexcept; bool operator<(contact const& other) const noexcept; - void check_validity() const override; + void check_validity(error_info* err) const override; key_type const& key() const noexcept; void merge(object const& obj) override; bool parse(const char* key, const char* value) override; diff --git a/engine/inc/com/centreon/engine/configuration/contactgroup.hh b/engine/inc/com/centreon/engine/configuration/contactgroup.hh index 86552d44da7..676d6b9c28a 100644 --- a/engine/inc/com/centreon/engine/configuration/contactgroup.hh +++ b/engine/inc/com/centreon/engine/configuration/contactgroup.hh @@ -37,7 +37,7 @@ class contactgroup : public object { bool operator==(contactgroup const& right) const noexcept; bool operator!=(contactgroup const& right) const noexcept; bool operator<(contactgroup const& right) const noexcept; - void check_validity() const override; + void check_validity(error_info* err) const override; key_type const& key() const noexcept; void merge(object const& obj) override; bool parse(char const* key, char const* value) override; diff --git a/engine/inc/com/centreon/engine/configuration/host.hh b/engine/inc/com/centreon/engine/configuration/host.hh index 6bb32c67dcb..6c9216b2af7 100644 --- a/engine/inc/com/centreon/engine/configuration/host.hh +++ b/engine/inc/com/centreon/engine/configuration/host.hh @@ -50,7 +50,7 @@ class host : public object { bool operator==(host const& other) const noexcept; bool operator!=(host const& other) const noexcept; bool operator<(host const& other) const noexcept; - void check_validity() const override; + void check_validity(error_info* err) const override; key_type key() const noexcept; void merge(object const& obj) override; bool parse(char const* key, char const* value) override; diff --git a/engine/inc/com/centreon/engine/configuration/hostdependency.hh b/engine/inc/com/centreon/engine/configuration/hostdependency.hh index 4e5bf5317fe..1bf37e76a36 100644 --- a/engine/inc/com/centreon/engine/configuration/hostdependency.hh +++ b/engine/inc/com/centreon/engine/configuration/hostdependency.hh @@ -50,7 +50,7 @@ class hostdependency : public object { bool operator==(hostdependency const& right) const throw(); bool operator!=(hostdependency const& right) const throw(); bool operator<(hostdependency const& right) const; - void check_validity() const override; + void check_validity(error_info* err) const override; key_type const& key() const throw(); void merge(object const& obj) override; bool parse(char const* key, char const* value) override; @@ -102,6 +102,6 @@ typedef std::shared_ptr hostdependency_ptr; typedef std::set set_hostdependency; } // namespace configuration -} +} // namespace com::centreon::engine #endif // !CCE_CONFIGURATION_HOSTDEPENDENCY_HH diff --git a/engine/inc/com/centreon/engine/configuration/hostescalation.hh b/engine/inc/com/centreon/engine/configuration/hostescalation.hh index b82af6d9df3..ca82f6f9928 100644 --- a/engine/inc/com/centreon/engine/configuration/hostescalation.hh +++ b/engine/inc/com/centreon/engine/configuration/hostescalation.hh @@ -45,7 +45,7 @@ class hostescalation : public object { bool operator==(hostescalation const& right) const throw(); bool operator!=(hostescalation const& right) const throw(); bool operator<(hostescalation const& right) const; - void check_validity() const override; + void check_validity(error_info* err) const override; key_type const& key() const throw(); void merge(object const& obj) override; bool parse(char const* key, char const* value) override; @@ -99,6 +99,6 @@ typedef std::shared_ptr hostescalation_ptr; typedef std::set set_hostescalation; } // namespace configuration -} +} // namespace com::centreon::engine #endif // !CCE_CONFIGURATION_HOSTESCALATION_HH diff --git a/engine/inc/com/centreon/engine/configuration/hostgroup.hh b/engine/inc/com/centreon/engine/configuration/hostgroup.hh index 2ffd52cea3c..de7bb1bbcfd 100644 --- a/engine/inc/com/centreon/engine/configuration/hostgroup.hh +++ b/engine/inc/com/centreon/engine/configuration/hostgroup.hh @@ -38,7 +38,7 @@ class hostgroup : public object { bool operator==(hostgroup const& right) const throw(); bool operator!=(hostgroup const& right) const throw(); bool operator<(hostgroup const& right) const throw(); - void check_validity() const override; + void check_validity(error_info* err) const override; key_type const& key() const throw(); void merge(object const& obj) override; bool parse(char const* key, char const* value) override; @@ -77,6 +77,6 @@ typedef std::shared_ptr hostgroup_ptr; typedef std::set set_hostgroup; } // namespace configuration -} +} // namespace com::centreon::engine #endif // !CCE_CONFIGURATION_HOSTGROUP_HH diff --git a/engine/inc/com/centreon/engine/configuration/object.hh b/engine/inc/com/centreon/engine/configuration/object.hh index e43e954c060..b7f75ba8fa4 100644 --- a/engine/inc/com/centreon/engine/configuration/object.hh +++ b/engine/inc/com/centreon/engine/configuration/object.hh @@ -1,6 +1,6 @@ /** * Copyright 2011-2015 Merethis - * Copyright 2016-2022 Centreon + * Copyright 2016-2024 Centreon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,10 @@ namespace com::centreon::engine { namespace configuration { class object { public: + struct error_info { + uint32_t config_warnings = 0; + uint32_t config_errors = 0; + }; enum object_type { command = 0, connector = 1, @@ -57,7 +61,10 @@ class object { object& operator=(object const& right); bool operator==(object const& right) const noexcept; bool operator!=(object const& right) const noexcept; - virtual void check_validity() const = 0; + virtual void check_validity(error_info* err) const { + err->config_errors += _err.config_errors; + err->config_warnings += _err.config_warnings; + } static std::shared_ptr create(std::string const& type_name); virtual void merge(object const& obj) = 0; const std::string& name() const noexcept; @@ -121,6 +128,8 @@ class object { bool _should_register; list_string _templates; object_type _type; + error_info _err; + std::shared_ptr _logger; }; typedef std::shared_ptr object_ptr; diff --git a/engine/inc/com/centreon/engine/configuration/parser.hh b/engine/inc/com/centreon/engine/configuration/parser.hh index 923147c36ca..f68a609817f 100644 --- a/engine/inc/com/centreon/engine/configuration/parser.hh +++ b/engine/inc/com/centreon/engine/configuration/parser.hh @@ -88,7 +88,7 @@ class parser { void _parse_directory_configuration(std::string const& path); void _parse_object_definitions(const std::string& path); void _parse_resource_file(std::string const& path); - void _resolve_template(); + void _resolve_template(object::error_info* err); void _parse_global_configuration(std::string const& path); void _store_into_list(object_ptr obj); template @@ -103,6 +103,9 @@ class parser { unsigned int _read_options; static store _store[]; std::array _templates; + + /* Configuration Logger */ + std::shared_ptr _logger; }; } // namespace configuration diff --git a/engine/inc/com/centreon/engine/configuration/service.hh b/engine/inc/com/centreon/engine/configuration/service.hh index 5831b2efa12..861162b8e08 100644 --- a/engine/inc/com/centreon/engine/configuration/service.hh +++ b/engine/inc/com/centreon/engine/configuration/service.hh @@ -30,6 +30,12 @@ namespace com::centreon::engine::configuration { class service : public object { public: + enum state { + state_ok = 0, + state_warning = 1, + state_critical = 2, + state_unknown = 3, + }; enum action_on { none = 0, ok = (1 << 0), @@ -47,7 +53,7 @@ class service : public object { bool operator==(service const& other) const noexcept; bool operator!=(service const& other) const noexcept; bool operator<(service const& other) const noexcept; - void check_validity() const override; + void check_validity(error_info* err) const override; key_type key() const; void merge(object const& obj) override; bool parse(char const* key, char const* value) override; diff --git a/engine/inc/com/centreon/engine/configuration/servicedependency.hh b/engine/inc/com/centreon/engine/configuration/servicedependency.hh index 85b91134e2b..1febeb9058e 100644 --- a/engine/inc/com/centreon/engine/configuration/servicedependency.hh +++ b/engine/inc/com/centreon/engine/configuration/servicedependency.hh @@ -51,7 +51,7 @@ class servicedependency : public object { bool operator==(servicedependency const& right) const throw(); bool operator!=(servicedependency const& right) const throw(); bool operator<(servicedependency const& right) const; - void check_validity() const override; + void check_validity(error_info* err) const override; key_type const& key() const throw(); void merge(object const& obj) override; bool parse(char const* key, char const* value) override; @@ -119,6 +119,6 @@ typedef std::shared_ptr servicedependency_ptr; typedef std::set set_servicedependency; } // namespace configuration -} +} // namespace com::centreon::engine #endif // !CCE_CONFIGURATION_SERVICEDEPENDENCY_HH diff --git a/engine/inc/com/centreon/engine/configuration/serviceescalation.hh b/engine/inc/com/centreon/engine/configuration/serviceescalation.hh index e66e8fae8ba..1f7734b4f21 100644 --- a/engine/inc/com/centreon/engine/configuration/serviceescalation.hh +++ b/engine/inc/com/centreon/engine/configuration/serviceescalation.hh @@ -72,7 +72,7 @@ class serviceescalation : public object { bool operator==(serviceescalation const& right) const noexcept; bool operator!=(serviceescalation const& right) const noexcept; bool operator<(serviceescalation const& right) const; - void check_validity() const override; + void check_validity(error_info* err) const override; key_type const& key() const noexcept; void merge(object const& obj) override; bool parse(char const* key, char const* value) override; diff --git a/engine/inc/com/centreon/engine/configuration/servicegroup.hh b/engine/inc/com/centreon/engine/configuration/servicegroup.hh index 59ff29a0cf4..16ebc6b6546 100644 --- a/engine/inc/com/centreon/engine/configuration/servicegroup.hh +++ b/engine/inc/com/centreon/engine/configuration/servicegroup.hh @@ -40,7 +40,7 @@ class servicegroup : public object { bool operator==(servicegroup const& right) const throw(); bool operator!=(servicegroup const& right) const throw(); bool operator<(servicegroup const& right) const throw(); - void check_validity() const override; + void check_validity(error_info* err) const override; key_type const& key() const throw(); void merge(object const& obj) override; bool parse(char const* key, char const* value) override; @@ -83,6 +83,6 @@ typedef std::shared_ptr servicegroup_ptr; typedef std::set set_servicegroup; } // namespace configuration -} +} // namespace com::centreon::engine #endif // !CCE_CONFIGURATION_SERVICEGROUP_HH diff --git a/engine/inc/com/centreon/engine/configuration/severity.hh b/engine/inc/com/centreon/engine/configuration/severity.hh index 4908bd7ae39..683110e6157 100644 --- a/engine/inc/com/centreon/engine/configuration/severity.hh +++ b/engine/inc/com/centreon/engine/configuration/severity.hh @@ -56,7 +56,7 @@ class severity : public object { bool operator==(const severity& other) const noexcept; bool operator!=(const severity& other) const noexcept; bool operator<(const severity& other) const noexcept; - void check_validity() const override; + void check_validity(error_info* err) const override; const key_type& key() const noexcept; void merge(const object& obj) override; bool parse(const char* key, const char* value) override; @@ -72,6 +72,6 @@ class severity : public object { typedef std::set set_severity; } // namespace configuration -} +} // namespace com::centreon::engine #endif // !CCE_CONFIGURATION_SEVERITY_HH diff --git a/engine/inc/com/centreon/engine/configuration/state.hh b/engine/inc/com/centreon/engine/configuration/state.hh index 80ce73c9fb4..2a341ae7e5a 100644 --- a/engine/inc/com/centreon/engine/configuration/state.hh +++ b/engine/inc/com/centreon/engine/configuration/state.hh @@ -61,6 +61,15 @@ class setter_base { * to manage configuration data. */ class state { + std::shared_ptr _config_logger; + object::error_info _err; + + struct sched_info_config { + double host_inter_check_delay; + double service_inter_check_delay; + int32_t service_interleave_factor; + } _scheduling_info; + public: /** * @enum state::date_format @@ -449,6 +458,7 @@ class state { void use_true_regexp_matching(bool value); bool use_send_recovery_notifications_anyways() const; void use_send_recovery_notifications_anyways(bool value); + sched_info_config& sched_info_config() { return _scheduling_info; } using setter_map = absl::flat_hash_map>; @@ -456,6 +466,8 @@ class state { void apply_extended_conf(const std::string& file_path, const rapidjson::Document& json_doc); + void clear_error(); + object::error_info* error_info(); private: static void _init_setter(); diff --git a/engine/inc/com/centreon/engine/configuration/tag.hh b/engine/inc/com/centreon/engine/configuration/tag.hh index b1819fa12ee..be0f851215d 100644 --- a/engine/inc/com/centreon/engine/configuration/tag.hh +++ b/engine/inc/com/centreon/engine/configuration/tag.hh @@ -1,20 +1,20 @@ -/* -** Copyright 2022 Centreon -** -** 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 -*/ +/** + * Copyright 2022-2024 Centreon + * + * 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_TAG_HH #define CCE_CONFIGURATION_TAG_HH @@ -55,7 +55,7 @@ class tag : public object { bool operator==(const tag& other) const noexcept; bool operator!=(const tag& other) const noexcept; bool operator<(const tag& other) const noexcept; - void check_validity() const override; + void check_validity(error_info* err) const override; const key_type& key() const noexcept; void merge(const object& obj) override; bool parse(const char* key, const char* value) override; @@ -68,6 +68,6 @@ class tag : public object { typedef std::set set_tag; } // namespace configuration -} +} // namespace com::centreon::engine #endif // !CCE_CONFIGURATION_TAG_HH diff --git a/engine/inc/com/centreon/engine/configuration/timeperiod.hh b/engine/inc/com/centreon/engine/configuration/timeperiod.hh index e16117080a3..afa32ff6844 100644 --- a/engine/inc/com/centreon/engine/configuration/timeperiod.hh +++ b/engine/inc/com/centreon/engine/configuration/timeperiod.hh @@ -42,7 +42,7 @@ class timeperiod : public object { bool operator==(timeperiod const& right) const; bool operator!=(timeperiod const& right) const; bool operator<(timeperiod const& right) const; - void check_validity() const override; + void check_validity(error_info* err) const override; key_type const& key() const throw(); void merge(object const& obj) override; bool parse(char const* key, char const* value) override; diff --git a/engine/src/configuration/anomalydetection.cc b/engine/src/configuration/anomalydetection.cc index f1d96c95305..a812f2992de 100644 --- a/engine/src/configuration/anomalydetection.cc +++ b/engine/src/configuration/anomalydetection.cc @@ -760,7 +760,9 @@ bool anomalydetection::operator<(anomalydetection const& other) const noexcept { * @exception exceptions::msg_fmt if this anomalydetection is an invalid * object. */ -void anomalydetection::check_validity() const { +void anomalydetection::check_validity(error_info* err) const { + object::check_validity(err); + if (_service_description.empty()) throw msg_fmt( "Service has no description (property " diff --git a/engine/src/configuration/command.cc b/engine/src/configuration/command.cc index f12fb86c2a4..35b20d4f64e 100644 --- a/engine/src/configuration/command.cc +++ b/engine/src/configuration/command.cc @@ -113,7 +113,7 @@ bool command::operator<(command const& right) const throw() { * * If the object is not valid, an exception is thrown. */ -void command::check_validity() const { +void command::check_validity(error_info* err [[maybe_unused]]) const { if (_command_name.empty()) throw error("Command has no name (property 'command_name')"); if (_command_line.empty()) diff --git a/engine/src/configuration/connector.cc b/engine/src/configuration/connector.cc index 9d2c9ed3fa9..9886d8d9899 100644 --- a/engine/src/configuration/connector.cc +++ b/engine/src/configuration/connector.cc @@ -18,6 +18,7 @@ * */ #include "com/centreon/engine/configuration/connector.hh" +#include "com/centreon/engine/configuration/object.hh" #include "com/centreon/exceptions/error.hh" using namespace com::centreon; @@ -112,7 +113,8 @@ bool connector::operator<(connector const& right) const throw() { * * If the object is not valid, an exception is thrown. */ -void connector::check_validity() const { +void connector::check_validity(error_info* err) const { + object::check_validity(err); if (_connector_name.empty()) throw error("Connector has no name (property 'connector_name')"); if (_connector_line.empty()) diff --git a/engine/src/configuration/contact.cc b/engine/src/configuration/contact.cc index 550cb0e0519..b68f4868511 100644 --- a/engine/src/configuration/contact.cc +++ b/engine/src/configuration/contact.cc @@ -230,7 +230,8 @@ bool contact::operator<(contact const& other) const noexcept { * * If the object is not valid, an exception is thrown. */ -void contact::check_validity() const { +void contact::check_validity(error_info* err) const { + object::check_validity(err); if (_contact_name.empty()) throw exceptions::msg_fmt("Contact has no name (property 'contact_name')"); } diff --git a/engine/src/configuration/contactgroup.cc b/engine/src/configuration/contactgroup.cc index 2348efbd749..0cf53620fed 100644 --- a/engine/src/configuration/contactgroup.cc +++ b/engine/src/configuration/contactgroup.cc @@ -120,7 +120,7 @@ bool contactgroup::operator<(contactgroup const& right) const throw() { * * If the object is not valid, an exception is thrown. */ -void contactgroup::check_validity() const { +void contactgroup::check_validity(error_info* err [[maybe_unused]]) const { if (_contactgroup_name.empty()) throw exceptions::msg_fmt( "Contact group has no name (property 'contactgroup_name')"); @@ -132,7 +132,7 @@ void contactgroup::check_validity() const { * @return The contact group name. */ contactgroup::key_type const& contactgroup::key() const throw() { - return (_contactgroup_name); + return _contactgroup_name; } /** diff --git a/engine/src/configuration/host.cc b/engine/src/configuration/host.cc index a958a6e7c8c..7c3965036e5 100644 --- a/engine/src/configuration/host.cc +++ b/engine/src/configuration/host.cc @@ -430,7 +430,9 @@ bool host::operator<(host const& other) const noexcept { * * If the object is not valid, an exception is thrown. */ -void host::check_validity() const { +void host::check_validity(error_info* err) const { + object::check_validity(err); + if (_host_name.empty()) throw msg_fmt("Host has no name (property 'host_name')"); if (_address.empty()) diff --git a/engine/src/configuration/hostdependency.cc b/engine/src/configuration/hostdependency.cc index e1ab0de267d..1681081cbb1 100644 --- a/engine/src/configuration/hostdependency.cc +++ b/engine/src/configuration/hostdependency.cc @@ -176,7 +176,9 @@ bool hostdependency::operator<(hostdependency const& right) const { * * If the object is not valid, an exception is thrown. */ -void hostdependency::check_validity() const { +void hostdependency::check_validity(error_info* err) const { + object::check_validity(err); + if (_hosts->empty() && _hostgroups->empty()) throw msg_fmt( "Host dependency is not attached to any host or host group (properties " diff --git a/engine/src/configuration/hostgroup.cc b/engine/src/configuration/hostgroup.cc index 8c1ef96cd5c..62dba8e2556 100644 --- a/engine/src/configuration/hostgroup.cc +++ b/engine/src/configuration/hostgroup.cc @@ -18,11 +18,12 @@ */ #include "com/centreon/engine/configuration/hostgroup.hh" -#include "com/centreon/engine/globals.hh" #include "com/centreon/exceptions/error.hh" +#include "common/log_v2/log_v2.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; +using com::centreon::common::log_v2::log_v2; using com::centreon::exceptions::error; #define SETTER(type, method) \ @@ -89,43 +90,40 @@ hostgroup& hostgroup::operator=(hostgroup const& right) { * @return True if is the same hostgroup, otherwise false. */ bool hostgroup::operator==(hostgroup const& right) const throw() { + auto logger = log_v2::instance().get(log_v2::CONFIG); if (!object::operator==(right)) { - config_logger->debug( - "configuration::hostgroup::equality => object don't match"); + logger->debug("configuration::hostgroup::equality => object don't match"); return false; } if (_action_url != right._action_url) { - config_logger->debug( + logger->debug( "configuration::hostgroup::equality => action url don't match"); return false; } if (_alias != right._alias) { - config_logger->debug( - "configuration::hostgroup::equality => alias don't match"); + logger->debug("configuration::hostgroup::equality => alias don't match"); return false; } if (_hostgroup_id != right._hostgroup_id) { - config_logger->debug( + logger->debug( "configuration::hostgroup::equality => hostgroup id don't match"); return false; } if (_hostgroup_name != right._hostgroup_name) { - config_logger->debug( + logger->debug( "configuration::hostgroup::equality => hostgroup name don't match"); return false; } if (_members != right._members) { - config_logger->debug( - "configuration::hostgroup::equality => members don't match"); + logger->debug("configuration::hostgroup::equality => members don't match"); return false; } if (_notes != right._notes) { - config_logger->debug( - "configuration::hostgroup::equality => notes don't match"); + logger->debug("configuration::hostgroup::equality => notes don't match"); return false; } if (_notes_url != right._notes_url) { - config_logger->debug( + logger->debug( "configuration::hostgroup::equality => notes url don't match"); return false; } @@ -173,7 +171,9 @@ bool hostgroup::operator<(hostgroup const& right) const throw() { * * If the object is not valid, an exception is thrown. */ -void hostgroup::check_validity() const { +void hostgroup::check_validity(error_info* err) const { + object::check_validity(err); + if (_hostgroup_name.empty()) throw error("Host group has no name (property 'hostgroup_name')"); } diff --git a/engine/src/configuration/object.cc b/engine/src/configuration/object.cc index b5e779b25e6..aaafbfff86e 100644 --- a/engine/src/configuration/object.cc +++ b/engine/src/configuration/object.cc @@ -35,9 +35,11 @@ #include "com/centreon/engine/configuration/tag.hh" #include "com/centreon/engine/configuration/timeperiod.hh" #include "com/centreon/exceptions/msg_fmt.hh" +#include "common/log_v2/log_v2.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; +using com::centreon::common::log_v2::log_v2; using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ @@ -54,7 +56,10 @@ object::setters const object::_setters[] = { * @param[in] type The object type. */ object::object(object::object_type type) - : _is_resolve(false), _should_register(true), _type(type) {} + : _is_resolve(false), + _should_register(true), + _type(type), + _logger{log_v2::instance().get(log_v2::CONFIG)} {} /** * Copy constructor. @@ -84,6 +89,7 @@ object& object::operator=(object const& right) { _should_register = right._should_register; _templates = right._templates; _type = right._type; + _logger = right._logger; } return *this; } diff --git a/engine/src/configuration/parser.cc b/engine/src/configuration/parser.cc index 64ec8772cdd..59c80a3c6af 100644 --- a/engine/src/configuration/parser.cc +++ b/engine/src/configuration/parser.cc @@ -18,14 +18,17 @@ */ #include "com/centreon/engine/configuration/parser.hh" -#include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/exceptions/error.hh" +// #include "com/centreon/engine/globals.hh" #include "com/centreon/exceptions/error.hh" #include "com/centreon/exceptions/msg_fmt.hh" #include "com/centreon/io/directory_entry.hh" +#include "common/log_v2/log_v2.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; using namespace com::centreon::io; +using com::centreon::common::log_v2::log_v2; using com::centreon::exceptions::error; using com::centreon::exceptions::msg_fmt; @@ -80,7 +83,9 @@ static bool get_next_line(std::ifstream& stream, * (use to skip some object type). */ parser::parser(unsigned int read_options) - : _config(nullptr), _read_options(read_options) {} + : _config(nullptr), + _read_options(read_options), + _logger{log_v2::instance().get(log_v2::CONFIG)} {} /** * Parse the object definition file. @@ -88,7 +93,7 @@ parser::parser(unsigned int read_options) * @param[in] path The object definitions path. */ void parser::_parse_object_definitions(std::string const& path) { - config_logger->info("Processing object config file '{}'", path); + _logger->info("Processing object config file '{}'", path); std::ifstream stream(path, std::ios::binary); if (!stream.is_open()) @@ -178,7 +183,7 @@ void parser::_parse_directory_configuration(std::string const& path) { * @param[in] path The configuration path. */ void parser::_parse_global_configuration(const std::string& path) { - config_logger->info("Reading main configuration file '{}'.", path); + _logger->info("Reading main configuration file '{}'.", path); std::ifstream stream(path, std::ios::binary); if (!stream.is_open()) @@ -215,7 +220,7 @@ void parser::_parse_global_configuration(const std::string& path) { * @param[in] path The resource file path. */ void parser::_parse_resource_file(std::string const& path) { - config_logger->info("Reading resource file '{}'", path); + _logger->info("Reading resource file '{}'", path); std::ifstream stream(path.c_str(), std::ios::binary); if (!stream.is_open()) @@ -253,7 +258,7 @@ void parser::_parse_resource_file(std::string const& path) { /** * Resolve template for register objects. */ -void parser::_resolve_template() { +void parser::_resolve_template(object::error_info* err) { for (map_object& templates : _templates) { for (map_object::iterator it = templates.begin(), end = templates.end(); it != end; ++it) @@ -267,7 +272,7 @@ void parser::_resolve_template() { it != end; ++it) { (*it)->resolve_template(templates); try { - (*it)->check_validity(); + (*it)->check_validity(err); } catch (std::exception const& e) { throw error("Configuration parsing failed {}: {}", _get_file_info(it->get()), e.what()); @@ -282,7 +287,7 @@ void parser::_resolve_template() { it != end; ++it) { it->second->resolve_template(templates); try { - it->second->check_validity(); + it->second->check_validity(err); } catch (const std::exception& e) { throw error("Configuration parsing failed {}: {}", _get_file_info(it->second.get()), e.what()); @@ -299,6 +304,7 @@ void parser::_resolve_template() { */ void parser::parse(const std::string& path, state& config) { _config = &config; + _config->clear_error(); // parse the global configuration file. _parse_global_configuration(path); @@ -311,7 +317,7 @@ void parser::parse(const std::string& path, state& config) { _apply(config.cfg_dir(), &parser::_parse_directory_configuration); // Apply template. - _resolve_template(); + _resolve_template(config.error_info()); // Fill state. _insert(_map_objects[object::command], config.commands()); diff --git a/engine/src/configuration/service.cc b/engine/src/configuration/service.cc index 618305b6863..7effeecfd83 100644 --- a/engine/src/configuration/service.cc +++ b/engine/src/configuration/service.cc @@ -21,14 +21,13 @@ #include #include #include -#include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/configuration/tag.hh" #include "com/centreon/exceptions/msg_fmt.hh" - -extern int config_warnings; -extern int config_errors; +#include "common/log_v2/log_v2.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; +using com::centreon::common::log_v2::log_v2; using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ @@ -121,7 +120,7 @@ static unsigned short const default_flap_detection_options(service::ok | service::critical); static unsigned int const default_freshness_threshold(0); static unsigned int const default_high_flap_threshold(0); -static unsigned int const default_initial_state(engine::service::state_ok); +static unsigned int const default_initial_state(service::state_ok); static bool const default_is_volatile(false); static unsigned int const default_low_flap_threshold(0); static unsigned int const default_max_check_attempts(3); @@ -303,271 +302,262 @@ service& service::operator=(service const& other) { */ bool service::operator==(service const& other) const noexcept { if (!object::operator==(other)) { - config_logger->debug( - "configuration::service::equality => object don't match"); + _logger->debug("configuration::service::equality => object don't match"); return false; } if (_acknowledgement_timeout != other._acknowledgement_timeout) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "acknowledgement_timeout don't match"); return false; } if (_action_url != other._action_url) { - config_logger->debug( + _logger->debug( "configuration::service::equality => action_url don't match"); return false; } if (_checks_active != other._checks_active) { - config_logger->debug( + _logger->debug( "configuration::service::equality => checks_active don't match"); return false; } if (_checks_passive != other._checks_passive) { - config_logger->debug( + _logger->debug( "configuration::service::equality => checks_passive don't match"); return false; } if (_check_command != other._check_command) { - config_logger->debug( + _logger->debug( "configuration::service::equality => checks_passive don't match"); return false; } if (_check_command_is_important != other._check_command_is_important) { - config_logger->debug( + _logger->debug( "configuration::service::equality => check_command don't match"); return false; } if (_check_freshness != other._check_freshness) { - config_logger->debug( + _logger->debug( "configuration::service::equality => check_freshness don't match"); return false; } if (_check_interval != other._check_interval) { - config_logger->debug( + _logger->debug( "configuration::service::equality => check_interval don't match"); return false; } if (_check_period != other._check_period) { - config_logger->debug( + _logger->debug( "configuration::service::equality => check_period don't match"); return false; } if (_contactgroups != other._contactgroups) { - config_logger->debug( + _logger->debug( "configuration::service::equality => contactgroups don't match"); return false; } if (_contacts != other._contacts) { - config_logger->debug( - "configuration::service::equality => contacts don't match"); + _logger->debug("configuration::service::equality => contacts don't match"); return false; } if (std::operator!=(_customvariables, other._customvariables)) { - config_logger->debug( + _logger->debug( "configuration::service::equality => customvariables don't match"); return false; } if (_display_name != other._display_name) { - config_logger->debug( + _logger->debug( "configuration::service::equality => display_name don't match"); return false; } if (_event_handler != other._event_handler) { - config_logger->debug( + _logger->debug( "configuration::service::equality => event_handler don't match"); return false; } if (_event_handler_enabled != other._event_handler_enabled) { - config_logger->debug( + _logger->debug( "configuration::service::equality => event_handler don't match"); return false; } if (_first_notification_delay != other._first_notification_delay) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "first_notification_delay don't match"); return false; } if (_flap_detection_enabled != other._flap_detection_enabled) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "flap_detection_enabled don't match"); return false; } if (_flap_detection_options != other._flap_detection_options) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "flap_detection_options don't match"); return false; } if (_freshness_threshold != other._freshness_threshold) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "freshness_threshold don't match"); return false; } if (_high_flap_threshold != other._high_flap_threshold) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "high_flap_threshold don't match"); return false; } if (_hostgroups != other._hostgroups) { - config_logger->debug( + _logger->debug( "configuration::service::equality => hostgroups don't match"); return false; } if (_hosts != other._hosts) { - config_logger->debug( - "configuration::service::equality => _hosts don't match"); + _logger->debug("configuration::service::equality => _hosts don't match"); return false; } if (_icon_image != other._icon_image) { - config_logger->debug( + _logger->debug( "configuration::service::equality => icon_image don't match"); return false; } if (_icon_image_alt != other._icon_image_alt) { - config_logger->debug( + _logger->debug( "configuration::service::equality => icon_image_alt don't match"); return false; } if (_initial_state != other._initial_state) { - config_logger->debug( + _logger->debug( "configuration::service::equality => initial_state don't match"); return false; } if (_is_volatile != other._is_volatile) { - config_logger->debug( + _logger->debug( "configuration::service::equality => is_volatile don't match"); return false; } if (_low_flap_threshold != other._low_flap_threshold) { - config_logger->debug( + _logger->debug( "configuration::service::equality => low_flap_threshold don't match"); return false; } if (_max_check_attempts != other._max_check_attempts) { - config_logger->debug( + _logger->debug( "configuration::service::equality => max_check_attempts don't match"); return false; } if (_notes != other._notes) { - config_logger->debug( - "configuration::service::equality => notes don't match"); + _logger->debug("configuration::service::equality => notes don't match"); return false; } if (_notes_url != other._notes_url) { - config_logger->debug( - "configuration::service::equality => notes_url don't match"); + _logger->debug("configuration::service::equality => notes_url don't match"); return false; } if (_notifications_enabled != other._notifications_enabled) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "notifications_enabled don't match"); return false; } if (_notification_interval != other._notification_interval) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "notification_interval don't match"); return false; } if (_notification_options != other._notification_options) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "notification_options don't match"); return false; } if (_notification_period != other._notification_period) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "notification_period don't match"); return false; } if (_obsess_over_service != other._obsess_over_service) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "obsess_over_service don't match"); return false; } if (_process_perf_data != other._process_perf_data) { - config_logger->debug( + _logger->debug( "configuration::service::equality => process_perf_data don't match"); return false; } if (_retain_nonstatus_information != other._retain_nonstatus_information) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "retain_nonstatus_information don't match"); return false; } if (_retain_status_information != other._retain_status_information) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "retain_status_information don't match"); return false; } if (_retry_interval != other._retry_interval) { - config_logger->debug( + _logger->debug( "configuration::service::equality => retry_interval don't match"); return false; } if (_recovery_notification_delay != other._recovery_notification_delay) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "recovery_notification_delay don't match"); return false; } if (_servicegroups != other._servicegroups) { - config_logger->debug( + _logger->debug( "configuration::service::equality => servicegroups don't match"); return false; } if (_service_description != other._service_description) { - config_logger->debug( + _logger->debug( "configuration::service::equality => " "service_description don't match"); return false; } if (_host_id != other._host_id) { - config_logger->debug( - "configuration::service::equality => host_id don't match"); + _logger->debug("configuration::service::equality => host_id don't match"); return false; } if (_service_id != other._service_id) { - config_logger->debug( + _logger->debug( "configuration::service::equality => service_id don't match"); return false; } if (_stalking_options != other._stalking_options) { - config_logger->debug( + _logger->debug( "configuration::service::equality => stalking_options don't match"); return false; } if (_timezone != other._timezone) { - config_logger->debug( - "configuration::service::equality => timezone don't match"); + _logger->debug("configuration::service::equality => timezone don't match"); return false; } if (_severity_id != other._severity_id) { - config_logger->debug( + _logger->debug( "configuration::service::equality => severity id don't match"); return false; } if (_icon_id != other._icon_id) { - config_logger->debug( - "configuration::service::equality => icon id don't match"); + _logger->debug("configuration::service::equality => icon id don't match"); return false; } if (_tags != other._tags) { - config_logger->debug( - "configuration::service::equality => tags don't match"); + _logger->debug("configuration::service::equality => tags don't match"); return false; } - config_logger->debug("configuration::service::equality => OK"); + _logger->debug("configuration::service::equality => OK"); return true; } @@ -696,7 +686,9 @@ bool service::operator<(service const& other) const noexcept { * * @return True if is a valid object, otherwise false. */ -void service::check_validity() const { +void service::check_validity(error_info* err) const { + object::check_validity(err); + if (_service_description.empty()) throw msg_fmt( "Service has no description (property 'service_description')"); @@ -1537,10 +1529,11 @@ bool service::_set_event_handler_enabled(bool value) { */ bool service::_set_failure_prediction_enabled(bool value) { (void)value; - config_logger->warn( + auto logger = log_v2::instance().get(log_v2::CONFIG); + logger->warn( "Warning: service failure_prediction_enabled is deprecated. This option " "will not be supported in 20.04."); - ++config_warnings; + ++_err.config_warnings; return true; } @@ -1553,10 +1546,10 @@ bool service::_set_failure_prediction_enabled(bool value) { */ bool service::_set_failure_prediction_options(std::string const& value) { (void)value; - config_logger->warn( + _logger->warn( "Warning: service failure_prediction_options is deprecated. This option " "will not be supported in 20.04."); - ++config_warnings; + ++_err.config_warnings; return true; } @@ -1698,13 +1691,13 @@ bool service::_set_initial_state(std::string const& value) { std::string_view data(value); data = absl::StripAsciiWhitespace(data); if (data == "o" || data == "ok") - _initial_state = engine::service::state_ok; + _initial_state = service::state_ok; else if (data == "w" || data == "warning") - _initial_state = engine::service::state_warning; + _initial_state = service::state_warning; else if (data == "u" || data == "unknown") - _initial_state = engine::service::state_unknown; + _initial_state = service::state_unknown; else if (data == "c" || data == "critical") - _initial_state = engine::service::state_critical; + _initial_state = service::state_critical; else return false; return true; @@ -1864,10 +1857,10 @@ bool service::_set_obsess_over_service(bool value) { */ bool service::_set_parallelize_check(bool value) { (void)value; - config_logger->warn( + _logger->warn( "Warning: service parallelize_check is deprecated This option will not " "be supported in 20.04."); - ++config_warnings; + ++_err.config_warnings; return true; } @@ -2038,8 +2031,8 @@ bool service::_set_category_tags(const std::string& value) { if (parse_ok) { _tags.emplace(id, tag::servicecategory); } else { - config_logger->warn("Warning: service ({}, {}) error for parsing tag {}", - _host_id, _service_id, value); + _logger->warn("Warning: service ({}, {}) error for parsing tag {}", + _host_id, _service_id, value); ret = false; } } @@ -2072,8 +2065,8 @@ bool service::_set_group_tags(const std::string& value) { if (parse_ok) { _tags.emplace(id, tag::servicegroup); } else { - config_logger->warn("Warning: service ({}, {}) error for parsing tag {}", - _host_id, _service_id, value); + _logger->warn("Warning: service ({}, {}) error for parsing tag {}", + _host_id, _service_id, value); ret = false; } } diff --git a/engine/src/configuration/servicedependency.cc b/engine/src/configuration/servicedependency.cc index ba5a722fae3..cf56efb6f47 100644 --- a/engine/src/configuration/servicedependency.cc +++ b/engine/src/configuration/servicedependency.cc @@ -19,14 +19,12 @@ */ #include "com/centreon/engine/configuration/servicedependency.hh" -#include "com/centreon/engine/globals.hh" #include "com/centreon/exceptions/msg_fmt.hh" - -extern int config_warnings; -extern int config_errors; +#include "common/log_v2/log_v2.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; +using com::centreon::common::log_v2::log_v2; using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ @@ -221,7 +219,8 @@ bool servicedependency::operator<(servicedependency const& right) const { * * If the object is not valid, an exception is thrown. */ -void servicedependency::check_validity() const { +void servicedependency::check_validity(error_info* err) const { + auto logger = log_v2::instance().get(log_v2::CONFIG); // Check base service(s). if (_servicegroups->empty()) { if (_service_description->empty()) @@ -251,7 +250,7 @@ void servicedependency::check_validity() const { // With no execution or failure options this dependency is useless. if (!_execution_failure_options && !_notification_failure_options) { - ++config_warnings; + ++err->config_warnings; std::ostringstream msg; msg << "Warning: Ignoring lame service dependency of "; if (!_dependent_servicegroups->empty()) @@ -273,7 +272,7 @@ void servicedependency::check_validity() const { else msg << "host '" << _hosts->front() << "'"; } - config_logger->warn(msg.str()); + logger->warn(msg.str()); } } diff --git a/engine/src/configuration/serviceescalation.cc b/engine/src/configuration/serviceescalation.cc index c28cf5517cd..cb1be130afe 100644 --- a/engine/src/configuration/serviceescalation.cc +++ b/engine/src/configuration/serviceescalation.cc @@ -18,11 +18,12 @@ */ #include "com/centreon/engine/configuration/serviceescalation.hh" -#include "com/centreon/engine/globals.hh" #include "com/centreon/exceptions/msg_fmt.hh" +#include "common/log_v2/log_v2.hh" using namespace com::centreon; using namespace com::centreon::engine::configuration; +using com::centreon::common::log_v2::log_v2; using com::centreon::exceptions::msg_fmt; #define SETTER(type, method) \ @@ -112,75 +113,76 @@ serviceescalation& serviceescalation::operator=( */ bool serviceescalation::operator==( serviceescalation const& right) const noexcept { + auto logger = log_v2::instance().get(log_v2::CONFIG); /* No comparison is made on the UUID because it is used between the * configuration object and the object. Since this object is randomly * constructor in almost all cases, we can have two equal escalations * with different uuid.*/ if (!object::operator==(right)) { - config_logger->debug( + logger->debug( "configuration::serviceescalation::equality => object don't match"); return false; } if (_contactgroups != right._contactgroups) { - config_logger->debug( + logger->debug( "configuration::serviceescalation::" "equality => contact groups don't match"); return false; } if (_escalation_options != right._escalation_options) { - config_logger->debug( + logger->debug( "configuration::serviceescalation::equality => escalation options " "don't match"); return false; } if (_escalation_period != right._escalation_period) { - config_logger->debug( + logger->debug( "configuration::serviceescalation::equality => escalation periods " "don't match"); return false; } if (_first_notification != right._first_notification) { - config_logger->debug( + logger->debug( "configuration::serviceescalation::equality => first notifications " "don't match"); return false; } if (_hostgroups != right._hostgroups) { - config_logger->debug( + logger->debug( "configuration::serviceescalation::equality => host groups don't " "match"); return false; } if (_hosts != right._hosts) { - config_logger->debug( + logger->debug( "configuration::serviceescalation::equality => hosts don't match"); return false; } if (_last_notification != right._last_notification) { - config_logger->debug( + logger->debug( "configuration::serviceescalation::equality => last notification " "don't match"); return false; } if (_notification_interval != right._notification_interval) { - config_logger->debug( + logger->debug( "configuration::serviceescalation::equality => notification " "interval don't match"); return false; } if (_servicegroups != right._servicegroups) { - config_logger->debug( + logger->debug( "configuration::serviceescalation::" "equality => service groups don't match"); return false; } if (_service_description != right._service_description) { - config_logger->debug( + logger->debug( "configuration::serviceescalation::equality => service descriptions " "don't match"); return false; } - config_logger->debug("configuration::serviceescalation::equality => OK"); + logger->debug("configuration::serviceescalation::equality => OK"); return true; } @@ -230,7 +232,9 @@ bool serviceescalation::operator<(serviceescalation const& right) const { * * If the object is not valid, an exception is thrown. */ -void serviceescalation::check_validity() const { +void serviceescalation::check_validity(error_info* err) const { + object::check_validity(err); + if (_servicegroups->empty()) { if (_service_description->empty()) throw msg_fmt( diff --git a/engine/src/configuration/servicegroup.cc b/engine/src/configuration/servicegroup.cc index dcd621c59fb..0fd7364cb83 100644 --- a/engine/src/configuration/servicegroup.cc +++ b/engine/src/configuration/servicegroup.cc @@ -141,7 +141,7 @@ bool servicegroup::operator<(servicegroup const& right) const throw() { * * If the object is not valid, an exception is thrown. */ -void servicegroup::check_validity() const { +void servicegroup::check_validity(error_info* err) const { if (_servicegroup_name.empty()) throw error( "Service group has no name " diff --git a/engine/src/configuration/severity.cc b/engine/src/configuration/severity.cc index b1db1e963ac..4f72bbd1fcf 100644 --- a/engine/src/configuration/severity.cc +++ b/engine/src/configuration/severity.cc @@ -130,7 +130,7 @@ bool severity::operator<(const severity& other) const noexcept { * * If the object is not valid, an exception is thrown. */ -void severity::check_validity() const { +void severity::check_validity(error_info* err) const { if (_severity_name.empty()) throw error("Severity has no name (property 'severity_name')"); if (_key.first == 0) diff --git a/engine/src/configuration/state.cc b/engine/src/configuration/state.cc index 33bae55a104..9ba8ba15612 100644 --- a/engine/src/configuration/state.cc +++ b/engine/src/configuration/state.cc @@ -17,10 +17,12 @@ * */ #include "com/centreon/engine/configuration/state.hh" +#include #include "com/centreon/common/rapidjson_helper.hh" #include "com/centreon/engine/configuration/logging.hh" -#include "com/centreon/engine/globals.hh" +// #include "com/centreon/engine/globals.hh" #include "com/centreon/io/file_entry.hh" +#include "common/log_v2/log_v2.hh" #include "compatibility/locations.h" using namespace com::centreon; @@ -31,7 +33,11 @@ using com::centreon::common::log_v2::log_v2; namespace com::centreon::engine::configuration::detail { template struct setter : public setter_base { - setter(const std::string_view& field_name) : setter_base(field_name) {} + std::shared_ptr _logger; + + setter(const std::string_view& field_name, + const std::shared_ptr& logger) + : setter_base(field_name), _logger{logger} {} bool apply_from_cfg(state& obj, char const* value) override { try { U val(0); @@ -65,7 +71,7 @@ struct setter : public setter_base { std::is_same_v); } } catch (const std::exception& e) { - config_logger->error(e.what()); + _logger->error(e.what()); } return true; } @@ -76,7 +82,7 @@ struct setter : public setter_base { common::rapidjson_helper(doc).get(setter_base::_field_name.data()); (obj.*ptr)(val); } catch (std::exception const& e) { - SPDLOG_LOGGER_ERROR(config_logger, "fail to update {} : {}", + SPDLOG_LOGGER_ERROR(_logger, "fail to update {} : {}", setter_base::_field_name, e.what()); return false; } @@ -86,12 +92,15 @@ struct setter : public setter_base { template struct setter : public setter_base { - setter(const std::string_view& field_name) : setter_base(field_name) {} + std::shared_ptr _logger; + setter(const std::string_view& field_name, + const std::shared_ptr logger) + : setter_base(field_name), _logger{logger} {} bool apply_from_cfg(state& obj, char const* value) override { try { (obj.*ptr)(value); } catch (std::exception const& e) { - SPDLOG_LOGGER_ERROR(config_logger, "fail to update {} with value {}: {}", + SPDLOG_LOGGER_ERROR(_logger, "fail to update {} with value {}: {}", _field_name, value, e.what()); return false; } @@ -103,7 +112,7 @@ struct setter : public setter_base { common::rapidjson_helper(doc).get_string(_field_name.data()); (obj.*ptr)(val); } catch (std::exception const& e) { - SPDLOG_LOGGER_ERROR(config_logger, "fail to update {} : {}", _field_name, + SPDLOG_LOGGER_ERROR(_logger, "fail to update {} : {}", _field_name, e.what()); return false; } @@ -112,9 +121,10 @@ struct setter : public setter_base { }; }; // namespace com::centreon::engine::configuration::detail -#define SETTER(type, method, field) \ - _setters.emplace(std::make_pair( \ - field, std::make_unique>(field))) +#define SETTER(type, method, field) \ + _setters.emplace(std::make_pair( \ + field, std::make_unique>( \ + field, log_v2::instance().get(log_v2::CONFIG)))) state::setter_map state::_setters; @@ -465,7 +475,8 @@ static const std::string default_rpc_listen_address("localhost"); * Default constructor. */ state::state() - : _accept_passive_host_checks(default_accept_passive_host_checks), + : _config_logger{log_v2::instance().get(log_v2::CONFIG)}, + _accept_passive_host_checks(default_accept_passive_host_checks), _accept_passive_service_checks(default_accept_passive_service_checks), _additional_freshness_latency(default_additional_freshness_latency), _admin_email(default_admin_email), @@ -3445,7 +3456,7 @@ std::string const& state::service_perfdata_file() const noexcept { * @param[in] value The new service_perfdata_file value. */ void state::service_perfdata_file(std::string const& value) { - config_logger->warn( + _config_logger->warn( "Warning: service_perfdata_command is no more used for a long time, you " "should not use it anymore."); _service_perfdata_file = value; @@ -3640,7 +3651,7 @@ bool state::set(char const* key, char const* value) { if (it != _setters.end()) return (it->second)->apply_from_cfg(*this, value); } catch (std::exception const& e) { - config_logger->error(e.what()); + _config_logger->error(e.what()); return false; } return true; @@ -3774,11 +3785,11 @@ void state::user(unsigned int key, std::string const& value) { * @param[in] value The new use_aggressive_host_checking value. */ void state::use_aggressive_host_checking(bool value __attribute__((unused))) { - config_logger->warn( + _config_logger->warn( "Warning: use_aggressive_host_checking is deprecated. This option is " "no " "more supported since version 21.04."); - ++config_warnings; + _err.config_warnings++; } /** @@ -4287,8 +4298,8 @@ void state::use_true_regexp_matching(bool value) { */ void state::_set_aggregate_status_updates(std::string const& value) { (void)value; - config_logger->warn("Warning: aggregate_status_updates variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: aggregate_status_updates variable ignored"); + _err.config_warnings++; } /** @@ -4298,8 +4309,8 @@ void state::_set_aggregate_status_updates(std::string const& value) { */ void state::_set_auth_file(std::string const& value) { (void)value; - config_logger->warn("Warning: auth_file variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: auth_file variable ignored"); + ++_err.config_warnings; } /** @@ -4309,8 +4320,8 @@ void state::_set_auth_file(std::string const& value) { */ void state::_set_bare_update_check(std::string const& value) { (void)value; - config_logger->warn("Warning: bare_update_check variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: bare_update_check variable ignored"); + ++_err.config_warnings; } /** @@ -4359,8 +4370,8 @@ void state::_set_cfg_file(const std::string& value) { */ void state::_set_check_for_updates(std::string const& value) { (void)value; - config_logger->warn("Warning: check_for_updates variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: check_for_updates variable ignored"); + ++_err.config_warnings; } /** @@ -4370,8 +4381,8 @@ void state::_set_check_for_updates(std::string const& value) { */ void state::_set_child_processes_fork_twice(std::string const& value) { (void)value; - config_logger->warn("Warning: child_processes_fork_twice variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: child_processes_fork_twice variable ignored"); + ++_err.config_warnings; } /** @@ -4388,8 +4399,8 @@ void state::_set_command_check_interval(std::string const& value) { _command_check_interval_is_seconds = true; val.erase(val.begin() + pos); } - detail::setter("").apply_from_cfg( - *this, val.c_str()); + detail::setter("", _config_logger) + .apply_from_cfg(*this, val.c_str()); } /** @@ -4399,8 +4410,8 @@ void state::_set_command_check_interval(std::string const& value) { */ void state::_set_comment_file(std::string const& value) { (void)value; - config_logger->warn("Warning: comment_file variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: comment_file variable ignored"); + ++_err.config_warnings; } /** @@ -4410,8 +4421,8 @@ void state::_set_comment_file(std::string const& value) { */ void state::_set_daemon_dumps_core(std::string const& value) { (void)value; - config_logger->warn("Warning: daemon_dumps_core variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: daemon_dumps_core variable ignored"); + ++_err.config_warnings; } /** @@ -4437,8 +4448,8 @@ void state::_set_date_format(std::string const& value) { */ void state::_set_downtime_file(std::string const& value) { (void)value; - config_logger->warn("Warning: downtime_file variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: downtime_file variable ignored"); + ++_err.config_warnings; } /** @@ -4448,8 +4459,8 @@ void state::_set_downtime_file(std::string const& value) { */ void state::_set_enable_embedded_perl(std::string const& value) { (void)value; - config_logger->warn("Warning: enable_embedded_perl variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: enable_embedded_perl variable ignored"); + ++_err.config_warnings; } /** @@ -4459,8 +4470,8 @@ void state::_set_enable_embedded_perl(std::string const& value) { */ void state::_set_enable_failure_prediction(std::string const& value) { (void)value; - config_logger->warn("Warning: enable_failure_prediction variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: enable_failure_prediction variable ignored"); + ++_err.config_warnings; return; } @@ -4471,7 +4482,8 @@ void state::_set_enable_failure_prediction(std::string const& value) { */ void state::_set_event_broker_options(std::string const& value) { if (value != "-1") - detail::setter("") + detail::setter("", + _config_logger) .apply_from_cfg(*this, value.c_str()); else _event_broker_options = @@ -4485,8 +4497,8 @@ void state::_set_event_broker_options(std::string const& value) { */ void state::_set_free_child_process_memory(std::string const& value) { (void)value; - config_logger->warn("Warning: free_child_process_memory variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: free_child_process_memory variable ignored"); + ++_err.config_warnings; } /** @@ -4503,8 +4515,8 @@ void state::_set_host_inter_check_delay_method(std::string const& value) { _host_inter_check_delay_method = icd_smart; else { _host_inter_check_delay_method = icd_user; - if (!absl::SimpleAtod(value, &scheduling_info.host_inter_check_delay) || - scheduling_info.host_inter_check_delay <= 0.0) + if (!absl::SimpleAtod(value, &_scheduling_info.host_inter_check_delay) || + _scheduling_info.host_inter_check_delay <= 0.0) throw exceptions::msg_fmt( "Invalid value for host_inter_check_delay_method, must be one of 'n' " "(none), 'd' (dumb), 's' (smart) or a stricly positive value ({} " @@ -4534,8 +4546,8 @@ void state::_set_host_perfdata_file_mode(std::string const& value) { */ void state::_set_lock_file(std::string const& value) { (void)value; - config_logger->warn("Warning: lock_file variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: lock_file variable ignored"); + ++_err.config_warnings; } /** @@ -4545,8 +4557,8 @@ void state::_set_lock_file(std::string const& value) { */ void state::_set_log_archive_path(std::string const& value) { (void)value; - config_logger->warn("Warning: log_archive_path variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: log_archive_path variable ignored"); + ++_err.config_warnings; } /** @@ -4556,8 +4568,8 @@ void state::_set_log_archive_path(std::string const& value) { */ void state::_set_log_initial_states(std::string const& value) { (void)value; - config_logger->warn("Warning: log_initial_states variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: log_initial_states variable ignored"); + ++_err.config_warnings; return; } @@ -4568,8 +4580,8 @@ void state::_set_log_initial_states(std::string const& value) { */ void state::_set_log_rotation_method(std::string const& value) { (void)value; - config_logger->warn("Warning: log_rotation_method variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: log_rotation_method variable ignored"); + ++_err.config_warnings; } /** @@ -4579,8 +4591,8 @@ void state::_set_log_rotation_method(std::string const& value) { */ void state::_set_nagios_group(std::string const& value) { (void)value; - config_logger->warn("Warning: nagios_group variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: nagios_group variable ignored"); + ++_err.config_warnings; } /** @@ -4590,8 +4602,8 @@ void state::_set_nagios_group(std::string const& value) { */ void state::_set_nagios_user(std::string const& value) { (void)value; - config_logger->warn("Warning: nagios_user variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: nagios_user variable ignored"); + ++_err.config_warnings; } /** @@ -4601,8 +4613,8 @@ void state::_set_nagios_user(std::string const& value) { */ void state::_set_object_cache_file(std::string const& value) { (void)value; - config_logger->warn("Warning: object_cache_file variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: object_cache_file variable ignored"); + ++_err.config_warnings; } /** @@ -4612,9 +4624,9 @@ void state::_set_object_cache_file(std::string const& value) { */ void state::_set_p1_file(std::string const& value) { (void)value; - config_logger->warn("Warning: p1_file variable ignored"); + _config_logger->warn("Warning: p1_file variable ignored"); - ++config_warnings; + ++_err.config_warnings; } /** @@ -4624,8 +4636,8 @@ void state::_set_p1_file(std::string const& value) { */ void state::_set_precached_object_file(std::string const& value) { (void)value; - config_logger->warn("Warning: precached_object_file variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: precached_object_file variable ignored"); + ++_err.config_warnings; } /** @@ -4651,9 +4663,9 @@ void state::_set_resource_file(std::string const& value) { void state::_set_retained_process_service_attribute_mask( std::string const& value) { (void)value; - config_logger->warn( + _config_logger->warn( "Warning: retained_process_service_attribute_mask variable ignored"); - ++config_warnings; + ++_err.config_warnings; } /** @@ -4663,9 +4675,9 @@ void state::_set_retained_process_service_attribute_mask( */ void state::_set_retained_service_attribute_mask(std::string const& value) { (void)value; - config_logger->warn( + _config_logger->warn( "Warning: retained_service_attribute_mask variable ignored"); - ++config_warnings; + ++_err.config_warnings; } /** @@ -4682,8 +4694,8 @@ void state::_set_service_inter_check_delay_method(std::string const& value) { _service_inter_check_delay_method = icd_smart; else { _service_inter_check_delay_method = icd_user; - if (!absl::SimpleAtod(value, &scheduling_info.service_inter_check_delay) || - scheduling_info.service_inter_check_delay <= 0.0) + if (!absl::SimpleAtod(value, &_scheduling_info.service_inter_check_delay) || + _scheduling_info.service_inter_check_delay <= 0.0) throw exceptions::msg_fmt( "Invalid value for service_inter_check_delay_method, must be one of " "'n' (none), 'd' (dumb), 's' (smart) or a strictly positive value " @@ -4702,9 +4714,9 @@ void state::_set_service_interleave_factor_method(std::string const& value) { _service_interleave_factor_method = ilf_smart; else { _service_interleave_factor_method = ilf_user; - if (!absl::SimpleAtoi(value, &scheduling_info.service_interleave_factor) || - scheduling_info.service_interleave_factor < 1) - scheduling_info.service_interleave_factor = 1; + if (!absl::SimpleAtoi(value, &_scheduling_info.service_interleave_factor) || + _scheduling_info.service_interleave_factor < 1) + _scheduling_info.service_interleave_factor = 1; } } @@ -4729,8 +4741,8 @@ void state::_set_service_perfdata_file_mode(std::string const& value) { */ void state::_set_temp_file(std::string const& value) { (void)value; - config_logger->warn("Warning: temp_file variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: temp_file variable ignored"); + ++_err.config_warnings; } /** @@ -4740,8 +4752,8 @@ void state::_set_temp_file(std::string const& value) { */ void state::_set_temp_path(std::string const& value) { (void)value; - config_logger->warn("Warning: temp_path variable ignored"); - ++config_warnings; + _config_logger->warn("Warning: temp_path variable ignored"); + ++_err.config_warnings; } /** @@ -4751,8 +4763,9 @@ void state::_set_temp_path(std::string const& value) { */ void state::_set_use_embedded_perl_implicitly(std::string const& value) { (void)value; - config_logger->warn("Warning: use_embedded_perl_implicitly variable ignored"); - ++config_warnings; + _config_logger->warn( + "Warning: use_embedded_perl_implicitly variable ignored"); + ++_err.config_warnings; } void state::macros_filter(std::string const& value) { @@ -4833,19 +4846,28 @@ void state::use_send_recovery_notifications_anyways(bool value) { */ void state::apply_extended_conf(const std::string& file_path, const rapidjson::Document& json_doc) { - SPDLOG_LOGGER_INFO(config_logger, "apply conf from file {}", file_path); + SPDLOG_LOGGER_INFO(_config_logger, "apply conf from file {}", file_path); for (rapidjson::Value::ConstMemberIterator member_iter = json_doc.MemberBegin(); member_iter != json_doc.MemberEnd(); ++member_iter) { const std::string_view field_name = member_iter->name.GetString(); auto setter = _setters.find(field_name); if (setter == _setters.end()) { - SPDLOG_LOGGER_ERROR(config_logger, "unknown field: {} in file {}", + SPDLOG_LOGGER_ERROR(_config_logger, "unknown field: {} in file {}", field_name, file_path); } else if (!setter->second->apply_from_json(*this, json_doc)) { - SPDLOG_LOGGER_ERROR(config_logger, + SPDLOG_LOGGER_ERROR(_config_logger, "fail to update field: {} from file {}", field_name, file_path); } } } + +void state::clear_error() { + _err.config_warnings = 0; + _err.config_errors = 0; +} + +object::error_info* state::error_info() { + return &_err; +} diff --git a/engine/src/configuration/tag.cc b/engine/src/configuration/tag.cc index 6fc8ed542f3..73e8a1a150d 100644 --- a/engine/src/configuration/tag.cc +++ b/engine/src/configuration/tag.cc @@ -18,6 +18,7 @@ */ #include "com/centreon/engine/configuration/tag.hh" +#include "com/centreon/engine/configuration/object.hh" #include "com/centreon/exceptions/error.hh" using namespace com::centreon; @@ -111,7 +112,9 @@ bool tag::operator<(const tag& other) const noexcept { * * If the object is not valid, an exception is thrown. */ -void tag::check_validity() const { +void tag::check_validity(error_info* err) const { + object::check_validity(err); + if (_tag_name.empty()) throw error("Tag has no name (property 'tag_name')"); if (_key.first == 0) diff --git a/engine/src/configuration/timeperiod.cc b/engine/src/configuration/timeperiod.cc index 552c563bd53..66b93a810b9 100644 --- a/engine/src/configuration/timeperiod.cc +++ b/engine/src/configuration/timeperiod.cc @@ -123,7 +123,7 @@ bool timeperiod::operator<(timeperiod const& right) const { * * If the object is not valid, an exception is thrown. */ -void timeperiod::check_validity() const { +void timeperiod::check_validity(error_info* err) const { if (_timeperiod_name.empty()) throw exceptions::msg_fmt( "Time period has no name (property 'timeperiod_name')"); diff --git a/engine/tests/configuration/applier/applier-service.cc b/engine/tests/configuration/applier/applier-service.cc index fbbf969e7b1..9593e9f0ad2 100644 --- a/engine/tests/configuration/applier/applier-service.cc +++ b/engine/tests/configuration/applier/applier-service.cc @@ -269,19 +269,20 @@ TEST_F(ApplierService, ServicesCheckValidity) { configuration::applier::service svc_aply; configuration::service csvc; + object::error_info err; // No service description - ASSERT_THROW(csvc.check_validity(), msg_fmt); + ASSERT_THROW(csvc.check_validity(&err), msg_fmt); ASSERT_TRUE(csvc.parse("service_description", "check description")); ASSERT_TRUE(csvc.parse("service_id", "53")); // No host attached to - ASSERT_THROW(csvc.check_validity(), msg_fmt); + ASSERT_THROW(csvc.check_validity(&err), msg_fmt); ASSERT_TRUE(csvc.parse("hosts", "test_host")); // No check command attached to - ASSERT_THROW(csvc.check_validity(), msg_fmt); + ASSERT_THROW(csvc.check_validity(&err), msg_fmt); configuration::applier::command cmd_aply; configuration::command cmd("cmd"); @@ -302,7 +303,7 @@ TEST_F(ApplierService, ServicesCheckValidity) { ASSERT_TRUE(csvc.parse("service_description", "foo")); // No check command - ASSERT_NO_THROW(csvc.check_validity()); + ASSERT_NO_THROW(csvc.check_validity(&err)); svc_aply.resolve_object(csvc); service_map const& sm(engine::service::services); @@ -765,8 +766,9 @@ TEST_F(ApplierService, ServicesCheckValidityTags) { configuration::applier::service svc_aply; configuration::service csvc; + object::error_info err; // No service description - ASSERT_THROW(csvc.check_validity(), msg_fmt); + ASSERT_THROW(csvc.check_validity(&err), msg_fmt); ASSERT_TRUE(csvc.parse("service_description", "check description")); ASSERT_TRUE(csvc.parse("service_id", "53")); @@ -774,12 +776,12 @@ TEST_F(ApplierService, ServicesCheckValidityTags) { ASSERT_TRUE(csvc.parse("category_tags", "3")); // No host attached to - ASSERT_THROW(csvc.check_validity(), msg_fmt); + ASSERT_THROW(csvc.check_validity(&err), msg_fmt); ASSERT_TRUE(csvc.parse("hosts", "test_host")); // No check command attached to - ASSERT_THROW(csvc.check_validity(), msg_fmt); + ASSERT_THROW(csvc.check_validity(&err), msg_fmt); configuration::applier::command cmd_aply; configuration::command cmd("cmd"); @@ -816,7 +818,7 @@ TEST_F(ApplierService, ServicesCheckValidityTags) { ASSERT_TRUE(csvc.parse("service_description", "foo")); // No check command - ASSERT_NO_THROW(csvc.check_validity()); + ASSERT_NO_THROW(csvc.check_validity(&err)); svc_aply.resolve_object(csvc); service_map const& sm(engine::service::services); diff --git a/engine/tests/configuration/contact.cc b/engine/tests/configuration/contact.cc index 6c1cc81c527..bd9365b5dc7 100644 --- a/engine/tests/configuration/contact.cc +++ b/engine/tests/configuration/contact.cc @@ -39,5 +39,6 @@ class ConfigContact : public ::testing::Test { // Then an exception is thrown. TEST_F(ConfigContact, NewContactWithNoName) { configuration::contact ctct(""); - ASSERT_THROW(ctct.check_validity(), std::exception); + object::error_info err; + ASSERT_THROW(ctct.check_validity(&err), std::exception); } diff --git a/engine/tests/configuration/severity.cc b/engine/tests/configuration/severity.cc index ab632869ea5..ad5d1e0bc47 100644 --- a/engine/tests/configuration/severity.cc +++ b/engine/tests/configuration/severity.cc @@ -38,15 +38,17 @@ class ConfigSeverity : public ::testing::Test { // When I create a configuration::severity with a null id // Then an exception is thrown. TEST_F(ConfigSeverity, NewSeverityWithNoKey) { + object::error_info err; configuration::severity sv({0, 0}); - ASSERT_THROW(sv.check_validity(), std::exception); + ASSERT_THROW(sv.check_validity(&err), std::exception); } // When I create a configuration::severity with a null level // Then an exception is thrown. TEST_F(ConfigSeverity, NewSeverityWithNoLevel) { configuration::severity sv({1, 0}); - ASSERT_THROW(sv.check_validity(), std::exception); + object::error_info err; + ASSERT_THROW(sv.check_validity(&err), std::exception); } // When I create a configuration::severity with an empty name @@ -54,7 +56,8 @@ TEST_F(ConfigSeverity, NewSeverityWithNoLevel) { TEST_F(ConfigSeverity, NewSeverityWithNoName) { configuration::severity sv({1, 0}); sv.parse("level", "2"); - ASSERT_THROW(sv.check_validity(), std::exception); + object::error_info err; + ASSERT_THROW(sv.check_validity(&err), std::exception); } // When I create a configuration::severity with a non empty name, @@ -69,7 +72,8 @@ TEST_F(ConfigSeverity, NewSeverityWellFilled) { ASSERT_EQ(sv.level(), 2); ASSERT_EQ(sv.severity_name(), "foobar"); ASSERT_EQ(sv.type(), configuration::severity::service); - ASSERT_NO_THROW(sv.check_validity()); + object::error_info err; + ASSERT_NO_THROW(sv.check_validity(&err)); } // When I create a configuration::severity with an icon id. @@ -81,5 +85,6 @@ TEST_F(ConfigSeverity, NewSeverityIconId) { sv.parse("severity_name", "foobar"); sv.parse("type", "host"); ASSERT_EQ(sv.icon_id(), 18); - ASSERT_NO_THROW(sv.check_validity()); + object::error_info err; + ASSERT_NO_THROW(sv.check_validity(&err)); } diff --git a/engine/tests/configuration/tag.cc b/engine/tests/configuration/tag.cc index 32a1e3fcacc..83b3f972196 100644 --- a/engine/tests/configuration/tag.cc +++ b/engine/tests/configuration/tag.cc @@ -39,14 +39,16 @@ class ConfigTag : public ::testing::Test { // Then an exception is thrown. TEST_F(ConfigTag, NewTagWithNoKey) { configuration::tag tg({0, 0}); - ASSERT_THROW(tg.check_validity(), std::exception); + object::error_info err; + ASSERT_THROW(tg.check_validity(&err), std::exception); } // When I create a configuration::tag with a null type // Then an exception is thrown. TEST_F(ConfigTag, NewTagWithNoLevel) { configuration::tag tg({1, 0}); - ASSERT_THROW(tg.check_validity(), std::exception); + object::error_info err; + ASSERT_THROW(tg.check_validity(&err), std::exception); } // When I create a configuration::tag with an empty name @@ -54,7 +56,8 @@ TEST_F(ConfigTag, NewTagWithNoLevel) { TEST_F(ConfigTag, NewTagWithNoName) { configuration::tag tg({1, 0}); tg.parse("type", "hostcategory"); - ASSERT_THROW(tg.check_validity(), std::exception); + object::error_info err; + ASSERT_THROW(tg.check_validity(&err), std::exception); } // When I create a configuration::tag with a non empty name, @@ -67,7 +70,8 @@ TEST_F(ConfigTag, NewTagWellFilled) { ASSERT_EQ(tg.key().first, 1); ASSERT_EQ(tg.key().second, engine::configuration::tag::servicegroup); ASSERT_EQ(tg.tag_name(), "foobar"); - ASSERT_NO_THROW(tg.check_validity()); + object::error_info err; + ASSERT_NO_THROW(tg.check_validity(&err)); } // When I create a configuration::tag with a non empty name, @@ -78,5 +82,6 @@ TEST_F(ConfigTag, NewTagIconId) { tg.parse("type", "hostgroup"); tg.parse("tag_name", "foobar"); ASSERT_EQ(tg.key().second, engine::configuration::tag::hostgroup); - ASSERT_NO_THROW(tg.check_validity()); + object::error_info err; + ASSERT_NO_THROW(tg.check_validity(&err)); } From 8ce0f01e71a6fe298101f5653da44b9a9ba459a5 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Tue, 28 May 2024 14:52:30 +0200 Subject: [PATCH 14/21] enh(engine): dependencies on engine removed in configuration --- .../engine/configuration/anomalydetection.hh | 6 + .../engine/configuration/applier/scheduler.hh | 2 - .../com/centreon/engine/configuration/host.hh | 5 + .../centreon/engine/configuration/state.hh | 22 - .../com/centreon/engine/events/sched_info.hh | 9 - .../inc/com/centreon/engine/macros/defines.hh | 2 - engine/inc/com/centreon/engine/xpddefault.hh | 16 - engine/src/configuration/anomalydetection.cc | 13 +- engine/src/configuration/applier/macros.cc | 2 - engine/src/configuration/applier/scheduler.cc | 74 +-- engine/src/configuration/applier/state.cc | 41 +- engine/src/configuration/host.cc | 23 +- engine/src/configuration/hostdependency.cc | 3 +- engine/src/configuration/parser.cc | 2 - engine/src/configuration/state.cc | 224 ++------ engine/src/macros.cc | 4 - engine/src/macros/grab_value.cc | 22 +- engine/src/xpddefault.cc | 488 +----------------- 18 files changed, 85 insertions(+), 873 deletions(-) diff --git a/engine/inc/com/centreon/engine/configuration/anomalydetection.hh b/engine/inc/com/centreon/engine/configuration/anomalydetection.hh index 24a9e1c8aa6..1923306ec0d 100644 --- a/engine/inc/com/centreon/engine/configuration/anomalydetection.hh +++ b/engine/inc/com/centreon/engine/configuration/anomalydetection.hh @@ -31,6 +31,12 @@ namespace configuration { class anomalydetection : public object { public: + enum state { + state_ok = 0, + state_warning = 1, + state_critical = 2, + state_unknown = 3, + }; enum action_on { none = 0, ok = (1 << 0), diff --git a/engine/inc/com/centreon/engine/configuration/applier/scheduler.hh b/engine/inc/com/centreon/engine/configuration/applier/scheduler.hh index 557a36442ef..455791ab2d5 100644 --- a/engine/inc/com/centreon/engine/configuration/applier/scheduler.hh +++ b/engine/inc/com/centreon/engine/configuration/applier/scheduler.hh @@ -95,7 +95,6 @@ class scheduler { timed_event* _evt_reschedule_checks; timed_event* _evt_retention_save; timed_event* _evt_sfreshness_check; - timed_event* _evt_service_perfdata; timed_event* _evt_status_save; unsigned int _old_auto_rescheduling_interval; unsigned int _old_check_reaper_interval; @@ -106,7 +105,6 @@ class scheduler { unsigned int _old_retention_update_interval; unsigned int _old_service_freshness_check_interval; std::string _old_service_perfdata_file_processing_command; - unsigned int _old_service_perfdata_file_processing_interval; unsigned int _old_status_update_interval; }; } // namespace applier diff --git a/engine/inc/com/centreon/engine/configuration/host.hh b/engine/inc/com/centreon/engine/configuration/host.hh index 6c9216b2af7..67a5966f654 100644 --- a/engine/inc/com/centreon/engine/configuration/host.hh +++ b/engine/inc/com/centreon/engine/configuration/host.hh @@ -34,6 +34,11 @@ namespace configuration { class host : public object { public: + enum state { + state_up = 0, + state_down = 1, + state_unreachable = 2, + }; enum action_on { none = 0, up = (1 << 0), diff --git a/engine/inc/com/centreon/engine/configuration/state.hh b/engine/inc/com/centreon/engine/configuration/state.hh index 2a341ae7e5a..00b73e5d4ef 100644 --- a/engine/inc/com/centreon/engine/configuration/state.hh +++ b/engine/inc/com/centreon/engine/configuration/state.hh @@ -102,12 +102,6 @@ class state { ilf_smart // smart interleave }; - /** - * @enum state::perdata_file_mode - * - */ - enum perfdata_file_mode { mode_pipe = 0, mode_file, mode_file_append }; - state(); state(state const& right); ~state() noexcept; @@ -247,15 +241,9 @@ class state { void host_inter_check_delay_method(inter_check_delay value); std::string const& host_perfdata_command() const noexcept; void host_perfdata_command(std::string const& value); - std::string const& host_perfdata_file() const noexcept; void host_perfdata_file(std::string const& value); - perfdata_file_mode host_perfdata_file_mode() const noexcept; - void host_perfdata_file_mode(perfdata_file_mode value); - std::string const& host_perfdata_file_processing_command() const noexcept; void host_perfdata_file_processing_command(std::string const& value); - unsigned int host_perfdata_file_processing_interval() const noexcept; void host_perfdata_file_processing_interval(unsigned int value); - std::string const& host_perfdata_file_template() const noexcept; void host_perfdata_file_template(std::string const& value); std::string const& illegal_object_chars() const noexcept; void illegal_object_chars(std::string const& value); @@ -373,15 +361,9 @@ class state { void service_interleave_factor_method(interleave_factor value); std::string const& service_perfdata_command() const noexcept; void service_perfdata_command(std::string const& value); - std::string const& service_perfdata_file() const noexcept; void service_perfdata_file(std::string const& value); - perfdata_file_mode service_perfdata_file_mode() const noexcept; - void service_perfdata_file_mode(perfdata_file_mode value); - std::string const& service_perfdata_file_processing_command() const noexcept; void service_perfdata_file_processing_command(std::string const& value); - unsigned int service_perfdata_file_processing_interval() const noexcept; void service_perfdata_file_processing_interval(unsigned int value); - std::string const& service_perfdata_file_template() const noexcept; void service_perfdata_file_template(std::string const& value); float sleep_time() const noexcept; void sleep_time(float value); @@ -569,7 +551,6 @@ class state { inter_check_delay _host_inter_check_delay_method; std::string _host_perfdata_command; std::string _host_perfdata_file; - perfdata_file_mode _host_perfdata_file_mode; std::string _host_perfdata_file_processing_command; unsigned int _host_perfdata_file_processing_interval; std::string _host_perfdata_file_template; @@ -626,10 +607,7 @@ class state { interleave_factor _service_interleave_factor_method; std::string _service_perfdata_command; std::string _service_perfdata_file; - perfdata_file_mode _service_perfdata_file_mode; std::string _service_perfdata_file_processing_command; - unsigned int _service_perfdata_file_processing_interval; - std::string _service_perfdata_file_template; static setter_map _setters; float _sleep_time; bool _soft_state_dependencies; diff --git a/engine/inc/com/centreon/engine/events/sched_info.hh b/engine/inc/com/centreon/engine/events/sched_info.hh index 689a33cf0a8..b1d329504b8 100644 --- a/engine/inc/com/centreon/engine/events/sched_info.hh +++ b/engine/inc/com/centreon/engine/events/sched_info.hh @@ -55,20 +55,11 @@ typedef struct sched_info_struct { time_t last_host_check; } sched_info; -#ifdef __cplusplus -extern "C" { -#endif /* C++ */ - void adjust_check_scheduling(); void display_scheduling_info(); -#ifdef __cplusplus -} - bool operator==(sched_info const& obj1, sched_info const& obj2) noexcept; bool operator!=(sched_info const& obj1, sched_info const& obj2) noexcept; std::ostream& operator<<(std::ostream& os, sched_info const& obj); -#endif /* C++ */ - #endif // !CCE_EVENTS_SCHED_INFO_HH diff --git a/engine/inc/com/centreon/engine/macros/defines.hh b/engine/inc/com/centreon/engine/macros/defines.hh index cb09fa59892..c2d9dc0a84b 100644 --- a/engine/inc/com/centreon/engine/macros/defines.hh +++ b/engine/inc/com/centreon/engine/macros/defines.hh @@ -105,8 +105,6 @@ #define MACRO_LOGFILE 69 #define MACRO_RESOURCEFILE 70 #define MACRO_COMMANDFILE 71 -#define MACRO_HOSTPERFDATAFILE 72 -#define MACRO_SERVICEPERFDATAFILE 73 #define MACRO_HOSTACTIONURL 74 #define MACRO_HOSTNOTESURL 75 #define MACRO_HOSTNOTES 76 diff --git a/engine/inc/com/centreon/engine/xpddefault.hh b/engine/inc/com/centreon/engine/xpddefault.hh index dc78ca827e7..ceaaa9f495b 100644 --- a/engine/inc/com/centreon/engine/xpddefault.hh +++ b/engine/inc/com/centreon/engine/xpddefault.hh @@ -30,7 +30,6 @@ extern "C" { #endif // C++ int xpddefault_initialize_performance_data(); -int xpddefault_cleanup_performance_data(); int xpddefault_update_service_performance_data( com::centreon::engine::service* svc); @@ -43,23 +42,8 @@ int xpddefault_run_host_performance_data_command( nagios_macros* mac, com::centreon::engine::host* hst); -int xpddefault_update_service_performance_data_file( - nagios_macros* mac, - com::centreon::engine::service* svc); -int xpddefault_update_host_performance_data_file( - nagios_macros* mac, - com::centreon::engine::host* hst); - void xpddefault_preprocess_file_templates(char* tmpl); -int xpddefault_open_host_perfdata_file(); -int xpddefault_open_service_perfdata_file(); -int xpddefault_close_host_perfdata_file(); -int xpddefault_close_service_perfdata_file(); - -int xpddefault_process_host_perfdata_file(); -int xpddefault_process_service_perfdata_file(); - #ifdef __cplusplus } #endif // C++ diff --git a/engine/src/configuration/anomalydetection.cc b/engine/src/configuration/anomalydetection.cc index a812f2992de..bb8cce78b38 100644 --- a/engine/src/configuration/anomalydetection.cc +++ b/engine/src/configuration/anomalydetection.cc @@ -21,8 +21,9 @@ #include #include #include -#include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/configuration/tag.hh" #include "com/centreon/exceptions/msg_fmt.hh" +#include "common/log_v2/log_v2.hh" extern int config_warnings; extern int config_errors; @@ -126,7 +127,7 @@ static unsigned short const default_flap_detection_options( anomalydetection::unknown | anomalydetection::critical); static unsigned int const default_freshness_threshold(0); static unsigned int const default_high_flap_threshold(0); -static unsigned int const default_initial_state(engine::service::state_ok); +static unsigned int const default_initial_state(anomalydetection::state_ok); static bool const default_is_volatile(false); static unsigned int const default_low_flap_threshold(0); static unsigned int const default_max_check_attempts(3); @@ -1775,13 +1776,13 @@ bool anomalydetection::_set_initial_state(std::string const& value) { std::string_view data(value); data = absl::StripAsciiWhitespace(data); if (data == "o" || data == "ok") - _initial_state = engine::service::state_ok; + _initial_state = anomalydetection::state_ok; else if (data == "w" || data == "warning") - _initial_state = engine::service::state_warning; + _initial_state = anomalydetection::state_warning; else if (data == "u" || data == "unknown") - _initial_state = engine::service::state_unknown; + _initial_state = anomalydetection::state_unknown; else if (data == "c" || data == "critical") - _initial_state = engine::service::state_critical; + _initial_state = anomalydetection::state_critical; else return false; return true; diff --git a/engine/src/configuration/applier/macros.cc b/engine/src/configuration/applier/macros.cc index 4fe1be01a90..78f863e09ff 100644 --- a/engine/src/configuration/applier/macros.cc +++ b/engine/src/configuration/applier/macros.cc @@ -63,8 +63,6 @@ void applier::macros::apply(configuration::state& config) { _set_macro(MACRO_RESOURCEFILE, config.resource_file().front()); _set_macro(MACRO_STATUSDATAFILE, config.status_file()); _set_macro(MACRO_RETENTIONDATAFILE, config.state_retention_file()); - _set_macro(MACRO_HOSTPERFDATAFILE, config.host_perfdata_file()); - _set_macro(MACRO_SERVICEPERFDATAFILE, config.service_perfdata_file()); _set_macro(MACRO_POLLERNAME, config.poller_name()); _set_macro(MACRO_POLLERID, std::to_string(config.poller_id())); diff --git a/engine/src/configuration/applier/scheduler.cc b/engine/src/configuration/applier/scheduler.cc index 7db63225c18..a340c5863f1 100644 --- a/engine/src/configuration/applier/scheduler.cc +++ b/engine/src/configuration/applier/scheduler.cc @@ -161,25 +161,19 @@ void applier::scheduler::apply( // Check if we need to add or modify objects into the scheduler. if (!hst_to_schedule.empty() || !svc_to_schedule.empty() || !ad_to_schedule.empty()) { - // Reset scheduling info. - // Keep data that has been set manually by the user - // (service interleave and intercheck delays). - int old_service_leave_factor = scheduling_info.service_interleave_factor; - double old_service_inter_check_delay = - scheduling_info.service_inter_check_delay; - double old_host_inter_check_delay_method = - scheduling_info.host_inter_check_delay; memset(&scheduling_info, 0, sizeof(scheduling_info)); if (config.service_interleave_factor_method() == configuration::state::ilf_user) - scheduling_info.service_interleave_factor = old_service_leave_factor; + scheduling_info.service_interleave_factor = + config.sched_info_config().service_interleave_factor; if (config.service_inter_check_delay_method() == configuration::state::icd_user) - scheduling_info.service_inter_check_delay = old_service_inter_check_delay; + scheduling_info.service_inter_check_delay = + config.sched_info_config().service_inter_check_delay; if (config.host_inter_check_delay_method() == configuration::state::icd_user) scheduling_info.host_inter_check_delay = - old_host_inter_check_delay_method; + config.sched_info_config().host_inter_check_delay; // Calculate scheduling parameters. _calculate_host_scheduling_params(); @@ -227,7 +221,6 @@ void applier::scheduler::clear() { _evt_reschedule_checks = nullptr; _evt_retention_save = nullptr; _evt_sfreshness_check = nullptr; - _evt_service_perfdata = nullptr; _evt_status_save = nullptr; _old_auto_rescheduling_interval = 0; _old_check_reaper_interval = 0; @@ -236,7 +229,6 @@ void applier::scheduler::clear() { _old_host_perfdata_file_processing_interval = 0; _old_retention_update_interval = 0; _old_service_freshness_check_interval = 0; - _old_service_perfdata_file_processing_interval = 0; _old_status_update_interval = 0; _old_host_perfdata_file_processing_command.clear(); _old_service_perfdata_file_processing_command.clear(); @@ -287,7 +279,6 @@ applier::scheduler::scheduler() _evt_reschedule_checks(nullptr), _evt_retention_save(nullptr), _evt_sfreshness_check(nullptr), - _evt_service_perfdata(nullptr), _evt_status_save(nullptr), _old_auto_rescheduling_interval(0), _old_check_reaper_interval(0), @@ -296,7 +287,6 @@ applier::scheduler::scheduler() _old_host_perfdata_file_processing_interval(0), _old_retention_update_interval(0), _old_service_freshness_check_interval(0), - _old_service_perfdata_file_processing_interval(0), _old_status_update_interval(0) {} /** @@ -416,53 +406,6 @@ void applier::scheduler::_apply_misc_event() { _config->status_update_interval()); _old_status_update_interval = _config->status_update_interval(); } - - union { - int (*func)(); - void* data; - } type; - - // Remove and add process host perfdata file. - if (!_evt_host_perfdata || - (_old_host_perfdata_file_processing_interval != - _config->host_perfdata_file_processing_interval()) || - (_old_host_perfdata_file_processing_command != - _config->host_perfdata_file_processing_command())) { - _remove_misc_event(_evt_host_perfdata); - if (_config->host_perfdata_file_processing_interval() > 0 && - !_config->host_perfdata_file_processing_command().empty()) { - type.func = &xpddefault_process_host_perfdata_file; - _evt_host_perfdata = _create_misc_event( - timed_event::EVENT_USER_FUNCTION, - now + _config->host_perfdata_file_processing_interval(), - _config->host_perfdata_file_processing_interval(), type.data); - } - _old_host_perfdata_file_processing_interval = - _config->host_perfdata_file_processing_interval(); - _old_host_perfdata_file_processing_command = - _config->host_perfdata_file_processing_command(); - } - - // Remove and add process service perfdata file. - if (!_evt_service_perfdata || - (_old_service_perfdata_file_processing_interval != - _config->service_perfdata_file_processing_interval()) || - (_old_service_perfdata_file_processing_command != - _config->service_perfdata_file_processing_command())) { - _remove_misc_event(_evt_service_perfdata); - if (_config->service_perfdata_file_processing_interval() > 0 && - !_config->service_perfdata_file_processing_command().empty()) { - type.func = &xpddefault_process_service_perfdata_file; - _evt_service_perfdata = _create_misc_event( - timed_event::EVENT_USER_FUNCTION, - now + _config->service_perfdata_file_processing_interval(), - _config->service_perfdata_file_processing_interval(), type.data); - } - _old_service_perfdata_file_processing_interval = - _config->service_perfdata_file_processing_interval(); - _old_service_perfdata_file_processing_command = - _config->service_perfdata_file_processing_command(); - } } /** @@ -905,14 +848,11 @@ void applier::scheduler::_schedule_host_events( // get current time. time_t const now(time(nullptr)); - unsigned int const end(hosts.size()); - // determine check times for host checks. int mult_factor(0); - for (unsigned int i(0); i < end; ++i) { - com::centreon::engine::host& hst(*hosts[i]); + for (auto hst_ptr : hosts) { + com::centreon::engine::host& hst = *hst_ptr; - engine_logger(dbg_events, most) << "Host '" << hst.name() << "'"; events_logger->debug("Host '{}'", hst.name()); // skip hosts that shouldn't be scheduled. diff --git a/engine/src/configuration/applier/state.cc b/engine/src/configuration/applier/state.cc index e697f92bdca..407ea98efbb 100644 --- a/engine/src/configuration/applier/state.cc +++ b/engine/src/configuration/applier/state.cc @@ -161,8 +161,6 @@ void applier::state::clear() { engine::comment::comments.clear(); engine::comment::set_next_comment_id(1llu); - xpddefault_cleanup_performance_data(); - applier::scheduler::instance().clear(); applier::macros::instance().clear(); applier::globals::instance().clear(); @@ -198,8 +196,6 @@ applier::state::~state() noexcept { engine::timeperiod::timeperiods.clear(); engine::comment::comments.clear(); engine::comment::set_next_comment_id(1llu); - - xpddefault_cleanup_performance_data(); } /** @@ -287,25 +283,7 @@ void applier::state::_apply(configuration::state const& new_cfg) { bool modify_perfdata(false); if (!has_already_been_loaded || config->host_perfdata_command() != new_cfg.host_perfdata_command() || - config->host_perfdata_file() != new_cfg.host_perfdata_file() || - config->host_perfdata_file_mode() != new_cfg.host_perfdata_file_mode() || - config->host_perfdata_file_processing_command() != - new_cfg.host_perfdata_file_processing_command() || - config->host_perfdata_file_processing_interval() != - new_cfg.host_perfdata_file_processing_interval() || - config->host_perfdata_file_template() != - new_cfg.host_perfdata_file_template() || - config->service_perfdata_command() != - new_cfg.service_perfdata_command() || - config->service_perfdata_file() != new_cfg.service_perfdata_file() || - config->service_perfdata_file_mode() != - new_cfg.service_perfdata_file_mode() || - config->service_perfdata_file_processing_command() != - new_cfg.service_perfdata_file_processing_command() || - config->service_perfdata_file_processing_interval() != - new_cfg.service_perfdata_file_processing_interval() || - config->service_perfdata_file_template() != - new_cfg.service_perfdata_file_template()) + config->service_perfdata_command() != new_cfg.service_perfdata_command()) modify_perfdata = true; // Initialize status file. @@ -315,8 +293,6 @@ void applier::state::_apply(configuration::state const& new_cfg) { modify_status = true; // Cleanup. - if (modify_perfdata) - xpddefault_cleanup_performance_data(); if (modify_status) xsddefault_cleanup_status_data(true); @@ -369,13 +345,6 @@ void applier::state::_apply(configuration::state const& new_cfg) { config->host_inter_check_delay_method( new_cfg.host_inter_check_delay_method()); config->host_perfdata_command(new_cfg.host_perfdata_command()); - config->host_perfdata_file(new_cfg.host_perfdata_file()); - config->host_perfdata_file_mode(new_cfg.host_perfdata_file_mode()); - config->host_perfdata_file_processing_command( - new_cfg.host_perfdata_file_processing_command()); - config->host_perfdata_file_processing_interval( - new_cfg.host_perfdata_file_processing_interval()); - config->host_perfdata_file_template(new_cfg.host_perfdata_file_template()); config->illegal_object_chars(new_cfg.illegal_object_chars()); config->illegal_output_chars(new_cfg.illegal_output_chars()); config->interval_length(new_cfg.interval_length()); @@ -421,14 +390,6 @@ void applier::state::_apply(configuration::state const& new_cfg) { config->service_interleave_factor_method( new_cfg.service_interleave_factor_method()); config->service_perfdata_command(new_cfg.service_perfdata_command()); - config->service_perfdata_file(new_cfg.service_perfdata_file()); - config->service_perfdata_file_mode(new_cfg.service_perfdata_file_mode()); - config->service_perfdata_file_processing_command( - new_cfg.service_perfdata_file_processing_command()); - config->service_perfdata_file_processing_interval( - new_cfg.service_perfdata_file_processing_interval()); - config->service_perfdata_file_template( - new_cfg.service_perfdata_file_template()); config->sleep_time(new_cfg.sleep_time()); config->soft_state_dependencies(new_cfg.soft_state_dependencies()); config->state_retention_file(new_cfg.state_retention_file()); diff --git a/engine/src/configuration/host.cc b/engine/src/configuration/host.cc index 7c3965036e5..6d799d4cc32 100644 --- a/engine/src/configuration/host.cc +++ b/engine/src/configuration/host.cc @@ -22,7 +22,7 @@ #include "absl/strings/numbers.h" #include "absl/strings/str_split.h" #include "absl/strings/string_view.h" -#include "com/centreon/engine/globals.hh" +#include "com/centreon/engine/configuration/tag.hh" #include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; @@ -119,8 +119,7 @@ static unsigned short const default_flap_detection_options(host::up | host::unreachable); static unsigned int const default_freshness_threshold(0); static unsigned int const default_high_flap_threshold(0); -static unsigned short const default_initial_state( - com::centreon::engine::host::state_up); +static unsigned short const default_initial_state(host::state_up); static unsigned int const default_low_flap_threshold(0); static unsigned int const default_max_check_attempts(3); static bool const default_notifications_enabled(true); @@ -1268,7 +1267,7 @@ bool host::_set_event_handler_enabled(bool value) { * @return True on success, otherwise false. */ bool host::_set_failure_prediction_enabled(bool value [[maybe_unused]]) { - config_logger->warn( + _logger->warn( "Warning: host failure_prediction_enabled is deprecated This option will " "not be supported in 20.04."); ++config_warnings; @@ -1284,7 +1283,7 @@ bool host::_set_failure_prediction_enabled(bool value [[maybe_unused]]) { */ bool host::_set_failure_prediction_options(std::string const& value [[maybe_unused]]) { - config_logger->warn( + _logger->warn( "Warning: service failure_prediction_options is deprecated This option " "will not be supported in 20.04."); ++config_warnings; @@ -1439,11 +1438,11 @@ bool host::_set_initial_state(std::string const& value) { std::string_view data(value); data = absl::StripAsciiWhitespace(data); if (data == "o" || data == "up") - _initial_state = engine::host::state_up; + _initial_state = host::state_up; else if (data == "d" || data == "down") - _initial_state = engine::host::state_down; + _initial_state = host::state_down; else if (data == "u" || data == "unreachable") - _initial_state = engine::host::state_unreachable; + _initial_state = host::state_unreachable; else return false; return true; @@ -1731,8 +1730,8 @@ bool host::_set_category_tags(const std::string& value) { if (parse_ok) { _tags.emplace(id, tag::hostcategory); } else { - config_logger->warn("Warning: host ({}) error for parsing tag {}", - _host_id, value); + _logger->warn("Warning: host ({}) error for parsing tag {}", _host_id, + value); ret = false; } } @@ -1765,8 +1764,8 @@ bool host::_set_group_tags(const std::string& value) { if (parse_ok) { _tags.emplace(id, tag::hostgroup); } else { - config_logger->warn("Warning: host ({}) error for parsing tag {}", - _host_id, value); + _logger->warn("Warning: host ({}) error for parsing tag {}", _host_id, + value); ret = false; } } diff --git a/engine/src/configuration/hostdependency.cc b/engine/src/configuration/hostdependency.cc index 1681081cbb1..cd8ef676fb6 100644 --- a/engine/src/configuration/hostdependency.cc +++ b/engine/src/configuration/hostdependency.cc @@ -19,7 +19,6 @@ */ #include "com/centreon/engine/configuration/hostdependency.hh" -#include "com/centreon/engine/globals.hh" #include "com/centreon/exceptions/msg_fmt.hh" extern int config_warnings; @@ -197,7 +196,7 @@ void hostdependency::check_validity(error_info* err) const { std::string dependend_host_name(!_dependent_hosts->empty() ? *_dependent_hosts->begin() : *_dependent_hostgroups->begin()); - config_logger->warn( + _logger->warn( "Warning: Ignoring lame host dependency of '{}' on host/hostgroups " "'{}'.", dependend_host_name, host_name); diff --git a/engine/src/configuration/parser.cc b/engine/src/configuration/parser.cc index 59c80a3c6af..9152103bd37 100644 --- a/engine/src/configuration/parser.cc +++ b/engine/src/configuration/parser.cc @@ -18,8 +18,6 @@ */ #include "com/centreon/engine/configuration/parser.hh" -#include "com/centreon/engine/exceptions/error.hh" -// #include "com/centreon/engine/globals.hh" #include "com/centreon/exceptions/error.hh" #include "com/centreon/exceptions/msg_fmt.hh" #include "com/centreon/io/directory_entry.hh" diff --git a/engine/src/configuration/state.cc b/engine/src/configuration/state.cc index 9ba8ba15612..ece56fdd5c0 100644 --- a/engine/src/configuration/state.cc +++ b/engine/src/configuration/state.cc @@ -20,7 +20,6 @@ #include #include "com/centreon/common/rapidjson_helper.hh" #include "com/centreon/engine/configuration/logging.hh" -// #include "com/centreon/engine/globals.hh" #include "com/centreon/io/file_entry.hh" #include "common/log_v2/log_v2.hh" #include "compatibility/locations.h" @@ -383,13 +382,6 @@ static unsigned int const default_host_check_timeout(30); static unsigned int const default_host_freshness_check_interval(60); static state::inter_check_delay const default_host_inter_check_delay_method( state::icd_smart); -static state::perfdata_file_mode const default_host_perfdata_file_mode( - state::mode_pipe); -static unsigned int const default_host_perfdata_file_processing_interval(0); -static std::string const default_host_perfdata_file_template( - "[HOSTPERFDATA]\t$TIMET$\t$HOSTNAME$\t$HOSTEXECUTIONTIME$\t$HOSTOUTPUT$" - "\t$" - "HOSTPERFDATA$"); static std::string const default_illegal_object_chars(""); static std::string const default_illegal_output_chars("`~$&|'\"<>"); static unsigned int const default_interval_length(60); @@ -431,13 +423,6 @@ static state::inter_check_delay const default_service_inter_check_delay_method( state::icd_smart); static state::interleave_factor const default_service_interleave_factor_method( state::ilf_smart); -static state::perfdata_file_mode const default_service_perfdata_file_mode( - state::mode_pipe); -static unsigned int const default_service_perfdata_file_processing_interval(0); -static std::string const default_service_perfdata_file_template( - "[SERVICEPERFDATA]\t$TIMET$\t$HOSTNAME$\t$SERVICEDESC$\t$" - "SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$" - "SERVICEPERFDATA$"); static float const default_sleep_time(0.5); static bool const default_soft_state_dependencies(false); static std::string const default_state_retention_file(DEFAULT_RETENTION_FILE); @@ -522,10 +507,6 @@ state::state() _host_check_timeout(default_host_check_timeout), _host_freshness_check_interval(default_host_freshness_check_interval), _host_inter_check_delay_method(default_host_inter_check_delay_method), - _host_perfdata_file_mode(default_host_perfdata_file_mode), - _host_perfdata_file_processing_interval( - default_host_perfdata_file_processing_interval), - _host_perfdata_file_template(default_host_perfdata_file_template), _illegal_object_chars(default_illegal_object_chars), _illegal_output_chars(default_illegal_output_chars), _interval_length(default_interval_length), @@ -576,10 +557,6 @@ state::state() default_service_inter_check_delay_method), _service_interleave_factor_method( default_service_interleave_factor_method), - _service_perfdata_file_mode(default_service_perfdata_file_mode), - _service_perfdata_file_processing_interval( - default_service_perfdata_file_processing_interval), - _service_perfdata_file_template(default_service_perfdata_file_template), _sleep_time(default_sleep_time), _soft_state_dependencies(default_soft_state_dependencies), _state_retention_file(default_state_retention_file), @@ -696,13 +673,6 @@ state& state::operator=(state const& right) { _host_freshness_check_interval = right._host_freshness_check_interval; _host_inter_check_delay_method = right._host_inter_check_delay_method; _host_perfdata_command = right._host_perfdata_command; - _host_perfdata_file = right._host_perfdata_file; - _host_perfdata_file_mode = right._host_perfdata_file_mode; - _host_perfdata_file_processing_command = - right._host_perfdata_file_processing_command; - _host_perfdata_file_processing_interval = - right._host_perfdata_file_processing_interval; - _host_perfdata_file_template = right._host_perfdata_file_template; _illegal_object_chars = right._illegal_object_chars; _illegal_output_chars = right._illegal_output_chars; _interval_length = right._interval_length; @@ -755,13 +725,6 @@ state& state::operator=(state const& right) { _service_inter_check_delay_method = right._service_inter_check_delay_method; _service_interleave_factor_method = right._service_interleave_factor_method; _service_perfdata_command = right._service_perfdata_command; - _service_perfdata_file = right._service_perfdata_file; - _service_perfdata_file_mode = right._service_perfdata_file_mode; - _service_perfdata_file_processing_command = - right._service_perfdata_file_processing_command; - _service_perfdata_file_processing_interval = - right._service_perfdata_file_processing_interval; - _service_perfdata_file_template = right._service_perfdata_file_template; _sleep_time = right._sleep_time; _soft_state_dependencies = right._soft_state_dependencies; _state_retention_file = right._state_retention_file; @@ -863,13 +826,6 @@ bool state::operator==(state const& right) const noexcept { _host_freshness_check_interval == right._host_freshness_check_interval && _host_inter_check_delay_method == right._host_inter_check_delay_method && _host_perfdata_command == right._host_perfdata_command && - _host_perfdata_file == right._host_perfdata_file && - _host_perfdata_file_mode == right._host_perfdata_file_mode && - _host_perfdata_file_processing_command == - right._host_perfdata_file_processing_command && - _host_perfdata_file_processing_interval == - right._host_perfdata_file_processing_interval && - _host_perfdata_file_template == right._host_perfdata_file_template && _illegal_object_chars == right._illegal_object_chars && _illegal_output_chars == right._illegal_output_chars && _interval_length == right._interval_length && @@ -922,14 +878,6 @@ bool state::operator==(state const& right) const noexcept { _service_interleave_factor_method == right._service_interleave_factor_method && _service_perfdata_command == right._service_perfdata_command && - _service_perfdata_file == right._service_perfdata_file && - _service_perfdata_file_mode == right._service_perfdata_file_mode && - _service_perfdata_file_processing_command == - right._service_perfdata_file_processing_command && - _service_perfdata_file_processing_interval == - right._service_perfdata_file_processing_interval && - _service_perfdata_file_template == - right._service_perfdata_file_template && _sleep_time == right._sleep_time && _soft_state_dependencies == right._soft_state_dependencies && _state_retention_file == right._state_retention_file && @@ -2274,50 +2222,16 @@ void state::host_perfdata_command(std::string const& value) { _host_perfdata_command = value; } -/** - * Get host_perfdata_file value. - * - * @return The host_perfdata_file value. - */ -std::string const& state::host_perfdata_file() const noexcept { - return _host_perfdata_file; -} - /** * Set host_perfdata_file value. * * @param[in] value The new host_perfdata_file value. */ void state::host_perfdata_file(std::string const& value) { - _host_perfdata_file = value; -} - -/** - * Get host_perfdata_file_mode value. - * - * @return The host_perfdata_file_mode value. - */ -state::perfdata_file_mode state::host_perfdata_file_mode() const noexcept { - return _host_perfdata_file_mode; -} - -/** - * Set host_perfdata_file_mode value. - * - * @param[in] value The new host_perfdata_file_mode value. - */ -void state::host_perfdata_file_mode(perfdata_file_mode value) { - _host_perfdata_file_mode = value; -} - -/** - * Get host_perfdata_file_processing_command value. - * - * @return The host_perfdata_file_processing_command value. - */ -std::string const& state::host_perfdata_file_processing_command() - const noexcept { - return _host_perfdata_file_processing_command; + _config_logger->warn( + "warning: host_perfdata_file is no more used for a long time, you " + "should not use it anymore."); + _err.config_warnings++; } /** @@ -2326,16 +2240,10 @@ std::string const& state::host_perfdata_file_processing_command() * @param[in] value The new host_perfdata_file_processing_command value. */ void state::host_perfdata_file_processing_command(std::string const& value) { - _host_perfdata_file_processing_command = value; -} - -/** - * Get host_perfdata_file_processing_interval value. - * - * @return The host_perfdata_file_processing_interval value. - */ -unsigned int state::host_perfdata_file_processing_interval() const noexcept { - return _host_perfdata_file_processing_interval; + _config_logger->warn( + "warning: host_perfdata_file_processing_command is no more used for a " + "long time, you should not use it anymore."); + _err.config_warnings++; } /** @@ -2344,16 +2252,10 @@ unsigned int state::host_perfdata_file_processing_interval() const noexcept { * @param[in] value The new host_perfdata_file_processing_interval value. */ void state::host_perfdata_file_processing_interval(unsigned int value) { - _host_perfdata_file_processing_interval = value; -} - -/** - * Get host_perfdata_file_template value. - * - * @return The host_perfdata_file_template value. - */ -std::string const& state::host_perfdata_file_template() const noexcept { - return _host_perfdata_file_template; + _config_logger->warn( + "warning: host_perfdata_file_processing_interval is no more used for a " + "long time, you should not use it anymore."); + _err.config_warnings++; } /** @@ -2362,7 +2264,10 @@ std::string const& state::host_perfdata_file_template() const noexcept { * @param[in] value The new host_perfdata_file_template value. */ void state::host_perfdata_file_template(std::string const& value) { - _host_perfdata_file_template = value; + _config_logger->warn( + "warning: host_perfdata_file_template is no more used for a long time, " + "you should not use it anymore."); + _err.config_warnings++; } /** @@ -3441,15 +3346,6 @@ void state::service_perfdata_command(std::string const& value) { _service_perfdata_command = value; } -/** - * Get service_perfdata_file value. - * - * @return The service_perfdata_file value. - */ -std::string const& state::service_perfdata_file() const noexcept { - return _service_perfdata_file; -} - /** * Set service_perfdata_file value. * @@ -3457,37 +3353,9 @@ std::string const& state::service_perfdata_file() const noexcept { */ void state::service_perfdata_file(std::string const& value) { _config_logger->warn( - "Warning: service_perfdata_command is no more used for a long time, you " + "Warning: service_perfdata_file is no more used for a long time, you " "should not use it anymore."); - _service_perfdata_file = value; -} - -/** - * Get service_perfdata_file_mode value. - * - * @return The service_perfdata_file_mode value. - */ -state::perfdata_file_mode state::service_perfdata_file_mode() const noexcept { - return _service_perfdata_file_mode; -} - -/** - * Set service_perfdata_file_mode value. - * - * @param[in] value The new service_perfdata_file_mode value. - */ -void state::service_perfdata_file_mode(perfdata_file_mode value) { - _service_perfdata_file_mode = value; -} - -/** - * Get service_perfdata_file_processing_command value. - * - * @return The service_perfdata_file_processing_command value. - */ -std::string const& state::service_perfdata_file_processing_command() - const noexcept { - return _service_perfdata_file_processing_command; + _err.config_warnings++; } /** @@ -3496,16 +3364,10 @@ std::string const& state::service_perfdata_file_processing_command() * @param[in] value The new service_perfdata_file_processing_command value. */ void state::service_perfdata_file_processing_command(std::string const& value) { - _service_perfdata_file_processing_command = value; -} - -/** - * Get service_perfdata_file_processing_interval value. - * - * @return The service_perfdata_file_processing_interval value. - */ -unsigned int state::service_perfdata_file_processing_interval() const noexcept { - return _service_perfdata_file_processing_interval; + _config_logger->warn( + "warning: service_perfdata_file_processing_command is no more used for a " + "long time, you should not use it anymore."); + _err.config_warnings++; } /** @@ -3513,17 +3375,12 @@ unsigned int state::service_perfdata_file_processing_interval() const noexcept { * * @param[in] value The new service_perfdata_file_processing_interval value. */ -void state::service_perfdata_file_processing_interval(unsigned int value) { - _service_perfdata_file_processing_interval = value; -} - -/** - * Get service_perfdata_file_template value. - * - * @return The service_perfdata_file_template value. - */ -std::string const& state::service_perfdata_file_template() const noexcept { - return _service_perfdata_file_template; +void state::service_perfdata_file_processing_interval(unsigned int value + [[maybe_unused]]) { + _config_logger->warn( + "Warning: service_perfdata_file_processing_interval is no more used for " + "a long time, you should not use it anymore."); + _err.config_warnings++; } /** @@ -3532,7 +3389,10 @@ std::string const& state::service_perfdata_file_template() const noexcept { * @param[in] value The new service_perfdata_file_template value. */ void state::service_perfdata_file_template(std::string const& value) { - _service_perfdata_file_template = value; + _config_logger->warn( + "Warning: service_perfdata_file_template is no more used for a long " + "time, you should not use it anymore."); + _err.config_warnings++; } /** @@ -4531,12 +4391,10 @@ void state::_set_host_inter_check_delay_method(std::string const& value) { * @param[in] value The new host_inter_check_delay_method value. */ void state::_set_host_perfdata_file_mode(std::string const& value) { - if (value == "p") - _host_perfdata_file_mode = mode_pipe; - else if (value == "w") - _host_perfdata_file_mode = mode_file; - else - _host_perfdata_file_mode = mode_file_append; + _config_logger->warn( + "Warning: host_perfdata_file_mode is no more used for a long time, you " + "should not use it anymore."); + _err.config_warnings++; } /** @@ -4726,12 +4584,10 @@ void state::_set_service_interleave_factor_method(std::string const& value) { * @param[in] value The new service_inter_check_delay_method value. */ void state::_set_service_perfdata_file_mode(std::string const& value) { - if (value == "p") - _service_perfdata_file_mode = mode_pipe; - else if (value == "w") - _service_perfdata_file_mode = mode_file; - else - _service_perfdata_file_mode = mode_file_append; + _config_logger->warn( + "warning: service_perfdata_file_mode is no more used for a long time, " + "you should not use it anymore."); + _err.config_warnings++; } /** diff --git a/engine/src/macros.cc b/engine/src/macros.cc index 673b76b8856..2087b5dd428 100644 --- a/engine/src/macros.cc +++ b/engine/src/macros.cc @@ -786,8 +786,6 @@ int init_macrox_names() { add_macrox_name(LOGFILE); add_macrox_name(RESOURCEFILE); add_macrox_name(COMMANDFILE); - add_macrox_name(HOSTPERFDATAFILE); - add_macrox_name(SERVICEPERFDATAFILE); add_macrox_name(HOSTACTIONURL); add_macrox_name(HOSTNOTESURL); add_macrox_name(HOSTNOTES); @@ -916,8 +914,6 @@ int clear_volatile_macros_r(nagios_macros* mac) { case MACRO_LOGFILE: case MACRO_RESOURCEFILE: case MACRO_COMMANDFILE: - case MACRO_HOSTPERFDATAFILE: - case MACRO_SERVICEPERFDATAFILE: case MACRO_PROCESSSTARTTIME: case MACRO_TEMPPATH: case MACRO_EVENTSTARTTIME: diff --git a/engine/src/macros/grab_value.cc b/engine/src/macros/grab_value.cc index 4db663943a7..bf170ccdd40 100644 --- a/engine/src/macros/grab_value.cc +++ b/engine/src/macros/grab_value.cc @@ -852,22 +852,12 @@ struct grab_value_redirection { routines[datetime_ids[i]] = &handle_datetime_macro; // Static macros. - static unsigned int const static_ids[] = {MACRO_ADMINEMAIL, - MACRO_ADMINPAGER, - MACRO_MAINCONFIGFILE, - MACRO_STATUSDATAFILE, - MACRO_RETENTIONDATAFILE, - MACRO_TEMPFILE, - MACRO_LOGFILE, - MACRO_RESOURCEFILE, - MACRO_COMMANDFILE, - MACRO_HOSTPERFDATAFILE, - MACRO_SERVICEPERFDATAFILE, - MACRO_PROCESSSTARTTIME, - MACRO_TEMPPATH, - MACRO_EVENTSTARTTIME, - MACRO_POLLERNAME, - MACRO_POLLERID}; + static unsigned int const static_ids[] = { + MACRO_ADMINEMAIL, MACRO_ADMINPAGER, MACRO_MAINCONFIGFILE, + MACRO_STATUSDATAFILE, MACRO_RETENTIONDATAFILE, MACRO_TEMPFILE, + MACRO_LOGFILE, MACRO_RESOURCEFILE, MACRO_COMMANDFILE, + MACRO_PROCESSSTARTTIME, MACRO_TEMPPATH, MACRO_EVENTSTARTTIME, + MACRO_POLLERNAME, MACRO_POLLERID}; for (unsigned int i = 0; i < sizeof(static_ids) / sizeof(*static_ids); ++i) routines[static_ids[i]] = &handle_static_macro; diff --git a/engine/src/xpddefault.cc b/engine/src/xpddefault.cc index 3aeaa79adc7..129903e8b55 100644 --- a/engine/src/xpddefault.cc +++ b/engine/src/xpddefault.cc @@ -38,22 +38,6 @@ using namespace com::centreon::engine::logging; static commands::command* xpddefault_host_perfdata_command_ptr(nullptr); static commands::command* xpddefault_service_perfdata_command_ptr(nullptr); -static char* xpddefault_host_perfdata_file_template(nullptr); -static char* xpddefault_service_perfdata_file_template(nullptr); - -static commands::command* xpddefault_host_perfdata_file_processing_command_ptr( - nullptr); -static commands::command* - xpddefault_service_perfdata_file_processing_command_ptr(nullptr); - -static FILE* xpddefault_host_perfdata_fp(nullptr); -static FILE* xpddefault_service_perfdata_fp(nullptr); -static int xpddefault_host_perfdata_fd(-1); -static int xpddefault_service_perfdata_fd(-1); - -static pthread_mutex_t xpddefault_host_perfdata_fp_lock; -static pthread_mutex_t xpddefault_service_perfdata_fp_lock; - /******************************************************************/ /************** INITIALIZATION & CLEANUP FUNCTIONS ****************/ /******************************************************************/ @@ -65,23 +49,6 @@ int xpddefault_initialize_performance_data() { // reset vars. xpddefault_host_perfdata_command_ptr = nullptr; xpddefault_service_perfdata_command_ptr = nullptr; - xpddefault_host_perfdata_file_processing_command_ptr = nullptr; - xpddefault_service_perfdata_file_processing_command_ptr = nullptr; - - // grab config info from main config file. - xpddefault_host_perfdata_file_template = - string::dup(config->host_perfdata_file_template()); - xpddefault_service_perfdata_file_template = - string::dup(config->service_perfdata_file_template()); - - // process special chars in templates. - xpddefault_preprocess_file_templates(xpddefault_host_perfdata_file_template); - xpddefault_preprocess_file_templates( - xpddefault_service_perfdata_file_template); - - // open the performance data files. - xpddefault_open_host_perfdata_file(); - xpddefault_open_service_perfdata_file(); // verify that performance data commands are valid. if (!config->host_perfdata_command().empty()) { @@ -136,77 +103,6 @@ int xpddefault_initialize_performance_data() { delete[] temp_buffer; } - if (!config->host_perfdata_file_processing_command().empty()) { - char* temp_buffer = - string::dup(config->host_perfdata_file_processing_command()); - - // get the command name, leave any arguments behind. - temp_command_name = my_strtok(temp_buffer, "!"); - command_map::iterator cmd_found = - commands::command::commands.find(temp_command_name); - - if (cmd_found == commands::command::commands.end() || !cmd_found->second) { - engine_logger(log_runtime_warning, basic) - << "Warning: Host performance file processing command '" - << temp_command_name - << "' was not found - host performance " - "data file will not be processed!"; - runtime_logger->warn( - "Warning: Host performance file processing command '{}' was not " - "found - host performance " - "data file will not be processed!", - temp_command_name); - } else - xpddefault_host_perfdata_file_processing_command_ptr = - cmd_found->second.get(); - - // free memory. - delete[] temp_buffer; - } - - if (!config->service_perfdata_file_processing_command().empty()) { - char* temp_buffer = - string::dup(config->service_perfdata_file_processing_command()); - - // get the command name, leave any arguments behind. - temp_command_name = my_strtok(temp_buffer, "!"); - command_map::iterator cmd_found = - commands::command::commands.find(temp_command_name); - - if (cmd_found == commands::command::commands.end() || !cmd_found->second) { - engine_logger(log_runtime_warning, basic) - << "Warning: Service performance file processing command '" - << temp_command_name - << "' was not found - service performance " - "data file will not be processed!"; - runtime_logger->warn( - "Warning: Service performance file processing command '{}' was not " - "found - service performance " - "data file will not be processed!", - temp_command_name); - } else - xpddefault_service_perfdata_file_processing_command_ptr = - cmd_found->second.get(); - // free memory. - delete[] temp_buffer; - } - - return OK; -} - -// cleans up performance data. -int xpddefault_cleanup_performance_data() { - // free memory. - delete[] xpddefault_host_perfdata_file_template; - delete[] xpddefault_service_perfdata_file_template; - - xpddefault_host_perfdata_file_template = nullptr; - xpddefault_service_perfdata_file_template = nullptr; - - // close the files. - xpddefault_close_host_perfdata_file(); - xpddefault_close_service_perfdata_file(); - return OK; } @@ -225,9 +121,7 @@ int xpddefault_update_service_performance_data( */ if (!svc || svc->get_perf_data().empty()) return OK; - if ((!xpddefault_service_perfdata_fp || - !xpddefault_service_perfdata_file_template) && - config->service_perfdata_command().empty()) + if (config->service_perfdata_command().empty()) return OK; grab_host_macros_r(mac, svc->get_host_ptr()); @@ -239,9 +133,6 @@ int xpddefault_update_service_performance_data( // get rid of used memory we won't need anymore. clear_argv_macros_r(mac); - // update the performance data file. - xpddefault_update_service_performance_data_file(mac, svc); - // now free() it all. clear_volatile_macros_r(mac); @@ -258,10 +149,6 @@ int xpddefault_update_host_performance_data(host* hst) { */ if (!hst || !hst->get_perf_data().empty()) return OK; - if ((!xpddefault_host_perfdata_fp || - !xpddefault_host_perfdata_file_template) && - config->host_perfdata_command().empty()) - return OK; // set up macros and get to work. grab_host_macros_r(mac, hst); @@ -272,9 +159,6 @@ int xpddefault_update_host_performance_data(host* hst) { // no more commands to run, so we won't need this any more. clear_argv_macros_r(mac); - // update the performance data file. - xpddefault_update_host_performance_data_file(mac, hst); - // free() all. clear_volatile_macros_r(mac); @@ -434,103 +318,6 @@ int xpddefault_run_host_performance_data_command(nagios_macros* mac, return result; } -/******************************************************************/ -/**************** FILE PERFORMANCE DATA FUNCTIONS *****************/ -/******************************************************************/ - -// open the host performance data file for writing. -int xpddefault_open_host_perfdata_file() { - if (!config->host_perfdata_file().empty()) { - if (config->host_perfdata_file_mode() == configuration::state::mode_pipe) { - // must open read-write to avoid failure if the other end isn't ready yet. - xpddefault_host_perfdata_fd = - open(config->host_perfdata_file().c_str(), O_NONBLOCK | O_RDWR); - xpddefault_host_perfdata_fp = fdopen(xpddefault_host_perfdata_fd, "w"); - } else - xpddefault_host_perfdata_fp = fopen( - config->host_perfdata_file().c_str(), - (config->host_perfdata_file_mode() == configuration::state::mode_file) - ? "w" - : "a"); - - if (xpddefault_host_perfdata_fp == nullptr) { - engine_logger(log_runtime_warning, basic) - << "Warning: File '" << xpddefault_host_perfdata_fp - << "' could not be opened - host performance data will not " - "be written to file!"; - runtime_logger->warn( - "Warning: File '{}' could not be opened - host performance data will " - "not " - "be written to file!", - (void*)xpddefault_host_perfdata_fp); - - return ERROR; - } - } - - return OK; -} - -// open the service performance data file for writing. -int xpddefault_open_service_perfdata_file() { - if (!config->service_perfdata_file().empty()) { - if (config->service_perfdata_file_mode() == - configuration::state::mode_pipe) { - // must open read-write to avoid failure if the other end isn't ready yet. - xpddefault_service_perfdata_fd = - open(config->service_perfdata_file().c_str(), O_NONBLOCK | O_RDWR); - xpddefault_service_perfdata_fp = - fdopen(xpddefault_service_perfdata_fd, "w"); - } else - xpddefault_service_perfdata_fp = - fopen(config->service_perfdata_file().c_str(), - (config->service_perfdata_file_mode() == - configuration::state::mode_file) - ? "w" - : "a"); - - if (xpddefault_service_perfdata_fp == nullptr) { - engine_logger(log_runtime_warning, basic) - << "Warning: File '" << config->service_perfdata_file() - << "' could not be opened - service performance data will not " - "be written to file!"; - runtime_logger->warn( - "Warning: File '{}' could not be opened - service performance data " - "will not " - "be written to file!", - config->service_perfdata_file()); - - return ERROR; - } - } - - return OK; -} - -// close the host performance data file. -int xpddefault_close_host_perfdata_file() { - if (xpddefault_host_perfdata_fp != nullptr) - fclose(xpddefault_host_perfdata_fp); - if (xpddefault_host_perfdata_fd >= 0) { - close(xpddefault_host_perfdata_fd); - xpddefault_host_perfdata_fd = -1; - } - - return OK; -} - -// close the service performance data file. -int xpddefault_close_service_perfdata_file() { - if (xpddefault_service_perfdata_fp != nullptr) - fclose(xpddefault_service_perfdata_fp); - if (xpddefault_service_perfdata_fd >= 0) { - close(xpddefault_service_perfdata_fd); - xpddefault_service_perfdata_fd = -1; - } - - return OK; -} - // processes delimiter characters in templates. void xpddefault_preprocess_file_templates(char* tmpl) { if (!tmpl) @@ -561,276 +348,3 @@ void xpddefault_preprocess_file_templates(char* tmpl) { } *tmp2 = 0; } - -// updates service performance data file. -int xpddefault_update_service_performance_data_file( - nagios_macros* mac, - com::centreon::engine::service* svc) { - std::string raw_output; - std::string processed_output; - int result(OK); - - engine_logger(dbg_functions, basic) - << "update_service_performance_data_file()"; - functions_logger->trace("update_service_performance_data_file()"); - - if (svc == nullptr) - return ERROR; - - // we don't have a file to write to. - if (xpddefault_service_perfdata_fp == nullptr || - xpddefault_service_perfdata_file_template == nullptr) - return OK; - - // get the raw line to write. - raw_output = xpddefault_service_perfdata_file_template; - - engine_logger(dbg_perfdata, most) - << "Raw service performance data file output: " << raw_output; - commands_logger->info("Raw service performance data file output: {}", - raw_output); - - // process any macros in the raw output line. - process_macros_r(mac, raw_output, processed_output, 0); - if (processed_output.empty()) - return ERROR; - - engine_logger(dbg_perfdata, most) - << "Processed service performance data file output: " << processed_output; - commands_logger->info("Processed service performance data file output: {}", - processed_output); - - // lock, write to and unlock host performance data file. - pthread_mutex_lock(&xpddefault_service_perfdata_fp_lock); - fputs(processed_output.c_str(), xpddefault_service_perfdata_fp); - fputc('\n', xpddefault_service_perfdata_fp); - fflush(xpddefault_service_perfdata_fp); - pthread_mutex_unlock(&xpddefault_service_perfdata_fp_lock); - - return result; -} - -// updates host performance data file. -int xpddefault_update_host_performance_data_file(nagios_macros* mac, - host* hst) { - std::string raw_output; - std::string processed_output; - int result(OK); - - engine_logger(dbg_functions, basic) << "update_host_performance_data_file()"; - functions_logger->trace("update_host_performance_data_file()"); - - if (hst == nullptr) - return ERROR; - - // we don't have a host perfdata file. - if (xpddefault_host_perfdata_fp == nullptr || - xpddefault_host_perfdata_file_template == nullptr) - return OK; - - // get the raw output. - raw_output = string::dup(xpddefault_host_perfdata_file_template); - - engine_logger(dbg_perfdata, most) - << "Raw host performance file output: " << raw_output; - commands_logger->info("Raw host performance file output: {}", raw_output); - - // process any macros in the raw output. - process_macros_r(mac, raw_output, processed_output, 0); - if (processed_output.empty()) - return ERROR; - - engine_logger(dbg_perfdata, most) - << "Processed host performance data file output: " << processed_output; - commands_logger->info("Processed host performance data file output: {}", - processed_output); - - // lock, write to and unlock host performance data file. - pthread_mutex_lock(&xpddefault_host_perfdata_fp_lock); - fputs(processed_output.c_str(), xpddefault_host_perfdata_fp); - fputc('\n', xpddefault_host_perfdata_fp); - fflush(xpddefault_host_perfdata_fp); - pthread_mutex_unlock(&xpddefault_host_perfdata_fp_lock); - - return result; -} - -// periodically process the host perf data file. -int xpddefault_process_host_perfdata_file() { - std::string raw_command_line; - std::string processed_command_line; - bool early_timeout = false; - double exectime(0.0); - int result(OK); - int macro_options(STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS); - nagios_macros* mac(get_global_macros()); - - engine_logger(dbg_functions, basic) << "process_host_perfdata_file()"; - functions_logger->trace("process_host_perfdata_file()"); - - // we don't have a command. - if (config->host_perfdata_file_processing_command().empty()) - return OK; - - // get the raw command line. - get_raw_command_line_r( - mac, xpddefault_host_perfdata_file_processing_command_ptr, - config->host_perfdata_file_processing_command().c_str(), raw_command_line, - macro_options); - if (raw_command_line.empty()) { - clear_volatile_macros_r(mac); - return ERROR; - } - - engine_logger(dbg_perfdata, most) - << "Raw host performance data file processing command line: " - << raw_command_line; - commands_logger->info( - "Raw host performance data file processing command line: {}", - raw_command_line); - - // process any macros in the raw command line. - process_macros_r(mac, raw_command_line, processed_command_line, - macro_options); - if (processed_command_line.empty()) { - clear_volatile_macros_r(mac); - return ERROR; - } - - engine_logger(dbg_perfdata, most) - << "Processed host performance data file processing command " - "line: " - << processed_command_line; - commands_logger->info( - "Processed host performance data file processing command line: {}", - processed_command_line); - - // lock and close the performance data file. - pthread_mutex_lock(&xpddefault_host_perfdata_fp_lock); - xpddefault_close_host_perfdata_file(); - - // run the command. - try { - std::string tmp; - my_system_r(mac, processed_command_line, config->perfdata_timeout(), - &early_timeout, &exectime, tmp, 0); - } catch (std::exception const& e) { - engine_logger(log_runtime_error, basic) - << "Error: can't execute host performance data file processing command " - "line '" - << processed_command_line << "' : " << e.what(); - runtime_logger->error( - "Error: can't execute host performance data file processing command " - "line '{}' : {}", - processed_command_line, e.what()); - } - clear_volatile_macros_r(mac); - - // re-open and unlock the performance data file. - xpddefault_open_host_perfdata_file(); - pthread_mutex_unlock(&xpddefault_host_perfdata_fp_lock); - - // check to see if the command timed out. - if (early_timeout) - engine_logger(log_runtime_warning, basic) - << "Warning: Host performance data file processing command '" - << processed_command_line << "' timed out after " - << config->perfdata_timeout() << " seconds"; - runtime_logger->warn( - "Warning: Host performance data file processing command '{}' timed out " - "after {} seconds", - processed_command_line, config->perfdata_timeout()); - return result; -} - -// periodically process the service perf data file. -int xpddefault_process_service_perfdata_file() { - std::string raw_command_line; - std::string processed_command_line; - bool early_timeout = false; - double exectime(0.0); - int result(OK); - int macro_options(STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS); - nagios_macros* mac(get_global_macros()); - - engine_logger(dbg_functions, basic) << "process_service_perfdata_file()"; - functions_logger->trace("process_service_perfdata_file()"); - - // we don't have a command. - if (config->service_perfdata_file_processing_command().empty()) - return OK; - - // get the raw command line. - get_raw_command_line_r( - mac, xpddefault_service_perfdata_file_processing_command_ptr, - config->service_perfdata_file_processing_command().c_str(), - raw_command_line, macro_options); - if (raw_command_line.empty()) { - clear_volatile_macros_r(mac); - return ERROR; - } - - engine_logger(dbg_perfdata, most) - << "Raw service performance data file processing " - "command line: " - << raw_command_line; - commands_logger->info( - "Raw service performance data file processing command line: {}", - raw_command_line); - - // process any macros in the raw command line. - process_macros_r(mac, raw_command_line, processed_command_line, - macro_options); - if (processed_command_line.empty()) { - clear_volatile_macros_r(mac); - return ERROR; - } - - engine_logger(dbg_perfdata, most) - << "Processed service performance data file processing " - "command line: " - << processed_command_line; - commands_logger->info( - "Processed service performance data file processing command line: {}", - processed_command_line); - - // lock and close the performance data file. - pthread_mutex_lock(&xpddefault_service_perfdata_fp_lock); - xpddefault_close_service_perfdata_file(); - - // run the command. - try { - std::string tmp; - my_system_r(mac, processed_command_line, config->perfdata_timeout(), - &early_timeout, &exectime, tmp, 0); - } catch (std::exception const& e) { - engine_logger(log_runtime_error, basic) - << "Error: can't execute service performance data file processing " - "command line '" - << processed_command_line << "' : " << e.what(); - runtime_logger->error( - "Error: can't execute service performance data file processing " - "command line '{}' : {}", - processed_command_line, e.what()); - } - - // re-open and unlock the performance data file. - xpddefault_open_service_perfdata_file(); - pthread_mutex_unlock(&xpddefault_service_perfdata_fp_lock); - - clear_volatile_macros_r(mac); - - // check to see if the command timed out. - if (early_timeout) - engine_logger(log_runtime_warning, basic) - << "Warning: Service performance data file processing command '" - << processed_command_line << "' timed out after " - << config->perfdata_timeout() << " seconds"; - runtime_logger->warn( - "Warning: Service performance data file processing command '{}' timed " - "out after {} seconds", - processed_command_line, config->perfdata_timeout()); - - // free memory. - return result; -} From ad463cf2a5bbc3f730bfdd89f7812fdd61affecb Mon Sep 17 00:00:00 2001 From: David Boucher Date: Wed, 29 May 2024 09:42:23 +0200 Subject: [PATCH 15/21] cleanup(engine): headers updated --- .../com/centreon/engine/configuration/host.hh | 26 +++-- .../engine/configuration/hostdependency.hh | 37 ++++---- .../engine/configuration/hostescalation.hh | 37 ++++---- .../centreon/engine/configuration/service.hh | 25 +++-- .../engine/configuration/servicedependency.hh | 95 +++++++++---------- .../engine/configuration/serviceescalation.hh | 25 +++-- .../engine/configuration/servicegroup.hh | 73 +++++++------- engine/src/configuration/contact.cc | 2 +- engine/src/configuration/hostdependency.cc | 48 +++++----- engine/src/configuration/servicegroup.cc | 7 +- .../applier/applier-connector.cc | 1 - 11 files changed, 183 insertions(+), 193 deletions(-) diff --git a/engine/inc/com/centreon/engine/configuration/host.hh b/engine/inc/com/centreon/engine/configuration/host.hh index 67a5966f654..7592a548ffe 100644 --- a/engine/inc/com/centreon/engine/configuration/host.hh +++ b/engine/inc/com/centreon/engine/configuration/host.hh @@ -1,26 +1,24 @@ /** - * Copyright 2011-2013,2015-2017,2022 Centreon + * Copyright 2011-2013,2015-2017,2022-2024 Centreon * - * This file is part of Centreon Engine. + * 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 * - * 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. + * http://www.apache.org/licenses/LICENSE-2.0 * - * 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. + * 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 * - * You should have received a copy of the GNU General Public License - * along with Centreon Engine. If not, see - * . */ - #ifndef CCE_CONFIGURATION_HOST_HH #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" diff --git a/engine/inc/com/centreon/engine/configuration/hostdependency.hh b/engine/inc/com/centreon/engine/configuration/hostdependency.hh index 1bf37e76a36..2e4a8a0e688 100644 --- a/engine/inc/com/centreon/engine/configuration/hostdependency.hh +++ b/engine/inc/com/centreon/engine/configuration/hostdependency.hh @@ -1,22 +1,21 @@ -/* -** Copyright 2011-2019 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-2024 Centreon + * + * 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_HOSTDEPENDENCY_HH #define CCE_CONFIGURATION_HOSTDEPENDENCY_HH diff --git a/engine/inc/com/centreon/engine/configuration/hostescalation.hh b/engine/inc/com/centreon/engine/configuration/hostescalation.hh index ca82f6f9928..f819329553a 100644 --- a/engine/inc/com/centreon/engine/configuration/hostescalation.hh +++ b/engine/inc/com/centreon/engine/configuration/hostescalation.hh @@ -1,22 +1,21 @@ -/* -** Copyright 2011-2013,2017 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,2017-2024 Centreon + * + * 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_HOSTESCALATION_HH #define CCE_CONFIGURATION_HOSTESCALATION_HH diff --git a/engine/inc/com/centreon/engine/configuration/service.hh b/engine/inc/com/centreon/engine/configuration/service.hh index 861162b8e08..f3d64ad5e26 100644 --- a/engine/inc/com/centreon/engine/configuration/service.hh +++ b/engine/inc/com/centreon/engine/configuration/service.hh @@ -1,22 +1,21 @@ /** - * Copyright 2011-2013,2015-2017-2022 Centreon + * Copyright 2011-2013,2015-2017-2024 Centreon * - * This file is part of Centreon Engine. + * 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 * - * 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. + * http://www.apache.org/licenses/LICENSE-2.0 * - * 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. + * 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 * - * 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 diff --git a/engine/inc/com/centreon/engine/configuration/servicedependency.hh b/engine/inc/com/centreon/engine/configuration/servicedependency.hh index 1febeb9058e..f3c48b5a3f2 100644 --- a/engine/inc/com/centreon/engine/configuration/servicedependency.hh +++ b/engine/inc/com/centreon/engine/configuration/servicedependency.hh @@ -1,22 +1,21 @@ -/* -** Copyright 2011-2013,2017 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,2017-2024 Centreon + * + * 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_SERVICEDEPENDENCY_HH #define CCE_CONFIGURATION_SERVICEDEPENDENCY_HH @@ -46,42 +45,42 @@ class servicedependency : public object { servicedependency(); servicedependency(servicedependency const& right); - ~servicedependency() throw() override; + ~servicedependency() noexcept override; servicedependency& operator=(servicedependency const& right); - bool operator==(servicedependency const& right) const throw(); - bool operator!=(servicedependency const& right) const throw(); + bool operator==(servicedependency const& right) const noexcept; + bool operator!=(servicedependency const& right) const noexcept; bool operator<(servicedependency const& right) const; void check_validity(error_info* err) const override; - key_type const& key() const throw(); + key_type const& key() const noexcept; void merge(object const& obj) override; bool parse(char const* key, char const* value) override; void dependency_period(std::string const& period); - std::string const& dependency_period() const throw(); - void dependency_type(dependency_kind type) throw(); - dependency_kind dependency_type() const throw(); - list_string& dependent_hostgroups() throw(); - list_string const& dependent_hostgroups() const throw(); - list_string& dependent_hosts() throw(); - list_string const& dependent_hosts() const throw(); - list_string& dependent_servicegroups() throw(); - list_string const& dependent_servicegroups() const throw(); - list_string& dependent_service_description() throw(); - list_string const& dependent_service_description() const throw(); - void execution_failure_options(unsigned int options) throw(); - unsigned int execution_failure_options() const throw(); - void inherits_parent(bool inherit) throw(); - bool inherits_parent() const throw(); - list_string& hostgroups() throw(); - list_string const& hostgroups() const throw(); - list_string& hosts() throw(); - list_string const& hosts() const throw(); - void notification_failure_options(unsigned int options) throw(); - unsigned int notification_failure_options() const throw(); - list_string& servicegroups() throw(); - list_string const& servicegroups() const throw(); - list_string& service_description() throw(); - list_string const& service_description() const throw(); + std::string const& dependency_period() const noexcept; + void dependency_type(dependency_kind type) noexcept; + dependency_kind dependency_type() const noexcept; + list_string& dependent_hostgroups() noexcept; + list_string const& dependent_hostgroups() const noexcept; + list_string& dependent_hosts() noexcept; + list_string const& dependent_hosts() const noexcept; + list_string& dependent_servicegroups() noexcept; + list_string const& dependent_servicegroups() const noexcept; + list_string& dependent_service_description() noexcept; + list_string const& dependent_service_description() const noexcept; + void execution_failure_options(unsigned int options) noexcept; + unsigned int execution_failure_options() const noexcept; + void inherits_parent(bool inherit) noexcept; + bool inherits_parent() const noexcept; + list_string& hostgroups() noexcept; + list_string const& hostgroups() const noexcept; + list_string& hosts() noexcept; + list_string const& hosts() const noexcept; + void notification_failure_options(unsigned int options) noexcept; + unsigned int notification_failure_options() const noexcept; + list_string& servicegroups() noexcept; + list_string const& servicegroups() const noexcept; + list_string& service_description() noexcept; + list_string const& service_description() const noexcept; private: typedef bool (*setter_func)(servicedependency&, char const*); diff --git a/engine/inc/com/centreon/engine/configuration/serviceescalation.hh b/engine/inc/com/centreon/engine/configuration/serviceescalation.hh index 1f7734b4f21..972658611d7 100644 --- a/engine/inc/com/centreon/engine/configuration/serviceescalation.hh +++ b/engine/inc/com/centreon/engine/configuration/serviceescalation.hh @@ -1,22 +1,21 @@ /** - * Copyright 2011-2013,2017 Centreon + * Copyright 2011-2013,2017-2024 Centreon * - * This file is part of Centreon Engine. + * 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 * - * 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. + * http://www.apache.org/licenses/LICENSE-2.0 * - * 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. + * 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 * - * You should have received a copy of the GNU General Public License - * along with Centreon Engine. If not, see - * . */ - #ifndef CCE_CONFIGURATION_SERVICEESCALATION_HH #define CCE_CONFIGURATION_SERVICEESCALATION_HH diff --git a/engine/inc/com/centreon/engine/configuration/servicegroup.hh b/engine/inc/com/centreon/engine/configuration/servicegroup.hh index 16ebc6b6546..b98d1c4852c 100644 --- a/engine/inc/com/centreon/engine/configuration/servicegroup.hh +++ b/engine/inc/com/centreon/engine/configuration/servicegroup.hh @@ -1,22 +1,21 @@ -/* -** Copyright 2011-2013,2017 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,2017-2024 Centreon + * + * 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_SERVICEGROUP_HH #define CCE_CONFIGURATION_SERVICEGROUP_HH @@ -26,35 +25,34 @@ typedef std::set > set_pair_string; -namespace com::centreon::engine { +namespace com::centreon::engine::configuration { -namespace configuration { class servicegroup : public object { public: typedef std::string key_type; servicegroup(key_type const& key = ""); servicegroup(servicegroup const& right); - ~servicegroup() throw() override; + ~servicegroup() noexcept override; servicegroup& operator=(servicegroup const& right); - bool operator==(servicegroup const& right) const throw(); - bool operator!=(servicegroup const& right) const throw(); - bool operator<(servicegroup const& right) const throw(); + bool operator==(servicegroup const& right) const noexcept; + bool operator!=(servicegroup const& right) const noexcept; + bool operator<(servicegroup const& right) const noexcept; void check_validity(error_info* err) const override; - key_type const& key() const throw(); + key_type const& key() const noexcept; void merge(object const& obj) override; bool parse(char const* key, char const* value) override; - std::string const& action_url() const throw(); - std::string const& alias() const throw(); - set_pair_string& members() throw(); - set_pair_string const& members() const throw(); - std::string const& notes() const throw(); - std::string const& notes_url() const throw(); - unsigned int servicegroup_id() const throw(); - set_string& servicegroup_members() throw(); - set_string const& servicegroup_members() const throw(); - std::string const& servicegroup_name() const throw(); + std::string const& action_url() const noexcept; + std::string const& alias() const noexcept; + set_pair_string& members() noexcept; + set_pair_string const& members() const noexcept; + std::string const& notes() const noexcept; + std::string const& notes_url() const noexcept; + unsigned int servicegroup_id() const noexcept; + set_string& servicegroup_members() noexcept; + set_string const& servicegroup_members() const noexcept; + std::string const& servicegroup_name() const noexcept; private: typedef bool (*setter_func)(servicegroup&, char const*); @@ -81,8 +79,7 @@ class servicegroup : public object { typedef std::shared_ptr servicegroup_ptr; typedef std::set set_servicegroup; -} // namespace configuration -} // namespace com::centreon::engine +} // namespace com::centreon::engine::configuration #endif // !CCE_CONFIGURATION_SERVICEGROUP_HH diff --git a/engine/src/configuration/contact.cc b/engine/src/configuration/contact.cc index b68f4868511..d57e7f2e729 100644 --- a/engine/src/configuration/contact.cc +++ b/engine/src/configuration/contact.cc @@ -1,5 +1,5 @@ /** - * Copyright 2011-2023 Centreon (https://www.centreon.com/) + * Copyright 2011-2024 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. diff --git a/engine/src/configuration/hostdependency.cc b/engine/src/configuration/hostdependency.cc index cd8ef676fb6..8a602475e0d 100644 --- a/engine/src/configuration/hostdependency.cc +++ b/engine/src/configuration/hostdependency.cc @@ -88,7 +88,7 @@ hostdependency::hostdependency(hostdependency const& right) : object(right) { /** * Destructor. */ -hostdependency::~hostdependency() throw() {} +hostdependency::~hostdependency() noexcept {} /** * Copy constructor. @@ -120,7 +120,7 @@ hostdependency& hostdependency::operator=(hostdependency const& right) { * * @return True if is the same hostdependency, otherwise false. */ -bool hostdependency::operator==(hostdependency const& right) const throw() { +bool hostdependency::operator==(hostdependency const& right) const noexcept { return (object::operator==(right) && _dependency_period == right._dependency_period && _dependency_type == right._dependency_type && @@ -139,7 +139,7 @@ bool hostdependency::operator==(hostdependency const& right) const throw() { * * @return True if is not the same hostdependency, otherwise false. */ -bool hostdependency::operator!=(hostdependency const& right) const throw() { +bool hostdependency::operator!=(hostdependency const& right) const noexcept { return !operator==(right); } @@ -208,7 +208,7 @@ void hostdependency::check_validity(error_info* err) const { * * @return This object. */ -hostdependency::key_type const& hostdependency::key() const throw() { +hostdependency::key_type const& hostdependency::key() const noexcept { return *this; } @@ -264,7 +264,7 @@ void hostdependency::dependency_period(std::string const& period) { * * @return The dependency_period. */ -std::string const& hostdependency::dependency_period() const throw() { +std::string const& hostdependency::dependency_period() const noexcept { return _dependency_period; } @@ -274,7 +274,7 @@ std::string const& hostdependency::dependency_period() const throw() { * @param[in] type Dependency type. */ void hostdependency::dependency_type( - hostdependency::dependency_kind type) throw() { + hostdependency::dependency_kind type) noexcept { _dependency_type = type; } @@ -283,8 +283,8 @@ void hostdependency::dependency_type( * * @return Dependency type. */ -hostdependency::dependency_kind hostdependency::dependency_type() const - throw() { +hostdependency::dependency_kind hostdependency::dependency_type() + const noexcept { return _dependency_type; } @@ -293,7 +293,7 @@ hostdependency::dependency_kind hostdependency::dependency_type() const * * @return Dependent host groups. */ -set_string& hostdependency::dependent_hostgroups() throw() { +set_string& hostdependency::dependent_hostgroups() noexcept { return *_dependent_hostgroups; } @@ -302,7 +302,7 @@ set_string& hostdependency::dependent_hostgroups() throw() { * * @return The dependent_hostgroups. */ -set_string const& hostdependency::dependent_hostgroups() const throw() { +set_string const& hostdependency::dependent_hostgroups() const noexcept { return *_dependent_hostgroups; } @@ -311,7 +311,7 @@ set_string const& hostdependency::dependent_hostgroups() const throw() { * * @return The dependent hosts. */ -set_string& hostdependency::dependent_hosts() throw() { +set_string& hostdependency::dependent_hosts() noexcept { return *_dependent_hosts; } @@ -320,7 +320,7 @@ set_string& hostdependency::dependent_hosts() throw() { * * @return The dependent_hosts. */ -set_string const& hostdependency::dependent_hosts() const throw() { +set_string const& hostdependency::dependent_hosts() const noexcept { return *_dependent_hosts; } @@ -329,8 +329,8 @@ set_string const& hostdependency::dependent_hosts() const throw() { * * @param[in] options New options. */ -void hostdependency::execution_failure_options(unsigned int options) throw() { - _execution_failure_options.set(options); +void hostdependency::execution_failure_options(unsigned int options) noexcept { + _execution_failure_options = options; } /** @@ -338,7 +338,7 @@ void hostdependency::execution_failure_options(unsigned int options) throw() { * * @return The execution_failure_options. */ -unsigned int hostdependency::execution_failure_options() const throw() { +unsigned int hostdependency::execution_failure_options() const noexcept { return _execution_failure_options; } @@ -347,7 +347,7 @@ unsigned int hostdependency::execution_failure_options() const throw() { * * @return The host groups. */ -set_string& hostdependency::hostgroups() throw() { +set_string& hostdependency::hostgroups() noexcept { return *_hostgroups; } @@ -356,7 +356,7 @@ set_string& hostdependency::hostgroups() throw() { * * @return The hostgroups. */ -set_string const& hostdependency::hostgroups() const throw() { +set_string const& hostdependency::hostgroups() const noexcept { return *_hostgroups; } @@ -365,7 +365,7 @@ set_string const& hostdependency::hostgroups() const throw() { * * @return The hosts. */ -set_string& hostdependency::hosts() throw() { +set_string& hostdependency::hosts() noexcept { return *_hosts; } @@ -374,7 +374,7 @@ set_string& hostdependency::hosts() throw() { * * @return The hosts. */ -set_string const& hostdependency::hosts() const throw() { +set_string const& hostdependency::hosts() const noexcept { return *_hosts; } @@ -383,7 +383,7 @@ set_string const& hostdependency::hosts() const throw() { * * @param[in] inherit True if dependency inherits parent. */ -void hostdependency::inherits_parent(bool inherit) throw() { +void hostdependency::inherits_parent(bool inherit) noexcept { _inherits_parent = inherit; } @@ -392,7 +392,7 @@ void hostdependency::inherits_parent(bool inherit) throw() { * * @return The inherits_parent. */ -bool hostdependency::inherits_parent() const throw() { +bool hostdependency::inherits_parent() const noexcept { return _inherits_parent; } @@ -402,8 +402,8 @@ bool hostdependency::inherits_parent() const throw() { * @param[in] options New options. */ void hostdependency::notification_failure_options( - unsigned int options) throw() { - _notification_failure_options.set(options); + unsigned int options) noexcept { + _notification_failure_options = options; } /** @@ -411,7 +411,7 @@ void hostdependency::notification_failure_options( * * @return The notification_failure_options. */ -unsigned int hostdependency::notification_failure_options() const throw() { +unsigned int hostdependency::notification_failure_options() const noexcept { return _notification_failure_options; } diff --git a/engine/src/configuration/servicegroup.cc b/engine/src/configuration/servicegroup.cc index 0fd7364cb83..c3615483ea6 100644 --- a/engine/src/configuration/servicegroup.cc +++ b/engine/src/configuration/servicegroup.cc @@ -17,6 +17,7 @@ * */ #include "com/centreon/engine/configuration/servicegroup.hh" +#include "com/centreon/engine/configuration/object.hh" #include "com/centreon/exceptions/error.hh" using namespace com::centreon; @@ -142,10 +143,10 @@ bool servicegroup::operator<(servicegroup const& right) const throw() { * If the object is not valid, an exception is thrown. */ void servicegroup::check_validity(error_info* err) const { + object::check_validity(err); + if (_servicegroup_name.empty()) - throw error( - "Service group has no name " - "(property 'servicegroup_name')"); + throw error("Service group has no name (property 'servicegroup_name')"); } /** diff --git a/engine/tests/configuration/applier/applier-connector.cc b/engine/tests/configuration/applier/applier-connector.cc index 57a4f13551c..fd96132f84c 100644 --- a/engine/tests/configuration/applier/applier-connector.cc +++ b/engine/tests/configuration/applier/applier-connector.cc @@ -83,7 +83,6 @@ TEST_F(ApplierConnector, ModifyNonExistingConnector) { // When a non existing connector is removed // Then nothing is done. TEST_F(ApplierConnector, RemoveNonExistingConnector) { - configuration::applier::connector aply; configuration::connector cnn("connector"); cnn.parse("connector_line", "echo 1"); From 1a8cf4693454615585167861e5e1dad2a9204c0d Mon Sep 17 00:00:00 2001 From: David Boucher Date: Wed, 29 May 2024 10:47:03 +0200 Subject: [PATCH 16/21] cleanup(engine): opt file updated --- engine/inc/com/centreon/engine/opt.hh | 77 +++++++++++++-------------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/engine/inc/com/centreon/engine/opt.hh b/engine/inc/com/centreon/engine/opt.hh index d9648a220fe..e6504fafdb7 100644 --- a/engine/inc/com/centreon/engine/opt.hh +++ b/engine/inc/com/centreon/engine/opt.hh @@ -1,22 +1,22 @@ -/* -** Copyright 2011-2013 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 Merethis + * Copyright 2024 Centreon + * + * 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_OPT_HH #define CCE_OPT_HH @@ -24,6 +24,9 @@ namespace com::centreon::engine { template class opt { + T _data; + bool _is_set; + public: opt() : _data{}, _is_set(false) {} opt(T const& right) : _data(right), _is_set(false) {} @@ -38,24 +41,24 @@ class opt { _is_set = right._is_set; return *this; } - bool operator==(opt const& right) const throw() { - return (_data == right._data); + bool operator==(opt const& right) const noexcept { + return _data == right._data; } - bool operator!=(opt const& right) const throw() { - return (!operator==(right)); + bool operator!=(opt const& right) const noexcept { + return !operator==(right); } - bool operator<(opt const& right) const throw() { - return (_data < right._data); + bool operator<(opt const& right) const noexcept { + return _data < right._data; } - operator T const &() const throw() { return (_data); } - T& operator*() throw() { return (_data); } - T const& operator*() const throw() { return (_data); } - T* operator->() throw() { return (&_data); } - T const* operator->() const throw() { return (&_data); } - T& get() throw() { return (_data); } - T const& get() const throw() { return (_data); } - bool is_set() const throw() { return (_is_set); } - void reset() throw() { _is_set = false; } + operator T const&() const noexcept { return (_data); } + T& operator*() noexcept { return (_data); } + T const& operator*() const noexcept { return (_data); } + T* operator->() noexcept { return (&_data); } + T const* operator->() const noexcept { return (&_data); } + T& get() noexcept { return (_data); } + T const& get() const noexcept { return (_data); } + bool is_set() const noexcept { return (_is_set); } + void reset() noexcept { _is_set = false; } void set(T const& right) { _data = right; _is_set = true; @@ -64,12 +67,8 @@ class opt { _data = right._data; _is_set = right._is_set; } - - private: - T _data; - bool _is_set; }; -} +} // namespace com::centreon::engine #endif // !CCE_OPT_HH From fbe9d04dcfab2b9df2278658a12982dd22813616 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Wed, 29 May 2024 13:23:33 +0200 Subject: [PATCH 17/21] fix(engine): wrong order while initializing the logger --- engine/src/main.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/engine/src/main.cc b/engine/src/main.cc index 3f86a4cecc7..f9ccd8190b4 100644 --- a/engine/src/main.cc +++ b/engine/src/main.cc @@ -113,11 +113,11 @@ int main(int argc, char* argv[]) { {NULL, no_argument, nullptr, '\0'}}; #endif // HAVE_GETOPT_H + log_v2::load("centengine"); + // Load singletons and global variable. config = new configuration::state; - // Hack to instanciate the logger. - log_v2::load("centengine"); auto config_logger = log_v2::instance().get(log_v2::CONFIG); auto process_logger = log_v2::instance().get(log_v2::PROCESS); init_loggers(); From e0dc20fcac82a8f52287781fb76a41ede29f0b18 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Mon, 3 Jun 2024 16:56:08 +0200 Subject: [PATCH 18/21] fix(engine): unit tests fixed --- engine/tests/macros/macro_hostname.cc | 22 ---------------------- engine/tests/macros/macro_service.cc | 22 ---------------------- tests/broker-engine/notifications.robot | 1 + 3 files changed, 1 insertion(+), 44 deletions(-) diff --git a/engine/tests/macros/macro_hostname.cc b/engine/tests/macros/macro_hostname.cc index 4d5010cb06c..664081a319e 100644 --- a/engine/tests/macros/macro_hostname.cc +++ b/engine/tests/macros/macro_hostname.cc @@ -690,28 +690,6 @@ TEST_F(MacroHostname, HostCheckCommand) { ASSERT_EQ(out, "cmd"); } -TEST_F(MacroHostname, HostPerDataFile) { - configuration::parser parser; - configuration::state st; - - std::remove("/tmp/test-config.cfg"); - - std::ofstream ofs("/tmp/test-config.cfg"); - ofs << "host_perfdata_file=/var/log/centreon-engine/host-perfdata.dat" - << std::endl; - ofs << "log_file=\"\"" << std::endl; - ofs.close(); - - parser.parse("/tmp/test-config.cfg", st); - configuration::applier::state::instance().apply(st); - init_macros(); - - std::string out; - nagios_macros* mac(get_global_macros()); - process_macros_r(mac, "$HOSTPERFDATAFILE:test_host$", out, 1); - ASSERT_EQ(out, "/var/log/centreon-engine/host-perfdata.dat"); -} - TEST_F(MacroHostname, HostDisplayName) { configuration::applier::host hst_aply; configuration::host hst; diff --git a/engine/tests/macros/macro_service.cc b/engine/tests/macros/macro_service.cc index f1b1df136f7..59f246cdf5b 100644 --- a/engine/tests/macros/macro_service.cc +++ b/engine/tests/macros/macro_service.cc @@ -966,28 +966,6 @@ TEST_F(MacroService, ServiceCheckCommand) { ASSERT_EQ(out, "cmd"); } -TEST_F(MacroService, ServicePerfDataFile) { - configuration::parser parser; - configuration::state st; - - std::remove("/tmp/test-config.cfg"); - - std::ofstream ofs("/tmp/test-config.cfg"); - ofs << "service_perfdata_file=/var/log/centreon-engine/service-perfdata.dat" - << std::endl; - ofs << "log_file=\"\"" << std::endl; - ofs.close(); - - parser.parse("/tmp/test-config.cfg", st); - configuration::applier::state::instance().apply(st); - init_macros(); - - std::string out; - nagios_macros* mac(get_global_macros()); - process_macros_r(mac, "$SERVICEPERFDATAFILE:test_host$", out, 1); - ASSERT_EQ(out, "/var/log/centreon-engine/service-perfdata.dat"); -} - TEST_F(MacroService, ServiceDisplayName) { configuration::applier::host hst_aply; configuration::applier::service svc_aply; diff --git a/tests/broker-engine/notifications.robot b/tests/broker-engine/notifications.robot index 9cde528b806..64b0bd94c7e 100644 --- a/tests/broker-engine/notifications.robot +++ b/tests/broker-engine/notifications.robot @@ -739,6 +739,7 @@ not12 ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 60 Should Be True ${result} The first notification of U3 is not sent + ${start} Get Round Current Date Ctn Process Service Result Hard host_1 service_1 ${2} The service_1 is CRITICAL Ctn Process Service Result Hard host_2 service_2 ${2} The service_2 is CRITICAL From f5eb7c83fc8be38553baf71696e8d8b5593cacd3 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Mon, 3 Jun 2024 18:38:11 +0200 Subject: [PATCH 19/21] fix(tests): issue with notifications fixed --- tests/broker-engine/notifications.robot | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/broker-engine/notifications.robot b/tests/broker-engine/notifications.robot index 64b0bd94c7e..9cde528b806 100644 --- a/tests/broker-engine/notifications.robot +++ b/tests/broker-engine/notifications.robot @@ -739,7 +739,6 @@ not12 ${result} Ctn Find In Log With Timeout ${engineLog0} ${start} ${content} 60 Should Be True ${result} The first notification of U3 is not sent - ${start} Get Round Current Date Ctn Process Service Result Hard host_1 service_1 ${2} The service_1 is CRITICAL Ctn Process Service Result Hard host_2 service_2 ${2} The service_2 is CRITICAL From 350c1123aa66e058d5a338e985c6bbc2e36c0a60 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Mon, 3 Jun 2024 20:48:25 +0200 Subject: [PATCH 20/21] fix(common/log_v2): issue due to rebase --- common/log_v2/log_v2.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/common/log_v2/log_v2.cc b/common/log_v2/log_v2.cc index 264b6e78573..94e857bc736 100644 --- a/common/log_v2/log_v2.cc +++ b/common/log_v2/log_v2.cc @@ -146,11 +146,6 @@ log_v2::log_v2(std::string name) : _log_name{std::move(name)} { create_loggers(config::logger_type::LOGGER_STDOUT); } -log_v2::~log_v2() noexcept { - /* When log_v2 is stopped, grpc mustn't log anymore. */ - gpr_set_log_function(nullptr); -} - /** * @brief Destructor. */ From cffedf1de9cd655012c65f0c74fd7f35d6c422b5 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Tue, 4 Jun 2024 18:13:35 +0200 Subject: [PATCH 21/21] fix(engine): bad rebase --- engine/src/configuration/hostescalation.cc | 511 +++++++++++++++++++++ engine/src/configuration/parser.cc | 13 - engine/src/configuration/severity.cc | 3 + 3 files changed, 514 insertions(+), 13 deletions(-) create mode 100644 engine/src/configuration/hostescalation.cc diff --git a/engine/src/configuration/hostescalation.cc b/engine/src/configuration/hostescalation.cc new file mode 100644 index 00000000000..bc76303d725 --- /dev/null +++ b/engine/src/configuration/hostescalation.cc @@ -0,0 +1,511 @@ +/** + * Copyright 2011-2013,2015 Merethis + * Copyright 2017-2024 Centreon + * + * 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/hostescalation.hh" +#include "com/centreon/engine/exceptions/error.hh" +#include "com/centreon/engine/string.hh" +#include "com/centreon/exceptions/msg_fmt.hh" + +using namespace com::centreon; +using namespace com::centreon::engine::configuration; +using com::centreon::exceptions::msg_fmt; + +#define SETTER(type, method) \ + &object::setter::generic + +std::unordered_map const hostescalation::_setters{ + {"hostgroup", SETTER(std::string const&, _set_hostgroups)}, + {"hostgroups", SETTER(std::string const&, _set_hostgroups)}, + {"hostgroup_name", SETTER(std::string const&, _set_hostgroups)}, + {"host", SETTER(std::string const&, _set_hosts)}, + {"host_name", SETTER(std::string const&, _set_hosts)}, + {"contact_groups", SETTER(std::string const&, _set_contactgroups)}, + {"escalation_options", SETTER(std::string const&, _set_escalation_options)}, + {"escalation_period", SETTER(std::string const&, _set_escalation_period)}, + {"first_notification", SETTER(uint32_t, _set_first_notification)}, + {"last_notification", SETTER(uint32_t, _set_last_notification)}, + {"notification_interval", SETTER(uint32_t, _set_notification_interval)}}; + +// Default values. +static unsigned short const default_escalation_options(hostescalation::none); +static unsigned int const default_first_notification(-2); +static unsigned int const default_last_notification(-2); +static unsigned int const default_notification_interval(0); + +/** + * Default constructor. + */ +hostescalation::hostescalation() + : object(object::hostescalation), + _escalation_options(default_escalation_options), + _first_notification(default_first_notification), + _last_notification(default_last_notification), + _notification_interval(default_notification_interval) {} + +/** + * Copy constructor. + * + * @param[in] right The hostescalation to copy. + */ +hostescalation::hostescalation(hostescalation const& right) : object(right) { + operator=(right); +} + +/** + * Destructor. + */ +hostescalation::~hostescalation() throw() {} + +/** + * Copy constructor. + * + * @param[in] right The hostescalation to copy. + * + * @return This hostescalation. + */ +hostescalation& hostescalation::operator=(hostescalation const& right) { + if (this != &right) { + object::operator=(right); + _contactgroups = right._contactgroups; + _escalation_options = right._escalation_options; + _escalation_period = right._escalation_period; + _first_notification = right._first_notification; + _hostgroups = right._hostgroups; + _hosts = right._hosts; + _last_notification = right._last_notification; + _notification_interval = right._notification_interval; + _uuid = right._uuid; + } + return *this; +} + +/** + * Equal operator. + * + * @param[in] right The hostescalation to compare. + * + * @return True if is the same hostescalation, otherwise false. + */ +bool hostescalation::operator==(hostescalation const& right) const throw() { + return (object::operator==(right) && _contactgroups == right._contactgroups && + _escalation_options == right._escalation_options && + _escalation_period == right._escalation_period && + _first_notification == right._first_notification && + _hostgroups == right._hostgroups && _hosts == right._hosts && + _last_notification == right._last_notification && + _notification_interval == right._notification_interval); +} + +/** + * Equal operator. + * + * @param[in] right The hostescalation to compare. + * + * @return True if is not the same hostescalation, otherwise false. + */ +bool hostescalation::operator!=(hostescalation const& right) const throw() { + return !operator==(right); +} + +/** + * Less-than operator. + * + * @param[in] right Object to compare to. + * + * @return True if this object is less than right. + */ +bool hostescalation::operator<(hostescalation const& right) const { + if (_hosts != right._hosts) + return _hosts < right._hosts; + else if (_hostgroups != right._hostgroups) + return _hostgroups < right._hostgroups; + else if (_contactgroups != right._contactgroups) + return _contactgroups < right._contactgroups; + else if (_escalation_options != right._escalation_options) + return _escalation_options < right._escalation_options; + else if (_escalation_period != right._escalation_period) + return _escalation_period < right._escalation_period; + else if (_first_notification != right._first_notification) + return _first_notification < right._first_notification; + else if (_last_notification != right._last_notification) + return _last_notification < right._last_notification; + return _notification_interval < right._notification_interval; +} + +/** + * @brief Check if the object is valid. + * + * If the object is not valid, an exception is thrown. + */ +void hostescalation::check_validity(error_info* err) const { + object::check_validity(err); + + if (_hosts->empty() && _hostgroups->empty()) + throw msg_fmt( + "Host escalation is not attached to any " + "host or host group (properties 'host_name' or " + "'hostgroup_name', respectively)"); +} + +/** + * Get host escalation key. + * + * @return This object. + */ +hostescalation::key_type const& hostescalation::key() const throw() { + return *this; +} + +/** + * Merge object. + * + * @param[in] obj The object to merge. + */ +void hostescalation::merge(object const& obj) { + if (obj.type() != _type) + throw(engine_error() << "Cannot merge host escalation with '" << obj.type() + << "'"); + hostescalation const& tmpl(static_cast(obj)); + + MRG_INHERIT(_contactgroups); + MRG_OPTION(_escalation_options); + MRG_OPTION(_escalation_period); + MRG_OPTION(_first_notification); + MRG_INHERIT(_hostgroups); + MRG_INHERIT(_hosts); + MRG_OPTION(_last_notification); + MRG_OPTION(_notification_interval); +} + +/** + * Parse and set the hostescalation property. + * + * @param[in] key The property name. + * @param[in] value The property value. + * + * @return True on success, otherwise false. + */ +bool hostescalation::parse(char const* key, char const* value) { + std::unordered_map::const_iterator + it{_setters.find(key)}; + if (it != _setters.end()) + return (it->second)(*this, value); + return false; +} + +/** + * Get contact groups. + * + * @return The contact groups. + */ +set_string& hostescalation::contactgroups() throw() { + return *_contactgroups; +} + +/** + * Get contactgroups. + * + * @return The contactgroups. + */ +set_string const& hostescalation::contactgroups() const throw() { + return *_contactgroups; +} + +/** + * Check if contact groups were defined. + * + * @return True if contact groups were defined. + */ +bool hostescalation::contactgroups_defined() const throw() { + return _contactgroups.is_set(); +} + +/** + * Set escalation options. + * + * @param[in] options New escalation options. + */ +void hostescalation::escalation_options(unsigned short options) throw() { + _escalation_options = options; + return; +} + +/** + * Get escalation_options. + * + * @return The escalation_options. + */ +unsigned short hostescalation::escalation_options() const throw() { + return _escalation_options; +} + +/** + * Set the escalation period. + * + * @param[in] period New escalation period. + */ +void hostescalation::escalation_period(std::string const& period) { + _escalation_period = period; + return; +} + +/** + * Get escalation_period. + * + * @return The escalation_period. + */ +std::string const& hostescalation::escalation_period() const throw() { + return _escalation_period; +} + +/** + * Check if escalation period was defined. + * + * @return True if escalation period was defined. + */ +bool hostescalation::escalation_period_defined() const throw() { + return _escalation_period.is_set(); +} + +/** + * Set the first notification. + * + * @param[in] n New first notification number. + */ +void hostescalation::first_notification(unsigned int n) throw() { + _first_notification = n; + return; +} + +/** + * Get first_notification. + * + * @return The first_notification. + */ +unsigned int hostescalation::first_notification() const throw() { + return _first_notification; +} + +/** + * Get host groups. + * + * @return The host groups. + */ +set_string& hostescalation::hostgroups() throw() { + return *_hostgroups; +} + +/** + * Get hostgroups. + * + * @return The hostgroups. + */ +set_string const& hostescalation::hostgroups() const throw() { + return *_hostgroups; +} + +/** + * Get hosts. + * + * @return The hosts. + */ +set_string& hostescalation::hosts() throw() { + return *_hosts; +} + +/** + * Get hosts. + * + * @return The hosts. + */ +set_string const& hostescalation::hosts() const throw() { + return *_hosts; +} + +/** + * Set the last notification. + * + * @param[in] n New last notification number. + */ +void hostescalation::last_notification(unsigned int n) throw() { + _last_notification = n; + return; +} + +/** + * Get last_notification. + * + * @return The last_notification. + */ +unsigned int hostescalation::last_notification() const throw() { + return _last_notification; +} + +/** + * Set the notification interval. + * + * @param[in] interval New notification interval. + */ +void hostescalation::notification_interval(unsigned int interval) { + _notification_interval = interval; + return; +} + +/** + * Get notification_interval. + * + * @return The notification_interval. + */ +unsigned int hostescalation::notification_interval() const throw() { + return _notification_interval; +} + +/** + * Check if the notification interval was defined. + * + * @return True if the notification interval was defined. + */ +bool hostescalation::notification_interval_defined() const throw() { + return _notification_interval.is_set(); +} + +/** + * Set contactgroups value. + * + * @param[in] value The new contactgroups value. + * + * @return True on success, otherwise false. + */ +bool hostescalation::_set_contactgroups(std::string const& value) { + _contactgroups = value; + return true; +} + +/** + * Set escalation_options value. + * + * @param[in] value The new escalation_options value. + * + * @return True on success, otherwise false. + */ +bool hostescalation::_set_escalation_options(std::string const& value) { + unsigned short options(none); + std::list values; + string::split(value, values, ','); + for (std::list::iterator it(values.begin()), end(values.end()); + it != end; ++it) { + string::trim(*it); + if (*it == "d" || *it == "down") + options |= down; + else if (*it == "u" || *it == "unreachable") + options |= unreachable; + else if (*it == "r" || *it == "recovery") + options |= recovery; + else if (*it == "n" || *it == "none") + options = none; + else if (*it == "a" || *it == "all") + options = down | unreachable | recovery; + else + return false; + } + _escalation_options = options; + return true; +} + +/** + * Set escalation_period value. + * + * @param[in] value The new escalation_period value. + * + * @return True on success, otherwise false. + */ +bool hostescalation::_set_escalation_period(std::string const& value) { + _escalation_period = value; + return true; +} + +/** + * Set first_notification value. + * + * @param[in] value The new first_notification value. + * + * @return True on success, otherwise false. + */ +bool hostescalation::_set_first_notification(unsigned int value) { + _first_notification = value; + return true; +} + +/** + * Set hostgroups value. + * + * @param[in] value The new hostgroups value. + * + * @return True on success, otherwise false. + */ +bool hostescalation::_set_hostgroups(std::string const& value) { + _hostgroups = value; + return true; +} + +/** + * Set hosts value. + * + * @param[in] value The new hosts value. + * + * @return True on success, otherwise false. + */ +bool hostescalation::_set_hosts(std::string const& value) { + _hosts = value; + return true; +} + +/** + * Set last_notification value. + * + * @param[in] value The new last_notification value. + * + * @return True on success, otherwise false. + */ +bool hostescalation::_set_last_notification(unsigned int value) { + _last_notification = value; + return true; +} + +/** + * Set notification_interval value. + * + * @param[in] value The new notification_interval value. + * + * @return True on success, otherwise false. + */ +bool hostescalation::_set_notification_interval(unsigned int value) { + _notification_interval = value; + return true; +} + +/** + * Get uuid value. + * + * @return uuid. + */ +Uuid const& hostescalation::uuid(void) const { + return _uuid; +} diff --git a/engine/src/configuration/parser.cc b/engine/src/configuration/parser.cc index 9152103bd37..a06625ce60b 100644 --- a/engine/src/configuration/parser.cc +++ b/engine/src/configuration/parser.cc @@ -372,19 +372,6 @@ void parser::_add_template(object_ptr obj) { tmpl[name] = obj; } -/** - * Apply parse method into list. - * - * @param[in] lst The list to apply action. - * @param[in] pfunc The method to apply. - */ -void parser::_apply(std::list const& lst, - void (parser::*pfunc)(std::string const&)) { - for (std::list::const_iterator it(lst.begin()), end(lst.end()); - it != end; ++it) - (this->*pfunc)(*it); -} - /** * Get the file information. * diff --git a/engine/src/configuration/severity.cc b/engine/src/configuration/severity.cc index 4f72bbd1fcf..c8d573a05e4 100644 --- a/engine/src/configuration/severity.cc +++ b/engine/src/configuration/severity.cc @@ -18,6 +18,7 @@ */ #include "com/centreon/engine/configuration/severity.hh" +#include "com/centreon/engine/configuration/object.hh" #include "com/centreon/exceptions/error.hh" using namespace com::centreon; @@ -131,6 +132,8 @@ bool severity::operator<(const severity& other) const noexcept { * If the object is not valid, an exception is thrown. */ void severity::check_validity(error_info* err) const { + object::check_validity(err); + if (_severity_name.empty()) throw error("Severity has no name (property 'severity_name')"); if (_key.first == 0)