From 6fde671f578e8be94ba0baac37c2099175e3858f Mon Sep 17 00:00:00 2001 From: David Boucher Date: Mon, 26 Apr 2021 18:05:45 +0200 Subject: [PATCH] fix(macros): hostgroups/servicegroups may be empty. REFS: MON-7197 --- CHANGELOG.md | 9 +++++++++ CMakeLists.txt | 2 +- src/macros/grab_host.cc | 17 +++++++---------- src/macros/grab_service.cc | 14 ++++++++++---- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fe38c794..5830001fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## 21.04.1 + +### Bugs + +*Macros* + +If a service / host is not in a service/host group, then the HOSTGROUPNAME/ +SERVICEGROUPNAME macros can lead to segfault. This patch fixes this issue. + ## 21.04.0 `To be released in april 2021` diff --git a/CMakeLists.txt b/CMakeLists.txt index aee8ec57d..97180f9c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,7 +68,7 @@ link_directories(${json11_LIB_DIRS}) # Version. set(CENTREON_ENGINE_MAJOR 21) set(CENTREON_ENGINE_MINOR 04) -set(CENTREON_ENGINE_PATCH 0) +set(CENTREON_ENGINE_PATCH 1) if (CENTREON_ENGINE_PRERELEASE) set(CENTREON_ENGINE_VERSION "${CENTREON_ENGINE_MAJOR}.${CENTREON_ENGINE_MINOR}.${CENTREON_ENGINE_PATCH}-${CENTREON_ENGINE_PRERELEASE}") else () diff --git a/src/macros/grab_host.cc b/src/macros/grab_host.cc index fb61da6ce..5e11d39aa 100755 --- a/src/macros/grab_host.cc +++ b/src/macros/grab_host.cc @@ -33,12 +33,6 @@ using namespace com::centreon::engine; using namespace com::centreon::engine::macros; using namespace com::centreon::engine::logging; -/************************************** - * * - * Local Functions * - * * - **************************************/ - /** * Generate total services macros. * @@ -141,10 +135,13 @@ static std::string get_host_group_names(host& hst, nagios_macros* mac) { */ static std::string get_host_group_name(host& hst, nagios_macros* mac) { (void)mac; - logger(dbg_notifications, more) << "on macro juste avant host group "; - std::string buf{hst.get_parent_groups().front()->get_group_name()}; - logger(dbg_notifications, more) << "on macro after host group " << buf; - return buf; + + // Find all hostgroups this host is associated with. + auto it = hst.get_parent_groups().begin(); + if (it == hst.get_parent_groups().end()) + return ""; + else + return (*it)->get_group_name(); } /** diff --git a/src/macros/grab_service.cc b/src/macros/grab_service.cc index 99b0008c6..1c215abc8 100644 --- a/src/macros/grab_service.cc +++ b/src/macros/grab_service.cc @@ -93,9 +93,11 @@ static std::string get_service_group_name(service& svc, nagios_macros* mac) { (void)mac; // Find all servicegroups this service is associated with. - std::string buf{svc.get_parent_groups().front()->get_group_name()}; - logger(dbg_notifications, more) << "on grab service service name :" << buf; - return buf; + auto it = svc.get_parent_groups().begin(); + if (it == svc.get_parent_groups().end()) + return ""; + else + return (*it)->get_group_name(); } /** @@ -234,7 +236,11 @@ struct grab_service_redirection { true}}, // Is volatile. {MACRO_SERVICEISVOLATILE, - {&get_member_as_string, true}}, + {&get_member_as_string, + true}}, // Attempt. {MACRO_SERVICEATTEMPT, {&get_member_as_string