4 Copyright (C) Andrew Tridgell 2007
5 Copyright (C) Ronnie Sahlberg 2007
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.
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.
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/>.
22 #include "lib/tevent/tevent.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "system/network.h"
26 #include "system/locale.h"
29 #include "../include/ctdb.h"
30 #include "../include/ctdb_client.h"
31 #include "../include/ctdb_private.h"
32 #include "../common/rb_tree.h"
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 */
39 struct ctdb_connection *ctdb_connection;
41 static void usage(void);
51 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
52 #define LONGTIMELIMIT() timeval_current_ofs(options.timelimit*10, 0)
55 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
58 #define XSTR(x) STR(x)
59 printf("CTDB version: %s\n", XSTR(CTDB_VERS));
66 verify that a node exists and is reachable
68 static void verify_node(struct ctdb_context *ctdb)
71 struct ctdb_node_map *nodemap=NULL;
73 if (options.pnn == CTDB_CURRENT_NODE) {
76 if (options.pnn == CTDB_BROADCAST_ALL) {
80 /* verify the node exists */
81 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
82 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
85 if (options.pnn >= nodemap->num) {
86 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", options.pnn));
89 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DELETED) {
90 DEBUG(DEBUG_ERR, ("Node %u is DELETED\n", options.pnn));
93 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DISCONNECTED) {
94 DEBUG(DEBUG_ERR, ("Node %u is DISCONNECTED\n", options.pnn));
98 /* verify we can access the node */
99 ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
101 DEBUG(DEBUG_ERR,("Can not access node. Node is not operational.\n"));
107 check if a database exists
109 static int db_exists(struct ctdb_context *ctdb, const char *db_name)
112 struct ctdb_dbid_map *dbmap=NULL;
114 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
116 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
120 for(i=0;i<dbmap->num;i++){
123 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
124 if (!strcmp(name, db_name)) {
133 see if a process exists
135 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
143 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
144 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
148 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
150 printf("%u:%u exists\n", pnn, pid);
152 printf("%u:%u does not exist\n", pnn, pid);
158 display statistics structure
160 static void show_statistics(struct ctdb_statistics *s)
162 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
164 const char *prefix=NULL;
166 int tmp, days, hours, minutes, seconds;
171 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
172 STATISTICS_FIELD(num_clients),
173 STATISTICS_FIELD(frozen),
174 STATISTICS_FIELD(recovering),
175 STATISTICS_FIELD(num_recoveries),
176 STATISTICS_FIELD(client_packets_sent),
177 STATISTICS_FIELD(client_packets_recv),
178 STATISTICS_FIELD(node_packets_sent),
179 STATISTICS_FIELD(node_packets_recv),
180 STATISTICS_FIELD(keepalive_packets_sent),
181 STATISTICS_FIELD(keepalive_packets_recv),
182 STATISTICS_FIELD(node.req_call),
183 STATISTICS_FIELD(node.reply_call),
184 STATISTICS_FIELD(node.req_dmaster),
185 STATISTICS_FIELD(node.reply_dmaster),
186 STATISTICS_FIELD(node.reply_error),
187 STATISTICS_FIELD(node.req_message),
188 STATISTICS_FIELD(node.req_control),
189 STATISTICS_FIELD(node.reply_control),
190 STATISTICS_FIELD(client.req_call),
191 STATISTICS_FIELD(client.req_message),
192 STATISTICS_FIELD(client.req_control),
193 STATISTICS_FIELD(timeouts.call),
194 STATISTICS_FIELD(timeouts.control),
195 STATISTICS_FIELD(timeouts.traverse),
196 STATISTICS_FIELD(total_calls),
197 STATISTICS_FIELD(pending_calls),
198 STATISTICS_FIELD(lockwait_calls),
199 STATISTICS_FIELD(pending_lockwait_calls),
200 STATISTICS_FIELD(childwrite_calls),
201 STATISTICS_FIELD(pending_childwrite_calls),
202 STATISTICS_FIELD(memory_used),
203 STATISTICS_FIELD(max_hop_count),
205 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
214 printf("CTDB version %u\n", CTDB_VERSION);
215 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
216 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
218 for (i=0;i<ARRAY_SIZE(fields);i++) {
219 if (strchr(fields[i].name, '.')) {
220 preflen = strcspn(fields[i].name, ".")+1;
221 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
222 prefix = fields[i].name;
223 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
228 printf(" %*s%-22s%*s%10u\n",
230 fields[i].name+preflen,
232 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
234 printf(" %-30s %.6f sec\n", "max_reclock_ctdbd", s->reclock.ctdbd);
235 printf(" %-30s %.6f sec\n", "max_reclock_recd", s->reclock.recd);
237 printf(" %-30s %.6f sec\n", "max_call_latency", s->max_call_latency);
238 printf(" %-30s %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
239 printf(" %-30s %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
240 printf(" %-30s %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
242 talloc_free(tmp_ctx);
246 display remote ctdb statistics combined from all nodes
248 static int control_statistics_all(struct ctdb_context *ctdb)
251 struct ctdb_statistics statistics;
255 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
256 CTDB_NO_MEMORY(ctdb, nodes);
258 ZERO_STRUCT(statistics);
260 for (i=0;i<num_nodes;i++) {
261 struct ctdb_statistics s1;
263 uint32_t *v1 = (uint32_t *)&s1;
264 uint32_t *v2 = (uint32_t *)&statistics;
266 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
267 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
269 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
272 for (j=0;j<num_ints;j++) {
275 statistics.max_hop_count =
276 MAX(statistics.max_hop_count, s1.max_hop_count);
277 statistics.max_call_latency =
278 MAX(statistics.max_call_latency, s1.max_call_latency);
279 statistics.max_lockwait_latency =
280 MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
283 printf("Gathered statistics for %u nodes\n", num_nodes);
284 show_statistics(&statistics);
289 display remote ctdb statistics
291 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
294 struct ctdb_statistics statistics;
296 if (options.pnn == CTDB_BROADCAST_ALL) {
297 return control_statistics_all(ctdb);
300 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
302 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
305 show_statistics(&statistics);
311 reset remote ctdb statistics
313 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
317 ret = ctdb_statistics_reset(ctdb, options.pnn);
319 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
327 display remote ctdb rolling statistics
329 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
332 struct ctdb_statistics_wire *stats;
333 int i, num_records = -1;
336 num_records = atoi(argv[0]) - 1;
339 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
341 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
344 for (i=0;i<stats->num;i++) {
345 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
348 show_statistics(&stats->stats[i]);
349 if (i == num_records) {
359 display uptime of remote node
361 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
364 struct ctdb_uptime *uptime = NULL;
365 int tmp, days, hours, minutes, seconds;
367 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
369 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
373 if (options.machinereadable){
374 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
375 printf(":%u:%u:%u:%lf\n",
376 (unsigned int)uptime->current_time.tv_sec,
377 (unsigned int)uptime->ctdbd_start_time.tv_sec,
378 (unsigned int)uptime->last_recovery_finished.tv_sec,
379 timeval_delta(&uptime->last_recovery_finished,
380 &uptime->last_recovery_started)
385 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
387 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
395 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
397 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
405 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
407 printf("Duration of last recovery/failover: %lf seconds\n",
408 timeval_delta(&uptime->last_recovery_finished,
409 &uptime->last_recovery_started));
415 show the PNN of the current node
417 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
422 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
424 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
428 printf("PNN:%d\n", mypnn);
434 struct pnn_node *next;
439 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
441 const char *nodes_list;
445 struct pnn_node *pnn_nodes = NULL;
446 struct pnn_node *pnn_node;
447 struct pnn_node *tmp_node;
449 /* read the nodes file */
450 nodes_list = getenv("CTDB_NODES");
451 if (nodes_list == NULL) {
452 nodes_list = "/etc/ctdb/nodes";
454 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
458 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
461 for (i=0, pnn=0; i<nlines; i++) {
465 /* strip leading spaces */
466 while((*node == ' ') || (*node == '\t')) {
473 if (strcmp(node, "") == 0) {
476 pnn_node = talloc(mem_ctx, struct pnn_node);
477 pnn_node->pnn = pnn++;
478 pnn_node->addr = talloc_strdup(pnn_node, node);
479 pnn_node->next = pnn_nodes;
480 pnn_nodes = pnn_node;
483 /* swap them around so we return them in incrementing order */
484 pnn_node = pnn_nodes;
488 pnn_node = pnn_node->next;
490 tmp_node->next = pnn_nodes;
491 pnn_nodes = tmp_node;
498 show the PNN of the current node
499 discover the pnn by loading the nodes file and try to bind to all
500 addresses one at a time until the ip address is found.
502 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
504 TALLOC_CTX *mem_ctx = talloc_new(NULL);
505 struct pnn_node *pnn_nodes;
506 struct pnn_node *pnn_node;
508 pnn_nodes = read_nodes_file(mem_ctx);
509 if (pnn_nodes == NULL) {
510 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
511 talloc_free(mem_ctx);
515 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
518 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
519 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
520 talloc_free(mem_ctx);
524 if (ctdb_sys_have_ip(&addr)) {
525 printf("PNN:%d\n", pnn_node->pnn);
526 talloc_free(mem_ctx);
531 printf("Failed to detect which PNN this node is\n");
532 talloc_free(mem_ctx);
537 display remote ctdb status
539 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
542 struct ctdb_vnn_map *vnnmap=NULL;
543 struct ctdb_node_map *nodemap=NULL;
544 uint32_t recmode, recmaster;
547 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
552 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
554 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
558 if(options.machinereadable){
559 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped:Inactive:\n");
560 for(i=0;i<nodemap->num;i++){
561 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
564 printf(":%d:%s:%d:%d:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
565 ctdb_addr_to_str(&nodemap->nodes[i].addr),
566 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
567 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
568 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
569 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
570 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED),
571 !!(nodemap->nodes[i].flags&NODE_FLAGS_INACTIVE));
576 printf("Number of nodes:%d\n", nodemap->num);
577 for(i=0;i<nodemap->num;i++){
578 static const struct {
582 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
583 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
584 { NODE_FLAGS_BANNED, "BANNED" },
585 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
586 { NODE_FLAGS_DELETED, "DELETED" },
587 { NODE_FLAGS_STOPPED, "STOPPED" },
588 { NODE_FLAGS_INACTIVE, "INACTIVE" },
590 char *flags_str = NULL;
593 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
596 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
597 if (nodemap->nodes[i].flags & flag_names[j].flag) {
598 if (flags_str == NULL) {
599 flags_str = talloc_strdup(ctdb, flag_names[j].name);
601 flags_str = talloc_asprintf_append(flags_str, "|%s",
604 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
607 if (flags_str == NULL) {
608 flags_str = talloc_strdup(ctdb, "OK");
609 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
611 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
612 ctdb_addr_to_str(&nodemap->nodes[i].addr),
614 nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
615 talloc_free(flags_str);
618 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
620 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
623 if (vnnmap->generation == INVALID_GENERATION) {
624 printf("Generation:INVALID\n");
626 printf("Generation:%d\n",vnnmap->generation);
628 printf("Size:%d\n",vnnmap->size);
629 for(i=0;i<vnnmap->size;i++){
630 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
633 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
635 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
638 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
640 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
642 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
645 printf("Recovery master:%d\n",recmaster);
652 struct natgw_node *next;
657 display the list of nodes belonging to this natgw configuration
659 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
662 uint32_t capabilities;
663 const char *natgw_list;
666 struct natgw_node *natgw_nodes = NULL;
667 struct natgw_node *natgw_node;
668 struct ctdb_node_map *nodemap=NULL;
671 /* read the natgw nodes file into a linked list */
672 natgw_list = getenv("NATGW_NODES");
673 if (natgw_list == NULL) {
674 natgw_list = "/etc/ctdb/natgw_nodes";
676 lines = file_lines_load(natgw_list, &nlines, ctdb);
678 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
681 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
684 for (i=0;i<nlines;i++) {
688 /* strip leading spaces */
689 while((*node == ' ') || (*node == '\t')) {
695 if (strcmp(node, "") == 0) {
698 natgw_node = talloc(ctdb, struct natgw_node);
699 natgw_node->addr = talloc_strdup(natgw_node, node);
700 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
701 natgw_node->next = natgw_nodes;
702 natgw_nodes = natgw_node;
705 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
707 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
712 while(i<nodemap->num) {
713 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
714 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
719 /* this node was not in the natgw so we just remove it from
722 if ((natgw_node == NULL)
723 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
726 for (j=i+1; j<nodemap->num; j++) {
727 nodemap->nodes[j-1] = nodemap->nodes[j];
736 /* pick a node to be natgwmaster
737 * we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
739 for(i=0;i<nodemap->num;i++){
740 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
741 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
743 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
746 if (!(capabilities&CTDB_CAP_NATGW)) {
749 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
753 /* we couldnt find any healthy node, try unhealthy ones */
754 if (i == nodemap->num) {
755 for(i=0;i<nodemap->num;i++){
756 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
757 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
759 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
762 if (!(capabilities&CTDB_CAP_NATGW)) {
765 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
770 /* unless all nodes are STOPPED, when we pick one anyway */
771 if (i == nodemap->num) {
772 for(i=0;i<nodemap->num;i++){
773 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
774 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
776 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
779 if (!(capabilities&CTDB_CAP_NATGW)) {
782 printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
786 /* or if we still can not find any */
787 if (i == nodemap->num) {
788 printf("-1 0.0.0.0\n");
792 /* print the pruned list of nodes belonging to this natgw list */
793 for(i=0;i<nodemap->num;i++){
794 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
797 printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
798 ctdb_addr_to_str(&nodemap->nodes[i].addr),
799 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
800 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
801 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
802 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
803 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
810 display the status of the scripts for monitoring (or other events)
812 static int control_one_scriptstatus(struct ctdb_context *ctdb,
813 enum ctdb_eventscript_call type)
815 struct ctdb_scripts_wire *script_status;
818 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
820 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
824 if (script_status == NULL) {
825 if (!options.machinereadable) {
826 printf("%s cycle never run\n",
827 ctdb_eventscript_call_names[type]);
832 if (!options.machinereadable) {
833 printf("%d scripts were executed last %s cycle\n",
834 script_status->num_scripts,
835 ctdb_eventscript_call_names[type]);
837 for (i=0; i<script_status->num_scripts; i++) {
838 const char *status = NULL;
840 switch (script_status->scripts[i].status) {
851 if (script_status->scripts[i].status > 0)
855 if (options.machinereadable) {
856 printf("%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
857 ctdb_eventscript_call_names[type],
858 script_status->scripts[i].name,
859 script_status->scripts[i].status,
861 (long)script_status->scripts[i].start.tv_sec,
862 (long)script_status->scripts[i].start.tv_usec,
863 (long)script_status->scripts[i].finished.tv_sec,
864 (long)script_status->scripts[i].finished.tv_usec,
865 script_status->scripts[i].output);
869 printf("%-20s Status:%s ",
870 script_status->scripts[i].name, status);
872 /* Some other error, eg from stat. */
873 printf("%-20s Status:CANNOT RUN (%s)",
874 script_status->scripts[i].name,
875 strerror(-script_status->scripts[i].status));
877 if (script_status->scripts[i].status >= 0) {
878 printf("Duration:%.3lf ",
879 timeval_delta(&script_status->scripts[i].finished,
880 &script_status->scripts[i].start));
882 if (script_status->scripts[i].status != -ENOEXEC) {
884 ctime(&script_status->scripts[i].start.tv_sec));
885 if (script_status->scripts[i].status != 0) {
886 printf(" OUTPUT:%s\n",
887 script_status->scripts[i].output);
897 static int control_scriptstatus(struct ctdb_context *ctdb,
898 int argc, const char **argv)
901 enum ctdb_eventscript_call type, min, max;
905 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
910 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
914 for (type = 0; type < CTDB_EVENT_MAX; type++) {
915 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
921 if (type == CTDB_EVENT_MAX) {
922 if (strcmp(arg, "all") == 0) {
924 max = CTDB_EVENT_MAX;
926 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
931 if (options.machinereadable) {
932 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
935 for (type = min; type < max; type++) {
936 ret = control_one_scriptstatus(ctdb, type);
946 enable an eventscript
948 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
956 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
958 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
966 disable an eventscript
968 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
976 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
978 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
986 display the pnn of the recovery master
988 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
993 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
995 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
998 printf("%d\n",recmaster);
1004 add a tickle to a public address
1006 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1008 struct ctdb_tcp_connection t;
1016 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1017 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1020 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1021 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1025 data.dptr = (uint8_t *)&t;
1026 data.dsize = sizeof(t);
1028 /* tell all nodes about this tcp connection */
1029 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1030 0, data, ctdb, NULL, NULL, NULL, NULL);
1032 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1041 delete a tickle from a node
1043 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1045 struct ctdb_tcp_connection t;
1053 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1054 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1057 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1058 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1062 data.dptr = (uint8_t *)&t;
1063 data.dsize = sizeof(t);
1065 /* tell all nodes about this tcp connection */
1066 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1067 0, data, ctdb, NULL, NULL, NULL, NULL);
1069 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1078 get a list of all tickles for this pnn
1080 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1082 struct ctdb_control_tcp_tickle_list *list;
1083 ctdb_sock_addr addr;
1092 port = atoi(argv[1]);
1095 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1096 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1100 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1102 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1106 if (options.machinereadable){
1107 printf(":source ip:port:destination ip:port:\n");
1108 for (i=0;i<list->tickles.num;i++) {
1109 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1112 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1113 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1116 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1117 printf("Num tickles:%u\n", list->tickles.num);
1118 for (i=0;i<list->tickles.num;i++) {
1119 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1122 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1123 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));
1133 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1135 struct ctdb_all_public_ips *ips;
1136 struct ctdb_public_ip ip;
1139 uint32_t disable_time;
1141 struct ctdb_node_map *nodemap=NULL;
1142 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1145 data.dptr = (uint8_t*)&disable_time;
1146 data.dsize = sizeof(disable_time);
1147 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1149 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1155 /* read the public ip list from the node */
1156 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1158 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1159 talloc_free(tmp_ctx);
1163 for (i=0;i<ips->num;i++) {
1164 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1169 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1170 pnn, ctdb_addr_to_str(addr)));
1171 talloc_free(tmp_ctx);
1178 data.dptr = (uint8_t *)&ip;
1179 data.dsize = sizeof(ip);
1181 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1183 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1184 talloc_free(tmp_ctx);
1188 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1189 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1196 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1197 talloc_free(tmp_ctx);
1201 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1203 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1204 talloc_free(tmp_ctx);
1208 /* update the recovery daemon so it now knows to expect the new
1209 node assignment for this ip.
1211 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1213 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1217 talloc_free(tmp_ctx);
1222 move/failover an ip address to a specific node
1224 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1227 ctdb_sock_addr addr;
1234 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1235 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1240 if (sscanf(argv[1], "%u", &pnn) != 1) {
1241 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1245 if (move_ip(ctdb, &addr, pnn) != 0) {
1246 DEBUG(DEBUG_ERR,("Failed to move ip to node %d\n", pnn));
1253 void getips_store_callback(void *param, void *data)
1255 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1256 struct ctdb_all_public_ips *ips = param;
1260 ips->ips[i].pnn = node_ip->pnn;
1261 ips->ips[i].addr = node_ip->addr;
1264 void getips_count_callback(void *param, void *data)
1266 uint32_t *count = param;
1272 static uint32_t *ip_key(ctdb_sock_addr *ip)
1274 static uint32_t key[IP_KEYLEN];
1276 bzero(key, sizeof(key));
1278 switch (ip->sa.sa_family) {
1280 key[0] = ip->ip.sin_addr.s_addr;
1283 key[0] = ip->ip6.sin6_addr.s6_addr32[3];
1284 key[1] = ip->ip6.sin6_addr.s6_addr32[2];
1285 key[2] = ip->ip6.sin6_addr.s6_addr32[1];
1286 key[3] = ip->ip6.sin6_addr.s6_addr32[0];
1289 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1296 static void *add_ip_callback(void *parm, void *data)
1302 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1304 struct ctdb_all_public_ips *tmp_ips;
1305 struct ctdb_node_map *nodemap=NULL;
1306 trbt_tree_t *ip_tree;
1310 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1312 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1316 ip_tree = trbt_create(tmp_ctx, 0);
1318 for(i=0;i<nodemap->num;i++){
1319 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1322 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1326 /* read the public ip list from this node */
1327 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1329 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1333 for (j=0; j<tmp_ips->num;j++) {
1334 struct ctdb_public_ip *node_ip;
1336 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1337 node_ip->pnn = tmp_ips->ips[j].pnn;
1338 node_ip->addr = tmp_ips->ips[j].addr;
1340 trbt_insertarray32_callback(ip_tree,
1341 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1345 talloc_free(tmp_ips);
1350 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1352 len = offsetof(struct ctdb_all_public_ips, ips) +
1353 count*sizeof(struct ctdb_public_ip);
1354 tmp_ips = talloc_zero_size(tmp_ctx, len);
1355 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1364 * scans all other nodes and returns a pnn for another node that can host this
1368 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1370 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1371 struct ctdb_all_public_ips *ips;
1372 struct ctdb_node_map *nodemap=NULL;
1375 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1377 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1378 talloc_free(tmp_ctx);
1382 for(i=0;i<nodemap->num;i++){
1383 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1386 if (nodemap->nodes[i].pnn == options.pnn) {
1390 /* read the public ip list from this node */
1391 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1393 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1397 for (j=0;j<ips->num;j++) {
1398 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1399 talloc_free(tmp_ctx);
1400 return nodemap->nodes[i].pnn;
1406 talloc_free(tmp_ctx);
1411 add a public ip address to a node
1413 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
1419 ctdb_sock_addr addr;
1420 struct ctdb_control_ip_iface *pub;
1421 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1422 struct ctdb_all_public_ips *ips;
1426 talloc_free(tmp_ctx);
1430 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1431 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1432 talloc_free(tmp_ctx);
1436 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1438 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1439 talloc_free(tmp_ctx);
1444 /* check if some other node is already serving this ip, if not,
1447 for (i=0;i<ips->num;i++) {
1448 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1453 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1454 pub = talloc_size(tmp_ctx, len);
1455 CTDB_NO_MEMORY(ctdb, pub);
1459 pub->len = strlen(argv[1])+1;
1460 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1462 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1464 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
1465 talloc_free(tmp_ctx);
1469 if (i == ips->num) {
1470 /* no one has this ip so we claim it */
1473 pnn = ips->ips[i].pnn;
1476 if (move_ip(ctdb, &addr, pnn) != 0) {
1477 DEBUG(DEBUG_ERR,("Failed to move ip to node %d\n", pnn));
1481 talloc_free(tmp_ctx);
1485 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
1487 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
1489 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1490 struct ctdb_node_map *nodemap=NULL;
1491 struct ctdb_all_public_ips *ips;
1494 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1496 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
1500 /* remove it from the nodes that are not hosting the ip currently */
1501 for(i=0;i<nodemap->num;i++){
1502 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1505 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1506 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1510 for (j=0;j<ips->num;j++) {
1511 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1519 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
1523 options.pnn = nodemap->nodes[i].pnn;
1524 control_delip(ctdb, argc, argv);
1528 /* remove it from every node (also the one hosting it) */
1529 for(i=0;i<nodemap->num;i++){
1530 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1533 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1534 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1538 for (j=0;j<ips->num;j++) {
1539 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1547 options.pnn = nodemap->nodes[i].pnn;
1548 control_delip(ctdb, argc, argv);
1551 talloc_free(tmp_ctx);
1556 delete a public ip address from a node
1558 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
1561 ctdb_sock_addr addr;
1562 struct ctdb_control_ip_iface pub;
1563 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1564 struct ctdb_all_public_ips *ips;
1567 talloc_free(tmp_ctx);
1571 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1572 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1576 if (options.pnn == CTDB_BROADCAST_ALL) {
1577 return control_delip_all(ctdb, argc, argv, &addr);
1584 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1586 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1587 talloc_free(tmp_ctx);
1591 for (i=0;i<ips->num;i++) {
1592 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1598 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
1599 ctdb_addr_to_str(&addr)));
1600 talloc_free(tmp_ctx);
1604 if (ips->ips[i].pnn == options.pnn) {
1605 ret = find_other_host_for_public_ip(ctdb, &addr);
1607 if (move_ip(ctdb, &addr, ret) != 0) {
1608 DEBUG(DEBUG_ERR,("Failed to move ip to node %d\n", ret));
1614 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
1616 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
1617 talloc_free(tmp_ctx);
1621 talloc_free(tmp_ctx);
1626 kill a tcp connection
1628 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1631 struct ctdb_control_killtcp killtcp;
1637 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
1638 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1642 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
1643 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1647 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
1649 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1660 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1663 ctdb_sock_addr addr;
1669 if (!parse_ip(argv[0], NULL, 0, &addr)) {
1670 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1674 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1676 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1684 register a server id
1686 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1689 struct ctdb_server_id server_id;
1695 server_id.pnn = strtoul(argv[0], NULL, 0);
1696 server_id.type = strtoul(argv[1], NULL, 0);
1697 server_id.server_id = strtoul(argv[2], NULL, 0);
1699 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1701 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1704 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
1710 unregister a server id
1712 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1715 struct ctdb_server_id server_id;
1721 server_id.pnn = strtoul(argv[0], NULL, 0);
1722 server_id.type = strtoul(argv[1], NULL, 0);
1723 server_id.server_id = strtoul(argv[2], NULL, 0);
1725 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
1727 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
1734 check if a server id exists
1736 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1740 struct ctdb_server_id server_id;
1746 server_id.pnn = strtoul(argv[0], NULL, 0);
1747 server_id.type = strtoul(argv[1], NULL, 0);
1748 server_id.server_id = strtoul(argv[2], NULL, 0);
1750 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
1752 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
1757 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
1759 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
1765 get a list of all server ids that are registered on a node
1767 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
1770 struct ctdb_server_id_list *server_ids;
1772 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
1774 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
1778 for (i=0; i<server_ids->num; i++) {
1779 printf("Server id %d:%d:%d\n",
1780 server_ids->server_ids[i].pnn,
1781 server_ids->server_ids[i].type,
1782 server_ids->server_ids[i].server_id);
1789 send a tcp tickle ack
1791 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1794 ctdb_sock_addr src, dst;
1800 if (!parse_ip_port(argv[0], &src)) {
1801 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1805 if (!parse_ip_port(argv[1], &dst)) {
1806 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1810 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
1814 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
1821 display public ip status
1823 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
1826 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1827 struct ctdb_all_public_ips *ips;
1829 if (options.pnn == CTDB_BROADCAST_ALL) {
1830 /* read the list of public ips from all nodes */
1831 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1833 /* read the public ip list from this node */
1834 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1837 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
1838 talloc_free(tmp_ctx);
1842 if (options.machinereadable){
1843 printf(":Public IP:Node:");
1844 if (options.verbose){
1845 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
1849 if (options.pnn == CTDB_BROADCAST_ALL) {
1850 printf("Public IPs on ALL nodes\n");
1852 printf("Public IPs on node %u\n", options.pnn);
1856 for (i=1;i<=ips->num;i++) {
1857 struct ctdb_control_public_ip_info *info = NULL;
1859 char *aciface = NULL;
1860 char *avifaces = NULL;
1861 char *cifaces = NULL;
1863 if (options.pnn == CTDB_BROADCAST_ALL) {
1864 pnn = ips->ips[ips->num-i].pnn;
1870 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
1871 &ips->ips[ips->num-i].addr, &info);
1878 for (j=0; j < info->num; j++) {
1879 if (cifaces == NULL) {
1880 cifaces = talloc_strdup(info,
1881 info->ifaces[j].name);
1883 cifaces = talloc_asprintf_append(cifaces,
1885 info->ifaces[j].name);
1888 if (info->active_idx == j) {
1889 aciface = info->ifaces[j].name;
1892 if (info->ifaces[j].link_state == 0) {
1896 if (avifaces == NULL) {
1897 avifaces = talloc_strdup(info, info->ifaces[j].name);
1899 avifaces = talloc_asprintf_append(avifaces,
1901 info->ifaces[j].name);
1906 if (options.machinereadable){
1908 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
1909 ips->ips[ips->num-i].pnn);
1910 if (options.verbose){
1913 avifaces?avifaces:"",
1914 cifaces?cifaces:"");
1918 if (options.verbose) {
1919 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
1920 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
1921 ips->ips[ips->num-i].pnn,
1923 avifaces?avifaces:"",
1924 cifaces?cifaces:"");
1927 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
1928 ips->ips[ips->num-i].pnn);
1934 talloc_free(tmp_ctx);
1941 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
1944 ctdb_sock_addr addr;
1945 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1946 struct ctdb_control_public_ip_info *info;
1949 talloc_free(tmp_ctx);
1953 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1954 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1958 /* read the public ip info from this node */
1959 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
1960 tmp_ctx, &addr, &info);
1962 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
1963 argv[0], options.pnn));
1964 talloc_free(tmp_ctx);
1968 printf("Public IP[%s] info on node %u\n",
1969 ctdb_addr_to_str(&info->ip.addr),
1972 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
1973 ctdb_addr_to_str(&info->ip.addr),
1974 info->ip.pnn, info->num);
1976 for (i=0; i<info->num; i++) {
1977 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
1979 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
1980 i+1, info->ifaces[i].name,
1981 info->ifaces[i].link_state?"up":"down",
1982 (unsigned int)info->ifaces[i].references,
1983 (i==info->active_idx)?" (active)":"");
1986 talloc_free(tmp_ctx);
1991 display interfaces status
1993 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
1996 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1997 struct ctdb_control_get_ifaces *ifaces;
1999 /* read the public ip list from this node */
2000 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn,
2003 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2005 talloc_free(tmp_ctx);
2009 if (options.machinereadable){
2010 printf(":Name:LinkStatus:References:\n");
2012 printf("Interfaces on node %u\n", options.pnn);
2015 for (i=0; i<ifaces->num; i++) {
2016 if (options.machinereadable){
2017 printf(":%s:%s:%u\n",
2018 ifaces->ifaces[i].name,
2019 ifaces->ifaces[i].link_state?"1":"0",
2020 (unsigned int)ifaces->ifaces[i].references);
2022 printf("name:%s link:%s references:%u\n",
2023 ifaces->ifaces[i].name,
2024 ifaces->ifaces[i].link_state?"up":"down",
2025 (unsigned int)ifaces->ifaces[i].references);
2029 talloc_free(tmp_ctx);
2035 set link status of an interface
2037 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2040 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2041 struct ctdb_control_iface_info info;
2049 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2050 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2052 talloc_free(tmp_ctx);
2055 strcpy(info.name, argv[0]);
2057 if (strcmp(argv[1], "up") == 0) {
2058 info.link_state = 1;
2059 } else if (strcmp(argv[1], "down") == 0) {
2060 info.link_state = 0;
2062 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2064 talloc_free(tmp_ctx);
2068 /* read the public ip list from this node */
2069 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2072 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2073 argv[0], options.pnn));
2074 talloc_free(tmp_ctx);
2078 talloc_free(tmp_ctx);
2083 display pid of a ctdb daemon
2085 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2090 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2092 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2095 printf("Pid:%d\n", pid);
2100 static uint32_t ipreallocate_finished;
2103 handler for receiving the response to ipreallocate
2105 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
2106 TDB_DATA data, void *private_data)
2108 ipreallocate_finished = 1;
2111 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2113 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2115 event_add_timed(ctdb->ev, ctdb,
2116 timeval_current_ofs(1, 0),
2117 ctdb_every_second, ctdb);
2121 ask the recovery daemon on the recovery master to perform a ip reallocation
2123 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2127 struct takeover_run_reply rd;
2129 struct ctdb_node_map *nodemap=NULL;
2131 struct timeval tv = timeval_current();
2133 /* we need some events to trigger so we can timeout and restart
2136 event_add_timed(ctdb->ev, ctdb,
2137 timeval_current_ofs(1, 0),
2138 ctdb_every_second, ctdb);
2140 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2142 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
2145 rd.srvid = getpid();
2147 /* register a message port for receiveing the reply so that we
2148 can receive the reply
2150 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
2152 data.dptr = (uint8_t *)&rd;
2153 data.dsize = sizeof(rd);
2156 /* check that there are valid nodes available */
2157 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
2158 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2161 for (i=0; i<nodemap->num;i++) {
2162 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
2166 if (i==nodemap->num) {
2167 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
2172 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
2174 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2178 /* verify the node exists */
2179 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
2180 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2185 /* check tha there are nodes available that can act as a recmaster */
2186 for (i=0; i<nodemap->num; i++) {
2187 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
2192 if (i == nodemap->num) {
2193 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
2197 /* verify the recovery master is not STOPPED, nor BANNED */
2198 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
2199 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
2206 /* verify the recovery master is not STOPPED, nor BANNED */
2207 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
2208 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
2214 ipreallocate_finished = 0;
2215 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
2217 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
2221 tv = timeval_current();
2222 /* this loop will terminate when we have received the reply */
2223 while (timeval_elapsed(&tv) < 3.0) {
2224 event_loop_once(ctdb->ev);
2226 if (ipreallocate_finished == 1) {
2230 DEBUG(DEBUG_ERR,("Timed out waiting for recmaster ipreallocate. Trying again\n"));
2240 disable a remote node
2242 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2245 struct ctdb_node_map *nodemap=NULL;
2247 /* check if the node is already disabled */
2248 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2249 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2252 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2253 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2258 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2260 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2266 /* read the nodemap and verify the change took effect */
2267 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2268 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2272 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2273 ret = control_ipreallocate(ctdb, argc, argv);
2275 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2283 enable a disabled remote node
2285 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2289 struct ctdb_node_map *nodemap=NULL;
2292 /* check if the node is already enabled */
2293 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2294 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2297 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2298 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2303 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2305 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2311 /* read the nodemap and verify the change took effect */
2312 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2313 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2317 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2319 ret = control_ipreallocate(ctdb, argc, argv);
2321 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2331 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2334 struct ctdb_node_map *nodemap=NULL;
2337 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2339 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2344 /* read the nodemap and verify the change took effect */
2345 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2346 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2350 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2351 ret = control_ipreallocate(ctdb, argc, argv);
2353 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2361 restart a stopped remote node
2363 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2367 struct ctdb_node_map *nodemap=NULL;
2370 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2372 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2378 /* read the nodemap and verify the change took effect */
2379 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2380 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2384 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2385 ret = control_ipreallocate(ctdb, argc, argv);
2387 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2394 static uint32_t get_generation(struct ctdb_context *ctdb)
2396 struct ctdb_vnn_map *vnnmap=NULL;
2399 /* wait until the recmaster is not in recovery mode */
2401 uint32_t recmode, recmaster;
2403 if (vnnmap != NULL) {
2404 talloc_free(vnnmap);
2408 /* get the recmaster */
2409 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
2411 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2415 /* get recovery mode */
2416 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
2418 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2422 /* get the current generation number */
2423 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2425 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2429 if ((recmode == CTDB_RECOVERY_NORMAL)
2430 && (vnnmap->generation != 1)){
2431 return vnnmap->generation;
2438 ban a node from the cluster
2440 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2443 struct ctdb_node_map *nodemap=NULL;
2444 struct ctdb_ban_time bantime;
2450 /* verify the node exists */
2451 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2453 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2457 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2458 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2462 bantime.pnn = options.pnn;
2463 bantime.time = strtoul(argv[0], NULL, 0);
2465 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2467 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2471 ret = control_ipreallocate(ctdb, argc, argv);
2473 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2482 unban a node from the cluster
2484 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2487 struct ctdb_node_map *nodemap=NULL;
2488 struct ctdb_ban_time bantime;
2490 /* verify the node exists */
2491 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2493 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2497 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2498 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2502 bantime.pnn = options.pnn;
2505 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2507 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2511 ret = control_ipreallocate(ctdb, argc, argv);
2513 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2522 show ban information for a node
2524 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2527 struct ctdb_node_map *nodemap=NULL;
2528 struct ctdb_ban_time *bantime;
2530 /* verify the node exists */
2531 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2533 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2537 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2539 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2543 if (bantime->time == 0) {
2544 printf("Node %u is not banned\n", bantime->pnn);
2546 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2555 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2559 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2561 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2571 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2574 uint32_t generation, next_generation;
2576 /* record the current generation number */
2577 generation = get_generation(ctdb);
2579 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2581 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
2585 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2587 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2591 /* wait until we are in a new generation */
2593 next_generation = get_generation(ctdb);
2594 if (next_generation != generation) {
2605 display monitoring mode of a remote node
2607 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2612 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2614 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2617 if (!options.machinereadable){
2618 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2621 printf(":%d:\n",monmode);
2628 display capabilities of a remote node
2630 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2632 uint32_t capabilities;
2635 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2637 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2641 if (!options.machinereadable){
2642 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2643 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2644 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
2645 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
2647 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
2648 printf(":%d:%d:%d:%d:\n",
2649 !!(capabilities&CTDB_CAP_RECMASTER),
2650 !!(capabilities&CTDB_CAP_LMASTER),
2651 !!(capabilities&CTDB_CAP_LVS),
2652 !!(capabilities&CTDB_CAP_NATGW));
2658 display lvs configuration
2660 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
2662 uint32_t *capabilities;
2663 struct ctdb_node_map *nodemap=NULL;
2665 int healthy_count = 0;
2667 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2669 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2673 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2674 CTDB_NO_MEMORY(ctdb, capabilities);
2676 /* collect capabilities for all connected nodes */
2677 for (i=0; i<nodemap->num; i++) {
2678 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2681 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2685 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2687 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2691 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2695 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2700 /* Print all LVS nodes */
2701 for (i=0; i<nodemap->num; i++) {
2702 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2705 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2708 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2712 if (healthy_count != 0) {
2713 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2718 printf("%d:%s\n", i,
2719 ctdb_addr_to_str(&nodemap->nodes[i].addr));
2726 display who is the lvs master
2728 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
2730 uint32_t *capabilities;
2731 struct ctdb_node_map *nodemap=NULL;
2733 int healthy_count = 0;
2735 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2737 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2741 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2742 CTDB_NO_MEMORY(ctdb, capabilities);
2744 /* collect capabilities for all connected nodes */
2745 for (i=0; i<nodemap->num; i++) {
2746 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2749 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2753 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2755 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2759 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2763 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2768 /* find and show the lvsmaster */
2769 for (i=0; i<nodemap->num; i++) {
2770 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2773 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2776 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2780 if (healthy_count != 0) {
2781 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2786 if (options.machinereadable){
2789 printf("Node %d is LVS master\n", i);
2794 printf("There is no LVS master\n");
2799 disable monitoring on a node
2801 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2806 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
2808 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
2811 printf("Monitoring mode:%s\n","DISABLED");
2817 enable monitoring on a node
2819 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2824 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
2826 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
2829 printf("Monitoring mode:%s\n","ACTIVE");
2835 display remote list of keys/data for a db
2837 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
2839 const char *db_name;
2840 struct ctdb_db_context *ctdb_db;
2850 if (db_exists(ctdb, db_name)) {
2851 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
2855 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
2857 if (ctdb_db == NULL) {
2858 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2862 /* traverse and dump the cluster tdb */
2863 ret = ctdb_dump_db(ctdb_db, stdout);
2865 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
2866 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
2867 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
2871 talloc_free(ctdb_db);
2873 printf("Dumped %d records\n", ret);
2879 fetch a record from a persistent database
2881 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
2883 const char *db_name;
2884 struct ctdb_db_context *ctdb_db;
2885 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2886 struct ctdb_transaction_handle *h;
2891 talloc_free(tmp_ctx);
2898 if (db_exists(ctdb, db_name)) {
2899 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
2900 talloc_free(tmp_ctx);
2904 ctdb_db = ctdb_attach(ctdb, db_name, true, 0);
2906 if (ctdb_db == NULL) {
2907 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2908 talloc_free(tmp_ctx);
2912 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
2914 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
2915 talloc_free(tmp_ctx);
2919 key.dptr = discard_const(argv[1]);
2920 key.dsize = strlen(argv[1]);
2921 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
2923 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
2924 talloc_free(tmp_ctx);
2928 if (data.dsize == 0 || data.dptr == NULL) {
2929 DEBUG(DEBUG_ERR,("Record is empty\n"));
2930 talloc_free(tmp_ctx);
2935 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
2937 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
2938 talloc_free(tmp_ctx);
2941 write(fd, data.dptr, data.dsize);
2944 write(1, data.dptr, data.dsize);
2947 /* abort the transaction */
2951 talloc_free(tmp_ctx);
2956 fetch a record from a tdb-file
2958 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
2960 const char *tdb_file;
2971 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
2973 DEBUG(DEBUG_ERR,("Failed to open TDB file %s\n", tdb_file));
2977 key.dptr = discard_const(argv[1]);
2978 key.dsize = strlen(argv[1]);
2979 data = tdb_fetch(tdb, key);
2980 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
2981 DEBUG(DEBUG_ERR,("Failed to read record %s from tdb %s\n", argv[1], tdb_file));
2989 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
2991 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
2994 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
2997 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3004 write a record to a persistent database
3006 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3008 const char *db_name;
3009 struct ctdb_db_context *ctdb_db;
3010 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3011 struct ctdb_transaction_handle *h;
3017 talloc_free(tmp_ctx);
3021 fd = open(argv[2], O_RDONLY);
3023 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3024 talloc_free(tmp_ctx);
3028 ret = fstat(fd, &st);
3030 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3032 talloc_free(tmp_ctx);
3036 if (!S_ISREG(st.st_mode)) {
3037 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3039 talloc_free(tmp_ctx);
3043 data.dsize = st.st_size;
3044 if (data.dsize == 0) {
3047 data.dptr = talloc_size(tmp_ctx, data.dsize);
3048 if (data.dptr == NULL) {
3049 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3051 talloc_free(tmp_ctx);
3054 ret = read(fd, data.dptr, data.dsize);
3055 if (ret != data.dsize) {
3056 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3058 talloc_free(tmp_ctx);
3067 ctdb_db = ctdb_attach(ctdb, db_name, true, 0);
3069 if (ctdb_db == NULL) {
3070 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3071 talloc_free(tmp_ctx);
3075 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3077 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3078 talloc_free(tmp_ctx);
3082 key.dptr = discard_const(argv[1]);
3083 key.dsize = strlen(argv[1]);
3084 ret = ctdb_transaction_store(h, key, data);
3086 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3087 talloc_free(tmp_ctx);
3091 ret = ctdb_transaction_commit(h);
3093 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3094 talloc_free(tmp_ctx);
3099 talloc_free(tmp_ctx);
3103 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
3104 TDB_DATA data, void *private_data)
3106 DEBUG(DEBUG_ERR,("Log data received\n"));
3107 if (data.dsize > 0) {
3108 printf("%s", data.dptr);
3115 display a list of log messages from the in memory ringbuffer
3117 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3121 struct ctdb_get_log_addr log_addr;
3123 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3128 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3129 talloc_free(tmp_ctx);
3133 log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3134 log_addr.srvid = getpid();
3135 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3136 log_addr.level = get_debug_by_desc(argv[0]);
3138 log_addr.level = strtol(argv[0], NULL, 0);
3142 data.dptr = (unsigned char *)&log_addr;
3143 data.dsize = sizeof(log_addr);
3145 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3147 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3150 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3152 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3153 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3154 if (ret != 0 || res != 0) {
3155 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3156 talloc_free(tmp_ctx);
3161 tv = timeval_current();
3162 /* this loop will terminate when we have received the reply */
3163 while (timeval_elapsed(&tv) < 3.0) {
3164 event_loop_once(ctdb->ev);
3167 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3169 talloc_free(tmp_ctx);
3174 clear the in memory log area
3176 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3181 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3183 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3184 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3185 if (ret != 0 || res != 0) {
3186 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3187 talloc_free(tmp_ctx);
3191 talloc_free(tmp_ctx);
3198 display a list of the databases on a remote ctdb
3200 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3203 struct ctdb_dbid_map *dbmap=NULL;
3205 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3207 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3211 if(options.machinereadable){
3212 printf(":ID:Name:Path:Persistent:Unhealthy:\n");
3213 for(i=0;i<dbmap->num;i++){
3219 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3220 dbmap->dbs[i].dbid, ctdb, &path);
3221 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3222 dbmap->dbs[i].dbid, ctdb, &name);
3223 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3224 dbmap->dbs[i].dbid, ctdb, &health);
3225 persistent = dbmap->dbs[i].persistent;
3226 printf(":0x%08X:%s:%s:%d:%d:\n",
3227 dbmap->dbs[i].dbid, name, path,
3228 !!(persistent), !!(health));
3233 printf("Number of databases:%d\n", dbmap->num);
3234 for(i=0;i<dbmap->num;i++){
3240 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3241 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3242 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3243 persistent = dbmap->dbs[i].persistent;
3244 printf("dbid:0x%08x name:%s path:%s%s%s\n",
3245 dbmap->dbs[i].dbid, name, path,
3246 persistent?" PERSISTENT":"",
3247 health?" UNHEALTHY":"");
3254 display the status of a database on a remote ctdb
3256 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3259 struct ctdb_dbid_map *dbmap=NULL;
3260 const char *db_name;
3268 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3270 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3274 for(i=0;i<dbmap->num;i++){
3280 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3281 if (strcmp(name, db_name) != 0) {
3285 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3286 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3287 persistent = dbmap->dbs[i].persistent;
3288 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nHEALTH: %s\n",
3289 dbmap->dbs[i].dbid, name, path,
3290 persistent?"yes":"no",
3291 health?health:"OK");
3295 DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
3300 check if the local node is recmaster or not
3301 it will return 1 if this node is the recmaster and 0 if it is not
3302 or if the local ctdb daemon could not be contacted
3304 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3306 uint32_t mypnn, recmaster;
3309 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
3311 printf("Failed to get pnn of node\n");
3315 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
3317 printf("Failed to get the recmaster\n");
3321 if (recmaster != mypnn) {
3322 printf("this node is not the recmaster\n");
3326 printf("this node is the recmaster\n");
3333 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
3336 struct timeval tv = timeval_current();
3337 ret = ctdb_ctrl_ping(ctdb, options.pnn);
3339 printf("Unable to get ping response from node %u\n", options.pnn);
3342 printf("response from %u time=%.6f sec (%d clients)\n",
3343 options.pnn, timeval_elapsed(&tv), ret);
3352 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
3363 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
3365 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
3369 printf("%-19s = %u\n", name, value);
3376 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
3387 value = strtoul(argv[1], NULL, 0);
3389 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
3391 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
3400 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
3406 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
3408 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
3412 for (i=0;i<count;i++) {
3413 control_getvar(ctdb, 1, &list[i]);
3422 display debug level on a node
3424 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3429 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
3431 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
3434 if (options.machinereadable){
3435 printf(":Name:Level:\n");
3436 printf(":%s:%d:\n",get_debug_by_level(level),level);
3438 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
3445 display reclock file of a node
3447 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3450 const char *reclock;
3452 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
3454 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3457 if (options.machinereadable){
3458 if (reclock != NULL) {
3459 printf("%s", reclock);
3462 if (reclock == NULL) {
3463 printf("No reclock file used.\n");
3465 printf("Reclock file:%s\n", reclock);
3473 set the reclock file of a node
3475 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3478 const char *reclock;
3482 } else if (argc == 1) {
3488 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
3490 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3497 set the natgw state on/off
3499 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
3502 uint32_t natgwstate;
3508 if (!strcmp(argv[0], "on")) {
3510 } else if (!strcmp(argv[0], "off")) {
3516 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
3518 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
3526 set the lmaster role on/off
3528 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3531 uint32_t lmasterrole;
3537 if (!strcmp(argv[0], "on")) {
3539 } else if (!strcmp(argv[0], "off")) {
3545 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
3547 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
3555 set the recmaster role on/off
3557 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3560 uint32_t recmasterrole;
3566 if (!strcmp(argv[0], "on")) {
3568 } else if (!strcmp(argv[0], "off")) {
3574 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
3576 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
3584 set debug level on a node or all nodes
3586 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3592 printf("You must specify the debug level. Valid levels are:\n");
3593 for (i=0; debug_levels[i].description != NULL; i++) {
3594 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3600 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3601 level = get_debug_by_desc(argv[0]);
3603 level = strtol(argv[0], NULL, 0);
3606 for (i=0; debug_levels[i].description != NULL; i++) {
3607 if (level == debug_levels[i].level) {
3611 if (debug_levels[i].description == NULL) {
3612 printf("Invalid debug level, must be one of\n");
3613 for (i=0; debug_levels[i].description != NULL; i++) {
3614 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3619 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
3621 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
3630 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
3636 priority = strtol(argv[0], NULL, 0);
3640 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
3642 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
3644 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
3651 attach to a database
3653 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
3655 const char *db_name;
3656 struct ctdb_db_context *ctdb_db;
3663 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3664 if (ctdb_db == NULL) {
3665 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3675 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
3677 struct ctdb_db_priority db_prio;
3684 db_prio.db_id = strtoul(argv[0], NULL, 0);
3685 db_prio.priority = strtoul(argv[1], NULL, 0);
3687 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
3689 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
3699 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
3701 uint32_t db_id, priority;
3708 db_id = strtoul(argv[0], NULL, 0);
3710 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
3712 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
3716 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
3722 run an eventscript on a node
3724 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
3730 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3733 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3737 data.dptr = (unsigned char *)discard_const(argv[0]);
3738 data.dsize = strlen((char *)data.dptr) + 1;
3740 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
3742 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
3743 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3744 if (ret != 0 || res != 0) {
3745 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
3746 talloc_free(tmp_ctx);
3749 talloc_free(tmp_ctx);
3753 #define DB_VERSION 1
3754 #define MAX_DB_NAME 64
3755 struct db_file_header {
3756 unsigned long version;
3758 unsigned long persistent;
3760 const char name[MAX_DB_NAME];
3763 struct backup_data {
3764 struct ctdb_marshall_buffer *records;
3767 bool traverse_error;
3770 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
3772 struct backup_data *bd = talloc_get_type(private, struct backup_data);
3773 struct ctdb_rec_data *rec;
3775 /* add the record */
3776 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
3778 bd->traverse_error = true;
3779 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
3782 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
3783 if (bd->records == NULL) {
3784 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
3785 bd->traverse_error = true;
3788 bd->records->count++;
3789 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
3790 bd->len += rec->length;
3798 * backup a database to a file
3800 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
3803 struct ctdb_dbid_map *dbmap=NULL;
3804 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3805 struct db_file_header dbhdr;
3806 struct ctdb_db_context *ctdb_db;
3807 struct backup_data *bd;
3810 const char *reason = NULL;
3813 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3817 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
3819 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3823 for(i=0;i<dbmap->num;i++){
3826 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
3827 if(!strcmp(argv[0], name)){
3828 talloc_free(discard_const(name));
3831 talloc_free(discard_const(name));
3833 if (i == dbmap->num) {
3834 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
3835 talloc_free(tmp_ctx);
3839 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3840 dbmap->dbs[i].dbid, tmp_ctx, &reason);
3842 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
3844 talloc_free(tmp_ctx);
3848 uint32_t allow_unhealthy = 0;
3850 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
3851 "AllowUnhealthyDBRead",
3854 if (allow_unhealthy != 1) {
3855 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
3858 DEBUG(DEBUG_ERR,("disallow backup : tunnable AllowUnhealthyDBRead = %u\n",
3860 talloc_free(tmp_ctx);
3864 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
3866 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
3867 "tunnable AllowUnhealthyDBRead = %u\n",
3871 ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
3872 if (ctdb_db == NULL) {
3873 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
3874 talloc_free(tmp_ctx);
3879 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
3881 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
3882 talloc_free(tmp_ctx);
3887 bd = talloc_zero(tmp_ctx, struct backup_data);
3889 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
3890 talloc_free(tmp_ctx);
3894 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
3895 if (bd->records == NULL) {
3896 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
3897 talloc_free(tmp_ctx);
3901 bd->len = offsetof(struct ctdb_marshall_buffer, data);
3902 bd->records->db_id = ctdb_db->db_id;
3903 /* traverse the database collecting all records */
3904 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
3905 bd->traverse_error) {
3906 DEBUG(DEBUG_ERR,("Traverse error\n"));
3907 talloc_free(tmp_ctx);
3911 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
3914 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
3916 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
3917 talloc_free(tmp_ctx);
3921 dbhdr.version = DB_VERSION;
3922 dbhdr.timestamp = time(NULL);
3923 dbhdr.persistent = dbmap->dbs[i].persistent;
3924 dbhdr.size = bd->len;
3925 if (strlen(argv[0]) >= MAX_DB_NAME) {
3926 DEBUG(DEBUG_ERR,("Too long dbname\n"));
3929 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
3930 ret = write(fh, &dbhdr, sizeof(dbhdr));
3932 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
3935 ret = write(fh, bd->records, bd->len);
3937 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
3946 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
3949 talloc_free(tmp_ctx);
3954 * restore a database from a file
3956 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
3959 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3962 struct db_file_header dbhdr;
3963 struct ctdb_db_context *ctdb_db;
3964 struct ctdb_node_map *nodemap=NULL;
3965 struct ctdb_vnn_map *vnnmap=NULL;
3967 struct ctdb_control_wipe_database w;
3969 uint32_t generation;
3974 if (argc < 1 || argc > 2) {
3975 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3979 fh = open(argv[0], O_RDONLY);
3981 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
3982 talloc_free(tmp_ctx);
3986 read(fh, &dbhdr, sizeof(dbhdr));
3987 if (dbhdr.version != DB_VERSION) {
3988 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
3989 talloc_free(tmp_ctx);
3993 dbname = discard_const(dbhdr.name);
3995 dbname = discard_const(argv[1]);
3998 outdata.dsize = dbhdr.size;
3999 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4000 if (outdata.dptr == NULL) {
4001 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4003 talloc_free(tmp_ctx);
4006 read(fh, outdata.dptr, outdata.dsize);
4009 tm = localtime(&dbhdr.timestamp);
4010 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4011 printf("Restoring database '%s' from backup @ %s\n",
4015 ctdb_db = ctdb_attach(ctdb, dbname, dbhdr.persistent, 0);
4016 if (ctdb_db == NULL) {
4017 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4018 talloc_free(tmp_ctx);
4022 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4024 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4025 talloc_free(tmp_ctx);
4030 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4032 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4033 talloc_free(tmp_ctx);
4037 /* freeze all nodes */
4038 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4039 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4040 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4046 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4047 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4048 talloc_free(tmp_ctx);
4053 generation = vnnmap->generation;
4054 data.dptr = (void *)&generation;
4055 data.dsize = sizeof(generation);
4057 /* start a cluster wide transaction */
4058 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4059 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4061 TIMELIMIT(), false, data,
4064 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4069 w.db_id = ctdb_db->db_id;
4070 w.transaction_id = generation;
4072 data.dptr = (void *)&w;
4073 data.dsize = sizeof(w);
4075 /* wipe all the remote databases. */
4076 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4077 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4079 TIMELIMIT(), false, data,
4082 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4083 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4084 talloc_free(tmp_ctx);
4088 /* push the database */
4089 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4090 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4092 TIMELIMIT(), false, outdata,
4095 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4096 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4097 talloc_free(tmp_ctx);
4101 data.dptr = (void *)&ctdb_db->db_id;
4102 data.dsize = sizeof(ctdb_db->db_id);
4104 /* mark the database as healthy */
4105 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4106 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4108 TIMELIMIT(), false, data,
4111 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4112 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4113 talloc_free(tmp_ctx);
4117 data.dptr = (void *)&generation;
4118 data.dsize = sizeof(generation);
4120 /* commit all the changes */
4121 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4123 TIMELIMIT(), false, data,
4126 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4127 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4128 talloc_free(tmp_ctx);
4133 /* thaw all nodes */
4134 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4135 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4141 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4142 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4143 talloc_free(tmp_ctx);
4148 talloc_free(tmp_ctx);
4153 * dump a database backup from a file
4155 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4157 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4159 struct db_file_header dbhdr;
4163 struct ctdb_rec_data *rec = NULL;
4164 struct ctdb_marshall_buffer *m;
4167 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4171 fh = open(argv[0], O_RDONLY);
4173 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4174 talloc_free(tmp_ctx);
4178 read(fh, &dbhdr, sizeof(dbhdr));
4179 if (dbhdr.version != DB_VERSION) {
4180 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4181 talloc_free(tmp_ctx);
4185 outdata.dsize = dbhdr.size;
4186 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4187 if (outdata.dptr == NULL) {
4188 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4190 talloc_free(tmp_ctx);
4193 read(fh, outdata.dptr, outdata.dsize);
4195 m = (struct ctdb_marshall_buffer *)outdata.dptr;
4197 tm = localtime(&dbhdr.timestamp);
4198 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4199 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
4200 dbhdr.name, m->db_id, tbuf);
4202 for (i=0; i < m->count; i++) {
4206 /* we do not want the header splitted, so we pass NULL*/
4207 rec = ctdb_marshall_loop_next(m, rec, &reqid,
4210 ctdb_dumpdb_record(ctdb, key, data, stdout);
4213 printf("Dumped %d records\n", i);
4214 talloc_free(tmp_ctx);
4219 * wipe a database from a file
4221 static int control_wipedb(struct ctdb_context *ctdb, int argc,
4225 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4227 struct ctdb_db_context *ctdb_db;
4228 struct ctdb_node_map *nodemap = NULL;
4229 struct ctdb_vnn_map *vnnmap = NULL;
4231 struct ctdb_control_wipe_database w;
4233 uint32_t generation;
4234 struct ctdb_dbid_map *dbmap = NULL;
4237 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4241 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4244 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
4249 for(i=0;i<dbmap->num;i++){
4252 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4253 dbmap->dbs[i].dbid, tmp_ctx, &name);
4254 if(!strcmp(argv[0], name)){
4255 talloc_free(discard_const(name));
4258 talloc_free(discard_const(name));
4260 if (i == dbmap->num) {
4261 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
4263 talloc_free(tmp_ctx);
4267 ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
4268 if (ctdb_db == NULL) {
4269 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
4271 talloc_free(tmp_ctx);
4275 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
4278 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4280 talloc_free(tmp_ctx);
4284 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4287 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
4289 talloc_free(tmp_ctx);
4293 /* freeze all nodes */
4294 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4295 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4296 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4303 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4304 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
4305 CTDB_RECOVERY_ACTIVE);
4306 talloc_free(tmp_ctx);
4311 generation = vnnmap->generation;
4312 data.dptr = (void *)&generation;
4313 data.dsize = sizeof(generation);
4315 /* start a cluster wide transaction */
4316 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4317 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4319 TIMELIMIT(), false, data,
4323 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
4324 "transactions.\n"));
4328 w.db_id = ctdb_db->db_id;
4329 w.transaction_id = generation;
4331 data.dptr = (void *)&w;
4332 data.dsize = sizeof(w);
4334 /* wipe all the remote databases. */
4335 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4336 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4338 TIMELIMIT(), false, data,
4341 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4342 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4343 talloc_free(tmp_ctx);
4347 data.dptr = (void *)&ctdb_db->db_id;
4348 data.dsize = sizeof(ctdb_db->db_id);
4350 /* mark the database as healthy */
4351 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4352 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4354 TIMELIMIT(), false, data,
4357 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4358 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4359 talloc_free(tmp_ctx);
4363 data.dptr = (void *)&generation;
4364 data.dsize = sizeof(generation);
4366 /* commit all the changes */
4367 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4369 TIMELIMIT(), false, data,
4372 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4373 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4374 talloc_free(tmp_ctx);
4378 /* thaw all nodes */
4379 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4380 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4386 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4387 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4388 talloc_free(tmp_ctx);
4392 talloc_free(tmp_ctx);
4397 * set flags of a node in the nodemap
4399 static int control_setflags(struct ctdb_context *ctdb, int argc, const char **argv)
4406 struct ctdb_node_flag_change c;
4413 if (sscanf(argv[0], "%d", &node) != 1) {
4414 DEBUG(DEBUG_ERR, ("Badly formed node\n"));
4418 if (sscanf(argv[1], "0x%x", &flags) != 1) {
4419 DEBUG(DEBUG_ERR, ("Badly formed flags\n"));
4426 c.new_flags = flags;
4428 data.dsize = sizeof(c);
4429 data.dptr = (unsigned char *)&c;
4431 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_MODIFY_FLAGS, 0,
4432 data, NULL, NULL, &status, NULL, NULL);
4433 if (ret != 0 || status != 0) {
4434 DEBUG(DEBUG_ERR,("Failed to modify flags\n"));
4443 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4449 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4450 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
4451 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
4452 if (ret != 0 || res != 0) {
4453 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
4454 talloc_free(tmp_ctx);
4457 write(1, data.dptr, data.dsize);
4458 talloc_free(tmp_ctx);
4463 handler for memory dumps
4465 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
4466 TDB_DATA data, void *private_data)
4468 write(1, data.dptr, data.dsize);
4473 dump memory usage on the recovery daemon
4475 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4479 struct rd_memdump_reply rd;
4481 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4483 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4486 rd.srvid = getpid();
4488 /* register a message port for receiveing the reply so that we
4489 can receive the reply
4491 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
4494 data.dptr = (uint8_t *)&rd;
4495 data.dsize = sizeof(rd);
4497 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
4499 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4503 /* this loop will terminate when we have received the reply */
4505 event_loop_once(ctdb->ev);
4512 send a message to a srvid
4514 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
4516 unsigned long srvid;
4524 srvid = strtoul(argv[0], NULL, 0);
4526 data.dptr = (uint8_t *)discard_const(argv[1]);
4527 data.dsize= strlen(argv[1]);
4529 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
4531 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4539 handler for msglisten
4541 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
4542 TDB_DATA data, void *private_data)
4546 printf("Message received: ");
4547 for (i=0;i<data.dsize;i++) {
4548 printf("%c", data.dptr[i]);
4554 listen for messages on a messageport
4556 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
4562 /* register a message port and listen for messages
4564 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
4565 printf("Listening for messages on srvid:%d\n", (int)srvid);
4568 event_loop_once(ctdb->ev);
4575 list all nodes in the cluster
4576 we parse the nodes file directly
4578 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
4580 TALLOC_CTX *mem_ctx = talloc_new(NULL);
4581 struct pnn_node *pnn_nodes;
4582 struct pnn_node *pnn_node;
4584 pnn_nodes = read_nodes_file(mem_ctx);
4585 if (pnn_nodes == NULL) {
4586 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
4587 talloc_free(mem_ctx);
4591 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
4592 ctdb_sock_addr addr;
4593 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
4594 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
4595 talloc_free(mem_ctx);
4598 if (options.machinereadable){
4599 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
4601 printf("%s\n", pnn_node->addr);
4604 talloc_free(mem_ctx);
4610 reload the nodes file on the local node
4612 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
4616 struct ctdb_node_map *nodemap=NULL;
4618 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4620 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
4624 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
4626 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
4630 /* reload the nodes file on all remote nodes */
4631 for (i=0;i<nodemap->num;i++) {
4632 if (nodemap->nodes[i].pnn == mypnn) {
4635 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
4636 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
4637 nodemap->nodes[i].pnn);
4639 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
4643 /* reload the nodes file on the local node */
4644 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
4645 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
4647 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
4650 /* initiate a recovery */
4651 control_recover(ctdb, argc, argv);
4657 static const struct {
4659 int (*fn)(struct ctdb_context *, int, const char **);
4661 bool without_daemon; /* can be run without daemon running ? */
4664 } ctdb_commands[] = {
4666 { "version", control_version, true, false, "show version of ctdb" },
4668 { "status", control_status, true, false, "show node status" },
4669 { "uptime", control_uptime, true, false, "show node uptime" },
4670 { "ping", control_ping, true, false, "ping all nodes" },
4671 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
4672 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
4673 { "listvars", control_listvars, true, false, "list tunable variables"},
4674 { "statistics", control_statistics, false, false, "show statistics" },
4675 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
4676 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
4677 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
4678 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
4679 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
4680 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
4681 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
4682 { "getdbmap", control_getdbmap, true, false, "show the database map" },
4683 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
4684 { "catdb", control_catdb, true, false, "dump a database" , "<dbname>"},
4685 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
4686 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
4687 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
4688 { "lvs", control_lvs, true, false, "show lvs configuration" },
4689 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
4690 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
4691 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
4692 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
4693 { "getdebug", control_getdebug, true, false, "get debug level" },
4694 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "<level>" },
4695 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer" },
4696 { "attach", control_attach, true, false, "attach to a database", "<dbname>" },
4697 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
4698 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
4699 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
4700 { "disable", control_disable, true, false, "disable a nodes public IP" },
4701 { "enable", control_enable, true, false, "enable a nodes public IP" },
4702 { "stop", control_stop, true, false, "stop a node" },
4703 { "continue", control_continue, true, false, "re-start a stopped node" },
4704 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
4705 { "unban", control_unban, true, false, "unban a node" },
4706 { "showban", control_showban, true, false, "show ban information"},
4707 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
4708 { "recover", control_recover, true, false, "force recovery" },
4709 { "sync", control_ipreallocate, true, false, "wait until ctdbd has synced all state changes" },
4710 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
4711 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
4712 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
4713 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
4714 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
4715 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
4716 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
4717 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
4719 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
4721 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
4722 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
4723 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
4724 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
4725 { "vacuum", ctdb_vacuum, false, false, "vacuum the databases of empty records", "[max_records]"},
4726 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
4727 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
4728 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
4729 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
4730 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
4731 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
4732 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
4733 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
4734 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
4735 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
4736 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
4737 { "recmaster", control_recmaster, false, false, "show the pnn for the recovery master."},
4738 { "setflags", control_setflags, false, false, "set flags for a node in the nodemap.", "<node> <flags>"},
4739 { "scriptstatus", control_scriptstatus, false, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
4740 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
4741 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
4742 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
4743 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
4744 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
4745 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
4746 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
4747 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
4748 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
4749 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbid> <prio:1-3>"},
4750 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbid>"},
4751 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
4752 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
4753 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
4754 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<db> <key> [<file>]" },
4755 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<db> <key> <file containing record>" },
4756 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file", "<tdb-file> <key> [<file>]" },
4762 static void usage(void)
4766 "Usage: ctdb [options] <control>\n" \
4768 " -n <node> choose node number, or 'all' (defaults to local node)\n"
4769 " -Y generate machinereadable output\n"
4770 " -v generate verbose output\n"
4771 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
4772 printf("Controls:\n");
4773 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
4774 printf(" %-15s %-27s %s\n",
4775 ctdb_commands[i].name,
4776 ctdb_commands[i].args?ctdb_commands[i].args:"",
4777 ctdb_commands[i].msg);
4783 static void ctdb_alarm(int sig)
4785 printf("Maximum runtime exceeded - exiting\n");
4792 int main(int argc, const char *argv[])
4794 struct ctdb_context *ctdb;
4795 char *nodestring = NULL;
4796 struct poptOption popt_options[] = {
4799 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
4800 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
4801 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
4802 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
4803 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
4807 const char **extra_argv;
4811 struct event_context *ev;
4812 const char *control;
4816 /* set some defaults */
4817 options.maxruntime = 0;
4818 options.timelimit = 3;
4819 options.pnn = CTDB_CURRENT_NODE;
4821 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
4823 while ((opt = poptGetNextOpt(pc)) != -1) {
4826 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
4827 poptBadOption(pc, 0), poptStrerror(opt)));
4832 /* setup the remaining options for the main program to use */
4833 extra_argv = poptGetArgs(pc);
4836 while (extra_argv[extra_argc]) extra_argc++;
4839 if (extra_argc < 1) {
4843 if (options.maxruntime == 0) {
4844 const char *ctdb_timeout;
4845 ctdb_timeout = getenv("CTDB_TIMEOUT");
4846 if (ctdb_timeout != NULL) {
4847 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
4849 /* default timeout is 120 seconds */
4850 options.maxruntime = 120;
4854 signal(SIGALRM, ctdb_alarm);
4855 alarm(options.maxruntime);
4857 /* setup the node number to contact */
4858 if (nodestring != NULL) {
4859 if (strcmp(nodestring, "all") == 0) {
4860 options.pnn = CTDB_BROADCAST_ALL;
4862 options.pnn = strtoul(nodestring, NULL, 0);
4866 control = extra_argv[0];
4868 ev = event_context_init(NULL);
4870 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
4873 tevent_loop_allow_nesting(ev);
4875 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
4876 if (strcmp(control, ctdb_commands[i].name) == 0) {
4879 if (ctdb_commands[i].without_daemon == true) {
4883 if (ctdb_commands[i].without_daemon == false) {
4884 const char *socket_name;
4886 /* initialise ctdb */
4887 ctdb = ctdb_cmdline_client(ev);
4890 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
4894 /* initialize a libctdb connection as well */
4895 socket_name = ctdb_get_socketname(ctdb);
4896 ctdb_connection = ctdb_connect(socket_name,
4897 ctdb_log_file, stderr);
4898 if (ctdb_connection == NULL) {
4899 fprintf(stderr, "Failed to connect to daemon from libctdb\n");
4903 /* verify the node exists */
4906 if (options.pnn == CTDB_CURRENT_NODE) {
4908 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
4916 if (ctdb_commands[i].auto_all &&
4917 options.pnn == CTDB_BROADCAST_ALL) {
4922 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
4923 CTDB_NO_MEMORY(ctdb, nodes);
4925 for (j=0;j<num_nodes;j++) {
4926 options.pnn = nodes[j];
4927 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
4931 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
4937 if (i == ARRAY_SIZE(ctdb_commands)) {
4938 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));