[Cluster-devel] cluster/cman/cman_tool Makefile cman_tool.h main.c

rohara at sourceware.org rohara at sourceware.org
Fri Oct 12 18:10:13 UTC 2007


CVSROOT:	/cvs/cluster
Module name:	cluster
Branch: 	RHEL4
Changes by:	rohara at sourceware.org	2007-10-12 18:10:12

Modified files:
	cman/cman_tool : Makefile cman_tool.h main.c 

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/Makefile.diff?cvsroot=cluster&only_with_tag=RHEL4&r1=1.4.2.1&r2=1.4.2.2
http://sourceware.org/cgi-bin/cvsweb.cgi/cluster/cman/cman_tool/cman_tool.h.diff?cvsroot=cluster&only_with_tag=RHEL4&r1=1.3.2.5&r2=1.3.2.6
http://sourceware.org/cgi-bin/cvsweb.cgi/cluster/cman/cman_tool/main.c.diff?cvsroot=cluster&only_with_tag=RHEL4&r1=1.12.2.9&r2=1.12.2.10

--- cluster/cman/cman_tool/Makefile	2005/03/08 16:42:49	1.4.2.1
+++ cluster/cman/cman_tool/Makefile	2007/10/12 18:10:12	1.4.2.2
@@ -37,7 +37,7 @@
 	cp ${TARGET} ${top_srcdir}/bin
 
 cman_tool: main.o join.o join_ccs.o
-	$(CC) $(LDFLAGS) -L$(libdir) -o $@ $^ -lccs
+	$(CC) $(LDFLAGS) -L$(libdir) -o $@ $^ -lccs -lcman
 
 main.o: main.c cman_tool.h
 	$(CC) $(CFLAGS) -c -o $@ $<
--- cluster/cman/cman_tool/cman_tool.h	2006/12/18 13:37:55	1.3.2.5
+++ cluster/cman/cman_tool/cman_tool.h	2007/10/12 18:10:12	1.3.2.6
@@ -35,7 +35,6 @@
 #include <limits.h>
 #include <unistd.h>
 
-
 extern char *prog_name;
 
 #ifndef TRUE
@@ -50,14 +49,22 @@
 	exit(EXIT_FAILURE); \
 } while (0)
 
-
 #define DEFAULT_PORT 6809
 #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
 {
@@ -68,6 +75,7 @@
 	char *multicast_names[MAX_INTERFACES];
 	char *nodenames[MAX_INTERFACES];
         char *interfaces[MAX_INTERFACES];
+	char *format_opts;
 	int votes;
 	int expected_votes;
 	int two_node;
--- cluster/cman/cman_tool/main.c	2006/07/10 13:15:56	1.12.2.9
+++ cluster/cman/cman_tool/main.c	2007/10/12 18:10:12	1.12.2.10
@@ -14,11 +14,14 @@
 #include <inttypes.h>
 #include <unistd.h>
 #include <signal.h>
+#include <time.h>
+#include <netinet/in.h>
 #include "copyright.cf"
 #include "cnxman-socket.h"
+#include "libcman.h"
 #include "cman_tool.h"
 
-#define OPTION_STRING		("m:n:v:e:2p:c:r:i:N:t:XVwqh?d")
+#define OPTION_STRING		("m:n:v:e:2p:c:r:i:N:t:XF:Vwqh?d")
 #define OP_JOIN			1
 #define OP_LEAVE		2
 #define OP_EXPECTED		3
@@ -88,6 +91,8 @@
 	printf("status             Show local record of cluster status\n");
 	printf("\n");
 	printf("nodes              Show local record of cluster nodes\n");
+	printf("  -n <nodename>    Only show information for specific node\n");
+	printf("  -F <format>      Specify output format (see man page)\n");
 	printf("\n");
 	printf("services           Show local record of cluster services\n");
 
@@ -133,6 +138,128 @@
 	show_file("/proc/cluster/services");
 }
 
+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_opt(commandline_t *comline)
+{
+	cman_handle_t h;
+	int count;
+	int i;
+	int j;
+	int numnodes;
+	int format[MAX_FORMAT_OPTS];
+	cman_node_t *nodes;
+
+	h = cman_init(NULL);
+
+	count = cman_get_node_count(h);
+	if (count < 0)
+		die("cman_get_node_count failed: %s", cman_error(errno));
+
+	count += 2;
+
+	nodes = malloc(sizeof(cman_node_t) * count);
+	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));
+
+	if (!comline->format_opts) {
+		printf("Node  Sts   Inc   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';
+			break;
+		case 1:
+			member_type = 'M';
+			break;
+		default:
+			member_type = '?';
+			break;
+		}
+
+		if (!comline->format_opts) {
+			printf("%4d   %c  %5d   %s\n",
+			       nodes[i].cn_nodeid, member_type,
+			       nodes[i].cn_incarnation, nodes[i].cn_name);
+		} else {
+			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:
+					break;
+				default:
+					break;
+				}
+			}
+			printf("\n");
+		}
+	}
+
+	free(nodes);
+	cman_finish(h);
+}
+
 static int open_cluster_socket()
 {
 	int cluster_sock;
@@ -444,6 +571,10 @@
 			comline->no_ccs = TRUE;
 			break;
 
+		case 'F':
+			comline->format_opts = strdup(optarg);
+			break;
+
 		case 'V':
 			printf("cman_tool %s (built %s %s)\n",
 				CMAN_RELEASE_NAME, __DATE__, __TIME__);
@@ -678,7 +809,13 @@
 		break;
 
 	case OP_NODES:
-		show_nodes();
+		if ((comline.num_nodenames != 0) ||
+		    (comline.format_opts != NULL))
+		{
+			show_nodes_opt(&comline);
+		} else {
+			show_nodes();
+		}
 		break;
 
 	case OP_SERVICES:




More information about the Cluster-devel mailing list