[Nagios-checkins] SF.net SVN: nagios:[2757] nagioscore/trunk
Nagios network monitoring software is enterprise server monitoring
Brought to you by:
egalstad,
sawolf-nagios
From: <ag...@us...> - 2013-05-08 14:02:34
|
Revision: 2757 http://nagios.svn.sourceforge.net/nagios/?rev=2757&view=rev Author: ageric Date: 2013-05-08 14:02:12 +0000 (Wed, 08 May 2013) Log Message: ----------- core: Avoid strdup()'ing group member names Since we know all string lists are duplicated to kingdom come when we inherit variable values, read them from config and register objects, we can at least avoid duplicating them once again here, cutting down memory used by lots, and allocations even more (making it far more pleasant to run large configurations through valgrind). The user-visible feature here is that error messages for non-existant objects listed as members of some particular group can contain the location in the configuration where things went south, which is particularly helpful for larger configurations. Signed-off-by: Andreas Ericsson <ae...@op...> Modified Paths: -------------- nagioscore/trunk/base/config.c nagioscore/trunk/common/objects.c nagioscore/trunk/xdata/xodtemplate.c Modified: nagioscore/trunk/base/config.c =================================================================== --- nagioscore/trunk/base/config.c 2013-05-08 14:01:47 UTC (rev 2756) +++ nagioscore/trunk/base/config.c 2013-05-08 14:02:12 UTC (rev 2757) @@ -1482,14 +1482,12 @@ contact *temp_contact = NULL; commandsmember *temp_commandsmember = NULL; contactgroup *temp_contactgroup = NULL; - contactsmember *temp_contactsmember = NULL; host *temp_host = NULL; host *temp_host2 = NULL; hostsmember *temp_hostsmember = NULL; servicesmember *sm = NULL; hostgroup *temp_hostgroup = NULL; servicegroup *temp_servicegroup = NULL; - servicesmember *temp_servicesmember = NULL; service *temp_service = NULL; command *temp_command = NULL; timeperiod *temp_timeperiod = NULL; @@ -1711,24 +1709,6 @@ if(verify_config) printf("Checking host groups...\n"); for(temp_hostgroup = hostgroup_list, total_objects = 0; temp_hostgroup != NULL; temp_hostgroup = temp_hostgroup->next, total_objects++) { - - /* check all group members */ - for(temp_hostsmember = temp_hostgroup->members; temp_hostsmember != NULL; temp_hostsmember = temp_hostsmember->next) { - - temp_host = find_host(temp_hostsmember->host_name); - if(temp_host == NULL) { - logit(NSLOG_VERIFICATION_ERROR, TRUE, "Error: Host '%s' specified in host group '%s' is not defined anywhere!", temp_hostsmember->host_name, temp_hostgroup->group_name); - errors++; - } - - /* save a pointer to this hostgroup for faster host/group membership lookups later */ - else - add_object_to_objectlist(&temp_host->hostgroups_ptr, (void *)temp_hostgroup); - - /* save host pointer for later */ - temp_hostsmember->host_ptr = temp_host; - } - /* check for illegal characters in hostgroup name */ if(use_precached_objects == FALSE) { if(contains_illegal_object_chars(temp_hostgroup->group_name) == TRUE) { @@ -1748,24 +1728,6 @@ if(verify_config) printf("Checking service groups...\n"); for(temp_servicegroup = servicegroup_list, total_objects = 0; temp_servicegroup != NULL; temp_servicegroup = temp_servicegroup->next, total_objects++) { - - /* check all group members */ - for(temp_servicesmember = temp_servicegroup->members; temp_servicesmember != NULL; temp_servicesmember = temp_servicesmember->next) { - - temp_service = find_service(temp_servicesmember->host_name, temp_servicesmember->service_description); - if(temp_service == NULL) { - logit(NSLOG_VERIFICATION_ERROR, TRUE, "Error: Service '%s' on host '%s' specified in service group '%s' is not defined anywhere!", temp_servicesmember->service_description, temp_servicesmember->host_name, temp_servicegroup->group_name); - errors++; - } - - /* save a pointer to this servicegroup for faster service/group membership lookups later */ - else - add_object_to_objectlist(&temp_service->servicegroups_ptr, (void *)temp_servicegroup); - - /* save service pointer for later */ - temp_servicesmember->service_ptr = temp_service; - } - /* check for illegal characters in servicegroup name */ if(use_precached_objects == FALSE) { if(contains_illegal_object_chars(temp_servicegroup->group_name) == TRUE) { @@ -1883,24 +1845,6 @@ if(verify_config) printf("Checking contact groups...\n"); for(temp_contactgroup = contactgroup_list, total_objects = 0; temp_contactgroup != NULL; temp_contactgroup = temp_contactgroup->next, total_objects++) { - - /* check all the group members */ - for(temp_contactsmember = temp_contactgroup->members; temp_contactsmember != NULL; temp_contactsmember = temp_contactsmember->next) { - - temp_contact = find_contact(temp_contactsmember->contact_name); - if(temp_contact == NULL) { - logit(NSLOG_VERIFICATION_ERROR, TRUE, "Error: Contact '%s' specified in contact group '%s' is not defined anywhere!", temp_contactsmember->contact_name, temp_contactgroup->group_name); - errors++; - } - - /* save a pointer to this contactgroup for faster contact/group membership lookups later */ - else - add_object_to_objectlist(&temp_contact->contactgroups_ptr, (void *)temp_contactgroup); - - /* save the contact pointer for later */ - temp_contactsmember->contact_ptr = temp_contact; - } - /* check for illegal characters in contactgroup name */ if(use_precached_objects == FALSE) { if(contains_illegal_object_chars(temp_contactgroup->group_name) == TRUE) { Modified: nagioscore/trunk/common/objects.c =================================================================== --- nagioscore/trunk/common/objects.c 2013-05-08 14:01:47 UTC (rev 2756) +++ nagioscore/trunk/common/objects.c 2013-05-08 14:02:12 UTC (rev 2757) @@ -1027,22 +1027,25 @@ hostsmember *new_member = NULL; hostsmember *last_member = NULL; hostsmember *temp_member = NULL; + struct host *h; /* make sure we have the data we need */ if(temp_hostgroup == NULL || (host_name == NULL || !strcmp(host_name, ""))) { logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Hostgroup or group member is NULL\n"); return NULL; } + if (!(h = find_host(host_name))) { + logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Failed to locate host '%s' for hostgroup '%s'\n", host_name, temp_hostgroup->group_name); + return NULL; + } /* allocate memory for a new member */ if((new_member = calloc(1, sizeof(hostsmember))) == NULL) return NULL; - /* duplicate vars */ - if((new_member->host_name = (char *)strdup(host_name)) == NULL) { - my_free(new_member); - return NULL; - } + /* assign vars */ + new_member->host_name = h->name; + new_member->host_ptr = h; /* add the new member to the member list, sorted by host name */ #ifndef NSCGI @@ -1074,6 +1077,8 @@ last_member->next = new_member; } + prepend_object_to_objectlist(&h->hostgroups_ptr, (void *)temp_hostgroup); + return new_member; } @@ -1147,26 +1152,26 @@ servicesmember *new_member = NULL; servicesmember *last_member = NULL; servicesmember *temp_member = NULL; + struct service *svc; /* make sure we have the data we need */ if(temp_servicegroup == NULL || (host_name == NULL || !strcmp(host_name, "")) || (svc_description == NULL || !strcmp(svc_description, ""))) { logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Servicegroup or group member is NULL\n"); return NULL; } + if (!(svc = find_service(host_name, svc_description))) { + logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Failed to locate service '%s' on host '%s' for servicegroup '%s'\n", svc_description, host_name, temp_servicegroup->group_name); + return NULL; + } /* allocate memory for a new member */ if((new_member = calloc(1, sizeof(servicesmember))) == NULL) return NULL; - /* duplicate vars */ - new_member->host_name = (char *)strdup(host_name); - new_member->service_description = (char *)strdup(svc_description); - if(new_member->host_name == NULL || new_member->service_description == NULL) { - my_free(new_member->host_name); - my_free(new_member->service_description); - my_free(new_member); - return NULL; - } + /* assign vars */ + new_member->host_name = svc->host_name; + new_member->service_description = svc->description; + new_member->service_ptr = svc; /* * add new member to member list, sorted by host name then @@ -1213,6 +1218,8 @@ last_member->next = new_member; } + prepend_object_to_objectlist(&svc->servicegroups_ptr, (void *)temp_servicegroup); + return new_member; } @@ -1453,6 +1460,7 @@ /* add a new member to a contact group */ contactsmember *add_contact_to_contactgroup(contactgroup *grp, char *contact_name) { contactsmember *new_contactsmember = NULL; + struct contact *c; /* make sure we have the data we need */ if(grp == NULL || (contact_name == NULL || !strcmp(contact_name, ""))) { @@ -1460,20 +1468,25 @@ return NULL; } + if (!(c = find_contact(contact_name))) { + logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Failed to locate contact '%s' for contactgroup '%s'\n", contact_name, grp->group_name); + return NULL; + } + /* allocate memory for a new member */ if((new_contactsmember = calloc(1, sizeof(contactsmember))) == NULL) return NULL; - /* duplicate vars */ - if((new_contactsmember->contact_name = (char *)strdup(contact_name)) == NULL) { - my_free(new_contactsmember); - return NULL; - } + /* assign vars */ + new_contactsmember->contact_name = c->name; + new_contactsmember->contact_ptr = c; /* add the new member to the head of the member list */ new_contactsmember->next = grp->members; grp->members = new_contactsmember; + prepend_object_to_objectlist(&c->contactgroups_ptr, (void *)grp); + return new_contactsmember; } @@ -2613,7 +2626,6 @@ this_hostsmember = this_hostgroup->members; while(this_hostsmember != NULL) { next_hostsmember = this_hostsmember->next; - my_free(this_hostsmember->host_name); my_free(this_hostsmember); this_hostsmember = next_hostsmember; } @@ -2637,8 +2649,6 @@ this_servicesmember = this_servicegroup->members; while(this_servicesmember != NULL) { next_servicesmember = this_servicesmember->next; - my_free(this_servicesmember->host_name); - my_free(this_servicesmember->service_description); my_free(this_servicesmember); this_servicesmember = next_servicesmember; } @@ -2712,7 +2722,6 @@ this_contactsmember = this_contactgroup->members; while(this_contactsmember != NULL) { next_contactsmember = this_contactsmember->next; - my_free(this_contactsmember->contact_name); my_free(this_contactsmember); this_contactsmember = next_contactsmember; } Modified: nagioscore/trunk/xdata/xodtemplate.c =================================================================== --- nagioscore/trunk/xdata/xodtemplate.c 2013-05-08 14:01:47 UTC (rev 2756) +++ nagioscore/trunk/xdata/xodtemplate.c 2013-05-08 14:02:12 UTC (rev 2757) @@ -6846,6 +6846,71 @@ /**************** OBJECT REGISTRATION FUNCTIONS *******************/ /******************************************************************/ +static int xodtemplate_register_contactgroup_members(xodtemplate_contactgroup *this_contactgroup) +{ + objectlist *list; + struct contactgroup *cg; + int num_regs = 0; + + if (!this_contactgroup->register_object) + return 0; + + cg = find_contactgroup(this_contactgroup->contactgroup_name); + for(list = this_contactgroup->member_list; list; list = list->next) { + xodtemplate_contact *c = (xodtemplate_contact *)list->object_ptr; + if (!add_contact_to_contactgroup(cg, c->contact_name)) { + logit(NSLOG_CONFIG_ERROR, TRUE, "Bad member of contactgroup '%s' (config file '%s', starting on line %d)\n", cg->group_name, xodtemplate_config_file_name(this_contactgroup->_config_file), this_contactgroup->_start_line); + return -1; + } + num_regs++; + } + return num_regs; +} + +static int xodtemplate_register_hostgroup_members(xodtemplate_hostgroup *this_hostgroup) +{ + objectlist *list; + struct hostgroup *hg; + int num_regs = 0; + + if (!this_hostgroup->register_object) + return 0; + + hg = find_hostgroup(this_hostgroup->hostgroup_name); + for(list = this_hostgroup->member_list; list; list = list->next) { + xodtemplate_host *h = (xodtemplate_host *)list->object_ptr; + if (!add_host_to_hostgroup(hg, h->host_name)) { + logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Bad member of hostgrop '%s' (config file '%s', starting on line %d)\n", hg->group_name, xodtemplate_config_file_name(this_hostgroup->_config_file), this_hostgroup->_start_line); + return -1; + } + num_regs++; + } + return num_regs; + } + +static int xodtemplate_register_servicegroup_members(xodtemplate_servicegroup *this_servicegroup) +{ + objectlist *list, *next; + struct servicegroup *sg; + int num_regs = 0; + + if (!this_servicegroup->register_object) + return 0; + + sg = find_servicegroup(this_servicegroup->servicegroup_name); + for(list = this_servicegroup->member_list; list; list = next) { + xodtemplate_service *s = (xodtemplate_service *)list->object_ptr; + next = list->next; + if (!add_service_to_servicegroup(sg, s->host_name, s->service_description)) { + logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Bad member of servicegroup '%s' (config file '%s', starting on line %d)\n", sg->group_name, xodtemplate_config_file_name(this_servicegroup->_config_file), this_servicegroup->_start_line); + return -1; + } + num_regs++; + } + + return num_regs; + } + /* * registers object definitions * The order goes like this: @@ -6869,7 +6934,7 @@ */ int xodtemplate_register_objects(void) { unsigned int i; - int result = OK; + int mcount; xodtemplate_timeperiod *temp_timeperiod = NULL; xodtemplate_command *temp_command = NULL; xodtemplate_contactgroup *temp_contactgroup = NULL; @@ -6884,6 +6949,7 @@ xodtemplate_servicedependency *sd, *next_sd; xodtemplate_serviceescalation *se, *next_se; unsigned int ocount[NUM_OBJECT_SKIPLISTS]; + unsigned int tot_members = 0; for (i = 0; i < ARRAY_SIZE(ocount); i++) { @@ -6904,7 +6970,7 @@ /* register timeperiods */ ptr = NULL; for(temp_timeperiod = (xodtemplate_timeperiod *)skiplist_get_first(xobject_skiplists[TIMEPERIOD_SKIPLIST], &ptr); temp_timeperiod != NULL; temp_timeperiod = (xodtemplate_timeperiod *)skiplist_get_next(&ptr)) { - if((result = xodtemplate_register_timeperiod(temp_timeperiod)) == ERROR) + if(xodtemplate_register_timeperiod(temp_timeperiod) == ERROR) return ERROR; } timing_point("%u timeperiods registered\n", num_objects.timeperiods); @@ -6912,7 +6978,7 @@ /* register commands */ ptr = NULL; for(temp_command = (xodtemplate_command *)skiplist_get_first(xobject_skiplists[COMMAND_SKIPLIST], &ptr); temp_command != NULL; temp_command = (xodtemplate_command *)skiplist_get_next(&ptr)) { - if((result = xodtemplate_register_command(temp_command)) == ERROR) + if(xodtemplate_register_command(temp_command) == ERROR) return ERROR; } timing_point("%u commands registered\n", num_objects.commands); @@ -6920,7 +6986,7 @@ /* register contactgroups */ ptr = NULL; for(temp_contactgroup = (xodtemplate_contactgroup *)skiplist_get_first(xobject_skiplists[CONTACTGROUP_SKIPLIST], &ptr); temp_contactgroup != NULL; temp_contactgroup = (xodtemplate_contactgroup *)skiplist_get_next(&ptr)) { - if((result = xodtemplate_register_contactgroup(temp_contactgroup)) == ERROR) + if(xodtemplate_register_contactgroup(temp_contactgroup) == ERROR) return ERROR; } timing_point("%u contactgroups registered\n", num_objects.contactgroups); @@ -6928,7 +6994,7 @@ /* register hostgroups */ ptr = NULL; for(temp_hostgroup = (xodtemplate_hostgroup *)skiplist_get_first(xobject_skiplists[HOSTGROUP_SKIPLIST], &ptr); temp_hostgroup != NULL; temp_hostgroup = (xodtemplate_hostgroup *)skiplist_get_next(&ptr)) { - if((result = xodtemplate_register_hostgroup(temp_hostgroup)) == ERROR) + if(xodtemplate_register_hostgroup(temp_hostgroup) == ERROR) return ERROR; } timing_point("%u hostgroups registered\n", num_objects.hostgroups); @@ -6936,7 +7002,7 @@ /* register servicegroups */ ptr = NULL; for(temp_servicegroup = (xodtemplate_servicegroup *)skiplist_get_first(xobject_skiplists[SERVICEGROUP_SKIPLIST], &ptr); temp_servicegroup != NULL; temp_servicegroup = (xodtemplate_servicegroup *)skiplist_get_next(&ptr)) { - if((result = xodtemplate_register_servicegroup(temp_servicegroup)) == ERROR) + if(xodtemplate_register_servicegroup(temp_servicegroup) == ERROR) return ERROR; } timing_point("%u servicegroups registered\n", num_objects.servicegroups); @@ -6944,7 +7010,7 @@ /* register contacts */ ptr = NULL; for(temp_contact = (xodtemplate_contact *)skiplist_get_first(xobject_skiplists[CONTACT_SKIPLIST], &ptr); temp_contact != NULL; temp_contact = (xodtemplate_contact *)skiplist_get_next(&ptr)) { - if((result = xodtemplate_register_contact(temp_contact)) == ERROR) + if(xodtemplate_register_contact(temp_contact) == ERROR) return ERROR; } timing_point("%u contacts registered\n", num_objects.contacts); @@ -6952,7 +7018,7 @@ /* register hosts */ ptr = NULL; for(temp_host = (xodtemplate_host *)skiplist_get_first(xobject_skiplists[HOST_SKIPLIST], &ptr); temp_host != NULL; temp_host = (xodtemplate_host *)skiplist_get_next(&ptr)) { - if((result = xodtemplate_register_host(temp_host)) == ERROR) + if(xodtemplate_register_host(temp_host) == ERROR) return ERROR; } timing_point("%u hosts registered\n", num_objects.hosts); @@ -6960,11 +7026,39 @@ /* register services */ ptr = NULL; for(temp_service = (xodtemplate_service *)skiplist_get_first(xobject_skiplists[SERVICE_SKIPLIST], &ptr); temp_service != NULL; temp_service = (xodtemplate_service *)skiplist_get_next(&ptr)) { - if((result = xodtemplate_register_service(temp_service)) == ERROR) + if(xodtemplate_register_service(temp_service) == ERROR) return ERROR; } timing_point("%u services registered\n", num_objects.services); + /* groups and objects are registered, so join them up */ + /* register contactgroup members */ + ptr = NULL; tot_members = 0; + for(temp_contactgroup = (xodtemplate_contactgroup *)skiplist_get_first(xobject_skiplists[CONTACTGROUP_SKIPLIST], &ptr); temp_contactgroup != NULL; temp_contactgroup = (xodtemplate_contactgroup *)skiplist_get_next(&ptr)) { + if((mcount = xodtemplate_register_contactgroup_members(temp_contactgroup)) < 0) + return ERROR; + tot_members += mcount; + } + timing_point("%u contactgroup memberships registered\n", tot_members); + + /* register hostgroup members */ + ptr = NULL; tot_members = 0; + for(temp_hostgroup = (xodtemplate_hostgroup *)skiplist_get_first(xobject_skiplists[HOSTGROUP_SKIPLIST], &ptr); temp_hostgroup != NULL; temp_hostgroup = (xodtemplate_hostgroup *)skiplist_get_next(&ptr)) { + if((mcount = xodtemplate_register_hostgroup_members(temp_hostgroup)) < 0) + return ERROR; + tot_members += mcount; + } + timing_point("%u hostgroup memberships registered\n", tot_members); + + /* register servicegroup members */ + ptr = NULL; tot_members = 0; + for(temp_servicegroup = (xodtemplate_servicegroup *)skiplist_get_first(xobject_skiplists[SERVICEGROUP_SKIPLIST], &ptr); temp_servicegroup != NULL; temp_servicegroup = (xodtemplate_servicegroup *)skiplist_get_next(&ptr)) { + if((mcount = xodtemplate_register_servicegroup_members(temp_servicegroup)) < 0) + return ERROR; + tot_members += mcount; + } + timing_point("%u servicegroup memberships registered\n", tot_members); + /* * These aren't in skiplists at all, but it's safe to destroy * them as we go along, since all dupes are at the head of the list @@ -7212,11 +7306,9 @@ } - /* registers a contactgroup definition */ int xodtemplate_register_contactgroup(xodtemplate_contactgroup *this_contactgroup) { contactgroup *new_contactgroup = NULL; - objectlist *list; /* bail out if we shouldn't register this object */ if(this_contactgroup->register_object == FALSE) @@ -7231,11 +7323,6 @@ return ERROR; } - for(list = this_contactgroup->member_list; list; list = list->next) { - xodtemplate_contact *c = (xodtemplate_contact *)list->object_ptr; - add_contact_to_contactgroup(new_contactgroup, c->contact_name); - } - return OK; } @@ -7244,7 +7331,6 @@ /* registers a hostgroup definition */ int xodtemplate_register_hostgroup(xodtemplate_hostgroup *this_hostgroup) { hostgroup *new_hostgroup = NULL; - objectlist *list; /* bail out if we shouldn't register this object */ if(this_hostgroup->register_object == FALSE) @@ -7258,20 +7344,13 @@ logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Could not register hostgroup (config file '%s', starting on line %d)\n", xodtemplate_config_file_name(this_hostgroup->_config_file), this_hostgroup->_start_line); return ERROR; } - for(list = this_hostgroup->member_list; list; list = list->next) { - xodtemplate_host *h = (xodtemplate_host *)list->object_ptr; - add_host_to_hostgroup(new_hostgroup, h->host_name); - } return OK; } - - /* registers a servicegroup definition */ int xodtemplate_register_servicegroup(xodtemplate_servicegroup *this_servicegroup) { servicegroup *new_servicegroup = NULL; - objectlist *list, *next; /* bail out if we shouldn't register this object */ if(this_servicegroup->register_object == FALSE) @@ -7286,17 +7365,10 @@ return ERROR; } - for(list = this_servicegroup->member_list; list; list = next) { - xodtemplate_service *s = (xodtemplate_service *)list->object_ptr; - next = list->next; - add_service_to_servicegroup(new_servicegroup, s->host_name, s->service_description); - } - return OK; } - /* registers a servicedependency definition */ int xodtemplate_register_servicedependency(xodtemplate_servicedependency *this_servicedependency) { servicedependency *new_servicedependency = NULL; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |