diff --git a/README.md b/README.md index 88c32b99..faf616ab 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## Welcome to Naemon Core ## -![GitHub Workflow Status](https://img.shields.io/github/workflow/status/naemon/naemon-core/citest) +![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/naemon/naemon-core/citest.yml) Naemon is a host/service/network monitoring program written in C and released under the GNU General Public License. It works by scheduling diff --git a/naemon-core.spec b/naemon-core.spec index 53d66758..3eb5261d 100644 --- a/naemon-core.spec +++ b/naemon-core.spec @@ -11,7 +11,6 @@ Packager: Naemon Core Development Team Vendor: Naemon Core Development Team Source0: naemon-%{version}.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-%{release} -Obsoletes: naemon-tools BuildRequires: gperf BuildRequires: logrotate BuildRequires: autoconf diff --git a/src/naemon/broker.h b/src/naemon/broker.h index 4e7b0b77..08a833c8 100644 --- a/src/naemon/broker.h +++ b/src/naemon/broker.h @@ -137,7 +137,7 @@ /****** EVENT FLAGS ************************/ #define NEBFLAG_NONE 0 -#define NEBFLAG_PROCESS_INITIATED 1 /* event was initiated by Nagios process */ +#define NEBFLAG_PROCESS_INITIATED 1 /* event was initiated by Naemon process */ #define NEBFLAG_USER_INITIATED 2 /* event was initiated by a user request */ #define NEBFLAG_MODULE_INITIATED 3 /* event was initiated by an event broker module */ diff --git a/src/naemon/checks_host.c b/src/naemon/checks_host.c index 91924f5b..f9246254 100644 --- a/src/naemon/checks_host.c +++ b/src/naemon/checks_host.c @@ -140,17 +140,14 @@ static void handle_host_check_event(struct nm_event_execution_properties *evprop host *hst = (host *)evprop->user_data; double latency; struct timeval tv; - struct timeval event_runtime; int options = hst->check_options; int result = OK; if (evprop->execution_type == EVENT_EXEC_NORMAL) { /* get event latency */ + latency = evprop->attributes.timed.latency; gettimeofday(&tv, NULL); - event_runtime.tv_sec = hst->next_check; - event_runtime.tv_usec = 0; - latency = (double)(tv_delta_f(&event_runtime, &tv)); /* When the callback is called, the pointer to the timed event is invalid */ hst->next_check_event = NULL; diff --git a/src/naemon/checks_service.c b/src/naemon/checks_service.c index 6d1abc9b..a63cb869 100644 --- a/src/naemon/checks_service.c +++ b/src/naemon/checks_service.c @@ -143,7 +143,6 @@ static void handle_service_check_event(struct nm_event_execution_properties *evp int nudge_seconds = 0; double latency; struct timeval tv; - struct timeval event_runtime; int options = temp_service->check_options; host *temp_host = NULL; @@ -152,10 +151,8 @@ static void handle_service_check_event(struct nm_event_execution_properties *evp if (evprop->execution_type == EVENT_EXEC_NORMAL) { /* get event latency */ + latency = evprop->attributes.timed.latency; gettimeofday(&tv, NULL); - event_runtime.tv_sec = temp_service->next_check; - event_runtime.tv_usec = 0; - latency = (double)(tv_delta_f(&event_runtime, &tv)); /* When the callback is called, the pointer to the timed event is invalid */ temp_service->next_check_event = NULL; @@ -486,6 +483,8 @@ int handle_async_service_check_result(service *temp_service, check_result *queue /* was this check passive or active? */ temp_service->check_type = (queued_check_result->check_type == CHECK_TYPE_ACTIVE) ? CHECK_TYPE_ACTIVE : CHECK_TYPE_PASSIVE; + temp_service->latency = queued_check_result->latency; + /* update check statistics for passive checks */ if (queued_check_result->check_type == CHECK_TYPE_PASSIVE) update_check_stats(PASSIVE_SERVICE_CHECK_STATS, queued_check_result->start_time.tv_sec); diff --git a/src/naemon/commands.c b/src/naemon/commands.c index 8416d32e..7d7a371c 100644 --- a/src/naemon/commands.c +++ b/src/naemon/commands.c @@ -183,7 +183,8 @@ int close_command_file(void) command_file_created = FALSE; /* close the command file */ - fclose(command_file_fp); + if (command_file_fp != NULL) + fclose(command_file_fp); return OK; } @@ -1848,7 +1849,7 @@ static int host_command_handler(const struct external_command *ext_command, time } return OK; case CMD_DEL_ALL_HOST_COMMENTS: - return delete_all_host_comments(target_host->name); + return delete_all_host_comments(target_host); case CMD_ENABLE_HOST_NOTIFICATIONS: enable_host_notifications(target_host); return OK; @@ -2251,7 +2252,7 @@ static int service_command_handler(const struct external_command *ext_command, t target_service->next_notification = GV_TIMESTAMP("notification_time"); return OK; case CMD_DEL_ALL_SVC_COMMENTS: - return delete_all_comments(SERVICE_COMMENT, target_service->host_name, target_service->description); + return delete_all_service_comments(target_service); case CMD_ENABLE_SVC_NOTIFICATIONS: enable_service_notifications(target_service); return OK; diff --git a/src/naemon/comments.c b/src/naemon/comments.c index 0c5ab190..47be837a 100644 --- a/src/naemon/comments.c +++ b/src/naemon/comments.c @@ -9,10 +9,9 @@ #include "events.h" #include "globals.h" #include "nm_alloc.h" +#include -comment *comment_list = NULL; -int defer_comment_sorting = 0; -comment **comment_hashlist = NULL; +GHashTable *comment_hashtable = NULL; /******************************************************************/ @@ -22,20 +21,8 @@ comment **comment_hashlist = NULL; /* initializes comment data */ int initialize_comment_data(void) { - comment *temp_comment = NULL; - - /* find the new starting index for comment id if its missing*/ - if (next_comment_id == 0L) { - for (temp_comment = comment_list; temp_comment != NULL; temp_comment = temp_comment->next) { - if (temp_comment->comment_id >= next_comment_id) - next_comment_id = temp_comment->comment_id + 1; - } - } - - /* initialize next comment id if necessary */ - if (next_comment_id == 0L) - next_comment_id = 1; - + comment_hashtable = g_hash_table_new(g_direct_hash, g_direct_equal); + next_comment_id = 1; return OK; } @@ -71,20 +58,30 @@ int add_new_comment(int type, int entry_type, char *host_name, char *svc_descrip return result; } +static unsigned long get_next_comment_id(void) +{ + unsigned long new_id = next_comment_id; + for (;;) { + if (!find_comment(new_id, HOST_COMMENT | SERVICE_COMMENT)) { + return new_id; + } + new_id++; + } + return 0; +} + /* adds a new host comment */ int add_new_host_comment(int entry_type, char *host_name, time_t entry_time, char *author_name, char *comment_data, int persistent, int source, int expires, time_t expire_time, unsigned long *comment_id) { int result = OK; if (!find_host(host_name)) { - nm_log(NSLOG_RUNTIME_ERROR, "Error: Ignoring request to add comment to non-existing host '%s'.\n", - host_name); + nm_log(NSLOG_RUNTIME_ERROR, "Error: Ignoring request to add comment to non-existing host '%s'.\n", host_name); return ERROR; } /* find the next valid comment id */ - while (find_comment(next_comment_id, HOST_COMMENT | SERVICE_COMMENT) != NULL) - next_comment_id++; + next_comment_id = get_next_comment_id(); /* add comment to list in memory */ add_host_comment(entry_type, host_name, entry_time, author_name, comment_data, next_comment_id, persistent, expires, expire_time, source); @@ -94,7 +91,7 @@ int add_new_host_comment(int entry_type, char *host_name, time_t entry_time, cha broker_comment_data(NEBTYPE_COMMENT_ADD, NEBFLAG_NONE, NEBATTR_NONE, HOST_COMMENT, entry_type, host_name, NULL, entry_time, author_name, comment_data, persistent, source, expires, expire_time, next_comment_id); - /* increment the comment id, AFTER, broker_comment_data(), + /* increment the comment id AFTER broker_comment_data(), * as we use it in that call */ next_comment_id++; @@ -107,14 +104,12 @@ int add_new_host_comment(int entry_type, char *host_name, time_t entry_time, cha int add_new_service_comment(int entry_type, char *host_name, char *svc_description, time_t entry_time, char *author_name, char *comment_data, int persistent, int source, int expires, time_t expire_time, unsigned long *comment_id) { if (!find_service(host_name, svc_description)) { - nm_log(NSLOG_RUNTIME_ERROR, "Error: Ignoring request to add comment to non-existing service '%s' on host '%s'\n", - svc_description, host_name); + nm_log(NSLOG_RUNTIME_ERROR, "Error: Ignoring request to add comment to non-existing service '%s' on host '%s'\n", svc_description, host_name); return ERROR; } /* find the next valid comment id */ - while (find_comment(next_comment_id, HOST_COMMENT | SERVICE_COMMENT) != NULL) - next_comment_id++; + next_comment_id = get_next_comment_id(); /* add comment to list in memory */ add_service_comment(entry_type, host_name, svc_description, entry_time, author_name, comment_data, next_comment_id, persistent, expires, expire_time, source); @@ -124,8 +119,7 @@ int add_new_service_comment(int entry_type, char *host_name, char *svc_descripti broker_comment_data(NEBTYPE_COMMENT_ADD, NEBFLAG_NONE, NEBATTR_NONE, SERVICE_COMMENT, entry_type, host_name, svc_description, entry_time, author_name, comment_data, persistent, source, expires, expire_time, next_comment_id); - /* - * increment the comment id, AFTER broker_comment_data, + /* increment the comment id AFTER broker_comment_data(), * as we use it in that call */ next_comment_id++; @@ -142,52 +136,27 @@ int add_new_service_comment(int entry_type, char *host_name, char *svc_descripti int delete_comment(int type, unsigned long comment_id) { comment *this_comment = NULL; - comment *last_comment = NULL; - comment *next_comment = NULL; - int hashslot = 0; - comment *this_hash = NULL; - comment *last_hash = NULL; /* find the comment we should remove */ - for (this_comment = comment_list, last_comment = comment_list; this_comment != NULL; this_comment = next_comment) { - next_comment = this_comment->next; - - /* we found the comment we should delete */ - if (this_comment->comment_id == comment_id && this_comment->comment_type == type) - break; - - last_comment = this_comment; - } - + this_comment = find_comment(comment_id, type); if (this_comment == NULL) return ERROR; broker_comment_data(NEBTYPE_COMMENT_DELETE, NEBFLAG_NONE, NEBATTR_NONE, type, this_comment->entry_type, this_comment->host_name, this_comment->service_description, this_comment->entry_time, this_comment->author, this_comment->comment_data, this_comment->persistent, this_comment->source, this_comment->expires, this_comment->expire_time, comment_id); /* remove the comment from the list in memory */ - /* first remove from chained hash list */ - hashslot = hashfunc(this_comment->host_name, NULL, COMMENT_HASHSLOTS); - last_hash = NULL; - for (this_hash = comment_hashlist[hashslot]; this_hash; this_hash = this_hash->nexthash) { - if (this_hash == this_comment) { - if (last_hash) - last_hash->nexthash = this_hash->nexthash; - else { - if (this_hash->nexthash) - comment_hashlist[hashslot] = this_hash->nexthash; - else - comment_hashlist[hashslot] = NULL; - } - break; - } - last_hash = this_hash; - } + g_hash_table_remove(comment_hashtable, GINT_TO_POINTER(this_comment->comment_id)); - /* then removed from linked list */ - if (comment_list == this_comment) - comment_list = this_comment->next; - else - last_comment->next = next_comment; + // remove from svc or host + if (type == HOST_COMMENT) { + host *temp_host = find_host(this_comment->host_name); + remove_object_from_objectlist(&temp_host->comments_list, this_comment); + } + else if (type == SERVICE_COMMENT) { + service *temp_service = find_service(this_comment->host_name, this_comment->service_description); + temp_service->comments_list = NULL; + remove_object_from_objectlist(&temp_service->comments_list, this_comment); + } nm_free(this_comment->host_name); nm_free(this_comment->service_description); @@ -223,147 +192,77 @@ int delete_service_comment(unsigned long comment_id) } -/* deletes all comments for a particular host or service */ -int delete_all_comments(int type, char *host_name, char *svc_description) -{ - int result = OK; - - if (type == HOST_COMMENT) - result = delete_all_host_comments(host_name); - else - result = delete_all_service_comments(host_name, svc_description); - - return result; -} - - /* deletes all comments for a particular host */ -int delete_all_host_comments(char *host_name) +int delete_all_host_comments(host *hst) { - int result = OK; - comment *temp_comment = NULL; - comment *next_comment = NULL; - - if (host_name == NULL) - return ERROR; + objectlist *temp_obj, *next = NULL; + comment * temp_comment = NULL; /* delete host comments from memory */ - for (temp_comment = get_first_comment_by_host(host_name); temp_comment != NULL; temp_comment = next_comment) { - next_comment = get_next_comment_by_host(host_name, temp_comment); - if (temp_comment->comment_type == HOST_COMMENT) - delete_comment(HOST_COMMENT, temp_comment->comment_id); + for (temp_obj = hst->comments_list; temp_obj != NULL; ) { + next = temp_obj->next; + temp_comment = temp_obj->object_ptr; + delete_comment(HOST_COMMENT, temp_comment->comment_id); + temp_obj = next; } - return result; + return OK; } /* deletes all non-persistent acknowledgement comments for a particular host */ int delete_host_acknowledgement_comments(host *hst) { - int result = OK; - comment *temp_comment = NULL; - comment *next_comment = NULL; - - if (hst == NULL) - return ERROR; + objectlist *temp_obj, *next = NULL; + comment * temp_comment = NULL; - /* delete comments from memory */ - temp_comment = get_first_comment_by_host(hst->name); - while (temp_comment) { - next_comment = get_next_comment_by_host(hst->name, temp_comment); - if (temp_comment->comment_type == HOST_COMMENT && temp_comment->entry_type == ACKNOWLEDGEMENT_COMMENT && temp_comment->persistent == FALSE) { + /* delete host comments from memory */ + for (temp_obj = hst->comments_list; temp_obj != NULL;) { + next = temp_obj->next; + temp_comment = temp_obj->object_ptr; + if (temp_comment->comment_type == HOST_COMMENT && temp_comment->entry_type == ACKNOWLEDGEMENT_COMMENT && temp_comment->persistent == FALSE) delete_comment(HOST_COMMENT, temp_comment->comment_id); - } - temp_comment = next_comment; + temp_obj = next; } - return result; + return OK; } /* deletes all comments for a particular service */ -int delete_all_service_comments(char *host_name, char *svc_description) +int delete_all_service_comments(service *svc) { - int result = OK; - comment *temp_comment = NULL; - comment *next_comment = NULL; - - if (host_name == NULL || svc_description == NULL) - return ERROR; + objectlist *temp_obj, *next = NULL; + comment * temp_comment = NULL; /* delete service comments from memory */ - for (temp_comment = comment_list; temp_comment != NULL; temp_comment = next_comment) { - next_comment = temp_comment->next; - if (temp_comment->comment_type == SERVICE_COMMENT && !g_strcmp0(temp_comment->host_name, host_name) && !g_strcmp0(temp_comment->service_description, svc_description)) - delete_comment(SERVICE_COMMENT, temp_comment->comment_id); + for (temp_obj = svc->comments_list; temp_obj != NULL;) { + next = temp_obj->next; + temp_comment = temp_obj->object_ptr; + delete_comment(SERVICE_COMMENT, temp_comment->comment_id); + temp_obj = next; } - return result; + return OK; } /* deletes all non-persistent acknowledgement comments for a particular service */ int delete_service_acknowledgement_comments(service *svc) { - int result = OK; - comment *temp_comment = NULL; - comment *next_comment = NULL; - - if (svc == NULL) - return ERROR; + objectlist *temp_obj, *next = NULL; + comment * temp_comment = NULL; /* delete comments from memory */ - for (temp_comment = comment_list; temp_comment != NULL; temp_comment = next_comment) { - next_comment = temp_comment->next; - if (temp_comment->comment_type == SERVICE_COMMENT && !g_strcmp0(temp_comment->host_name, svc->host_name) && !g_strcmp0(temp_comment->service_description, svc->description) && temp_comment->entry_type == ACKNOWLEDGEMENT_COMMENT && temp_comment->persistent == FALSE) + for (temp_obj = svc->comments_list; temp_obj != NULL;) { + next = temp_obj->next; + temp_comment = temp_obj->object_ptr; + if (temp_comment->comment_type == SERVICE_COMMENT && temp_comment->entry_type == ACKNOWLEDGEMENT_COMMENT && temp_comment->persistent == FALSE) delete_comment(SERVICE_COMMENT, temp_comment->comment_id); + temp_obj = next; } - return result; -} - - -/******************************************************************/ -/****************** CHAINED HASH FUNCTIONS ************************/ -/******************************************************************/ - -/* adds comment to hash list in memory */ -int add_comment_to_hashlist(comment *new_comment) -{ - comment *temp_comment = NULL; - comment *lastpointer = NULL; - int hashslot = 0; - - /* initialize hash list */ - if (comment_hashlist == NULL) { - int i; - - comment_hashlist = nm_malloc(sizeof(comment *) * COMMENT_HASHSLOTS); - - for (i = 0; i < COMMENT_HASHSLOTS; i++) - comment_hashlist[i] = NULL; - } - - if (!new_comment) - return 0; - - hashslot = hashfunc(new_comment->host_name, NULL, COMMENT_HASHSLOTS); - lastpointer = NULL; - for (temp_comment = comment_hashlist[hashslot]; temp_comment && g_strcmp0(temp_comment->host_name, new_comment->host_name) < 0; temp_comment = temp_comment->nexthash) { - if (g_strcmp0(temp_comment->host_name, new_comment->host_name) >= 0) - break; - lastpointer = temp_comment; - } - - /* multiples are allowed */ - if (lastpointer) - lastpointer->nexthash = new_comment; - else - comment_hashlist[hashslot] = new_comment; - new_comment->nexthash = temp_comment; - - return 1; + return OK; } @@ -397,14 +296,24 @@ int add_service_comment(int entry_type, char *host_name, char *svc_description, int add_comment(int comment_type, int entry_type, char *host_name, char *svc_description, time_t entry_time, char *author, char *comment_data, unsigned long comment_id, int persistent, int expires, time_t expire_time, int source) { comment *new_comment = NULL; - comment *last_comment = NULL; - comment *temp_comment = NULL; - int result = OK; + host *temp_host = NULL; + service *temp_service = NULL; /* make sure we have the data we need */ if (host_name == NULL || author == NULL || comment_data == NULL || (comment_type == SERVICE_COMMENT && svc_description == NULL)) return ERROR; + if (comment_type == HOST_COMMENT) { + temp_host = find_host(host_name); + if(temp_host == NULL) + return ERROR; + } + else if (comment_type == SERVICE_COMMENT) { + temp_service = find_service(host_name, svc_description); + if(temp_service == NULL) + return ERROR; + } + /* allocate memory for the comment */ new_comment = nm_calloc(1, sizeof(comment)); @@ -425,47 +334,13 @@ int add_comment(int comment_type, int entry_type, char *host_name, char *svc_des new_comment->expires = (expires == TRUE) ? TRUE : FALSE; new_comment->expire_time = expire_time; - /* add comment to hash list */ - if (result == OK) { - if (!add_comment_to_hashlist(new_comment)) - result = ERROR; - } + g_hash_table_insert(comment_hashtable, GINT_TO_POINTER(new_comment->comment_id), new_comment); - /* handle errors */ - if (result == ERROR) { - nm_free(new_comment->comment_data); - nm_free(new_comment->author); - nm_free(new_comment->service_description); - nm_free(new_comment->host_name); - nm_free(new_comment); - return ERROR; - } + if (comment_type == HOST_COMMENT) + prepend_object_to_objectlist(&temp_host->comments_list, (void *)new_comment); - if (defer_comment_sorting) { - new_comment->next = comment_list; - comment_list = new_comment; - } else { - /* add new comment to comment list, sorted by comment id */ - last_comment = comment_list; - for (temp_comment = comment_list; temp_comment != NULL; temp_comment = temp_comment->next) { - if (new_comment->comment_id < temp_comment->comment_id) { - new_comment->next = temp_comment; - if (temp_comment == comment_list) - comment_list = new_comment; - else - last_comment->next = new_comment; - break; - } else - last_comment = temp_comment; - } - if (comment_list == NULL) { - new_comment->next = NULL; - comment_list = new_comment; - } else if (temp_comment == NULL) { - new_comment->next = NULL; - last_comment->next = new_comment; - } - } + if (comment_type == SERVICE_COMMENT) + prepend_object_to_objectlist(&temp_service->comments_list, (void *)new_comment); broker_comment_data(NEBTYPE_COMMENT_LOAD, NEBFLAG_NONE, NEBATTR_NONE, comment_type, entry_type, host_name, svc_description, entry_time, author, comment_data, persistent, source, expires, expire_time, comment_id); @@ -473,50 +348,6 @@ int add_comment(int comment_type, int entry_type, char *host_name, char *svc_des } -static int comment_compar(const void *p1, const void *p2) -{ - comment *c1 = *(comment **)p1; - comment *c2 = *(comment **)p2; - return c1->comment_id - c2->comment_id; -} - - -int sort_comments(void) -{ - comment **array, *temp_comment; - unsigned long i = 0, unsorted_comments = 0; - - if (!defer_comment_sorting) - return OK; - defer_comment_sorting = 0; - - temp_comment = comment_list; - while (temp_comment != NULL) { - temp_comment = temp_comment->next; - unsorted_comments++; - } - - if (!unsorted_comments) - return OK; - - array = nm_malloc(sizeof(*array) * unsorted_comments); - while (comment_list) { - array[i++] = comment_list; - comment_list = comment_list->next; - } - - qsort((void *)array, i, sizeof(*array), comment_compar); - comment_list = temp_comment = array[0]; - for (i = 1; i < unsorted_comments; i++) { - temp_comment->next = array[i]; - temp_comment = temp_comment->next; - } - temp_comment->next = NULL; - nm_free(array); - return OK; -} - - /******************************************************************/ /********************* CLEANUP FUNCTIONS **************************/ /******************************************************************/ @@ -524,23 +355,25 @@ int sort_comments(void) /* frees memory allocated for the comment data */ void free_comment_data(void) { - comment *this_comment = NULL; - comment *next_comment = NULL; + GHashTableIter iter; + gpointer comment_; + + if(comment_hashtable == NULL) + return; /* free memory for the comment list */ - for (this_comment = comment_list; this_comment != NULL; this_comment = next_comment) { - next_comment = this_comment->next; - nm_free(this_comment->host_name); - nm_free(this_comment->service_description); - nm_free(this_comment->author); - nm_free(this_comment->comment_data); - nm_free(this_comment); + g_hash_table_iter_init(&iter, comment_hashtable); + while (g_hash_table_iter_next(&iter, NULL, &comment_)) { + comment *temp_comment = comment_; + nm_free(temp_comment->host_name); + nm_free(temp_comment->service_description); + nm_free(temp_comment->author); + nm_free(temp_comment->comment_data); + nm_free(temp_comment); } - /* free hash list and reset list pointer */ - nm_free(comment_hashlist); - comment_hashlist = NULL; - comment_list = NULL; + g_hash_table_destroy(comment_hashtable); + comment_hashtable = NULL; return; } @@ -553,16 +386,19 @@ void free_comment_data(void) /* get the number of comments associated with a particular host */ int number_of_host_comments(char *host_name) { - comment *temp_comment = NULL; + objectlist *temp_obj = NULL; + host * temp_host = NULL; int total_comments = 0; if (host_name == NULL) return 0; - for (temp_comment = get_first_comment_by_host(host_name); temp_comment != NULL; temp_comment = get_next_comment_by_host(host_name, temp_comment)) { - if (temp_comment->comment_type == HOST_COMMENT) - total_comments++; - } + temp_host = find_host(host_name); + if (temp_host == NULL) + return 0; + + for (temp_obj = temp_host->comments_list; temp_obj != NULL; temp_obj = temp_obj->next) + total_comments++; return total_comments; } @@ -571,53 +407,29 @@ int number_of_host_comments(char *host_name) /* get the number of comments associated with a particular service */ int number_of_service_comments(char *host_name, char *svc_description) { - comment *temp_comment = NULL; + objectlist *temp_obj = NULL; + service *temp_service = NULL; int total_comments = 0; if (host_name == NULL || svc_description == NULL) return 0; - for (temp_comment = get_first_comment_by_host(host_name); temp_comment != NULL; temp_comment = get_next_comment_by_host(host_name, temp_comment)) { - if (temp_comment->comment_type == SERVICE_COMMENT && !g_strcmp0(temp_comment->service_description, svc_description)) - total_comments++; - } - - return total_comments; -} - - -/******************************************************************/ -/********************* TRAVERSAL FUNCTIONS ************************/ -/******************************************************************/ + temp_service = find_service(host_name, svc_description); + if (temp_service == NULL) + return 0; -comment *get_first_comment_by_host(char *host_name) -{ + for (temp_obj = temp_service->comments_list; temp_obj != NULL; temp_obj = temp_obj->next) + total_comments++; - return get_next_comment_by_host(host_name, NULL); + return total_comments; } - -comment *get_next_comment_by_host(char *host_name, comment *start) +/* get the total number of comments */ +int number_of_comments() { - comment *temp_comment = NULL; - - if (host_name == NULL || comment_hashlist == NULL) - return NULL; - - if (start == NULL) - temp_comment = comment_hashlist[hashfunc(host_name, NULL, COMMENT_HASHSLOTS)]; - else - temp_comment = start->nexthash; - - for (; temp_comment && g_strcmp0(temp_comment->host_name, host_name) < 0; temp_comment = temp_comment->nexthash); - - if (temp_comment && g_strcmp0(temp_comment->host_name, host_name) == 0) - return temp_comment; - - return NULL; + return (int)g_hash_table_size(comment_hashtable); } - /******************************************************************/ /********************** SEARCH FUNCTIONS **************************/ /******************************************************************/ @@ -641,10 +453,9 @@ comment *find_comment(unsigned long comment_id, int comment_type) { comment *temp_comment = NULL; - for (temp_comment = comment_list; temp_comment != NULL; temp_comment = temp_comment->next) { - if (temp_comment->comment_id == comment_id && (temp_comment->comment_type & comment_type)) - return temp_comment; - } + temp_comment = g_hash_table_lookup(comment_hashtable, GINT_TO_POINTER(comment_id)); + if (temp_comment && (temp_comment->comment_type & comment_type)) + return temp_comment; return NULL; } diff --git a/src/naemon/comments.h b/src/naemon/comments.h index ea176a7b..0e3bffb3 100644 --- a/src/naemon/comments.h +++ b/src/naemon/comments.h @@ -26,11 +26,6 @@ #define FLAPPING_COMMENT 3 #define ACKNOWLEDGEMENT_COMMENT 4 - -/*************************** CHAINED HASH LIMITS ***************************/ -#define COMMENT_HASHSLOTS 1024 - - /**************************** DATA STRUCTURES ******************************/ NAGIOS_BEGIN_DECL @@ -49,11 +44,11 @@ typedef struct comment { char *service_description; char *author; char *comment_data; + struct comment *prev; struct comment *next; - struct comment *nexthash; } comment; -extern struct comment *comment_list; +extern GHashTable *comment_hashtable; int initialize_comment_data(void); /* initializes comment data */ int add_new_comment(int, int, char *, char *, time_t, char *, char *, int, int, int, time_t, unsigned long *); /* adds a new host or service comment */ @@ -62,29 +57,23 @@ int add_new_service_comment(int, char *, char *, time_t, char *, char *, int, in int delete_comment(int, unsigned long); /* deletes a host or service comment */ int delete_host_comment(unsigned long); /* deletes a host comment */ int delete_service_comment(unsigned long); /* deletes a service comment */ -int delete_all_comments(int, char *, char *); /* deletes all comments for a particular host or service */ -int delete_all_host_comments(char *); /* deletes all comments for a specific host */ +int delete_all_host_comments(struct host *); /* deletes all comments for a specific host */ int delete_host_acknowledgement_comments(struct host *); /* deletes all non-persistent ack comments for a specific host */ -int delete_all_service_comments(char *, char *); /* deletes all comments for a specific service */ +int delete_all_service_comments(struct service *); /* deletes all comments for a specific service */ int delete_service_acknowledgement_comments(struct service *); /* deletes all non-persistent ack comments for a specific service */ struct comment *find_comment(unsigned long, int); /* finds a specific comment */ struct comment *find_service_comment(unsigned long); /* finds a specific service comment */ struct comment *find_host_comment(unsigned long); /* finds a specific host comment */ -struct comment *get_first_comment_by_host(char *); -struct comment *get_next_comment_by_host(char *, struct comment *); - -int number_of_host_comments(char *); /* returns the number of comments associated with a particular host */ -int number_of_service_comments(char *, char *); /* returns the number of comments associated with a particular service */ +int number_of_host_comments(char *); /* returns the number of comments associated with a particular host */ +int number_of_service_comments(char *, char *); /* returns the number of comments associated with a particular service */ +int number_of_comments(void); int add_comment(int, int, char *, char *, time_t, char *, char *, unsigned long, int, int, time_t, int); /* adds a comment (host or service) */ -int sort_comments(void); int add_host_comment(int, char *, time_t, char *, char *, unsigned long, int, int, time_t, int); /* adds a host comment */ int add_service_comment(int, char *, char *, time_t, char *, char *, unsigned long, int, int, time_t, int); /* adds a service comment */ -int add_comment_to_hashlist(struct comment *); - void free_comment_data(void); /* frees memory allocated to the comment list */ NAGIOS_END_DECL diff --git a/src/naemon/common.h b/src/naemon/common.h index 21e5bdad..26e8ed07 100644 --- a/src/naemon/common.h +++ b/src/naemon/common.h @@ -19,8 +19,6 @@ extern char illegal_output_char_map[256]; extern int log_rotation_method; extern int check_external_commands; -/* set this if you're going to add a ton of comments at once */ -extern int defer_comment_sorting; extern unsigned long next_downtime_id; extern char *object_cache_file; diff --git a/src/naemon/downtime.c b/src/naemon/downtime.c index 2343b385..1e0da0a7 100644 --- a/src/naemon/downtime.c +++ b/src/naemon/downtime.c @@ -488,9 +488,9 @@ int register_downtime(int type, unsigned long downtime_id) /* add a non-persistent comment to the host or service regarding the scheduled outage */ if (find_comment(temp_downtime->comment_id, HOST_COMMENT | SERVICE_COMMENT) == NULL) { if (temp_downtime->type == SERVICE_DOWNTIME) - add_new_comment(SERVICE_COMMENT, DOWNTIME_COMMENT, svc->host_name, svc->description, time(NULL), (NULL == temp_downtime->author ? "(Nagios Process)" : temp_downtime->author), temp_buffer, 0, COMMENTSOURCE_INTERNAL, FALSE, (time_t)0, &(temp_downtime->comment_id)); + add_new_comment(SERVICE_COMMENT, DOWNTIME_COMMENT, svc->host_name, svc->description, time(NULL), (NULL == temp_downtime->author ? "(Naemon Process)" : temp_downtime->author), temp_buffer, 0, COMMENTSOURCE_INTERNAL, FALSE, (time_t)0, &(temp_downtime->comment_id)); else - add_new_comment(HOST_COMMENT, DOWNTIME_COMMENT, hst->name, NULL, time(NULL), (NULL == temp_downtime->author ? "(Nagios Process)" : temp_downtime->author), temp_buffer, 0, COMMENTSOURCE_INTERNAL, FALSE, (time_t)0, &(temp_downtime->comment_id)); + add_new_comment(HOST_COMMENT, DOWNTIME_COMMENT, hst->name, NULL, time(NULL), (NULL == temp_downtime->author ? "(Naemon Process)" : temp_downtime->author), temp_buffer, 0, COMMENTSOURCE_INTERNAL, FALSE, (time_t)0, &(temp_downtime->comment_id)); } nm_free(temp_buffer); @@ -1298,6 +1298,12 @@ scheduled_downtime *find_service_downtime(unsigned long downtime_id) return find_downtime(SERVICE_DOWNTIME, downtime_id); } +/* get the total number of downtimes */ +int number_of_downtimes() +{ + return (int)g_hash_table_size(dt_hashtable); +} + /******************************************************************/ /********************* CLEANUP FUNCTIONS **************************/ @@ -1309,7 +1315,8 @@ void free_downtime_data(void) scheduled_downtime *this_downtime = NULL; scheduled_downtime *next_downtime = NULL; - g_hash_table_destroy(dt_hashtable); + if(dt_hashtable != NULL) + g_hash_table_destroy(dt_hashtable); dt_hashtable = NULL; /* free memory for the scheduled_downtime list */ diff --git a/src/naemon/downtime.h b/src/naemon/downtime.h index c7cfa230..fd39e533 100644 --- a/src/naemon/downtime.h +++ b/src/naemon/downtime.h @@ -30,7 +30,6 @@ typedef struct scheduled_downtime { char *comment; unsigned long comment_id; int start_flex_downtime; - int incremented_pending_downtime; /* UNUSED */ struct scheduled_downtime *next; struct timed_event *start_event, *stop_event; struct scheduled_downtime *prev; @@ -74,6 +73,7 @@ int sort_downtime(void); struct scheduled_downtime *find_downtime(int, unsigned long); struct scheduled_downtime *find_host_downtime(unsigned long); struct scheduled_downtime *find_service_downtime(unsigned long); +int number_of_downtimes(void); void free_downtime_data(void); /* frees memory allocated to scheduled downtime list */ diff --git a/src/naemon/naemon.c b/src/naemon/naemon.c index 9e012613..72979036 100644 --- a/src/naemon/naemon.c +++ b/src/naemon/naemon.c @@ -638,6 +638,11 @@ int main(int argc, char **argv) initialize_downtime_data(); timing_point("Initialized downtime data\n"); + /* initialize comment data */ + timing_point("Initializing comment data\n"); + initialize_comment_data(); + timing_point("Initialized comment data\n"); + /* read initial service and host state information */ timing_point("Initializing retention data\n"); initialize_retention_data(); @@ -646,11 +651,8 @@ int main(int argc, char **argv) timing_point("Reading initial state information\n"); read_initial_state_information(); timing_point("Read initial state information\n"); - - /* initialize comment data */ - timing_point("Initializing comment data\n"); - initialize_comment_data(); - timing_point("Initialized comment data\n"); + timing_point("Restored %d downtimes\n", number_of_downtimes()); + timing_point("Restored %d comments\n", number_of_comments()); /* initialize performance data */ timing_point("Initializing performance data\n"); diff --git a/src/naemon/nebmodules.h b/src/naemon/nebmodules.h index f1d619b0..6fe064b9 100644 --- a/src/naemon/nebmodules.h +++ b/src/naemon/nebmodules.h @@ -10,7 +10,7 @@ NAGIOS_BEGIN_DECL /***** MODULE VERSION INFORMATION *****/ #define NEB_API_VERSION(x) int __neb_api_version = x; -#define CURRENT_NEB_API_VERSION 5 +#define CURRENT_NEB_API_VERSION 6 /***** MODULE INFORMATION *****/ diff --git a/src/naemon/notifications.h b/src/naemon/notifications.h index ee15f1dc..fade769e 100644 --- a/src/naemon/notifications.h +++ b/src/naemon/notifications.h @@ -83,7 +83,7 @@ typedef struct notify_list { struct notify_list *next; } notification; -const char *notification_reason_name(unsigned int reason_type); +const char *notification_reason_name(enum NotificationReason reason_type); int check_service_notification_viability(service *, int, int); /* checks viability of notifying all contacts about a service */ int is_valid_escalation_for_service_notification(service *, serviceescalation *, int); /* checks if an escalation entry is valid for a particular service notification */ int should_service_notification_be_escalated(service *); /* checks if a service notification should be escalated */ diff --git a/src/naemon/objectlist.c b/src/naemon/objectlist.c index 9b84601d..2bde8f96 100644 --- a/src/naemon/objectlist.c +++ b/src/naemon/objectlist.c @@ -72,6 +72,26 @@ int prepend_unique_object_to_objectlist(objectlist **list, void *object_ptr, int return prepend_unique_object_to_objectlist_ptr(list, object_ptr, *comparator_helper, comparator); } +/* remove pointer from objectlist */ +int remove_object_from_objectlist(objectlist **list, void *object_ptr) { + objectlist *item, *next, *prev; + + if (list == NULL || object_ptr == NULL) + return ERROR; + + for (prev = NULL, item = *list; item; prev = item, item = next) { + next = item->next; + if (item->object_ptr == object_ptr) { + if (prev) + prev->next = next; + else + *list = next; + nm_free(item); + item = prev; + } + } + return OK; +} /* frees memory allocated to a temporary object list */ int free_objectlist(objectlist **temp_list) diff --git a/src/naemon/objectlist.h b/src/naemon/objectlist.h index 7bd016e1..64c01d9c 100644 --- a/src/naemon/objectlist.h +++ b/src/naemon/objectlist.h @@ -56,6 +56,15 @@ int prepend_unique_object_to_objectlist(objectlist **list, void *object_ptr, int * @returns OK if successful, OBJECTLIST_DUPE if the element was already in the list, ERROR otherwise. */ int prepend_unique_object_to_objectlist_ptr(objectlist **list, void *object_ptr, int (*comparator)(const void *a, const void *b, void *user_data), void *user_data); + +/** + * Remove first matching object_ptr from the list. + * @param list An reference to an objectlist. Note that an empty objectlist is just NULL. + * @param object_ptr The object you want to remove from the list. + * @returns OK if successful, ERROR otherwise. + */ +int remove_object_from_objectlist(objectlist **list, void *object_ptr); + /** * Free all the allocated memory of the objectlist. Note: this will completely * orphan any allocated memory inside the objectlist. diff --git a/src/naemon/objects_host.c b/src/naemon/objects_host.c index 445bc82c..8ad52878 100644 --- a/src/naemon/objects_host.c +++ b/src/naemon/objects_host.c @@ -310,6 +310,7 @@ void destroy_host(host *this_host) nm_free(this_host->plugin_output); nm_free(this_host->long_plugin_output); nm_free(this_host->perf_data); + free_objectlist(&this_host->comments_list); free_objectlist(&this_host->hostgroups_ptr); free_objectlist(&this_host->notify_deps); free_objectlist(&this_host->exec_deps); diff --git a/src/naemon/objects_host.h b/src/naemon/objects_host.h index 3609deb4..9bd1f943 100644 --- a/src/naemon/objects_host.h +++ b/src/naemon/objects_host.h @@ -119,6 +119,7 @@ struct host { time_t last_state_history_update; int is_flapping; unsigned long flapping_comment_id; + struct objectlist *comments_list; double percent_state_change; int total_services; unsigned long modified_attributes; diff --git a/src/naemon/objects_service.c b/src/naemon/objects_service.c index 04cf4746..9dc89e51 100644 --- a/src/naemon/objects_service.c +++ b/src/naemon/objects_service.c @@ -326,6 +326,7 @@ void destroy_service(service *this_service, int truncate_lists) nm_free(this_service->long_plugin_output); nm_free(this_service->perf_data); nm_free(this_service->event_handler_args); + free_objectlist(&this_service->comments_list); free_objectlist(&this_service->servicegroups_ptr); free_objectlist(&this_service->notify_deps); free_objectlist(&this_service->exec_deps); diff --git a/src/naemon/objects_service.h b/src/naemon/objects_service.h index 4db85217..16da2dd4 100644 --- a/src/naemon/objects_service.h +++ b/src/naemon/objects_service.h @@ -110,6 +110,7 @@ struct service { int state_history_index; int is_flapping; unsigned long flapping_comment_id; + struct objectlist *comments_list; double percent_state_change; unsigned long modified_attributes; struct host *host_ptr; diff --git a/src/naemon/shared.c b/src/naemon/shared.c index d2de982c..59fd78d4 100644 --- a/src/naemon/shared.c +++ b/src/naemon/shared.c @@ -413,30 +413,6 @@ void strip(char *buffer) } -/************************************************** - *************** HASH FUNCTIONS ******************* - **************************************************/ -/* dual hash function */ -int hashfunc(const char *name1, const char *name2, int hashslots) -{ - unsigned int i, result; - - result = 0; - - if (name1) - for (i = 0; i < strlen(name1); i++) - result += name1[i]; - - if (name2) - for (i = 0; i < strlen(name2); i++) - result += name2[i]; - - result = result % hashslots; - - return result; -} - - /* * given a date/time in time_t format, produce a corresponding * date/time string, including timezone diff --git a/src/naemon/shared.h b/src/naemon/shared.h index 8db0ead7..5775eb3e 100644 --- a/src/naemon/shared.h +++ b/src/naemon/shared.h @@ -48,7 +48,6 @@ int mmap_fclose(mmapfile *temp_mmapfile); char *mmap_fgets(mmapfile *temp_mmapfile); char *mmap_fgets_multiline(mmapfile * temp_mmapfile); void strip(char *buffer); -int hashfunc(const char *name1, const char *name2, int hashslots); void get_datetime_string(time_t *raw_time, char *buffer, int buffer_length, int type); void get_time_breakdown(unsigned long raw_time, int *days, int *hours, diff --git a/src/naemon/utils.c b/src/naemon/utils.c index 5c721950..bc4f1e62 100644 --- a/src/naemon/utils.c +++ b/src/naemon/utils.c @@ -9,6 +9,7 @@ #include "objects_servicedependency.h" #include "statusdata.h" #include "comments.h" +#include "downtime.h" #include "macros.h" #include "broker.h" #include "nebmods.h" @@ -963,6 +964,7 @@ void free_memory(nagios_macros *mac) destroy_objects_servicegroup(TRUE); free_comment_data(); + free_downtime_data(); nm_free(global_host_event_handler); nm_free(global_service_event_handler); diff --git a/src/naemon/xrddefault.c b/src/naemon/xrddefault.c index 4f80980a..b2893d6f 100644 --- a/src/naemon/xrddefault.c +++ b/src/naemon/xrddefault.c @@ -67,6 +67,8 @@ int xrddefault_save_state_information(void) service *temp_service = NULL; contact *temp_contact = NULL; comment *temp_comment = NULL; + GHashTableIter iter; + gpointer comment_; scheduled_downtime *temp_downtime = NULL; int x = 0; int fd = 0; @@ -378,25 +380,28 @@ int xrddefault_save_state_information(void) } /* save all comments */ - for (temp_comment = comment_list; temp_comment != NULL; temp_comment = temp_comment->next) { - - if (temp_comment->comment_type == HOST_COMMENT) - fprintf(fp, "hostcomment {\n"); - else - fprintf(fp, "servicecomment {\n"); - fprintf(fp, "host_name=%s\n", temp_comment->host_name); - if (temp_comment->comment_type == SERVICE_COMMENT) - fprintf(fp, "service_description=%s\n", temp_comment->service_description); - fprintf(fp, "entry_type=%d\n", temp_comment->entry_type); - fprintf(fp, "comment_id=%lu\n", temp_comment->comment_id); - fprintf(fp, "source=%d\n", temp_comment->source); - fprintf(fp, "persistent=%d\n", temp_comment->persistent); - fprintf(fp, "entry_time=%lu\n", temp_comment->entry_time); - fprintf(fp, "expires=%d\n", temp_comment->expires); - fprintf(fp, "expire_time=%lu\n", temp_comment->expire_time); - fprintf(fp, "author=%s\n", temp_comment->author); - fprintf(fp, "comment_data=%s\n", temp_comment->comment_data); - fprintf(fp, "}\n"); + if(comment_hashtable != NULL) { + g_hash_table_iter_init(&iter, comment_hashtable); + while (g_hash_table_iter_next(&iter, NULL, &comment_)) { + temp_comment = comment_; + if (temp_comment->comment_type == HOST_COMMENT) + fprintf(fp, "hostcomment {\n"); + else + fprintf(fp, "servicecomment {\n"); + fprintf(fp, "host_name=%s\n", temp_comment->host_name); + if (temp_comment->comment_type == SERVICE_COMMENT) + fprintf(fp, "service_description=%s\n", temp_comment->service_description); + fprintf(fp, "entry_type=%d\n", temp_comment->entry_type); + fprintf(fp, "comment_id=%lu\n", temp_comment->comment_id); + fprintf(fp, "source=%d\n", temp_comment->source); + fprintf(fp, "persistent=%d\n", temp_comment->persistent); + fprintf(fp, "entry_time=%lu\n", temp_comment->entry_time); + fprintf(fp, "expires=%d\n", temp_comment->expires); + fprintf(fp, "expire_time=%lu\n", temp_comment->expire_time); + fprintf(fp, "author=%s\n", temp_comment->author); + fprintf(fp, "comment_data=%s\n", temp_comment->comment_data); + fprintf(fp, "}\n"); + } } /* save all downtime */ @@ -549,7 +554,6 @@ int xrddefault_read_state_information(void) /* Big speedup when reading retention.dat in bulk */ defer_downtime_sorting = 1; - defer_comment_sorting = 1; /* read all lines in the retention file */ while (1) { @@ -1685,8 +1689,6 @@ int xrddefault_read_state_information(void) if (sort_downtime() != OK) return ERROR; - if (sort_comments() != OK) - return ERROR; return OK; } diff --git a/src/naemon/xsddefault.c b/src/naemon/xsddefault.c index 35e8b997..a0c26e7e 100644 --- a/src/naemon/xsddefault.c +++ b/src/naemon/xsddefault.c @@ -76,6 +76,8 @@ int xsddefault_save_status_data(void) service *temp_service = NULL; contact *temp_contact = NULL; comment *temp_comment = NULL; + GHashTableIter iter; + gpointer comment_; scheduled_downtime *temp_downtime = NULL; time_t current_time; int fd = 0; @@ -335,25 +337,28 @@ int xsddefault_save_status_data(void) } /* save all comments */ - for (temp_comment = comment_list; temp_comment != NULL; temp_comment = temp_comment->next) { - - if (temp_comment->comment_type == HOST_COMMENT) - fprintf(fp, "hostcomment {\n"); - else - fprintf(fp, "servicecomment {\n"); - fprintf(fp, "\thost_name=%s\n", temp_comment->host_name); - if (temp_comment->comment_type == SERVICE_COMMENT) - fprintf(fp, "\tservice_description=%s\n", temp_comment->service_description); - fprintf(fp, "\tentry_type=%d\n", temp_comment->entry_type); - fprintf(fp, "\tcomment_id=%lu\n", temp_comment->comment_id); - fprintf(fp, "\tsource=%d\n", temp_comment->source); - fprintf(fp, "\tpersistent=%d\n", temp_comment->persistent); - fprintf(fp, "\tentry_time=%lu\n", temp_comment->entry_time); - fprintf(fp, "\texpires=%d\n", temp_comment->expires); - fprintf(fp, "\texpire_time=%lu\n", temp_comment->expire_time); - fprintf(fp, "\tauthor=%s\n", temp_comment->author); - fprintf(fp, "\tcomment_data=%s\n", temp_comment->comment_data); - fprintf(fp, "\t}\n\n"); + if(comment_hashtable != NULL) { + g_hash_table_iter_init(&iter, comment_hashtable); + while (g_hash_table_iter_next(&iter, NULL, &comment_)) { + temp_comment = comment_; + if (temp_comment->comment_type == HOST_COMMENT) + fprintf(fp, "hostcomment {\n"); + else + fprintf(fp, "servicecomment {\n"); + fprintf(fp, "\thost_name=%s\n", temp_comment->host_name); + if (temp_comment->comment_type == SERVICE_COMMENT) + fprintf(fp, "\tservice_description=%s\n", temp_comment->service_description); + fprintf(fp, "\tentry_type=%d\n", temp_comment->entry_type); + fprintf(fp, "\tcomment_id=%lu\n", temp_comment->comment_id); + fprintf(fp, "\tsource=%d\n", temp_comment->source); + fprintf(fp, "\tpersistent=%d\n", temp_comment->persistent); + fprintf(fp, "\tentry_time=%lu\n", temp_comment->entry_time); + fprintf(fp, "\texpires=%d\n", temp_comment->expires); + fprintf(fp, "\texpire_time=%lu\n", temp_comment->expire_time); + fprintf(fp, "\tauthor=%s\n", temp_comment->author); + fprintf(fp, "\tcomment_data=%s\n", temp_comment->comment_data); + fprintf(fp, "\t}\n\n"); + } } /* save all downtime */ diff --git a/t-tap/smallconfig/retention.dat b/t-tap/smallconfig/retention.dat index 54bd419b..980e1676 100644 --- a/t-tap/smallconfig/retention.dat +++ b/t-tap/smallconfig/retention.dat @@ -442,7 +442,7 @@ persistent=1 entry_time=1264451960 expires=0 expire_time=0 -author=(Nagios Process) +author=(Naemon Process) comment_data=Notifications for this host are being suppressed because it was detected as having been flapping between different states (34.5% change > 20.0% threshold). When the host state stabilizes and the flapping stops, notifications will be re-enabled. } servicecomment { @@ -455,7 +455,7 @@ persistent=1 entry_time=1264451960 expires=0 expire_time=0 -author=(Nagios Process) +author=(Naemon Process) comment_data=Notifications for this service are being suppressed because it was detected as having been flapping between different states (40.3% change >= 20.0% threshold). When the service state stabilizes and the flapping stops, notifications will be re-enabled. } hostcomment { diff --git a/t-tap/test_commands.c b/t-tap/test_commands.c index 6cec0daf..5b58b562 100644 --- a/t-tap/test_commands.c +++ b/t-tap/test_commands.c @@ -449,6 +449,7 @@ void test_host_commands(void) time_t check_time = 0; char *cmdstr = NULL; scheduled_downtime *downtime = NULL; + unsigned long downtime_id = 0; target_host = find_host(host_name); target_host->obsess = FALSE; pre = number_of_host_comments(host_name); @@ -588,10 +589,11 @@ void test_host_commands(void) ok(CMD_ERROR_OK == process_external_command1("[1234567890] ENABLE_HOST_FLAP_DETECTION;host1"), "core command: ENABLE_HOST_FLAP_DETECTION"); ok(target_host->flap_detection_enabled, "ENABLE_HOST_FLAP_DETECTION enables host flap detection"); - assert(NULL == find_service_downtime(0)); + downtime_id = next_downtime_id; + assert(NULL == find_service_downtime(downtime_id)); nm_asprintf(&cmdstr, "[1234567890] SCHEDULE_HOST_SVC_DOWNTIME;host1;%llu;%llu;1;0;0;myself;my downtime comment", (unsigned long long int)time(NULL), (unsigned long long int)time(NULL) + 1500); ok(CMD_ERROR_OK == process_external_command1(cmdstr), "core command: SCHEDULE_HOST_SVC_DOWNTIME"); - strcmp(host_name, find_service_downtime(0)->host_name); + ok(!strcmp(host_name, find_service_downtime(downtime_id)->host_name), "got hostname from service downtime"); ok(0 == 0, "SCHEDULE_HOST_SVC_DOWNTIME schedules downtime for services on a host"); free(cmdstr); @@ -717,13 +719,14 @@ void test_core_commands(void) int main(int /*@unused@*/ argc, char /*@unused@*/ **arv) { const char *test_config_file = TESTDIR "naemon.cfg"; - plan_tests(521); + plan_tests(522); init_event_queue(); config_file_dir = nspath_absolute_dirname(test_config_file, NULL); assert(OK == read_main_config_file(test_config_file)); assert(OK == read_all_object_data(test_config_file)); assert(OK == initialize_downtime_data()); + assert(OK == initialize_comment_data()); assert(OK == initialize_retention_data()); nagios_iobs = iobroker_create(); test_register(); diff --git a/t-tap/test_config.c b/t-tap/test_config.c index 756c3faf..6274eb57 100644 --- a/t-tap/test_config.c +++ b/t-tap/test_config.c @@ -85,6 +85,7 @@ int main(int argc, char **argv) initialize_retention_data(); initialize_downtime_data(); + initialize_comment_data(); ok(xrddefault_read_state_information() == OK, "Reading retention data"); diff --git a/t-tap/test_downtime.c b/t-tap/test_downtime.c index 6b3b52e2..9392bd11 100644 --- a/t-tap/test_downtime.c +++ b/t-tap/test_downtime.c @@ -39,6 +39,7 @@ int main(int argc, char **argv) time(&now); init_event_queue(); + initialize_comment_data(); initialize_downtime_data(); init_objects_host(4); init_objects_service(8); diff --git a/tests/.gitignore b/tests/.gitignore index 8018abd0..0657bcac 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -12,3 +12,12 @@ /test-utils /test-check-scheduling /test-objects +/test-arith +/test-arith-builtins +/test-check-dependencies +/test-check-result-processing +/test-neb-callbacks +/test-query-handler +/test-retention +/test-scheduled-downtimes +/test-worker diff --git a/tests/test-scheduled-downtimes.c b/tests/test-scheduled-downtimes.c index a564ecba..62b73c12 100644 --- a/tests/test-scheduled-downtimes.c +++ b/tests/test-scheduled-downtimes.c @@ -47,6 +47,7 @@ void setup(void) init_objects_service(2); init_objects_command(1); initialize_downtime_data(); + initialize_comment_data(); initialize_retention_data(); workdir = getcwd(NULL, 0); @@ -320,7 +321,7 @@ START_TEST(host_downtime_id_retained_across_reload) dt = find_downtime(ANY_DOWNTIME, downtime_id); ck_assert(dt != NULL); - ck_assert(0 == dt->comment_id); + ck_assert(1 == dt->comment_id); ck_assert(OK == handle_scheduled_downtime(dt)); comment_id = dt->comment_id;