4 Copyright (C) Amitay Isaacs 2015
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/network.h"
22 #include "system/filesys.h"
23 #include "system/time.h"
24 #include "system/wait.h"
25 #include "system/dir.h"
33 #include "common/version.h"
34 #include "lib/util/debug.h"
35 #include "lib/util/samba_util.h"
36 #include "lib/util/sys_rw.h"
38 #include "common/db_hash.h"
39 #include "common/logging.h"
40 #include "protocol/protocol.h"
41 #include "protocol/protocol_api.h"
42 #include "protocol/protocol_util.h"
43 #include "common/system.h"
44 #include "client/client.h"
45 #include "client/client_sync.h"
47 #define TIMEOUT() timeval_current_ofs(options.timelimit, 0)
49 #define SRVID_CTDB_TOOL (CTDB_SRVID_TOOL_RANGE | 0x0001000000000000LL)
50 #define SRVID_CTDB_PUSHDB (CTDB_SRVID_TOOL_RANGE | 0x0002000000000000LL)
54 const char *debuglevelstr;
62 int printemptyrecords;
69 static poptContext pc;
72 struct tevent_context *ev;
73 struct ctdb_client_context *client;
74 struct ctdb_node_map *nodemap;
75 uint32_t pnn, cmd_pnn;
79 static void usage(const char *command);
85 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
87 return (tv2->tv_sec - tv->tv_sec) +
88 (tv2->tv_usec - tv->tv_usec) * 1.0e-6;
91 static struct ctdb_node_and_flags *get_node_by_pnn(
92 struct ctdb_node_map *nodemap,
97 for (i=0; i<nodemap->num; i++) {
98 if (nodemap->node[i].pnn == pnn) {
99 return &nodemap->node[i];
105 static const char *pretty_print_flags(TALLOC_CTX *mem_ctx, uint32_t flags)
107 static const struct {
111 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
112 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
113 { NODE_FLAGS_BANNED, "BANNED" },
114 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
115 { NODE_FLAGS_DELETED, "DELETED" },
116 { NODE_FLAGS_STOPPED, "STOPPED" },
117 { NODE_FLAGS_INACTIVE, "INACTIVE" },
119 char *flags_str = NULL;
122 for (i=0; i<ARRAY_SIZE(flag_names); i++) {
123 if (flags & flag_names[i].flag) {
124 if (flags_str == NULL) {
125 flags_str = talloc_asprintf(mem_ctx,
126 "%s", flag_names[i].name);
128 flags_str = talloc_asprintf_append(flags_str,
129 "|%s", flag_names[i].name);
131 if (flags_str == NULL) {
132 return "OUT-OF-MEMORY";
136 if (flags_str == NULL) {
143 static uint64_t next_srvid(struct ctdb_context *ctdb)
150 * Get consistent nodemap information.
152 * If nodemap is already cached, use that. If not get it.
153 * If the current node is BANNED, then get nodemap from "better" node.
155 static struct ctdb_node_map *get_nodemap(struct ctdb_context *ctdb, bool force)
158 struct ctdb_node_map *nodemap;
159 struct ctdb_node_and_flags *node;
160 uint32_t current_node;
164 TALLOC_FREE(ctdb->nodemap);
167 if (ctdb->nodemap != NULL) {
168 return ctdb->nodemap;
171 tmp_ctx = talloc_new(ctdb);
172 if (tmp_ctx == NULL) {
176 current_node = ctdb->pnn;
178 ret = ctdb_ctrl_get_nodemap(tmp_ctx, ctdb->ev, ctdb->client,
179 current_node, TIMEOUT(), &nodemap);
181 fprintf(stderr, "Failed to get nodemap from node %u\n",
186 node = get_node_by_pnn(nodemap, current_node);
187 if (node->flags & NODE_FLAGS_BANNED) {
190 current_node = (current_node + 1) % nodemap->num;
191 node = get_node_by_pnn(nodemap, current_node);
193 (NODE_FLAGS_DELETED|NODE_FLAGS_DISCONNECTED))) {
196 } while (current_node != ctdb->pnn);
198 if (current_node == ctdb->pnn) {
199 /* Tried all nodes in the cluster */
200 fprintf(stderr, "Warning: All nodes are banned.\n");
207 ctdb->nodemap = talloc_steal(ctdb, nodemap);
211 talloc_free(tmp_ctx);
215 static bool verify_pnn(struct ctdb_context *ctdb, int pnn)
217 struct ctdb_node_map *nodemap;
225 nodemap = get_nodemap(ctdb, false);
226 if (nodemap == NULL) {
231 for (i=0; i<nodemap->num; i++) {
232 if (nodemap->node[i].pnn == pnn) {
238 fprintf(stderr, "Node %u does not exist\n", pnn);
242 if (nodemap->node[i].flags &
243 (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED)) {
244 fprintf(stderr, "Node %u has status %s\n", pnn,
245 pretty_print_flags(ctdb, nodemap->node[i].flags));
252 static struct ctdb_node_map *talloc_nodemap(TALLOC_CTX *mem_ctx,
253 struct ctdb_node_map *nodemap)
255 struct ctdb_node_map *nodemap2;
257 nodemap2 = talloc_zero(mem_ctx, struct ctdb_node_map);
258 if (nodemap2 == NULL) {
262 nodemap2->node = talloc_array(nodemap2, struct ctdb_node_and_flags,
264 if (nodemap2->node == NULL) {
265 talloc_free(nodemap2);
273 * Get the number and the list of matching nodes
275 * nodestring := NULL | all | pnn,[pnn,...]
277 * If nodestring is NULL, use the current node.
279 static bool parse_nodestring(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
280 const char *nodestring,
281 struct ctdb_node_map **out)
283 struct ctdb_node_map *nodemap, *nodemap2;
284 struct ctdb_node_and_flags *node;
287 nodemap = get_nodemap(ctdb, false);
288 if (nodemap == NULL) {
292 nodemap2 = talloc_nodemap(mem_ctx, nodemap);
293 if (nodemap2 == NULL) {
297 if (nodestring == NULL) {
298 for (i=0; i<nodemap->num; i++) {
299 if (nodemap->node[i].pnn == ctdb->cmd_pnn) {
300 nodemap2->node[0] = nodemap->node[i];
309 if (strcmp(nodestring, "all") == 0) {
310 for (i=0; i<nodemap->num; i++) {
311 nodemap2->node[i] = nodemap->node[i];
313 nodemap2->num = nodemap->num;
319 ns = talloc_strdup(mem_ctx, nodestring);
324 tok = strtok(ns, ",");
325 while (tok != NULL) {
329 pnn = (uint32_t)strtoul(tok, &endptr, 0);
330 if (pnn == 0 && tok == endptr) {
331 fprintf(stderr, "Invalid node %s\n", tok);
335 node = get_node_by_pnn(nodemap, pnn);
337 fprintf(stderr, "Node %u does not exist\n",
342 nodemap2->node[nodemap2->num] = *node;
345 tok = strtok(NULL, ",");
354 /* Compare IP address */
355 static bool ctdb_same_ip(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
359 if (ip1->sa.sa_family != ip2->sa.sa_family) {
363 switch (ip1->sa.sa_family) {
365 ret = (memcmp(&ip1->ip.sin_addr, &ip2->ip.sin_addr,
366 sizeof(struct in_addr)) == 0);
370 ret = (memcmp(&ip1->ip6.sin6_addr, &ip2->ip6.sin6_addr,
371 sizeof(struct in6_addr)) == 0);
378 /* Append a node to a node map with given address and flags */
379 static bool node_map_add(struct ctdb_node_map *nodemap,
380 const char *nstr, uint32_t flags)
384 struct ctdb_node_and_flags *n;
387 ret = ctdb_sock_addr_from_string(nstr, &addr, false);
389 fprintf(stderr, "Invalid IP address %s\n", nstr);
394 nodemap->node = talloc_realloc(nodemap, nodemap->node,
395 struct ctdb_node_and_flags, num+1);
396 if (nodemap->node == NULL) {
400 n = &nodemap->node[num];
405 nodemap->num = num+1;
409 /* Read a nodes file into a node map */
410 static struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
416 struct ctdb_node_map *nodemap;
418 nodemap = talloc_zero(mem_ctx, struct ctdb_node_map);
419 if (nodemap == NULL) {
423 lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
428 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
432 for (i=0; i<nlines; i++) {
438 /* strip leading spaces */
439 while((*node == ' ') || (*node == '\t')) {
445 /* strip trailing spaces */
447 ((node[len-1] == ' ') || (node[len-1] == '\t')))
457 /* A "deleted" node is a node that is
458 commented out in the nodes file. This is
459 used instead of removing a line, which
460 would cause subsequent nodes to change
462 flags = NODE_FLAGS_DELETED;
463 node = discard_const("0.0.0.0");
467 if (! node_map_add(nodemap, node, flags)) {
469 TALLOC_FREE(nodemap);
478 static struct ctdb_node_map *read_nodes_file(TALLOC_CTX *mem_ctx, uint32_t pnn)
480 struct ctdb_node_map *nodemap;
482 const char *nodes_list = NULL;
484 if (pnn != CTDB_UNKNOWN_PNN) {
485 nodepath = talloc_asprintf(mem_ctx, "CTDB_NODES_%u", pnn);
486 if (nodepath != NULL) {
487 nodes_list = getenv(nodepath);
490 if (nodes_list == NULL) {
491 nodes_list = getenv("CTDB_NODES");
493 if (nodes_list == NULL) {
494 const char *basedir = getenv("CTDB_BASE");
495 if (basedir == NULL) {
496 basedir = CTDB_ETCDIR;
498 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes", basedir);
499 if (nodes_list == NULL) {
500 fprintf(stderr, "Memory allocation error\n");
505 nodemap = ctdb_read_nodes_file(mem_ctx, nodes_list);
506 if (nodemap == NULL) {
507 fprintf(stderr, "Failed to read nodes file \"%s\"\n",
515 static struct ctdb_dbid *db_find(TALLOC_CTX *mem_ctx,
516 struct ctdb_context *ctdb,
517 struct ctdb_dbid_map *dbmap,
520 struct ctdb_dbid *db = NULL;
524 for (i=0; i<dbmap->num; i++) {
525 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
526 ctdb->pnn, TIMEOUT(),
527 dbmap->dbs[i].db_id, &name);
532 if (strcmp(db_name, name) == 0) {
533 talloc_free(discard_const(name));
542 static bool db_exists(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
543 const char *db_arg, uint32_t *db_id,
544 const char **db_name, uint8_t *db_flags)
546 struct ctdb_dbid_map *dbmap;
547 struct ctdb_dbid *db = NULL;
549 const char *name = NULL;
552 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
553 ctdb->pnn, TIMEOUT(), &dbmap);
558 if (strncmp(db_arg, "0x", 2) == 0) {
559 id = strtoul(db_arg, NULL, 0);
560 for (i=0; i<dbmap->num; i++) {
561 if (id == dbmap->dbs[i].db_id) {
568 db = db_find(mem_ctx, ctdb, dbmap, name);
572 fprintf(stderr, "No database matching '%s' found\n", db_arg);
577 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
578 ctdb->pnn, TIMEOUT(), id, &name);
587 if (db_name != NULL) {
588 *db_name = talloc_strdup(mem_ctx, name);
590 if (db_flags != NULL) {
591 *db_flags = db->flags;
596 static int h2i(char h)
598 if (h >= 'a' && h <= 'f') {
601 if (h >= 'A' && h <= 'F') {
607 static int hex_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
614 fprintf(stderr, "Key (%s) contains odd number of hex digits\n",
619 data.dsize = len / 2;
620 data.dptr = talloc_size(mem_ctx, data.dsize);
621 if (data.dptr == NULL) {
625 for (i=0; i<data.dsize; i++) {
626 data.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
633 static int str_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
639 if (strncmp(str, "0x", 2) == 0) {
640 ret = hex_to_data(str+2, len-2, mem_ctx, &data);
642 data.dptr = talloc_memdup(mem_ctx, str, len);
643 if (data.dptr == NULL) {
653 static int run_helper(TALLOC_CTX *mem_ctx, const char *command,
654 const char *path, int argc, const char **argv)
657 int save_errno, status, ret;
658 const char **new_argv;
661 new_argv = talloc_array(mem_ctx, const char *, argc + 2);
662 if (new_argv == NULL) {
667 for (i=0; i<argc; i++) {
668 new_argv[i+1] = argv[i];
670 new_argv[argc+1] = NULL;
675 talloc_free(new_argv);
676 fprintf(stderr, "Failed to fork %s (%s) - %s\n",
677 command, path, strerror(save_errno));
682 ret = execv(path, discard_const(new_argv));
686 /* Should not happen */
690 talloc_free(new_argv);
692 ret = waitpid(pid, &status, 0);
695 fprintf(stderr, "waitpid() failed for %s - %s\n",
696 command, strerror(save_errno));
700 if (WIFEXITED(status)) {
701 int pstatus = WEXITSTATUS(status);
702 if (WIFSIGNALED(status)) {
703 fprintf(stderr, "%s terminated with signal %d\n",
704 command, WTERMSIG(status));
706 } else if (pstatus >= 64 && pstatus < 255) {
707 fprintf(stderr, "%s failed with error %d\n",
708 command, pstatus-64);
714 } else if (WIFSIGNALED(status)) {
715 fprintf(stderr, "%s terminated with signal %d\n",
716 command, WTERMSIG(status));
727 static int control_version(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
728 int argc, const char **argv)
730 printf("%s\n", ctdb_version_string);
734 static bool partially_online(TALLOC_CTX *mem_ctx,
735 struct ctdb_context *ctdb,
736 struct ctdb_node_and_flags *node)
738 struct ctdb_iface_list *iface_list;
742 if (node->flags != 0) {
746 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
747 node->pnn, TIMEOUT(), &iface_list);
753 for (i=0; i < iface_list->num; i++) {
754 if (iface_list->iface[i].link_state == 0) {
763 static void print_nodemap_machine(TALLOC_CTX *mem_ctx,
764 struct ctdb_context *ctdb,
765 struct ctdb_node_map *nodemap,
768 struct ctdb_node_and_flags *node;
771 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
775 "Disconnected", options.sep,
776 "Banned", options.sep,
777 "Disabled", options.sep,
778 "Unhealthy", options.sep,
779 "Stopped", options.sep,
780 "Inactive", options.sep,
781 "PartiallyOnline", options.sep,
782 "ThisNode", options.sep);
784 for (i=0; i<nodemap->num; i++) {
785 node = &nodemap->node[i];
786 if (node->flags & NODE_FLAGS_DELETED) {
790 printf("%s%u%s%s%s%d%s%d%s%d%s%d%s%d%s%d%s%d%s%c%s\n",
792 node->pnn, options.sep,
793 ctdb_sock_addr_to_string(mem_ctx, &node->addr, false),
795 !! (node->flags & NODE_FLAGS_DISCONNECTED), options.sep,
796 !! (node->flags & NODE_FLAGS_BANNED), options.sep,
797 !! (node->flags & NODE_FLAGS_PERMANENTLY_DISABLED),
799 !! (node->flags & NODE_FLAGS_UNHEALTHY), options.sep,
800 !! (node->flags & NODE_FLAGS_STOPPED), options.sep,
801 !! (node->flags & NODE_FLAGS_INACTIVE), options.sep,
802 partially_online(mem_ctx, ctdb, node), options.sep,
803 (node->pnn == mypnn)?'Y':'N', options.sep);
808 static void print_nodemap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
809 struct ctdb_node_map *nodemap, uint32_t mypnn,
812 struct ctdb_node_and_flags *node;
813 int num_deleted_nodes = 0;
816 for (i=0; i<nodemap->num; i++) {
817 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
823 if (num_deleted_nodes == 0) {
824 printf("Number of nodes:%d\n", nodemap->num);
826 printf("Number of nodes:%d "
827 "(including %d deleted nodes)\n",
828 nodemap->num, num_deleted_nodes);
832 for (i=0; i<nodemap->num; i++) {
833 node = &nodemap->node[i];
834 if (node->flags & NODE_FLAGS_DELETED) {
838 printf("pnn:%u %-16s %s%s\n",
840 ctdb_sock_addr_to_string(mem_ctx, &node->addr, false),
841 partially_online(mem_ctx, ctdb, node) ?
843 pretty_print_flags(mem_ctx, node->flags),
844 node->pnn == mypnn ? " (THIS NODE)" : "");
848 static void print_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
849 struct ctdb_node_map *nodemap, uint32_t mypnn,
850 struct ctdb_vnn_map *vnnmap, int recmode,
855 print_nodemap(mem_ctx, ctdb, nodemap, mypnn, true);
857 if (vnnmap->generation == INVALID_GENERATION) {
858 printf("Generation:INVALID\n");
860 printf("Generation:%u\n", vnnmap->generation);
862 printf("Size:%d\n", vnnmap->size);
863 for (i=0; i<vnnmap->size; i++) {
864 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
867 printf("Recovery mode:%s (%d)\n",
868 recmode == CTDB_RECOVERY_NORMAL ? "NORMAL" : "RECOVERY",
870 printf("Recovery master:%d\n", recmaster);
873 static int control_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
874 int argc, const char **argv)
876 struct ctdb_node_map *nodemap;
877 struct ctdb_vnn_map *vnnmap;
886 nodemap = get_nodemap(ctdb, false);
887 if (nodemap == NULL) {
891 if (options.machinereadable == 1) {
892 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
896 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
897 ctdb->cmd_pnn, TIMEOUT(), &vnnmap);
902 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
903 ctdb->cmd_pnn, TIMEOUT(), &recmode);
908 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
909 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
914 print_status(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn, vnnmap,
919 static int control_uptime(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
920 int argc, const char **argv)
922 struct ctdb_uptime *uptime;
923 int ret, tmp, days, hours, minutes, seconds;
925 ret = ctdb_ctrl_uptime(mem_ctx, ctdb->ev, ctdb->client,
926 ctdb->cmd_pnn, TIMEOUT(), &uptime);
931 printf("Current time of node %-4u : %s",
932 ctdb->cmd_pnn, ctime(&uptime->current_time.tv_sec));
934 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
935 seconds = tmp % 60; tmp /= 60;
936 minutes = tmp % 60; tmp /= 60;
937 hours = tmp % 24; tmp /= 24;
940 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s",
941 days, hours, minutes, seconds,
942 ctime(&uptime->ctdbd_start_time.tv_sec));
944 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
945 seconds = tmp % 60; tmp /= 60;
946 minutes = tmp % 60; tmp /= 60;
947 hours = tmp % 24; tmp /= 24;
950 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s",
951 days, hours, minutes, seconds,
952 ctime(&uptime->last_recovery_finished.tv_sec));
954 printf("Duration of last recovery/failover: %lf seconds\n",
955 timeval_delta(&uptime->last_recovery_finished,
956 &uptime->last_recovery_started));
961 static int control_ping(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
962 int argc, const char **argv)
965 int ret, num_clients;
967 tv = timeval_current();
968 ret = ctdb_ctrl_ping(mem_ctx, ctdb->ev, ctdb->client,
969 ctdb->cmd_pnn, TIMEOUT(), &num_clients);
974 printf("response from %u time=%.6f sec (%d clients)\n",
975 ctdb->cmd_pnn, timeval_elapsed(&tv), num_clients);
979 const char *runstate_to_string(enum ctdb_runstate runstate);
980 enum ctdb_runstate runstate_from_string(const char *runstate_str);
982 static int control_runstate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
983 int argc, const char **argv)
985 enum ctdb_runstate runstate;
989 ret = ctdb_ctrl_get_runstate(mem_ctx, ctdb->ev, ctdb->client,
990 ctdb->cmd_pnn, TIMEOUT(), &runstate);
996 for (i=0; i<argc; i++) {
997 enum ctdb_runstate t;
1000 t = ctdb_runstate_from_string(argv[i]);
1001 if (t == CTDB_RUNSTATE_UNKNOWN) {
1002 printf("Invalid run state (%s)\n", argv[i]);
1006 if (t == runstate) {
1013 printf("CTDB not in required run state (got %s)\n",
1014 ctdb_runstate_to_string(runstate));
1018 printf("%s\n", ctdb_runstate_to_string(runstate));
1022 static int control_getvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1023 int argc, const char **argv)
1025 struct ctdb_var_list *tun_var_list;
1034 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1035 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1038 "Failed to get list of variables from node %u\n",
1044 for (i=0; i<tun_var_list->count; i++) {
1045 if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
1052 printf("No such tunable %s\n", argv[0]);
1056 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
1057 ctdb->cmd_pnn, TIMEOUT(), argv[0], &value);
1062 printf("%-26s = %u\n", argv[0], value);
1066 static int control_setvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1067 int argc, const char **argv)
1069 struct ctdb_var_list *tun_var_list;
1070 struct ctdb_tunable tunable;
1078 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1079 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1082 "Failed to get list of variables from node %u\n",
1088 for (i=0; i<tun_var_list->count; i++) {
1089 if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
1096 printf("No such tunable %s\n", argv[0]);
1100 tunable.name = argv[0];
1101 tunable.value = strtoul(argv[1], NULL, 0);
1103 ret = ctdb_ctrl_set_tunable(mem_ctx, ctdb->ev, ctdb->client,
1104 ctdb->cmd_pnn, TIMEOUT(), &tunable);
1108 "Setting obsolete tunable variable '%s'\n",
1117 static int control_listvars(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1118 int argc, const char **argv)
1120 struct ctdb_var_list *tun_var_list;
1127 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1128 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1133 for (i=0; i<tun_var_list->count; i++) {
1134 control_getvar(mem_ctx, ctdb, 1, &tun_var_list->var[i]);
1143 } stats_fields[] = {
1144 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
1145 STATISTICS_FIELD(num_clients),
1146 STATISTICS_FIELD(frozen),
1147 STATISTICS_FIELD(recovering),
1148 STATISTICS_FIELD(num_recoveries),
1149 STATISTICS_FIELD(client_packets_sent),
1150 STATISTICS_FIELD(client_packets_recv),
1151 STATISTICS_FIELD(node_packets_sent),
1152 STATISTICS_FIELD(node_packets_recv),
1153 STATISTICS_FIELD(keepalive_packets_sent),
1154 STATISTICS_FIELD(keepalive_packets_recv),
1155 STATISTICS_FIELD(node.req_call),
1156 STATISTICS_FIELD(node.reply_call),
1157 STATISTICS_FIELD(node.req_dmaster),
1158 STATISTICS_FIELD(node.reply_dmaster),
1159 STATISTICS_FIELD(node.reply_error),
1160 STATISTICS_FIELD(node.req_message),
1161 STATISTICS_FIELD(node.req_control),
1162 STATISTICS_FIELD(node.reply_control),
1163 STATISTICS_FIELD(node.req_tunnel),
1164 STATISTICS_FIELD(client.req_call),
1165 STATISTICS_FIELD(client.req_message),
1166 STATISTICS_FIELD(client.req_control),
1167 STATISTICS_FIELD(client.req_tunnel),
1168 STATISTICS_FIELD(timeouts.call),
1169 STATISTICS_FIELD(timeouts.control),
1170 STATISTICS_FIELD(timeouts.traverse),
1171 STATISTICS_FIELD(locks.num_calls),
1172 STATISTICS_FIELD(locks.num_current),
1173 STATISTICS_FIELD(locks.num_pending),
1174 STATISTICS_FIELD(locks.num_failed),
1175 STATISTICS_FIELD(total_calls),
1176 STATISTICS_FIELD(pending_calls),
1177 STATISTICS_FIELD(childwrite_calls),
1178 STATISTICS_FIELD(pending_childwrite_calls),
1179 STATISTICS_FIELD(memory_used),
1180 STATISTICS_FIELD(max_hop_count),
1181 STATISTICS_FIELD(total_ro_delegations),
1182 STATISTICS_FIELD(total_ro_revokes),
1185 #define LATENCY_AVG(v) ((v).num ? (v).total / (v).num : 0.0 )
1187 static void print_statistics_machine(struct ctdb_statistics *s,
1193 printf("CTDB version%s", options.sep);
1194 printf("Current time of statistics%s", options.sep);
1195 printf("Statistics collected since%s", options.sep);
1196 for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
1197 printf("%s%s", stats_fields[i].name, options.sep);
1199 printf("num_reclock_ctdbd_latency%s", options.sep);
1200 printf("min_reclock_ctdbd_latency%s", options.sep);
1201 printf("avg_reclock_ctdbd_latency%s", options.sep);
1202 printf("max_reclock_ctdbd_latency%s", options.sep);
1204 printf("num_reclock_recd_latency%s", options.sep);
1205 printf("min_reclock_recd_latency%s", options.sep);
1206 printf("avg_reclock_recd_latency%s", options.sep);
1207 printf("max_reclock_recd_latency%s", options.sep);
1209 printf("num_call_latency%s", options.sep);
1210 printf("min_call_latency%s", options.sep);
1211 printf("avg_call_latency%s", options.sep);
1212 printf("max_call_latency%s", options.sep);
1214 printf("num_lockwait_latency%s", options.sep);
1215 printf("min_lockwait_latency%s", options.sep);
1216 printf("avg_lockwait_latency%s", options.sep);
1217 printf("max_lockwait_latency%s", options.sep);
1219 printf("num_childwrite_latency%s", options.sep);
1220 printf("min_childwrite_latency%s", options.sep);
1221 printf("avg_childwrite_latency%s", options.sep);
1222 printf("max_childwrite_latency%s", options.sep);
1226 printf("%u%s", CTDB_PROTOCOL, options.sep);
1227 printf("%u%s", (uint32_t)s->statistics_current_time.tv_sec, options.sep);
1228 printf("%u%s", (uint32_t)s->statistics_start_time.tv_sec, options.sep);
1229 for (i=0;i<ARRAY_SIZE(stats_fields);i++) {
1231 *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s),
1234 printf("%u%s", s->reclock.ctdbd.num, options.sep);
1235 printf("%.6f%s", s->reclock.ctdbd.min, options.sep);
1236 printf("%.6f%s", LATENCY_AVG(s->reclock.ctdbd), options.sep);
1237 printf("%.6f%s", s->reclock.ctdbd.max, options.sep);
1239 printf("%u%s", s->reclock.recd.num, options.sep);
1240 printf("%.6f%s", s->reclock.recd.min, options.sep);
1241 printf("%.6f%s", LATENCY_AVG(s->reclock.recd), options.sep);
1242 printf("%.6f%s", s->reclock.recd.max, options.sep);
1244 printf("%d%s", s->call_latency.num, options.sep);
1245 printf("%.6f%s", s->call_latency.min, options.sep);
1246 printf("%.6f%s", LATENCY_AVG(s->call_latency), options.sep);
1247 printf("%.6f%s", s->call_latency.max, options.sep);
1249 printf("%d%s", s->childwrite_latency.num, options.sep);
1250 printf("%.6f%s", s->childwrite_latency.min, options.sep);
1251 printf("%.6f%s", LATENCY_AVG(s->childwrite_latency), options.sep);
1252 printf("%.6f%s", s->childwrite_latency.max, options.sep);
1256 static void print_statistics(struct ctdb_statistics *s)
1258 int tmp, days, hours, minutes, seconds;
1260 const char *prefix = NULL;
1263 tmp = s->statistics_current_time.tv_sec -
1264 s->statistics_start_time.tv_sec;
1265 seconds = tmp % 60; tmp /= 60;
1266 minutes = tmp % 60; tmp /= 60;
1267 hours = tmp % 24; tmp /= 24;
1270 printf("CTDB version %u\n", CTDB_PROTOCOL);
1271 printf("Current time of statistics : %s",
1272 ctime(&s->statistics_current_time.tv_sec));
1273 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s",
1274 days, hours, minutes, seconds,
1275 ctime(&s->statistics_start_time.tv_sec));
1277 for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
1278 if (strchr(stats_fields[i].name, '.') != NULL) {
1279 preflen = strcspn(stats_fields[i].name, ".") + 1;
1281 strncmp(prefix, stats_fields[i].name, preflen) != 0) {
1282 prefix = stats_fields[i].name;
1283 printf(" %*.*s\n", preflen-1, preflen-1,
1284 stats_fields[i].name);
1289 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
1290 stats_fields[i].name+preflen, preflen ? 0 : 4, "",
1291 *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s));
1294 printf(" hop_count_buckets:");
1295 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
1296 printf(" %d", s->hop_count_bucket[i]);
1299 printf(" lock_buckets:");
1300 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
1301 printf(" %d", s->locks.buckets[i]);
1304 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1305 "locks_latency MIN/AVG/MAX",
1306 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
1307 s->locks.latency.max, s->locks.latency.num);
1309 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1310 "reclock_ctdbd MIN/AVG/MAX",
1311 s->reclock.ctdbd.min, LATENCY_AVG(s->reclock.ctdbd),
1312 s->reclock.ctdbd.max, s->reclock.ctdbd.num);
1314 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1315 "reclock_recd MIN/AVG/MAX",
1316 s->reclock.recd.min, LATENCY_AVG(s->reclock.recd),
1317 s->reclock.recd.max, s->reclock.recd.num);
1319 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1320 "call_latency MIN/AVG/MAX",
1321 s->call_latency.min, LATENCY_AVG(s->call_latency),
1322 s->call_latency.max, s->call_latency.num);
1324 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1325 "childwrite_latency MIN/AVG/MAX",
1326 s->childwrite_latency.min,
1327 LATENCY_AVG(s->childwrite_latency),
1328 s->childwrite_latency.max, s->childwrite_latency.num);
1331 static int control_statistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1332 int argc, const char **argv)
1334 struct ctdb_statistics *stats;
1338 usage("statistics");
1341 ret = ctdb_ctrl_statistics(mem_ctx, ctdb->ev, ctdb->client,
1342 ctdb->cmd_pnn, TIMEOUT(), &stats);
1347 if (options.machinereadable) {
1348 print_statistics_machine(stats, true);
1350 print_statistics(stats);
1356 static int control_statistics_reset(TALLOC_CTX *mem_ctx,
1357 struct ctdb_context *ctdb,
1358 int argc, const char **argv)
1363 usage("statisticsreset");
1366 ret = ctdb_ctrl_statistics_reset(mem_ctx, ctdb->ev, ctdb->client,
1367 ctdb->cmd_pnn, TIMEOUT());
1375 static int control_stats(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1376 int argc, const char **argv)
1378 struct ctdb_statistics_list *slist;
1379 int ret, count = 0, i;
1380 bool show_header = true;
1387 count = atoi(argv[0]);
1390 ret = ctdb_ctrl_get_stat_history(mem_ctx, ctdb->ev, ctdb->client,
1391 ctdb->cmd_pnn, TIMEOUT(), &slist);
1396 for (i=0; i<slist->num; i++) {
1397 if (slist->stats[i].statistics_start_time.tv_sec == 0) {
1400 if (options.machinereadable == 1) {
1401 print_statistics_machine(&slist->stats[i],
1403 show_header = false;
1405 print_statistics(&slist->stats[i]);
1407 if (count > 0 && i == count) {
1415 static int ctdb_public_ip_cmp(const void *a, const void *b)
1417 const struct ctdb_public_ip *ip_a = a;
1418 const struct ctdb_public_ip *ip_b = b;
1420 return ctdb_sock_addr_cmp(&ip_a->addr, &ip_b->addr);
1423 static void print_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1424 struct ctdb_public_ip_list *ips,
1425 struct ctdb_public_ip_info **ipinfo,
1429 char *conf, *avail, *active;
1431 if (options.machinereadable == 1) {
1432 printf("%s%s%s%s%s", options.sep,
1433 "Public IP", options.sep,
1434 "Node", options.sep);
1435 if (options.verbose == 1) {
1436 printf("%s%s%s%s%s%s\n",
1437 "ActiveInterfaces", options.sep,
1438 "AvailableInterfaces", options.sep,
1439 "ConfiguredInterfaces", options.sep);
1445 printf("Public IPs on ALL nodes\n");
1447 printf("Public IPs on node %u\n", ctdb->cmd_pnn);
1451 for (i = 0; i < ips->num; i++) {
1453 if (options.machinereadable == 1) {
1454 printf("%s%s%s%d%s", options.sep,
1455 ctdb_sock_addr_to_string(
1456 mem_ctx, &ips->ip[i].addr, false),
1458 (int)ips->ip[i].pnn, options.sep);
1460 printf("%s", ctdb_sock_addr_to_string(
1461 mem_ctx, &ips->ip[i].addr, false));
1464 if (options.verbose == 0) {
1465 if (options.machinereadable == 1) {
1468 printf(" %d\n", (int)ips->ip[i].pnn);
1477 if (ipinfo[i] == NULL) {
1481 for (j=0; j<ipinfo[i]->ifaces->num; j++) {
1482 struct ctdb_iface *iface;
1484 iface = &ipinfo[i]->ifaces->iface[j];
1486 conf = talloc_strdup(mem_ctx, iface->name);
1488 conf = talloc_asprintf_append(
1489 conf, ",%s", iface->name);
1492 if (ipinfo[i]->active_idx == j) {
1493 active = iface->name;
1496 if (iface->link_state == 0) {
1500 if (avail == NULL) {
1501 avail = talloc_strdup(mem_ctx, iface->name);
1503 avail = talloc_asprintf_append(
1504 avail, ",%s", iface->name);
1510 if (options.machinereadable == 1) {
1511 printf("%s%s%s%s%s%s\n",
1512 active ? active : "", options.sep,
1513 avail ? avail : "", options.sep,
1514 conf ? conf : "", options.sep);
1516 printf(" node[%d] active[%s] available[%s]"
1517 " configured[%s]\n",
1518 (int)ips->ip[i].pnn, active ? active : "",
1519 avail ? avail : "", conf ? conf : "");
1524 static int collect_ips(uint8_t *keybuf, size_t keylen, uint8_t *databuf,
1525 size_t datalen, void *private_data)
1527 struct ctdb_public_ip_list *ips = talloc_get_type_abort(
1528 private_data, struct ctdb_public_ip_list);
1529 struct ctdb_public_ip *ip;
1531 ip = (struct ctdb_public_ip *)databuf;
1532 ips->ip[ips->num] = *ip;
1538 static int get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
1539 struct ctdb_public_ip_list **out)
1541 struct ctdb_node_map *nodemap;
1542 struct ctdb_public_ip_list *ips;
1543 struct db_hash_context *ipdb;
1545 int ret, count, i, j;
1547 nodemap = get_nodemap(ctdb, false);
1548 if (nodemap == NULL) {
1552 ret = db_hash_init(mem_ctx, "ips", 101, DB_HASH_COMPLEX, &ipdb);
1557 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
1563 for (i=0; i<count; i++) {
1564 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1565 pnn_list[i], TIMEOUT(),
1571 for (j=0; j<ips->num; j++) {
1572 struct ctdb_public_ip ip;
1574 ip.pnn = ips->ip[j].pnn;
1575 ip.addr = ips->ip[j].addr;
1577 if (pnn_list[i] == ip.pnn) {
1578 /* Node claims IP is hosted on it, so
1579 * save that information
1581 ret = db_hash_add(ipdb, (uint8_t *)&ip.addr,
1583 (uint8_t *)&ip, sizeof(ip));
1588 /* Node thinks IP is hosted elsewhere,
1589 * so overwrite with CTDB_UNKNOWN_PNN
1590 * if there's no existing entry
1592 ret = db_hash_exists(ipdb, (uint8_t *)&ip.addr,
1594 if (ret == ENOENT) {
1595 ip.pnn = CTDB_UNKNOWN_PNN;
1596 ret = db_hash_add(ipdb,
1597 (uint8_t *)&ip.addr,
1611 talloc_free(pnn_list);
1613 ret = db_hash_traverse(ipdb, NULL, NULL, &count);
1618 ips = talloc_zero(mem_ctx, struct ctdb_public_ip_list);
1623 ips->ip = talloc_array(ips, struct ctdb_public_ip, count);
1624 if (ips->ip == NULL) {
1628 ret = db_hash_traverse(ipdb, collect_ips, ips, &count);
1633 if (count != ips->num) {
1647 static int control_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1648 int argc, const char **argv)
1650 struct ctdb_public_ip_list *ips;
1651 struct ctdb_public_ip_info **ipinfo;
1653 bool do_all = false;
1660 if (strcmp(argv[0], "all") == 0) {
1668 ret = get_all_public_ips(ctdb, mem_ctx, &ips);
1670 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1671 ctdb->cmd_pnn, TIMEOUT(),
1678 qsort(ips->ip, ips->num, sizeof(struct ctdb_public_ip),
1679 ctdb_public_ip_cmp);
1681 ipinfo = talloc_array(mem_ctx, struct ctdb_public_ip_info *, ips->num);
1682 if (ipinfo == NULL) {
1686 for (i=0; i<ips->num; i++) {
1689 pnn = ips->ip[i].pnn;
1691 pnn = ctdb->cmd_pnn;
1693 if (pnn == CTDB_UNKNOWN_PNN) {
1697 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev,
1699 TIMEOUT(), &ips->ip[i].addr,
1706 print_ip(mem_ctx, ctdb, ips, ipinfo, do_all);
1710 static int control_ipinfo(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1711 int argc, const char **argv)
1713 struct ctdb_public_ip_info *ipinfo;
1714 ctdb_sock_addr addr;
1721 ret = ctdb_sock_addr_from_string(argv[0], &addr, false);
1723 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
1727 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev, ctdb->client,
1728 ctdb->cmd_pnn, TIMEOUT(), &addr,
1732 printf("Node %u does not know about IP %s\n",
1733 ctdb->cmd_pnn, argv[0]);
1738 printf("Public IP[%s] info on node %u\n",
1739 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr, false),
1742 printf("IP:%s\nCurrentNode:%u\nNumInterfaces:%u\n",
1743 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr, false),
1744 ipinfo->ip.pnn, ipinfo->ifaces->num);
1746 for (i=0; i<ipinfo->ifaces->num; i++) {
1747 struct ctdb_iface *iface;
1749 iface = &ipinfo->ifaces->iface[i];
1750 iface->name[CTDB_IFACE_SIZE] = '\0';
1751 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
1753 iface->link_state == 0 ? "down" : "up",
1755 (i == ipinfo->active_idx) ? " (active)" : "");
1761 static int control_ifaces(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1762 int argc, const char **argv)
1764 struct ctdb_iface_list *ifaces;
1771 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1772 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1777 if (ifaces->num == 0) {
1778 printf("No interfaces configured on node %u\n",
1783 if (options.machinereadable) {
1784 printf("%s%s%s%s%s%s%s\n", options.sep,
1785 "Name", options.sep,
1786 "LinkStatus", options.sep,
1787 "References", options.sep);
1789 printf("Interfaces on node %u\n", ctdb->cmd_pnn);
1792 for (i=0; i<ifaces->num; i++) {
1793 if (options.machinereadable) {
1794 printf("%s%s%s%u%s%u%s\n", options.sep,
1795 ifaces->iface[i].name, options.sep,
1796 ifaces->iface[i].link_state, options.sep,
1797 ifaces->iface[i].references, options.sep);
1799 printf("name:%s link:%s references:%u\n",
1800 ifaces->iface[i].name,
1801 ifaces->iface[i].link_state ? "up" : "down",
1802 ifaces->iface[i].references);
1809 static int control_setifacelink(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1810 int argc, const char **argv)
1812 struct ctdb_iface_list *ifaces;
1813 struct ctdb_iface *iface;
1817 usage("setifacelink");
1820 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
1821 fprintf(stderr, "Interface name '%s' too long\n", argv[0]);
1825 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1826 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1829 "Failed to get interface information from node %u\n",
1835 for (i=0; i<ifaces->num; i++) {
1836 if (strcmp(ifaces->iface[i].name, argv[0]) == 0) {
1837 iface = &ifaces->iface[i];
1842 if (iface == NULL) {
1843 printf("Interface %s not configured on node %u\n",
1844 argv[0], ctdb->cmd_pnn);
1848 if (strcmp(argv[1], "up") == 0) {
1849 iface->link_state = 1;
1850 } else if (strcmp(argv[1], "down") == 0) {
1851 iface->link_state = 0;
1853 usage("setifacelink");
1857 iface->references = 0;
1859 ret = ctdb_ctrl_set_iface_link_state(mem_ctx, ctdb->ev, ctdb->client,
1860 ctdb->cmd_pnn, TIMEOUT(), iface);
1868 static int control_process_exists(TALLOC_CTX *mem_ctx,
1869 struct ctdb_context *ctdb,
1870 int argc, const char **argv)
1876 if (argc != 1 && argc != 2) {
1877 usage("process-exists");
1880 pid = atoi(argv[0]);
1882 srvid = strtoull(argv[1], NULL, 0);
1886 ret = ctdb_ctrl_process_exists(mem_ctx, ctdb->ev, ctdb->client,
1887 ctdb->cmd_pnn, TIMEOUT(), pid, &status);
1889 struct ctdb_pid_srvid pid_srvid;
1891 pid_srvid.pid = pid;
1892 pid_srvid.srvid = srvid;
1894 ret = ctdb_ctrl_check_pid_srvid(mem_ctx, ctdb->ev,
1895 ctdb->client, ctdb->cmd_pnn,
1896 TIMEOUT(), &pid_srvid,
1905 printf("PID %d %s\n", pid,
1906 (status == 0 ? "exists" : "does not exist"));
1908 printf("PID %d with SRVID 0x%"PRIx64" %s\n", pid, srvid,
1909 (status == 0 ? "exists" : "does not exist"));
1914 static int control_getdbmap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1915 int argc, const char **argv)
1917 struct ctdb_dbid_map *dbmap;
1924 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
1925 ctdb->cmd_pnn, TIMEOUT(), &dbmap);
1930 if (options.machinereadable == 1) {
1931 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1934 "Name", options.sep,
1935 "Path", options.sep,
1936 "Persistent", options.sep,
1937 "Sticky", options.sep,
1938 "Unhealthy", options.sep,
1939 "Readonly", options.sep,
1940 "Replicated", options.sep);
1942 printf("Number of databases:%d\n", dbmap->num);
1945 for (i=0; i<dbmap->num; i++) {
1955 db_id = dbmap->dbs[i].db_id;
1957 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
1958 ctdb->cmd_pnn, TIMEOUT(), db_id,
1964 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1965 ctdb->cmd_pnn, TIMEOUT(), db_id,
1971 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1972 ctdb->cmd_pnn, TIMEOUT(), db_id,
1978 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
1979 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
1980 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
1981 replicated = dbmap->dbs[i].flags & CTDB_DB_FLAGS_REPLICATED;
1983 if (options.machinereadable == 1) {
1984 printf("%s0x%08X%s%s%s%s%s%d%s%d%s%d%s%d%s%d%s\n",
1989 !! (persistent), options.sep,
1990 !! (sticky), options.sep,
1991 !! (health), options.sep,
1992 !! (readonly), options.sep,
1993 !! (replicated), options.sep);
1995 printf("dbid:0x%08x name:%s path:%s%s%s%s%s%s\n",
1997 persistent ? " PERSISTENT" : "",
1998 sticky ? " STICKY" : "",
1999 readonly ? " READONLY" : "",
2000 replicated ? " REPLICATED" : "",
2001 health ? " UNHEALTHY" : "");
2004 talloc_free(discard_const(name));
2005 talloc_free(discard_const(path));
2006 talloc_free(discard_const(health));
2012 static int control_getdbstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2013 int argc, const char **argv)
2016 const char *db_name, *db_path, *db_health;
2021 usage("getdbstatus");
2024 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2028 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
2029 ctdb->cmd_pnn, TIMEOUT(), db_id,
2035 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
2036 ctdb->cmd_pnn, TIMEOUT(), db_id,
2042 printf("dbid: 0x%08x\nname: %s\npath: %s\n", db_id, db_name, db_path);
2043 printf("PERSISTENT: %s\nREPLICATED: %s\nSTICKY: %s\nREADONLY: %s\n",
2044 (db_flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
2045 (db_flags & CTDB_DB_FLAGS_REPLICATED ? "yes" : "no"),
2046 (db_flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
2047 (db_flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"));
2048 printf("HEALTH: %s\n", (db_health ? db_health : "OK"));
2052 struct dump_record_state {
2056 #define ISASCII(x) (isprint(x) && ! strchr("\"\\", (x)))
2058 static void dump_tdb_data(const char *name, TDB_DATA val)
2062 fprintf(stdout, "%s(%zu) = \"", name, val.dsize);
2063 for (i=0; i<val.dsize; i++) {
2064 if (ISASCII(val.dptr[i])) {
2065 fprintf(stdout, "%c", val.dptr[i]);
2067 fprintf(stdout, "\\%02X", val.dptr[i]);
2070 fprintf(stdout, "\"\n");
2073 static void dump_ltdb_header(struct ctdb_ltdb_header *header)
2075 fprintf(stdout, "dmaster: %u\n", header->dmaster);
2076 fprintf(stdout, "rsn: %" PRIu64 "\n", header->rsn);
2077 fprintf(stdout, "flags: 0x%08x", header->flags);
2078 if (header->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
2079 fprintf(stdout, " MIGRATED_WITH_DATA");
2081 if (header->flags & CTDB_REC_FLAG_VACUUM_MIGRATED) {
2082 fprintf(stdout, " VACUUM_MIGRATED");
2084 if (header->flags & CTDB_REC_FLAG_AUTOMATIC) {
2085 fprintf(stdout, " AUTOMATIC");
2087 if (header->flags & CTDB_REC_RO_HAVE_DELEGATIONS) {
2088 fprintf(stdout, " RO_HAVE_DELEGATIONS");
2090 if (header->flags & CTDB_REC_RO_HAVE_READONLY) {
2091 fprintf(stdout, " RO_HAVE_READONLY");
2093 if (header->flags & CTDB_REC_RO_REVOKING_READONLY) {
2094 fprintf(stdout, " RO_REVOKING_READONLY");
2096 if (header->flags & CTDB_REC_RO_REVOKE_COMPLETE) {
2097 fprintf(stdout, " RO_REVOKE_COMPLETE");
2099 fprintf(stdout, "\n");
2103 static int dump_record(uint32_t reqid, struct ctdb_ltdb_header *header,
2104 TDB_DATA key, TDB_DATA data, void *private_data)
2106 struct dump_record_state *state =
2107 (struct dump_record_state *)private_data;
2111 dump_tdb_data("key", key);
2112 dump_ltdb_header(header);
2113 dump_tdb_data("data", data);
2114 fprintf(stdout, "\n");
2119 static int control_catdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2120 int argc, const char **argv)
2122 struct ctdb_db_context *db;
2123 const char *db_name;
2126 struct dump_record_state state;
2133 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2137 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2140 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2146 ret = ctdb_db_traverse(mem_ctx, ctdb->ev, ctdb->client, db,
2147 ctdb->cmd_pnn, TIMEOUT(),
2148 dump_record, &state);
2150 printf("Dumped %u records\n", state.count);
2155 static int control_cattdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2156 int argc, const char **argv)
2158 struct ctdb_db_context *db;
2159 const char *db_name;
2162 struct dump_record_state state;
2169 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2173 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2176 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2181 ret = ctdb_db_traverse_local(db, true, true, dump_record, &state);
2183 printf("Dumped %u record(s)\n", state.count);
2188 static int control_getcapabilities(TALLOC_CTX *mem_ctx,
2189 struct ctdb_context *ctdb,
2190 int argc, const char **argv)
2196 usage("getcapabilities");
2199 ret = ctdb_ctrl_get_capabilities(mem_ctx, ctdb->ev, ctdb->client,
2200 ctdb->cmd_pnn, TIMEOUT(), &caps);
2205 if (options.machinereadable == 1) {
2206 printf("%s%s%s%s%s\n",
2208 "RECMASTER", options.sep,
2209 "LMASTER", options.sep);
2210 printf("%s%d%s%d%s\n", options.sep,
2211 !! (caps & CTDB_CAP_RECMASTER), options.sep,
2212 !! (caps & CTDB_CAP_LMASTER), options.sep);
2214 printf("RECMASTER: %s\n",
2215 (caps & CTDB_CAP_RECMASTER) ? "YES" : "NO");
2216 printf("LMASTER: %s\n",
2217 (caps & CTDB_CAP_LMASTER) ? "YES" : "NO");
2223 static int control_pnn(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2224 int argc, const char **argv)
2226 printf("%u\n", ctdb_client_pnn(ctdb->client));
2230 static int control_lvs(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2231 int argc, const char **argv)
2233 char *t, *lvs_helper = NULL;
2239 t = getenv("CTDB_LVS_HELPER");
2241 lvs_helper = talloc_strdup(mem_ctx, t);
2243 lvs_helper = talloc_asprintf(mem_ctx, "%s/ctdb_lvs",
2244 CTDB_HELPER_BINDIR);
2247 if (lvs_helper == NULL) {
2248 fprintf(stderr, "Unable to set LVS helper\n");
2252 return run_helper(mem_ctx, "LVS helper", lvs_helper, argc, argv);
2255 static int control_setdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2256 int argc, const char **argv)
2266 found = debug_level_parse(argv[0], &log_level);
2269 "Invalid debug level '%s'. Valid levels are:\n",
2271 fprintf(stderr, "\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
2275 ret = ctdb_ctrl_setdebug(mem_ctx, ctdb->ev, ctdb->client,
2276 ctdb->cmd_pnn, TIMEOUT(), log_level);
2284 static int control_getdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2285 int argc, const char **argv)
2288 const char *log_str;
2295 ret = ctdb_ctrl_getdebug(mem_ctx, ctdb->ev, ctdb->client,
2296 ctdb->cmd_pnn, TIMEOUT(), &loglevel);
2301 log_str = debug_level_to_string(loglevel);
2302 printf("%s\n", log_str);
2307 static int control_attach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2308 int argc, const char **argv)
2310 const char *db_name;
2311 uint8_t db_flags = 0;
2314 if (argc < 1 || argc > 2) {
2320 if (strcmp(argv[1], "persistent") == 0) {
2321 db_flags = CTDB_DB_FLAGS_PERSISTENT;
2322 } else if (strcmp(argv[1], "readonly") == 0) {
2323 db_flags = CTDB_DB_FLAGS_READONLY;
2324 } else if (strcmp(argv[1], "sticky") == 0) {
2325 db_flags = CTDB_DB_FLAGS_STICKY;
2326 } else if (strcmp(argv[1], "replicated") == 0) {
2327 db_flags = CTDB_DB_FLAGS_REPLICATED;
2333 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2342 static int control_detach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2343 int argc, const char **argv)
2345 const char *db_name;
2348 struct ctdb_node_map *nodemap;
2356 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2357 ctdb->cmd_pnn, TIMEOUT(), &recmode);
2362 if (recmode == CTDB_RECOVERY_ACTIVE) {
2363 fprintf(stderr, "Database cannot be detached"
2364 " when recovery is active\n");
2368 nodemap = get_nodemap(ctdb, false);
2369 if (nodemap == NULL) {
2373 for (i=0; i<nodemap->num; i++) {
2376 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
2379 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
2382 if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
2383 fprintf(stderr, "Database cannot be detached on"
2384 " inactive (stopped or banned) node %u\n",
2385 nodemap->node[i].pnn);
2389 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
2390 nodemap->node[i].pnn, TIMEOUT(),
2391 "AllowClientDBAttach", &value);
2394 "Unable to get tunable AllowClientDBAttach"
2395 " from node %u\n", nodemap->node[i].pnn);
2401 "Database access is still active on node %u."
2402 " Set AllowclientDBAttach=0 on all nodes.\n",
2403 nodemap->node[i].pnn);
2409 for (i=0; i<argc; i++) {
2410 if (! db_exists(mem_ctx, ctdb, argv[i], &db_id, &db_name,
2416 (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
2418 "Only volatile databases can be detached\n");
2422 ret = ctdb_detach(ctdb->ev, ctdb->client, TIMEOUT(), db_id);
2424 fprintf(stderr, "Database %s detach failed\n", db_name);
2432 static int control_dumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2433 int argc, const char **argv)
2435 const char *mem_str;
2439 ret = ctdb_ctrl_dump_memory(mem_ctx, ctdb->ev, ctdb->client,
2440 ctdb->cmd_pnn, TIMEOUT(), &mem_str);
2445 n = write(1, mem_str, strlen(mem_str)+1);
2446 if (n < 0 || n != strlen(mem_str)+1) {
2447 fprintf(stderr, "Failed to write talloc summary\n");
2454 static void dump_memory(uint64_t srvid, TDB_DATA data, void *private_data)
2456 bool *done = (bool *)private_data;
2459 n = write(1, data.dptr, data.dsize);
2460 if (n < 0 || n != data.dsize) {
2461 fprintf(stderr, "Failed to write talloc summary\n");
2467 static int control_rddumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2468 int argc, const char **argv)
2470 struct ctdb_srvid_message msg = { 0 };
2474 msg.pnn = ctdb->pnn;
2475 msg.srvid = next_srvid(ctdb);
2477 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2478 msg.srvid, dump_memory, &done);
2483 ret = ctdb_message_mem_dump(mem_ctx, ctdb->ev, ctdb->client,
2484 ctdb->cmd_pnn, &msg);
2489 ctdb_client_wait(ctdb->ev, &done);
2493 static int control_getpid(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2494 int argc, const char **argv)
2499 ret = ctdb_ctrl_get_pid(mem_ctx, ctdb->ev, ctdb->client,
2500 ctdb->cmd_pnn, TIMEOUT(), &pid);
2505 printf("%u\n", pid);
2509 static int check_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2510 const char *desc, uint32_t flag, bool set_flag)
2512 struct ctdb_node_map *nodemap;
2515 nodemap = get_nodemap(ctdb, false);
2516 if (nodemap == NULL) {
2520 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2521 if (set_flag == flag_is_set) {
2523 fprintf(stderr, "Node %u is already %s\n",
2524 ctdb->cmd_pnn, desc);
2526 fprintf(stderr, "Node %u is not %s\n",
2527 ctdb->cmd_pnn, desc);
2535 static void wait_for_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2536 uint32_t flag, bool set_flag)
2538 struct ctdb_node_map *nodemap;
2542 nodemap = get_nodemap(ctdb, true);
2543 if (nodemap == NULL) {
2545 "Failed to get nodemap, trying again\n");
2550 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2551 if (flag_is_set == set_flag) {
2559 static int ctdb_ctrl_modflags(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2560 struct ctdb_client_context *client,
2561 uint32_t destnode, struct timeval timeout,
2562 uint32_t set, uint32_t clear)
2564 struct ctdb_node_map *nodemap;
2565 struct ctdb_node_flag_change flag_change;
2566 struct ctdb_req_control request;
2570 ret = ctdb_ctrl_get_nodemap(mem_ctx, ev, client, destnode,
2571 tevent_timeval_zero(), &nodemap);
2576 flag_change.pnn = destnode;
2577 flag_change.old_flags = nodemap->node[destnode].flags;
2578 flag_change.new_flags = flag_change.old_flags | set;
2579 flag_change.new_flags &= ~clear;
2581 count = list_of_connected_nodes(nodemap, -1, mem_ctx, &pnn_list);
2586 ctdb_req_control_modify_flags(&request, &flag_change);
2587 ret = ctdb_client_control_multi(mem_ctx, ev, client, pnn_list, count,
2588 tevent_timeval_zero(), &request,
2593 struct ipreallocate_state {
2598 static void ipreallocate_handler(uint64_t srvid, TDB_DATA data,
2601 struct ipreallocate_state *state =
2602 (struct ipreallocate_state *)private_data;
2604 if (data.dsize != sizeof(int)) {
2609 state->status = *(int *)data.dptr;
2613 static int ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb)
2615 struct ctdb_srvid_message msg = { 0 };
2616 struct ipreallocate_state state;
2619 msg.pnn = ctdb->pnn;
2620 msg.srvid = next_srvid(ctdb);
2623 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2625 ipreallocate_handler, &state);
2631 ret = ctdb_message_takeover_run(mem_ctx, ctdb->ev,
2633 CTDB_BROADCAST_CONNECTED,
2639 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done,
2645 if (state.status >= 0) {
2654 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2659 static int control_disable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2660 int argc, const char **argv)
2668 ret = check_flags(mem_ctx, ctdb, "disabled",
2669 NODE_FLAGS_PERMANENTLY_DISABLED, true);
2674 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2675 ctdb->cmd_pnn, TIMEOUT(),
2676 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2679 "Failed to set DISABLED flag on node %u\n",
2684 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, true);
2685 return ipreallocate(mem_ctx, ctdb);
2688 static int control_enable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2689 int argc, const char **argv)
2697 ret = check_flags(mem_ctx, ctdb, "disabled",
2698 NODE_FLAGS_PERMANENTLY_DISABLED, false);
2703 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2704 ctdb->cmd_pnn, TIMEOUT(),
2705 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2707 fprintf(stderr, "Failed to reset DISABLED flag on node %u\n",
2712 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, false);
2713 return ipreallocate(mem_ctx, ctdb);
2716 static int control_stop(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2717 int argc, const char **argv)
2725 ret = check_flags(mem_ctx, ctdb, "stopped",
2726 NODE_FLAGS_STOPPED, true);
2731 ret = ctdb_ctrl_stop_node(mem_ctx, ctdb->ev, ctdb->client,
2732 ctdb->cmd_pnn, TIMEOUT());
2734 fprintf(stderr, "Failed to stop node %u\n", ctdb->cmd_pnn);
2738 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, true);
2739 return ipreallocate(mem_ctx, ctdb);
2742 static int control_continue(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2743 int argc, const char **argv)
2751 ret = check_flags(mem_ctx, ctdb, "stopped",
2752 NODE_FLAGS_STOPPED, false);
2757 ret = ctdb_ctrl_continue_node(mem_ctx, ctdb->ev, ctdb->client,
2758 ctdb->cmd_pnn, TIMEOUT());
2760 fprintf(stderr, "Failed to continue stopped node %u\n",
2765 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, false);
2766 return ipreallocate(mem_ctx, ctdb);
2769 static int control_ban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2770 int argc, const char **argv)
2772 struct ctdb_ban_state ban_state;
2779 ret = check_flags(mem_ctx, ctdb, "banned",
2780 NODE_FLAGS_BANNED, true);
2785 ban_state.pnn = ctdb->cmd_pnn;
2786 ban_state.time = strtoul(argv[0], NULL, 0);
2788 if (ban_state.time == 0) {
2789 fprintf(stderr, "Ban time cannot be zero\n");
2793 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2794 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2796 fprintf(stderr, "Failed to ban node %u\n", ctdb->cmd_pnn);
2800 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, true);
2801 return ipreallocate(mem_ctx, ctdb);
2805 static int control_unban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2806 int argc, const char **argv)
2808 struct ctdb_ban_state ban_state;
2815 ret = check_flags(mem_ctx, ctdb, "banned",
2816 NODE_FLAGS_BANNED, false);
2821 ban_state.pnn = ctdb->cmd_pnn;
2824 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2825 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2827 fprintf(stderr, "Failed to unban node %u\n", ctdb->cmd_pnn);
2831 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, false);
2832 return ipreallocate(mem_ctx, ctdb);
2836 static void wait_for_shutdown(void *private_data)
2838 bool *done = (bool *)private_data;
2843 static int control_shutdown(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2844 int argc, const char **argv)
2853 if (ctdb->pnn == ctdb->cmd_pnn) {
2854 ctdb_client_set_disconnect_callback(ctdb->client,
2859 ret = ctdb_ctrl_shutdown(mem_ctx, ctdb->ev, ctdb->client,
2860 ctdb->cmd_pnn, TIMEOUT());
2862 fprintf(stderr, "Unable to shutdown node %u\n", ctdb->cmd_pnn);
2866 if (ctdb->pnn == ctdb->cmd_pnn) {
2867 ctdb_client_wait(ctdb->ev, &done);
2873 static int get_generation(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2874 uint32_t *generation)
2878 struct ctdb_vnn_map *vnnmap;
2882 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2883 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
2885 fprintf(stderr, "Failed to find recovery master\n");
2889 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2890 recmaster, TIMEOUT(), &recmode);
2892 fprintf(stderr, "Failed to get recovery mode from node %u\n",
2897 if (recmode == CTDB_RECOVERY_ACTIVE) {
2902 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
2903 recmaster, TIMEOUT(), &vnnmap);
2905 fprintf(stderr, "Failed to get generation from node %u\n",
2910 if (vnnmap->generation == INVALID_GENERATION) {
2911 talloc_free(vnnmap);
2916 *generation = vnnmap->generation;
2917 talloc_free(vnnmap);
2922 static int control_recover(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2923 int argc, const char **argv)
2925 uint32_t generation, next_generation;
2932 ret = get_generation(mem_ctx, ctdb, &generation);
2937 ret = ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
2938 ctdb->cmd_pnn, TIMEOUT(),
2939 CTDB_RECOVERY_ACTIVE);
2941 fprintf(stderr, "Failed to set recovery mode active\n");
2946 ret = get_generation(mem_ctx, ctdb, &next_generation);
2949 "Failed to confirm end of recovery\n");
2953 if (next_generation != generation) {
2963 static int control_ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2964 int argc, const char **argv)
2967 usage("ipreallocate");
2970 return ipreallocate(mem_ctx, ctdb);
2973 static int control_isnotrecmaster(TALLOC_CTX *mem_ctx,
2974 struct ctdb_context *ctdb,
2975 int argc, const char **argv)
2981 usage("isnotrecmaster");
2984 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2985 ctdb->pnn, TIMEOUT(), &recmaster);
2987 fprintf(stderr, "Failed to get recmaster\n");
2991 if (recmaster != ctdb->pnn) {
2992 printf("this node is not the recmaster\n");
2996 printf("this node is the recmaster\n");
3000 static int control_gratarp(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3001 int argc, const char **argv)
3003 struct ctdb_addr_info addr_info;
3010 ret = ctdb_sock_addr_from_string(argv[0], &addr_info.addr, false);
3012 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3015 addr_info.iface = argv[1];
3017 ret = ctdb_ctrl_send_gratuitous_arp(mem_ctx, ctdb->ev, ctdb->client,
3018 ctdb->cmd_pnn, TIMEOUT(),
3021 fprintf(stderr, "Unable to send gratuitous arp from node %u\n",
3029 static int control_tickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3030 int argc, const char **argv)
3032 ctdb_sock_addr src, dst;
3035 if (argc != 0 && argc != 2) {
3040 struct ctdb_connection_list *clist;
3043 /* Client first but the src/dst logic is confused */
3044 ret = ctdb_connection_list_read(mem_ctx, false, &clist);
3050 for (i = 0; i < clist->num; i++) {
3051 ret = ctdb_sys_send_tcp(&clist->conn[i].src,
3052 &clist->conn[i].dst,
3061 if (num_failed > 0) {
3062 fprintf(stderr, "Failed to send %d tickles\n",
3071 ret = ctdb_sock_addr_from_string(argv[0], &src, true);
3073 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3077 ret = ctdb_sock_addr_from_string(argv[1], &dst, true);
3079 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3083 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
3085 fprintf(stderr, "Failed to send tickle ack\n");
3092 static int control_gettickles(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3093 int argc, const char **argv)
3095 ctdb_sock_addr addr;
3096 struct ctdb_tickle_list *tickles;
3100 if (argc < 1 || argc > 2) {
3101 usage("gettickles");
3105 port = strtoul(argv[1], NULL, 10);
3108 ret = ctdb_sock_addr_from_string(argv[0], &addr, false);
3110 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3113 ctdb_sock_addr_set_port(&addr, port);
3115 ret = ctdb_ctrl_get_tcp_tickle_list(mem_ctx, ctdb->ev, ctdb->client,
3116 ctdb->cmd_pnn, TIMEOUT(), &addr,
3119 fprintf(stderr, "Failed to get list of connections\n");
3123 if (options.machinereadable) {
3124 printf("%s%s%s%s%s%s%s%s%s\n",
3126 "Source IP", options.sep,
3127 "Port", options.sep,
3128 "Destiation IP", options.sep,
3129 "Port", options.sep);
3130 for (i=0; i<tickles->num; i++) {
3131 printf("%s%s%s%u%s%s%s%u%s\n", options.sep,
3132 ctdb_sock_addr_to_string(
3133 mem_ctx, &tickles->conn[i].src, false),
3135 ntohs(tickles->conn[i].src.ip.sin_port),
3137 ctdb_sock_addr_to_string(
3138 mem_ctx, &tickles->conn[i].dst, false),
3140 ntohs(tickles->conn[i].dst.ip.sin_port),
3144 printf("Connections for IP: %s\n",
3145 ctdb_sock_addr_to_string(mem_ctx,
3146 &tickles->addr, false));
3147 printf("Num connections: %u\n", tickles->num);
3148 for (i=0; i<tickles->num; i++) {
3149 printf("SRC: %s DST: %s\n",
3150 ctdb_sock_addr_to_string(
3151 mem_ctx, &tickles->conn[i].src, true),
3152 ctdb_sock_addr_to_string(
3153 mem_ctx, &tickles->conn[i].dst, true));
3157 talloc_free(tickles);
3161 typedef void (*clist_request_func)(struct ctdb_req_control *request,
3162 struct ctdb_connection *conn);
3164 typedef int (*clist_reply_func)(struct ctdb_reply_control *reply);
3166 struct process_clist_state {
3167 struct ctdb_connection_list *clist;
3169 int num_failed, num_total;
3170 clist_reply_func reply_func;
3173 static void process_clist_done(struct tevent_req *subreq);
3175 static struct tevent_req *process_clist_send(
3176 TALLOC_CTX *mem_ctx,
3177 struct ctdb_context *ctdb,
3178 struct ctdb_connection_list *clist,
3179 clist_request_func request_func,
3180 clist_reply_func reply_func)
3182 struct tevent_req *req, *subreq;
3183 struct process_clist_state *state;
3184 struct ctdb_req_control request;
3187 req = tevent_req_create(mem_ctx, &state, struct process_clist_state);
3192 state->clist = clist;
3193 state->reply_func = reply_func;
3195 for (i = 0; i < clist->num; i++) {
3196 request_func(&request, &clist->conn[i]);
3197 subreq = ctdb_client_control_send(state, ctdb->ev,
3198 ctdb->client, ctdb->cmd_pnn,
3199 TIMEOUT(), &request);
3200 if (tevent_req_nomem(subreq, req)) {
3201 return tevent_req_post(req, ctdb->ev);
3203 tevent_req_set_callback(subreq, process_clist_done, req);
3209 static void process_clist_done(struct tevent_req *subreq)
3211 struct tevent_req *req = tevent_req_callback_data(
3212 subreq, struct tevent_req);
3213 struct process_clist_state *state = tevent_req_data(
3214 req, struct process_clist_state);
3215 struct ctdb_reply_control *reply;
3219 status = ctdb_client_control_recv(subreq, NULL, state, &reply);
3220 TALLOC_FREE(subreq);
3222 state->num_failed += 1;
3226 ret = state->reply_func(reply);
3228 state->num_failed += 1;
3233 state->num_total += 1;
3234 if (state->num_total == state->clist->num) {
3235 tevent_req_done(req);
3239 static int process_clist_recv(struct tevent_req *req)
3241 struct process_clist_state *state = tevent_req_data(
3242 req, struct process_clist_state);
3244 return state->num_failed;
3247 static int control_addtickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3248 int argc, const char **argv)
3250 struct ctdb_connection conn;
3253 if (argc != 0 && argc != 2) {
3258 struct ctdb_connection_list *clist;
3259 struct tevent_req *req;
3261 /* Client first but the src/dst logic is confused */
3262 ret = ctdb_connection_list_read(mem_ctx, false, &clist);
3266 if (clist->num == 0) {
3270 req = process_clist_send(mem_ctx, ctdb, clist,
3271 ctdb_req_control_tcp_add_delayed_update,
3272 ctdb_reply_control_tcp_add_delayed_update);
3278 tevent_req_poll(req, ctdb->ev);
3281 ret = process_clist_recv(req);
3283 fprintf(stderr, "Failed to add %d tickles\n", ret);
3290 ret = ctdb_sock_addr_from_string(argv[0], &conn.src, true);
3292 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3295 ret = ctdb_sock_addr_from_string(argv[1], &conn.dst, true);
3297 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3301 ret = ctdb_ctrl_tcp_add_delayed_update(mem_ctx, ctdb->ev,
3302 ctdb->client, ctdb->cmd_pnn,
3305 fprintf(stderr, "Failed to register connection\n");
3312 static int control_deltickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3313 int argc, const char **argv)
3315 struct ctdb_connection conn;
3318 if (argc != 0 && argc != 2) {
3323 struct ctdb_connection_list *clist;
3324 struct tevent_req *req;
3326 /* Client first but the src/dst logic is confused */
3327 ret = ctdb_connection_list_read(mem_ctx, false, &clist);
3331 if (clist->num == 0) {
3335 req = process_clist_send(mem_ctx, ctdb, clist,
3336 ctdb_req_control_tcp_remove,
3337 ctdb_reply_control_tcp_remove);
3343 tevent_req_poll(req, ctdb->ev);
3346 ret = process_clist_recv(req);
3348 fprintf(stderr, "Failed to remove %d tickles\n", ret);
3355 ret = ctdb_sock_addr_from_string(argv[0], &conn.src, true);
3357 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3360 ret = ctdb_sock_addr_from_string(argv[1], &conn.dst, true);
3362 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3366 ret = ctdb_ctrl_tcp_remove(mem_ctx, ctdb->ev, ctdb->client,
3367 ctdb->cmd_pnn, TIMEOUT(), &conn);
3369 fprintf(stderr, "Failed to unregister connection\n");
3376 static int control_listnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3377 int argc, const char **argv)
3379 struct ctdb_node_map *nodemap;
3386 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
3387 if (nodemap == NULL) {
3391 for (i=0; i<nodemap->num; i++) {
3392 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
3396 if (options.machinereadable) {
3397 printf("%s%u%s%s%s\n", options.sep,
3398 nodemap->node[i].pnn, options.sep,
3399 ctdb_sock_addr_to_string(
3400 mem_ctx, &nodemap->node[i].addr, false),
3404 ctdb_sock_addr_to_string(
3405 mem_ctx, &nodemap->node[i].addr, false));
3412 static bool nodemap_identical(struct ctdb_node_map *nodemap1,
3413 struct ctdb_node_map *nodemap2)
3417 if (nodemap1->num != nodemap2->num) {
3421 for (i=0; i<nodemap1->num; i++) {
3422 struct ctdb_node_and_flags *n1, *n2;
3424 n1 = &nodemap1->node[i];
3425 n2 = &nodemap2->node[i];
3427 if ((n1->pnn != n2->pnn) ||
3428 (n1->flags != n2->flags) ||
3429 ! ctdb_sock_addr_same_ip(&n1->addr, &n2->addr)) {
3437 static int check_node_file_changes(TALLOC_CTX *mem_ctx,
3438 struct ctdb_node_map *nm,
3439 struct ctdb_node_map *fnm,
3443 bool check_failed = false;
3447 for (i=0; i<nm->num; i++) {
3448 if (i >= fnm->num) {
3450 "Node %u (%s) missing from nodes file\n",
3452 ctdb_sock_addr_to_string(
3453 mem_ctx, &nm->node[i].addr, false));
3454 check_failed = true;
3457 if (nm->node[i].flags & NODE_FLAGS_DELETED &&
3458 fnm->node[i].flags & NODE_FLAGS_DELETED) {
3459 /* Node remains deleted */
3463 if (! (nm->node[i].flags & NODE_FLAGS_DELETED) &&
3464 ! (fnm->node[i].flags & NODE_FLAGS_DELETED)) {
3465 /* Node not newly nor previously deleted */
3466 if (! ctdb_same_ip(&nm->node[i].addr,
3467 &fnm->node[i].addr)) {
3469 "Node %u has changed IP address"
3470 " (was %s, now %s)\n",
3472 ctdb_sock_addr_to_string(
3474 &nm->node[i].addr, false),
3475 ctdb_sock_addr_to_string(
3477 &fnm->node[i].addr, false));
3478 check_failed = true;
3480 if (nm->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3482 "WARNING: Node %u is disconnected."
3483 " You MUST fix this node manually!\n",
3490 if (fnm->node[i].flags & NODE_FLAGS_DELETED) {
3491 /* Node is being deleted */
3492 printf("Node %u is DELETED\n", nm->node[i].pnn);
3494 if (! (nm->node[i].flags & NODE_FLAGS_DISCONNECTED)) {
3496 "ERROR: Node %u is still connected\n",
3498 check_failed = true;
3503 if (nm->node[i].flags & NODE_FLAGS_DELETED) {
3504 /* Node was previously deleted */
3505 printf("Node %u is UNDELETED\n", nm->node[i].pnn);
3512 "ERROR: Nodes will not be reloaded due to previous error\n");
3516 /* Leftover nodes in file are NEW */
3517 for (; i < fnm->num; i++) {
3518 printf("Node %u is NEW\n", fnm->node[i].pnn);
3525 struct disable_recoveries_state {
3533 static void disable_recoveries_handler(uint64_t srvid, TDB_DATA data,
3536 struct disable_recoveries_state *state =
3537 (struct disable_recoveries_state *)private_data;
3540 if (data.dsize != sizeof(int)) {
3545 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
3546 ret = *(int *)data.dptr;
3548 state->status = ret;
3552 for (i=0; i<state->node_count; i++) {
3553 if (state->pnn_list[i] == ret) {
3554 state->reply[i] = true;
3560 for (i=0; i<state->node_count; i++) {
3561 if (! state->reply[i]) {
3562 state->done = false;
3568 static int disable_recoveries(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3569 uint32_t timeout, uint32_t *pnn_list, int count)
3571 struct ctdb_disable_message disable = { 0 };
3572 struct disable_recoveries_state state;
3575 disable.pnn = ctdb->pnn;
3576 disable.srvid = next_srvid(ctdb);
3577 disable.timeout = timeout;
3579 state.pnn_list = pnn_list;
3580 state.node_count = count;
3583 state.reply = talloc_zero_array(mem_ctx, bool, count);
3584 if (state.reply == NULL) {
3588 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
3590 disable_recoveries_handler,
3596 for (i=0; i<count; i++) {
3597 ret = ctdb_message_disable_recoveries(mem_ctx, ctdb->ev,
3606 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
3608 fprintf(stderr, "Timed out waiting to disable recoveries\n");
3610 ret = (state.status >= 0 ? 0 : 1);
3614 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
3615 disable.srvid, &state);
3619 static int control_reloadnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3620 int argc, const char **argv)
3622 struct ctdb_node_map *nodemap = NULL;
3623 struct ctdb_node_map *file_nodemap;
3624 struct ctdb_node_map *remote_nodemap;
3625 struct ctdb_req_control request;
3626 struct ctdb_reply_control **reply;
3632 nodemap = get_nodemap(ctdb, false);
3633 if (nodemap == NULL) {
3637 file_nodemap = read_nodes_file(mem_ctx, ctdb->pnn);
3638 if (file_nodemap == NULL) {
3642 for (i=0; i<nodemap->num; i++) {
3643 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3647 ret = ctdb_ctrl_get_nodes_file(mem_ctx, ctdb->ev, ctdb->client,
3648 nodemap->node[i].pnn, TIMEOUT(),
3652 "ERROR: Failed to get nodes file from node %u\n",
3653 nodemap->node[i].pnn);
3657 if (! nodemap_identical(file_nodemap, remote_nodemap)) {
3659 "ERROR: Nodes file on node %u differs"
3660 " from current node (%u)\n",
3661 nodemap->node[i].pnn, ctdb->pnn);
3666 ret = check_node_file_changes(mem_ctx, nodemap, file_nodemap, &reload);
3672 fprintf(stderr, "No change in nodes file,"
3673 " skipping unnecessary reload\n");
3677 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
3678 mem_ctx, &pnn_list);
3680 fprintf(stderr, "Memory allocation error\n");
3684 ret = disable_recoveries(mem_ctx, ctdb, 2*options.timelimit,
3687 fprintf(stderr, "Failed to disable recoveries\n");
3691 ctdb_req_control_reload_nodes_file(&request);
3692 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3693 pnn_list, count, TIMEOUT(),
3694 &request, NULL, &reply);
3696 bool failed = false;
3698 for (i=0; i<count; i++) {
3699 ret = ctdb_reply_control_reload_nodes_file(reply[i]);
3702 "Node %u failed to reload nodes\n",
3709 "You MUST fix failed nodes manually!\n");
3713 ret = disable_recoveries(mem_ctx, ctdb, 0, pnn_list, count);
3715 fprintf(stderr, "Failed to enable recoveries\n");
3722 static int moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3723 ctdb_sock_addr *addr, uint32_t pnn)
3725 struct ctdb_public_ip_list *pubip_list;
3726 struct ctdb_public_ip pubip;
3727 struct ctdb_node_map *nodemap;
3728 struct ctdb_req_control request;
3732 ret = ctdb_message_disable_ip_check(mem_ctx, ctdb->ev, ctdb->client,
3733 CTDB_BROADCAST_CONNECTED,
3734 2*options.timelimit);
3736 fprintf(stderr, "Failed to disable IP check\n");
3740 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3741 pnn, TIMEOUT(), false, &pubip_list);
3743 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3748 for (i=0; i<pubip_list->num; i++) {
3749 if (ctdb_same_ip(addr, &pubip_list->ip[i].addr)) {
3754 if (i == pubip_list->num) {
3755 fprintf(stderr, "Node %u CANNOT host IP address %s\n",
3756 pnn, ctdb_sock_addr_to_string(mem_ctx, addr, false));
3760 nodemap = get_nodemap(ctdb, false);
3761 if (nodemap == NULL) {
3765 count = list_of_active_nodes(nodemap, pnn, mem_ctx, &pnn_list);
3767 fprintf(stderr, "Memory allocation error\n");
3773 ctdb_req_control_release_ip(&request, &pubip);
3775 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3776 pnn_list, count, TIMEOUT(),
3777 &request, NULL, NULL);
3779 fprintf(stderr, "Failed to release IP on nodes\n");
3783 ret = ctdb_ctrl_takeover_ip(mem_ctx, ctdb->ev, ctdb->client,
3784 pnn, TIMEOUT(), &pubip);
3786 fprintf(stderr, "Failed to takeover IP on node %u\n", pnn);
3793 static int control_moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3794 int argc, const char **argv)
3796 ctdb_sock_addr addr;
3798 int ret, retries = 0;
3804 ret = ctdb_sock_addr_from_string(argv[0], &addr, false);
3806 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3810 pnn = strtoul(argv[1], NULL, 10);
3811 if (pnn == CTDB_UNKNOWN_PNN) {
3812 fprintf(stderr, "Invalid PNN %s\n", argv[1]);
3816 while (retries < 5) {
3817 ret = moveip(mem_ctx, ctdb, &addr, pnn);
3827 fprintf(stderr, "Failed to move IP %s to node %u\n",
3835 static int rebalancenode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3840 ret = ctdb_message_rebalance_node(mem_ctx, ctdb->ev, ctdb->client,
3841 CTDB_BROADCAST_CONNECTED, pnn);
3844 "Failed to ask recovery master to distribute IPs\n");
3851 static int control_addip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3852 int argc, const char **argv)
3854 ctdb_sock_addr addr;
3855 struct ctdb_public_ip_list *pubip_list;
3856 struct ctdb_addr_info addr_info;
3858 int ret, i, retries = 0;
3864 if (! parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
3865 fprintf(stderr, "Invalid IP/Mask %s\n", argv[0]);
3869 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3870 ctdb->cmd_pnn, TIMEOUT(),
3871 false, &pubip_list);
3873 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3878 for (i=0; i<pubip_list->num; i++) {
3879 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3880 fprintf(stderr, "Node already knows about IP %s\n",
3881 ctdb_sock_addr_to_string(mem_ctx,
3887 addr_info.addr = addr;
3888 addr_info.mask = mask;
3889 addr_info.iface = argv[1];
3891 while (retries < 5) {
3892 ret = ctdb_ctrl_add_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3893 ctdb->cmd_pnn, TIMEOUT(),
3904 fprintf(stderr, "Failed to add public IP to node %u."
3905 " Giving up\n", ctdb->cmd_pnn);
3909 ret = rebalancenode(mem_ctx, ctdb, ctdb->cmd_pnn);
3917 static int control_delip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3918 int argc, const char **argv)
3920 ctdb_sock_addr addr;
3921 struct ctdb_public_ip_list *pubip_list;
3922 struct ctdb_addr_info addr_info;
3929 ret = ctdb_sock_addr_from_string(argv[0], &addr, false);
3931 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3935 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3936 ctdb->cmd_pnn, TIMEOUT(),
3937 false, &pubip_list);
3939 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3944 for (i=0; i<pubip_list->num; i++) {
3945 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3950 if (i == pubip_list->num) {
3951 fprintf(stderr, "Node does not know about IP address %s\n",
3952 ctdb_sock_addr_to_string(mem_ctx, &addr, false));
3956 addr_info.addr = addr;
3958 addr_info.iface = NULL;
3960 ret = ctdb_ctrl_del_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3961 ctdb->cmd_pnn, TIMEOUT(), &addr_info);
3963 fprintf(stderr, "Failed to delete public IP from node %u\n",
3971 #define DB_VERSION 3
3972 #define MAX_DB_NAME 64
3973 #define MAX_REC_BUFFER_SIZE (100*1000)
3976 unsigned long version;
3978 unsigned long flags;
3981 char name[MAX_DB_NAME];
3984 struct backup_state {
3985 TALLOC_CTX *mem_ctx;
3986 struct ctdb_rec_buffer *recbuf;
3989 unsigned int nbuf, nrec;
3992 static int backup_handler(uint32_t reqid, struct ctdb_ltdb_header *header,
3993 TDB_DATA key, TDB_DATA data, void *private_data)
3995 struct backup_state *state = (struct backup_state *)private_data;
3999 if (state->recbuf == NULL) {
4000 state->recbuf = ctdb_rec_buffer_init(state->mem_ctx,
4002 if (state->recbuf == NULL) {
4007 ret = ctdb_rec_buffer_add(state->recbuf, state->recbuf, reqid,
4013 len = ctdb_rec_buffer_len(state->recbuf);
4014 if (len < MAX_REC_BUFFER_SIZE) {
4018 ret = ctdb_rec_buffer_write(state->recbuf, state->fd);
4020 fprintf(stderr, "Failed to write records to backup file\n");
4025 state->nrec += state->recbuf->count;
4026 TALLOC_FREE(state->recbuf);
4031 static int control_backupdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4032 int argc, const char **argv)
4034 const char *db_name;
4035 struct ctdb_db_context *db;
4038 struct backup_state state;
4039 struct db_header db_hdr;
4046 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4050 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4053 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4057 fd = open(argv[1], O_RDWR|O_CREAT, 0600);
4060 fprintf(stderr, "Failed to open file %s for writing\n",
4065 /* Write empty header first */
4066 ZERO_STRUCT(db_hdr);
4067 ret = write(fd, &db_hdr, sizeof(struct db_header));
4071 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4075 state.mem_ctx = mem_ctx;
4076 state.recbuf = NULL;
4081 ret = ctdb_db_traverse_local(db, true, false, backup_handler, &state);
4083 fprintf(stderr, "Failed to collect records from DB %s\n",
4089 if (state.recbuf != NULL) {
4090 ret = ctdb_rec_buffer_write(state.recbuf, state.fd);
4093 "Failed to write records to backup file\n");
4099 state.nrec += state.recbuf->count;
4100 TALLOC_FREE(state.recbuf);
4103 db_hdr.version = DB_VERSION;
4104 db_hdr.timestamp = time(NULL);
4105 db_hdr.flags = db_flags;
4106 db_hdr.nbuf = state.nbuf;
4107 db_hdr.nrec = state.nrec;
4108 strncpy(db_hdr.name, db_name, MAX_DB_NAME-1);
4110 lseek(fd, 0, SEEK_SET);
4111 ret = write(fd, &db_hdr, sizeof(struct db_header));
4115 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4120 printf("Database backed up to %s\n", argv[1]);
4124 static int control_restoredb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4125 int argc, const char **argv)
4127 const char *db_name = NULL;
4128 struct ctdb_db_context *db;
4129 struct db_header db_hdr;
4130 struct ctdb_node_map *nodemap;
4131 struct ctdb_req_control request;
4132 struct ctdb_reply_control **reply;
4133 struct ctdb_transdb wipedb;
4134 struct ctdb_pulldb_ext pulldb;
4135 struct ctdb_rec_buffer *recbuf;
4136 uint32_t generation;
4144 if (argc < 1 || argc > 2) {
4148 fd = open(argv[0], O_RDONLY, 0600);
4151 fprintf(stderr, "Failed to open file %s for reading\n",
4160 n = read(fd, &db_hdr, sizeof(struct db_header));
4164 fprintf(stderr, "Failed to read db header from file %s\n",
4168 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4170 if (db_hdr.version != DB_VERSION) {
4172 "Wrong version of backup file, expected %u, got %lu\n",
4173 DB_VERSION, db_hdr.version);
4178 if (db_name == NULL) {
4179 db_name = db_hdr.name;
4182 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4183 localtime(&db_hdr.timestamp));
4184 printf("Restoring database %s from backup @ %s\n", db_name, timebuf);
4186 db_flags = db_hdr.flags & 0xff;
4187 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4190 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4195 nodemap = get_nodemap(ctdb, false);
4196 if (nodemap == NULL) {
4197 fprintf(stderr, "Failed to get nodemap\n");
4202 ret = get_generation(mem_ctx, ctdb, &generation);
4204 fprintf(stderr, "Failed to get current generation\n");
4209 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4216 wipedb.db_id = ctdb_db_id(db);
4217 wipedb.tid = generation;
4219 ctdb_req_control_db_freeze(&request, wipedb.db_id);
4220 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4221 ctdb->client, pnn_list, count,
4222 TIMEOUT(), &request, NULL, NULL);
4228 ctdb_req_control_db_transaction_start(&request, &wipedb);
4229 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4230 pnn_list, count, TIMEOUT(),
4231 &request, NULL, NULL);
4236 ctdb_req_control_wipe_database(&request, &wipedb);
4237 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4238 pnn_list, count, TIMEOUT(),
4239 &request, NULL, NULL);
4244 pulldb.db_id = ctdb_db_id(db);
4246 pulldb.srvid = SRVID_CTDB_PUSHDB;
4248 ctdb_req_control_db_push_start(&request, &pulldb);
4249 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4250 pnn_list, count, TIMEOUT(),
4251 &request, NULL, NULL);
4256 for (i=0; i<db_hdr.nbuf; i++) {
4257 struct ctdb_req_message message;
4261 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4266 data.dsize = ctdb_rec_buffer_len(recbuf);
4267 data.dptr = talloc_size(mem_ctx, data.dsize);
4268 if (data.dptr == NULL) {
4272 ctdb_rec_buffer_push(recbuf, data.dptr, &np);
4274 message.srvid = pulldb.srvid;
4275 message.data.data = data;
4277 ret = ctdb_client_message_multi(mem_ctx, ctdb->ev,
4285 talloc_free(recbuf);
4286 talloc_free(data.dptr);
4289 ctdb_req_control_db_push_confirm(&request, pulldb.db_id);
4290 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4291 pnn_list, count, TIMEOUT(),
4292 &request, NULL, &reply);
4297 for (i=0; i<count; i++) {
4298 uint32_t num_records;
4300 ret = ctdb_reply_control_db_push_confirm(reply[i],
4303 fprintf(stderr, "Invalid response from node %u\n",
4308 if (num_records != db_hdr.nrec) {
4309 fprintf(stderr, "Node %u received %u of %lu records\n",
4310 pnn_list[i], num_records, db_hdr.nrec);
4315 ctdb_req_control_db_set_healthy(&request, wipedb.db_id);
4316 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4317 pnn_list, count, TIMEOUT(),
4318 &request, NULL, NULL);
4323 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4324 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4325 pnn_list, count, TIMEOUT(),
4326 &request, NULL, NULL);
4331 ctdb_req_control_db_thaw(&request, wipedb.db_id);
4332 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4333 ctdb->client, pnn_list, count,
4334 TIMEOUT(), &request, NULL, NULL);
4339 printf("Database %s restored\n", db_name);
4346 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4347 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4351 struct dumpdbbackup_state {
4352 ctdb_rec_parser_func_t parser;
4353 struct dump_record_state sub_state;
4356 static int dumpdbbackup_handler(uint32_t reqid,
4357 struct ctdb_ltdb_header *header,
4358 TDB_DATA key, TDB_DATA data,
4361 struct dumpdbbackup_state *state =
4362 (struct dumpdbbackup_state *)private_data;
4363 struct ctdb_ltdb_header hdr;
4366 ret = ctdb_ltdb_header_extract(&data, &hdr);
4371 return state->parser(reqid, &hdr, key, data, &state->sub_state);
4374 static int control_dumpdbbackup(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4375 int argc, const char **argv)
4377 struct db_header db_hdr;
4379 struct dumpdbbackup_state state;
4384 usage("dumpbackup");
4387 fd = open(argv[0], O_RDONLY, 0600);
4390 fprintf(stderr, "Failed to open file %s for reading\n",
4395 n = read(fd, &db_hdr, sizeof(struct db_header));
4399 fprintf(stderr, "Failed to read db header from file %s\n",
4403 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4405 if (db_hdr.version != DB_VERSION) {
4407 "Wrong version of backup file, expected %u, got %lu\n",
4408 DB_VERSION, db_hdr.version);
4413 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4414 localtime(&db_hdr.timestamp));
4415 printf("Dumping database %s from backup @ %s\n",
4416 db_hdr.name, timebuf);
4418 state.parser = dump_record;
4419 state.sub_state.count = 0;
4421 for (i=0; i<db_hdr.nbuf; i++) {
4422 struct ctdb_rec_buffer *recbuf;
4424 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4426 fprintf(stderr, "Failed to read records\n");
4431 ret = ctdb_rec_buffer_traverse(recbuf, dumpdbbackup_handler,
4434 fprintf(stderr, "Failed to dump records\n");
4441 printf("Dumped %u record(s)\n", state.sub_state.count);
4445 static int control_wipedb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4446 int argc, const char **argv)
4448 const char *db_name;
4449 struct ctdb_db_context *db;
4452 struct ctdb_node_map *nodemap;
4453 struct ctdb_req_control request;
4454 struct ctdb_transdb wipedb;
4455 uint32_t generation;
4463 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4467 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4470 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4474 nodemap = get_nodemap(ctdb, false);
4475 if (nodemap == NULL) {
4476 fprintf(stderr, "Failed to get nodemap\n");
4480 ret = get_generation(mem_ctx, ctdb, &generation);
4482 fprintf(stderr, "Failed to get current generation\n");
4486 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4492 ctdb_req_control_db_freeze(&request, db_id);
4493 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4494 ctdb->client, pnn_list, count,
4495 TIMEOUT(), &request, NULL, NULL);
4500 wipedb.db_id = db_id;
4501 wipedb.tid = generation;
4503 ctdb_req_control_db_transaction_start(&request, &wipedb);
4504 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4505 pnn_list, count, TIMEOUT(),
4506 &request, NULL, NULL);
4511 ctdb_req_control_wipe_database(&request, &wipedb);
4512 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4513 pnn_list, count, TIMEOUT(),
4514 &request, NULL, NULL);
4519 ctdb_req_control_db_set_healthy(&request, db_id);
4520 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4521 pnn_list, count, TIMEOUT(),
4522 &request, NULL, NULL);
4527 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4528 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4529 pnn_list, count, TIMEOUT(),
4530 &request, NULL, NULL);
4535 ctdb_req_control_db_thaw(&request, db_id);
4536 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4537 ctdb->client, pnn_list, count,
4538 TIMEOUT(), &request, NULL, NULL);
4543 printf("Database %s wiped\n", db_name);
4548 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4549 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4553 static int control_recmaster(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4554 int argc, const char **argv)
4559 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
4560 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
4565 printf("%u\n", recmaster);
4569 static int control_event(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4570 int argc, const char **argv)
4572 char *t, *event_helper = NULL;
4573 char *eventd_socket = NULL;
4574 const char **new_argv;
4577 t = getenv("CTDB_EVENT_HELPER");
4579 event_helper = talloc_strdup(mem_ctx, t);
4581 event_helper = talloc_asprintf(mem_ctx, "%s/ctdb_event",
4582 CTDB_HELPER_BINDIR);
4585 if (event_helper == NULL) {
4586 fprintf(stderr, "Unable to set event daemon helper\n");
4590 t = getenv("CTDB_SOCKET");
4592 eventd_socket = talloc_asprintf(mem_ctx, "%s/eventd.sock",
4595 eventd_socket = talloc_asprintf(mem_ctx, "%s/eventd.sock",
4599 if (eventd_socket == NULL) {
4600 fprintf(stderr, "Unable to set event daemon socket\n");
4604 new_argv = talloc_array(mem_ctx, const char *, argc + 1);
4605 if (new_argv == NULL) {
4606 fprintf(stderr, "Memory allocation error\n");
4610 new_argv[0] = eventd_socket;
4611 for (i=0; i<argc; i++) {
4612 new_argv[i+1] = argv[i];
4615 return run_helper(mem_ctx, "event daemon helper", event_helper,
4619 static int control_scriptstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4620 int argc, const char **argv)
4622 const char *new_argv[3];
4625 usage("scriptstatus");
4628 new_argv[0] = "status";
4629 new_argv[1] = (argc == 0) ? "monitor" : argv[0];
4632 (void) control_event(mem_ctx, ctdb, 2, new_argv);
4636 static int control_natgw(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4637 int argc, const char **argv)
4639 char *t, *natgw_helper = NULL;
4645 t = getenv("CTDB_NATGW_HELPER");
4647 natgw_helper = talloc_strdup(mem_ctx, t);
4649 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4650 CTDB_HELPER_BINDIR);
4653 if (natgw_helper == NULL) {
4654 fprintf(stderr, "Unable to set NAT gateway helper\n");
4658 return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
4663 * Find the PNN of the current node
4664 * discover the pnn by loading the nodes file and try to bind
4665 * to all addresses one at a time until the ip address is found.
4667 static bool find_node_xpnn(TALLOC_CTX *mem_ctx, uint32_t *pnn)
4669 struct ctdb_node_map *nodemap;
4672 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
4673 if (nodemap == NULL) {
4677 for (i=0; i<nodemap->num; i++) {
4678 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
4681 if (ctdb_sys_have_ip(&nodemap->node[i].addr)) {
4683 *pnn = nodemap->node[i].pnn;
4685 talloc_free(nodemap);
4690 fprintf(stderr, "Failed to detect PNN of the current node.\n");
4691 talloc_free(nodemap);
4695 static int control_getreclock(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4696 int argc, const char **argv)
4698 const char *reclock;
4702 usage("getreclock");
4705 ret = ctdb_ctrl_get_reclock_file(mem_ctx, ctdb->ev, ctdb->client,
4706 ctdb->cmd_pnn, TIMEOUT(), &reclock);
4711 if (reclock != NULL) {
4712 printf("%s\n", reclock);
4718 static int control_setlmasterrole(TALLOC_CTX *mem_ctx,
4719 struct ctdb_context *ctdb,
4720 int argc, const char **argv)
4722 uint32_t lmasterrole = 0;
4726 usage("setlmasterrole");
4729 if (strcmp(argv[0], "on") == 0) {
4731 } else if (strcmp(argv[0], "off") == 0) {
4734 usage("setlmasterrole");
4737 ret = ctdb_ctrl_set_lmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4738 ctdb->cmd_pnn, TIMEOUT(), lmasterrole);
4746 static int control_setrecmasterrole(TALLOC_CTX *mem_ctx,
4747 struct ctdb_context *ctdb,
4748 int argc, const char **argv)
4750 uint32_t recmasterrole = 0;
4754 usage("setrecmasterrole");
4757 if (strcmp(argv[0], "on") == 0) {
4759 } else if (strcmp(argv[0], "off") == 0) {
4762 usage("setrecmasterrole");
4765 ret = ctdb_ctrl_set_recmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4766 ctdb->cmd_pnn, TIMEOUT(),
4775 static int control_setdbreadonly(TALLOC_CTX *mem_ctx,
4776 struct ctdb_context *ctdb,
4777 int argc, const char **argv)
4784 usage("setdbreadonly");
4787 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
4791 if (db_flags & (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
4792 fprintf(stderr, "READONLY can be set only on volatile DB\n");
4796 ret = ctdb_ctrl_set_db_readonly(mem_ctx, ctdb->ev, ctdb->client,
4797 ctdb->cmd_pnn, TIMEOUT(), db_id);
4805 static int control_setdbsticky(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4806 int argc, const char **argv)
4813 usage("setdbsticky");
4816 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
4820 if (db_flags & (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
4821 fprintf(stderr, "STICKY can be set only on volatile DB\n");
4825 ret = ctdb_ctrl_set_db_sticky(mem_ctx, ctdb->ev, ctdb->client,
4826 ctdb->cmd_pnn, TIMEOUT(), db_id);
4834 static int control_pfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4835 int argc, const char **argv)
4837 const char *db_name;
4838 struct ctdb_db_context *db;
4839 struct ctdb_transaction_handle *h;
4844 if (argc < 2 || argc > 3) {
4848 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
4853 (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED))) {
4854 fprintf(stderr, "Transactions not supported on DB %s\n",
4859 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4862 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4866 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
4868 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
4872 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
4873 TIMEOUT(), db, true, &h);
4875 fprintf(stderr, "Failed to start transaction on db %s\n",
4880 ret = ctdb_transaction_fetch_record(h, key, mem_ctx, &data);
4882 fprintf(stderr, "Failed to read record for key %s\n",
4884 ctdb_transaction_cancel(h);
4888 printf("%.*s\n", (int)data.dsize, data.dptr);
4890 ctdb_transaction_cancel(h);
4894 static int control_pstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4895 int argc, const char **argv)
4897 const char *db_name;
4898 struct ctdb_db_context *db;
4899 struct ctdb_transaction_handle *h;
4908 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
4913 (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED))) {
4914 fprintf(stderr, "Transactions not supported on DB %s\n",
4919 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4922 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4926 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
4928 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
4932 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
4934 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
4938 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
4939 TIMEOUT(), db, false, &h);
4941 fprintf(stderr, "Failed to start transaction on db %s\n",
4946 ret = ctdb_transaction_store_record(h, key, data);
4948 fprintf(stderr, "Failed to store record for key %s\n",
4950 ctdb_transaction_cancel(h);
4954 ret = ctdb_transaction_commit(h);
4956 fprintf(stderr, "Failed to commit transaction on db %s\n",
4958 ctdb_transaction_cancel(h);
4965 static int control_pdelete(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4966 int argc, const char **argv)
4968 const char *db_name;
4969 struct ctdb_db_context *db;
4970 struct ctdb_transaction_handle *h;
4979 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
4984 (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED))) {
4985 fprintf(stderr, "Transactions not supported on DB %s\n",
4990 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4993 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4997 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
4999 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5003 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5004 TIMEOUT(), db, false, &h);
5006 fprintf(stderr, "Failed to start transaction on db %s\n",
5011 ret = ctdb_transaction_delete_record(h, key);
5013 fprintf(stderr, "Failed to delete record for key %s\n",
5015 ctdb_transaction_cancel(h);
5019 ret = ctdb_transaction_commit(h);
5021 fprintf(stderr, "Failed to commit transaction on db %s\n",
5023 ctdb_transaction_cancel(h);
5030 static int ptrans_parse_string(TALLOC_CTX *mem_ctx, const char **ptr, TDB_DATA *data)
5038 /* Skip whitespace */
5039 n = strspn(*ptr, " \t");
5043 /* Quoted ASCII string - no wide characters! */
5045 n = strcspn(t, "\"");
5048 ret = str_to_data(t, n, mem_ctx, data);
5055 fprintf(stderr, "Unmatched \" in input %s\n", *ptr);
5059 fprintf(stderr, "Unsupported input format in %s\n", *ptr);
5066 #define MAX_LINE_SIZE 1024
5068 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
5069 TDB_DATA *key, TDB_DATA *value)
5071 char line [MAX_LINE_SIZE]; /* FIXME: make this more flexible? */
5075 ptr = fgets(line, MAX_LINE_SIZE, file);
5081 ret = ptrans_parse_string(mem_ctx, &ptr, key);
5082 if (ret != 0 || ptr == NULL || key->dptr == NULL) {
5083 /* Line Ignored but not EOF */
5089 ret = ptrans_parse_string(mem_ctx, &ptr, value);
5091 /* Line Ignored but not EOF */
5092 talloc_free(key->dptr);
5100 static int control_ptrans(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5101 int argc, const char **argv)
5103 const char *db_name;
5104 struct ctdb_db_context *db;
5105 struct ctdb_transaction_handle *h;
5108 TDB_DATA key, value;
5111 if (argc < 1 || argc > 2) {
5115 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5120 (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED))) {
5121 fprintf(stderr, "Transactions not supported on DB %s\n",
5127 file = fopen(argv[1], "r");
5129 fprintf(stderr, "Failed to open file %s\n", argv[1]);
5136 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5139 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5143 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5144 TIMEOUT(), db, false, &h);
5146 fprintf(stderr, "Failed to start transaction on db %s\n",
5151 while (ptrans_get_key_value(mem_ctx, file, &key, &value)) {
5152 if (key.dsize != 0) {
5153 ret = ctdb_transaction_store_record(h, key, value);
5155 fprintf(stderr, "Failed to store record\n");
5156 ctdb_transaction_cancel(h);
5159 talloc_free(key.dptr);
5160 talloc_free(value.dptr);
5164 ret = ctdb_transaction_commit(h);
5166 fprintf(stderr, "Failed to commit transaction on db %s\n",
5168 ctdb_transaction_cancel(h);
5172 if (file != stdin) {
5178 static int control_tfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5179 int argc, const char **argv)
5181 struct tdb_context *tdb;
5183 struct ctdb_ltdb_header header;
5186 if (argc < 2 || argc > 3) {
5190 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5192 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5196 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5198 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5203 data = tdb_fetch(tdb, key);
5204 if (data.dptr == NULL) {
5205 fprintf(stderr, "No record for key %s\n", argv[1]);
5210 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
5211 fprintf(stderr, "Invalid record for key %s\n", argv[1]);
5222 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
5224 fprintf(stderr, "Failed to open output file %s\n",
5229 nwritten = sys_write(fd, data.dptr, data.dsize);
5230 if (nwritten != data.dsize) {
5231 fprintf(stderr, "Failed to write record to file\n");
5240 ret = ctdb_ltdb_header_extract(&data, &header);
5242 fprintf(stderr, "Failed to parse header from data\n");
5246 dump_ltdb_header(&header);
5247 dump_tdb_data("data", data);
5252 static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5253 int argc, const char **argv)
5255 struct tdb_context *tdb;
5256 TDB_DATA key, data[2], value;
5257 struct ctdb_ltdb_header header;
5258 uint8_t header_buf[sizeof(struct ctdb_ltdb_header)];
5262 if (argc < 3 || argc > 5) {
5266 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5268 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5272 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5274 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5279 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &value);
5281 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5286 ZERO_STRUCT(header);
5289 header.rsn = (uint64_t)strtoull(argv[3], NULL, 0);
5292 header.dmaster = (uint32_t)atol(argv[4]);
5295 header.flags = (uint32_t)atol(argv[5]);
5298 ctdb_ltdb_header_push(&header, header_buf, &np);
5301 data[0].dptr = header_buf;
5303 data[1].dsize = value.dsize;
5304 data[1].dptr = value.dptr;
5306 ret = tdb_storev(tdb, key, data, 2, TDB_REPLACE);
5308 fprintf(stderr, "Failed to write record %s to file %s\n",
5317 static int control_readkey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5318 int argc, const char **argv)
5320 const char *db_name;
5321 struct ctdb_db_context *db;
5322 struct ctdb_record_handle *h;
5325 bool readonly = false;
5328 if (argc < 2 || argc > 3) {
5333 if (strcmp(argv[2], "readonly") == 0) {
5340 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5344 if (db_flags & (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
5345 fprintf(stderr, "DB %s is not a volatile database\n",
5350 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5353 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5357 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5359 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5363 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5364 db, key, readonly, &h, NULL, &data);
5366 fprintf(stderr, "Failed to read record for key %s\n",
5369 printf("Data: size:%zu ptr:[%.*s]\n", data.dsize,
5370 (int)data.dsize, data.dptr);
5377 static int control_writekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5378 int argc, const char **argv)
5380 const char *db_name;
5381 struct ctdb_db_context *db;
5382 struct ctdb_record_handle *h;
5391 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5395 if (db_flags & (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
5396 fprintf(stderr, "DB %s is not a volatile database\n",
5401 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5404 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5408 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5410 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5414 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5416 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5420 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5421 db, key, false, &h, NULL, NULL);
5423 fprintf(stderr, "Failed to lock record for key %s\n", argv[0]);
5427 ret = ctdb_store_record(h, data);
5429 fprintf(stderr, "Failed to store record for key %s\n",
5437 static int control_deletekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5438 int argc, const char **argv)
5440 const char *db_name;
5441 struct ctdb_db_context *db;
5442 struct ctdb_record_handle *h;
5451 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5455 if (db_flags & (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
5456 fprintf(stderr, "DB %s is not a volatile database\n",
5461 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5464 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5468 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5470 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5474 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5475 db, key, false, &h, NULL, &data);
5477 fprintf(stderr, "Failed to fetch record for key %s\n",
5482 ret = ctdb_delete_record(h);
5484 fprintf(stderr, "Failed to delete record for key %s\n",
5492 static int control_checktcpport(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5493 int argc, const char **argv)
5495 struct sockaddr_in sin;
5501 usage("chktcpport");
5504 port = atoi(argv[0]);
5506 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
5508 fprintf(stderr, "Failed to open local socket\n");
5512 v = fcntl(s, F_GETFL, 0);
5513 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK)) {
5514 fprintf(stderr, "Unable to set socket non-blocking\n");
5519 bzero(&sin, sizeof(sin));
5520 sin.sin_family = AF_INET;
5521 sin.sin_port = htons(port);
5522 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
5525 fprintf(stderr, "Failed to bind to TCP port %u\n", port);
5532 static int control_getdbseqnum(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5533 int argc, const char **argv)
5536 const char *db_name;
5541 usage("getdbseqnum");
5544 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5548 ret = ctdb_ctrl_get_db_seqnum(mem_ctx, ctdb->ev, ctdb->client,
5549 ctdb->cmd_pnn, TIMEOUT(), db_id,
5552 fprintf(stderr, "Failed to get sequence number for DB %s\n",
5557 printf("0x%"PRIx64"\n", seqnum);
5561 static int control_nodestatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5562 int argc, const char **argv)
5564 const char *nodestring = NULL;
5565 struct ctdb_node_map *nodemap;
5567 bool print_hdr = false;
5570 usage("nodestatus");
5574 nodestring = argv[0];
5575 if (strcmp(nodestring, "all") == 0) {
5580 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap)) {
5584 if (options.machinereadable) {
5585 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5587 print_nodemap(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn, print_hdr);
5591 for (i=0; i<nodemap->num; i++) {
5592 ret |= nodemap->node[i].flags;
5601 } db_stats_fields[] = {
5602 #define DBSTATISTICS_FIELD(n) { #n, offsetof(struct ctdb_db_statistics, n) }
5603 DBSTATISTICS_FIELD(db_ro_delegations),
5604 DBSTATISTICS_FIELD(db_ro_revokes),
5605 DBSTATISTICS_FIELD(locks.num_calls),
5606 DBSTATISTICS_FIELD(locks.num_current),
5607 DBSTATISTICS_FIELD(locks.num_pending),
5608 DBSTATISTICS_FIELD(locks.num_failed),
5611 static void print_dbstatistics(const char *db_name,
5612 struct ctdb_db_statistics *s)
5615 const char *prefix = NULL;
5618 printf("DB Statistics %s\n", db_name);
5620 for (i=0; i<ARRAY_SIZE(db_stats_fields); i++) {
5621 if (strchr(db_stats_fields[i].name, '.') != NULL) {
5622 preflen = strcspn(db_stats_fields[i].name, ".") + 1;
5624 strncmp(prefix, db_stats_fields[i].name, preflen) != 0) {
5625 prefix = db_stats_fields[i].name;
5626 printf(" %*.*s\n", preflen-1, preflen-1,
5627 db_stats_fields[i].name);
5632 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
5633 db_stats_fields[i].name+preflen, preflen ? 0 : 4, "",
5634 *(uint32_t *)(db_stats_fields[i].offset+(uint8_t *)s));
5637 printf(" hop_count_buckets:");
5638 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5639 printf(" %d", s->hop_count_bucket[i]);
5643 printf(" lock_buckets:");
5644 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5645 printf(" %d", s->locks.buckets[i]);
5649 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5650 "locks_latency MIN/AVG/MAX",
5651 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
5652 s->locks.latency.max, s->locks.latency.num);
5654 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5655 "vacuum_latency MIN/AVG/MAX",
5656 s->vacuum.latency.min, LATENCY_AVG(s->vacuum.latency),
5657 s->vacuum.latency.max, s->vacuum.latency.num);
5659 printf(" Num Hot Keys: %d\n", s->num_hot_keys);
5660 for (i=0; i<s->num_hot_keys; i++) {
5662 printf(" Count:%d Key:", s->hot_keys[i].count);
5663 for (j=0; j<s->hot_keys[i].key.dsize; j++) {
5664 printf("%02x", s->hot_keys[i].key.dptr[j] & 0xff);
5670 static int control_dbstatistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5671 int argc, const char **argv)
5674 const char *db_name;
5675 struct ctdb_db_statistics *dbstats;
5679 usage("dbstatistics");
5682 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5686 ret = ctdb_ctrl_get_db_statistics(mem_ctx, ctdb->ev, ctdb->client,
5687 ctdb->cmd_pnn, TIMEOUT(), db_id,
5690 fprintf(stderr, "Failed to get statistics for DB %s\n",
5695 print_dbstatistics(db_name, dbstats);
5699 struct disable_takeover_runs_state {
5707 static void disable_takeover_run_handler(uint64_t srvid, TDB_DATA data,
5710 struct disable_takeover_runs_state *state =
5711 (struct disable_takeover_runs_state *)private_data;
5714 if (data.dsize != sizeof(int)) {
5719 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
5720 ret = *(int *)data.dptr;
5722 state->status = ret;
5726 for (i=0; i<state->node_count; i++) {
5727 if (state->pnn_list[i] == ret) {
5728 state->reply[i] = true;
5734 for (i=0; i<state->node_count; i++) {
5735 if (! state->reply[i]) {
5736 state->done = false;
5742 static int disable_takeover_runs(TALLOC_CTX *mem_ctx,
5743 struct ctdb_context *ctdb, uint32_t timeout,
5744 uint32_t *pnn_list, int count)
5746 struct ctdb_disable_message disable = { 0 };
5747 struct disable_takeover_runs_state state;
5750 disable.pnn = ctdb->pnn;
5751 disable.srvid = next_srvid(ctdb);
5752 disable.timeout = timeout;
5754 state.pnn_list = pnn_list;
5755 state.node_count = count;
5758 state.reply = talloc_zero_array(mem_ctx, bool, count);
5759 if (state.reply == NULL) {
5763 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
5765 disable_takeover_run_handler,
5771 for (i=0; i<count; i++) {
5772 ret = ctdb_message_disable_takeover_runs(mem_ctx, ctdb->ev,
5781 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
5783 fprintf(stderr, "Timed out waiting to disable takeover runs\n");
5785 ret = (state.status >= 0 ? 0 : 1);
5789 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
5790 disable.srvid, &state);
5794 static int control_reloadips(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5795 int argc, const char **argv)
5797 const char *nodestring = NULL;
5798 struct ctdb_node_map *nodemap, *nodemap2;
5799 struct ctdb_req_control request;
5800 uint32_t *pnn_list, *pnn_list2;
5801 int ret, count, count2;
5808 nodestring = argv[0];
5811 nodemap = get_nodemap(ctdb, false);
5812 if (nodemap == NULL) {
5816 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap2)) {
5820 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
5821 mem_ctx, &pnn_list);
5823 fprintf(stderr, "Memory allocation error\n");
5827 count2 = list_of_active_nodes(nodemap2, CTDB_UNKNOWN_PNN,
5828 mem_ctx, &pnn_list2);
5830 fprintf(stderr, "Memory allocation error\n");
5834 /* Disable takeover runs on all connected nodes. A reply
5835 * indicating success is needed from each node so all nodes
5836 * will need to be active.
5838 * A check could be added to not allow reloading of IPs when
5839 * there are disconnected nodes. However, this should
5840 * probably be left up to the administrator.
5842 ret = disable_takeover_runs(mem_ctx, ctdb, 2*options.timelimit,
5845 fprintf(stderr, "Failed to disable takeover runs\n");
5849 /* Now tell all the desired nodes to reload their public IPs.
5850 * Keep trying this until it succeeds. This assumes all
5851 * failures are transient, which might not be true...
5853 ctdb_req_control_reload_public_ips(&request);
5854 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
5855 pnn_list2, count2, TIMEOUT(),
5856 &request, NULL, NULL);
5858 fprintf(stderr, "Failed to reload IPs on some nodes.\n");
5861 /* It isn't strictly necessary to wait until takeover runs are
5862 * re-enabled but doing so can't hurt.
5864 ret = disable_takeover_runs(mem_ctx, ctdb, 0, pnn_list, count);
5866 fprintf(stderr, "Failed to enable takeover runs\n");
5870 return ipreallocate(mem_ctx, ctdb);
5873 static int control_ipiface(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5874 int argc, const char **argv)
5876 ctdb_sock_addr addr;
5884 ret = ctdb_sock_addr_from_string(argv[0], &addr, false);
5886 fprintf(stderr, "Failed to Parse IP %s\n", argv[0]);
5890 iface = ctdb_sys_find_ifname(&addr);
5891 if (iface == NULL) {
5892 fprintf(stderr, "Failed to find interface for IP %s\n",
5902 static const struct ctdb_cmd {
5904 int (*fn)(TALLOC_CTX *, struct ctdb_context *, int, const char **);
5905 bool without_daemon; /* can be run without daemon running ? */
5906 bool remote; /* can be run on remote nodes */
5909 } ctdb_commands[] = {
5910 { "version", control_version, true, false,
5911 "show version of ctdb", NULL },
5912 { "status", control_status, false, true,
5913 "show node status", NULL },
5914 { "uptime", control_uptime, false, true,
5915 "show node uptime", NULL },
5916 { "ping", control_ping, false, true,
5917 "ping all nodes", NULL },
5918 { "runstate", control_runstate, false, true,
5919 "get/check runstate of a node",
5920 "[setup|first_recovery|startup|running]" },
5921 { "getvar", control_getvar, false, true,
5922 "get a tunable variable", "<name>" },
5923 { "setvar", control_setvar, false, true,
5924 "set a tunable variable", "<name> <value>" },
5925 { "listvars", control_listvars, false, true,
5926 "list tunable variables", NULL },
5927 { "statistics", control_statistics, false, true,
5928 "show ctdb statistics", NULL },
5929 { "statisticsreset", control_statistics_reset, false, true,
5930 "reset ctdb statistics", NULL },
5931 { "stats", control_stats, false, true,
5932 "show rolling statistics", "[count]" },
5933 { "ip", control_ip, false, true,
5934 "show public ips", "[all]" },
5935 { "ipinfo", control_ipinfo, false, true,
5936 "show public ip details", "<ip>" },
5937 { "ifaces", control_ifaces, false, true,
5938 "show interfaces", NULL },
5939 { "setifacelink", control_setifacelink, false, true,
5940 "set interface link status", "<iface> up|down" },
5941 { "process-exists", control_process_exists, false, true,
5942 "check if a process exists on a node", "<pid> [<srvid>]" },
5943 { "getdbmap", control_getdbmap, false, true,
5944 "show attached databases", NULL },
5945 { "getdbstatus", control_getdbstatus, false, true,
5946 "show database status", "<dbname|dbid>" },
5947 { "catdb", control_catdb, false, false,
5948 "dump cluster-wide ctdb database", "<dbname|dbid>" },
5949 { "cattdb", control_cattdb, false, false,
5950 "dump local ctdb database", "<dbname|dbid>" },
5951 { "getcapabilities", control_getcapabilities, false, true,
5952 "show node capabilities", NULL },
5953 { "pnn", control_pnn, false, false,
5954 "show the pnn of the currnet node", NULL },
5955 { "lvs", control_lvs, false, false,
5956 "show lvs configuration", "master|list|status" },
5957 { "setdebug", control_setdebug, false, true,
5958 "set debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
5959 { "getdebug", control_getdebug, false, true,
5960 "get debug level", NULL },
5961 { "attach", control_attach, false, false,
5962 "attach a database", "<dbname> [persistent|replicated]" },
5963 { "detach", control_detach, false, false,
5964 "detach database(s)", "<dbname|dbid> ..." },
5965 { "dumpmemory", control_dumpmemory, false, true,
5966 "dump ctdbd memory map", NULL },
5967 { "rddumpmemory", control_rddumpmemory, false, true,
5968 "dump recoverd memory map", NULL },
5969 { "getpid", control_getpid, false, true,
5970 "get ctdbd process ID", NULL },
5971 { "disable", control_disable, false, true,
5972 "disable a node", NULL },
5973 { "enable", control_enable, false, true,
5974 "enable a node", NULL },
5975 { "stop", control_stop, false, true,
5976 "stop a node", NULL },
5977 { "continue", control_continue, false, true,
5978 "continue a stopped node", NULL },
5979 { "ban", control_ban, false, true,
5980 "ban a node", "<bantime>"},
5981 { "unban", control_unban, false, true,
5982 "unban a node", NULL },
5983 { "shutdown", control_shutdown, false, true,
5984 "shutdown ctdb daemon", NULL },
5985 { "recover", control_recover, false, true,
5986 "force recovery", NULL },
5987 { "sync", control_ipreallocate, false, true,
5988 "run ip reallocation (deprecated)", NULL },
5989 { "ipreallocate", control_ipreallocate, false, true,
5990 "run ip reallocation", NULL },
5991 { "isnotrecmaster", control_isnotrecmaster, false, false,
5992 "check if local node is the recmaster", NULL },
5993 { "gratarp", control_gratarp, false, true,
5994 "send a gratuitous arp", "<ip> <interface>" },
5995 { "tickle", control_tickle, true, false,
5996 "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5997 { "gettickles", control_gettickles, false, true,
5998 "get the list of tickles", "<ip> [<port>]" },
5999 { "addtickle", control_addtickle, false, true,
6000 "add a tickle", "<ip>:<port> <ip>:<port>" },
6001 { "deltickle", control_deltickle, false, true,
6002 "delete a tickle", "<ip>:<port> <ip>:<port>" },
6003 { "listnodes", control_listnodes, true, true,
6004 "list nodes in the cluster", NULL },
6005 { "reloadnodes", control_reloadnodes, false, false,
6006 "reload the nodes file all nodes", NULL },
6007 { "moveip", control_moveip, false, false,
6008 "move an ip address to another node", "<ip> <node>" },
6009 { "addip", control_addip, false, true,
6010 "add an ip address to a node", "<ip/mask> <iface>" },
6011 { "delip", control_delip, false, true,
6012 "delete an ip address from a node", "<ip>" },
6013 { "backupdb", control_backupdb, false, false,
6014 "backup a database into a file", "<dbname|dbid> <file>" },
6015 { "restoredb", control_restoredb, false, false,
6016 "restore a database from a file", "<file> [dbname]" },
6017 { "dumpdbbackup", control_dumpdbbackup, true, false,
6018 "dump database from a backup file", "<file>" },
6019 { "wipedb", control_wipedb, false, false,
6020 "wipe the contents of a database.", "<dbname|dbid>"},
6021 { "recmaster", control_recmaster, false, true,
6022 "show the pnn for the recovery master", NULL },
6023 { "event", control_event, true, false,
6024 "event and event script commands", NULL },
6025 { "scriptstatus", control_scriptstatus, true, false,
6026 "show event script status",
6027 "[init|setup|startup|monitor|takeip|releaseip|ipreallocated]" },
6028 { "natgw", control_natgw, false, false,
6029 "show natgw configuration", "master|list|status" },
6030 { "getreclock", control_getreclock, false, true,
6031 "get recovery lock file", NULL },
6032 { "setlmasterrole", control_setlmasterrole, false, true,
6033 "set LMASTER role", "on|off" },
6034 { "setrecmasterrole", control_setrecmasterrole, false, true,
6035 "set RECMASTER role", "on|off"},
6036 { "setdbreadonly", control_setdbreadonly, false, true,
6037 "enable readonly records", "<dbname|dbid>" },
6038 { "setdbsticky", control_setdbsticky, false, true,
6039 "enable sticky records", "<dbname|dbid>"},
6040 { "pfetch", control_pfetch, false, false,
6041 "fetch record from persistent database", "<dbname|dbid> <key> [<file>]" },
6042 { "pstore", control_pstore, false, false,
6043 "write record to persistent database", "<dbname|dbid> <key> <value>" },
6044 { "pdelete", control_pdelete, false, false,
6045 "delete record from persistent database", "<dbname|dbid> <key>" },
6046 { "ptrans", control_ptrans, false, false,
6047 "update a persistent database (from file or stdin)", "<dbname|dbid> [<file>]" },
6048 { "tfetch", control_tfetch, false, true,
6049 "fetch a record", "<tdb-file> <key> [<file>]" },
6050 { "tstore", control_tstore, false, true,
6051 "store a record", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6052 { "readkey", control_readkey, false, false,
6053 "read value of a database key", "<dbname|dbid> <key> [readonly]" },
6054 { "writekey", control_writekey, false, false,
6055 "write value for a database key", "<dbname|dbid> <key> <value>" },
6056 { "deletekey", control_deletekey, false, false,
6057 "delete a database key", "<dbname|dbid> <key>" },
6058 { "checktcpport", control_checktcpport, true, false,
6059 "check if a service is bound to a specific tcp port or not", "<port>" },
6060 { "getdbseqnum", control_getdbseqnum, false, false,
6061 "get database sequence number", "<dbname|dbid>" },
6062 { "nodestatus", control_nodestatus, false, true,
6063 "show and return node status", "[all|<pnn-list>]" },
6064 { "dbstatistics", control_dbstatistics, false, true,
6065 "show database statistics", "<dbname|dbid>" },
6066 { "reloadips", control_reloadips, false, false,
6067 "reload the public addresses file", "[all|<pnn-list>]" },
6068 { "ipiface", control_ipiface, true, false,
6069 "Find the interface an ip address is hosted on", "<ip>" },
6072 static const struct ctdb_cmd *match_command(const char *command)
6074 const struct ctdb_cmd *cmd;
6077 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6078 cmd = &ctdb_commands[i];
6079 if (strlen(command) == strlen(cmd->name) &&
6080 strncmp(command, cmd->name, strlen(command)) == 0) {
6090 * Show usage message
6092 static void usage_full(void)
6096 poptPrintHelp(pc, stdout, 0);
6097 printf("\nCommands:\n");
6098 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6099 printf(" %-15s %-27s %s\n",
6100 ctdb_commands[i].name,
6101 ctdb_commands[i].args ? ctdb_commands[i].args : "",
6102 ctdb_commands[i].msg);
6106 static void usage(const char *command)
6108 const struct ctdb_cmd *cmd;
6110 if (command == NULL) {
6115 cmd = match_command(command);
6119 poptPrintUsage(pc, stdout, 0);
6120 printf("\nCommands:\n");
6121 printf(" %-15s %-27s %s\n",
6122 cmd->name, cmd->args ? cmd->args : "", cmd->msg);
6128 struct poptOption cmdline_options[] = {
6130 { "socket", 's', POPT_ARG_STRING, &options.socket, 0,
6131 "CTDB socket path", "filename" },
6132 { "debug", 'd', POPT_ARG_STRING, &options.debuglevelstr, 0,
6134 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0,
6135 "timelimit (in seconds)" },
6136 { "node", 'n', POPT_ARG_INT, &options.pnn, 0,
6137 "node specification - integer" },
6138 { NULL, 'Y', POPT_ARG_NONE, &options.machinereadable, 0,
6139 "enable machine readable output", NULL },
6140 { "separator", 'x', POPT_ARG_STRING, &options.sep, 0,
6141 "specify separator for machine readable output", "CHAR" },
6142 { NULL, 'X', POPT_ARG_NONE, &options.machineparsable, 0,
6143 "enable machine parsable output with separator |", NULL },
6144 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0,
6145 "enable verbose output", NULL },
6146 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0,
6147 "die if runtime exceeds this limit (in seconds)" },
6151 static int process_command(const struct ctdb_cmd *cmd, int argc,
6154 TALLOC_CTX *tmp_ctx;
6155 struct ctdb_context *ctdb;
6158 uint64_t srvid_offset;
6160 tmp_ctx = talloc_new(NULL);
6161 if (tmp_ctx == NULL) {
6162 fprintf(stderr, "Memory allocation error\n");
6166 if (cmd->without_daemon) {
6167 if (options.pnn != -1) {
6169 "Cannot specify node for command %s\n",
6174 ret = cmd->fn(tmp_ctx, NULL, argc-1, argv+1);
6175 talloc_free(tmp_ctx);
6179 ctdb = talloc_zero(tmp_ctx, struct ctdb_context);
6181 fprintf(stderr, "Memory allocation error\n");
6185 ctdb->ev = tevent_context_init(ctdb);
6186 if (ctdb->ev == NULL) {
6187 fprintf(stderr, "Failed to initialize tevent\n");
6191 ret = ctdb_client_init(ctdb, ctdb->ev, options.socket, &ctdb->client);
6193 fprintf(stderr, "Failed to connect to CTDB daemon (%s)\n",
6196 if (!find_node_xpnn(ctdb, NULL)) {
6197 fprintf(stderr, "Is this node part of CTDB cluster?\n");
6202 ctdb->pnn = ctdb_client_pnn(ctdb->client);
6203 srvid_offset = getpid() & 0xFFFF;
6204 ctdb->srvid = SRVID_CTDB_TOOL | (srvid_offset << 16);
6206 if (options.pnn != -1) {
6207 status = verify_pnn(ctdb, options.pnn);
6212 ctdb->cmd_pnn = options.pnn;
6214 ctdb->cmd_pnn = ctdb->pnn;
6217 if (! cmd->remote && ctdb->pnn != ctdb->cmd_pnn) {
6218 fprintf(stderr, "Node cannot be specified for command %s\n",
6223 ret = cmd->fn(tmp_ctx, ctdb, argc-1, argv+1);
6224 talloc_free(tmp_ctx);
6228 talloc_free(tmp_ctx);
6232 static void signal_handler(int sig)
6234 fprintf(stderr, "Maximum runtime exceeded - exiting\n");
6237 static void alarm_handler(int sig)
6239 /* Kill any child processes */
6240 signal(SIGTERM, signal_handler);
6246 int main(int argc, const char *argv[])
6249 const char **extra_argv;
6251 const struct ctdb_cmd *cmd;
6252 const char *ctdb_socket;
6258 /* Set default options */
6259 options.socket = CTDB_SOCKET;
6260 options.debuglevelstr = NULL;
6261 options.timelimit = 10;
6263 options.maxruntime = 0;
6266 ctdb_socket = getenv("CTDB_SOCKET");
6267 if (ctdb_socket != NULL) {
6268 options.socket = ctdb_socket;
6271 pc = poptGetContext(argv[0], argc, argv, cmdline_options,
6272 POPT_CONTEXT_KEEP_FIRST);
6273 while ((opt = poptGetNextOpt(pc)) != -1) {
6274 fprintf(stderr, "Invalid option %s: %s\n",
6275 poptBadOption(pc, 0), poptStrerror(opt));
6279 if (options.maxruntime == 0) {
6280 const char *ctdb_timeout;
6282 ctdb_timeout = getenv("CTDB_TIMEOUT");
6283 if (ctdb_timeout != NULL) {
6284 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6286 options.maxruntime = 120;
6289 if (options.maxruntime <= 120) {
6290 /* default timeout is 120 seconds */
6291 options.maxruntime = 120;
6294 if (options.machineparsable) {
6295 options.machinereadable = 1;
6298 /* setup the remaining options for the commands */
6300 extra_argv = poptGetArgs(pc);
6303 while (extra_argv[extra_argc]) extra_argc++;
6306 if (extra_argc < 1) {
6310 cmd = match_command(extra_argv[0]);
6312 fprintf(stderr, "Unknown command '%s'\n", extra_argv[0]);
6316 /* Enable logging */
6317 setup_logging("ctdb", DEBUG_STDERR);
6318 if (debug_level_parse(options.debuglevelstr, &loglevel)) {
6319 DEBUGLEVEL = loglevel;
6321 DEBUGLEVEL = DEBUG_ERR;
6324 signal(SIGALRM, alarm_handler);
6325 alarm(options.maxruntime);
6327 ret = process_command(cmd, extra_argc, extra_argv);
6332 (void)poptFreeContext(pc);