Просмотр исходного кода

Qdevice: Implement status command

Signed-off-by: Jan Friesse <jfriesse@redhat.com>
Jan Friesse 9 лет назад
Родитель
Сommit
8a16901fee

+ 1 - 1
qdevices/Makefile.am

@@ -56,7 +56,7 @@ corosync_qdevice_SOURCES	= corosync-qdevice.c qdevice-cmap.c qdevice-instance.c
 				  msg.c msgio.c nss-sock.c tlv.c \
 				  unix-socket.c unix-socket-client.c unix-socket-client-list.c \
 				  unix-socket-ipc.c qdevice-ipc.c pr-poll-array.c dynar-simple-lex.c \
-				  dynar-str.c \
+				  dynar-str.c qdevice-ipc-cmd.c \
 				  qdevice-net-poll.c qdevice-net-send.c qdevice-net-votequorum.c \
 				  qdevice-net-socket.c qdevice-net-nss.c qdevice-net-msg-received.c \
 				  qdevice-net-cast-vote-timer.c qdevice-net-echo-request-timer.c \

+ 2 - 0
qdevices/qdevice-instance.c

@@ -44,6 +44,8 @@ qdevice_instance_init(struct qdevice_instance *instance)
 
 	node_list_init(&instance->config_node_list);
 
+	instance->vq_last_poll = ((time_t) -1);
+
 	return (0);
 }
 

+ 3 - 0
qdevices/qdevice-instance.h

@@ -96,6 +96,9 @@ struct qdevice_instance {
 	 */
 	uint32_t vq_expected_votes;
 
+	time_t vq_last_poll;
+	int vq_last_poll_cast_vote;
+
 	void *model_data;
 };
 

+ 263 - 0
qdevices/qdevice-ipc-cmd.c

