[SCM] CTDB repository - branch master updated - ctdb-1.0.74-8-gc98f90a

Ronnie Sahlberg sahlberg at samba.org
Mon Mar 23 08:12:27 GMT 2009


The branch, master has been updated
       via  c98f90ad61c9b1e679116fbed948ddca4111968d (commit)
       via  16f31786a031255ab5b3099a0a3c745de973347a (commit)
      from  b6e34503ac094a274a569a69e3d93d92ad911f4d (commit)

http://gitweb.samba.org/?p=sahlberg/ctdb.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit c98f90ad61c9b1e679116fbed948ddca4111968d
Author: root <root at rcn1.VSOFS1.COM>
Date:   Mon Mar 23 19:07:45 2009 +1100

    add a new command "ctdb scriptstatus"
    this command shows which eventscripts were executed during the last monitoring cycle and the status from each eventscript.
    
    If an eventscript timedout or returned an error we also
    show the output from the eventscript.
    
    Example :
    [root at rcn1 ctdb-git]# ./bin/ctdb scriptstatus
    6 scripts were executed last monitoring cycle
    00.ctdb              Status:OK    Duration:0.021 Mon Mar 23 19:04:32 2009
    10.interface         Status:OK    Duration:0.048 Mon Mar 23 19:04:32 2009
    20.multipathd        Status:OK    Duration:0.011 Mon Mar 23 19:04:33 2009
    40.vsftpd            Status:OK    Duration:0.011 Mon Mar 23 19:04:33 2009
    41.httpd             Status:OK    Duration:0.011 Mon Mar 23 19:04:33 2009
    50.samba             Status:ERROR    Duration:0.057 Mon Mar 23 19:04:33 2009
       OUTPUT:ERROR: Samba tcp port 445 is not responding
    
    Add a new helper function "switch_from_server_to_client()" which both
    the recovery daemon can use as well as in the child process we start for running the actual eventscripts.
    
    Create several new controls, both for the eventscript child process to inform the master daemon of the current status of the scripts as well as for the ctdb tool to extract this information from the runninc daemon.

commit 16f31786a031255ab5b3099a0a3c745de973347a
Author: root <root at rcn1.VSOFS1.COM>
Date:   Mon Mar 23 12:37:30 2009 +1100

    create a helper function that converts a ctdb instance in daemon mode to become
    a ctdb client instance.
    
    use this from the recovery daemon child process to switch to client mode
    and connect back to the main daemon

-----------------------------------------------------------------------

Summary of changes:
 client/ctdb_client.c   |  143 ++++++++++++++++++++++
 include/ctdb.h         |   26 ++++
 include/ctdb_private.h |   22 ++++
 server/ctdb_control.c  |   19 +++
 server/ctdb_logging.c  |    4 +
 server/ctdb_recoverd.c |   30 +----
 server/eventscript.c   |  305 +++++++++++++++++++++++++++++++++++++++++++++++-
 tools/ctdb.c           |   39 ++++++
 8 files changed, 560 insertions(+), 28 deletions(-)


Changeset truncated at 500 lines:

diff --git a/client/ctdb_client.c b/client/ctdb_client.c
index 16fc03b..3bdb4b2 100644
--- a/client/ctdb_client.c
+++ b/client/ctdb_client.c
@@ -3472,3 +3472,146 @@ int ctdb_ctrl_recd_ping(struct ctdb_context *ctdb)
 
 	return 0;
 }
