make sure we return an errorcode when the ctdb command has hung and is timeodout...
[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 #include "db_wrap.h"
33
34 #define ERR_TIMEOUT     20
35
36 static void usage(void);
37
38 static struct {
39         int timelimit;
40         uint32_t pnn;
41         int machinereadable;
42         int maxruntime;
43 } options;
44
45 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
46
47 #ifdef CTDB_VERS
48 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
49 {
50 #define STR(x) #x
51 #define XSTR(x) STR(x)
52         printf("CTDB version: %s\n", XSTR(CTDB_VERS));
53         return 0;
54 }
55 #endif
56
57
58 /*
59  check if a database exists
60 */
61 static int db_exists(struct ctdb_context *ctdb, const char *db_name)
62 {
63         int i, ret;
64         struct ctdb_dbid_map *dbmap=NULL;
65
66         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
67         if (ret != 0) {
68                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
69                 return -1;
70         }
71
72         for(i=0;i<dbmap->num;i++){
73                 const char *name;
74
75                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
76                 if (!strcmp(name, db_name)) {
77                         return 0;
78                 }
79         }
80
81         return -1;
82 }
83
84 /*
85   see if a process exists
86  */
87 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
88 {
89         uint32_t pnn, pid;
90         int ret;
91         if (argc < 1) {
92                 usage();
93         }
94
95         if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
96                 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
97                 return -1;
98         }
99
100         ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
101         if (ret == 0) {
102                 printf("%u:%u exists\n", pnn, pid);
103         } else {
104                 printf("%u:%u does not exist\n", pnn, pid);
105         }
106         return ret;
107 }
108
109 /*
110   display statistics structure
111  */
112 static void show_statistics(struct ctdb_statistics *s)
113 {
114         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
115         int i;
116         const char *prefix=NULL;
117         int preflen=0;
118         const struct {
119                 const char *name;
120                 uint32_t offset;
121         } fields[] = {
122 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
123                 STATISTICS_FIELD(num_clients),
124                 STATISTICS_FIELD(frozen),
125                 STATISTICS_FIELD(recovering),
126                 STATISTICS_FIELD(client_packets_sent),
127                 STATISTICS_FIELD(client_packets_recv),
128                 STATISTICS_FIELD(node_packets_sent),
129                 STATISTICS_FIELD(node_packets_recv),
130                 STATISTICS_FIELD(keepalive_packets_sent),
131                 STATISTICS_FIELD(keepalive_packets_recv),
132                 STATISTICS_FIELD(node.req_call),
133                 STATISTICS_FIELD(node.reply_call),
134                 STATISTICS_FIELD(node.req_dmaster),
135                 STATISTICS_FIELD(node.reply_dmaster),
136                 STATISTICS_FIELD(node.reply_error),
137                 STATISTICS_FIELD(node.req_message),
138                 STATISTICS_FIELD(node.req_control),
139                 STATISTICS_FIELD(node.reply_control),
140                 STATISTICS_FIELD(client.req_call),
141                 STATISTICS_FIELD(client.req_message),
142                 STATISTICS_FIELD(client.req_control),
143                 STATISTICS_FIELD(timeouts.call),
144                 STATISTICS_FIELD(timeouts.control),
145                 STATISTICS_FIELD(timeouts.traverse),
146                 STATISTICS_FIELD(total_calls),
147                 STATISTICS_FIELD(pending_calls),
148                 STATISTICS_FIELD(lockwait_calls),
149                 STATISTICS_FIELD(pending_lockwait_calls),
150                 STATISTICS_FIELD(childwrite_calls),
151                 STATISTICS_FIELD(pending_childwrite_calls),
152                 STATISTICS_FIELD(memory_used),
153                 STATISTICS_FIELD(max_hop_count),
154         };
155         printf("CTDB version %u\n", CTDB_VERSION);
156         for (i=0;i<ARRAY_SIZE(fields);i++) {
157                 if (strchr(fields[i].name, '.')) {
158                         preflen = strcspn(fields[i].name, ".")+1;
159                         if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
160                                 prefix = fields[i].name;
161                                 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
162                         }
163                 } else {
164                         preflen = 0;
165                 }
166                 printf(" %*s%-22s%*s%10u\n", 
167                        preflen?4:0, "",
168                        fields[i].name+preflen, 
169                        preflen?0:4, "",
170                        *(uint32_t *)(fields[i].offset+(uint8_t *)s));
171         }
172         printf(" %-30s     %.6f sec\n", "max_call_latency", s->max_call_latency);
173         printf(" %-30s     %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
174         printf(" %-30s     %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
175         talloc_free(tmp_ctx);
176 }
177
178 /*
179   display remote ctdb statistics combined from all nodes
180  */
181 static int control_statistics_all(struct ctdb_context *ctdb)
182 {
183         int ret, i;
184         struct ctdb_statistics statistics;
185         uint32_t *nodes;
186         uint32_t num_nodes;
187
188         nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
189         CTDB_NO_MEMORY(ctdb, nodes);
190         
191         ZERO_STRUCT(statistics);
192
193         for (i=0;i<num_nodes;i++) {
194                 struct ctdb_statistics s1;
195                 int j;
196                 uint32_t *v1 = (uint32_t *)&s1;
197                 uint32_t *v2 = (uint32_t *)&statistics;
198                 uint32_t num_ints = 
199                         offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
200                 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
201                 if (ret != 0) {
202                         DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
203                         return ret;
204                 }
205                 for (j=0;j<num_ints;j++) {
206                         v2[j] += v1[j];
207                 }
208                 statistics.max_hop_count = 
209                         MAX(statistics.max_hop_count, s1.max_hop_count);
210                 statistics.max_call_latency = 
211                         MAX(statistics.max_call_latency, s1.max_call_latency);
212                 statistics.max_lockwait_latency = 
213                         MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
214         }
215         talloc_free(nodes);
216         printf("Gathered statistics for %u nodes\n", num_nodes);
217         show_statistics(&statistics);
218         return 0;
219 }
220
221 /*
222   display remote ctdb statistics
223  */
224 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
225 {
226         int ret;
227         struct ctdb_statistics statistics;
228
229         if (options.pnn == CTDB_BROADCAST_ALL) {
230                 return control_statistics_all(ctdb);
231         }
232
233         ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
234         if (ret != 0) {
235                 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
236                 return ret;
237         }
238         show_statistics(&statistics);
239         return 0;
240 }
241
242
243 /*
244   reset remote ctdb statistics
245  */
246 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
247 {
248         int ret;
249
250         ret = ctdb_statistics_reset(ctdb, options.pnn);
251         if (ret != 0) {
252                 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
253                 return ret;
254         }
255         return 0;
256 }
257
258
259 /*
260   display uptime of remote node
261  */
262 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
263 {
264         int ret;
265         int mypnn;
266         struct ctdb_uptime *uptime = NULL;
267         int tmp, days, hours, minutes, seconds;
268
269         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
270         if (mypnn == -1) {
271                 return -1;
272         }
273
274         ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
275         if (ret != 0) {
276                 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
277                 return ret;
278         }
279
280         if (options.machinereadable){
281                 printf(":Current Node Time:Ctdb Start Time:Last Recovery Time:Last Recovery Duration:\n");
282                 printf(":%u:%u:%u:%lf\n",
283                         (unsigned int)uptime->current_time.tv_sec,
284                         (unsigned int)uptime->ctdbd_start_time.tv_sec,
285                         (unsigned int)uptime->last_recovery_finished.tv_sec,
286                         timeval_delta(&uptime->last_recovery_finished,
287                                       &uptime->last_recovery_started)
288                 );
289                 return 0;
290         }
291
292         printf("Current time of node  : %s", ctime(&uptime->current_time.tv_sec));
293
294         tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
295         seconds = tmp%60;
296         tmp    /= 60;
297         minutes = tmp%60;
298         tmp    /= 60;
299         hours   = tmp%24;
300         tmp    /= 24;
301         days    = tmp;
302         printf("Ctdbd start time      : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
303
304         tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
305         seconds = tmp%60;
306         tmp    /= 60;
307         minutes = tmp%60;
308         tmp    /= 60;
309         hours   = tmp%24;
310         tmp    /= 24;
311         days    = tmp;
312         printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
313         
314         printf("Duration of last recovery : %lf seconds\n",
315                 timeval_delta(&uptime->last_recovery_finished,
316                               &uptime->last_recovery_started));
317
318         return 0;
319 }
320
321 /*
322   show the PNN of the current node
323  */
324 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
325 {
326         int mypnn;
327
328         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
329         if (mypnn == -1) {
330                 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node."));
331                 return -1;
332         }
333
334         printf("PNN:%d\n", mypnn);
335         return 0;
336 }
337
338 /*
339   display remote ctdb status
340  */
341 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
342 {
343         int i, ret;
344         struct ctdb_vnn_map *vnnmap=NULL;
345         struct ctdb_node_map *nodemap=NULL;
346         uint32_t recmode, recmaster;
347         int mypnn;
348
349         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
350         if (mypnn == -1) {
351                 return -1;
352         }
353
354         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
355         if (ret != 0) {
356                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
357                 return ret;
358         }
359
360         if(options.machinereadable){
361                 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
362                 for(i=0;i<nodemap->num;i++){
363                         printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
364                                 ctdb_addr_to_str(&nodemap->nodes[i].addr),
365                                !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
366                                !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
367                                !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
368                                !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
369                 }
370                 return 0;
371         }
372
373         printf("Number of nodes:%d\n", nodemap->num);
374         for(i=0;i<nodemap->num;i++){
375                 static const struct {
376                         uint32_t flag;
377                         const char *name;
378                 } flag_names[] = {
379                         { NODE_FLAGS_DISCONNECTED,          "DISCONNECTED" },
380                         { NODE_FLAGS_PERMANENTLY_DISABLED,  "DISABLED" },
381                         { NODE_FLAGS_BANNED,                "BANNED" },
382                         { NODE_FLAGS_UNHEALTHY,             "UNHEALTHY" },
383                 };
384                 char *flags_str = NULL;
385                 int j;
386                 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
387                         if (nodemap->nodes[i].flags & flag_names[j].flag) {
388                                 if (flags_str == NULL) {
389                                         flags_str = talloc_strdup(ctdb, flag_names[j].name);
390                                 } else {
391                                         flags_str = talloc_asprintf_append(flags_str, "|%s",
392                                                                            flag_names[j].name);
393                                 }
394                                 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
395                         }
396                 }
397                 if (flags_str == NULL) {
398                         flags_str = talloc_strdup(ctdb, "OK");
399                         CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
400                 }
401                 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
402                        ctdb_addr_to_str(&nodemap->nodes[i].addr),
403                        flags_str,
404                        nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
405                 talloc_free(flags_str);
406         }
407
408         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
409         if (ret != 0) {
410                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
411                 return ret;
412         }
413         if (vnnmap->generation == INVALID_GENERATION) {
414                 printf("Generation:INVALID\n");
415         } else {
416                 printf("Generation:%d\n",vnnmap->generation);
417         }
418         printf("Size:%d\n",vnnmap->size);
419         for(i=0;i<vnnmap->size;i++){
420                 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
421         }
422
423         ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
424         if (ret != 0) {
425                 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
426                 return ret;
427         }
428         printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
429
430         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
431         if (ret != 0) {
432                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
433                 return ret;
434         }
435         printf("Recovery master:%d\n",recmaster);
436
437         return 0;
438 }
439
440 /*
441   display the pnn of the recovery master
442  */
443 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
444 {
445         int ret;
446         uint32_t recmaster;
447
448         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
449         if (ret != 0) {
450                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
451                 return ret;
452         }
453         printf("%d\n",recmaster);
454
455         return 0;
456 }
457
458 /*
459   get a list of all tickles for this pnn
460  */
461 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
462 {
463         struct ctdb_control_tcp_tickle_list *list;
464         ctdb_sock_addr addr;
465         int i, ret;
466
467         if (argc < 1) {
468                 usage();
469         }
470
471         if (parse_ip(argv[0], &addr) == 0) {
472                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
473                 return -1;
474         }
475
476         ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
477         if (ret == -1) {
478                 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
479                 return -1;
480         }
481
482         printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
483         printf("Num tickles:%u\n", list->tickles.num);
484         for (i=0;i<list->tickles.num;i++) {
485                 printf("SRC: %s:%u   ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
486                 printf("DST: %s:%u\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
487         }
488
489         talloc_free(list);
490         
491         return 0;
492 }
493
494 /* send a release ip to all nodes */
495 static int control_send_release(struct ctdb_context *ctdb, uint32_t pnn,
496 ctdb_sock_addr *addr)
497 {
498         int ret;
499         struct ctdb_public_ip pip;
500         TDB_DATA data;
501         struct ctdb_node_map *nodemap=NULL;
502
503         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
504         if (ret != 0) {
505                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
506                 return ret;
507         }
508
509         /* send a moveip message to the recovery master */
510         pip.pnn    = pnn;
511         pip.addr   = *addr;
512         data.dsize = sizeof(pip);
513         data.dptr  = (unsigned char *)&pip;
514
515
516         /* send release ip to all nodes */
517         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
518                         list_of_active_nodes(ctdb, nodemap, ctdb, true),
519                         TIMELIMIT(), false, data,
520                         NULL, NULL, NULL) != 0) {
521                 DEBUG(DEBUG_ERR, (__location__ " Unable to send 'ReleaseIP' to all nodes.\n"));
522                 return -1;
523         }
524
525         return 0;
526 }
527
528 /*
529   move/failover an ip address to a specific node
530  */
531 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
532 {
533         uint32_t pnn;
534         ctdb_sock_addr addr;
535         uint32_t value;
536         struct ctdb_all_public_ips *ips;
537         int i, ret;
538
539         if (argc < 2) {
540                 usage();
541         }
542
543         if (parse_ip(argv[0], &addr) == 0) {
544                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
545                 return -1;
546         }
547
548
549         if (sscanf(argv[1], "%u", &pnn) != 1) {
550                 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
551                 return -1;
552         }
553
554         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "DeterministicIPs", &value);
555         if (ret == -1) {
556                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'DeterministicIPs' from local node\n"));
557                 return -1;
558         }
559         if (value != 0) {
560                 DEBUG(DEBUG_ERR, ("The tunable 'DeterministicIPs' is set. You can only move ip addresses when this feature is disabled\n"));
561                 return -1;
562         }
563
564         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "NoIPFailback", &value);
565         if (ret == -1) {
566                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'NoIPFailback' from local node\n"));
567                 return -1;
568         }
569         if (value == 0) {
570                 DEBUG(DEBUG_ERR, ("The tunable 'NoIPFailback' is NOT set. You can only move ip addresses when this feature is enabled\n"));
571                 return -1;
572         }
573
574         /* read the public ip list from the node */
575         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
576         if (ret != 0) {
577                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
578                 return -1;
579         }
580
581         for (i=0;i<ips->num;i++) {
582                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
583                         break;
584                 }
585         }
586         if (i==ips->num) {
587                 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
588                         pnn, ctdb_addr_to_str(&addr)));
589                 return -1;
590         }
591         if (ips->ips[i].pnn == pnn) {
592                 DEBUG(DEBUG_ERR, ("Host %u is already hosting '%s'\n",
593                         pnn, ctdb_addr_to_str(&ips->ips[i].addr)));
594                 return -1;
595         }
596
597         ret = control_send_release(ctdb, pnn, &ips->ips[i].addr);
598         if (ret != 0) {
599                 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));;
600                 return -1;
601         }
602
603         return 0;
604 }
605
606 void getips_store_callback(void *param, void *data)
607 {
608         struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
609         struct ctdb_all_public_ips *ips = param;
610         int i;
611
612         i = ips->num++;
613         ips->ips[i].pnn  = node_ip->pnn;
614         ips->ips[i].addr = node_ip->addr;
615 }
616
617 void getips_count_callback(void *param, void *data)
618 {
619         uint32_t *count = param;
620
621         (*count)++;
622 }
623
624 #define IP_KEYLEN       4
625 static uint32_t *ip_key(ctdb_sock_addr *ip)
626 {
627         static uint32_t key[IP_KEYLEN];
628
629         bzero(key, sizeof(key));
630
631         switch (ip->sa.sa_family) {
632         case AF_INET:
633                 key[0]  = ip->ip.sin_addr.s_addr;
634                 break;
635         case AF_INET6:
636                 key[0]  = ip->ip6.sin6_addr.s6_addr32[3];
637                 key[1]  = ip->ip6.sin6_addr.s6_addr32[2];
638                 key[2]  = ip->ip6.sin6_addr.s6_addr32[1];
639                 key[3]  = ip->ip6.sin6_addr.s6_addr32[0];
640                 break;
641         default:
642                 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
643                 return key;
644         }
645
646         return key;
647 }
648
649 static void *add_ip_callback(void *parm, void *data)
650 {
651         return parm;
652 }
653
654 static int
655 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
656 {
657         struct ctdb_all_public_ips *tmp_ips;
658         struct ctdb_node_map *nodemap=NULL;
659         trbt_tree_t *ip_tree;
660         int i, j, len, ret;
661         uint32_t count;
662
663         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
664         if (ret != 0) {
665                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
666                 return ret;
667         }
668
669         ip_tree = trbt_create(tmp_ctx, 0);
670
671         for(i=0;i<nodemap->num;i++){
672                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
673                         continue;
674                 }
675
676                 /* read the public ip list from this node */
677                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
678                 if (ret != 0) {
679                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
680                         return -1;
681                 }
682         
683                 for (j=0; j<tmp_ips->num;j++) {
684                         struct ctdb_public_ip *node_ip;
685
686                         node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
687                         node_ip->pnn  = tmp_ips->ips[j].pnn;
688                         node_ip->addr = tmp_ips->ips[j].addr;
689
690                         trbt_insertarray32_callback(ip_tree,
691                                 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
692                                 add_ip_callback,
693                                 node_ip);
694                 }
695                 talloc_free(tmp_ips);
696         }
697
698         /* traverse */
699         count = 0;
700         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
701
702         len = offsetof(struct ctdb_all_public_ips, ips) + 
703                 count*sizeof(struct ctdb_public_ip);
704         tmp_ips = talloc_zero_size(tmp_ctx, len);
705         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
706
707         *ips = tmp_ips;
708
709         return 0;
710 }
711
712
713 /* 
714  * scans all other nodes and returns a pnn for another node that can host this 
715  * ip address or -1
716  */
717 static int
718 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
719 {
720         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
721         struct ctdb_all_public_ips *ips;
722         struct ctdb_node_map *nodemap=NULL;
723         int i, j, ret;
724
725         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
726         if (ret != 0) {
727                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
728                 talloc_free(tmp_ctx);
729                 return ret;
730         }
731
732         for(i=0;i<nodemap->num;i++){
733                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
734                         continue;
735                 }
736                 if (nodemap->nodes[i].pnn == options.pnn) {
737                         continue;
738                 }
739
740                 /* read the public ip list from this node */
741                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
742                 if (ret != 0) {
743                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
744                         return -1;
745                 }
746
747                 for (j=0;j<ips->num;j++) {
748                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
749                                 talloc_free(tmp_ctx);
750                                 return nodemap->nodes[i].pnn;
751                         }
752                 }
753                 talloc_free(ips);
754         }
755
756         talloc_free(tmp_ctx);
757         return -1;
758 }
759
760 /*
761   add a public ip address to a node
762  */
763 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
764 {
765         int i, ret;
766         int len;
767         unsigned mask;
768         ctdb_sock_addr addr;
769         struct ctdb_control_ip_iface *pub;
770         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
771         struct ctdb_all_public_ips *ips;
772
773         if (argc != 2) {
774                 talloc_free(tmp_ctx);
775                 usage();
776         }
777
778         if (!parse_ip_mask(argv[0], &addr, &mask)) {
779                 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
780                 talloc_free(tmp_ctx);
781                 return -1;
782         }
783
784         ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
785         if (ret != 0) {
786                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
787                 talloc_free(tmp_ctx);
788                 return ret;
789         }
790
791
792         len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
793         pub = talloc_size(tmp_ctx, len); 
794         CTDB_NO_MEMORY(ctdb, pub);
795
796         pub->addr  = addr;
797         pub->mask  = mask;
798         pub->len   = strlen(argv[1])+1;
799         memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
800
801         ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
802         if (ret != 0) {
803                 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
804                 talloc_free(tmp_ctx);
805                 return ret;
806         }
807
808
809         /* check if some other node is already serving this ip, if not,
810          * we will claim it
811          */
812         for (i=0;i<ips->num;i++) {
813                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
814                         break;
815                 }
816         }
817         /* no one has this ip so we claim it */
818         if (i == ips->num) {
819                 ret = control_send_release(ctdb, options.pnn, &addr);
820         } else {
821                 ret = control_send_release(ctdb, ips->ips[i].pnn, &addr);
822         }
823
824         if (ret != 0) {
825                 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));
826                 return -1;
827         }
828
829         talloc_free(tmp_ctx);
830         return 0;
831 }
832
833 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
834
835 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
836 {
837         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
838         struct ctdb_node_map *nodemap=NULL;
839         struct ctdb_all_public_ips *ips;
840         int ret, i, j;
841
842         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
843         if (ret != 0) {
844                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
845                 return ret;
846         }
847
848         /* remove it from the nodes that are not hosting the ip currently */
849         for(i=0;i<nodemap->num;i++){
850                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
851                         continue;
852                 }
853                 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
854                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
855                         continue;
856                 }
857
858                 for (j=0;j<ips->num;j++) {
859                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
860                                 break;
861                         }
862                 }
863                 if (j==ips->num) {
864                         continue;
865                 }
866
867                 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
868                         continue;
869                 }
870
871                 options.pnn = nodemap->nodes[i].pnn;
872                 control_delip(ctdb, argc, argv);
873         }
874
875
876         /* remove it from every node (also the one hosting it) */
877         for(i=0;i<nodemap->num;i++){
878                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
879                         continue;
880                 }
881                 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
882                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
883                         continue;
884                 }
885
886                 for (j=0;j<ips->num;j++) {
887                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
888                                 break;
889                         }
890                 }
891                 if (j==ips->num) {
892                         continue;
893                 }
894
895                 options.pnn = nodemap->nodes[i].pnn;
896                 control_delip(ctdb, argc, argv);
897         }
898
899         talloc_free(tmp_ctx);
900         return 0;
901 }
902         
903 /*
904   delete a public ip address from a node
905  */
906 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
907 {
908         int i, ret;
909         ctdb_sock_addr addr;
910         struct ctdb_control_ip_iface pub;
911         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
912         struct ctdb_all_public_ips *ips;
913
914         if (argc != 1) {
915                 talloc_free(tmp_ctx);
916                 usage();
917         }
918
919         if (parse_ip(argv[0], &addr) == 0) {
920                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
921                 return -1;
922         }
923
924         if (options.pnn == CTDB_BROADCAST_ALL) {
925                 return control_delip_all(ctdb, argc, argv, &addr);
926         }
927
928         pub.addr  = addr;
929         pub.mask  = 0;
930         pub.len   = 0;
931
932         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
933         if (ret != 0) {
934                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
935                 talloc_free(tmp_ctx);
936                 return ret;
937         }
938         
939         for (i=0;i<ips->num;i++) {
940                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
941                         break;
942                 }
943         }
944
945         if (i==ips->num) {
946                 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
947                         ctdb_addr_to_str(&addr)));
948                 talloc_free(tmp_ctx);
949                 return -1;
950         }
951
952         if (ips->ips[i].pnn == options.pnn) {
953                 ret = find_other_host_for_public_ip(ctdb, &addr);
954                 if (ret != -1) {
955                         ret = control_send_release(ctdb, ret, &addr);
956                         if (ret != 0) {
957                                 DEBUG(DEBUG_ERR, ("Failed to migrate this ip to another node. Use moveip of recover to reassign this address to a node\n"));
958                         }
959                 }
960         }
961
962         ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
963         if (ret != 0) {
964                 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
965                 talloc_free(tmp_ctx);
966                 return ret;
967         }
968
969         talloc_free(tmp_ctx);
970         return 0;
971 }
972
973 /*
974   kill a tcp connection
975  */
976 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
977 {
978         int ret;
979         struct ctdb_control_killtcp killtcp;
980
981         if (argc < 2) {
982                 usage();
983         }
984
985         if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
986                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
987                 return -1;
988         }
989
990         if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
991                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
992                 return -1;
993         }
994
995         ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
996         if (ret != 0) {
997                 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
998                 return ret;
999         }
1000
1001         return 0;
1002 }
1003
1004
1005 /*
1006   send a gratious arp
1007  */
1008 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1009 {
1010         int ret;
1011         ctdb_sock_addr addr;
1012
1013         if (argc < 2) {
1014                 usage();
1015         }
1016
1017         if (!parse_ip(argv[0], &addr)) {
1018                 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1019                 return -1;
1020         }
1021
1022         ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1023         if (ret != 0) {
1024                 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1025                 return ret;
1026         }
1027
1028         return 0;
1029 }
1030
1031 /*
1032   register a server id
1033  */
1034 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1035 {
1036         int ret;
1037         struct ctdb_server_id server_id;
1038
1039         if (argc < 3) {
1040                 usage();
1041         }
1042
1043         server_id.pnn       = strtoul(argv[0], NULL, 0);
1044         server_id.type      = strtoul(argv[1], NULL, 0);
1045         server_id.server_id = strtoul(argv[2], NULL, 0);
1046
1047         ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1048         if (ret != 0) {
1049                 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1050                 return ret;
1051         }
1052         return -1;
1053 }
1054
1055 /*
1056   unregister a server id
1057  */
1058 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1059 {
1060         int ret;
1061         struct ctdb_server_id server_id;
1062
1063         if (argc < 3) {
1064                 usage();
1065         }
1066
1067         server_id.pnn       = strtoul(argv[0], NULL, 0);
1068         server_id.type      = strtoul(argv[1], NULL, 0);
1069         server_id.server_id = strtoul(argv[2], NULL, 0);
1070
1071         ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
1072         if (ret != 0) {
1073                 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
1074                 return ret;
1075         }
1076         return -1;
1077 }
1078
1079 /*
1080   check if a server id exists
1081  */
1082 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1083 {
1084         uint32_t status;
1085         int ret;
1086         struct ctdb_server_id server_id;
1087
1088         if (argc < 3) {
1089                 usage();
1090         }
1091
1092         server_id.pnn       = strtoul(argv[0], NULL, 0);
1093         server_id.type      = strtoul(argv[1], NULL, 0);
1094         server_id.server_id = strtoul(argv[2], NULL, 0);
1095
1096         ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
1097         if (ret != 0) {
1098                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
1099                 return ret;
1100         }
1101
1102         if (status) {
1103                 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
1104         } else {
1105                 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
1106         }
1107         return 0;
1108 }
1109
1110 /*
1111   get a list of all server ids that are registered on a node
1112  */
1113 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
1114 {
1115         int i, ret;
1116         struct ctdb_server_id_list *server_ids;
1117
1118         ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
1119         if (ret != 0) {
1120                 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
1121                 return ret;
1122         }
1123
1124         for (i=0; i<server_ids->num; i++) {
1125                 printf("Server id %d:%d:%d\n", 
1126                         server_ids->server_ids[i].pnn, 
1127                         server_ids->server_ids[i].type, 
1128                         server_ids->server_ids[i].server_id); 
1129         }
1130
1131         return -1;
1132 }
1133
1134 /*
1135   send a tcp tickle ack
1136  */
1137 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1138 {
1139         int ret;
1140         ctdb_sock_addr  src, dst;
1141
1142         if (argc < 2) {
1143                 usage();
1144         }
1145
1146         if (!parse_ip_port(argv[0], &src)) {
1147                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1148                 return -1;
1149         }
1150
1151         if (!parse_ip_port(argv[1], &dst)) {
1152                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1153                 return -1;
1154         }
1155
1156         ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
1157         if (ret==0) {
1158                 return 0;
1159         }
1160         DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
1161
1162         return -1;
1163 }
1164
1165
1166 /*
1167   display public ip status
1168  */
1169 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
1170 {
1171         int i, ret;
1172         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1173         struct ctdb_all_public_ips *ips;
1174
1175         if (options.pnn == CTDB_BROADCAST_ALL) {
1176                 /* read the list of public ips from all nodes */
1177                 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1178         } else {
1179                 /* read the public ip list from this node */
1180                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1181         }
1182         if (ret != 0) {
1183                 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
1184                 talloc_free(tmp_ctx);
1185                 return ret;
1186         }
1187
1188         if (options.machinereadable){
1189                 printf(":Public IP:Node:\n");
1190         } else {
1191                 if (options.pnn == CTDB_BROADCAST_ALL) {
1192                         printf("Public IPs on ALL nodes\n");
1193                 } else {
1194                         printf("Public IPs on node %u\n", options.pnn);
1195                 }
1196         }
1197
1198         for (i=1;i<=ips->num;i++) {
1199                 if (options.machinereadable){
1200                         printf(":%s:%d:\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1201                 } else {
1202                         printf("%s %d\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1203                 }
1204         }
1205
1206         talloc_free(tmp_ctx);
1207         return 0;
1208 }
1209
1210 /*
1211   display pid of a ctdb daemon
1212  */
1213 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
1214 {
1215         uint32_t pid;
1216         int ret;
1217
1218         ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
1219         if (ret != 0) {
1220                 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
1221                 return ret;
1222         }
1223         printf("Pid:%d\n", pid);
1224
1225         return 0;
1226 }
1227
1228 /*
1229   disable a remote node
1230  */
1231 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
1232 {
1233         int ret;
1234
1235         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
1236         if (ret != 0) {
1237                 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
1238                 return ret;
1239         }
1240
1241         return 0;
1242 }
1243
1244 /*
1245   enable a disabled remote node
1246  */
1247 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
1248 {
1249         int ret;
1250
1251         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
1252         if (ret != 0) {
1253                 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
1254                 return ret;
1255         }
1256
1257         return 0;
1258 }
1259
1260 static uint32_t get_generation(struct ctdb_context *ctdb)
1261 {
1262         struct ctdb_vnn_map *vnnmap=NULL;
1263         int ret;
1264
1265         /* wait until the recmaster is not in recovery mode */
1266         while (1) {
1267                 uint32_t recmode, recmaster;
1268                 
1269                 if (vnnmap != NULL) {
1270                         talloc_free(vnnmap);
1271                         vnnmap = NULL;
1272                 }
1273
1274                 /* get the recmaster */
1275                 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
1276                 if (ret != 0) {
1277                         DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1278                         exit(10);
1279                 }
1280
1281                 /* get recovery mode */
1282                 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
1283                 if (ret != 0) {
1284                         DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1285                         exit(10);
1286                 }
1287
1288                 /* get the current generation number */
1289                 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
1290                 if (ret != 0) {
1291                         DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
1292                         exit(10);
1293                 }
1294
1295                 if ((recmode == CTDB_RECOVERY_NORMAL)
1296                 &&  (vnnmap->generation != 1)){
1297                         return vnnmap->generation;
1298                 }
1299                 sleep(1);
1300         }
1301 }
1302
1303 /*
1304   ban a node from the cluster
1305  */
1306 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
1307 {
1308         int ret;
1309         struct ctdb_ban_info b;
1310         TDB_DATA data;
1311         uint32_t ban_time;
1312         struct ctdb_node_map *nodemap=NULL;
1313         uint32_t generation, next_generation;
1314
1315         if (argc < 1) {
1316                 usage();
1317         }
1318         
1319         /* record the current generation number */
1320         generation = get_generation(ctdb);
1321
1322
1323         /* verify the node exists */
1324         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
1325         if (ret != 0) {
1326                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1327                 return ret;
1328         }
1329         if (options.pnn >= nodemap->num) {
1330                 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", options.pnn));
1331                 return ret;
1332         }
1333
1334         /* verify we can access the node */
1335         ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1336         if (ret == -1) {
1337                 DEBUG(DEBUG_ERR,("Can not ban node. Node is not operational.\n"));
1338                 return -1;
1339         }
1340
1341         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
1342                 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
1343                 return -1;
1344         }
1345
1346         ban_time = strtoul(argv[0], NULL, 0);
1347
1348         b.pnn = options.pnn;
1349         b.ban_time = ban_time;
1350
1351         data.dptr = (uint8_t *)&b;
1352         data.dsize = sizeof(b);
1353
1354         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
1355         if (ret != 0) {
1356                 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
1357                 return -1;
1358         }
1359
1360         /* wait until we are in a new generation */
1361         while (1) {
1362                 next_generation = get_generation(ctdb);
1363                 if (next_generation != generation) {
1364                         return 0;
1365                 }
1366                 sleep(1);
1367         }
1368
1369         return 0;
1370 }
1371
1372
1373 /*
1374   unban a node from the cluster
1375  */
1376 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
1377 {
1378         int ret;
1379         TDB_DATA data;
1380         uint32_t generation, next_generation;
1381
1382         /* record the current generation number */
1383         generation = get_generation(ctdb);
1384
1385         /* verify we can access the node */
1386         ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1387         if (ret == -1) {
1388                 DEBUG(DEBUG_ERR,("Can not unban node. Node is not operational.\n"));
1389                 return -1;
1390         }
1391
1392         data.dptr = (uint8_t *)&options.pnn;
1393         data.dsize = sizeof(uint32_t);
1394
1395         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
1396         if (ret != 0) {
1397                 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
1398                 return -1;
1399         }
1400         
1401         /* wait until we are in a new generation */
1402         while (1) {
1403                 next_generation = get_generation(ctdb);
1404                 if (next_generation != generation) {
1405                         return 0;
1406                 }
1407                 sleep(1);
1408         }
1409
1410         return 0;
1411 }
1412
1413
1414 /*
1415   shutdown a daemon
1416  */
1417 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
1418 {
1419         int ret;
1420
1421         ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
1422         if (ret != 0) {
1423                 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
1424                 return ret;
1425         }
1426
1427         return 0;
1428 }
1429
1430 /*
1431   trigger a recovery
1432  */
1433 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
1434 {
1435         int ret;
1436
1437         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1438         if (ret != 0) {
1439                 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
1440                 return ret;
1441         }
1442
1443         ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
1444         if (ret != 0) {
1445                 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
1446                 return ret;
1447         }
1448
1449         return 0;
1450 }
1451
1452
1453 /*
1454   display monitoring mode of a remote node
1455  */
1456 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
1457 {
1458         uint32_t monmode;
1459         int ret;
1460
1461         ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
1462         if (ret != 0) {
1463                 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
1464                 return ret;
1465         }
1466         if (!options.machinereadable){
1467                 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
1468         } else {
1469                 printf(":mode:\n");
1470                 printf(":%d:\n",monmode);
1471         }
1472         return 0;
1473 }
1474
1475
1476 /*
1477   display capabilities of a remote node
1478  */
1479 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
1480 {
1481         uint32_t capabilities;
1482         int ret;
1483
1484         ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
1485         if (ret != 0) {
1486                 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
1487                 return ret;
1488         }
1489         
1490         if (!options.machinereadable){
1491                 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
1492                 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
1493                 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
1494         } else {
1495                 printf(":RECMASTER:LMASTER:LVS:\n");
1496                 printf(":%d:%d:%d:\n",
1497                         !!(capabilities&CTDB_CAP_RECMASTER),
1498                         !!(capabilities&CTDB_CAP_LMASTER),
1499                         !!(capabilities&CTDB_CAP_LVS));
1500         }
1501         return 0;
1502 }
1503
1504 /*
1505   display lvs configuration
1506  */
1507 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
1508 {
1509         uint32_t *capabilities;
1510         struct ctdb_node_map *nodemap=NULL;
1511         int i, ret;
1512         int healthy_count = 0;
1513
1514         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1515         if (ret != 0) {
1516                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1517                 return ret;
1518         }
1519
1520         capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1521         CTDB_NO_MEMORY(ctdb, capabilities);
1522         
1523         /* collect capabilities for all connected nodes */
1524         for (i=0; i<nodemap->num; i++) {
1525                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1526                         continue;
1527                 }
1528                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1529                         continue;
1530                 }
1531         
1532                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1533                 if (ret != 0) {
1534                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1535                         return ret;
1536                 }
1537
1538                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1539                         continue;
1540                 }
1541
1542                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1543                         healthy_count++;
1544                 }
1545         }
1546
1547         /* Print all LVS nodes */
1548         for (i=0; i<nodemap->num; i++) {
1549                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1550                         continue;
1551                 }
1552                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1553                         continue;
1554                 }
1555                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1556                         continue;
1557                 }
1558
1559                 if (healthy_count != 0) {
1560                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1561                                 continue;
1562                         }
1563                 }
1564
1565                 printf("%d:%s\n", i, 
1566                         ctdb_addr_to_str(&nodemap->nodes[i].addr));
1567         }
1568
1569         return 0;
1570 }
1571
1572 /*
1573   display who is the lvs master
1574  */
1575 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1576 {
1577         uint32_t *capabilities;
1578         struct ctdb_node_map *nodemap=NULL;
1579         int i, ret;
1580         int healthy_count = 0;
1581
1582         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1583         if (ret != 0) {
1584                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1585                 return ret;
1586         }
1587
1588         capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1589         CTDB_NO_MEMORY(ctdb, capabilities);
1590         
1591         /* collect capabilities for all connected nodes */
1592         for (i=0; i<nodemap->num; i++) {
1593                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1594                         continue;
1595                 }
1596                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1597                         continue;
1598                 }
1599         
1600                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1601                 if (ret != 0) {
1602                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1603                         return ret;
1604                 }
1605
1606                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1607                         continue;
1608                 }
1609
1610                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1611                         healthy_count++;
1612                 }
1613         }
1614
1615         /* find and show the lvsmaster */
1616         for (i=0; i<nodemap->num; i++) {
1617                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1618                         continue;
1619                 }
1620                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1621                         continue;
1622                 }
1623                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1624                         continue;
1625                 }
1626
1627                 if (healthy_count != 0) {
1628                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1629                                 continue;
1630                         }
1631                 }
1632
1633                 printf("Node %d is LVS master\n", i);
1634                 return 0;
1635         }
1636
1637         printf("There is no LVS master\n");
1638         return 0;
1639 }
1640
1641 /*
1642   disable monitoring on a  node
1643  */
1644 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1645 {
1646         
1647         int ret;
1648
1649         ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
1650         if (ret != 0) {
1651                 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
1652                 return ret;
1653         }
1654         printf("Monitoring mode:%s\n","DISABLED");
1655
1656         return 0;
1657 }
1658
1659 /*
1660   enable monitoring on a  node
1661  */
1662 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1663 {
1664         
1665         int ret;
1666
1667         ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
1668         if (ret != 0) {
1669                 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
1670                 return ret;
1671         }
1672         printf("Monitoring mode:%s\n","ACTIVE");
1673
1674         return 0;
1675 }
1676
1677 /*
1678   display remote list of keys/data for a db
1679  */
1680 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
1681 {
1682         const char *db_name;
1683         struct ctdb_db_context *ctdb_db;
1684         int ret;
1685
1686         if (argc < 1) {
1687                 usage();
1688         }
1689
1690         db_name = argv[0];
1691
1692
1693         if (db_exists(ctdb, db_name)) {
1694                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
1695                 return -1;
1696         }
1697
1698         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
1699
1700         if (ctdb_db == NULL) {
1701                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1702                 return -1;
1703         }
1704
1705         /* traverse and dump the cluster tdb */
1706         ret = ctdb_dump_db(ctdb_db, stdout);
1707         if (ret == -1) {
1708                 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
1709                 return -1;
1710         }
1711         talloc_free(ctdb_db);
1712
1713         printf("Dumped %d records\n", ret);
1714         return 0;
1715 }
1716
1717
1718 /*
1719   display a list of the databases on a remote ctdb
1720  */
1721 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
1722 {
1723         int i, ret;
1724         struct ctdb_dbid_map *dbmap=NULL;
1725
1726         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
1727         if (ret != 0) {
1728                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
1729                 return ret;
1730         }
1731
1732         printf("Number of databases:%d\n", dbmap->num);
1733         for(i=0;i<dbmap->num;i++){
1734                 const char *path;
1735                 const char *name;
1736                 bool persistent;
1737
1738                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
1739                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
1740                 persistent = dbmap->dbs[i].persistent;
1741                 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name, 
1742                        path, persistent?"PERSISTENT":"");
1743         }
1744
1745         return 0;
1746 }
1747
1748 /*
1749   check if the local node is recmaster or not
1750   it will return 1 if this node is the recmaster and 0 if it is not
1751   or if the local ctdb daemon could not be contacted
1752  */
1753 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1754 {
1755         uint32_t mypnn, recmaster;
1756         int ret;
1757
1758         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1759         if (mypnn == -1) {
1760                 printf("Failed to get pnn of node\n");
1761                 return 1;
1762         }
1763
1764         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1765         if (ret != 0) {
1766                 printf("Failed to get the recmaster\n");
1767                 return 1;
1768         }
1769
1770         if (recmaster != mypnn) {
1771                 printf("this node is not the recmaster\n");
1772                 return 1;
1773         }
1774
1775         printf("this node is the recmaster\n");
1776         return 0;
1777 }
1778
1779 /*
1780   ping a node
1781  */
1782 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
1783 {
1784         int ret;
1785         struct timeval tv = timeval_current();
1786         ret = ctdb_ctrl_ping(ctdb, options.pnn);
1787         if (ret == -1) {
1788                 printf("Unable to get ping response from node %u\n", options.pnn);
1789                 return -1;
1790         } else {
1791                 printf("response from %u time=%.6f sec  (%d clients)\n", 
1792                        options.pnn, timeval_elapsed(&tv), ret);
1793         }
1794         return 0;
1795 }
1796
1797
1798 /*
1799   get a tunable
1800  */
1801 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
1802 {
1803         const char *name;
1804         uint32_t value;
1805         int ret;
1806
1807         if (argc < 1) {
1808                 usage();
1809         }
1810
1811         name = argv[0];
1812         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
1813         if (ret == -1) {
1814                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
1815                 return -1;
1816         }
1817
1818         printf("%-19s = %u\n", name, value);
1819         return 0;
1820 }
1821
1822 /*
1823   set a tunable
1824  */
1825 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
1826 {
1827         const char *name;
1828         uint32_t value;
1829         int ret;
1830
1831         if (argc < 2) {
1832                 usage();
1833         }
1834
1835         name = argv[0];
1836         value = strtoul(argv[1], NULL, 0);
1837
1838         ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
1839         if (ret == -1) {
1840                 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
1841                 return -1;
1842         }
1843         return 0;
1844 }
1845
1846 /*
1847   list all tunables
1848  */
1849 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
1850 {
1851         uint32_t count;
1852         const char **list;
1853         int ret, i;
1854
1855         ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
1856         if (ret == -1) {
1857                 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
1858                 return -1;
1859         }
1860
1861         for (i=0;i<count;i++) {
1862                 control_getvar(ctdb, 1, &list[i]);
1863         }
1864
1865         talloc_free(list);
1866         
1867         return 0;
1868 }
1869
1870 static struct {
1871         int32_t level;
1872         const char *description;
1873 } debug_levels[] = {
1874         {DEBUG_EMERG,   "EMERG"},
1875         {DEBUG_ALERT,   "ALERT"},
1876         {DEBUG_CRIT,    "CRIT"},
1877         {DEBUG_ERR,     "ERR"},
1878         {DEBUG_WARNING, "WARNING"},
1879         {DEBUG_NOTICE,  "NOTICE"},
1880         {DEBUG_INFO,    "INFO"},
1881         {DEBUG_DEBUG,   "DEBUG"}
1882 };
1883
1884 static const char *get_debug_by_level(int32_t level)
1885 {
1886         int i;
1887
1888         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1889                 if (debug_levels[i].level == level) {
1890                         return debug_levels[i].description;
1891                 }
1892         }
1893         return "Unknown";
1894 }
1895
1896 static int32_t get_debug_by_desc(const char *desc)
1897 {
1898         int i;
1899
1900         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1901                 if (!strcmp(debug_levels[i].description, desc)) {
1902                         return debug_levels[i].level;
1903                 }
1904         }
1905
1906         fprintf(stderr, "Invalid debug level '%s'\nMust be one of\n", desc);
1907         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1908                 fprintf(stderr, "    %s\n", debug_levels[i].description);
1909         }
1910
1911         exit(10);
1912 }
1913
1914 /*
1915   display debug level on a node
1916  */
1917 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1918 {
1919         int ret;
1920         int32_t level;
1921
1922         ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1923         if (ret != 0) {
1924                 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
1925                 return ret;
1926         } else {
1927                 if (options.machinereadable){
1928                         printf(":Name:Level:\n");
1929                         printf(":%s:%d:\n",get_debug_by_level(level),level);
1930                 } else {
1931                         printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
1932                 }
1933         }
1934         return 0;
1935 }
1936
1937
1938 /*
1939   set debug level on a node or all nodes
1940  */
1941 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1942 {
1943         int ret;
1944         int32_t level;
1945
1946         if (argc < 1) {
1947                 usage();
1948         }
1949
1950         if (isalpha(argv[0][0])) { 
1951                 level = get_debug_by_desc(argv[0]);
1952         } else {
1953                 level = strtol(argv[0], NULL, 0);
1954         }
1955
1956         ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1957         if (ret != 0) {
1958                 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1959         }
1960         return 0;
1961 }
1962
1963
1964 /*
1965   freeze a node
1966  */
1967 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1968 {
1969         int ret;
1970
1971         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1972         if (ret != 0) {
1973                 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
1974         }               
1975         return 0;
1976 }
1977
1978 /*
1979   thaw a node
1980  */
1981 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
1982 {
1983         int ret;
1984
1985         ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
1986         if (ret != 0) {
1987                 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
1988         }               
1989         return 0;
1990 }
1991
1992
1993 /*
1994   attach to a database
1995  */
1996 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
1997 {
1998         const char *db_name;
1999         struct ctdb_db_context *ctdb_db;
2000
2001         if (argc < 1) {
2002                 usage();
2003         }
2004         db_name = argv[0];
2005
2006         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
2007         if (ctdb_db == NULL) {
2008                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2009                 return -1;
2010         }
2011
2012         return 0;
2013 }
2014
2015 /*
2016   run an eventscript on a node
2017  */
2018 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
2019 {
2020         TDB_DATA data;
2021         int ret;
2022         int32_t res;
2023         char *errmsg;
2024         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2025
2026         if (argc != 1) {
2027                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2028                 return -1;
2029         }
2030
2031         data.dptr = (unsigned char *)discard_const(argv[0]);
2032         data.dsize = strlen((char *)data.dptr) + 1;
2033
2034         DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
2035
2036         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
2037                            0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
2038         if (ret != 0 || res != 0) {
2039                 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
2040                 talloc_free(tmp_ctx);
2041                 return -1;
2042         }
2043         talloc_free(tmp_ctx);
2044         return 0;
2045 }
2046
2047 #define DB_VERSION 1
2048 #define MAX_DB_NAME 64
2049 struct db_file_header {
2050         unsigned long version;
2051         time_t timestamp;
2052         unsigned long persistent;
2053         unsigned long size;
2054         const char name[MAX_DB_NAME];
2055 };
2056
2057 struct backup_data {
2058         struct ctdb_marshall_buffer *records;
2059         uint32_t len;
2060         uint32_t total;
2061         bool traverse_error;
2062 };
2063
2064 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
2065 {
2066         struct backup_data *bd = talloc_get_type(private, struct backup_data);
2067         struct ctdb_rec_data *rec;
2068
2069         /* add the record */
2070         rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
2071         if (rec == NULL) {
2072                 bd->traverse_error = true;
2073                 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
2074                 return -1;
2075         }
2076         bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
2077         if (bd->records == NULL) {
2078                 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
2079                 bd->traverse_error = true;
2080                 return -1;
2081         }
2082         bd->records->count++;
2083         memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
2084         bd->len += rec->length;
2085         talloc_free(rec);
2086
2087         bd->total++;
2088         return 0;
2089 }
2090
2091 /*
2092  * backup a database to a file 
2093  */
2094 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
2095 {
2096         int i, ret;
2097         struct ctdb_dbid_map *dbmap=NULL;
2098         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2099         struct db_file_header dbhdr;
2100         struct ctdb_db_context *ctdb_db;
2101         struct backup_data *bd;
2102         int fh;
2103
2104         if (argc != 2) {
2105                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2106                 return -1;
2107         }
2108
2109         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
2110         if (ret != 0) {
2111                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
2112                 return ret;
2113         }
2114
2115         for(i=0;i<dbmap->num;i++){
2116                 const char *name;
2117
2118                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
2119                 if(!strcmp(argv[0], name)){
2120                         talloc_free(discard_const(name));
2121                         break;
2122                 }
2123                 talloc_free(discard_const(name));
2124         }
2125         if (i == dbmap->num) {
2126                 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
2127                 talloc_free(tmp_ctx);
2128                 return -1;
2129         }
2130
2131
2132         ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
2133         if (ctdb_db == NULL) {
2134                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
2135                 return -1;
2136         }
2137
2138
2139         ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
2140         if (ret == -1) {
2141                 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
2142                 talloc_free(tmp_ctx);
2143                 return -1;
2144         }
2145
2146
2147         bd = talloc_zero(tmp_ctx, struct backup_data);
2148         if (bd == NULL) {
2149                 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
2150                 talloc_free(tmp_ctx);
2151                 return -1;
2152         }
2153
2154         bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
2155         if (bd->records == NULL) {
2156                 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
2157                 talloc_free(tmp_ctx);
2158                 return -1;
2159         }
2160
2161         bd->len = offsetof(struct ctdb_marshall_buffer, data);
2162         bd->records->db_id = ctdb_db->db_id;
2163         /* traverse the database collecting all records */
2164         if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
2165             bd->traverse_error) {
2166                 DEBUG(DEBUG_ERR,("Traverse error\n"));
2167                 talloc_free(tmp_ctx);
2168                 return -1;              
2169         }
2170
2171         tdb_transaction_cancel(ctdb_db->ltdb->tdb);
2172
2173
2174         fh = open(argv[1], O_RDWR|O_CREAT, 0600);
2175         if (fh == -1) {
2176                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
2177                 talloc_free(tmp_ctx);
2178                 return -1;
2179         }
2180
2181         dbhdr.version = DB_VERSION;
2182         dbhdr.timestamp = time(NULL);
2183         dbhdr.persistent = dbmap->dbs[i].persistent;
2184         dbhdr.size = bd->len;
2185         if (strlen(argv[0]) >= MAX_DB_NAME) {
2186                 DEBUG(DEBUG_ERR,("Too long dbname\n"));
2187                 talloc_free(tmp_ctx);
2188                 return -1;
2189         }
2190         strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
2191         write(fh, &dbhdr, sizeof(dbhdr));
2192         write(fh, bd->records, bd->len);
2193
2194         close(fh);
2195         talloc_free(tmp_ctx);
2196         return 0;
2197 }
2198
2199 /*
2200  * restore a database from a file 
2201  */
2202 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
2203 {
2204         int ret;
2205         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2206         TDB_DATA outdata;
2207         TDB_DATA data;
2208         struct db_file_header dbhdr;
2209         struct ctdb_db_context *ctdb_db;
2210         struct ctdb_node_map *nodemap=NULL;
2211         struct ctdb_vnn_map *vnnmap=NULL;
2212         int fh;
2213         struct ctdb_control_wipe_database w;
2214         uint32_t *nodes;
2215         uint32_t generation;
2216         struct tm *tm;
2217         char tbuf[100];
2218
2219         if (argc != 1) {
2220                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2221                 return -1;
2222         }
2223
2224         fh = open(argv[0], O_RDONLY);
2225         if (fh == -1) {
2226                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
2227                 talloc_free(tmp_ctx);
2228                 return -1;
2229         }
2230
2231         read(fh, &dbhdr, sizeof(dbhdr));
2232         if (dbhdr.version != DB_VERSION) {
2233                 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
2234                 talloc_free(tmp_ctx);
2235                 return -1;
2236         }
2237
2238         outdata.dsize = dbhdr.size;
2239         outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
2240         if (outdata.dptr == NULL) {
2241                 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
2242                 close(fh);
2243                 talloc_free(tmp_ctx);
2244                 return -1;
2245         }               
2246         read(fh, outdata.dptr, outdata.dsize);
2247         close(fh);
2248
2249         tm = localtime(&dbhdr.timestamp);
2250         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
2251         printf("Restoring database '%s' from backup @ %s\n",
2252                 dbhdr.name, tbuf);
2253
2254
2255         ctdb_db = ctdb_attach(ctdb, dbhdr.name, dbhdr.persistent, 0);
2256         if (ctdb_db == NULL) {
2257                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbhdr.name));
2258                 talloc_free(tmp_ctx);
2259                 return -1;
2260         }
2261
2262         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2263         if (ret != 0) {
2264                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2265                 talloc_free(tmp_ctx);
2266                 return ret;
2267         }
2268
2269
2270         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
2271         if (ret != 0) {
2272                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
2273                 talloc_free(tmp_ctx);
2274                 return ret;
2275         }
2276
2277         /* freeze all nodes */
2278         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2279         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
2280                                         nodes, TIMELIMIT(),
2281                                         false, tdb_null,
2282                                         NULL, NULL,
2283                                         NULL) != 0) {
2284                 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
2285                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2286                 talloc_free(tmp_ctx);
2287                 return -1;
2288         }
2289
2290         generation = vnnmap->generation;
2291         data.dptr = (void *)&generation;
2292         data.dsize = sizeof(generation);
2293
2294         /* start a cluster wide transaction */
2295         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2296         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
2297                                         nodes,
2298                                         TIMELIMIT(), false, data,
2299                                         NULL, NULL,
2300                                         NULL) != 0) {
2301                 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
2302                 return -1;
2303         }
2304
2305
2306         w.db_id = ctdb_db->db_id;
2307         w.transaction_id = generation;
2308
2309         data.dptr = (void *)&w;
2310         data.dsize = sizeof(w);
2311
2312         /* wipe all the remote databases. */
2313         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2314         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
2315                                         nodes,
2316                                         TIMELIMIT(), false, data,
2317                                         NULL, NULL,
2318                                         NULL) != 0) {
2319                 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
2320                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2321                 talloc_free(tmp_ctx);
2322                 return -1;
2323         }
2324         
2325         /* push the database */
2326         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2327         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
2328                                         nodes,
2329                                         TIMELIMIT(), false, outdata,
2330                                         NULL, NULL,
2331                                         NULL) != 0) {
2332                 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
2333                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2334                 talloc_free(tmp_ctx);
2335                 return -1;
2336         }
2337
2338         data.dptr = (void *)&generation;
2339         data.dsize = sizeof(generation);
2340
2341         /* commit all the changes */
2342         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
2343                                         nodes,
2344                                         TIMELIMIT(), false, data,
2345                                         NULL, NULL,
2346                                         NULL) != 0) {
2347                 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
2348                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2349                 talloc_free(tmp_ctx);
2350                 return -1;
2351         }
2352
2353
2354         /* thaw all nodes */
2355         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2356         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
2357                                         nodes, TIMELIMIT(),
2358                                         false, tdb_null,
2359                                         NULL, NULL,
2360                                         NULL) != 0) {
2361                 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
2362                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2363                 talloc_free(tmp_ctx);
2364                 return -1;
2365         }
2366
2367
2368         talloc_free(tmp_ctx);
2369         return 0;
2370 }
2371
2372 /*
2373  * set flags of a node in the nodemap
2374  */
2375 static int control_setflags(struct ctdb_context *ctdb, int argc, const char **argv)
2376 {
2377         int ret;
2378         int32_t status;
2379         int node;
2380         int flags;
2381         TDB_DATA data;
2382         struct ctdb_node_flag_change c;
2383
2384         if (argc != 2) {
2385                 usage();
2386                 return -1;
2387         }
2388
2389         if (sscanf(argv[0], "%d", &node) != 1) {
2390                 DEBUG(DEBUG_ERR, ("Badly formed node\n"));
2391                 usage();
2392                 return -1;
2393         }
2394         if (sscanf(argv[1], "0x%x", &flags) != 1) {
2395                 DEBUG(DEBUG_ERR, ("Badly formed flags\n"));
2396                 usage();
2397                 return -1;
2398         }
2399
2400         c.pnn       = node;
2401         c.old_flags = 0;
2402         c.new_flags = flags;
2403
2404         data.dsize = sizeof(c);
2405         data.dptr = (unsigned char *)&c;
2406
2407         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_MODIFY_FLAGS, 0, 
2408                            data, NULL, NULL, &status, NULL, NULL);
2409         if (ret != 0 || status != 0) {
2410                 DEBUG(DEBUG_ERR,("Failed to modify flags\n"));
2411                 return -1;
2412         }
2413         return 0;
2414 }
2415
2416 /*
2417   dump memory usage
2418  */
2419 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2420 {
2421         TDB_DATA data;
2422         int ret;
2423         int32_t res;
2424         char *errmsg;
2425         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2426         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
2427                            0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
2428         if (ret != 0 || res != 0) {
2429                 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
2430                 talloc_free(tmp_ctx);
2431                 return -1;
2432         }
2433         write(1, data.dptr, data.dsize);
2434         talloc_free(tmp_ctx);
2435         return 0;
2436 }
2437
2438 /*
2439   handler for memory dumps
2440 */
2441 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid, 
2442                              TDB_DATA data, void *private_data)
2443 {
2444         write(1, data.dptr, data.dsize);
2445         exit(0);
2446 }
2447
2448 /*
2449   dump memory usage on the recovery daemon
2450  */
2451 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2452 {
2453         int ret;
2454         TDB_DATA data;
2455         struct rd_memdump_reply rd;
2456
2457         rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2458         if (rd.pnn == -1) {
2459                 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
2460                 return -1;
2461         }
2462         rd.srvid = getpid();
2463
2464         /* register a message port for receiveing the reply so that we
2465            can receive the reply
2466         */
2467         ctdb_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
2468
2469
2470         data.dptr = (uint8_t *)&rd;
2471         data.dsize = sizeof(rd);
2472
2473         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
2474         if (ret != 0) {
2475                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
2476                 return -1;
2477         }
2478
2479         /* this loop will terminate when we have received the reply */
2480         while (1) {     
2481                 event_loop_once(ctdb->ev);
2482         }
2483
2484         return 0;
2485 }
2486
2487 /*
2488   list all nodes in the cluster
2489  */
2490 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
2491 {
2492         int i, ret;
2493         struct ctdb_node_map *nodemap=NULL;
2494
2495         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2496         if (ret != 0) {
2497                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2498                 return ret;
2499         }
2500
2501         for(i=0;i<nodemap->num;i++){
2502                 printf("%s\n", ctdb_addr_to_str(&nodemap->nodes[i].addr));
2503         }
2504
2505         return 0;
2506 }
2507
2508 /*
2509   reload the nodes file on the local node
2510  */
2511 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
2512 {
2513         int i, ret;
2514         int mypnn;
2515         struct ctdb_node_map *nodemap=NULL;
2516
2517         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2518         if (mypnn == -1) {
2519                 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
2520                 return -1;
2521         }
2522
2523         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2524         if (ret != 0) {
2525                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2526                 return ret;
2527         }
2528
2529         /* reload the nodes file on all remote nodes */
2530         for (i=0;i<nodemap->num;i++) {
2531                 if (nodemap->nodes[i].pnn == mypnn) {
2532                         continue;
2533                 }
2534                 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
2535                 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
2536                         nodemap->nodes[i].pnn);
2537                 if (ret != 0) {
2538                         DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
2539                 }
2540         }
2541
2542         /* reload the nodes file on the local node */
2543         DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
2544         ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
2545         if (ret != 0) {
2546                 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
2547         }
2548
2549         /* initiate a recovery */
2550         control_recover(ctdb, argc, argv);
2551
2552         return 0;
2553 }
2554
2555
2556 static const struct {
2557         const char *name;
2558         int (*fn)(struct ctdb_context *, int, const char **);
2559         bool auto_all;
2560         const char *msg;
2561         const char *args;
2562 } ctdb_commands[] = {
2563 #ifdef CTDB_VERS
2564         { "version",         control_version,           true,  "show version of ctdb" },
2565 #endif
2566         { "status",          control_status,            true,  "show node status" },
2567         { "uptime",          control_uptime,            true,  "show node uptime" },
2568         { "ping",            control_ping,              true,  "ping all nodes" },
2569         { "getvar",          control_getvar,            true,  "get a tunable variable",               "<name>"},
2570         { "setvar",          control_setvar,            true,  "set a tunable variable",               "<name> <value>"},
2571         { "listvars",        control_listvars,          true,  "list tunable variables"},
2572         { "statistics",      control_statistics,        false, "show statistics" },
2573         { "statisticsreset", control_statistics_reset,  true,  "reset statistics"},
2574         { "ip",              control_ip,                false,  "show which public ip's that ctdb manages" },
2575         { "process-exists",  control_process_exists,    true,  "check if a process exists on a node",  "<pid>"},
2576         { "getdbmap",        control_getdbmap,          true,  "show the database map" },
2577         { "catdb",           control_catdb,             true,  "dump a database" ,                     "<dbname>"},
2578         { "getmonmode",      control_getmonmode,        true,  "show monitoring mode" },
2579         { "getcapabilities", control_getcapabilities,   true,  "show node capabilities" },
2580         { "pnn",             control_pnn,               true,  "show the pnn of the currnet node" },
2581         { "lvs",             control_lvs,               true,  "show lvs configuration" },
2582         { "lvsmaster",       control_lvsmaster,         true,  "show which node is the lvs master" },
2583         { "disablemonitor",      control_disable_monmode,        true,  "set monitoring mode to DISABLE" },
2584         { "enablemonitor",      control_enable_monmode,        true,  "set monitoring mode to ACTIVE" },
2585         { "setdebug",        control_setdebug,          true,  "set debug level",                      "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
2586         { "getdebug",        control_getdebug,          true,  "get debug level" },
2587         { "attach",          control_attach,            true,  "attach to a database",                 "<dbname>" },
2588         { "dumpmemory",      control_dumpmemory,        true,  "dump memory map to stdout" },
2589         { "rddumpmemory",    control_rddumpmemory,      true,  "dump memory map from the recovery daemon to stdout" },
2590         { "getpid",          control_getpid,            true,  "get ctdbd process ID" },
2591         { "disable",         control_disable,           true,  "disable a nodes public IP" },
2592         { "enable",          control_enable,            true,  "enable a nodes public IP" },
2593         { "ban",             control_ban,               true,  "ban a node from the cluster",          "<bantime|0>"},
2594         { "unban",           control_unban,             true,  "unban a node from the cluster" },
2595         { "shutdown",        control_shutdown,          true,  "shutdown ctdbd" },
2596         { "recover",         control_recover,           true,  "force recovery" },
2597         { "freeze",          control_freeze,            true,  "freeze all databases" },
2598         { "thaw",            control_thaw,              true,  "thaw all databases" },
2599         { "isnotrecmaster",  control_isnotrecmaster,    false,  "check if the local node is recmaster or not" },
2600         { "killtcp",         kill_tcp,                  false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
2601         { "gratiousarp",     control_gratious_arp,      false, "send a gratious arp", "<ip> <interface>" },
2602         { "tickle",          tickle_tcp,                false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
2603         { "gettickles",      control_get_tickles,       false, "get the list of tickles registered for this ip", "<ip>" },
2604
2605         { "regsrvid",        regsrvid,                  false, "register a server id", "<pnn> <type> <id>" },
2606         { "unregsrvid",      unregsrvid,                false, "unregister a server id", "<pnn> <type> <id>" },
2607         { "chksrvid",        chksrvid,                  false, "check if a server id exists", "<pnn> <type> <id>" },
2608         { "getsrvids",       getsrvids,                 false, "get a list of all server ids"},
2609         { "vacuum",          ctdb_vacuum,               false, "vacuum the databases of empty records", "[max_records]"},
2610         { "repack",          ctdb_repack,               false, "repack all databases", "[max_freelist]"},
2611         { "listnodes",       control_listnodes,         false, "list all nodes in the cluster"},
2612         { "reloadnodes",     control_reload_nodes_file,         false, "reload the nodes file and restart the transport on all nodes"},
2613         { "moveip",          control_moveip,            false, "move/failover an ip address to another node", "<ip> <node>"},
2614         { "addip",           control_addip,             true, "add a ip address to a node", "<ip/mask> <iface>"},
2615         { "delip",           control_delip,             false, "delete an ip address from a node", "<ip>"},
2616         { "eventscript",     control_eventscript,       true, "run the eventscript with the given parameters on a node", "<arguments>"},
2617         { "backupdb",        control_backupdb,          false, "backup the database into a file.", "<database> <file>"},
2618         { "restoredb",        control_restoredb,          false, "restore the database from a file.", "<file>"},
2619         { "recmaster",        control_recmaster,          false, "show the pnn for the recovery master."},
2620         { "setflags",        control_setflags,            false, "set flags for a node in the nodemap.", "<node> <flags>"},
2621 };
2622
2623 /*
2624   show usage message
2625  */
2626 static void usage(void)
2627 {
2628         int i;
2629         printf(
2630 "Usage: ctdb [options] <control>\n" \
2631 "Options:\n" \
2632 "   -n <node>          choose node number, or 'all' (defaults to local node)\n"
2633 "   -Y                 generate machinereadable output\n"
2634 "   -t <timelimit>     set timelimit for control in seconds (default %u)\n", options.timelimit);
2635         printf("Controls:\n");
2636         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2637                 printf("  %-15s %-27s  %s\n", 
2638                        ctdb_commands[i].name, 
2639                        ctdb_commands[i].args?ctdb_commands[i].args:"",
2640                        ctdb_commands[i].msg);
2641         }
2642         exit(1);
2643 }
2644
2645
2646 static void ctdb_alarm(int sig)
2647 {
2648         printf("Maximum runtime exceeded - exiting\n");
2649         _exit(ERR_TIMEOUT);
2650 }
2651
2652 /*
2653   main program
2654 */
2655 int main(int argc, const char *argv[])
2656 {
2657         struct ctdb_context *ctdb;
2658         char *nodestring = NULL;
2659         struct poptOption popt_options[] = {
2660                 POPT_AUTOHELP
2661                 POPT_CTDB_CMDLINE
2662                 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
2663                 { "node",      'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
2664                 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
2665                 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
2666                 POPT_TABLEEND
2667         };
2668         int opt;
2669         const char **extra_argv;
2670         int extra_argc = 0;
2671         int ret=-1, i;
2672         poptContext pc;
2673         struct event_context *ev;
2674         const char *control;
2675
2676         setlinebuf(stdout);
2677         
2678         /* set some defaults */
2679         options.maxruntime = 0;
2680         options.timelimit = 3;
2681         options.pnn = CTDB_CURRENT_NODE;
2682
2683         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
2684
2685         while ((opt = poptGetNextOpt(pc)) != -1) {
2686                 switch (opt) {
2687                 default:
2688                         DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
2689                                 poptBadOption(pc, 0), poptStrerror(opt)));
2690                         exit(1);
2691                 }
2692         }
2693
2694         /* setup the remaining options for the main program to use */
2695         extra_argv = poptGetArgs(pc);
2696         if (extra_argv) {
2697                 extra_argv++;
2698                 while (extra_argv[extra_argc]) extra_argc++;
2699         }
2700
2701         if (extra_argc < 1) {
2702                 usage();
2703         }
2704
2705         if (options.maxruntime != 0) {
2706                 signal(SIGALRM, ctdb_alarm);
2707                 alarm(options.maxruntime);
2708         }
2709
2710         /* setup the node number to contact */
2711         if (nodestring != NULL) {
2712                 if (strcmp(nodestring, "all") == 0) {
2713                         options.pnn = CTDB_BROADCAST_ALL;
2714                 } else {
2715                         options.pnn = strtoul(nodestring, NULL, 0);
2716                 }
2717         }
2718
2719         control = extra_argv[0];
2720
2721         ev = event_context_init(NULL);
2722
2723         /* initialise ctdb */
2724         ctdb = ctdb_cmdline_client(ev);
2725         if (ctdb == NULL) {
2726                 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
2727                 exit(1);
2728         }
2729
2730         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2731                 if (strcmp(control, ctdb_commands[i].name) == 0) {
2732                         int j;
2733
2734                         if (options.pnn == CTDB_CURRENT_NODE) {
2735                                 int pnn;
2736                                 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);         
2737                                 if (pnn == -1) {
2738                                         return -1;
2739                                 }
2740                                 options.pnn = pnn;
2741                         }
2742
2743                         if (ctdb_commands[i].auto_all && 
2744                             options.pnn == CTDB_BROADCAST_ALL) {
2745                                 uint32_t *nodes;
2746                                 uint32_t num_nodes;
2747                                 ret = 0;
2748
2749                                 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
2750                                 CTDB_NO_MEMORY(ctdb, nodes);
2751         
2752                                 for (j=0;j<num_nodes;j++) {
2753                                         options.pnn = nodes[j];
2754                                         ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2755                                 }
2756                                 talloc_free(nodes);
2757                         } else {
2758                                 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2759                         }
2760                         break;
2761                 }
2762         }
2763
2764         if (i == ARRAY_SIZE(ctdb_commands)) {
2765                 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
2766                 exit(1);
2767         }
2768
2769         return ret;
2770 }