[Cluster-devel] cluster/cman cman_tool/cman_tool.h cman_tool/m ...

rohara at sourceware.org rohara at sourceware.org
Wed Sep 19 15:59:47 UTC 2007


CVSROOT:	/cvs/cluster
Module name:	cluster
Changes by:	rohara at sourceware.org	2007-09-19 15:59:46

Modified files:
	cman/cman_tool : cman_tool.h main.c 
	cman/man       : cman_tool.8 

Log message:
	Add ability to format output and filter based on node name.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/cluster/cman/cman_tool/cman_tool.h.diff?cvsroot=cluster&r1=1.12&r2=1.13
http://sourceware.org/cgi-bin/cvsweb.cgi/cluster/cman/cman_tool/main.c.diff?cvsroot=cluster&r1=1.56&r2=1.57
http://sourceware.org/cgi-bin/cvsweb.cgi/cluster/cman/man/cman_tool.8.diff?cvsroot=cluster&r1=1.12&r2=1.13

--- cluster/cman/cman_tool/cman_tool.h	2006/05/10 14:20:06	1.12
+++ cluster/cman/cman_tool/cman_tool.h	2007/09/19 15:59:46	1.13
@@ -35,7 +35,6 @@
 #include <limits.h>
 #include <unistd.h>
 
-
 extern char *prog_name;
 
 #ifndef TRUE
@@ -50,13 +49,21 @@
 	exit(EXIT_FAILURE); \
 } while (0)
 
-
 #define DEFAULT_VOTES 1
 #define MAX_INTERFACES 10
+#define MAX_FORMAT_OPTS 10
 #define MAX_NODE_NAME_LEN 65
 #define MAX_MCAST_NAME_LEN 256
 #define MAX_PATH_LEN 256
 
+enum format_opt
+{
+	FMT_NONE,
+	FMT_ID,
+	FMT_NAME,
+	FMT_TYPE,
+	FMT_ADDR,
+};
 
 struct commandline
 {
@@ -67,6 +74,7 @@
         char *interfaces[MAX_INTERFACES];
 	char *override_nodename;
 	char *key_filename;
+	char *format_opts;
 	int votes;
 	int expected_votes;
 	int two_node;
--- cluster/cman/cman_tool/main.c	2007/08/28 13:14:06	1.56
+++ cluster/cman/cman_tool/main.c	2007/09/19 15:59:46	1.57
@@ -20,7 +20,7 @@
 #include "libcman.h"
 #include "cman_tool.h"
 
-#define OPTION_STRING		("m:n:v:e:2p:c:r:i:N:t:o:k:Vwfqah?d::")
+#define OPTION_STRING		("m:n:v:e:2p:c:r:i:N:t:o:k:F:Vwfqah?d::")
 #define OP_JOIN			1
 #define OP_LEAVE		2
 #define OP_EXPECTED		3
@@ -110,6 +110,8 @@
 		printf("nodes              Show local record of cluster nodes\n");
 		printf("  -f                 Also show when node was last fenced\n");
 		printf("  -a                 Also show node address(es)\n");
+		printf("  -n <nodename>      Only show information for specific node\n");
+		printf("  -F <format>        Specify output format (see man page)\n");
 		printf("\n");
 	}
 
@@ -199,7 +201,8 @@
 	char tmpbuf[1024];
 	cman_extra_info_t *einfo = (cman_extra_info_t *)info_buf;
 	int quorate;
-	int i,j;
+	int i;
+	int j;
 	int portnum;
 	char *addrptr;
 
@@ -300,13 +303,46 @@
 	return a->cn_nodeid - b->cn_nodeid;
 }
 
