Revert "When adding ips to nodes, set up a deferred rebalance for the whole node...
[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/tevent/tevent.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_client.h"
31 #include "../include/ctdb_private.h"
32 #include "../common/rb_tree.h"
33 #include "db_wrap.h"
34
35 #define ERR_TIMEOUT     20      /* timed out trying to reach node */
36 #define ERR_NONODE      21      /* node does not exist */
37 #define ERR_DISNODE     22      /* node is disconnected */
38
39 struct ctdb_connection *ctdb_connection;
40
41 static void usage(void);
42
43 static struct {
44         int timelimit;
45         uint32_t pnn;
46         int machinereadable;
47         int verbose;
48         int maxruntime;
49 } options;
50
51 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
52 #define LONGTIMELIMIT() timeval_current_ofs(options.timelimit*10, 0)
53
54 #ifdef CTDB_VERS
55 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
56 {
57 #define STR(x) #x
58 #define XSTR(x) STR(x)
59         printf("CTDB version: %s\n", XSTR(CTDB_VERS));
60         return 0;
61 }
62 #endif
63
64
65 /*
66   verify that a node exists and is reachable
67  */
68 static void verify_node(struct ctdb_context *ctdb)
69 {
70         int ret;
71         struct ctdb_node_map *nodemap=NULL;
72
73         if (options.pnn == CTDB_CURRENT_NODE) {
74                 return;
75         }
76         if (options.pnn == CTDB_BROADCAST_ALL) {
77                 return;
78         }
79
80         /* verify the node exists */
81         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
82                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
83                 exit(10);
84         }
85         if (options.pnn >= nodemap->num) {
86                 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", options.pnn));
87                 exit(ERR_NONODE);
88         }
89         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DELETED) {
90                 DEBUG(DEBUG_ERR, ("Node %u is DELETED\n", options.pnn));
91                 exit(ERR_DISNODE);
92         }
93         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DISCONNECTED) {
94                 DEBUG(DEBUG_ERR, ("Node %u is DISCONNECTED\n", options.pnn));
95                 exit(ERR_DISNODE);
96         }
97
98         /* verify we can access the node */
99         ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
100         if (ret == -1) {
101                 DEBUG(DEBUG_ERR,("Can not access node. Node is not operational.\n"));
102                 exit(10);
103         }
104 }
105
106 /*
107  check if a database exists
108 */
109 static int db_exists(struct ctdb_context *ctdb, const char *db_name)
110 {
111         int i, ret;
112         struct ctdb_dbid_map *dbmap=NULL;
113
114         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
115         if (ret != 0) {
116                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
117                 return -1;
118         }
119
120         for(i=0;i<dbmap->num;i++){
121                 const char *name;
122
123                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
124                 if (!strcmp(name, db_name)) {
125                         return 0;
126                 }
127         }
128
129         return -1;
130 }
131
132 /*
133   see if a process exists
134  */
135 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
136 {
137         uint32_t pnn, pid;
138         int ret;
139         if (argc < 1) {
140                 usage();
141         }
142
143         if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
144                 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
145                 return -1;
146         }
147
148         ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
149         if (ret == 0) {
150                 printf("%u:%u exists\n", pnn, pid);
151         } else {
152                 printf("%u:%u does not exist\n", pnn, pid);
153         }
154         return ret;
155 }
156
157 /*
158   display statistics structure
159  */
160 static void show_statistics(struct ctdb_statistics *s, int show_header)
161 {
162         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
163         int i;
164         const char *prefix=NULL;
165         int preflen=0;
166         int tmp, days, hours, minutes, seconds;
167         const struct {
168                 const char *name;
169                 uint32_t offset;
170         } fields[] = {
171 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
172                 STATISTICS_FIELD(num_clients),
173                 STATISTICS_FIELD(frozen),
174                 STATISTICS_FIELD(recovering),
175                 STATISTICS_FIELD(num_recoveries),
176                 STATISTICS_FIELD(client_packets_sent),
177                 STATISTICS_FIELD(client_packets_recv),
178                 STATISTICS_FIELD(node_packets_sent),
179                 STATISTICS_FIELD(node_packets_recv),
180                 STATISTICS_FIELD(keepalive_packets_sent),
181                 STATISTICS_FIELD(keepalive_packets_recv),
182                 STATISTICS_FIELD(node.req_call),
183                 STATISTICS_FIELD(node.reply_call),
184                 STATISTICS_FIELD(node.req_dmaster),
185                 STATISTICS_FIELD(node.reply_dmaster),
186                 STATISTICS_FIELD(node.reply_error),
187                 STATISTICS_FIELD(node.req_message),
188                 STATISTICS_FIELD(node.req_control),
189                 STATISTICS_FIELD(node.reply_control),
190                 STATISTICS_FIELD(client.req_call),
191                 STATISTICS_FIELD(client.req_message),
192                 STATISTICS_FIELD(client.req_control),
193                 STATISTICS_FIELD(timeouts.call),
194                 STATISTICS_FIELD(timeouts.control),
195                 STATISTICS_FIELD(timeouts.traverse),
196                 STATISTICS_FIELD(total_calls),
197                 STATISTICS_FIELD(pending_calls),
198                 STATISTICS_FIELD(lockwait_calls),
199                 STATISTICS_FIELD(pending_lockwait_calls),
200                 STATISTICS_FIELD(childwrite_calls),
201                 STATISTICS_FIELD(pending_childwrite_calls),
202                 STATISTICS_FIELD(memory_used),
203                 STATISTICS_FIELD(max_hop_count),
204         };
205         tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
206         seconds = tmp%60;
207         tmp    /= 60;
208         minutes = tmp%60;
209         tmp    /= 60;
210         hours   = tmp%24;
211         tmp    /= 24;
212         days    = tmp;
213
214         if (options.machinereadable){
215                 if (show_header) {
216                         printf("CTDB version:");
217                         printf("Current time of statistics:");
218                         printf("Statistics collected since:");
219                         for (i=0;i<ARRAY_SIZE(fields);i++) {
220                                 printf("%s:", fields[i].name);
221                         }
222                         printf("num_reclock_ctdbd_latency:");
223                         printf("min_reclock_ctdbd_latency:");
224                         printf("avg_reclock_ctdbd_latency:");
225                         printf("max_reclock_ctdbd_latency:");
226
227                         printf("num_reclock_recd_latency:");
228                         printf("min_reclock_recd_latency:");
229                         printf("avg_reclock_recd_latency:");
230                         printf("max_reclock_recd_latency:");
231
232                         printf("num_call_latency:");
233                         printf("min_call_latency:");
234                         printf("avg_call_latency:");
235                         printf("max_call_latency:");
236
237                         printf("num_lockwait_latency:");
238                         printf("min_lockwait_latency:");
239                         printf("avg_lockwait_latency:");
240                         printf("max_lockwait_latency:");
241
242                         printf("num_childwrite_latency:");
243                         printf("min_childwrite_latency:");
244                         printf("avg_childwrite_latency:");
245                         printf("max_childwrite_latency:");
246                         printf("\n");
247                 }
248                 printf("%d:", CTDB_VERSION);
249                 printf("%d:", (int)s->statistics_current_time.tv_sec);
250                 printf("%d:", (int)s->statistics_start_time.tv_sec);
251                 for (i=0;i<ARRAY_SIZE(fields);i++) {
252                         printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
253                 }
254                 printf("%d:", s->reclock.ctdbd.num);
255                 printf("%.6f:", s->reclock.ctdbd.min);
256                 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
257                 printf("%.6f:", s->reclock.ctdbd.max);
258
259                 printf("%d:", s->reclock.recd.num);
260                 printf("%.6f:", s->reclock.recd.min);
261                 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
262                 printf("%.6f:", s->reclock.recd.max);
263
264                 printf("%d:", s->call_latency.num);
265                 printf("%.6f:", s->call_latency.min);
266                 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
267                 printf("%.6f:", s->call_latency.max);
268
269                 printf("%d:", s->lockwait_latency.num);
270                 printf("%.6f:", s->lockwait_latency.min);
271                 printf("%.6f:", s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0);
272                 printf("%.6f:", s->lockwait_latency.max);
273
274                 printf("%d:", s->childwrite_latency.num);
275                 printf("%.6f:", s->childwrite_latency.min);
276                 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
277                 printf("%.6f:", s->childwrite_latency.max);
278                 printf("\n");
279         } else {
280                 printf("CTDB version %u\n", CTDB_VERSION);
281                 printf("Current time of statistics  :                %s", ctime(&s->statistics_current_time.tv_sec));
282                 printf("Statistics collected since  : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
283
284                 for (i=0;i<ARRAY_SIZE(fields);i++) {
285                         if (strchr(fields[i].name, '.')) {
286                                 preflen = strcspn(fields[i].name, ".")+1;
287                                 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
288                                         prefix = fields[i].name;
289                                         printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
290                                 }
291                         } else {
292                                 preflen = 0;
293                         }
294                         printf(" %*s%-22s%*s%10u\n", 
295                                preflen?4:0, "",
296                                fields[i].name+preflen, 
297                                preflen?0:4, "",
298                                *(uint32_t *)(fields[i].offset+(uint8_t *)s));
299                 }
300                 printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "reclock_ctdbd       MIN/AVG/MAX", s->reclock.ctdbd.min, s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0, s->reclock.ctdbd.max, s->reclock.ctdbd.num);
301
302                 printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "reclock_recd       MIN/AVG/MAX", s->reclock.recd.min, s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0, s->reclock.recd.max, s->reclock.recd.num);
303
304                 printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "call_latency       MIN/AVG/MAX", s->call_latency.min, s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0, s->call_latency.max, s->call_latency.num);
305                 printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "lockwait_latency   MIN/AVG/MAX", s->lockwait_latency.min, s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0, s->lockwait_latency.max, s->lockwait_latency.num);
306                 printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "childwrite_latency MIN/AVG/MAX", s->childwrite_latency.min, s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0, s->childwrite_latency.max, s->childwrite_latency.num);
307         }
308
309         talloc_free(tmp_ctx);
310 }
311
312 /*
313   display remote ctdb statistics combined from all nodes
314  */
315 static int control_statistics_all(struct ctdb_context *ctdb)
316 {
317         int ret, i;
318         struct ctdb_statistics statistics;
319         uint32_t *nodes;
320         uint32_t num_nodes;
321
322         nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
323         CTDB_NO_MEMORY(ctdb, nodes);
324         
325         ZERO_STRUCT(statistics);
326
327         for (i=0;i<num_nodes;i++) {
328                 struct ctdb_statistics s1;
329                 int j;
330                 uint32_t *v1 = (uint32_t *)&s1;
331                 uint32_t *v2 = (uint32_t *)&statistics;
332                 uint32_t num_ints = 
333                         offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
334                 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
335                 if (ret != 0) {
336                         DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
337                         return ret;
338                 }
339                 for (j=0;j<num_ints;j++) {
340                         v2[j] += v1[j];
341                 }
342                 statistics.max_hop_count = 
343                         MAX(statistics.max_hop_count, s1.max_hop_count);
344                 statistics.call_latency.max = 
345                         MAX(statistics.call_latency.max, s1.call_latency.max);
346                 statistics.lockwait_latency.max = 
347                         MAX(statistics.lockwait_latency.max, s1.lockwait_latency.max);
348         }
349         talloc_free(nodes);
350         printf("Gathered statistics for %u nodes\n", num_nodes);
351         show_statistics(&statistics, 1);
352         return 0;
353 }
354
355 /*
356   display remote ctdb statistics
357  */
358 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
359 {
360         int ret;
361         struct ctdb_statistics statistics;
362
363         if (options.pnn == CTDB_BROADCAST_ALL) {
364                 return control_statistics_all(ctdb);
365         }
366
367         ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
368         if (ret != 0) {
369                 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
370                 return ret;
371         }
372         show_statistics(&statistics, 1);
373         return 0;
374 }
375
376
377 /*
378   reset remote ctdb statistics
379  */
380 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
381 {
382         int ret;
383
384         ret = ctdb_statistics_reset(ctdb, options.pnn);
385         if (ret != 0) {
386                 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
387                 return ret;
388         }
389         return 0;
390 }
391
392
393 /*
394   display remote ctdb rolling statistics
395  */
396 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
397 {
398         int ret;
399         struct ctdb_statistics_wire *stats;
400         int i, num_records = -1;
401
402         if (argc ==1) {
403                 num_records = atoi(argv[0]) - 1;
404         }
405
406         ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
407         if (ret != 0) {
408                 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
409                 return ret;
410         }
411         for (i=0;i<stats->num;i++) {
412                 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
413                         continue;
414                 }
415                 show_statistics(&stats->stats[i], i==0);
416                 if (i == num_records) {
417                         break;
418                 }
419         }
420         return 0;
421 }
422
423
424 /*
425   display uptime of remote node
426  */
427 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
428 {
429         int ret;
430         struct ctdb_uptime *uptime = NULL;
431         int tmp, days, hours, minutes, seconds;
432
433         ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
434         if (ret != 0) {
435                 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
436                 return ret;
437         }
438
439         if (options.machinereadable){
440                 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
441                 printf(":%u:%u:%u:%lf\n",
442                         (unsigned int)uptime->current_time.tv_sec,
443                         (unsigned int)uptime->ctdbd_start_time.tv_sec,
444                         (unsigned int)uptime->last_recovery_finished.tv_sec,
445                         timeval_delta(&uptime->last_recovery_finished,
446                                       &uptime->last_recovery_started)
447                 );
448                 return 0;
449         }
450
451         printf("Current time of node          :                %s", ctime(&uptime->current_time.tv_sec));
452
453         tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
454         seconds = tmp%60;
455         tmp    /= 60;
456         minutes = tmp%60;
457         tmp    /= 60;
458         hours   = tmp%24;
459         tmp    /= 24;
460         days    = tmp;
461         printf("Ctdbd start time              : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
462
463         tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
464         seconds = tmp%60;
465         tmp    /= 60;
466         minutes = tmp%60;
467         tmp    /= 60;
468         hours   = tmp%24;
469         tmp    /= 24;
470         days    = tmp;
471         printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
472         
473         printf("Duration of last recovery/failover: %lf seconds\n",
474                 timeval_delta(&uptime->last_recovery_finished,
475                               &uptime->last_recovery_started));
476
477         return 0;
478 }
479
480 /*
481   show the PNN of the current node
482  */
483 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
484 {
485         uint32_t mypnn;
486         bool ret;
487
488         ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
489         if (!ret) {
490                 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
491                 return -1;
492         }
493
494         printf("PNN:%d\n", mypnn);
495         return 0;
496 }
497
498
499 struct pnn_node {
500         struct pnn_node *next;
501         const char *addr;
502         int pnn;
503 };
504
505 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
506 {
507         const char *nodes_list;
508         int nlines;
509         char **lines;
510         int i, pnn;
511         struct pnn_node *pnn_nodes = NULL;
512         struct pnn_node *pnn_node;
513         struct pnn_node *tmp_node;
514
515         /* read the nodes file */
516         nodes_list = getenv("CTDB_NODES");
517         if (nodes_list == NULL) {
518                 nodes_list = "/etc/ctdb/nodes";
519         }
520         lines = file_lines_load(nodes_list, &nlines, mem_ctx);
521         if (lines == NULL) {
522                 return NULL;
523         }
524         while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
525                 nlines--;
526         }
527         for (i=0, pnn=0; i<nlines; i++) {
528                 char *node;
529
530                 node = lines[i];
531                 /* strip leading spaces */
532                 while((*node == ' ') || (*node == '\t')) {
533                         node++;
534                 }
535                 if (*node == '#') {
536                         pnn++;
537                         continue;
538                 }
539                 if (strcmp(node, "") == 0) {
540                         continue;
541                 }
542                 pnn_node = talloc(mem_ctx, struct pnn_node);
543                 pnn_node->pnn = pnn++;
544                 pnn_node->addr = talloc_strdup(pnn_node, node);
545                 pnn_node->next = pnn_nodes;
546                 pnn_nodes = pnn_node;
547         }
548
549         /* swap them around so we return them in incrementing order */
550         pnn_node = pnn_nodes;
551         pnn_nodes = NULL;
552         while (pnn_node) {
553                 tmp_node = pnn_node;
554                 pnn_node = pnn_node->next;
555
556                 tmp_node->next = pnn_nodes;
557                 pnn_nodes = tmp_node;
558         }
559
560         return pnn_nodes;
561 }
562
563 /*
564   show the PNN of the current node
565   discover the pnn by loading the nodes file and try to bind to all
566   addresses one at a time until the ip address is found.
567  */
568 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
569 {
570         TALLOC_CTX *mem_ctx = talloc_new(NULL);
571         struct pnn_node *pnn_nodes;
572         struct pnn_node *pnn_node;
573
574         pnn_nodes = read_nodes_file(mem_ctx);
575         if (pnn_nodes == NULL) {
576                 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
577                 talloc_free(mem_ctx);
578                 return -1;
579         }
580
581         for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
582                 ctdb_sock_addr addr;
583
584                 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
585                         DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
586                         talloc_free(mem_ctx);
587                         return -1;
588                 }
589
590                 if (ctdb_sys_have_ip(&addr)) {
591                         printf("PNN:%d\n", pnn_node->pnn);
592                         talloc_free(mem_ctx);
593                         return 0;
594                 }
595         }
596
597         printf("Failed to detect which PNN this node is\n");
598         talloc_free(mem_ctx);
599         return -1;
600 }
601
602 /*
603   display remote ctdb status
604  */
605 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
606 {
607         int i, ret;
608         struct ctdb_vnn_map *vnnmap=NULL;
609         struct ctdb_node_map *nodemap=NULL;
610         uint32_t recmode, recmaster;
611         int mypnn;
612
613         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
614         if (mypnn == -1) {
615                 return -1;
616         }
617
618         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
619         if (ret != 0) {
620                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
621                 return ret;
622         }
623
624         if(options.machinereadable){
625                 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped:Inactive:\n");
626                 for(i=0;i<nodemap->num;i++){
627                         if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
628                                 continue;
629                         }
630                         printf(":%d:%s:%d:%d:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
631                                 ctdb_addr_to_str(&nodemap->nodes[i].addr),
632                                !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
633                                !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
634                                !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
635                                !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
636                                !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED),
637                                !!(nodemap->nodes[i].flags&NODE_FLAGS_INACTIVE));
638                 }
639                 return 0;
640         }
641
642         printf("Number of nodes:%d\n", nodemap->num);
643         for(i=0;i<nodemap->num;i++){
644                 static const struct {
645                         uint32_t flag;
646                         const char *name;
647                 } flag_names[] = {
648                         { NODE_FLAGS_DISCONNECTED,          "DISCONNECTED" },
649                         { NODE_FLAGS_PERMANENTLY_DISABLED,  "DISABLED" },
650                         { NODE_FLAGS_BANNED,                "BANNED" },
651                         { NODE_FLAGS_UNHEALTHY,             "UNHEALTHY" },
652                         { NODE_FLAGS_DELETED,               "DELETED" },
653                         { NODE_FLAGS_STOPPED,               "STOPPED" },
654                         { NODE_FLAGS_INACTIVE,              "INACTIVE" },
655                 };
656                 char *flags_str = NULL;
657                 int j;
658
659                 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
660                         continue;
661                 }
662                 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
663                         if (nodemap->nodes[i].flags & flag_names[j].flag) {
664                                 if (flags_str == NULL) {
665                                         flags_str = talloc_strdup(ctdb, flag_names[j].name);
666                                 } else {
667                                         flags_str = talloc_asprintf_append(flags_str, "|%s",
668                                                                            flag_names[j].name);
669                                 }
670                                 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
671                         }
672                 }
673                 if (flags_str == NULL) {
674                         flags_str = talloc_strdup(ctdb, "OK");
675                         CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
676                 }
677                 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
678                        ctdb_addr_to_str(&nodemap->nodes[i].addr),
679                        flags_str,
680                        nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
681                 talloc_free(flags_str);
682         }
683
684         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
685         if (ret != 0) {
686                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
687                 return ret;
688         }
689         if (vnnmap->generation == INVALID_GENERATION) {
690                 printf("Generation:INVALID\n");
691         } else {
692                 printf("Generation:%d\n",vnnmap->generation);
693         }
694         printf("Size:%d\n",vnnmap->size);
695         for(i=0;i<vnnmap->size;i++){
696                 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
697         }
698
699         ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
700         if (ret != 0) {
701                 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
702                 return ret;
703         }
704         printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
705
706         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
707         if (ret != 0) {
708                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
709                 return ret;
710         }
711         printf("Recovery master:%d\n",recmaster);
712
713         return 0;
714 }
715
716
717 struct natgw_node {
718         struct natgw_node *next;
719         const char *addr;
720 };
721
722 /*
723   display the list of nodes belonging to this natgw configuration
724  */
725 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
726 {
727         int i, ret;
728         uint32_t capabilities;
729         const char *natgw_list;
730         int nlines;
731         char **lines;
732         struct natgw_node *natgw_nodes = NULL;
733         struct natgw_node *natgw_node;
734         struct ctdb_node_map *nodemap=NULL;
735
736
737         /* read the natgw nodes file into a linked list */
738         natgw_list = getenv("NATGW_NODES");
739         if (natgw_list == NULL) {
740                 natgw_list = "/etc/ctdb/natgw_nodes";
741         }
742         lines = file_lines_load(natgw_list, &nlines, ctdb);
743         if (lines == NULL) {
744                 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
745                 return -1;
746         }
747         while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
748                 nlines--;
749         }
750         for (i=0;i<nlines;i++) {
751                 char *node;
752
753                 node = lines[i];
754                 /* strip leading spaces */
755                 while((*node == ' ') || (*node == '\t')) {
756                         node++;
757                 }
758                 if (*node == '#') {
759                         continue;
760                 }
761                 if (strcmp(node, "") == 0) {
762                         continue;
763                 }
764                 natgw_node = talloc(ctdb, struct natgw_node);
765                 natgw_node->addr = talloc_strdup(natgw_node, node);
766                 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
767                 natgw_node->next = natgw_nodes;
768                 natgw_nodes = natgw_node;
769         }
770
771         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
772         if (ret != 0) {
773                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
774                 return ret;
775         }
776
777         i=0;
778         while(i<nodemap->num) {
779                 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
780                         if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
781                                 break;
782                         }
783                 }
784
785                 /* this node was not in the natgw so we just remove it from
786                  * the list
787                  */
788                 if ((natgw_node == NULL) 
789                 ||  (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
790                         int j;
791
792                         for (j=i+1; j<nodemap->num; j++) {
793                                 nodemap->nodes[j-1] = nodemap->nodes[j];
794                         }
795                         nodemap->num--;
796                         continue;
797                 }
798
799                 i++;
800         }               
801
802         /* pick a node to be natgwmaster
803          * we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
804          */
805         for(i=0;i<nodemap->num;i++){
806                 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
807                         ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
808                         if (ret != 0) {
809                                 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
810                                 return ret;
811                         }
812                         if (!(capabilities&CTDB_CAP_NATGW)) {
813                                 continue;
814                         }
815                         printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
816                         break;
817                 }
818         }
819         /* we couldnt find any healthy node, try unhealthy ones */
820         if (i == nodemap->num) {
821                 for(i=0;i<nodemap->num;i++){
822                         if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
823                                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
824                                 if (ret != 0) {
825                                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
826                                         return ret;
827                                 }
828                                 if (!(capabilities&CTDB_CAP_NATGW)) {
829                                         continue;
830                                 }
831                                 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
832                                 break;
833                         }
834                 }
835         }
836         /* unless all nodes are STOPPED, when we pick one anyway */
837         if (i == nodemap->num) {
838                 for(i=0;i<nodemap->num;i++){
839                         if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
840                                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
841                                 if (ret != 0) {
842                                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
843                                         return ret;
844                                 }
845                                 if (!(capabilities&CTDB_CAP_NATGW)) {
846                                         continue;
847                                 }
848                                 printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
849                                 break;
850                         }
851                 }
852                 /* or if we still can not find any */
853                 if (i == nodemap->num) {
854                         printf("-1 0.0.0.0\n");
855                 }
856         }
857
858         /* print the pruned list of nodes belonging to this natgw list */
859         for(i=0;i<nodemap->num;i++){
860                 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
861                         continue;
862                 }
863                 printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
864                         ctdb_addr_to_str(&nodemap->nodes[i].addr),
865                        !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
866                        !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
867                        !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
868                        !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
869                        !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
870         }
871
872         return 0;
873 }
874
875 /*
876   display the status of the scripts for monitoring (or other events)
877  */
878 static int control_one_scriptstatus(struct ctdb_context *ctdb,
879                                     enum ctdb_eventscript_call type)
880 {
881         struct ctdb_scripts_wire *script_status;
882         int ret, i;
883
884         ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
885         if (ret != 0) {
886                 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
887                 return ret;
888         }
889
890         if (script_status == NULL) {
891                 if (!options.machinereadable) {
892                         printf("%s cycle never run\n",
893                                ctdb_eventscript_call_names[type]);
894                 }
895                 return 0;
896         }
897
898         if (!options.machinereadable) {
899                 printf("%d scripts were executed last %s cycle\n",
900                        script_status->num_scripts,
901                        ctdb_eventscript_call_names[type]);
902         }
903         for (i=0; i<script_status->num_scripts; i++) {
904                 const char *status = NULL;
905
906                 switch (script_status->scripts[i].status) {
907                 case -ETIME:
908                         status = "TIMEDOUT";
909                         break;
910                 case -ENOEXEC:
911                         status = "DISABLED";
912                         break;
913                 case 0:
914                         status = "OK";
915                         break;
916                 default:
917                         if (script_status->scripts[i].status > 0)
918                                 status = "ERROR";
919                         break;
920                 }
921                 if (options.machinereadable) {
922                         printf("%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
923                                ctdb_eventscript_call_names[type],
924                                script_status->scripts[i].name,
925                                script_status->scripts[i].status,
926                                status,
927                                (long)script_status->scripts[i].start.tv_sec,
928                                (long)script_status->scripts[i].start.tv_usec,
929                                (long)script_status->scripts[i].finished.tv_sec,
930                                (long)script_status->scripts[i].finished.tv_usec,
931                                script_status->scripts[i].output);
932                         continue;
933                 }
934                 if (status)
935                         printf("%-20s Status:%s    ",
936                                script_status->scripts[i].name, status);
937                 else
938                         /* Some other error, eg from stat. */
939                         printf("%-20s Status:CANNOT RUN (%s)",
940                                script_status->scripts[i].name,
941                                strerror(-script_status->scripts[i].status));
942
943                 if (script_status->scripts[i].status >= 0) {
944                         printf("Duration:%.3lf ",
945                         timeval_delta(&script_status->scripts[i].finished,
946                               &script_status->scripts[i].start));
947                 }
948                 if (script_status->scripts[i].status != -ENOEXEC) {
949                         printf("%s",
950                                ctime(&script_status->scripts[i].start.tv_sec));
951                         if (script_status->scripts[i].status != 0) {
952                                 printf("   OUTPUT:%s\n",
953                                        script_status->scripts[i].output);
954                         }
955                 } else {
956                         printf("\n");
957                 }
958         }
959         return 0;
960 }
961
962
963 static int control_scriptstatus(struct ctdb_context *ctdb,
964                                 int argc, const char **argv)
965 {
966         int ret;
967         enum ctdb_eventscript_call type, min, max;
968         const char *arg;
969
970         if (argc > 1) {
971                 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
972                 return -1;
973         }
974
975         if (argc == 0)
976                 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
977         else
978                 arg = argv[0];
979
980         for (type = 0; type < CTDB_EVENT_MAX; type++) {
981                 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
982                         min = type;
983                         max = type+1;
984                         break;
985                 }
986         }
987         if (type == CTDB_EVENT_MAX) {
988                 if (strcmp(arg, "all") == 0) {
989                         min = 0;
990                         max = CTDB_EVENT_MAX;
991                 } else {
992                         DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
993                         return -1;
994                 }
995         }
996
997         if (options.machinereadable) {
998                 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
999         }
1000
1001         for (type = min; type < max; type++) {
1002                 ret = control_one_scriptstatus(ctdb, type);
1003                 if (ret != 0) {
1004                         return ret;
1005                 }
1006         }
1007
1008         return 0;
1009 }
1010
1011 /*
1012   enable an eventscript
1013  */
1014 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1015 {
1016         int ret;
1017
1018         if (argc < 1) {
1019                 usage();
1020         }
1021
1022         ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1023         if (ret != 0) {
1024           DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1025                 return ret;
1026         }
1027
1028         return 0;
1029 }
1030
1031 /*
1032   disable an eventscript
1033  */
1034 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1035 {
1036         int ret;
1037
1038         if (argc < 1) {
1039                 usage();
1040         }
1041
1042         ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1043         if (ret != 0) {
1044           DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1045                 return ret;
1046         }
1047
1048         return 0;
1049 }
1050
1051 /*
1052   display the pnn of the recovery master
1053  */
1054 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1055 {
1056         int ret;
1057         uint32_t recmaster;
1058
1059         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1060         if (ret != 0) {
1061                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1062                 return ret;
1063         }
1064         printf("%d\n",recmaster);
1065
1066         return 0;
1067 }
1068
1069 /*
1070   add a tickle to a public address
1071  */
1072 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1073 {
1074         struct ctdb_tcp_connection t;
1075         TDB_DATA data;
1076         int ret;
1077
1078         if (argc < 2) {
1079                 usage();
1080         }
1081
1082         if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1083                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1084                 return -1;
1085         }
1086         if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1087                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1088                 return -1;
1089         }
1090
1091         data.dptr = (uint8_t *)&t;
1092         data.dsize = sizeof(t);
1093
1094         /* tell all nodes about this tcp connection */
1095         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1096                            0, data, ctdb, NULL, NULL, NULL, NULL);
1097         if (ret != 0) {
1098                 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1099                 return -1;
1100         }
1101         
1102         return 0;
1103 }
1104
1105
1106 /*
1107   delete a tickle from a node
1108  */
1109 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1110 {
1111         struct ctdb_tcp_connection t;
1112         TDB_DATA data;
1113         int ret;
1114
1115         if (argc < 2) {
1116                 usage();
1117         }
1118
1119         if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1120                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1121                 return -1;
1122         }
1123         if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1124                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1125                 return -1;
1126         }
1127
1128         data.dptr = (uint8_t *)&t;
1129         data.dsize = sizeof(t);
1130
1131         /* tell all nodes about this tcp connection */
1132         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1133                            0, data, ctdb, NULL, NULL, NULL, NULL);
1134         if (ret != 0) {
1135                 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1136                 return -1;
1137         }
1138         
1139         return 0;
1140 }
1141
1142
1143 /*
1144   get a list of all tickles for this pnn
1145  */
1146 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1147 {
1148         struct ctdb_control_tcp_tickle_list *list;
1149         ctdb_sock_addr addr;
1150         int i, ret;
1151         unsigned port = 0;
1152
1153         if (argc < 1) {
1154                 usage();
1155         }
1156
1157         if (argc == 2) {
1158                 port = atoi(argv[1]);
1159         }
1160
1161         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1162                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1163                 return -1;
1164         }
1165
1166         ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1167         if (ret == -1) {
1168                 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1169                 return -1;
1170         }
1171
1172         if (options.machinereadable){
1173                 printf(":source ip:port:destination ip:port:\n");
1174                 for (i=0;i<list->tickles.num;i++) {
1175                         if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1176                                 continue;
1177                         }
1178                         printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1179                         printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1180                 }
1181         } else {
1182                 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1183                 printf("Num tickles:%u\n", list->tickles.num);
1184                 for (i=0;i<list->tickles.num;i++) {
1185                         if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1186                                 continue;
1187                         }
1188                         printf("SRC: %s:%u   ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1189                         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));
1190                 }
1191         }
1192
1193         talloc_free(list);
1194         
1195         return 0;
1196 }
1197
1198
1199 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1200 {
1201         struct ctdb_all_public_ips *ips;
1202         struct ctdb_public_ip ip;
1203         int i, ret;
1204         uint32_t *nodes;
1205         uint32_t disable_time;
1206         TDB_DATA data;
1207         struct ctdb_node_map *nodemap=NULL;
1208         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1209
1210         disable_time = 30;
1211         data.dptr  = (uint8_t*)&disable_time;
1212         data.dsize = sizeof(disable_time);
1213         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1214         if (ret != 0) {
1215                 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1216                 return -1;
1217         }
1218
1219
1220
1221         /* read the public ip list from the node */
1222         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1223         if (ret != 0) {
1224                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1225                 talloc_free(tmp_ctx);
1226                 return -1;
1227         }
1228
1229         for (i=0;i<ips->num;i++) {
1230                 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1231                         break;
1232                 }
1233         }
1234         if (i==ips->num) {
1235                 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1236                         pnn, ctdb_addr_to_str(addr)));
1237                 talloc_free(tmp_ctx);
1238                 return -1;
1239         }
1240
1241         ip.pnn  = pnn;
1242         ip.addr = *addr;
1243
1244         data.dptr  = (uint8_t *)&ip;
1245         data.dsize = sizeof(ip);
1246
1247         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1248         if (ret != 0) {
1249                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1250                 talloc_free(tmp_ctx);
1251                 return ret;
1252         }
1253
1254         nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1255         ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1256                                         nodes, 0,
1257                                         LONGTIMELIMIT(),
1258                                         false, data,
1259                                         NULL, NULL,
1260                                         NULL);
1261         if (ret != 0) {
1262                 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1263                 talloc_free(tmp_ctx);
1264                 return -1;
1265         }
1266
1267         ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1268         if (ret != 0) {
1269                 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1270                 talloc_free(tmp_ctx);
1271                 return -1;
1272         }
1273
1274         /* update the recovery daemon so it now knows to expect the new
1275            node assignment for this ip.
1276         */
1277         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1278         if (ret != 0) {
1279                 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1280                 return -1;
1281         }
1282
1283         talloc_free(tmp_ctx);
1284         return 0;
1285 }
1286
1287 /*
1288   move/failover an ip address to a specific node
1289  */
1290 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1291 {
1292         uint32_t pnn;
1293         int ret, retries = 0;
1294         ctdb_sock_addr addr;
1295
1296         if (argc < 2) {
1297                 usage();
1298                 return -1;
1299         }
1300
1301         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1302                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1303                 return -1;
1304         }
1305
1306
1307         if (sscanf(argv[1], "%u", &pnn) != 1) {
1308                 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1309                 return -1;
1310         }
1311
1312         do {
1313                 ret = move_ip(ctdb, &addr, pnn);
1314                 if (ret != 0) {
1315                         DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 second and try again.\n", pnn));
1316                         sleep(3);
1317                         retries++;
1318                 }
1319         } while (retries < 5 && ret != 0);
1320         if (ret != 0) {
1321                 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1322                 return -1;
1323         }
1324
1325         return 0;
1326 }
1327
1328 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1329 {
1330         struct ctdb_public_ip ip;
1331         int ret;
1332         uint32_t *nodes;
1333         uint32_t disable_time;
1334         TDB_DATA data;
1335         struct ctdb_node_map *nodemap=NULL;
1336         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1337
1338         disable_time = 30;
1339         data.dptr  = (uint8_t*)&disable_time;
1340         data.dsize = sizeof(disable_time);
1341         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1342         if (ret != 0) {
1343                 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1344                 return -1;
1345         }
1346
1347         ip.pnn  = -1;
1348         ip.addr = *addr;
1349
1350         data.dptr  = (uint8_t *)&ip;
1351         data.dsize = sizeof(ip);
1352
1353         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1354         if (ret != 0) {
1355                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1356                 talloc_free(tmp_ctx);
1357                 return ret;
1358         }
1359
1360         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1361         ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1362                                         nodes, 0,
1363                                         LONGTIMELIMIT(),
1364                                         false, data,
1365                                         NULL, NULL,
1366                                         NULL);
1367         if (ret != 0) {
1368                 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1369                 talloc_free(tmp_ctx);
1370                 return -1;
1371         }
1372
1373         talloc_free(tmp_ctx);
1374         return 0;
1375 }
1376
1377 /*
1378   release an ip form all nodes and have it re-assigned by recd
1379  */
1380 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1381 {
1382         ctdb_sock_addr addr;
1383
1384         if (argc < 1) {
1385                 usage();
1386                 return -1;
1387         }
1388
1389         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1390                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1391                 return -1;
1392         }
1393
1394         if (rebalance_ip(ctdb, &addr) != 0) {
1395                 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
1396                 return -1;
1397         }
1398
1399         return 0;
1400 }
1401
1402 void getips_store_callback(void *param, void *data)
1403 {
1404         struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1405         struct ctdb_all_public_ips *ips = param;
1406         int i;
1407
1408         i = ips->num++;
1409         ips->ips[i].pnn  = node_ip->pnn;
1410         ips->ips[i].addr = node_ip->addr;
1411 }
1412
1413 void getips_count_callback(void *param, void *data)
1414 {
1415         uint32_t *count = param;
1416
1417         (*count)++;
1418 }
1419
1420 #define IP_KEYLEN       4
1421 static uint32_t *ip_key(ctdb_sock_addr *ip)
1422 {
1423         static uint32_t key[IP_KEYLEN];
1424
1425         bzero(key, sizeof(key));
1426
1427         switch (ip->sa.sa_family) {
1428         case AF_INET:
1429                 key[0]  = ip->ip.sin_addr.s_addr;
1430                 break;
1431         case AF_INET6:
1432                 key[0]  = ip->ip6.sin6_addr.s6_addr32[3];
1433                 key[1]  = ip->ip6.sin6_addr.s6_addr32[2];
1434                 key[2]  = ip->ip6.sin6_addr.s6_addr32[1];
1435                 key[3]  = ip->ip6.sin6_addr.s6_addr32[0];
1436                 break;
1437         default:
1438                 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1439                 return key;
1440         }
1441
1442         return key;
1443 }
1444
1445 static void *add_ip_callback(void *parm, void *data)
1446 {
1447         return parm;
1448 }
1449
1450 static int
1451 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1452 {
1453         struct ctdb_all_public_ips *tmp_ips;
1454         struct ctdb_node_map *nodemap=NULL;
1455         trbt_tree_t *ip_tree;
1456         int i, j, len, ret;
1457         uint32_t count;
1458
1459         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1460         if (ret != 0) {
1461                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1462                 return ret;
1463         }
1464
1465         ip_tree = trbt_create(tmp_ctx, 0);
1466
1467         for(i=0;i<nodemap->num;i++){
1468                 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1469                         continue;
1470                 }
1471                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1472                         continue;
1473                 }
1474
1475                 /* read the public ip list from this node */
1476                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1477                 if (ret != 0) {
1478                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1479                         return -1;
1480                 }
1481         
1482                 for (j=0; j<tmp_ips->num;j++) {
1483                         struct ctdb_public_ip *node_ip;
1484
1485                         node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1486                         node_ip->pnn  = tmp_ips->ips[j].pnn;
1487                         node_ip->addr = tmp_ips->ips[j].addr;
1488
1489                         trbt_insertarray32_callback(ip_tree,
1490                                 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1491                                 add_ip_callback,
1492                                 node_ip);
1493                 }
1494                 talloc_free(tmp_ips);
1495         }
1496
1497         /* traverse */
1498         count = 0;
1499         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1500
1501         len = offsetof(struct ctdb_all_public_ips, ips) + 
1502                 count*sizeof(struct ctdb_public_ip);
1503         tmp_ips = talloc_zero_size(tmp_ctx, len);
1504         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1505
1506         *ips = tmp_ips;
1507
1508         return 0;
1509 }
1510
1511
1512 /* 
1513  * scans all other nodes and returns a pnn for another node that can host this 
1514  * ip address or -1
1515  */
1516 static int
1517 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1518 {
1519         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1520         struct ctdb_all_public_ips *ips;
1521         struct ctdb_node_map *nodemap=NULL;
1522         int i, j, ret;
1523
1524         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1525         if (ret != 0) {
1526                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1527                 talloc_free(tmp_ctx);
1528                 return ret;
1529         }
1530
1531         for(i=0;i<nodemap->num;i++){
1532                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1533                         continue;
1534                 }
1535                 if (nodemap->nodes[i].pnn == options.pnn) {
1536                         continue;
1537                 }
1538
1539                 /* read the public ip list from this node */
1540                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1541                 if (ret != 0) {
1542                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1543                         return -1;
1544                 }
1545
1546                 for (j=0;j<ips->num;j++) {
1547                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1548                                 talloc_free(tmp_ctx);
1549                                 return nodemap->nodes[i].pnn;
1550                         }
1551                 }
1552                 talloc_free(ips);
1553         }
1554
1555         talloc_free(tmp_ctx);
1556         return -1;
1557 }
1558
1559 static uint32_t ipreallocate_finished;
1560
1561 /*
1562   handler for receiving the response to ipreallocate
1563 */
1564 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid, 
1565                              TDB_DATA data, void *private_data)
1566 {
1567         ipreallocate_finished = 1;
1568 }
1569
1570 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1571 {
1572         struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1573
1574         event_add_timed(ctdb->ev, ctdb, 
1575                                 timeval_current_ofs(1, 0),
1576                                 ctdb_every_second, ctdb);
1577 }
1578
1579 /*
1580   ask the recovery daemon on the recovery master to perform a ip reallocation
1581  */
1582 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
1583 {
1584         int i, ret;
1585         TDB_DATA data;
1586         struct takeover_run_reply rd;
1587         uint32_t recmaster;
1588         struct ctdb_node_map *nodemap=NULL;
1589         int retries=0;
1590         struct timeval tv = timeval_current();
1591
1592         /* we need some events to trigger so we can timeout and restart
1593            the loop
1594         */
1595         event_add_timed(ctdb->ev, ctdb, 
1596                                 timeval_current_ofs(1, 0),
1597                                 ctdb_every_second, ctdb);
1598
1599         rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1600         if (rd.pnn == -1) {
1601                 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1602                 return -1;
1603         }
1604         rd.srvid = getpid();
1605
1606         /* register a message port for receiveing the reply so that we
1607            can receive the reply
1608         */
1609         ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1610
1611         data.dptr = (uint8_t *)&rd;
1612         data.dsize = sizeof(rd);
1613
1614 again:
1615         /* check that there are valid nodes available */
1616         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
1617                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1618                 return -1;
1619         }
1620         for (i=0; i<nodemap->num;i++) {
1621                 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
1622                         break;
1623                 }
1624         }
1625         if (i==nodemap->num) {
1626                 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
1627                 return 0;
1628         }
1629
1630
1631         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1632         if (ret != 0) {
1633                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1634                 return ret;
1635         }
1636
1637         /* verify the node exists */
1638         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
1639                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1640                 return -1;
1641         }
1642
1643
1644         /* check tha there are nodes available that can act as a recmaster */
1645         for (i=0; i<nodemap->num; i++) {
1646                 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1647                         continue;
1648                 }
1649                 break;
1650         }
1651         if (i == nodemap->num) {
1652                 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
1653                 return 0;
1654         }
1655
1656         /* verify the recovery master is not STOPPED, nor BANNED */
1657         if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1658                 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1659                 retries++;
1660                 sleep(1);
1661                 goto again;
1662         } 
1663         
1664         /* verify the recovery master is not STOPPED, nor BANNED */
1665         if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1666                 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1667                 retries++;
1668                 sleep(1);
1669                 goto again;
1670         } 
1671
1672         ipreallocate_finished = 0;
1673         ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
1674         if (ret != 0) {
1675                 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
1676                 return -1;
1677         }
1678
1679         tv = timeval_current();
1680         /* this loop will terminate when we have received the reply */
1681         while (timeval_elapsed(&tv) < 5.0 && ipreallocate_finished == 0) {
1682                 event_loop_once(ctdb->ev);
1683         }
1684         if (ipreallocate_finished == 1) {
1685                 return 0;
1686         }
1687
1688         retries++;
1689         sleep(1);
1690         goto again;
1691
1692         return 0;
1693 }
1694
1695
1696 /*
1697   add a public ip address to a node
1698  */
1699 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
1700 {
1701         int i, ret;
1702         int len, retries = 0;
1703         unsigned mask;
1704         ctdb_sock_addr addr;
1705         struct ctdb_control_ip_iface *pub;
1706         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1707         struct ctdb_all_public_ips *ips;
1708
1709
1710         if (argc != 2) {
1711                 talloc_free(tmp_ctx);
1712                 usage();
1713         }
1714
1715         if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1716                 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1717                 talloc_free(tmp_ctx);
1718                 return -1;
1719         }
1720
1721         /* read the public ip list from the node */
1722         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1723         if (ret != 0) {
1724                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
1725                 talloc_free(tmp_ctx);
1726                 return -1;
1727         }
1728         for (i=0;i<ips->num;i++) {
1729                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1730                         DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
1731                         return 0;
1732                 }
1733         }
1734
1735
1736
1737         /* Dont timeout. This command waits for an ip reallocation
1738            which sometimes can take quite a while if there has
1739            been a recent recovery
1740         */
1741         alarm(0);
1742
1743         len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1744         pub = talloc_size(tmp_ctx, len); 
1745         CTDB_NO_MEMORY(ctdb, pub);
1746
1747         pub->addr  = addr;
1748         pub->mask  = mask;
1749         pub->len   = strlen(argv[1])+1;
1750         memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1751
1752         do {
1753                 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1754                 if (ret != 0) {
1755                         DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
1756                         sleep(3);
1757                         retries++;
1758                 }
1759         } while (retries < 5 && ret != 0);
1760         if (ret != 0) {
1761                 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
1762                 talloc_free(tmp_ctx);
1763                 return ret;
1764         }
1765
1766         if (rebalance_ip(ctdb, &addr) != 0) {
1767                 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
1768                 return -1;
1769         }
1770
1771         talloc_free(tmp_ctx);
1772         return 0;
1773 }
1774
1775 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
1776
1777 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
1778 {
1779         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1780         struct ctdb_node_map *nodemap=NULL;
1781         struct ctdb_all_public_ips *ips;
1782         int ret, i, j;
1783
1784         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1785         if (ret != 0) {
1786                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
1787                 return ret;
1788         }
1789
1790         /* remove it from the nodes that are not hosting the ip currently */
1791         for(i=0;i<nodemap->num;i++){
1792                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1793                         continue;
1794                 }
1795                 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1796                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1797                         continue;
1798                 }
1799
1800                 for (j=0;j<ips->num;j++) {
1801                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1802                                 break;
1803                         }
1804                 }
1805                 if (j==ips->num) {
1806                         continue;
1807                 }
1808
1809                 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
1810                         continue;
1811                 }
1812
1813                 options.pnn = nodemap->nodes[i].pnn;
1814                 control_delip(ctdb, argc, argv);
1815         }
1816
1817
1818         /* remove it from every node (also the one hosting it) */
1819         for(i=0;i<nodemap->num;i++){
1820                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1821                         continue;
1822                 }
1823                 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1824                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1825                         continue;
1826                 }
1827
1828                 for (j=0;j<ips->num;j++) {
1829                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1830                                 break;
1831                         }
1832                 }
1833                 if (j==ips->num) {
1834                         continue;
1835                 }
1836
1837                 options.pnn = nodemap->nodes[i].pnn;
1838                 control_delip(ctdb, argc, argv);
1839         }
1840
1841         talloc_free(tmp_ctx);
1842         return 0;
1843 }
1844         
1845 /*
1846   delete a public ip address from a node
1847  */
1848 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
1849 {
1850         int i, ret;
1851         int retries = 0;
1852         ctdb_sock_addr addr;
1853         struct ctdb_control_ip_iface pub;
1854         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1855         struct ctdb_all_public_ips *ips;
1856
1857         if (argc != 1) {
1858                 talloc_free(tmp_ctx);
1859                 usage();
1860         }
1861
1862         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1863                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1864                 return -1;
1865         }
1866
1867         if (options.pnn == CTDB_BROADCAST_ALL) {
1868                 return control_delip_all(ctdb, argc, argv, &addr);
1869         }
1870
1871         pub.addr  = addr;
1872         pub.mask  = 0;
1873         pub.len   = 0;
1874
1875         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1876         if (ret != 0) {
1877                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1878                 talloc_free(tmp_ctx);
1879                 return ret;
1880         }
1881         
1882         for (i=0;i<ips->num;i++) {
1883                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1884                         break;
1885                 }
1886         }
1887
1888         if (i==ips->num) {
1889                 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
1890                         ctdb_addr_to_str(&addr)));
1891                 talloc_free(tmp_ctx);
1892                 return -1;
1893         }
1894
1895         if (ips->ips[i].pnn == options.pnn) {
1896                 ret = find_other_host_for_public_ip(ctdb, &addr);
1897                 if (ret != -1) {
1898                         do {
1899                                 ret = move_ip(ctdb, &addr, ret);
1900                                 if (ret != 0) {
1901                                         DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 seconds and try again.\n", options.pnn));
1902                                         sleep(3);
1903                                         retries++;
1904                                 }
1905                         } while (retries < 5 && ret != 0);
1906                         if (ret != 0) {
1907                                 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", options.pnn));
1908                                 return -1;
1909                         }
1910                 }
1911         }
1912
1913         ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
1914         if (ret != 0) {
1915                 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
1916                 talloc_free(tmp_ctx);
1917                 return ret;
1918         }
1919
1920         talloc_free(tmp_ctx);
1921         return 0;
1922 }
1923
1924 /*
1925   kill a tcp connection
1926  */
1927 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1928 {
1929         int ret;
1930         struct ctdb_control_killtcp killtcp;
1931
1932         if (argc < 2) {
1933                 usage();
1934         }
1935
1936         if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
1937                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1938                 return -1;
1939         }
1940
1941         if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
1942                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1943                 return -1;
1944         }
1945
1946         ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
1947         if (ret != 0) {
1948                 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1949                 return ret;
1950         }
1951
1952         return 0;
1953 }
1954
1955
1956 /*
1957   send a gratious arp
1958  */
1959 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1960 {
1961         int ret;
1962         ctdb_sock_addr addr;
1963
1964         if (argc < 2) {
1965                 usage();
1966         }
1967
1968         if (!parse_ip(argv[0], NULL, 0, &addr)) {
1969                 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1970                 return -1;
1971         }
1972
1973         ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1974         if (ret != 0) {
1975                 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1976                 return ret;
1977         }
1978
1979         return 0;
1980 }
1981
1982 /*
1983   register a server id
1984  */
1985 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1986 {
1987         int ret;
1988         struct ctdb_server_id server_id;
1989
1990         if (argc < 3) {
1991                 usage();
1992         }
1993
1994         server_id.pnn       = strtoul(argv[0], NULL, 0);
1995         server_id.type      = strtoul(argv[1], NULL, 0);
1996         server_id.server_id = strtoul(argv[2], NULL, 0);
1997
1998         ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1999         if (ret != 0) {
2000                 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2001                 return ret;
2002         }
2003         DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2004         sleep(999);
2005         return -1;
2006 }
2007
2008 /*
2009   unregister a server id
2010  */
2011 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2012 {
2013         int ret;
2014         struct ctdb_server_id server_id;
2015
2016         if (argc < 3) {
2017                 usage();
2018         }
2019
2020         server_id.pnn       = strtoul(argv[0], NULL, 0);
2021         server_id.type      = strtoul(argv[1], NULL, 0);
2022         server_id.server_id = strtoul(argv[2], NULL, 0);
2023
2024         ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2025         if (ret != 0) {
2026                 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2027                 return ret;
2028         }
2029         return -1;
2030 }
2031
2032 /*
2033   check if a server id exists
2034  */
2035 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2036 {
2037         uint32_t status;
2038         int ret;
2039         struct ctdb_server_id server_id;
2040
2041         if (argc < 3) {
2042                 usage();
2043         }
2044
2045         server_id.pnn       = strtoul(argv[0], NULL, 0);
2046         server_id.type      = strtoul(argv[1], NULL, 0);
2047         server_id.server_id = strtoul(argv[2], NULL, 0);
2048
2049         ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2050         if (ret != 0) {
2051                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2052                 return ret;
2053         }
2054
2055         if (status) {
2056                 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2057         } else {
2058                 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2059         }
2060         return 0;
2061 }
2062
2063 /*
2064   get a list of all server ids that are registered on a node
2065  */
2066 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2067 {
2068         int i, ret;
2069         struct ctdb_server_id_list *server_ids;
2070
2071         ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2072         if (ret != 0) {
2073                 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2074                 return ret;
2075         }
2076
2077         for (i=0; i<server_ids->num; i++) {
2078                 printf("Server id %d:%d:%d\n", 
2079                         server_ids->server_ids[i].pnn, 
2080                         server_ids->server_ids[i].type, 
2081                         server_ids->server_ids[i].server_id); 
2082         }
2083
2084         return -1;
2085 }
2086
2087 /*
2088   send a tcp tickle ack
2089  */
2090 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2091 {
2092         int ret;
2093         ctdb_sock_addr  src, dst;
2094
2095         if (argc < 2) {
2096                 usage();
2097         }
2098
2099         if (!parse_ip_port(argv[0], &src)) {
2100                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2101                 return -1;
2102         }
2103
2104         if (!parse_ip_port(argv[1], &dst)) {
2105                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2106                 return -1;
2107         }
2108
2109         ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2110         if (ret==0) {
2111                 return 0;
2112         }
2113         DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2114
2115         return -1;
2116 }
2117
2118
2119 /*
2120   display public ip status
2121  */
2122 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2123 {
2124         int i, ret;
2125         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2126         struct ctdb_all_public_ips *ips;
2127
2128         if (options.pnn == CTDB_BROADCAST_ALL) {
2129                 /* read the list of public ips from all nodes */
2130                 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2131         } else {
2132                 /* read the public ip list from this node */
2133                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2134         }
2135         if (ret != 0) {
2136                 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2137                 talloc_free(tmp_ctx);
2138                 return ret;
2139         }
2140
2141         if (options.machinereadable){
2142                 printf(":Public IP:Node:");
2143                 if (options.verbose){
2144                         printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2145                 }
2146                 printf("\n");
2147         } else {
2148                 if (options.pnn == CTDB_BROADCAST_ALL) {
2149                         printf("Public IPs on ALL nodes\n");
2150                 } else {
2151                         printf("Public IPs on node %u\n", options.pnn);
2152                 }
2153         }
2154
2155         for (i=1;i<=ips->num;i++) {
2156                 struct ctdb_control_public_ip_info *info = NULL;
2157                 int32_t pnn;
2158                 char *aciface = NULL;
2159                 char *avifaces = NULL;
2160                 char *cifaces = NULL;
2161
2162                 if (options.pnn == CTDB_BROADCAST_ALL) {
2163                         pnn = ips->ips[ips->num-i].pnn;
2164                 } else {
2165                         pnn = options.pnn;
2166                 }
2167
2168                 if (pnn != -1) {
2169                         ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2170                                                    &ips->ips[ips->num-i].addr, &info);
2171                 } else {
2172                         ret = -1;
2173                 }
2174
2175                 if (ret == 0) {
2176                         int j;
2177                         for (j=0; j < info->num; j++) {
2178                                 if (cifaces == NULL) {
2179                                         cifaces = talloc_strdup(info,
2180                                                                 info->ifaces[j].name);
2181                                 } else {
2182                                         cifaces = talloc_asprintf_append(cifaces,
2183                                                                          ",%s",
2184                                                                          info->ifaces[j].name);
2185                                 }
2186
2187                                 if (info->active_idx == j) {
2188                                         aciface = info->ifaces[j].name;
2189                                 }
2190
2191                                 if (info->ifaces[j].link_state == 0) {
2192                                         continue;
2193                                 }
2194
2195                                 if (avifaces == NULL) {
2196                                         avifaces = talloc_strdup(info, info->ifaces[j].name);
2197                                 } else {
2198                                         avifaces = talloc_asprintf_append(avifaces,
2199                                                                           ",%s",
2200                                                                           info->ifaces[j].name);
2201                                 }
2202                         }
2203                 }
2204
2205                 if (options.machinereadable){
2206                         printf(":%s:%d:",
2207                                 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2208                                 ips->ips[ips->num-i].pnn);
2209                         if (options.verbose){
2210                                 printf("%s:%s:%s:",
2211                                         aciface?aciface:"",
2212                                         avifaces?avifaces:"",
2213                                         cifaces?cifaces:"");
2214                         }
2215                         printf("\n");
2216                 } else {
2217                         if (options.verbose) {
2218                                 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2219                                         ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2220                                         ips->ips[ips->num-i].pnn,
2221                                         aciface?aciface:"",
2222                                         avifaces?avifaces:"",
2223                                         cifaces?cifaces:"");
2224                         } else {
2225                                 printf("%s %d\n",
2226                                         ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2227                                         ips->ips[ips->num-i].pnn);
2228                         }
2229                 }
2230                 talloc_free(info);
2231         }
2232
2233         talloc_free(tmp_ctx);
2234         return 0;
2235 }
2236
2237 /*
2238   public ip info
2239  */
2240 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2241 {
2242         int i, ret;
2243         ctdb_sock_addr addr;
2244         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2245         struct ctdb_control_public_ip_info *info;
2246
2247         if (argc != 1) {
2248                 talloc_free(tmp_ctx);
2249                 usage();
2250         }
2251
2252         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2253                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2254                 return -1;
2255         }
2256
2257         /* read the public ip info from this node */
2258         ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2259                                            tmp_ctx, &addr, &info);
2260         if (ret != 0) {
2261                 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2262                                   argv[0], options.pnn));
2263                 talloc_free(tmp_ctx);
2264                 return ret;
2265         }
2266
2267         printf("Public IP[%s] info on node %u\n",
2268                ctdb_addr_to_str(&info->ip.addr),
2269                options.pnn);
2270
2271         printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2272                ctdb_addr_to_str(&info->ip.addr),
2273                info->ip.pnn, info->num);
2274
2275         for (i=0; i<info->num; i++) {
2276                 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2277
2278                 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2279                        i+1, info->ifaces[i].name,
2280                        info->ifaces[i].link_state?"up":"down",
2281                        (unsigned int)info->ifaces[i].references,
2282                        (i==info->active_idx)?" (active)":"");
2283         }
2284
2285         talloc_free(tmp_ctx);
2286         return 0;
2287 }
2288
2289 /*
2290   display interfaces status
2291  */
2292 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2293 {
2294         int i, ret;
2295         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2296         struct ctdb_control_get_ifaces *ifaces;
2297
2298         /* read the public ip list from this node */
2299         ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn,
2300                                    tmp_ctx, &ifaces);
2301         if (ret != 0) {
2302                 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2303                                   options.pnn));
2304                 talloc_free(tmp_ctx);
2305                 return ret;
2306         }
2307
2308         if (options.machinereadable){
2309                 printf(":Name:LinkStatus:References:\n");
2310         } else {
2311                 printf("Interfaces on node %u\n", options.pnn);
2312         }
2313
2314         for (i=0; i<ifaces->num; i++) {
2315                 if (options.machinereadable){
2316                         printf(":%s:%s:%u\n",
2317                                ifaces->ifaces[i].name,
2318                                ifaces->ifaces[i].link_state?"1":"0",
2319                                (unsigned int)ifaces->ifaces[i].references);
2320                 } else {
2321                         printf("name:%s link:%s references:%u\n",
2322                                ifaces->ifaces[i].name,
2323                                ifaces->ifaces[i].link_state?"up":"down",
2324                                (unsigned int)ifaces->ifaces[i].references);
2325                 }
2326         }
2327
2328         talloc_free(tmp_ctx);
2329         return 0;
2330 }
2331
2332
2333 /*
2334   set link status of an interface
2335  */
2336 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2337 {
2338         int ret;
2339         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2340         struct ctdb_control_iface_info info;
2341
2342         ZERO_STRUCT(info);
2343
2344         if (argc != 2) {
2345                 usage();
2346         }
2347
2348         if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2349                 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2350                                   argv[0]));
2351                 talloc_free(tmp_ctx);
2352                 return -1;
2353         }
2354         strcpy(info.name, argv[0]);
2355
2356         if (strcmp(argv[1], "up") == 0) {
2357                 info.link_state = 1;
2358         } else if (strcmp(argv[1], "down") == 0) {
2359                 info.link_state = 0;
2360         } else {
2361                 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2362                                   argv[1]));
2363                 talloc_free(tmp_ctx);
2364                 return -1;
2365         }
2366
2367         /* read the public ip list from this node */
2368         ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2369                                    tmp_ctx, &info);
2370         if (ret != 0) {
2371                 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2372                                   argv[0], options.pnn));
2373                 talloc_free(tmp_ctx);
2374                 return ret;
2375         }
2376
2377         talloc_free(tmp_ctx);
2378         return 0;
2379 }
2380
2381 /*
2382   display pid of a ctdb daemon
2383  */
2384 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2385 {
2386         uint32_t pid;
2387         int ret;
2388
2389         ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2390         if (ret != 0) {
2391                 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2392                 return ret;
2393         }
2394         printf("Pid:%d\n", pid);
2395
2396         return 0;
2397 }
2398
2399 /*
2400   disable a remote node
2401  */
2402 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2403 {
2404         int ret;
2405         struct ctdb_node_map *nodemap=NULL;
2406
2407         /* check if the node is already disabled */
2408         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2409                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2410                 exit(10);
2411         }
2412         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2413                 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2414                 return 0;
2415         }
2416
2417         do {
2418                 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2419                 if (ret != 0) {
2420                         DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2421                         return ret;
2422                 }
2423
2424                 sleep(1);
2425
2426                 /* read the nodemap and verify the change took effect */
2427                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2428                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2429                         exit(10);
2430                 }
2431
2432         } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2433         ret = control_ipreallocate(ctdb, argc, argv);
2434         if (ret != 0) {
2435                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2436                 return ret;
2437         }
2438
2439         return 0;
2440 }
2441
2442 /*
2443   enable a disabled remote node
2444  */
2445 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2446 {
2447         int ret;
2448
2449         struct ctdb_node_map *nodemap=NULL;
2450
2451
2452         /* check if the node is already enabled */
2453         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2454                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2455                 exit(10);
2456         }
2457         if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2458                 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2459                 return 0;
2460         }
2461
2462         do {
2463                 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2464                 if (ret != 0) {
2465                         DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2466                         return ret;
2467                 }
2468
2469                 sleep(1);
2470
2471                 /* read the nodemap and verify the change took effect */
2472                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2473                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2474                         exit(10);
2475                 }
2476
2477         } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2478
2479         ret = control_ipreallocate(ctdb, argc, argv);
2480         if (ret != 0) {
2481                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2482                 return ret;
2483         }
2484
2485         return 0;
2486 }
2487
2488 /*
2489   stop a remote node
2490  */
2491 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2492 {
2493         int ret;
2494         struct ctdb_node_map *nodemap=NULL;
2495
2496         do {
2497                 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2498                 if (ret != 0) {
2499                         DEBUG(DEBUG_ERR, ("Unable to stop node %u   try again\n", options.pnn));
2500                 }
2501         
2502                 sleep(1);
2503
2504                 /* read the nodemap and verify the change took effect */
2505                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2506                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2507                         exit(10);
2508                 }
2509
2510         } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2511         ret = control_ipreallocate(ctdb, argc, argv);
2512         if (ret != 0) {
2513                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2514                 return ret;
2515         }
2516
2517         return 0;
2518 }
2519
2520 /*
2521   restart a stopped remote node
2522  */
2523 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2524 {
2525         int ret;
2526
2527         struct ctdb_node_map *nodemap=NULL;
2528
2529         do {
2530                 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2531                 if (ret != 0) {
2532                         DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2533                         return ret;
2534                 }
2535         
2536                 sleep(1);
2537
2538                 /* read the nodemap and verify the change took effect */
2539                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2540                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2541                         exit(10);
2542                 }
2543
2544         } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2545         ret = control_ipreallocate(ctdb, argc, argv);
2546         if (ret != 0) {
2547                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2548                 return ret;
2549         }
2550
2551         return 0;
2552 }
2553
2554 static uint32_t get_generation(struct ctdb_context *ctdb)
2555 {
2556         struct ctdb_vnn_map *vnnmap=NULL;
2557         int ret;
2558
2559         /* wait until the recmaster is not in recovery mode */
2560         while (1) {
2561                 uint32_t recmode, recmaster;
2562                 
2563                 if (vnnmap != NULL) {
2564                         talloc_free(vnnmap);
2565                         vnnmap = NULL;
2566                 }
2567
2568                 /* get the recmaster */
2569                 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
2570                 if (ret != 0) {
2571                         DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2572                         exit(10);
2573                 }
2574
2575                 /* get recovery mode */
2576                 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
2577                 if (ret != 0) {
2578                         DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2579                         exit(10);
2580                 }
2581
2582                 /* get the current generation number */
2583                 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2584                 if (ret != 0) {
2585                         DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2586                         exit(10);
2587                 }
2588
2589                 if ((recmode == CTDB_RECOVERY_NORMAL)
2590                 &&  (vnnmap->generation != 1)){
2591                         return vnnmap->generation;
2592                 }
2593                 sleep(1);
2594         }
2595 }
2596
2597 /*
2598   ban a node from the cluster
2599  */
2600 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2601 {
2602         int ret;
2603         struct ctdb_node_map *nodemap=NULL;
2604         struct ctdb_ban_time bantime;
2605
2606         if (argc < 1) {
2607                 usage();
2608         }
2609         
2610         /* verify the node exists */
2611         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2612         if (ret != 0) {
2613                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2614                 return ret;
2615         }
2616
2617         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2618                 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2619                 return -1;
2620         }
2621
2622         bantime.pnn  = options.pnn;
2623         bantime.time = strtoul(argv[0], NULL, 0);
2624
2625         ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2626         if (ret != 0) {
2627                 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2628                 return -1;
2629         }       
2630
2631         ret = control_ipreallocate(ctdb, argc, argv);
2632         if (ret != 0) {
2633                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2634                 return ret;
2635         }
2636
2637         return 0;
2638 }
2639
2640
2641 /*
2642   unban a node from the cluster
2643  */
2644 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2645 {
2646         int ret;
2647         struct ctdb_node_map *nodemap=NULL;
2648         struct ctdb_ban_time bantime;
2649
2650         /* verify the node exists */
2651         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2652         if (ret != 0) {
2653                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2654                 return ret;
2655         }
2656
2657         if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2658                 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2659                 return -1;
2660         }
2661
2662         bantime.pnn  = options.pnn;
2663         bantime.time = 0;
2664
2665         ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2666         if (ret != 0) {
2667                 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2668                 return -1;
2669         }       
2670
2671         ret = control_ipreallocate(ctdb, argc, argv);
2672         if (ret != 0) {
2673                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2674                 return ret;
2675         }
2676
2677         return 0;
2678 }
2679
2680
2681 /*
2682   show ban information for a node
2683  */
2684 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2685 {
2686         int ret;
2687         struct ctdb_node_map *nodemap=NULL;
2688         struct ctdb_ban_time *bantime;
2689
2690         /* verify the node exists */
2691         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2692         if (ret != 0) {
2693                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2694                 return ret;
2695         }
2696
2697         ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2698         if (ret != 0) {
2699                 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2700                 return -1;
2701         }       
2702
2703         if (bantime->time == 0) {
2704                 printf("Node %u is not banned\n", bantime->pnn);
2705         } else {
2706                 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2707         }
2708
2709         return 0;
2710 }
2711
2712 /*
2713   shutdown a daemon
2714  */
2715 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2716 {
2717         int ret;
2718
2719         ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2720         if (ret != 0) {
2721                 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2722                 return ret;
2723         }
2724
2725         return 0;
2726 }
2727
2728 /*
2729   trigger a recovery
2730  */
2731 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2732 {
2733         int ret;
2734         uint32_t generation, next_generation;
2735
2736         /* record the current generation number */
2737         generation = get_generation(ctdb);
2738
2739         ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2740         if (ret != 0) {
2741                 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
2742                 return ret;
2743         }
2744
2745         ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2746         if (ret != 0) {
2747                 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2748                 return ret;
2749         }
2750
2751         /* wait until we are in a new generation */
2752         while (1) {
2753                 next_generation = get_generation(ctdb);
2754                 if (next_generation != generation) {
2755                         return 0;
2756                 }
2757                 sleep(1);
2758         }
2759
2760         return 0;
2761 }
2762
2763
2764 /*
2765   display monitoring mode of a remote node
2766  */
2767 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2768 {
2769         uint32_t monmode;
2770         int ret;
2771
2772         ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2773         if (ret != 0) {
2774                 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2775                 return ret;
2776         }
2777         if (!options.machinereadable){
2778                 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2779         } else {
2780                 printf(":mode:\n");
2781                 printf(":%d:\n",monmode);
2782         }
2783         return 0;
2784 }
2785
2786
2787 /*
2788   display capabilities of a remote node
2789  */
2790 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2791 {
2792         uint32_t capabilities;
2793         int ret;
2794
2795         ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2796         if (ret != 0) {
2797                 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2798                 return ret;
2799         }
2800         
2801         if (!options.machinereadable){
2802                 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2803                 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2804                 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
2805                 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
2806         } else {
2807                 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
2808                 printf(":%d:%d:%d:%d:\n",
2809                         !!(capabilities&CTDB_CAP_RECMASTER),
2810                         !!(capabilities&CTDB_CAP_LMASTER),
2811                         !!(capabilities&CTDB_CAP_LVS),
2812                         !!(capabilities&CTDB_CAP_NATGW));
2813         }
2814         return 0;
2815 }
2816
2817 /*
2818   display lvs configuration
2819  */
2820 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
2821 {
2822         uint32_t *capabilities;
2823         struct ctdb_node_map *nodemap=NULL;
2824         int i, ret;
2825         int healthy_count = 0;
2826
2827         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2828         if (ret != 0) {
2829                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2830                 return ret;
2831         }
2832
2833         capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2834         CTDB_NO_MEMORY(ctdb, capabilities);
2835         
2836         /* collect capabilities for all connected nodes */
2837         for (i=0; i<nodemap->num; i++) {
2838                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2839                         continue;
2840                 }
2841                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2842                         continue;
2843                 }
2844         
2845                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2846                 if (ret != 0) {
2847                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2848                         return ret;
2849                 }
2850
2851                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2852                         continue;
2853                 }
2854
2855                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2856                         healthy_count++;
2857                 }
2858         }
2859
2860         /* Print all LVS nodes */
2861         for (i=0; i<nodemap->num; i++) {
2862                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2863                         continue;
2864                 }
2865                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2866                         continue;
2867                 }
2868                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2869                         continue;
2870                 }
2871
2872                 if (healthy_count != 0) {
2873                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2874                                 continue;
2875                         }
2876                 }
2877
2878                 printf("%d:%s\n", i, 
2879                         ctdb_addr_to_str(&nodemap->nodes[i].addr));
2880         }
2881
2882         return 0;
2883 }
2884
2885 /*
2886   display who is the lvs master
2887  */
2888 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
2889 {
2890         uint32_t *capabilities;
2891         struct ctdb_node_map *nodemap=NULL;
2892         int i, ret;
2893         int healthy_count = 0;
2894
2895         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2896         if (ret != 0) {
2897                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2898                 return ret;
2899         }
2900
2901         capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2902         CTDB_NO_MEMORY(ctdb, capabilities);
2903         
2904         /* collect capabilities for all connected nodes */
2905         for (i=0; i<nodemap->num; i++) {
2906                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2907                         continue;
2908                 }
2909                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2910                         continue;
2911                 }
2912         
2913                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2914                 if (ret != 0) {
2915                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2916                         return ret;
2917                 }
2918
2919                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2920                         continue;
2921                 }
2922
2923                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2924                         healthy_count++;
2925                 }
2926         }
2927
2928         /* find and show the lvsmaster */
2929         for (i=0; i<nodemap->num; i++) {
2930                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2931                         continue;
2932                 }
2933                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2934                         continue;
2935                 }
2936                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2937                         continue;
2938                 }
2939
2940                 if (healthy_count != 0) {
2941                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2942                                 continue;
2943                         }
2944                 }
2945
2946                 if (options.machinereadable){
2947                         printf("%d\n", i);
2948                 } else {
2949                         printf("Node %d is LVS master\n", i);
2950                 }
2951                 return 0;
2952         }
2953
2954         printf("There is no LVS master\n");
2955         return -1;
2956 }
2957
2958 /*
2959   disable monitoring on a  node
2960  */
2961 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2962 {
2963         
2964         int ret;
2965
2966         ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
2967         if (ret != 0) {
2968                 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
2969                 return ret;
2970         }
2971         printf("Monitoring mode:%s\n","DISABLED");
2972
2973         return 0;
2974 }
2975
2976 /*
2977   enable monitoring on a  node
2978  */
2979 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2980 {
2981         
2982         int ret;
2983
2984         ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
2985         if (ret != 0) {
2986                 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
2987                 return ret;
2988         }
2989         printf("Monitoring mode:%s\n","ACTIVE");
2990
2991         return 0;
2992 }
2993
2994 /*
2995   display remote list of keys/data for a db
2996  */
2997 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
2998 {
2999         const char *db_name;
3000         struct ctdb_db_context *ctdb_db;
3001         int ret;
3002
3003         if (argc < 1) {
3004                 usage();
3005         }
3006
3007         db_name = argv[0];
3008
3009
3010         if (db_exists(ctdb, db_name)) {
3011                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3012                 return -1;
3013         }
3014
3015         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3016
3017         if (ctdb_db == NULL) {
3018                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3019                 return -1;
3020         }
3021
3022         /* traverse and dump the cluster tdb */
3023         ret = ctdb_dump_db(ctdb_db, stdout);
3024         if (ret == -1) {
3025                 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3026                 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3027                                   " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3028                                   db_name));
3029                 return -1;
3030         }
3031         talloc_free(ctdb_db);
3032
3033         printf("Dumped %d records\n", ret);
3034         return 0;
3035 }
3036
3037 /*
3038   display the content of a database key
3039  */
3040 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3041 {
3042         const char *db_name;
3043         struct ctdb_db_context *ctdb_db;
3044         struct ctdb_record_handle *h;
3045         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3046         TDB_DATA key, data;
3047
3048         if (argc < 2) {
3049                 usage();
3050         }
3051
3052         db_name = argv[0];
3053
3054
3055         if (db_exists(ctdb, db_name)) {
3056                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3057                 return -1;
3058         }
3059
3060         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3061
3062         if (ctdb_db == NULL) {
3063                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3064                 return -1;
3065         }
3066
3067         key.dptr  = discard_const(argv[1]);
3068         key.dsize = strlen((char *)key.dptr);
3069  
3070         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3071         if (h == NULL) {
3072                 printf("Failed to fetch record '%s' on node %d\n", 
3073                         (const char *)key.dptr, ctdb_get_pnn(ctdb));
3074                 talloc_free(tmp_ctx);
3075                 exit(10);
3076         }
3077
3078         printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3079
3080         talloc_free(ctdb_db);
3081         talloc_free(tmp_ctx);
3082         return 0;
3083 }
3084
3085 /*
3086   display the content of a database key
3087  */
3088 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3089 {
3090         const char *db_name;
3091         struct ctdb_db_context *ctdb_db;
3092         struct ctdb_record_handle *h;
3093         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3094         TDB_DATA key, data;
3095
3096         if (argc < 3) {
3097                 usage();
3098         }
3099
3100         db_name = argv[0];
3101
3102
3103         if (db_exists(ctdb, db_name)) {
3104                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3105                 return -1;
3106         }
3107
3108         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3109
3110         if (ctdb_db == NULL) {
3111                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3112                 return -1;
3113         }
3114
3115         key.dptr  = discard_const(argv[1]);
3116         key.dsize = strlen((char *)key.dptr);
3117  
3118         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3119         if (h == NULL) {
3120                 printf("Failed to fetch record '%s' on node %d\n", 
3121                         (const char *)key.dptr, ctdb_get_pnn(ctdb));
3122                 talloc_free(tmp_ctx);
3123                 exit(10);
3124         }
3125
3126         data.dptr  = discard_const(argv[2]);
3127         data.dsize = strlen((char *)data.dptr);
3128  
3129         if (ctdb_record_store(h, data) != 0) {
3130                 printf("Failed to store record\n");
3131         }
3132
3133         talloc_free(h);
3134         talloc_free(ctdb_db);
3135         talloc_free(tmp_ctx);
3136         return 0;
3137 }
3138
3139 /*
3140   fetch a record from a persistent database
3141  */
3142 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3143 {
3144         const char *db_name;
3145         struct ctdb_db_context *ctdb_db;
3146         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3147         struct ctdb_transaction_handle *h;
3148         TDB_DATA key, data;
3149         int fd, ret;
3150
3151         if (argc < 2) {
3152                 talloc_free(tmp_ctx);
3153                 usage();
3154         }
3155
3156         db_name = argv[0];
3157
3158
3159         if (db_exists(ctdb, db_name)) {
3160                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3161                 talloc_free(tmp_ctx);
3162                 return -1;
3163         }
3164
3165         ctdb_db = ctdb_attach(ctdb, db_name, true, 0);
3166
3167         if (ctdb_db == NULL) {
3168                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3169                 talloc_free(tmp_ctx);
3170                 return -1;
3171         }
3172
3173         h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3174         if (h == NULL) {
3175                 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3176                 talloc_free(tmp_ctx);
3177                 return -1;
3178         }
3179
3180         key.dptr  = discard_const(argv[1]);
3181         key.dsize = strlen(argv[1]);
3182         ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3183         if (ret != 0) {
3184                 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3185                 talloc_free(tmp_ctx);
3186                 return -1;
3187         }
3188
3189         if (data.dsize == 0 || data.dptr == NULL) {
3190                 DEBUG(DEBUG_ERR,("Record is empty\n"));
3191                 talloc_free(tmp_ctx);
3192                 return -1;
3193         }
3194
3195         if (argc == 3) {
3196           fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3197                 if (fd == -1) {
3198                         DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3199                         talloc_free(tmp_ctx);
3200                         return -1;
3201                 }
3202                 write(fd, data.dptr, data.dsize);
3203                 close(fd);
3204         } else {
3205                 write(1, data.dptr, data.dsize);
3206         }
3207
3208         /* abort the transaction */
3209         talloc_free(h);
3210
3211
3212         talloc_free(tmp_ctx);
3213         return 0;
3214 }
3215
3216 /*
3217   fetch a record from a tdb-file
3218  */
3219 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3220 {
3221         const char *tdb_file;
3222         TDB_CONTEXT *tdb;
3223         TDB_DATA key, data;
3224         int fd;
3225
3226         if (argc < 2) {
3227                 usage();
3228         }
3229
3230         tdb_file = argv[0];
3231
3232         tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3233         if (tdb == NULL) {
3234                 DEBUG(DEBUG_ERR,("Failed to open TDB file %s\n", tdb_file));
3235                 return -1;
3236         }
3237
3238         key.dptr  = discard_const(argv[1]);
3239         key.dsize = strlen(argv[1]);
3240         data = tdb_fetch(tdb, key);
3241         if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3242                 DEBUG(DEBUG_ERR,("Failed to read record %s from tdb %s\n", argv[1], tdb_file));
3243                 tdb_close(tdb);
3244                 return -1;
3245         }
3246
3247         tdb_close(tdb);
3248
3249         if (argc == 3) {
3250           fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3251                 if (fd == -1) {
3252                         DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3253                         return -1;
3254                 }
3255                 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3256                 close(fd);
3257         } else {
3258                 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3259         }
3260
3261         return 0;
3262 }
3263
3264 /*
3265   write a record to a persistent database
3266  */
3267 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3268 {
3269         const char *db_name;
3270         struct ctdb_db_context *ctdb_db;
3271         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3272         struct ctdb_transaction_handle *h;
3273         struct stat st;
3274         TDB_DATA key, data;
3275         int fd, ret;
3276
3277         if (argc < 3) {
3278                 talloc_free(tmp_ctx);
3279                 usage();
3280         }
3281
3282         fd = open(argv[2], O_RDONLY);
3283         if (fd == -1) {
3284                 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s  %s\n", argv[2], strerror(errno)));
3285                 talloc_free(tmp_ctx);
3286                 return -1;
3287         }
3288         
3289         ret = fstat(fd, &st);
3290         if (ret == -1) {
3291                 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3292                 close(fd);
3293                 talloc_free(tmp_ctx);
3294                 return -1;
3295         }
3296
3297         if (!S_ISREG(st.st_mode)) {
3298                 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3299                 close(fd);
3300                 talloc_free(tmp_ctx);
3301                 return -1;
3302         }
3303
3304         data.dsize = st.st_size;
3305         if (data.dsize == 0) {
3306                 data.dptr  = NULL;
3307         } else {
3308                 data.dptr = talloc_size(tmp_ctx, data.dsize);
3309                 if (data.dptr == NULL) {
3310                         DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3311                         close(fd);
3312                         talloc_free(tmp_ctx);
3313                         return -1;
3314                 }
3315                 ret = read(fd, data.dptr, data.dsize);
3316                 if (ret != data.dsize) {
3317                         DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3318                         close(fd);
3319                         talloc_free(tmp_ctx);
3320                         return -1;
3321                 }
3322         }
3323         close(fd);
3324
3325
3326         db_name = argv[0];
3327
3328         ctdb_db = ctdb_attach(ctdb, db_name, true, 0);
3329
3330         if (ctdb_db == NULL) {
3331                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3332                 talloc_free(tmp_ctx);
3333                 return -1;
3334         }
3335
3336         h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3337         if (h == NULL) {
3338                 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3339                 talloc_free(tmp_ctx);
3340                 return -1;
3341         }
3342
3343         key.dptr  = discard_const(argv[1]);
3344         key.dsize = strlen(argv[1]);
3345         ret = ctdb_transaction_store(h, key, data);
3346         if (ret != 0) {
3347                 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3348                 talloc_free(tmp_ctx);
3349                 return -1;
3350         }
3351
3352         ret = ctdb_transaction_commit(h);
3353         if (ret != 0) {
3354                 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3355                 talloc_free(tmp_ctx);
3356                 return -1;
3357         }
3358
3359
3360         talloc_free(tmp_ctx);
3361         return 0;
3362 }
3363
3364 /*
3365   check if a service is bound to a port or not
3366  */
3367 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
3368 {
3369         int s, ret;
3370         unsigned v;
3371         int port;
3372         struct sockaddr_in sin;
3373
3374         if (argc != 1) {
3375                 printf("Use: ctdb chktcport <port>\n");
3376                 return EINVAL;
3377         }
3378
3379         port = atoi(argv[0]);
3380
3381         s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
3382         if (s == -1) {
3383                 printf("Failed to open local socket\n");
3384                 return errno;
3385         }
3386
3387         v = fcntl(s, F_GETFL, 0);
3388         fcntl(s, F_SETFL, v | O_NONBLOCK);
3389
3390         bzero(&sin, sizeof(sin));
3391         sin.sin_family = PF_INET;
3392         sin.sin_port   = htons(port);
3393         ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
3394         close(s);
3395         if (ret == -1) {
3396                 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
3397                 return errno;
3398         }
3399
3400         return 0;
3401 }
3402
3403
3404
3405 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid, 
3406                              TDB_DATA data, void *private_data)
3407 {
3408         DEBUG(DEBUG_ERR,("Log data received\n"));
3409         if (data.dsize > 0) {
3410                 printf("%s", data.dptr);
3411         }
3412
3413         exit(0);
3414 }
3415
3416 /*
3417   display a list of log messages from the in memory ringbuffer
3418  */
3419 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3420 {
3421         int ret;
3422         int32_t res;
3423         struct ctdb_get_log_addr log_addr;
3424         TDB_DATA data;
3425         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3426         char *errmsg;
3427         struct timeval tv;
3428
3429         if (argc != 1) {
3430                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3431                 talloc_free(tmp_ctx);
3432                 return -1;
3433         }
3434
3435         log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3436         log_addr.srvid = getpid();
3437         if (isalpha(argv[0][0]) || argv[0][0] == '-') { 
3438                 log_addr.level = get_debug_by_desc(argv[0]);
3439         } else {
3440                 log_addr.level = strtol(argv[0], NULL, 0);
3441         }
3442
3443
3444         data.dptr = (unsigned char *)&log_addr;
3445         data.dsize = sizeof(log_addr);
3446
3447         DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3448
3449         ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3450         sleep(1);
3451
3452         DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3453
3454         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3455                            0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3456         if (ret != 0 || res != 0) {
3457                 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3458                 talloc_free(tmp_ctx);
3459                 return -1;
3460         }
3461
3462
3463         tv = timeval_current();
3464         /* this loop will terminate when we have received the reply */
3465         while (timeval_elapsed(&tv) < 3.0) {    
3466                 event_loop_once(ctdb->ev);
3467         }
3468
3469         DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3470
3471         talloc_free(tmp_ctx);
3472         return 0;
3473 }
3474
3475 /*
3476   clear the in memory log area
3477  */
3478 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3479 {
3480         int ret;
3481         int32_t res;
3482         char *errmsg;
3483         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3484
3485         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3486                            0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3487         if (ret != 0 || res != 0) {
3488                 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3489                 talloc_free(tmp_ctx);
3490                 return -1;
3491         }
3492
3493         talloc_free(tmp_ctx);
3494         return 0;
3495 }
3496
3497
3498
3499 /*
3500   display a list of the databases on a remote ctdb
3501  */
3502 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3503 {
3504         int i, ret;
3505         struct ctdb_dbid_map *dbmap=NULL;
3506
3507         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3508         if (ret != 0) {
3509                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3510                 return ret;
3511         }
3512
3513         if(options.machinereadable){
3514                 printf(":ID:Name:Path:Persistent:Unhealthy:\n");
3515                 for(i=0;i<dbmap->num;i++){
3516                         const char *path;
3517                         const char *name;
3518                         const char *health;
3519                         bool persistent;
3520
3521                         ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3522                                             dbmap->dbs[i].dbid, ctdb, &path);
3523                         ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3524                                             dbmap->dbs[i].dbid, ctdb, &name);
3525                         ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3526                                               dbmap->dbs[i].dbid, ctdb, &health);
3527                         persistent = dbmap->dbs[i].persistent;
3528                         printf(":0x%08X:%s:%s:%d:%d:\n",
3529                                dbmap->dbs[i].dbid, name, path,
3530                                !!(persistent), !!(health));
3531                 }
3532                 return 0;
3533         }
3534
3535         printf("Number of databases:%d\n", dbmap->num);
3536         for(i=0;i<dbmap->num;i++){
3537                 const char *path;
3538                 const char *name;
3539                 const char *health;
3540                 bool persistent;
3541
3542                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3543                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3544                 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3545                 persistent = dbmap->dbs[i].persistent;
3546                 printf("dbid:0x%08x name:%s path:%s%s%s\n",
3547                        dbmap->dbs[i].dbid, name, path,
3548                        persistent?" PERSISTENT":"",
3549                        health?" UNHEALTHY":"");
3550         }
3551
3552         return 0;
3553 }
3554
3555 /*
3556   display the status of a database on a remote ctdb
3557  */
3558 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3559 {
3560         int i, ret;
3561         struct ctdb_dbid_map *dbmap=NULL;
3562         const char *db_name;
3563
3564         if (argc < 1) {
3565                 usage();
3566         }
3567
3568         db_name = argv[0];
3569
3570         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3571         if (ret != 0) {
3572                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3573                 return ret;
3574         }
3575
3576         for(i=0;i<dbmap->num;i++){
3577                 const char *path;
3578                 const char *name;
3579                 const char *health;
3580                 bool persistent;
3581
3582                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3583                 if (strcmp(name, db_name) != 0) {
3584                         continue;
3585                 }
3586
3587                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3588                 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3589                 persistent = dbmap->dbs[i].persistent;
3590                 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nHEALTH: %s\n",
3591                        dbmap->dbs[i].dbid, name, path,
3592                        persistent?"yes":"no",
3593                        health?health:"OK");
3594                 return 0;
3595         }
3596
3597         DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
3598         return 0;
3599 }
3600
3601 /*
3602   check if the local node is recmaster or not
3603   it will return 1 if this node is the recmaster and 0 if it is not
3604   or if the local ctdb daemon could not be contacted
3605  */
3606 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3607 {
3608         uint32_t mypnn, recmaster;
3609         int ret;
3610
3611         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
3612         if (mypnn == -1) {
3613                 printf("Failed to get pnn of node\n");
3614                 return 1;
3615         }
3616
3617         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
3618         if (ret != 0) {
3619                 printf("Failed to get the recmaster\n");
3620                 return 1;
3621         }
3622
3623         if (recmaster != mypnn) {
3624                 printf("this node is not the recmaster\n");
3625                 return 1;
3626         }
3627
3628         printf("this node is the recmaster\n");
3629         return 0;
3630 }
3631
3632 /*
3633   ping a node
3634  */
3635 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
3636 {
3637         int ret;
3638         struct timeval tv = timeval_current();
3639         ret = ctdb_ctrl_ping(ctdb, options.pnn);
3640         if (ret == -1) {
3641                 printf("Unable to get ping response from node %u\n", options.pnn);
3642                 return -1;
3643         } else {
3644                 printf("response from %u time=%.6f sec  (%d clients)\n", 
3645                        options.pnn, timeval_elapsed(&tv), ret);
3646         }
3647         return 0;
3648 }
3649
3650
3651 /*
3652   get a tunable
3653  */
3654 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
3655 {
3656         const char *name;
3657         uint32_t value;
3658         int ret;
3659
3660         if (argc < 1) {
3661                 usage();
3662         }
3663
3664         name = argv[0];
3665         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
3666         if (ret == -1) {
3667                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
3668                 return -1;
3669         }
3670
3671         printf("%-19s = %u\n", name, value);
3672         return 0;
3673 }
3674
3675 /*
3676   set a tunable
3677  */
3678 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
3679 {
3680         const char *name;
3681         uint32_t value;
3682         int ret;
3683
3684         if (argc < 2) {
3685                 usage();
3686         }
3687
3688         name = argv[0];
3689         value = strtoul(argv[1], NULL, 0);
3690
3691         ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
3692         if (ret == -1) {
3693                 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
3694                 return -1;
3695         }
3696         return 0;
3697 }
3698
3699 /*
3700   list all tunables
3701  */
3702 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
3703 {
3704         uint32_t count;
3705         const char **list;
3706         int ret, i;
3707
3708         ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
3709         if (ret == -1) {
3710                 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
3711                 return -1;
3712         }
3713
3714         for (i=0;i<count;i++) {
3715                 control_getvar(ctdb, 1, &list[i]);
3716         }
3717
3718         talloc_free(list);
3719         
3720         return 0;
3721 }
3722
3723 /*
3724   display debug level on a node
3725  */
3726 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3727 {
3728         int ret;
3729         int32_t level;
3730
3731         ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
3732         if (ret != 0) {
3733                 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
3734                 return ret;
3735         } else {
3736                 if (options.machinereadable){
3737                         printf(":Name:Level:\n");
3738                         printf(":%s:%d:\n",get_debug_by_level(level),level);
3739                 } else {
3740                         printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
3741                 }
3742         }
3743         return 0;
3744 }
3745
3746 /*
3747   display reclock file of a node
3748  */
3749 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3750 {
3751         int ret;
3752         const char *reclock;
3753
3754         ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
3755         if (ret != 0) {
3756                 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3757                 return ret;
3758         } else {
3759                 if (options.machinereadable){
3760                         if (reclock != NULL) {
3761                                 printf("%s", reclock);
3762                         }
3763                 } else {
3764                         if (reclock == NULL) {
3765                                 printf("No reclock file used.\n");
3766                         } else {
3767                                 printf("Reclock file:%s\n", reclock);
3768                         }
3769                 }
3770         }
3771         return 0;
3772 }
3773
3774 /*
3775   set the reclock file of a node
3776  */
3777 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3778 {
3779         int ret;
3780         const char *reclock;
3781
3782         if (argc == 0) {
3783                 reclock = NULL;
3784         } else if (argc == 1) {
3785                 reclock = argv[0];
3786         } else {
3787                 usage();
3788         }
3789
3790         ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
3791         if (ret != 0) {
3792                 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3793                 return ret;
3794         }
3795         return 0;
3796 }
3797
3798 /*
3799   set the natgw state on/off
3800  */
3801 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
3802 {
3803         int ret;
3804         uint32_t natgwstate;
3805
3806         if (argc == 0) {
3807                 usage();
3808         }
3809
3810         if (!strcmp(argv[0], "on")) {
3811                 natgwstate = 1;
3812         } else if (!strcmp(argv[0], "off")) {
3813                 natgwstate = 0;
3814         } else {
3815                 usage();
3816         }
3817
3818         ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
3819         if (ret != 0) {
3820                 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
3821                 return ret;
3822         }
3823
3824         return 0;
3825 }
3826
3827 /*
3828   set the lmaster role on/off
3829  */
3830 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3831 {
3832         int ret;
3833         uint32_t lmasterrole;
3834
3835         if (argc == 0) {
3836                 usage();
3837         }
3838
3839         if (!strcmp(argv[0], "on")) {
3840                 lmasterrole = 1;
3841         } else if (!strcmp(argv[0], "off")) {
3842                 lmasterrole = 0;
3843         } else {
3844                 usage();
3845         }
3846
3847         ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
3848         if (ret != 0) {
3849                 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
3850                 return ret;
3851         }
3852
3853         return 0;
3854 }
3855
3856 /*
3857   set the recmaster role on/off
3858  */
3859 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3860 {
3861         int ret;
3862         uint32_t recmasterrole;
3863
3864         if (argc == 0) {
3865                 usage();
3866         }
3867
3868         if (!strcmp(argv[0], "on")) {
3869                 recmasterrole = 1;
3870         } else if (!strcmp(argv[0], "off")) {
3871                 recmasterrole = 0;
3872         } else {
3873                 usage();
3874         }
3875
3876         ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
3877         if (ret != 0) {
3878                 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
3879                 return ret;
3880         }
3881
3882         return 0;
3883 }
3884
3885 /*
3886   set debug level on a node or all nodes
3887  */
3888 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3889 {
3890         int i, ret;
3891         int32_t level;
3892
3893         if (argc == 0) {
3894                 printf("You must specify the debug level. Valid levels are:\n");
3895                 for (i=0; debug_levels[i].description != NULL; i++) {
3896                         printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3897                 }
3898
3899                 return 0;
3900         }
3901
3902         if (isalpha(argv[0][0]) || argv[0][0] == '-') { 
3903                 level = get_debug_by_desc(argv[0]);
3904         } else {
3905                 level = strtol(argv[0], NULL, 0);
3906         }
3907
3908         for (i=0; debug_levels[i].description != NULL; i++) {
3909                 if (level == debug_levels[i].level) {
3910                         break;
3911                 }
3912         }
3913         if (debug_levels[i].description == NULL) {
3914                 printf("Invalid debug level, must be one of\n");
3915                 for (i=0; debug_levels[i].description != NULL; i++) {
3916                         printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3917                 }
3918                 return -1;
3919         }
3920
3921         ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
3922         if (ret != 0) {
3923                 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
3924         }
3925         return 0;
3926 }
3927
3928
3929 /*
3930   thaw a node
3931  */
3932 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
3933 {
3934         int ret;
3935         uint32_t priority;
3936         
3937         if (argc == 1) {
3938                 priority = strtol(argv[0], NULL, 0);
3939         } else {
3940                 priority = 0;
3941         }
3942         DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
3943
3944         ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
3945         if (ret != 0) {
3946                 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
3947         }               
3948         return 0;
3949 }
3950
3951
3952 /*
3953   attach to a database
3954  */
3955 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
3956 {
3957         const char *db_name;
3958         struct ctdb_db_context *ctdb_db;
3959
3960         if (argc < 1) {
3961                 usage();
3962         }
3963         db_name = argv[0];
3964
3965         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3966         if (ctdb_db == NULL) {
3967                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3968                 return -1;
3969         }
3970
3971         return 0;
3972 }
3973
3974 /*
3975   set db priority
3976  */
3977 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
3978 {
3979         struct ctdb_db_priority db_prio;
3980         int ret;
3981
3982         if (argc < 2) {
3983                 usage();
3984         }
3985
3986         db_prio.db_id    = strtoul(argv[0], NULL, 0);
3987         db_prio.priority = strtoul(argv[1], NULL, 0);
3988
3989         ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
3990         if (ret != 0) {
3991                 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
3992                 return -1;
3993         }
3994
3995         return 0;
3996 }
3997
3998 /*
3999   get db priority
4000  */
4001 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4002 {
4003         uint32_t db_id, priority;
4004         int ret;
4005
4006         if (argc < 1) {
4007                 usage();
4008         }
4009
4010         db_id = strtoul(argv[0], NULL, 0);
4011
4012         ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4013         if (ret != 0) {
4014                 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4015                 return -1;
4016         }
4017
4018         DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
4019
4020         return 0;
4021 }
4022
4023 /*
4024   run an eventscript on a node
4025  */
4026 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
4027 {
4028         TDB_DATA data;
4029         int ret;
4030         int32_t res;
4031         char *errmsg;
4032         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4033
4034         if (argc != 1) {
4035                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4036                 return -1;
4037         }
4038
4039         data.dptr = (unsigned char *)discard_const(argv[0]);
4040         data.dsize = strlen((char *)data.dptr) + 1;
4041
4042         DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
4043
4044         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
4045                            0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4046         if (ret != 0 || res != 0) {
4047                 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
4048                 talloc_free(tmp_ctx);
4049                 return -1;
4050         }
4051         talloc_free(tmp_ctx);
4052         return 0;
4053 }
4054
4055 #define DB_VERSION 1
4056 #define MAX_DB_NAME 64
4057 struct db_file_header {
4058         unsigned long version;
4059         time_t timestamp;
4060         unsigned long persistent;
4061         unsigned long size;
4062         const char name[MAX_DB_NAME];
4063 };
4064
4065 struct backup_data {
4066         struct ctdb_marshall_buffer *records;
4067         uint32_t len;
4068         uint32_t total;
4069         bool traverse_error;
4070 };
4071
4072 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
4073 {
4074         struct backup_data *bd = talloc_get_type(private, struct backup_data);
4075         struct ctdb_rec_data *rec;
4076
4077         /* add the record */
4078         rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
4079         if (rec == NULL) {
4080                 bd->traverse_error = true;
4081                 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
4082                 return -1;
4083         }
4084         bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
4085         if (bd->records == NULL) {
4086                 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
4087                 bd->traverse_error = true;
4088                 return -1;
4089         }
4090         bd->records->count++;
4091         memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
4092         bd->len += rec->length;
4093         talloc_free(rec);
4094
4095         bd->total++;
4096         return 0;
4097 }
4098
4099 /*
4100  * backup a database to a file 
4101  */
4102 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
4103 {
4104         int i, ret;
4105         struct ctdb_dbid_map *dbmap=NULL;
4106         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4107         struct db_file_header dbhdr;
4108         struct ctdb_db_context *ctdb_db;
4109         struct backup_data *bd;
4110         int fh = -1;
4111         int status = -1;
4112         const char *reason = NULL;
4113
4114         if (argc != 2) {
4115                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4116                 return -1;
4117         }
4118
4119         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4120         if (ret != 0) {
4121                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4122                 return ret;
4123         }
4124
4125         for(i=0;i<dbmap->num;i++){
4126                 const char *name;
4127
4128                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4129                 if(!strcmp(argv[0], name)){
4130                         talloc_free(discard_const(name));
4131                         break;
4132                 }
4133                 talloc_free(discard_const(name));
4134         }
4135         if (i == dbmap->num) {
4136                 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4137                 talloc_free(tmp_ctx);
4138                 return -1;
4139         }
4140
4141         ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4142                                     dbmap->dbs[i].dbid, tmp_ctx, &reason);
4143         if (ret != 0) {
4144                 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4145                                  argv[0]));
4146                 talloc_free(tmp_ctx);
4147                 return -1;
4148         }
4149         if (reason) {
4150                 uint32_t allow_unhealthy = 0;
4151
4152                 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4153                                       "AllowUnhealthyDBRead",
4154                                       &allow_unhealthy);
4155
4156                 if (allow_unhealthy != 1) {
4157                         DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4158                                          argv[0], reason));
4159
4160                         DEBUG(DEBUG_ERR,("disallow backup : tunnable AllowUnhealthyDBRead = %u\n",
4161                                          allow_unhealthy));
4162                         talloc_free(tmp_ctx);
4163                         return -1;
4164                 }
4165
4166                 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4167                                      argv[0], argv[0]));
4168                 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4169                                      "tunnable AllowUnhealthyDBRead = %u\n",
4170                                      allow_unhealthy));
4171         }
4172
4173         ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
4174         if (ctdb_db == NULL) {
4175                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4176                 talloc_free(tmp_ctx);
4177                 return -1;
4178         }
4179
4180
4181         ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4182         if (ret == -1) {
4183                 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4184                 talloc_free(tmp_ctx);
4185                 return -1;
4186         }
4187
4188
4189         bd = talloc_zero(tmp_ctx, struct backup_data);
4190         if (bd == NULL) {
4191                 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4192                 talloc_free(tmp_ctx);
4193                 return -1;
4194         }
4195
4196         bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4197         if (bd->records == NULL) {
4198                 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4199                 talloc_free(tmp_ctx);
4200                 return -1;
4201         }
4202
4203         bd->len = offsetof(struct ctdb_marshall_buffer, data);
4204         bd->records->db_id = ctdb_db->db_id;
4205         /* traverse the database collecting all records */
4206         if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4207             bd->traverse_error) {
4208                 DEBUG(DEBUG_ERR,("Traverse error\n"));
4209                 talloc_free(tmp_ctx);
4210                 return -1;              
4211         }
4212
4213         tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4214
4215
4216         fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4217         if (fh == -1) {
4218                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4219                 talloc_free(tmp_ctx);
4220                 return -1;
4221         }
4222
4223         dbhdr.version = DB_VERSION;
4224         dbhdr.timestamp = time(NULL);
4225         dbhdr.persistent = dbmap->dbs[i].persistent;
4226         dbhdr.size = bd->len;
4227         if (strlen(argv[0]) >= MAX_DB_NAME) {
4228                 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4229                 goto done;
4230         }
4231         strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
4232         ret = write(fh, &dbhdr, sizeof(dbhdr));
4233         if (ret == -1) {
4234                 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4235                 goto done;
4236         }
4237         ret = write(fh, bd->records, bd->len);
4238         if (ret == -1) {
4239                 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4240                 goto done;
4241         }
4242
4243         status = 0;
4244 done:
4245         if (fh != -1) {
4246                 ret = close(fh);
4247                 if (ret == -1) {
4248                         DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4249                 }
4250         }
4251
4252         DEBUG(DEBUG_ERR,("Database acked up to %s\n", argv[1]));
4253
4254         talloc_free(tmp_ctx);
4255         return status;
4256 }
4257
4258 /*
4259  * restore a database from a file 
4260  */
4261 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4262 {
4263         int ret;
4264         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4265         TDB_DATA outdata;
4266         TDB_DATA data;
4267         struct db_file_header dbhdr;
4268         struct ctdb_db_context *ctdb_db;
4269         struct ctdb_node_map *nodemap=NULL;
4270         struct ctdb_vnn_map *vnnmap=NULL;
4271         int i, fh;
4272         struct ctdb_control_wipe_database w;
4273         uint32_t *nodes;
4274         uint32_t generation;
4275         struct tm *tm;
4276         char tbuf[100];
4277         char *dbname;
4278
4279         if (argc < 1 || argc > 2) {
4280                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4281                 return -1;
4282         }
4283
4284         fh = open(argv[0], O_RDONLY);
4285         if (fh == -1) {
4286                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4287                 talloc_free(tmp_ctx);
4288                 return -1;
4289         }
4290
4291         read(fh, &dbhdr, sizeof(dbhdr));
4292         if (dbhdr.version != DB_VERSION) {
4293                 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4294                 talloc_free(tmp_ctx);
4295                 return -1;
4296         }
4297
4298         dbname = discard_const(dbhdr.name);
4299         if (argc == 2) {
4300                 dbname = discard_const(argv[1]);
4301         }
4302
4303         outdata.dsize = dbhdr.size;
4304         outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4305         if (outdata.dptr == NULL) {
4306                 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4307                 close(fh);
4308                 talloc_free(tmp_ctx);
4309                 return -1;
4310         }               
4311         read(fh, outdata.dptr, outdata.dsize);
4312         close(fh);
4313
4314         tm = localtime(&dbhdr.timestamp);
4315         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4316         printf("Restoring database '%s' from backup @ %s\n",
4317                 dbname, tbuf);
4318
4319
4320         ctdb_db = ctdb_attach(ctdb, dbname, dbhdr.persistent, 0);
4321         if (ctdb_db == NULL) {
4322                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4323                 talloc_free(tmp_ctx);
4324                 return -1;
4325         }
4326
4327         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4328         if (ret != 0) {
4329                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4330                 talloc_free(tmp_ctx);
4331                 return ret;
4332         }
4333
4334
4335         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4336         if (ret != 0) {
4337                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4338                 talloc_free(tmp_ctx);
4339                 return ret;
4340         }
4341
4342         /* freeze all nodes */
4343         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4344         for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4345                 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4346                                         nodes, i,
4347                                         TIMELIMIT(),
4348                                         false, tdb_null,
4349                                         NULL, NULL,
4350                                         NULL) != 0) {
4351                         DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4352                         ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4353                         talloc_free(tmp_ctx);
4354                         return -1;
4355                 }
4356         }
4357
4358         generation = vnnmap->generation;
4359         data.dptr = (void *)&generation;
4360         data.dsize = sizeof(generation);
4361
4362         /* start a cluster wide transaction */
4363         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4364         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4365                                         nodes, 0,
4366                                         TIMELIMIT(), false, data,
4367                                         NULL, NULL,
4368                                         NULL) != 0) {
4369                 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4370                 return -1;
4371         }
4372
4373
4374         w.db_id = ctdb_db->db_id;
4375         w.transaction_id = generation;
4376
4377         data.dptr = (void *)&w;
4378         data.dsize = sizeof(w);
4379
4380         /* wipe all the remote databases. */
4381         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4382         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4383                                         nodes, 0,
4384                                         TIMELIMIT(), false, data,
4385                                         NULL, NULL,
4386                                         NULL) != 0) {
4387                 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4388                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4389                 talloc_free(tmp_ctx);
4390                 return -1;
4391         }
4392         
4393         /* push the database */
4394         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4395         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4396                                         nodes, 0,
4397                                         TIMELIMIT(), false, outdata,
4398                                         NULL, NULL,
4399                                         NULL) != 0) {
4400                 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4401                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4402                 talloc_free(tmp_ctx);
4403                 return -1;
4404         }
4405
4406         data.dptr = (void *)&ctdb_db->db_id;
4407         data.dsize = sizeof(ctdb_db->db_id);
4408
4409         /* mark the database as healthy */
4410         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4411         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4412                                         nodes, 0,
4413                                         TIMELIMIT(), false, data,
4414                                         NULL, NULL,
4415                                         NULL) != 0) {
4416                 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4417                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4418                 talloc_free(tmp_ctx);
4419                 return -1;
4420         }
4421
4422         data.dptr = (void *)&generation;
4423         data.dsize = sizeof(generation);
4424
4425         /* commit all the changes */
4426         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4427                                         nodes, 0,
4428                                         TIMELIMIT(), false, data,
4429                                         NULL, NULL,
4430                                         NULL) != 0) {
4431                 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4432                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4433                 talloc_free(tmp_ctx);
4434                 return -1;
4435         }
4436
4437
4438         /* thaw all nodes */
4439         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4440         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4441                                         nodes, 0,
4442                                         TIMELIMIT(),
4443                                         false, tdb_null,
4444                                         NULL, NULL,
4445                                         NULL) != 0) {
4446                 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4447                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4448                 talloc_free(tmp_ctx);
4449                 return -1;
4450         }
4451
4452
4453         talloc_free(tmp_ctx);
4454         return 0;
4455 }
4456
4457 /*
4458  * dump a database backup from a file
4459  */
4460 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4461 {
4462         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4463         TDB_DATA outdata;
4464         struct db_file_header dbhdr;
4465         int i, fh;
4466         struct tm *tm;
4467         char tbuf[100];
4468         struct ctdb_rec_data *rec = NULL;
4469         struct ctdb_marshall_buffer *m;
4470
4471         if (argc != 1) {
4472                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4473                 return -1;
4474         }
4475
4476         fh = open(argv[0], O_RDONLY);
4477         if (fh == -1) {
4478                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4479                 talloc_free(tmp_ctx);
4480                 return -1;
4481         }
4482
4483         read(fh, &dbhdr, sizeof(dbhdr));
4484         if (dbhdr.version != DB_VERSION) {
4485                 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4486                 talloc_free(tmp_ctx);
4487                 return -1;
4488         }
4489
4490         outdata.dsize = dbhdr.size;
4491         outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4492         if (outdata.dptr == NULL) {
4493                 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4494                 close(fh);
4495                 talloc_free(tmp_ctx);
4496                 return -1;
4497         }
4498         read(fh, outdata.dptr, outdata.dsize);
4499         close(fh);
4500         m = (struct ctdb_marshall_buffer *)outdata.dptr;
4501
4502         tm = localtime(&dbhdr.timestamp);
4503         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4504         printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
4505                 dbhdr.name, m->db_id, tbuf);
4506
4507         for (i=0; i < m->count; i++) {
4508                 uint32_t reqid = 0;
4509                 TDB_DATA key, data;
4510
4511                 /* we do not want the header splitted, so we pass NULL*/
4512                 rec = ctdb_marshall_loop_next(m, rec, &reqid,
4513                                               NULL, &key, &data);
4514
4515                 ctdb_dumpdb_record(ctdb, key, data, stdout);
4516         }
4517
4518         printf("Dumped %d records\n", i);
4519         talloc_free(tmp_ctx);
4520         return 0;
4521 }
4522
4523 /*
4524  * wipe a database from a file
4525  */
4526 static int control_wipedb(struct ctdb_context *ctdb, int argc,
4527                           const char **argv)
4528 {
4529         int ret;
4530         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4531         TDB_DATA data;
4532         struct ctdb_db_context *ctdb_db;
4533         struct ctdb_node_map *nodemap = NULL;
4534         struct ctdb_vnn_map *vnnmap = NULL;
4535         int i;
4536         struct ctdb_control_wipe_database w;
4537         uint32_t *nodes;
4538         uint32_t generation;
4539         struct ctdb_dbid_map *dbmap = NULL;
4540
4541         if (argc != 1) {
4542                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4543                 return -1;
4544         }
4545
4546         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4547                                  &dbmap);
4548         if (ret != 0) {
4549                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
4550                                   options.pnn));
4551                 return ret;
4552         }
4553
4554         for(i=0;i<dbmap->num;i++){
4555                 const char *name;
4556
4557                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4558                                     dbmap->dbs[i].dbid, tmp_ctx, &name);
4559                 if(!strcmp(argv[0], name)){
4560                         talloc_free(discard_const(name));
4561                         break;
4562                 }
4563                 talloc_free(discard_const(name));
4564         }
4565         if (i == dbmap->num) {
4566                 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
4567                                   argv[0]));
4568                 talloc_free(tmp_ctx);
4569                 return -1;
4570         }
4571
4572         ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
4573         if (ctdb_db == NULL) {
4574                 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
4575                                   argv[0]));
4576                 talloc_free(tmp_ctx);
4577                 return -1;
4578         }
4579
4580         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
4581                                    &nodemap);
4582         if (ret != 0) {
4583                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4584                                   options.pnn));
4585                 talloc_free(tmp_ctx);
4586                 return ret;
4587         }
4588
4589         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4590                                   &vnnmap);
4591         if (ret != 0) {
4592                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
4593                                   options.pnn));
4594                 talloc_free(tmp_ctx);
4595                 return ret;
4596         }
4597
4598         /* freeze all nodes */
4599         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4600         for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4601                 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4602                                                 nodes, i,
4603                                                 TIMELIMIT(),
4604                                                 false, tdb_null,
4605                                                 NULL, NULL,
4606                                                 NULL);
4607                 if (ret != 0) {
4608                         DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4609                         ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
4610                                              CTDB_RECOVERY_ACTIVE);
4611                         talloc_free(tmp_ctx);
4612                         return -1;
4613                 }
4614         }
4615
4616         generation = vnnmap->generation;
4617         data.dptr = (void *)&generation;
4618         data.dsize = sizeof(generation);
4619
4620         /* start a cluster wide transaction */
4621         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4622         ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4623                                         nodes, 0,
4624                                         TIMELIMIT(), false, data,
4625                                         NULL, NULL,
4626                                         NULL);
4627         if (ret!= 0) {
4628                 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
4629                                   "transactions.\n"));
4630                 return -1;
4631         }
4632
4633         w.db_id = ctdb_db->db_id;
4634         w.transaction_id = generation;
4635
4636         data.dptr = (void *)&w;
4637         data.dsize = sizeof(w);
4638
4639         /* wipe all the remote databases. */
4640         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4641         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4642                                         nodes, 0,
4643                                         TIMELIMIT(), false, data,
4644                                         NULL, NULL,
4645                                         NULL) != 0) {
4646                 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4647                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4648                 talloc_free(tmp_ctx);
4649                 return -1;
4650         }
4651
4652         data.dptr = (void *)&ctdb_db->db_id;
4653         data.dsize = sizeof(ctdb_db->db_id);
4654
4655         /* mark the database as healthy */
4656         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4657         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4658                                         nodes, 0,
4659                                         TIMELIMIT(), false, data,
4660                                         NULL, NULL,
4661                                         NULL) != 0) {
4662                 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4663                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4664                 talloc_free(tmp_ctx);
4665                 return -1;
4666         }
4667
4668         data.dptr = (void *)&generation;
4669         data.dsize = sizeof(generation);
4670
4671         /* commit all the changes */
4672         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4673                                         nodes, 0,
4674                                         TIMELIMIT(), false, data,
4675                                         NULL, NULL,
4676                                         NULL) != 0) {
4677                 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4678                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4679                 talloc_free(tmp_ctx);
4680                 return -1;
4681         }
4682
4683         /* thaw all nodes */
4684         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4685         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4686                                         nodes, 0,
4687                                         TIMELIMIT(),
4688                                         false, tdb_null,
4689                                         NULL, NULL,
4690                                         NULL) != 0) {
4691                 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4692                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4693                 talloc_free(tmp_ctx);
4694                 return -1;
4695         }
4696
4697         DEBUG(DEBUG_ERR, ("Database wiped.\n"));
4698
4699         talloc_free(tmp_ctx);
4700         return 0;
4701 }
4702
4703 /*
4704   dump memory usage
4705  */
4706 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4707 {
4708         TDB_DATA data;
4709         int ret;
4710         int32_t res;
4711         char *errmsg;
4712         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4713         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
4714                            0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
4715         if (ret != 0 || res != 0) {
4716                 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
4717                 talloc_free(tmp_ctx);
4718                 return -1;
4719         }
4720         write(1, data.dptr, data.dsize);
4721         talloc_free(tmp_ctx);
4722         return 0;
4723 }
4724
4725 /*
4726   handler for memory dumps
4727 */
4728 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid, 
4729                              TDB_DATA data, void *private_data)
4730 {
4731         write(1, data.dptr, data.dsize);
4732         exit(0);
4733 }
4734
4735 /*
4736   dump memory usage on the recovery daemon
4737  */
4738 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4739 {
4740         int ret;
4741         TDB_DATA data;
4742         struct rd_memdump_reply rd;
4743
4744         rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4745         if (rd.pnn == -1) {
4746                 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4747                 return -1;
4748         }
4749         rd.srvid = getpid();
4750
4751         /* register a message port for receiveing the reply so that we
4752            can receive the reply
4753         */
4754         ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
4755
4756
4757         data.dptr = (uint8_t *)&rd;
4758         data.dsize = sizeof(rd);
4759
4760         ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
4761         if (ret != 0) {
4762                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4763                 return -1;
4764         }
4765
4766         /* this loop will terminate when we have received the reply */
4767         while (1) {     
4768                 event_loop_once(ctdb->ev);
4769         }
4770
4771         return 0;
4772 }
4773
4774 /*
4775   send a message to a srvid
4776  */
4777 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
4778 {
4779         unsigned long srvid;
4780         int ret;
4781         TDB_DATA data;
4782
4783         if (argc < 2) {
4784                 usage();
4785         }
4786
4787         srvid      = strtoul(argv[0], NULL, 0);
4788
4789         data.dptr = (uint8_t *)discard_const(argv[1]);
4790         data.dsize= strlen(argv[1]);
4791
4792         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
4793         if (ret != 0) {
4794                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4795                 return -1;
4796         }
4797
4798         return 0;
4799 }
4800
4801 /*
4802   handler for msglisten
4803 */
4804 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid, 
4805                              TDB_DATA data, void *private_data)
4806 {
4807         int i;
4808
4809         printf("Message received: ");
4810         for (i=0;i<data.dsize;i++) {
4811                 printf("%c", data.dptr[i]);
4812         }
4813         printf("\n");
4814 }
4815
4816 /*
4817   listen for messages on a messageport
4818  */
4819 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
4820 {
4821         uint64_t srvid;
4822
4823         srvid = getpid();
4824
4825         /* register a message port and listen for messages
4826         */
4827         ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
4828         printf("Listening for messages on srvid:%d\n", (int)srvid);
4829
4830         while (1) {     
4831                 event_loop_once(ctdb->ev);
4832         }
4833
4834         return 0;
4835 }
4836
4837 /*
4838   list all nodes in the cluster
4839   we parse the nodes file directly
4840  */
4841 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
4842 {
4843         TALLOC_CTX *mem_ctx = talloc_new(NULL);
4844         struct pnn_node *pnn_nodes;
4845         struct pnn_node *pnn_node;
4846
4847         pnn_nodes = read_nodes_file(mem_ctx);
4848         if (pnn_nodes == NULL) {
4849                 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
4850                 talloc_free(mem_ctx);
4851                 return -1;
4852         }
4853
4854         for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
4855                 ctdb_sock_addr addr;
4856                 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
4857                         DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
4858                         talloc_free(mem_ctx);
4859                         return -1;
4860                 }
4861                 if (options.machinereadable){
4862                         printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
4863                 } else {
4864                         printf("%s\n", pnn_node->addr);
4865                 }
4866         }
4867         talloc_free(mem_ctx);
4868
4869         return 0;
4870 }
4871
4872 /*
4873   reload the nodes file on the local node
4874  */
4875 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
4876 {
4877         int i, ret;
4878         int mypnn;
4879         struct ctdb_node_map *nodemap=NULL;
4880
4881         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4882         if (mypnn == -1) {
4883                 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
4884                 return -1;
4885         }
4886
4887         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
4888         if (ret != 0) {
4889                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
4890                 return ret;
4891         }
4892
4893         /* reload the nodes file on all remote nodes */
4894         for (i=0;i<nodemap->num;i++) {
4895                 if (nodemap->nodes[i].pnn == mypnn) {
4896                         continue;
4897                 }
4898                 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
4899                 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
4900                         nodemap->nodes[i].pnn);
4901                 if (ret != 0) {
4902                         DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
4903                 }
4904         }
4905
4906         /* reload the nodes file on the local node */
4907         DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
4908         ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
4909         if (ret != 0) {
4910                 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
4911         }
4912
4913         /* initiate a recovery */
4914         control_recover(ctdb, argc, argv);
4915
4916         return 0;
4917 }
4918
4919
4920 static const struct {
4921         const char *name;
4922         int (*fn)(struct ctdb_context *, int, const char **);
4923         bool auto_all;
4924         bool without_daemon; /* can be run without daemon running ? */
4925         const char *msg;
4926         const char *args;
4927 } ctdb_commands[] = {
4928 #ifdef CTDB_VERS
4929         { "version",         control_version,           true,   false,  "show version of ctdb" },
4930 #endif
4931         { "status",          control_status,            true,   false,  "show node status" },
4932         { "uptime",          control_uptime,            true,   false,  "show node uptime" },
4933         { "ping",            control_ping,              true,   false,  "ping all nodes" },
4934         { "getvar",          control_getvar,            true,   false,  "get a tunable variable",               "<name>"},
4935         { "setvar",          control_setvar,            true,   false,  "set a tunable variable",               "<name> <value>"},
4936         { "listvars",        control_listvars,          true,   false,  "list tunable variables"},
4937         { "statistics",      control_statistics,        false,  false, "show statistics" },
4938         { "statisticsreset", control_statistics_reset,  true,   false,  "reset statistics"},
4939         { "stats",           control_stats,             false,  false,  "show rolling statistics", "[number of history records]" },
4940         { "ip",              control_ip,                false,  false,  "show which public ip's that ctdb manages" },
4941         { "ipinfo",          control_ipinfo,            true,   false,  "show details about a public ip that ctdb manages", "<ip>" },
4942         { "ifaces",          control_ifaces,            true,   false,  "show which interfaces that ctdb manages" },
4943         { "setifacelink",    control_setifacelink,      true,   false,  "set interface link status", "<iface> <status>" },
4944         { "process-exists",  control_process_exists,    true,   false,  "check if a process exists on a node",  "<pid>"},
4945         { "getdbmap",        control_getdbmap,          true,   false,  "show the database map" },
4946         { "getdbstatus",     control_getdbstatus,       true,   false,  "show the status of a database", "<dbname>" },
4947         { "catdb",           control_catdb,             true,   false,  "dump a database" ,                     "<dbname>"},
4948         { "getmonmode",      control_getmonmode,        true,   false,  "show monitoring mode" },
4949         { "getcapabilities", control_getcapabilities,   true,   false,  "show node capabilities" },
4950         { "pnn",             control_pnn,               true,   false,  "show the pnn of the currnet node" },
4951         { "lvs",             control_lvs,               true,   false,  "show lvs configuration" },
4952         { "lvsmaster",       control_lvsmaster,         true,   false,  "show which node is the lvs master" },
4953         { "disablemonitor",      control_disable_monmode,true,  false,  "set monitoring mode to DISABLE" },
4954         { "enablemonitor",      control_enable_monmode, true,   false,  "set monitoring mode to ACTIVE" },
4955         { "setdebug",        control_setdebug,          true,   false,  "set debug level",                      "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
4956         { "getdebug",        control_getdebug,          true,   false,  "get debug level" },
4957         { "getlog",          control_getlog,            true,   false,  "get the log data from the in memory ringbuffer", "<level>" },
4958         { "clearlog",          control_clearlog,        true,   false,  "clear the log data from the in memory ringbuffer" },
4959         { "attach",          control_attach,            true,   false,  "attach to a database",                 "<dbname>" },
4960         { "dumpmemory",      control_dumpmemory,        true,   false,  "dump memory map to stdout" },
4961         { "rddumpmemory",    control_rddumpmemory,      true,   false,  "dump memory map from the recovery daemon to stdout" },
4962         { "getpid",          control_getpid,            true,   false,  "get ctdbd process ID" },
4963         { "disable",         control_disable,           true,   false,  "disable a nodes public IP" },
4964         { "enable",          control_enable,            true,   false,  "enable a nodes public IP" },
4965         { "stop",            control_stop,              true,   false,  "stop a node" },
4966         { "continue",        control_continue,          true,   false,  "re-start a stopped node" },
4967         { "ban",             control_ban,               true,   false,  "ban a node from the cluster",          "<bantime|0>"},
4968         { "unban",           control_unban,             true,   false,  "unban a node" },
4969         { "showban",         control_showban,           true,   false,  "show ban information"},
4970         { "shutdown",        control_shutdown,          true,   false,  "shutdown ctdbd" },
4971         { "recover",         control_recover,           true,   false,  "force recovery" },
4972         { "sync",            control_ipreallocate,      true,   false,  "wait until ctdbd has synced all state changes" },
4973         { "ipreallocate",    control_ipreallocate,      true,   false,  "force the recovery daemon to perform a ip reallocation procedure" },
4974         { "thaw",            control_thaw,              true,   false,  "thaw databases", "[priority:1-3]" },
4975         { "isnotrecmaster",  control_isnotrecmaster,    false,  false,  "check if the local node is recmaster or not" },
4976         { "killtcp",         kill_tcp,                  false,  false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
4977         { "gratiousarp",     control_gratious_arp,      false,  false, "send a gratious arp", "<ip> <interface>" },
4978         { "tickle",          tickle_tcp,                false,  false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
4979         { "gettickles",      control_get_tickles,       false,  false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
4980         { "addtickle",       control_add_tickle,        false,  false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
4981
4982         { "deltickle",       control_del_tickle,        false,  false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
4983
4984         { "regsrvid",        regsrvid,                  false,  false, "register a server id", "<pnn> <type> <id>" },
4985         { "unregsrvid",      unregsrvid,                false,  false, "unregister a server id", "<pnn> <type> <id>" },
4986         { "chksrvid",        chksrvid,                  false,  false, "check if a server id exists", "<pnn> <type> <id>" },
4987         { "getsrvids",       getsrvids,                 false,  false, "get a list of all server ids"},
4988         { "vacuum",          ctdb_vacuum,               false,  false, "vacuum the databases of empty records", "[max_records]"},
4989         { "repack",          ctdb_repack,               false,  false, "repack all databases", "[max_freelist]"},
4990         { "listnodes",       control_listnodes,         false,  true, "list all nodes in the cluster"},
4991         { "reloadnodes",     control_reload_nodes_file, false,  false, "reload the nodes file and restart the transport on all nodes"},
4992         { "moveip",          control_moveip,            false,  false, "move/failover an ip address to another node", "<ip> <node>"},
4993         { "rebalanceip",     control_rebalanceip,       false,  false, "release an ip from the node and let recd rebalance it", "<ip>"},
4994         { "addip",           control_addip,             true,   false, "add a ip address to a node", "<ip/mask> <iface>"},
4995         { "delip",           control_delip,             false,  false, "delete an ip address from a node", "<ip>"},
4996         { "eventscript",     control_eventscript,       true,   false, "run the eventscript with the given parameters on a node", "<arguments>"},
4997         { "backupdb",        control_backupdb,          false,  false, "backup the database into a file.", "<database> <file>"},
4998         { "restoredb",        control_restoredb,        false,  false, "restore the database from a file.", "<file> [dbname]"},
4999         { "dumpdbbackup",    control_dumpdbbackup,      false,  true,  "dump database backup from a file.", "<file>"},
5000         { "wipedb",           control_wipedb,        false,     false, "wipe the contents of a database.", "<dbname>"},
5001         { "recmaster",        control_recmaster,        false,  false, "show the pnn for the recovery master."},
5002         { "scriptstatus",    control_scriptstatus,  false,      false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
5003         { "enablescript",     control_enablescript,  false,     false, "enable an eventscript", "<script>"},
5004         { "disablescript",    control_disablescript,  false,    false, "disable an eventscript", "<script>"},
5005         { "natgwlist",        control_natgwlist,        false,  false, "show the nodes belonging to this natgw configuration"},
5006         { "xpnn",             control_xpnn,             true,   true,  "find the pnn of the local node without talking to the daemon (unreliable)" },
5007         { "getreclock",       control_getreclock,       false,  false, "Show the reclock file of a node"},
5008         { "setreclock",       control_setreclock,       false,  false, "Set/clear the reclock file of a node", "[filename]"},
5009         { "setnatgwstate",    control_setnatgwstate,    false,  false, "Set NATGW state to on/off", "{on|off}"},
5010         { "setlmasterrole",   control_setlmasterrole,   false,  false, "Set LMASTER role to on/off", "{on|off}"},
5011         { "setrecmasterrole", control_setrecmasterrole, false,  false, "Set RECMASTER role to on/off", "{on|off}"},
5012         { "setdbprio",        control_setdbprio,        false,  false, "Set DB priority", "<dbid> <prio:1-3>"},
5013         { "getdbprio",        control_getdbprio,        false,  false, "Get DB priority", "<dbid>"},
5014         { "msglisten",        control_msglisten,        false,  false, "Listen on a srvid port for messages", "<msg srvid>"},
5015         { "msgsend",          control_msgsend,  false,  false, "Send a message to srvid", "<srvid> <message>"},
5016         { "sync",            control_ipreallocate,      false,  false,  "wait until ctdbd has synced all state changes" },
5017         { "pfetch",          control_pfetch,            false,  false,  "fetch a record from a persistent database", "<db> <key> [<file>]" },
5018         { "pstore",          control_pstore,            false,  false,  "write a record to a persistent database", "<db> <key> <file containing record>" },
5019         { "tfetch",          control_tfetch,            false,  true,  "fetch a record from a [c]tdb-file", "<tdb-file> <key> [<file>]" },
5020         { "readkey",         control_readkey,           true,   false,  "read the content off a database key", "<tdb-file> <key>" },
5021         { "writekey",        control_writekey,          true,   false,  "write to a database key", "<tdb-file> <key> <value>" },
5022         { "checktcpport",    control_chktcpport,        false,  true,  "check if a service is bound to a specific tcp port or not", "<port>" },
5023 };
5024
5025 /*
5026   show usage message
5027  */
5028 static void usage(void)
5029 {
5030         int i;
5031         printf(
5032 "Usage: ctdb [options] <control>\n" \
5033 "Options:\n" \
5034 "   -n <node>          choose node number, or 'all' (defaults to local node)\n"
5035 "   -Y                 generate machinereadable output\n"
5036 "   -v                 generate verbose output\n"
5037 "   -t <timelimit>     set timelimit for control in seconds (default %u)\n", options.timelimit);
5038         printf("Controls:\n");
5039         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5040                 printf("  %-15s %-27s  %s\n", 
5041                        ctdb_commands[i].name, 
5042                        ctdb_commands[i].args?ctdb_commands[i].args:"",
5043                        ctdb_commands[i].msg);
5044         }
5045         exit(1);
5046 }
5047
5048
5049 static void ctdb_alarm(int sig)
5050 {
5051         printf("Maximum runtime exceeded - exiting\n");
5052         _exit(ERR_TIMEOUT);
5053 }
5054
5055 /*
5056   main program
5057 */
5058 int main(int argc, const char *argv[])
5059 {
5060         struct ctdb_context *ctdb;
5061         char *nodestring = NULL;
5062         struct poptOption popt_options[] = {
5063                 POPT_AUTOHELP
5064                 POPT_CTDB_CMDLINE
5065                 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
5066                 { "node",      'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
5067                 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
5068                 { "verbose",    'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
5069                 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
5070                 POPT_TABLEEND
5071         };
5072         int opt;
5073         const char **extra_argv;
5074         int extra_argc = 0;
5075         int ret=-1, i;
5076         poptContext pc;
5077         struct event_context *ev;
5078         const char *control;
5079
5080         setlinebuf(stdout);
5081         
5082         /* set some defaults */
5083         options.maxruntime = 0;
5084         options.timelimit = 3;
5085         options.pnn = CTDB_CURRENT_NODE;
5086
5087         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
5088
5089         while ((opt = poptGetNextOpt(pc)) != -1) {
5090                 switch (opt) {
5091                 default:
5092                         DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
5093                                 poptBadOption(pc, 0), poptStrerror(opt)));
5094                         exit(1);
5095                 }
5096         }
5097
5098         /* setup the remaining options for the main program to use */
5099         extra_argv = poptGetArgs(pc);
5100         if (extra_argv) {
5101                 extra_argv++;
5102                 while (extra_argv[extra_argc]) extra_argc++;
5103         }
5104
5105         if (extra_argc < 1) {
5106                 usage();
5107         }
5108
5109         if (options.maxruntime == 0) {
5110                 const char *ctdb_timeout;
5111                 ctdb_timeout = getenv("CTDB_TIMEOUT");
5112                 if (ctdb_timeout != NULL) {
5113                         options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5114                 } else {
5115                         /* default timeout is 120 seconds */
5116                         options.maxruntime = 120;
5117                 }
5118         }
5119
5120         signal(SIGALRM, ctdb_alarm);
5121         alarm(options.maxruntime);
5122
5123         /* setup the node number to contact */
5124         if (nodestring != NULL) {
5125                 if (strcmp(nodestring, "all") == 0) {
5126                         options.pnn = CTDB_BROADCAST_ALL;
5127                 } else {
5128                         options.pnn = strtoul(nodestring, NULL, 0);
5129                 }
5130         }
5131
5132         control = extra_argv[0];
5133
5134         ev = event_context_init(NULL);
5135         if (!ev) {
5136                 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5137                 exit(1);
5138         }
5139         tevent_loop_allow_nesting(ev);
5140
5141         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5142                 if (strcmp(control, ctdb_commands[i].name) == 0) {
5143                         int j;
5144
5145                         if (ctdb_commands[i].without_daemon == true) {
5146                                 close(2);
5147                         }
5148
5149                         if (ctdb_commands[i].without_daemon == false) {
5150                                 const char *socket_name;
5151
5152                                 /* initialise ctdb */
5153                                 ctdb = ctdb_cmdline_client(ev);
5154
5155                                 if (ctdb == NULL) {
5156                                         DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5157                                         exit(1);
5158                                 }
5159
5160                                 /* initialize a libctdb connection as well */
5161                                 socket_name = ctdb_get_socketname(ctdb);
5162                                 ctdb_connection = ctdb_connect(socket_name,
5163                                                        ctdb_log_file, stderr);
5164                                 if (ctdb_connection == NULL) {
5165                                         fprintf(stderr, "Failed to connect to daemon from libctdb\n");
5166                                         exit(1);
5167                                 }                               
5168                         
5169                                 /* verify the node exists */
5170                                 verify_node(ctdb);
5171
5172                                 if (options.pnn == CTDB_CURRENT_NODE) {
5173                                         int pnn;
5174                                         pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);         
5175                                         if (pnn == -1) {
5176                                                 return -1;
5177                                         }
5178                                         options.pnn = pnn;
5179                                 }
5180                         }
5181
5182                         if (ctdb_commands[i].auto_all && 
5183                             options.pnn == CTDB_BROADCAST_ALL) {
5184                                 uint32_t *nodes;
5185                                 uint32_t num_nodes;
5186                                 ret = 0;
5187
5188                                 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
5189                                 CTDB_NO_MEMORY(ctdb, nodes);
5190         
5191                                 for (j=0;j<num_nodes;j++) {
5192                                         options.pnn = nodes[j];
5193                                         ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5194                                 }
5195                                 talloc_free(nodes);
5196                         } else {
5197                                 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5198                         }
5199                         break;
5200                 }
5201         }
5202
5203         if (i == ARRAY_SIZE(ctdb_commands)) {
5204                 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
5205                 exit(1);
5206         }
5207
5208         return ret;
5209 }