@@ -0,0 +1,263 @@
+/*
+ * Copyright (c) 2015-2016 Red Hat, Inc.
+ *
+ * All rights reserved.
+ *
+ * Author: Jan Friesse (jfriesse@redhat.com)
+ *
+ * This software licensed under BSD license, the text of which follows:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice,
+ *   this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * - Neither the name of the Red Hat, Inc. nor the names of its
+ *   contributors may be used to endorse or promote products derived from this
+ *   software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "qdevice-ipc-cmd.h"
+#include "qdevice-log.h"
+#include "qdevice-model.h"
+#include "dynar-str.h"
+#include "utils.h"
+
+static int
+qdevice_ipc_cmd_status_add_header(struct qdevice_instance *instance, struct dynar *outbuf,
+    int verbose)
+{
+
+	return ((dynar_str_catf(outbuf, "Qdevice information\n") != -1) &&
+	    (dynar_str_catf(outbuf, "-------------------\n") != -1));
+}
+
+static int
+qdevice_ipc_cmd_status_add_model(struct qdevice_instance *instance, struct dynar *outbuf,
+    int verbose)
+{
+
+	return (dynar_str_catf(outbuf, "Model:\t\t\t%s\n",
+		qdevice_mode_type_to_str(instance->model_type)) != -1);
+}
+
+static int
+qdevice_ipc_cmd_status_add_nodeid(struct qdevice_instance *instance, struct dynar *outbuf,
+    int verbose)
+{
+
+	return (dynar_str_catf(outbuf, "Node ID:\t\t"UTILS_PRI_NODE_ID"\n",
+	    instance->node_id) != -1);
+}
+
+static int
+qdevice_ipc_cmd_status_add_intervals(struct qdevice_instance *instance, struct dynar *outbuf,
+    int verbose)
+{
+
+	if (!verbose) {
+		return (1);
+	}
+
+	return ((dynar_str_catf(outbuf, "HB interval:\t\t%"PRIu32"ms\n",
+	    instance->heartbeat_interval) != -1) &&
+	    (dynar_str_catf(outbuf, "Sync HB interval:\t%"PRIu32"ms\n",
+	    instance->sync_heartbeat_interval) != -1));
+}
+
+static int
+qdevice_ipc_cmd_status_add_config_node_list(struct qdevice_instance *instance, struct dynar *outbuf,
+    int verbose)
+{
+        struct node_list_entry *node_info;
+	size_t zi;
+
+	if (instance->config_node_list_version_set) {
+		if (dynar_str_catf(outbuf, "Configuration version:\t"UTILS_PRI_CONFIG_VERSION"\n",
+		    instance->config_node_list_version) == -1) {
+			return (0);
+		}
+	}
+
+	if (dynar_str_catf(outbuf, "Configured node list:\n") == -1) {
+		return (0);
+	}
+
+	zi = 0;
+
+	TAILQ_FOREACH(node_info, &instance->config_node_list, entries) {
+		if ((dynar_str_catf(outbuf, "    %zu\tNode ID = "UTILS_PRI_NODE_ID, zi,
+		    node_info->node_id) == -1) ||
+		    (node_info->data_center_id != 0 && dynar_str_catf(outbuf, ", Data center ID = "
+		    UTILS_PRI_DATACENTER_ID, node_info->data_center_id) == -1) ||
+		    (dynar_str_catf(outbuf, "\n") == -1)) {
+			return (0);
+		}
+
+		zi++;
+	}
+
+	return (1);
+}
+
+static int
+qdevice_ipc_cmd_status_add_membership_node_list(struct qdevice_instance *instance, struct dynar *outbuf,
+    int verbose)
+{
+	uint32_t u32;
+
+	if (verbose && dynar_str_catf(outbuf, "Ring ID:\t\t"UTILS_PRI_RING_ID"\n",
+	    instance->vq_node_list_ring_id.nodeid, instance->vq_node_list_ring_id.seq) == -1) {
+		return (0);
+	}
+
+	if (dynar_str_catf(outbuf, "Membership node list:\t") == -1) {
+		return (0);
+	}
+
+	for (u32 = 0; u32 < instance->vq_node_list_entries; u32++) {
+		if (u32 != 0) {
+			if (dynar_str_catf(outbuf, ", ") == -1) {
+				return (0);
+			}
+		}
+
+		if (dynar_str_catf(outbuf, UTILS_PRI_NODE_ID, instance->vq_node_list[u32]) == -1) {
+			return (0);
+		}
+	}
+
+	if (dynar_str_catf(outbuf, "\n") == -1) {
+		return (0);
+	}
+
+	return (1);
+}
+
+static const char *
+qdevice_ipc_cmd_vq_nodestate_to_str(uint32_t state)
+{
+
+	switch (state) {
+	case VOTEQUORUM_NODESTATE_MEMBER: return ("member"); break;
+	case VOTEQUORUM_NODESTATE_DEAD: return ("dead"); break;
+	case VOTEQUORUM_NODESTATE_LEAVING: return ("leaving"); break;
+	default:
+		qdevice_log(LOG_ERR, "qdevice_ipc_cmd_vq_nodestate_to_str: Unhandled votequorum "
+		    "node state %"PRIu32, state);
+		exit(1);
+		break;
+	}
+
+	return ("Unhandled votequorum node state");
+}
+
+static int
+qdevice_ipc_cmd_status_add_quorum_node_list(struct qdevice_instance *instance, struct dynar *outbuf,
+    int verbose)
+{
+	uint32_t u32;
+	votequorum_node_t *node;
+
+	if (!verbose) {
+		return (1);
+	}
+
+	if (dynar_str_catf(outbuf, "Quorate:\t\t%s\n",
+	    (instance->vq_quorum_quorate ? "Yes" : "No")) == -1) {
+		return (0);
+	}
+
+	if (dynar_str_catf(outbuf, "Quorum node list:\n") == -1) {
+		return (0);
+	}
+
+	for (u32 = 0; u32 < instance->vq_quorum_node_list_entries; u32++) {
+		node = &instance->vq_quorum_node_list[u32];
+
+		if (node->nodeid == 0) {
+			continue ;
+		}
+
+		if (dynar_str_catf(outbuf, "    %"PRIu32"\tNode ID = "UTILS_PRI_NODE_ID
+		    ", State = %s\n", u32, node->nodeid,
+		    qdevice_ipc_cmd_vq_nodestate_to_str(node->state)) == -1) {
+			return (0);
+		}
+	}
+
+	return (1);
+}
+
+static int
+qdevice_ipc_cmd_status_add_expected_votes(struct qdevice_instance *instance, struct dynar *outbuf,
+    int verbose)
+{
+
+	if (!verbose) {
+		return (1);
+	}
+
+	return (dynar_str_catf(outbuf, "Expected votes:\t\t"UTILS_PRI_EXPECTED_VOTES"\n",
+	    instance->vq_expected_votes) != -1);
+}
+
+static int
+qdevice_ipc_cmd_status_add_last_poll(struct qdevice_instance *instance, struct dynar *outbuf,
+    int verbose)
+{
+	struct tm tm_res;
+
+	if (!verbose) {
+		return (1);
+	}
+
+	if (instance->vq_last_poll == ((time_t) -1)) {
+		return (dynar_str_catf(outbuf, "Last poll call:\t\tNever\n") != -1);
+	}
+
+	localtime_r(&instance->vq_last_poll, &tm_res);
+
+	if (dynar_str_catf(outbuf, "Last poll call:\t\t%04d-%02d-%02dT%02d:%02d:%02d%s\n",
+	    tm_res.tm_year + 1900, tm_res.tm_mon + 1, tm_res.tm_mday,
+	    tm_res.tm_hour, tm_res.tm_min, tm_res.tm_sec,
+	    (instance->vq_last_poll_cast_vote ? " (cast vote)" : "")) == -1) {
+		return (0);
+	}
+
+	return (1);
+}
+
+int
+qdevice_ipc_cmd_status(struct qdevice_instance *instance, struct dynar *outbuf, int verbose)
+{
+
+	if (qdevice_ipc_cmd_status_add_header(instance, outbuf, verbose) &&
+	    qdevice_ipc_cmd_status_add_model(instance, outbuf, verbose) &&
+	    qdevice_ipc_cmd_status_add_nodeid(instance, outbuf, verbose) &&
+	    qdevice_ipc_cmd_status_add_intervals(instance, outbuf, verbose) &&
+	    qdevice_ipc_cmd_status_add_config_node_list(instance, outbuf, verbose) &&
+	    qdevice_ipc_cmd_status_add_membership_node_list(instance, outbuf, verbose) &&
+	    qdevice_ipc_cmd_status_add_quorum_node_list(instance, outbuf, verbose) &&
+	    qdevice_ipc_cmd_status_add_expected_votes(instance, outbuf, verbose) &&
+	    qdevice_ipc_cmd_status_add_last_poll(instance, outbuf, verbose)) {
+		return (0);
+	}
+
+	return (-1);
+}

+ 52 - 0
qdevices/qdevice-ipc-cmd.h

@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2015-2016 Red Hat, Inc.
+ *
+ * All rights reserved.
+ *
+ * Author: Jan Friesse (jfriesse@redhat.com)
+ *
+ * This software licensed under BSD license, the text of which follows:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice,
+ *   this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * - Neither the name of the Red Hat, Inc. nor the names of its
+ *   contributors may be used to endorse or promote products derived from this
+ *   software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _QDEVICE_IPC_CMD_H_
+#define _QDEVICE_IPC_CMD_H_
+
+#include "dynar.h"
+#include "qdevice-instance.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern int	qdevice_ipc_cmd_status(struct qdevice_instance *instance, struct dynar *outbuf,
+    int verbose);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _QDEVICE_IPC_CMD_H_ */

