make LVS a capability so that we can see which nodes are configured with
[sahlberg/ctdb.git] / 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 #include "../common/rb_tree.h"
32
33 static void usage(void);
34
35 static struct {
36         int timelimit;
37         uint32_t pnn;
38         int machinereadable;
39         int maxruntime;
40 } options;
41
42 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
43
44 #ifdef CTDB_VERS
45 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
46 {
47 #define STR(x) #x
48 #define XSTR(x) STR(x)
49         printf("CTDB version: %s\n", XSTR(CTDB_VERS));
50         return 0;
51 }
52 #endif
53
54 /*
55   see if a process exists
56  */
57 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
58 {
59         uint32_t pnn, pid;
60         int ret;
61         if (argc < 1) {
62                 usage();
63         }
64
65         if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
66                 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
67                 return -1;
68         }
69
70         ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
71         if (ret == 0) {
72                 printf("%u:%u exists\n", pnn, pid);
73         } else {
74                 printf("%u:%u does not exist\n", pnn, pid);
75         }
76         return ret;
77 }
78
79 /*
80   display statistics structure
81  */
82 static void show_statistics(struct ctdb_statistics *s)
83 {
84         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
85         int i;
86         const char *prefix=NULL;
87         int preflen=0;
88         const struct {
89                 const char *name;
90                 uint32_t offset;
91         } fields[] = {
92 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
93                 STATISTICS_FIELD(num_clients),
94                 STATISTICS_FIELD(frozen),
95                 STATISTICS_FIELD(recovering),
96                 STATISTICS_FIELD(client_packets_sent),
97                 STATISTICS_FIELD(client_packets_recv),
98                 STATISTICS_FIELD(node_packets_sent),
99                 STATISTICS_FIELD(node_packets_recv),
100                 STATISTICS_FIELD(keepalive_packets_sent),
101                 STATISTICS_FIELD(keepalive_packets_recv),
102                 STATISTICS_FIELD(node.req_call),
103                 STATISTICS_FIELD(node.reply_call),
104                 STATISTICS_FIELD(node.req_dmaster),
105                 STATISTICS_FIELD(node.reply_dmaster),
106                 STATISTICS_FIELD(node.reply_error),
107                 STATISTICS_FIELD(node.req_message),
108                 STATISTICS_FIELD(node.req_control),
109                 STATISTICS_FIELD(node.reply_control),
110                 STATISTICS_FIELD(client.req_call),
111                 STATISTICS_FIELD(client.req_message),
112                 STATISTICS_FIELD(client.req_control),
113                 STATISTICS_FIELD(timeouts.call),
114                 STATISTICS_FIELD(timeouts.control),
115                 STATISTICS_FIELD(timeouts.traverse),
116                 STATISTICS_FIELD(total_calls),
117                 STATISTICS_FIELD(pending_calls),
118                 STATISTICS_FIELD(lockwait_calls),
119                 STATISTICS_FIELD(pending_lockwait_calls),
120                 STATISTICS_FIELD(childwrite_calls),
121                 STATISTICS_FIELD(pending_childwrite_calls),
122                 STATISTICS_FIELD(memory_used),
123                 STATISTICS_FIELD(max_hop_count),
124         };
125         printf("CTDB version %u\n", CTDB_VERSION);
126         for (i=0;i<ARRAY_SIZE(fields);i++) {
127                 if (strchr(fields[i].name, '.')) {
128                         preflen = strcspn(fields[i].name, ".")+1;
129                         if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
130                                 prefix = fields[i].name;
131                                 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
132                         }
133                 } else {
134                         preflen = 0;
135                 }
136                 printf(" %*s%-22s%*s%10u\n", 
137                        preflen?4:0, "",
138                        fields[i].name+preflen, 
139                        preflen?0:4, "",
140                        *(uint32_t *)(fields[i].offset+(uint8_t *)s));
141         }
142         printf(" %-30s     %.6f sec\n", "max_call_latency", s->max_call_latency);
143         printf(" %-30s     %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
144         printf(" %-30s     %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
145         talloc_free(tmp_ctx);
146 }
147
148 /*
149   display remote ctdb statistics combined from all nodes
150  */
151 static int control_statistics_all(struct ctdb_context *ctdb)
152 {
153         int ret, i;
154         struct ctdb_statistics statistics;
155         uint32_t *nodes;
156         uint32_t num_nodes;
157
158         nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
159         CTDB_NO_MEMORY(ctdb, nodes);
160         
161         ZERO_STRUCT(statistics);
162
163         for (i=0;i<num_nodes;i++) {
164                 struct ctdb_statistics s1;
165                 int j;
166                 uint32_t *v1 = (uint32_t *)&s1;
167                 uint32_t *v2 = (uint32_t *)&statistics;
168                 uint32_t num_ints = 
169                         offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
170                 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
171                 if (ret != 0) {
172                         DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
173                         return ret;
174                 }
175                 for (j=0;j<num_ints;j++) {
176                         v2[j] += v1[j];
177                 }
178                 statistics.max_hop_count = 
179                         MAX(statistics.max_hop_count, s1.max_hop_count);
180                 statistics.max_call_latency = 
181                         MAX(statistics.max_call_latency, s1.max_call_latency);
182                 statistics.max_lockwait_latency = 
183                         MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
184         }
185         talloc_free(nodes);
186         printf("Gathered statistics for %u nodes\n", num_nodes);
187         show_statistics(&statistics);
188         return 0;
189 }
190
191 /*
192   display remote ctdb statistics
193  */
194 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
195 {
196         int ret;
197         struct ctdb_statistics statistics;
198
199         if (options.pnn == CTDB_BROADCAST_ALL) {
200                 return control_statistics_all(ctdb);
201         }
202
203         ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
204         if (ret != 0) {
205                 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
206                 return ret;
207         }
208         show_statistics(&statistics);
209         return 0;
210 }
211
212
213 /*
214   reset remote ctdb statistics
215  */
216 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
217 {
218         int ret;
219
220         ret = ctdb_statistics_reset(ctdb, options.pnn);
221         if (ret != 0) {
222                 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
223                 return ret;
224         }
225         return 0;
226 }
227
228
229 /*
230   display uptime of remote node
231  */
232 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
233 {
234         int ret;
235         int mypnn;
236         struct ctdb_uptime *uptime = NULL;
237         int tmp, days, hours, minutes, seconds;
238
239         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
240         if (mypnn == -1) {
241                 return -1;
242         }
243
244         ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
245         if (ret != 0) {
246                 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
247                 return ret;
248         }
249
250         if (options.machinereadable){
251                 printf(":Current Node Time:Ctdb Start Time:Last Recovery Time:Last Recovery Duration:\n");
252                 printf(":%u:%u:%u:%lf\n",
253                         (unsigned int)uptime->current_time.tv_sec,
254                         (unsigned int)uptime->ctdbd_start_time.tv_sec,
255                         (unsigned int)uptime->last_recovery_finished.tv_sec,
256                         timeval_delta(&uptime->last_recovery_finished,
257                                       &uptime->last_recovery_started)
258                 );
259                 return 0;
260         }
261
262         printf("Current time of node  : %s", ctime(&uptime->current_time.tv_sec));
263
264         tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
265         seconds = tmp%60;
266         tmp    /= 60;
267         minutes = tmp%60;
268         tmp    /= 60;
269         hours   = tmp%24;
270         tmp    /= 24;
271         days    = tmp;
272         printf("Ctdbd start time      : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
273
274         tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
275         seconds = tmp%60;
276         tmp    /= 60;
277         minutes = tmp%60;
278         tmp    /= 60;
279         hours   = tmp%24;
280         tmp    /= 24;
281         days    = tmp;
282         printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
283         
284         printf("Duration of last recovery : %lf seconds\n",
285                 timeval_delta(&uptime->last_recovery_finished,
286                               &uptime->last_recovery_started));
287
288         return 0;
289 }
290
291 /*
292   display remote ctdb status
293  */
294 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
295 {
296         int i, ret;
297         struct ctdb_vnn_map *vnnmap=NULL;
298         struct ctdb_node_map *nodemap=NULL;
299         uint32_t recmode, recmaster;
300         int mypnn;
301
302         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
303         if (mypnn == -1) {
304                 return -1;
305         }
306
307         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
308         if (ret != 0) {
309                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
310                 return ret;
311         }
312
313         if(options.machinereadable){
314                 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
315                 for(i=0;i<nodemap->num;i++){
316                         printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
317                                 inet_ntoa(nodemap->nodes[i].sin.sin_addr),
318                                !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
319                                !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
320                                !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
321                                !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
322                 }
323                 return 0;
324         }
325
326         printf("Number of nodes:%d\n", nodemap->num);
327         for(i=0;i<nodemap->num;i++){
328                 static const struct {
329                         uint32_t flag;
330                         const char *name;
331                 } flag_names[] = {
332                         { NODE_FLAGS_DISCONNECTED,          "DISCONNECTED" },
333                         { NODE_FLAGS_PERMANENTLY_DISABLED,  "DISABLED" },
334                         { NODE_FLAGS_BANNED,                "BANNED" },
335                         { NODE_FLAGS_UNHEALTHY,             "UNHEALTHY" },
336                 };
337                 char *flags_str = NULL;
338                 int j;
339                 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
340                         if (nodemap->nodes[i].flags & flag_names[j].flag) {
341                                 if (flags_str == NULL) {
342                                         flags_str = talloc_strdup(ctdb, flag_names[j].name);
343                                 } else {
344                                         flags_str = talloc_asprintf_append(flags_str, "|%s",
345                                                                            flag_names[j].name);
346                                 }
347                                 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
348                         }
349                 }
350                 if (flags_str == NULL) {
351                         flags_str = talloc_strdup(ctdb, "OK");
352                         CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
353                 }
354                 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
355                        inet_ntoa(nodemap->nodes[i].sin.sin_addr),
356                        flags_str,
357                        nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
358                 talloc_free(flags_str);
359         }
360
361         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
362         if (ret != 0) {
363                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
364                 return ret;
365         }
366         if (vnnmap->generation == INVALID_GENERATION) {
367                 printf("Generation:INVALID\n");
368         } else {
369                 printf("Generation:%d\n",vnnmap->generation);
370         }
371         printf("Size:%d\n",vnnmap->size);
372         for(i=0;i<vnnmap->size;i++){
373                 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
374         }
375
376         ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
377         if (ret != 0) {
378                 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
379                 return ret;
380         }
381         printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
382
383         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
384         if (ret != 0) {
385                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
386                 return ret;
387         }
388         printf("Recovery master:%d\n",recmaster);
389
390         return 0;
391 }
392
393 /*
394   get a list of all tickles for this pnn
395  */
396 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
397 {
398         struct ctdb_control_tcp_tickle_list *list;
399         struct sockaddr_in ip;
400         int i, ret;
401
402         if (argc < 1) {
403                 usage();
404         }
405
406         ip.sin_family = AF_INET;
407         if (inet_aton(argv[0], &ip.sin_addr) == 0) {
408                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
409                 return -1;
410         }
411
412         ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &ip, &list);
413         if (ret == -1) {
414                 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
415                 return -1;
416         }
417
418         printf("Tickles for ip:%s\n", inet_ntoa(list->ip.sin_addr));
419         printf("Num tickles:%u\n", list->tickles.num);
420         for (i=0;i<list->tickles.num;i++) {
421                 printf("SRC: %s:%u   ", inet_ntoa(list->tickles.connections[i].saddr.sin_addr), ntohs(list->tickles.connections[i].saddr.sin_port));
422                 printf("DST: %s:%u\n", inet_ntoa(list->tickles.connections[i].daddr.sin_addr), ntohs(list->tickles.connections[i].daddr.sin_port));
423         }
424
425         talloc_free(list);
426         
427         return 0;
428 }
429
430 /* send a release ip to all nodes */
431 static int control_send_release(struct ctdb_context *ctdb, uint32_t pnn,
432 struct sockaddr_in *sin)
433 {
434         int ret;
435         struct ctdb_public_ip pip;
436         TDB_DATA data;
437         struct ctdb_node_map *nodemap=NULL;
438
439         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
440         if (ret != 0) {
441                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
442                 return ret;
443         }
444
445         /* send a moveip message to the recovery master */
446         pip.pnn = pnn;
447         pip.sin.sin_family = AF_INET;
448         pip.sin.sin_addr   = sin->sin_addr;
449         data.dsize = sizeof(pip);
450         data.dptr = (unsigned char *)&pip;
451
452
453         /* send release ip to all nodes */
454         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
455                         list_of_active_nodes(ctdb, nodemap, ctdb, true),
456                         TIMELIMIT(), false, data,
457                         NULL, NULL, NULL) != 0) {
458                 DEBUG(DEBUG_ERR, (__location__ " Unable to send 'ReleaseIP' to all nodes.\n"));
459                 return -1;
460         }
461
462         return 0;
463 }
464
465 /*
466   move/failover an ip address to a specific node
467  */
468 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
469 {
470         uint32_t pnn;
471         struct sockaddr_in ip;
472         uint32_t value;
473         struct ctdb_all_public_ips *ips;
474         int i, ret;
475
476         if (argc < 2) {
477                 usage();
478         }
479
480         ip.sin_family = AF_INET;
481         if (inet_aton(argv[0], &ip.sin_addr) == 0) {
482                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
483                 return -1;
484         }
485
486
487         if (sscanf(argv[1], "%u", &pnn) != 1) {
488                 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
489                 return -1;
490         }
491
492         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "DeterministicIPs", &value);
493         if (ret == -1) {
494                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'DeterministicIPs' from local node\n"));
495                 return -1;
496         }
497         if (value != 0) {
498                 DEBUG(DEBUG_ERR, ("The tunable 'DeterministicIPs' is set. You can only move ip addresses when this feature is disabled\n"));
499                 return -1;
500         }
501
502         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "NoIPFailback", &value);
503         if (ret == -1) {
504                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'NoIPFailback' from local node\n"));
505                 return -1;
506         }
507         if (value == 0) {
508                 DEBUG(DEBUG_ERR, ("The tunable 'NoIPFailback' is NOT set. You can only move ip addresses when this feature is enabled\n"));
509                 return -1;
510         }
511
512         /* read the public ip list from the node */
513         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
514         if (ret != 0) {
515                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
516                 return -1;
517         }
518
519         for (i=0;i<ips->num;i++) {
520                 if (ctdb_same_ipv4(&ip, &ips->ips[i].sin)) {
521                         break;
522                 }
523         }
524         if (i==ips->num) {
525                 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
526                         pnn, inet_ntoa(ip.sin_addr)));
527                 return -1;
528         }
529         if (ips->ips[i].pnn == pnn) {
530                 DEBUG(DEBUG_ERR, ("Host %u is already hosting '%s'\n",
531                         pnn, inet_ntoa(ips->ips[i].sin.sin_addr)));
532                 return -1;
533         }
534
535         ret = control_send_release(ctdb, pnn, &ips->ips[i].sin);
536         if (ret != 0) {
537                 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));;
538                 return -1;
539         }
540
541         return 0;
542 }
543
544 struct node_ip {
545         uint32_t pnn;
546         struct sockaddr_in sin;
547 };
548
549 void getips_store_callback(void *param, void *data)
550 {
551         struct node_ip *node_ip = (struct node_ip *)data;
552         struct ctdb_all_public_ips *ips = param;
553         int i;
554
555         i = ips->num++;
556         ips->ips[i].pnn = node_ip->pnn;
557         ips->ips[i].sin = node_ip->sin;
558 }
559
560 void getips_count_callback(void *param, void *data)
561 {
562         uint32_t *count = param;
563
564         (*count)++;
565 }
566
567 static int
568 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
569 {
570         struct ctdb_all_public_ips *tmp_ips;
571         struct ctdb_node_map *nodemap=NULL;
572         trbt_tree_t *tree;
573         int i, j, len, ret;
574         uint32_t count;
575
576         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
577         if (ret != 0) {
578                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
579                 return ret;
580         }
581
582         tree = trbt_create(tmp_ctx, 0);
583
584         for(i=0;i<nodemap->num;i++){
585                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
586                         continue;
587                 }
588
589                 /* read the public ip list from this node */
590                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
591                 if (ret != 0) {
592                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
593                         return -1;
594                 }
595         
596                 for (j=0; j<tmp_ips->num;j++) {
597                         struct node_ip *node_ip;
598
599                         node_ip = talloc(tmp_ctx, struct node_ip);
600                         node_ip->pnn = tmp_ips->ips[j].pnn;
601                         node_ip->sin = tmp_ips->ips[j].sin;
602
603                         trbt_insert32(tree, tmp_ips->ips[j].sin.sin_addr.s_addr, node_ip);
604                 }
605                 talloc_free(tmp_ips);
606         }
607
608         /* traverse */
609         count = 0;
610         trbt_traversearray32(tree, 1, getips_count_callback, &count);
611
612         len = offsetof(struct ctdb_all_public_ips, ips) + 
613                 count*sizeof(struct ctdb_public_ip);
614         tmp_ips = talloc_zero_size(tmp_ctx, len);
615         trbt_traversearray32(tree, 1, getips_store_callback, tmp_ips);
616
617         *ips = tmp_ips;
618
619         return 0;
620 }
621
622
623 /* 
624  * scans all other nodes and returns a pnn for another node that can host this 
625  * ip address or -1
626  */
627 static int
628 find_other_host_for_public_ip(struct ctdb_context *ctdb, struct sockaddr_in *addr)
629 {
630         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
631         struct ctdb_all_public_ips *ips;
632         struct ctdb_node_map *nodemap=NULL;
633         int i, j, ret;
634
635         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
636         if (ret != 0) {
637                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
638                 talloc_free(tmp_ctx);
639                 return ret;
640         }
641
642         for(i=0;i<nodemap->num;i++){
643                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
644                         continue;
645                 }
646                 if (nodemap->nodes[i].pnn == options.pnn) {
647                         continue;
648                 }
649
650                 /* read the public ip list from this node */
651                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
652                 if (ret != 0) {
653                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
654                         return -1;
655                 }
656
657                 for (j=0;j<ips->num;j++) {
658                         if (ctdb_same_ipv4(addr, &ips->ips[j].sin)) {
659                                 talloc_free(tmp_ctx);
660                                 return nodemap->nodes[i].pnn;
661                         }
662                 }
663                 talloc_free(ips);
664         }
665
666         talloc_free(tmp_ctx);
667         return -1;
668 }
669
670 /*
671   add a public ip address to a node
672  */
673 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
674 {
675         int i, ret;
676         int len;
677         unsigned mask;
678         struct sockaddr_in addr;
679         struct ctdb_control_ip_iface *pub;
680         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
681         struct ctdb_all_public_ips *ips;
682
683         if (argc != 2) {
684                 talloc_free(tmp_ctx);
685                 usage();
686         }
687
688         if (!parse_ip_mask(argv[0], &addr, &mask)) {
689                 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
690                 talloc_free(tmp_ctx);
691                 return -1;
692         }
693
694         ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
695         if (ret != 0) {
696                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
697                 talloc_free(tmp_ctx);
698                 return ret;
699         }
700
701
702         len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
703         pub = talloc_size(tmp_ctx, len); 
704         CTDB_NO_MEMORY(ctdb, pub);
705
706         pub->sin   = addr;
707         pub->mask  = mask;
708         pub->len   = strlen(argv[1])+1;
709         memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
710
711         ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
712         if (ret != 0) {
713                 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
714                 talloc_free(tmp_ctx);
715                 return ret;
716         }
717
718
719         /* check if some other node is already serving this ip, if not,
720          * we will claim it
721          */
722         for (i=0;i<ips->num;i++) {
723                 if (ctdb_same_ipv4(&addr, &ips->ips[i].sin)) {
724                         break;
725                 }
726         }
727         /* no one has this ip so we claim it */
728         if (i == ips->num) {
729                 ret = control_send_release(ctdb, options.pnn, &addr);
730         } else {
731                 ret = control_send_release(ctdb, ips->ips[i].pnn, &addr);
732         }
733
734         if (ret != 0) {
735                 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));
736                 return -1;
737         }
738
739         talloc_free(tmp_ctx);
740         return 0;
741 }
742
743 /*
744   delete a public ip address from a node
745  */
746 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
747 {
748         int i, ret;
749         struct sockaddr_in addr;
750         struct ctdb_control_ip_iface pub;
751         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
752         struct ctdb_all_public_ips *ips;
753
754         if (argc != 1) {
755                 talloc_free(tmp_ctx);
756                 usage();
757         }
758
759         addr.sin_family = AF_INET;
760         if (inet_aton(argv[0], &addr.sin_addr) == 0) {
761                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
762                 return -1;
763         }
764
765         pub.sin   = addr;
766         pub.mask  = 0;
767         pub.len   = 0;
768
769         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
770         if (ret != 0) {
771                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
772                 talloc_free(tmp_ctx);
773                 return ret;
774         }
775         
776         for (i=0;i<ips->num;i++) {
777                 if (ctdb_same_ipv4(&addr, &ips->ips[i].sin)) {
778                         break;
779                 }
780         }
781
782         if (i==ips->num) {
783                 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
784                         inet_ntoa(addr.sin_addr)));
785                 talloc_free(tmp_ctx);
786                 return -1;
787         }
788
789         if (ips->ips[i].pnn == options.pnn) {
790                 ret = find_other_host_for_public_ip(ctdb, &addr);
791                 if (ret != -1) {
792                         ret = control_send_release(ctdb, ret, &addr);
793                         if (ret != 0) {
794                                 DEBUG(DEBUG_ERR, ("Failed to migrate this ip to another node. Use moveip of recover to reassign this address to a node\n"));
795                         }
796                 }
797         }
798
799         ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
800         if (ret != 0) {
801                 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
802                 talloc_free(tmp_ctx);
803                 return ret;
804         }
805
806         talloc_free(tmp_ctx);
807         return 0;
808 }
809
810 /*
811   kill a tcp connection
812  */
813 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
814 {
815         int ret;
816         struct ctdb_control_killtcp killtcp;
817
818         if (argc < 2) {
819                 usage();
820         }
821
822         if (!parse_ip_port(argv[0], (ctdb_sock_addr *)&killtcp.src)) {
823                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
824                 return -1;
825         }
826
827         if (!parse_ip_port(argv[1], (ctdb_sock_addr *)&killtcp.dst)) {
828                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
829                 return -1;
830         }
831
832         ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
833         if (ret != 0) {
834                 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
835                 return ret;
836         }
837
838         return 0;
839 }
840
841
842 /*
843   send a gratious arp
844  */
845 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
846 {
847         int ret;
848         ctdb_sock_addr addr;
849
850         if (argc < 2) {
851                 usage();
852         }
853
854         if (!parse_ip(argv[0], &addr)) {
855                 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
856                 return -1;
857         }
858
859         ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
860         if (ret != 0) {
861                 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
862                 return ret;
863         }
864
865         return 0;
866 }
867
868 /*
869   register a server id
870  */
871 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
872 {
873         int ret;
874         struct ctdb_server_id server_id;
875
876         if (argc < 3) {
877                 usage();
878         }
879
880         server_id.pnn       = strtoul(argv[0], NULL, 0);
881         server_id.type      = strtoul(argv[1], NULL, 0);
882         server_id.server_id = strtoul(argv[2], NULL, 0);
883
884         ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
885         if (ret != 0) {
886                 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
887                 return ret;
888         }
889         return -1;
890 }
891
892 /*
893   unregister a server id
894  */
895 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
896 {
897         int ret;
898         struct ctdb_server_id server_id;
899
900         if (argc < 3) {
901                 usage();
902         }
903
904         server_id.pnn       = strtoul(argv[0], NULL, 0);
905         server_id.type      = strtoul(argv[1], NULL, 0);
906         server_id.server_id = strtoul(argv[2], NULL, 0);
907
908         ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
909         if (ret != 0) {
910                 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
911                 return ret;
912         }
913         return -1;
914 }
915
916 /*
917   check if a server id exists
918  */
919 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
920 {
921         uint32_t status;
922         int ret;
923         struct ctdb_server_id server_id;
924
925         if (argc < 3) {
926                 usage();
927         }
928
929         server_id.pnn       = strtoul(argv[0], NULL, 0);
930         server_id.type      = strtoul(argv[1], NULL, 0);
931         server_id.server_id = strtoul(argv[2], NULL, 0);
932
933         ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
934         if (ret != 0) {
935                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
936                 return ret;
937         }
938
939         if (status) {
940                 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
941         } else {
942                 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
943         }
944         return 0;
945 }
946
947 /*
948   get a list of all server ids that are registered on a node
949  */
950 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
951 {
952         int i, ret;
953         struct ctdb_server_id_list *server_ids;
954
955         ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
956         if (ret != 0) {
957                 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
958                 return ret;
959         }
960
961         for (i=0; i<server_ids->num; i++) {
962                 printf("Server id %d:%d:%d\n", 
963                         server_ids->server_ids[i].pnn, 
964                         server_ids->server_ids[i].type, 
965                         server_ids->server_ids[i].server_id); 
966         }
967
968         return -1;
969 }
970
971 /*
972   send a tcp tickle ack
973  */
974 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
975 {
976         int ret;
977         ctdb_sock_addr  src, dst;
978
979         if (argc < 2) {
980                 usage();
981         }
982
983         if (!parse_ip_port(argv[0], &src)) {
984                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
985                 return -1;
986         }
987
988         if (!parse_ip_port(argv[1], &dst)) {
989                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
990                 return -1;
991         }
992
993         ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
994         if (ret==0) {
995                 return 0;
996         }
997         DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
998
999         return -1;
1000 }
1001
1002
1003 /*
1004   display public ip status
1005  */
1006 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
1007 {
1008         int i, ret;
1009         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1010         struct ctdb_all_public_ips *ips;
1011
1012         if (options.pnn == CTDB_BROADCAST_ALL) {
1013                 /* read the list of public ips from all nodes */
1014                 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1015         } else {
1016                 /* read the public ip list from this node */
1017                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1018         }
1019         if (ret != 0) {
1020                 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
1021                 talloc_free(tmp_ctx);
1022                 return ret;
1023         }
1024
1025         if (options.machinereadable){
1026                 printf(":Public IP:Node:\n");
1027         } else {
1028                 if (options.pnn == CTDB_BROADCAST_ALL) {
1029                         printf("Public IPs on ALL nodes\n");
1030                 } else {
1031                         printf("Public IPs on node %u\n", options.pnn);
1032                 }
1033         }
1034
1035         for (i=1;i<=ips->num;i++) {
1036                 if (options.machinereadable){
1037                         printf(":%s:%d:\n", inet_ntoa(ips->ips[ips->num-i].sin.sin_addr), ips->ips[ips->num-i].pnn);
1038                 } else {
1039                         printf("%s %d\n", inet_ntoa(ips->ips[ips->num-i].sin.sin_addr), ips->ips[ips->num-i].pnn);
1040                 }
1041         }
1042
1043         talloc_free(tmp_ctx);
1044         return 0;
1045 }
1046
1047 /*
1048   display pid of a ctdb daemon
1049  */
1050 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
1051 {
1052         uint32_t pid;
1053         int ret;
1054
1055         ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
1056         if (ret != 0) {
1057                 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
1058                 return ret;
1059         }
1060         printf("Pid:%d\n", pid);
1061
1062         return 0;
1063 }
1064
1065 /*
1066   disable a remote node
1067  */
1068 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
1069 {
1070         int ret;
1071
1072         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
1073         if (ret != 0) {
1074                 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
1075                 return ret;
1076         }
1077
1078         return 0;
1079 }
1080
1081 /*
1082   enable a disabled remote node
1083  */
1084 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
1085 {
1086         int ret;
1087
1088         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
1089         if (ret != 0) {
1090                 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
1091                 return ret;
1092         }
1093
1094         return 0;
1095 }
1096
1097 /*
1098   ban a node from the cluster
1099  */
1100 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
1101 {
1102         int ret;
1103         struct ctdb_ban_info b;
1104         TDB_DATA data;
1105         uint32_t ban_time;
1106
1107         if (argc < 1) {
1108                 usage();
1109         }
1110
1111         ban_time = strtoul(argv[0], NULL, 0);
1112
1113         b.pnn = options.pnn;
1114         b.ban_time = ban_time;
1115
1116         data.dptr = (uint8_t *)&b;
1117         data.dsize = sizeof(b);
1118
1119         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
1120         if (ret != 0) {
1121                 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
1122                 return -1;
1123         }
1124         
1125         return 0;
1126 }
1127
1128
1129 /*
1130   unban a node from the cluster
1131  */
1132 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
1133 {
1134         int ret;
1135         TDB_DATA data;
1136
1137         data.dptr = (uint8_t *)&options.pnn;
1138         data.dsize = sizeof(uint32_t);
1139
1140         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
1141         if (ret != 0) {
1142                 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
1143                 return -1;
1144         }
1145         
1146         return 0;
1147 }
1148
1149
1150 /*
1151   shutdown a daemon
1152  */
1153 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
1154 {
1155         int ret;
1156
1157         ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
1158         if (ret != 0) {
1159                 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
1160                 return ret;
1161         }
1162
1163         return 0;
1164 }
1165
1166 /*
1167   trigger a recovery
1168  */
1169 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
1170 {
1171         int ret;
1172
1173         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1174         if (ret != 0) {
1175                 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
1176                 return ret;
1177         }
1178
1179         ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
1180         if (ret != 0) {
1181                 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
1182                 return ret;
1183         }
1184
1185         return 0;
1186 }
1187
1188
1189 /*
1190   display monitoring mode of a remote node
1191  */
1192 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
1193 {
1194         uint32_t monmode;
1195         int ret;
1196
1197         ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
1198         if (ret != 0) {
1199                 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
1200                 return ret;
1201         }
1202         if (!options.machinereadable){
1203                 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
1204         } else {
1205                 printf(":mode:\n");
1206                 printf(":%d:\n",monmode);
1207         }
1208         return 0;
1209 }
1210
1211
1212 /*
1213   display capabilities of a remote node
1214  */
1215 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
1216 {
1217         uint32_t capabilities;
1218         int ret;
1219
1220         ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
1221         if (ret != 0) {
1222                 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
1223                 return ret;
1224         }
1225         
1226         if (!options.machinereadable){
1227                 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
1228                 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
1229                 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
1230         } else {
1231                 printf(":RECMASTER:LMASTER:LVS:\n");
1232                 printf(":%d:%d:%d:\n",
1233                         !!(capabilities&CTDB_CAP_RECMASTER),
1234                         !!(capabilities&CTDB_CAP_LMASTER),
1235                         !!(capabilities&CTDB_CAP_LVS));
1236         }
1237         return 0;
1238 }
1239
1240 /*
1241   disable monitoring on a  node
1242  */
1243 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1244 {
1245         
1246         int ret;
1247
1248         ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
1249         if (ret != 0) {
1250                 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
1251                 return ret;
1252         }
1253         printf("Monitoring mode:%s\n","DISABLED");
1254
1255         return 0;
1256 }
1257
1258 /*
1259   enable monitoring on a  node
1260  */
1261 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1262 {
1263         
1264         int ret;
1265
1266         ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
1267         if (ret != 0) {
1268                 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
1269                 return ret;
1270         }
1271         printf("Monitoring mode:%s\n","ACTIVE");
1272
1273         return 0;
1274 }
1275
1276 /*
1277   display remote list of keys/data for a db
1278  */
1279 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
1280 {
1281         const char *db_name;
1282         struct ctdb_db_context *ctdb_db;
1283         int ret;
1284
1285         if (argc < 1) {
1286                 usage();
1287         }
1288
1289         db_name = argv[0];
1290         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
1291
1292         if (ctdb_db == NULL) {
1293                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1294                 return -1;
1295         }
1296
1297         /* traverse and dump the cluster tdb */
1298         ret = ctdb_dump_db(ctdb_db, stdout);
1299         if (ret == -1) {
1300                 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
1301                 return -1;
1302         }
1303         talloc_free(ctdb_db);
1304
1305         printf("Dumped %d records\n", ret);
1306         return 0;
1307 }
1308
1309
1310 /*
1311   display a list of the databases on a remote ctdb
1312  */
1313 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
1314 {
1315         int i, ret;
1316         struct ctdb_dbid_map *dbmap=NULL;
1317
1318         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
1319         if (ret != 0) {
1320                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
1321                 return ret;
1322         }
1323
1324         printf("Number of databases:%d\n", dbmap->num);
1325         for(i=0;i<dbmap->num;i++){
1326                 const char *path;
1327                 const char *name;
1328                 bool persistent;
1329
1330                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
1331                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
1332                 persistent = dbmap->dbs[i].persistent;
1333                 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name, 
1334                        path, persistent?"PERSISTENT":"");
1335         }
1336
1337         return 0;
1338 }
1339
1340 /*
1341   get the filename of the reclock file
1342  */
1343 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
1344 {
1345         int i, ret, fd;
1346         const char *reclock;
1347         struct ctdb_node_map *nodemap=NULL;
1348         char *pnnfile;
1349
1350         ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
1351         if (ret != 0) {
1352                 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
1353                 return ret;
1354         }
1355
1356         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1357         if (ret != 0) {
1358                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1359                 return ret;
1360         }
1361
1362
1363         pnnfile = talloc_asprintf(ctdb, "%s.pnn", reclock);
1364         CTDB_NO_MEMORY(ctdb, pnnfile);
1365
1366         fd = open(pnnfile, O_RDONLY);
1367         if (fd == -1) {
1368                 DEBUG(DEBUG_CRIT,(__location__ " Failed to open reclock pnn file %s - (%s)\n", 
1369                          pnnfile, strerror(errno)));
1370                 exit(10);
1371         }
1372
1373
1374         printf("Reclock file : %s\n", reclock);
1375         for (i=0; i<nodemap->num; i++) {
1376                 int count;
1377
1378                 count = ctdb_read_pnn_lock(fd, nodemap->nodes[i].pnn);
1379
1380                 printf("pnn:%d %-16s", nodemap->nodes[i].pnn,
1381                        inet_ntoa(nodemap->nodes[i].sin.sin_addr));
1382                 if (count == -1) {
1383                         printf(" NOT ACTIVE\n");
1384                 } else {
1385                         printf(" ACTIVE with %d connections\n", count);
1386                 }
1387         }
1388
1389         close(fd);
1390         return 0;
1391 }
1392
1393
1394 /*
1395   check if the local node is recmaster or not
1396   it will return 1 if this node is the recmaster and 0 if it is not
1397   or if the local ctdb daemon could not be contacted
1398  */
1399 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1400 {
1401         uint32_t mypnn, recmaster;
1402         int ret;
1403
1404         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1405         if (mypnn == -1) {
1406                 printf("Failed to get pnn of node\n");
1407                 return 1;
1408         }
1409
1410         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1411         if (ret != 0) {
1412                 printf("Failed to get the recmaster\n");
1413                 return 1;
1414         }
1415
1416         if (recmaster != mypnn) {
1417                 printf("this node is not the recmaster\n");
1418                 return 1;
1419         }
1420
1421         printf("this node is the recmaster\n");
1422         return 0;
1423 }
1424
1425 /*
1426   ping a node
1427  */
1428 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
1429 {
1430         int ret;
1431         struct timeval tv = timeval_current();
1432         ret = ctdb_ctrl_ping(ctdb, options.pnn);
1433         if (ret == -1) {
1434                 printf("Unable to get ping response from node %u\n", options.pnn);
1435         } else {
1436                 printf("response from %u time=%.6f sec  (%d clients)\n", 
1437                        options.pnn, timeval_elapsed(&tv), ret);
1438         }
1439         return 0;
1440 }
1441
1442
1443 /*
1444   get a tunable
1445  */
1446 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
1447 {
1448         const char *name;
1449         uint32_t value;
1450         int ret;
1451
1452         if (argc < 1) {
1453                 usage();
1454         }
1455
1456         name = argv[0];
1457         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
1458         if (ret == -1) {
1459                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
1460                 return -1;
1461         }
1462
1463         printf("%-19s = %u\n", name, value);
1464         return 0;
1465 }
1466
1467 /*
1468   set a tunable
1469  */
1470 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
1471 {
1472         const char *name;
1473         uint32_t value;
1474         int ret;
1475
1476         if (argc < 2) {
1477                 usage();
1478         }
1479
1480         name = argv[0];
1481         value = strtoul(argv[1], NULL, 0);
1482
1483         ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
1484         if (ret == -1) {
1485                 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
1486                 return -1;
1487         }
1488         return 0;
1489 }
1490
1491 /*
1492   list all tunables
1493  */
1494 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
1495 {
1496         uint32_t count;
1497         const char **list;
1498         int ret, i;
1499
1500         ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
1501         if (ret == -1) {
1502                 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
1503                 return -1;
1504         }
1505
1506         for (i=0;i<count;i++) {
1507                 control_getvar(ctdb, 1, &list[i]);
1508         }
1509
1510         talloc_free(list);
1511         
1512         return 0;
1513 }
1514
1515 static struct {
1516         int32_t level;
1517         const char *description;
1518 } debug_levels[] = {
1519         {DEBUG_EMERG,   "EMERG"},
1520         {DEBUG_ALERT,   "ALERT"},
1521         {DEBUG_CRIT,    "CRIT"},
1522         {DEBUG_ERR,     "ERR"},
1523         {DEBUG_WARNING, "WARNING"},
1524         {DEBUG_NOTICE,  "NOTICE"},
1525         {DEBUG_INFO,    "INFO"},
1526         {DEBUG_DEBUG,   "DEBUG"}
1527 };
1528
1529 static const char *get_debug_by_level(int32_t level)
1530 {
1531         int i;
1532
1533         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1534                 if (debug_levels[i].level == level) {
1535                         return debug_levels[i].description;
1536                 }
1537         }
1538         return "Unknown";
1539 }
1540
1541 static int32_t get_debug_by_desc(const char *desc)
1542 {
1543         int i;
1544
1545         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1546                 if (!strcmp(debug_levels[i].description, desc)) {
1547                         return debug_levels[i].level;
1548                 }
1549         }
1550         return DEBUG_ERR;
1551 }
1552
1553 /*
1554   display debug level on a node
1555  */
1556 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1557 {
1558         int ret;
1559         int32_t level;
1560
1561         ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1562         if (ret != 0) {
1563                 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
1564                 return ret;
1565         } else {
1566                 if (options.machinereadable){
1567                         printf(":Name:Level:\n");
1568                         printf(":%s:%d:\n",get_debug_by_level(level),level);
1569                 } else {
1570                         printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
1571                 }
1572         }
1573         return 0;
1574 }
1575
1576
1577 /*
1578   set debug level on a node or all nodes
1579  */
1580 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1581 {
1582         int ret;
1583         uint32_t level;
1584
1585         if (argc < 1) {
1586                 usage();
1587         }
1588
1589         if (isalpha(argv[0][0])) { 
1590                 level = get_debug_by_desc(argv[0]);
1591         } else {
1592                 level = strtoul(argv[0], NULL, 0);
1593         }
1594
1595         ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1596         if (ret != 0) {
1597                 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1598         }
1599         return 0;
1600 }
1601
1602
1603 /*
1604   freeze a node
1605  */
1606 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1607 {
1608         int ret;
1609
1610         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1611         if (ret != 0) {
1612                 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
1613         }               
1614         return 0;
1615 }
1616
1617 /*
1618   thaw a node
1619  */
1620 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
1621 {
1622         int ret;
1623
1624         ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
1625         if (ret != 0) {
1626                 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
1627         }               
1628         return 0;
1629 }
1630
1631
1632 /*
1633   attach to a database
1634  */
1635 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
1636 {
1637         const char *db_name;
1638         struct ctdb_db_context *ctdb_db;
1639
1640         if (argc < 1) {
1641                 usage();
1642         }
1643         db_name = argv[0];
1644
1645         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
1646         if (ctdb_db == NULL) {
1647                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1648                 return -1;
1649         }
1650
1651         return 0;
1652 }
1653
1654 /*
1655   dump memory usage
1656  */
1657 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
1658 {
1659         TDB_DATA data;
1660         int ret;
1661         int32_t res;
1662         char *errmsg;
1663         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1664
1665         if (argc != 1) {
1666                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
1667                 return -1;
1668         }
1669
1670         data.dptr = (unsigned char *)discard_const(argv[0]);
1671         data.dsize = strlen((char *)data.dptr) + 1;
1672
1673         DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
1674
1675         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
1676                            0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
1677         if (ret != 0 || res != 0) {
1678                 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
1679                 talloc_free(tmp_ctx);
1680                 return -1;
1681         }
1682         talloc_free(tmp_ctx);
1683         return 0;
1684 }
1685
1686 /*
1687   dump memory usage
1688  */
1689 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1690 {
1691         TDB_DATA data;
1692         int ret;
1693         int32_t res;
1694         char *errmsg;
1695         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1696         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
1697                            0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
1698         if (ret != 0 || res != 0) {
1699                 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
1700                 talloc_free(tmp_ctx);
1701                 return -1;
1702         }
1703         write(1, data.dptr, data.dsize);
1704         talloc_free(tmp_ctx);
1705         return 0;
1706 }
1707
1708 /*
1709   handler for memory dumps
1710 */
1711 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid, 
1712                              TDB_DATA data, void *private_data)
1713 {
1714         write(1, data.dptr, data.dsize);
1715         exit(0);
1716 }
1717
1718 /*
1719   dump memory usage on the recovery daemon
1720  */
1721 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1722 {
1723         int ret;
1724         TDB_DATA data;
1725         struct rd_memdump_reply rd;
1726
1727         rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1728         if (rd.pnn == -1) {
1729                 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1730                 return -1;
1731         }
1732         rd.srvid = getpid();
1733
1734         /* register a message port for receiveing the reply so that we
1735            can receive the reply
1736         */
1737         ctdb_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
1738
1739
1740         data.dptr = (uint8_t *)&rd;
1741         data.dsize = sizeof(rd);
1742
1743         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
1744         if (ret != 0) {
1745                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
1746                 return -1;
1747         }
1748
1749         /* this loop will terminate when we have received the reply */
1750         while (1) {     
1751                 event_loop_once(ctdb->ev);
1752         }
1753
1754         return 0;
1755 }
1756
1757 /*
1758   list all nodes in the cluster
1759  */
1760 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
1761 {
1762         int i, ret;
1763         struct ctdb_node_map *nodemap=NULL;
1764
1765         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1766         if (ret != 0) {
1767                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1768                 return ret;
1769         }
1770
1771         for(i=0;i<nodemap->num;i++){
1772                 printf("%s\n", inet_ntoa(nodemap->nodes[i].sin.sin_addr));
1773         }
1774
1775         return 0;
1776 }
1777
1778 /*
1779   reload the nodes file on the local node
1780  */
1781 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
1782 {
1783         int i, ret;
1784         int mypnn;
1785         struct ctdb_node_map *nodemap=NULL;
1786
1787         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1788         if (mypnn == -1) {
1789                 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
1790                 return -1;
1791         }
1792
1793         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
1794         if (ret != 0) {
1795                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1796                 return ret;
1797         }
1798
1799         /* reload the nodes file on all remote nodes */
1800         for (i=0;i<nodemap->num;i++) {
1801                 if (nodemap->nodes[i].pnn == mypnn) {
1802                         continue;
1803                 }
1804                 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
1805                 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
1806                         nodemap->nodes[i].pnn);
1807                 if (ret != 0) {
1808                         DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
1809                 }
1810         }
1811
1812         /* reload the nodes file on the local node */
1813         DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
1814         ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
1815         if (ret != 0) {
1816                 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
1817         }
1818
1819         return 0;
1820 }
1821
1822
1823 static const struct {
1824         const char *name;
1825         int (*fn)(struct ctdb_context *, int, const char **);
1826         bool auto_all;
1827         const char *msg;
1828         const char *args;
1829 } ctdb_commands[] = {
1830 #ifdef CTDB_VERS
1831         { "version",         control_version,           true,  "show version of ctdb" },
1832 #endif
1833         { "status",          control_status,            true,  "show node status" },
1834         { "uptime",          control_uptime,            true,  "show node uptime" },
1835         { "ping",            control_ping,              true,  "ping all nodes" },
1836         { "getvar",          control_getvar,            true,  "get a tunable variable",               "<name>"},
1837         { "setvar",          control_setvar,            true,  "set a tunable variable",               "<name> <value>"},
1838         { "listvars",        control_listvars,          true,  "list tunable variables"},
1839         { "statistics",      control_statistics,        false, "show statistics" },
1840         { "statisticsreset", control_statistics_reset,  true,  "reset statistics"},
1841         { "ip",              control_ip,                false,  "show which public ip's that ctdb manages" },
1842         { "process-exists",  control_process_exists,    true,  "check if a process exists on a node",  "<pid>"},
1843         { "getdbmap",        control_getdbmap,          true,  "show the database map" },
1844         { "catdb",           control_catdb,             true,  "dump a database" ,                     "<dbname>"},
1845         { "getmonmode",      control_getmonmode,        true,  "show monitoring mode" },
1846         { "getcapabilities", control_getcapabilities,   true,  "show node capabilities" },
1847         { "disablemonitor",      control_disable_monmode,        true,  "set monitoring mode to DISABLE" },
1848         { "enablemonitor",      control_enable_monmode,        true,  "set monitoring mode to ACTIVE" },
1849         { "setdebug",        control_setdebug,          true,  "set debug level",                      "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
1850         { "getdebug",        control_getdebug,          true,  "get debug level" },
1851         { "attach",          control_attach,            true,  "attach to a database",                 "<dbname>" },
1852         { "dumpmemory",      control_dumpmemory,        true,  "dump memory map to stdout" },
1853         { "rddumpmemory",    control_rddumpmemory,      true,  "dump memory map from the recovery daemon to stdout" },
1854         { "getpid",          control_getpid,            true,  "get ctdbd process ID" },
1855         { "disable",         control_disable,           true,  "disable a nodes public IP" },
1856         { "enable",          control_enable,            true,  "enable a nodes public IP" },
1857         { "ban",             control_ban,               true,  "ban a node from the cluster",          "<bantime|0>"},
1858         { "unban",           control_unban,             true,  "unban a node from the cluster" },
1859         { "shutdown",        control_shutdown,          true,  "shutdown ctdbd" },
1860         { "recover",         control_recover,           true,  "force recovery" },
1861         { "freeze",          control_freeze,            true,  "freeze all databases" },
1862         { "thaw",            control_thaw,              true,  "thaw all databases" },
1863         { "isnotrecmaster",  control_isnotrecmaster,    false,  "check if the local node is recmaster or not" },
1864         { "killtcp",         kill_tcp,                  false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
1865         { "gratiousarp",     control_gratious_arp,      false, "send a gratious arp", "<ip> <interface>" },
1866         { "tickle",          tickle_tcp,                false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
1867         { "gettickles",      control_get_tickles,       false, "get the list of tickles registered for this ip", "<ip>" },
1868
1869         { "regsrvid",        regsrvid,                  false, "register a server id", "<pnn> <type> <id>" },
1870         { "unregsrvid",      unregsrvid,                false, "unregister a server id", "<pnn> <type> <id>" },
1871         { "chksrvid",        chksrvid,                  false, "check if a server id exists", "<pnn> <type> <id>" },
1872         { "getsrvids",       getsrvids,                 false, "get a list of all server ids"},
1873         { "vacuum",          ctdb_vacuum,               false, "vacuum the databases of empty records", "[max_records]"},
1874         { "repack",          ctdb_repack,               false, "repack all databases", "[max_freelist]"},
1875         { "listnodes",       control_listnodes,         false, "list all nodes in the cluster"},
1876         { "reloadnodes",     control_reload_nodes_file,         false, "reload the nodes file and restart the transport on all nodes"},
1877         { "getreclock",      control_getreclock,        false,  "get the path to the reclock file" },
1878         { "moveip",          control_moveip,            false, "move/failover an ip address to another node", "<ip> <node>"},
1879         { "addip",           control_addip,             true, "add a ip address to a node", "<ip/mask> <iface>"},
1880         { "delip",           control_delip,             false, "delete an ip address from a node", "<ip>"},
1881         { "eventscript",     control_eventscript,       true, "run the eventscript with the given parameters on a node", "<arguments>"},
1882 };
1883
1884 /*
1885   show usage message
1886  */
1887 static void usage(void)
1888 {
1889         int i;
1890         printf(
1891 "Usage: ctdb [options] <control>\n" \
1892 "Options:\n" \
1893 "   -n <node>          choose node number, or 'all' (defaults to local node)\n"
1894 "   -Y                 generate machinereadable output\n"
1895 "   -t <timelimit>     set timelimit for control in seconds (default %u)\n", options.timelimit);
1896         printf("Controls:\n");
1897         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1898                 printf("  %-15s %-27s  %s\n", 
1899                        ctdb_commands[i].name, 
1900                        ctdb_commands[i].args?ctdb_commands[i].args:"",
1901                        ctdb_commands[i].msg);
1902         }
1903         exit(1);
1904 }
1905
1906
1907 static void ctdb_alarm(int sig)
1908 {
1909         printf("Maximum runtime exceeded - exiting\n");
1910         _exit(0);
1911 }
1912
1913 /*
1914   main program
1915 */
1916 int main(int argc, const char *argv[])
1917 {
1918         struct ctdb_context *ctdb;
1919         char *nodestring = NULL;
1920         struct poptOption popt_options[] = {
1921                 POPT_AUTOHELP
1922                 POPT_CTDB_CMDLINE
1923                 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
1924                 { "node",      'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
1925                 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
1926                 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
1927                 POPT_TABLEEND
1928         };
1929         int opt;
1930         const char **extra_argv;
1931         int extra_argc = 0;
1932         int ret=-1, i;
1933         poptContext pc;
1934         struct event_context *ev;
1935         const char *control;
1936
1937         setlinebuf(stdout);
1938         
1939         /* set some defaults */
1940         options.maxruntime = 0;
1941         options.timelimit = 3;
1942         options.pnn = CTDB_CURRENT_NODE;
1943
1944         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
1945
1946         while ((opt = poptGetNextOpt(pc)) != -1) {
1947                 switch (opt) {
1948                 default:
1949                         DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
1950                                 poptBadOption(pc, 0), poptStrerror(opt)));
1951                         exit(1);
1952                 }
1953         }
1954
1955         /* setup the remaining options for the main program to use */
1956         extra_argv = poptGetArgs(pc);
1957         if (extra_argv) {
1958                 extra_argv++;
1959                 while (extra_argv[extra_argc]) extra_argc++;
1960         }
1961
1962         if (extra_argc < 1) {
1963                 usage();
1964         }
1965
1966         if (options.maxruntime != 0) {
1967                 signal(SIGALRM, ctdb_alarm);
1968                 alarm(options.maxruntime);
1969         }
1970
1971         /* setup the node number to contact */
1972         if (nodestring != NULL) {
1973                 if (strcmp(nodestring, "all") == 0) {
1974                         options.pnn = CTDB_BROADCAST_ALL;
1975                 } else {
1976                         options.pnn = strtoul(nodestring, NULL, 0);
1977                 }
1978         }
1979
1980         control = extra_argv[0];
1981
1982         ev = event_context_init(NULL);
1983
1984         /* initialise ctdb */
1985         ctdb = ctdb_cmdline_client(ev);
1986         if (ctdb == NULL) {
1987                 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
1988                 exit(1);
1989         }
1990
1991         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1992                 if (strcmp(control, ctdb_commands[i].name) == 0) {
1993                         int j;
1994
1995                         if (options.pnn == CTDB_CURRENT_NODE) {
1996                                 int pnn;
1997                                 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);         
1998                                 if (pnn == -1) {
1999                                         return -1;
2000                                 }
2001                                 options.pnn = pnn;
2002                         }
2003
2004                         if (ctdb_commands[i].auto_all && 
2005                             options.pnn == CTDB_BROADCAST_ALL) {
2006                                 uint32_t *nodes;
2007                                 uint32_t num_nodes;
2008                                 ret = 0;
2009
2010                                 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
2011                                 CTDB_NO_MEMORY(ctdb, nodes);
2012         
2013                                 for (j=0;j<num_nodes;j++) {
2014                                         options.pnn = nodes[j];
2015                                         ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2016                                 }
2017                                 talloc_free(nodes);
2018                         } else {
2019                                 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2020                         }
2021                         break;
2022                 }
2023         }
2024
2025         if (i == ARRAY_SIZE(ctdb_commands)) {
2026                 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
2027                 exit(1);
2028         }
2029
2030         return ret;
2031 }