+
+/* when forking the main daemon and the child process needs to connect back
+ * to the daemon as a client process, this function can be used to change
+ * the ctdb context from daemon into client mode
+ */
+int switch_from_server_to_client(struct ctdb_context *ctdb)
+{
+	int ret;
+
+	/* shutdown the transport */
+	if (ctdb->methods) {
+		ctdb->methods->shutdown(ctdb);
+	}
+
+	/* get a new event context */
+	talloc_free(ctdb->ev);
+	ctdb->ev = event_context_init(ctdb);
+
+	close(ctdb->daemon.sd);
+	ctdb->daemon.sd = -1;
+
+	/* the client does not need to be realtime */
+	if (ctdb->do_setsched) {
+		ctdb_restore_scheduler(ctdb);
+	}
+
+	/* initialise ctdb */
+	ret = ctdb_socket_connect(ctdb);
+	if (ret != 0) {
+		DEBUG(DEBUG_ALERT, (__location__ " Failed to init ctdb client\n"));
+		return -1;
+	}
+
+	 return 0;
+}
+
+/*
+  tell the main daemon we are starting a new monitor event script
+ */
+int ctdb_ctrl_event_script_init(struct ctdb_context *ctdb)
+{
+	int ret;
+	int32_t res;
+
+	ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, 0, CTDB_CONTROL_EVENT_SCRIPT_INIT, 0, tdb_null, 
+			   ctdb, NULL, &res, NULL, NULL);
+	if (ret != 0 || res != 0) {
+		DEBUG(DEBUG_ERR,("Failed to send event_script_init\n"));
+		return -1;
+	}
+
+	return 0;
+}
+
+/*
+  tell the main daemon we are starting a new monitor event script
+ */
+int ctdb_ctrl_event_script_finished(struct ctdb_context *ctdb)
+{
+	int ret;
+	int32_t res;
+
+	ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, 0, CTDB_CONTROL_EVENT_SCRIPT_FINISHED, 0, tdb_null, 
+			   ctdb, NULL, &res, NULL, NULL);
+	if (ret != 0 || res != 0) {
+		DEBUG(DEBUG_ERR,("Failed to send event_script_init\n"));
+		return -1;
+	}
+
+	return 0;
+}
+
+/*
+  tell the main daemon we are starting to run an eventscript
+ */
+int ctdb_ctrl_event_script_start(struct ctdb_context *ctdb, const char *name)
+{
+	int ret;
+	int32_t res;
+	TDB_DATA data;
+
+	data.dptr = discard_const(name);
+	data.dsize = strlen(name)+1;
+
+	ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, 0, CTDB_CONTROL_EVENT_SCRIPT_START, 0, data, 
+			   ctdb, NULL, &res, NULL, NULL);
+	if (ret != 0 || res != 0) {
+		DEBUG(DEBUG_ERR,("Failed to send event_script_start\n"));
+		return -1;
+	}
+
+	return 0;
+}
+
+/*
+  tell the main daemon the status of the script we ran
+ */
+int ctdb_ctrl_event_script_stop(struct ctdb_context *ctdb, int32_t result)
+{
+	int ret;
+	int32_t res;
+	TDB_DATA data;
+
+	data.dptr = (uint8_t *)&result;
+	data.dsize = sizeof(result);
+
+	ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, 0, CTDB_CONTROL_EVENT_SCRIPT_STOP, 0, data, 
+			   ctdb, NULL, &res, NULL, NULL);
+	if (ret != 0 || res != 0) {
+		DEBUG(DEBUG_ERR,("Failed to send event_script_stop\n"));
+		return -1;
+	}
+
+	return 0;
+}
+
+
+/*
+  get the status of running the monitor eventscripts
+ */
+int ctdb_ctrl_getscriptstatus(struct ctdb_context *ctdb, 
+		struct timeval timeout, uint32_t destnode, 
+		TALLOC_CTX *mem_ctx,
+		struct ctdb_monitoring_wire **script_status)
+{
+	int ret;
+	TDB_DATA outdata;
+	int32_t res;
+
+	ret = ctdb_control(ctdb, destnode, 0, 
+			   CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS, 0, tdb_null, 
+			   mem_ctx, &outdata, &res, &timeout, NULL);
+	if (ret != 0 || res != 0 || outdata.dsize == 0) {
+		DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getscriptstatus failed ret:%d res:%d\n", ret, res));
+		return -1;
+	}
+
+	*script_status = (struct ctdb_monitoring_wire *)talloc_memdup(mem_ctx, outdata.dptr, outdata.dsize);
+	talloc_free(outdata.dptr);
+		    
+	return 0;
+}
+
diff --git a/include/ctdb.h b/include/ctdb.h
index 2ec477f..866ba76 100644
--- a/include/ctdb.h
+++ b/include/ctdb.h
@@ -610,4 +610,30 @@ int ctdb_transaction_commit(struct ctdb_transaction_handle *h);
 
 int ctdb_ctrl_recd_ping(struct ctdb_context *ctdb);
 
