[Ocfs2-tools-devel] [PATCH 13/25] o2cb: Add ops list-clusters, list-cluster, list-nodes and list-heartbeats

Sunil Mushran sunil.mushran at oracle.com
Wed Jun 23 11:44:23 PDT 2010


Adds listing operations to list the various entities in the o2cb config file.

Signed-off-by: Sunil Mushran <sunil.mushran at oracle.com>
---
 o2cb_ctl/Makefile   |    4 +-
 o2cb_ctl/o2cbtool.c |   27 +++++++
 o2cb_ctl/o2cbtool.h |    4 +
 o2cb_ctl/op_lists.c |  202 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 236 insertions(+), 1 deletions(-)
 create mode 100644 o2cb_ctl/op_lists.c

diff --git a/o2cb_ctl/Makefile b/o2cb_ctl/Makefile
index 743f813..7d31772 100644
--- a/o2cb_ctl/Makefile
+++ b/o2cb_ctl/Makefile
@@ -27,7 +27,8 @@ DEFINES = -DVERSION=\"$(VERSION)\"
 
 O2CB_CONFIG_CFILES = o2cb_config.c jconfig.c jiterator.c
 O2CB_CTL_CFILES = ${O2CB_CONFIG_CFILES} o2cb_ctl.c
-O2CB_CFILES = ${O2CB_CONFIG_CFILES} o2cbtool.c op_cluster.c op_node.c op_heartbeat.c
+O2CB_CFILES = ${O2CB_CONFIG_CFILES} o2cbtool.c op_cluster.c op_node.c op_heartbeat.c \
+		op_lists.c
 
 O2CB_CTL_OBJS = $(subst .c,.o,$(O2CB_CTL_CFILES))
 O2CB_OBJS = $(subst .c,.o,$(O2CB_CFILES))
@@ -50,6 +51,7 @@ o2cbtool_CPPFLAGS = $(GLIB_CFLAGS) -DG_DISABLE_DEPRECATED
 op_cluster_CPPFLAGS = $(GLIB_CFLAGS) -DG_DISABLE_DEPRECATED
 op_node_CPPFLAGS = $(GLIB_CFLAGS) -DG_DISABLE_DEPRECATED
 op_heartbeat_CPPFLAGS = $(GLIB_CFLAGS) -DG_DISABLE_DEPRECATED
+op_lists_CPPFLAGS = $(GLIB_CFLAGS) -DG_DISABLE_DEPRECATED
 
 o2cb_ctl: $(O2CB_CTL_OBJS) $(LIBOCFS2_DEPS) $(LIBO2CB_DEPS)
 	$(LINK) $(LIBO2CB_LIBS) $(GLIB_LIBS) $(LIBOCFS2_LIBS) $(COM_ERR_LIBS)
diff --git a/o2cb_ctl/o2cbtool.c b/o2cb_ctl/o2cbtool.c
index 5dff9a6..d6bb28c 100644
--- a/o2cb_ctl/o2cbtool.c
+++ b/o2cb_ctl/o2cbtool.c
@@ -69,6 +69,33 @@ struct o2cb_command o2cbtool_cmds[] = {
 		.o_help = "Toggles the heartbeat mode between global and local.",
 	},
 	{
+		.o_name = "list-clusters",
+		.o_action = o2cbtool_list_clusters,
+		.o_usage = "",
+		.o_help = "Lists all the cluster names in the config file.",
+	},
+	{
+		.o_name = "list-cluster",
+		.o_action = o2cbtool_list_objects,
+		.o_usage = "[--oneline] <clustername>",
+		.o_help = "Lists all the nodes and heartbeat regions "
+			"associated with the cluster in the config file.",
+	},
+	{
+		.o_name = "list-nodes",
+		.o_action = o2cbtool_list_objects,
+		.o_usage = "[--oneline] <clustername>",
+		.o_help = "Lists all the nodes associated with the cluster in "
+			"the config file. ",
+	},
+	{
+		.o_name = "list-heartbeats",
+		.o_action = o2cbtool_list_objects,
+		.o_usage = "[--oneline] <clustername>",
+		.o_help = "Lists all the heartbeat regions associated with "
+			"the cluster in the config file.",
+	},
+	{
 		.o_name = NULL,
 		.o_action = NULL,
 	},
