[Ocfs2-tools-devel] [PATCH 1/2] o2cb: Add op cluster-status

Sunil Mushran sunil.mushran at oracle.com
Mon Apr 4 16:28:31 PDT 2011


o2cb cluster-status [<clustername>]

Subcommand cluster-status returns 0 if the clustername is online or
1 otherwise.

Signed-off-by: Sunil Mushran <sunil.mushran at oracle.com>
---
 o2cb_ctl/Makefile    |    4 +-
 o2cb_ctl/o2cbtool.c  |    8 +++-
 o2cb_ctl/o2cbtool.h  |    8 +++-
 o2cb_ctl/o2cbutils.c |   78 ++++++++++++++++++++++++++++++++
 o2cb_ctl/op_start.c  |   31 +------------
 o2cb_ctl/op_status.c |  120 ++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 216 insertions(+), 33 deletions(-)
 create mode 100644 o2cb_ctl/o2cbutils.c
 create mode 100644 o2cb_ctl/op_status.c

diff --git a/o2cb_ctl/Makefile b/o2cb_ctl/Makefile
index 34c307f..47a8b10 100644
--- a/o2cb_ctl/Makefile
+++ b/o2cb_ctl/Makefile
@@ -28,7 +28,7 @@ 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 \
-		op_lists.c op_register.c op_start.c o2cb_scandisk.c
+		op_lists.c op_register.c op_start.c o2cb_scandisk.c op_status.c o2cbutils.c
 
 O2CB_CTL_OBJS = $(subst .c,.o,$(O2CB_CTL_CFILES))
 O2CB_OBJS = $(subst .c,.o,$(O2CB_CFILES))
@@ -55,6 +55,8 @@ op_lists_CPPFLAGS = $(GLIB_CFLAGS) -DG_DISABLE_DEPRECATED
 op_register_CPPFLAGS = $(GLIB_CFLAGS) -DG_DISABLE_DEPRECATED
 op_start_CPPFLAGS = $(GLIB_CFLAGS) -DG_DISABLE_DEPRECATED
 o2cb_scandisk_CPPFLAGS = $(GLIB_CFLAGS) -DG_DISABLE_DEPRECATED
+op_status_CPPFLAGS = $(GLIB_CFLAGS) -DG_DISABLE_DEPRECATED
+o2cbutils_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 9b4a6c3..8307c77 100644
--- a/o2cb_ctl/o2cbtool.c
+++ b/o2cb_ctl/o2cbtool.c
@@ -5,7 +5,7 @@
  *
  * Manipulates o2cb cluster configuration
  *
- * Copyright (C) 2010 Oracle.  All rights reserved.
+ * Copyright (C) 2010, 2011 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
@@ -120,6 +120,12 @@ struct o2cb_command o2cbtool_cmds[] = {
 		.o_help = "Stops global heartbeat."
 	},
 	{
+		.o_name = "cluster-status",
+		.o_action = o2cbtool_cluster_status,
+		.o_usage = "[<clustername>]",
+		.o_help = "Returns 0 if cluster online, 1 otherwise.",
+	},
+	{
 		.o_name = NULL,
 		.o_action = NULL,
 	},
diff --git a/o2cb_ctl/o2cbtool.h b/o2cb_ctl/o2cbtool.h
index 4788fae..9aa4c50 100644
--- a/o2cb_ctl/o2cbtool.h
+++ b/o2cb_ctl/o2cbtool.h
@@ -5,7 +5,7 @@
  *
  * Manipulates o2cb cluster configuration
  *
- * Copyright (C) 2010 Oracle.  All rights reserved.
+ * Copyright (C) 2010, 2011 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
@@ -86,3 +86,9 @@ errcode_t o2cbtool_unregister_cluster(struct o2cb_command *cmd);
 
 errcode_t o2cbtool_start_heartbeat(struct o2cb_command *cmd);
 errcode_t o2cbtool_stop_heartbeat(struct o2cb_command *cmd);
+
+errcode_t o2cbtool_cluster_status(struct o2cb_command *cmd);
+
+/* utility functions */
+int is_cluster_registered(char *clustername);
+int is_heartbeat_active(char *clustername);
diff --git a/o2cb_ctl/o2cbutils.c b/o2cb_ctl/o2cbutils.c
new file mode 100644
index 0000000..0de7a1e
--- /dev/null
+++ b/o2cb_ctl/o2cbutils.c
@@ -0,0 +1,78 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * o2cbutils.c
+ *
+ * utility functions
+ *
+ * Copyright (C) 2011 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"
+
+/*
+ * Only after the local node is added to the cluster is the cluster
+ * considered registered
+ *
+ * Returns 1 if registered, 0 otherwise.
+ */
+int is_cluster_registered(char *clustername)
+{
+	char **nodename = NULL;
+	uint32_t local;
+	int ret = 0, i = 0;
+
+	if (o2cb_list_nodes(clustername, &nodename))
+		goto bail;
+
+	while(nodename && nodename[i] && *(nodename[i])) {
+		if (o2cb_get_node_local(clustername, nodename[i], &local))
+			break;
+		if (local) {
+			ret = 1;
+			break;
+		}
+		++i;
+	}
+
+	if (nodename)
+		o2cb_free_nodes_list(nodename);
+
+bail:
+	return ret;
+}
+
+/*
+ * Returns 1 if atleast one heartbeat region is active.
+ * 0 otherwise.
+ */
+int is_heartbeat_active(char *clustername)
+{
+	gchar **regions = NULL;
+	errcode_t err;
+	int active = 0;
+
+	/* lookup active heartbeats */
+	err = o2cb_list_hb_regions(clustername, &regions);
+	if (err)
+		goto bail;
+
+	/* if found, heartbeat is active */
+	if (regions[0] && *(regions[0]))
+		active = 1;
+
+	if (regions)
+		o2cb_free_hb_regions_list(regions);
+
+bail:
+	return active;
+}
diff --git a/o2cb_ctl/op_start.c b/o2cb_ctl/op_start.c
index db4bc9c..95c255e 100644
--- a/o2cb_ctl/op_start.c
+++ b/o2cb_ctl/op_start.c
@@ -5,7 +5,7 @@
  *
  * Starts and stops the global heartbeat
  *