+int switch_from_server_to_client(struct ctdb_context *ctdb);
+
+#define MONITOR_SCRIPT_OK      0
+#define MONITOR_SCRIPT_TIMEOUT 1
+
+#define MAX_SCRIPT_NAME 31
+#define MAX_SCRIPT_OUTPUT 511
+struct ctdb_monitoring_script_wire {
+	char name[MAX_SCRIPT_NAME+1];
+	struct timeval start;
+	struct timeval finished;
+	int32_t status;
+	int32_t timedout;
+	char output[MAX_SCRIPT_OUTPUT+1];
+};
+
+struct ctdb_monitoring_wire {
+	uint32_t num_scripts;
+	struct ctdb_monitoring_script_wire scripts[1];
+};
+
+int ctdb_ctrl_getscriptstatus(struct ctdb_context *ctdb, 
+		    struct timeval timeout, uint32_t destnode, 
+		    TALLOC_CTX *mem_ctx, struct ctdb_monitoring_wire **script_status);
+
+
 #endif
diff --git a/include/ctdb_private.h b/include/ctdb_private.h
index ceac384..988b6d9 100644
--- a/include/ctdb_private.h
+++ b/include/ctdb_private.h
@@ -412,6 +412,8 @@ struct ctdb_context {
 	TALLOC_CTX *eventscripts_ctx; /* a context to hold data for the RUN_EVENTSCRIPTS control */
 	uint32_t *recd_ping_count;
 	TALLOC_CTX *release_ips_ctx; /* a context used to automatically drop all IPs if we fail to recover the node */
+	TALLOC_CTX *script_monitoring_ctx; /* a context where we store results while running the monitor event */
+	TALLOC_CTX *last_monitoring_ctx; 
 };
 
 struct ctdb_db_context {
@@ -550,6 +552,11 @@ enum ctdb_controls {CTDB_CONTROL_PROCESS_EXISTS          = 0,
 		    CTDB_CONTROL_TAKEOVER_IP             = 89,
 		    CTDB_CONTROL_GET_PUBLIC_IPS          = 90,
 		    CTDB_CONTROL_GET_NODEMAP             = 91,
+		    CTDB_CONTROL_EVENT_SCRIPT_INIT       = 92,
+		    CTDB_CONTROL_EVENT_SCRIPT_START      = 93,
+		    CTDB_CONTROL_EVENT_SCRIPT_STOP       = 94,
+		    CTDB_CONTROL_EVENT_SCRIPT_FINISHED   = 95,
+		    CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS = 96,
 };	
 
 /*
@@ -1401,4 +1408,19 @@ int32_t ctdb_control_set_recmaster(struct ctdb_context *ctdb, uint32_t opcode, T
 
 extern int script_log_level;
 
+int ctdb_ctrl_event_script_init(struct ctdb_context *ctdb);
+int ctdb_ctrl_event_script_start(struct ctdb_context *ctdb, const char *name);
+int ctdb_ctrl_event_script_stop(struct ctdb_context *ctdb, int32_t res);
+int ctdb_ctrl_event_script_finished(struct ctdb_context *ctdb);
+
+int32_t ctdb_control_event_script_init(struct ctdb_context *ctdb);
+int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, TDB_DATA indata);
+int32_t ctdb_control_event_script_stop(struct ctdb_context *ctdb, TDB_DATA indata);
+int32_t ctdb_control_event_script_finished(struct ctdb_context *ctdb);
+
+
+int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb, TDB_DATA *outdata);
+
+int ctdb_log_event_script_output(struct ctdb_context *ctdb, char *str, uint16_t len);
+
 #endif
diff --git a/server/ctdb_control.c b/server/ctdb_control.c
index b8b31c9..ac77696 100644
--- a/server/ctdb_control.c
+++ b/server/ctdb_control.c
@@ -418,6 +418,25 @@ static int32_t ctdb_control_dispatch(struct ctdb_context *ctdb,
 		CHECK_CONTROL_DATA_SIZE(0);
 		return ctdb_control_recd_ping(ctdb);
 
+	case CTDB_CONTROL_EVENT_SCRIPT_INIT:
+		CHECK_CONTROL_DATA_SIZE(0);
+		return ctdb_control_event_script_init(ctdb);
+
+	case CTDB_CONTROL_EVENT_SCRIPT_START:
+		return ctdb_control_event_script_start(ctdb, indata);
+	
+	case CTDB_CONTROL_EVENT_SCRIPT_STOP:
+		CHECK_CONTROL_DATA_SIZE(sizeof(int32_t));
+		return ctdb_control_event_script_stop(ctdb, indata);
+
+	case CTDB_CONTROL_EVENT_SCRIPT_FINISHED:
+		CHECK_CONTROL_DATA_SIZE(0);
+		return ctdb_control_event_script_finished(ctdb);
+
+	case CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS:
+		CHECK_CONTROL_DATA_SIZE(0);
+		return ctdb_control_get_event_script_status(ctdb, outdata);
+
 	default:
 		DEBUG(DEBUG_CRIT,(__location__ " Unknown CTDB control opcode %u\n", opcode));
 		return -1;
diff --git a/server/ctdb_logging.c b/server/ctdb_logging.c
index 06c7eb8..45a9f74 100644
--- a/server/ctdb_logging.c
+++ b/server/ctdb_logging.c
@@ -163,6 +163,8 @@ static void ctdb_log_handler(struct event_context *ev, struct fd_event *fde,
 		}
 		if (script_log_level <= LogLevel) {
 			do_debug("%*.*s\n", n2, n2, ctdb->log->buf);
+			/* log it in the eventsystem as well */
+			ctdb_log_event_script_output(ctdb, ctdb->log->buf, n2);
 		}
 		memmove(ctdb->log->buf, p+1, sizeof(ctdb->log->buf) - n1);
 		ctdb->log->buf_used -= n1;
