[icinga-checkins] icinga.org: icinga-core/tdressler/fixes: core: fix unable to exclude hostgroups when using nested hostgroups ( Alexander Sulfrian) #2619

git at icinga.org git at icinga.org
Sun Apr 14 21:29:14 CEST 2013


Module: icinga-core
Branch: tdressler/fixes
Commit: df5047f51804909ffbd10f9d09cc5b3dec49bf1c
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=df5047f51804909ffbd10f9d09cc5b3dec49bf1c

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Sun Apr  7 01:09:08 2013 +0200

core: fix unable to exclude hostgroups when using nested hostgroups (Alexander Sulfrian) #2619

patch adds a dedicated reject list, which then also allows to exclude
hostgroups. thanks for the neat patch.

a testcase is added as well, it passes config verification where
previously an error was thrown.

refs #2619

---

 Changelog           |    1 +
 THANKS              |    1 +
 tests/etc/2619.cfg  |   31 +++++++++++++++++++
 xdata/xodtemplate.c |   81 +++++++++++++++++++++++++++++++++++++++++----------
 xdata/xodtemplate.h |    3 +-
 5 files changed, 100 insertions(+), 17 deletions(-)

diff --git a/Changelog b/Changelog
index 1e93ffb..91b6853 100644
--- a/Changelog
+++ b/Changelog
@@ -56,6 +56,7 @@ FIXES
 * core: fix triggered downtimes for child hosts are missing after icinga restart (thx Michael Lucka) #3390 - MF
 * core: fix services with empty hostgroups aren't processed even if it has host_name specified (allow_empty_hostgroups=1) (thx Viranch Metha) #3736 - MF
 * core: revamp USE_LTDL definitions in nebmods (Andreas Ericsson, op5 Team) #3817 - MF
+* core: fix unable to exclude hostgroups when using nested hostgroups (Alexander Sulfrian) #2619 - MF
 
 * idoutils: fix many memory leaks in ido2db on dbi_result_free and others (thx Klaus Wagner) #3406 - MF
 * idoutils: fix ido2db crashes when Oracle queries fail #3324 - GB
diff --git a/THANKS b/THANKS
index 3ea1fee..bd6fa0c 100644
--- a/THANKS
+++ b/THANKS
@@ -302,6 +302,7 @@ in various ways.  If we missed your name, let us know.
 * Kevin Stone
 * Herbert Straub
 * Sven Strickroth
+* Alexander Sulfrian
 * SUSE Linux Team
 * Horvath Tamas
 * Nicholas Tang
