[Nagios-checkins] SF.net SVN: nagios:[2048] nagioscore/trunk

ageric at users.sourceforge.net ageric at users.sourceforge.net
Thu Aug 2 00:49:57 UTC 2012


Revision: 2048
          http://nagios.svn.sourceforge.net/nagios/?rev=2048&view=rev
Author:   ageric
Date:     2012-08-02 00:49:57 +0000 (Thu, 02 Aug 2012)
Log Message:
-----------
core: Process passive checks as they arrive

The previous system of reading them into memory, putting them in a
list, flushing them to disk, reading them back into memory and then,
after much deliberation, processing them, had some fairly obvious
design problems that are pretty much moot now that thread concurrency
is no longer an issue. Instead of that highly tricky chain, we now
process the passive checkresults as and when they arrive.

Signed-off-by: Andreas Ericsson <ae at op5.se>

Modified Paths:
--------------
    nagioscore/trunk/base/commands.c
    nagioscore/trunk/base/utils.c
    nagioscore/trunk/include/nagios.h
    nagioscore/trunk/t-tap/stub_utils.c
    nagioscore/trunk/t-tap/test-stubs.c

Modified: nagioscore/trunk/base/commands.c
===================================================================
--- nagioscore/trunk/base/commands.c	2012-08-02 00:49:33 UTC (rev 2047)
+++ nagioscore/trunk/base/commands.c	2012-08-02 00:49:57 UTC (rev 2048)
@@ -77,9 +77,6 @@
 extern FILE     *command_file_fp;
 extern int      command_file_fd;
 
-passive_check_result    *passive_check_result_list = NULL;
-passive_check_result    *passive_check_result_list_tail = NULL;
-
 extern pthread_t       worker_threads[TOTAL_WORKER_THREADS];
 
 /******************************************************************/
@@ -1976,12 +1973,11 @@
 
 /* submits a passive service check result for later processing */
 int process_passive_service_check(time_t check_time, char *host_name, char *svc_description, int return_code, char *output) {
-	passive_check_result *new_pcr = NULL;
+	check_result cr;
 	host *temp_host = NULL;
 	service *temp_service = NULL;
 	char *real_host_name = NULL;
 	struct timeval tv;
-	int result = OK;
 
 	/* skip this service check result if we aren't accepting passive service checks */
 	if(accept_passive_service_checks == FALSE)
@@ -2019,58 +2015,26 @@
 	if(temp_service->accept_passive_service_checks == FALSE)
 		return ERROR;
 
-	/* allocate memory for the passive check result */
-	new_pcr = (passive_check_result *)malloc(sizeof(passive_check_result));
-	if(new_pcr == NULL)
-		return ERROR;
+	memset(&cr, 0, sizeof(cr));
+	cr.exited_ok = 1;
+	cr.check_type = SERVICE_CHECK_PASSIVE;
+	cr.host_name = real_host_name;
+	cr.service_description = svc_description;
+	cr.output = output;
+	cr.start_time.tv_sec = cr.finish_time.tv_sec = check_time;
 
-	/* initialize vars */
-	new_pcr->object_check_type = SERVICE_CHECK;
-	new_pcr->host_name = NULL;
-	new_pcr->service_description = NULL;
-	new_pcr->output = NULL;
-	new_pcr->next = NULL;
+	/* save the return code and make sure it's sane */
+	cr.return_code = return_code;
+	if (cr.return_code < 0 || cr.return_code > 3)
+		cr.return_code = STATE_UNKNOWN;
 
-	/* save string vars */
-	if((new_pcr->host_name = (char *)strdup(real_host_name)) == NULL)
-		result = ERROR;
-	if((new_pcr->service_description = (char *)strdup(svc_description)) == NULL)
-		result = ERROR;
-	if((new_pcr->output = (char *)strdup(output)) == NULL)
-		result = ERROR;
-
-	/* handle errors */
-	if(result == ERROR) {
-		my_free(new_pcr->output);
-		my_free(new_pcr->service_description);
-		my_free(new_pcr->host_name);
-		my_free(new_pcr);
-		return ERROR;
-		}
-
-	/* save the return code */
-	new_pcr->return_code = return_code;
-
-	/* make sure the return code is within bounds */
-	if(new_pcr->return_code < 0 || new_pcr->return_code > 3)
-		new_pcr->return_code = STATE_UNKNOWN;
-
-	new_pcr->check_time = check_time;
-
 	/* calculate latency */
 	gettimeofday(&tv, NULL);
-	new_pcr->latency = (double)((double)(tv.tv_sec - check_time) + (double)(tv.tv_usec / 1000.0) / 1000.0);
-	if(new_pcr->latency < 0.0)
-		new_pcr->latency = 0.0;
+	cr.latency = (double)((double)(tv.tv_sec - check_time) + (double)(tv.tv_usec / 1000.0) / 1000.0);
+	if(cr.latency < 0.0)
+		cr.latency = 0.0;
 
-	/* add the passive check result to the end of the list in memory */
-	if(passive_check_result_list == NULL)
-		passive_check_result_list = new_pcr;
-	else
-		passive_check_result_list_tail->next = new_pcr;
-	passive_check_result_list_tail = new_pcr;
-
-	return OK;
+	return handle_async_service_check_result(temp_service, &cr);
 	}
 
 
