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, bool *persistent)
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)) {
126 *persistent = dbmap->dbs[i].persistent;
136 see if a process exists
138 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
146 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
147 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
151 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
153 printf("%u:%u exists\n", pnn, pid);
155 printf("%u:%u does not exist\n", pnn, pid);
161 display statistics structure
163 static void show_statistics(struct ctdb_statistics *s, int show_header)
165 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
167 const char *prefix=NULL;
169 int tmp, days, hours, minutes, seconds;
174 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
175 STATISTICS_FIELD(num_clients),
176 STATISTICS_FIELD(frozen),
177 STATISTICS_FIELD(recovering),
178 STATISTICS_FIELD(num_recoveries),
179 STATISTICS_FIELD(client_packets_sent),
180 STATISTICS_FIELD(client_packets_recv),
181 STATISTICS_FIELD(node_packets_sent),
182 STATISTICS_FIELD(node_packets_recv),
183 STATISTICS_FIELD(keepalive_packets_sent),
184 STATISTICS_FIELD(keepalive_packets_recv),
185 STATISTICS_FIELD(node.req_call),
186 STATISTICS_FIELD(node.reply_call),
187 STATISTICS_FIELD(node.req_dmaster),
188 STATISTICS_FIELD(node.reply_dmaster),
189 STATISTICS_FIELD(node.reply_error),
190 STATISTICS_FIELD(node.req_message),
191 STATISTICS_FIELD(node.req_control),
192 STATISTICS_FIELD(node.reply_control),
193 STATISTICS_FIELD(client.req_call),
194 STATISTICS_FIELD(client.req_message),
195 STATISTICS_FIELD(client.req_control),
196 STATISTICS_FIELD(timeouts.call),
197 STATISTICS_FIELD(timeouts.control),
198 STATISTICS_FIELD(timeouts.traverse),
199 STATISTICS_FIELD(total_calls),
200 STATISTICS_FIELD(pending_calls),
201 STATISTICS_FIELD(lockwait_calls),
202 STATISTICS_FIELD(pending_lockwait_calls),
203 STATISTICS_FIELD(childwrite_calls),
204 STATISTICS_FIELD(pending_childwrite_calls),
205 STATISTICS_FIELD(memory_used),
206 STATISTICS_FIELD(max_hop_count),
208 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
217 if (options.machinereadable){
219 printf("CTDB version:");
220 printf("Current time of statistics:");
221 printf("Statistics collected since:");
222 for (i=0;i<ARRAY_SIZE(fields);i++) {
223 printf("%s:", fields[i].name);
225 printf("max_reclock_ctdbd:");
226 printf("max_reclock_recd:");
227 printf("max_call_latency:");
228 printf("max_lockwait_latency:");
229 printf("max_childwrite_latency:");
230 printf("max_childwrite_latency:");
233 printf("%d:", CTDB_VERSION);
234 printf("%d:", (int)s->statistics_current_time.tv_sec);
235 printf("%d:", (int)s->statistics_start_time.tv_sec);
236 for (i=0;i<ARRAY_SIZE(fields);i++) {
237 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
239 printf("%.6f:", s->reclock.ctdbd);
240 printf("%.6f:", s->reclock.recd);
241 printf("%.6f:", s->max_call_latency);
242 printf("%.6f:", s->max_lockwait_latency);
243 printf("%.6f:", s->max_childwrite_latency);
244 printf("%.6f:", s->max_childwrite_latency);
247 printf("CTDB version %u\n", CTDB_VERSION);
248 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
249 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
251 for (i=0;i<ARRAY_SIZE(fields);i++) {
252 if (strchr(fields[i].name, '.')) {
253 preflen = strcspn(fields[i].name, ".")+1;
254 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
255 prefix = fields[i].name;
256 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
261 printf(" %*s%-22s%*s%10u\n",
263 fields[i].name+preflen,
265 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
267 printf(" %-30s %.6f sec\n", "max_reclock_ctdbd", s->reclock.ctdbd);
268 printf(" %-30s %.6f sec\n", "max_reclock_recd", s->reclock.recd);
270 printf(" %-30s %.6f sec\n", "max_call_latency", s->max_call_latency);
271 printf(" %-30s %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
272 printf(" %-30s %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
273 printf(" %-30s %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
276 talloc_free(tmp_ctx);
280 display remote ctdb statistics combined from all nodes
282 static int control_statistics_all(struct ctdb_context *ctdb)
285 struct ctdb_statistics statistics;
289 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
290 CTDB_NO_MEMORY(ctdb, nodes);
292 ZERO_STRUCT(statistics);
294 for (i=0;i<num_nodes;i++) {
295 struct ctdb_statistics s1;
297 uint32_t *v1 = (uint32_t *)&s1;
298 uint32_t *v2 = (uint32_t *)&statistics;
300 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
301 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
303 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
306 for (j=0;j<num_ints;j++) {
309 statistics.max_hop_count =
310 MAX(statistics.max_hop_count, s1.max_hop_count);
311 statistics.max_call_latency =
312 MAX(statistics.max_call_latency, s1.max_call_latency);
313 statistics.max_lockwait_latency =
314 MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
317 printf("Gathered statistics for %u nodes\n", num_nodes);
318 show_statistics(&statistics, 1);
323 display remote ctdb statistics
325 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
328 struct ctdb_statistics statistics;
330 if (options.pnn == CTDB_BROADCAST_ALL) {
331 return control_statistics_all(ctdb);
334 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
336 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
339 show_statistics(&statistics, 1);
345 reset remote ctdb statistics
347 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
351 ret = ctdb_statistics_reset(ctdb, options.pnn);
353 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
361 display remote ctdb rolling statistics
363 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
366 struct ctdb_statistics_wire *stats;
367 int i, num_records = -1;
370 num_records = atoi(argv[0]) - 1;
373 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
375 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
378 for (i=0;i<stats->num;i++) {
379 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
382 show_statistics(&stats->stats[i], i==0);
383 if (i == num_records) {
392 display uptime of remote node
394 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
397 struct ctdb_uptime *uptime = NULL;
398 int tmp, days, hours, minutes, seconds;
400 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
402 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
406 if (options.machinereadable){
407 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
408 printf(":%u:%u:%u:%lf\n",
409 (unsigned int)uptime->current_time.tv_sec,
410 (unsigned int)uptime->ctdbd_start_time.tv_sec,
411 (unsigned int)uptime->last_recovery_finished.tv_sec,
412 timeval_delta(&uptime->last_recovery_finished,
413 &uptime->last_recovery_started)
418 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
420 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
428 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
430 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
438 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
440 printf("Duration of last recovery/failover: %lf seconds\n",
441 timeval_delta(&uptime->last_recovery_finished,
442 &uptime->last_recovery_started));
448 show the PNN of the current node
450 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
455 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
457 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
461 printf("PNN:%d\n", mypnn);
467 struct pnn_node *next;
472 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
474 const char *nodes_list;
478 struct pnn_node *pnn_nodes = NULL;
479 struct pnn_node *pnn_node;
480 struct pnn_node *tmp_node;
482 /* read the nodes file */
483 nodes_list = getenv("CTDB_NODES");
484 if (nodes_list == NULL) {
485 nodes_list = "/etc/ctdb/nodes";
487 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
491 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
494 for (i=0, pnn=0; i<nlines; i++) {
498 /* strip leading spaces */
499 while((*node == ' ') || (*node == '\t')) {
506 if (strcmp(node, "") == 0) {
509 pnn_node = talloc(mem_ctx, struct pnn_node);
510 pnn_node->pnn = pnn++;
511 pnn_node->addr = talloc_strdup(pnn_node, node);
512 pnn_node->next = pnn_nodes;
513 pnn_nodes = pnn_node;
516 /* swap them around so we return them in incrementing order */
517 pnn_node = pnn_nodes;
521 pnn_node = pnn_node->next;
523 tmp_node->next = pnn_nodes;
524 pnn_nodes = tmp_node;
531 show the PNN of the current node
532 discover the pnn by loading the nodes file and try to bind to all
533 addresses one at a time until the ip address is found.
535 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
537 TALLOC_CTX *mem_ctx = talloc_new(NULL);
538 struct pnn_node *pnn_nodes;
539 struct pnn_node *pnn_node;
541 pnn_nodes = read_nodes_file(mem_ctx);
542 if (pnn_nodes == NULL) {
543 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
544 talloc_free(mem_ctx);
548 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
551 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
552 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
553 talloc_free(mem_ctx);
557 if (ctdb_sys_have_ip(&addr)) {
558 printf("PNN:%d\n", pnn_node->pnn);
559 talloc_free(mem_ctx);
564 printf("Failed to detect which PNN this node is\n");
565 talloc_free(mem_ctx);
570 display remote ctdb status
572 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
575 struct ctdb_vnn_map *vnnmap=NULL;
576 struct ctdb_node_map *nodemap=NULL;
577 uint32_t recmode, recmaster;
580 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
585 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
587 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
591 if(options.machinereadable){
592 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped:Inactive:PartiallyOnline:\n");
593 for(i=0;i<nodemap->num;i++){
594 int partially_online = 0;
597 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
600 if (nodemap->nodes[i].flags == 0) {
601 struct ctdb_control_get_ifaces *ifaces;
603 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(),
604 nodemap->nodes[i].pnn,
607 for (j=0; j < ifaces->num; j++) {
608 if (ifaces->ifaces[j].link_state != 0) {
611 partially_online = 1;
617 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
618 ctdb_addr_to_str(&nodemap->nodes[i].addr),
619 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
620 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
621 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
622 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
623 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED),
624 !!(nodemap->nodes[i].flags&NODE_FLAGS_INACTIVE),
630 printf("Number of nodes:%d\n", nodemap->num);
631 for(i=0;i<nodemap->num;i++){
632 static const struct {
636 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
637 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
638 { NODE_FLAGS_BANNED, "BANNED" },
639 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
640 { NODE_FLAGS_DELETED, "DELETED" },
641 { NODE_FLAGS_STOPPED, "STOPPED" },
642 { NODE_FLAGS_INACTIVE, "INACTIVE" },
644 char *flags_str = NULL;
647 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
650 if (nodemap->nodes[i].flags == 0) {
651 struct ctdb_control_get_ifaces *ifaces;
653 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(),
654 nodemap->nodes[i].pnn,
657 for (j=0; j < ifaces->num; j++) {
658 if (ifaces->ifaces[j].link_state != 0) {
661 flags_str = talloc_strdup(ctdb, "PARTIALLYONLINE");
667 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
668 if (nodemap->nodes[i].flags & flag_names[j].flag) {
669 if (flags_str == NULL) {
670 flags_str = talloc_strdup(ctdb, flag_names[j].name);
672 flags_str = talloc_asprintf_append(flags_str, "|%s",
675 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
678 if (flags_str == NULL) {
679 flags_str = talloc_strdup(ctdb, "OK");
680 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
682 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
683 ctdb_addr_to_str(&nodemap->nodes[i].addr),
685 nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
686 talloc_free(flags_str);
689 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
691 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
694 if (vnnmap->generation == INVALID_GENERATION) {
695 printf("Generation:INVALID\n");
697 printf("Generation:%d\n",vnnmap->generation);
699 printf("Size:%d\n",vnnmap->size);
700 for(i=0;i<vnnmap->size;i++){
701 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
704 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
706 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
709 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
711 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
713 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
716 printf("Recovery master:%d\n",recmaster);
723 struct natgw_node *next;
728 display the list of nodes belonging to this natgw configuration
730 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
733 uint32_t capabilities;
734 const char *natgw_list;
737 struct natgw_node *natgw_nodes = NULL;
738 struct natgw_node *natgw_node;
739 struct ctdb_node_map *nodemap=NULL;
742 /* read the natgw nodes file into a linked list */
743 natgw_list = getenv("NATGW_NODES");
744 if (natgw_list == NULL) {
745 natgw_list = "/etc/ctdb/natgw_nodes";
747 lines = file_lines_load(natgw_list, &nlines, ctdb);
749 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
752 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
755 for (i=0;i<nlines;i++) {
759 /* strip leading spaces */
760 while((*node == ' ') || (*node == '\t')) {
766 if (strcmp(node, "") == 0) {
769 natgw_node = talloc(ctdb, struct natgw_node);
770 natgw_node->addr = talloc_strdup(natgw_node, node);
771 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
772 natgw_node->next = natgw_nodes;
773 natgw_nodes = natgw_node;
776 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
778 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
783 while(i<nodemap->num) {
784 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
785 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
790 /* this node was not in the natgw so we just remove it from
793 if ((natgw_node == NULL)
794 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
797 for (j=i+1; j<nodemap->num; j++) {
798 nodemap->nodes[j-1] = nodemap->nodes[j];
807 /* pick a node to be natgwmaster
808 * we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
810 for(i=0;i<nodemap->num;i++){
811 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
812 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
814 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
817 if (!(capabilities&CTDB_CAP_NATGW)) {
820 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
824 /* we couldnt find any healthy node, try unhealthy ones */
825 if (i == nodemap->num) {
826 for(i=0;i<nodemap->num;i++){
827 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
828 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
830 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
833 if (!(capabilities&CTDB_CAP_NATGW)) {
836 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
841 /* unless all nodes are STOPPED, when we pick one anyway */
842 if (i == nodemap->num) {
843 for(i=0;i<nodemap->num;i++){
844 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
845 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
847 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
850 if (!(capabilities&CTDB_CAP_NATGW)) {
853 printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
857 /* or if we still can not find any */
858 if (i == nodemap->num) {
859 printf("-1 0.0.0.0\n");
863 /* print the pruned list of nodes belonging to this natgw list */
864 for(i=0;i<nodemap->num;i++){
865 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
868 printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
869 ctdb_addr_to_str(&nodemap->nodes[i].addr),
870 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
871 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
872 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
873 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
874 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
881 display the status of the scripts for monitoring (or other events)
883 static int control_one_scriptstatus(struct ctdb_context *ctdb,
884 enum ctdb_eventscript_call type)
886 struct ctdb_scripts_wire *script_status;
889 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
891 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
895 if (script_status == NULL) {
896 if (!options.machinereadable) {
897 printf("%s cycle never run\n",
898 ctdb_eventscript_call_names[type]);
903 if (!options.machinereadable) {
904 printf("%d scripts were executed last %s cycle\n",
905 script_status->num_scripts,
906 ctdb_eventscript_call_names[type]);
908 for (i=0; i<script_status->num_scripts; i++) {
909 const char *status = NULL;
911 switch (script_status->scripts[i].status) {
922 if (script_status->scripts[i].status > 0)
926 if (options.machinereadable) {
927 printf("%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
928 ctdb_eventscript_call_names[type],
929 script_status->scripts[i].name,
930 script_status->scripts[i].status,
932 (long)script_status->scripts[i].start.tv_sec,
933 (long)script_status->scripts[i].start.tv_usec,
934 (long)script_status->scripts[i].finished.tv_sec,
935 (long)script_status->scripts[i].finished.tv_usec,
936 script_status->scripts[i].output);
940 printf("%-20s Status:%s ",
941 script_status->scripts[i].name, status);
943 /* Some other error, eg from stat. */
944 printf("%-20s Status:CANNOT RUN (%s)",
945 script_status->scripts[i].name,
946 strerror(-script_status->scripts[i].status));
948 if (script_status->scripts[i].status >= 0) {
949 printf("Duration:%.3lf ",
950 timeval_delta(&script_status->scripts[i].finished,
951 &script_status->scripts[i].start));
953 if (script_status->scripts[i].status != -ENOEXEC) {
955 ctime(&script_status->scripts[i].start.tv_sec));
956 if (script_status->scripts[i].status != 0) {
957 printf(" OUTPUT:%s\n",
958 script_status->scripts[i].output);
968 static int control_scriptstatus(struct ctdb_context *ctdb,
969 int argc, const char **argv)
972 enum ctdb_eventscript_call type, min, max;
976 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
981 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
985 for (type = 0; type < CTDB_EVENT_MAX; type++) {
986 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
992 if (type == CTDB_EVENT_MAX) {
993 if (strcmp(arg, "all") == 0) {
995 max = CTDB_EVENT_MAX;
997 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1002 if (options.machinereadable) {
1003 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1006 for (type = min; type < max; type++) {
1007 ret = control_one_scriptstatus(ctdb, type);
1017 enable an eventscript
1019 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1027 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1029 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1037 disable an eventscript
1039 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1047 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1049 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1057 display the pnn of the recovery master
1059 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1064 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1066 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1069 printf("%d\n",recmaster);
1075 add a tickle to a public address
1077 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1079 struct ctdb_tcp_connection t;
1087 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1088 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1091 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1092 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1096 data.dptr = (uint8_t *)&t;
1097 data.dsize = sizeof(t);
1099 /* tell all nodes about this tcp connection */
1100 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1101 0, data, ctdb, NULL, NULL, NULL, NULL);
1103 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1112 delete a tickle from a node
1114 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1116 struct ctdb_tcp_connection t;
1124 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1125 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1128 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1129 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1133 data.dptr = (uint8_t *)&t;
1134 data.dsize = sizeof(t);
1136 /* tell all nodes about this tcp connection */
1137 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1138 0, data, ctdb, NULL, NULL, NULL, NULL);
1140 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1149 get a list of all tickles for this pnn
1151 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1153 struct ctdb_control_tcp_tickle_list *list;
1154 ctdb_sock_addr addr;
1163 port = atoi(argv[1]);
1166 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1167 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1171 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1173 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1177 if (options.machinereadable){
1178 printf(":source ip:port:destination ip:port:\n");
1179 for (i=0;i<list->tickles.num;i++) {
1180 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1183 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1184 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1187 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1188 printf("Num tickles:%u\n", list->tickles.num);
1189 for (i=0;i<list->tickles.num;i++) {
1190 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1193 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1194 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));
1204 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1206 struct ctdb_all_public_ips *ips;
1207 struct ctdb_public_ip ip;
1210 uint32_t disable_time;
1212 struct ctdb_node_map *nodemap=NULL;
1213 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1216 data.dptr = (uint8_t*)&disable_time;
1217 data.dsize = sizeof(disable_time);
1218 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1220 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1226 /* read the public ip list from the node */
1227 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1229 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1230 talloc_free(tmp_ctx);
1234 for (i=0;i<ips->num;i++) {
1235 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1240 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1241 pnn, ctdb_addr_to_str(addr)));
1242 talloc_free(tmp_ctx);
1249 data.dptr = (uint8_t *)&ip;
1250 data.dsize = sizeof(ip);
1252 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1254 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1255 talloc_free(tmp_ctx);
1259 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1260 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1267 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1268 talloc_free(tmp_ctx);
1272 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1274 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1275 talloc_free(tmp_ctx);
1279 /* update the recovery daemon so it now knows to expect the new
1280 node assignment for this ip.
1282 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1284 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1288 talloc_free(tmp_ctx);
1293 move/failover an ip address to a specific node
1295 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1298 ctdb_sock_addr addr;
1305 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1306 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1311 if (sscanf(argv[1], "%u", &pnn) != 1) {
1312 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1316 if (move_ip(ctdb, &addr, pnn) != 0) {
1317 DEBUG(DEBUG_ERR,("Failed to move ip to node %d\n", pnn));
1324 void getips_store_callback(void *param, void *data)
1326 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1327 struct ctdb_all_public_ips *ips = param;
1331 ips->ips[i].pnn = node_ip->pnn;
1332 ips->ips[i].addr = node_ip->addr;
1335 void getips_count_callback(void *param, void *data)
1337 uint32_t *count = param;
1343 static uint32_t *ip_key(ctdb_sock_addr *ip)
1345 static uint32_t key[IP_KEYLEN];
1347 bzero(key, sizeof(key));
1349 switch (ip->sa.sa_family) {
1351 key[0] = ip->ip.sin_addr.s_addr;
1354 key[0] = ip->ip6.sin6_addr.s6_addr32[3];
1355 key[1] = ip->ip6.sin6_addr.s6_addr32[2];
1356 key[2] = ip->ip6.sin6_addr.s6_addr32[1];
1357 key[3] = ip->ip6.sin6_addr.s6_addr32[0];
1360 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1367 static void *add_ip_callback(void *parm, void *data)
1373 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1375 struct ctdb_all_public_ips *tmp_ips;
1376 struct ctdb_node_map *nodemap=NULL;
1377 trbt_tree_t *ip_tree;
1381 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1383 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1387 ip_tree = trbt_create(tmp_ctx, 0);
1389 for(i=0;i<nodemap->num;i++){
1390 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1393 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1397 /* read the public ip list from this node */
1398 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1400 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1404 for (j=0; j<tmp_ips->num;j++) {
1405 struct ctdb_public_ip *node_ip;
1407 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1408 node_ip->pnn = tmp_ips->ips[j].pnn;
1409 node_ip->addr = tmp_ips->ips[j].addr;
1411 trbt_insertarray32_callback(ip_tree,
1412 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1416 talloc_free(tmp_ips);
1421 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1423 len = offsetof(struct ctdb_all_public_ips, ips) +
1424 count*sizeof(struct ctdb_public_ip);
1425 tmp_ips = talloc_zero_size(tmp_ctx, len);
1426 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1435 * scans all other nodes and returns a pnn for another node that can host this
1439 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1441 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1442 struct ctdb_all_public_ips *ips;
1443 struct ctdb_node_map *nodemap=NULL;
1446 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1448 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1449 talloc_free(tmp_ctx);
1453 for(i=0;i<nodemap->num;i++){
1454 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1457 if (nodemap->nodes[i].pnn == options.pnn) {
1461 /* read the public ip list from this node */
1462 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1464 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1468 for (j=0;j<ips->num;j++) {
1469 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1470 talloc_free(tmp_ctx);
1471 return nodemap->nodes[i].pnn;
1477 talloc_free(tmp_ctx);
1482 add a public ip address to a node
1484 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
1490 ctdb_sock_addr addr;
1491 struct ctdb_control_ip_iface *pub;
1492 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1493 struct ctdb_all_public_ips *ips;
1497 talloc_free(tmp_ctx);
1501 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1502 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1503 talloc_free(tmp_ctx);
1507 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1509 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1510 talloc_free(tmp_ctx);
1515 /* check if some other node is already serving this ip, if not,
1518 for (i=0;i<ips->num;i++) {
1519 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1524 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1525 pub = talloc_size(tmp_ctx, len);
1526 CTDB_NO_MEMORY(ctdb, pub);
1530 pub->len = strlen(argv[1])+1;
1531 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1533 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1535 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
1536 talloc_free(tmp_ctx);
1540 if (i == ips->num) {
1541 /* no one has this ip so we claim it */
1544 pnn = ips->ips[i].pnn;
1547 if (move_ip(ctdb, &addr, pnn) != 0) {
1548 DEBUG(DEBUG_ERR,("Failed to move ip to node %d\n", pnn));
1552 talloc_free(tmp_ctx);
1556 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
1558 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
1560 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1561 struct ctdb_node_map *nodemap=NULL;
1562 struct ctdb_all_public_ips *ips;
1565 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1567 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
1571 /* remove it from the nodes that are not hosting the ip currently */
1572 for(i=0;i<nodemap->num;i++){
1573 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1576 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1577 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1581 for (j=0;j<ips->num;j++) {
1582 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1590 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
1594 options.pnn = nodemap->nodes[i].pnn;
1595 control_delip(ctdb, argc, argv);
1599 /* remove it from every node (also the one hosting it) */
1600 for(i=0;i<nodemap->num;i++){
1601 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1604 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1605 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1609 for (j=0;j<ips->num;j++) {
1610 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1618 options.pnn = nodemap->nodes[i].pnn;
1619 control_delip(ctdb, argc, argv);
1622 talloc_free(tmp_ctx);
1627 delete a public ip address from a node
1629 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
1632 ctdb_sock_addr addr;
1633 struct ctdb_control_ip_iface pub;
1634 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1635 struct ctdb_all_public_ips *ips;
1638 talloc_free(tmp_ctx);
1642 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1643 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1647 if (options.pnn == CTDB_BROADCAST_ALL) {
1648 return control_delip_all(ctdb, argc, argv, &addr);
1655 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1657 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1658 talloc_free(tmp_ctx);
1662 for (i=0;i<ips->num;i++) {
1663 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1669 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
1670 ctdb_addr_to_str(&addr)));
1671 talloc_free(tmp_ctx);
1675 if (ips->ips[i].pnn == options.pnn) {
1676 ret = find_other_host_for_public_ip(ctdb, &addr);
1678 if (move_ip(ctdb, &addr, ret) != 0) {
1679 DEBUG(DEBUG_ERR,("Failed to move ip to node %d\n", ret));
1685 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
1687 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
1688 talloc_free(tmp_ctx);
1692 talloc_free(tmp_ctx);
1697 kill a tcp connection
1699 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1702 struct ctdb_control_killtcp killtcp;
1708 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
1709 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1713 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
1714 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1718 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
1720 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1731 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1734 ctdb_sock_addr addr;
1740 if (!parse_ip(argv[0], NULL, 0, &addr)) {
1741 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1745 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1747 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1755 register a server id
1757 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1760 struct ctdb_server_id server_id;
1766 server_id.pnn = strtoul(argv[0], NULL, 0);
1767 server_id.type = strtoul(argv[1], NULL, 0);
1768 server_id.server_id = strtoul(argv[2], NULL, 0);
1770 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1772 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1775 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
1781 unregister a server id
1783 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1786 struct ctdb_server_id server_id;
1792 server_id.pnn = strtoul(argv[0], NULL, 0);
1793 server_id.type = strtoul(argv[1], NULL, 0);
1794 server_id.server_id = strtoul(argv[2], NULL, 0);
1796 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
1798 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
1805 check if a server id exists
1807 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1811 struct ctdb_server_id server_id;
1817 server_id.pnn = strtoul(argv[0], NULL, 0);
1818 server_id.type = strtoul(argv[1], NULL, 0);
1819 server_id.server_id = strtoul(argv[2], NULL, 0);
1821 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
1823 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
1828 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
1830 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
1836 get a list of all server ids that are registered on a node
1838 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
1841 struct ctdb_server_id_list *server_ids;
1843 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
1845 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
1849 for (i=0; i<server_ids->num; i++) {
1850 printf("Server id %d:%d:%d\n",
1851 server_ids->server_ids[i].pnn,
1852 server_ids->server_ids[i].type,
1853 server_ids->server_ids[i].server_id);
1860 send a tcp tickle ack
1862 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1865 ctdb_sock_addr src, dst;
1871 if (!parse_ip_port(argv[0], &src)) {
1872 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1876 if (!parse_ip_port(argv[1], &dst)) {
1877 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1881 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
1885 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
1892 display public ip status
1894 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
1897 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1898 struct ctdb_all_public_ips *ips;
1900 if (options.pnn == CTDB_BROADCAST_ALL) {
1901 /* read the list of public ips from all nodes */
1902 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1904 /* read the public ip list from this node */
1905 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1908 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
1909 talloc_free(tmp_ctx);
1913 if (options.machinereadable){
1914 printf(":Public IP:Node:");
1915 if (options.verbose){
1916 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
1920 if (options.pnn == CTDB_BROADCAST_ALL) {
1921 printf("Public IPs on ALL nodes\n");
1923 printf("Public IPs on node %u\n", options.pnn);
1927 for (i=1;i<=ips->num;i++) {
1928 struct ctdb_control_public_ip_info *info = NULL;
1930 char *aciface = NULL;
1931 char *avifaces = NULL;
1932 char *cifaces = NULL;
1934 if (options.pnn == CTDB_BROADCAST_ALL) {
1935 pnn = ips->ips[ips->num-i].pnn;
1941 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
1942 &ips->ips[ips->num-i].addr, &info);
1949 for (j=0; j < info->num; j++) {
1950 if (cifaces == NULL) {
1951 cifaces = talloc_strdup(info,
1952 info->ifaces[j].name);
1954 cifaces = talloc_asprintf_append(cifaces,
1956 info->ifaces[j].name);
1959 if (info->active_idx == j) {
1960 aciface = info->ifaces[j].name;
1963 if (info->ifaces[j].link_state == 0) {
1967 if (avifaces == NULL) {
1968 avifaces = talloc_strdup(info, info->ifaces[j].name);
1970 avifaces = talloc_asprintf_append(avifaces,
1972 info->ifaces[j].name);
1977 if (options.machinereadable){
1979 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
1980 ips->ips[ips->num-i].pnn);
1981 if (options.verbose){
1984 avifaces?avifaces:"",
1985 cifaces?cifaces:"");
1989 if (options.verbose) {
1990 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
1991 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
1992 ips->ips[ips->num-i].pnn,
1994 avifaces?avifaces:"",
1995 cifaces?cifaces:"");
1998 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
1999 ips->ips[ips->num-i].pnn);
2005 talloc_free(tmp_ctx);
2012 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2015 ctdb_sock_addr addr;
2016 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2017 struct ctdb_control_public_ip_info *info;
2020 talloc_free(tmp_ctx);
2024 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2025 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2029 /* read the public ip info from this node */
2030 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2031 tmp_ctx, &addr, &info);
2033 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2034 argv[0], options.pnn));
2035 talloc_free(tmp_ctx);
2039 printf("Public IP[%s] info on node %u\n",
2040 ctdb_addr_to_str(&info->ip.addr),
2043 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2044 ctdb_addr_to_str(&info->ip.addr),
2045 info->ip.pnn, info->num);
2047 for (i=0; i<info->num; i++) {
2048 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2050 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2051 i+1, info->ifaces[i].name,
2052 info->ifaces[i].link_state?"up":"down",
2053 (unsigned int)info->ifaces[i].references,
2054 (i==info->active_idx)?" (active)":"");
2057 talloc_free(tmp_ctx);
2062 display interfaces status
2064 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2067 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2068 struct ctdb_control_get_ifaces *ifaces;
2070 /* read the public ip list from this node */
2071 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn,
2074 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2076 talloc_free(tmp_ctx);
2080 if (options.machinereadable){
2081 printf(":Name:LinkStatus:References:\n");
2083 printf("Interfaces on node %u\n", options.pnn);
2086 for (i=0; i<ifaces->num; i++) {
2087 if (options.machinereadable){
2088 printf(":%s:%s:%u\n",
2089 ifaces->ifaces[i].name,
2090 ifaces->ifaces[i].link_state?"1":"0",
2091 (unsigned int)ifaces->ifaces[i].references);
2093 printf("name:%s link:%s references:%u\n",
2094 ifaces->ifaces[i].name,
2095 ifaces->ifaces[i].link_state?"up":"down",
2096 (unsigned int)ifaces->ifaces[i].references);
2100 talloc_free(tmp_ctx);
2106 set link status of an interface
2108 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2111 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2112 struct ctdb_control_iface_info info;
2120 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2121 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2123 talloc_free(tmp_ctx);
2126 strcpy(info.name, argv[0]);
2128 if (strcmp(argv[1], "up") == 0) {
2129 info.link_state = 1;
2130 } else if (strcmp(argv[1], "down") == 0) {
2131 info.link_state = 0;
2133 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2135 talloc_free(tmp_ctx);
2139 /* read the public ip list from this node */
2140 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2143 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2144 argv[0], options.pnn));
2145 talloc_free(tmp_ctx);
2149 talloc_free(tmp_ctx);
2154 display pid of a ctdb daemon
2156 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2161 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2163 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2166 printf("Pid:%d\n", pid);
2171 static uint32_t ipreallocate_finished;
2174 handler for receiving the response to ipreallocate
2176 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
2177 TDB_DATA data, void *private_data)
2179 ipreallocate_finished = 1;
2182 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2184 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2186 event_add_timed(ctdb->ev, ctdb,
2187 timeval_current_ofs(1, 0),
2188 ctdb_every_second, ctdb);
2192 ask the recovery daemon on the recovery master to perform a ip reallocation
2194 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2198 struct takeover_run_reply rd;
2200 struct ctdb_node_map *nodemap=NULL;
2202 struct timeval tv = timeval_current();
2204 /* we need some events to trigger so we can timeout and restart
2207 event_add_timed(ctdb->ev, ctdb,
2208 timeval_current_ofs(1, 0),
2209 ctdb_every_second, ctdb);
2211 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2213 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
2216 rd.srvid = getpid();
2218 /* register a message port for receiveing the reply so that we
2219 can receive the reply
2221 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
2223 data.dptr = (uint8_t *)&rd;
2224 data.dsize = sizeof(rd);
2227 /* check that there are valid nodes available */
2228 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
2229 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2232 for (i=0; i<nodemap->num;i++) {
2233 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
2237 if (i==nodemap->num) {
2238 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
2243 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
2245 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2249 /* verify the node exists */
2250 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
2251 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2256 /* check tha there are nodes available that can act as a recmaster */
2257 for (i=0; i<nodemap->num; i++) {
2258 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
2263 if (i == nodemap->num) {
2264 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
2268 /* verify the recovery master is not STOPPED, nor BANNED */
2269 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
2270 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
2277 /* verify the recovery master is not STOPPED, nor BANNED */
2278 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
2279 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
2285 ipreallocate_finished = 0;
2286 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
2288 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
2292 tv = timeval_current();
2293 /* this loop will terminate when we have received the reply */
2294 while (timeval_elapsed(&tv) < 3.0) {
2295 event_loop_once(ctdb->ev);
2297 if (ipreallocate_finished == 1) {
2301 DEBUG(DEBUG_ERR,("Timed out waiting for recmaster ipreallocate. Trying again\n"));
2311 disable a remote node
2313 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2316 struct ctdb_node_map *nodemap=NULL;
2318 /* check if the node is already disabled */
2319 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2320 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2323 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2324 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2329 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2331 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2337 /* read the nodemap and verify the change took effect */
2338 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2339 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2343 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2344 ret = control_ipreallocate(ctdb, argc, argv);
2346 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2354 enable a disabled remote node
2356 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2360 struct ctdb_node_map *nodemap=NULL;
2363 /* check if the node is already enabled */
2364 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2365 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2368 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2369 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2374 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2376 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2382 /* read the nodemap and verify the change took effect */
2383 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2384 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2388 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2390 ret = control_ipreallocate(ctdb, argc, argv);
2392 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2402 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2405 struct ctdb_node_map *nodemap=NULL;
2408 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2410 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2415 /* read the nodemap and verify the change took effect */
2416 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2417 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2421 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2422 ret = control_ipreallocate(ctdb, argc, argv);
2424 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2432 restart a stopped remote node
2434 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2438 struct ctdb_node_map *nodemap=NULL;
2441 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2443 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2449 /* read the nodemap and verify the change took effect */
2450 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2451 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2455 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2456 ret = control_ipreallocate(ctdb, argc, argv);
2458 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2465 static uint32_t get_generation(struct ctdb_context *ctdb)
2467 struct ctdb_vnn_map *vnnmap=NULL;
2470 /* wait until the recmaster is not in recovery mode */
2472 uint32_t recmode, recmaster;
2474 if (vnnmap != NULL) {
2475 talloc_free(vnnmap);
2479 /* get the recmaster */
2480 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
2482 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2486 /* get recovery mode */
2487 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
2489 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2493 /* get the current generation number */
2494 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2496 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2500 if ((recmode == CTDB_RECOVERY_NORMAL)
2501 && (vnnmap->generation != 1)){
2502 return vnnmap->generation;
2509 ban a node from the cluster
2511 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2514 struct ctdb_node_map *nodemap=NULL;
2515 struct ctdb_ban_time bantime;
2521 /* verify the node exists */
2522 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2524 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2528 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2529 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2533 bantime.pnn = options.pnn;
2534 bantime.time = strtoul(argv[0], NULL, 0);
2536 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2538 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2542 ret = control_ipreallocate(ctdb, argc, argv);
2544 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2553 unban a node from the cluster
2555 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2558 struct ctdb_node_map *nodemap=NULL;
2559 struct ctdb_ban_time bantime;
2561 /* verify the node exists */
2562 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2564 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2568 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2569 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2573 bantime.pnn = options.pnn;
2576 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2578 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2582 ret = control_ipreallocate(ctdb, argc, argv);
2584 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2593 show ban information for a node
2595 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2598 struct ctdb_node_map *nodemap=NULL;
2599 struct ctdb_ban_time *bantime;
2601 /* verify the node exists */
2602 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2604 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2608 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2610 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2614 if (bantime->time == 0) {
2615 printf("Node %u is not banned\n", bantime->pnn);
2617 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2626 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2630 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2632 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2642 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2645 uint32_t generation, next_generation;
2647 /* record the current generation number */
2648 generation = get_generation(ctdb);
2650 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2652 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
2656 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2658 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2662 /* wait until we are in a new generation */
2664 next_generation = get_generation(ctdb);
2665 if (next_generation != generation) {
2676 display monitoring mode of a remote node
2678 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2683 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2685 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2688 if (!options.machinereadable){
2689 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2692 printf(":%d:\n",monmode);
2699 display capabilities of a remote node
2701 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2703 uint32_t capabilities;
2706 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2708 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2712 if (!options.machinereadable){
2713 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2714 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2715 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
2716 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
2718 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
2719 printf(":%d:%d:%d:%d:\n",
2720 !!(capabilities&CTDB_CAP_RECMASTER),
2721 !!(capabilities&CTDB_CAP_LMASTER),
2722 !!(capabilities&CTDB_CAP_LVS),
2723 !!(capabilities&CTDB_CAP_NATGW));
2729 display lvs configuration
2731 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
2733 uint32_t *capabilities;
2734 struct ctdb_node_map *nodemap=NULL;
2736 int healthy_count = 0;
2738 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2740 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2744 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2745 CTDB_NO_MEMORY(ctdb, capabilities);
2747 /* collect capabilities for all connected nodes */
2748 for (i=0; i<nodemap->num; i++) {
2749 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2752 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2756 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2758 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2762 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2766 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2771 /* Print all LVS nodes */
2772 for (i=0; i<nodemap->num; i++) {
2773 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2776 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2779 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2783 if (healthy_count != 0) {
2784 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2789 printf("%d:%s\n", i,
2790 ctdb_addr_to_str(&nodemap->nodes[i].addr));
2797 display who is the lvs master
2799 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
2801 uint32_t *capabilities;
2802 struct ctdb_node_map *nodemap=NULL;
2804 int healthy_count = 0;
2806 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2808 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2812 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2813 CTDB_NO_MEMORY(ctdb, capabilities);
2815 /* collect capabilities for all connected nodes */
2816 for (i=0; i<nodemap->num; i++) {
2817 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2820 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2824 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2826 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2830 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2834 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2839 /* find and show the lvsmaster */
2840 for (i=0; i<nodemap->num; i++) {
2841 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2844 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2847 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2851 if (healthy_count != 0) {
2852 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2857 if (options.machinereadable){
2860 printf("Node %d is LVS master\n", i);
2865 printf("There is no LVS master\n");
2870 disable monitoring on a node
2872 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2877 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
2879 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
2882 printf("Monitoring mode:%s\n","DISABLED");
2888 enable monitoring on a node
2890 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2895 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
2897 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
2900 printf("Monitoring mode:%s\n","ACTIVE");
2906 display remote list of keys/data for a db
2908 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
2910 const char *db_name;
2911 struct ctdb_db_context *ctdb_db;
2922 if (db_exists(ctdb, db_name, &persistent)) {
2923 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
2927 ctdb_db = ctdb_attach(ctdb, db_name, persistent, 0);
2929 if (ctdb_db == NULL) {
2930 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2934 /* traverse and dump the cluster tdb */
2935 ret = ctdb_dump_db(ctdb_db, stdout);
2937 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
2938 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
2939 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
2943 talloc_free(ctdb_db);
2945 printf("Dumped %d records\n", ret);
2951 fetch a record from a persistent database
2953 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
2955 const char *db_name;
2956 struct ctdb_db_context *ctdb_db;
2957 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2958 struct ctdb_transaction_handle *h;
2964 talloc_free(tmp_ctx);
2971 if (db_exists(ctdb, db_name, &persistent)) {
2972 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
2973 talloc_free(tmp_ctx);
2978 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
2979 talloc_free(tmp_ctx);
2983 ctdb_db = ctdb_attach(ctdb, db_name, persistent, 0);
2985 if (ctdb_db == NULL) {
2986 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2987 talloc_free(tmp_ctx);
2991 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
2993 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
2994 talloc_free(tmp_ctx);
2998 key.dptr = discard_const(argv[1]);
2999 key.dsize = strlen(argv[1]);
3000 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3002 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3003 talloc_free(tmp_ctx);
3007 if (data.dsize == 0 || data.dptr == NULL) {
3008 DEBUG(DEBUG_ERR,("Record is empty\n"));
3009 talloc_free(tmp_ctx);
3014 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3016 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3017 talloc_free(tmp_ctx);
3020 write(fd, data.dptr, data.dsize);
3023 write(1, data.dptr, data.dsize);
3026 /* abort the transaction */
3030 talloc_free(tmp_ctx);
3035 fetch a record from a tdb-file
3037 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3039 const char *tdb_file;
3050 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3052 DEBUG(DEBUG_ERR,("Failed to open TDB file %s\n", tdb_file));
3056 key.dptr = discard_const(argv[1]);
3057 key.dsize = strlen(argv[1]);
3058 data = tdb_fetch(tdb, key);
3059 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3060 DEBUG(DEBUG_ERR,("Failed to read record %s from tdb %s\n", argv[1], tdb_file));
3068 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3070 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3073 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3076 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3083 write a record to a persistent database
3085 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3087 const char *db_name;
3088 struct ctdb_db_context *ctdb_db;
3089 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3090 struct ctdb_transaction_handle *h;
3096 talloc_free(tmp_ctx);
3100 fd = open(argv[2], O_RDONLY);
3102 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3103 talloc_free(tmp_ctx);
3107 ret = fstat(fd, &st);
3109 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3111 talloc_free(tmp_ctx);
3115 if (!S_ISREG(st.st_mode)) {
3116 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3118 talloc_free(tmp_ctx);
3122 data.dsize = st.st_size;
3123 if (data.dsize == 0) {
3126 data.dptr = talloc_size(tmp_ctx, data.dsize);
3127 if (data.dptr == NULL) {
3128 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3130 talloc_free(tmp_ctx);
3133 ret = read(fd, data.dptr, data.dsize);
3134 if (ret != data.dsize) {
3135 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3137 talloc_free(tmp_ctx);
3146 ctdb_db = ctdb_attach(ctdb, db_name, true, 0);
3148 if (ctdb_db == NULL) {
3149 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3150 talloc_free(tmp_ctx);
3154 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3156 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3157 talloc_free(tmp_ctx);
3161 key.dptr = discard_const(argv[1]);
3162 key.dsize = strlen(argv[1]);
3163 ret = ctdb_transaction_store(h, key, data);
3165 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3166 talloc_free(tmp_ctx);
3170 ret = ctdb_transaction_commit(h);
3172 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3173 talloc_free(tmp_ctx);
3178 talloc_free(tmp_ctx);
3182 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
3183 TDB_DATA data, void *private_data)
3185 DEBUG(DEBUG_ERR,("Log data received\n"));
3186 if (data.dsize > 0) {
3187 printf("%s", data.dptr);
3194 display a list of log messages from the in memory ringbuffer
3196 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3200 struct ctdb_get_log_addr log_addr;
3202 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3207 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3208 talloc_free(tmp_ctx);
3212 log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3213 log_addr.srvid = getpid();
3214 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3215 log_addr.level = get_debug_by_desc(argv[0]);
3217 log_addr.level = strtol(argv[0], NULL, 0);
3221 data.dptr = (unsigned char *)&log_addr;
3222 data.dsize = sizeof(log_addr);
3224 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3226 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3229 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3231 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3232 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3233 if (ret != 0 || res != 0) {
3234 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3235 talloc_free(tmp_ctx);
3240 tv = timeval_current();
3241 /* this loop will terminate when we have received the reply */
3242 while (timeval_elapsed(&tv) < 3.0) {
3243 event_loop_once(ctdb->ev);
3246 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3248 talloc_free(tmp_ctx);
3253 clear the in memory log area
3255 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3260 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3262 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3263 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3264 if (ret != 0 || res != 0) {
3265 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3266 talloc_free(tmp_ctx);
3270 talloc_free(tmp_ctx);
3277 display a list of the databases on a remote ctdb
3279 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3282 struct ctdb_dbid_map *dbmap=NULL;
3284 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3286 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3290 if(options.machinereadable){
3291 printf(":ID:Name:Path:Persistent:Unhealthy:\n");
3292 for(i=0;i<dbmap->num;i++){
3298 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3299 dbmap->dbs[i].dbid, ctdb, &path);
3300 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3301 dbmap->dbs[i].dbid, ctdb, &name);
3302 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3303 dbmap->dbs[i].dbid, ctdb, &health);
3304 persistent = dbmap->dbs[i].persistent;
3305 printf(":0x%08X:%s:%s:%d:%d:\n",
3306 dbmap->dbs[i].dbid, name, path,
3307 !!(persistent), !!(health));
3312 printf("Number of databases:%d\n", dbmap->num);
3313 for(i=0;i<dbmap->num;i++){
3319 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3320 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3321 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3322 persistent = dbmap->dbs[i].persistent;
3323 printf("dbid:0x%08x name:%s path:%s%s%s\n",
3324 dbmap->dbs[i].dbid, name, path,
3325 persistent?" PERSISTENT":"",
3326 health?" UNHEALTHY":"");
3333 display the status of a database on a remote ctdb
3335 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3338 struct ctdb_dbid_map *dbmap=NULL;
3339 const char *db_name;
3347 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3349 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3353 for(i=0;i<dbmap->num;i++){
3359 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3360 if (strcmp(name, db_name) != 0) {
3364 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3365 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3366 persistent = dbmap->dbs[i].persistent;
3367 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nHEALTH: %s\n",
3368 dbmap->dbs[i].dbid, name, path,
3369 persistent?"yes":"no",
3370 health?health:"OK");
3374 DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
3379 check if the local node is recmaster or not
3380 it will return 1 if this node is the recmaster and 0 if it is not
3381 or if the local ctdb daemon could not be contacted
3383 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3385 uint32_t mypnn, recmaster;
3388 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
3390 printf("Failed to get pnn of node\n");
3394 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
3396 printf("Failed to get the recmaster\n");
3400 if (recmaster != mypnn) {
3401 printf("this node is not the recmaster\n");
3405 printf("this node is the recmaster\n");
3412 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
3415 struct timeval tv = timeval_current();
3416 ret = ctdb_ctrl_ping(ctdb, options.pnn);
3418 printf("Unable to get ping response from node %u\n", options.pnn);
3421 printf("response from %u time=%.6f sec (%d clients)\n",
3422 options.pnn, timeval_elapsed(&tv), ret);
3431 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
3442 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
3444 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
3448 printf("%-19s = %u\n", name, value);
3455 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
3466 value = strtoul(argv[1], NULL, 0);
3468 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
3470 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
3479 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
3485 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
3487 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
3491 for (i=0;i<count;i++) {
3492 control_getvar(ctdb, 1, &list[i]);
3501 display debug level on a node
3503 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3508 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
3510 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
3513 if (options.machinereadable){
3514 printf(":Name:Level:\n");
3515 printf(":%s:%d:\n",get_debug_by_level(level),level);
3517 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
3524 display reclock file of a node
3526 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3529 const char *reclock;
3531 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
3533 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3536 if (options.machinereadable){
3537 if (reclock != NULL) {
3538 printf("%s", reclock);
3541 if (reclock == NULL) {
3542 printf("No reclock file used.\n");
3544 printf("Reclock file:%s\n", reclock);
3552 set the reclock file of a node
3554 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3557 const char *reclock;
3561 } else if (argc == 1) {
3567 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
3569 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3576 set the natgw state on/off
3578 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
3581 uint32_t natgwstate;
3587 if (!strcmp(argv[0], "on")) {
3589 } else if (!strcmp(argv[0], "off")) {
3595 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
3597 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
3605 set the lmaster role on/off
3607 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3610 uint32_t lmasterrole;
3616 if (!strcmp(argv[0], "on")) {
3618 } else if (!strcmp(argv[0], "off")) {
3624 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
3626 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
3634 set the recmaster role on/off
3636 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3639 uint32_t recmasterrole;
3645 if (!strcmp(argv[0], "on")) {
3647 } else if (!strcmp(argv[0], "off")) {
3653 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
3655 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
3663 set debug level on a node or all nodes
3665 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3671 printf("You must specify the debug level. Valid levels are:\n");
3672 for (i=0; debug_levels[i].description != NULL; i++) {
3673 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3679 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3680 level = get_debug_by_desc(argv[0]);
3682 level = strtol(argv[0], NULL, 0);
3685 for (i=0; debug_levels[i].description != NULL; i++) {
3686 if (level == debug_levels[i].level) {
3690 if (debug_levels[i].description == NULL) {
3691 printf("Invalid debug level, must be one of\n");
3692 for (i=0; debug_levels[i].description != NULL; i++) {
3693 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3698 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
3700 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
3709 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
3715 priority = strtol(argv[0], NULL, 0);
3719 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
3721 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
3723 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
3730 attach to a database
3732 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
3734 const char *db_name;
3735 struct ctdb_db_context *ctdb_db;
3736 bool persistent = false;
3746 if (strcmp(argv[1], "persistent") != 0) {
3752 ctdb_db = ctdb_attach(ctdb, db_name, persistent, 0);
3753 if (ctdb_db == NULL) {
3754 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3764 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
3766 struct ctdb_db_priority db_prio;
3773 db_prio.db_id = strtoul(argv[0], NULL, 0);
3774 db_prio.priority = strtoul(argv[1], NULL, 0);
3776 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
3778 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
3788 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
3790 uint32_t db_id, priority;
3797 db_id = strtoul(argv[0], NULL, 0);
3799 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
3801 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
3805 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
3811 run an eventscript on a node
3813 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
3819 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3822 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3826 data.dptr = (unsigned char *)discard_const(argv[0]);
3827 data.dsize = strlen((char *)data.dptr) + 1;
3829 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
3831 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
3832 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3833 if (ret != 0 || res != 0) {
3834 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
3835 talloc_free(tmp_ctx);
3838 talloc_free(tmp_ctx);
3842 #define DB_VERSION 1
3843 #define MAX_DB_NAME 64
3844 struct db_file_header {
3845 unsigned long version;
3847 unsigned long persistent;
3849 const char name[MAX_DB_NAME];
3852 struct backup_data {
3853 struct ctdb_marshall_buffer *records;
3856 bool traverse_error;
3859 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
3861 struct backup_data *bd = talloc_get_type(private, struct backup_data);
3862 struct ctdb_rec_data *rec;
3864 /* add the record */
3865 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
3867 bd->traverse_error = true;
3868 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
3871 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
3872 if (bd->records == NULL) {
3873 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
3874 bd->traverse_error = true;
3877 bd->records->count++;
3878 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
3879 bd->len += rec->length;
3887 * backup a database to a file
3889 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
3892 struct ctdb_dbid_map *dbmap=NULL;
3893 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3894 struct db_file_header dbhdr;
3895 struct ctdb_db_context *ctdb_db;
3896 struct backup_data *bd;
3899 const char *reason = NULL;
3902 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3906 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
3908 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3912 for(i=0;i<dbmap->num;i++){
3915 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
3916 if(!strcmp(argv[0], name)){
3917 talloc_free(discard_const(name));
3920 talloc_free(discard_const(name));
3922 if (i == dbmap->num) {
3923 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
3924 talloc_free(tmp_ctx);
3928 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3929 dbmap->dbs[i].dbid, tmp_ctx, &reason);
3931 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
3933 talloc_free(tmp_ctx);
3937 uint32_t allow_unhealthy = 0;
3939 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
3940 "AllowUnhealthyDBRead",
3943 if (allow_unhealthy != 1) {
3944 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
3947 DEBUG(DEBUG_ERR,("disallow backup : tunnable AllowUnhealthyDBRead = %u\n",
3949 talloc_free(tmp_ctx);
3953 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
3955 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
3956 "tunnable AllowUnhealthyDBRead = %u\n",
3960 ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
3961 if (ctdb_db == NULL) {
3962 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
3963 talloc_free(tmp_ctx);
3968 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
3970 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
3971 talloc_free(tmp_ctx);
3976 bd = talloc_zero(tmp_ctx, struct backup_data);
3978 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
3979 talloc_free(tmp_ctx);
3983 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
3984 if (bd->records == NULL) {
3985 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
3986 talloc_free(tmp_ctx);
3990 bd->len = offsetof(struct ctdb_marshall_buffer, data);
3991 bd->records->db_id = ctdb_db->db_id;
3992 /* traverse the database collecting all records */
3993 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
3994 bd->traverse_error) {
3995 DEBUG(DEBUG_ERR,("Traverse error\n"));
3996 talloc_free(tmp_ctx);
4000 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4003 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4005 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4006 talloc_free(tmp_ctx);
4010 dbhdr.version = DB_VERSION;
4011 dbhdr.timestamp = time(NULL);
4012 dbhdr.persistent = dbmap->dbs[i].persistent;
4013 dbhdr.size = bd->len;
4014 if (strlen(argv[0]) >= MAX_DB_NAME) {
4015 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4018 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
4019 ret = write(fh, &dbhdr, sizeof(dbhdr));
4021 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4024 ret = write(fh, bd->records, bd->len);
4026 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4035 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4038 talloc_free(tmp_ctx);
4043 * restore a database from a file
4045 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4048 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4051 struct db_file_header dbhdr;
4052 struct ctdb_db_context *ctdb_db;
4053 struct ctdb_node_map *nodemap=NULL;
4054 struct ctdb_vnn_map *vnnmap=NULL;
4056 struct ctdb_control_wipe_database w;
4058 uint32_t generation;
4063 if (argc < 1 || argc > 2) {
4064 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4068 fh = open(argv[0], O_RDONLY);
4070 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4071 talloc_free(tmp_ctx);
4075 read(fh, &dbhdr, sizeof(dbhdr));
4076 if (dbhdr.version != DB_VERSION) {
4077 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4078 talloc_free(tmp_ctx);
4082 dbname = discard_const(dbhdr.name);
4084 dbname = discard_const(argv[1]);
4087 outdata.dsize = dbhdr.size;
4088 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4089 if (outdata.dptr == NULL) {
4090 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4092 talloc_free(tmp_ctx);
4095 read(fh, outdata.dptr, outdata.dsize);
4098 tm = localtime(&dbhdr.timestamp);
4099 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4100 printf("Restoring database '%s' from backup @ %s\n",
4104 ctdb_db = ctdb_attach(ctdb, dbname, dbhdr.persistent, 0);
4105 if (ctdb_db == NULL) {
4106 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4107 talloc_free(tmp_ctx);
4111 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4113 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4114 talloc_free(tmp_ctx);
4119 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4121 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4122 talloc_free(tmp_ctx);
4126 /* freeze all nodes */
4127 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4128 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4129 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4135 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4136 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4137 talloc_free(tmp_ctx);
4142 generation = vnnmap->generation;
4143 data.dptr = (void *)&generation;
4144 data.dsize = sizeof(generation);
4146 /* start a cluster wide transaction */
4147 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4148 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4150 TIMELIMIT(), false, data,
4153 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4158 w.db_id = ctdb_db->db_id;
4159 w.transaction_id = generation;
4161 data.dptr = (void *)&w;
4162 data.dsize = sizeof(w);
4164 /* wipe all the remote databases. */
4165 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4166 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4168 TIMELIMIT(), false, data,
4171 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4172 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4173 talloc_free(tmp_ctx);
4177 /* push the database */
4178 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4179 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4181 TIMELIMIT(), false, outdata,
4184 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4185 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4186 talloc_free(tmp_ctx);
4190 data.dptr = (void *)&ctdb_db->db_id;
4191 data.dsize = sizeof(ctdb_db->db_id);
4193 /* mark the database as healthy */
4194 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4195 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4197 TIMELIMIT(), false, data,
4200 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4201 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4202 talloc_free(tmp_ctx);
4206 data.dptr = (void *)&generation;
4207 data.dsize = sizeof(generation);
4209 /* commit all the changes */
4210 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4212 TIMELIMIT(), false, data,
4215 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4216 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4217 talloc_free(tmp_ctx);
4222 /* thaw all nodes */
4223 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4224 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4230 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4231 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4232 talloc_free(tmp_ctx);
4237 talloc_free(tmp_ctx);
4242 * dump a database backup from a file
4244 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4246 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4248 struct db_file_header dbhdr;
4252 struct ctdb_rec_data *rec = NULL;
4253 struct ctdb_marshall_buffer *m;
4256 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4260 fh = open(argv[0], O_RDONLY);
4262 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4263 talloc_free(tmp_ctx);
4267 read(fh, &dbhdr, sizeof(dbhdr));
4268 if (dbhdr.version != DB_VERSION) {
4269 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4270 talloc_free(tmp_ctx);
4274 outdata.dsize = dbhdr.size;
4275 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4276 if (outdata.dptr == NULL) {
4277 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4279 talloc_free(tmp_ctx);
4282 read(fh, outdata.dptr, outdata.dsize);
4284 m = (struct ctdb_marshall_buffer *)outdata.dptr;
4286 tm = localtime(&dbhdr.timestamp);
4287 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4288 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
4289 dbhdr.name, m->db_id, tbuf);
4291 for (i=0; i < m->count; i++) {
4295 /* we do not want the header splitted, so we pass NULL*/
4296 rec = ctdb_marshall_loop_next(m, rec, &reqid,
4299 ctdb_dumpdb_record(ctdb, key, data, stdout);
4302 printf("Dumped %d records\n", i);
4303 talloc_free(tmp_ctx);
4308 * wipe a database from a file
4310 static int control_wipedb(struct ctdb_context *ctdb, int argc,
4314 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4316 struct ctdb_db_context *ctdb_db;
4317 struct ctdb_node_map *nodemap = NULL;
4318 struct ctdb_vnn_map *vnnmap = NULL;
4320 struct ctdb_control_wipe_database w;
4322 uint32_t generation;
4323 struct ctdb_dbid_map *dbmap = NULL;
4326 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4330 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4333 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
4338 for(i=0;i<dbmap->num;i++){
4341 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4342 dbmap->dbs[i].dbid, tmp_ctx, &name);
4343 if(!strcmp(argv[0], name)){
4344 talloc_free(discard_const(name));
4347 talloc_free(discard_const(name));
4349 if (i == dbmap->num) {
4350 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
4352 talloc_free(tmp_ctx);
4356 ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
4357 if (ctdb_db == NULL) {
4358 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
4360 talloc_free(tmp_ctx);
4364 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
4367 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4369 talloc_free(tmp_ctx);
4373 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4376 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
4378 talloc_free(tmp_ctx);
4382 /* freeze all nodes */
4383 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4384 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4385 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4392 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4393 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
4394 CTDB_RECOVERY_ACTIVE);
4395 talloc_free(tmp_ctx);
4400 generation = vnnmap->generation;
4401 data.dptr = (void *)&generation;
4402 data.dsize = sizeof(generation);
4404 /* start a cluster wide transaction */
4405 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4406 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4408 TIMELIMIT(), false, data,
4412 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
4413 "transactions.\n"));
4417 w.db_id = ctdb_db->db_id;
4418 w.transaction_id = generation;
4420 data.dptr = (void *)&w;
4421 data.dsize = sizeof(w);
4423 /* wipe all the remote databases. */
4424 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4425 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4427 TIMELIMIT(), false, data,
4430 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4431 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4432 talloc_free(tmp_ctx);
4436 data.dptr = (void *)&ctdb_db->db_id;
4437 data.dsize = sizeof(ctdb_db->db_id);
4439 /* mark the database as healthy */
4440 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4441 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4443 TIMELIMIT(), false, data,
4446 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4447 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4448 talloc_free(tmp_ctx);
4452 data.dptr = (void *)&generation;
4453 data.dsize = sizeof(generation);
4455 /* commit all the changes */
4456 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4458 TIMELIMIT(), false, data,
4461 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4462 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4463 talloc_free(tmp_ctx);
4467 /* thaw all nodes */
4468 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4469 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4475 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4476 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4477 talloc_free(tmp_ctx);
4481 talloc_free(tmp_ctx);
4488 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4494 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4495 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
4496 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
4497 if (ret != 0 || res != 0) {
4498 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
4499 talloc_free(tmp_ctx);
4502 write(1, data.dptr, data.dsize);
4503 talloc_free(tmp_ctx);
4508 handler for memory dumps
4510 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
4511 TDB_DATA data, void *private_data)
4513 write(1, data.dptr, data.dsize);
4518 dump memory usage on the recovery daemon
4520 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4524 struct rd_memdump_reply rd;
4526 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4528 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4531 rd.srvid = getpid();
4533 /* register a message port for receiveing the reply so that we
4534 can receive the reply
4536 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
4539 data.dptr = (uint8_t *)&rd;
4540 data.dsize = sizeof(rd);
4542 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
4544 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4548 /* this loop will terminate when we have received the reply */
4550 event_loop_once(ctdb->ev);
4557 send a message to a srvid
4559 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
4561 unsigned long srvid;
4569 srvid = strtoul(argv[0], NULL, 0);
4571 data.dptr = (uint8_t *)discard_const(argv[1]);
4572 data.dsize= strlen(argv[1]);
4574 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
4576 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4584 handler for msglisten
4586 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
4587 TDB_DATA data, void *private_data)
4591 printf("Message received: ");
4592 for (i=0;i<data.dsize;i++) {
4593 printf("%c", data.dptr[i]);
4599 listen for messages on a messageport
4601 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
4607 /* register a message port and listen for messages
4609 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
4610 printf("Listening for messages on srvid:%d\n", (int)srvid);
4613 event_loop_once(ctdb->ev);
4620 list all nodes in the cluster
4621 we parse the nodes file directly
4623 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
4625 TALLOC_CTX *mem_ctx = talloc_new(NULL);
4626 struct pnn_node *pnn_nodes;
4627 struct pnn_node *pnn_node;
4629 pnn_nodes = read_nodes_file(mem_ctx);
4630 if (pnn_nodes == NULL) {
4631 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
4632 talloc_free(mem_ctx);
4636 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
4637 ctdb_sock_addr addr;
4638 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
4639 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
4640 talloc_free(mem_ctx);
4643 if (options.machinereadable){
4644 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
4646 printf("%s\n", pnn_node->addr);
4649 talloc_free(mem_ctx);
4655 reload the nodes file on the local node
4657 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
4661 struct ctdb_node_map *nodemap=NULL;
4663 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4665 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
4669 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
4671 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
4675 /* reload the nodes file on all remote nodes */
4676 for (i=0;i<nodemap->num;i++) {
4677 if (nodemap->nodes[i].pnn == mypnn) {
4680 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
4681 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
4682 nodemap->nodes[i].pnn);
4684 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
4688 /* reload the nodes file on the local node */
4689 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
4690 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
4692 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
4695 /* initiate a recovery */
4696 control_recover(ctdb, argc, argv);
4702 static const struct {
4704 int (*fn)(struct ctdb_context *, int, const char **);
4706 bool without_daemon; /* can be run without daemon running ? */
4709 } ctdb_commands[] = {
4711 { "version", control_version, true, false, "show version of ctdb" },
4713 { "status", control_status, true, false, "show node status" },
4714 { "uptime", control_uptime, true, false, "show node uptime" },
4715 { "ping", control_ping, true, false, "ping all nodes" },
4716 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
4717 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
4718 { "listvars", control_listvars, true, false, "list tunable variables"},
4719 { "statistics", control_statistics, false, false, "show statistics" },
4720 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
4721 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
4722 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
4723 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
4724 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
4725 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
4726 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
4727 { "getdbmap", control_getdbmap, true, false, "show the database map" },
4728 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
4729 { "catdb", control_catdb, true, false, "dump a database" , "<dbname>"},
4730 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
4731 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
4732 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
4733 { "lvs", control_lvs, true, false, "show lvs configuration" },
4734 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
4735 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
4736 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
4737 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
4738 { "getdebug", control_getdebug, true, false, "get debug level" },
4739 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "<level>" },
4740 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer" },
4741 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
4742 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
4743 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
4744 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
4745 { "disable", control_disable, true, false, "disable a nodes public IP" },
4746 { "enable", control_enable, true, false, "enable a nodes public IP" },
4747 { "stop", control_stop, true, false, "stop a node" },
4748 { "continue", control_continue, true, false, "re-start a stopped node" },
4749 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
4750 { "unban", control_unban, true, false, "unban a node" },
4751 { "showban", control_showban, true, false, "show ban information"},
4752 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
4753 { "recover", control_recover, true, false, "force recovery" },
4754 { "sync", control_ipreallocate, true, false, "wait until ctdbd has synced all state changes" },
4755 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
4756 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
4757 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
4758 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
4759 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
4760 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
4761 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
4762 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
4764 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
4766 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
4767 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
4768 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
4769 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
4770 { "vacuum", ctdb_vacuum, false, false, "vacuum the databases of empty records", "[max_records]"},
4771 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
4772 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
4773 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
4774 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
4775 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
4776 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
4777 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
4778 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
4779 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
4780 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
4781 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
4782 { "recmaster", control_recmaster, false, false, "show the pnn for the recovery master."},
4783 { "scriptstatus", control_scriptstatus, false, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
4784 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
4785 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
4786 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
4787 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
4788 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
4789 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
4790 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
4791 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
4792 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
4793 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbid> <prio:1-3>"},
4794 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbid>"},
4795 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
4796 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
4797 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
4798 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<db> <key> [<file>]" },
4799 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<db> <key> <file containing record>" },
4800 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file", "<tdb-file> <key> [<file>]" },
4806 static void usage(void)
4810 "Usage: ctdb [options] <control>\n" \
4812 " -n <node> choose node number, or 'all' (defaults to local node)\n"
4813 " -Y generate machinereadable output\n"
4814 " -v generate verbose output\n"
4815 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
4816 printf("Controls:\n");
4817 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
4818 printf(" %-15s %-27s %s\n",
4819 ctdb_commands[i].name,
4820 ctdb_commands[i].args?ctdb_commands[i].args:"",
4821 ctdb_commands[i].msg);
4827 static void ctdb_alarm(int sig)
4829 printf("Maximum runtime exceeded - exiting\n");
4836 int main(int argc, const char *argv[])
4838 struct ctdb_context *ctdb;
4839 char *nodestring = NULL;
4840 struct poptOption popt_options[] = {
4843 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
4844 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
4845 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
4846 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
4847 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
4851 const char **extra_argv;
4855 struct event_context *ev;
4856 const char *control;
4860 /* set some defaults */
4861 options.maxruntime = 0;
4862 options.timelimit = 3;
4863 options.pnn = CTDB_CURRENT_NODE;
4865 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
4867 while ((opt = poptGetNextOpt(pc)) != -1) {
4870 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
4871 poptBadOption(pc, 0), poptStrerror(opt)));
4876 /* setup the remaining options for the main program to use */
4877 extra_argv = poptGetArgs(pc);
4880 while (extra_argv[extra_argc]) extra_argc++;
4883 if (extra_argc < 1) {
4887 if (options.maxruntime == 0) {
4888 const char *ctdb_timeout;
4889 ctdb_timeout = getenv("CTDB_TIMEOUT");
4890 if (ctdb_timeout != NULL) {
4891 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
4893 /* default timeout is 120 seconds */
4894 options.maxruntime = 120;
4898 signal(SIGALRM, ctdb_alarm);
4899 alarm(options.maxruntime);
4901 /* setup the node number to contact */
4902 if (nodestring != NULL) {
4903 if (strcmp(nodestring, "all") == 0) {
4904 options.pnn = CTDB_BROADCAST_ALL;
4906 options.pnn = strtoul(nodestring, NULL, 0);
4910 control = extra_argv[0];
4912 ev = event_context_init(NULL);
4914 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
4917 tevent_loop_allow_nesting(ev);
4919 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
4920 if (strcmp(control, ctdb_commands[i].name) == 0) {
4923 if (ctdb_commands[i].without_daemon == true) {
4927 /* initialise ctdb */
4928 ctdb = ctdb_cmdline_client(ev);
4930 if (ctdb_commands[i].without_daemon == false) {
4931 const char *socket_name;
4934 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
4938 /* initialize a libctdb connection as well */
4939 socket_name = ctdb_get_socketname(ctdb);
4940 ctdb_connection = ctdb_connect(socket_name,
4941 ctdb_log_file, stderr);
4942 if (ctdb_connection == NULL) {
4943 fprintf(stderr, "Failed to connect to daemon from libctdb\n");
4947 /* verify the node exists */
4950 if (options.pnn == CTDB_CURRENT_NODE) {
4952 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
4960 if (ctdb_commands[i].auto_all &&
4961 options.pnn == CTDB_BROADCAST_ALL) {
4966 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
4967 CTDB_NO_MEMORY(ctdb, nodes);
4969 for (j=0;j<num_nodes;j++) {
4970 options.pnn = nodes[j];
4971 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
4975 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
4981 if (i == ARRAY_SIZE(ctdb_commands)) {
4982 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));