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 "ctdb_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 "common/system.h"
43 #include "client/client.h"
45 #define TIMEOUT() timeval_current_ofs(options.timelimit, 0)
47 #define SRVID_CTDB_TOOL (CTDB_SRVID_TOOL_RANGE | 0x0001000000000000LL)
48 #define SRVID_CTDB_PUSHDB (CTDB_SRVID_TOOL_RANGE | 0x0002000000000000LL)
52 const char *debuglevelstr;
60 int printemptyrecords;
67 static poptContext pc;
70 struct tevent_context *ev;
71 struct ctdb_client_context *client;
72 struct ctdb_node_map *nodemap;
73 uint32_t pnn, cmd_pnn;
77 static void usage(const char *command);
83 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
85 return (tv2->tv_sec - tv->tv_sec) +
86 (tv2->tv_usec - tv->tv_usec) * 1.0e-6;
89 static struct ctdb_node_and_flags *get_node_by_pnn(
90 struct ctdb_node_map *nodemap,
95 for (i=0; i<nodemap->num; i++) {
96 if (nodemap->node[i].pnn == pnn) {
97 return &nodemap->node[i];
103 static const char *pretty_print_flags(TALLOC_CTX *mem_ctx, uint32_t flags)
105 static const struct {
109 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
110 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
111 { NODE_FLAGS_BANNED, "BANNED" },
112 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
113 { NODE_FLAGS_DELETED, "DELETED" },
114 { NODE_FLAGS_STOPPED, "STOPPED" },
115 { NODE_FLAGS_INACTIVE, "INACTIVE" },
117 char *flags_str = NULL;
120 for (i=0; i<ARRAY_SIZE(flag_names); i++) {
121 if (flags & flag_names[i].flag) {
122 if (flags_str == NULL) {
123 flags_str = talloc_asprintf(mem_ctx,
124 "%s", flag_names[i].name);
126 flags_str = talloc_asprintf_append(flags_str,
127 "|%s", flag_names[i].name);
129 if (flags_str == NULL) {
130 return "OUT-OF-MEMORY";
134 if (flags_str == NULL) {
141 static uint64_t next_srvid(struct ctdb_context *ctdb)
148 * Get consistent nodemap information.
150 * If nodemap is already cached, use that. If not get it.
151 * If the current node is BANNED, then get nodemap from "better" node.
153 static struct ctdb_node_map *get_nodemap(struct ctdb_context *ctdb, bool force)
156 struct ctdb_node_map *nodemap;
157 struct ctdb_node_and_flags *node;
158 uint32_t current_node;
162 TALLOC_FREE(ctdb->nodemap);
165 if (ctdb->nodemap != NULL) {
166 return ctdb->nodemap;
169 tmp_ctx = talloc_new(ctdb);
170 if (tmp_ctx == NULL) {
174 current_node = ctdb->pnn;
176 ret = ctdb_ctrl_get_nodemap(tmp_ctx, ctdb->ev, ctdb->client,
177 current_node, TIMEOUT(), &nodemap);
179 fprintf(stderr, "Failed to get nodemap from node %u\n",
184 node = get_node_by_pnn(nodemap, current_node);
185 if (node->flags & NODE_FLAGS_BANNED) {
188 current_node = (current_node + 1) % nodemap->num;
189 node = get_node_by_pnn(nodemap, current_node);
191 (NODE_FLAGS_DELETED|NODE_FLAGS_DISCONNECTED))) {
194 } while (current_node != ctdb->pnn);
196 if (current_node == ctdb->pnn) {
197 /* Tried all nodes in the cluster */
198 fprintf(stderr, "Warning: All nodes are banned.\n");
205 ctdb->nodemap = talloc_steal(ctdb, nodemap);
209 talloc_free(tmp_ctx);
213 static bool verify_pnn(struct ctdb_context *ctdb, int pnn)
215 struct ctdb_node_map *nodemap;
223 nodemap = get_nodemap(ctdb, false);
224 if (nodemap == NULL) {
229 for (i=0; i<nodemap->num; i++) {
230 if (nodemap->node[i].pnn == pnn) {
236 fprintf(stderr, "Node %u does not exist\n", pnn);
240 if (nodemap->node[i].flags &
241 (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED)) {
242 fprintf(stderr, "Node %u has status %s\n", pnn,
243 pretty_print_flags(ctdb, nodemap->node[i].flags));
250 static struct ctdb_node_map *talloc_nodemap(TALLOC_CTX *mem_ctx,
251 struct ctdb_node_map *nodemap)
253 struct ctdb_node_map *nodemap2;
255 nodemap2 = talloc_zero(mem_ctx, struct ctdb_node_map);
256 if (nodemap2 == NULL) {
260 nodemap2->node = talloc_array(nodemap2, struct ctdb_node_and_flags,
262 if (nodemap2->node == NULL) {
263 talloc_free(nodemap2);
271 * Get the number and the list of matching nodes
273 * nodestring := NULL | all | pnn,[pnn,...]
275 * If nodestring is NULL, use the current node.
277 static bool parse_nodestring(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
278 const char *nodestring,
279 struct ctdb_node_map **out)
281 struct ctdb_node_map *nodemap, *nodemap2;
282 struct ctdb_node_and_flags *node;
285 nodemap = get_nodemap(ctdb, false);
286 if (nodemap == NULL) {
290 nodemap2 = talloc_nodemap(mem_ctx, nodemap);
291 if (nodemap2 == NULL) {
295 if (nodestring == NULL) {
296 for (i=0; i<nodemap->num; i++) {
297 if (nodemap->node[i].pnn == ctdb->cmd_pnn) {
298 nodemap2->node[0] = nodemap->node[i];
307 if (strcmp(nodestring, "all") == 0) {
308 for (i=0; i<nodemap->num; i++) {
309 nodemap2->node[i] = nodemap->node[i];
311 nodemap2->num = nodemap->num;
317 ns = talloc_strdup(mem_ctx, nodestring);
322 tok = strtok(ns, ",");
323 while (tok != NULL) {
327 pnn = (uint32_t)strtoul(tok, &endptr, 0);
328 if (pnn == 0 && tok == endptr) {
329 fprintf(stderr, "Invalid node %s\n", tok);
333 node = get_node_by_pnn(nodemap, pnn);
335 fprintf(stderr, "Node %u does not exist\n",
340 nodemap2->node[nodemap2->num] = *node;
343 tok = strtok(NULL, ",");
352 /* Compare IP address */
353 static bool ctdb_same_ip(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
357 if (ip1->sa.sa_family != ip2->sa.sa_family) {
361 switch (ip1->sa.sa_family) {
363 ret = (memcmp(&ip1->ip.sin_addr, &ip2->ip.sin_addr,
364 sizeof(struct in_addr)) == 0);
368 ret = (memcmp(&ip1->ip6.sin6_addr, &ip2->ip6.sin6_addr,
369 sizeof(struct in6_addr)) == 0);
376 /* Append a node to a node map with given address and flags */
377 static bool node_map_add(struct ctdb_node_map *nodemap,
378 const char *nstr, uint32_t flags)
382 struct ctdb_node_and_flags *n;
384 if (! parse_ip(nstr, NULL, 0, &addr)) {
385 fprintf(stderr, "Invalid IP address %s\n", nstr);
390 nodemap->node = talloc_realloc(nodemap, nodemap->node,
391 struct ctdb_node_and_flags, num+1);
392 if (nodemap->node == NULL) {
396 n = &nodemap->node[num];
401 nodemap->num = num+1;
405 /* Read a nodes file into a node map */
406 static struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
412 struct ctdb_node_map *nodemap;
414 nodemap = talloc_zero(mem_ctx, struct ctdb_node_map);
415 if (nodemap == NULL) {
419 lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
424 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
428 for (i=0; i<nlines; i++) {
434 /* strip leading spaces */
435 while((*node == ' ') || (*node == '\t')) {
441 /* strip trailing spaces */
443 ((node[len-1] == ' ') || (node[len-1] == '\t')))
453 /* A "deleted" node is a node that is
454 commented out in the nodes file. This is
455 used instead of removing a line, which
456 would cause subsequent nodes to change
458 flags = NODE_FLAGS_DELETED;
459 node = discard_const("0.0.0.0");
463 if (! node_map_add(nodemap, node, flags)) {
465 TALLOC_FREE(nodemap);
474 static struct ctdb_node_map *read_nodes_file(TALLOC_CTX *mem_ctx, uint32_t pnn)
476 struct ctdb_node_map *nodemap;
478 const char *nodes_list = NULL;
480 if (pnn != CTDB_UNKNOWN_PNN) {
481 nodepath = talloc_asprintf(mem_ctx, "CTDB_NODES_%u", pnn);
482 if (nodepath != NULL) {
483 nodes_list = getenv(nodepath);
486 if (nodes_list == NULL) {
487 nodes_list = getenv("CTDB_NODES");
489 if (nodes_list == NULL) {
490 const char *basedir = getenv("CTDB_BASE");
491 if (basedir == NULL) {
492 basedir = CTDB_ETCDIR;
494 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes", basedir);
495 if (nodes_list == NULL) {
496 fprintf(stderr, "Memory allocation error\n");
501 nodemap = ctdb_read_nodes_file(mem_ctx, nodes_list);
502 if (nodemap == NULL) {
503 fprintf(stderr, "Failed to read nodes file \"%s\"\n",
511 static struct ctdb_dbid *db_find(TALLOC_CTX *mem_ctx,
512 struct ctdb_context *ctdb,
513 struct ctdb_dbid_map *dbmap,
516 struct ctdb_dbid *db = NULL;
520 for (i=0; i<dbmap->num; i++) {
521 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
522 ctdb->pnn, TIMEOUT(),
523 dbmap->dbs[i].db_id, &name);
528 if (strcmp(db_name, name) == 0) {
529 talloc_free(discard_const(name));
538 static bool db_exists(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
539 const char *db_arg, uint32_t *db_id,
540 const char **db_name, uint8_t *db_flags)
542 struct ctdb_dbid_map *dbmap;
543 struct ctdb_dbid *db = NULL;
545 const char *name = NULL;
548 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
549 ctdb->pnn, TIMEOUT(), &dbmap);
554 if (strncmp(db_arg, "0x", 2) == 0) {
555 id = strtoul(db_arg, NULL, 0);
556 for (i=0; i<dbmap->num; i++) {
557 if (id == dbmap->dbs[i].db_id) {
564 db = db_find(mem_ctx, ctdb, dbmap, name);
568 fprintf(stderr, "No database matching '%s' found\n", db_arg);
573 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
574 ctdb->pnn, TIMEOUT(), id, &name);
583 if (db_name != NULL) {
584 *db_name = talloc_strdup(mem_ctx, name);
586 if (db_flags != NULL) {
587 *db_flags = db->flags;
592 static int h2i(char h)
594 if (h >= 'a' && h <= 'f') {
597 if (h >= 'A' && h <= 'F') {
603 static int hex_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
610 fprintf(stderr, "Key (%s) contains odd number of hex digits\n",
615 data.dsize = len / 2;
616 data.dptr = talloc_size(mem_ctx, data.dsize);
617 if (data.dptr == NULL) {
621 for (i=0; i<data.dsize; i++) {
622 data.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
629 static int str_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
635 if (strncmp(str, "0x", 2) == 0) {
636 ret = hex_to_data(str+2, len-2, mem_ctx, &data);
638 data.dptr = talloc_memdup(mem_ctx, str, len);
639 if (data.dptr == NULL) {
649 static int run_helper(TALLOC_CTX *mem_ctx, const char *command,
650 const char *path, int argc, const char **argv)
653 int save_errno, status, ret;
654 const char **new_argv;
657 new_argv = talloc_array(mem_ctx, const char *, argc + 2);
658 if (new_argv == NULL) {
663 for (i=0; i<argc; i++) {
664 new_argv[i+1] = argv[i];
666 new_argv[argc+1] = NULL;
671 talloc_free(new_argv);
672 fprintf(stderr, "Failed to fork %s (%s) - %s\n",
673 command, path, strerror(save_errno));
678 ret = execv(path, discard_const(new_argv));
682 /* Should not happen */
686 talloc_free(new_argv);
688 ret = waitpid(pid, &status, 0);
691 fprintf(stderr, "waitpid() failed for %s - %s\n",
692 command, strerror(save_errno));
696 if (WIFEXITED(status)) {
697 int pstatus = WEXITSTATUS(status);
698 if (WIFSIGNALED(status)) {
699 fprintf(stderr, "%s terminated with signal %d\n",
700 command, WTERMSIG(status));
702 } else if (pstatus >= 64 && pstatus < 255) {
703 fprintf(stderr, "%s failed with error %d\n",
704 command, pstatus-64);
710 } else if (WIFSIGNALED(status)) {
711 fprintf(stderr, "%s terminated with signal %d\n",
712 command, WTERMSIG(status));
723 static int control_version(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
724 int argc, const char **argv)
726 printf("%s\n", CTDB_VERSION_STRING);
730 static bool partially_online(TALLOC_CTX *mem_ctx,
731 struct ctdb_context *ctdb,
732 struct ctdb_node_and_flags *node)
734 struct ctdb_iface_list *iface_list;
738 if (node->flags != 0) {
742 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
743 node->pnn, TIMEOUT(), &iface_list);
749 for (i=0; i < iface_list->num; i++) {
750 if (iface_list->iface[i].link_state == 0) {
759 static void print_nodemap_machine(TALLOC_CTX *mem_ctx,
760 struct ctdb_context *ctdb,
761 struct ctdb_node_map *nodemap,
764 struct ctdb_node_and_flags *node;
767 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
771 "Disconnected", options.sep,
772 "Banned", options.sep,
773 "Disabled", options.sep,
774 "Unhealthy", options.sep,
775 "Stopped", options.sep,
776 "Inactive", options.sep,
777 "PartiallyOnline", options.sep,
778 "ThisNode", options.sep);
780 for (i=0; i<nodemap->num; i++) {
781 node = &nodemap->node[i];
782 if (node->flags & NODE_FLAGS_DELETED) {
786 printf("%s%u%s%s%s%d%s%d%s%d%s%d%s%d%s%d%s%d%s%c%s\n",
788 node->pnn, options.sep,
789 ctdb_sock_addr_to_string(mem_ctx, &node->addr),
791 !! (node->flags & NODE_FLAGS_DISCONNECTED), options.sep,
792 !! (node->flags & NODE_FLAGS_BANNED), options.sep,
793 !! (node->flags & NODE_FLAGS_PERMANENTLY_DISABLED),
795 !! (node->flags & NODE_FLAGS_UNHEALTHY), options.sep,
796 !! (node->flags & NODE_FLAGS_STOPPED), options.sep,
797 !! (node->flags & NODE_FLAGS_INACTIVE), options.sep,
798 partially_online(mem_ctx, ctdb, node), options.sep,
799 (node->pnn == mypnn)?'Y':'N', options.sep);
804 static void print_nodemap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
805 struct ctdb_node_map *nodemap, uint32_t mypnn)
807 struct ctdb_node_and_flags *node;
808 int num_deleted_nodes = 0;
811 for (i=0; i<nodemap->num; i++) {
812 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
817 if (num_deleted_nodes == 0) {
818 printf("Number of nodes:%d\n", nodemap->num);
820 printf("Number of nodes:%d (including %d deleted nodes)\n",
821 nodemap->num, num_deleted_nodes);
824 for (i=0; i<nodemap->num; i++) {
825 node = &nodemap->node[i];
826 if (node->flags & NODE_FLAGS_DELETED) {
830 printf("pnn:%u %-16s %s%s\n",
832 ctdb_sock_addr_to_string(mem_ctx, &node->addr),
833 partially_online(mem_ctx, ctdb, node) ?
835 pretty_print_flags(mem_ctx, node->flags),
836 node->pnn == mypnn ? " (THIS NODE)" : "");
840 static void print_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
841 struct ctdb_node_map *nodemap, uint32_t mypnn,
842 struct ctdb_vnn_map *vnnmap, int recmode,
847 print_nodemap(mem_ctx, ctdb, nodemap, mypnn);
849 if (vnnmap->generation == INVALID_GENERATION) {
850 printf("Generation:INVALID\n");
852 printf("Generation:%u\n", vnnmap->generation);
854 printf("Size:%d\n", vnnmap->size);
855 for (i=0; i<vnnmap->size; i++) {
856 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
859 printf("Recovery mode:%s (%d)\n",
860 recmode == CTDB_RECOVERY_NORMAL ? "NORMAL" : "RECOVERY",
862 printf("Recovery master:%d\n", recmaster);
865 static int control_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
866 int argc, const char **argv)
868 struct ctdb_node_map *nodemap;
869 struct ctdb_vnn_map *vnnmap;
878 nodemap = get_nodemap(ctdb, false);
879 if (nodemap == NULL) {
883 if (options.machinereadable == 1) {
884 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
888 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
889 ctdb->cmd_pnn, TIMEOUT(), &vnnmap);
894 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
895 ctdb->cmd_pnn, TIMEOUT(), &recmode);
900 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
901 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
906 print_status(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn, vnnmap,
911 static int control_uptime(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
912 int argc, const char **argv)
914 struct ctdb_uptime *uptime;
915 int ret, tmp, days, hours, minutes, seconds;
917 ret = ctdb_ctrl_uptime(mem_ctx, ctdb->ev, ctdb->client,
918 ctdb->cmd_pnn, TIMEOUT(), &uptime);
923 printf("Current time of node %-4u : %s",
924 ctdb->cmd_pnn, ctime(&uptime->current_time.tv_sec));
926 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
927 seconds = tmp % 60; tmp /= 60;
928 minutes = tmp % 60; tmp /= 60;
929 hours = tmp % 24; tmp /= 24;
932 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s",
933 days, hours, minutes, seconds,
934 ctime(&uptime->ctdbd_start_time.tv_sec));
936 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
937 seconds = tmp % 60; tmp /= 60;
938 minutes = tmp % 60; tmp /= 60;
939 hours = tmp % 24; tmp /= 24;
942 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s",
943 days, hours, minutes, seconds,
944 ctime(&uptime->last_recovery_finished.tv_sec));
946 printf("Duration of last recovery/failover: %lf seconds\n",
947 timeval_delta(&uptime->last_recovery_finished,
948 &uptime->last_recovery_started));
953 static int control_ping(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
954 int argc, const char **argv)
957 int ret, num_clients;
959 tv = timeval_current();
960 ret = ctdb_ctrl_ping(mem_ctx, ctdb->ev, ctdb->client,
961 ctdb->cmd_pnn, TIMEOUT(), &num_clients);
966 printf("response from %u time=%.6f sec (%d clients)\n",
967 ctdb->cmd_pnn, timeval_elapsed(&tv), num_clients);
971 const char *runstate_to_string(enum ctdb_runstate runstate);
972 enum ctdb_runstate runstate_from_string(const char *runstate_str);
974 static int control_runstate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
975 int argc, const char **argv)
977 enum ctdb_runstate runstate;
981 ret = ctdb_ctrl_get_runstate(mem_ctx, ctdb->ev, ctdb->client,
982 ctdb->cmd_pnn, TIMEOUT(), &runstate);
988 for (i=0; i<argc; i++) {
989 enum ctdb_runstate t;
992 t = ctdb_runstate_from_string(argv[i]);
993 if (t == CTDB_RUNSTATE_UNKNOWN) {
994 printf("Invalid run state (%s)\n", argv[i]);
1005 printf("CTDB not in required run state (got %s)\n",
1006 ctdb_runstate_to_string(runstate));
1010 printf("%s\n", ctdb_runstate_to_string(runstate));
1014 static int control_getvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1015 int argc, const char **argv)
1017 struct ctdb_var_list *tun_var_list;
1026 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1027 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1030 "Failed to get list of variables from node %u\n",
1036 for (i=0; i<tun_var_list->count; i++) {
1037 if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
1044 printf("No such tunable %s\n", argv[0]);
1048 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
1049 ctdb->cmd_pnn, TIMEOUT(), argv[0], &value);
1054 printf("%-26s = %u\n", argv[0], value);
1058 static int control_setvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1059 int argc, const char **argv)
1061 struct ctdb_var_list *tun_var_list;
1062 struct ctdb_tunable tunable;
1070 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1071 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1074 "Failed to get list of variables from node %u\n",
1080 for (i=0; i<tun_var_list->count; i++) {
1081 if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
1088 printf("No such tunable %s\n", argv[0]);
1092 tunable.name = argv[0];
1093 tunable.value = strtoul(argv[1], NULL, 0);
1095 ret = ctdb_ctrl_set_tunable(mem_ctx, ctdb->ev, ctdb->client,
1096 ctdb->cmd_pnn, TIMEOUT(), &tunable);
1100 "Setting obsolete tunable variable '%s'\n",
1109 static int control_listvars(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1110 int argc, const char **argv)
1112 struct ctdb_var_list *tun_var_list;
1119 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1120 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1125 for (i=0; i<tun_var_list->count; i++) {
1126 control_getvar(mem_ctx, ctdb, 1, &tun_var_list->var[i]);
1135 } stats_fields[] = {
1136 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
1137 STATISTICS_FIELD(num_clients),
1138 STATISTICS_FIELD(frozen),
1139 STATISTICS_FIELD(recovering),
1140 STATISTICS_FIELD(num_recoveries),
1141 STATISTICS_FIELD(client_packets_sent),
1142 STATISTICS_FIELD(client_packets_recv),
1143 STATISTICS_FIELD(node_packets_sent),
1144 STATISTICS_FIELD(node_packets_recv),
1145 STATISTICS_FIELD(keepalive_packets_sent),
1146 STATISTICS_FIELD(keepalive_packets_recv),
1147 STATISTICS_FIELD(node.req_call),
1148 STATISTICS_FIELD(node.reply_call),
1149 STATISTICS_FIELD(node.req_dmaster),
1150 STATISTICS_FIELD(node.reply_dmaster),
1151 STATISTICS_FIELD(node.reply_error),
1152 STATISTICS_FIELD(node.req_message),
1153 STATISTICS_FIELD(node.req_control),
1154 STATISTICS_FIELD(node.reply_control),
1155 STATISTICS_FIELD(client.req_call),
1156 STATISTICS_FIELD(client.req_message),
1157 STATISTICS_FIELD(client.req_control),
1158 STATISTICS_FIELD(timeouts.call),
1159 STATISTICS_FIELD(timeouts.control),
1160 STATISTICS_FIELD(timeouts.traverse),
1161 STATISTICS_FIELD(locks.num_calls),
1162 STATISTICS_FIELD(locks.num_current),
1163 STATISTICS_FIELD(locks.num_pending),
1164 STATISTICS_FIELD(locks.num_failed),
1165 STATISTICS_FIELD(total_calls),
1166 STATISTICS_FIELD(pending_calls),
1167 STATISTICS_FIELD(childwrite_calls),
1168 STATISTICS_FIELD(pending_childwrite_calls),
1169 STATISTICS_FIELD(memory_used),
1170 STATISTICS_FIELD(max_hop_count),
1171 STATISTICS_FIELD(total_ro_delegations),
1172 STATISTICS_FIELD(total_ro_revokes),
1175 #define LATENCY_AVG(v) ((v).num ? (v).total / (v).num : 0.0 )
1177 static void print_statistics_machine(struct ctdb_statistics *s,
1183 printf("CTDB version%s", options.sep);
1184 printf("Current time of statistics%s", options.sep);
1185 printf("Statistics collected since%s", options.sep);
1186 for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
1187 printf("%s%s", stats_fields[i].name, options.sep);
1189 printf("num_reclock_ctdbd_latency%s", options.sep);
1190 printf("min_reclock_ctdbd_latency%s", options.sep);
1191 printf("avg_reclock_ctdbd_latency%s", options.sep);
1192 printf("max_reclock_ctdbd_latency%s", options.sep);
1194 printf("num_reclock_recd_latency%s", options.sep);
1195 printf("min_reclock_recd_latency%s", options.sep);
1196 printf("avg_reclock_recd_latency%s", options.sep);
1197 printf("max_reclock_recd_latency%s", options.sep);
1199 printf("num_call_latency%s", options.sep);
1200 printf("min_call_latency%s", options.sep);
1201 printf("avg_call_latency%s", options.sep);
1202 printf("max_call_latency%s", options.sep);
1204 printf("num_lockwait_latency%s", options.sep);
1205 printf("min_lockwait_latency%s", options.sep);
1206 printf("avg_lockwait_latency%s", options.sep);
1207 printf("max_lockwait_latency%s", options.sep);
1209 printf("num_childwrite_latency%s", options.sep);
1210 printf("min_childwrite_latency%s", options.sep);
1211 printf("avg_childwrite_latency%s", options.sep);
1212 printf("max_childwrite_latency%s", options.sep);
1216 printf("%u%s", CTDB_PROTOCOL, options.sep);
1217 printf("%u%s", (uint32_t)s->statistics_current_time.tv_sec, options.sep);
1218 printf("%u%s", (uint32_t)s->statistics_start_time.tv_sec, options.sep);
1219 for (i=0;i<ARRAY_SIZE(stats_fields);i++) {
1221 *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s),
1224 printf("%u%s", s->reclock.ctdbd.num, options.sep);
1225 printf("%.6f%s", s->reclock.ctdbd.min, options.sep);
1226 printf("%.6f%s", LATENCY_AVG(s->reclock.ctdbd), options.sep);
1227 printf("%.6f%s", s->reclock.ctdbd.max, options.sep);
1229 printf("%u%s", s->reclock.recd.num, options.sep);
1230 printf("%.6f%s", s->reclock.recd.min, options.sep);
1231 printf("%.6f%s", LATENCY_AVG(s->reclock.recd), options.sep);
1232 printf("%.6f%s", s->reclock.recd.max, options.sep);
1234 printf("%d%s", s->call_latency.num, options.sep);
1235 printf("%.6f%s", s->call_latency.min, options.sep);
1236 printf("%.6f%s", LATENCY_AVG(s->call_latency), options.sep);
1237 printf("%.6f%s", s->call_latency.max, options.sep);
1239 printf("%d%s", s->childwrite_latency.num, options.sep);
1240 printf("%.6f%s", s->childwrite_latency.min, options.sep);
1241 printf("%.6f%s", LATENCY_AVG(s->childwrite_latency), options.sep);
1242 printf("%.6f%s", s->childwrite_latency.max, options.sep);
1246 static void print_statistics(struct ctdb_statistics *s)
1248 int tmp, days, hours, minutes, seconds;
1250 const char *prefix = NULL;
1253 tmp = s->statistics_current_time.tv_sec -
1254 s->statistics_start_time.tv_sec;
1255 seconds = tmp % 60; tmp /= 60;
1256 minutes = tmp % 60; tmp /= 60;
1257 hours = tmp % 24; tmp /= 24;
1260 printf("CTDB version %u\n", CTDB_PROTOCOL);
1261 printf("Current time of statistics : %s",
1262 ctime(&s->statistics_current_time.tv_sec));
1263 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s",
1264 days, hours, minutes, seconds,
1265 ctime(&s->statistics_start_time.tv_sec));
1267 for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
1268 if (strchr(stats_fields[i].name, '.') != NULL) {
1269 preflen = strcspn(stats_fields[i].name, ".") + 1;
1271 strncmp(prefix, stats_fields[i].name, preflen) != 0) {
1272 prefix = stats_fields[i].name;
1273 printf(" %*.*s\n", preflen-1, preflen-1,
1274 stats_fields[i].name);
1279 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
1280 stats_fields[i].name+preflen, preflen ? 0 : 4, "",
1281 *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s));
1284 printf(" hop_count_buckets:");
1285 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
1286 printf(" %d", s->hop_count_bucket[i]);
1289 printf(" lock_buckets:");
1290 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
1291 printf(" %d", s->locks.buckets[i]);
1294 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1295 "locks_latency MIN/AVG/MAX",
1296 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
1297 s->locks.latency.max, s->locks.latency.num);
1299 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1300 "reclock_ctdbd MIN/AVG/MAX",
1301 s->reclock.ctdbd.min, LATENCY_AVG(s->reclock.ctdbd),
1302 s->reclock.ctdbd.max, s->reclock.ctdbd.num);
1304 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1305 "reclock_recd MIN/AVG/MAX",
1306 s->reclock.recd.min, LATENCY_AVG(s->reclock.recd),
1307 s->reclock.recd.max, s->reclock.recd.num);
1309 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1310 "call_latency MIN/AVG/MAX",
1311 s->call_latency.min, LATENCY_AVG(s->call_latency),
1312 s->call_latency.max, s->call_latency.num);
1314 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1315 "childwrite_latency MIN/AVG/MAX",
1316 s->childwrite_latency.min,
1317 LATENCY_AVG(s->childwrite_latency),
1318 s->childwrite_latency.max, s->childwrite_latency.num);
1321 static int control_statistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1322 int argc, const char **argv)
1324 struct ctdb_statistics *stats;
1328 usage("statistics");
1331 ret = ctdb_ctrl_statistics(mem_ctx, ctdb->ev, ctdb->client,
1332 ctdb->cmd_pnn, TIMEOUT(), &stats);
1337 if (options.machinereadable) {
1338 print_statistics_machine(stats, true);
1340 print_statistics(stats);
1346 static int control_statistics_reset(TALLOC_CTX *mem_ctx,
1347 struct ctdb_context *ctdb,
1348 int argc, const char **argv)
1353 usage("statisticsreset");
1356 ret = ctdb_ctrl_statistics_reset(mem_ctx, ctdb->ev, ctdb->client,
1357 ctdb->cmd_pnn, TIMEOUT());
1365 static int control_stats(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1366 int argc, const char **argv)
1368 struct ctdb_statistics_list *slist;
1369 int ret, count = 0, i;
1370 bool show_header = true;
1377 count = atoi(argv[0]);
1380 ret = ctdb_ctrl_get_stat_history(mem_ctx, ctdb->ev, ctdb->client,
1381 ctdb->cmd_pnn, TIMEOUT(), &slist);
1386 for (i=0; i<slist->num; i++) {
1387 if (slist->stats[i].statistics_start_time.tv_sec == 0) {
1390 if (options.machinereadable == 1) {
1391 print_statistics_machine(&slist->stats[i],
1393 show_header = false;
1395 print_statistics(&slist->stats[i]);
1397 if (count > 0 && i == count) {
1405 static int ctdb_public_ip_cmp(const void *a, const void *b)
1407 const struct ctdb_public_ip *ip_a = a;
1408 const struct ctdb_public_ip *ip_b = b;
1410 return ctdb_sock_addr_cmp(&ip_a->addr, &ip_b->addr);
1413 static void print_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1414 struct ctdb_public_ip_list *ips,
1415 struct ctdb_public_ip_info **ipinfo,
1419 char *conf, *avail, *active;
1421 if (options.machinereadable == 1) {
1422 printf("%s%s%s%s%s", options.sep,
1423 "Public IP", options.sep,
1424 "Node", options.sep);
1425 if (options.verbose == 1) {
1426 printf("%s%s%s%s%s%s\n",
1427 "ActiveInterfaces", options.sep,
1428 "AvailableInterfaces", options.sep,
1429 "ConfiguredInterfaces", options.sep);
1435 printf("Public IPs on ALL nodes\n");
1437 printf("Public IPs on node %u\n", ctdb->cmd_pnn);
1441 for (i = 0; i < ips->num; i++) {
1443 if (options.machinereadable == 1) {
1444 printf("%s%s%s%d%s", options.sep,
1445 ctdb_sock_addr_to_string(
1446 mem_ctx, &ips->ip[i].addr),
1448 (int)ips->ip[i].pnn, options.sep);
1450 printf("%s", ctdb_sock_addr_to_string(
1451 mem_ctx, &ips->ip[i].addr));
1454 if (options.verbose == 0) {
1455 if (options.machinereadable == 1) {
1458 printf(" %d\n", (int)ips->ip[i].pnn);
1467 if (ipinfo[i] == NULL) {
1471 for (j=0; j<ipinfo[i]->ifaces->num; j++) {
1472 struct ctdb_iface *iface;
1474 iface = &ipinfo[i]->ifaces->iface[j];
1476 conf = talloc_strdup(mem_ctx, iface->name);
1478 conf = talloc_asprintf_append(
1479 conf, ",%s", iface->name);
1482 if (ipinfo[i]->active_idx == j) {
1483 active = iface->name;
1486 if (iface->link_state == 0) {
1490 if (avail == NULL) {
1491 avail = talloc_strdup(mem_ctx, iface->name);
1493 avail = talloc_asprintf_append(
1494 avail, ",%s", iface->name);
1500 if (options.machinereadable == 1) {
1501 printf("%s%s%s%s%s%s\n",
1502 active ? active : "", options.sep,
1503 avail ? avail : "", options.sep,
1504 conf ? conf : "", options.sep);
1506 printf(" node[%d] active[%s] available[%s]"
1507 " configured[%s]\n",
1508 (int)ips->ip[i].pnn, active ? active : "",
1509 avail ? avail : "", conf ? conf : "");
1514 static int collect_ips(uint8_t *keybuf, size_t keylen, uint8_t *databuf,
1515 size_t datalen, void *private_data)
1517 struct ctdb_public_ip_list *ips = talloc_get_type_abort(
1518 private_data, struct ctdb_public_ip_list);
1519 struct ctdb_public_ip *ip;
1521 ip = (struct ctdb_public_ip *)databuf;
1522 ips->ip[ips->num] = *ip;
1528 static int get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
1529 struct ctdb_public_ip_list **out)
1531 struct ctdb_node_map *nodemap;
1532 struct ctdb_public_ip_list *ips;
1533 struct db_hash_context *ipdb;
1535 int ret, count, i, j;
1537 nodemap = get_nodemap(ctdb, false);
1538 if (nodemap == NULL) {
1542 ret = db_hash_init(mem_ctx, "ips", 101, DB_HASH_COMPLEX, &ipdb);
1547 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
1553 for (i=0; i<count; i++) {
1554 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1555 pnn_list[i], TIMEOUT(),
1561 for (j=0; j<ips->num; j++) {
1562 struct ctdb_public_ip ip;
1564 ip.pnn = ips->ip[j].pnn;
1565 ip.addr = ips->ip[j].addr;
1567 if (pnn_list[i] == ip.pnn) {
1568 /* Node claims IP is hosted on it, so
1569 * save that information
1571 ret = db_hash_add(ipdb, (uint8_t *)&ip.addr,
1573 (uint8_t *)&ip, sizeof(ip));
1578 /* Node thinks IP is hosted elsewhere,
1579 * so overwrite with CTDB_UNKNOWN_PNN
1580 * if there's no existing entry
1582 ret = db_hash_exists(ipdb, (uint8_t *)&ip.addr,
1584 if (ret == ENOENT) {
1585 ip.pnn = CTDB_UNKNOWN_PNN;
1586 ret = db_hash_add(ipdb,
1587 (uint8_t *)&ip.addr,
1601 talloc_free(pnn_list);
1603 ret = db_hash_traverse(ipdb, NULL, NULL, &count);
1608 ips = talloc_zero(mem_ctx, struct ctdb_public_ip_list);
1613 ips->ip = talloc_array(ips, struct ctdb_public_ip, count);
1614 if (ips->ip == NULL) {
1618 ret = db_hash_traverse(ipdb, collect_ips, ips, &count);
1623 if (count != ips->num) {
1637 static int control_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1638 int argc, const char **argv)
1640 struct ctdb_public_ip_list *ips;
1641 struct ctdb_public_ip_info **ipinfo;
1643 bool do_all = false;
1650 if (strcmp(argv[0], "all") == 0) {
1658 ret = get_all_public_ips(ctdb, mem_ctx, &ips);
1660 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1661 ctdb->cmd_pnn, TIMEOUT(),
1668 qsort(ips->ip, ips->num, sizeof(struct ctdb_public_ip),
1669 ctdb_public_ip_cmp);
1671 ipinfo = talloc_array(mem_ctx, struct ctdb_public_ip_info *, ips->num);
1672 if (ipinfo == NULL) {
1676 for (i=0; i<ips->num; i++) {
1679 pnn = ips->ip[i].pnn;
1681 pnn = ctdb->cmd_pnn;
1683 if (pnn == CTDB_UNKNOWN_PNN) {
1687 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev,
1689 TIMEOUT(), &ips->ip[i].addr,
1696 print_ip(mem_ctx, ctdb, ips, ipinfo, do_all);
1700 static int control_ipinfo(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1701 int argc, const char **argv)
1703 struct ctdb_public_ip_info *ipinfo;
1704 ctdb_sock_addr addr;
1711 if (! parse_ip(argv[0], NULL, 0, &addr)) {
1712 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
1716 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev, ctdb->client,
1717 ctdb->cmd_pnn, TIMEOUT(), &addr,
1721 printf("Node %u does not know about IP %s\n",
1722 ctdb->cmd_pnn, argv[0]);
1727 printf("Public IP[%s] info on node %u\n",
1728 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1731 printf("IP:%s\nCurrentNode:%u\nNumInterfaces:%u\n",
1732 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1733 ipinfo->ip.pnn, ipinfo->ifaces->num);
1735 for (i=0; i<ipinfo->ifaces->num; i++) {
1736 struct ctdb_iface *iface;
1738 iface = &ipinfo->ifaces->iface[i];
1739 iface->name[CTDB_IFACE_SIZE] = '\0';
1740 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
1742 iface->link_state == 0 ? "down" : "up",
1744 (i == ipinfo->active_idx) ? " (active)" : "");
1750 static int control_ifaces(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1751 int argc, const char **argv)
1753 struct ctdb_iface_list *ifaces;
1760 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1761 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1766 if (ifaces->num == 0) {
1767 printf("No interfaces configured on node %u\n",
1772 if (options.machinereadable) {
1773 printf("%s%s%s%s%s%s%s\n", options.sep,
1774 "Name", options.sep,
1775 "LinkStatus", options.sep,
1776 "References", options.sep);
1778 printf("Interfaces on node %u\n", ctdb->cmd_pnn);
1781 for (i=0; i<ifaces->num; i++) {
1782 if (options.machinereadable) {
1783 printf("%s%s%s%u%s%u%s\n", options.sep,
1784 ifaces->iface[i].name, options.sep,
1785 ifaces->iface[i].link_state, options.sep,
1786 ifaces->iface[i].references, options.sep);
1788 printf("name:%s link:%s references:%u\n",
1789 ifaces->iface[i].name,
1790 ifaces->iface[i].link_state ? "up" : "down",
1791 ifaces->iface[i].references);
1798 static int control_setifacelink(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1799 int argc, const char **argv)
1801 struct ctdb_iface_list *ifaces;
1802 struct ctdb_iface *iface;
1806 usage("setifacelink");
1809 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
1810 fprintf(stderr, "Interface name '%s' too long\n", argv[0]);
1814 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1815 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1818 "Failed to get interface information from node %u\n",
1824 for (i=0; i<ifaces->num; i++) {
1825 if (strcmp(ifaces->iface[i].name, argv[0]) == 0) {
1826 iface = &ifaces->iface[i];
1831 if (iface == NULL) {
1832 printf("Interface %s not configured on node %u\n",
1833 argv[0], ctdb->cmd_pnn);
1837 if (strcmp(argv[1], "up") == 0) {
1838 iface->link_state = 1;
1839 } else if (strcmp(argv[1], "down") == 0) {
1840 iface->link_state = 0;
1842 usage("setifacelink");
1846 iface->references = 0;
1848 ret = ctdb_ctrl_set_iface_link_state(mem_ctx, ctdb->ev, ctdb->client,
1849 ctdb->cmd_pnn, TIMEOUT(), iface);
1857 static int control_process_exists(TALLOC_CTX *mem_ctx,
1858 struct ctdb_context *ctdb,
1859 int argc, const char **argv)
1865 usage("process-exists");
1868 pid = atoi(argv[0]);
1869 ret = ctdb_ctrl_process_exists(mem_ctx, ctdb->ev, ctdb->client,
1870 ctdb->cmd_pnn, TIMEOUT(), pid, &status);
1876 printf("PID %u exists\n", pid);
1878 printf("PID %u does not exist\n", pid);
1883 static int control_getdbmap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1884 int argc, const char **argv)
1886 struct ctdb_dbid_map *dbmap;
1893 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
1894 ctdb->cmd_pnn, TIMEOUT(), &dbmap);
1899 if (options.machinereadable == 1) {
1900 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1903 "Name", options.sep,
1904 "Path", options.sep,
1905 "Persistent", options.sep,
1906 "Sticky", options.sep,
1907 "Unhealthy", options.sep,
1908 "Readonly", options.sep);
1910 printf("Number of databases:%d\n", dbmap->num);
1913 for (i=0; i<dbmap->num; i++) {
1922 db_id = dbmap->dbs[i].db_id;
1924 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
1925 ctdb->cmd_pnn, TIMEOUT(), db_id,
1931 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1932 ctdb->cmd_pnn, TIMEOUT(), db_id,
1938 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1939 ctdb->cmd_pnn, TIMEOUT(), db_id,
1945 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
1946 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
1947 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
1949 if (options.machinereadable == 1) {
1950 printf("%s0x%08X%s%s%s%s%s%d%s%d%s%d%s%d%s\n",
1955 !! (persistent), options.sep,
1956 !! (sticky), options.sep,
1957 !! (health), options.sep,
1958 !! (readonly), options.sep);
1960 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
1962 persistent ? " PERSISTENT" : "",
1963 sticky ? " STICKY" : "",
1964 readonly ? " READONLY" : "",
1965 health ? " UNHEALTHY" : "");
1968 talloc_free(discard_const(name));
1969 talloc_free(discard_const(path));
1970 talloc_free(discard_const(health));
1976 static int control_getdbstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1977 int argc, const char **argv)
1980 const char *db_name, *db_path, *db_health;
1985 usage("getdbstatus");
1988 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
1992 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1993 ctdb->cmd_pnn, TIMEOUT(), db_id,
1999 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
2000 ctdb->cmd_pnn, TIMEOUT(), db_id,
2006 printf("dbid: 0x%08x\nname: %s\npath: %s\n", db_id, db_name, db_path);
2007 printf("PERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
2008 (db_flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
2009 (db_flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
2010 (db_flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
2011 (db_health ? db_health : "OK"));
2015 struct dump_record_state {
2019 #define ISASCII(x) (isprint(x) && ! strchr("\"\\", (x)))
2021 static void dump_tdb_data(const char *name, TDB_DATA val)
2025 fprintf(stdout, "%s(%zu) = \"", name, val.dsize);
2026 for (i=0; i<val.dsize; i++) {
2027 if (ISASCII(val.dptr[i])) {
2028 fprintf(stdout, "%c", val.dptr[i]);
2030 fprintf(stdout, "\\%02X", val.dptr[i]);
2033 fprintf(stdout, "\"\n");
2036 static void dump_ltdb_header(struct ctdb_ltdb_header *header)
2038 fprintf(stdout, "dmaster: %u\n", header->dmaster);
2039 fprintf(stdout, "rsn: %" PRIu64 "\n", header->rsn);
2040 fprintf(stdout, "flags: 0x%08x", header->flags);
2041 if (header->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
2042 fprintf(stdout, " MIGRATED_WITH_DATA");
2044 if (header->flags & CTDB_REC_FLAG_VACUUM_MIGRATED) {
2045 fprintf(stdout, " VACUUM_MIGRATED");
2047 if (header->flags & CTDB_REC_FLAG_AUTOMATIC) {
2048 fprintf(stdout, " AUTOMATIC");
2050 if (header->flags & CTDB_REC_RO_HAVE_DELEGATIONS) {
2051 fprintf(stdout, " RO_HAVE_DELEGATIONS");
2053 if (header->flags & CTDB_REC_RO_HAVE_READONLY) {
2054 fprintf(stdout, " RO_HAVE_READONLY");
2056 if (header->flags & CTDB_REC_RO_REVOKING_READONLY) {
2057 fprintf(stdout, " RO_REVOKING_READONLY");
2059 if (header->flags & CTDB_REC_RO_REVOKE_COMPLETE) {
2060 fprintf(stdout, " RO_REVOKE_COMPLETE");
2062 fprintf(stdout, "\n");
2066 static int dump_record(uint32_t reqid, struct ctdb_ltdb_header *header,
2067 TDB_DATA key, TDB_DATA data, void *private_data)
2069 struct dump_record_state *state =
2070 (struct dump_record_state *)private_data;
2074 dump_tdb_data("key", key);
2075 dump_ltdb_header(header);
2076 dump_tdb_data("data", data);
2077 fprintf(stdout, "\n");
2082 struct traverse_state {
2083 TALLOC_CTX *mem_ctx;
2085 ctdb_rec_parser_func_t func;
2086 struct dump_record_state sub_state;
2089 static void traverse_handler(uint64_t srvid, TDB_DATA data, void *private_data)
2091 struct traverse_state *state = (struct traverse_state *)private_data;
2092 struct ctdb_rec_data *rec;
2093 struct ctdb_ltdb_header header;
2096 ret = ctdb_rec_data_pull(data.dptr, data.dsize, state->mem_ctx, &rec);
2101 if (rec->key.dsize == 0 && rec->data.dsize == 0) {
2103 /* end of traverse */
2108 ret = ctdb_ltdb_header_extract(&rec->data, &header);
2114 if (rec->data.dsize == 0) {
2119 ret = state->func(rec->reqid, &header, rec->key, rec->data,
2127 static int control_catdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2128 int argc, const char **argv)
2130 struct ctdb_db_context *db;
2131 const char *db_name;
2134 struct ctdb_traverse_start_ext traverse;
2135 struct traverse_state state;
2142 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2146 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2149 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2154 ZERO_STRUCT(traverse);
2156 traverse.db_id = db_id;
2158 traverse.srvid = next_srvid(ctdb);
2159 traverse.withemptyrecords = false;
2161 state.mem_ctx = mem_ctx;
2163 state.func = dump_record;
2164 state.sub_state.count = 0;
2166 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2168 traverse_handler, &state);
2173 ret = ctdb_ctrl_traverse_start_ext(mem_ctx, ctdb->ev, ctdb->client,
2174 ctdb->cmd_pnn, TIMEOUT(),
2180 ctdb_client_wait(ctdb->ev, &state.done);
2182 printf("Dumped %u records\n", state.sub_state.count);
2184 ret = ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2185 traverse.srvid, &state);
2193 static int control_cattdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2194 int argc, const char **argv)
2196 struct ctdb_db_context *db;
2197 const char *db_name;
2200 struct dump_record_state state;
2207 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2211 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2214 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2219 ret = ctdb_db_traverse(db, true, true, dump_record, &state);
2221 printf("Dumped %u record(s)\n", state.count);
2226 static int control_getmonmode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2227 int argc, const char **argv)
2232 usage("getmonmode");
2235 ret = ctdb_ctrl_get_monmode(mem_ctx, ctdb->ev, ctdb->client,
2236 ctdb->cmd_pnn, TIMEOUT(), &mode);
2242 (mode == CTDB_MONITORING_ENABLED) ? "ENABLED" : "DISABLED");
2246 static int control_getcapabilities(TALLOC_CTX *mem_ctx,
2247 struct ctdb_context *ctdb,
2248 int argc, const char **argv)
2254 usage("getcapabilities");
2257 ret = ctdb_ctrl_get_capabilities(mem_ctx, ctdb->ev, ctdb->client,
2258 ctdb->cmd_pnn, TIMEOUT(), &caps);
2263 if (options.machinereadable == 1) {
2264 printf("%s%s%s%s%s\n",
2266 "RECMASTER", options.sep,
2267 "LMASTER", options.sep);
2268 printf("%s%d%s%d%s\n", options.sep,
2269 !! (caps & CTDB_CAP_RECMASTER), options.sep,
2270 !! (caps & CTDB_CAP_LMASTER), options.sep);
2272 printf("RECMASTER: %s\n",
2273 (caps & CTDB_CAP_RECMASTER) ? "YES" : "NO");
2274 printf("LMASTER: %s\n",
2275 (caps & CTDB_CAP_LMASTER) ? "YES" : "NO");
2281 static int control_pnn(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2282 int argc, const char **argv)
2284 printf("%u\n", ctdb_client_pnn(ctdb->client));
2288 static int control_lvs(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2289 int argc, const char **argv)
2291 char *t, *lvs_helper = NULL;
2297 t = getenv("CTDB_LVS_HELPER");
2299 lvs_helper = talloc_strdup(mem_ctx, t);
2301 lvs_helper = talloc_asprintf(mem_ctx, "%s/ctdb_lvs",
2302 CTDB_HELPER_BINDIR);
2305 if (lvs_helper == NULL) {
2306 fprintf(stderr, "Unable to set LVS helper\n");
2310 return run_helper(mem_ctx, "LVS helper", lvs_helper, argc, argv);
2313 static int control_disable_monitor(TALLOC_CTX *mem_ctx,
2314 struct ctdb_context *ctdb,
2315 int argc, const char **argv)
2320 usage("disablemonitor");
2323 ret = ctdb_ctrl_disable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2324 ctdb->cmd_pnn, TIMEOUT());
2332 static int control_enable_monitor(TALLOC_CTX *mem_ctx,
2333 struct ctdb_context *ctdb,
2334 int argc, const char **argv)
2339 usage("enablemonitor");
2342 ret = ctdb_ctrl_enable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2343 ctdb->cmd_pnn, TIMEOUT());
2351 static int control_setdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2352 int argc, const char **argv)
2362 found = debug_level_parse(argv[0], &log_level);
2365 "Invalid debug level '%s'. Valid levels are:\n",
2367 fprintf(stderr, "\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
2371 ret = ctdb_ctrl_setdebug(mem_ctx, ctdb->ev, ctdb->client,
2372 ctdb->cmd_pnn, TIMEOUT(), log_level);
2380 static int control_getdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2381 int argc, const char **argv)
2384 const char *log_str;
2391 ret = ctdb_ctrl_getdebug(mem_ctx, ctdb->ev, ctdb->client,
2392 ctdb->cmd_pnn, TIMEOUT(), &loglevel);
2397 log_str = debug_level_to_string(loglevel);
2398 printf("%s\n", log_str);
2403 static int control_attach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2404 int argc, const char **argv)
2406 const char *db_name;
2407 uint8_t db_flags = 0;
2410 if (argc < 1 || argc > 2) {
2416 if (strcmp(argv[1], "persistent") == 0) {
2417 db_flags = CTDB_DB_FLAGS_PERSISTENT;
2418 } else if (strcmp(argv[1], "readonly") == 0) {
2419 db_flags = CTDB_DB_FLAGS_READONLY;
2420 } else if (strcmp(argv[1], "sticky") == 0) {
2421 db_flags = CTDB_DB_FLAGS_STICKY;
2427 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2436 static int control_detach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2437 int argc, const char **argv)
2439 const char *db_name;
2442 struct ctdb_node_map *nodemap;
2450 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2451 ctdb->cmd_pnn, TIMEOUT(), &recmode);
2456 if (recmode == CTDB_RECOVERY_ACTIVE) {
2457 fprintf(stderr, "Database cannot be detached"
2458 " when recovery is active\n");
2462 nodemap = get_nodemap(ctdb, false);
2463 if (nodemap == NULL) {
2467 for (i=0; i<nodemap->num; i++) {
2470 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
2473 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
2476 if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
2477 fprintf(stderr, "Database cannot be detached on"
2478 " inactive (stopped or banned) node %u\n",
2479 nodemap->node[i].pnn);
2483 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
2484 nodemap->node[i].pnn, TIMEOUT(),
2485 "AllowClientDBAttach", &value);
2488 "Unable to get tunable AllowClientDBAttach"
2489 " from node %u\n", nodemap->node[i].pnn);
2495 "Database access is still active on node %u."
2496 " Set AllowclientDBAttach=0 on all nodes.\n",
2497 nodemap->node[i].pnn);
2503 for (i=0; i<argc; i++) {
2504 if (! db_exists(mem_ctx, ctdb, argv[i], &db_id, &db_name,
2509 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
2511 "Persistent database %s cannot be detached\n",
2516 ret = ctdb_detach(mem_ctx, ctdb->ev, ctdb->client,
2519 fprintf(stderr, "Database %s detach failed\n", db_name);
2527 static int control_dumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2528 int argc, const char **argv)
2530 const char *mem_str;
2534 ret = ctdb_ctrl_dump_memory(mem_ctx, ctdb->ev, ctdb->client,
2535 ctdb->cmd_pnn, TIMEOUT(), &mem_str);
2540 n = write(1, mem_str, strlen(mem_str)+1);
2541 if (n < 0 || n != strlen(mem_str)+1) {
2542 fprintf(stderr, "Failed to write talloc summary\n");
2549 static void dump_memory(uint64_t srvid, TDB_DATA data, void *private_data)
2551 bool *done = (bool *)private_data;
2554 n = write(1, data.dptr, data.dsize);
2555 if (n < 0 || n != data.dsize) {
2556 fprintf(stderr, "Failed to write talloc summary\n");
2562 static int control_rddumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2563 int argc, const char **argv)
2565 struct ctdb_srvid_message msg = { 0 };
2569 msg.pnn = ctdb->pnn;
2570 msg.srvid = next_srvid(ctdb);
2572 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2573 msg.srvid, dump_memory, &done);
2578 ret = ctdb_message_mem_dump(mem_ctx, ctdb->ev, ctdb->client,
2579 ctdb->cmd_pnn, &msg);
2584 ctdb_client_wait(ctdb->ev, &done);
2588 static int control_getpid(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2589 int argc, const char **argv)
2594 ret = ctdb_ctrl_get_pid(mem_ctx, ctdb->ev, ctdb->client,
2595 ctdb->cmd_pnn, TIMEOUT(), &pid);
2600 printf("%u\n", pid);
2604 static int check_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2605 const char *desc, uint32_t flag, bool set_flag)
2607 struct ctdb_node_map *nodemap;
2610 nodemap = get_nodemap(ctdb, false);
2611 if (nodemap == NULL) {
2615 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2616 if (set_flag == flag_is_set) {
2618 fprintf(stderr, "Node %u is already %s\n",
2619 ctdb->cmd_pnn, desc);
2621 fprintf(stderr, "Node %u is not %s\n",
2622 ctdb->cmd_pnn, desc);
2630 static void wait_for_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2631 uint32_t flag, bool set_flag)
2633 struct ctdb_node_map *nodemap;
2637 nodemap = get_nodemap(ctdb, true);
2638 if (nodemap == NULL) {
2640 "Failed to get nodemap, trying again\n");
2645 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2646 if (flag_is_set == set_flag) {
2654 struct ipreallocate_state {
2659 static void ipreallocate_handler(uint64_t srvid, TDB_DATA data,
2662 struct ipreallocate_state *state =
2663 (struct ipreallocate_state *)private_data;
2665 if (data.dsize != sizeof(int)) {
2670 state->status = *(int *)data.dptr;
2674 static int ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb)
2676 struct ctdb_srvid_message msg = { 0 };
2677 struct ipreallocate_state state;
2680 msg.pnn = ctdb->pnn;
2681 msg.srvid = next_srvid(ctdb);
2684 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2686 ipreallocate_handler, &state);
2692 ret = ctdb_message_takeover_run(mem_ctx, ctdb->ev,
2694 CTDB_BROADCAST_CONNECTED,
2700 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done,
2706 if (state.status >= 0) {
2715 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2720 static int control_disable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2721 int argc, const char **argv)
2729 ret = check_flags(mem_ctx, ctdb, "disabled",
2730 NODE_FLAGS_PERMANENTLY_DISABLED, true);
2735 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2736 ctdb->cmd_pnn, TIMEOUT(),
2737 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2740 "Failed to set DISABLED flag on node %u\n",
2745 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, true);
2746 return ipreallocate(mem_ctx, ctdb);
2749 static int control_enable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2750 int argc, const char **argv)
2758 ret = check_flags(mem_ctx, ctdb, "disabled",
2759 NODE_FLAGS_PERMANENTLY_DISABLED, false);
2764 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2765 ctdb->cmd_pnn, TIMEOUT(),
2766 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2768 fprintf(stderr, "Failed to reset DISABLED flag on node %u\n",
2773 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, false);
2774 return ipreallocate(mem_ctx, ctdb);
2777 static int control_stop(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2778 int argc, const char **argv)
2786 ret = check_flags(mem_ctx, ctdb, "stopped",
2787 NODE_FLAGS_STOPPED, true);
2792 ret = ctdb_ctrl_stop_node(mem_ctx, ctdb->ev, ctdb->client,
2793 ctdb->cmd_pnn, TIMEOUT());
2795 fprintf(stderr, "Failed to stop node %u\n", ctdb->cmd_pnn);
2799 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, true);
2800 return ipreallocate(mem_ctx, ctdb);
2803 static int control_continue(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2804 int argc, const char **argv)
2812 ret = check_flags(mem_ctx, ctdb, "stopped",
2813 NODE_FLAGS_STOPPED, false);
2818 ret = ctdb_ctrl_continue_node(mem_ctx, ctdb->ev, ctdb->client,
2819 ctdb->cmd_pnn, TIMEOUT());
2821 fprintf(stderr, "Failed to continue stopped node %u\n",
2826 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, false);
2827 return ipreallocate(mem_ctx, ctdb);
2830 static int control_ban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2831 int argc, const char **argv)
2833 struct ctdb_ban_state ban_state;
2840 ret = check_flags(mem_ctx, ctdb, "banned",
2841 NODE_FLAGS_BANNED, true);
2846 ban_state.pnn = ctdb->cmd_pnn;
2847 ban_state.time = strtoul(argv[0], NULL, 0);
2849 if (ban_state.time == 0) {
2850 fprintf(stderr, "Ban time cannot be zero\n");
2854 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2855 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2857 fprintf(stderr, "Failed to ban node %u\n", ctdb->cmd_pnn);
2861 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, true);
2862 return ipreallocate(mem_ctx, ctdb);
2866 static int control_unban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2867 int argc, const char **argv)
2869 struct ctdb_ban_state ban_state;
2876 ret = check_flags(mem_ctx, ctdb, "banned",
2877 NODE_FLAGS_BANNED, false);
2882 ban_state.pnn = ctdb->cmd_pnn;
2885 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2886 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2888 fprintf(stderr, "Failed to unban node %u\n", ctdb->cmd_pnn);
2892 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, false);
2893 return ipreallocate(mem_ctx, ctdb);
2897 static int control_shutdown(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2898 int argc, const char **argv)
2906 ret = ctdb_ctrl_shutdown(mem_ctx, ctdb->ev, ctdb->client,
2907 ctdb->cmd_pnn, TIMEOUT());
2909 fprintf(stderr, "Unable to shutdown node %u\n", ctdb->cmd_pnn);
2916 static int get_generation(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2917 uint32_t *generation)
2921 struct ctdb_vnn_map *vnnmap;
2925 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2926 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
2928 fprintf(stderr, "Failed to find recovery master\n");
2932 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2933 recmaster, TIMEOUT(), &recmode);
2935 fprintf(stderr, "Failed to get recovery mode from node %u\n",
2940 if (recmode == CTDB_RECOVERY_ACTIVE) {
2945 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
2946 recmaster, TIMEOUT(), &vnnmap);
2948 fprintf(stderr, "Failed to get generation from node %u\n",
2953 if (vnnmap->generation == INVALID_GENERATION) {
2954 talloc_free(vnnmap);
2959 *generation = vnnmap->generation;
2960 talloc_free(vnnmap);
2965 static int control_recover(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2966 int argc, const char **argv)
2968 uint32_t generation, next_generation;
2975 ret = get_generation(mem_ctx, ctdb, &generation);
2980 ret = ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
2981 ctdb->cmd_pnn, TIMEOUT(),
2982 CTDB_RECOVERY_ACTIVE);
2984 fprintf(stderr, "Failed to set recovery mode active\n");
2989 ret = get_generation(mem_ctx, ctdb, &next_generation);
2992 "Failed to confirm end of recovery\n");
2996 if (next_generation != generation) {
3006 static int control_ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3007 int argc, const char **argv)
3010 usage("ipreallocate");
3013 return ipreallocate(mem_ctx, ctdb);
3016 static int control_isnotrecmaster(TALLOC_CTX *mem_ctx,
3017 struct ctdb_context *ctdb,
3018 int argc, const char **argv)
3024 usage("isnotrecmaster");
3027 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
3028 ctdb->pnn, TIMEOUT(), &recmaster);
3030 fprintf(stderr, "Failed to get recmaster\n");
3034 if (recmaster != ctdb->pnn) {
3035 printf("this node is not the recmaster\n");
3039 printf("this node is the recmaster\n");
3043 static int control_gratarp(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3044 int argc, const char **argv)
3046 struct ctdb_addr_info addr_info;
3053 if (! parse_ip(argv[0], NULL, 0, &addr_info.addr)) {
3054 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3057 addr_info.iface = argv[1];
3059 ret = ctdb_ctrl_send_gratuitous_arp(mem_ctx, ctdb->ev, ctdb->client,
3060 ctdb->cmd_pnn, TIMEOUT(),
3063 fprintf(stderr, "Unable to send gratuitous arp from node %u\n",
3071 static int control_tickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3072 int argc, const char **argv)
3074 ctdb_sock_addr src, dst;
3077 if (argc != 0 && argc != 2) {
3082 struct ctdb_connection *clist;
3086 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3092 for (i=0; i<count; i++) {
3093 ret = ctdb_sys_send_tcp(&clist[i].src,
3103 if (num_failed > 0) {
3104 fprintf(stderr, "Failed to send %d tickles\n",
3113 if (! parse_ip_port(argv[0], &src)) {
3114 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3118 if (! parse_ip_port(argv[1], &dst)) {
3119 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3123 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
3125 fprintf(stderr, "Failed to send tickle ack\n");
3132 static int control_gettickles(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3133 int argc, const char **argv)
3135 ctdb_sock_addr addr;
3136 struct ctdb_tickle_list *tickles;
3140 if (argc < 1 || argc > 2) {
3141 usage("gettickles");
3145 port = strtoul(argv[1], NULL, 10);
3148 if (! parse_ip(argv[0], NULL, port, &addr)) {
3149 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3153 ret = ctdb_ctrl_get_tcp_tickle_list(mem_ctx, ctdb->ev, ctdb->client,
3154 ctdb->cmd_pnn, TIMEOUT(), &addr,
3157 fprintf(stderr, "Failed to get list of connections\n");
3161 if (options.machinereadable) {
3162 printf("%s%s%s%s%s%s%s%s%s\n",
3164 "Source IP", options.sep,
3165 "Port", options.sep,
3166 "Destiation IP", options.sep,
3167 "Port", options.sep);
3168 for (i=0; i<tickles->num; i++) {
3169 printf("%s%s%s%u%s%s%s%u%s\n", options.sep,
3170 ctdb_sock_addr_to_string(
3171 mem_ctx, &tickles->conn[i].src),
3173 ntohs(tickles->conn[i].src.ip.sin_port),
3175 ctdb_sock_addr_to_string(
3176 mem_ctx, &tickles->conn[i].dst),
3178 ntohs(tickles->conn[i].dst.ip.sin_port),
3182 printf("Connections for IP: %s\n",
3183 ctdb_sock_addr_to_string(mem_ctx, &tickles->addr));
3184 printf("Num connections: %u\n", tickles->num);
3185 for (i=0; i<tickles->num; i++) {
3186 printf("SRC: %s:%u DST: %s:%u\n",
3187 ctdb_sock_addr_to_string(
3188 mem_ctx, &tickles->conn[i].src),
3189 ntohs(tickles->conn[i].src.ip.sin_port),
3190 ctdb_sock_addr_to_string(
3191 mem_ctx, &tickles->conn[i].dst),
3192 ntohs(tickles->conn[i].dst.ip.sin_port));
3196 talloc_free(tickles);
3200 typedef void (*clist_request_func)(struct ctdb_req_control *request,
3201 struct ctdb_connection *conn);
3203 typedef int (*clist_reply_func)(struct ctdb_reply_control *reply);
3205 struct process_clist_state {
3206 struct ctdb_connection *clist;
3208 int num_failed, num_total;
3209 clist_reply_func reply_func;
3212 static void process_clist_done(struct tevent_req *subreq);
3214 static struct tevent_req *process_clist_send(
3215 TALLOC_CTX *mem_ctx,
3216 struct ctdb_context *ctdb,
3217 struct ctdb_connection *clist,
3219 clist_request_func request_func,
3220 clist_reply_func reply_func)
3222 struct tevent_req *req, *subreq;
3223 struct process_clist_state *state;
3224 struct ctdb_req_control request;
3227 req = tevent_req_create(mem_ctx, &state, struct process_clist_state);
3232 state->clist = clist;
3233 state->count = count;
3234 state->reply_func = reply_func;
3236 for (i=0; i<count; i++) {
3237 request_func(&request, &clist[i]);
3238 subreq = ctdb_client_control_send(state, ctdb->ev,
3239 ctdb->client, ctdb->cmd_pnn,
3240 TIMEOUT(), &request);
3241 if (tevent_req_nomem(subreq, req)) {
3242 return tevent_req_post(req, ctdb->ev);
3244 tevent_req_set_callback(subreq, process_clist_done, req);
3250 static void process_clist_done(struct tevent_req *subreq)
3252 struct tevent_req *req = tevent_req_callback_data(
3253 subreq, struct tevent_req);
3254 struct process_clist_state *state = tevent_req_data(
3255 req, struct process_clist_state);
3256 struct ctdb_reply_control *reply;
3260 status = ctdb_client_control_recv(subreq, NULL, state, &reply);
3261 TALLOC_FREE(subreq);
3263 state->num_failed += 1;
3267 ret = state->reply_func(reply);
3269 state->num_failed += 1;
3274 state->num_total += 1;
3275 if (state->num_total == state->count) {
3276 tevent_req_done(req);
3280 static int process_clist_recv(struct tevent_req *req)
3282 struct process_clist_state *state = tevent_req_data(
3283 req, struct process_clist_state);
3285 return state->num_failed;
3288 static int control_addtickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3289 int argc, const char **argv)
3291 struct ctdb_connection conn;
3294 if (argc != 0 && argc != 2) {
3299 struct ctdb_connection *clist;
3300 struct tevent_req *req;
3303 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3311 req = process_clist_send(mem_ctx, ctdb, clist, count,
3312 ctdb_req_control_tcp_add_delayed_update,
3313 ctdb_reply_control_tcp_add_delayed_update);
3319 tevent_req_poll(req, ctdb->ev);
3322 ret = process_clist_recv(req);
3324 fprintf(stderr, "Failed to add %d tickles\n", ret);
3331 if (! parse_ip_port(argv[0], &conn.src)) {
3332 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3335 if (! parse_ip_port(argv[1], &conn.dst)) {
3336 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3340 ret = ctdb_ctrl_tcp_add_delayed_update(mem_ctx, ctdb->ev,
3341 ctdb->client, ctdb->cmd_pnn,
3344 fprintf(stderr, "Failed to register connection\n");
3351 static int control_deltickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3352 int argc, const char **argv)
3354 struct ctdb_connection conn;
3357 if (argc != 0 && argc != 2) {
3362 struct ctdb_connection *clist;
3363 struct tevent_req *req;
3366 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3374 req = process_clist_send(mem_ctx, ctdb, clist, count,
3375 ctdb_req_control_tcp_remove,
3376 ctdb_reply_control_tcp_remove);
3382 tevent_req_poll(req, ctdb->ev);
3385 ret = process_clist_recv(req);
3387 fprintf(stderr, "Failed to remove %d tickles\n", ret);
3394 if (! parse_ip_port(argv[0], &conn.src)) {
3395 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3398 if (! parse_ip_port(argv[1], &conn.dst)) {
3399 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3403 ret = ctdb_ctrl_tcp_remove(mem_ctx, ctdb->ev, ctdb->client,
3404 ctdb->cmd_pnn, TIMEOUT(), &conn);
3406 fprintf(stderr, "Failed to unregister connection\n");
3413 static int control_check_srvids(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3414 int argc, const char **argv)
3421 usage("check_srvids");
3424 srvid = talloc_array(mem_ctx, uint64_t, argc);
3425 if (srvid == NULL) {
3426 fprintf(stderr, "Memory allocation error\n");
3430 for (i=0; i<argc; i++) {
3431 srvid[i] = strtoull(argv[i], NULL, 0);
3434 ret = ctdb_ctrl_check_srvids(mem_ctx, ctdb->ev, ctdb->client,
3435 ctdb->cmd_pnn, TIMEOUT(), srvid, argc,
3438 fprintf(stderr, "Failed to check srvids on node %u\n",
3443 for (i=0; i<argc; i++) {
3444 printf("SRVID 0x%" PRIx64 " %s\n", srvid[i],
3445 (result[i] ? "exists" : "does not exist"));
3451 static int control_listnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3452 int argc, const char **argv)
3454 struct ctdb_node_map *nodemap;
3461 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
3462 if (nodemap == NULL) {
3466 for (i=0; i<nodemap->num; i++) {
3467 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
3471 if (options.machinereadable) {
3472 printf("%s%u%s%s%s\n", options.sep,
3473 nodemap->node[i].pnn, options.sep,
3474 ctdb_sock_addr_to_string(
3475 mem_ctx, &nodemap->node[i].addr),
3479 ctdb_sock_addr_to_string(
3480 mem_ctx, &nodemap->node[i].addr));
3487 static bool nodemap_identical(struct ctdb_node_map *nodemap1,
3488 struct ctdb_node_map *nodemap2)
3492 if (nodemap1->num != nodemap2->num) {
3496 for (i=0; i<nodemap1->num; i++) {
3497 struct ctdb_node_and_flags *n1, *n2;
3499 n1 = &nodemap1->node[i];
3500 n2 = &nodemap2->node[i];
3502 if ((n1->pnn != n2->pnn) ||
3503 (n1->flags != n2->flags) ||
3504 ! ctdb_sock_addr_same_ip(&n1->addr, &n2->addr)) {
3512 static int check_node_file_changes(TALLOC_CTX *mem_ctx,
3513 struct ctdb_node_map *nm,
3514 struct ctdb_node_map *fnm,
3518 bool check_failed = false;
3522 for (i=0; i<nm->num; i++) {
3523 if (i >= fnm->num) {
3525 "Node %u (%s) missing from nodes file\n",
3527 ctdb_sock_addr_to_string(
3528 mem_ctx, &nm->node[i].addr));
3529 check_failed = true;
3532 if (nm->node[i].flags & NODE_FLAGS_DELETED &&
3533 fnm->node[i].flags & NODE_FLAGS_DELETED) {
3534 /* Node remains deleted */
3538 if (! (nm->node[i].flags & NODE_FLAGS_DELETED) &&
3539 ! (fnm->node[i].flags & NODE_FLAGS_DELETED)) {
3540 /* Node not newly nor previously deleted */
3541 if (! ctdb_same_ip(&nm->node[i].addr,
3542 &fnm->node[i].addr)) {
3544 "Node %u has changed IP address"
3545 " (was %s, now %s)\n",
3547 ctdb_sock_addr_to_string(
3548 mem_ctx, &nm->node[i].addr),
3549 ctdb_sock_addr_to_string(
3550 mem_ctx, &fnm->node[i].addr));
3551 check_failed = true;
3553 if (nm->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3555 "WARNING: Node %u is disconnected."
3556 " You MUST fix this node manually!\n",
3563 if (fnm->node[i].flags & NODE_FLAGS_DELETED) {
3564 /* Node is being deleted */
3565 printf("Node %u is DELETED\n", nm->node[i].pnn);
3567 if (! (nm->node[i].flags & NODE_FLAGS_DISCONNECTED)) {
3569 "ERROR: Node %u is still connected\n",
3571 check_failed = true;
3576 if (nm->node[i].flags & NODE_FLAGS_DELETED) {
3577 /* Node was previously deleted */
3578 printf("Node %u is UNDELETED\n", nm->node[i].pnn);
3585 "ERROR: Nodes will not be reloaded due to previous error\n");
3589 /* Leftover nodes in file are NEW */
3590 for (; i < fnm->num; i++) {
3591 printf("Node %u is NEW\n", fnm->node[i].pnn);
3598 struct disable_recoveries_state {
3606 static void disable_recoveries_handler(uint64_t srvid, TDB_DATA data,
3609 struct disable_recoveries_state *state =
3610 (struct disable_recoveries_state *)private_data;
3613 if (data.dsize != sizeof(int)) {
3618 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
3619 ret = *(int *)data.dptr;
3621 state->status = ret;
3625 for (i=0; i<state->node_count; i++) {
3626 if (state->pnn_list[i] == ret) {
3627 state->reply[i] = true;
3633 for (i=0; i<state->node_count; i++) {
3634 if (! state->reply[i]) {
3635 state->done = false;
3641 static int disable_recoveries(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3642 uint32_t timeout, uint32_t *pnn_list, int count)
3644 struct ctdb_disable_message disable = { 0 };
3645 struct disable_recoveries_state state;
3648 disable.pnn = ctdb->pnn;
3649 disable.srvid = next_srvid(ctdb);
3650 disable.timeout = timeout;
3652 state.pnn_list = pnn_list;
3653 state.node_count = count;
3656 state.reply = talloc_zero_array(mem_ctx, bool, count);
3657 if (state.reply == NULL) {
3661 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
3663 disable_recoveries_handler,
3669 for (i=0; i<count; i++) {
3670 ret = ctdb_message_disable_recoveries(mem_ctx, ctdb->ev,
3679 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
3681 fprintf(stderr, "Timed out waiting to disable recoveries\n");
3683 ret = (state.status >= 0 ? 0 : 1);
3687 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
3688 disable.srvid, &state);
3692 static int control_reloadnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3693 int argc, const char **argv)
3695 struct ctdb_node_map *nodemap = NULL;
3696 struct ctdb_node_map *file_nodemap;
3697 struct ctdb_node_map *remote_nodemap;
3698 struct ctdb_req_control request;
3699 struct ctdb_reply_control **reply;
3705 nodemap = get_nodemap(ctdb, false);
3706 if (nodemap == NULL) {
3710 file_nodemap = read_nodes_file(mem_ctx, ctdb->pnn);
3711 if (file_nodemap == NULL) {
3715 for (i=0; i<nodemap->num; i++) {
3716 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3720 ret = ctdb_ctrl_get_nodes_file(mem_ctx, ctdb->ev, ctdb->client,
3721 nodemap->node[i].pnn, TIMEOUT(),
3725 "ERROR: Failed to get nodes file from node %u\n",
3726 nodemap->node[i].pnn);
3730 if (! nodemap_identical(file_nodemap, remote_nodemap)) {
3732 "ERROR: Nodes file on node %u differs"
3733 " from current node (%u)\n",
3734 nodemap->node[i].pnn, ctdb->pnn);
3739 ret = check_node_file_changes(mem_ctx, nodemap, file_nodemap, &reload);
3745 fprintf(stderr, "No change in nodes file,"
3746 " skipping unnecessary reload\n");
3750 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
3751 mem_ctx, &pnn_list);
3753 fprintf(stderr, "Memory allocation error\n");
3757 ret = disable_recoveries(mem_ctx, ctdb, 2*options.timelimit,
3760 fprintf(stderr, "Failed to disable recoveries\n");
3764 ctdb_req_control_reload_nodes_file(&request);
3765 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3766 pnn_list, count, TIMEOUT(),
3767 &request, NULL, &reply);
3769 bool failed = false;
3771 for (i=0; i<count; i++) {
3772 ret = ctdb_reply_control_reload_nodes_file(reply[i]);
3775 "Node %u failed to reload nodes\n",
3782 "You MUST fix failed nodes manually!\n");
3786 ret = disable_recoveries(mem_ctx, ctdb, 0, pnn_list, count);
3788 fprintf(stderr, "Failed to enable recoveries\n");
3795 static int moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3796 ctdb_sock_addr *addr, uint32_t pnn)
3798 struct ctdb_public_ip_list *pubip_list;
3799 struct ctdb_public_ip pubip;
3800 struct ctdb_node_map *nodemap;
3801 struct ctdb_req_control request;
3805 ret = ctdb_message_disable_ip_check(mem_ctx, ctdb->ev, ctdb->client,
3806 CTDB_BROADCAST_CONNECTED,
3807 2*options.timelimit);
3809 fprintf(stderr, "Failed to disable IP check\n");
3813 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3814 pnn, TIMEOUT(), false, &pubip_list);
3816 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3821 for (i=0; i<pubip_list->num; i++) {
3822 if (ctdb_same_ip(addr, &pubip_list->ip[i].addr)) {
3827 if (i == pubip_list->num) {
3828 fprintf(stderr, "Node %u CANNOT host IP address %s\n",
3829 pnn, ctdb_sock_addr_to_string(mem_ctx, addr));
3833 nodemap = get_nodemap(ctdb, false);
3834 if (nodemap == NULL) {
3838 count = list_of_active_nodes(nodemap, pnn, mem_ctx, &pnn_list);
3840 fprintf(stderr, "Memory allocation error\n");
3846 ctdb_req_control_release_ip(&request, &pubip);
3848 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3849 pnn_list, count, TIMEOUT(),
3850 &request, NULL, NULL);
3852 fprintf(stderr, "Failed to release IP on nodes\n");
3856 ret = ctdb_ctrl_takeover_ip(mem_ctx, ctdb->ev, ctdb->client,
3857 pnn, TIMEOUT(), &pubip);
3859 fprintf(stderr, "Failed to takeover IP on node %u\n", pnn);
3866 static int control_moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3867 int argc, const char **argv)
3869 ctdb_sock_addr addr;
3871 int ret, retries = 0;
3877 if (! parse_ip(argv[0], NULL, 0, &addr)) {
3878 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3882 pnn = strtoul(argv[1], NULL, 10);
3883 if (pnn == CTDB_UNKNOWN_PNN) {
3884 fprintf(stderr, "Invalid PNN %s\n", argv[1]);
3888 while (retries < 5) {
3889 ret = moveip(mem_ctx, ctdb, &addr, pnn);
3899 fprintf(stderr, "Failed to move IP %s to node %u\n",
3907 static int rebalancenode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3912 ret = ctdb_message_rebalance_node(mem_ctx, ctdb->ev, ctdb->client,
3913 CTDB_BROADCAST_CONNECTED, pnn);
3916 "Failed to ask recovery master to distribute IPs\n");
3923 static int control_addip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3924 int argc, const char **argv)
3926 ctdb_sock_addr addr;
3927 struct ctdb_public_ip_list *pubip_list;
3928 struct ctdb_addr_info addr_info;
3930 int ret, i, retries = 0;
3936 if (! parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
3937 fprintf(stderr, "Invalid IP/Mask %s\n", argv[0]);
3941 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3942 ctdb->cmd_pnn, TIMEOUT(),
3943 false, &pubip_list);
3945 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3950 for (i=0; i<pubip_list->num; i++) {
3951 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3952 fprintf(stderr, "Node already knows about IP %s\n",
3953 ctdb_sock_addr_to_string(mem_ctx, &addr));
3958 addr_info.addr = addr;
3959 addr_info.mask = mask;
3960 addr_info.iface = argv[1];
3962 while (retries < 5) {
3963 ret = ctdb_ctrl_add_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3964 ctdb->cmd_pnn, TIMEOUT(),
3975 fprintf(stderr, "Failed to add public IP to node %u."
3976 " Giving up\n", ctdb->cmd_pnn);
3980 ret = rebalancenode(mem_ctx, ctdb, ctdb->cmd_pnn);
3988 static int control_delip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3989 int argc, const char **argv)
3991 ctdb_sock_addr addr;
3992 struct ctdb_public_ip_list *pubip_list;
3993 struct ctdb_addr_info addr_info;
4000 if (! parse_ip(argv[0], NULL, 0, &addr)) {
4001 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
4005 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
4006 ctdb->cmd_pnn, TIMEOUT(),
4007 false, &pubip_list);
4009 fprintf(stderr, "Failed to get Public IPs from node %u\n",
4014 for (i=0; i<pubip_list->num; i++) {
4015 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
4020 if (i == pubip_list->num) {
4021 fprintf(stderr, "Node does not know about IP address %s\n",
4022 ctdb_sock_addr_to_string(mem_ctx, &addr));
4026 addr_info.addr = addr;
4028 addr_info.iface = NULL;
4030 ret = ctdb_ctrl_del_public_ip(mem_ctx, ctdb->ev, ctdb->client,
4031 ctdb->cmd_pnn, TIMEOUT(), &addr_info);
4033 fprintf(stderr, "Failed to delete public IP from node %u\n",
4041 #define DB_VERSION 3
4042 #define MAX_DB_NAME 64
4043 #define MAX_REC_BUFFER_SIZE (100*1000)
4046 unsigned long version;
4048 unsigned long flags;
4051 char name[MAX_DB_NAME];
4054 struct backup_state {
4055 TALLOC_CTX *mem_ctx;
4056 struct ctdb_rec_buffer *recbuf;
4059 unsigned int nbuf, nrec;
4062 static int backup_handler(uint32_t reqid, struct ctdb_ltdb_header *header,
4063 TDB_DATA key, TDB_DATA data, void *private_data)
4065 struct backup_state *state = (struct backup_state *)private_data;
4069 if (state->recbuf == NULL) {
4070 state->recbuf = ctdb_rec_buffer_init(state->mem_ctx,
4072 if (state->recbuf == NULL) {
4077 ret = ctdb_rec_buffer_add(state->recbuf, state->recbuf, reqid,
4083 len = ctdb_rec_buffer_len(state->recbuf);
4084 if (len < MAX_REC_BUFFER_SIZE) {
4088 ret = ctdb_rec_buffer_write(state->recbuf, state->fd);
4090 fprintf(stderr, "Failed to write records to backup file\n");
4095 state->nrec += state->recbuf->count;
4096 TALLOC_FREE(state->recbuf);
4101 static int control_backupdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4102 int argc, const char **argv)
4104 const char *db_name;
4105 struct ctdb_db_context *db;
4108 struct backup_state state;
4109 struct db_header db_hdr;
4116 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4120 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4123 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4127 fd = open(argv[1], O_RDWR|O_CREAT, 0600);
4130 fprintf(stderr, "Failed to open file %s for writing\n",
4135 /* Write empty header first */
4136 ZERO_STRUCT(db_hdr);
4137 ret = write(fd, &db_hdr, sizeof(struct db_header));
4141 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4145 state.mem_ctx = mem_ctx;
4146 state.recbuf = NULL;
4151 ret = ctdb_db_traverse(db, true, false, backup_handler, &state);
4153 fprintf(stderr, "Failed to collect records from DB %s\n",
4159 if (state.recbuf != NULL) {
4160 ret = ctdb_rec_buffer_write(state.recbuf, state.fd);
4163 "Failed to write records to backup file\n");
4169 state.nrec += state.recbuf->count;
4170 TALLOC_FREE(state.recbuf);
4173 db_hdr.version = DB_VERSION;
4174 db_hdr.timestamp = time(NULL);
4175 db_hdr.flags = db_flags;
4176 db_hdr.nbuf = state.nbuf;
4177 db_hdr.nrec = state.nrec;
4178 strncpy(db_hdr.name, db_name, MAX_DB_NAME-1);
4180 lseek(fd, 0, SEEK_SET);
4181 ret = write(fd, &db_hdr, sizeof(struct db_header));
4185 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4190 printf("Database backed up to %s\n", argv[1]);
4194 static int control_restoredb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4195 int argc, const char **argv)
4197 const char *db_name = NULL;
4198 struct ctdb_db_context *db;
4199 struct db_header db_hdr;
4200 struct ctdb_node_map *nodemap;
4201 struct ctdb_req_control request;
4202 struct ctdb_reply_control **reply;
4203 struct ctdb_transdb wipedb;
4204 struct ctdb_pulldb_ext pulldb;
4205 struct ctdb_rec_buffer *recbuf;
4206 uint32_t generation;
4212 if (argc < 1 || argc > 2) {
4216 fd = open(argv[0], O_RDONLY, 0600);
4219 fprintf(stderr, "Failed to open file %s for reading\n",
4228 ret = read(fd, &db_hdr, sizeof(struct db_header));
4232 fprintf(stderr, "Failed to read db header from file %s\n",
4236 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4238 if (db_hdr.version != DB_VERSION) {
4240 "Wrong version of backup file, expected %u, got %lu\n",
4241 DB_VERSION, db_hdr.version);
4246 if (db_name == NULL) {
4247 db_name = db_hdr.name;
4250 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4251 localtime(&db_hdr.timestamp));
4252 printf("Restoring database %s from backup @ %s\n", db_name, timebuf);
4254 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4257 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4262 nodemap = get_nodemap(ctdb, false);
4263 if (nodemap == NULL) {
4264 fprintf(stderr, "Failed to get nodemap\n");
4269 ret = get_generation(mem_ctx, ctdb, &generation);
4271 fprintf(stderr, "Failed to get current generation\n");
4276 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4283 wipedb.db_id = ctdb_db_id(db);
4284 wipedb.tid = generation;
4286 ctdb_req_control_db_freeze(&request, wipedb.db_id);
4287 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4288 ctdb->client, pnn_list, count,
4289 TIMEOUT(), &request, NULL, NULL);
4295 ctdb_req_control_db_transaction_start(&request, &wipedb);
4296 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4297 pnn_list, count, TIMEOUT(),
4298 &request, NULL, NULL);
4303 ctdb_req_control_wipe_database(&request, &wipedb);
4304 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4305 pnn_list, count, TIMEOUT(),
4306 &request, NULL, NULL);
4311 pulldb.db_id = ctdb_db_id(db);
4313 pulldb.srvid = SRVID_CTDB_PUSHDB;
4315 ctdb_req_control_db_push_start(&request, &pulldb);
4316 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4317 pnn_list, count, TIMEOUT(),
4318 &request, NULL, NULL);
4323 for (i=0; i<db_hdr.nbuf; i++) {
4324 struct ctdb_req_message message;
4327 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4332 data.dsize = ctdb_rec_buffer_len(recbuf);
4333 data.dptr = talloc_size(mem_ctx, data.dsize);
4334 if (data.dptr == NULL) {
4338 ctdb_rec_buffer_push(recbuf, data.dptr);
4340 message.srvid = pulldb.srvid;
4341 message.data.data = data;
4343 ret = ctdb_client_message_multi(mem_ctx, ctdb->ev,
4351 talloc_free(recbuf);
4352 talloc_free(data.dptr);
4355 ctdb_req_control_db_push_confirm(&request, pulldb.db_id);
4356 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4357 pnn_list, count, TIMEOUT(),
4358 &request, NULL, &reply);
4363 for (i=0; i<count; i++) {
4364 uint32_t num_records;
4366 ret = ctdb_reply_control_db_push_confirm(reply[i],
4369 fprintf(stderr, "Invalid response from node %u\n",
4374 if (num_records != db_hdr.nrec) {
4375 fprintf(stderr, "Node %u received %u of %lu records\n",
4376 pnn_list[i], num_records, db_hdr.nrec);
4381 ctdb_req_control_db_set_healthy(&request, wipedb.db_id);
4382 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4383 pnn_list, count, TIMEOUT(),
4384 &request, NULL, NULL);
4389 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4390 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4391 pnn_list, count, TIMEOUT(),
4392 &request, NULL, NULL);
4397 ctdb_req_control_db_thaw(&request, wipedb.db_id);
4398 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4399 ctdb->client, pnn_list, count,
4400 TIMEOUT(), &request, NULL, NULL);
4405 printf("Database %s restored\n", db_name);
4411 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4412 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4416 struct dumpdbbackup_state {
4417 ctdb_rec_parser_func_t parser;
4418 struct dump_record_state sub_state;
4421 static int dumpdbbackup_handler(uint32_t reqid,
4422 struct ctdb_ltdb_header *header,
4423 TDB_DATA key, TDB_DATA data,
4426 struct dumpdbbackup_state *state =
4427 (struct dumpdbbackup_state *)private_data;
4428 struct ctdb_ltdb_header hdr;
4431 ret = ctdb_ltdb_header_extract(&data, &hdr);
4436 return state->parser(reqid, &hdr, key, data, &state->sub_state);
4439 static int control_dumpdbbackup(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4440 int argc, const char **argv)
4442 struct db_header db_hdr;
4444 struct dumpdbbackup_state state;
4448 usage("dumpbackup");
4451 fd = open(argv[0], O_RDONLY, 0600);
4454 fprintf(stderr, "Failed to open file %s for reading\n",
4459 ret = read(fd, &db_hdr, sizeof(struct db_header));
4463 fprintf(stderr, "Failed to read db header from file %s\n",
4467 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4469 if (db_hdr.version != DB_VERSION) {
4471 "Wrong version of backup file, expected %u, got %lu\n",
4472 DB_VERSION, db_hdr.version);
4477 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4478 localtime(&db_hdr.timestamp));
4479 printf("Dumping database %s from backup @ %s\n",
4480 db_hdr.name, timebuf);
4482 state.parser = dump_record;
4483 state.sub_state.count = 0;
4485 for (i=0; i<db_hdr.nbuf; i++) {
4486 struct ctdb_rec_buffer *recbuf;
4488 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4490 fprintf(stderr, "Failed to read records\n");
4495 ret = ctdb_rec_buffer_traverse(recbuf, dumpdbbackup_handler,
4498 fprintf(stderr, "Failed to dump records\n");
4505 printf("Dumped %u record(s)\n", state.sub_state.count);
4509 static int control_wipedb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4510 int argc, const char **argv)
4512 const char *db_name;
4513 struct ctdb_db_context *db;
4516 struct ctdb_node_map *nodemap;
4517 struct ctdb_req_control request;
4518 struct ctdb_transdb wipedb;
4519 uint32_t generation;
4527 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4531 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4534 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4538 nodemap = get_nodemap(ctdb, false);
4539 if (nodemap == NULL) {
4540 fprintf(stderr, "Failed to get nodemap\n");
4544 ret = get_generation(mem_ctx, ctdb, &generation);
4546 fprintf(stderr, "Failed to get current generation\n");
4550 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4556 ctdb_req_control_db_freeze(&request, db_id);
4557 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4558 ctdb->client, pnn_list, count,
4559 TIMEOUT(), &request, NULL, NULL);
4564 wipedb.db_id = db_id;
4565 wipedb.tid = generation;
4567 ctdb_req_control_db_transaction_start(&request, &wipedb);
4568 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4569 pnn_list, count, TIMEOUT(),
4570 &request, NULL, NULL);
4575 ctdb_req_control_wipe_database(&request, &wipedb);
4576 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4577 pnn_list, count, TIMEOUT(),
4578 &request, NULL, NULL);
4583 ctdb_req_control_db_set_healthy(&request, db_id);
4584 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4585 pnn_list, count, TIMEOUT(),
4586 &request, NULL, NULL);
4591 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4592 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4593 pnn_list, count, TIMEOUT(),
4594 &request, NULL, NULL);
4599 ctdb_req_control_db_thaw(&request, db_id);
4600 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4601 ctdb->client, pnn_list, count,
4602 TIMEOUT(), &request, NULL, NULL);
4607 printf("Database %s wiped\n", db_name);
4612 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4613 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4617 static int control_recmaster(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4618 int argc, const char **argv)
4623 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
4624 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
4629 printf("%u\n", recmaster);
4633 static int control_event(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4634 int argc, const char **argv)
4636 char *t, *event_helper = NULL;
4637 char *eventd_socket = NULL;
4638 const char **new_argv;
4641 t = getenv("CTDB_EVENT_HELPER");
4643 event_helper = talloc_strdup(mem_ctx, t);
4645 event_helper = talloc_asprintf(mem_ctx, "%s/ctdb_event",
4646 CTDB_HELPER_BINDIR);
4649 if (event_helper == NULL) {
4650 fprintf(stderr, "Unable to set event daemon helper\n");
4654 t = getenv("CTDB_SOCKET");
4656 eventd_socket = talloc_asprintf(mem_ctx, "%s/eventd.sock",
4659 eventd_socket = talloc_asprintf(mem_ctx, "%s/eventd.sock",
4663 if (eventd_socket == NULL) {
4664 fprintf(stderr, "Unable to set event daemon socket\n");
4668 new_argv = talloc_array(mem_ctx, const char *, argc + 1);
4669 if (new_argv == NULL) {
4670 fprintf(stderr, "Memory allocation error\n");
4674 new_argv[0] = eventd_socket;
4675 for (i=0; i<argc; i++) {
4676 new_argv[i+1] = argv[i];
4679 return run_helper(mem_ctx, "event daemon helper", event_helper,
4683 static int control_scriptstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4684 int argc, const char **argv)
4686 const char *new_argv[3];
4689 usage("scriptstatus");
4692 new_argv[0] = "status";
4693 new_argv[1] = (argc == 0) ? "monitor" : argv[0];
4696 (void) control_event(mem_ctx, ctdb, 2, new_argv);
4700 static int control_natgw(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4701 int argc, const char **argv)
4703 char *t, *natgw_helper = NULL;
4709 t = getenv("CTDB_NATGW_HELPER");
4711 natgw_helper = talloc_strdup(mem_ctx, t);
4713 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4714 CTDB_HELPER_BINDIR);
4717 if (natgw_helper == NULL) {
4718 fprintf(stderr, "Unable to set NAT gateway helper\n");
4722 return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
4726 static int control_natgwlist(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4727 int argc, const char **argv)
4729 char *t, *natgw_helper = NULL;
4730 const char *cmd_argv[] = { "natgwlist", NULL };
4736 t = getenv("CTDB_NATGW_HELPER");
4738 natgw_helper = talloc_strdup(mem_ctx, t);
4740 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4741 CTDB_HELPER_BINDIR);
4744 if (natgw_helper == NULL) {
4745 fprintf(stderr, "Unable to set NAT gateway helper\n");
4749 return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
4754 * Find the PNN of the current node
4755 * discover the pnn by loading the nodes file and try to bind
4756 * to all addresses one at a time until the ip address is found.
4758 static bool find_node_xpnn(TALLOC_CTX *mem_ctx, uint32_t *pnn)
4760 struct ctdb_node_map *nodemap;
4763 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
4764 if (nodemap == NULL) {
4768 for (i=0; i<nodemap->num; i++) {
4769 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
4772 if (ctdb_sys_have_ip(&nodemap->node[i].addr)) {
4774 *pnn = nodemap->node[i].pnn;
4776 talloc_free(nodemap);
4781 fprintf(stderr, "Failed to detect PNN of the current node.\n");
4782 talloc_free(nodemap);
4786 static int control_getreclock(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4787 int argc, const char **argv)
4789 const char *reclock;
4793 usage("getreclock");
4796 ret = ctdb_ctrl_get_reclock_file(mem_ctx, ctdb->ev, ctdb->client,
4797 ctdb->cmd_pnn, TIMEOUT(), &reclock);
4802 if (reclock != NULL) {
4803 printf("%s\n", reclock);
4809 static int control_setlmasterrole(TALLOC_CTX *mem_ctx,
4810 struct ctdb_context *ctdb,
4811 int argc, const char **argv)
4813 uint32_t lmasterrole = 0;
4817 usage("setlmasterrole");
4820 if (strcmp(argv[0], "on") == 0) {
4822 } else if (strcmp(argv[0], "off") == 0) {
4825 usage("setlmasterrole");
4828 ret = ctdb_ctrl_set_lmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4829 ctdb->cmd_pnn, TIMEOUT(), lmasterrole);
4837 static int control_setrecmasterrole(TALLOC_CTX *mem_ctx,
4838 struct ctdb_context *ctdb,
4839 int argc, const char **argv)
4841 uint32_t recmasterrole = 0;
4845 usage("setrecmasterrole");
4848 if (strcmp(argv[0], "on") == 0) {
4850 } else if (strcmp(argv[0], "off") == 0) {
4853 usage("setrecmasterrole");
4856 ret = ctdb_ctrl_set_recmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4857 ctdb->cmd_pnn, TIMEOUT(),
4866 static int control_setdbreadonly(TALLOC_CTX *mem_ctx,
4867 struct ctdb_context *ctdb,
4868 int argc, const char **argv)
4875 usage("setdbreadonly");
4878 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
4882 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
4883 fprintf(stderr, "Cannot set READONLY on persistent DB\n");
4887 ret = ctdb_ctrl_set_db_readonly(mem_ctx, ctdb->ev, ctdb->client,
4888 ctdb->cmd_pnn, TIMEOUT(), db_id);
4896 static int control_setdbsticky(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4897 int argc, const char **argv)
4904 usage("setdbsticky");
4907 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
4911 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
4912 fprintf(stderr, "Cannot set STICKY on persistent DB\n");
4916 ret = ctdb_ctrl_set_db_sticky(mem_ctx, ctdb->ev, ctdb->client,
4917 ctdb->cmd_pnn, TIMEOUT(), db_id);
4925 static int control_pfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4926 int argc, const char **argv)
4928 const char *db_name;
4929 struct ctdb_db_context *db;
4930 struct ctdb_transaction_handle *h;
4935 if (argc < 2 || argc > 3) {
4939 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
4943 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
4944 fprintf(stderr, "DB %s is not a persistent database\n",
4949 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4952 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4956 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
4958 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
4962 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
4963 TIMEOUT(), db, true, &h);
4965 fprintf(stderr, "Failed to start transaction on db %s\n",
4970 ret = ctdb_transaction_fetch_record(h, key, mem_ctx, &data);
4972 fprintf(stderr, "Failed to read record for key %s\n",
4974 ctdb_transaction_cancel(h);
4978 printf("%.*s\n", (int)data.dsize, data.dptr);
4980 ctdb_transaction_cancel(h);
4984 static int control_pstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4985 int argc, const char **argv)
4987 const char *db_name;
4988 struct ctdb_db_context *db;
4989 struct ctdb_transaction_handle *h;
4998 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5002 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5003 fprintf(stderr, "DB %s is not a persistent database\n",
5008 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5011 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5015 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5017 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5021 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5023 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5027 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5028 TIMEOUT(), db, false, &h);
5030 fprintf(stderr, "Failed to start transaction on db %s\n",
5035 ret = ctdb_transaction_store_record(h, key, data);
5037 fprintf(stderr, "Failed to store record for key %s\n",
5039 ctdb_transaction_cancel(h);
5043 ret = ctdb_transaction_commit(h);
5045 fprintf(stderr, "Failed to commit transaction on db %s\n",
5047 ctdb_transaction_cancel(h);
5054 static int control_pdelete(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5055 int argc, const char **argv)
5057 const char *db_name;
5058 struct ctdb_db_context *db;
5059 struct ctdb_transaction_handle *h;
5068 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5072 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5073 fprintf(stderr, "DB %s is not a persistent database\n",
5078 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5081 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5085 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5087 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5091 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5092 TIMEOUT(), db, false, &h);
5094 fprintf(stderr, "Failed to start transaction on db %s\n",
5099 ret = ctdb_transaction_delete_record(h, key);
5101 fprintf(stderr, "Failed to delete record for key %s\n",
5103 ctdb_transaction_cancel(h);
5107 ret = ctdb_transaction_commit(h);
5109 fprintf(stderr, "Failed to commit transaction on db %s\n",
5111 ctdb_transaction_cancel(h);
5118 static int ptrans_parse_string(TALLOC_CTX *mem_ctx, const char **ptr, TDB_DATA *data)
5126 /* Skip whitespace */
5127 n = strspn(*ptr, " \t");
5131 /* Quoted ASCII string - no wide characters! */
5133 n = strcspn(t, "\"");
5136 ret = str_to_data(t, n, mem_ctx, data);
5143 fprintf(stderr, "Unmatched \" in input %s\n", *ptr);
5147 fprintf(stderr, "Unsupported input format in %s\n", *ptr);
5154 #define MAX_LINE_SIZE 1024
5156 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
5157 TDB_DATA *key, TDB_DATA *value)
5159 char line [MAX_LINE_SIZE]; /* FIXME: make this more flexible? */
5163 ptr = fgets(line, MAX_LINE_SIZE, file);
5169 ret = ptrans_parse_string(mem_ctx, &ptr, key);
5170 if (ret != 0 || ptr == NULL || key->dptr == NULL) {
5171 /* Line Ignored but not EOF */
5177 ret = ptrans_parse_string(mem_ctx, &ptr, value);
5179 /* Line Ignored but not EOF */
5180 talloc_free(key->dptr);
5188 static int control_ptrans(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5189 int argc, const char **argv)
5191 const char *db_name;
5192 struct ctdb_db_context *db;
5193 struct ctdb_transaction_handle *h;
5196 TDB_DATA key, value;
5199 if (argc < 1 || argc > 2) {
5203 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5207 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5208 fprintf(stderr, "DB %s is not a persistent database\n",
5214 file = fopen(argv[1], "r");
5216 fprintf(stderr, "Failed to open file %s\n", argv[1]);
5223 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5226 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5230 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5231 TIMEOUT(), db, false, &h);
5233 fprintf(stderr, "Failed to start transaction on db %s\n",
5238 while (ptrans_get_key_value(mem_ctx, file, &key, &value)) {
5239 if (key.dsize != 0) {
5240 ret = ctdb_transaction_store_record(h, key, value);
5242 fprintf(stderr, "Failed to store record\n");
5243 ctdb_transaction_cancel(h);
5246 talloc_free(key.dptr);
5247 talloc_free(value.dptr);
5251 ret = ctdb_transaction_commit(h);
5253 fprintf(stderr, "Failed to commit transaction on db %s\n",
5255 ctdb_transaction_cancel(h);
5259 if (file != stdin) {
5265 static int control_tfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5266 int argc, const char **argv)
5268 struct tdb_context *tdb;
5270 struct ctdb_ltdb_header header;
5273 if (argc < 2 || argc > 3) {
5277 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5279 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5283 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5285 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5290 data = tdb_fetch(tdb, key);
5291 if (data.dptr == NULL) {
5292 fprintf(stderr, "No record for key %s\n", argv[1]);
5297 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
5298 fprintf(stderr, "Invalid record for key %s\n", argv[1]);
5309 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
5311 fprintf(stderr, "Failed to open output file %s\n",
5316 nwritten = sys_write(fd, data.dptr, data.dsize);
5317 if (nwritten != data.dsize) {
5318 fprintf(stderr, "Failed to write record to file\n");
5327 ret = ctdb_ltdb_header_extract(&data, &header);
5329 fprintf(stderr, "Failed to parse header from data\n");
5333 dump_ltdb_header(&header);
5334 dump_tdb_data("data", data);
5339 static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5340 int argc, const char **argv)
5342 struct tdb_context *tdb;
5343 TDB_DATA key, data[2], value;
5344 struct ctdb_ltdb_header header;
5345 uint8_t header_buf[sizeof(struct ctdb_ltdb_header)];
5348 if (argc < 3 || argc > 5) {
5352 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5354 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5358 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5360 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5365 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &value);
5367 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5372 ZERO_STRUCT(header);
5375 header.rsn = (uint64_t)strtoull(argv[3], NULL, 0);
5378 header.dmaster = (uint32_t)atol(argv[4]);
5381 header.flags = (uint32_t)atol(argv[5]);
5384 ctdb_ltdb_header_push(&header, header_buf);
5386 data[0].dsize = ctdb_ltdb_header_len(&header);
5387 data[0].dptr = header_buf;
5389 data[1].dsize = value.dsize;
5390 data[1].dptr = value.dptr;
5392 ret = tdb_storev(tdb, key, data, 2, TDB_REPLACE);
5394 fprintf(stderr, "Failed to write record %s to file %s\n",
5403 static int control_readkey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5404 int argc, const char **argv)
5406 const char *db_name;
5407 struct ctdb_db_context *db;
5408 struct ctdb_record_handle *h;
5411 bool readonly = false;
5414 if (argc < 2 || argc > 3) {
5419 if (strcmp(argv[2], "readonly") == 0) {
5426 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5430 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5431 fprintf(stderr, "DB %s is not a volatile database\n",
5436 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5439 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5443 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5445 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5449 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5450 db, key, readonly, &h, NULL, &data);
5452 fprintf(stderr, "Failed to read record for key %s\n",
5455 printf("Data: size:%zu ptr:[%.*s]\n", data.dsize,
5456 (int)data.dsize, data.dptr);
5463 static int control_writekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5464 int argc, const char **argv)
5466 const char *db_name;
5467 struct ctdb_db_context *db;
5468 struct ctdb_record_handle *h;
5477 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5481 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5482 fprintf(stderr, "DB %s is not a volatile database\n",
5487 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5490 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5494 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5496 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5500 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5502 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5506 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5507 db, key, false, &h, NULL, NULL);
5509 fprintf(stderr, "Failed to lock record for key %s\n", argv[0]);
5513 ret = ctdb_store_record(h, data);
5515 fprintf(stderr, "Failed to store record for key %s\n",
5523 static int control_deletekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5524 int argc, const char **argv)
5526 const char *db_name;
5527 struct ctdb_db_context *db;
5528 struct ctdb_record_handle *h;
5537 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5541 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5542 fprintf(stderr, "DB %s is not a volatile database\n",
5547 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5550 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5554 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5556 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5560 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5561 db, key, false, &h, NULL, &data);
5563 fprintf(stderr, "Failed to fetch record for key %s\n",
5568 ret = ctdb_delete_record(h);
5570 fprintf(stderr, "Failed to delete record for key %s\n",
5578 static int control_checktcpport(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5579 int argc, const char **argv)
5581 struct sockaddr_in sin;
5587 usage("chktcpport");
5590 port = atoi(argv[0]);
5592 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
5594 fprintf(stderr, "Failed to open local socket\n");
5598 v = fcntl(s, F_GETFL, 0);
5599 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK)) {
5600 fprintf(stderr, "Unable to set socket non-blocking\n");
5605 bzero(&sin, sizeof(sin));
5606 sin.sin_family = AF_INET;
5607 sin.sin_port = htons(port);
5608 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
5611 fprintf(stderr, "Failed to bind to TCP port %u\n", port);
5618 static int control_getdbseqnum(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5619 int argc, const char **argv)
5622 const char *db_name;
5627 usage("getdbseqnum");
5630 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5634 ret = ctdb_ctrl_get_db_seqnum(mem_ctx, ctdb->ev, ctdb->client,
5635 ctdb->cmd_pnn, TIMEOUT(), db_id,
5638 fprintf(stderr, "Failed to get sequence number for DB %s\n",
5643 printf("0x%"PRIx64"\n", seqnum);
5647 static int control_nodestatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5648 int argc, const char **argv)
5650 const char *nodestring = NULL;
5651 struct ctdb_node_map *nodemap;
5655 usage("nodestatus");
5659 nodestring = argv[0];
5662 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap)) {
5666 nodemap = get_nodemap(ctdb, false);
5667 if (nodemap == NULL) {
5671 if (options.machinereadable) {
5672 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5674 print_nodemap(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5678 for (i=0; i<nodemap->num; i++) {
5679 ret |= nodemap->node[i].flags;
5688 } db_stats_fields[] = {
5689 #define DBSTATISTICS_FIELD(n) { #n, offsetof(struct ctdb_db_statistics, n) }
5690 DBSTATISTICS_FIELD(db_ro_delegations),
5691 DBSTATISTICS_FIELD(db_ro_revokes),
5692 DBSTATISTICS_FIELD(locks.num_calls),
5693 DBSTATISTICS_FIELD(locks.num_current),
5694 DBSTATISTICS_FIELD(locks.num_pending),
5695 DBSTATISTICS_FIELD(locks.num_failed),
5696 DBSTATISTICS_FIELD(db_ro_delegations),
5699 static void print_dbstatistics(const char *db_name,
5700 struct ctdb_db_statistics *s)
5703 const char *prefix = NULL;
5706 printf("DB Statistics %s\n", db_name);
5708 for (i=0; i<ARRAY_SIZE(db_stats_fields); i++) {
5709 if (strchr(db_stats_fields[i].name, '.') != NULL) {
5710 preflen = strcspn(db_stats_fields[i].name, ".") + 1;
5712 strncmp(prefix, db_stats_fields[i].name, preflen) != 0) {
5713 prefix = db_stats_fields[i].name;
5714 printf(" %*.*s\n", preflen-1, preflen-1,
5715 db_stats_fields[i].name);
5720 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
5721 db_stats_fields[i].name+preflen, preflen ? 0 : 4, "",
5722 *(uint32_t *)(db_stats_fields[i].offset+(uint8_t *)s));
5725 printf(" hop_count_buckets:");
5726 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5727 printf(" %d", s->hop_count_bucket[i]);
5731 printf(" lock_buckets:");
5732 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5733 printf(" %d", s->locks.buckets[i]);
5737 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5738 "locks_latency MIN/AVG/MAX",
5739 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
5740 s->locks.latency.max, s->locks.latency.num);
5742 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5743 "vacuum_latency MIN/AVG/MAX",
5744 s->vacuum.latency.min, LATENCY_AVG(s->vacuum.latency),
5745 s->vacuum.latency.max, s->vacuum.latency.num);
5747 printf(" Num Hot Keys: %d\n", s->num_hot_keys);
5748 for (i=0; i<s->num_hot_keys; i++) {
5750 printf(" Count:%d Key:", s->hot_keys[i].count);
5751 for (j=0; j<s->hot_keys[i].key.dsize; j++) {
5752 printf("%02x", s->hot_keys[i].key.dptr[j] & 0xff);
5758 static int control_dbstatistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5759 int argc, const char **argv)
5762 const char *db_name;
5763 struct ctdb_db_statistics *dbstats;
5767 usage("dbstatistics");
5770 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5774 ret = ctdb_ctrl_get_db_statistics(mem_ctx, ctdb->ev, ctdb->client,
5775 ctdb->cmd_pnn, TIMEOUT(), db_id,
5778 fprintf(stderr, "Failed to get statistics for DB %s\n",
5783 print_dbstatistics(db_name, dbstats);
5787 struct disable_takeover_runs_state {
5795 static void disable_takeover_run_handler(uint64_t srvid, TDB_DATA data,
5798 struct disable_takeover_runs_state *state =
5799 (struct disable_takeover_runs_state *)private_data;
5802 if (data.dsize != sizeof(int)) {
5807 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
5808 ret = *(int *)data.dptr;
5810 state->status = ret;
5814 for (i=0; i<state->node_count; i++) {
5815 if (state->pnn_list[i] == ret) {
5816 state->reply[i] = true;
5822 for (i=0; i<state->node_count; i++) {
5823 if (! state->reply[i]) {
5824 state->done = false;
5830 static int disable_takeover_runs(TALLOC_CTX *mem_ctx,
5831 struct ctdb_context *ctdb, uint32_t timeout,
5832 uint32_t *pnn_list, int count)
5834 struct ctdb_disable_message disable = { 0 };
5835 struct disable_takeover_runs_state state;
5838 disable.pnn = ctdb->pnn;
5839 disable.srvid = next_srvid(ctdb);
5840 disable.timeout = timeout;
5842 state.pnn_list = pnn_list;
5843 state.node_count = count;
5846 state.reply = talloc_zero_array(mem_ctx, bool, count);
5847 if (state.reply == NULL) {
5851 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
5853 disable_takeover_run_handler,
5859 for (i=0; i<count; i++) {
5860 ret = ctdb_message_disable_takeover_runs(mem_ctx, ctdb->ev,
5869 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
5871 fprintf(stderr, "Timed out waiting to disable takeover runs\n");
5873 ret = (state.status >= 0 ? 0 : 1);
5877 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
5878 disable.srvid, &state);
5882 static int control_reloadips(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5883 int argc, const char **argv)
5885 const char *nodestring = NULL;
5886 struct ctdb_node_map *nodemap, *nodemap2;
5887 struct ctdb_req_control request;
5888 uint32_t *pnn_list, *pnn_list2;
5889 int ret, count, count2;
5896 nodestring = argv[0];
5899 nodemap = get_nodemap(ctdb, false);
5900 if (nodemap == NULL) {
5904 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap2)) {
5908 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
5909 mem_ctx, &pnn_list);
5911 fprintf(stderr, "Memory allocation error\n");
5915 count2 = list_of_active_nodes(nodemap2, CTDB_UNKNOWN_PNN,
5916 mem_ctx, &pnn_list2);
5918 fprintf(stderr, "Memory allocation error\n");
5922 /* Disable takeover runs on all connected nodes. A reply
5923 * indicating success is needed from each node so all nodes
5924 * will need to be active.
5926 * A check could be added to not allow reloading of IPs when
5927 * there are disconnected nodes. However, this should
5928 * probably be left up to the administrator.
5930 ret = disable_takeover_runs(mem_ctx, ctdb, 2*options.timelimit,
5933 fprintf(stderr, "Failed to disable takeover runs\n");
5937 /* Now tell all the desired nodes to reload their public IPs.
5938 * Keep trying this until it succeeds. This assumes all
5939 * failures are transient, which might not be true...
5941 ctdb_req_control_reload_public_ips(&request);
5942 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
5943 pnn_list2, count2, TIMEOUT(),
5944 &request, NULL, NULL);
5946 fprintf(stderr, "Failed to reload IPs on some nodes.\n");
5949 /* It isn't strictly necessary to wait until takeover runs are
5950 * re-enabled but doing so can't hurt.
5952 ret = disable_takeover_runs(mem_ctx, ctdb, 0, pnn_list, count);
5954 fprintf(stderr, "Failed to enable takeover runs\n");
5958 return ipreallocate(mem_ctx, ctdb);
5961 static int control_ipiface(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5962 int argc, const char **argv)
5964 ctdb_sock_addr addr;
5971 if (! parse_ip(argv[0], NULL, 0, &addr)) {
5972 fprintf(stderr, "Failed to Parse IP %s\n", argv[0]);
5976 iface = ctdb_sys_find_ifname(&addr);
5977 if (iface == NULL) {
5978 fprintf(stderr, "Failed to find interface for IP %s\n",
5988 static const struct ctdb_cmd {
5990 int (*fn)(TALLOC_CTX *, struct ctdb_context *, int, const char **);
5991 bool without_daemon; /* can be run without daemon running ? */
5992 bool remote; /* can be run on remote nodes */
5995 } ctdb_commands[] = {
5996 { "version", control_version, true, false,
5997 "show version of ctdb", NULL },
5998 { "status", control_status, false, true,
5999 "show node status", NULL },
6000 { "uptime", control_uptime, false, true,
6001 "show node uptime", NULL },
6002 { "ping", control_ping, false, true,
6003 "ping all nodes", NULL },
6004 { "runstate", control_runstate, false, true,
6005 "get/check runstate of a node",
6006 "[setup|first_recovery|startup|running]" },
6007 { "getvar", control_getvar, false, true,
6008 "get a tunable variable", "<name>" },
6009 { "setvar", control_setvar, false, true,
6010 "set a tunable variable", "<name> <value>" },
6011 { "listvars", control_listvars, false, true,
6012 "list tunable variables", NULL },
6013 { "statistics", control_statistics, false, true,
6014 "show ctdb statistics", NULL },
6015 { "statisticsreset", control_statistics_reset, false, true,
6016 "reset ctdb statistics", NULL },
6017 { "stats", control_stats, false, true,
6018 "show rolling statistics", "[count]" },
6019 { "ip", control_ip, false, true,
6020 "show public ips", "[all]" },
6021 { "ipinfo", control_ipinfo, false, true,
6022 "show public ip details", "<ip>" },
6023 { "ifaces", control_ifaces, false, true,
6024 "show interfaces", NULL },
6025 { "setifacelink", control_setifacelink, false, true,
6026 "set interface link status", "<iface> up|down" },
6027 { "process-exists", control_process_exists, false, true,
6028 "check if a process exists on a node", "<pid>" },
6029 { "getdbmap", control_getdbmap, false, true,
6030 "show attached databases", NULL },
6031 { "getdbstatus", control_getdbstatus, false, true,
6032 "show database status", "<dbname|dbid>" },
6033 { "catdb", control_catdb, false, false,
6034 "dump cluster-wide ctdb database", "<dbname|dbid>" },
6035 { "cattdb", control_cattdb, false, false,
6036 "dump local ctdb database", "<dbname|dbid>" },
6037 { "getmonmode", control_getmonmode, false, true,
6038 "show monitoring mode", NULL },
6039 { "getcapabilities", control_getcapabilities, false, true,
6040 "show node capabilities", NULL },
6041 { "pnn", control_pnn, false, false,
6042 "show the pnn of the currnet node", NULL },
6043 { "lvs", control_lvs, false, false,
6044 "show lvs configuration", "master|list|status" },
6045 { "disablemonitor", control_disable_monitor, false, true,
6046 "disable monitoring", NULL },
6047 { "enablemonitor", control_enable_monitor, false, true,
6048 "enable monitoring", NULL },
6049 { "setdebug", control_setdebug, false, true,
6050 "set debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
6051 { "getdebug", control_getdebug, false, true,
6052 "get debug level", NULL },
6053 { "attach", control_attach, false, false,
6054 "attach a database", "<dbname> [persistent]" },
6055 { "detach", control_detach, false, false,
6056 "detach database(s)", "<dbname|dbid> ..." },
6057 { "dumpmemory", control_dumpmemory, false, true,
6058 "dump ctdbd memory map", NULL },
6059 { "rddumpmemory", control_rddumpmemory, false, true,
6060 "dump recoverd memory map", NULL },
6061 { "getpid", control_getpid, false, true,
6062 "get ctdbd process ID", NULL },
6063 { "disable", control_disable, false, true,
6064 "disable a node", NULL },
6065 { "enable", control_enable, false, true,
6066 "enable a node", NULL },
6067 { "stop", control_stop, false, true,
6068 "stop a node", NULL },
6069 { "continue", control_continue, false, true,
6070 "continue a stopped node", NULL },
6071 { "ban", control_ban, false, true,
6072 "ban a node", "<bantime>"},
6073 { "unban", control_unban, false, true,
6074 "unban a node", NULL },
6075 { "shutdown", control_shutdown, false, true,
6076 "shutdown ctdb daemon", NULL },
6077 { "recover", control_recover, false, true,
6078 "force recovery", NULL },
6079 { "sync", control_ipreallocate, false, true,
6080 "run ip reallocation (deprecated)", NULL },
6081 { "ipreallocate", control_ipreallocate, false, true,
6082 "run ip reallocation", NULL },
6083 { "isnotrecmaster", control_isnotrecmaster, false, false,
6084 "check if local node is the recmaster", NULL },
6085 { "gratarp", control_gratarp, false, true,
6086 "send a gratuitous arp", "<ip> <interface>" },
6087 { "tickle", control_tickle, true, false,
6088 "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6089 { "gettickles", control_gettickles, false, true,
6090 "get the list of tickles", "<ip> [<port>]" },
6091 { "addtickle", control_addtickle, false, true,
6092 "add a tickle", "<ip>:<port> <ip>:<port>" },
6093 { "deltickle", control_deltickle, false, true,
6094 "delete a tickle", "<ip>:<port> <ip>:<port>" },
6095 { "check_srvids", control_check_srvids, false, true,
6096 "check if srvid is registered", "<id> [<id> ...]" },
6097 { "listnodes", control_listnodes, true, true,
6098 "list nodes in the cluster", NULL },
6099 { "reloadnodes", control_reloadnodes, false, false,
6100 "reload the nodes file all nodes", NULL },
6101 { "moveip", control_moveip, false, false,
6102 "move an ip address to another node", "<ip> <node>" },
6103 { "addip", control_addip, false, true,
6104 "add an ip address to a node", "<ip/mask> <iface>" },
6105 { "delip", control_delip, false, true,
6106 "delete an ip address from a node", "<ip>" },
6107 { "backupdb", control_backupdb, false, false,
6108 "backup a database into a file", "<dbname|dbid> <file>" },
6109 { "restoredb", control_restoredb, false, false,
6110 "restore a database from a file", "<file> [dbname]" },
6111 { "dumpdbbackup", control_dumpdbbackup, true, false,
6112 "dump database from a backup file", "<file>" },
6113 { "wipedb", control_wipedb, false, false,
6114 "wipe the contents of a database.", "<dbname|dbid>"},
6115 { "recmaster", control_recmaster, false, true,
6116 "show the pnn for the recovery master", NULL },
6117 { "event", control_event, true, false,
6118 "event and event script commands", NULL },
6119 { "scriptstatus", control_scriptstatus, true, false,
6120 "show event script status",
6121 "[init|setup|startup|monitor|takeip|releaseip|ipreallocated]" },
6122 { "natgw", control_natgw, false, false,
6123 "show natgw configuration", "master|list|status" },
6124 { "natgwlist", control_natgwlist, false, false,
6125 "show the nodes belonging to this natgw configuration", NULL },
6126 { "getreclock", control_getreclock, false, true,
6127 "get recovery lock file", NULL },
6128 { "setlmasterrole", control_setlmasterrole, false, true,
6129 "set LMASTER role", "on|off" },
6130 { "setrecmasterrole", control_setrecmasterrole, false, true,
6131 "set RECMASTER role", "on|off"},
6132 { "setdbreadonly", control_setdbreadonly, false, true,
6133 "enable readonly records", "<dbname|dbid>" },
6134 { "setdbsticky", control_setdbsticky, false, true,
6135 "enable sticky records", "<dbname|dbid>"},
6136 { "pfetch", control_pfetch, false, false,
6137 "fetch record from persistent database", "<dbname|dbid> <key> [<file>]" },
6138 { "pstore", control_pstore, false, false,
6139 "write record to persistent database", "<dbname|dbid> <key> <value>" },
6140 { "pdelete", control_pdelete, false, false,
6141 "delete record from persistent database", "<dbname|dbid> <key>" },
6142 { "ptrans", control_ptrans, false, false,
6143 "update a persistent database (from file or stdin)", "<dbname|dbid> [<file>]" },
6144 { "tfetch", control_tfetch, false, true,
6145 "fetch a record", "<tdb-file> <key> [<file>]" },
6146 { "tstore", control_tstore, false, true,
6147 "store a record", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6148 { "readkey", control_readkey, false, false,
6149 "read value of a database key", "<dbname|dbid> <key> [readonly]" },
6150 { "writekey", control_writekey, false, false,
6151 "write value for a database key", "<dbname|dbid> <key> <value>" },
6152 { "deletekey", control_deletekey, false, false,
6153 "delete a database key", "<dbname|dbid> <key>" },
6154 { "checktcpport", control_checktcpport, true, false,
6155 "check if a service is bound to a specific tcp port or not", "<port>" },
6156 { "getdbseqnum", control_getdbseqnum, false, false,
6157 "get database sequence number", "<dbname|dbid>" },
6158 { "nodestatus", control_nodestatus, false, true,
6159 "show and return node status", "[all|<pnn-list>]" },
6160 { "dbstatistics", control_dbstatistics, false, true,
6161 "show database statistics", "<dbname|dbid>" },
6162 { "reloadips", control_reloadips, false, false,
6163 "reload the public addresses file", "[all|<pnn-list>]" },
6164 { "ipiface", control_ipiface, true, false,
6165 "Find the interface an ip address is hosted on", "<ip>" },
6168 static const struct ctdb_cmd *match_command(const char *command)
6170 const struct ctdb_cmd *cmd;
6173 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6174 cmd = &ctdb_commands[i];
6175 if (strlen(command) == strlen(cmd->name) &&
6176 strncmp(command, cmd->name, strlen(command)) == 0) {
6186 * Show usage message
6188 static void usage_full(void)
6192 poptPrintHelp(pc, stdout, 0);
6193 printf("\nCommands:\n");
6194 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6195 printf(" %-15s %-27s %s\n",
6196 ctdb_commands[i].name,
6197 ctdb_commands[i].args ? ctdb_commands[i].args : "",
6198 ctdb_commands[i].msg);
6202 static void usage(const char *command)
6204 const struct ctdb_cmd *cmd;
6206 if (command == NULL) {
6211 cmd = match_command(command);
6215 poptPrintUsage(pc, stdout, 0);
6216 printf("\nCommands:\n");
6217 printf(" %-15s %-27s %s\n",
6218 cmd->name, cmd->args ? cmd->args : "", cmd->msg);
6224 struct poptOption cmdline_options[] = {
6226 { "socket", 's', POPT_ARG_STRING, &options.socket, 0,
6227 "CTDB socket path", "filename" },
6228 { "debug", 'd', POPT_ARG_STRING, &options.debuglevelstr, 0,
6230 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0,
6231 "timelimit (in seconds)" },
6232 { "node", 'n', POPT_ARG_INT, &options.pnn, 0,
6233 "node specification - integer" },
6234 { NULL, 'Y', POPT_ARG_NONE, &options.machinereadable, 0,
6235 "enable machine readable output", NULL },
6236 { "separator", 'x', POPT_ARG_STRING, &options.sep, 0,
6237 "specify separator for machine readable output", "CHAR" },
6238 { NULL, 'X', POPT_ARG_NONE, &options.machineparsable, 0,
6239 "enable machine parsable output with separator |", NULL },
6240 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0,
6241 "enable verbose output", NULL },
6242 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0,
6243 "die if runtime exceeds this limit (in seconds)" },
6247 static int process_command(const struct ctdb_cmd *cmd, int argc,
6250 TALLOC_CTX *tmp_ctx;
6251 struct ctdb_context *ctdb;
6254 uint64_t srvid_offset;
6256 tmp_ctx = talloc_new(NULL);
6257 if (tmp_ctx == NULL) {
6258 fprintf(stderr, "Memory allocation error\n");
6262 if (cmd->without_daemon) {
6263 if (options.pnn != -1) {
6265 "Cannot specify node for command %s\n",
6270 ret = cmd->fn(tmp_ctx, NULL, argc-1, argv+1);
6271 talloc_free(tmp_ctx);
6275 ctdb = talloc_zero(tmp_ctx, struct ctdb_context);
6277 fprintf(stderr, "Memory allocation error\n");
6281 ctdb->ev = tevent_context_init(ctdb);
6282 if (ctdb->ev == NULL) {
6283 fprintf(stderr, "Failed to initialize tevent\n");
6287 ret = ctdb_client_init(ctdb, ctdb->ev, options.socket, &ctdb->client);
6289 fprintf(stderr, "Failed to connect to CTDB daemon (%s)\n",
6292 if (!find_node_xpnn(ctdb, NULL)) {
6293 fprintf(stderr, "Is this node part of CTDB cluster?\n");
6298 ctdb->pnn = ctdb_client_pnn(ctdb->client);
6299 srvid_offset = getpid() & 0xFFFF;
6300 ctdb->srvid = SRVID_CTDB_TOOL | (srvid_offset << 16);
6302 if (options.pnn != -1) {
6303 status = verify_pnn(ctdb, options.pnn);
6308 ctdb->cmd_pnn = options.pnn;
6310 ctdb->cmd_pnn = ctdb->pnn;
6313 if (! cmd->remote && ctdb->pnn != ctdb->cmd_pnn) {
6314 fprintf(stderr, "Node cannot be specified for command %s\n",
6319 ret = cmd->fn(tmp_ctx, ctdb, argc-1, argv+1);
6320 talloc_free(tmp_ctx);
6324 talloc_free(tmp_ctx);
6328 static void signal_handler(int sig)
6330 fprintf(stderr, "Maximum runtime exceeded - exiting\n");
6333 static void alarm_handler(int sig)
6335 /* Kill any child processes */
6336 signal(SIGTERM, signal_handler);
6342 int main(int argc, const char *argv[])
6345 const char **extra_argv;
6347 const struct ctdb_cmd *cmd;
6348 const char *ctdb_socket;
6354 /* Set default options */
6355 options.socket = CTDB_SOCKET;
6356 options.debuglevelstr = NULL;
6357 options.timelimit = 10;
6359 options.maxruntime = 0;
6362 ctdb_socket = getenv("CTDB_SOCKET");
6363 if (ctdb_socket != NULL) {
6364 options.socket = ctdb_socket;
6367 pc = poptGetContext(argv[0], argc, argv, cmdline_options,
6368 POPT_CONTEXT_KEEP_FIRST);
6369 while ((opt = poptGetNextOpt(pc)) != -1) {
6370 fprintf(stderr, "Invalid option %s: %s\n",
6371 poptBadOption(pc, 0), poptStrerror(opt));
6375 if (options.maxruntime == 0) {
6376 const char *ctdb_timeout;
6378 ctdb_timeout = getenv("CTDB_TIMEOUT");
6379 if (ctdb_timeout != NULL) {
6380 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6382 options.maxruntime = 120;
6385 if (options.maxruntime <= 120) {
6386 /* default timeout is 120 seconds */
6387 options.maxruntime = 120;
6390 if (options.machineparsable) {
6391 options.machinereadable = 1;
6394 /* setup the remaining options for the commands */
6396 extra_argv = poptGetArgs(pc);
6399 while (extra_argv[extra_argc]) extra_argc++;
6402 if (extra_argc < 1) {
6406 cmd = match_command(extra_argv[0]);
6408 fprintf(stderr, "Unknown command '%s'\n", extra_argv[0]);
6412 /* Enable logging */
6413 setup_logging("ctdb", DEBUG_STDERR);
6414 if (debug_level_parse(options.debuglevelstr, &loglevel)) {
6415 DEBUGLEVEL = loglevel;
6417 DEBUGLEVEL = DEBUG_ERR;
6420 signal(SIGALRM, alarm_handler);
6421 alarm(options.maxruntime);
6423 ret = process_command(cmd, extra_argc, extra_argv);
6428 (void)poptFreeContext(pc);