diff --git a/tests/etc/2619.cfg b/tests/etc/2619.cfg
new file mode 100644
index 0000000..aa3db81
--- /dev/null
+++ b/tests/etc/2619.cfg
@@ -0,0 +1,31 @@
+#############################################################################################
+# ICINGA TEST CONFIG BY ISSUES
+# (c) 2009-2013 Icinga Development Team and Community Contributors
+#
+# #2619
+# unable to exclude hostgroups when using nested hostgroups
+#############################################################################################
+
+define hostgroup {
+	hostgroup_name 			2619groupA
+}
+define hostgroup {
+	hostgroup_name 			2619groupB
+}
+define host {
+	use 				generic-host
+	host_name 			2619foo
+	address 			127.0.0.1
+	hostgroups 			2619groupA
+}
+define host {
+	use				generic-host
+	host_name 			2619foo2
+	address 			127.0.0.1
+	hostgroups 			2619groupB,2619groupA
+}
+
+define hostgroup {
+	hostgroup_name 			2619groupC
+	hostgroup_members 		2619groupA,!2619groupB
+}
diff --git a/xdata/xodtemplate.c b/xdata/xodtemplate.c
index 69325a8..71e66f4 100644
--- a/xdata/xodtemplate.c
+++ b/xdata/xodtemplate.c
@@ -7897,6 +7897,7 @@ int xodtemplate_recombobulate_hostgroups(void) {
 	xodtemplate_host *temp_host = NULL;
 	xodtemplate_hostgroup *temp_hostgroup = NULL;
 	xodtemplate_memberlist *temp_memberlist = NULL;
+	xodtemplate_memberlist *reject_memberlist = NULL;
 	xodtemplate_memberlist *this_memberlist = NULL;
 	char *hostgroup_names = NULL;
 	char *temp_ptr = NULL;
@@ -7971,9 +7972,30 @@ int xodtemplate_recombobulate_hostgroups(void) {
 #endif
 
 	/* expand subgroup membership recursively */
-	for (temp_hostgroup = xodtemplate_hostgroup_list; temp_hostgroup; temp_hostgroup = temp_hostgroup->next)
-		if (xodtemplate_recombobulate_hostgroup_subgroups(temp_hostgroup, NULL) != OK)
+	for (temp_hostgroup = xodtemplate_hostgroup_list; temp_hostgroup; temp_hostgroup = temp_hostgroup->next) {
+		if (xodtemplate_recombobulate_hostgroup_subgroups(temp_hostgroup, &temp_memberlist, &reject_memberlist) != OK) {
+			xodtemplate_free_memberlist(&temp_memberlist);
 			return ERROR;
+		}
+
+		xodtemplate_reject_hosts_from_hostgroup(&temp_memberlist, &reject_memberlist);
+
+		for (this_memberlist = temp_memberlist; this_memberlist; this_memberlist = this_memberlist->next) {
+			/* add this host to the hostgroup members directive */
+			if (temp_hostgroup->members == NULL)
+				temp_hostgroup->members = (char *)strdup(this_memberlist->name1);
+			else {
+				new_members = (char *)realloc(temp_hostgroup->members, strlen(temp_hostgroup->members) + strlen(this_memberlist->name1) + 2);
+				if (new_members != NULL) {
+					temp_hostgroup->members = new_members;
+					strcat(temp_hostgroup->members, ",");
+					strcat(temp_hostgroup->members, this_memberlist->name1);
+				}
+			}
+		}
+		xodtemplate_free_memberlist(&temp_memberlist);
+
+	}
 
 	/* expand members of all hostgroups - this could be done in xodtemplate_register_hostgroup(), but we can save the CGIs some work if we do it here */
 	for (temp_hostgroup = xodtemplate_hostgroup_list; temp_hostgroup; temp_hostgroup = temp_hostgroup->next) {
@@ -8027,13 +8049,14 @@ int xodtemplate_recombobulate_hostgroups(void) {
 
 
 
-int xodtemplate_recombobulate_hostgroup_subgroups(xodtemplate_hostgroup *temp_hostgroup, char **members) {
+int xodtemplate_recombobulate_hostgroup_subgroups(xodtemplate_hostgroup *temp_hostgroup, xodtemplate_memberlist **member_list, xodtemplate_memberlist **reject_list) {
 	xodtemplate_hostgroup *sub_group = NULL;
+	xodtemplate_memberlist *reject_memberlist = NULL;
 	char *orig_hgmembers = NULL;
 	char *hgmembers = NULL;
-	char *newmembers = NULL;
 	char *buf = NULL;
 	char *ptr = NULL;
+	int reject_item = FALSE;
 
 	if (temp_hostgroup == NULL)
 		return ERROR;
@@ -8061,21 +8084,26 @@ int xodtemplate_recombobulate_hostgroup_subgroups(xodtemplate_hostgroup *temp_ho
 			strip(buf);
 
 			/* find subgroup and recurse */
+			if (buf[0] == '!') {
+				reject_item = TRUE;
+				buf++;
+				logit(NSLOG_CONFIG_ERROR, TRUE, "Warning: Excluding member group '%s' specified in hostgroup (config file '%s', starting on line %d)\n", buf, xodtemplate_config_file_name(temp_hostgroup->_config_file), temp_hostgroup->_start_line);
+			}
+			else {
+				reject_item = FALSE;
+			}
+
 			if ((sub_group = xodtemplate_find_real_hostgroup(buf)) == NULL) {
 				logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Could not find member group '%s' specified in hostgroup (config file '%s', starting on line %d)\n", buf, xodtemplate_config_file_name(temp_hostgroup->_config_file), temp_hostgroup->_start_line);
 				return ERROR;
 			}
-			xodtemplate_recombobulate_hostgroup_subgroups(sub_group, &newmembers);
 
-			/* add new (sub) members */
-			if (newmembers != NULL) {
-				if (temp_hostgroup->members == NULL)
-					temp_hostgroup->members = (char *)strdup(newmembers);
-				else if ((temp_hostgroup->members = realloc(temp_hostgroup->members, strlen(temp_hostgroup->members) + strlen(newmembers) + 2))) {
-					strcat(temp_hostgroup->members, ",");
-					strcat(temp_hostgroup->members, newmembers);
-				}
-			}
+			if (reject_item)
+				xodtemplate_recombobulate_hostgroup_subgroups(sub_group, &reject_memberlist, member_list);
+			else
+				xodtemplate_recombobulate_hostgroup_subgroups(sub_group, member_list, &reject_memberlist);
+
+			xodtemplate_reject_hosts_from_hostgroup(member_list, &reject_memberlist);
 		}
 
 		/* free memory */
@@ -8086,8 +8114,7 @@ int xodtemplate_recombobulate_hostgroup_subgroups(xodtemplate_hostgroup *temp_ho
 	}
 
 	/* return host members */
-	if (members != NULL)
-		*members = temp_hostgroup->members;
+	xodtemplate_expand_hosts(member_list, reject_list, temp_hostgroup->members, temp_hostgroup->_config_file, temp_hostgroup->_start_line);
 
 	return OK;
 }
@@ -12989,6 +13016,28 @@ int xodtemplate_add_hostgroup_members_to_memberlist(xodtemplate_memberlist **lis
 }
 
 
+int xodtemplate_reject_hosts_from_hostgroup(xodtemplate_memberlist **member_list, xodtemplate_memberlist **reject_list) {
+	xodtemplate_memberlist *member_list_ptr = NULL;
+	xodtemplate_memberlist *reject_list_ptr = NULL;
+
+	if (member_list == NULL || reject_list == NULL) {
+		return ERROR;
+	}
+
+	/* remove rejects (if any) from the list (no duplicate entries exist in either list) */
+	for (reject_list_ptr = *reject_list; reject_list_ptr; reject_list_ptr = reject_list_ptr->next) {
+		for (member_list_ptr = *member_list; member_list_ptr; member_list_ptr = member_list_ptr->next) {
+			if (!strcmp(reject_list_ptr->name1, member_list_ptr->name1)) {
+				xodtemplate_remove_memberlist_item(member_list_ptr, member_list);
+				break;
+			}
+		}
+	}
+
+	xodtemplate_free_memberlist(reject_list);
+	return OK;
+}
+
 
 /* expands a comma-delimited list of servicegroups and/or service descriptions */
 xodtemplate_memberlist *xodtemplate_expand_servicegroups_and_services(char *servicegroups, char *host_name, char *services, int _config_file, int _start_line) {
diff --git a/xdata/xodtemplate.h b/xdata/xodtemplate.h
index 7ef2b16..438ed56 100644
--- a/xdata/xodtemplate.h
+++ b/xdata/xodtemplate.h
@@ -881,6 +881,7 @@ xodtemplate_memberlist *xodtemplate_expand_hostgroups_and_hosts(char *,char *,in
 int xodtemplate_expand_hostgroups(xodtemplate_memberlist **,xodtemplate_memberlist **,char *,int,int);
 int xodtemplate_expand_hosts(xodtemplate_memberlist **,xodtemplate_memberlist **,char *,int,int);
 int xodtemplate_add_hostgroup_members_to_memberlist(xodtemplate_memberlist **,xodtemplate_hostgroup *,int,int);
+int xodtemplate_reject_hosts_from_hostgroup(xodtemplate_memberlist **, xodtemplate_memberlist **);
 
 xodtemplate_memberlist *xodtemplate_expand_servicegroups_and_services(char *,char *,char *,int,int);
 int xodtemplate_expand_servicegroups(xodtemplate_memberlist **,xodtemplate_memberlist **,char *,int,int);
@@ -946,7 +947,7 @@ int xodtemplate_recombobulate_contactgroups(void);
 int xodtemplate_recombobulate_contactgroup_subgroups(xodtemplate_contactgroup *,char **);
 int xodtemplate_recombobulate_object_contacts(void);
 int xodtemplate_recombobulate_hostgroups(void);
-int xodtemplate_recombobulate_hostgroup_subgroups(xodtemplate_hostgroup *, char **);
+int xodtemplate_recombobulate_hostgroup_subgroups(xodtemplate_hostgroup *, xodtemplate_memberlist **, xodtemplate_memberlist **);
 int xodtemplate_recombobulate_servicegroups(void);
 int xodtemplate_recombobulate_servicegroup_subgroups(xodtemplate_servicegroup *,char **);
 





More information about the icinga-checkins mailing list