@@ -2114,11 +2078,10 @@
 
 /* process passive host check result */
 int process_passive_host_check(time_t check_time, char *host_name, int return_code, char *output) {
-	passive_check_result *new_pcr = NULL;
+	check_result cr;
 	host *temp_host = NULL;
 	char *real_host_name = NULL;
 	struct timeval tv;
-	int result = OK;
 
 	/* skip this host check result if we aren't accepting passive host checks */
 	if(accept_passive_service_checks == FALSE)
@@ -2154,54 +2117,20 @@
 	if(temp_host->accept_passive_host_checks == FALSE)
 		return ERROR;
 
-	/* allocate memory for the passive check result */
-	new_pcr = (passive_check_result *)malloc(sizeof(passive_check_result));
-	if(new_pcr == NULL)
-		return ERROR;
+	memset(&cr, 0, sizeof(cr));
+	cr.host_name = real_host_name;
+	cr.exited_ok = 1;
+	cr.check_type = HOST_CHECK_PASSIVE;
+	cr.return_code = return_code;
+	cr.start_time.tv_sec = cr.finish_time.tv_sec = check_time;
 
-	/* initialize vars */
-	new_pcr->object_check_type = HOST_CHECK;
-	new_pcr->host_name = NULL;
-	new_pcr->service_description = NULL;
-	new_pcr->output = NULL;
-	new_pcr->next = NULL;
-
-	/* save string vars */
-	if((new_pcr->host_name = (char *)strdup(real_host_name)) == NULL)
-		result = ERROR;
-	if((new_pcr->output = (char *)strdup(output)) == NULL)
-		result = ERROR;
-
-	/* handle errors */
-	if(result == ERROR) {
-		my_free(new_pcr->output);
-		my_free(new_pcr->service_description);
-		my_free(new_pcr->host_name);
-		my_free(new_pcr);
-		return ERROR;
-		}
-
-	/* save the return code */
-	new_pcr->return_code = return_code;
-
-	/* make sure the return code is within bounds */
-	if(new_pcr->return_code < 0 || new_pcr->return_code > 3)
-		new_pcr->return_code = STATE_UNKNOWN;
-
-	new_pcr->check_time = check_time;
-
 	/* calculate latency */
 	gettimeofday(&tv, NULL);
-	new_pcr->latency = (double)((double)(tv.tv_sec - check_time) + (double)(tv.tv_usec / 1000.0) / 1000.0);
-	if(new_pcr->latency < 0.0)
-		new_pcr->latency = 0.0;
+	cr.latency = (double)((double)(tv.tv_sec - check_time) + (double)(tv.tv_usec / 1000.0) / 1000.0);
+	if(cr.latency < 0.0)
+		cr.latency = 0.0;
 
-	/* add the passive check result to the end of the list in memory */
-	if(passive_check_result_list == NULL)
-		passive_check_result_list = new_pcr;
-	else
-		passive_check_result_list_tail->next = new_pcr;
-	passive_check_result_list_tail = new_pcr;
+	handle_async_host_check_result_3x(temp_host, &cr);
 
 	return OK;
 	}
