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(), &ips);
1560 for (j=0; j<ips->num; j++) {
1561 struct ctdb_public_ip ip;
1563 ip.pnn = ips->ip[j].pnn;
1564 ip.addr = ips->ip[j].addr;
1566 if (pnn_list[i] == ip.pnn) {
1567 /* Node claims IP is hosted on it, so
1568 * save that information
1570 ret = db_hash_add(ipdb, (uint8_t *)&ip.addr,
1572 (uint8_t *)&ip, sizeof(ip));
1577 /* Node thinks IP is hosted elsewhere,
1578 * so overwrite with CTDB_UNKNOWN_PNN
1579 * if there's no existing entry
1581 ret = db_hash_exists(ipdb, (uint8_t *)&ip.addr,
1583 if (ret == ENOENT) {
1584 ip.pnn = CTDB_UNKNOWN_PNN;
1585 ret = db_hash_add(ipdb,
1586 (uint8_t *)&ip.addr,
1600 talloc_free(pnn_list);
1602 ret = db_hash_traverse(ipdb, NULL, NULL, &count);
1607 ips = talloc_zero(mem_ctx, struct ctdb_public_ip_list);
1612 ips->ip = talloc_array(ips, struct ctdb_public_ip, count);
1613 if (ips->ip == NULL) {
1617 ret = db_hash_traverse(ipdb, collect_ips, ips, &count);
1622 if (count != ips->num) {
1636 static int control_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1637 int argc, const char **argv)
1639 struct ctdb_public_ip_list *ips;
1640 struct ctdb_public_ip_info **ipinfo;
1642 bool do_all = false;
1649 if (strcmp(argv[0], "all") == 0) {
1657 ret = get_all_public_ips(ctdb, mem_ctx, &ips);
1659 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1660 ctdb->cmd_pnn, TIMEOUT(), &ips);
1666 qsort(ips->ip, ips->num, sizeof(struct ctdb_public_ip),
1667 ctdb_public_ip_cmp);
1669 ipinfo = talloc_array(mem_ctx, struct ctdb_public_ip_info *, ips->num);
1670 if (ipinfo == NULL) {
1674 for (i=0; i<ips->num; i++) {
1677 pnn = ips->ip[i].pnn;
1679 pnn = ctdb->cmd_pnn;
1681 if (pnn == CTDB_UNKNOWN_PNN) {
1685 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev,
1687 TIMEOUT(), &ips->ip[i].addr,
1694 print_ip(mem_ctx, ctdb, ips, ipinfo, do_all);
1698 static int control_ipinfo(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1699 int argc, const char **argv)
1701 struct ctdb_public_ip_info *ipinfo;
1702 ctdb_sock_addr addr;
1709 if (! parse_ip(argv[0], NULL, 0, &addr)) {
1710 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
1714 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev, ctdb->client,
1715 ctdb->cmd_pnn, TIMEOUT(), &addr,
1719 printf("Node %u does not know about IP %s\n",
1720 ctdb->cmd_pnn, argv[0]);
1725 printf("Public IP[%s] info on node %u\n",
1726 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1729 printf("IP:%s\nCurrentNode:%u\nNumInterfaces:%u\n",
1730 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1731 ipinfo->ip.pnn, ipinfo->ifaces->num);
1733 for (i=0; i<ipinfo->ifaces->num; i++) {
1734 struct ctdb_iface *iface;
1736 iface = &ipinfo->ifaces->iface[i];
1737 iface->name[CTDB_IFACE_SIZE] = '\0';
1738 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
1740 iface->link_state == 0 ? "down" : "up",
1742 (i == ipinfo->active_idx) ? " (active)" : "");
1748 static int control_ifaces(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1749 int argc, const char **argv)
1751 struct ctdb_iface_list *ifaces;
1758 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1759 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1764 if (ifaces->num == 0) {
1765 printf("No interfaces configured on node %u\n",
1770 if (options.machinereadable) {
1771 printf("%s%s%s%s%s%s%s\n", options.sep,
1772 "Name", options.sep,
1773 "LinkStatus", options.sep,
1774 "References", options.sep);
1776 printf("Interfaces on node %u\n", ctdb->cmd_pnn);
1779 for (i=0; i<ifaces->num; i++) {
1780 if (options.machinereadable) {
1781 printf("%s%s%s%u%s%u%s\n", options.sep,
1782 ifaces->iface[i].name, options.sep,
1783 ifaces->iface[i].link_state, options.sep,
1784 ifaces->iface[i].references, options.sep);
1786 printf("name:%s link:%s references:%u\n",
1787 ifaces->iface[i].name,
1788 ifaces->iface[i].link_state ? "up" : "down",
1789 ifaces->iface[i].references);
1796 static int control_setifacelink(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1797 int argc, const char **argv)
1799 struct ctdb_iface_list *ifaces;
1800 struct ctdb_iface *iface;
1804 usage("setifacelink");
1807 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
1808 fprintf(stderr, "Interface name '%s' too long\n", argv[0]);
1812 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1813 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1816 "Failed to get interface information from node %u\n",
1822 for (i=0; i<ifaces->num; i++) {
1823 if (strcmp(ifaces->iface[i].name, argv[0]) == 0) {
1824 iface = &ifaces->iface[i];
1829 if (iface == NULL) {
1830 printf("Interface %s not configured on node %u\n",
1831 argv[0], ctdb->cmd_pnn);
1835 if (strcmp(argv[1], "up") == 0) {
1836 iface->link_state = 1;
1837 } else if (strcmp(argv[1], "down") == 0) {
1838 iface->link_state = 0;
1840 usage("setifacelink");
1844 iface->references = 0;
1846 ret = ctdb_ctrl_set_iface_link_state(mem_ctx, ctdb->ev, ctdb->client,
1847 ctdb->cmd_pnn, TIMEOUT(), iface);
1855 static int control_process_exists(TALLOC_CTX *mem_ctx,
1856 struct ctdb_context *ctdb,
1857 int argc, const char **argv)
1863 usage("process-exists");
1866 pid = atoi(argv[0]);
1867 ret = ctdb_ctrl_process_exists(mem_ctx, ctdb->ev, ctdb->client,
1868 ctdb->cmd_pnn, TIMEOUT(), pid, &status);
1874 printf("PID %u exists\n", pid);
1876 printf("PID %u does not exist\n", pid);
1881 static int control_getdbmap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1882 int argc, const char **argv)
1884 struct ctdb_dbid_map *dbmap;
1891 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
1892 ctdb->cmd_pnn, TIMEOUT(), &dbmap);
1897 if (options.machinereadable == 1) {
1898 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1901 "Name", options.sep,
1902 "Path", options.sep,
1903 "Persistent", options.sep,
1904 "Sticky", options.sep,
1905 "Unhealthy", options.sep,
1906 "Readonly", options.sep);
1908 printf("Number of databases:%d\n", dbmap->num);
1911 for (i=0; i<dbmap->num; i++) {
1920 db_id = dbmap->dbs[i].db_id;
1922 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
1923 ctdb->cmd_pnn, TIMEOUT(), db_id,
1929 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1930 ctdb->cmd_pnn, TIMEOUT(), db_id,
1936 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1937 ctdb->cmd_pnn, TIMEOUT(), db_id,
1943 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
1944 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
1945 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
1947 if (options.machinereadable == 1) {
1948 printf("%s0x%08X%s%s%s%s%s%d%s%d%s%d%s%d%s\n",
1953 !! (persistent), options.sep,
1954 !! (sticky), options.sep,
1955 !! (health), options.sep,
1956 !! (readonly), options.sep);
1958 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
1960 persistent ? " PERSISTENT" : "",
1961 sticky ? " STICKY" : "",
1962 readonly ? " READONLY" : "",
1963 health ? " UNHEALTHY" : "");
1966 talloc_free(discard_const(name));
1967 talloc_free(discard_const(path));
1968 talloc_free(discard_const(health));
1974 static int control_getdbstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1975 int argc, const char **argv)
1978 const char *db_name, *db_path, *db_health;
1983 usage("getdbstatus");
1986 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
1990 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1991 ctdb->cmd_pnn, TIMEOUT(), db_id,
1997 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1998 ctdb->cmd_pnn, TIMEOUT(), db_id,
2004 printf("dbid: 0x%08x\nname: %s\npath: %s\n", db_id, db_name, db_path);
2005 printf("PERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
2006 (db_flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
2007 (db_flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
2008 (db_flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
2009 (db_health ? db_health : "OK"));
2013 struct dump_record_state {
2017 #define ISASCII(x) (isprint(x) && ! strchr("\"\\", (x)))
2019 static void dump_tdb_data(const char *name, TDB_DATA val)
2023 fprintf(stdout, "%s(%zu) = \"", name, val.dsize);
2024 for (i=0; i<val.dsize; i++) {
2025 if (ISASCII(val.dptr[i])) {
2026 fprintf(stdout, "%c", val.dptr[i]);
2028 fprintf(stdout, "\\%02X", val.dptr[i]);
2031 fprintf(stdout, "\"\n");
2034 static void dump_ltdb_header(struct ctdb_ltdb_header *header)
2036 fprintf(stdout, "dmaster: %u\n", header->dmaster);
2037 fprintf(stdout, "rsn: %" PRIu64 "\n", header->rsn);
2038 fprintf(stdout, "flags: 0x%08x", header->flags);
2039 if (header->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
2040 fprintf(stdout, " MIGRATED_WITH_DATA");
2042 if (header->flags & CTDB_REC_FLAG_VACUUM_MIGRATED) {
2043 fprintf(stdout, " VACUUM_MIGRATED");
2045 if (header->flags & CTDB_REC_FLAG_AUTOMATIC) {
2046 fprintf(stdout, " AUTOMATIC");
2048 if (header->flags & CTDB_REC_RO_HAVE_DELEGATIONS) {
2049 fprintf(stdout, " RO_HAVE_DELEGATIONS");
2051 if (header->flags & CTDB_REC_RO_HAVE_READONLY) {
2052 fprintf(stdout, " RO_HAVE_READONLY");
2054 if (header->flags & CTDB_REC_RO_REVOKING_READONLY) {
2055 fprintf(stdout, " RO_REVOKING_READONLY");
2057 if (header->flags & CTDB_REC_RO_REVOKE_COMPLETE) {
2058 fprintf(stdout, " RO_REVOKE_COMPLETE");
2060 fprintf(stdout, "\n");
2064 static int dump_record(uint32_t reqid, struct ctdb_ltdb_header *header,
2065 TDB_DATA key, TDB_DATA data, void *private_data)
2067 struct dump_record_state *state =
2068 (struct dump_record_state *)private_data;
2072 dump_tdb_data("key", key);
2073 dump_ltdb_header(header);
2074 dump_tdb_data("data", data);
2075 fprintf(stdout, "\n");
2080 struct traverse_state {
2081 TALLOC_CTX *mem_ctx;
2083 ctdb_rec_parser_func_t func;
2084 struct dump_record_state sub_state;
2087 static void traverse_handler(uint64_t srvid, TDB_DATA data, void *private_data)
2089 struct traverse_state *state = (struct traverse_state *)private_data;
2090 struct ctdb_rec_data *rec;
2091 struct ctdb_ltdb_header header;
2094 ret = ctdb_rec_data_pull(data.dptr, data.dsize, state->mem_ctx, &rec);
2099 if (rec->key.dsize == 0 && rec->data.dsize == 0) {
2101 /* end of traverse */
2106 ret = ctdb_ltdb_header_extract(&rec->data, &header);
2112 if (rec->data.dsize == 0) {
2117 ret = state->func(rec->reqid, &header, rec->key, rec->data,
2125 static int control_catdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2126 int argc, const char **argv)
2128 struct ctdb_db_context *db;
2129 const char *db_name;
2132 struct ctdb_traverse_start_ext traverse;
2133 struct traverse_state state;
2140 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2144 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2147 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2152 ZERO_STRUCT(traverse);
2154 traverse.db_id = db_id;
2156 traverse.srvid = next_srvid(ctdb);
2157 traverse.withemptyrecords = false;
2159 state.mem_ctx = mem_ctx;
2161 state.func = dump_record;
2162 state.sub_state.count = 0;
2164 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2166 traverse_handler, &state);
2171 ret = ctdb_ctrl_traverse_start_ext(mem_ctx, ctdb->ev, ctdb->client,
2172 ctdb->cmd_pnn, TIMEOUT(),
2178 ctdb_client_wait(ctdb->ev, &state.done);
2180 printf("Dumped %u records\n", state.sub_state.count);
2182 ret = ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2183 traverse.srvid, &state);
2191 static int control_cattdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2192 int argc, const char **argv)
2194 struct ctdb_db_context *db;
2195 const char *db_name;
2198 struct dump_record_state state;
2205 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2209 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2212 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2217 ret = ctdb_db_traverse(db, true, true, dump_record, &state);
2219 printf("Dumped %u record(s)\n", state.count);
2224 static int control_getmonmode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2225 int argc, const char **argv)
2230 usage("getmonmode");
2233 ret = ctdb_ctrl_get_monmode(mem_ctx, ctdb->ev, ctdb->client,
2234 ctdb->cmd_pnn, TIMEOUT(), &mode);
2240 (mode == CTDB_MONITORING_ENABLED) ? "ENABLED" : "DISABLED");
2244 static int control_getcapabilities(TALLOC_CTX *mem_ctx,
2245 struct ctdb_context *ctdb,
2246 int argc, const char **argv)
2252 usage("getcapabilities");
2255 ret = ctdb_ctrl_get_capabilities(mem_ctx, ctdb->ev, ctdb->client,
2256 ctdb->cmd_pnn, TIMEOUT(), &caps);
2261 if (options.machinereadable == 1) {
2262 printf("%s%s%s%s%s\n",
2264 "RECMASTER", options.sep,
2265 "LMASTER", options.sep);
2266 printf("%s%d%s%d%s\n", options.sep,
2267 !! (caps & CTDB_CAP_RECMASTER), options.sep,
2268 !! (caps & CTDB_CAP_LMASTER), options.sep);
2270 printf("RECMASTER: %s\n",
2271 (caps & CTDB_CAP_RECMASTER) ? "YES" : "NO");
2272 printf("LMASTER: %s\n",
2273 (caps & CTDB_CAP_LMASTER) ? "YES" : "NO");
2279 static int control_pnn(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2280 int argc, const char **argv)
2282 printf("%u\n", ctdb_client_pnn(ctdb->client));
2286 static int control_lvs(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2287 int argc, const char **argv)
2289 char *t, *lvs_helper = NULL;
2295 t = getenv("CTDB_LVS_HELPER");
2297 lvs_helper = talloc_strdup(mem_ctx, t);
2299 lvs_helper = talloc_asprintf(mem_ctx, "%s/ctdb_lvs",
2300 CTDB_HELPER_BINDIR);
2303 if (lvs_helper == NULL) {
2304 fprintf(stderr, "Unable to set LVS helper\n");
2308 return run_helper(mem_ctx, "LVS helper", lvs_helper, argc, argv);
2311 static int control_disable_monitor(TALLOC_CTX *mem_ctx,
2312 struct ctdb_context *ctdb,
2313 int argc, const char **argv)
2318 usage("disablemonitor");
2321 ret = ctdb_ctrl_disable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2322 ctdb->cmd_pnn, TIMEOUT());
2330 static int control_enable_monitor(TALLOC_CTX *mem_ctx,
2331 struct ctdb_context *ctdb,
2332 int argc, const char **argv)
2337 usage("enablemonitor");
2340 ret = ctdb_ctrl_enable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2341 ctdb->cmd_pnn, TIMEOUT());
2349 static int control_setdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2350 int argc, const char **argv)
2360 found = debug_level_parse(argv[0], &log_level);
2363 "Invalid debug level '%s'. Valid levels are:\n",
2365 fprintf(stderr, "\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
2369 ret = ctdb_ctrl_setdebug(mem_ctx, ctdb->ev, ctdb->client,
2370 ctdb->cmd_pnn, TIMEOUT(), log_level);
2378 static int control_getdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2379 int argc, const char **argv)
2382 const char *log_str;
2389 ret = ctdb_ctrl_getdebug(mem_ctx, ctdb->ev, ctdb->client,
2390 ctdb->cmd_pnn, TIMEOUT(), &loglevel);
2395 log_str = debug_level_to_string(loglevel);
2396 printf("%s\n", log_str);
2401 static int control_attach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2402 int argc, const char **argv)
2404 const char *db_name;
2405 uint8_t db_flags = 0;
2408 if (argc < 1 || argc > 2) {
2414 if (strcmp(argv[1], "persistent") == 0) {
2415 db_flags = CTDB_DB_FLAGS_PERSISTENT;
2416 } else if (strcmp(argv[1], "readonly") == 0) {
2417 db_flags = CTDB_DB_FLAGS_READONLY;
2418 } else if (strcmp(argv[1], "sticky") == 0) {
2419 db_flags = CTDB_DB_FLAGS_STICKY;
2425 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2434 static int control_detach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2435 int argc, const char **argv)
2437 const char *db_name;
2440 struct ctdb_node_map *nodemap;
2448 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2449 ctdb->cmd_pnn, TIMEOUT(), &recmode);
2454 if (recmode == CTDB_RECOVERY_ACTIVE) {
2455 fprintf(stderr, "Database cannot be detached"
2456 " when recovery is active\n");
2460 nodemap = get_nodemap(ctdb, false);
2461 if (nodemap == NULL) {
2465 for (i=0; i<nodemap->num; i++) {
2468 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
2471 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
2474 if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
2475 fprintf(stderr, "Database cannot be detached on"
2476 " inactive (stopped or banned) node %u\n",
2477 nodemap->node[i].pnn);
2481 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
2482 nodemap->node[i].pnn, TIMEOUT(),
2483 "AllowClientDBAttach", &value);
2486 "Unable to get tunable AllowClientDBAttach"
2487 " from node %u\n", nodemap->node[i].pnn);
2493 "Database access is still active on node %u."
2494 " Set AllowclientDBAttach=0 on all nodes.\n",
2495 nodemap->node[i].pnn);
2501 for (i=0; i<argc; i++) {
2502 if (! db_exists(mem_ctx, ctdb, argv[i], &db_id, &db_name,
2507 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
2509 "Persistent database %s cannot be detached\n",
2514 ret = ctdb_detach(mem_ctx, ctdb->ev, ctdb->client,
2517 fprintf(stderr, "Database %s detach failed\n", db_name);
2525 static int control_dumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2526 int argc, const char **argv)
2528 const char *mem_str;
2532 ret = ctdb_ctrl_dump_memory(mem_ctx, ctdb->ev, ctdb->client,
2533 ctdb->cmd_pnn, TIMEOUT(), &mem_str);
2538 n = write(1, mem_str, strlen(mem_str)+1);
2539 if (n < 0 || n != strlen(mem_str)+1) {
2540 fprintf(stderr, "Failed to write talloc summary\n");
2547 static void dump_memory(uint64_t srvid, TDB_DATA data, void *private_data)
2549 bool *done = (bool *)private_data;
2552 n = write(1, data.dptr, data.dsize);
2553 if (n < 0 || n != data.dsize) {
2554 fprintf(stderr, "Failed to write talloc summary\n");
2560 static int control_rddumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2561 int argc, const char **argv)
2563 struct ctdb_srvid_message msg = { 0 };
2567 msg.pnn = ctdb->pnn;
2568 msg.srvid = next_srvid(ctdb);
2570 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2571 msg.srvid, dump_memory, &done);
2576 ret = ctdb_message_mem_dump(mem_ctx, ctdb->ev, ctdb->client,
2577 ctdb->cmd_pnn, &msg);
2582 ctdb_client_wait(ctdb->ev, &done);
2586 static int control_getpid(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2587 int argc, const char **argv)
2592 ret = ctdb_ctrl_get_pid(mem_ctx, ctdb->ev, ctdb->client,
2593 ctdb->cmd_pnn, TIMEOUT(), &pid);
2598 printf("%u\n", pid);
2602 static int check_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2603 const char *desc, uint32_t flag, bool set_flag)
2605 struct ctdb_node_map *nodemap;
2608 nodemap = get_nodemap(ctdb, false);
2609 if (nodemap == NULL) {
2613 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2614 if (set_flag == flag_is_set) {
2616 fprintf(stderr, "Node %u is already %s\n",
2617 ctdb->cmd_pnn, desc);
2619 fprintf(stderr, "Node %u is not %s\n",
2620 ctdb->cmd_pnn, desc);
2628 static void wait_for_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2629 uint32_t flag, bool set_flag)
2631 struct ctdb_node_map *nodemap;
2635 nodemap = get_nodemap(ctdb, true);
2636 if (nodemap == NULL) {
2638 "Failed to get nodemap, trying again\n");
2643 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2644 if (flag_is_set == set_flag) {
2652 struct ipreallocate_state {
2657 static void ipreallocate_handler(uint64_t srvid, TDB_DATA data,
2660 struct ipreallocate_state *state =
2661 (struct ipreallocate_state *)private_data;
2663 if (data.dsize != sizeof(int)) {
2668 state->status = *(int *)data.dptr;
2672 static int ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb)
2674 struct ctdb_srvid_message msg = { 0 };
2675 struct ipreallocate_state state;
2678 msg.pnn = ctdb->pnn;
2679 msg.srvid = next_srvid(ctdb);
2682 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2684 ipreallocate_handler, &state);
2690 ret = ctdb_message_takeover_run(mem_ctx, ctdb->ev,
2692 CTDB_BROADCAST_CONNECTED,
2698 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done,
2704 if (state.status >= 0) {
2713 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2718 static int control_disable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2719 int argc, const char **argv)
2727 ret = check_flags(mem_ctx, ctdb, "disabled",
2728 NODE_FLAGS_PERMANENTLY_DISABLED, true);
2733 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2734 ctdb->cmd_pnn, TIMEOUT(),
2735 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2738 "Failed to set DISABLED flag on node %u\n",
2743 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, true);
2744 return ipreallocate(mem_ctx, ctdb);
2747 static int control_enable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2748 int argc, const char **argv)
2756 ret = check_flags(mem_ctx, ctdb, "disabled",
2757 NODE_FLAGS_PERMANENTLY_DISABLED, false);
2762 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2763 ctdb->cmd_pnn, TIMEOUT(),
2764 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2766 fprintf(stderr, "Failed to reset DISABLED flag on node %u\n",
2771 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, false);
2772 return ipreallocate(mem_ctx, ctdb);
2775 static int control_stop(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2776 int argc, const char **argv)
2784 ret = check_flags(mem_ctx, ctdb, "stopped",
2785 NODE_FLAGS_STOPPED, true);
2790 ret = ctdb_ctrl_stop_node(mem_ctx, ctdb->ev, ctdb->client,
2791 ctdb->cmd_pnn, TIMEOUT());
2793 fprintf(stderr, "Failed to stop node %u\n", ctdb->cmd_pnn);
2797 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, true);
2798 return ipreallocate(mem_ctx, ctdb);
2801 static int control_continue(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2802 int argc, const char **argv)
2810 ret = check_flags(mem_ctx, ctdb, "stopped",
2811 NODE_FLAGS_STOPPED, false);
2816 ret = ctdb_ctrl_continue_node(mem_ctx, ctdb->ev, ctdb->client,
2817 ctdb->cmd_pnn, TIMEOUT());
2819 fprintf(stderr, "Failed to continue stopped node %u\n",
2824 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, false);
2825 return ipreallocate(mem_ctx, ctdb);
2828 static int control_ban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2829 int argc, const char **argv)
2831 struct ctdb_ban_state ban_state;
2838 ret = check_flags(mem_ctx, ctdb, "banned",
2839 NODE_FLAGS_BANNED, true);
2844 ban_state.pnn = ctdb->cmd_pnn;
2845 ban_state.time = strtoul(argv[0], NULL, 0);
2847 if (ban_state.time == 0) {
2848 fprintf(stderr, "Ban time cannot be zero\n");
2852 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2853 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2855 fprintf(stderr, "Failed to ban node %u\n", ctdb->cmd_pnn);
2859 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, true);
2860 return ipreallocate(mem_ctx, ctdb);
2864 static int control_unban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2865 int argc, const char **argv)
2867 struct ctdb_ban_state ban_state;
2874 ret = check_flags(mem_ctx, ctdb, "banned",
2875 NODE_FLAGS_BANNED, false);
2880 ban_state.pnn = ctdb->cmd_pnn;
2883 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2884 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2886 fprintf(stderr, "Failed to unban node %u\n", ctdb->cmd_pnn);
2890 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, false);
2891 return ipreallocate(mem_ctx, ctdb);
2895 static int control_shutdown(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2896 int argc, const char **argv)
2904 ret = ctdb_ctrl_shutdown(mem_ctx, ctdb->ev, ctdb->client,
2905 ctdb->cmd_pnn, TIMEOUT());
2907 fprintf(stderr, "Unable to shutdown node %u\n", ctdb->cmd_pnn);
2914 static int get_generation(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2915 uint32_t *generation)
2919 struct ctdb_vnn_map *vnnmap;
2923 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2924 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
2926 fprintf(stderr, "Failed to find recovery master\n");
2930 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2931 recmaster, TIMEOUT(), &recmode);
2933 fprintf(stderr, "Failed to get recovery mode from node %u\n",
2938 if (recmode == CTDB_RECOVERY_ACTIVE) {
2943 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
2944 recmaster, TIMEOUT(), &vnnmap);
2946 fprintf(stderr, "Failed to get generation from node %u\n",
2951 if (vnnmap->generation == INVALID_GENERATION) {
2952 talloc_free(vnnmap);
2957 *generation = vnnmap->generation;
2958 talloc_free(vnnmap);
2963 static int control_recover(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2964 int argc, const char **argv)
2966 uint32_t generation, next_generation;
2973 ret = get_generation(mem_ctx, ctdb, &generation);
2978 ret = ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
2979 ctdb->cmd_pnn, TIMEOUT(),
2980 CTDB_RECOVERY_ACTIVE);
2982 fprintf(stderr, "Failed to set recovery mode active\n");
2987 ret = get_generation(mem_ctx, ctdb, &next_generation);
2990 "Failed to confirm end of recovery\n");
2994 if (next_generation != generation) {
3004 static int control_ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3005 int argc, const char **argv)
3008 usage("ipreallocate");
3011 return ipreallocate(mem_ctx, ctdb);
3014 static int control_isnotrecmaster(TALLOC_CTX *mem_ctx,
3015 struct ctdb_context *ctdb,
3016 int argc, const char **argv)
3022 usage("isnotrecmaster");
3025 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
3026 ctdb->pnn, TIMEOUT(), &recmaster);
3028 fprintf(stderr, "Failed to get recmaster\n");
3032 if (recmaster != ctdb->pnn) {
3033 printf("this node is not the recmaster\n");
3037 printf("this node is the recmaster\n");
3041 static int control_gratarp(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3042 int argc, const char **argv)
3044 struct ctdb_addr_info addr_info;
3051 if (! parse_ip(argv[0], NULL, 0, &addr_info.addr)) {
3052 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3055 addr_info.iface = argv[1];
3057 ret = ctdb_ctrl_send_gratuitous_arp(mem_ctx, ctdb->ev, ctdb->client,
3058 ctdb->cmd_pnn, TIMEOUT(),
3061 fprintf(stderr, "Unable to send gratuitous arp from node %u\n",
3069 static int control_tickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3070 int argc, const char **argv)
3072 ctdb_sock_addr src, dst;
3075 if (argc != 0 && argc != 2) {
3080 struct ctdb_connection *clist;
3084 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3090 for (i=0; i<count; i++) {
3091 ret = ctdb_sys_send_tcp(&clist[i].src,
3101 if (num_failed > 0) {
3102 fprintf(stderr, "Failed to send %d tickles\n",
3111 if (! parse_ip_port(argv[0], &src)) {
3112 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3116 if (! parse_ip_port(argv[1], &dst)) {
3117 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3121 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
3123 fprintf(stderr, "Failed to send tickle ack\n");
3130 static int control_gettickles(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3131 int argc, const char **argv)
3133 ctdb_sock_addr addr;
3134 struct ctdb_tickle_list *tickles;
3138 if (argc < 1 || argc > 2) {
3139 usage("gettickles");
3143 port = strtoul(argv[1], NULL, 10);
3146 if (! parse_ip(argv[0], NULL, port, &addr)) {
3147 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3151 ret = ctdb_ctrl_get_tcp_tickle_list(mem_ctx, ctdb->ev, ctdb->client,
3152 ctdb->cmd_pnn, TIMEOUT(), &addr,
3155 fprintf(stderr, "Failed to get list of connections\n");
3159 if (options.machinereadable) {
3160 printf("%s%s%s%s%s%s%s%s%s\n",
3162 "Source IP", options.sep,
3163 "Port", options.sep,
3164 "Destiation IP", options.sep,
3165 "Port", options.sep);
3166 for (i=0; i<tickles->num; i++) {
3167 printf("%s%s%s%u%s%s%s%u%s\n", options.sep,
3168 ctdb_sock_addr_to_string(
3169 mem_ctx, &tickles->conn[i].src),
3171 ntohs(tickles->conn[i].src.ip.sin_port),
3173 ctdb_sock_addr_to_string(
3174 mem_ctx, &tickles->conn[i].dst),
3176 ntohs(tickles->conn[i].dst.ip.sin_port),
3180 printf("Connections for IP: %s\n",
3181 ctdb_sock_addr_to_string(mem_ctx, &tickles->addr));
3182 printf("Num connections: %u\n", tickles->num);
3183 for (i=0; i<tickles->num; i++) {
3184 printf("SRC: %s:%u DST: %s:%u\n",
3185 ctdb_sock_addr_to_string(
3186 mem_ctx, &tickles->conn[i].src),
3187 ntohs(tickles->conn[i].src.ip.sin_port),
3188 ctdb_sock_addr_to_string(
3189 mem_ctx, &tickles->conn[i].dst),
3190 ntohs(tickles->conn[i].dst.ip.sin_port));
3194 talloc_free(tickles);
3198 typedef void (*clist_request_func)(struct ctdb_req_control *request,
3199 struct ctdb_connection *conn);
3201 typedef int (*clist_reply_func)(struct ctdb_reply_control *reply);
3203 struct process_clist_state {
3204 struct ctdb_connection *clist;
3206 int num_failed, num_total;
3207 clist_reply_func reply_func;
3210 static void process_clist_done(struct tevent_req *subreq);
3212 static struct tevent_req *process_clist_send(
3213 TALLOC_CTX *mem_ctx,
3214 struct ctdb_context *ctdb,
3215 struct ctdb_connection *clist,
3217 clist_request_func request_func,
3218 clist_reply_func reply_func)
3220 struct tevent_req *req, *subreq;
3221 struct process_clist_state *state;
3222 struct ctdb_req_control request;
3225 req = tevent_req_create(mem_ctx, &state, struct process_clist_state);
3230 state->clist = clist;
3231 state->count = count;
3232 state->reply_func = reply_func;
3234 for (i=0; i<count; i++) {
3235 request_func(&request, &clist[i]);
3236 subreq = ctdb_client_control_send(state, ctdb->ev,
3237 ctdb->client, ctdb->cmd_pnn,
3238 TIMEOUT(), &request);
3239 if (tevent_req_nomem(subreq, req)) {
3240 return tevent_req_post(req, ctdb->ev);
3242 tevent_req_set_callback(subreq, process_clist_done, req);
3248 static void process_clist_done(struct tevent_req *subreq)
3250 struct tevent_req *req = tevent_req_callback_data(
3251 subreq, struct tevent_req);
3252 struct process_clist_state *state = tevent_req_data(
3253 req, struct process_clist_state);
3254 struct ctdb_reply_control *reply;
3258 status = ctdb_client_control_recv(subreq, NULL, state, &reply);
3259 TALLOC_FREE(subreq);
3261 state->num_failed += 1;
3265 ret = state->reply_func(reply);
3267 state->num_failed += 1;
3272 state->num_total += 1;
3273 if (state->num_total == state->count) {
3274 tevent_req_done(req);
3278 static int process_clist_recv(struct tevent_req *req)
3280 struct process_clist_state *state = tevent_req_data(
3281 req, struct process_clist_state);
3283 return state->num_failed;
3286 static int control_addtickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3287 int argc, const char **argv)
3289 struct ctdb_connection conn;
3292 if (argc != 0 && argc != 2) {
3297 struct ctdb_connection *clist;
3298 struct tevent_req *req;
3301 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3309 req = process_clist_send(mem_ctx, ctdb, clist, count,
3310 ctdb_req_control_tcp_add_delayed_update,
3311 ctdb_reply_control_tcp_add_delayed_update);
3317 tevent_req_poll(req, ctdb->ev);
3320 ret = process_clist_recv(req);
3322 fprintf(stderr, "Failed to add %d tickles\n", ret);
3329 if (! parse_ip_port(argv[0], &conn.src)) {
3330 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3333 if (! parse_ip_port(argv[1], &conn.dst)) {
3334 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3338 ret = ctdb_ctrl_tcp_add_delayed_update(mem_ctx, ctdb->ev,
3339 ctdb->client, ctdb->cmd_pnn,
3342 fprintf(stderr, "Failed to register connection\n");
3349 static int control_deltickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3350 int argc, const char **argv)
3352 struct ctdb_connection conn;
3355 if (argc != 0 && argc != 2) {
3360 struct ctdb_connection *clist;
3361 struct tevent_req *req;
3364 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3372 req = process_clist_send(mem_ctx, ctdb, clist, count,
3373 ctdb_req_control_tcp_remove,
3374 ctdb_reply_control_tcp_remove);
3380 tevent_req_poll(req, ctdb->ev);
3383 ret = process_clist_recv(req);
3385 fprintf(stderr, "Failed to remove %d tickles\n", ret);
3392 if (! parse_ip_port(argv[0], &conn.src)) {
3393 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3396 if (! parse_ip_port(argv[1], &conn.dst)) {
3397 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3401 ret = ctdb_ctrl_tcp_remove(mem_ctx, ctdb->ev, ctdb->client,
3402 ctdb->cmd_pnn, TIMEOUT(), &conn);
3404 fprintf(stderr, "Failed to unregister connection\n");
3411 static int control_check_srvids(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3412 int argc, const char **argv)
3419 usage("check_srvids");
3422 srvid = talloc_array(mem_ctx, uint64_t, argc);
3423 if (srvid == NULL) {
3424 fprintf(stderr, "Memory allocation error\n");
3428 for (i=0; i<argc; i++) {
3429 srvid[i] = strtoull(argv[i], NULL, 0);
3432 ret = ctdb_ctrl_check_srvids(mem_ctx, ctdb->ev, ctdb->client,
3433 ctdb->cmd_pnn, TIMEOUT(), srvid, argc,
3436 fprintf(stderr, "Failed to check srvids on node %u\n",
3441 for (i=0; i<argc; i++) {
3442 printf("SRVID 0x%" PRIx64 " %s\n", srvid[i],
3443 (result[i] ? "exists" : "does not exist"));
3449 static int control_listnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3450 int argc, const char **argv)
3452 struct ctdb_node_map *nodemap;
3459 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
3460 if (nodemap == NULL) {
3464 for (i=0; i<nodemap->num; i++) {
3465 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
3469 if (options.machinereadable) {
3470 printf("%s%u%s%s%s\n", options.sep,
3471 nodemap->node[i].pnn, options.sep,
3472 ctdb_sock_addr_to_string(
3473 mem_ctx, &nodemap->node[i].addr),
3477 ctdb_sock_addr_to_string(
3478 mem_ctx, &nodemap->node[i].addr));
3485 static bool nodemap_identical(struct ctdb_node_map *nodemap1,
3486 struct ctdb_node_map *nodemap2)
3490 if (nodemap1->num != nodemap2->num) {
3494 for (i=0; i<nodemap1->num; i++) {
3495 struct ctdb_node_and_flags *n1, *n2;
3497 n1 = &nodemap1->node[i];
3498 n2 = &nodemap2->node[i];
3500 if ((n1->pnn != n2->pnn) ||
3501 (n1->flags != n2->flags) ||
3502 ! ctdb_sock_addr_same_ip(&n1->addr, &n2->addr)) {
3510 static int check_node_file_changes(TALLOC_CTX *mem_ctx,
3511 struct ctdb_node_map *nm,
3512 struct ctdb_node_map *fnm,
3516 bool check_failed = false;
3520 for (i=0; i<nm->num; i++) {
3521 if (i >= fnm->num) {
3523 "Node %u (%s) missing from nodes file\n",
3525 ctdb_sock_addr_to_string(
3526 mem_ctx, &nm->node[i].addr));
3527 check_failed = true;
3530 if (nm->node[i].flags & NODE_FLAGS_DELETED &&
3531 fnm->node[i].flags & NODE_FLAGS_DELETED) {
3532 /* Node remains deleted */
3536 if (! (nm->node[i].flags & NODE_FLAGS_DELETED) &&
3537 ! (fnm->node[i].flags & NODE_FLAGS_DELETED)) {
3538 /* Node not newly nor previously deleted */
3539 if (! ctdb_same_ip(&nm->node[i].addr,
3540 &fnm->node[i].addr)) {
3542 "Node %u has changed IP address"
3543 " (was %s, now %s)\n",
3545 ctdb_sock_addr_to_string(
3546 mem_ctx, &nm->node[i].addr),
3547 ctdb_sock_addr_to_string(
3548 mem_ctx, &fnm->node[i].addr));
3549 check_failed = true;
3551 if (nm->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3553 "WARNING: Node %u is disconnected."
3554 " You MUST fix this node manually!\n",
3561 if (fnm->node[i].flags & NODE_FLAGS_DELETED) {
3562 /* Node is being deleted */
3563 printf("Node %u is DELETED\n", nm->node[i].pnn);
3565 if (! (nm->node[i].flags & NODE_FLAGS_DISCONNECTED)) {
3567 "ERROR: Node %u is still connected\n",
3569 check_failed = true;
3574 if (nm->node[i].flags & NODE_FLAGS_DELETED) {
3575 /* Node was previously deleted */
3576 printf("Node %u is UNDELETED\n", nm->node[i].pnn);
3583 "ERROR: Nodes will not be reloaded due to previous error\n");
3587 /* Leftover nodes in file are NEW */
3588 for (; i < fnm->num; i++) {
3589 printf("Node %u is NEW\n", fnm->node[i].pnn);
3596 struct disable_recoveries_state {
3604 static void disable_recoveries_handler(uint64_t srvid, TDB_DATA data,
3607 struct disable_recoveries_state *state =
3608 (struct disable_recoveries_state *)private_data;
3611 if (data.dsize != sizeof(int)) {
3616 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
3617 ret = *(int *)data.dptr;
3619 state->status = ret;
3623 for (i=0; i<state->node_count; i++) {
3624 if (state->pnn_list[i] == ret) {
3625 state->reply[i] = true;
3631 for (i=0; i<state->node_count; i++) {
3632 if (! state->reply[i]) {
3633 state->done = false;
3639 static int disable_recoveries(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3640 uint32_t timeout, uint32_t *pnn_list, int count)
3642 struct ctdb_disable_message disable = { 0 };
3643 struct disable_recoveries_state state;
3646 disable.pnn = ctdb->pnn;
3647 disable.srvid = next_srvid(ctdb);
3648 disable.timeout = timeout;
3650 state.pnn_list = pnn_list;
3651 state.node_count = count;
3654 state.reply = talloc_zero_array(mem_ctx, bool, count);
3655 if (state.reply == NULL) {
3659 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
3661 disable_recoveries_handler,
3667 for (i=0; i<count; i++) {
3668 ret = ctdb_message_disable_recoveries(mem_ctx, ctdb->ev,
3677 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
3679 fprintf(stderr, "Timed out waiting to disable recoveries\n");
3681 ret = (state.status >= 0 ? 0 : 1);
3685 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
3686 disable.srvid, &state);
3690 static int control_reloadnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3691 int argc, const char **argv)
3693 struct ctdb_node_map *nodemap = NULL;
3694 struct ctdb_node_map *file_nodemap;
3695 struct ctdb_node_map *remote_nodemap;
3696 struct ctdb_req_control request;
3697 struct ctdb_reply_control **reply;
3703 nodemap = get_nodemap(ctdb, false);
3704 if (nodemap == NULL) {
3708 file_nodemap = read_nodes_file(mem_ctx, ctdb->pnn);
3709 if (file_nodemap == NULL) {
3713 for (i=0; i<nodemap->num; i++) {
3714 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3718 ret = ctdb_ctrl_get_nodes_file(mem_ctx, ctdb->ev, ctdb->client,
3719 nodemap->node[i].pnn, TIMEOUT(),
3723 "ERROR: Failed to get nodes file from node %u\n",
3724 nodemap->node[i].pnn);
3728 if (! nodemap_identical(file_nodemap, remote_nodemap)) {
3730 "ERROR: Nodes file on node %u differs"
3731 " from current node (%u)\n",
3732 nodemap->node[i].pnn, ctdb->pnn);
3737 ret = check_node_file_changes(mem_ctx, nodemap, file_nodemap, &reload);
3743 fprintf(stderr, "No change in nodes file,"
3744 " skipping unnecessary reload\n");
3748 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
3749 mem_ctx, &pnn_list);
3751 fprintf(stderr, "Memory allocation error\n");
3755 ret = disable_recoveries(mem_ctx, ctdb, 2*options.timelimit,
3758 fprintf(stderr, "Failed to disable recoveries\n");
3762 ctdb_req_control_reload_nodes_file(&request);
3763 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3764 pnn_list, count, TIMEOUT(),
3765 &request, NULL, &reply);
3767 bool failed = false;
3769 for (i=0; i<count; i++) {
3770 ret = ctdb_reply_control_reload_nodes_file(reply[i]);
3773 "Node %u failed to reload nodes\n",
3780 "You MUST fix failed nodes manually!\n");
3784 ret = disable_recoveries(mem_ctx, ctdb, 0, pnn_list, count);
3786 fprintf(stderr, "Failed to enable recoveries\n");
3793 static int moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3794 ctdb_sock_addr *addr, uint32_t pnn)
3796 struct ctdb_public_ip_list *pubip_list;
3797 struct ctdb_public_ip pubip;
3798 struct ctdb_node_map *nodemap;
3799 struct ctdb_req_control request;
3803 ret = ctdb_message_disable_ip_check(mem_ctx, ctdb->ev, ctdb->client,
3804 CTDB_BROADCAST_CONNECTED,
3805 2*options.timelimit);
3807 fprintf(stderr, "Failed to disable IP check\n");
3811 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3812 pnn, TIMEOUT(), &pubip_list);
3814 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3819 for (i=0; i<pubip_list->num; i++) {
3820 if (ctdb_same_ip(addr, &pubip_list->ip[i].addr)) {
3825 if (i == pubip_list->num) {
3826 fprintf(stderr, "Node %u CANNOT host IP address %s\n",
3827 pnn, ctdb_sock_addr_to_string(mem_ctx, addr));
3831 nodemap = get_nodemap(ctdb, false);
3832 if (nodemap == NULL) {
3836 count = list_of_active_nodes(nodemap, pnn, mem_ctx, &pnn_list);
3838 fprintf(stderr, "Memory allocation error\n");
3844 ctdb_req_control_release_ip(&request, &pubip);
3846 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3847 pnn_list, count, TIMEOUT(),
3848 &request, NULL, NULL);
3850 fprintf(stderr, "Failed to release IP on nodes\n");
3854 ret = ctdb_ctrl_takeover_ip(mem_ctx, ctdb->ev, ctdb->client,
3855 pnn, TIMEOUT(), &pubip);
3857 fprintf(stderr, "Failed to takeover IP on node %u\n", pnn);
3864 static int control_moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3865 int argc, const char **argv)
3867 ctdb_sock_addr addr;
3869 int ret, retries = 0;
3875 if (! parse_ip(argv[0], NULL, 0, &addr)) {
3876 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3880 pnn = strtoul(argv[1], NULL, 10);
3881 if (pnn == CTDB_UNKNOWN_PNN) {
3882 fprintf(stderr, "Invalid PNN %s\n", argv[1]);
3886 while (retries < 5) {
3887 ret = moveip(mem_ctx, ctdb, &addr, pnn);
3897 fprintf(stderr, "Failed to move IP %s to node %u\n",
3905 static int rebalancenode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3910 ret = ctdb_message_rebalance_node(mem_ctx, ctdb->ev, ctdb->client,
3911 CTDB_BROADCAST_CONNECTED, pnn);
3914 "Failed to ask recovery master to distribute IPs\n");
3921 static int control_addip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3922 int argc, const char **argv)
3924 ctdb_sock_addr addr;
3925 struct ctdb_public_ip_list *pubip_list;
3926 struct ctdb_addr_info addr_info;
3928 int ret, i, retries = 0;
3934 if (! parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
3935 fprintf(stderr, "Invalid IP/Mask %s\n", argv[0]);
3939 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3940 ctdb->cmd_pnn, TIMEOUT(), &pubip_list);
3942 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3947 for (i=0; i<pubip_list->num; i++) {
3948 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3949 fprintf(stderr, "Node already knows about IP %s\n",
3950 ctdb_sock_addr_to_string(mem_ctx, &addr));
3955 addr_info.addr = addr;
3956 addr_info.mask = mask;
3957 addr_info.iface = argv[1];
3959 while (retries < 5) {
3960 ret = ctdb_ctrl_add_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3961 ctdb->cmd_pnn, TIMEOUT(),
3972 fprintf(stderr, "Failed to add public IP to node %u."
3973 " Giving up\n", ctdb->cmd_pnn);
3977 ret = rebalancenode(mem_ctx, ctdb, ctdb->cmd_pnn);
3985 static int control_delip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3986 int argc, const char **argv)
3988 ctdb_sock_addr addr;
3989 struct ctdb_public_ip_list *pubip_list;
3990 struct ctdb_addr_info addr_info;
3997 if (! parse_ip(argv[0], NULL, 0, &addr)) {
3998 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
4002 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
4003 ctdb->cmd_pnn, TIMEOUT(), &pubip_list);
4005 fprintf(stderr, "Failed to get Public IPs from node %u\n",
4010 for (i=0; i<pubip_list->num; i++) {
4011 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
4016 if (i == pubip_list->num) {
4017 fprintf(stderr, "Node does not know about IP address %s\n",
4018 ctdb_sock_addr_to_string(mem_ctx, &addr));
4022 addr_info.addr = addr;
4024 addr_info.iface = NULL;
4026 ret = ctdb_ctrl_del_public_ip(mem_ctx, ctdb->ev, ctdb->client,
4027 ctdb->cmd_pnn, TIMEOUT(), &addr_info);
4029 fprintf(stderr, "Failed to delete public IP from node %u\n",
4037 #define DB_VERSION 3
4038 #define MAX_DB_NAME 64
4039 #define MAX_REC_BUFFER_SIZE (100*1000)
4042 unsigned long version;
4044 unsigned long flags;
4047 char name[MAX_DB_NAME];
4050 struct backup_state {
4051 TALLOC_CTX *mem_ctx;
4052 struct ctdb_rec_buffer *recbuf;
4055 unsigned int nbuf, nrec;
4058 static int backup_handler(uint32_t reqid, struct ctdb_ltdb_header *header,
4059 TDB_DATA key, TDB_DATA data, void *private_data)
4061 struct backup_state *state = (struct backup_state *)private_data;
4065 if (state->recbuf == NULL) {
4066 state->recbuf = ctdb_rec_buffer_init(state->mem_ctx,
4068 if (state->recbuf == NULL) {
4073 ret = ctdb_rec_buffer_add(state->recbuf, state->recbuf, reqid,
4079 len = ctdb_rec_buffer_len(state->recbuf);
4080 if (len < MAX_REC_BUFFER_SIZE) {
4084 ret = ctdb_rec_buffer_write(state->recbuf, state->fd);
4086 fprintf(stderr, "Failed to write records to backup file\n");
4091 state->nrec += state->recbuf->count;
4092 TALLOC_FREE(state->recbuf);
4097 static int control_backupdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4098 int argc, const char **argv)
4100 const char *db_name;
4101 struct ctdb_db_context *db;
4104 struct backup_state state;
4105 struct db_header db_hdr;
4112 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4116 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4119 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4123 fd = open(argv[1], O_RDWR|O_CREAT, 0600);
4126 fprintf(stderr, "Failed to open file %s for writing\n",
4131 /* Write empty header first */
4132 ZERO_STRUCT(db_hdr);
4133 ret = write(fd, &db_hdr, sizeof(struct db_header));
4137 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4141 state.mem_ctx = mem_ctx;
4142 state.recbuf = NULL;
4147 ret = ctdb_db_traverse(db, true, false, backup_handler, &state);
4149 fprintf(stderr, "Failed to collect records from DB %s\n",
4155 if (state.recbuf != NULL) {
4156 ret = ctdb_rec_buffer_write(state.recbuf, state.fd);
4159 "Failed to write records to backup file\n");
4165 state.nrec += state.recbuf->count;
4166 TALLOC_FREE(state.recbuf);
4169 db_hdr.version = DB_VERSION;
4170 db_hdr.timestamp = time(NULL);
4171 db_hdr.flags = db_flags;
4172 db_hdr.nbuf = state.nbuf;
4173 db_hdr.nrec = state.nrec;
4174 strncpy(db_hdr.name, db_name, MAX_DB_NAME-1);
4176 lseek(fd, 0, SEEK_SET);
4177 ret = write(fd, &db_hdr, sizeof(struct db_header));
4181 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4186 printf("Database backed up to %s\n", argv[1]);
4190 static int control_restoredb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4191 int argc, const char **argv)
4193 const char *db_name = NULL;
4194 struct ctdb_db_context *db;
4195 struct db_header db_hdr;
4196 struct ctdb_node_map *nodemap;
4197 struct ctdb_req_control request;
4198 struct ctdb_reply_control **reply;
4199 struct ctdb_transdb wipedb;
4200 struct ctdb_pulldb_ext pulldb;
4201 struct ctdb_rec_buffer *recbuf;
4202 uint32_t generation;
4208 if (argc < 1 || argc > 2) {
4212 fd = open(argv[0], O_RDONLY, 0600);
4215 fprintf(stderr, "Failed to open file %s for reading\n",
4224 ret = read(fd, &db_hdr, sizeof(struct db_header));
4228 fprintf(stderr, "Failed to read db header from file %s\n",
4232 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4234 if (db_hdr.version != DB_VERSION) {
4236 "Wrong version of backup file, expected %u, got %lu\n",
4237 DB_VERSION, db_hdr.version);
4242 if (db_name == NULL) {
4243 db_name = db_hdr.name;
4246 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4247 localtime(&db_hdr.timestamp));
4248 printf("Restoring database %s from backup @ %s\n", db_name, timebuf);
4250 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4253 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4258 nodemap = get_nodemap(ctdb, false);
4259 if (nodemap == NULL) {
4260 fprintf(stderr, "Failed to get nodemap\n");
4265 ret = get_generation(mem_ctx, ctdb, &generation);
4267 fprintf(stderr, "Failed to get current generation\n");
4272 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4279 wipedb.db_id = ctdb_db_id(db);
4280 wipedb.tid = generation;
4282 ctdb_req_control_db_freeze(&request, wipedb.db_id);
4283 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4284 ctdb->client, pnn_list, count,
4285 TIMEOUT(), &request, NULL, NULL);
4291 ctdb_req_control_db_transaction_start(&request, &wipedb);
4292 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4293 pnn_list, count, TIMEOUT(),
4294 &request, NULL, NULL);
4299 ctdb_req_control_wipe_database(&request, &wipedb);
4300 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4301 pnn_list, count, TIMEOUT(),
4302 &request, NULL, NULL);
4307 pulldb.db_id = ctdb_db_id(db);
4309 pulldb.srvid = SRVID_CTDB_PUSHDB;
4311 ctdb_req_control_db_push_start(&request, &pulldb);
4312 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4313 pnn_list, count, TIMEOUT(),
4314 &request, NULL, NULL);
4319 for (i=0; i<db_hdr.nbuf; i++) {
4320 struct ctdb_req_message message;
4323 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4328 data.dsize = ctdb_rec_buffer_len(recbuf);
4329 data.dptr = talloc_size(mem_ctx, data.dsize);
4330 if (data.dptr == NULL) {
4334 ctdb_rec_buffer_push(recbuf, data.dptr);
4336 message.srvid = pulldb.srvid;
4337 message.data.data = data;
4339 ret = ctdb_client_message_multi(mem_ctx, ctdb->ev,
4347 talloc_free(recbuf);
4348 talloc_free(data.dptr);
4351 ctdb_req_control_db_push_confirm(&request, pulldb.db_id);
4352 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4353 pnn_list, count, TIMEOUT(),
4354 &request, NULL, &reply);
4359 for (i=0; i<count; i++) {
4360 uint32_t num_records;
4362 ret = ctdb_reply_control_db_push_confirm(reply[i],
4365 fprintf(stderr, "Invalid response from node %u\n",
4370 if (num_records != db_hdr.nrec) {
4371 fprintf(stderr, "Node %u received %u of %lu records\n",
4372 pnn_list[i], num_records, db_hdr.nrec);
4377 ctdb_req_control_db_set_healthy(&request, wipedb.db_id);
4378 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4379 pnn_list, count, TIMEOUT(),
4380 &request, NULL, NULL);
4385 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4386 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4387 pnn_list, count, TIMEOUT(),
4388 &request, NULL, NULL);
4393 ctdb_req_control_db_thaw(&request, wipedb.db_id);
4394 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4395 ctdb->client, pnn_list, count,
4396 TIMEOUT(), &request, NULL, NULL);
4401 printf("Database %s restored\n", db_name);
4407 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4408 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4412 struct dumpdbbackup_state {
4413 ctdb_rec_parser_func_t parser;
4414 struct dump_record_state sub_state;
4417 static int dumpdbbackup_handler(uint32_t reqid,
4418 struct ctdb_ltdb_header *header,
4419 TDB_DATA key, TDB_DATA data,
4422 struct dumpdbbackup_state *state =
4423 (struct dumpdbbackup_state *)private_data;
4424 struct ctdb_ltdb_header hdr;
4427 ret = ctdb_ltdb_header_extract(&data, &hdr);
4432 return state->parser(reqid, &hdr, key, data, &state->sub_state);
4435 static int control_dumpdbbackup(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4436 int argc, const char **argv)
4438 struct db_header db_hdr;
4440 struct dumpdbbackup_state state;
4444 usage("dumpbackup");
4447 fd = open(argv[0], O_RDONLY, 0600);
4450 fprintf(stderr, "Failed to open file %s for reading\n",
4455 ret = read(fd, &db_hdr, sizeof(struct db_header));
4459 fprintf(stderr, "Failed to read db header from file %s\n",
4463 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4465 if (db_hdr.version != DB_VERSION) {
4467 "Wrong version of backup file, expected %u, got %lu\n",
4468 DB_VERSION, db_hdr.version);
4473 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4474 localtime(&db_hdr.timestamp));
4475 printf("Dumping database %s from backup @ %s\n",
4476 db_hdr.name, timebuf);
4478 state.parser = dump_record;
4479 state.sub_state.count = 0;
4481 for (i=0; i<db_hdr.nbuf; i++) {
4482 struct ctdb_rec_buffer *recbuf;
4484 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4486 fprintf(stderr, "Failed to read records\n");
4491 ret = ctdb_rec_buffer_traverse(recbuf, dumpdbbackup_handler,
4494 fprintf(stderr, "Failed to dump records\n");
4501 printf("Dumped %u record(s)\n", state.sub_state.count);
4505 static int control_wipedb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4506 int argc, const char **argv)
4508 const char *db_name;
4509 struct ctdb_db_context *db;
4512 struct ctdb_node_map *nodemap;
4513 struct ctdb_req_control request;
4514 struct ctdb_transdb wipedb;
4515 uint32_t generation;
4523 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4527 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4530 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4534 nodemap = get_nodemap(ctdb, false);
4535 if (nodemap == NULL) {
4536 fprintf(stderr, "Failed to get nodemap\n");
4540 ret = get_generation(mem_ctx, ctdb, &generation);
4542 fprintf(stderr, "Failed to get current generation\n");
4546 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4552 ctdb_req_control_db_freeze(&request, db_id);
4553 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4554 ctdb->client, pnn_list, count,
4555 TIMEOUT(), &request, NULL, NULL);
4560 wipedb.db_id = db_id;
4561 wipedb.tid = generation;
4563 ctdb_req_control_db_transaction_start(&request, &wipedb);
4564 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4565 pnn_list, count, TIMEOUT(),
4566 &request, NULL, NULL);
4571 ctdb_req_control_wipe_database(&request, &wipedb);
4572 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4573 pnn_list, count, TIMEOUT(),
4574 &request, NULL, NULL);
4579 ctdb_req_control_db_set_healthy(&request, db_id);
4580 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4581 pnn_list, count, TIMEOUT(),
4582 &request, NULL, NULL);
4587 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4588 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4589 pnn_list, count, TIMEOUT(),
4590 &request, NULL, NULL);
4595 ctdb_req_control_db_thaw(&request, db_id);
4596 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4597 ctdb->client, pnn_list, count,
4598 TIMEOUT(), &request, NULL, NULL);
4603 printf("Database %s wiped\n", db_name);
4608 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4609 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4613 static int control_recmaster(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4614 int argc, const char **argv)
4619 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
4620 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
4625 printf("%u\n", recmaster);
4629 static int control_event(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4630 int argc, const char **argv)
4632 char *t, *event_helper = NULL;
4633 char *eventd_socket = NULL;
4634 const char **new_argv;
4637 t = getenv("CTDB_EVENT_HELPER");
4639 event_helper = talloc_strdup(mem_ctx, t);
4641 event_helper = talloc_asprintf(mem_ctx, "%s/ctdb_event",
4642 CTDB_HELPER_BINDIR);
4645 if (event_helper == NULL) {
4646 fprintf(stderr, "Unable to set event daemon helper\n");
4650 t = getenv("CTDB_SOCKET");
4652 eventd_socket = talloc_asprintf(mem_ctx, "%s/eventd.sock",
4655 eventd_socket = talloc_asprintf(mem_ctx, "%s/eventd.sock",
4659 if (eventd_socket == NULL) {
4660 fprintf(stderr, "Unable to set event daemon socket\n");
4664 new_argv = talloc_array(mem_ctx, const char *, argc + 1);
4665 if (new_argv == NULL) {
4666 fprintf(stderr, "Memory allocation error\n");
4670 new_argv[0] = eventd_socket;
4671 for (i=0; i<argc; i++) {
4672 new_argv[i+1] = argv[i];
4675 return run_helper(mem_ctx, "event daemon helper", event_helper,
4679 static int control_scriptstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4680 int argc, const char **argv)
4682 const char *new_argv[3];
4685 usage("scriptstatus");
4688 new_argv[0] = "status";
4689 new_argv[1] = (argc == 0) ? "monitor" : argv[0];
4692 (void) control_event(mem_ctx, ctdb, 2, new_argv);
4696 static int control_natgw(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4697 int argc, const char **argv)
4699 char *t, *natgw_helper = NULL;
4705 t = getenv("CTDB_NATGW_HELPER");
4707 natgw_helper = talloc_strdup(mem_ctx, t);
4709 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4710 CTDB_HELPER_BINDIR);
4713 if (natgw_helper == NULL) {
4714 fprintf(stderr, "Unable to set NAT gateway helper\n");
4718 return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
4722 static int control_natgwlist(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4723 int argc, const char **argv)
4725 char *t, *natgw_helper = NULL;
4726 const char *cmd_argv[] = { "natgwlist", NULL };
4732 t = getenv("CTDB_NATGW_HELPER");
4734 natgw_helper = talloc_strdup(mem_ctx, t);
4736 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4737 CTDB_HELPER_BINDIR);
4740 if (natgw_helper == NULL) {
4741 fprintf(stderr, "Unable to set NAT gateway helper\n");
4745 return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
4750 * Find the PNN of the current node
4751 * discover the pnn by loading the nodes file and try to bind
4752 * to all addresses one at a time until the ip address is found.
4754 static bool find_node_xpnn(TALLOC_CTX *mem_ctx, uint32_t *pnn)
4756 struct ctdb_node_map *nodemap;
4759 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
4760 if (nodemap == NULL) {
4764 for (i=0; i<nodemap->num; i++) {
4765 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
4768 if (ctdb_sys_have_ip(&nodemap->node[i].addr)) {
4770 *pnn = nodemap->node[i].pnn;
4772 talloc_free(nodemap);
4777 fprintf(stderr, "Failed to detect PNN of the current node.\n");
4778 talloc_free(nodemap);
4782 static int control_getreclock(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4783 int argc, const char **argv)
4785 const char *reclock;
4789 usage("getreclock");
4792 ret = ctdb_ctrl_get_reclock_file(mem_ctx, ctdb->ev, ctdb->client,
4793 ctdb->cmd_pnn, TIMEOUT(), &reclock);
4798 if (reclock != NULL) {
4799 printf("%s\n", reclock);
4805 static int control_setlmasterrole(TALLOC_CTX *mem_ctx,
4806 struct ctdb_context *ctdb,
4807 int argc, const char **argv)
4809 uint32_t lmasterrole = 0;
4813 usage("setlmasterrole");
4816 if (strcmp(argv[0], "on") == 0) {
4818 } else if (strcmp(argv[0], "off") == 0) {
4821 usage("setlmasterrole");
4824 ret = ctdb_ctrl_set_lmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4825 ctdb->cmd_pnn, TIMEOUT(), lmasterrole);
4833 static int control_setrecmasterrole(TALLOC_CTX *mem_ctx,
4834 struct ctdb_context *ctdb,
4835 int argc, const char **argv)
4837 uint32_t recmasterrole = 0;
4841 usage("setrecmasterrole");
4844 if (strcmp(argv[0], "on") == 0) {
4846 } else if (strcmp(argv[0], "off") == 0) {
4849 usage("setrecmasterrole");
4852 ret = ctdb_ctrl_set_recmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4853 ctdb->cmd_pnn, TIMEOUT(),
4862 static int control_setdbreadonly(TALLOC_CTX *mem_ctx,
4863 struct ctdb_context *ctdb,
4864 int argc, const char **argv)
4871 usage("setdbreadonly");
4874 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
4878 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
4879 fprintf(stderr, "Cannot set READONLY on persistent DB\n");
4883 ret = ctdb_ctrl_set_db_readonly(mem_ctx, ctdb->ev, ctdb->client,
4884 ctdb->cmd_pnn, TIMEOUT(), db_id);
4892 static int control_setdbsticky(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4893 int argc, const char **argv)
4900 usage("setdbsticky");
4903 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
4907 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
4908 fprintf(stderr, "Cannot set STICKY on persistent DB\n");
4912 ret = ctdb_ctrl_set_db_sticky(mem_ctx, ctdb->ev, ctdb->client,
4913 ctdb->cmd_pnn, TIMEOUT(), db_id);
4921 static int control_pfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4922 int argc, const char **argv)
4924 const char *db_name;
4925 struct ctdb_db_context *db;
4926 struct ctdb_transaction_handle *h;
4931 if (argc < 2 || argc > 3) {
4935 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
4939 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
4940 fprintf(stderr, "DB %s is not a persistent database\n",
4945 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4948 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4952 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
4954 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
4958 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
4959 TIMEOUT(), db, true, &h);
4961 fprintf(stderr, "Failed to start transaction on db %s\n",
4966 ret = ctdb_transaction_fetch_record(h, key, mem_ctx, &data);
4968 fprintf(stderr, "Failed to read record for key %s\n",
4970 ctdb_transaction_cancel(h);
4974 printf("%.*s\n", (int)data.dsize, data.dptr);
4976 ctdb_transaction_cancel(h);
4980 static int control_pstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4981 int argc, const char **argv)
4983 const char *db_name;
4984 struct ctdb_db_context *db;
4985 struct ctdb_transaction_handle *h;
4994 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
4998 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
4999 fprintf(stderr, "DB %s is not a persistent database\n",
5004 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5007 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5011 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5013 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5017 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5019 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5023 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5024 TIMEOUT(), db, false, &h);
5026 fprintf(stderr, "Failed to start transaction on db %s\n",
5031 ret = ctdb_transaction_store_record(h, key, data);
5033 fprintf(stderr, "Failed to store record for key %s\n",
5035 ctdb_transaction_cancel(h);
5039 ret = ctdb_transaction_commit(h);
5041 fprintf(stderr, "Failed to commit transaction on db %s\n",
5043 ctdb_transaction_cancel(h);
5050 static int control_pdelete(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5051 int argc, const char **argv)
5053 const char *db_name;
5054 struct ctdb_db_context *db;
5055 struct ctdb_transaction_handle *h;
5064 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5068 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5069 fprintf(stderr, "DB %s is not a persistent database\n",
5074 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5077 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5081 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5083 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5087 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5088 TIMEOUT(), db, false, &h);
5090 fprintf(stderr, "Failed to start transaction on db %s\n",
5095 ret = ctdb_transaction_delete_record(h, key);
5097 fprintf(stderr, "Failed to delete record for key %s\n",
5099 ctdb_transaction_cancel(h);
5103 ret = ctdb_transaction_commit(h);
5105 fprintf(stderr, "Failed to commit transaction on db %s\n",
5107 ctdb_transaction_cancel(h);
5114 static int ptrans_parse_string(TALLOC_CTX *mem_ctx, const char **ptr, TDB_DATA *data)
5122 /* Skip whitespace */
5123 n = strspn(*ptr, " \t");
5127 /* Quoted ASCII string - no wide characters! */
5129 n = strcspn(t, "\"");
5132 ret = str_to_data(t, n, mem_ctx, data);
5139 fprintf(stderr, "Unmatched \" in input %s\n", *ptr);
5143 fprintf(stderr, "Unsupported input format in %s\n", *ptr);
5150 #define MAX_LINE_SIZE 1024
5152 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
5153 TDB_DATA *key, TDB_DATA *value)
5155 char line [MAX_LINE_SIZE]; /* FIXME: make this more flexible? */
5159 ptr = fgets(line, MAX_LINE_SIZE, file);
5165 ret = ptrans_parse_string(mem_ctx, &ptr, key);
5166 if (ret != 0 || ptr == NULL || key->dptr == NULL) {
5167 /* Line Ignored but not EOF */
5173 ret = ptrans_parse_string(mem_ctx, &ptr, value);
5175 /* Line Ignored but not EOF */
5176 talloc_free(key->dptr);
5184 static int control_ptrans(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5185 int argc, const char **argv)
5187 const char *db_name;
5188 struct ctdb_db_context *db;
5189 struct ctdb_transaction_handle *h;
5192 TDB_DATA key, value;
5195 if (argc < 1 || argc > 2) {
5199 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5203 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5204 fprintf(stderr, "DB %s is not a persistent database\n",
5210 file = fopen(argv[1], "r");
5212 fprintf(stderr, "Failed to open file %s\n", argv[1]);
5219 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5222 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5226 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5227 TIMEOUT(), db, false, &h);
5229 fprintf(stderr, "Failed to start transaction on db %s\n",
5234 while (ptrans_get_key_value(mem_ctx, file, &key, &value)) {
5235 if (key.dsize != 0) {
5236 ret = ctdb_transaction_store_record(h, key, value);
5238 fprintf(stderr, "Failed to store record\n");
5239 ctdb_transaction_cancel(h);
5242 talloc_free(key.dptr);
5243 talloc_free(value.dptr);
5247 ret = ctdb_transaction_commit(h);
5249 fprintf(stderr, "Failed to commit transaction on db %s\n",
5251 ctdb_transaction_cancel(h);
5255 if (file != stdin) {
5261 static int control_tfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5262 int argc, const char **argv)
5264 struct tdb_context *tdb;
5266 struct ctdb_ltdb_header header;
5269 if (argc < 2 || argc > 3) {
5273 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5275 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5279 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5281 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5286 data = tdb_fetch(tdb, key);
5287 if (data.dptr == NULL) {
5288 fprintf(stderr, "No record for key %s\n", argv[1]);
5293 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
5294 fprintf(stderr, "Invalid record for key %s\n", argv[1]);
5305 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
5307 fprintf(stderr, "Failed to open output file %s\n",
5312 nwritten = sys_write(fd, data.dptr, data.dsize);
5313 if (nwritten != data.dsize) {
5314 fprintf(stderr, "Failed to write record to file\n");
5323 ret = ctdb_ltdb_header_extract(&data, &header);
5325 fprintf(stderr, "Failed to parse header from data\n");
5329 dump_ltdb_header(&header);
5330 dump_tdb_data("data", data);
5335 static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5336 int argc, const char **argv)
5338 struct tdb_context *tdb;
5339 TDB_DATA key, data[2], value;
5340 struct ctdb_ltdb_header header;
5341 uint8_t header_buf[sizeof(struct ctdb_ltdb_header)];
5344 if (argc < 3 || argc > 5) {
5348 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5350 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5354 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5356 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5361 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &value);
5363 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5368 ZERO_STRUCT(header);
5371 header.rsn = (uint64_t)strtoull(argv[3], NULL, 0);
5374 header.dmaster = (uint32_t)atol(argv[4]);
5377 header.flags = (uint32_t)atol(argv[5]);
5380 ctdb_ltdb_header_push(&header, header_buf);
5382 data[0].dsize = ctdb_ltdb_header_len(&header);
5383 data[0].dptr = header_buf;
5385 data[1].dsize = value.dsize;
5386 data[1].dptr = value.dptr;
5388 ret = tdb_storev(tdb, key, data, 2, TDB_REPLACE);
5390 fprintf(stderr, "Failed to write record %s to file %s\n",
5399 static int control_readkey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5400 int argc, const char **argv)
5402 const char *db_name;
5403 struct ctdb_db_context *db;
5404 struct ctdb_record_handle *h;
5407 bool readonly = false;
5410 if (argc < 2 || argc > 3) {
5415 if (strcmp(argv[2], "readonly") == 0) {
5422 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5426 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5427 fprintf(stderr, "DB %s is not a volatile database\n",
5432 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5435 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5439 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5441 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5445 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5446 db, key, readonly, &h, NULL, &data);
5448 fprintf(stderr, "Failed to read record for key %s\n",
5451 printf("Data: size:%zu ptr:[%.*s]\n", data.dsize,
5452 (int)data.dsize, data.dptr);
5459 static int control_writekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5460 int argc, const char **argv)
5462 const char *db_name;
5463 struct ctdb_db_context *db;
5464 struct ctdb_record_handle *h;
5473 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5477 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5478 fprintf(stderr, "DB %s is not a volatile database\n",
5483 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5486 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5490 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5492 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5496 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5498 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5502 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5503 db, key, false, &h, NULL, NULL);
5505 fprintf(stderr, "Failed to lock record for key %s\n", argv[0]);
5509 ret = ctdb_store_record(h, data);
5511 fprintf(stderr, "Failed to store record for key %s\n",
5519 static int control_deletekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5520 int argc, const char **argv)
5522 const char *db_name;
5523 struct ctdb_db_context *db;
5524 struct ctdb_record_handle *h;
5533 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5537 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5538 fprintf(stderr, "DB %s is not a volatile database\n",
5543 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5546 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5550 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5552 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5556 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5557 db, key, false, &h, NULL, &data);
5559 fprintf(stderr, "Failed to fetch record for key %s\n",
5564 ret = ctdb_delete_record(h);
5566 fprintf(stderr, "Failed to delete record for key %s\n",
5574 static int control_checktcpport(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5575 int argc, const char **argv)
5577 struct sockaddr_in sin;
5583 usage("chktcpport");
5586 port = atoi(argv[0]);
5588 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
5590 fprintf(stderr, "Failed to open local socket\n");
5594 v = fcntl(s, F_GETFL, 0);
5595 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK)) {
5596 fprintf(stderr, "Unable to set socket non-blocking\n");
5601 bzero(&sin, sizeof(sin));
5602 sin.sin_family = AF_INET;
5603 sin.sin_port = htons(port);
5604 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
5607 fprintf(stderr, "Failed to bind to TCP port %u\n", port);
5614 static int control_getdbseqnum(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5615 int argc, const char **argv)
5618 const char *db_name;
5623 usage("getdbseqnum");
5626 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5630 ret = ctdb_ctrl_get_db_seqnum(mem_ctx, ctdb->ev, ctdb->client,
5631 ctdb->cmd_pnn, TIMEOUT(), db_id,
5634 fprintf(stderr, "Failed to get sequence number for DB %s\n",
5639 printf("0x%"PRIx64"\n", seqnum);
5643 static int control_nodestatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5644 int argc, const char **argv)
5646 const char *nodestring = NULL;
5647 struct ctdb_node_map *nodemap;
5651 usage("nodestatus");
5655 nodestring = argv[0];
5658 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap)) {
5662 nodemap = get_nodemap(ctdb, false);
5663 if (nodemap == NULL) {
5667 if (options.machinereadable) {
5668 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5670 print_nodemap(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5674 for (i=0; i<nodemap->num; i++) {
5675 ret |= nodemap->node[i].flags;
5684 } db_stats_fields[] = {
5685 #define DBSTATISTICS_FIELD(n) { #n, offsetof(struct ctdb_db_statistics, n) }
5686 DBSTATISTICS_FIELD(db_ro_delegations),
5687 DBSTATISTICS_FIELD(db_ro_revokes),
5688 DBSTATISTICS_FIELD(locks.num_calls),
5689 DBSTATISTICS_FIELD(locks.num_current),
5690 DBSTATISTICS_FIELD(locks.num_pending),
5691 DBSTATISTICS_FIELD(locks.num_failed),
5692 DBSTATISTICS_FIELD(db_ro_delegations),
5695 static void print_dbstatistics(const char *db_name,
5696 struct ctdb_db_statistics *s)
5699 const char *prefix = NULL;
5702 printf("DB Statistics %s\n", db_name);
5704 for (i=0; i<ARRAY_SIZE(db_stats_fields); i++) {
5705 if (strchr(db_stats_fields[i].name, '.') != NULL) {
5706 preflen = strcspn(db_stats_fields[i].name, ".") + 1;
5708 strncmp(prefix, db_stats_fields[i].name, preflen) != 0) {
5709 prefix = db_stats_fields[i].name;
5710 printf(" %*.*s\n", preflen-1, preflen-1,
5711 db_stats_fields[i].name);
5716 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
5717 db_stats_fields[i].name+preflen, preflen ? 0 : 4, "",
5718 *(uint32_t *)(db_stats_fields[i].offset+(uint8_t *)s));
5721 printf(" hop_count_buckets:");
5722 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5723 printf(" %d", s->hop_count_bucket[i]);
5727 printf(" lock_buckets:");
5728 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5729 printf(" %d", s->locks.buckets[i]);
5733 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5734 "locks_latency MIN/AVG/MAX",
5735 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
5736 s->locks.latency.max, s->locks.latency.num);
5738 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5739 "vacuum_latency MIN/AVG/MAX",
5740 s->vacuum.latency.min, LATENCY_AVG(s->vacuum.latency),
5741 s->vacuum.latency.max, s->vacuum.latency.num);
5743 printf(" Num Hot Keys: %d\n", s->num_hot_keys);
5744 for (i=0; i<s->num_hot_keys; i++) {
5746 printf(" Count:%d Key:", s->hot_keys[i].count);
5747 for (j=0; j<s->hot_keys[i].key.dsize; j++) {
5748 printf("%02x", s->hot_keys[i].key.dptr[j] & 0xff);
5754 static int control_dbstatistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5755 int argc, const char **argv)
5758 const char *db_name;
5759 struct ctdb_db_statistics *dbstats;
5763 usage("dbstatistics");
5766 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5770 ret = ctdb_ctrl_get_db_statistics(mem_ctx, ctdb->ev, ctdb->client,
5771 ctdb->cmd_pnn, TIMEOUT(), db_id,
5774 fprintf(stderr, "Failed to get statistics for DB %s\n",
5779 print_dbstatistics(db_name, dbstats);
5783 struct disable_takeover_runs_state {
5791 static void disable_takeover_run_handler(uint64_t srvid, TDB_DATA data,
5794 struct disable_takeover_runs_state *state =
5795 (struct disable_takeover_runs_state *)private_data;
5798 if (data.dsize != sizeof(int)) {
5803 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
5804 ret = *(int *)data.dptr;
5806 state->status = ret;
5810 for (i=0; i<state->node_count; i++) {
5811 if (state->pnn_list[i] == ret) {
5812 state->reply[i] = true;
5818 for (i=0; i<state->node_count; i++) {
5819 if (! state->reply[i]) {
5820 state->done = false;
5826 static int disable_takeover_runs(TALLOC_CTX *mem_ctx,
5827 struct ctdb_context *ctdb, uint32_t timeout,
5828 uint32_t *pnn_list, int count)
5830 struct ctdb_disable_message disable = { 0 };
5831 struct disable_takeover_runs_state state;
5834 disable.pnn = ctdb->pnn;
5835 disable.srvid = next_srvid(ctdb);
5836 disable.timeout = timeout;
5838 state.pnn_list = pnn_list;
5839 state.node_count = count;
5842 state.reply = talloc_zero_array(mem_ctx, bool, count);
5843 if (state.reply == NULL) {
5847 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
5849 disable_takeover_run_handler,
5855 for (i=0; i<count; i++) {
5856 ret = ctdb_message_disable_takeover_runs(mem_ctx, ctdb->ev,
5865 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
5867 fprintf(stderr, "Timed out waiting to disable takeover runs\n");
5869 ret = (state.status >= 0 ? 0 : 1);
5873 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
5874 disable.srvid, &state);
5878 static int control_reloadips(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5879 int argc, const char **argv)
5881 const char *nodestring = NULL;
5882 struct ctdb_node_map *nodemap, *nodemap2;
5883 struct ctdb_req_control request;
5884 uint32_t *pnn_list, *pnn_list2;
5885 int ret, count, count2;
5892 nodestring = argv[0];
5895 nodemap = get_nodemap(ctdb, false);
5896 if (nodemap == NULL) {
5900 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap2)) {
5904 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
5905 mem_ctx, &pnn_list);
5907 fprintf(stderr, "Memory allocation error\n");
5911 count2 = list_of_active_nodes(nodemap2, CTDB_UNKNOWN_PNN,
5912 mem_ctx, &pnn_list2);
5914 fprintf(stderr, "Memory allocation error\n");
5918 /* Disable takeover runs on all connected nodes. A reply
5919 * indicating success is needed from each node so all nodes
5920 * will need to be active.
5922 * A check could be added to not allow reloading of IPs when
5923 * there are disconnected nodes. However, this should
5924 * probably be left up to the administrator.
5926 ret = disable_takeover_runs(mem_ctx, ctdb, 2*options.timelimit,
5929 fprintf(stderr, "Failed to disable takeover runs\n");
5933 /* Now tell all the desired nodes to reload their public IPs.
5934 * Keep trying this until it succeeds. This assumes all
5935 * failures are transient, which might not be true...
5937 ctdb_req_control_reload_public_ips(&request);
5938 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
5939 pnn_list2, count2, TIMEOUT(),
5940 &request, NULL, NULL);
5942 fprintf(stderr, "Failed to reload IPs on some nodes.\n");
5945 /* It isn't strictly necessary to wait until takeover runs are
5946 * re-enabled but doing so can't hurt.
5948 ret = disable_takeover_runs(mem_ctx, ctdb, 0, pnn_list, count);
5950 fprintf(stderr, "Failed to enable takeover runs\n");
5954 return ipreallocate(mem_ctx, ctdb);
5957 static int control_ipiface(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5958 int argc, const char **argv)
5960 ctdb_sock_addr addr;
5967 if (! parse_ip(argv[0], NULL, 0, &addr)) {
5968 fprintf(stderr, "Failed to Parse IP %s\n", argv[0]);
5972 iface = ctdb_sys_find_ifname(&addr);
5973 if (iface == NULL) {
5974 fprintf(stderr, "Failed to find interface for IP %s\n",
5984 static const struct ctdb_cmd {
5986 int (*fn)(TALLOC_CTX *, struct ctdb_context *, int, const char **);
5987 bool without_daemon; /* can be run without daemon running ? */
5988 bool remote; /* can be run on remote nodes */
5991 } ctdb_commands[] = {
5992 { "version", control_version, true, false,
5993 "show version of ctdb", NULL },
5994 { "status", control_status, false, true,
5995 "show node status", NULL },
5996 { "uptime", control_uptime, false, true,
5997 "show node uptime", NULL },
5998 { "ping", control_ping, false, true,
5999 "ping all nodes", NULL },
6000 { "runstate", control_runstate, false, true,
6001 "get/check runstate of a node",
6002 "[setup|first_recovery|startup|running]" },
6003 { "getvar", control_getvar, false, true,
6004 "get a tunable variable", "<name>" },
6005 { "setvar", control_setvar, false, true,
6006 "set a tunable variable", "<name> <value>" },
6007 { "listvars", control_listvars, false, true,
6008 "list tunable variables", NULL },
6009 { "statistics", control_statistics, false, true,
6010 "show ctdb statistics", NULL },
6011 { "statisticsreset", control_statistics_reset, false, true,
6012 "reset ctdb statistics", NULL },
6013 { "stats", control_stats, false, true,
6014 "show rolling statistics", "[count]" },
6015 { "ip", control_ip, false, true,
6016 "show public ips", "[all]" },
6017 { "ipinfo", control_ipinfo, false, true,
6018 "show public ip details", "<ip>" },
6019 { "ifaces", control_ifaces, false, true,
6020 "show interfaces", NULL },
6021 { "setifacelink", control_setifacelink, false, true,
6022 "set interface link status", "<iface> up|down" },
6023 { "process-exists", control_process_exists, false, true,
6024 "check if a process exists on a node", "<pid>" },
6025 { "getdbmap", control_getdbmap, false, true,
6026 "show attached databases", NULL },
6027 { "getdbstatus", control_getdbstatus, false, true,
6028 "show database status", "<dbname|dbid>" },
6029 { "catdb", control_catdb, false, false,
6030 "dump cluster-wide ctdb database", "<dbname|dbid>" },
6031 { "cattdb", control_cattdb, false, false,
6032 "dump local ctdb database", "<dbname|dbid>" },
6033 { "getmonmode", control_getmonmode, false, true,
6034 "show monitoring mode", NULL },
6035 { "getcapabilities", control_getcapabilities, false, true,
6036 "show node capabilities", NULL },
6037 { "pnn", control_pnn, false, false,
6038 "show the pnn of the currnet node", NULL },
6039 { "lvs", control_lvs, false, false,
6040 "show lvs configuration", "master|list|status" },
6041 { "disablemonitor", control_disable_monitor, false, true,
6042 "disable monitoring", NULL },
6043 { "enablemonitor", control_enable_monitor, false, true,
6044 "enable monitoring", NULL },
6045 { "setdebug", control_setdebug, false, true,
6046 "set debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
6047 { "getdebug", control_getdebug, false, true,
6048 "get debug level", NULL },
6049 { "attach", control_attach, false, false,
6050 "attach a database", "<dbname> [persistent]" },
6051 { "detach", control_detach, false, false,
6052 "detach database(s)", "<dbname|dbid> ..." },
6053 { "dumpmemory", control_dumpmemory, false, true,
6054 "dump ctdbd memory map", NULL },
6055 { "rddumpmemory", control_rddumpmemory, false, true,
6056 "dump recoverd memory map", NULL },
6057 { "getpid", control_getpid, false, true,
6058 "get ctdbd process ID", NULL },
6059 { "disable", control_disable, false, true,
6060 "disable a node", NULL },
6061 { "enable", control_enable, false, true,
6062 "enable a node", NULL },
6063 { "stop", control_stop, false, true,
6064 "stop a node", NULL },
6065 { "continue", control_continue, false, true,
6066 "continue a stopped node", NULL },
6067 { "ban", control_ban, false, true,
6068 "ban a node", "<bantime>"},
6069 { "unban", control_unban, false, true,
6070 "unban a node", NULL },
6071 { "shutdown", control_shutdown, false, true,
6072 "shutdown ctdb daemon", NULL },
6073 { "recover", control_recover, false, true,
6074 "force recovery", NULL },
6075 { "sync", control_ipreallocate, false, true,
6076 "run ip reallocation (deprecated)", NULL },
6077 { "ipreallocate", control_ipreallocate, false, true,
6078 "run ip reallocation", NULL },
6079 { "isnotrecmaster", control_isnotrecmaster, false, false,
6080 "check if local node is the recmaster", NULL },
6081 { "gratarp", control_gratarp, false, true,
6082 "send a gratuitous arp", "<ip> <interface>" },
6083 { "tickle", control_tickle, true, false,
6084 "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6085 { "gettickles", control_gettickles, false, true,
6086 "get the list of tickles", "<ip> [<port>]" },
6087 { "addtickle", control_addtickle, false, true,
6088 "add a tickle", "<ip>:<port> <ip>:<port>" },
6089 { "deltickle", control_deltickle, false, true,
6090 "delete a tickle", "<ip>:<port> <ip>:<port>" },
6091 { "check_srvids", control_check_srvids, false, true,
6092 "check if srvid is registered", "<id> [<id> ...]" },
6093 { "listnodes", control_listnodes, true, true,
6094 "list nodes in the cluster", NULL },
6095 { "reloadnodes", control_reloadnodes, false, false,
6096 "reload the nodes file all nodes", NULL },
6097 { "moveip", control_moveip, false, false,
6098 "move an ip address to another node", "<ip> <node>" },
6099 { "addip", control_addip, false, true,
6100 "add an ip address to a node", "<ip/mask> <iface>" },
6101 { "delip", control_delip, false, true,
6102 "delete an ip address from a node", "<ip>" },
6103 { "backupdb", control_backupdb, false, false,
6104 "backup a database into a file", "<dbname|dbid> <file>" },
6105 { "restoredb", control_restoredb, false, false,
6106 "restore a database from a file", "<file> [dbname]" },
6107 { "dumpdbbackup", control_dumpdbbackup, true, false,
6108 "dump database from a backup file", "<file>" },
6109 { "wipedb", control_wipedb, false, false,
6110 "wipe the contents of a database.", "<dbname|dbid>"},
6111 { "recmaster", control_recmaster, false, true,
6112 "show the pnn for the recovery master", NULL },
6113 { "event", control_event, true, false,
6114 "event and event script commands", NULL },
6115 { "scriptstatus", control_scriptstatus, true, false,
6116 "show event script status",
6117 "[init|setup|startup|monitor|takeip|releaseip|ipreallocated]" },
6118 { "natgw", control_natgw, false, false,
6119 "show natgw configuration", "master|list|status" },
6120 { "natgwlist", control_natgwlist, false, false,
6121 "show the nodes belonging to this natgw configuration", NULL },
6122 { "getreclock", control_getreclock, false, true,
6123 "get recovery lock file", NULL },
6124 { "setlmasterrole", control_setlmasterrole, false, true,
6125 "set LMASTER role", "on|off" },
6126 { "setrecmasterrole", control_setrecmasterrole, false, true,
6127 "set RECMASTER role", "on|off"},
6128 { "setdbreadonly", control_setdbreadonly, false, true,
6129 "enable readonly records", "<dbname|dbid>" },
6130 { "setdbsticky", control_setdbsticky, false, true,
6131 "enable sticky records", "<dbname|dbid>"},
6132 { "pfetch", control_pfetch, false, false,
6133 "fetch record from persistent database", "<dbname|dbid> <key> [<file>]" },
6134 { "pstore", control_pstore, false, false,
6135 "write record to persistent database", "<dbname|dbid> <key> <value>" },
6136 { "pdelete", control_pdelete, false, false,
6137 "delete record from persistent database", "<dbname|dbid> <key>" },
6138 { "ptrans", control_ptrans, false, false,
6139 "update a persistent database (from file or stdin)", "<dbname|dbid> [<file>]" },
6140 { "tfetch", control_tfetch, false, true,
6141 "fetch a record", "<tdb-file> <key> [<file>]" },
6142 { "tstore", control_tstore, false, true,
6143 "store a record", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6144 { "readkey", control_readkey, false, false,
6145 "read value of a database key", "<dbname|dbid> <key> [readonly]" },
6146 { "writekey", control_writekey, false, false,
6147 "write value for a database key", "<dbname|dbid> <key> <value>" },
6148 { "deletekey", control_deletekey, false, false,
6149 "delete a database key", "<dbname|dbid> <key>" },
6150 { "checktcpport", control_checktcpport, true, false,
6151 "check if a service is bound to a specific tcp port or not", "<port>" },
6152 { "getdbseqnum", control_getdbseqnum, false, false,
6153 "get database sequence number", "<dbname|dbid>" },
6154 { "nodestatus", control_nodestatus, false, true,
6155 "show and return node status", "[all|<pnn-list>]" },
6156 { "dbstatistics", control_dbstatistics, false, true,
6157 "show database statistics", "<dbname|dbid>" },
6158 { "reloadips", control_reloadips, false, false,
6159 "reload the public addresses file", "[all|<pnn-list>]" },
6160 { "ipiface", control_ipiface, true, false,
6161 "Find the interface an ip address is hosted on", "<ip>" },
6164 static const struct ctdb_cmd *match_command(const char *command)
6166 const struct ctdb_cmd *cmd;
6169 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6170 cmd = &ctdb_commands[i];
6171 if (strlen(command) == strlen(cmd->name) &&
6172 strncmp(command, cmd->name, strlen(command)) == 0) {
6182 * Show usage message
6184 static void usage_full(void)
6188 poptPrintHelp(pc, stdout, 0);
6189 printf("\nCommands:\n");
6190 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6191 printf(" %-15s %-27s %s\n",
6192 ctdb_commands[i].name,
6193 ctdb_commands[i].args ? ctdb_commands[i].args : "",
6194 ctdb_commands[i].msg);
6198 static void usage(const char *command)
6200 const struct ctdb_cmd *cmd;
6202 if (command == NULL) {
6207 cmd = match_command(command);
6211 poptPrintUsage(pc, stdout, 0);
6212 printf("\nCommands:\n");
6213 printf(" %-15s %-27s %s\n",
6214 cmd->name, cmd->args ? cmd->args : "", cmd->msg);
6220 struct poptOption cmdline_options[] = {
6222 { "socket", 's', POPT_ARG_STRING, &options.socket, 0,
6223 "CTDB socket path", "filename" },
6224 { "debug", 'd', POPT_ARG_STRING, &options.debuglevelstr, 0,
6226 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0,
6227 "timelimit (in seconds)" },
6228 { "node", 'n', POPT_ARG_INT, &options.pnn, 0,
6229 "node specification - integer" },
6230 { NULL, 'Y', POPT_ARG_NONE, &options.machinereadable, 0,
6231 "enable machine readable output", NULL },
6232 { "separator", 'x', POPT_ARG_STRING, &options.sep, 0,
6233 "specify separator for machine readable output", "CHAR" },
6234 { NULL, 'X', POPT_ARG_NONE, &options.machineparsable, 0,
6235 "enable machine parsable output with separator |", NULL },
6236 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0,
6237 "enable verbose output", NULL },
6238 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0,
6239 "die if runtime exceeds this limit (in seconds)" },
6243 static int process_command(const struct ctdb_cmd *cmd, int argc,
6246 TALLOC_CTX *tmp_ctx;
6247 struct ctdb_context *ctdb;
6250 uint64_t srvid_offset;
6252 tmp_ctx = talloc_new(NULL);
6253 if (tmp_ctx == NULL) {
6254 fprintf(stderr, "Memory allocation error\n");
6258 if (cmd->without_daemon) {
6259 if (options.pnn != -1) {
6261 "Cannot specify node for command %s\n",
6266 ret = cmd->fn(tmp_ctx, NULL, argc-1, argv+1);
6267 talloc_free(tmp_ctx);
6271 ctdb = talloc_zero(tmp_ctx, struct ctdb_context);
6273 fprintf(stderr, "Memory allocation error\n");
6277 ctdb->ev = tevent_context_init(ctdb);
6278 if (ctdb->ev == NULL) {
6279 fprintf(stderr, "Failed to initialize tevent\n");
6283 ret = ctdb_client_init(ctdb, ctdb->ev, options.socket, &ctdb->client);
6285 fprintf(stderr, "Failed to connect to CTDB daemon (%s)\n",
6288 if (!find_node_xpnn(ctdb, NULL)) {
6289 fprintf(stderr, "Is this node part of CTDB cluster?\n");
6294 ctdb->pnn = ctdb_client_pnn(ctdb->client);
6295 srvid_offset = getpid() & 0xFFFF;
6296 ctdb->srvid = SRVID_CTDB_TOOL | (srvid_offset << 16);
6298 if (options.pnn != -1) {
6299 status = verify_pnn(ctdb, options.pnn);
6304 ctdb->cmd_pnn = options.pnn;
6306 ctdb->cmd_pnn = ctdb->pnn;
6309 if (! cmd->remote && ctdb->pnn != ctdb->cmd_pnn) {
6310 fprintf(stderr, "Node cannot be specified for command %s\n",
6315 ret = cmd->fn(tmp_ctx, ctdb, argc-1, argv+1);
6316 talloc_free(tmp_ctx);
6320 talloc_free(tmp_ctx);
6324 static void signal_handler(int sig)
6326 fprintf(stderr, "Maximum runtime exceeded - exiting\n");
6329 static void alarm_handler(int sig)
6331 /* Kill any child processes */
6332 signal(SIGTERM, signal_handler);
6338 int main(int argc, const char *argv[])
6341 const char **extra_argv;
6343 const struct ctdb_cmd *cmd;
6344 const char *ctdb_socket;
6350 /* Set default options */
6351 options.socket = CTDB_SOCKET;
6352 options.debuglevelstr = NULL;
6353 options.timelimit = 10;
6355 options.maxruntime = 0;
6358 ctdb_socket = getenv("CTDB_SOCKET");
6359 if (ctdb_socket != NULL) {
6360 options.socket = ctdb_socket;
6363 pc = poptGetContext(argv[0], argc, argv, cmdline_options,
6364 POPT_CONTEXT_KEEP_FIRST);
6365 while ((opt = poptGetNextOpt(pc)) != -1) {
6366 fprintf(stderr, "Invalid option %s: %s\n",
6367 poptBadOption(pc, 0), poptStrerror(opt));
6371 if (options.maxruntime == 0) {
6372 const char *ctdb_timeout;
6374 ctdb_timeout = getenv("CTDB_TIMEOUT");
6375 if (ctdb_timeout != NULL) {
6376 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6378 options.maxruntime = 120;
6381 if (options.maxruntime <= 120) {
6382 /* default timeout is 120 seconds */
6383 options.maxruntime = 120;
6386 if (options.machineparsable) {
6387 options.machinereadable = 1;
6390 /* setup the remaining options for the commands */
6392 extra_argv = poptGetArgs(pc);
6395 while (extra_argv[extra_argc]) extra_argc++;
6398 if (extra_argc < 1) {
6402 cmd = match_command(extra_argv[0]);
6404 fprintf(stderr, "Unknown command '%s'\n", extra_argv[0]);
6408 /* Enable logging */
6409 setup_logging("ctdb", DEBUG_STDERR);
6410 if (debug_level_parse(options.debuglevelstr, &loglevel)) {
6411 DEBUGLEVEL = loglevel;
6413 DEBUGLEVEL = DEBUG_ERR;
6416 signal(SIGALRM, alarm_handler);
6417 alarm(options.maxruntime);
6419 ret = process_command(cmd, extra_argc, extra_argv);
6424 (void)poptFreeContext(pc);