+ 31 - 3
qdevices/qdevice-ipc.c

@@ -38,6 +38,7 @@
 #include "unix-socket-ipc.h"
 #include "dynar-simple-lex.h"
 #include "dynar-str.h"
+#include "qdevice-ipc-cmd.h"
 
 int
 qdevice_ipc_init(struct qdevice_instance *instance)
@@ -163,7 +164,11 @@ qdevice_ipc_send_buffer(struct qdevice_instance *instance, struct unix_socket_cl
 	if (dynar_str_prepend(&client->send_buffer, "OK\n") != 0) {
 		qdevice_log(LOG_ERR, "Can't send error to client (buffer too small)");
 
-		return (-1);
+		if (qdevice_ipc_send_error(instance, client, "Internal IPC buffer too small") != 0) {
+			return (-1);
+		}
+
+		return (0);
 	}
 
 	unix_socket_client_write_buffer(client, 1);
@@ -178,15 +183,22 @@ qdevice_ipc_parse_line(struct qdevice_instance *instance, struct unix_socket_cli
 	struct dynar *token;
 	char *str;
 	struct qdevice_ipc_user_data *ipc_user_data;
+	int verbose;
 
 	ipc_user_data = (struct qdevice_ipc_user_data *)client->user_data;
 
 	dynar_simple_lex_init(&lex, &client->receive_buffer);
 	token = dynar_simple_lex_token_next(&lex);
 
+	verbose = 0;
+
 	if (token == NULL) {
 		qdevice_log(LOG_ERR, "Can't alloc memory for simple lex");
 
+		if (qdevice_ipc_send_error(instance, client, "Command too long") != 0) {
+			client->schedule_disconnect = 1;
+		}
+
 		return;
 	}
 
@@ -205,8 +217,24 @@ qdevice_ipc_parse_line(struct qdevice_instance *instance, struct unix_socket_cli
 			client->schedule_disconnect = 1;
 		}
 	} else if (strcasecmp(str, "status") == 0) {
-		qdevice_log(LOG_DEBUG, "IPC client requested status display");
-		// Send output
+		token = dynar_simple_lex_token_next(&lex);
+		str = dynar_data(token);
+
+		if (token != NULL && strcmp(str, "") != 0) {
+			if (strcasecmp(str, "verbose") == 0) {
+				verbose = 1;
+			}
+		}
+
+		if (qdevice_ipc_cmd_status(instance, &client->send_buffer, verbose) != 0) {
+			if (qdevice_ipc_send_error(instance, client, "Can't get QDevice status") != 0) {
+				client->schedule_disconnect = 1;
+			}
+		} else {
+			if (qdevice_ipc_send_buffer(instance, client) != 0) {
+				client->schedule_disconnect = 1;
+			}
+		}
 	} else {
 		qdevice_log(LOG_DEBUG, "IPC client sent unknown command");
 		if (qdevice_ipc_send_error(instance, client, "Unknown command '%s'", str) != 0) {

+ 15 - 0
qdevices/qdevice-model.c

@@ -194,3 +194,18 @@ qdevice_model_str_to_type(const char *str, enum qdevice_model_type *model_type)
 
 	return (-1);
 }
+
+const char *
+qdevice_mode_type_to_str(enum qdevice_model_type model_type)
+{
+
+	switch (model_type) {
+	case QDEVICE_MODEL_TYPE_NET: return ("Net"); break;
+	case QDEVICE_MODEL_TYPE_ARRAY_SIZE: return ("Unknown model"); break;
+	/*
+	 * Default is not defined intentionally. Compiler shows warning when new model is added
+	 */
+	}
+
+	return ("Unknown model");
+}

+ 4 - 2
qdevices/qdevice-model.h

@@ -79,14 +79,16 @@ struct qdevice_model {
 	    uint32_t expected_votes);
 };
 
-extern int		qdevice_model_register(
+extern int		 qdevice_model_register(
     enum qdevice_model_type model_type, struct qdevice_model *model);
 
 extern void qdevice_model_register_all(void);
 
-extern int		qdevice_model_str_to_type(const char *str,
+extern int		 qdevice_model_str_to_type(const char *str,
     enum qdevice_model_type *model_type);
 
+extern const char 	*qdevice_mode_type_to_str(enum qdevice_model_type model_type);
+
 #ifdef __cplusplus
 }
 #endif

+ 3 - 0
qdevices/qdevice-votequorum.c

@@ -253,6 +253,9 @@ qdevice_votequorum_poll(struct qdevice_instance *instance, int cast_vote)
 {
 	cs_error_t res;
 
+	instance->vq_last_poll = time(NULL);
+	instance->vq_last_poll_cast_vote = cast_vote;
+
 	res = votequorum_qdevice_poll(instance->votequorum_handle,
 	    QDEVICE_VOTEQUORUM_DEVICE_NAME, cast_vote,
 	    instance->vq_node_list_ring_id);