- * Copyright (C) 2010 Oracle.  All rights reserved.
+ * Copyright (C) 2010, 2011 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
@@ -24,35 +24,6 @@ extern const char *stackname;
 
 static errcode_t stop_global_heartbeat(O2CBCluster *cluster, char *clustername,
 				       int only_missing);
-/*
- * Only after the local node is added to the cluster is the cluster
- * considered registered
- */
-static int is_cluster_registered(char *clustername)
-{
-	char **nodename = NULL;
-	uint32_t local;
-	int ret = 0, i = 0;
-
-	if (o2cb_list_nodes(clustername, &nodename))
-		goto bail;
-
-	while(nodename && nodename[i] && *(nodename[i])) {
-		if (o2cb_get_node_local(clustername, nodename[i], &local))
-			break;
-		if (local) {
-			ret = 1;
-			break;
-		}
-		++i;
-	}
-
-	if (nodename)
-		o2cb_free_nodes_list(nodename);
-
-bail:
-	return ret;
-}
 
 static void stop_heartbeat(struct o2cb_device *od)
 {
diff --git a/o2cb_ctl/op_status.c b/o2cb_ctl/op_status.c
new file mode 100644
index 0000000..8bbdd38
--- /dev/null
+++ b/o2cb_ctl/op_status.c
@@ -0,0 +1,120 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * op_status.c
+ *
+ * Returns status of the cluster
+ *
+ * Copyright (C) 2011 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"
+
+extern const char *stackname;
+
+static errcode_t get_active_clustername(char *name, int namelen)
+{
+	gchar **clusternames = NULL;
+	errcode_t ret;
+
+	/* Lookup the registered cluster */
+	ret = o2cb_list_clusters(&clusternames);
+	if (ret) {
+		tcom_err(ret, "while looking up the registered cluster");
+		goto bail;
+	}
+
+	if (!*clusternames) {
+		ret = O2CB_ET_SERVICE_UNAVAILABLE;
+		goto bail;
+	}
+
+	strncpy(name, clusternames[0], namelen);
+	name[namelen] = '\0';
+
+bail:
+	if (clusternames)
+		o2cb_free_cluster_list(clusternames);
+	return ret;
+}
+
+/*
+ * cluster-status [<clustername>]
+ *
+ * Return 0 if online and 1 otherwise.
+ */
+errcode_t o2cbtool_cluster_status(struct o2cb_command *cmd)
+{
+	O2CBCluster *cluster;
+	errcode_t err;
+	char clustername[OCFS2_CLUSTER_NAME_LEN + 1];
+	int global = 0, status = 1;
+
+	*clustername = '\0';
+	cmd->o_print_usage = 0;
+
+	err = o2cbtool_init_cluster_stack();
+	if (err)
+		goto bail;
+
+	/* Get active clustername */
+	err = get_active_clustername(clustername, sizeof(clustername));
+	if (err)
+		goto bail;
+
+	/* If none, found exit */
+	if (strlen(clustername) == 0)
+		goto bail;
+	verbosef(VL_DEBUG, "Active cluster '%s'\n", clustername);
+
+	/* If found but name != arg, then exit */
+	if (cmd->o_argc > 1 &&
+	    strncmp(clustername, cmd->o_argv[1], OCFS2_CLUSTER_NAME_LEN)) {
+		strncpy(clustername, cmd->o_argv[1], OCFS2_CLUSTER_NAME_LEN);
+		clustername[OCFS2_CLUSTER_NAME_LEN] = '\0';
+		goto bail;
+	}
+
+	/* Check if cluster is registered */
+	if (!is_cluster_registered(clustername))
+		goto bail;
+	verbosef(VL_DEBUG, "Cluster '%s' is registered\n", clustername);
+
+	/* Get heartbeat mode */
+	err = o2cb_global_heartbeat_mode(clustername, &global);
+	if (err)
+		goto bail;
+
+	/* If local heartbeat, then cluster is online */
+	if (!global) {
+		status = 0;
+		goto bail;
+	}
+	verbosef(VL_DEBUG, "Global heartbeat is enabled\n");
+
+	/*
+	 * In global heartbeat mode, atleast one region should be active
+	 * for the cluster to be called online
+	 */ 
+	if (!is_heartbeat_active(clustername))
+		goto bail;
+
+	status = 0;
+bail:
+	if (strlen(clustername))
+		verbosef(VL_OUT, "Cluster '%s' is %s\n", clustername,
+			 (!status ? "online" : "offline"));
+	else
+		verbosef(VL_OUT, "%s\n", (!status ? "online" : "offline"));
+
+	return status;
+}
-- 
1.7.1




More information about the Ocfs2-tools-devel mailing list