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 static int control_eventscript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4038 int argc, const char **argv)
4043 usage("eventscript");
4046 if (strcmp(argv[0], "monitor") != 0) {
4047 fprintf(stderr, "Only monitor event can be run\n");
4051 ret = ctdb_ctrl_run_eventscripts(mem_ctx, ctdb->ev, ctdb->client,
4052 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4054 fprintf(stderr, "Failed to run monitor event on node %u\n",
4062 #define DB_VERSION 3
4063 #define MAX_DB_NAME 64
4064 #define MAX_REC_BUFFER_SIZE (100*1000)
4067 unsigned long version;
4069 unsigned long flags;
4072 char name[MAX_DB_NAME];
4075 struct backup_state {
4076 TALLOC_CTX *mem_ctx;
4077 struct ctdb_rec_buffer *recbuf;
4080 unsigned int nbuf, nrec;
4083 static int backup_handler(uint32_t reqid, struct ctdb_ltdb_header *header,
4084 TDB_DATA key, TDB_DATA data, void *private_data)
4086 struct backup_state *state = (struct backup_state *)private_data;
4090 if (state->recbuf == NULL) {
4091 state->recbuf = ctdb_rec_buffer_init(state->mem_ctx,
4093 if (state->recbuf == NULL) {
4098 ret = ctdb_rec_buffer_add(state->recbuf, state->recbuf, reqid,
4104 len = ctdb_rec_buffer_len(state->recbuf);
4105 if (len < MAX_REC_BUFFER_SIZE) {
4109 ret = ctdb_rec_buffer_write(state->recbuf, state->fd);
4111 fprintf(stderr, "Failed to write records to backup file\n");
4116 state->nrec += state->recbuf->count;
4117 TALLOC_FREE(state->recbuf);
4122 static int control_backupdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4123 int argc, const char **argv)
4125 const char *db_name;
4126 struct ctdb_db_context *db;
4129 struct backup_state state;
4130 struct db_header db_hdr;
4137 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4141 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4144 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4148 fd = open(argv[1], O_RDWR|O_CREAT, 0600);
4151 fprintf(stderr, "Failed to open file %s for writing\n",
4156 /* Write empty header first */
4157 ZERO_STRUCT(db_hdr);
4158 ret = write(fd, &db_hdr, sizeof(struct db_header));
4162 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4166 state.mem_ctx = mem_ctx;
4167 state.recbuf = NULL;
4172 ret = ctdb_db_traverse(db, true, false, backup_handler, &state);
4174 fprintf(stderr, "Failed to collect records from DB %s\n",
4180 if (state.recbuf != NULL) {
4181 ret = ctdb_rec_buffer_write(state.recbuf, state.fd);
4184 "Failed to write records to backup file\n");
4190 state.nrec += state.recbuf->count;
4191 TALLOC_FREE(state.recbuf);
4194 db_hdr.version = DB_VERSION;
4195 db_hdr.timestamp = time(NULL);
4196 db_hdr.flags = db_flags;
4197 db_hdr.nbuf = state.nbuf;
4198 db_hdr.nrec = state.nrec;
4199 strncpy(db_hdr.name, db_name, MAX_DB_NAME-1);
4201 lseek(fd, 0, SEEK_SET);
4202 ret = write(fd, &db_hdr, sizeof(struct db_header));
4206 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4211 printf("Database backed up to %s\n", argv[1]);
4215 static int control_restoredb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4216 int argc, const char **argv)
4218 const char *db_name = NULL;
4219 struct ctdb_db_context *db;
4220 struct db_header db_hdr;
4221 struct ctdb_node_map *nodemap;
4222 struct ctdb_req_control request;
4223 struct ctdb_reply_control **reply;
4224 struct ctdb_transdb wipedb;
4225 struct ctdb_pulldb_ext pulldb;
4226 struct ctdb_rec_buffer *recbuf;
4227 uint32_t generation;
4233 if (argc < 1 || argc > 2) {
4237 fd = open(argv[0], O_RDONLY, 0600);
4240 fprintf(stderr, "Failed to open file %s for reading\n",
4249 ret = read(fd, &db_hdr, sizeof(struct db_header));
4253 fprintf(stderr, "Failed to read db header from file %s\n",
4257 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4259 if (db_hdr.version != DB_VERSION) {
4261 "Wrong version of backup file, expected %u, got %lu\n",
4262 DB_VERSION, db_hdr.version);
4267 if (db_name == NULL) {
4268 db_name = db_hdr.name;
4271 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4272 localtime(&db_hdr.timestamp));
4273 printf("Restoring database %s from backup @ %s\n", db_name, timebuf);
4275 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4278 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4283 nodemap = get_nodemap(ctdb, false);
4284 if (nodemap == NULL) {
4285 fprintf(stderr, "Failed to get nodemap\n");
4290 ret = get_generation(mem_ctx, ctdb, &generation);
4292 fprintf(stderr, "Failed to get current generation\n");
4297 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4304 wipedb.db_id = ctdb_db_id(db);
4305 wipedb.tid = generation;
4307 ctdb_req_control_db_freeze(&request, wipedb.db_id);
4308 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4309 ctdb->client, pnn_list, count,
4310 TIMEOUT(), &request, NULL, NULL);
4316 ctdb_req_control_db_transaction_start(&request, &wipedb);
4317 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4318 pnn_list, count, TIMEOUT(),
4319 &request, NULL, NULL);
4324 ctdb_req_control_wipe_database(&request, &wipedb);
4325 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4326 pnn_list, count, TIMEOUT(),
4327 &request, NULL, NULL);
4332 pulldb.db_id = ctdb_db_id(db);
4334 pulldb.srvid = SRVID_CTDB_PUSHDB;
4336 ctdb_req_control_db_push_start(&request, &pulldb);
4337 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4338 pnn_list, count, TIMEOUT(),
4339 &request, NULL, NULL);
4344 for (i=0; i<db_hdr.nbuf; i++) {
4345 struct ctdb_req_message message;
4348 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4353 data.dsize = ctdb_rec_buffer_len(recbuf);
4354 data.dptr = talloc_size(mem_ctx, data.dsize);
4355 if (data.dptr == NULL) {
4359 ctdb_rec_buffer_push(recbuf, data.dptr);
4361 message.srvid = pulldb.srvid;
4362 message.data.data = data;
4364 ret = ctdb_client_message_multi(mem_ctx, ctdb->ev,
4372 talloc_free(recbuf);
4373 talloc_free(data.dptr);
4376 ctdb_req_control_db_push_confirm(&request, pulldb.db_id);
4377 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4378 pnn_list, count, TIMEOUT(),
4379 &request, NULL, &reply);
4384 for (i=0; i<count; i++) {
4385 uint32_t num_records;
4387 ret = ctdb_reply_control_db_push_confirm(reply[i],
4390 fprintf(stderr, "Invalid response from node %u\n",
4395 if (num_records != db_hdr.nrec) {
4396 fprintf(stderr, "Node %u received %u of %lu records\n",
4397 pnn_list[i], num_records, db_hdr.nrec);
4402 ctdb_req_control_db_set_healthy(&request, wipedb.db_id);
4403 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4404 pnn_list, count, TIMEOUT(),
4405 &request, NULL, NULL);
4410 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4411 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4412 pnn_list, count, TIMEOUT(),
4413 &request, NULL, NULL);
4418 ctdb_req_control_db_thaw(&request, wipedb.db_id);
4419 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4420 ctdb->client, pnn_list, count,
4421 TIMEOUT(), &request, NULL, NULL);
4426 printf("Database %s restored\n", db_name);
4432 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4433 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4437 struct dumpdbbackup_state {
4438 ctdb_rec_parser_func_t parser;
4439 struct dump_record_state sub_state;
4442 static int dumpdbbackup_handler(uint32_t reqid,
4443 struct ctdb_ltdb_header *header,
4444 TDB_DATA key, TDB_DATA data,
4447 struct dumpdbbackup_state *state =
4448 (struct dumpdbbackup_state *)private_data;
4449 struct ctdb_ltdb_header hdr;
4452 ret = ctdb_ltdb_header_extract(&data, &hdr);
4457 return state->parser(reqid, &hdr, key, data, &state->sub_state);
4460 static int control_dumpdbbackup(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4461 int argc, const char **argv)
4463 struct db_header db_hdr;
4465 struct dumpdbbackup_state state;
4469 usage("dumpbackup");
4472 fd = open(argv[0], O_RDONLY, 0600);
4475 fprintf(stderr, "Failed to open file %s for reading\n",
4480 ret = read(fd, &db_hdr, sizeof(struct db_header));
4484 fprintf(stderr, "Failed to read db header from file %s\n",
4488 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4490 if (db_hdr.version != DB_VERSION) {
4492 "Wrong version of backup file, expected %u, got %lu\n",
4493 DB_VERSION, db_hdr.version);
4498 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4499 localtime(&db_hdr.timestamp));
4500 printf("Dumping database %s from backup @ %s\n",
4501 db_hdr.name, timebuf);
4503 state.parser = dump_record;
4504 state.sub_state.count = 0;
4506 for (i=0; i<db_hdr.nbuf; i++) {
4507 struct ctdb_rec_buffer *recbuf;
4509 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4511 fprintf(stderr, "Failed to read records\n");
4516 ret = ctdb_rec_buffer_traverse(recbuf, dumpdbbackup_handler,
4519 fprintf(stderr, "Failed to dump records\n");
4526 printf("Dumped %u record(s)\n", state.sub_state.count);
4530 static int control_wipedb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4531 int argc, const char **argv)
4533 const char *db_name;
4534 struct ctdb_db_context *db;
4537 struct ctdb_node_map *nodemap;
4538 struct ctdb_req_control request;
4539 struct ctdb_transdb wipedb;
4540 uint32_t generation;
4548 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4552 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4555 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4559 nodemap = get_nodemap(ctdb, false);
4560 if (nodemap == NULL) {
4561 fprintf(stderr, "Failed to get nodemap\n");
4565 ret = get_generation(mem_ctx, ctdb, &generation);
4567 fprintf(stderr, "Failed to get current generation\n");
4571 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4577 ctdb_req_control_db_freeze(&request, db_id);
4578 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4579 ctdb->client, pnn_list, count,
4580 TIMEOUT(), &request, NULL, NULL);
4585 wipedb.db_id = db_id;
4586 wipedb.tid = generation;
4588 ctdb_req_control_db_transaction_start(&request, &wipedb);
4589 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4590 pnn_list, count, TIMEOUT(),
4591 &request, NULL, NULL);
4596 ctdb_req_control_wipe_database(&request, &wipedb);
4597 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4598 pnn_list, count, TIMEOUT(),
4599 &request, NULL, NULL);
4604 ctdb_req_control_db_set_healthy(&request, db_id);
4605 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4606 pnn_list, count, TIMEOUT(),
4607 &request, NULL, NULL);
4612 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4613 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4614 pnn_list, count, TIMEOUT(),
4615 &request, NULL, NULL);
4620 ctdb_req_control_db_thaw(&request, db_id);
4621 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4622 ctdb->client, pnn_list, count,
4623 TIMEOUT(), &request, NULL, NULL);
4628 printf("Database %s wiped\n", db_name);
4633 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4634 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4638 static int control_recmaster(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4639 int argc, const char **argv)
4644 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
4645 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
4650 printf("%u\n", recmaster);
4654 static void print_scriptstatus_one(struct ctdb_script_list *slist,
4655 const char *event_str)
4660 if (slist == NULL) {
4661 if (! options.machinereadable) {
4662 printf("%s cycle never run\n", event_str);
4667 for (i=0; i<slist->num_scripts; i++) {
4668 if (slist->script[i].status != -ENOEXEC) {
4673 if (! options.machinereadable) {
4674 printf("%d scripts were executed last %s cycle\n",
4675 num_run, event_str);
4678 for (i=0; i<slist->num_scripts; i++) {
4679 const char *status = NULL;
4681 switch (slist->script[i].status) {
4683 status = "TIMEDOUT";
4686 status = "DISABLED";
4692 if (slist->script[i].status > 0) {
4698 if (options.machinereadable) {
4699 printf("%s%s%s%s%s%i%s%s%s%lu.%06lu%s%lu.%06lu%s%s%s\n",
4701 event_str, options.sep,
4702 slist->script[i].name, options.sep,
4703 slist->script[i].status, options.sep,
4704 status, options.sep,
4705 (unsigned long)slist->script[i].start.tv_sec,
4706 slist->script[i].start.tv_usec, options.sep,
4707 (unsigned long)slist->script[i].finished.tv_sec,
4708 slist->script[i].finished.tv_usec, options.sep,
4709 slist->script[i].output, options.sep);
4714 printf("%-20s Status:%s ",
4715 slist->script[i].name, status);
4717 /* Some other error, eg from stat. */
4718 printf("%-20s Status:CANNOT RUN (%s)",
4719 slist->script[i].name,
4720 strerror(-slist->script[i].status));
4723 if (slist->script[i].status >= 0) {
4724 printf("Duration:%.3lf ",
4725 timeval_delta(&slist->script[i].finished,
4726 &slist->script[i].start));
4728 if (slist->script[i].status != -ENOEXEC) {
4729 printf("%s", ctime(&slist->script[i].start.tv_sec));
4730 if (slist->script[i].status != 0) {
4731 printf(" OUTPUT:%s\n",
4732 slist->script[i].output);
4740 static void print_scriptstatus(struct ctdb_script_list **slist,
4741 int count, const char **event_str)
4745 if (options.machinereadable) {
4746 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
4748 "Type", options.sep,
4749 "Name", options.sep,
4750 "Code", options.sep,
4751 "Status", options.sep,
4752 "Start", options.sep,
4754 "Error Output", options.sep);
4757 for (i=0; i<count; i++) {
4758 print_scriptstatus_one(slist[i], event_str[i]);
4762 static int control_event(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4763 int argc, const char **argv)
4765 char *t, *event_helper = NULL;
4766 char *eventd_socket = NULL;
4767 const char **new_argv;
4770 t = getenv("CTDB_EVENT_HELPER");
4772 event_helper = talloc_strdup(mem_ctx, t);
4774 event_helper = talloc_asprintf(mem_ctx, "%s/ctdb_event",
4775 CTDB_HELPER_BINDIR);
4778 if (event_helper == NULL) {
4779 fprintf(stderr, "Unable to set event daemon helper\n");
4783 t = getenv("CTDB_SOCKET");
4785 eventd_socket = talloc_asprintf(mem_ctx, "%s/eventd.sock",
4788 eventd_socket = talloc_asprintf(mem_ctx, "%s/eventd.sock",
4792 if (eventd_socket == NULL) {
4793 fprintf(stderr, "Unable to set event daemon socket\n");
4797 new_argv = talloc_array(mem_ctx, const char *, argc + 1);
4798 if (new_argv == NULL) {
4799 fprintf(stderr, "Memory allocation error\n");
4803 new_argv[0] = eventd_socket;
4804 for (i=0; i<argc; i++) {
4805 new_argv[i+1] = argv[i];
4808 return run_helper(mem_ctx, "event daemon helper", event_helper,
4812 static int control_scriptstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4813 int argc, const char **argv)
4815 struct ctdb_script_list **slist;
4816 const char *event_str;
4817 enum ctdb_event event;
4818 const char *all_events[] = {
4819 "init", "setup", "startup", "monitor",
4820 "takeip", "releaseip", "updateip", "ipreallocated" };
4823 int count, start, end, num;
4826 usage("scriptstatus");
4830 event_str = "monitor";
4832 event_str = argv[0];
4838 for (i=0; i<ARRAY_SIZE(all_events); i++) {
4839 if (strcmp(event_str, all_events[i]) == 0) {
4848 if (strcmp(event_str, "all") == 0) {
4850 count = ARRAY_SIZE(all_events);
4856 fprintf(stderr, "Unknown event name %s\n", argv[0]);
4857 usage("scriptstatus");
4860 slist = talloc_array(mem_ctx, struct ctdb_script_list *, count);
4861 if (slist == NULL) {
4862 fprintf(stderr, "Memory allocation error\n");
4867 for (i=start; i<end; i++) {
4868 event = ctdb_event_from_string(all_events[i]);
4870 ret = ctdb_ctrl_get_event_script_status(mem_ctx, ctdb->ev,
4877 "failed to get script status for %s event\n",
4882 if (slist[num] == NULL) {
4887 /* The ETIME status is ignored for certain events.
4888 * In that case the status is 0, but endtime is not set.
4890 for (j=0; j<slist[num]->num_scripts; j++) {
4891 if (slist[num]->script[j].status == 0 &&
4892 timeval_is_zero(&slist[num]->script[j].finished)) {
4893 slist[num]->script[j].status = -ETIME;
4900 print_scriptstatus(slist, count, &all_events[start]);
4904 static int control_enablescript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4905 int argc, const char **argv)
4910 usage("enablescript");
4913 ret = ctdb_ctrl_enable_script(mem_ctx, ctdb->ev, ctdb->client,
4914 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4916 fprintf(stderr, "Failed to enable script %s\n", argv[0]);
4923 static int control_disablescript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4924 int argc, const char **argv)
4929 usage("disablescript");
4932 ret = ctdb_ctrl_disable_script(mem_ctx, ctdb->ev, ctdb->client,
4933 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4935 fprintf(stderr, "Failed to disable script %s\n", argv[0]);
4942 static int control_natgw(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4943 int argc, const char **argv)
4945 char *t, *natgw_helper = NULL;
4951 t = getenv("CTDB_NATGW_HELPER");
4953 natgw_helper = talloc_strdup(mem_ctx, t);
4955 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4956 CTDB_HELPER_BINDIR);
4959 if (natgw_helper == NULL) {
4960 fprintf(stderr, "Unable to set NAT gateway helper\n");
4964 return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
4968 static int control_natgwlist(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4969 int argc, const char **argv)
4971 char *t, *natgw_helper = NULL;
4972 const char *cmd_argv[] = { "natgwlist", NULL };
4978 t = getenv("CTDB_NATGW_HELPER");
4980 natgw_helper = talloc_strdup(mem_ctx, t);
4982 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4983 CTDB_HELPER_BINDIR);
4986 if (natgw_helper == NULL) {
4987 fprintf(stderr, "Unable to set NAT gateway helper\n");
4991 return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
4996 * Find the PNN of the current node
4997 * discover the pnn by loading the nodes file and try to bind
4998 * to all addresses one at a time until the ip address is found.
5000 static bool find_node_xpnn(TALLOC_CTX *mem_ctx, uint32_t *pnn)
5002 struct ctdb_node_map *nodemap;
5005 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
5006 if (nodemap == NULL) {
5010 for (i=0; i<nodemap->num; i++) {
5011 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
5014 if (ctdb_sys_have_ip(&nodemap->node[i].addr)) {
5016 *pnn = nodemap->node[i].pnn;
5018 talloc_free(nodemap);
5023 fprintf(stderr, "Failed to detect PNN of the current node.\n");
5024 talloc_free(nodemap);
5028 static int control_getreclock(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5029 int argc, const char **argv)
5031 const char *reclock;
5035 usage("getreclock");
5038 ret = ctdb_ctrl_get_reclock_file(mem_ctx, ctdb->ev, ctdb->client,
5039 ctdb->cmd_pnn, TIMEOUT(), &reclock);
5044 if (reclock != NULL) {
5045 printf("%s\n", reclock);
5051 static int control_setlmasterrole(TALLOC_CTX *mem_ctx,
5052 struct ctdb_context *ctdb,
5053 int argc, const char **argv)
5055 uint32_t lmasterrole = 0;
5059 usage("setlmasterrole");
5062 if (strcmp(argv[0], "on") == 0) {
5064 } else if (strcmp(argv[0], "off") == 0) {
5067 usage("setlmasterrole");
5070 ret = ctdb_ctrl_set_lmasterrole(mem_ctx, ctdb->ev, ctdb->client,
5071 ctdb->cmd_pnn, TIMEOUT(), lmasterrole);
5079 static int control_setrecmasterrole(TALLOC_CTX *mem_ctx,
5080 struct ctdb_context *ctdb,
5081 int argc, const char **argv)
5083 uint32_t recmasterrole = 0;
5087 usage("setrecmasterrole");
5090 if (strcmp(argv[0], "on") == 0) {
5092 } else if (strcmp(argv[0], "off") == 0) {
5095 usage("setrecmasterrole");
5098 ret = ctdb_ctrl_set_recmasterrole(mem_ctx, ctdb->ev, ctdb->client,
5099 ctdb->cmd_pnn, TIMEOUT(),
5108 static int control_setdbreadonly(TALLOC_CTX *mem_ctx,
5109 struct ctdb_context *ctdb,
5110 int argc, const char **argv)
5117 usage("setdbreadonly");
5120 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
5124 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5125 fprintf(stderr, "Cannot set READONLY on persistent DB\n");
5129 ret = ctdb_ctrl_set_db_readonly(mem_ctx, ctdb->ev, ctdb->client,
5130 ctdb->cmd_pnn, TIMEOUT(), db_id);
5138 static int control_setdbsticky(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5139 int argc, const char **argv)
5146 usage("setdbsticky");
5149 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
5153 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5154 fprintf(stderr, "Cannot set STICKY on persistent DB\n");
5158 ret = ctdb_ctrl_set_db_sticky(mem_ctx, ctdb->ev, ctdb->client,
5159 ctdb->cmd_pnn, TIMEOUT(), db_id);
5167 static int control_pfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5168 int argc, const char **argv)
5170 const char *db_name;
5171 struct ctdb_db_context *db;
5172 struct ctdb_transaction_handle *h;
5177 if (argc < 2 || argc > 3) {
5181 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5185 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5186 fprintf(stderr, "DB %s is not a persistent database\n",
5191 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5194 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5198 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5200 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5204 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5205 TIMEOUT(), db, true, &h);
5207 fprintf(stderr, "Failed to start transaction on db %s\n",
5212 ret = ctdb_transaction_fetch_record(h, key, mem_ctx, &data);
5214 fprintf(stderr, "Failed to read record for key %s\n",
5216 ctdb_transaction_cancel(h);
5220 printf("%.*s\n", (int)data.dsize, data.dptr);
5222 ctdb_transaction_cancel(h);
5226 static int control_pstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5227 int argc, const char **argv)
5229 const char *db_name;
5230 struct ctdb_db_context *db;
5231 struct ctdb_transaction_handle *h;
5240 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5244 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5245 fprintf(stderr, "DB %s is not a persistent database\n",
5250 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5253 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5257 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5259 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5263 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5265 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5269 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5270 TIMEOUT(), db, false, &h);
5272 fprintf(stderr, "Failed to start transaction on db %s\n",
5277 ret = ctdb_transaction_store_record(h, key, data);
5279 fprintf(stderr, "Failed to store record for key %s\n",
5281 ctdb_transaction_cancel(h);
5285 ret = ctdb_transaction_commit(h);
5287 fprintf(stderr, "Failed to commit transaction on db %s\n",
5289 ctdb_transaction_cancel(h);
5296 static int control_pdelete(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5297 int argc, const char **argv)
5299 const char *db_name;
5300 struct ctdb_db_context *db;
5301 struct ctdb_transaction_handle *h;
5310 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5314 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5315 fprintf(stderr, "DB %s is not a persistent database\n",
5320 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5323 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5327 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5329 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5333 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5334 TIMEOUT(), db, false, &h);
5336 fprintf(stderr, "Failed to start transaction on db %s\n",
5341 ret = ctdb_transaction_delete_record(h, key);
5343 fprintf(stderr, "Failed to delete record for key %s\n",
5345 ctdb_transaction_cancel(h);
5349 ret = ctdb_transaction_commit(h);
5351 fprintf(stderr, "Failed to commit transaction on db %s\n",
5353 ctdb_transaction_cancel(h);
5360 static int ptrans_parse_string(TALLOC_CTX *mem_ctx, const char **ptr, TDB_DATA *data)
5368 /* Skip whitespace */
5369 n = strspn(*ptr, " \t");
5373 /* Quoted ASCII string - no wide characters! */
5375 n = strcspn(t, "\"");
5378 ret = str_to_data(t, n, mem_ctx, data);
5385 fprintf(stderr, "Unmatched \" in input %s\n", *ptr);
5389 fprintf(stderr, "Unsupported input format in %s\n", *ptr);
5396 #define MAX_LINE_SIZE 1024
5398 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
5399 TDB_DATA *key, TDB_DATA *value)
5401 char line [MAX_LINE_SIZE]; /* FIXME: make this more flexible? */
5405 ptr = fgets(line, MAX_LINE_SIZE, file);
5411 ret = ptrans_parse_string(mem_ctx, &ptr, key);
5412 if (ret != 0 || ptr == NULL || key->dptr == NULL) {
5413 /* Line Ignored but not EOF */
5419 ret = ptrans_parse_string(mem_ctx, &ptr, value);
5421 /* Line Ignored but not EOF */
5422 talloc_free(key->dptr);
5430 static int control_ptrans(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5431 int argc, const char **argv)
5433 const char *db_name;
5434 struct ctdb_db_context *db;
5435 struct ctdb_transaction_handle *h;
5438 TDB_DATA key, value;
5441 if (argc < 1 || argc > 2) {
5445 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5449 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5450 fprintf(stderr, "DB %s is not a persistent database\n",
5456 file = fopen(argv[1], "r");
5458 fprintf(stderr, "Failed to open file %s\n", argv[1]);
5465 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5468 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5472 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5473 TIMEOUT(), db, false, &h);
5475 fprintf(stderr, "Failed to start transaction on db %s\n",
5480 while (ptrans_get_key_value(mem_ctx, file, &key, &value)) {
5481 if (key.dsize != 0) {
5482 ret = ctdb_transaction_store_record(h, key, value);
5484 fprintf(stderr, "Failed to store record\n");
5485 ctdb_transaction_cancel(h);
5488 talloc_free(key.dptr);
5489 talloc_free(value.dptr);
5493 ret = ctdb_transaction_commit(h);
5495 fprintf(stderr, "Failed to commit transaction on db %s\n",
5497 ctdb_transaction_cancel(h);
5501 if (file != stdin) {
5507 static int control_tfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5508 int argc, const char **argv)
5510 struct tdb_context *tdb;
5512 struct ctdb_ltdb_header header;
5515 if (argc < 2 || argc > 3) {
5519 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5521 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5525 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5527 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5532 data = tdb_fetch(tdb, key);
5533 if (data.dptr == NULL) {
5534 fprintf(stderr, "No record for key %s\n", argv[1]);
5539 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
5540 fprintf(stderr, "Invalid record for key %s\n", argv[1]);
5551 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
5553 fprintf(stderr, "Failed to open output file %s\n",
5558 nwritten = sys_write(fd, data.dptr, data.dsize);
5559 if (nwritten != data.dsize) {
5560 fprintf(stderr, "Failed to write record to file\n");
5569 ret = ctdb_ltdb_header_extract(&data, &header);
5571 fprintf(stderr, "Failed to parse header from data\n");
5575 dump_ltdb_header(&header);
5576 dump_tdb_data("data", data);
5581 static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5582 int argc, const char **argv)
5584 struct tdb_context *tdb;
5585 TDB_DATA key, data[2], value;
5586 struct ctdb_ltdb_header header;
5587 uint8_t header_buf[sizeof(struct ctdb_ltdb_header)];
5590 if (argc < 3 || argc > 5) {
5594 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5596 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5600 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5602 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5607 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &value);
5609 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5614 ZERO_STRUCT(header);
5617 header.rsn = (uint64_t)strtoull(argv[3], NULL, 0);
5620 header.dmaster = (uint32_t)atol(argv[4]);
5623 header.flags = (uint32_t)atol(argv[5]);
5626 ctdb_ltdb_header_push(&header, header_buf);
5628 data[0].dsize = ctdb_ltdb_header_len(&header);
5629 data[0].dptr = header_buf;
5631 data[1].dsize = value.dsize;
5632 data[1].dptr = value.dptr;
5634 ret = tdb_storev(tdb, key, data, 2, TDB_REPLACE);
5636 fprintf(stderr, "Failed to write record %s to file %s\n",
5645 static int control_readkey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5646 int argc, const char **argv)
5648 const char *db_name;
5649 struct ctdb_db_context *db;
5650 struct ctdb_record_handle *h;
5653 bool readonly = false;
5656 if (argc < 2 || argc > 3) {
5661 if (strcmp(argv[2], "readonly") == 0) {
5668 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5672 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5673 fprintf(stderr, "DB %s is not a volatile database\n",
5678 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5681 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5685 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5687 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5691 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5692 db, key, readonly, &h, NULL, &data);
5694 fprintf(stderr, "Failed to read record for key %s\n",
5697 printf("Data: size:%zu ptr:[%.*s]\n", data.dsize,
5698 (int)data.dsize, data.dptr);
5705 static int control_writekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5706 int argc, const char **argv)
5708 const char *db_name;
5709 struct ctdb_db_context *db;
5710 struct ctdb_record_handle *h;
5719 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5723 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5724 fprintf(stderr, "DB %s is not a volatile database\n",
5729 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5732 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5736 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5738 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5742 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5744 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5748 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5749 db, key, false, &h, NULL, NULL);
5751 fprintf(stderr, "Failed to lock record for key %s\n", argv[0]);
5755 ret = ctdb_store_record(h, data);
5757 fprintf(stderr, "Failed to store record for key %s\n",
5765 static int control_deletekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5766 int argc, const char **argv)
5768 const char *db_name;
5769 struct ctdb_db_context *db;
5770 struct ctdb_record_handle *h;
5779 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5783 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5784 fprintf(stderr, "DB %s is not a volatile database\n",
5789 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5792 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5796 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5798 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5802 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5803 db, key, false, &h, NULL, &data);
5805 fprintf(stderr, "Failed to fetch record for key %s\n",
5810 ret = ctdb_delete_record(h);
5812 fprintf(stderr, "Failed to delete record for key %s\n",
5820 static int control_checktcpport(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5821 int argc, const char **argv)
5823 struct sockaddr_in sin;
5829 usage("chktcpport");
5832 port = atoi(argv[0]);
5834 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
5836 fprintf(stderr, "Failed to open local socket\n");
5840 v = fcntl(s, F_GETFL, 0);
5841 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK)) {
5842 fprintf(stderr, "Unable to set socket non-blocking\n");
5847 bzero(&sin, sizeof(sin));
5848 sin.sin_family = AF_INET;
5849 sin.sin_port = htons(port);
5850 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
5853 fprintf(stderr, "Failed to bind to TCP port %u\n", port);
5860 static int control_getdbseqnum(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5861 int argc, const char **argv)
5864 const char *db_name;
5869 usage("getdbseqnum");
5872 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5876 ret = ctdb_ctrl_get_db_seqnum(mem_ctx, ctdb->ev, ctdb->client,
5877 ctdb->cmd_pnn, TIMEOUT(), db_id,
5880 fprintf(stderr, "Failed to get sequence number for DB %s\n",
5885 printf("0x%"PRIx64"\n", seqnum);
5889 static int control_nodestatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5890 int argc, const char **argv)
5892 const char *nodestring = NULL;
5893 struct ctdb_node_map *nodemap;
5897 usage("nodestatus");
5901 nodestring = argv[0];
5904 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap)) {
5908 nodemap = get_nodemap(ctdb, false);
5909 if (nodemap == NULL) {
5913 if (options.machinereadable) {
5914 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5916 print_nodemap(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5920 for (i=0; i<nodemap->num; i++) {
5921 ret |= nodemap->node[i].flags;
5930 } db_stats_fields[] = {
5931 #define DBSTATISTICS_FIELD(n) { #n, offsetof(struct ctdb_db_statistics, n) }
5932 DBSTATISTICS_FIELD(db_ro_delegations),
5933 DBSTATISTICS_FIELD(db_ro_revokes),
5934 DBSTATISTICS_FIELD(locks.num_calls),
5935 DBSTATISTICS_FIELD(locks.num_current),
5936 DBSTATISTICS_FIELD(locks.num_pending),
5937 DBSTATISTICS_FIELD(locks.num_failed),
5938 DBSTATISTICS_FIELD(db_ro_delegations),
5941 static void print_dbstatistics(const char *db_name,
5942 struct ctdb_db_statistics *s)
5945 const char *prefix = NULL;
5948 printf("DB Statistics %s\n", db_name);
5950 for (i=0; i<ARRAY_SIZE(db_stats_fields); i++) {
5951 if (strchr(db_stats_fields[i].name, '.') != NULL) {
5952 preflen = strcspn(db_stats_fields[i].name, ".") + 1;
5954 strncmp(prefix, db_stats_fields[i].name, preflen) != 0) {
5955 prefix = db_stats_fields[i].name;
5956 printf(" %*.*s\n", preflen-1, preflen-1,
5957 db_stats_fields[i].name);
5962 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
5963 db_stats_fields[i].name+preflen, preflen ? 0 : 4, "",
5964 *(uint32_t *)(db_stats_fields[i].offset+(uint8_t *)s));
5967 printf(" hop_count_buckets:");
5968 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5969 printf(" %d", s->hop_count_bucket[i]);
5973 printf(" lock_buckets:");
5974 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5975 printf(" %d", s->locks.buckets[i]);
5979 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5980 "locks_latency MIN/AVG/MAX",
5981 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
5982 s->locks.latency.max, s->locks.latency.num);
5984 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5985 "vacuum_latency MIN/AVG/MAX",
5986 s->vacuum.latency.min, LATENCY_AVG(s->vacuum.latency),
5987 s->vacuum.latency.max, s->vacuum.latency.num);
5989 printf(" Num Hot Keys: %d\n", s->num_hot_keys);
5990 for (i=0; i<s->num_hot_keys; i++) {
5992 printf(" Count:%d Key:", s->hot_keys[i].count);
5993 for (j=0; j<s->hot_keys[i].key.dsize; j++) {
5994 printf("%02x", s->hot_keys[i].key.dptr[j] & 0xff);
6000 static int control_dbstatistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
6001 int argc, const char **argv)
6004 const char *db_name;
6005 struct ctdb_db_statistics *dbstats;
6009 usage("dbstatistics");
6012 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
6016 ret = ctdb_ctrl_get_db_statistics(mem_ctx, ctdb->ev, ctdb->client,
6017 ctdb->cmd_pnn, TIMEOUT(), db_id,
6020 fprintf(stderr, "Failed to get statistics for DB %s\n",
6025 print_dbstatistics(db_name, dbstats);
6029 struct disable_takeover_runs_state {
6037 static void disable_takeover_run_handler(uint64_t srvid, TDB_DATA data,
6040 struct disable_takeover_runs_state *state =
6041 (struct disable_takeover_runs_state *)private_data;
6044 if (data.dsize != sizeof(int)) {
6049 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
6050 ret = *(int *)data.dptr;
6052 state->status = ret;
6056 for (i=0; i<state->node_count; i++) {
6057 if (state->pnn_list[i] == ret) {
6058 state->reply[i] = true;
6064 for (i=0; i<state->node_count; i++) {
6065 if (! state->reply[i]) {
6066 state->done = false;
6072 static int disable_takeover_runs(TALLOC_CTX *mem_ctx,
6073 struct ctdb_context *ctdb, uint32_t timeout,
6074 uint32_t *pnn_list, int count)
6076 struct ctdb_disable_message disable = { 0 };
6077 struct disable_takeover_runs_state state;
6080 disable.pnn = ctdb->pnn;
6081 disable.srvid = next_srvid(ctdb);
6082 disable.timeout = timeout;
6084 state.pnn_list = pnn_list;
6085 state.node_count = count;
6088 state.reply = talloc_zero_array(mem_ctx, bool, count);
6089 if (state.reply == NULL) {
6093 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
6095 disable_takeover_run_handler,
6101 for (i=0; i<count; i++) {
6102 ret = ctdb_message_disable_takeover_runs(mem_ctx, ctdb->ev,
6111 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
6113 fprintf(stderr, "Timed out waiting to disable takeover runs\n");
6115 ret = (state.status >= 0 ? 0 : 1);
6119 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
6120 disable.srvid, &state);
6124 static int control_reloadips(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
6125 int argc, const char **argv)
6127 const char *nodestring = NULL;
6128 struct ctdb_node_map *nodemap, *nodemap2;
6129 struct ctdb_req_control request;
6130 uint32_t *pnn_list, *pnn_list2;
6131 int ret, count, count2;
6138 nodestring = argv[0];
6141 nodemap = get_nodemap(ctdb, false);
6142 if (nodemap == NULL) {
6146 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap2)) {
6150 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
6151 mem_ctx, &pnn_list);
6153 fprintf(stderr, "Memory allocation error\n");
6157 count2 = list_of_active_nodes(nodemap2, CTDB_UNKNOWN_PNN,
6158 mem_ctx, &pnn_list2);
6160 fprintf(stderr, "Memory allocation error\n");
6164 /* Disable takeover runs on all connected nodes. A reply
6165 * indicating success is needed from each node so all nodes
6166 * will need to be active.
6168 * A check could be added to not allow reloading of IPs when
6169 * there are disconnected nodes. However, this should
6170 * probably be left up to the administrator.
6172 ret = disable_takeover_runs(mem_ctx, ctdb, 2*options.timelimit,
6175 fprintf(stderr, "Failed to disable takeover runs\n");
6179 /* Now tell all the desired nodes to reload their public IPs.
6180 * Keep trying this until it succeeds. This assumes all
6181 * failures are transient, which might not be true...
6183 ctdb_req_control_reload_public_ips(&request);
6184 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
6185 pnn_list2, count2, TIMEOUT(),
6186 &request, NULL, NULL);
6188 fprintf(stderr, "Failed to reload IPs on some nodes.\n");
6191 /* It isn't strictly necessary to wait until takeover runs are
6192 * re-enabled but doing so can't hurt.
6194 ret = disable_takeover_runs(mem_ctx, ctdb, 0, pnn_list, count);
6196 fprintf(stderr, "Failed to enable takeover runs\n");
6200 return ipreallocate(mem_ctx, ctdb);
6203 static int control_ipiface(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
6204 int argc, const char **argv)
6206 ctdb_sock_addr addr;
6213 if (! parse_ip(argv[0], NULL, 0, &addr)) {
6214 fprintf(stderr, "Failed to Parse IP %s\n", argv[0]);
6218 iface = ctdb_sys_find_ifname(&addr);
6219 if (iface == NULL) {
6220 fprintf(stderr, "Failed to find interface for IP %s\n",
6230 static const struct ctdb_cmd {
6232 int (*fn)(TALLOC_CTX *, struct ctdb_context *, int, const char **);
6233 bool without_daemon; /* can be run without daemon running ? */
6234 bool remote; /* can be run on remote nodes */
6237 } ctdb_commands[] = {
6238 { "version", control_version, true, false,
6239 "show version of ctdb", NULL },
6240 { "status", control_status, false, true,
6241 "show node status", NULL },
6242 { "uptime", control_uptime, false, true,
6243 "show node uptime", NULL },
6244 { "ping", control_ping, false, true,
6245 "ping all nodes", NULL },
6246 { "runstate", control_runstate, false, true,
6247 "get/check runstate of a node",
6248 "[setup|first_recovery|startup|running]" },
6249 { "getvar", control_getvar, false, true,
6250 "get a tunable variable", "<name>" },
6251 { "setvar", control_setvar, false, true,
6252 "set a tunable variable", "<name> <value>" },
6253 { "listvars", control_listvars, false, true,
6254 "list tunable variables", NULL },
6255 { "statistics", control_statistics, false, true,
6256 "show ctdb statistics", NULL },
6257 { "statisticsreset", control_statistics_reset, false, true,
6258 "reset ctdb statistics", NULL },
6259 { "stats", control_stats, false, true,
6260 "show rolling statistics", "[count]" },
6261 { "ip", control_ip, false, true,
6262 "show public ips", "[all]" },
6263 { "ipinfo", control_ipinfo, false, true,
6264 "show public ip details", "<ip>" },
6265 { "ifaces", control_ifaces, false, true,
6266 "show interfaces", NULL },
6267 { "setifacelink", control_setifacelink, false, true,
6268 "set interface link status", "<iface> up|down" },
6269 { "process-exists", control_process_exists, false, true,
6270 "check if a process exists on a node", "<pid>" },
6271 { "getdbmap", control_getdbmap, false, true,
6272 "show attached databases", NULL },
6273 { "getdbstatus", control_getdbstatus, false, true,
6274 "show database status", "<dbname|dbid>" },
6275 { "catdb", control_catdb, false, false,
6276 "dump cluster-wide ctdb database", "<dbname|dbid>" },
6277 { "cattdb", control_cattdb, false, false,
6278 "dump local ctdb database", "<dbname|dbid>" },
6279 { "getmonmode", control_getmonmode, false, true,
6280 "show monitoring mode", NULL },
6281 { "getcapabilities", control_getcapabilities, false, true,
6282 "show node capabilities", NULL },
6283 { "pnn", control_pnn, false, false,
6284 "show the pnn of the currnet node", NULL },
6285 { "lvs", control_lvs, false, false,
6286 "show lvs configuration", "master|list|status" },
6287 { "disablemonitor", control_disable_monitor, false, true,
6288 "disable monitoring", NULL },
6289 { "enablemonitor", control_enable_monitor, false, true,
6290 "enable monitoring", NULL },
6291 { "setdebug", control_setdebug, false, true,
6292 "set debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
6293 { "getdebug", control_getdebug, false, true,
6294 "get debug level", NULL },
6295 { "attach", control_attach, false, false,
6296 "attach a database", "<dbname> [persistent]" },
6297 { "detach", control_detach, false, false,
6298 "detach database(s)", "<dbname|dbid> ..." },
6299 { "dumpmemory", control_dumpmemory, false, true,
6300 "dump ctdbd memory map", NULL },
6301 { "rddumpmemory", control_rddumpmemory, false, true,
6302 "dump recoverd memory map", NULL },
6303 { "getpid", control_getpid, false, true,
6304 "get ctdbd process ID", NULL },
6305 { "disable", control_disable, false, true,
6306 "disable a node", NULL },
6307 { "enable", control_enable, false, true,
6308 "enable a node", NULL },
6309 { "stop", control_stop, false, true,
6310 "stop a node", NULL },
6311 { "continue", control_continue, false, true,
6312 "continue a stopped node", NULL },
6313 { "ban", control_ban, false, true,
6314 "ban a node", "<bantime>"},
6315 { "unban", control_unban, false, true,
6316 "unban a node", NULL },
6317 { "shutdown", control_shutdown, false, true,
6318 "shutdown ctdb daemon", NULL },
6319 { "recover", control_recover, false, true,
6320 "force recovery", NULL },
6321 { "sync", control_ipreallocate, false, true,
6322 "run ip reallocation (deprecated)", NULL },
6323 { "ipreallocate", control_ipreallocate, false, true,
6324 "run ip reallocation", NULL },
6325 { "isnotrecmaster", control_isnotrecmaster, false, false,
6326 "check if local node is the recmaster", NULL },
6327 { "gratarp", control_gratarp, false, true,
6328 "send a gratuitous arp", "<ip> <interface>" },
6329 { "tickle", control_tickle, true, false,
6330 "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6331 { "gettickles", control_gettickles, false, true,
6332 "get the list of tickles", "<ip> [<port>]" },
6333 { "addtickle", control_addtickle, false, true,
6334 "add a tickle", "<ip>:<port> <ip>:<port>" },
6335 { "deltickle", control_deltickle, false, true,
6336 "delete a tickle", "<ip>:<port> <ip>:<port>" },
6337 { "check_srvids", control_check_srvids, false, true,
6338 "check if srvid is registered", "<id> [<id> ...]" },
6339 { "listnodes", control_listnodes, true, true,
6340 "list nodes in the cluster", NULL },
6341 { "reloadnodes", control_reloadnodes, false, false,
6342 "reload the nodes file all nodes", NULL },
6343 { "moveip", control_moveip, false, false,
6344 "move an ip address to another node", "<ip> <node>" },
6345 { "addip", control_addip, false, true,
6346 "add an ip address to a node", "<ip/mask> <iface>" },
6347 { "delip", control_delip, false, true,
6348 "delete an ip address from a node", "<ip>" },
6349 { "eventscript", control_eventscript, false, true,
6350 "run an event", "monitor" },
6351 { "backupdb", control_backupdb, false, false,
6352 "backup a database into a file", "<dbname|dbid> <file>" },
6353 { "restoredb", control_restoredb, false, false,
6354 "restore a database from a file", "<file> [dbname]" },
6355 { "dumpdbbackup", control_dumpdbbackup, true, false,
6356 "dump database from a backup file", "<file>" },
6357 { "wipedb", control_wipedb, false, false,
6358 "wipe the contents of a database.", "<dbname|dbid>"},
6359 { "recmaster", control_recmaster, false, true,
6360 "show the pnn for the recovery master", NULL },
6361 { "event", control_event, true, false,
6362 "event and event script commands", NULL },
6363 { "scriptstatus", control_scriptstatus, false, true,
6364 "show event script status",
6365 "[init|setup|startup|monitor|takeip|releaseip|ipreallocated]" },
6366 { "enablescript", control_enablescript, false, true,
6367 "enable an eventscript", "<script>"},
6368 { "disablescript", control_disablescript, false, true,
6369 "disable an eventscript", "<script>"},
6370 { "natgw", control_natgw, false, false,
6371 "show natgw configuration", "master|list|status" },
6372 { "natgwlist", control_natgwlist, false, false,
6373 "show the nodes belonging to this natgw configuration", NULL },
6374 { "getreclock", control_getreclock, false, true,
6375 "get recovery lock file", NULL },
6376 { "setlmasterrole", control_setlmasterrole, false, true,
6377 "set LMASTER role", "on|off" },
6378 { "setrecmasterrole", control_setrecmasterrole, false, true,
6379 "set RECMASTER role", "on|off"},
6380 { "setdbreadonly", control_setdbreadonly, false, true,
6381 "enable readonly records", "<dbname|dbid>" },
6382 { "setdbsticky", control_setdbsticky, false, true,
6383 "enable sticky records", "<dbname|dbid>"},
6384 { "pfetch", control_pfetch, false, false,
6385 "fetch record from persistent database", "<dbname|dbid> <key> [<file>]" },
6386 { "pstore", control_pstore, false, false,
6387 "write record to persistent database", "<dbname|dbid> <key> <value>" },
6388 { "pdelete", control_pdelete, false, false,
6389 "delete record from persistent database", "<dbname|dbid> <key>" },
6390 { "ptrans", control_ptrans, false, false,
6391 "update a persistent database (from file or stdin)", "<dbname|dbid> [<file>]" },
6392 { "tfetch", control_tfetch, false, true,
6393 "fetch a record", "<tdb-file> <key> [<file>]" },
6394 { "tstore", control_tstore, false, true,
6395 "store a record", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6396 { "readkey", control_readkey, false, false,
6397 "read value of a database key", "<dbname|dbid> <key> [readonly]" },
6398 { "writekey", control_writekey, false, false,
6399 "write value for a database key", "<dbname|dbid> <key> <value>" },
6400 { "deletekey", control_deletekey, false, false,
6401 "delete a database key", "<dbname|dbid> <key>" },
6402 { "checktcpport", control_checktcpport, true, false,
6403 "check if a service is bound to a specific tcp port or not", "<port>" },
6404 { "getdbseqnum", control_getdbseqnum, false, false,
6405 "get database sequence number", "<dbname|dbid>" },
6406 { "nodestatus", control_nodestatus, false, true,
6407 "show and return node status", "[all|<pnn-list>]" },
6408 { "dbstatistics", control_dbstatistics, false, true,
6409 "show database statistics", "<dbname|dbid>" },
6410 { "reloadips", control_reloadips, false, false,
6411 "reload the public addresses file", "[all|<pnn-list>]" },
6412 { "ipiface", control_ipiface, true, false,
6413 "Find the interface an ip address is hosted on", "<ip>" },
6416 static const struct ctdb_cmd *match_command(const char *command)
6418 const struct ctdb_cmd *cmd;
6421 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6422 cmd = &ctdb_commands[i];
6423 if (strlen(command) == strlen(cmd->name) &&
6424 strncmp(command, cmd->name, strlen(command)) == 0) {
6434 * Show usage message
6436 static void usage_full(void)
6440 poptPrintHelp(pc, stdout, 0);
6441 printf("\nCommands:\n");
6442 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6443 printf(" %-15s %-27s %s\n",
6444 ctdb_commands[i].name,
6445 ctdb_commands[i].args ? ctdb_commands[i].args : "",
6446 ctdb_commands[i].msg);
6450 static void usage(const char *command)
6452 const struct ctdb_cmd *cmd;
6454 if (command == NULL) {
6459 cmd = match_command(command);
6463 poptPrintUsage(pc, stdout, 0);
6464 printf("\nCommands:\n");
6465 printf(" %-15s %-27s %s\n",
6466 cmd->name, cmd->args ? cmd->args : "", cmd->msg);
6472 struct poptOption cmdline_options[] = {
6474 { "socket", 's', POPT_ARG_STRING, &options.socket, 0,
6475 "CTDB socket path", "filename" },
6476 { "debug", 'd', POPT_ARG_STRING, &options.debuglevelstr, 0,
6478 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0,
6479 "timelimit (in seconds)" },
6480 { "node", 'n', POPT_ARG_INT, &options.pnn, 0,
6481 "node specification - integer" },
6482 { NULL, 'Y', POPT_ARG_NONE, &options.machinereadable, 0,
6483 "enable machine readable output", NULL },
6484 { "separator", 'x', POPT_ARG_STRING, &options.sep, 0,
6485 "specify separator for machine readable output", "CHAR" },
6486 { NULL, 'X', POPT_ARG_NONE, &options.machineparsable, 0,
6487 "enable machine parsable output with separator |", NULL },
6488 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0,
6489 "enable verbose output", NULL },
6490 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0,
6491 "die if runtime exceeds this limit (in seconds)" },
6495 static int process_command(const struct ctdb_cmd *cmd, int argc,
6498 TALLOC_CTX *tmp_ctx;
6499 struct ctdb_context *ctdb;
6502 uint64_t srvid_offset;
6504 tmp_ctx = talloc_new(NULL);
6505 if (tmp_ctx == NULL) {
6506 fprintf(stderr, "Memory allocation error\n");
6510 if (cmd->without_daemon) {
6511 if (options.pnn != -1) {
6513 "Cannot specify node for command %s\n",
6518 ret = cmd->fn(tmp_ctx, NULL, argc-1, argv+1);
6519 talloc_free(tmp_ctx);
6523 ctdb = talloc_zero(tmp_ctx, struct ctdb_context);
6525 fprintf(stderr, "Memory allocation error\n");
6529 ctdb->ev = tevent_context_init(ctdb);
6530 if (ctdb->ev == NULL) {
6531 fprintf(stderr, "Failed to initialize tevent\n");
6535 ret = ctdb_client_init(ctdb, ctdb->ev, options.socket, &ctdb->client);
6537 fprintf(stderr, "Failed to connect to CTDB daemon (%s)\n",
6540 if (!find_node_xpnn(ctdb, NULL)) {
6541 fprintf(stderr, "Is this node part of CTDB cluster?\n");
6546 ctdb->pnn = ctdb_client_pnn(ctdb->client);
6547 srvid_offset = getpid() & 0xFFFF;
6548 ctdb->srvid = SRVID_CTDB_TOOL | (srvid_offset << 16);
6550 if (options.pnn != -1) {
6551 status = verify_pnn(ctdb, options.pnn);
6556 ctdb->cmd_pnn = options.pnn;
6558 ctdb->cmd_pnn = ctdb->pnn;
6561 if (! cmd->remote && ctdb->pnn != ctdb->cmd_pnn) {
6562 fprintf(stderr, "Node cannot be specified for command %s\n",
6567 ret = cmd->fn(tmp_ctx, ctdb, argc-1, argv+1);
6568 talloc_free(tmp_ctx);
6572 talloc_free(tmp_ctx);
6576 static void signal_handler(int sig)
6578 fprintf(stderr, "Maximum runtime exceeded - exiting\n");
6581 static void alarm_handler(int sig)
6583 /* Kill any child processes */
6584 signal(SIGTERM, signal_handler);
6590 int main(int argc, const char *argv[])
6593 const char **extra_argv;
6595 const struct ctdb_cmd *cmd;
6596 const char *ctdb_socket;
6602 /* Set default options */
6603 options.socket = CTDB_SOCKET;
6604 options.debuglevelstr = NULL;
6605 options.timelimit = 10;
6607 options.maxruntime = 0;
6610 ctdb_socket = getenv("CTDB_SOCKET");
6611 if (ctdb_socket != NULL) {
6612 options.socket = ctdb_socket;
6615 pc = poptGetContext(argv[0], argc, argv, cmdline_options,
6616 POPT_CONTEXT_KEEP_FIRST);
6617 while ((opt = poptGetNextOpt(pc)) != -1) {
6618 fprintf(stderr, "Invalid option %s: %s\n",
6619 poptBadOption(pc, 0), poptStrerror(opt));
6623 if (options.maxruntime == 0) {
6624 const char *ctdb_timeout;
6626 ctdb_timeout = getenv("CTDB_TIMEOUT");
6627 if (ctdb_timeout != NULL) {
6628 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6630 options.maxruntime = 120;
6633 if (options.maxruntime <= 120) {
6634 /* default timeout is 120 seconds */
6635 options.maxruntime = 120;
6638 if (options.machineparsable) {
6639 options.machinereadable = 1;
6642 /* setup the remaining options for the commands */
6644 extra_argv = poptGetArgs(pc);
6647 while (extra_argv[extra_argc]) extra_argc++;
6650 if (extra_argc < 1) {
6654 cmd = match_command(extra_argv[0]);
6656 fprintf(stderr, "Unknown command '%s'\n", extra_argv[0]);
6660 /* Enable logging */
6661 setup_logging("ctdb", DEBUG_STDERR);
6662 if (debug_level_parse(options.debuglevelstr, &loglevel)) {
6663 DEBUGLEVEL = loglevel;
6665 DEBUGLEVEL = DEBUG_ERR;
6668 signal(SIGALRM, alarm_handler);
6669 alarm(options.maxruntime);
6671 ret = process_command(cmd, extra_argc, extra_argv);
6676 (void)poptFreeContext(pc);