to make it easier/less disruptive to add nodes to a running cluster
[samba.git] / ctdb / tools / ctdb.c
1 /* 
2    ctdb control tool
3
4    Copyright (C) Andrew Tridgell  2007
5    Copyright (C) Ronnie Sahlberg  2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "system/network.h"
26 #include "system/locale.h"
27 #include "popt.h"
28 #include "cmdline.h"
29 #include "../include/ctdb.h"
30 #include "../include/ctdb_private.h"
31
32 static void usage(void);
33
34 static struct {
35         int timelimit;
36         uint32_t pnn;
37         int machinereadable;
38         int maxruntime;
39 } options;
40
41 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
42
43 /*
44   see if a process exists
45  */
46 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
47 {
48         uint32_t pnn, pid;
49         int ret;
50         if (argc < 1) {
51                 usage();
52         }
53
54         if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
55                 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
56                 return -1;
57         }
58
59         ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
60         if (ret == 0) {
61                 printf("%u:%u exists\n", pnn, pid);
62         } else {
63                 printf("%u:%u does not exist\n", pnn, pid);
64         }
65         return ret;
66 }
67
68 /*
69   display statistics structure
70  */
71 static void show_statistics(struct ctdb_statistics *s)
72 {
73         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
74         int i;
75         const char *prefix=NULL;
76         int preflen=0;
77         const struct {
78                 const char *name;
79                 uint32_t offset;
80         } fields[] = {
81 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
82                 STATISTICS_FIELD(num_clients),
83                 STATISTICS_FIELD(frozen),
84                 STATISTICS_FIELD(recovering),
85                 STATISTICS_FIELD(client_packets_sent),
86                 STATISTICS_FIELD(client_packets_recv),
87                 STATISTICS_FIELD(node_packets_sent),
88                 STATISTICS_FIELD(node_packets_recv),
89                 STATISTICS_FIELD(keepalive_packets_sent),
90                 STATISTICS_FIELD(keepalive_packets_recv),
91                 STATISTICS_FIELD(node.req_call),
92                 STATISTICS_FIELD(node.reply_call),
93                 STATISTICS_FIELD(node.req_dmaster),
94                 STATISTICS_FIELD(node.reply_dmaster),
95                 STATISTICS_FIELD(node.reply_error),
96                 STATISTICS_FIELD(node.req_message),
97                 STATISTICS_FIELD(node.req_control),
98                 STATISTICS_FIELD(node.reply_control),
99                 STATISTICS_FIELD(client.req_call),
100                 STATISTICS_FIELD(client.req_message),
101                 STATISTICS_FIELD(client.req_control),
102                 STATISTICS_FIELD(timeouts.call),
103                 STATISTICS_FIELD(timeouts.control),
104                 STATISTICS_FIELD(timeouts.traverse),
105                 STATISTICS_FIELD(total_calls),
106                 STATISTICS_FIELD(pending_calls),
107                 STATISTICS_FIELD(lockwait_calls),
108                 STATISTICS_FIELD(pending_lockwait_calls),
109                 STATISTICS_FIELD(memory_used),
110                 STATISTICS_FIELD(max_hop_count),
111         };
112         printf("CTDB version %u\n", CTDB_VERSION);
113         for (i=0;i<ARRAY_SIZE(fields);i++) {
114                 if (strchr(fields[i].name, '.')) {
115                         preflen = strcspn(fields[i].name, ".")+1;
116                         if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
117                                 prefix = fields[i].name;
118                                 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
119                         }
120                 } else {
121                         preflen = 0;
122                 }
123                 printf(" %*s%-22s%*s%10u\n", 
124                        preflen?4:0, "",
125                        fields[i].name+preflen, 
126                        preflen?0:4, "",
127                        *(uint32_t *)(fields[i].offset+(uint8_t *)s));
128         }
129         printf(" %-30s     %.6f sec\n", "max_call_latency", s->max_call_latency);
130         printf(" %-30s     %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
131         talloc_free(tmp_ctx);
132 }
133
134 /*
135   display remote ctdb statistics combined from all nodes
136  */
137 static int control_statistics_all(struct ctdb_context *ctdb)
138 {
139         int ret, i;
140         struct ctdb_statistics statistics;
141         uint32_t *nodes;
142         uint32_t num_nodes;
143
144         nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
145         CTDB_NO_MEMORY(ctdb, nodes);
146         
147         ZERO_STRUCT(statistics);
148
149         for (i=0;i<num_nodes;i++) {
150                 struct ctdb_statistics s1;
151                 int j;
152                 uint32_t *v1 = (uint32_t *)&s1;
153                 uint32_t *v2 = (uint32_t *)&statistics;
154                 uint32_t num_ints = 
155                         offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
156                 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
157                 if (ret != 0) {
158                         DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
159                         return ret;
160                 }
161                 for (j=0;j<num_ints;j++) {
162                         v2[j] += v1[j];
163                 }
164                 statistics.max_hop_count = 
165                         MAX(statistics.max_hop_count, s1.max_hop_count);
166                 statistics.max_call_latency = 
167                         MAX(statistics.max_call_latency, s1.max_call_latency);
168                 statistics.max_lockwait_latency = 
169                         MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
170         }
171         talloc_free(nodes);
172         printf("Gathered statistics for %u nodes\n", num_nodes);
173         show_statistics(&statistics);
174         return 0;
175 }
176
177 /*
178   display remote ctdb statistics
179  */
180 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
181 {
182         int ret;
183         struct ctdb_statistics statistics;
184
185         if (options.pnn == CTDB_BROADCAST_ALL) {
186                 return control_statistics_all(ctdb);
187         }
188
189         ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
190         if (ret != 0) {
191                 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
192                 return ret;
193         }
194         show_statistics(&statistics);
195         return 0;
196 }
197
198
199 /*
200   reset remote ctdb statistics
201  */
202 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
203 {
204         int ret;
205
206         ret = ctdb_statistics_reset(ctdb, options.pnn);
207         if (ret != 0) {
208                 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
209                 return ret;
210         }
211         return 0;
212 }
213
214
215 /*
216   display uptime of remote node
217  */
218 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
219 {
220         int ret;
221         int mypnn;
222         struct ctdb_uptime *uptime = NULL;
223         int tmp, days, hours, minutes, seconds;
224
225         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
226         if (mypnn == -1) {
227                 return -1;
228         }
229
230         ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
231         if (ret != 0) {
232                 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
233                 return ret;
234         }
235
236         printf("Current time of node  : %s", ctime(&uptime->current_time.tv_sec));
237
238         tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
239         seconds = tmp%60;
240         tmp    /= 60;
241         minutes = tmp%60;
242         tmp    /= 60;
243         hours   = tmp%24;
244         tmp    /= 24;
245         days    = tmp;
246         printf("Ctdbd start time      : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
247
248         tmp = uptime->current_time.tv_sec - uptime->last_recovery_time.tv_sec;
249         seconds = tmp%60;
250         tmp    /= 60;
251         minutes = tmp%60;
252         tmp    /= 60;
253         hours   = tmp%24;
254         tmp    /= 24;
255         days    = tmp;
256         printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_time.tv_sec));
257
258         return 0;
259 }
260
261 /*
262   display remote ctdb status
263  */
264 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
265 {
266         int i, ret;
267         struct ctdb_vnn_map *vnnmap=NULL;
268         struct ctdb_node_map *nodemap=NULL;
269         uint32_t recmode, recmaster;
270         int mypnn;
271
272         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
273         if (mypnn == -1) {
274                 return -1;
275         }
276
277         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
278         if (ret != 0) {
279                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
280                 return ret;
281         }
282
283         if(options.machinereadable){
284                 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
285                 for(i=0;i<nodemap->num;i++){
286                         printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
287                                 inet_ntoa(nodemap->nodes[i].sin.sin_addr),
288                                !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
289                                !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
290                                !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
291                                !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
292                 }
293                 return 0;
294         }
295
296         printf("Number of nodes:%d\n", nodemap->num);
297         for(i=0;i<nodemap->num;i++){
298                 static const struct {
299                         uint32_t flag;
300                         const char *name;
301                 } flag_names[] = {
302                         { NODE_FLAGS_DISCONNECTED,          "DISCONNECTED" },
303                         { NODE_FLAGS_PERMANENTLY_DISABLED,  "DISABLED" },
304                         { NODE_FLAGS_BANNED,                "BANNED" },
305                         { NODE_FLAGS_UNHEALTHY,             "UNHEALTHY" },
306                 };
307                 char *flags_str = NULL;
308                 int j;
309                 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
310                         if (nodemap->nodes[i].flags & flag_names[j].flag) {
311                                 if (flags_str == NULL) {
312                                         flags_str = talloc_strdup(ctdb, flag_names[j].name);
313                                 } else {
314                                         flags_str = talloc_asprintf_append(flags_str, "|%s",
315                                                                            flag_names[j].name);
316                                 }
317                                 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
318                         }
319                 }
320                 if (flags_str == NULL) {
321                         flags_str = talloc_strdup(ctdb, "OK");
322                         CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
323                 }
324                 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
325                        inet_ntoa(nodemap->nodes[i].sin.sin_addr),
326                        flags_str,
327                        nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
328                 talloc_free(flags_str);
329         }
330
331         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
332         if (ret != 0) {
333                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
334                 return ret;
335         }
336         if (vnnmap->generation == INVALID_GENERATION) {
337                 printf("Generation:INVALID\n");
338         } else {
339                 printf("Generation:%d\n",vnnmap->generation);
340         }
341         printf("Size:%d\n",vnnmap->size);
342         for(i=0;i<vnnmap->size;i++){
343                 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
344         }
345
346         ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
347         if (ret != 0) {
348                 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
349                 return ret;
350         }
351         printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
352
353         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
354         if (ret != 0) {
355                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
356                 return ret;
357         }
358         printf("Recovery master:%d\n",recmaster);
359
360         return 0;
361 }
362
363 /*
364   get a list of all tickles for this pnn
365  */
366 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
367 {
368         struct ctdb_control_tcp_tickle_list *list;
369         struct sockaddr_in ip;
370         int i, ret;
371
372         if (argc < 1) {
373                 usage();
374         }
375
376         ip.sin_family = AF_INET;
377         if (inet_aton(argv[0], &ip.sin_addr) == 0) {
378                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
379                 return -1;
380         }
381
382         ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &ip, &list);
383         if (ret == -1) {
384                 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
385                 return -1;
386         }
387
388         printf("Tickles for ip:%s\n", inet_ntoa(list->ip.sin_addr));
389         printf("Num tickles:%u\n", list->tickles.num);
390         for (i=0;i<list->tickles.num;i++) {
391                 printf("SRC: %s:%u   ", inet_ntoa(list->tickles.connections[i].saddr.sin_addr), ntohs(list->tickles.connections[i].saddr.sin_port));
392                 printf("DST: %s:%u\n", inet_ntoa(list->tickles.connections[i].daddr.sin_addr), ntohs(list->tickles.connections[i].daddr.sin_port));
393         }
394
395         talloc_free(list);
396         
397         return 0;
398 }
399
400 /*
401   kill a tcp connection
402  */
403 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
404 {
405         int ret;
406         struct ctdb_control_killtcp killtcp;
407
408         if (argc < 2) {
409                 usage();
410         }
411
412         if (!parse_ip_port(argv[0], &killtcp.src)) {
413                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
414                 return -1;
415         }
416
417         if (!parse_ip_port(argv[1], &killtcp.dst)) {
418                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
419                 return -1;
420         }
421
422         ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
423         if (ret != 0) {
424                 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
425                 return ret;
426         }
427
428         return 0;
429 }
430
431
432 /*
433   send a gratious arp
434  */
435 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
436 {
437         int ret;
438         struct sockaddr_in sin;
439
440         if (argc < 2) {
441                 usage();
442         }
443
444         sin.sin_family = AF_INET;
445         if (inet_aton(argv[0], &sin.sin_addr) == 0) {
446                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
447                 return -1;
448         }
449
450         ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &sin, argv[1]);
451         if (ret != 0) {
452                 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
453                 return ret;
454         }
455
456         return 0;
457 }
458
459 /*
460   register a server id
461  */
462 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
463 {
464         int ret;
465         struct ctdb_server_id server_id;
466
467         if (argc < 3) {
468                 usage();
469         }
470
471         server_id.pnn       = strtoul(argv[0], NULL, 0);
472         server_id.type      = strtoul(argv[1], NULL, 0);
473         server_id.server_id = strtoul(argv[2], NULL, 0);
474
475         ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
476         if (ret != 0) {
477                 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
478                 return ret;
479         }
480         return -1;
481 }
482
483 /*
484   unregister a server id
485  */
486 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
487 {
488         int ret;
489         struct ctdb_server_id server_id;
490
491         if (argc < 3) {
492                 usage();
493         }
494
495         server_id.pnn       = strtoul(argv[0], NULL, 0);
496         server_id.type      = strtoul(argv[1], NULL, 0);
497         server_id.server_id = strtoul(argv[2], NULL, 0);
498
499         ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
500         if (ret != 0) {
501                 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
502                 return ret;
503         }
504         return -1;
505 }
506
507 /*
508   check if a server id exists
509  */
510 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
511 {
512         uint32_t status;
513         int ret;
514         struct ctdb_server_id server_id;
515
516         if (argc < 3) {
517                 usage();
518         }
519
520         server_id.pnn       = strtoul(argv[0], NULL, 0);
521         server_id.type      = strtoul(argv[1], NULL, 0);
522         server_id.server_id = strtoul(argv[2], NULL, 0);
523
524         ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
525         if (ret != 0) {
526                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
527                 return ret;
528         }
529
530         if (status) {
531                 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
532         } else {
533                 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
534         }
535         return 0;
536 }
537
538 /*
539   get a list of all server ids that are registered on a node
540  */
541 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
542 {
543         int i, ret;
544         struct ctdb_server_id_list *server_ids;
545
546         ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
547         if (ret != 0) {
548                 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
549                 return ret;
550         }
551
552         for (i=0; i<server_ids->num; i++) {
553                 printf("Server id %d:%d:%d\n", 
554                         server_ids->server_ids[i].pnn, 
555                         server_ids->server_ids[i].type, 
556                         server_ids->server_ids[i].server_id); 
557         }
558
559         return -1;
560 }
561
562 /*
563   send a tcp tickle ack
564  */
565 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
566 {
567         int s, ret;
568         struct sockaddr_in src, dst;
569
570         if (argc < 2) {
571                 usage();
572         }
573
574         if (!parse_ip_port(argv[0], &src)) {
575                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
576                 return -1;
577         }
578
579         if (!parse_ip_port(argv[1], &dst)) {
580                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
581                 return -1;
582         }
583
584         s = ctdb_sys_open_sending_socket();
585         if (s == -1) {
586                 DEBUG(DEBUG_ERR, ("Failed to open socket for sending tickle\n"));
587                 return 0;
588         }
589
590         ret = ctdb_sys_send_tcp(s, &src, &dst, 0, 0, 0);
591         close(s);
592         if (ret==0) {
593                 return 0;
594         }
595         DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
596
597         return -1;
598 }
599
600
601 /*
602   display public ip status
603  */
604 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
605 {
606         int i, ret;
607         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
608         struct ctdb_all_public_ips *ips;
609
610         /* read the public ip list from this node */
611         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
612         if (ret != 0) {
613                 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
614                 talloc_free(tmp_ctx);
615                 return ret;
616         }
617
618         if (options.machinereadable){
619                 printf(":Public IP:Node:\n");
620         } else {
621                 printf("Public IPs on node %u\n", options.pnn);
622         }
623
624         for (i=1;i<=ips->num;i++) {
625                 printf("%s %d\n", inet_ntoa(ips->ips[ips->num-i].sin.sin_addr), ips->ips[ips->num-i].pnn);
626         }
627
628         talloc_free(tmp_ctx);
629         return 0;
630 }
631
632 /*
633   display pid of a ctdb daemon
634  */
635 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
636 {
637         uint32_t pid;
638         int ret;
639
640         ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
641         if (ret != 0) {
642                 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
643                 return ret;
644         }
645         printf("Pid:%d\n", pid);
646
647         return 0;
648 }
649
650 /*
651   disable a remote node
652  */
653 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
654 {
655         int ret;
656
657         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
658         if (ret != 0) {
659                 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
660                 return ret;
661         }
662
663         return 0;
664 }
665
666 /*
667   enable a disabled remote node
668  */
669 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
670 {
671         int ret;
672
673         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
674         if (ret != 0) {
675                 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
676                 return ret;
677         }
678
679         return 0;
680 }
681
682 /*
683   ban a node from the cluster
684  */
685 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
686 {
687         int ret;
688         struct ctdb_ban_info b;
689         TDB_DATA data;
690         uint32_t ban_time;
691
692         if (argc < 1) {
693                 usage();
694         }
695
696         ban_time = strtoul(argv[0], NULL, 0);
697
698         b.pnn = options.pnn;
699         b.ban_time = ban_time;
700
701         data.dptr = (uint8_t *)&b;
702         data.dsize = sizeof(b);
703
704         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
705         if (ret != 0) {
706                 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
707                 return -1;
708         }
709         
710         return 0;
711 }
712
713
714 /*
715   unban a node from the cluster
716  */
717 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
718 {
719         int ret;
720         TDB_DATA data;
721
722         data.dptr = (uint8_t *)&options.pnn;
723         data.dsize = sizeof(uint32_t);
724
725         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
726         if (ret != 0) {
727                 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
728                 return -1;
729         }
730         
731         return 0;
732 }
733
734
735 /*
736   shutdown a daemon
737  */
738 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
739 {
740         int ret;
741
742         ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
743         if (ret != 0) {
744                 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
745                 return ret;
746         }
747
748         return 0;
749 }
750
751 /*
752   trigger a recovery
753  */
754 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
755 {
756         int ret;
757
758         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
759         if (ret != 0) {
760                 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
761                 return ret;
762         }
763
764         ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
765         if (ret != 0) {
766                 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
767                 return ret;
768         }
769
770         return 0;
771 }
772
773
774 /*
775   display monitoring mode of a remote node
776  */
777 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
778 {
779         uint32_t monmode;
780         int ret;
781
782         ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
783         if (ret != 0) {
784                 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
785                 return ret;
786         }
787         printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
788
789         return 0;
790 }
791
792 /*
793   display remote list of keys/data for a db
794  */
795 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
796 {
797         const char *db_name;
798         struct ctdb_db_context *ctdb_db;
799         int ret;
800
801         if (argc < 1) {
802                 usage();
803         }
804
805         db_name = argv[0];
806         ctdb_db = ctdb_attach(ctdb, db_name, false);
807
808         if (ctdb_db == NULL) {
809                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
810                 return -1;
811         }
812
813         /* traverse and dump the cluster tdb */
814         ret = ctdb_dump_db(ctdb_db, stdout);
815         if (ret == -1) {
816                 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
817                 return -1;
818         }
819         talloc_free(ctdb_db);
820
821         printf("Dumped %d records\n", ret);
822         return 0;
823 }
824
825
826 /*
827   display a list of the databases on a remote ctdb
828  */
829 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
830 {
831         int i, ret;
832         struct ctdb_dbid_map *dbmap=NULL;
833
834         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
835         if (ret != 0) {
836                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
837                 return ret;
838         }
839
840         printf("Number of databases:%d\n", dbmap->num);
841         for(i=0;i<dbmap->num;i++){
842                 const char *path;
843                 const char *name;
844                 bool persistent;
845
846                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
847                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
848                 persistent = dbmap->dbs[i].persistent;
849                 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name, 
850                        path, persistent?"PERSISTENT":"");
851         }
852
853         return 0;
854 }
855
856 /*
857   check if the local node is recmaster or not
858   it will return 1 if this node is the recmaster and 0 if it is not
859   or if the local ctdb daemon could not be contacted
860  */
861 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
862 {
863         uint32_t mypnn, recmaster;
864         int ret;
865
866         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
867         if (mypnn == -1) {
868                 printf("Failed to get pnn of node\n");
869                 return 1;
870         }
871
872         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
873         if (ret != 0) {
874                 printf("Failed to get the recmaster\n");
875                 return 1;
876         }
877
878         if (recmaster != mypnn) {
879                 printf("this node is not the recmaster\n");
880                 return 1;
881         }
882
883         printf("this node is the recmaster\n");
884         return 0;
885 }
886
887 /*
888   ping a node
889  */
890 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
891 {
892         int ret;
893         struct timeval tv = timeval_current();
894         ret = ctdb_ctrl_ping(ctdb, options.pnn);
895         if (ret == -1) {
896                 printf("Unable to get ping response from node %u\n", options.pnn);
897         } else {
898                 printf("response from %u time=%.6f sec  (%d clients)\n", 
899                        options.pnn, timeval_elapsed(&tv), ret);
900         }
901         return 0;
902 }
903
904
905 /*
906   get a tunable
907  */
908 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
909 {
910         const char *name;
911         uint32_t value;
912         int ret;
913
914         if (argc < 1) {
915                 usage();
916         }
917
918         name = argv[0];
919         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
920         if (ret == -1) {
921                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
922                 return -1;
923         }
924
925         printf("%-19s = %u\n", name, value);
926         return 0;
927 }
928
929 /*
930   set a tunable
931  */
932 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
933 {
934         const char *name;
935         uint32_t value;
936         int ret;
937
938         if (argc < 2) {
939                 usage();
940         }
941
942         name = argv[0];
943         value = strtoul(argv[1], NULL, 0);
944
945         ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
946         if (ret == -1) {
947                 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
948                 return -1;
949         }
950         return 0;
951 }
952
953 /*
954   list all tunables
955  */
956 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
957 {
958         uint32_t count;
959         const char **list;
960         int ret, i;
961
962         ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
963         if (ret == -1) {
964                 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
965                 return -1;
966         }
967
968         for (i=0;i<count;i++) {
969                 control_getvar(ctdb, 1, &list[i]);
970         }
971
972         talloc_free(list);
973         
974         return 0;
975 }
976
977 static struct {
978         int32_t level;
979         const char *description;
980 } debug_levels[] = {
981         {DEBUG_EMERG,   "EMERG"},
982         {DEBUG_ALERT,   "ALERT"},
983         {DEBUG_CRIT,    "CRIT"},
984         {DEBUG_ERR,     "ERR"},
985         {DEBUG_WARNING, "WARNING"},
986         {DEBUG_NOTICE,  "NOTICE"},
987         {DEBUG_INFO,    "INFO"},
988         {DEBUG_DEBUG,   "DEBUG"}
989 };
990
991 static const char *get_debug_by_level(int32_t level)
992 {
993         int i;
994
995         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
996                 if (debug_levels[i].level == level) {
997                         return debug_levels[i].description;
998                 }
999         }
1000         return "Unknown";
1001 }
1002
1003 static int32_t get_debug_by_desc(const char *desc)
1004 {
1005         int i;
1006
1007         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1008                 if (!strcmp(debug_levels[i].description, desc)) {
1009                         return debug_levels[i].level;
1010                 }
1011         }
1012         return DEBUG_ERR;
1013 }
1014
1015 /*
1016   display debug level on a node
1017  */
1018 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1019 {
1020         int ret;
1021         int32_t level;
1022
1023         ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1024         if (ret != 0) {
1025           DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", 
1026                        options.pnn));
1027         } else {
1028                 printf("Node %u is at debug level %s (%u)\n", options.pnn, get_debug_by_level(level), level);
1029         }
1030         return 0;
1031 }
1032
1033
1034 /*
1035   set debug level on a node or all nodes
1036  */
1037 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1038 {
1039         int ret;
1040         uint32_t level;
1041
1042         if (argc < 1) {
1043                 usage();
1044         }
1045
1046         if (isalpha(argv[0][0])) { 
1047                 level = get_debug_by_desc(argv[0]);
1048         } else {
1049                 level = strtoul(argv[0], NULL, 0);
1050         }
1051
1052         ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1053         if (ret != 0) {
1054                 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1055         }
1056         return 0;
1057 }
1058
1059
1060 /*
1061   freeze a node
1062  */
1063 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1064 {
1065         int ret;
1066
1067         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1068         if (ret != 0) {
1069                 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
1070         }               
1071         return 0;
1072 }
1073
1074 /*
1075   thaw a node
1076  */
1077 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
1078 {
1079         int ret;
1080
1081         ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
1082         if (ret != 0) {
1083                 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
1084         }               
1085         return 0;
1086 }
1087
1088
1089 /*
1090   attach to a database
1091  */
1092 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
1093 {
1094         const char *db_name;
1095         struct ctdb_db_context *ctdb_db;
1096
1097         if (argc < 1) {
1098                 usage();
1099         }
1100         db_name = argv[0];
1101
1102         ctdb_db = ctdb_attach(ctdb, db_name, false);
1103         if (ctdb_db == NULL) {
1104                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1105                 return -1;
1106         }
1107
1108         return 0;
1109 }
1110
1111 /*
1112   dump memory usage
1113  */
1114 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1115 {
1116         TDB_DATA data;
1117         int ret;
1118         int32_t res;
1119         char *errmsg;
1120         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1121         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
1122                            0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
1123         if (ret != 0 || res != 0) {
1124                 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
1125                 talloc_free(tmp_ctx);
1126                 return -1;
1127         }
1128         write(1, data.dptr, data.dsize);
1129         talloc_free(tmp_ctx);
1130         return 0;
1131 }
1132
1133 /*
1134   list all nodes in the cluster
1135  */
1136 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
1137 {
1138         int i, ret;
1139         struct ctdb_node_map *nodemap=NULL;
1140
1141         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1142         if (ret != 0) {
1143                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1144                 return ret;
1145         }
1146
1147         for(i=0;i<nodemap->num;i++){
1148                 printf("%s\n", inet_ntoa(nodemap->nodes[i].sin.sin_addr));
1149         }
1150
1151         return 0;
1152 }
1153
1154 /*
1155   reload the nodes file on the local node
1156  */
1157 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
1158 {
1159         int ret;
1160
1161
1162         ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
1163                 CTDB_CURRENT_NODE);
1164         if (ret != 0) {
1165                 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on local node You MUST fix that node manually!\n"));
1166         }
1167         
1168                                 
1169         return 0;
1170 }
1171
1172
1173 static const struct {
1174         const char *name;
1175         int (*fn)(struct ctdb_context *, int, const char **);
1176         bool auto_all;
1177         const char *msg;
1178         const char *args;
1179 } ctdb_commands[] = {
1180         { "status",          control_status,            true,  "show node status" },
1181         { "uptime",          control_uptime,            true,  "show node uptime" },
1182         { "ping",            control_ping,              true,  "ping all nodes" },
1183         { "getvar",          control_getvar,            true,  "get a tunable variable",               "<name>"},
1184         { "setvar",          control_setvar,            true,  "set a tunable variable",               "<name> <value>"},
1185         { "listvars",        control_listvars,          true,  "list tunable variables"},
1186         { "statistics",      control_statistics,        false, "show statistics" },
1187         { "statisticsreset", control_statistics_reset,  true,  "reset statistics"},
1188         { "ip",              control_ip,                true,  "show which public ip's that ctdb manages" },
1189         { "process-exists",  control_process_exists,    true,  "check if a process exists on a node",  "<pid>"},
1190         { "getdbmap",        control_getdbmap,          true,  "show the database map" },
1191         { "catdb",           control_catdb,             true,  "dump a database" ,                     "<dbname>"},
1192         { "getmonmode",      control_getmonmode,        true,  "show monitoring mode" },
1193         { "setdebug",        control_setdebug,          true,  "set debug level",                      "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
1194         { "getdebug",        control_getdebug,          true,  "get debug level" },
1195         { "attach",          control_attach,            true,  "attach to a database",                 "<dbname>" },
1196         { "dumpmemory",      control_dumpmemory,        true,  "dump memory map to stdout" },
1197         { "getpid",          control_getpid,            true,  "get ctdbd process ID" },
1198         { "disable",         control_disable,           true,  "disable a nodes public IP" },
1199         { "enable",          control_enable,            true,  "enable a nodes public IP" },
1200         { "ban",             control_ban,               true,  "ban a node from the cluster",          "<bantime|0>"},
1201         { "unban",           control_unban,             true,  "unban a node from the cluster" },
1202         { "shutdown",        control_shutdown,          true,  "shutdown ctdbd" },
1203         { "recover",         control_recover,           true,  "force recovery" },
1204         { "freeze",          control_freeze,            true,  "freeze all databases" },
1205         { "thaw",            control_thaw,              true,  "thaw all databases" },
1206         { "isnotrecmaster",  control_isnotrecmaster,    false,  "check if the local node is recmaster or not" },
1207         { "killtcp",         kill_tcp,                  false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
1208         { "gratiousarp",     control_gratious_arp,      false, "send a gratious arp", "<ip> <interface>" },
1209         { "tickle",          tickle_tcp,                false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
1210         { "gettickles",      control_get_tickles,       false, "get the list of tickles registered for this ip", "<ip>" },
1211
1212         { "regsrvid",        regsrvid,                  false, "register a server id", "<pnn> <type> <id>" },
1213         { "unregsrvid",      unregsrvid,                false, "unregister a server id", "<pnn> <type> <id>" },
1214         { "chksrvid",        chksrvid,                  false, "check if a server id exists", "<pnn> <type> <id>" },
1215         { "getsrvids",       getsrvids,                 false, "get a list of all server ids"},
1216         { "vacuum",          ctdb_vacuum,               false, "vacuum the databases of empty records", "[max_records]"},
1217         { "repack",          ctdb_repack,               false, "repack all databases", "[max_freelist]"},
1218         { "listnodes",       control_listnodes,         false, "list all nodes in the cluster"},
1219         { "reloadnodes",         control_reload_nodes_file,             false, "update the nodes file and restart the transport on the local node"},
1220 };
1221
1222 /*
1223   show usage message
1224  */
1225 static void usage(void)
1226 {
1227         int i;
1228         printf(
1229 "Usage: ctdb [options] <control>\n" \
1230 "Options:\n" \
1231 "   -n <node>          choose node number, or 'all' (defaults to local node)\n"
1232 "   -Y                 generate machinereadable output\n"
1233 "   -t <timelimit>     set timelimit for control in seconds (default %u)\n", options.timelimit);
1234         printf("Controls:\n");
1235         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1236                 printf("  %-15s %-27s  %s\n", 
1237                        ctdb_commands[i].name, 
1238                        ctdb_commands[i].args?ctdb_commands[i].args:"",
1239                        ctdb_commands[i].msg);
1240         }
1241         exit(1);
1242 }
1243
1244
1245 static void ctdb_alarm(int sig)
1246 {
1247         printf("Maximum runtime exceeded - exiting\n");
1248         _exit(0);
1249 }
1250
1251 /*
1252   main program
1253 */
1254 int main(int argc, const char *argv[])
1255 {
1256         struct ctdb_context *ctdb;
1257         char *nodestring = NULL;
1258         struct poptOption popt_options[] = {
1259                 POPT_AUTOHELP
1260                 POPT_CTDB_CMDLINE
1261                 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
1262                 { "node",      'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
1263                 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
1264                 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
1265                 POPT_TABLEEND
1266         };
1267         int opt;
1268         const char **extra_argv;
1269         int extra_argc = 0;
1270         int ret=-1, i;
1271         poptContext pc;
1272         struct event_context *ev;
1273         const char *control;
1274
1275         setlinebuf(stdout);
1276         
1277         /* set some defaults */
1278         options.maxruntime = 0;
1279         options.timelimit = 3;
1280         options.pnn = CTDB_CURRENT_NODE;
1281
1282         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
1283
1284         while ((opt = poptGetNextOpt(pc)) != -1) {
1285                 switch (opt) {
1286                 default:
1287                         DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
1288                                 poptBadOption(pc, 0), poptStrerror(opt)));
1289                         exit(1);
1290                 }
1291         }
1292
1293         /* setup the remaining options for the main program to use */
1294         extra_argv = poptGetArgs(pc);
1295         if (extra_argv) {
1296                 extra_argv++;
1297                 while (extra_argv[extra_argc]) extra_argc++;
1298         }
1299
1300         if (extra_argc < 1) {
1301                 usage();
1302         }
1303
1304         if (options.maxruntime != 0) {
1305                 signal(SIGALRM, ctdb_alarm);
1306                 alarm(options.maxruntime);
1307         }
1308
1309         /* setup the node number to contact */
1310         if (nodestring != NULL) {
1311                 if (strcmp(nodestring, "all") == 0) {
1312                         options.pnn = CTDB_BROADCAST_ALL;
1313                 } else {
1314                         options.pnn = strtoul(nodestring, NULL, 0);
1315                 }
1316         }
1317
1318         control = extra_argv[0];
1319
1320         ev = event_context_init(NULL);
1321
1322         /* initialise ctdb */
1323         ctdb = ctdb_cmdline_client(ev);
1324         if (ctdb == NULL) {
1325                 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
1326                 exit(1);
1327         }
1328
1329         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1330                 if (strcmp(control, ctdb_commands[i].name) == 0) {
1331                         int j;
1332
1333                         if (options.pnn == CTDB_CURRENT_NODE) {
1334                                 int pnn;
1335                                 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);         
1336                                 if (pnn == -1) {
1337                                         return -1;
1338                                 }
1339                                 options.pnn = pnn;
1340                         }
1341
1342                         if (ctdb_commands[i].auto_all && 
1343                             options.pnn == CTDB_BROADCAST_ALL) {
1344                                 uint32_t *nodes;
1345                                 uint32_t num_nodes;
1346                                 ret = 0;
1347
1348                                 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
1349                                 CTDB_NO_MEMORY(ctdb, nodes);
1350         
1351                                 for (j=0;j<num_nodes;j++) {
1352                                         options.pnn = nodes[j];
1353                                         ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
1354                                 }
1355                                 talloc_free(nodes);
1356                         } else {
1357                                 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
1358                         }
1359                         break;
1360                 }
1361         }
1362
1363         if (i == ARRAY_SIZE(ctdb_commands)) {
1364                 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
1365                 exit(1);
1366         }
1367
1368         return ret;
1369 }