@@ -174,6 +176,8 @@ static void ctdb_log_handler(struct event_context *ev, struct fd_event *fde,
 		if (script_log_level <= LogLevel) {
 			do_debug("%*.*s\n", 
 				(int)ctdb->log->buf_used, (int)ctdb->log->buf_used, ctdb->log->buf);
+			/* log it in the eventsystem as well */
+			ctdb_log_event_script_output(ctdb, ctdb->log->buf, ctdb->log->buf_used);
 		}
 		ctdb->log->buf_used = 0;
 	}
diff --git a/server/ctdb_recoverd.c b/server/ctdb_recoverd.c
index 540749d..28be460 100644
--- a/server/ctdb_recoverd.c
+++ b/server/ctdb_recoverd.c
@@ -2906,7 +2906,6 @@ static void recd_sig_child_handler(struct event_context *ev,
  */
 int ctdb_start_recoverd(struct ctdb_context *ctdb)
 {
-	int ret;
 	int fd[2];
 	struct signal_event *se;
 
@@ -2931,35 +2930,16 @@ int ctdb_start_recoverd(struct ctdb_context *ctdb)
 
 	close(fd[1]);
 
-	/* shutdown the transport */
-	if (ctdb->methods) {
-		ctdb->methods->shutdown(ctdb);
-	}
-
-	/* get a new event context */
-	talloc_free(ctdb->ev);
-	ctdb->ev = event_context_init(ctdb);
-
-	event_add_fd(ctdb->ev, ctdb, fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE, 
-		     ctdb_recoverd_parent, &fd[0]);	
-
-	close(ctdb->daemon.sd);
-	ctdb->daemon.sd = -1;
-
 	srandom(getpid() ^ time(NULL));
 
-	/* the recovery daemon does not need to be realtime */
-	if (ctdb->do_setsched) {
-		ctdb_restore_scheduler(ctdb);
-	}
-
-	/* initialise ctdb */
-	ret = ctdb_socket_connect(ctdb);
-	if (ret != 0) {
-		DEBUG(DEBUG_ALERT, (__location__ " Failed to init ctdb\n"));
+	if (switch_from_server_to_client(ctdb) != 0) {
+		DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch recovery daemon into client mode. shutting down.\n"));
 		exit(1);
 	}
 
+	event_add_fd(ctdb->ev, ctdb, fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE, 
+		     ctdb_recoverd_parent, &fd[0]);	
+
 	/* set up a handler to pick up sigchld */
 	se = event_add_signal(ctdb->ev, ctdb,
 				     SIGCHLD, 0,
diff --git a/server/eventscript.c b/server/eventscript.c
index 6edd1a4..2d0ac40 100644
--- a/server/eventscript.c
+++ b/server/eventscript.c
@@ -52,6 +52,235 @@ struct ctdb_event_script_state {
 	const char *options;
 };
 
+
+struct ctdb_monitor_script_status {
+	struct ctdb_monitor_script_status *next;
+	const char *name;
+	struct timeval start;
+	struct timeval finished;
+	int32_t status;
+	int32_t timedout;
+	char *output;
+};
+
+struct ctdb_monitoring_status {
+	struct timeval start;
+	struct timeval finished;
+	int32_t status;
+	struct ctdb_monitor_script_status *scripts;
+};
+
+
+/* called from ctdb_logging when we have received output on STDERR from
+ * one of the eventscripts
+ */
+int ctdb_log_event_script_output(struct ctdb_context *ctdb, char *str, uint16_t len)
+{
+	struct ctdb_monitoring_status *monitoring_status =
+		talloc_get_type(ctdb->script_monitoring_ctx,
+			struct ctdb_monitoring_status);
+	struct ctdb_monitor_script_status *script;
+
+	if (monitoring_status == NULL) {
+		return -1;
+	}
+
+	script = monitoring_status->scripts;
+	if (script == NULL) {
+		return -1;
+	}
+
+	if (script->output == NULL) {
+		script->output = talloc_asprintf(script, "%*.*s", len, len, str);
+	} else {
+		script->output = talloc_asprintf_append(script->output, "%*.*s", len, len, str);
+	}
+
+	return 0;
+}
+
+/* called from the event script child process when we are starting a new
+ * monitor event
+ */
+int32_t ctdb_control_event_script_init(struct ctdb_context *ctdb)
+{
+	struct ctdb_monitoring_status *monitoring_status;
+
+	DEBUG(DEBUG_INFO, ("event script init called\n"));
+	if (ctdb->script_monitoring_ctx != NULL) {
+		talloc_free(ctdb->script_monitoring_ctx);
+		ctdb->script_monitoring_ctx = NULL;
+	}
+
+	monitoring_status = talloc_zero(ctdb, struct ctdb_monitoring_status);
+	if (monitoring_status == NULL) {
+		DEBUG(DEBUG_ERR, (__location__ " ERROR: Failed to talloc script_monitoring context\n"));
+		return -1;
+	}
+
+	ctdb->script_monitoring_ctx = monitoring_status;
+	monitoring_status->start = timeval_current();	
+
+	return 0;
+}
+
+
+/* called from the event script child process when we are star running
+ * an eventscript
+ */
+int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, TDB_DATA indata)
+{
+	const char *name = (const char *)indata.dptr;
+	struct ctdb_monitoring_status *monitoring_status =
+		talloc_get_type(ctdb->script_monitoring_ctx,
+			struct ctdb_monitoring_status);
+	struct ctdb_monitor_script_status *script;
+
+	DEBUG(DEBUG_INFO, ("event script start called : %s\n", name));
+
+	if (monitoring_status == NULL) {
+		DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when starting to run script %s\n", name));
+		return -1;
+	}
+
+	script = talloc_zero(monitoring_status, struct ctdb_monitor_script_status);
+	if (script == NULL) {
+		DEBUG(DEBUG_ERR,(__location__ " Failed to talloc ctdb_monitor_script_status for script %s\n", name));
+		return -1;
+	}
+
+	script->next  = monitoring_status->scripts;
+	script->name  = talloc_strdup(script, name);
+	script->start = timeval_current();
+	monitoring_status->scripts = script;
+
+	return 0;
+}
+
+/* called from the event script child process when we have finished running
+ * an eventscript
+ */
+int32_t ctdb_control_event_script_stop(struct ctdb_context *ctdb, TDB_DATA indata)
+{
+	int32_t res = *((int32_t *)indata.dptr);
+	struct ctdb_monitoring_status *monitoring_status =
+		talloc_get_type(ctdb->script_monitoring_ctx,
+			struct ctdb_monitoring_status);
+	struct ctdb_monitor_script_status *script;
+
+	DEBUG(DEBUG_INFO, ("event script stop called : %d\n", (int)res));
+
+	if (monitoring_status == NULL) {
+		DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when script finished.\n"));
+		return -1;
+	}
+
+	script = monitoring_status->scripts;
+	if (script == NULL) {
+		DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
+		return -1;
+	}
+
+	script->finished = timeval_current();
+	script->status   = res;
+
+	return 0;
+}
+
+/* called from the event script child process when we have completed a
+ * monitor event
+ */
+int32_t ctdb_control_event_script_finished(struct ctdb_context *ctdb)
+{
+	struct ctdb_monitoring_status *monitoring_status =
+		talloc_get_type(ctdb->script_monitoring_ctx,
+			struct ctdb_monitoring_status);
+
+	DEBUG(DEBUG_INFO, ("event script finished called\n"));
+
+	if (monitoring_status == NULL) {
+		DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when monitoring event finished\n"));
+		return -1;
+	}
+
+	monitoring_status->finished = timeval_current();	
+	monitoring_status->status   = MONITOR_SCRIPT_OK;
+	if (ctdb->last_monitoring_ctx) {
+		talloc_free(ctdb->last_monitoring_ctx);
+	}


-- 
CTDB repository


More information about the samba-cvs mailing list