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