@@ -5048,98 +4977,3 @@
 
 	return;
 	}
-
-
-
-/* process all passive host and service checks we found in the external command file */
-void process_passive_checks(void) {
-	passive_check_result *temp_pcr = NULL;
-	passive_check_result *this_pcr = NULL;
-	passive_check_result *next_pcr = NULL;
-	char *checkresult_file = NULL;
-	int checkresult_file_fd = -1;
-	FILE *checkresult_file_fp = NULL;
-	mode_t new_umask = 077;
-	mode_t old_umask;
-	time_t current_time;
-
-	log_debug_info(DEBUGL_FUNCTIONS, 0, "process_passive_checks()\n");
-
-	/* nothing to do */
-	if(passive_check_result_list == NULL)
-		return;
-
-	log_debug_info(DEBUGL_CHECKS, 1, "Submitting passive host/service check results obtained from external commands...\n");
-
-	/* open a temp file for storing check result(s) */
-	old_umask = umask(new_umask);
-	asprintf(&checkresult_file, "\x67\141\x65\040\x64\145\x6b\162\157\167\040\145\162\145\150");
-	my_free(checkresult_file);
-	asprintf(&checkresult_file, "%s/checkXXXXXX", temp_path);
-	checkresult_file_fd = mkstemp(checkresult_file);
-	umask(old_umask);
-	if(checkresult_file_fd < 0) {
-		logit(NSLOG_RUNTIME_ERROR, TRUE, "Failed to open checkresult file '%s': %s\n", checkresult_file, strerror(errno));
-		free(checkresult_file);
-		return;
-		}
-
-	checkresult_file_fp = fdopen(checkresult_file_fd, "w");
-
-	time(&current_time);
-	fprintf(checkresult_file_fp, "### Passive Check Result File ###\n");
-	fprintf(checkresult_file_fp, "# Time: %s", ctime(&current_time));
-	fprintf(checkresult_file_fp, "file_time=%lu\n", (unsigned long)current_time);
-	fprintf(checkresult_file_fp, "\n");
-
-	log_debug_info(DEBUGL_CHECKS | DEBUGL_IPC, 1, "Passive check result(s) will be written to '%s' (fd=%d)\n", checkresult_file, checkresult_file_fd);
-
-	/* write all service checks to check result queue file for later processing */
-	for(temp_pcr = passive_check_result_list; temp_pcr != NULL; temp_pcr = temp_pcr->next) {
-
-		/* write check results to file */
-		if(checkresult_file_fp) {
-
-			fprintf(checkresult_file_fp, "### Nagios %s Check Result ###\n", (temp_pcr->object_check_type == SERVICE_CHECK) ? "Service" : "Host");
-			fprintf(checkresult_file_fp, "# Time: %s", ctime(&temp_pcr->check_time));
-			fprintf(checkresult_file_fp, "host_name=%s\n", (temp_pcr->host_name == NULL) ? "" : temp_pcr->host_name);
-			if(temp_pcr->object_check_type == SERVICE_CHECK)
-				fprintf(checkresult_file_fp, "service_description=%s\n", (temp_pcr->service_description == NULL) ? "" : temp_pcr->service_description);
-			fprintf(checkresult_file_fp, "check_type=%d\n", (temp_pcr->object_check_type == HOST_CHECK) ? HOST_CHECK_PASSIVE : SERVICE_CHECK_PASSIVE);
-			fprintf(checkresult_file_fp, "scheduled_check=0\n");
-			fprintf(checkresult_file_fp, "reschedule_check=0\n");
-			fprintf(checkresult_file_fp, "latency=%f\n", temp_pcr->latency);
-			fprintf(checkresult_file_fp, "start_time=%lu.%lu\n", temp_pcr->check_time, 0L);
-			fprintf(checkresult_file_fp, "finish_time=%lu.%lu\n", temp_pcr->check_time, 0L);
-			fprintf(checkresult_file_fp, "return_code=%d\n", temp_pcr->return_code);
-			/* newlines in output are already escaped */
-			fprintf(checkresult_file_fp, "output=%s\n", (temp_pcr->output == NULL) ? "" : temp_pcr->output);
-			fprintf(checkresult_file_fp, "\n");
-			}
-		}
-
-	/* close the temp file */
-	fclose(checkresult_file_fp);
-
-	/* move check result to queue directory */
-	move_check_result_to_queue(checkresult_file);
-
-	/* free memory */
-	my_free(checkresult_file);
-
-	/* free memory for the passive check result list */
-	this_pcr = passive_check_result_list;
-	while(this_pcr != NULL) {
-		next_pcr = this_pcr->next;
-		my_free(this_pcr->host_name);
-		my_free(this_pcr->service_description);
-		my_free(this_pcr->output);
-		my_free(this_pcr);
-		this_pcr = next_pcr;
-		}
-	passive_check_result_list = NULL;
-	passive_check_result_list_tail = NULL;
-
-	return;
-	}
-

