[PATCH] wproc: Remove depencency for other parts of system

Max Sikstrom max.sikstrom at op5.com
Fri Aug 23 10:28:56 CEST 2013


From: Max Sikström <msikstrom at op5.com>

A worker is a closed defined system that executes commands, and distributes it
over several processes, and does it quite well.

Because of the nature of a job to be asyncrhonous, the simplest way is to let
worker handler job types to know what should be done when finished; start job,
do other things, do some stuff when job is done, dependent on what job type it
was.

But that concept doesn't scale well when new kinds of jobs is needed, for
example through nebmods, or new systems in the system. The worker code gets
cluttered with non-worker related functionality, and some logic about the other
parts functionality needs to be done within the worker module.

The concept of callbacks and pointers to functions is a much cleaner way to
handle job results. Each module that uses the worker can define how to handle
the job result itself instead. No special cases is needed in the worker code.

Reviewed-by: Robin Sonefors <rsonefors at op5.com>
Signed-off-by: Max Sikström <msikstrom at op5.com>
---
 base/checks.c        |   45 +++++++-
 base/notifications.c |   38 ++++++-
 base/sehandlers.c    |   96 +++++++++--------
 base/workers.c       |  301 +++----------------------------------------------
 include/workers.h    |   24 +----
 xdata/xpddefault.c   |   11 ++-
 6 files changed, 160 insertions(+), 355 deletions(-)

