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