diff --git a/o2cb_ctl/o2cbtool.h b/o2cb_ctl/o2cbtool.h
index bd5d12a..b3ca1ef 100644
--- a/o2cb_ctl/o2cbtool.h
+++ b/o2cb_ctl/o2cbtool.h
@@ -58,6 +58,7 @@ enum {
 	IP_OPTION,
 	PORT_OPTION,
 	NODENUM_OPTION,
+	ONELINE_OPTION,
 };
 
 errcode_t o2cbtool_add_cluster(struct o2cb_command *cmd);
@@ -69,3 +70,6 @@ errcode_t o2cbtool_remove_node(struct o2cb_command *cmd);
 errcode_t o2cbtool_add_heartbeat(struct o2cb_command *cmd);
 errcode_t o2cbtool_remove_heartbeat(struct o2cb_command *cmd);
 errcode_t o2cbtool_heartbeat_mode(struct o2cb_command *cmd);
+
+errcode_t o2cbtool_list_clusters(struct o2cb_command *cmd);
+errcode_t o2cbtool_list_objects(struct o2cb_command *cmd);
diff --git a/o2cb_ctl/op_lists.c b/o2cb_ctl/op_lists.c
new file mode 100644
index 0000000..e67aac4
--- /dev/null
+++ b/o2cb_ctl/op_lists.c
@@ -0,0 +1,202 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * op_lists.c
+ *
+ * Lists various entities in the o2cb cluster config
+ *
+ * Copyright (C) 2010 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License, version 2,  as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#include "o2cbtool.h"
+
+static int list_parse_options(int argc, char *argv[], int *oneline,
+			      char **clustername)
+{
+	int c, ret = -1, show_usage = 0;
+	static struct option long_options[] = {
+		{ "oneline", 0, 0, ONELINE_OPTION },
+		{ 0, 0, 0, 0 },
+	};
+
+	while (1) {
+		c = getopt_long(argc, argv, "", long_options, NULL);
+		if (c == -1)
+			break;
+		switch (c) {
+		case ONELINE_OPTION:
+			*oneline = 1;
+			break;
+		default:
+			++show_usage;
+			break;
+		}
+	}
+
+	if (optind + 1 > argc || show_usage)
+		goto bail;
+
+	*clustername = argv[optind];
+
+	ret = 0;
+bail:
+	return ret;
+}
+
+static void show_heartbeats(O2CBCluster *cluster, gchar *clustername,
+			    int oneline)
+{
+	O2CBHeartbeat *hb;
+	JIterator *iter;
+	gchar *region;
+	gchar *format;
+
+	if (oneline) {
+		verbosef(VL_OUT, "heartbeat:\n");
+		format = "  %s\n";
+	} else
+		format = "heartbeat:\n\tregion = %s\n\tcluster = %s\n\n";
+
+	iter = o2cb_cluster_get_heartbeat_regions(cluster);
+	while (j_iterator_has_more(iter)) {
+		hb = j_iterator_get_next(iter);
+		region = o2cb_heartbeat_get_region(hb);
+		if (oneline)
+			verbosef(VL_OUT, format, region);
+		else
+			verbosef(VL_OUT, format, region, clustername);
+		g_free(region);
+	}
+	j_iterator_free(iter);
+}
+
+static void show_nodes(O2CBCluster *cluster, gchar *clustername,
+		       int oneline)
+{
+	O2CBNode *node;
+	JIterator *iter;
+	gchar *nodename, *ip;
+	gint port, nodenum;
+	gchar *format;
+
+	if (oneline) {
+		verbosef(VL_OUT, "node:\n");
+		format = "  %3d, %s, %s:%d\n";
+	} else
+		format = "node:\n\tip_port = %d\n\tip_address = %s\n\t"
+			"number = %d\n\tname = %s\n\tcluster = %s\n\n";
+
+	iter = o2cb_cluster_get_nodes(cluster);
+	while (j_iterator_has_more(iter)) {
+		node = j_iterator_get_next(iter);
+		nodename = o2cb_node_get_name(node);
+		ip = o2cb_node_get_ip_string(node);
+		nodenum = o2cb_node_get_number(node);
+		port = o2cb_node_get_port(node);
+		if (oneline)
+			verbosef(VL_OUT, format, nodenum, nodename, ip, port);
+		else
+			verbosef(VL_OUT, format, port, ip, nodenum, nodename,
+				 clustername);
+		g_free(nodename);
+		g_free(ip);
+	}
+	j_iterator_free(iter);
+}
+
+static void show_cluster(O2CBCluster *cluster, gchar *clustername,
+			 int oneline)
+{
+	guint nodecount;
+	gchar *hbmode;
+
+	nodecount = o2cb_cluster_get_node_count(cluster);
+	hbmode = o2cb_cluster_get_heartbeat_mode(cluster);
+	if (!hbmode)
+		hbmode = strdup("local");
+
+	verbosef(VL_OUT, "cluster:\n");
+	if (oneline)
+		verbosef(VL_OUT, "  %3d, %s, %s\n", nodecount, clustername,
+			 hbmode);
+	else
+		verbosef(VL_OUT, "\tnode_count = %d\n\t"
+			 "heartbeat_mode = %s\n\tname = %s\n\n",
+			 nodecount, hbmode, clustername);
+
+	g_free(hbmode);
+}
+
+/*
+ * list-cluster [--oneline] <clustername>
+ * list-nodes [--oneline] <clustername>
+ * list-heartbeats [--oneline] <clustername>
+ */
+errcode_t o2cbtool_list_objects(struct o2cb_command *cmd)
+{
+	O2CBCluster *cluster;
+	gchar *clustername;
+	int ret = -1;
+	int oneline = 0;
+
+	ret = list_parse_options(cmd->o_argc, cmd->o_argv, &oneline,
+				 &clustername);
+	if (ret) {
+		errorf("usage: %s %s\n", cmd->o_name, cmd->o_usage);
+		goto bail;
+	}
+
+	cluster = o2cb_config_get_cluster_by_name(cmd->o_config, clustername);
+	if (!cluster) {
+		errorf("Unknown cluster '%s'\n", clustername);
+		goto bail;
+	}
+
+	if (!strcmp(cmd->o_argv[0], "list-heartbeats"))
+		show_heartbeats(cluster, clustername, oneline);
+	else if (!strcmp(cmd->o_argv[0], "list-nodes"))
+		show_nodes(cluster, clustername, oneline);
+	else {
+		show_heartbeats(cluster, clustername, oneline);
+		show_nodes(cluster, clustername, oneline);
+		show_cluster(cluster, clustername, oneline);
+	}
+
+	ret = 0;
+
+bail:
+	return ret;
+}
+
+/*
+ * list-clusters
+ */
+errcode_t o2cbtool_list_clusters(struct o2cb_command *cmd)
+{
+	O2CBCluster *cluster;
+	gchar *clustername;
+	JIterator *iter;
+
+	iter = o2cb_config_get_clusters(cmd->o_config);
+	if (!iter)
+		return -1;
+
+	while (j_iterator_has_more(iter)) {
+		cluster = j_iterator_get_next(iter);
+		clustername = o2cb_cluster_get_name(cluster);
+		printf("%s\n", clustername);
+		g_free(clustername);
+	}
+	j_iterator_free(iter);
+
+	return 0;
+}
-- 
1.7.0.4




More information about the Ocfs2-tools-devel mailing list