+static int node_filter(commandline_t *comline, const char *node)
+{
+	int i;
+
+	for (i = 0; i < comline->num_nodenames; i++) {
+		if (strcmp(comline->nodenames[i], node) == 0) {
+			return TRUE;
+		}
+	}
+
+	return FALSE;
+}
+
+static int get_format_opt(const char *opt)
+{
+	if (!opt)
+		return FMT_NONE;
+
+	if (!strcmp(opt, "id"))
+		return FMT_ID;
+	if (!strcmp(opt, "name"))
+		return FMT_NAME;
+	if (!strcmp(opt, "type"))
+		return FMT_TYPE;
+	if (!strcmp(opt, "addr"))
+		return FMT_ADDR;
+
+	return FMT_NONE;
+}
+
 static void show_nodes(commandline_t *comline)
 {
 	cman_handle_t h;
 	int count;
 	int i;
+	int j;
+	int k;
 	int numnodes;
 	int dis_count;
+	int format[MAX_FORMAT_OPTS];
 	cman_node_t *dis_nodes;
 	cman_node_t *nodes;
 	struct tm *jtime;
@@ -325,6 +361,16 @@
 	if (!nodes)
 		die("cannot allocate memory for nodes list\n");
 
+	if (comline->format_opts != NULL) {
+		char *format_str = comline->format_opts;
+		char *format_tmp;
+		for (i = 0; i < MAX_FORMAT_OPTS; i++) {
+			format_tmp = strtok(format_str, ",");
+			format_str = NULL;
+			format[i] = get_format_opt(format_tmp);
+		}
+	}
+
 	if (cman_get_nodes(h, count, &numnodes, nodes) < 0)
 		die("cman_get_nodes failed: %s", cman_error(errno));
 
@@ -333,9 +379,8 @@
 	dis_nodes = malloc(sizeof(cman_node_t) * count);
 
 	if (cman_get_disallowed_nodes(h, count, &dis_count, dis_nodes) == 0) {
-		int i,j;
-		for (i=0; i<numnodes; i++) {
-			for (j=0; j<dis_count; j++) {
+		for (i = 0; i < numnodes; i++) {
+			for (j = 0; j < dis_count; j++) {
 				if (dis_nodes[j].cn_nodeid == nodes[i].cn_nodeid)
 					nodes[i].cn_member = 2;
 			}
@@ -350,10 +395,19 @@
 		printf("      members list may seem inconsistent across the cluster\n");
 	}
 
-	printf("Node  Sts   Inc   Joined               Name\n");
-	for (i=0; i<numnodes; i++) {
+	if (!comline->format_opts) {
+		printf("Node  Sts   Inc   Joined               Name\n");
+	}
+
+	for (i = 0; i < numnodes; i++) {
 		char member_type;
 
+		if (comline->num_nodenames > 0) {
+			if (node_filter(comline, nodes[i].cn_name) == 0) {
+				continue;
+			}
+		}
+
 		switch (nodes[i].cn_member) {
 		case 0:
 			member_type = 'X';
@@ -375,11 +429,13 @@
 		else
 			strcpy(jstring, "                   ");
 
-		printf("%4d   %c  %5d   %s  %s\n",
-		       nodes[i].cn_nodeid, member_type,
-		       nodes[i].cn_incarnation, jstring, nodes[i].cn_name);
+		if (!comline->format_opts) {
+			printf("%4d   %c  %5d   %s  %s\n",
+			       nodes[i].cn_nodeid, member_type,
+			       nodes[i].cn_incarnation, jstring, nodes[i].cn_name);
+		}
 
-		if (comline->fence_opt) {
+		if (comline->fence_opt && !comline->format_opts) {
 			char agent[255];
 			uint64_t fence_time;
 			int fenced;
@@ -396,25 +452,57 @@
 				}
 			}
 		}
+		
+		int numaddrs;
+		struct cman_node_address addrs[MAX_INTERFACES];
 
-		if (comline->addresses_opt)
-		{
-			int numaddrs;
-			struct cman_node_address addrs[MAX_INTERFACES];
+		if (comline->addresses_opt || comline->format_opts) {
 			if (!cman_get_node_addrs(h, nodes[i].cn_nodeid, MAX_INTERFACES, &numaddrs, addrs) &&
 				numaddrs)
 			{
-				int i;
-				printf("       Addresses: ");
-				for (i=0; i<numaddrs; i++)
-				{
-					print_address(addrs[i].cna_address);
+				if (!comline->format_opts) {
+					printf("       Addresses: ");
+					for (i = 0; i < numaddrs; i++)
+					{
+						print_address(addrs[i].cna_address);
+						printf(" ");
+					}
+					printf("\n");
+				}
+			}
+		}
+
+		if (comline->format_opts) {
+			for (j = 0; j < MAX_FORMAT_OPTS; j++) {
+				switch (format[j]) {
+				case FMT_NONE:
+					break;
+				case FMT_ID:
+					printf("%d ", nodes[i].cn_nodeid);
+					break;
+				case FMT_NAME:
+					printf("%s ", nodes[i].cn_name);
+					break;
+				case FMT_TYPE:
+					printf("%c ", member_type);
+					break;
+				case FMT_ADDR:
+					for (k = 0; k < numaddrs; k++) {
+						print_address(addrs[k].cna_address);
+						if (k != (numaddrs - 1)) {
+							printf(",");
+						}
+					}
 					printf(" ");
+					break;
+				default:
+					break;
 				}
-				printf("\n");
 			}
+			printf("\n");
 		}
 	}
+
 	free(nodes);
 	free(dis_nodes);
 	cman_finish(h);
@@ -701,6 +789,10 @@
 			comline->clustername_opt = TRUE;
 			break;
 
+		case 'F':
+			comline->format_opts = strdup(optarg);
+			break;
+
 		case 'V':
 			printf("cman_tool %s (built %s %s)\n",
 				RELEASE_VERSION, __DATE__, __TIME__);
--- cluster/cman/man/cman_tool.8	2007/08/28 13:14:10	1.12
+++ cluster/cman/man/cman_tool.8	2007/09/19 15:59:46	1.13
@@ -242,7 +242,18 @@
 .TP
 .I -a
 Shows the IP address(es) the nodes are communicating on.
-
+.br
+.TP
+.I -n <nodename>
+Shows node information for a specific node. This should be the unqualified node
+name as it appears in 'cman_tool nodes'.
+.br
+.TP
+.I -F <format>
+Specify the format of the output. The format string may contain one or
+more format options, each seperated by a comma. Valid format options
+include: id, name, type, and addr.
+.br
 .SH "DEBUG" OPTIONS
 .TP
 .I -d <value>




More information about the Cluster-devel mailing list