diff --git a/base/checks.c b/base/checks.c
index 76a9b7d..c5c1ff2 100644
--- a/base/checks.c
+++ b/base/checks.c
@@ -61,6 +61,47 @@ int reap_check_results(void) {
 
 
 
+/******************************************************************/
+/********************** CHECK COMPLETE CALLBACK *******************/
+/******************************************************************/
+
+static void check_complete_callback(struct wproc_result *wpres, void *data, int flags) {
+	check_result *cr = (check_result*)data;
+	if(wpres) {
+		/* Got a result. If wpres == NULL, only clean up */
+
+		memcpy(&cr->rusage, &wpres->rusage, sizeof(wpres->rusage));
+		cr->start_time.tv_sec = wpres->start.tv_sec;
+		cr->start_time.tv_usec = wpres->start.tv_usec;
+		cr->finish_time.tv_sec = wpres->stop.tv_sec;
+		cr->finish_time.tv_usec = wpres->stop.tv_usec;
+		if (WIFEXITED(wpres->wait_status)) {
+			cr->return_code = WEXITSTATUS(wpres->wait_status);
+		} else {
+			cr->return_code = STATE_UNKNOWN;
+		}
+
+		if (wpres->outstd && *wpres->outstd) {
+			cr->output = strdup(wpres->outstd);
+		} else if (wpres->outerr) {
+			asprintf(&cr->output, "(No output on stdout) stderr: %s", wpres->outerr);
+		} else {
+			cr->output = NULL;
+		}
+
+		cr->early_timeout = wpres->early_timeout;
+		cr->exited_ok = wpres->exited_ok;
+		cr->engine = NULL;
+		cr->source = "something"; /* wp->name; */
+
+		process_check_result(cr);
+	}
+
+	/* Always cleanup */
+	free_check_result(cr);
+	free(cr);
+}
+
 
 /******************************************************************/
 /****************** SERVICE MONITORING FUNCTIONS ******************/
@@ -293,7 +334,7 @@ int run_async_service_check(service *svc, int check_options, double latency, int
 	svc->latency = old_latency;
 
 	/* paw off the check to a worker to run */
-	runchk_result = wproc_run_check(cr, processed_command, &mac);
+	runchk_result = wproc_run_callback(processed_command, service_check_timeout, check_complete_callback, (void*)cr, &mac);
 	if (runchk_result == ERROR) {
 		logit(NSLOG_RUNTIME_ERROR, TRUE, "Unable to run check for service '%s' on host '%s'\n", svc->description, svc->host_name);
 	}
@@ -2078,7 +2119,7 @@ int run_async_host_check(host *hst, int check_options, double latency, int sched
 	/* reset latency (permanent value for this check will get set later) */
 	hst->latency = old_latency;
 
-	runchk_result = wproc_run_check(cr, processed_command, &mac);
+	runchk_result = wproc_run_callback(processed_command, service_check_timeout, check_complete_callback, (void*)cr, &mac);
 	if (runchk_result == ERROR) {
 		logit(NSLOG_RUNTIME_ERROR, TRUE, "Unable to send check for host '%s' to worker (ret=%d)\n", hst->name, runchk_result);
 	} else {
diff --git a/base/notifications.c b/base/notifications.c
index 13ae651..e9b6dee 100644
--- a/base/notifications.c
+++ b/base/notifications.c
@@ -30,6 +30,13 @@
 #include "../include/neberrors.h"
 #include "../include/workers.h"
 
+
+struct notification_job {
+	char *contact_name;
+	char *host_name;
+	char *service_description;
+};
+
 /*** silly helpers ****/
 static contact *find_contact_by_name_or_alias(const char *name)
 {
@@ -59,11 +66,28 @@ const char *notification_reason_name(unsigned int reason_type)
 	return "(unknown)";
 }
 
-
 /******************************************************************/
 /***************** SERVICE NOTIFICATION FUNCTIONS *****************/
 /******************************************************************/
 
+static void notification_callback(struct wproc_result *wpres, void *data, int flags) {
+	struct notification_job *oj = (struct notification_job *)data;
+	if(wpres) {
+		double runtime = ((double)wpres->runtime.tv_sec) + ((double)wpres->runtime.tv_usec) / 1000000.0;
+		if (wpres->early_timeout) {
+			if (oj->service_description) {
+				logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Notifying contact '%s' of service '%s' on host '%s' by command '%s' timed out after %.2f seconds\n",
+					  oj->contact_name, oj->service_description,
+					  oj->host_name, wpres->command, runtime);
+			} else {
+				logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Notifying contact '%s' of host '%s' by command '%s' timed out after %.2f seconds\n",
+					  oj->contact_name, oj->host_name,
+					  wpres->command, runtime);
+			}
+		}
+	}
+	free(oj);
+}
 
 /* notify contacts about a service problem or recovery */
 int service_notification(service *svc, int type, char *not_author, char *not_data, int options) {
@@ -798,7 +822,11 @@ int notify_contact_of_service(nagios_macros *mac, contact *cntct, service *svc,
 			}
 
 		/* run the notification command */
-		wproc_notify(cntct->name, svc->host_name, svc->description, processed_command, mac);
+		struct notification_job *oj = calloc(1, sizeof(struct notification_job));
+		oj->contact_name = cntct->name;
+		oj->host_name = svc->host_name;
+		oj->service_description = svc->description;
+		wproc_run_callback(processed_command, notification_timeout, notification_callback, (void*)oj, mac);
 
 		/* free memory */
 		my_free(command_name);
@@ -1692,7 +1720,11 @@ int notify_contact_of_host(nagios_macros *mac, contact *cntct, host *hst, int ty
 			}
 
 		/* run the notification command */
-		wproc_notify(cntct->name, hst->name, NULL, processed_command, mac);
+		struct notification_job *oj = calloc(1, sizeof(struct notification_job));
+		oj->contact_name = cntct->name;
+		oj->host_name = hst->name;
+		oj->service_description = NULL;
+		wproc_run_callback(processed_command, notification_timeout, notification_callback, (void*)oj, mac);
 
 		/* @todo Handle nebmod stuff when getting results from workers */
 
diff --git a/base/sehandlers.c b/base/sehandlers.c
index c51d904..8df5f25 100644
--- a/base/sehandlers.c
+++ b/base/sehandlers.c
@@ -39,6 +39,18 @@
 /************* OBSESSIVE COMPULSIVE HANDLER FUNCTIONS *************/
 /******************************************************************/
 
+static void ocsp_complete_callback(struct wproc_result *wpres, void *data, int flags) {
+	service *svc = (service *)data;
+	if(wpres) {
+		if (wpres->early_timeout) {
+			double runtime = ((double)wpres->runtime.tv_sec) + ((double)wpres->runtime.tv_usec) / 1000000.0;
+			logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: OCSP command '%s' for service '%s' on host '%s' timed out after %.2f seconds\n",
+				  wpres->command, svc->description, svc->host_name,
+				  runtime);
+		}
+	}
+}
+
 
 /* handles service check results in an obsessive compulsive manner... */
 int obsessive_compulsive_service_check_processor(service *svc) {
@@ -90,7 +102,7 @@ int obsessive_compulsive_service_check_processor(service *svc) {
 	log_debug_info(DEBUGL_CHECKS, 2, "Processed obsessive compulsive service processor command line: %s\n", processed_command);
 
 	/* run the command through a worker */
-	wproc_run_service_job(WPJOB_OCSP, ocsp_timeout, svc, processed_command, &mac);
+	wproc_run_callback(processed_command, ocsp_timeout, ocsp_complete_callback, (void*)svc, &mac);
 
 	/* free memory */
 	clear_volatile_macros_r(&mac);
@@ -100,6 +112,16 @@ int obsessive_compulsive_service_check_processor(service *svc) {
 	}
 
 
+static void ochp_complete_callback(struct wproc_result *wpres, void *data, int flags) {
+	host *hst = (host *)data;
+	if(wpres) {
+		if (wpres->early_timeout) {
+			double runtime = ((double)wpres->runtime.tv_sec) + ((double)wpres->runtime.tv_usec) / 1000000.0;
+			logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: OCHP command '%s' for host '%s' timed out after %.2f seconds\n",
+				  wpres->command, hst->name, runtime);
+		}
+	}
+}
 
 /* handles host check results in an obsessive compulsive manner... */
 int obsessive_compulsive_host_check_processor(host *hst) {
@@ -145,7 +167,7 @@ int obsessive_compulsive_host_check_processor(host *hst) {
 	log_debug_info(DEBUGL_CHECKS, 2, "Processed obsessive compulsive host processor command line: %s\n", processed_command);
 
 	/* run the command through a worker */
-	wproc_run_host_job(WPJOB_OCHP, ochp_timeout, hst, processed_command, &mac);
+	wproc_run_callback(processed_command, ochp_timeout, ochp_complete_callback, hst, &mac);
 
 	/* free memory */
 	clear_volatile_macros_r(&mac);
@@ -161,6 +183,17 @@ int obsessive_compulsive_host_check_processor(host *hst) {
 /**************** SERVICE EVENT HANDLER FUNCTIONS *****************/
 /******************************************************************/
 
+static void evthandler_complete_with_msg(struct wproc_result *wpres, void *data, int flags) {
+	char *msg = (char *)data;
+	if(wpres) {
+		if (wpres->early_timeout) {
+			double runtime = ((double)wpres->runtime.tv_sec) + ((double)wpres->runtime.tv_usec) / 1000000.0;
+			logit(NSLOG_EVENT_HANDLER | NSLOG_RUNTIME_WARNING, TRUE, msg,
+					wpres->command, runtime);
+		}
+	}
+}
+
 
 /* handles changes in the state of a service */
 int handle_service_event(service *svc) {
@@ -212,7 +245,6 @@ int run_global_service_event_handler(nagios_macros *mac, service *svc) {
 	char *raw_logentry = NULL;
 	char *processed_logentry = NULL;
 	char *command_output = NULL;
-	int early_timeout = FALSE;
 	double exectime = 0.0;
 	int result = 0;
 #ifdef USE_EVENT_BROKER
@@ -269,7 +301,7 @@ int run_global_service_event_handler(nagios_macros *mac, service *svc) {
 	/* send event data to broker */
 	end_time.tv_sec = 0L;
 	end_time.tv_usec = 0L;
-	neb_result = broker_event_handler(NEBTYPE_EVENTHANDLER_START, NEBFLAG_NONE, NEBATTR_NONE, GLOBAL_SERVICE_EVENTHANDLER, (void *)svc, svc->current_state, svc->state_type, start_time, end_time, exectime, event_handler_timeout, early_timeout, result, global_service_event_handler, processed_command, NULL, NULL);
+	neb_result = broker_event_handler(NEBTYPE_EVENTHANDLER_START, NEBFLAG_NONE, NEBATTR_NONE, GLOBAL_SERVICE_EVENTHANDLER, (void *)svc, svc->current_state, svc->state_type, start_time, end_time, exectime, event_handler_timeout, FALSE, result, global_service_event_handler, processed_command, NULL, NULL);
 
 	/* neb module wants to override (or cancel) the event handler - perhaps it will run the eventhandler itself */
 	if(neb_result == NEBERROR_CALLBACKOVERRIDE) {
@@ -281,21 +313,15 @@ int run_global_service_event_handler(nagios_macros *mac, service *svc) {
 #endif
 
 	/* run the command through a worker */
-	/* XXX FIXME make base/workers.c handle the eventbroker stuff below */
-	result = wproc_run(WPJOB_GLOBAL_SVC_EVTHANDLER, processed_command, event_handler_timeout, mac);
-
-	/* check to see if the event handler timed out */
-	if(early_timeout == TRUE)
-		logit(NSLOG_EVENT_HANDLER | NSLOG_RUNTIME_WARNING, TRUE, "Warning: Global service event handler command '%s' timed out after %d seconds\n", processed_command, event_handler_timeout);
+	result = wproc_run_callback(processed_command, event_handler_timeout, evthandler_complete_with_msg,
+			"Warning: Global service event handler command '%s' timed out after %.2f seconds\n" , mac);
 
 #ifdef USE_EVENT_BROKER
 	/* get end time */
 	gettimeofday(&end_time, NULL);
-#endif
 
-#ifdef USE_EVENT_BROKER
 	/* send event data to broker */
-	broker_event_handler(NEBTYPE_EVENTHANDLER_END, NEBFLAG_NONE, NEBATTR_NONE, GLOBAL_SERVICE_EVENTHANDLER, (void *)svc, svc->current_state, svc->state_type, start_time, end_time, exectime, event_handler_timeout, early_timeout, result, global_service_event_handler, processed_command, command_output, NULL);
+	broker_event_handler(NEBTYPE_EVENTHANDLER_END, NEBFLAG_NONE, NEBATTR_NONE, GLOBAL_SERVICE_EVENTHANDLER, (void *)svc, svc->current_state, svc->state_type, start_time, end_time, exectime, event_handler_timeout, FALSE, result, global_service_event_handler, processed_command, command_output, NULL);
 #endif
 
 	/* free memory */
@@ -316,7 +342,6 @@ int run_service_event_handler(nagios_macros *mac, service *svc) {
 	char *raw_logentry = NULL;
 	char *processed_logentry = NULL;
 	char *command_output = NULL;
-	int early_timeout = FALSE;
 	double exectime = 0.0;
 	int result = 0;
 #ifdef USE_EVENT_BROKER
@@ -369,7 +394,7 @@ int run_service_event_handler(nagios_macros *mac, service *svc) {
 	/* send event data to broker */
 	end_time.tv_sec = 0L;
 	end_time.tv_usec = 0L;
-	neb_result = broker_event_handler(NEBTYPE_EVENTHANDLER_START, NEBFLAG_NONE, NEBATTR_NONE, SERVICE_EVENTHANDLER, (void *)svc, svc->current_state, svc->state_type, start_time, end_time, exectime, event_handler_timeout, early_timeout, result, svc->event_handler, processed_command, NULL, NULL);
+	neb_result = broker_event_handler(NEBTYPE_EVENTHANDLER_START, NEBFLAG_NONE, NEBATTR_NONE, SERVICE_EVENTHANDLER, (void *)svc, svc->current_state, svc->state_type, start_time, end_time, exectime, event_handler_timeout, FALSE, result, svc->event_handler, processed_command, NULL, NULL);
 
 	/* neb module wants to override (or cancel) the event handler - perhaps it will run the eventhandler itself */
 	if(neb_result == NEBERROR_CALLBACKOVERRIDE) {
@@ -381,21 +406,15 @@ int run_service_event_handler(nagios_macros *mac, service *svc) {
 #endif
 
 	/* run the command through a worker */
-	/* XXX FIXME make base/workers.c handle the eventbroker stuff below */
-	result = wproc_run(WPJOB_SVC_EVTHANDLER, processed_command, event_handler_timeout, mac);
-
-	/* check to see if the event handler timed out */
-	if(early_timeout == TRUE)
-		logit(NSLOG_EVENT_HANDLER | NSLOG_RUNTIME_WARNING, TRUE, "Warning: Service event handler command '%s' timed out after %d seconds\n", processed_command, event_handler_timeout);
+	result = wproc_run_callback(processed_command, event_handler_timeout, evthandler_complete_with_msg,
+			"Warning: Service event handler command '%s' timed out after %.2f seconds\n" , mac);
 
 #ifdef USE_EVENT_BROKER
 	/* get end time */
 	gettimeofday(&end_time, NULL);
-#endif
 
-#ifdef USE_EVENT_BROKER
 	/* send event data to broker */
-	broker_event_handler(NEBTYPE_EVENTHANDLER_END, NEBFLAG_NONE, NEBATTR_NONE, SERVICE_EVENTHANDLER, (void *)svc, svc->current_state, svc->state_type, start_time, end_time, exectime, event_handler_timeout, early_timeout, result, svc->event_handler, processed_command, command_output, NULL);
+	broker_event_handler(NEBTYPE_EVENTHANDLER_END, NEBFLAG_NONE, NEBATTR_NONE, SERVICE_EVENTHANDLER, (void *)svc, svc->current_state, svc->state_type, start_time, end_time, exectime, event_handler_timeout, FALSE, result, svc->event_handler, processed_command, command_output, NULL);
 #endif
 
 	/* free memory */
@@ -457,7 +476,6 @@ int run_global_host_event_handler(nagios_macros *mac, host *hst) {
 	char *raw_logentry = NULL;
 	char *processed_logentry = NULL;
 	char *command_output = NULL;
-	int early_timeout = FALSE;
 	double exectime = 0.0;
 	int result = 0;
 #ifdef USE_EVENT_BROKER
@@ -513,7 +531,7 @@ int run_global_host_event_handler(nagios_macros *mac, host *hst) {
 	/* send event data to broker */
 	end_time.tv_sec = 0L;
 	end_time.tv_usec = 0L;
-	neb_result = broker_event_handler(NEBTYPE_EVENTHANDLER_START, NEBFLAG_NONE, NEBATTR_NONE, GLOBAL_HOST_EVENTHANDLER, (void *)hst, hst->current_state, hst->state_type, start_time, end_time, exectime, event_handler_timeout, early_timeout, result, global_host_event_handler, processed_command, NULL, NULL);
+	neb_result = broker_event_handler(NEBTYPE_EVENTHANDLER_START, NEBFLAG_NONE, NEBATTR_NONE, GLOBAL_HOST_EVENTHANDLER, (void *)hst, hst->current_state, hst->state_type, start_time, end_time, exectime, event_handler_timeout, FALSE, result, global_host_event_handler, processed_command, NULL, NULL);
 
 	/* neb module wants to override (or cancel) the event handler - perhaps it will run the eventhandler itself */
 	if(neb_result == NEBERROR_CALLBACKOVERRIDE) {
@@ -525,21 +543,15 @@ int run_global_host_event_handler(nagios_macros *mac, host *hst) {
 #endif
 
 	/* run the command through a worker */
-	/* XXX FIXME make base/workers.c handle the eventbroker stuff below */
-	wproc_run(WPJOB_GLOBAL_HOST_EVTHANDLER, processed_command, event_handler_timeout, mac);
-
-	/* check for a timeout in the execution of the event handler command */
-	if(early_timeout == TRUE)
-		logit(NSLOG_EVENT_HANDLER | NSLOG_RUNTIME_WARNING, TRUE, "Warning: Global host event handler command '%s' timed out after %d seconds\n", processed_command, event_handler_timeout);
+	result = wproc_run_callback(processed_command, event_handler_timeout, evthandler_complete_with_msg,
+			"Warning: Global host event handler command '%s' timed out after %.2f seconds\n" , mac);
 
 #ifdef USE_EVENT_BROKER
 	/* get end time */
 	gettimeofday(&end_time, NULL);
-#endif
 
-#ifdef USE_EVENT_BROKER
 	/* send event data to broker */
-	broker_event_handler(NEBTYPE_EVENTHANDLER_END, NEBFLAG_NONE, NEBATTR_NONE, GLOBAL_HOST_EVENTHANDLER, (void *)hst, hst->current_state, hst->state_type, start_time, end_time, exectime, event_handler_timeout, early_timeout, result, global_host_event_handler, processed_command, command_output, NULL);
+	broker_event_handler(NEBTYPE_EVENTHANDLER_END, NEBFLAG_NONE, NEBATTR_NONE, GLOBAL_HOST_EVENTHANDLER, (void *)hst, hst->current_state, hst->state_type, start_time, end_time, exectime, event_handler_timeout, FALSE, result, global_host_event_handler, processed_command, command_output, NULL);
 #endif
 
 	/* free memory */
@@ -559,7 +571,6 @@ int run_host_event_handler(nagios_macros *mac, host *hst) {
 	char *raw_logentry = NULL;
 	char *processed_logentry = NULL;
 	char *command_output = NULL;
-	int early_timeout = FALSE;
 	double exectime = 0.0;
 	int result = 0;
 #ifdef USE_EVENT_BROKER
@@ -611,7 +622,7 @@ int run_host_event_handler(nagios_macros *mac, host *hst) {
 	/* send event data to broker */
 	end_time.tv_sec = 0L;
 	end_time.tv_usec = 0L;
-	neb_result = broker_event_handler(NEBTYPE_EVENTHANDLER_START, NEBFLAG_NONE, NEBATTR_NONE, HOST_EVENTHANDLER, (void *)hst, hst->current_state, hst->state_type, start_time, end_time, exectime, event_handler_timeout, early_timeout, result, hst->event_handler, processed_command, NULL, NULL);
+	neb_result = broker_event_handler(NEBTYPE_EVENTHANDLER_START, NEBFLAG_NONE, NEBATTR_NONE, HOST_EVENTHANDLER, (void *)hst, hst->current_state, hst->state_type, start_time, end_time, exectime, event_handler_timeout, FALSE, result, hst->event_handler, processed_command, NULL, NULL);
 
 	/* neb module wants to override (or cancel) the event handler - perhaps it will run the eventhandler itself */
 	if(neb_result == NEBERROR_CALLBACKOVERRIDE) {
@@ -623,20 +634,15 @@ int run_host_event_handler(nagios_macros *mac, host *hst) {
 #endif
 
 	/* run the command through a worker */
-	result = wproc_run(WPJOB_HOST_EVTHANDLER, processed_command, event_handler_timeout, mac);
-
-	/* check to see if the event handler timed out */
-	if(early_timeout == TRUE)
-		logit(NSLOG_EVENT_HANDLER | NSLOG_RUNTIME_WARNING, TRUE, "Warning: Host event handler command '%s' timed out after %d seconds\n", processed_command, event_handler_timeout);
+	result = wproc_run_callback(processed_command, event_handler_timeout, evthandler_complete_with_msg,
+			"Warning: Host event handler command '%s' timed out after %.2f seconds\n" , mac);
 
 #ifdef USE_EVENT_BROKER
 	/* get end time */
 	gettimeofday(&end_time, NULL);
-#endif
 
-#ifdef USE_EVENT_BROKER
 	/* send event data to broker */
-	broker_event_handler(NEBTYPE_EVENTHANDLER_END, NEBFLAG_NONE, NEBATTR_NONE, HOST_EVENTHANDLER, (void *)hst, hst->current_state, hst->state_type, start_time, end_time, exectime, event_handler_timeout, early_timeout, result, hst->event_handler, processed_command, command_output, NULL);
+	broker_event_handler(NEBTYPE_EVENTHANDLER_END, NEBFLAG_NONE, NEBATTR_NONE, HOST_EVENTHANDLER, (void *)hst, hst->current_state, hst->state_type, start_time, end_time, exectime, event_handler_timeout, FALSE, result, hst->event_handler, processed_command, command_output, NULL);
 #endif
 
 	/* free memory */
diff --git a/base/workers.c b/base/workers.c
index 881e434..a6db4f5 100644
--- a/base/workers.c
+++ b/base/workers.c
@@ -19,10 +19,10 @@ struct wproc_worker;
 
 struct wproc_job {
 	unsigned int id;
-	unsigned int type;
 	unsigned int timeout;
 	char *command;
-	void *arg;
+	void (*callback)(struct wproc_result *, void *, int);
+	void *data;
 	struct wproc_worker *wp;
 };
 
@@ -52,40 +52,9 @@ static struct wproc_list workers = {0, 0, NULL};
 static dkhash_table *specialized_workers;
 static struct wproc_list *to_remove = NULL;
 
-typedef struct wproc_callback_job {
-	void *data;
-	void (*callback)(struct wproc_result *, void *, int);
-} wproc_callback_job;
-
-typedef struct wproc_object_job {
-	char *contact_name;
-	char *host_name;
-	char *service_description;
-} wproc_object_job;
-
 unsigned int wproc_num_workers_online = 0, wproc_num_workers_desired = 0;
 unsigned int wproc_num_workers_spawned = 0;
 
-#define tv2float(tv) ((float)((tv)->tv_sec) + ((float)(tv)->tv_usec) / 1000000.0)
-
-static const char *wpjob_type_name(unsigned int type)
-{
-	switch (type) {
-	case WPJOB_CHECK: return "CHECK";
-	case WPJOB_NOTIFY: return "NOTIFY";
-	case WPJOB_OCSP: return "OCSP";
-	case WPJOB_OCHP: return "OCHP";
-	case WPJOB_GLOBAL_SVC_EVTHANDLER: return "GLOBAL SERVICE EVENTHANDLER";
-	case WPJOB_SVC_EVTHANDLER: return "SERVICE EVENTHANDLER";
-	case WPJOB_GLOBAL_HOST_EVTHANDLER: return "GLOBAL HOST EVENTHANDLER";
-	case WPJOB_HOST_EVTHANDLER: return "HOST EVENTHANDLER";
-	case WPJOB_CALLBACK: return "CALLBACK";
-	case WPJOB_HOST_PERFDATA: return "HOST PERFDATA";
-	case WPJOB_SVC_PERFDATA: return "SERVICE PERFDATA";
-	}
-	return "UNKNOWN";
-}
-
 static void wproc_logdump_buffer(int level, int show, const char *prefix, char *buf)
 {
 	char *ptr, *eol;
@@ -223,7 +192,7 @@ static struct wproc_worker *get_worker(const char *cmd)
 	return wp_list->wps[wp_list->idx++ % wp_list->len];
 }
 
-static struct wproc_job *create_job(int type, void *arg, time_t timeout, const char *cmd)
+static struct wproc_job *create_job(void (*callback)(struct wproc_result *, void *, int), void *data, time_t timeout, const char *cmd)
 {
 	struct wproc_job *job;
 	struct wproc_worker *wp;
@@ -240,8 +209,8 @@ static struct wproc_job *create_job(int type, void *arg, time_t timeout, const c
 
 	job->wp = wp;
 	job->id = get_job_id(wp);
-	job->type = type;
-	job->arg = arg;
+	job->callback = callback;
+	job->data = data;
 	job->timeout = timeout;
 	if (fanout_add(wp->jobs, job->id, job) < 0 || !(job->command = strdup(cmd))) {
 		free(job);
@@ -253,16 +222,10 @@ static struct wproc_job *create_job(int type, void *arg, time_t timeout, const c
 
 static void run_job_callback(struct wproc_job *job, struct wproc_result *wpres, int val)
 {
-	wproc_callback_job *cj;
-
-	if (!job || !job->arg)
+	if (!job || !job->callback)
 		return;
-	cj = (struct wproc_callback_job *)job->arg;
-
-	if (!cj->callback)
-		return;
-	cj->callback(wpres, cj->data, val);
-	cj->callback = NULL;
+	job->callback(wpres, job->data, val);
+	job->callback = NULL;
 }
 
 static void destroy_job(struct wproc_job *job)
@@ -270,31 +233,8 @@ static void destroy_job(struct wproc_job *job)
 	if (!job)
 		return;
 
-	switch (job->type) {
-	case WPJOB_CHECK:
-		free_check_result(job->arg);
-		free(job->arg);
-		break;
-	case WPJOB_NOTIFY:
-	case WPJOB_OCSP:
-	case WPJOB_OCHP:
-		free(job->arg);
-		break;
-
-	case WPJOB_GLOBAL_SVC_EVTHANDLER:
-	case WPJOB_SVC_EVTHANDLER:
-	case WPJOB_GLOBAL_HOST_EVTHANDLER:
-	case WPJOB_HOST_EVTHANDLER:
-		/* these require nothing special */
-		break;
-	case WPJOB_CALLBACK:
-		/* call with NULL result to make callback clean things up */
-		run_job_callback(job, NULL, 0);
-		break;
-	default:
-		logit(NSLOG_RUNTIME_WARNING, TRUE, "wproc: Unknown job type: %d\n", job->type);
-		break;
-	}
+	/* Run the callback once, but with wpres=NULL if the job is destroyed */
+	run_job_callback(job, NULL, 0);
 
 	my_free(job->command);
 	if (job->wp) {
@@ -433,41 +373,6 @@ static int str2timeval(char *str, struct timeval *tv)
 	return 0;
 }
 
-static int handle_worker_check(wproc_result *wpres, struct wproc_worker *wp, struct wproc_job *job)
-{
-	int result = ERROR;
-	check_result *cr = (check_result *)job->arg;
-
-	memcpy(&cr->rusage, &wpres->rusage, sizeof(wpres->rusage));
-	cr->start_time.tv_sec = wpres->start.tv_sec;
-	cr->start_time.tv_usec = wpres->start.tv_usec;
-	cr->finish_time.tv_sec = wpres->stop.tv_sec;
-	cr->finish_time.tv_usec = wpres->stop.tv_usec;
-	if (WIFEXITED(wpres->wait_status)) {
-		cr->return_code = WEXITSTATUS(wpres->wait_status);
-	} else {
-		cr->return_code = STATE_UNKNOWN;
-	}
-
-	if (wpres->outstd && *wpres->outstd) {
-		cr->output = strdup(wpres->outstd);
-	} else if (wpres->outerr) {
-		asprintf(&cr->output, "(No output on stdout) stderr: %s", wpres->outerr);
-	} else {
-		cr->output = NULL;
-	}
-
-	cr->early_timeout = wpres->early_timeout;
-	cr->exited_ok = wpres->exited_ok;
-	cr->engine = NULL;
-	cr->source = wp->name;
-
-	process_check_result(cr);
-	free_check_result(cr);
-
-	return result;
-}
-
 /*
  * parses a worker result. We do no strdup()'s here, so when
  * kvv is destroyed, all references to strings will become
@@ -493,7 +398,7 @@ static int parse_worker_result(wproc_result *wpres, struct kvvec *kvv)
 			wpres->job_id = atoi(value);
 			break;
 		case WPRES_type:
-			wpres->type = atoi(value);
+			/* FIXME: ignored */
 			break;
 		case WPRES_command:
 			wpres->command = value;
@@ -587,7 +492,6 @@ static void fo_reassign_wproc_job(void *job_)
 
 static int handle_worker_result(int sd, int events, void *arg)
 {
-	wproc_object_job *oj = NULL;
 	char *buf, *error_reason = NULL;
 	unsigned long size;
 	int ret;
@@ -650,12 +554,6 @@ static int handle_worker_result(int sd, int events, void *arg)
 				  wpres.job_id, wp->name);
 			continue;
 		}
-		if (wpres.type != job->type) {
-			logit(NSLOG_RUNTIME_WARNING, TRUE, "wproc: %s claims job %d is type %d, but we think it's type %d\n",
-				  wp->name, job->id, wpres.type, job->type);
-			break;
-		}
-		oj = (wproc_object_job *)job->arg;
 
 		/*
 		 * ETIME ("Timer expired") doesn't really happen
@@ -674,24 +572,11 @@ static int handle_worker_result(int sd, int events, void *arg)
 			         WCOREDUMP(wpres.wait_status) ? " (core dumped)" : "",
 			         tv_delta_f(&wpres.start, &wpres.stop));
 		}
-		else if (job->type != WPJOB_CHECK && WEXITSTATUS(wpres.wait_status) != 0) {
-			asprintf(&error_reason, "is a non-check helper but exited with return code %d",
-			         WEXITSTATUS(wpres.wait_status));
-		}
+
 		if (error_reason) {
-			logit(NSLOG_RUNTIME_ERROR, TRUE, "wproc: %s job %d from worker %s %s",
-			      wpjob_type_name(job->type), job->id, wp->name, error_reason);
+			logit(NSLOG_RUNTIME_ERROR, TRUE, "wproc: job %d from worker %s %s",
+			      job->id, wp->name, error_reason);
 			logit(NSLOG_RUNTIME_ERROR, TRUE, "wproc:   command: %s\n", job->command);
-			if (job->type != WPJOB_CHECK && oj) {
-				logit(NSLOG_RUNTIME_ERROR, TRUE, "wproc:   host=%s; service=%s; contact=%s\n",
-				      oj->host_name ? oj->host_name : "(none)",
-				      oj->service_description ? oj->service_description : "(none)",
-				      oj->contact_name ? oj->contact_name : "(none)");
-			} else if (oj) {
-				struct check_result *cr = (struct check_result *)job->arg;
-				logit(NSLOG_RUNTIME_ERROR, TRUE, "wproc:   host=%s; service=%s;\n",
-				      cr->host_name, cr->service_description);
-			}
 			logit(NSLOG_RUNTIME_ERROR, TRUE, "wproc:   early_timeout=%d; exited_ok=%d; wait_status=%d; error_code=%d;\n",
 			      wpres.early_timeout, wpres.exited_ok, wpres.wait_status, wpres.error_code);
 			wproc_logdump_buffer(NSLOG_RUNTIME_ERROR, TRUE, "wproc:   stderr", wpres.outerr);
@@ -699,74 +584,9 @@ static int handle_worker_result(int sd, int events, void *arg)
 		}
 		my_free(error_reason);
 
-		switch (job->type) {
-		case WPJOB_CHECK:
-			ret = handle_worker_check(&wpres, wp, job);
-			break;
-		case WPJOB_NOTIFY:
-			if (wpres.early_timeout) {
-				if (oj->service_description) {
-					logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Notifying contact '%s' of service '%s' on host '%s' by command '%s' timed out after %.2f seconds\n",
-						  oj->contact_name, oj->service_description,
-						  oj->host_name, job->command,
-						  tv2float(&wpres.runtime));
-				} else {
-					logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Notifying contact '%s' of host '%s' by command '%s' timed out after %.2f seconds\n",
-						  oj->contact_name, oj->host_name,
-						  job->command, tv2float(&wpres.runtime));
-				}
-			}
-			break;
-		case WPJOB_OCSP:
-			if (wpres.early_timeout) {
-				logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: OCSP command '%s' for service '%s' on host '%s' timed out after %.2f seconds\n",
-					  job->command, oj->service_description, oj->host_name,
-					  tv2float(&wpres.runtime));
-			}
-			break;
-		case WPJOB_OCHP:
-			if (wpres.early_timeout) {
-				logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: OCHP command '%s' for host '%s' timed out after %.2f seconds\n",
-					  job->command, oj->host_name, tv2float(&wpres.runtime));
-			}
-			break;
-		case WPJOB_GLOBAL_SVC_EVTHANDLER:
-			if (wpres.early_timeout) {
-				logit(NSLOG_EVENT_HANDLER | NSLOG_RUNTIME_WARNING, TRUE,
-					  "Warning: Global service event handler command '%s' timed out after %.2f seconds\n",
-					  job->command, tv2float(&wpres.runtime));
-			}
-			break;
-		case WPJOB_SVC_EVTHANDLER:
-			if (wpres.early_timeout) {
-				logit(NSLOG_EVENT_HANDLER | NSLOG_RUNTIME_WARNING, TRUE,
-					  "Warning: Service event handler command '%s' timed out after %.2f seconds\n",
-					  job->command, tv2float(&wpres.runtime));
-			}
-			break;
-		case WPJOB_GLOBAL_HOST_EVTHANDLER:
-			if (wpres.early_timeout) {
-				logit(NSLOG_EVENT_HANDLER | NSLOG_RUNTIME_WARNING, TRUE,
-					  "Warning: Global host event handler command '%s' timed out after %.2f seconds\n",
-					  job->command, tv2float(&wpres.runtime));
-			}
-			break;
-		case WPJOB_HOST_EVTHANDLER:
-			if (wpres.early_timeout) {
-				logit(NSLOG_EVENT_HANDLER | NSLOG_RUNTIME_WARNING, TRUE,
-					  "Warning: Host event handler command '%s' timed out after %.2f seconds\n",
-					  job->command, tv2float(&wpres.runtime));
-			}
-			break;
+		/* Run the callback with the wpres struct as argument */
+		run_job_callback(job, &wpres, 0);
 
-		case WPJOB_CALLBACK:
-			run_job_callback(job, &wpres, 0);
-			break;
-
-		default:
-			logit(NSLOG_RUNTIME_WARNING, TRUE, "Worker %d: Unknown jobtype: %d\n", wp->pid, job->type);
-			break;
-		}
 		destroy_job(job);
 	}
 
@@ -989,7 +809,7 @@ static int wproc_run_job(struct wproc_job *job, nagios_macros *mac)
 		return ERROR;
 
 	kvvec_addkv(&kvv, "job_id", (char *)mkstr("%d", job->id));
-	kvvec_addkv(&kvv, "type", (char *)mkstr("%d", job->type));
+	kvvec_addkv(&kvv, "type", "0"); /* Dummy field for compatibility */
 	kvvec_addkv(&kvv, "command", job->command);
 	kvvec_addkv(&kvv, "timeout", (char *)mkstr("%u", job->timeout));
 	kvvb = build_kvvec_buf(&kvv);
@@ -1010,94 +830,11 @@ static int wproc_run_job(struct wproc_job *job, nagios_macros *mac)
 	return result;
 }
 
-static wproc_object_job *create_object_job(char *cname, char *hname, char *sdesc)
-{
-	wproc_object_job *oj;
-
-	oj = calloc(1, sizeof(*oj));
-	if (oj) {
-		oj->host_name = hname;
-		if (cname)
-			oj->contact_name = cname;
-		if (sdesc)
-			oj->service_description = sdesc;
-	}
-
-	return oj;
-}
-
-int wproc_notify(char *cname, char *hname, char *sdesc, char *cmd, nagios_macros *mac)
-{
-	struct wproc_job *job;
-	wproc_object_job *oj;
-
-	if (!(oj = create_object_job(cname, hname, sdesc)))
-		return ERROR;
-
-	job = create_job(WPJOB_NOTIFY, oj, notification_timeout, cmd);
-
-	return wproc_run_job(job, mac);
-}
-
-int wproc_run_service_job(int jtype, int timeout, service *svc, char *cmd, nagios_macros *mac)
-{
-	struct wproc_job *job;
-	wproc_object_job *oj;
-
-	if (!(oj = create_object_job(NULL, svc->host_name, svc->description)))
-		return ERROR;
-
-	job = create_job(jtype, oj, timeout, cmd);
-
-	return wproc_run_job(job, mac);
-}
-
-int wproc_run_host_job(int jtype, int timeout, host *hst, char *cmd, nagios_macros *mac)
-{
-	struct wproc_job *job;
-	wproc_object_job *oj;
-
-	if (!(oj = create_object_job(NULL, hst->name, NULL)))
-		return ERROR;
-
-	job = create_job(jtype, oj, timeout, cmd);
-
-	return wproc_run_job(job, mac);
-}
-
-int wproc_run_check(check_result *cr, char *cmd, nagios_macros *mac)
-{
-	struct wproc_job *job;
-	int timeout;
-
-	if (cr->service_description)
-		timeout = service_check_timeout;
-	else
-		timeout = host_check_timeout;
-
-	job = create_job(WPJOB_CHECK, cr, timeout, cmd);
-	return wproc_run_job(job, mac);
-}
-
-int wproc_run(int jtype, char *cmd, int timeout, nagios_macros *mac)
-{
-	struct wproc_job *job;
-
-	job = create_job(jtype, NULL, timeout, cmd);
-	return wproc_run_job(job, mac);
-}
-
-int wproc_run_callback(char *cmd, int timeout,
+int wproc_run_callback(char *command_line, int timeout,
 		void (*cb)(struct wproc_result *, void *, int), void *data,
 		nagios_macros *mac)
 {
 	struct wproc_job *job;
-	struct wproc_callback_job *cj;
-	if (!(cj = calloc(1, sizeof(*cj))))
-		return ERROR;
-	cj->callback = cb;
-	cj->data = data;
-
-	job = create_job(WPJOB_CALLBACK, cj, timeout, cmd);
+	job = create_job(cb, data, timeout, command_line);
 	return wproc_run_job(job, mac);
 }
diff --git a/include/workers.h b/include/workers.h
index b11229b..cf28287 100644
--- a/include/workers.h
+++ b/include/workers.h
@@ -2,28 +2,15 @@
 #define INCLUDE_workers_h__
 #include "lib/libnagios.h"
 #include "lib/worker.h"
-#include "nagios.h" /* for check_result definition */
-
-/* different jobtypes. We add more as needed */
-#define WPJOB_CHECK   0
-#define WPJOB_NOTIFY  1
-#define WPJOB_OCSP    2
-#define WPJOB_OCHP    3
-#define WPJOB_GLOBAL_SVC_EVTHANDLER 4
-#define WPJOB_SVC_EVTHANDLER  5
-#define WPJOB_GLOBAL_HOST_EVTHANDLER 6
-#define WPJOB_HOST_EVTHANDLER 7
-#define WPJOB_CALLBACK 8
-#define WPJOB_HOST_PERFDATA 9
-#define WPJOB_SVC_PERFDATA 10
 
 #define WPROC_FORCE  (1 << 0)
 
 NAGIOS_BEGIN_DECL;
 
+/* FIXME: Needs to be somewhere better... */
 typedef struct wproc_result {
 	unsigned int job_id;
-	unsigned int type;
+	unsigned int type; /* not used */
 	time_t timeout;
 	struct timeval start;
 	struct timeval stop;
@@ -49,12 +36,7 @@ extern int wproc_can_spawn(struct load_control *lc);
 extern void free_worker_memory(int flags);
 extern int workers_alive(void);
 extern int init_workers(int desired_workers);
-extern int wproc_run_check(check_result *cr, char *cmd, nagios_macros *mac);
-extern int wproc_notify(char *cname, char *hname, char *sdesc, char *cmd, nagios_macros *mac);
-extern int wproc_run(int job_type, char *cmd, int timeout, nagios_macros *mac);
-extern int wproc_run_service_job(int jtype, int timeout, service *svc, char *cmd, nagios_macros *mac);
-extern int wproc_run_host_job(int jtype, int timeout, host *hst, char *cmd, nagios_macros *mac);
-extern int wproc_run_callback(char *cmt, int timeout, void (*cb)(struct wproc_result *, void *, int), void *data, nagios_macros *mac);
+extern int wproc_run_callback(char *command_line, int timeout, void (*cb)(struct wproc_result *, void *, int), void *data, nagios_macros *mac);
 
 NAGIOS_END_DECL;
 #endif
diff --git a/xdata/xpddefault.c b/xdata/xpddefault.c
index 62bfe56..94bddbf 100644
--- a/xdata/xpddefault.c
+++ b/xdata/xpddefault.c
@@ -41,6 +41,13 @@ static FILE    *service_perfdata_fp = NULL;
 static int     host_perfdata_fd = -1;
 static int     service_perfdata_fd = -1;
 
+/******************************************************************/
+/********************** CHECK COMPLETE CALLBACK *******************/
+/******************************************************************/
+
+static void perfdata_command_callback(struct wproc_result *wpres, void *data, int flags) {
+	/* Dummy method as callback. we don't care about the result for now */
+}
 
 /******************************************************************/
 /************** INITIALIZATION & CLEANUP FUNCTIONS ****************/
@@ -338,7 +345,7 @@ int xpddefault_run_service_performance_data_command(nagios_macros *mac, service
 	log_debug_info(DEBUGL_PERFDATA, 2, "Processed service performance data command line: %s\n", processed_command_line);
 
 	/* run the command */
-	wproc_run(WPJOB_SVC_PERFDATA, processed_command_line, perfdata_timeout, NULL);
+	wproc_run_callback(processed_command_line, perfdata_timeout, perfdata_command_callback, NULL, NULL);
 
 	/* free memory */
 	my_free(processed_command_line);
@@ -379,7 +386,7 @@ int xpddefault_run_host_performance_data_command(nagios_macros *mac, host *hst)
 	log_debug_info(DEBUGL_PERFDATA, 2, "Processed host performance data command line: %s\n", processed_command_line);
 
 	/* run the command */
-	wproc_run(WPJOB_HOST_PERFDATA, processed_command_line, perfdata_timeout, NULL);
+	wproc_run_callback(processed_command_line, perfdata_timeout, perfdata_command_callback, NULL, NULL);
 
 	/* free memory */
 	my_free(processed_command_line);
-- 
1.7.1


------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and 
AppDynamics. Performance Central is your source for news, insights, 
analysis and resources for efficient Application Performance Management. 
Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel


More information about the Developers mailing list