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"
32 #include "ctdb_version.h"
33 #include "lib/util/debug.h"
34 #include "lib/util/samba_util.h"
35 #include "lib/util/sys_rw.h"
37 #include "common/db_hash.h"
38 #include "common/logging.h"
39 #include "protocol/protocol.h"
40 #include "protocol/protocol_api.h"
41 #include "common/system.h"
42 #include "client/client.h"
44 #define TIMEOUT() timeval_current_ofs(options.timelimit, 0)
46 #define SRVID_CTDB_TOOL (CTDB_SRVID_TOOL_RANGE | 0x0001000000000000LL)
47 #define SRVID_CTDB_PUSHDB (CTDB_SRVID_TOOL_RANGE | 0x0002000000000000LL)
51 const char *debuglevelstr;
59 int printemptyrecords;
66 static poptContext pc;
69 struct tevent_context *ev;
70 struct ctdb_client_context *client;
71 struct ctdb_node_map *nodemap;
72 uint32_t pnn, cmd_pnn;
76 static void usage(const char *command);
82 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
84 return (tv2->tv_sec - tv->tv_sec) +
85 (tv2->tv_usec - tv->tv_usec) * 1.0e-6;
88 static struct ctdb_node_and_flags *get_node_by_pnn(
89 struct ctdb_node_map *nodemap,
94 for (i=0; i<nodemap->num; i++) {
95 if (nodemap->node[i].pnn == pnn) {
96 return &nodemap->node[i];
102 static const char *pretty_print_flags(TALLOC_CTX *mem_ctx, uint32_t flags)
104 static const struct {
108 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
109 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
110 { NODE_FLAGS_BANNED, "BANNED" },
111 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
112 { NODE_FLAGS_DELETED, "DELETED" },
113 { NODE_FLAGS_STOPPED, "STOPPED" },
114 { NODE_FLAGS_INACTIVE, "INACTIVE" },
116 char *flags_str = NULL;
119 for (i=0; i<ARRAY_SIZE(flag_names); i++) {
120 if (flags & flag_names[i].flag) {
121 if (flags_str == NULL) {
122 flags_str = talloc_asprintf(mem_ctx,
123 "%s", flag_names[i].name);
125 flags_str = talloc_asprintf_append(flags_str,
126 "|%s", flag_names[i].name);
128 if (flags_str == NULL) {
129 return "OUT-OF-MEMORY";
133 if (flags_str == NULL) {
140 static uint64_t next_srvid(struct ctdb_context *ctdb)
147 * Get consistent nodemap information.
149 * If nodemap is already cached, use that. If not get it.
150 * If the current node is BANNED, then get nodemap from "better" node.
152 static struct ctdb_node_map *get_nodemap(struct ctdb_context *ctdb, bool force)
155 struct ctdb_node_map *nodemap;
156 struct ctdb_node_and_flags *node;
157 uint32_t current_node;
161 TALLOC_FREE(ctdb->nodemap);
164 if (ctdb->nodemap != NULL) {
165 return ctdb->nodemap;
168 tmp_ctx = talloc_new(ctdb);
169 if (tmp_ctx == NULL) {
173 current_node = ctdb->pnn;
175 ret = ctdb_ctrl_get_nodemap(tmp_ctx, ctdb->ev, ctdb->client,
176 current_node, TIMEOUT(), &nodemap);
178 fprintf(stderr, "Failed to get nodemap from node %u\n",
183 node = get_node_by_pnn(nodemap, current_node);
184 if (node->flags & NODE_FLAGS_BANNED) {
187 current_node = (current_node + 1) % nodemap->num;
188 node = get_node_by_pnn(nodemap, current_node);
190 (NODE_FLAGS_DELETED|NODE_FLAGS_DISCONNECTED))) {
193 } while (current_node != ctdb->pnn);
195 if (current_node == ctdb->pnn) {
196 /* Tried all nodes in the cluster */
197 fprintf(stderr, "Warning: All nodes are banned.\n");
204 ctdb->nodemap = talloc_steal(ctdb, nodemap);
208 talloc_free(tmp_ctx);
212 static bool verify_pnn(struct ctdb_context *ctdb, int pnn)
214 struct ctdb_node_map *nodemap;
222 nodemap = get_nodemap(ctdb, false);
223 if (nodemap == NULL) {
228 for (i=0; i<nodemap->num; i++) {
229 if (nodemap->node[i].pnn == pnn) {
235 fprintf(stderr, "Node %u does not exist\n", pnn);
239 if (nodemap->node[i].flags &
240 (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED)) {
241 fprintf(stderr, "Node %u has status %s\n", pnn,
242 pretty_print_flags(ctdb, nodemap->node[i].flags));
249 static struct ctdb_node_map *talloc_nodemap(TALLOC_CTX *mem_ctx,
250 struct ctdb_node_map *nodemap)
252 struct ctdb_node_map *nodemap2;
254 nodemap2 = talloc_zero(mem_ctx, struct ctdb_node_map);
255 if (nodemap2 == NULL) {
259 nodemap2->node = talloc_array(nodemap2, struct ctdb_node_and_flags,
261 if (nodemap2->node == NULL) {
262 talloc_free(nodemap2);
270 * Get the number and the list of matching nodes
272 * nodestring := NULL | all | pnn,[pnn,...]
274 * If nodestring is NULL, use the current node.
276 static bool parse_nodestring(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
277 const char *nodestring,
278 struct ctdb_node_map **out)
280 struct ctdb_node_map *nodemap, *nodemap2;
281 struct ctdb_node_and_flags *node;
284 nodemap = get_nodemap(ctdb, false);
285 if (nodemap == NULL) {
289 nodemap2 = talloc_nodemap(mem_ctx, nodemap);
290 if (nodemap2 == NULL) {
294 if (nodestring == NULL) {
295 for (i=0; i<nodemap->num; i++) {
296 if (nodemap->node[i].pnn == ctdb->cmd_pnn) {
297 nodemap2->node[0] = nodemap->node[i];
306 if (strcmp(nodestring, "all") == 0) {
307 for (i=0; i<nodemap->num; i++) {
308 nodemap2->node[i] = nodemap->node[i];
310 nodemap2->num = nodemap->num;
316 ns = talloc_strdup(mem_ctx, nodestring);
321 tok = strtok(ns, ",");
322 while (tok != NULL) {
326 pnn = (uint32_t)strtoul(tok, &endptr, 0);
327 if (pnn == 0 && tok == endptr) {
328 fprintf(stderr, "Invalid node %s\n", tok);
332 node = get_node_by_pnn(nodemap, pnn);
334 fprintf(stderr, "Node %u does not exist\n",
339 nodemap2->node[nodemap2->num] = *node;
342 tok = strtok(NULL, ",");
351 /* Compare IP address */
352 static bool ctdb_same_ip(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
356 if (ip1->sa.sa_family != ip2->sa.sa_family) {
360 switch (ip1->sa.sa_family) {
362 ret = (memcmp(&ip1->ip.sin_addr, &ip2->ip.sin_addr,
363 sizeof(struct in_addr)) == 0);
367 ret = (memcmp(&ip1->ip6.sin6_addr, &ip2->ip6.sin6_addr,
368 sizeof(struct in6_addr)) == 0);
375 /* Append a node to a node map with given address and flags */
376 static bool node_map_add(struct ctdb_node_map *nodemap,
377 const char *nstr, uint32_t flags)
381 struct ctdb_node_and_flags *n;
383 if (! parse_ip(nstr, NULL, 0, &addr)) {
384 fprintf(stderr, "Invalid IP address %s\n", nstr);
389 nodemap->node = talloc_realloc(nodemap, nodemap->node,
390 struct ctdb_node_and_flags, num+1);
391 if (nodemap->node == NULL) {
395 n = &nodemap->node[num];
400 nodemap->num = num+1;
404 /* Read a nodes file into a node map */
405 static struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
411 struct ctdb_node_map *nodemap;
413 nodemap = talloc_zero(mem_ctx, struct ctdb_node_map);
414 if (nodemap == NULL) {
418 lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
423 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
427 for (i=0; i<nlines; i++) {
433 /* strip leading spaces */
434 while((*node == ' ') || (*node == '\t')) {
440 /* strip trailing spaces */
442 ((node[len-1] == ' ') || (node[len-1] == '\t')))
452 /* A "deleted" node is a node that is
453 commented out in the nodes file. This is
454 used instead of removing a line, which
455 would cause subsequent nodes to change
457 flags = NODE_FLAGS_DELETED;
458 node = discard_const("0.0.0.0");
462 if (! node_map_add(nodemap, node, flags)) {
464 TALLOC_FREE(nodemap);
473 static struct ctdb_node_map *read_nodes_file(TALLOC_CTX *mem_ctx, uint32_t pnn)
475 struct ctdb_node_map *nodemap;
477 const char *nodes_list = NULL;
479 if (pnn != CTDB_UNKNOWN_PNN) {
480 nodepath = talloc_asprintf(mem_ctx, "CTDB_NODES_%u", pnn);
481 if (nodepath != NULL) {
482 nodes_list = getenv(nodepath);
485 if (nodes_list == NULL) {
486 nodes_list = getenv("CTDB_NODES");
488 if (nodes_list == NULL) {
489 const char *basedir = getenv("CTDB_BASE");
490 if (basedir == NULL) {
491 basedir = CTDB_ETCDIR;
493 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes", basedir);
494 if (nodes_list == NULL) {
495 fprintf(stderr, "Memory allocation error\n");
500 nodemap = ctdb_read_nodes_file(mem_ctx, nodes_list);
501 if (nodemap == NULL) {
502 fprintf(stderr, "Failed to read nodes file \"%s\"\n",
510 static struct ctdb_dbid *db_find(TALLOC_CTX *mem_ctx,
511 struct ctdb_context *ctdb,
512 struct ctdb_dbid_map *dbmap,
515 struct ctdb_dbid *db = NULL;
519 for (i=0; i<dbmap->num; i++) {
520 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
521 ctdb->pnn, TIMEOUT(),
522 dbmap->dbs[i].db_id, &name);
527 if (strcmp(db_name, name) == 0) {
528 talloc_free(discard_const(name));
537 static bool db_exists(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
538 const char *db_arg, uint32_t *db_id,
539 const char **db_name, uint8_t *db_flags)
541 struct ctdb_dbid_map *dbmap;
542 struct ctdb_dbid *db = NULL;
544 const char *name = NULL;
547 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
548 ctdb->pnn, TIMEOUT(), &dbmap);
553 if (strncmp(db_arg, "0x", 2) == 0) {
554 id = strtoul(db_arg, NULL, 0);
555 for (i=0; i<dbmap->num; i++) {
556 if (id == dbmap->dbs[i].db_id) {
563 db = db_find(mem_ctx, ctdb, dbmap, name);
567 fprintf(stderr, "No database matching '%s' found\n", db_arg);
572 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
573 ctdb->pnn, TIMEOUT(), id, &name);
582 if (db_name != NULL) {
583 *db_name = talloc_strdup(mem_ctx, name);
585 if (db_flags != NULL) {
586 *db_flags = db->flags;
591 static int h2i(char h)
593 if (h >= 'a' && h <= 'f') {
596 if (h >= 'A' && h <= 'F') {
602 static int hex_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
609 fprintf(stderr, "Key (%s) contains odd number of hex digits\n",
614 data.dsize = len / 2;
615 data.dptr = talloc_size(mem_ctx, data.dsize);
616 if (data.dptr == NULL) {
620 for (i=0; i<data.dsize; i++) {
621 data.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
628 static int str_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
634 if (strncmp(str, "0x", 2) == 0) {
635 ret = hex_to_data(str+2, len-2, mem_ctx, &data);
637 data.dptr = talloc_memdup(mem_ctx, str, len);
638 if (data.dptr == NULL) {
648 static int run_helper(const char *command, const char *path, const char *arg1)
651 int save_errno, status, ret;
656 fprintf(stderr, "Failed to fork %s (%s) - %s\n",
657 command, path, strerror(save_errno));
662 ret = execl(path, path, arg1, NULL);
666 /* Should not happen */
670 ret = waitpid(pid, &status, 0);
673 fprintf(stderr, "waitpid() failed for %s - %s\n",
674 command, strerror(save_errno));
678 if (WIFEXITED(status)) {
679 ret = WEXITSTATUS(status);
683 if (WIFSIGNALED(status)) {
684 fprintf(stderr, "%s terminated with signal %d\n",
685 command, WTERMSIG(status));
696 static int control_version(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
697 int argc, const char **argv)
699 printf("%s\n", CTDB_VERSION_STRING);
703 static bool partially_online(TALLOC_CTX *mem_ctx,
704 struct ctdb_context *ctdb,
705 struct ctdb_node_and_flags *node)
707 struct ctdb_iface_list *iface_list;
711 if (node->flags != 0) {
715 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
716 node->pnn, TIMEOUT(), &iface_list);
722 for (i=0; i < iface_list->num; i++) {
723 if (iface_list->iface[i].link_state == 0) {
732 static void print_nodemap_machine(TALLOC_CTX *mem_ctx,
733 struct ctdb_context *ctdb,
734 struct ctdb_node_map *nodemap,
737 struct ctdb_node_and_flags *node;
740 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
744 "Disconnected", options.sep,
745 "Banned", options.sep,
746 "Disabled", options.sep,
747 "Unhealthy", options.sep,
748 "Stopped", options.sep,
749 "Inactive", options.sep,
750 "PartiallyOnline", options.sep,
751 "ThisNode", options.sep);
753 for (i=0; i<nodemap->num; i++) {
754 node = &nodemap->node[i];
755 if (node->flags & NODE_FLAGS_DELETED) {
759 printf("%s%u%s%s%s%d%s%d%s%d%s%d%s%d%s%d%s%d%s%c%s\n",
761 node->pnn, options.sep,
762 ctdb_sock_addr_to_string(mem_ctx, &node->addr),
764 !! (node->flags & NODE_FLAGS_DISCONNECTED), options.sep,
765 !! (node->flags & NODE_FLAGS_BANNED), options.sep,
766 !! (node->flags & NODE_FLAGS_PERMANENTLY_DISABLED),
768 !! (node->flags & NODE_FLAGS_UNHEALTHY), options.sep,
769 !! (node->flags & NODE_FLAGS_STOPPED), options.sep,
770 !! (node->flags & NODE_FLAGS_INACTIVE), options.sep,
771 partially_online(mem_ctx, ctdb, node), options.sep,
772 (node->pnn == mypnn)?'Y':'N', options.sep);
777 static void print_nodemap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
778 struct ctdb_node_map *nodemap, uint32_t mypnn)
780 struct ctdb_node_and_flags *node;
781 int num_deleted_nodes = 0;
784 for (i=0; i<nodemap->num; i++) {
785 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
790 if (num_deleted_nodes == 0) {
791 printf("Number of nodes:%d\n", nodemap->num);
793 printf("Number of nodes:%d (including %d deleted nodes)\n",
794 nodemap->num, num_deleted_nodes);
797 for (i=0; i<nodemap->num; i++) {
798 node = &nodemap->node[i];
799 if (node->flags & NODE_FLAGS_DELETED) {
803 printf("pnn:%u %-16s %s%s\n",
805 ctdb_sock_addr_to_string(mem_ctx, &node->addr),
806 partially_online(mem_ctx, ctdb, node) ?
808 pretty_print_flags(mem_ctx, node->flags),
809 node->pnn == mypnn ? " (THIS NODE)" : "");
813 static void print_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
814 struct ctdb_node_map *nodemap, uint32_t mypnn,
815 struct ctdb_vnn_map *vnnmap, int recmode,
820 print_nodemap(mem_ctx, ctdb, nodemap, mypnn);
822 if (vnnmap->generation == INVALID_GENERATION) {
823 printf("Generation:INVALID\n");
825 printf("Generation:%u\n", vnnmap->generation);
827 printf("Size:%d\n", vnnmap->size);
828 for (i=0; i<vnnmap->size; i++) {
829 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
832 printf("Recovery mode:%s (%d)\n",
833 recmode == CTDB_RECOVERY_NORMAL ? "NORMAL" : "RECOVERY",
835 printf("Recovery master:%d\n", recmaster);
838 static int control_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
839 int argc, const char **argv)
841 struct ctdb_node_map *nodemap;
842 struct ctdb_vnn_map *vnnmap;
851 nodemap = get_nodemap(ctdb, false);
852 if (nodemap == NULL) {
856 if (options.machinereadable == 1) {
857 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
861 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
862 ctdb->cmd_pnn, TIMEOUT(), &vnnmap);
867 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
868 ctdb->cmd_pnn, TIMEOUT(), &recmode);
873 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
874 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
879 print_status(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn, vnnmap,
884 static int control_uptime(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
885 int argc, const char **argv)
887 struct ctdb_uptime *uptime;
888 int ret, tmp, days, hours, minutes, seconds;
890 ret = ctdb_ctrl_uptime(mem_ctx, ctdb->ev, ctdb->client,
891 ctdb->cmd_pnn, TIMEOUT(), &uptime);
896 printf("Current time of node %-4u : %s",
897 ctdb->cmd_pnn, ctime(&uptime->current_time.tv_sec));
899 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
900 seconds = tmp % 60; tmp /= 60;
901 minutes = tmp % 60; tmp /= 60;
902 hours = tmp % 24; tmp /= 24;
905 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s",
906 days, hours, minutes, seconds,
907 ctime(&uptime->ctdbd_start_time.tv_sec));
909 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
910 seconds = tmp % 60; tmp /= 60;
911 minutes = tmp % 60; tmp /= 60;
912 hours = tmp % 24; tmp /= 24;
915 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s",
916 days, hours, minutes, seconds,
917 ctime(&uptime->last_recovery_finished.tv_sec));
919 printf("Duration of last recovery/failover: %lf seconds\n",
920 timeval_delta(&uptime->last_recovery_finished,
921 &uptime->last_recovery_started));
926 static int control_ping(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
927 int argc, const char **argv)
930 int ret, num_clients;
932 tv = timeval_current();
933 ret = ctdb_ctrl_ping(mem_ctx, ctdb->ev, ctdb->client,
934 ctdb->cmd_pnn, TIMEOUT(), &num_clients);
939 printf("response from %u time=%.6f sec (%d clients)\n",
940 ctdb->cmd_pnn, timeval_elapsed(&tv), num_clients);
944 const char *runstate_to_string(enum ctdb_runstate runstate);
945 enum ctdb_runstate runstate_from_string(const char *runstate_str);
947 static int control_runstate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
948 int argc, const char **argv)
950 enum ctdb_runstate runstate;
954 ret = ctdb_ctrl_get_runstate(mem_ctx, ctdb->ev, ctdb->client,
955 ctdb->cmd_pnn, TIMEOUT(), &runstate);
961 for (i=0; i<argc; i++) {
962 enum ctdb_runstate t;
965 t = ctdb_runstate_from_string(argv[i]);
966 if (t == CTDB_RUNSTATE_UNKNOWN) {
967 printf("Invalid run state (%s)\n", argv[i]);
978 printf("CTDB not in required run state (got %s)\n",
979 ctdb_runstate_to_string(runstate));
983 printf("%s\n", ctdb_runstate_to_string(runstate));
987 static int control_getvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
988 int argc, const char **argv)
990 struct ctdb_var_list *tun_var_list;
999 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1000 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1003 "Failed to get list of variables from node %u\n",
1009 for (i=0; i<tun_var_list->count; i++) {
1010 if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
1017 printf("No such tunable %s\n", argv[0]);
1021 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
1022 ctdb->cmd_pnn, TIMEOUT(), argv[0], &value);
1027 printf("%-26s = %u\n", argv[0], value);
1031 static int control_setvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1032 int argc, const char **argv)
1034 struct ctdb_var_list *tun_var_list;
1035 struct ctdb_tunable tunable;
1043 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1044 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1047 "Failed to get list of variables from node %u\n",
1053 for (i=0; i<tun_var_list->count; i++) {
1054 if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
1061 printf("No such tunable %s\n", argv[0]);
1065 tunable.name = argv[0];
1066 tunable.value = strtoul(argv[1], NULL, 0);
1068 ret = ctdb_ctrl_set_tunable(mem_ctx, ctdb->ev, ctdb->client,
1069 ctdb->cmd_pnn, TIMEOUT(), &tunable);
1073 "Setting obsolete tunable variable '%s'\n",
1082 static int control_listvars(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1083 int argc, const char **argv)
1085 struct ctdb_var_list *tun_var_list;
1092 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1093 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1098 for (i=0; i<tun_var_list->count; i++) {
1099 control_getvar(mem_ctx, ctdb, 1, &tun_var_list->var[i]);
1108 } stats_fields[] = {
1109 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
1110 STATISTICS_FIELD(num_clients),
1111 STATISTICS_FIELD(frozen),
1112 STATISTICS_FIELD(recovering),
1113 STATISTICS_FIELD(num_recoveries),
1114 STATISTICS_FIELD(client_packets_sent),
1115 STATISTICS_FIELD(client_packets_recv),
1116 STATISTICS_FIELD(node_packets_sent),
1117 STATISTICS_FIELD(node_packets_recv),
1118 STATISTICS_FIELD(keepalive_packets_sent),
1119 STATISTICS_FIELD(keepalive_packets_recv),
1120 STATISTICS_FIELD(node.req_call),
1121 STATISTICS_FIELD(node.reply_call),
1122 STATISTICS_FIELD(node.req_dmaster),
1123 STATISTICS_FIELD(node.reply_dmaster),
1124 STATISTICS_FIELD(node.reply_error),
1125 STATISTICS_FIELD(node.req_message),
1126 STATISTICS_FIELD(node.req_control),
1127 STATISTICS_FIELD(node.reply_control),
1128 STATISTICS_FIELD(client.req_call),
1129 STATISTICS_FIELD(client.req_message),
1130 STATISTICS_FIELD(client.req_control),
1131 STATISTICS_FIELD(timeouts.call),
1132 STATISTICS_FIELD(timeouts.control),
1133 STATISTICS_FIELD(timeouts.traverse),
1134 STATISTICS_FIELD(locks.num_calls),
1135 STATISTICS_FIELD(locks.num_current),
1136 STATISTICS_FIELD(locks.num_pending),
1137 STATISTICS_FIELD(locks.num_failed),
1138 STATISTICS_FIELD(total_calls),
1139 STATISTICS_FIELD(pending_calls),
1140 STATISTICS_FIELD(childwrite_calls),
1141 STATISTICS_FIELD(pending_childwrite_calls),
1142 STATISTICS_FIELD(memory_used),
1143 STATISTICS_FIELD(max_hop_count),
1144 STATISTICS_FIELD(total_ro_delegations),
1145 STATISTICS_FIELD(total_ro_revokes),
1148 #define LATENCY_AVG(v) ((v).num ? (v).total / (v).num : 0.0 )
1150 static void print_statistics_machine(struct ctdb_statistics *s,
1156 printf("CTDB version%s", options.sep);
1157 printf("Current time of statistics%s", options.sep);
1158 printf("Statistics collected since%s", options.sep);
1159 for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
1160 printf("%s%s", stats_fields[i].name, options.sep);
1162 printf("num_reclock_ctdbd_latency%s", options.sep);
1163 printf("min_reclock_ctdbd_latency%s", options.sep);
1164 printf("avg_reclock_ctdbd_latency%s", options.sep);
1165 printf("max_reclock_ctdbd_latency%s", options.sep);
1167 printf("num_reclock_recd_latency%s", options.sep);
1168 printf("min_reclock_recd_latency%s", options.sep);
1169 printf("avg_reclock_recd_latency%s", options.sep);
1170 printf("max_reclock_recd_latency%s", options.sep);
1172 printf("num_call_latency%s", options.sep);
1173 printf("min_call_latency%s", options.sep);
1174 printf("avg_call_latency%s", options.sep);
1175 printf("max_call_latency%s", options.sep);
1177 printf("num_lockwait_latency%s", options.sep);
1178 printf("min_lockwait_latency%s", options.sep);
1179 printf("avg_lockwait_latency%s", options.sep);
1180 printf("max_lockwait_latency%s", options.sep);
1182 printf("num_childwrite_latency%s", options.sep);
1183 printf("min_childwrite_latency%s", options.sep);
1184 printf("avg_childwrite_latency%s", options.sep);
1185 printf("max_childwrite_latency%s", options.sep);
1189 printf("%u%s", CTDB_PROTOCOL, options.sep);
1190 printf("%u%s", (uint32_t)s->statistics_current_time.tv_sec, options.sep);
1191 printf("%u%s", (uint32_t)s->statistics_start_time.tv_sec, options.sep);
1192 for (i=0;i<ARRAY_SIZE(stats_fields);i++) {
1194 *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s),
1197 printf("%u%s", s->reclock.ctdbd.num, options.sep);
1198 printf("%.6f%s", s->reclock.ctdbd.min, options.sep);
1199 printf("%.6f%s", LATENCY_AVG(s->reclock.ctdbd), options.sep);
1200 printf("%.6f%s", s->reclock.ctdbd.max, options.sep);
1202 printf("%u%s", s->reclock.recd.num, options.sep);
1203 printf("%.6f%s", s->reclock.recd.min, options.sep);
1204 printf("%.6f%s", LATENCY_AVG(s->reclock.recd), options.sep);
1205 printf("%.6f%s", s->reclock.recd.max, options.sep);
1207 printf("%d%s", s->call_latency.num, options.sep);
1208 printf("%.6f%s", s->call_latency.min, options.sep);
1209 printf("%.6f%s", LATENCY_AVG(s->call_latency), options.sep);
1210 printf("%.6f%s", s->call_latency.max, options.sep);
1212 printf("%d%s", s->childwrite_latency.num, options.sep);
1213 printf("%.6f%s", s->childwrite_latency.min, options.sep);
1214 printf("%.6f%s", LATENCY_AVG(s->childwrite_latency), options.sep);
1215 printf("%.6f%s", s->childwrite_latency.max, options.sep);
1219 static void print_statistics(struct ctdb_statistics *s)
1221 int tmp, days, hours, minutes, seconds;
1223 const char *prefix = NULL;
1226 tmp = s->statistics_current_time.tv_sec -
1227 s->statistics_start_time.tv_sec;
1228 seconds = tmp % 60; tmp /= 60;
1229 minutes = tmp % 60; tmp /= 60;
1230 hours = tmp % 24; tmp /= 24;
1233 printf("CTDB version %u\n", CTDB_PROTOCOL);
1234 printf("Current time of statistics : %s",
1235 ctime(&s->statistics_current_time.tv_sec));
1236 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s",
1237 days, hours, minutes, seconds,
1238 ctime(&s->statistics_start_time.tv_sec));
1240 for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
1241 if (strchr(stats_fields[i].name, '.') != NULL) {
1242 preflen = strcspn(stats_fields[i].name, ".") + 1;
1244 strncmp(prefix, stats_fields[i].name, preflen) != 0) {
1245 prefix = stats_fields[i].name;
1246 printf(" %*.*s\n", preflen-1, preflen-1,
1247 stats_fields[i].name);
1252 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
1253 stats_fields[i].name+preflen, preflen ? 0 : 4, "",
1254 *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s));
1257 printf(" hop_count_buckets:");
1258 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
1259 printf(" %d", s->hop_count_bucket[i]);
1262 printf(" lock_buckets:");
1263 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
1264 printf(" %d", s->locks.buckets[i]);
1267 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1268 "locks_latency MIN/AVG/MAX",
1269 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
1270 s->locks.latency.max, s->locks.latency.num);
1272 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1273 "reclock_ctdbd MIN/AVG/MAX",
1274 s->reclock.ctdbd.min, LATENCY_AVG(s->reclock.ctdbd),
1275 s->reclock.ctdbd.max, s->reclock.ctdbd.num);
1277 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1278 "reclock_recd MIN/AVG/MAX",
1279 s->reclock.recd.min, LATENCY_AVG(s->reclock.recd),
1280 s->reclock.recd.max, s->reclock.recd.num);
1282 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1283 "call_latency MIN/AVG/MAX",
1284 s->call_latency.min, LATENCY_AVG(s->call_latency),
1285 s->call_latency.max, s->call_latency.num);
1287 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1288 "childwrite_latency MIN/AVG/MAX",
1289 s->childwrite_latency.min,
1290 LATENCY_AVG(s->childwrite_latency),
1291 s->childwrite_latency.max, s->childwrite_latency.num);
1294 static int control_statistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1295 int argc, const char **argv)
1297 struct ctdb_statistics *stats;
1301 usage("statistics");
1304 ret = ctdb_ctrl_statistics(mem_ctx, ctdb->ev, ctdb->client,
1305 ctdb->cmd_pnn, TIMEOUT(), &stats);
1310 if (options.machinereadable) {
1311 print_statistics_machine(stats, true);
1313 print_statistics(stats);
1319 static int control_statistics_reset(TALLOC_CTX *mem_ctx,
1320 struct ctdb_context *ctdb,
1321 int argc, const char **argv)
1326 usage("statisticsreset");
1329 ret = ctdb_ctrl_statistics_reset(mem_ctx, ctdb->ev, ctdb->client,
1330 ctdb->cmd_pnn, TIMEOUT());
1338 static int control_stats(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1339 int argc, const char **argv)
1341 struct ctdb_statistics_list *slist;
1342 int ret, count = 0, i;
1343 bool show_header = true;
1350 count = atoi(argv[0]);
1353 ret = ctdb_ctrl_get_stat_history(mem_ctx, ctdb->ev, ctdb->client,
1354 ctdb->cmd_pnn, TIMEOUT(), &slist);
1359 for (i=0; i<slist->num; i++) {
1360 if (slist->stats[i].statistics_start_time.tv_sec == 0) {
1363 if (options.machinereadable == 1) {
1364 print_statistics_machine(&slist->stats[i],
1366 show_header = false;
1368 print_statistics(&slist->stats[i]);
1370 if (count > 0 && i == count) {
1378 static int ctdb_public_ip_cmp(const void *a, const void *b)
1380 const struct ctdb_public_ip *ip_a = a;
1381 const struct ctdb_public_ip *ip_b = b;
1383 return ctdb_sock_addr_cmp(&ip_a->addr, &ip_b->addr);
1386 static void print_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1387 struct ctdb_public_ip_list *ips,
1388 struct ctdb_public_ip_info **ipinfo,
1392 char *conf, *avail, *active;
1394 if (options.machinereadable == 1) {
1395 printf("%s%s%s%s%s", options.sep,
1396 "Public IP", options.sep,
1397 "Node", options.sep);
1398 if (options.verbose == 1) {
1399 printf("%s%s%s%s%s%s\n",
1400 "ActiveInterfaces", options.sep,
1401 "AvailableInterfaces", options.sep,
1402 "ConfiguredInterfaces", options.sep);
1408 printf("Public IPs on ALL nodes\n");
1410 printf("Public IPs on node %u\n", ctdb->cmd_pnn);
1414 for (i = 0; i < ips->num; i++) {
1416 if (options.machinereadable == 1) {
1417 printf("%s%s%s%d%s", options.sep,
1418 ctdb_sock_addr_to_string(
1419 mem_ctx, &ips->ip[i].addr),
1421 (int)ips->ip[i].pnn, options.sep);
1423 printf("%s", ctdb_sock_addr_to_string(
1424 mem_ctx, &ips->ip[i].addr));
1427 if (options.verbose == 0) {
1428 if (options.machinereadable == 1) {
1431 printf(" %d\n", (int)ips->ip[i].pnn);
1440 if (ipinfo[i] == NULL) {
1444 for (j=0; j<ipinfo[i]->ifaces->num; j++) {
1445 struct ctdb_iface *iface;
1447 iface = &ipinfo[i]->ifaces->iface[j];
1449 conf = talloc_strdup(mem_ctx, iface->name);
1451 conf = talloc_asprintf_append(
1452 conf, ",%s", iface->name);
1455 if (ipinfo[i]->active_idx == j) {
1456 active = iface->name;
1459 if (iface->link_state == 0) {
1463 if (avail == NULL) {
1464 avail = talloc_strdup(mem_ctx, iface->name);
1466 avail = talloc_asprintf_append(
1467 avail, ",%s", iface->name);
1473 if (options.machinereadable == 1) {
1474 printf("%s%s%s%s%s%s\n",
1475 active ? active : "", options.sep,
1476 avail ? avail : "", options.sep,
1477 conf ? conf : "", options.sep);
1479 printf(" node[%u] active[%s] available[%s] configured[%s]\n",
1480 ips->ip[i].pnn, active ? active : "",
1481 avail ? avail : "", conf ? conf : "");
1486 static int collect_ips(uint8_t *keybuf, size_t keylen, uint8_t *databuf,
1487 size_t datalen, void *private_data)
1489 struct ctdb_public_ip_list *ips = talloc_get_type_abort(
1490 private_data, struct ctdb_public_ip_list);
1491 struct ctdb_public_ip *ip;
1493 ip = (struct ctdb_public_ip *)databuf;
1494 ips->ip[ips->num] = *ip;
1500 static int get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
1501 struct ctdb_public_ip_list **out)
1503 struct ctdb_node_map *nodemap;
1504 struct ctdb_public_ip_list *ips;
1505 struct db_hash_context *ipdb;
1507 int ret, count, i, j;
1509 nodemap = get_nodemap(ctdb, false);
1510 if (nodemap == NULL) {
1514 ret = db_hash_init(mem_ctx, "ips", 101, DB_HASH_COMPLEX, &ipdb);
1519 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
1525 for (i=0; i<count; i++) {
1526 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1527 pnn_list[i], TIMEOUT(), &ips);
1532 for (j=0; j<ips->num; j++) {
1533 struct ctdb_public_ip ip;
1535 ip.pnn = ips->ip[j].pnn;
1536 ip.addr = ips->ip[j].addr;
1538 ret = db_hash_add(ipdb, (uint8_t *)&ip.addr,
1540 (uint8_t *)&ip, sizeof(ip));
1549 talloc_free(pnn_list);
1551 ret = db_hash_traverse(ipdb, NULL, NULL, &count);
1556 ips = talloc_zero(mem_ctx, struct ctdb_public_ip_list);
1561 ips->ip = talloc_array(ips, struct ctdb_public_ip, count);
1562 if (ips->ip == NULL) {
1566 ret = db_hash_traverse(ipdb, collect_ips, ips, &count);
1571 if (count != ips->num) {
1585 static int control_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1586 int argc, const char **argv)
1588 struct ctdb_public_ip_list *ips;
1589 struct ctdb_public_ip_info **ipinfo;
1591 bool do_all = false;
1598 if (strcmp(argv[0], "all") == 0) {
1606 ret = get_all_public_ips(ctdb, mem_ctx, &ips);
1608 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1609 ctdb->cmd_pnn, TIMEOUT(), &ips);
1615 qsort(ips->ip, ips->num, sizeof(struct ctdb_public_ip),
1616 ctdb_public_ip_cmp);
1618 ipinfo = talloc_array(mem_ctx, struct ctdb_public_ip_info *, ips->num);
1619 if (ipinfo == NULL) {
1623 for (i=0; i<ips->num; i++) {
1626 pnn = ips->ip[i].pnn;
1628 pnn = ctdb->cmd_pnn;
1630 if (pnn == CTDB_UNKNOWN_PNN) {
1634 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev,
1636 TIMEOUT(), &ips->ip[i].addr,
1643 print_ip(mem_ctx, ctdb, ips, ipinfo, do_all);
1647 static int control_ipinfo(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1648 int argc, const char **argv)
1650 struct ctdb_public_ip_info *ipinfo;
1651 ctdb_sock_addr addr;
1658 if (! parse_ip(argv[0], NULL, 0, &addr)) {
1659 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
1663 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev, ctdb->client,
1664 ctdb->cmd_pnn, TIMEOUT(), &addr,
1668 printf("Node %u does not know about IP %s\n",
1669 ctdb->cmd_pnn, argv[0]);
1674 printf("Public IP[%s] info on node %u\n",
1675 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1678 printf("IP:%s\nCurrentNode:%u\nNumInterfaces:%u\n",
1679 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1680 ipinfo->ip.pnn, ipinfo->ifaces->num);
1682 for (i=0; i<ipinfo->ifaces->num; i++) {
1683 struct ctdb_iface *iface;
1685 iface = &ipinfo->ifaces->iface[i];
1686 iface->name[CTDB_IFACE_SIZE] = '\0';
1687 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
1689 iface->link_state == 0 ? "down" : "up",
1691 (i == ipinfo->active_idx) ? " (active)" : "");
1697 static int control_ifaces(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1698 int argc, const char **argv)
1700 struct ctdb_iface_list *ifaces;
1707 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1708 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1713 if (ifaces->num == 0) {
1714 printf("No interfaces configured on node %u\n",
1719 if (options.machinereadable) {
1720 printf("%s%s%s%s%s%s%s\n", options.sep,
1721 "Name", options.sep,
1722 "LinkStatus", options.sep,
1723 "References", options.sep);
1725 printf("Interfaces on node %u\n", ctdb->cmd_pnn);
1728 for (i=0; i<ifaces->num; i++) {
1729 if (options.machinereadable) {
1730 printf("%s%s%s%u%s%u%s\n", options.sep,
1731 ifaces->iface[i].name, options.sep,
1732 ifaces->iface[i].link_state, options.sep,
1733 ifaces->iface[i].references, options.sep);
1735 printf("name:%s link:%s references:%u\n",
1736 ifaces->iface[i].name,
1737 ifaces->iface[i].link_state ? "up" : "down",
1738 ifaces->iface[i].references);
1745 static int control_setifacelink(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1746 int argc, const char **argv)
1748 struct ctdb_iface_list *ifaces;
1749 struct ctdb_iface *iface;
1753 usage("setifacelink");
1756 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
1757 fprintf(stderr, "Interface name '%s' too long\n", argv[0]);
1761 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1762 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1765 "Failed to get interface information from node %u\n",
1771 for (i=0; i<ifaces->num; i++) {
1772 if (strcmp(ifaces->iface[i].name, argv[0]) == 0) {
1773 iface = &ifaces->iface[i];
1778 if (iface == NULL) {
1779 printf("Interface %s not configured on node %u\n",
1780 argv[0], ctdb->cmd_pnn);
1784 if (strcmp(argv[1], "up") == 0) {
1785 iface->link_state = 1;
1786 } else if (strcmp(argv[1], "down") == 0) {
1787 iface->link_state = 0;
1789 usage("setifacelink");
1793 iface->references = 0;
1795 ret = ctdb_ctrl_set_iface_link_state(mem_ctx, ctdb->ev, ctdb->client,
1796 ctdb->cmd_pnn, TIMEOUT(), iface);
1804 static int control_process_exists(TALLOC_CTX *mem_ctx,
1805 struct ctdb_context *ctdb,
1806 int argc, const char **argv)
1812 usage("process-exists");
1815 pid = atoi(argv[0]);
1816 ret = ctdb_ctrl_process_exists(mem_ctx, ctdb->ev, ctdb->client,
1817 ctdb->cmd_pnn, TIMEOUT(), pid, &status);
1823 printf("PID %u exists\n", pid);
1825 printf("PID %u does not exist\n", pid);
1830 static int control_getdbmap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1831 int argc, const char **argv)
1833 struct ctdb_dbid_map *dbmap;
1840 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
1841 ctdb->cmd_pnn, TIMEOUT(), &dbmap);
1846 if (options.machinereadable == 1) {
1847 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1850 "Name", options.sep,
1851 "Path", options.sep,
1852 "Persistent", options.sep,
1853 "Sticky", options.sep,
1854 "Unhealthy", options.sep,
1855 "Readonly", options.sep);
1857 printf("Number of databases:%d\n", dbmap->num);
1860 for (i=0; i<dbmap->num; i++) {
1869 db_id = dbmap->dbs[i].db_id;
1871 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
1872 ctdb->cmd_pnn, TIMEOUT(), db_id,
1878 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1879 ctdb->cmd_pnn, TIMEOUT(), db_id,
1885 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1886 ctdb->cmd_pnn, TIMEOUT(), db_id,
1892 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
1893 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
1894 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
1896 if (options.machinereadable == 1) {
1897 printf("%s0x%08X%s%s%s%s%s%d%s%d%s%d%s%d%s\n",
1902 !! (persistent), options.sep,
1903 !! (sticky), options.sep,
1904 !! (health), options.sep,
1905 !! (readonly), options.sep);
1907 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
1909 persistent ? " PERSISTENT" : "",
1910 sticky ? " STICKY" : "",
1911 readonly ? " READONLY" : "",
1912 health ? " UNHEALTHY" : "");
1915 talloc_free(discard_const(name));
1916 talloc_free(discard_const(path));
1917 talloc_free(discard_const(health));
1923 static int control_getdbstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1924 int argc, const char **argv)
1927 const char *db_name, *db_path, *db_health;
1932 usage("getdbstatus");
1935 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
1939 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1940 ctdb->cmd_pnn, TIMEOUT(), db_id,
1946 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1947 ctdb->cmd_pnn, TIMEOUT(), db_id,
1953 printf("dbid: 0x%08x\nname: %s\npath: %s\n", db_id, db_name, db_path);
1954 printf("PERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
1955 (db_flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
1956 (db_flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
1957 (db_flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
1958 (db_health ? db_health : "OK"));
1962 struct dump_record_state {
1966 #define ISASCII(x) (isprint(x) && ! strchr("\"\\", (x)))
1968 static void dump_tdb_data(const char *name, TDB_DATA val)
1972 fprintf(stdout, "%s(%zu) = \"", name, val.dsize);
1973 for (i=0; i<val.dsize; i++) {
1974 if (ISASCII(val.dptr[i])) {
1975 fprintf(stdout, "%c", val.dptr[i]);
1977 fprintf(stdout, "\\%02X", val.dptr[i]);
1980 fprintf(stdout, "\"\n");
1983 static void dump_ltdb_header(struct ctdb_ltdb_header *header)
1985 fprintf(stdout, "dmaster: %u\n", header->dmaster);
1986 fprintf(stdout, "rsn: %" PRIu64 "\n", header->rsn);
1987 fprintf(stdout, "flags: 0x%08x", header->flags);
1988 if (header->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
1989 fprintf(stdout, " MIGRATED_WITH_DATA");
1991 if (header->flags & CTDB_REC_FLAG_VACUUM_MIGRATED) {
1992 fprintf(stdout, " VACUUM_MIGRATED");
1994 if (header->flags & CTDB_REC_FLAG_AUTOMATIC) {
1995 fprintf(stdout, " AUTOMATIC");
1997 if (header->flags & CTDB_REC_RO_HAVE_DELEGATIONS) {
1998 fprintf(stdout, " RO_HAVE_DELEGATIONS");
2000 if (header->flags & CTDB_REC_RO_HAVE_READONLY) {
2001 fprintf(stdout, " RO_HAVE_READONLY");
2003 if (header->flags & CTDB_REC_RO_REVOKING_READONLY) {
2004 fprintf(stdout, " RO_REVOKING_READONLY");
2006 if (header->flags & CTDB_REC_RO_REVOKE_COMPLETE) {
2007 fprintf(stdout, " RO_REVOKE_COMPLETE");
2009 fprintf(stdout, "\n");
2013 static int dump_record(uint32_t reqid, struct ctdb_ltdb_header *header,
2014 TDB_DATA key, TDB_DATA data, void *private_data)
2016 struct dump_record_state *state =
2017 (struct dump_record_state *)private_data;
2021 dump_tdb_data("key", key);
2022 dump_ltdb_header(header);
2023 dump_tdb_data("data", data);
2024 fprintf(stdout, "\n");
2029 struct traverse_state {
2030 TALLOC_CTX *mem_ctx;
2032 ctdb_rec_parser_func_t func;
2033 struct dump_record_state sub_state;
2036 static void traverse_handler(uint64_t srvid, TDB_DATA data, void *private_data)
2038 struct traverse_state *state = (struct traverse_state *)private_data;
2039 struct ctdb_rec_data *rec;
2040 struct ctdb_ltdb_header header;
2043 ret = ctdb_rec_data_pull(data.dptr, data.dsize, state->mem_ctx, &rec);
2048 if (rec->key.dsize == 0 && rec->data.dsize == 0) {
2050 /* end of traverse */
2055 ret = ctdb_ltdb_header_extract(&rec->data, &header);
2061 if (rec->data.dsize == 0) {
2066 ret = state->func(rec->reqid, &header, rec->key, rec->data,
2074 static int control_catdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2075 int argc, const char **argv)
2077 struct ctdb_db_context *db;
2078 const char *db_name;
2081 struct ctdb_traverse_start_ext traverse;
2082 struct traverse_state state;
2089 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2093 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2096 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2101 ZERO_STRUCT(traverse);
2103 traverse.db_id = db_id;
2105 traverse.srvid = next_srvid(ctdb);
2106 traverse.withemptyrecords = false;
2108 state.mem_ctx = mem_ctx;
2110 state.func = dump_record;
2111 state.sub_state.count = 0;
2113 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2115 traverse_handler, &state);
2120 ret = ctdb_ctrl_traverse_start_ext(mem_ctx, ctdb->ev, ctdb->client,
2121 ctdb->cmd_pnn, TIMEOUT(),
2127 ctdb_client_wait(ctdb->ev, &state.done);
2129 printf("Dumped %u records\n", state.sub_state.count);
2131 ret = ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2132 traverse.srvid, &state);
2140 static int control_cattdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2141 int argc, const char **argv)
2143 struct ctdb_db_context *db;
2144 const char *db_name;
2147 struct dump_record_state state;
2154 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2158 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2161 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2166 ret = ctdb_db_traverse(db, true, true, dump_record, &state);
2168 printf("Dumped %u record(s)\n", state.count);
2173 static int control_getmonmode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2174 int argc, const char **argv)
2179 usage("getmonmode");
2182 ret = ctdb_ctrl_get_monmode(mem_ctx, ctdb->ev, ctdb->client,
2183 ctdb->cmd_pnn, TIMEOUT(), &mode);
2189 (mode == CTDB_MONITORING_ENABLED) ? "ENABLED" : "DISABLED");
2193 static int control_getcapabilities(TALLOC_CTX *mem_ctx,
2194 struct ctdb_context *ctdb,
2195 int argc, const char **argv)
2201 usage("getcapabilities");
2204 ret = ctdb_ctrl_get_capabilities(mem_ctx, ctdb->ev, ctdb->client,
2205 ctdb->cmd_pnn, TIMEOUT(), &caps);
2210 if (options.machinereadable == 1) {
2211 printf("%s%s%s%s%s\n",
2213 "RECMASTER", options.sep,
2214 "LMASTER", options.sep);
2215 printf("%s%d%s%d%s\n", options.sep,
2216 !! (caps & CTDB_CAP_RECMASTER), options.sep,
2217 !! (caps & CTDB_CAP_LMASTER), options.sep);
2219 printf("RECMASTER: %s\n",
2220 (caps & CTDB_CAP_RECMASTER) ? "YES" : "NO");
2221 printf("LMASTER: %s\n",
2222 (caps & CTDB_CAP_LMASTER) ? "YES" : "NO");
2228 static int control_pnn(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2229 int argc, const char **argv)
2231 printf("%u\n", ctdb_client_pnn(ctdb->client));
2235 static int control_lvs(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2236 int argc, const char **argv)
2238 char *t, *lvs_helper = NULL;
2244 t = getenv("CTDB_LVS_HELPER");
2246 lvs_helper = talloc_strdup(mem_ctx, t);
2248 lvs_helper = talloc_asprintf(mem_ctx, "%s/ctdb_lvs",
2249 CTDB_HELPER_BINDIR);
2252 if (lvs_helper == NULL) {
2253 fprintf(stderr, "Unable to set LVS helper\n");
2257 return run_helper("LVS helper", lvs_helper, argv[0]);
2260 static int control_disable_monitor(TALLOC_CTX *mem_ctx,
2261 struct ctdb_context *ctdb,
2262 int argc, const char **argv)
2267 usage("disablemonitor");
2270 ret = ctdb_ctrl_disable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2271 ctdb->cmd_pnn, TIMEOUT());
2279 static int control_enable_monitor(TALLOC_CTX *mem_ctx,
2280 struct ctdb_context *ctdb,
2281 int argc, const char **argv)
2286 usage("enablemonitor");
2289 ret = ctdb_ctrl_enable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2290 ctdb->cmd_pnn, TIMEOUT());
2298 static int control_setdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2299 int argc, const char **argv)
2309 found = debug_level_parse(argv[0], &log_level);
2312 "Invalid debug level '%s'. Valid levels are:\n",
2314 fprintf(stderr, "\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
2318 ret = ctdb_ctrl_setdebug(mem_ctx, ctdb->ev, ctdb->client,
2319 ctdb->cmd_pnn, TIMEOUT(), log_level);
2327 static int control_getdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2328 int argc, const char **argv)
2331 const char *log_str;
2338 ret = ctdb_ctrl_getdebug(mem_ctx, ctdb->ev, ctdb->client,
2339 ctdb->cmd_pnn, TIMEOUT(), &loglevel);
2344 log_str = debug_level_to_string(loglevel);
2345 printf("%s\n", log_str);
2350 static int control_attach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2351 int argc, const char **argv)
2353 const char *db_name;
2354 uint8_t db_flags = 0;
2357 if (argc < 1 || argc > 2) {
2363 if (strcmp(argv[1], "persistent") == 0) {
2364 db_flags = CTDB_DB_FLAGS_PERSISTENT;
2365 } else if (strcmp(argv[1], "readonly") == 0) {
2366 db_flags = CTDB_DB_FLAGS_READONLY;
2367 } else if (strcmp(argv[1], "sticky") == 0) {
2368 db_flags = CTDB_DB_FLAGS_STICKY;
2374 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2383 static int control_detach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2384 int argc, const char **argv)
2386 const char *db_name;
2389 struct ctdb_node_map *nodemap;
2397 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2398 ctdb->cmd_pnn, TIMEOUT(), &recmode);
2403 if (recmode == CTDB_RECOVERY_ACTIVE) {
2404 fprintf(stderr, "Database cannot be detached"
2405 " when recovery is active\n");
2409 nodemap = get_nodemap(ctdb, false);
2410 if (nodemap == NULL) {
2414 for (i=0; i<nodemap->num; i++) {
2417 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
2420 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
2423 if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
2424 fprintf(stderr, "Database cannot be detached on"
2425 " inactive (stopped or banned) node %u\n",
2426 nodemap->node[i].pnn);
2430 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
2431 nodemap->node[i].pnn, TIMEOUT(),
2432 "AllowClientDBAttach", &value);
2435 "Unable to get tunable AllowClientDBAttach"
2436 " from node %u\n", nodemap->node[i].pnn);
2442 "Database access is still active on node %u."
2443 " Set AllowclientDBAttach=0 on all nodes.\n",
2444 nodemap->node[i].pnn);
2450 for (i=0; i<argc; i++) {
2451 if (! db_exists(mem_ctx, ctdb, argv[i], &db_id, &db_name,
2456 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
2458 "Persistent database %s cannot be detached\n",
2463 ret = ctdb_detach(mem_ctx, ctdb->ev, ctdb->client,
2466 fprintf(stderr, "Database %s detach failed\n", db_name);
2474 static int control_dumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2475 int argc, const char **argv)
2477 const char *mem_str;
2481 ret = ctdb_ctrl_dump_memory(mem_ctx, ctdb->ev, ctdb->client,
2482 ctdb->cmd_pnn, TIMEOUT(), &mem_str);
2487 n = write(1, mem_str, strlen(mem_str)+1);
2488 if (n < 0 || n != strlen(mem_str)+1) {
2489 fprintf(stderr, "Failed to write talloc summary\n");
2496 static void dump_memory(uint64_t srvid, TDB_DATA data, void *private_data)
2498 bool *done = (bool *)private_data;
2501 n = write(1, data.dptr, data.dsize);
2502 if (n < 0 || n != data.dsize) {
2503 fprintf(stderr, "Failed to write talloc summary\n");
2509 static int control_rddumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2510 int argc, const char **argv)
2512 struct ctdb_srvid_message msg = { 0 };
2516 msg.pnn = ctdb->pnn;
2517 msg.srvid = next_srvid(ctdb);
2519 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2520 msg.srvid, dump_memory, &done);
2525 ret = ctdb_message_mem_dump(mem_ctx, ctdb->ev, ctdb->client,
2526 ctdb->cmd_pnn, &msg);
2531 ctdb_client_wait(ctdb->ev, &done);
2535 static int control_getpid(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2536 int argc, const char **argv)
2541 ret = ctdb_ctrl_get_pid(mem_ctx, ctdb->ev, ctdb->client,
2542 ctdb->cmd_pnn, TIMEOUT(), &pid);
2547 printf("%u\n", pid);
2551 static int check_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2552 const char *desc, uint32_t flag, bool set_flag)
2554 struct ctdb_node_map *nodemap;
2557 nodemap = get_nodemap(ctdb, false);
2558 if (nodemap == NULL) {
2562 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2563 if (set_flag == flag_is_set) {
2565 fprintf(stderr, "Node %u is already %s\n",
2566 ctdb->cmd_pnn, desc);
2568 fprintf(stderr, "Node %u is not %s\n",
2569 ctdb->cmd_pnn, desc);
2577 static void wait_for_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2578 uint32_t flag, bool set_flag)
2580 struct ctdb_node_map *nodemap;
2584 nodemap = get_nodemap(ctdb, true);
2585 if (nodemap == NULL) {
2587 "Failed to get nodemap, trying again\n");
2592 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2593 if (flag_is_set == set_flag) {
2601 struct ipreallocate_state {
2606 static void ipreallocate_handler(uint64_t srvid, TDB_DATA data,
2609 struct ipreallocate_state *state =
2610 (struct ipreallocate_state *)private_data;
2612 if (data.dsize != sizeof(int)) {
2617 state->status = *(int *)data.dptr;
2621 static int ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb)
2623 struct ctdb_srvid_message msg = { 0 };
2624 struct ipreallocate_state state;
2627 msg.pnn = ctdb->pnn;
2628 msg.srvid = next_srvid(ctdb);
2631 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2633 ipreallocate_handler, &state);
2639 ret = ctdb_message_takeover_run(mem_ctx, ctdb->ev,
2641 CTDB_BROADCAST_CONNECTED,
2647 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done,
2653 if (state.status >= 0) {
2662 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2667 static int control_disable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2668 int argc, const char **argv)
2676 ret = check_flags(mem_ctx, ctdb, "disabled",
2677 NODE_FLAGS_PERMANENTLY_DISABLED, true);
2682 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2683 ctdb->cmd_pnn, TIMEOUT(),
2684 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2687 "Failed to set DISABLED flag on node %u\n",
2692 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, true);
2693 return ipreallocate(mem_ctx, ctdb);
2696 static int control_enable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2697 int argc, const char **argv)
2705 ret = check_flags(mem_ctx, ctdb, "disabled",
2706 NODE_FLAGS_PERMANENTLY_DISABLED, false);
2711 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2712 ctdb->cmd_pnn, TIMEOUT(),
2713 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2715 fprintf(stderr, "Failed to reset DISABLED flag on node %u\n",
2720 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, false);
2721 return ipreallocate(mem_ctx, ctdb);
2724 static int control_stop(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2725 int argc, const char **argv)
2733 ret = check_flags(mem_ctx, ctdb, "stopped",
2734 NODE_FLAGS_STOPPED, true);
2739 ret = ctdb_ctrl_stop_node(mem_ctx, ctdb->ev, ctdb->client,
2740 ctdb->cmd_pnn, TIMEOUT());
2742 fprintf(stderr, "Failed to stop node %u\n", ctdb->cmd_pnn);
2746 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, true);
2747 return ipreallocate(mem_ctx, ctdb);
2750 static int control_continue(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2751 int argc, const char **argv)
2759 ret = check_flags(mem_ctx, ctdb, "stopped",
2760 NODE_FLAGS_STOPPED, false);
2765 ret = ctdb_ctrl_continue_node(mem_ctx, ctdb->ev, ctdb->client,
2766 ctdb->cmd_pnn, TIMEOUT());
2768 fprintf(stderr, "Failed to continue stopped node %u\n",
2773 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, false);
2774 return ipreallocate(mem_ctx, ctdb);
2777 static int control_ban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2778 int argc, const char **argv)
2780 struct ctdb_ban_state ban_state;
2787 ret = check_flags(mem_ctx, ctdb, "banned",
2788 NODE_FLAGS_BANNED, true);
2793 ban_state.pnn = ctdb->cmd_pnn;
2794 ban_state.time = strtoul(argv[0], NULL, 0);
2796 if (ban_state.time == 0) {
2797 fprintf(stderr, "Ban time cannot be zero\n");
2801 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2802 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2804 fprintf(stderr, "Failed to ban node %u\n", ctdb->cmd_pnn);
2808 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, true);
2809 return ipreallocate(mem_ctx, ctdb);
2813 static int control_unban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2814 int argc, const char **argv)
2816 struct ctdb_ban_state ban_state;
2823 ret = check_flags(mem_ctx, ctdb, "banned",
2824 NODE_FLAGS_BANNED, false);
2829 ban_state.pnn = ctdb->cmd_pnn;
2832 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2833 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2835 fprintf(stderr, "Failed to unban node %u\n", ctdb->cmd_pnn);
2839 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, false);
2840 return ipreallocate(mem_ctx, ctdb);
2844 static int control_shutdown(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2845 int argc, const char **argv)
2853 ret = ctdb_ctrl_shutdown(mem_ctx, ctdb->ev, ctdb->client,
2854 ctdb->cmd_pnn, TIMEOUT());
2856 fprintf(stderr, "Unable to shutdown node %u\n", ctdb->cmd_pnn);
2863 static int get_generation(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2864 uint32_t *generation)
2868 struct ctdb_vnn_map *vnnmap;
2872 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2873 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
2875 fprintf(stderr, "Failed to find recovery master\n");
2879 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2880 recmaster, TIMEOUT(), &recmode);
2882 fprintf(stderr, "Failed to get recovery mode from node %u\n",
2887 if (recmode == CTDB_RECOVERY_ACTIVE) {
2892 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
2893 recmaster, TIMEOUT(), &vnnmap);
2895 fprintf(stderr, "Failed to get generation from node %u\n",
2900 if (vnnmap->generation == INVALID_GENERATION) {
2901 talloc_free(vnnmap);
2906 *generation = vnnmap->generation;
2907 talloc_free(vnnmap);
2912 static int control_recover(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2913 int argc, const char **argv)
2915 uint32_t generation, next_generation;
2922 ret = get_generation(mem_ctx, ctdb, &generation);
2927 ret = ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
2928 ctdb->cmd_pnn, TIMEOUT(),
2929 CTDB_RECOVERY_ACTIVE);
2931 fprintf(stderr, "Failed to set recovery mode active\n");
2936 ret = get_generation(mem_ctx, ctdb, &next_generation);
2939 "Failed to confirm end of recovery\n");
2943 if (next_generation != generation) {
2953 static int control_ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2954 int argc, const char **argv)
2957 usage("ipreallocate");
2960 return ipreallocate(mem_ctx, ctdb);
2963 static int control_isnotrecmaster(TALLOC_CTX *mem_ctx,
2964 struct ctdb_context *ctdb,
2965 int argc, const char **argv)
2971 usage("isnotrecmaster");
2974 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2975 ctdb->pnn, TIMEOUT(), &recmaster);
2977 fprintf(stderr, "Failed to get recmaster\n");
2981 if (recmaster != ctdb->pnn) {
2982 printf("this node is not the recmaster\n");
2986 printf("this node is the recmaster\n");
2990 static int control_gratarp(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2991 int argc, const char **argv)
2993 struct ctdb_addr_info addr_info;
3000 if (! parse_ip(argv[0], NULL, 0, &addr_info.addr)) {
3001 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3004 addr_info.iface = argv[1];
3006 ret = ctdb_ctrl_send_gratuitous_arp(mem_ctx, ctdb->ev, ctdb->client,
3007 ctdb->cmd_pnn, TIMEOUT(),
3010 fprintf(stderr, "Unable to send gratuitous arp from node %u\n",
3018 static int control_tickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3019 int argc, const char **argv)
3021 ctdb_sock_addr src, dst;
3024 if (argc != 0 && argc != 2) {
3029 struct ctdb_connection *clist;
3033 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3039 for (i=0; i<count; i++) {
3040 ret = ctdb_sys_send_tcp(&clist[i].src,
3050 if (num_failed > 0) {
3051 fprintf(stderr, "Failed to send %d tickles\n",
3060 if (! parse_ip_port(argv[0], &src)) {
3061 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3065 if (! parse_ip_port(argv[1], &dst)) {
3066 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3070 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
3072 fprintf(stderr, "Failed to send tickle ack\n");
3079 static int control_gettickles(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3080 int argc, const char **argv)
3082 ctdb_sock_addr addr;
3083 struct ctdb_tickle_list *tickles;
3087 if (argc < 1 || argc > 2) {
3088 usage("gettickles");
3092 port = strtoul(argv[1], NULL, 10);
3095 if (! parse_ip(argv[0], NULL, port, &addr)) {
3096 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3100 ret = ctdb_ctrl_get_tcp_tickle_list(mem_ctx, ctdb->ev, ctdb->client,
3101 ctdb->cmd_pnn, TIMEOUT(), &addr,
3104 fprintf(stderr, "Failed to get list of connections\n");
3108 if (options.machinereadable) {
3109 printf("%s%s%s%s%s%s%s%s%s\n",
3111 "Source IP", options.sep,
3112 "Port", options.sep,
3113 "Destiation IP", options.sep,
3114 "Port", options.sep);
3115 for (i=0; i<tickles->num; i++) {
3116 printf("%s%s%s%u%s%s%s%u%s\n", options.sep,
3117 ctdb_sock_addr_to_string(
3118 mem_ctx, &tickles->conn[i].src),
3120 ntohs(tickles->conn[i].src.ip.sin_port),
3122 ctdb_sock_addr_to_string(
3123 mem_ctx, &tickles->conn[i].dst),
3125 ntohs(tickles->conn[i].dst.ip.sin_port),
3129 printf("Connections for IP: %s\n",
3130 ctdb_sock_addr_to_string(mem_ctx, &tickles->addr));
3131 printf("Num connections: %u\n", tickles->num);
3132 for (i=0; i<tickles->num; i++) {
3133 printf("SRC: %s:%u DST: %s:%u\n",
3134 ctdb_sock_addr_to_string(
3135 mem_ctx, &tickles->conn[i].src),
3136 ntohs(tickles->conn[i].src.ip.sin_port),
3137 ctdb_sock_addr_to_string(
3138 mem_ctx, &tickles->conn[i].dst),
3139 ntohs(tickles->conn[i].dst.ip.sin_port));
3143 talloc_free(tickles);
3147 typedef void (*clist_request_func)(struct ctdb_req_control *request,
3148 struct ctdb_connection *conn);
3150 typedef int (*clist_reply_func)(struct ctdb_reply_control *reply);
3152 struct process_clist_state {
3153 struct ctdb_connection *clist;
3155 int num_failed, num_total;
3156 clist_reply_func reply_func;
3159 static void process_clist_done(struct tevent_req *subreq);
3161 static struct tevent_req *process_clist_send(
3162 TALLOC_CTX *mem_ctx,
3163 struct ctdb_context *ctdb,
3164 struct ctdb_connection *clist,
3166 clist_request_func request_func,
3167 clist_reply_func reply_func)
3169 struct tevent_req *req, *subreq;
3170 struct process_clist_state *state;
3171 struct ctdb_req_control request;
3174 req = tevent_req_create(mem_ctx, &state, struct process_clist_state);
3179 state->clist = clist;
3180 state->count = count;
3181 state->reply_func = reply_func;
3183 for (i=0; i<count; i++) {
3184 request_func(&request, &clist[i]);
3185 subreq = ctdb_client_control_send(state, ctdb->ev,
3186 ctdb->client, ctdb->cmd_pnn,
3187 TIMEOUT(), &request);
3188 if (tevent_req_nomem(subreq, req)) {
3189 return tevent_req_post(req, ctdb->ev);
3191 tevent_req_set_callback(subreq, process_clist_done, req);
3197 static void process_clist_done(struct tevent_req *subreq)
3199 struct tevent_req *req = tevent_req_callback_data(
3200 subreq, struct tevent_req);
3201 struct process_clist_state *state = tevent_req_data(
3202 req, struct process_clist_state);
3203 struct ctdb_reply_control *reply;
3207 status = ctdb_client_control_recv(subreq, NULL, state, &reply);
3208 TALLOC_FREE(subreq);
3210 state->num_failed += 1;
3214 ret = state->reply_func(reply);
3216 state->num_failed += 1;
3221 state->num_total += 1;
3222 if (state->num_total == state->count) {
3223 tevent_req_done(req);
3227 static int process_clist_recv(struct tevent_req *req)
3229 struct process_clist_state *state = tevent_req_data(
3230 req, struct process_clist_state);
3232 return state->num_failed;
3235 static int control_addtickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3236 int argc, const char **argv)
3238 struct ctdb_connection conn;
3241 if (argc != 0 && argc != 2) {
3246 struct ctdb_connection *clist;
3247 struct tevent_req *req;
3250 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3258 req = process_clist_send(mem_ctx, ctdb, clist, count,
3259 ctdb_req_control_tcp_add_delayed_update,
3260 ctdb_reply_control_tcp_add_delayed_update);
3266 tevent_req_poll(req, ctdb->ev);
3269 ret = process_clist_recv(req);
3271 fprintf(stderr, "Failed to add %d tickles\n", ret);
3278 if (! parse_ip_port(argv[0], &conn.src)) {
3279 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3282 if (! parse_ip_port(argv[1], &conn.dst)) {
3283 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3287 ret = ctdb_ctrl_tcp_add_delayed_update(mem_ctx, ctdb->ev,
3288 ctdb->client, ctdb->cmd_pnn,
3291 fprintf(stderr, "Failed to register connection\n");
3298 static int control_deltickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3299 int argc, const char **argv)
3301 struct ctdb_connection conn;
3304 if (argc != 0 && argc != 2) {
3309 struct ctdb_connection *clist;
3310 struct tevent_req *req;
3313 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3321 req = process_clist_send(mem_ctx, ctdb, clist, count,
3322 ctdb_req_control_tcp_remove,
3323 ctdb_reply_control_tcp_remove);
3329 tevent_req_poll(req, ctdb->ev);
3332 ret = process_clist_recv(req);
3334 fprintf(stderr, "Failed to remove %d tickles\n", ret);
3341 if (! parse_ip_port(argv[0], &conn.src)) {
3342 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3345 if (! parse_ip_port(argv[1], &conn.dst)) {
3346 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3350 ret = ctdb_ctrl_tcp_remove(mem_ctx, ctdb->ev, ctdb->client,
3351 ctdb->cmd_pnn, TIMEOUT(), &conn);
3353 fprintf(stderr, "Failed to unregister connection\n");
3360 static int control_check_srvids(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3361 int argc, const char **argv)
3368 usage("check_srvids");
3371 srvid = talloc_array(mem_ctx, uint64_t, argc);
3372 if (srvid == NULL) {
3373 fprintf(stderr, "Memory allocation error\n");
3377 for (i=0; i<argc; i++) {
3378 srvid[i] = strtoull(argv[i], NULL, 0);
3381 ret = ctdb_ctrl_check_srvids(mem_ctx, ctdb->ev, ctdb->client,
3382 ctdb->cmd_pnn, TIMEOUT(), srvid, argc,
3385 fprintf(stderr, "Failed to check srvids on node %u\n",
3390 for (i=0; i<argc; i++) {
3391 printf("SRVID 0x%" PRIx64 " %s\n", srvid[i],
3392 (result[i] ? "exists" : "does not exist"));
3398 static int control_listnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3399 int argc, const char **argv)
3401 struct ctdb_node_map *nodemap;
3408 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
3409 if (nodemap == NULL) {
3413 for (i=0; i<nodemap->num; i++) {
3414 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
3418 if (options.machinereadable) {
3419 printf("%s%u%s%s%s\n", options.sep,
3420 nodemap->node[i].pnn, options.sep,
3421 ctdb_sock_addr_to_string(
3422 mem_ctx, &nodemap->node[i].addr),
3426 ctdb_sock_addr_to_string(
3427 mem_ctx, &nodemap->node[i].addr));
3434 static bool nodemap_identical(struct ctdb_node_map *nodemap1,
3435 struct ctdb_node_map *nodemap2)
3439 if (nodemap1->num != nodemap2->num) {
3443 for (i=0; i<nodemap1->num; i++) {
3444 struct ctdb_node_and_flags *n1, *n2;
3446 n1 = &nodemap1->node[i];
3447 n2 = &nodemap2->node[i];
3449 if ((n1->pnn != n2->pnn) ||
3450 (n1->flags != n2->flags) ||
3451 ! ctdb_sock_addr_same_ip(&n1->addr, &n2->addr)) {
3459 static int check_node_file_changes(TALLOC_CTX *mem_ctx,
3460 struct ctdb_node_map *nm,
3461 struct ctdb_node_map *fnm,
3465 bool check_failed = false;
3469 for (i=0; i<nm->num; i++) {
3470 if (i >= fnm->num) {
3472 "Node %u (%s) missing from nodes file\n",
3474 ctdb_sock_addr_to_string(
3475 mem_ctx, &nm->node[i].addr));
3476 check_failed = true;
3479 if (nm->node[i].flags & NODE_FLAGS_DELETED &&
3480 fnm->node[i].flags & NODE_FLAGS_DELETED) {
3481 /* Node remains deleted */
3485 if (! (nm->node[i].flags & NODE_FLAGS_DELETED) &&
3486 ! (fnm->node[i].flags & NODE_FLAGS_DELETED)) {
3487 /* Node not newly nor previously deleted */
3488 if (! ctdb_same_ip(&nm->node[i].addr,
3489 &fnm->node[i].addr)) {
3491 "Node %u has changed IP address"
3492 " (was %s, now %s)\n",
3494 ctdb_sock_addr_to_string(
3495 mem_ctx, &nm->node[i].addr),
3496 ctdb_sock_addr_to_string(
3497 mem_ctx, &fnm->node[i].addr));
3498 check_failed = true;
3500 if (nm->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3502 "WARNING: Node %u is disconnected."
3503 " You MUST fix this node manually!\n",
3510 if (fnm->node[i].flags & NODE_FLAGS_DELETED) {
3511 /* Node is being deleted */
3512 printf("Node %u is DELETED\n", nm->node[i].pnn);
3514 if (! (nm->node[i].flags & NODE_FLAGS_DISCONNECTED)) {
3516 "ERROR: Node %u is still connected\n",
3518 check_failed = true;
3523 if (nm->node[i].flags & NODE_FLAGS_DELETED) {
3524 /* Node was previously deleted */
3525 printf("Node %u is UNDELETED\n", nm->node[i].pnn);
3532 "ERROR: Nodes will not be reloaded due to previous error\n");
3536 /* Leftover nodes in file are NEW */
3537 for (; i < fnm->num; i++) {
3538 printf("Node %u is NEW\n", fnm->node[i].pnn);
3545 struct disable_recoveries_state {
3553 static void disable_recoveries_handler(uint64_t srvid, TDB_DATA data,
3556 struct disable_recoveries_state *state =
3557 (struct disable_recoveries_state *)private_data;
3560 if (data.dsize != sizeof(int)) {
3565 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
3566 ret = *(int *)data.dptr;
3568 state->status = ret;
3572 for (i=0; i<state->node_count; i++) {
3573 if (state->pnn_list[i] == ret) {
3574 state->reply[i] = true;
3580 for (i=0; i<state->node_count; i++) {
3581 if (! state->reply[i]) {
3582 state->done = false;
3588 static int disable_recoveries(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3589 uint32_t timeout, uint32_t *pnn_list, int count)
3591 struct ctdb_disable_message disable = { 0 };
3592 struct disable_recoveries_state state;
3595 disable.pnn = ctdb->pnn;
3596 disable.srvid = next_srvid(ctdb);
3597 disable.timeout = timeout;
3599 state.pnn_list = pnn_list;
3600 state.node_count = count;
3603 state.reply = talloc_zero_array(mem_ctx, bool, count);
3604 if (state.reply == NULL) {
3608 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
3610 disable_recoveries_handler,
3616 for (i=0; i<count; i++) {
3617 ret = ctdb_message_disable_recoveries(mem_ctx, ctdb->ev,
3626 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
3628 fprintf(stderr, "Timed out waiting to disable recoveries\n");
3630 ret = (state.status >= 0 ? 0 : 1);
3634 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
3635 disable.srvid, &state);
3639 static int control_reloadnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3640 int argc, const char **argv)
3642 struct ctdb_node_map *nodemap = NULL;
3643 struct ctdb_node_map *file_nodemap;
3644 struct ctdb_node_map *remote_nodemap;
3645 struct ctdb_req_control request;
3646 struct ctdb_reply_control **reply;
3652 nodemap = get_nodemap(ctdb, false);
3653 if (nodemap == NULL) {
3657 file_nodemap = read_nodes_file(mem_ctx, ctdb->pnn);
3658 if (file_nodemap == NULL) {
3662 for (i=0; i<nodemap->num; i++) {
3663 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3667 ret = ctdb_ctrl_get_nodes_file(mem_ctx, ctdb->ev, ctdb->client,
3668 nodemap->node[i].pnn, TIMEOUT(),
3672 "ERROR: Failed to get nodes file from node %u\n",
3673 nodemap->node[i].pnn);
3677 if (! nodemap_identical(file_nodemap, remote_nodemap)) {
3679 "ERROR: Nodes file on node %u differs"
3680 " from current node (%u)\n",
3681 nodemap->node[i].pnn, ctdb->pnn);
3686 ret = check_node_file_changes(mem_ctx, nodemap, file_nodemap, &reload);
3692 fprintf(stderr, "No change in nodes file,"
3693 " skipping unnecessary reload\n");
3697 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
3698 mem_ctx, &pnn_list);
3700 fprintf(stderr, "Memory allocation error\n");
3704 ret = disable_recoveries(mem_ctx, ctdb, 2*options.timelimit,
3707 fprintf(stderr, "Failed to disable recoveries\n");
3711 ctdb_req_control_reload_nodes_file(&request);
3712 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3713 pnn_list, count, TIMEOUT(),
3714 &request, NULL, &reply);
3716 bool failed = false;
3718 for (i=0; i<count; i++) {
3719 ret = ctdb_reply_control_reload_nodes_file(reply[i]);
3722 "Node %u failed to reload nodes\n",
3729 "You MUST fix failed nodes manually!\n");
3733 ret = disable_recoveries(mem_ctx, ctdb, 0, pnn_list, count);
3735 fprintf(stderr, "Failed to enable recoveries\n");
3742 static int moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3743 ctdb_sock_addr *addr, uint32_t pnn)
3745 struct ctdb_public_ip_list *pubip_list;
3746 struct ctdb_public_ip pubip;
3747 struct ctdb_node_map *nodemap;
3748 struct ctdb_req_control request;
3752 ret = ctdb_message_disable_ip_check(mem_ctx, ctdb->ev, ctdb->client,
3753 CTDB_BROADCAST_CONNECTED,
3754 2*options.timelimit);
3756 fprintf(stderr, "Failed to disable IP check\n");
3760 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3761 pnn, TIMEOUT(), &pubip_list);
3763 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3768 for (i=0; i<pubip_list->num; i++) {
3769 if (ctdb_same_ip(addr, &pubip_list->ip[i].addr)) {
3774 if (i == pubip_list->num) {
3775 fprintf(stderr, "Node %u CANNOT host IP address %s\n",
3776 pnn, ctdb_sock_addr_to_string(mem_ctx, addr));
3780 nodemap = get_nodemap(ctdb, false);
3781 if (nodemap == NULL) {
3785 count = list_of_active_nodes(nodemap, pnn, mem_ctx, &pnn_list);
3787 fprintf(stderr, "Memory allocation error\n");
3793 ctdb_req_control_release_ip(&request, &pubip);
3795 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3796 pnn_list, count, TIMEOUT(),
3797 &request, NULL, NULL);
3799 fprintf(stderr, "Failed to release IP on nodes\n");
3803 ret = ctdb_ctrl_takeover_ip(mem_ctx, ctdb->ev, ctdb->client,
3804 pnn, TIMEOUT(), &pubip);
3806 fprintf(stderr, "Failed to takeover IP on node %u\n", pnn);
3813 static int control_moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3814 int argc, const char **argv)
3816 ctdb_sock_addr addr;
3818 int ret, retries = 0;
3824 if (! parse_ip(argv[0], NULL, 0, &addr)) {
3825 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3829 pnn = strtoul(argv[1], NULL, 10);
3830 if (pnn == CTDB_UNKNOWN_PNN) {
3831 fprintf(stderr, "Invalid PNN %s\n", argv[1]);
3835 while (retries < 5) {
3836 ret = moveip(mem_ctx, ctdb, &addr, pnn);
3846 fprintf(stderr, "Failed to move IP %s to node %u\n",
3854 static int rebalancenode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3859 ret = ctdb_message_rebalance_node(mem_ctx, ctdb->ev, ctdb->client,
3860 CTDB_BROADCAST_CONNECTED, pnn);
3863 "Failed to ask recovery master to distribute IPs\n");
3870 static int control_addip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3871 int argc, const char **argv)
3873 ctdb_sock_addr addr;
3874 struct ctdb_public_ip_list *pubip_list;
3875 struct ctdb_addr_info addr_info;
3877 int ret, i, retries = 0;
3883 if (! parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
3884 fprintf(stderr, "Invalid IP/Mask %s\n", argv[0]);
3888 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3889 ctdb->cmd_pnn, TIMEOUT(), &pubip_list);
3891 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3896 for (i=0; i<pubip_list->num; i++) {
3897 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3898 fprintf(stderr, "Node already knows about IP %s\n",
3899 ctdb_sock_addr_to_string(mem_ctx, &addr));
3904 addr_info.addr = addr;
3905 addr_info.mask = mask;
3906 addr_info.iface = argv[1];
3908 while (retries < 5) {
3909 ret = ctdb_ctrl_add_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3910 ctdb->cmd_pnn, TIMEOUT(),
3921 fprintf(stderr, "Failed to add public IP to node %u."
3922 " Giving up\n", ctdb->cmd_pnn);
3926 ret = rebalancenode(mem_ctx, ctdb, ctdb->cmd_pnn);
3934 static int control_delip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3935 int argc, const char **argv)
3937 ctdb_sock_addr addr;
3938 struct ctdb_public_ip_list *pubip_list;
3939 struct ctdb_addr_info addr_info;
3946 if (! parse_ip(argv[0], NULL, 0, &addr)) {
3947 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3951 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3952 ctdb->cmd_pnn, TIMEOUT(), &pubip_list);
3954 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3959 for (i=0; i<pubip_list->num; i++) {
3960 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3965 if (i == pubip_list->num) {
3966 fprintf(stderr, "Node does not know about IP address %s\n",
3967 ctdb_sock_addr_to_string(mem_ctx, &addr));
3971 addr_info.addr = addr;
3973 addr_info.iface = NULL;
3975 ret = ctdb_ctrl_del_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3976 ctdb->cmd_pnn, TIMEOUT(), &addr_info);
3978 fprintf(stderr, "Failed to delete public IP from node %u\n",
3986 static int control_eventscript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3987 int argc, const char **argv)
3992 usage("eventscript");
3995 if (strcmp(argv[0], "monitor") != 0) {
3996 fprintf(stderr, "Only monitor event can be run\n");
4000 ret = ctdb_ctrl_run_eventscripts(mem_ctx, ctdb->ev, ctdb->client,
4001 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4003 fprintf(stderr, "Failed to run monitor event on node %u\n",
4011 #define DB_VERSION 3
4012 #define MAX_DB_NAME 64
4013 #define MAX_REC_BUFFER_SIZE (100*1000)
4016 unsigned long version;
4018 unsigned long flags;
4021 char name[MAX_DB_NAME];
4024 struct backup_state {
4025 TALLOC_CTX *mem_ctx;
4026 struct ctdb_rec_buffer *recbuf;
4029 unsigned int nbuf, nrec;
4032 static int backup_handler(uint32_t reqid, struct ctdb_ltdb_header *header,
4033 TDB_DATA key, TDB_DATA data, void *private_data)
4035 struct backup_state *state = (struct backup_state *)private_data;
4039 if (state->recbuf == NULL) {
4040 state->recbuf = ctdb_rec_buffer_init(state->mem_ctx,
4042 if (state->recbuf == NULL) {
4047 ret = ctdb_rec_buffer_add(state->recbuf, state->recbuf, reqid,
4053 len = ctdb_rec_buffer_len(state->recbuf);
4054 if (len < MAX_REC_BUFFER_SIZE) {
4058 ret = ctdb_rec_buffer_write(state->recbuf, state->fd);
4060 fprintf(stderr, "Failed to write records to backup file\n");
4065 state->nrec += state->recbuf->count;
4066 TALLOC_FREE(state->recbuf);
4071 static int control_backupdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4072 int argc, const char **argv)
4074 const char *db_name;
4075 struct ctdb_db_context *db;
4078 struct backup_state state;
4079 struct db_header db_hdr;
4086 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4090 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4093 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4097 fd = open(argv[1], O_RDWR|O_CREAT, 0600);
4100 fprintf(stderr, "Failed to open file %s for writing\n",
4105 /* Write empty header first */
4106 ZERO_STRUCT(db_hdr);
4107 ret = write(fd, &db_hdr, sizeof(struct db_header));
4111 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4115 state.mem_ctx = mem_ctx;
4116 state.recbuf = NULL;
4121 ret = ctdb_db_traverse(db, true, false, backup_handler, &state);
4123 fprintf(stderr, "Failed to collect records from DB %s\n",
4129 if (state.recbuf != NULL) {
4130 ret = ctdb_rec_buffer_write(state.recbuf, state.fd);
4133 "Failed to write records to backup file\n");
4139 state.nrec += state.recbuf->count;
4140 TALLOC_FREE(state.recbuf);
4143 db_hdr.version = DB_VERSION;
4144 db_hdr.timestamp = time(NULL);
4145 db_hdr.flags = db_flags;
4146 db_hdr.nbuf = state.nbuf;
4147 db_hdr.nrec = state.nrec;
4148 strncpy(db_hdr.name, db_name, MAX_DB_NAME-1);
4150 lseek(fd, 0, SEEK_SET);
4151 ret = write(fd, &db_hdr, sizeof(struct db_header));
4155 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4160 printf("Database backed up to %s\n", argv[1]);
4164 static int control_restoredb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4165 int argc, const char **argv)
4167 const char *db_name = NULL;
4168 struct ctdb_db_context *db;
4169 struct db_header db_hdr;
4170 struct ctdb_node_map *nodemap;
4171 struct ctdb_req_control request;
4172 struct ctdb_reply_control **reply;
4173 struct ctdb_transdb wipedb;
4174 struct ctdb_pulldb_ext pulldb;
4175 struct ctdb_rec_buffer *recbuf;
4176 uint32_t generation;
4182 if (argc < 1 || argc > 2) {
4186 fd = open(argv[0], O_RDONLY, 0600);
4189 fprintf(stderr, "Failed to open file %s for reading\n",
4198 ret = read(fd, &db_hdr, sizeof(struct db_header));
4202 fprintf(stderr, "Failed to read db header from file %s\n",
4206 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4208 if (db_hdr.version != DB_VERSION) {
4210 "Wrong version of backup file, expected %u, got %lu\n",
4211 DB_VERSION, db_hdr.version);
4216 if (db_name == NULL) {
4217 db_name = db_hdr.name;
4220 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4221 localtime(&db_hdr.timestamp));
4222 printf("Restoring database %s from backup @ %s\n", db_name, timebuf);
4224 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4227 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4232 nodemap = get_nodemap(ctdb, false);
4233 if (nodemap == NULL) {
4234 fprintf(stderr, "Failed to get nodemap\n");
4239 ret = get_generation(mem_ctx, ctdb, &generation);
4241 fprintf(stderr, "Failed to get current generation\n");
4246 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4253 wipedb.db_id = ctdb_db_id(db);
4254 wipedb.tid = generation;
4256 ctdb_req_control_db_freeze(&request, wipedb.db_id);
4257 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4258 ctdb->client, pnn_list, count,
4259 TIMEOUT(), &request, NULL, NULL);
4265 ctdb_req_control_db_transaction_start(&request, &wipedb);
4266 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4267 pnn_list, count, TIMEOUT(),
4268 &request, NULL, NULL);
4273 ctdb_req_control_wipe_database(&request, &wipedb);
4274 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4275 pnn_list, count, TIMEOUT(),
4276 &request, NULL, NULL);
4281 pulldb.db_id = ctdb_db_id(db);
4283 pulldb.srvid = SRVID_CTDB_PUSHDB;
4285 ctdb_req_control_db_push_start(&request, &pulldb);
4286 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4287 pnn_list, count, TIMEOUT(),
4288 &request, NULL, NULL);
4293 for (i=0; i<db_hdr.nbuf; i++) {
4294 struct ctdb_req_message message;
4297 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4302 data.dsize = ctdb_rec_buffer_len(recbuf);
4303 data.dptr = talloc_size(mem_ctx, data.dsize);
4304 if (data.dptr == NULL) {
4308 ctdb_rec_buffer_push(recbuf, data.dptr);
4310 message.srvid = pulldb.srvid;
4311 message.data.data = data;
4313 ret = ctdb_client_message_multi(mem_ctx, ctdb->ev,
4321 talloc_free(recbuf);
4322 talloc_free(data.dptr);
4325 ctdb_req_control_db_push_confirm(&request, pulldb.db_id);
4326 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4327 pnn_list, count, TIMEOUT(),
4328 &request, NULL, &reply);
4333 for (i=0; i<count; i++) {
4334 uint32_t num_records;
4336 ret = ctdb_reply_control_db_push_confirm(reply[i],
4339 fprintf(stderr, "Invalid response from node %u\n",
4344 if (num_records != db_hdr.nrec) {
4345 fprintf(stderr, "Node %u received %u of %lu records\n",
4346 pnn_list[i], num_records, db_hdr.nrec);
4351 ctdb_req_control_db_set_healthy(&request, wipedb.db_id);
4352 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4353 pnn_list, count, TIMEOUT(),
4354 &request, NULL, NULL);
4359 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4360 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4361 pnn_list, count, TIMEOUT(),
4362 &request, NULL, NULL);
4367 ctdb_req_control_db_thaw(&request, wipedb.db_id);
4368 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4369 ctdb->client, pnn_list, count,
4370 TIMEOUT(), &request, NULL, NULL);
4375 printf("Database %s restored\n", db_name);
4381 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4382 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4386 struct dumpdbbackup_state {
4387 ctdb_rec_parser_func_t parser;
4388 struct dump_record_state sub_state;
4391 static int dumpdbbackup_handler(uint32_t reqid,
4392 struct ctdb_ltdb_header *header,
4393 TDB_DATA key, TDB_DATA data,
4396 struct dumpdbbackup_state *state =
4397 (struct dumpdbbackup_state *)private_data;
4398 struct ctdb_ltdb_header hdr;
4401 ret = ctdb_ltdb_header_extract(&data, &hdr);
4406 return state->parser(reqid, &hdr, key, data, &state->sub_state);
4409 static int control_dumpdbbackup(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4410 int argc, const char **argv)
4412 struct db_header db_hdr;
4414 struct dumpdbbackup_state state;
4418 usage("dumpbackup");
4421 fd = open(argv[0], O_RDONLY, 0600);
4424 fprintf(stderr, "Failed to open file %s for reading\n",
4429 ret = read(fd, &db_hdr, sizeof(struct db_header));
4433 fprintf(stderr, "Failed to read db header from file %s\n",
4437 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4439 if (db_hdr.version != DB_VERSION) {
4441 "Wrong version of backup file, expected %u, got %lu\n",
4442 DB_VERSION, db_hdr.version);
4447 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4448 localtime(&db_hdr.timestamp));
4449 printf("Dumping database %s from backup @ %s\n",
4450 db_hdr.name, timebuf);
4452 state.parser = dump_record;
4453 state.sub_state.count = 0;
4455 for (i=0; i<db_hdr.nbuf; i++) {
4456 struct ctdb_rec_buffer *recbuf;
4458 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4460 fprintf(stderr, "Failed to read records\n");
4465 ret = ctdb_rec_buffer_traverse(recbuf, dumpdbbackup_handler,
4468 fprintf(stderr, "Failed to dump records\n");
4475 printf("Dumped %u record(s)\n", state.sub_state.count);
4479 static int control_wipedb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4480 int argc, const char **argv)
4482 const char *db_name;
4483 struct ctdb_db_context *db;
4486 struct ctdb_node_map *nodemap;
4487 struct ctdb_req_control request;
4488 struct ctdb_transdb wipedb;
4489 uint32_t generation;
4497 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4501 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4504 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4508 nodemap = get_nodemap(ctdb, false);
4509 if (nodemap == NULL) {
4510 fprintf(stderr, "Failed to get nodemap\n");
4514 ret = get_generation(mem_ctx, ctdb, &generation);
4516 fprintf(stderr, "Failed to get current generation\n");
4520 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4526 ctdb_req_control_db_freeze(&request, db_id);
4527 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4528 ctdb->client, pnn_list, count,
4529 TIMEOUT(), &request, NULL, NULL);
4534 wipedb.db_id = db_id;
4535 wipedb.tid = generation;
4537 ctdb_req_control_db_transaction_start(&request, &wipedb);
4538 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4539 pnn_list, count, TIMEOUT(),
4540 &request, NULL, NULL);
4545 ctdb_req_control_wipe_database(&request, &wipedb);
4546 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4547 pnn_list, count, TIMEOUT(),
4548 &request, NULL, NULL);
4553 ctdb_req_control_db_set_healthy(&request, db_id);
4554 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4555 pnn_list, count, TIMEOUT(),
4556 &request, NULL, NULL);
4561 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4562 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4563 pnn_list, count, TIMEOUT(),
4564 &request, NULL, NULL);
4569 ctdb_req_control_db_thaw(&request, db_id);
4570 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4571 ctdb->client, pnn_list, count,
4572 TIMEOUT(), &request, NULL, NULL);
4577 printf("Database %s wiped\n", db_name);
4582 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4583 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4587 static int control_recmaster(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4588 int argc, const char **argv)
4593 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
4594 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
4599 printf("%u\n", recmaster);
4603 static void print_scriptstatus_one(struct ctdb_script_list *slist,
4604 const char *event_str)
4609 if (slist == NULL) {
4610 if (! options.machinereadable) {
4611 printf("%s cycle never run\n", event_str);
4616 for (i=0; i<slist->num_scripts; i++) {
4617 if (slist->script[i].status != -ENOEXEC) {
4622 if (! options.machinereadable) {
4623 printf("%d scripts were executed last %s cycle\n",
4624 num_run, event_str);
4627 for (i=0; i<slist->num_scripts; i++) {
4628 const char *status = NULL;
4630 switch (slist->script[i].status) {
4632 status = "TIMEDOUT";
4635 status = "DISABLED";
4641 if (slist->script[i].status > 0) {
4647 if (options.machinereadable) {
4648 printf("%s%s%s%s%s%i%s%s%s%lu.%06lu%s%lu.%06lu%s%s%s\n",
4650 event_str, options.sep,
4651 slist->script[i].name, options.sep,
4652 slist->script[i].status, options.sep,
4653 status, options.sep,
4654 (unsigned long)slist->script[i].start.tv_sec,
4655 slist->script[i].start.tv_usec, options.sep,
4656 (unsigned long)slist->script[i].finished.tv_sec,
4657 slist->script[i].finished.tv_usec, options.sep,
4658 slist->script[i].output, options.sep);
4663 printf("%-20s Status:%s ",
4664 slist->script[i].name, status);
4666 /* Some other error, eg from stat. */
4667 printf("%-20s Status:CANNOT RUN (%s)",
4668 slist->script[i].name,
4669 strerror(-slist->script[i].status));
4672 if (slist->script[i].status >= 0) {
4673 printf("Duration:%.3lf ",
4674 timeval_delta(&slist->script[i].finished,
4675 &slist->script[i].start));
4677 if (slist->script[i].status != -ENOEXEC) {
4678 printf("%s", ctime(&slist->script[i].start.tv_sec));
4679 if (slist->script[i].status != 0) {
4680 printf(" OUTPUT:%s\n",
4681 slist->script[i].output);
4689 static void print_scriptstatus(struct ctdb_script_list **slist,
4690 int count, const char **event_str)
4694 if (options.machinereadable) {
4695 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
4697 "Type", options.sep,
4698 "Name", options.sep,
4699 "Code", options.sep,
4700 "Status", options.sep,
4701 "Start", options.sep,
4703 "Error Output", options.sep);
4706 for (i=0; i<count; i++) {
4707 print_scriptstatus_one(slist[i], event_str[i]);
4711 static int control_scriptstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4712 int argc, const char **argv)
4714 struct ctdb_script_list **slist;
4715 const char *event_str;
4716 enum ctdb_event event;
4717 const char *all_events[] = {
4718 "init", "setup", "startup", "monitor",
4719 "takeip", "releaseip", "updateip", "ipreallocated" };
4722 int count, start, end, num;
4725 usage("scriptstatus");
4729 event_str = "monitor";
4731 event_str = argv[0];
4737 for (i=0; i<ARRAY_SIZE(all_events); i++) {
4738 if (strcmp(event_str, all_events[i]) == 0) {
4747 if (strcmp(event_str, "all") == 0) {
4749 count = ARRAY_SIZE(all_events);
4755 fprintf(stderr, "Unknown event name %s\n", argv[0]);
4756 usage("scriptstatus");
4759 slist = talloc_array(mem_ctx, struct ctdb_script_list *, count);
4760 if (slist == NULL) {
4761 fprintf(stderr, "Memory allocation error\n");
4766 for (i=start; i<end; i++) {
4767 event = ctdb_event_from_string(all_events[i]);
4769 ret = ctdb_ctrl_get_event_script_status(mem_ctx, ctdb->ev,
4776 "failed to get script status for %s event\n",
4781 if (slist[num] == NULL) {
4786 /* The ETIME status is ignored for certain events.
4787 * In that case the status is 0, but endtime is not set.
4789 for (j=0; j<slist[num]->num_scripts; j++) {
4790 if (slist[num]->script[j].status == 0 &&
4791 timeval_is_zero(&slist[num]->script[j].finished)) {
4792 slist[num]->script[j].status = -ETIME;
4799 print_scriptstatus(slist, count, &all_events[start]);
4803 static int control_enablescript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4804 int argc, const char **argv)
4809 usage("enablescript");
4812 ret = ctdb_ctrl_enable_script(mem_ctx, ctdb->ev, ctdb->client,
4813 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4815 fprintf(stderr, "Failed to enable script %s\n", argv[0]);
4822 static int control_disablescript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4823 int argc, const char **argv)
4828 usage("disablescript");
4831 ret = ctdb_ctrl_disable_script(mem_ctx, ctdb->ev, ctdb->client,
4832 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4834 fprintf(stderr, "Failed to disable script %s\n", argv[0]);
4841 static int control_natgw(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4842 int argc, const char **argv)
4844 char *t, *natgw_helper = NULL;
4850 t = getenv("CTDB_NATGW_HELPER");
4852 natgw_helper = talloc_strdup(mem_ctx, t);
4854 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4855 CTDB_HELPER_BINDIR);
4858 if (natgw_helper == NULL) {
4859 fprintf(stderr, "Unable to set NAT gateway helper\n");
4863 return run_helper("NAT gateway helper", natgw_helper, argv[0]);
4866 static int control_natgwlist(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4867 int argc, const char **argv)
4869 char *t, *natgw_helper = NULL;
4875 t = getenv("CTDB_NATGW_HELPER");
4877 natgw_helper = talloc_strdup(mem_ctx, t);
4879 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4880 CTDB_HELPER_BINDIR);
4883 if (natgw_helper == NULL) {
4884 fprintf(stderr, "Unable to set NAT gateway helper\n");
4888 return run_helper("NAT gateway helper", natgw_helper, "natgwlist");
4892 * Find the PNN of the current node
4893 * discover the pnn by loading the nodes file and try to bind
4894 * to all addresses one at a time until the ip address is found.
4896 static bool find_node_xpnn(TALLOC_CTX *mem_ctx, uint32_t *pnn)
4898 struct ctdb_node_map *nodemap;
4901 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
4902 if (nodemap == NULL) {
4906 for (i=0; i<nodemap->num; i++) {
4907 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
4910 if (ctdb_sys_have_ip(&nodemap->node[i].addr)) {
4912 *pnn = nodemap->node[i].pnn;
4914 talloc_free(nodemap);
4919 fprintf(stderr, "Failed to detect PNN of the current node.\n");
4920 talloc_free(nodemap);
4924 static int control_getreclock(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4925 int argc, const char **argv)
4927 const char *reclock;
4931 usage("getreclock");
4934 ret = ctdb_ctrl_get_reclock_file(mem_ctx, ctdb->ev, ctdb->client,
4935 ctdb->cmd_pnn, TIMEOUT(), &reclock);
4940 if (reclock != NULL) {
4941 printf("%s\n", reclock);
4947 static int control_setlmasterrole(TALLOC_CTX *mem_ctx,
4948 struct ctdb_context *ctdb,
4949 int argc, const char **argv)
4951 uint32_t lmasterrole = 0;
4955 usage("setlmasterrole");
4958 if (strcmp(argv[0], "on") == 0) {
4960 } else if (strcmp(argv[0], "off") == 0) {
4963 usage("setlmasterrole");
4966 ret = ctdb_ctrl_set_lmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4967 ctdb->cmd_pnn, TIMEOUT(), lmasterrole);
4975 static int control_setrecmasterrole(TALLOC_CTX *mem_ctx,
4976 struct ctdb_context *ctdb,
4977 int argc, const char **argv)
4979 uint32_t recmasterrole = 0;
4983 usage("setrecmasterrole");
4986 if (strcmp(argv[0], "on") == 0) {
4988 } else if (strcmp(argv[0], "off") == 0) {
4991 usage("setrecmasterrole");
4994 ret = ctdb_ctrl_set_recmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4995 ctdb->cmd_pnn, TIMEOUT(),
5004 static int control_setdbreadonly(TALLOC_CTX *mem_ctx,
5005 struct ctdb_context *ctdb,
5006 int argc, const char **argv)
5013 usage("setdbreadonly");
5016 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
5020 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5021 fprintf(stderr, "Cannot set READONLY on persistent DB\n");
5025 ret = ctdb_ctrl_set_db_readonly(mem_ctx, ctdb->ev, ctdb->client,
5026 ctdb->cmd_pnn, TIMEOUT(), db_id);
5034 static int control_setdbsticky(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5035 int argc, const char **argv)
5042 usage("setdbsticky");
5045 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
5049 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5050 fprintf(stderr, "Cannot set STICKY on persistent DB\n");
5054 ret = ctdb_ctrl_set_db_sticky(mem_ctx, ctdb->ev, ctdb->client,
5055 ctdb->cmd_pnn, TIMEOUT(), db_id);
5063 static int control_pfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5064 int argc, const char **argv)
5066 const char *db_name;
5067 struct ctdb_db_context *db;
5068 struct ctdb_transaction_handle *h;
5073 if (argc < 2 || argc > 3) {
5077 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5081 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5082 fprintf(stderr, "DB %s is not a persistent database\n",
5087 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5090 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5094 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5096 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5100 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5101 TIMEOUT(), db, true, &h);
5103 fprintf(stderr, "Failed to start transaction on db %s\n",
5108 ret = ctdb_transaction_fetch_record(h, key, mem_ctx, &data);
5110 fprintf(stderr, "Failed to read record for key %s\n",
5112 ctdb_transaction_cancel(h);
5116 printf("%.*s\n", (int)data.dsize, data.dptr);
5118 ctdb_transaction_cancel(h);
5122 static int control_pstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5123 int argc, const char **argv)
5125 const char *db_name;
5126 struct ctdb_db_context *db;
5127 struct ctdb_transaction_handle *h;
5136 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5140 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5141 fprintf(stderr, "DB %s is not a persistent database\n",
5146 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5149 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5153 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5155 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5159 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5161 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5165 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5166 TIMEOUT(), db, false, &h);
5168 fprintf(stderr, "Failed to start transaction on db %s\n",
5173 ret = ctdb_transaction_store_record(h, key, data);
5175 fprintf(stderr, "Failed to store record for key %s\n",
5177 ctdb_transaction_cancel(h);
5181 ret = ctdb_transaction_commit(h);
5183 fprintf(stderr, "Failed to commit transaction on db %s\n",
5185 ctdb_transaction_cancel(h);
5192 static int control_pdelete(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5193 int argc, const char **argv)
5195 const char *db_name;
5196 struct ctdb_db_context *db;
5197 struct ctdb_transaction_handle *h;
5206 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5210 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5211 fprintf(stderr, "DB %s is not a persistent database\n",
5216 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5219 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5223 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5225 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5229 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5230 TIMEOUT(), db, false, &h);
5232 fprintf(stderr, "Failed to start transaction on db %s\n",
5237 ret = ctdb_transaction_delete_record(h, key);
5239 fprintf(stderr, "Failed to delete record for key %s\n",
5241 ctdb_transaction_cancel(h);
5245 ret = ctdb_transaction_commit(h);
5247 fprintf(stderr, "Failed to commit transaction on db %s\n",
5249 ctdb_transaction_cancel(h);
5256 static int ptrans_parse_string(TALLOC_CTX *mem_ctx, const char **ptr, TDB_DATA *data)
5264 /* Skip whitespace */
5265 n = strspn(*ptr, " \t");
5269 /* Quoted ASCII string - no wide characters! */
5271 n = strcspn(t, "\"");
5274 ret = str_to_data(t, n, mem_ctx, data);
5281 fprintf(stderr, "Unmatched \" in input %s\n", *ptr);
5285 fprintf(stderr, "Unsupported input format in %s\n", *ptr);
5292 #define MAX_LINE_SIZE 1024
5294 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
5295 TDB_DATA *key, TDB_DATA *value)
5297 char line [MAX_LINE_SIZE]; /* FIXME: make this more flexible? */
5301 ptr = fgets(line, MAX_LINE_SIZE, file);
5307 ret = ptrans_parse_string(mem_ctx, &ptr, key);
5308 if (ret != 0 || ptr == NULL || key->dptr == NULL) {
5309 /* Line Ignored but not EOF */
5315 ret = ptrans_parse_string(mem_ctx, &ptr, value);
5317 /* Line Ignored but not EOF */
5318 talloc_free(key->dptr);
5326 static int control_ptrans(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5327 int argc, const char **argv)
5329 const char *db_name;
5330 struct ctdb_db_context *db;
5331 struct ctdb_transaction_handle *h;
5334 TDB_DATA key, value;
5337 if (argc < 1 || argc > 2) {
5341 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5345 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5346 fprintf(stderr, "DB %s is not a persistent database\n",
5352 file = fopen(argv[1], "r");
5354 fprintf(stderr, "Failed to open file %s\n", argv[1]);
5361 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5364 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5368 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5369 TIMEOUT(), db, false, &h);
5371 fprintf(stderr, "Failed to start transaction on db %s\n",
5376 while (ptrans_get_key_value(mem_ctx, file, &key, &value)) {
5377 if (key.dsize != 0) {
5378 ret = ctdb_transaction_store_record(h, key, value);
5380 fprintf(stderr, "Failed to store record\n");
5381 ctdb_transaction_cancel(h);
5384 talloc_free(key.dptr);
5385 talloc_free(value.dptr);
5389 ret = ctdb_transaction_commit(h);
5391 fprintf(stderr, "Failed to commit transaction on db %s\n",
5393 ctdb_transaction_cancel(h);
5397 if (file != stdin) {
5403 static int control_tfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5404 int argc, const char **argv)
5406 struct tdb_context *tdb;
5408 struct ctdb_ltdb_header header;
5411 if (argc < 2 || argc > 3) {
5415 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5417 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5421 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5423 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5428 data = tdb_fetch(tdb, key);
5429 if (data.dptr == NULL) {
5430 fprintf(stderr, "No record for key %s\n", argv[1]);
5435 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
5436 fprintf(stderr, "Invalid record for key %s\n", argv[1]);
5447 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
5449 fprintf(stderr, "Failed to open output file %s\n",
5454 nwritten = sys_write(fd, data.dptr, data.dsize);
5455 if (nwritten != data.dsize) {
5456 fprintf(stderr, "Failed to write record to file\n");
5465 ret = ctdb_ltdb_header_extract(&data, &header);
5467 fprintf(stderr, "Failed to parse header from data\n");
5471 dump_ltdb_header(&header);
5472 dump_tdb_data("data", data);
5477 static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5478 int argc, const char **argv)
5480 struct tdb_context *tdb;
5481 TDB_DATA key, data[2], value;
5482 struct ctdb_ltdb_header header;
5483 uint8_t header_buf[sizeof(struct ctdb_ltdb_header)];
5486 if (argc < 3 || argc > 5) {
5490 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5492 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5496 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5498 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5503 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &value);
5505 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5510 ZERO_STRUCT(header);
5513 header.rsn = (uint64_t)strtoull(argv[3], NULL, 0);
5516 header.dmaster = (uint32_t)atol(argv[4]);
5519 header.flags = (uint32_t)atol(argv[5]);
5522 ctdb_ltdb_header_push(&header, header_buf);
5524 data[0].dsize = ctdb_ltdb_header_len(&header);
5525 data[0].dptr = header_buf;
5527 data[1].dsize = value.dsize;
5528 data[1].dptr = value.dptr;
5530 ret = tdb_storev(tdb, key, data, 2, TDB_REPLACE);
5532 fprintf(stderr, "Failed to write record %s to file %s\n",
5541 static int control_readkey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5542 int argc, const char **argv)
5544 const char *db_name;
5545 struct ctdb_db_context *db;
5546 struct ctdb_record_handle *h;
5549 bool readonly = false;
5552 if (argc < 2 || argc > 3) {
5557 if (strcmp(argv[2], "readonly") == 0) {
5564 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5568 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5569 fprintf(stderr, "DB %s is not a volatile database\n",
5574 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5577 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5581 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5583 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5587 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5588 db, key, readonly, &h, NULL, &data);
5590 fprintf(stderr, "Failed to read record for key %s\n",
5593 printf("Data: size:%zu ptr:[%.*s]\n", data.dsize,
5594 (int)data.dsize, data.dptr);
5601 static int control_writekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5602 int argc, const char **argv)
5604 const char *db_name;
5605 struct ctdb_db_context *db;
5606 struct ctdb_record_handle *h;
5615 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5619 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5620 fprintf(stderr, "DB %s is not a volatile database\n",
5625 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5628 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5632 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5634 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5638 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5640 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5644 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5645 db, key, false, &h, NULL, NULL);
5647 fprintf(stderr, "Failed to lock record for key %s\n", argv[0]);
5651 ret = ctdb_store_record(h, data);
5653 fprintf(stderr, "Failed to store record for key %s\n",
5661 static int control_deletekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5662 int argc, const char **argv)
5664 const char *db_name;
5665 struct ctdb_db_context *db;
5666 struct ctdb_record_handle *h;
5675 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5679 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5680 fprintf(stderr, "DB %s is not a volatile database\n",
5685 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5688 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5692 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5694 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5698 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5699 db, key, false, &h, NULL, &data);
5701 fprintf(stderr, "Failed to fetch record for key %s\n",
5706 ret = ctdb_delete_record(h);
5708 fprintf(stderr, "Failed to delete record for key %s\n",
5716 static int control_checktcpport(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5717 int argc, const char **argv)
5719 struct sockaddr_in sin;
5725 usage("chktcpport");
5728 port = atoi(argv[0]);
5730 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
5732 fprintf(stderr, "Failed to open local socket\n");
5736 v = fcntl(s, F_GETFL, 0);
5737 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK)) {
5738 fprintf(stderr, "Unable to set socket non-blocking\n");
5743 bzero(&sin, sizeof(sin));
5744 sin.sin_family = AF_INET;
5745 sin.sin_port = htons(port);
5746 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
5749 fprintf(stderr, "Failed to bind to TCP port %u\n", port);
5756 static int control_getdbseqnum(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5757 int argc, const char **argv)
5760 const char *db_name;
5765 usage("getdbseqnum");
5768 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5772 ret = ctdb_ctrl_get_db_seqnum(mem_ctx, ctdb->ev, ctdb->client,
5773 ctdb->cmd_pnn, TIMEOUT(), db_id,
5776 fprintf(stderr, "Failed to get sequence number for DB %s\n",
5781 printf("0x%"PRIx64"\n", seqnum);
5785 static int control_nodestatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5786 int argc, const char **argv)
5788 const char *nodestring = NULL;
5789 struct ctdb_node_map *nodemap;
5793 usage("nodestatus");
5797 nodestring = argv[0];
5800 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap)) {
5804 nodemap = get_nodemap(ctdb, false);
5805 if (nodemap == NULL) {
5809 if (options.machinereadable) {
5810 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5812 print_nodemap(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5816 for (i=0; i<nodemap->num; i++) {
5817 ret |= nodemap->node[i].flags;
5826 } db_stats_fields[] = {
5827 #define DBSTATISTICS_FIELD(n) { #n, offsetof(struct ctdb_db_statistics, n) }
5828 DBSTATISTICS_FIELD(db_ro_delegations),
5829 DBSTATISTICS_FIELD(db_ro_revokes),
5830 DBSTATISTICS_FIELD(locks.num_calls),
5831 DBSTATISTICS_FIELD(locks.num_current),
5832 DBSTATISTICS_FIELD(locks.num_pending),
5833 DBSTATISTICS_FIELD(locks.num_failed),
5834 DBSTATISTICS_FIELD(db_ro_delegations),
5837 static void print_dbstatistics(const char *db_name,
5838 struct ctdb_db_statistics *s)
5841 const char *prefix = NULL;
5844 printf("DB Statistics %s\n", db_name);
5846 for (i=0; i<ARRAY_SIZE(db_stats_fields); i++) {
5847 if (strchr(db_stats_fields[i].name, '.') != NULL) {
5848 preflen = strcspn(db_stats_fields[i].name, ".") + 1;
5850 strncmp(prefix, db_stats_fields[i].name, preflen) != 0) {
5851 prefix = db_stats_fields[i].name;
5852 printf(" %*.*s\n", preflen-1, preflen-1,
5853 db_stats_fields[i].name);
5858 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
5859 db_stats_fields[i].name+preflen, preflen ? 0 : 4, "",
5860 *(uint32_t *)(db_stats_fields[i].offset+(uint8_t *)s));
5863 printf(" hop_count_buckets:");
5864 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5865 printf(" %d", s->hop_count_bucket[i]);
5869 printf(" lock_buckets:");
5870 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5871 printf(" %d", s->locks.buckets[i]);
5875 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5876 "locks_latency MIN/AVG/MAX",
5877 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
5878 s->locks.latency.max, s->locks.latency.num);
5880 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5881 "vacuum_latency MIN/AVG/MAX",
5882 s->vacuum.latency.min, LATENCY_AVG(s->vacuum.latency),
5883 s->vacuum.latency.max, s->vacuum.latency.num);
5885 printf(" Num Hot Keys: %d\n", s->num_hot_keys);
5886 for (i=0; i<s->num_hot_keys; i++) {
5888 printf(" Count:%d Key:", s->hot_keys[i].count);
5889 for (j=0; j<s->hot_keys[i].key.dsize; j++) {
5890 printf("%02x", s->hot_keys[i].key.dptr[j] & 0xff);
5896 static int control_dbstatistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5897 int argc, const char **argv)
5900 const char *db_name;
5901 struct ctdb_db_statistics *dbstats;
5905 usage("dbstatistics");
5908 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5912 ret = ctdb_ctrl_get_db_statistics(mem_ctx, ctdb->ev, ctdb->client,
5913 ctdb->cmd_pnn, TIMEOUT(), db_id,
5916 fprintf(stderr, "Failed to get statistics for DB %s\n",
5921 print_dbstatistics(db_name, dbstats);
5925 struct disable_takeover_runs_state {
5933 static void disable_takeover_run_handler(uint64_t srvid, TDB_DATA data,
5936 struct disable_takeover_runs_state *state =
5937 (struct disable_takeover_runs_state *)private_data;
5940 if (data.dsize != sizeof(int)) {
5945 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
5946 ret = *(int *)data.dptr;
5948 state->status = ret;
5952 for (i=0; i<state->node_count; i++) {
5953 if (state->pnn_list[i] == ret) {
5954 state->reply[i] = true;
5960 for (i=0; i<state->node_count; i++) {
5961 if (! state->reply[i]) {
5962 state->done = false;
5968 static int disable_takeover_runs(TALLOC_CTX *mem_ctx,
5969 struct ctdb_context *ctdb, uint32_t timeout,
5970 uint32_t *pnn_list, int count)
5972 struct ctdb_disable_message disable = { 0 };
5973 struct disable_takeover_runs_state state;
5976 disable.pnn = ctdb->pnn;
5977 disable.srvid = next_srvid(ctdb);
5978 disable.timeout = timeout;
5980 state.pnn_list = pnn_list;
5981 state.node_count = count;
5984 state.reply = talloc_zero_array(mem_ctx, bool, count);
5985 if (state.reply == NULL) {
5989 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
5991 disable_takeover_run_handler,
5997 for (i=0; i<count; i++) {
5998 ret = ctdb_message_disable_takeover_runs(mem_ctx, ctdb->ev,
6007 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
6009 fprintf(stderr, "Timed out waiting to disable takeover runs\n");
6011 ret = (state.status >= 0 ? 0 : 1);
6015 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
6016 disable.srvid, &state);
6020 static int control_reloadips(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
6021 int argc, const char **argv)
6023 const char *nodestring = NULL;
6024 struct ctdb_node_map *nodemap, *nodemap2;
6025 struct ctdb_req_control request;
6026 uint32_t *pnn_list, *pnn_list2;
6027 int ret, count, count2;
6034 nodestring = argv[0];
6037 nodemap = get_nodemap(ctdb, false);
6038 if (nodemap == NULL) {
6042 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap2)) {
6046 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
6047 mem_ctx, &pnn_list);
6049 fprintf(stderr, "Memory allocation error\n");
6053 count2 = list_of_active_nodes(nodemap2, CTDB_UNKNOWN_PNN,
6054 mem_ctx, &pnn_list2);
6056 fprintf(stderr, "Memory allocation error\n");
6060 /* Disable takeover runs on all connected nodes. A reply
6061 * indicating success is needed from each node so all nodes
6062 * will need to be active.
6064 * A check could be added to not allow reloading of IPs when
6065 * there are disconnected nodes. However, this should
6066 * probably be left up to the administrator.
6068 ret = disable_takeover_runs(mem_ctx, ctdb, 2*options.timelimit,
6071 fprintf(stderr, "Failed to disable takeover runs\n");
6075 /* Now tell all the desired nodes to reload their public IPs.
6076 * Keep trying this until it succeeds. This assumes all
6077 * failures are transient, which might not be true...
6079 ctdb_req_control_reload_public_ips(&request);
6080 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
6081 pnn_list2, count2, TIMEOUT(),
6082 &request, NULL, NULL);
6084 fprintf(stderr, "Failed to reload IPs on some nodes.\n");
6087 /* It isn't strictly necessary to wait until takeover runs are
6088 * re-enabled but doing so can't hurt.
6090 ret = disable_takeover_runs(mem_ctx, ctdb, 0, pnn_list, count);
6092 fprintf(stderr, "Failed to enable takeover runs\n");
6096 return ipreallocate(mem_ctx, ctdb);
6099 static int control_ipiface(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
6100 int argc, const char **argv)
6102 ctdb_sock_addr addr;
6109 if (! parse_ip(argv[0], NULL, 0, &addr)) {
6110 fprintf(stderr, "Failed to Parse IP %s\n", argv[0]);
6114 iface = ctdb_sys_find_ifname(&addr);
6115 if (iface == NULL) {
6116 fprintf(stderr, "Failed to find interface for IP %s\n",
6126 static const struct ctdb_cmd {
6128 int (*fn)(TALLOC_CTX *, struct ctdb_context *, int, const char **);
6129 bool without_daemon; /* can be run without daemon running ? */
6130 bool remote; /* can be run on remote nodes */
6133 } ctdb_commands[] = {
6134 { "version", control_version, true, false,
6135 "show version of ctdb", NULL },
6136 { "status", control_status, false, true,
6137 "show node status", NULL },
6138 { "uptime", control_uptime, false, true,
6139 "show node uptime", NULL },
6140 { "ping", control_ping, false, true,
6141 "ping all nodes", NULL },
6142 { "runstate", control_runstate, false, true,
6143 "get/check runstate of a node",
6144 "[setup|first_recovery|startup|running]" },
6145 { "getvar", control_getvar, false, true,
6146 "get a tunable variable", "<name>" },
6147 { "setvar", control_setvar, false, true,
6148 "set a tunable variable", "<name> <value>" },
6149 { "listvars", control_listvars, false, true,
6150 "list tunable variables", NULL },
6151 { "statistics", control_statistics, false, true,
6152 "show ctdb statistics", NULL },
6153 { "statisticsreset", control_statistics_reset, false, true,
6154 "reset ctdb statistics", NULL },
6155 { "stats", control_stats, false, true,
6156 "show rolling statistics", "[count]" },
6157 { "ip", control_ip, false, true,
6158 "show public ips", "[all]" },
6159 { "ipinfo", control_ipinfo, false, true,
6160 "show public ip details", "<ip>" },
6161 { "ifaces", control_ifaces, false, true,
6162 "show interfaces", NULL },
6163 { "setifacelink", control_setifacelink, false, true,
6164 "set interface link status", "<iface> up|down" },
6165 { "process-exists", control_process_exists, false, true,
6166 "check if a process exists on a node", "<pid>" },
6167 { "getdbmap", control_getdbmap, false, true,
6168 "show attached databases", NULL },
6169 { "getdbstatus", control_getdbstatus, false, true,
6170 "show database status", "<dbname|dbid>" },
6171 { "catdb", control_catdb, false, false,
6172 "dump cluster-wide ctdb database", "<dbname|dbid>" },
6173 { "cattdb", control_cattdb, false, false,
6174 "dump local ctdb database", "<dbname|dbid>" },
6175 { "getmonmode", control_getmonmode, false, true,
6176 "show monitoring mode", NULL },
6177 { "getcapabilities", control_getcapabilities, false, true,
6178 "show node capabilities", NULL },
6179 { "pnn", control_pnn, false, false,
6180 "show the pnn of the currnet node", NULL },
6181 { "lvs", control_lvs, false, false,
6182 "show lvs configuration", "master|list|status" },
6183 { "disablemonitor", control_disable_monitor, false, true,
6184 "disable monitoring", NULL },
6185 { "enablemonitor", control_enable_monitor, false, true,
6186 "enable monitoring", NULL },
6187 { "setdebug", control_setdebug, false, true,
6188 "set debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
6189 { "getdebug", control_getdebug, false, true,
6190 "get debug level", NULL },
6191 { "attach", control_attach, false, false,
6192 "attach a database", "<dbname> [persistent]" },
6193 { "detach", control_detach, false, false,
6194 "detach database(s)", "<dbname|dbid> ..." },
6195 { "dumpmemory", control_dumpmemory, false, true,
6196 "dump ctdbd memory map", NULL },
6197 { "rddumpmemory", control_rddumpmemory, false, true,
6198 "dump recoverd memory map", NULL },
6199 { "getpid", control_getpid, false, true,
6200 "get ctdbd process ID", NULL },
6201 { "disable", control_disable, false, true,
6202 "disable a node", NULL },
6203 { "enable", control_enable, false, true,
6204 "enable a node", NULL },
6205 { "stop", control_stop, false, true,
6206 "stop a node", NULL },
6207 { "continue", control_continue, false, true,
6208 "continue a stopped node", NULL },
6209 { "ban", control_ban, false, true,
6210 "ban a node", "<bantime>"},
6211 { "unban", control_unban, false, true,
6212 "unban a node", NULL },
6213 { "shutdown", control_shutdown, false, true,
6214 "shutdown ctdb daemon", NULL },
6215 { "recover", control_recover, false, true,
6216 "force recovery", NULL },
6217 { "sync", control_ipreallocate, false, true,
6218 "run ip reallocation (deprecated)", NULL },
6219 { "ipreallocate", control_ipreallocate, false, true,
6220 "run ip reallocation", NULL },
6221 { "isnotrecmaster", control_isnotrecmaster, false, false,
6222 "check if local node is the recmaster", NULL },
6223 { "gratarp", control_gratarp, false, true,
6224 "send a gratuitous arp", "<ip> <interface>" },
6225 { "tickle", control_tickle, true, false,
6226 "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6227 { "gettickles", control_gettickles, false, true,
6228 "get the list of tickles", "<ip> [<port>]" },
6229 { "addtickle", control_addtickle, false, true,
6230 "add a tickle", "<ip>:<port> <ip>:<port>" },
6231 { "deltickle", control_deltickle, false, true,
6232 "delete a tickle", "<ip>:<port> <ip>:<port>" },
6233 { "check_srvids", control_check_srvids, false, true,
6234 "check if srvid is registered", "<id> [<id> ...]" },
6235 { "listnodes", control_listnodes, true, true,
6236 "list nodes in the cluster", NULL },
6237 { "reloadnodes", control_reloadnodes, false, false,
6238 "reload the nodes file all nodes", NULL },
6239 { "moveip", control_moveip, false, false,
6240 "move an ip address to another node", "<ip> <node>" },
6241 { "addip", control_addip, false, true,
6242 "add an ip address to a node", "<ip/mask> <iface>" },
6243 { "delip", control_delip, false, true,
6244 "delete an ip address from a node", "<ip>" },
6245 { "eventscript", control_eventscript, false, true,
6246 "run an event", "monitor" },
6247 { "backupdb", control_backupdb, false, false,
6248 "backup a database into a file", "<dbname|dbid> <file>" },
6249 { "restoredb", control_restoredb, false, false,
6250 "restore a database from a file", "<file> [dbname]" },
6251 { "dumpdbbackup", control_dumpdbbackup, true, false,
6252 "dump database from a backup file", "<file>" },
6253 { "wipedb", control_wipedb, false, false,
6254 "wipe the contents of a database.", "<dbname|dbid>"},
6255 { "recmaster", control_recmaster, false, true,
6256 "show the pnn for the recovery master", NULL },
6257 { "scriptstatus", control_scriptstatus, false, true,
6258 "show event script status",
6259 "[init|setup|startup|monitor|takeip|releaseip|ipreallocated]" },
6260 { "enablescript", control_enablescript, false, true,
6261 "enable an eventscript", "<script>"},
6262 { "disablescript", control_disablescript, false, true,
6263 "disable an eventscript", "<script>"},
6264 { "natgw", control_natgw, false, false,
6265 "show natgw configuration", "master|list|status" },
6266 { "natgwlist", control_natgwlist, false, false,
6267 "show the nodes belonging to this natgw configuration", NULL },
6268 { "getreclock", control_getreclock, false, true,
6269 "get recovery lock file", NULL },
6270 { "setlmasterrole", control_setlmasterrole, false, true,
6271 "set LMASTER role", "on|off" },
6272 { "setrecmasterrole", control_setrecmasterrole, false, true,
6273 "set RECMASTER role", "on|off"},
6274 { "setdbreadonly", control_setdbreadonly, false, true,
6275 "enable readonly records", "<dbname|dbid>" },
6276 { "setdbsticky", control_setdbsticky, false, true,
6277 "enable sticky records", "<dbname|dbid>"},
6278 { "pfetch", control_pfetch, false, false,
6279 "fetch record from persistent database", "<dbname|dbid> <key> [<file>]" },
6280 { "pstore", control_pstore, false, false,
6281 "write record to persistent database", "<dbname|dbid> <key> <value>" },
6282 { "pdelete", control_pdelete, false, false,
6283 "delete record from persistent database", "<dbname|dbid> <key>" },
6284 { "ptrans", control_ptrans, false, false,
6285 "update a persistent database (from file or stdin)", "<dbname|dbid> [<file>]" },
6286 { "tfetch", control_tfetch, false, true,
6287 "fetch a record", "<tdb-file> <key> [<file>]" },
6288 { "tstore", control_tstore, false, true,
6289 "store a record", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6290 { "readkey", control_readkey, false, false,
6291 "read value of a database key", "<dbname|dbid> <key> [readonly]" },
6292 { "writekey", control_writekey, false, false,
6293 "write value for a database key", "<dbname|dbid> <key> <value>" },
6294 { "deletekey", control_deletekey, false, false,
6295 "delete a database key", "<dbname|dbid> <key>" },
6296 { "checktcpport", control_checktcpport, true, false,
6297 "check if a service is bound to a specific tcp port or not", "<port>" },
6298 { "getdbseqnum", control_getdbseqnum, false, false,
6299 "get database sequence number", "<dbname|dbid>" },
6300 { "nodestatus", control_nodestatus, false, true,
6301 "show and return node status", "[all|<pnn-list>]" },
6302 { "dbstatistics", control_dbstatistics, false, true,
6303 "show database statistics", "<dbname|dbid>" },
6304 { "reloadips", control_reloadips, false, false,
6305 "reload the public addresses file", "[all|<pnn-list>]" },
6306 { "ipiface", control_ipiface, true, false,
6307 "Find the interface an ip address is hosted on", "<ip>" },
6310 static const struct ctdb_cmd *match_command(const char *command)
6312 const struct ctdb_cmd *cmd;
6315 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6316 cmd = &ctdb_commands[i];
6317 if (strlen(command) == strlen(cmd->name) &&
6318 strncmp(command, cmd->name, strlen(command)) == 0) {
6328 * Show usage message
6330 static void usage_full(void)
6334 poptPrintHelp(pc, stdout, 0);
6335 printf("\nCommands:\n");
6336 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6337 printf(" %-15s %-27s %s\n",
6338 ctdb_commands[i].name,
6339 ctdb_commands[i].args ? ctdb_commands[i].args : "",
6340 ctdb_commands[i].msg);
6344 static void usage(const char *command)
6346 const struct ctdb_cmd *cmd;
6348 if (command == NULL) {
6353 cmd = match_command(command);
6357 poptPrintUsage(pc, stdout, 0);
6358 printf("\nCommands:\n");
6359 printf(" %-15s %-27s %s\n",
6360 cmd->name, cmd->args ? cmd->args : "", cmd->msg);
6366 struct poptOption cmdline_options[] = {
6368 { "socket", 's', POPT_ARG_STRING, &options.socket, 0,
6369 "CTDB socket path", "filename" },
6370 { "debug", 'd', POPT_ARG_STRING, &options.debuglevelstr, 0,
6372 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0,
6373 "timelimit (in seconds)" },
6374 { "node", 'n', POPT_ARG_INT, &options.pnn, 0,
6375 "node specification - integer" },
6376 { NULL, 'Y', POPT_ARG_NONE, &options.machinereadable, 0,
6377 "enable machine readable output", NULL },
6378 { "separator", 'x', POPT_ARG_STRING, &options.sep, 0,
6379 "specify separator for machine readable output", "CHAR" },
6380 { NULL, 'X', POPT_ARG_NONE, &options.machineparsable, 0,
6381 "enable machine parsable output with separator |", NULL },
6382 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0,
6383 "enable verbose output", NULL },
6384 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0,
6385 "die if runtime exceeds this limit (in seconds)" },
6389 static int process_command(const struct ctdb_cmd *cmd, int argc,
6392 TALLOC_CTX *tmp_ctx;
6393 struct ctdb_context *ctdb;
6396 uint64_t srvid_offset;
6398 tmp_ctx = talloc_new(NULL);
6399 if (tmp_ctx == NULL) {
6400 fprintf(stderr, "Memory allocation error\n");
6404 if (cmd->without_daemon) {
6405 if (options.pnn != -1) {
6407 "Cannot specify node for command %s\n",
6412 ret = cmd->fn(tmp_ctx, NULL, argc-1, argv+1);
6413 talloc_free(tmp_ctx);
6417 ctdb = talloc_zero(tmp_ctx, struct ctdb_context);
6419 fprintf(stderr, "Memory allocation error\n");
6423 ctdb->ev = tevent_context_init(ctdb);
6424 if (ctdb->ev == NULL) {
6425 fprintf(stderr, "Failed to initialize tevent\n");
6429 ret = ctdb_client_init(ctdb, ctdb->ev, options.socket, &ctdb->client);
6431 fprintf(stderr, "Failed to connect to CTDB daemon (%s)\n",
6434 if (!find_node_xpnn(ctdb, NULL)) {
6435 fprintf(stderr, "Is this node part of CTDB cluster?\n");
6440 ctdb->pnn = ctdb_client_pnn(ctdb->client);
6441 srvid_offset = getpid() & 0xFFFF;
6442 ctdb->srvid = SRVID_CTDB_TOOL | (srvid_offset << 16);
6444 if (options.pnn != -1) {
6445 status = verify_pnn(ctdb, options.pnn);
6450 ctdb->cmd_pnn = options.pnn;
6452 ctdb->cmd_pnn = ctdb->pnn;
6455 if (! cmd->remote && ctdb->pnn != ctdb->cmd_pnn) {
6456 fprintf(stderr, "Node cannot be specified for command %s\n",
6461 ret = cmd->fn(tmp_ctx, ctdb, argc-1, argv+1);
6462 talloc_free(tmp_ctx);
6466 talloc_free(tmp_ctx);
6470 static void signal_handler(int sig)
6472 fprintf(stderr, "Maximum runtime exceeded - exiting\n");
6475 static void alarm_handler(int sig)
6477 /* Kill any child processes */
6478 signal(SIGTERM, signal_handler);
6484 int main(int argc, const char *argv[])
6487 const char **extra_argv;
6489 const struct ctdb_cmd *cmd;
6490 const char *ctdb_socket;
6496 /* Set default options */
6497 options.socket = CTDB_SOCKET;
6498 options.debuglevelstr = NULL;
6499 options.timelimit = 10;
6501 options.maxruntime = 0;
6504 ctdb_socket = getenv("CTDB_SOCKET");
6505 if (ctdb_socket != NULL) {
6506 options.socket = ctdb_socket;
6509 pc = poptGetContext(argv[0], argc, argv, cmdline_options,
6510 POPT_CONTEXT_KEEP_FIRST);
6511 while ((opt = poptGetNextOpt(pc)) != -1) {
6512 fprintf(stderr, "Invalid option %s: %s\n",
6513 poptBadOption(pc, 0), poptStrerror(opt));
6517 if (options.maxruntime == 0) {
6518 const char *ctdb_timeout;
6520 ctdb_timeout = getenv("CTDB_TIMEOUT");
6521 if (ctdb_timeout != NULL) {
6522 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6524 options.maxruntime = 120;
6527 if (options.maxruntime <= 120) {
6528 /* default timeout is 120 seconds */
6529 options.maxruntime = 120;
6532 if (options.machineparsable) {
6533 options.machinereadable = 1;
6536 /* setup the remaining options for the commands */
6538 extra_argv = poptGetArgs(pc);
6541 while (extra_argv[extra_argc]) extra_argc++;
6544 if (extra_argc < 1) {
6548 cmd = match_command(extra_argv[0]);
6550 fprintf(stderr, "Unknown command '%s'\n", extra_argv[0]);
6554 /* Enable logging */
6555 setup_logging("ctdb", DEBUG_STDERR);
6556 if (debug_level_parse(options.debuglevelstr, &loglevel)) {
6557 DEBUGLEVEL = loglevel;
6559 DEBUGLEVEL = DEBUG_ERR;
6562 signal(SIGALRM, alarm_handler);
6563 alarm(options.maxruntime);
6565 ret = process_command(cmd, extra_argc, extra_argv);
6570 (void)poptFreeContext(pc);