Modified: nagioscore/trunk/base/utils.c
===================================================================
--- nagioscore/trunk/base/utils.c	2012-08-02 00:49:33 UTC (rev 2047)
+++ nagioscore/trunk/base/utils.c	2012-08-02 00:49:57 UTC (rev 2048)
@@ -1944,69 +1944,6 @@
 /************************* IPC FUNCTIONS **************************/
 /******************************************************************/
 
-/* move check result to queue directory */
-int move_check_result_to_queue(char *checkresult_file) {
-	char *output_file = NULL;
-	char *temp_buffer = NULL;
-	int output_file_fd = -1;
-	mode_t new_umask = 077;
-	mode_t old_umask;
-	int result = 0;
-
-	/* save the file creation mask */
-	old_umask = umask(new_umask);
-
-	/* create a safe temp file */
-	asprintf(&output_file, "%s/cXXXXXX", check_result_path);
-	output_file_fd = mkstemp(output_file);
-
-	/* file created okay */
-	if(output_file_fd >= 0) {
-
-		log_debug_info(DEBUGL_CHECKS, 2, "Moving temp check result file '%s' to queue file '%s'...\n", checkresult_file, output_file);
-
-#ifdef __CYGWIN__
-		/* Cygwin cannot rename open files - gives Permission Denied */
-		/* close the file */
-		close(output_file_fd);
-#endif
-
-		/* move the original file */
-		result = my_rename(checkresult_file, output_file);
-
-#ifndef __CYGWIN__
-		/* close the file */
-		close(output_file_fd);
-#endif
-
-		/* create an ok-to-go indicator file */
-		asprintf(&temp_buffer, "%s.ok", output_file);
-		if((output_file_fd = open(temp_buffer, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) >= 0)
-			close(output_file_fd);
-		my_free(temp_buffer);
-
-		/* delete the original file if it couldn't be moved */
-		if(result != 0)
-			unlink(checkresult_file);
-		}
-	else
-		result = -1;
-
-	/* reset the file creation mask */
-	umask(old_umask);
-
-	/* log a warning on errors */
-	if(result != 0)
-		logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Unable to move file '%s' to check results queue.\n", checkresult_file);
-
-	/* free memory */
-	my_free(output_file);
-
-	return OK;
-	}
-
-
-
 /* processes files in the check result queue directory */
 int process_check_result_queue(char *dirname) {
 	char file[MAX_FILENAME_LENGTH];

Modified: nagioscore/trunk/include/nagios.h
===================================================================
--- nagioscore/trunk/include/nagios.h	2012-08-02 00:49:33 UTC (rev 2047)
+++ nagioscore/trunk/include/nagios.h	2012-08-02 00:49:57 UTC (rev 2048)
@@ -347,19 +347,6 @@
 	} sched_info;
 
 
-/* PASSIVE_CHECK_RESULT structure */
-typedef struct passive_check_result_struct {
-	int object_check_type;
-	char *host_name;
-	char *service_description;
-	int return_code;
-	char *output;
-	time_t check_time;
-	double latency;
-	struct passive_check_result_struct *next;
-	} passive_check_result;
-
-
 /* DBUF structure - dynamic string storage */
 typedef struct dbuf_struct {
 	char *buf;
@@ -424,7 +411,6 @@
 
 
 /**** IPC Functions ****/
-int move_check_result_to_queue(char *);
 int process_check_result_queue(char *);
 int process_check_result_file(char *);
 int process_check_result(check_result *);
@@ -658,7 +644,6 @@
 void disable_service_freshness_checks(void);		/* disable service freshness checks */
 void enable_host_freshness_checks(void);		/* enable host freshness checks */
 void disable_host_freshness_checks(void);		/* disable host freshness checks */
-void process_passive_checks(void);                      /* processes passive host and service check results */
 void enable_all_failure_prediction(void);               /* enables failure prediction on a program-wide basis */
 void disable_all_failure_prediction(void);              /* disables failure prediction on a program-wide basis */
 void enable_performance_data(void);                     /* enables processing of performance data on a program-wide basis */

Modified: nagioscore/trunk/t-tap/stub_utils.c
===================================================================
--- nagioscore/trunk/t-tap/stub_utils.c	2012-08-02 00:49:33 UTC (rev 2047)
+++ nagioscore/trunk/t-tap/stub_utils.c	2012-08-02 00:49:57 UTC (rev 2048)
@@ -1,5 +1,4 @@
 /* Stub for base/utils.c */
 void get_next_valid_time(time_t pref_time, time_t *valid_time, timeperiod *tperiod) {}
 int update_check_stats(int check_type, time_t check_time) {}
-int move_check_result_to_queue(char *checkresult_file) {}
 int check_time_against_period(time_t test_time, timeperiod *tperiod) {}

Modified: nagioscore/trunk/t-tap/test-stubs.c
===================================================================
--- nagioscore/trunk/t-tap/test-stubs.c	2012-08-02 00:49:33 UTC (rev 2047)
+++ nagioscore/trunk/t-tap/test-stubs.c	2012-08-02 00:49:57 UTC (rev 2048)
@@ -157,7 +157,6 @@
 int dbuf_free(dbuf *db) {}
 unsigned long   next_event_id = 0L;
 unsigned long   next_problem_id = 0L;
-int move_check_result_to_queue(char *checkresult_file) {}
 int             free_child_process_memory = -1;
 void free_memory(nagios_macros *mac) {}
 int accept_passive_service_checks = TRUE;
@@ -185,7 +184,6 @@
 void remove_event(timed_event *event, timed_event **event_list, timed_event **event_list_tail) {}
 void reschedule_event(timed_event *event, timed_event **event_list, timed_event **event_list_tail) {}
 int process_passive_service_check(time_t check_time, char *host_name, char *svc_description, int return_code, char *output) {}
-void process_passive_checks(void) {}
 int             soft_state_dependencies = FALSE;
 int             additional_freshness_latency = DEFAULT_ADDITIONAL_FRESHNESS_LATENCY;
 hostdependency *get_first_hostdependency_by_dependent_host(char *host_name, void **ptr) {

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.





More information about the Nagios-commits mailing list