/* 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;
	unsigned long new_comment_id = 0L;

	result = xcddefault_add_new_host_comment(entry_type, host_name, entry_time, author_name, comment_data, persistent, source, expires, expire_time, &new_comment_id);

	/* save comment id */
	if(comment_id != NULL)
		*comment_id = new_comment_id;

#ifdef USE_EVENT_BROKER
	/* send data to event broker */
	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, new_comment_id, NULL);
#endif

	return result;
	}
Exemple #2
0
/* adds a new service comment */
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) {
	int result = OK;
	unsigned long new_comment_id = 0L;

	/**** IMPLEMENTATION-SPECIFIC CALLS ****/
#ifdef USE_XCDDEFAULT
	result = xcddefault_add_new_service_comment(entry_type, host_name, svc_description, entry_time, author_name, comment_data, persistent, source, expires, expire_time, &new_comment_id);
#endif

	/* save comment id */
	if(comment_id != NULL)
		*comment_id = new_comment_id;

#ifdef USE_EVENT_BROKER
	/* send data to event broker */
	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, new_comment_id, NULL);
#endif

	return result;
	}
/* adds a comment to the list in memory */
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) {
	nagios_comment *new_comment = NULL;
	nagios_comment *last_comment = NULL;
	nagios_comment *temp_comment = NULL;
	int result = OK;

	/* 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;

	/* allocate memory for the comment */
	if((new_comment = (nagios_comment *)calloc(1, sizeof(nagios_comment))) == NULL)
		return ERROR;

	/* duplicate vars */
	if((new_comment->host_name = (char *)strdup(host_name)) == NULL)
		result = ERROR;
	if(comment_type == SERVICE_COMMENT) {
		if((new_comment->service_description = (char *)strdup(svc_description)) == NULL)
			result = ERROR;
		}
	if((new_comment->author = (char *)strdup(author)) == NULL)
		result = ERROR;
	if((new_comment->comment_data = (char *)strdup(comment_data)) == NULL)
		result = ERROR;

	new_comment->comment_type = comment_type;
	new_comment->entry_type = entry_type;
	new_comment->source = source;
	new_comment->entry_time = entry_time;
	new_comment->comment_id = comment_id;
	new_comment->persistent = (persistent == TRUE) ? TRUE : FALSE;
	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;
		}

	/* handle errors */
	if(result == ERROR) {
		my_free(new_comment->comment_data);
		my_free(new_comment->author);
		my_free(new_comment->service_description);
		my_free(new_comment->host_name);
		my_free(new_comment);
		return ERROR;
		}

	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;
			}
		}

#ifdef NSCORE
#ifdef USE_EVENT_BROKER
	/* send data to event broker */
	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, NULL);
#endif
#endif

	return OK;
	}
/* deletes a host or service comment */
int delete_comment(int type, unsigned long comment_id) {
	nagios_comment *this_comment = NULL;
	nagios_comment *last_comment = NULL;
	nagios_comment *next_comment = NULL;
	int hashslot = 0;
	nagios_comment *this_hash = NULL;
	nagios_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;
		}

	if(this_comment == NULL)
		return ERROR;

	/* remove the comment from the list in memory */
#ifdef USE_EVENT_BROKER
	/* send data to event broker */
	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, NULL);
#endif

	/* 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;
		}

	/* then removed from linked list */
	if(comment_list == this_comment)
		comment_list = this_comment->next;
	else
		last_comment->next = next_comment;

	/* free memory */
	my_free(this_comment->host_name);
	my_free(this_comment->service_description);
	my_free(this_comment->author);
	my_free(this_comment->comment_data);
	my_free(this_comment);

	return OK;
	}
Exemple #5
0
/* deletes a host or service comment */
int delete_comment(int type, unsigned long comment_id) {
	int result = OK;
	comment *this_comment = NULL;
	comment *last_comment = NULL;
	comment *next_comment = NULL;
	int hashslot = 0;
	comment *this_hash = NULL;
	comment *last_hash = NULL;

	/* lock the comments so we can modify them safely */
#ifdef NSCORE
	pthread_mutex_lock(&nagios_comment_lock);
#endif

	/* 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;
		}

	/* remove the comment from the list in memory */
	if(this_comment != NULL) {

#ifdef USE_EVENT_BROKER
		/* send data to event broker */
		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, NULL);
#endif

		/* 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;
			}

		/* then removed from linked list */
		if(comment_list == this_comment)
			comment_list = this_comment->next;
		else
			last_comment->next = next_comment;

		/* free memory */
		my_free(this_comment->host_name);
		my_free(this_comment->service_description);
		my_free(this_comment->author);
		my_free(this_comment->comment_data);
		my_free(this_comment);

		result = OK;
		}
	else
		result = ERROR;

	/**** IMPLEMENTATION-SPECIFIC CALLS ****/
#ifdef USE_XCDDEFAULT
	if(type == HOST_COMMENT)
		result = xcddefault_delete_host_comment(comment_id);
	else
		result = xcddefault_delete_service_comment(comment_id);
#endif

#ifdef NSCORE
	pthread_mutex_unlock(&nagios_comment_lock);
#endif

	return result;
	}