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 for (j=0; j<ipinfo[i]->ifaces->num; j++) {
1441 struct ctdb_iface *iface;
1443 iface = &ipinfo[i]->ifaces->iface[j];
1445 conf = talloc_strdup(mem_ctx, iface->name);
1447 conf = talloc_asprintf_append(
1448 conf, ",%s", iface->name);
1451 if (ipinfo[i]->active_idx == j) {
1452 active = iface->name;
1455 if (iface->link_state == 0) {
1459 if (avail == NULL) {
1460 avail = talloc_strdup(mem_ctx, iface->name);
1462 avail = talloc_asprintf_append(
1463 avail, ",%s", iface->name);
1467 if (options.machinereadable == 1) {
1468 printf("%s%s%s%s%s%s\n",
1469 active ? active : "", options.sep,
1470 avail ? avail : "", options.sep,
1471 conf ? conf : "", options.sep);
1473 printf(" node[%u] active[%s] available[%s] configured[%s]\n",
1474 ips->ip[i].pnn, active ? active : "",
1475 avail ? avail : "", conf ? conf : "");
1480 static int collect_ips(uint8_t *keybuf, size_t keylen, uint8_t *databuf,
1481 size_t datalen, void *private_data)
1483 struct ctdb_public_ip_list *ips = talloc_get_type_abort(
1484 private_data, struct ctdb_public_ip_list);
1485 struct ctdb_public_ip *ip;
1487 ip = (struct ctdb_public_ip *)databuf;
1488 ips->ip[ips->num] = *ip;
1494 static int get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
1495 struct ctdb_public_ip_list **out)
1497 struct ctdb_node_map *nodemap;
1498 struct ctdb_public_ip_list *ips;
1499 struct db_hash_context *ipdb;
1501 int ret, count, i, j;
1503 nodemap = get_nodemap(ctdb, false);
1504 if (nodemap == NULL) {
1508 ret = db_hash_init(mem_ctx, "ips", 101, DB_HASH_COMPLEX, &ipdb);
1513 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
1519 for (i=0; i<count; i++) {
1520 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1521 pnn_list[i], TIMEOUT(), &ips);
1526 for (j=0; j<ips->num; j++) {
1527 struct ctdb_public_ip ip;
1529 ip.pnn = ips->ip[j].pnn;
1530 ip.addr = ips->ip[j].addr;
1532 ret = db_hash_add(ipdb, (uint8_t *)&ip.addr,
1534 (uint8_t *)&ip, sizeof(ip));
1543 talloc_free(pnn_list);
1545 ret = db_hash_traverse(ipdb, NULL, NULL, &count);
1550 ips = talloc_zero(mem_ctx, struct ctdb_public_ip_list);
1555 ips->ip = talloc_array(ips, struct ctdb_public_ip, count);
1556 if (ips->ip == NULL) {
1560 ret = db_hash_traverse(ipdb, collect_ips, ips, &count);
1565 if (count != ips->num) {
1579 static int control_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1580 int argc, const char **argv)
1582 struct ctdb_public_ip_list *ips;
1583 struct ctdb_public_ip_info **ipinfo;
1585 bool do_all = false;
1592 if (strcmp(argv[0], "all") == 0) {
1600 ret = get_all_public_ips(ctdb, mem_ctx, &ips);
1602 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1603 ctdb->cmd_pnn, TIMEOUT(), &ips);
1609 qsort(ips->ip, ips->num, sizeof(struct ctdb_public_ip),
1610 ctdb_public_ip_cmp);
1612 ipinfo = talloc_array(mem_ctx, struct ctdb_public_ip_info *, ips->num);
1613 if (ipinfo == NULL) {
1617 for (i=0; i<ips->num; i++) {
1620 pnn = ips->ip[i].pnn;
1622 pnn = ctdb->cmd_pnn;
1624 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev,
1626 TIMEOUT(), &ips->ip[i].addr,
1633 print_ip(mem_ctx, ctdb, ips, ipinfo, do_all);
1637 static int control_ipinfo(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1638 int argc, const char **argv)
1640 struct ctdb_public_ip_info *ipinfo;
1641 ctdb_sock_addr addr;
1648 if (! parse_ip(argv[0], NULL, 0, &addr)) {
1649 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
1653 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev, ctdb->client,
1654 ctdb->cmd_pnn, TIMEOUT(), &addr,
1658 printf("Node %u does not know about IP %s\n",
1659 ctdb->cmd_pnn, argv[0]);
1664 printf("Public IP[%s] info on node %u\n",
1665 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1668 printf("IP:%s\nCurrentNode:%u\nNumInterfaces:%u\n",
1669 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1670 ipinfo->ip.pnn, ipinfo->ifaces->num);
1672 for (i=0; i<ipinfo->ifaces->num; i++) {
1673 struct ctdb_iface *iface;
1675 iface = &ipinfo->ifaces->iface[i];
1676 iface->name[CTDB_IFACE_SIZE] = '\0';
1677 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
1679 iface->link_state == 0 ? "down" : "up",
1681 (i == ipinfo->active_idx) ? " (active)" : "");
1687 static int control_ifaces(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1688 int argc, const char **argv)
1690 struct ctdb_iface_list *ifaces;
1697 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1698 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1703 if (ifaces->num == 0) {
1704 printf("No interfaces configured on node %u\n",
1709 if (options.machinereadable) {
1710 printf("%s%s%s%s%s%s%s\n", options.sep,
1711 "Name", options.sep,
1712 "LinkStatus", options.sep,
1713 "References", options.sep);
1715 printf("Interfaces on node %u\n", ctdb->cmd_pnn);
1718 for (i=0; i<ifaces->num; i++) {
1719 if (options.machinereadable) {
1720 printf("%s%s%s%u%s%u%s\n", options.sep,
1721 ifaces->iface[i].name, options.sep,
1722 ifaces->iface[i].link_state, options.sep,
1723 ifaces->iface[i].references, options.sep);
1725 printf("name:%s link:%s references:%u\n",
1726 ifaces->iface[i].name,
1727 ifaces->iface[i].link_state ? "up" : "down",
1728 ifaces->iface[i].references);
1735 static int control_setifacelink(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1736 int argc, const char **argv)
1738 struct ctdb_iface_list *ifaces;
1739 struct ctdb_iface *iface;
1743 usage("setifacelink");
1746 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
1747 fprintf(stderr, "Interface name '%s' too long\n", argv[0]);
1751 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1752 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1755 "Failed to get interface information from node %u\n",
1761 for (i=0; i<ifaces->num; i++) {
1762 if (strcmp(ifaces->iface[i].name, argv[0]) == 0) {
1763 iface = &ifaces->iface[i];
1768 if (iface == NULL) {
1769 printf("Interface %s not configured on node %u\n",
1770 argv[0], ctdb->cmd_pnn);
1774 if (strcmp(argv[1], "up") == 0) {
1775 iface->link_state = 1;
1776 } else if (strcmp(argv[1], "down") == 0) {
1777 iface->link_state = 0;
1779 usage("setifacelink");
1783 iface->references = 0;
1785 ret = ctdb_ctrl_set_iface_link_state(mem_ctx, ctdb->ev, ctdb->client,
1786 ctdb->cmd_pnn, TIMEOUT(), iface);
1794 static int control_process_exists(TALLOC_CTX *mem_ctx,
1795 struct ctdb_context *ctdb,
1796 int argc, const char **argv)
1802 usage("process-exists");
1805 pid = atoi(argv[0]);
1806 ret = ctdb_ctrl_process_exists(mem_ctx, ctdb->ev, ctdb->client,
1807 ctdb->cmd_pnn, TIMEOUT(), pid, &status);
1813 printf("PID %u exists\n", pid);
1815 printf("PID %u does not exist\n", pid);
1820 static int control_getdbmap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1821 int argc, const char **argv)
1823 struct ctdb_dbid_map *dbmap;
1830 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
1831 ctdb->cmd_pnn, TIMEOUT(), &dbmap);
1836 if (options.machinereadable == 1) {
1837 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1840 "Name", options.sep,
1841 "Path", options.sep,
1842 "Persistent", options.sep,
1843 "Sticky", options.sep,
1844 "Unhealthy", options.sep,
1845 "Readonly", options.sep);
1847 printf("Number of databases:%d\n", dbmap->num);
1850 for (i=0; i<dbmap->num; i++) {
1859 db_id = dbmap->dbs[i].db_id;
1861 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
1862 ctdb->cmd_pnn, TIMEOUT(), db_id,
1868 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1869 ctdb->cmd_pnn, TIMEOUT(), db_id,
1875 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1876 ctdb->cmd_pnn, TIMEOUT(), db_id,
1882 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
1883 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
1884 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
1886 if (options.machinereadable == 1) {
1887 printf("%s0x%08X%s%s%s%s%s%d%s%d%s%d%s%d%s\n",
1892 !! (persistent), options.sep,
1893 !! (sticky), options.sep,
1894 !! (health), options.sep,
1895 !! (readonly), options.sep);
1897 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
1899 persistent ? " PERSISTENT" : "",
1900 sticky ? " STICKY" : "",
1901 readonly ? " READONLY" : "",
1902 health ? " UNHEALTHY" : "");
1905 talloc_free(discard_const(name));
1906 talloc_free(discard_const(path));
1907 talloc_free(discard_const(health));
1913 static int control_getdbstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1914 int argc, const char **argv)
1917 const char *db_name, *db_path, *db_health;
1922 usage("getdbstatus");
1925 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
1929 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1930 ctdb->cmd_pnn, TIMEOUT(), db_id,
1936 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1937 ctdb->cmd_pnn, TIMEOUT(), db_id,
1943 printf("dbid: 0x%08x\nname: %s\npath: %s\n", db_id, db_name, db_path);
1944 printf("PERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
1945 (db_flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
1946 (db_flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
1947 (db_flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
1948 (db_health ? db_health : "OK"));
1952 struct dump_record_state {
1956 #define ISASCII(x) (isprint(x) && ! strchr("\"\\", (x)))
1958 static void dump_tdb_data(const char *name, TDB_DATA val)
1962 fprintf(stdout, "%s(%zu) = \"", name, val.dsize);
1963 for (i=0; i<val.dsize; i++) {
1964 if (ISASCII(val.dptr[i])) {
1965 fprintf(stdout, "%c", val.dptr[i]);
1967 fprintf(stdout, "\\%02X", val.dptr[i]);
1970 fprintf(stdout, "\"\n");
1973 static void dump_ltdb_header(struct ctdb_ltdb_header *header)
1975 fprintf(stdout, "dmaster: %u\n", header->dmaster);
1976 fprintf(stdout, "rsn: %" PRIu64 "\n", header->rsn);
1977 fprintf(stdout, "flags: 0x%08x", header->flags);
1978 if (header->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
1979 fprintf(stdout, " MIGRATED_WITH_DATA");
1981 if (header->flags & CTDB_REC_FLAG_VACUUM_MIGRATED) {
1982 fprintf(stdout, " VACUUM_MIGRATED");
1984 if (header->flags & CTDB_REC_FLAG_AUTOMATIC) {
1985 fprintf(stdout, " AUTOMATIC");
1987 if (header->flags & CTDB_REC_RO_HAVE_DELEGATIONS) {
1988 fprintf(stdout, " RO_HAVE_DELEGATIONS");
1990 if (header->flags & CTDB_REC_RO_HAVE_READONLY) {
1991 fprintf(stdout, " RO_HAVE_READONLY");
1993 if (header->flags & CTDB_REC_RO_REVOKING_READONLY) {
1994 fprintf(stdout, " RO_REVOKING_READONLY");
1996 if (header->flags & CTDB_REC_RO_REVOKE_COMPLETE) {
1997 fprintf(stdout, " RO_REVOKE_COMPLETE");
1999 fprintf(stdout, "\n");
2003 static int dump_record(uint32_t reqid, struct ctdb_ltdb_header *header,
2004 TDB_DATA key, TDB_DATA data, void *private_data)
2006 struct dump_record_state *state =
2007 (struct dump_record_state *)private_data;
2011 dump_tdb_data("key", key);
2012 dump_ltdb_header(header);
2013 dump_tdb_data("data", data);
2014 fprintf(stdout, "\n");
2019 struct traverse_state {
2020 TALLOC_CTX *mem_ctx;
2022 ctdb_rec_parser_func_t func;
2023 struct dump_record_state sub_state;
2026 static void traverse_handler(uint64_t srvid, TDB_DATA data, void *private_data)
2028 struct traverse_state *state = (struct traverse_state *)private_data;
2029 struct ctdb_rec_data *rec;
2030 struct ctdb_ltdb_header header;
2033 ret = ctdb_rec_data_pull(data.dptr, data.dsize, state->mem_ctx, &rec);
2038 if (rec->key.dsize == 0 && rec->data.dsize == 0) {
2040 /* end of traverse */
2045 ret = ctdb_ltdb_header_extract(&rec->data, &header);
2051 if (rec->data.dsize == 0) {
2056 ret = state->func(rec->reqid, &header, rec->key, rec->data,
2064 static int control_catdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2065 int argc, const char **argv)
2067 struct ctdb_db_context *db;
2068 const char *db_name;
2071 struct ctdb_traverse_start_ext traverse;
2072 struct traverse_state state;
2079 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2083 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2086 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2091 ZERO_STRUCT(traverse);
2093 traverse.db_id = db_id;
2095 traverse.srvid = next_srvid(ctdb);
2096 traverse.withemptyrecords = false;
2098 state.mem_ctx = mem_ctx;
2100 state.func = dump_record;
2101 state.sub_state.count = 0;
2103 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2105 traverse_handler, &state);
2110 ret = ctdb_ctrl_traverse_start_ext(mem_ctx, ctdb->ev, ctdb->client,
2111 ctdb->cmd_pnn, TIMEOUT(),
2117 ctdb_client_wait(ctdb->ev, &state.done);
2119 printf("Dumped %u records\n", state.sub_state.count);
2121 ret = ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2122 traverse.srvid, &state);
2130 static int control_cattdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2131 int argc, const char **argv)
2133 struct ctdb_db_context *db;
2134 const char *db_name;
2137 struct dump_record_state state;
2144 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2148 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2151 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2156 ret = ctdb_db_traverse(db, true, true, dump_record, &state);
2158 printf("Dumped %u record(s)\n", state.count);
2163 static int control_getmonmode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2164 int argc, const char **argv)
2169 usage("getmonmode");
2172 ret = ctdb_ctrl_get_monmode(mem_ctx, ctdb->ev, ctdb->client,
2173 ctdb->cmd_pnn, TIMEOUT(), &mode);
2179 (mode == CTDB_MONITORING_ENABLED) ? "ENABLED" : "DISABLED");
2183 static int control_getcapabilities(TALLOC_CTX *mem_ctx,
2184 struct ctdb_context *ctdb,
2185 int argc, const char **argv)
2191 usage("getcapabilities");
2194 ret = ctdb_ctrl_get_capabilities(mem_ctx, ctdb->ev, ctdb->client,
2195 ctdb->cmd_pnn, TIMEOUT(), &caps);
2200 if (options.machinereadable == 1) {
2201 printf("%s%s%s%s%s\n",
2203 "RECMASTER", options.sep,
2204 "LMASTER", options.sep);
2205 printf("%s%d%s%d%s\n", options.sep,
2206 !! (caps & CTDB_CAP_RECMASTER), options.sep,
2207 !! (caps & CTDB_CAP_LMASTER), options.sep);
2209 printf("RECMASTER: %s\n",
2210 (caps & CTDB_CAP_RECMASTER) ? "YES" : "NO");
2211 printf("LMASTER: %s\n",
2212 (caps & CTDB_CAP_LMASTER) ? "YES" : "NO");
2218 static int control_pnn(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2219 int argc, const char **argv)
2221 printf("%u\n", ctdb_client_pnn(ctdb->client));
2225 static int control_lvs(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2226 int argc, const char **argv)
2228 char *t, *lvs_helper = NULL;
2234 t = getenv("CTDB_LVS_HELPER");
2236 lvs_helper = talloc_strdup(mem_ctx, t);
2238 lvs_helper = talloc_asprintf(mem_ctx, "%s/ctdb_lvs",
2239 CTDB_HELPER_BINDIR);
2242 if (lvs_helper == NULL) {
2243 fprintf(stderr, "Unable to set LVS helper\n");
2247 return run_helper("LVS helper", lvs_helper, argv[0]);
2250 static int control_disable_monitor(TALLOC_CTX *mem_ctx,
2251 struct ctdb_context *ctdb,
2252 int argc, const char **argv)
2257 usage("disablemonitor");
2260 ret = ctdb_ctrl_disable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2261 ctdb->cmd_pnn, TIMEOUT());
2269 static int control_enable_monitor(TALLOC_CTX *mem_ctx,
2270 struct ctdb_context *ctdb,
2271 int argc, const char **argv)
2276 usage("enablemonitor");
2279 ret = ctdb_ctrl_enable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2280 ctdb->cmd_pnn, TIMEOUT());
2288 static int control_setdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2289 int argc, const char **argv)
2299 found = debug_level_parse(argv[0], &log_level);
2302 "Invalid debug level '%s'. Valid levels are:\n",
2304 fprintf(stderr, "\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
2308 ret = ctdb_ctrl_setdebug(mem_ctx, ctdb->ev, ctdb->client,
2309 ctdb->cmd_pnn, TIMEOUT(), log_level);
2317 static int control_getdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2318 int argc, const char **argv)
2321 const char *log_str;
2328 ret = ctdb_ctrl_getdebug(mem_ctx, ctdb->ev, ctdb->client,
2329 ctdb->cmd_pnn, TIMEOUT(), &loglevel);
2334 log_str = debug_level_to_string(loglevel);
2335 printf("%s\n", log_str);
2340 static int control_attach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2341 int argc, const char **argv)
2343 const char *db_name;
2344 uint8_t db_flags = 0;
2347 if (argc < 1 || argc > 2) {
2353 if (strcmp(argv[1], "persistent") == 0) {
2354 db_flags = CTDB_DB_FLAGS_PERSISTENT;
2355 } else if (strcmp(argv[1], "readonly") == 0) {
2356 db_flags = CTDB_DB_FLAGS_READONLY;
2357 } else if (strcmp(argv[1], "sticky") == 0) {
2358 db_flags = CTDB_DB_FLAGS_STICKY;
2364 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2373 static int control_detach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2374 int argc, const char **argv)
2376 const char *db_name;
2379 struct ctdb_node_map *nodemap;
2387 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2388 ctdb->cmd_pnn, TIMEOUT(), &recmode);
2393 if (recmode == CTDB_RECOVERY_ACTIVE) {
2394 fprintf(stderr, "Database cannot be detached"
2395 " when recovery is active\n");
2399 nodemap = get_nodemap(ctdb, false);
2400 if (nodemap == NULL) {
2404 for (i=0; i<nodemap->num; i++) {
2407 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
2410 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
2413 if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
2414 fprintf(stderr, "Database cannot be detached on"
2415 " inactive (stopped or banned) node %u\n",
2416 nodemap->node[i].pnn);
2420 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
2421 nodemap->node[i].pnn, TIMEOUT(),
2422 "AllowClientDBAttach", &value);
2425 "Unable to get tunable AllowClientDBAttach"
2426 " from node %u\n", nodemap->node[i].pnn);
2432 "Database access is still active on node %u."
2433 " Set AllowclientDBAttach=0 on all nodes.\n",
2434 nodemap->node[i].pnn);
2440 for (i=0; i<argc; i++) {
2441 if (! db_exists(mem_ctx, ctdb, argv[i], &db_id, &db_name,
2446 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
2448 "Persistent database %s cannot be detached\n",
2453 ret = ctdb_detach(mem_ctx, ctdb->ev, ctdb->client,
2456 fprintf(stderr, "Database %s detach failed\n", db_name);
2464 static int control_dumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2465 int argc, const char **argv)
2467 const char *mem_str;
2471 ret = ctdb_ctrl_dump_memory(mem_ctx, ctdb->ev, ctdb->client,
2472 ctdb->cmd_pnn, TIMEOUT(), &mem_str);
2477 n = write(1, mem_str, strlen(mem_str)+1);
2478 if (n < 0 || n != strlen(mem_str)+1) {
2479 fprintf(stderr, "Failed to write talloc summary\n");
2486 static void dump_memory(uint64_t srvid, TDB_DATA data, void *private_data)
2488 bool *done = (bool *)private_data;
2491 n = write(1, data.dptr, data.dsize);
2492 if (n < 0 || n != data.dsize) {
2493 fprintf(stderr, "Failed to write talloc summary\n");
2499 static int control_rddumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2500 int argc, const char **argv)
2502 struct ctdb_srvid_message msg = { 0 };
2506 msg.pnn = ctdb->pnn;
2507 msg.srvid = next_srvid(ctdb);
2509 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2510 msg.srvid, dump_memory, &done);
2515 ret = ctdb_message_mem_dump(mem_ctx, ctdb->ev, ctdb->client,
2516 ctdb->cmd_pnn, &msg);
2521 ctdb_client_wait(ctdb->ev, &done);
2525 static int control_getpid(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2526 int argc, const char **argv)
2531 ret = ctdb_ctrl_get_pid(mem_ctx, ctdb->ev, ctdb->client,
2532 ctdb->cmd_pnn, TIMEOUT(), &pid);
2537 printf("%u\n", pid);
2541 static int check_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2542 const char *desc, uint32_t flag, bool set_flag)
2544 struct ctdb_node_map *nodemap;
2547 nodemap = get_nodemap(ctdb, false);
2548 if (nodemap == NULL) {
2552 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2553 if (set_flag == flag_is_set) {
2555 fprintf(stderr, "Node %u is already %s\n",
2556 ctdb->cmd_pnn, desc);
2558 fprintf(stderr, "Node %u is not %s\n",
2559 ctdb->cmd_pnn, desc);
2567 static void wait_for_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2568 uint32_t flag, bool set_flag)
2570 struct ctdb_node_map *nodemap;
2574 nodemap = get_nodemap(ctdb, true);
2575 if (nodemap == NULL) {
2577 "Failed to get nodemap, trying again\n");
2582 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2583 if (flag_is_set == set_flag) {
2591 struct ipreallocate_state {
2596 static void ipreallocate_handler(uint64_t srvid, TDB_DATA data,
2599 struct ipreallocate_state *state =
2600 (struct ipreallocate_state *)private_data;
2602 if (data.dsize != sizeof(int)) {
2607 state->status = *(int *)data.dptr;
2611 static int ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb)
2613 struct ctdb_srvid_message msg = { 0 };
2614 struct ipreallocate_state state;
2617 msg.pnn = ctdb->pnn;
2618 msg.srvid = next_srvid(ctdb);
2621 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2623 ipreallocate_handler, &state);
2629 ret = ctdb_message_takeover_run(mem_ctx, ctdb->ev,
2631 CTDB_BROADCAST_CONNECTED,
2637 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done,
2643 if (state.status >= 0) {
2652 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2657 static int control_disable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2658 int argc, const char **argv)
2666 ret = check_flags(mem_ctx, ctdb, "disabled",
2667 NODE_FLAGS_PERMANENTLY_DISABLED, true);
2672 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2673 ctdb->cmd_pnn, TIMEOUT(),
2674 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2677 "Failed to set DISABLED flag on node %u\n",
2682 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, true);
2683 return ipreallocate(mem_ctx, ctdb);
2686 static int control_enable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2687 int argc, const char **argv)
2695 ret = check_flags(mem_ctx, ctdb, "disabled",
2696 NODE_FLAGS_PERMANENTLY_DISABLED, false);
2701 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2702 ctdb->cmd_pnn, TIMEOUT(),
2703 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2705 fprintf(stderr, "Failed to reset DISABLED flag on node %u\n",
2710 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, false);
2711 return ipreallocate(mem_ctx, ctdb);
2714 static int control_stop(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2715 int argc, const char **argv)
2723 ret = check_flags(mem_ctx, ctdb, "stopped",
2724 NODE_FLAGS_STOPPED, true);
2729 ret = ctdb_ctrl_stop_node(mem_ctx, ctdb->ev, ctdb->client,
2730 ctdb->cmd_pnn, TIMEOUT());
2732 fprintf(stderr, "Failed to stop node %u\n", ctdb->cmd_pnn);
2736 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, true);
2737 return ipreallocate(mem_ctx, ctdb);
2740 static int control_continue(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2741 int argc, const char **argv)
2749 ret = check_flags(mem_ctx, ctdb, "stopped",
2750 NODE_FLAGS_STOPPED, false);
2755 ret = ctdb_ctrl_continue_node(mem_ctx, ctdb->ev, ctdb->client,
2756 ctdb->cmd_pnn, TIMEOUT());
2758 fprintf(stderr, "Failed to continue stopped node %u\n",
2763 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, false);
2764 return ipreallocate(mem_ctx, ctdb);
2767 static int control_ban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2768 int argc, const char **argv)
2770 struct ctdb_ban_state ban_state;
2777 ret = check_flags(mem_ctx, ctdb, "banned",
2778 NODE_FLAGS_BANNED, true);
2783 ban_state.pnn = ctdb->cmd_pnn;
2784 ban_state.time = strtoul(argv[0], NULL, 0);
2786 if (ban_state.time == 0) {
2787 fprintf(stderr, "Ban time cannot be zero\n");
2791 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2792 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2794 fprintf(stderr, "Failed to ban node %u\n", ctdb->cmd_pnn);
2798 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, true);
2799 return ipreallocate(mem_ctx, ctdb);
2803 static int control_unban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2804 int argc, const char **argv)
2806 struct ctdb_ban_state ban_state;
2813 ret = check_flags(mem_ctx, ctdb, "banned",
2814 NODE_FLAGS_BANNED, false);
2819 ban_state.pnn = ctdb->cmd_pnn;
2822 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2823 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2825 fprintf(stderr, "Failed to unban node %u\n", ctdb->cmd_pnn);
2829 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, false);
2830 return ipreallocate(mem_ctx, ctdb);
2834 static int control_shutdown(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2835 int argc, const char **argv)
2843 ret = ctdb_ctrl_shutdown(mem_ctx, ctdb->ev, ctdb->client,
2844 ctdb->cmd_pnn, TIMEOUT());
2846 fprintf(stderr, "Unable to shutdown node %u\n", ctdb->cmd_pnn);
2853 static int get_generation(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2854 uint32_t *generation)
2858 struct ctdb_vnn_map *vnnmap;
2862 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2863 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
2865 fprintf(stderr, "Failed to find recovery master\n");
2869 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2870 recmaster, TIMEOUT(), &recmode);
2872 fprintf(stderr, "Failed to get recovery mode from node %u\n",
2877 if (recmode == CTDB_RECOVERY_ACTIVE) {
2882 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
2883 recmaster, TIMEOUT(), &vnnmap);
2885 fprintf(stderr, "Failed to get generation from node %u\n",
2890 if (vnnmap->generation == INVALID_GENERATION) {
2891 talloc_free(vnnmap);
2896 *generation = vnnmap->generation;
2897 talloc_free(vnnmap);
2902 static int control_recover(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2903 int argc, const char **argv)
2905 uint32_t generation, next_generation;
2912 ret = get_generation(mem_ctx, ctdb, &generation);
2917 ret = ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
2918 ctdb->cmd_pnn, TIMEOUT(),
2919 CTDB_RECOVERY_ACTIVE);
2921 fprintf(stderr, "Failed to set recovery mode active\n");
2926 ret = get_generation(mem_ctx, ctdb, &next_generation);
2929 "Failed to confirm end of recovery\n");
2933 if (next_generation != generation) {
2943 static int control_ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2944 int argc, const char **argv)
2947 usage("ipreallocate");
2950 return ipreallocate(mem_ctx, ctdb);
2953 static int control_isnotrecmaster(TALLOC_CTX *mem_ctx,
2954 struct ctdb_context *ctdb,
2955 int argc, const char **argv)
2961 usage("isnotrecmaster");
2964 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2965 ctdb->pnn, TIMEOUT(), &recmaster);
2967 fprintf(stderr, "Failed to get recmaster\n");
2971 if (recmaster != ctdb->pnn) {
2972 printf("this node is not the recmaster\n");
2976 printf("this node is the recmaster\n");
2980 static int control_gratarp(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2981 int argc, const char **argv)
2983 struct ctdb_addr_info addr_info;
2990 if (! parse_ip(argv[0], NULL, 0, &addr_info.addr)) {
2991 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
2994 addr_info.iface = argv[1];
2996 ret = ctdb_ctrl_send_gratuitous_arp(mem_ctx, ctdb->ev, ctdb->client,
2997 ctdb->cmd_pnn, TIMEOUT(),
3000 fprintf(stderr, "Unable to send gratuitous arp from node %u\n",
3008 static int control_tickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3009 int argc, const char **argv)
3011 ctdb_sock_addr src, dst;
3014 if (argc != 0 && argc != 2) {
3019 struct ctdb_connection *clist;
3023 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3029 for (i=0; i<count; i++) {
3030 ret = ctdb_sys_send_tcp(&clist[i].src,
3040 if (num_failed > 0) {
3041 fprintf(stderr, "Failed to send %d tickles\n",
3050 if (! parse_ip_port(argv[0], &src)) {
3051 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3055 if (! parse_ip_port(argv[1], &dst)) {
3056 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3060 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
3062 fprintf(stderr, "Failed to send tickle ack\n");
3069 static int control_gettickles(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3070 int argc, const char **argv)
3072 ctdb_sock_addr addr;
3073 struct ctdb_tickle_list *tickles;
3077 if (argc < 1 || argc > 2) {
3078 usage("gettickles");
3082 port = strtoul(argv[1], NULL, 10);
3085 if (! parse_ip(argv[0], NULL, port, &addr)) {
3086 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3090 ret = ctdb_ctrl_get_tcp_tickle_list(mem_ctx, ctdb->ev, ctdb->client,
3091 ctdb->cmd_pnn, TIMEOUT(), &addr,
3094 fprintf(stderr, "Failed to get list of connections\n");
3098 if (options.machinereadable) {
3099 printf("%s%s%s%s%s%s%s%s%s\n",
3101 "Source IP", options.sep,
3102 "Port", options.sep,
3103 "Destiation IP", options.sep,
3104 "Port", options.sep);
3105 for (i=0; i<tickles->num; i++) {
3106 printf("%s%s%s%u%s%s%s%u%s\n", options.sep,
3107 ctdb_sock_addr_to_string(
3108 mem_ctx, &tickles->conn[i].src),
3110 ntohs(tickles->conn[i].src.ip.sin_port),
3112 ctdb_sock_addr_to_string(
3113 mem_ctx, &tickles->conn[i].dst),
3115 ntohs(tickles->conn[i].dst.ip.sin_port),
3119 printf("Connections for IP: %s\n",
3120 ctdb_sock_addr_to_string(mem_ctx, &tickles->addr));
3121 printf("Num connections: %u\n", tickles->num);
3122 for (i=0; i<tickles->num; i++) {
3123 printf("SRC: %s:%u DST: %s:%u\n",
3124 ctdb_sock_addr_to_string(
3125 mem_ctx, &tickles->conn[i].src),
3126 ntohs(tickles->conn[i].src.ip.sin_port),
3127 ctdb_sock_addr_to_string(
3128 mem_ctx, &tickles->conn[i].dst),
3129 ntohs(tickles->conn[i].dst.ip.sin_port));
3133 talloc_free(tickles);
3137 typedef void (*clist_request_func)(struct ctdb_req_control *request,
3138 struct ctdb_connection *conn);
3140 typedef int (*clist_reply_func)(struct ctdb_reply_control *reply);
3142 struct process_clist_state {
3143 struct ctdb_connection *clist;
3145 int num_failed, num_total;
3146 clist_reply_func reply_func;
3149 static void process_clist_done(struct tevent_req *subreq);
3151 static struct tevent_req *process_clist_send(
3152 TALLOC_CTX *mem_ctx,
3153 struct ctdb_context *ctdb,
3154 struct ctdb_connection *clist,
3156 clist_request_func request_func,
3157 clist_reply_func reply_func)
3159 struct tevent_req *req, *subreq;
3160 struct process_clist_state *state;
3161 struct ctdb_req_control request;
3164 req = tevent_req_create(mem_ctx, &state, struct process_clist_state);
3169 state->clist = clist;
3170 state->count = count;
3171 state->reply_func = reply_func;
3173 for (i=0; i<count; i++) {
3174 request_func(&request, &clist[i]);
3175 subreq = ctdb_client_control_send(state, ctdb->ev,
3176 ctdb->client, ctdb->cmd_pnn,
3177 TIMEOUT(), &request);
3178 if (tevent_req_nomem(subreq, req)) {
3179 return tevent_req_post(req, ctdb->ev);
3181 tevent_req_set_callback(subreq, process_clist_done, req);
3187 static void process_clist_done(struct tevent_req *subreq)
3189 struct tevent_req *req = tevent_req_callback_data(
3190 subreq, struct tevent_req);
3191 struct process_clist_state *state = tevent_req_data(
3192 req, struct process_clist_state);
3193 struct ctdb_reply_control *reply;
3197 status = ctdb_client_control_recv(subreq, NULL, state, &reply);
3198 TALLOC_FREE(subreq);
3200 state->num_failed += 1;
3204 ret = state->reply_func(reply);
3206 state->num_failed += 1;
3211 state->num_total += 1;
3212 if (state->num_total == state->count) {
3213 tevent_req_done(req);
3217 static int process_clist_recv(struct tevent_req *req)
3219 struct process_clist_state *state = tevent_req_data(
3220 req, struct process_clist_state);
3222 return state->num_failed;
3225 static int control_addtickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3226 int argc, const char **argv)
3228 struct ctdb_connection conn;
3231 if (argc != 0 && argc != 2) {
3236 struct ctdb_connection *clist;
3237 struct tevent_req *req;
3240 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3248 req = process_clist_send(mem_ctx, ctdb, clist, count,
3249 ctdb_req_control_tcp_add_delayed_update,
3250 ctdb_reply_control_tcp_add_delayed_update);
3256 tevent_req_poll(req, ctdb->ev);
3259 ret = process_clist_recv(req);
3261 fprintf(stderr, "Failed to add %d tickles\n", ret);
3268 if (! parse_ip_port(argv[0], &conn.src)) {
3269 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3272 if (! parse_ip_port(argv[1], &conn.dst)) {
3273 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3277 ret = ctdb_ctrl_tcp_add_delayed_update(mem_ctx, ctdb->ev,
3278 ctdb->client, ctdb->cmd_pnn,
3281 fprintf(stderr, "Failed to register connection\n");
3288 static int control_deltickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3289 int argc, const char **argv)
3291 struct ctdb_connection conn;
3294 if (argc != 0 && argc != 2) {
3299 struct ctdb_connection *clist;
3300 struct tevent_req *req;
3303 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3311 req = process_clist_send(mem_ctx, ctdb, clist, count,
3312 ctdb_req_control_tcp_remove,
3313 ctdb_reply_control_tcp_remove);
3319 tevent_req_poll(req, ctdb->ev);
3322 ret = process_clist_recv(req);
3324 fprintf(stderr, "Failed to remove %d tickles\n", ret);
3331 if (! parse_ip_port(argv[0], &conn.src)) {
3332 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3335 if (! parse_ip_port(argv[1], &conn.dst)) {
3336 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3340 ret = ctdb_ctrl_tcp_remove(mem_ctx, ctdb->ev, ctdb->client,
3341 ctdb->cmd_pnn, TIMEOUT(), &conn);
3343 fprintf(stderr, "Failed to unregister connection\n");
3350 static int control_check_srvids(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3351 int argc, const char **argv)
3358 usage("check_srvids");
3361 srvid = talloc_array(mem_ctx, uint64_t, argc);
3362 if (srvid == NULL) {
3363 fprintf(stderr, "Memory allocation error\n");
3367 for (i=0; i<argc; i++) {
3368 srvid[i] = strtoull(argv[i], NULL, 0);
3371 ret = ctdb_ctrl_check_srvids(mem_ctx, ctdb->ev, ctdb->client,
3372 ctdb->cmd_pnn, TIMEOUT(), srvid, argc,
3375 fprintf(stderr, "Failed to check srvids on node %u\n",
3380 for (i=0; i<argc; i++) {
3381 printf("SRVID 0x%" PRIx64 " %s\n", srvid[i],
3382 (result[i] ? "exists" : "does not exist"));
3388 static int control_listnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3389 int argc, const char **argv)
3391 struct ctdb_node_map *nodemap;
3398 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
3399 if (nodemap == NULL) {
3403 for (i=0; i<nodemap->num; i++) {
3404 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
3408 if (options.machinereadable) {
3409 printf("%s%u%s%s%s\n", options.sep,
3410 nodemap->node[i].pnn, options.sep,
3411 ctdb_sock_addr_to_string(
3412 mem_ctx, &nodemap->node[i].addr),
3416 ctdb_sock_addr_to_string(
3417 mem_ctx, &nodemap->node[i].addr));
3424 static bool nodemap_identical(struct ctdb_node_map *nodemap1,
3425 struct ctdb_node_map *nodemap2)
3429 if (nodemap1->num != nodemap2->num) {
3433 for (i=0; i<nodemap1->num; i++) {
3434 struct ctdb_node_and_flags *n1, *n2;
3436 n1 = &nodemap1->node[i];
3437 n2 = &nodemap2->node[i];
3439 if ((n1->pnn != n2->pnn) ||
3440 (n1->flags != n2->flags) ||
3441 ! ctdb_sock_addr_same_ip(&n1->addr, &n2->addr)) {
3449 static int check_node_file_changes(TALLOC_CTX *mem_ctx,
3450 struct ctdb_node_map *nm,
3451 struct ctdb_node_map *fnm,
3455 bool check_failed = false;
3459 for (i=0; i<nm->num; i++) {
3460 if (i >= fnm->num) {
3462 "Node %u (%s) missing from nodes file\n",
3464 ctdb_sock_addr_to_string(
3465 mem_ctx, &nm->node[i].addr));
3466 check_failed = true;
3469 if (nm->node[i].flags & NODE_FLAGS_DELETED &&
3470 fnm->node[i].flags & NODE_FLAGS_DELETED) {
3471 /* Node remains deleted */
3475 if (! (nm->node[i].flags & NODE_FLAGS_DELETED) &&
3476 ! (fnm->node[i].flags & NODE_FLAGS_DELETED)) {
3477 /* Node not newly nor previously deleted */
3478 if (! ctdb_same_ip(&nm->node[i].addr,
3479 &fnm->node[i].addr)) {
3481 "Node %u has changed IP address"
3482 " (was %s, now %s)\n",
3484 ctdb_sock_addr_to_string(
3485 mem_ctx, &nm->node[i].addr),
3486 ctdb_sock_addr_to_string(
3487 mem_ctx, &fnm->node[i].addr));
3488 check_failed = true;
3490 if (nm->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3492 "WARNING: Node %u is disconnected."
3493 " You MUST fix this node manually!\n",
3500 if (fnm->node[i].flags & NODE_FLAGS_DELETED) {
3501 /* Node is being deleted */
3502 printf("Node %u is DELETED\n", nm->node[i].pnn);
3504 if (! (nm->node[i].flags & NODE_FLAGS_DISCONNECTED)) {
3506 "ERROR: Node %u is still connected\n",
3508 check_failed = true;
3513 if (nm->node[i].flags & NODE_FLAGS_DELETED) {
3514 /* Node was previously deleted */
3515 printf("Node %u is UNDELETED\n", nm->node[i].pnn);
3522 "ERROR: Nodes will not be reloaded due to previous error\n");
3526 /* Leftover nodes in file are NEW */
3527 for (; i < fnm->num; i++) {
3528 printf("Node %u is NEW\n", fnm->node[i].pnn);
3535 struct disable_recoveries_state {
3543 static void disable_recoveries_handler(uint64_t srvid, TDB_DATA data,
3546 struct disable_recoveries_state *state =
3547 (struct disable_recoveries_state *)private_data;
3550 if (data.dsize != sizeof(int)) {
3555 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
3556 ret = *(int *)data.dptr;
3558 state->status = ret;
3562 for (i=0; i<state->node_count; i++) {
3563 if (state->pnn_list[i] == ret) {
3564 state->reply[i] = true;
3570 for (i=0; i<state->node_count; i++) {
3571 if (! state->reply[i]) {
3572 state->done = false;
3578 static int disable_recoveries(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3579 uint32_t timeout, uint32_t *pnn_list, int count)
3581 struct ctdb_disable_message disable = { 0 };
3582 struct disable_recoveries_state state;
3585 disable.pnn = ctdb->pnn;
3586 disable.srvid = next_srvid(ctdb);
3587 disable.timeout = timeout;
3589 state.pnn_list = pnn_list;
3590 state.node_count = count;
3593 state.reply = talloc_zero_array(mem_ctx, bool, count);
3594 if (state.reply == NULL) {
3598 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
3600 disable_recoveries_handler,
3606 for (i=0; i<count; i++) {
3607 ret = ctdb_message_disable_recoveries(mem_ctx, ctdb->ev,
3616 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
3618 fprintf(stderr, "Timed out waiting to disable recoveries\n");
3620 ret = (state.status >= 0 ? 0 : 1);
3624 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
3625 disable.srvid, &state);
3629 static int control_reloadnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3630 int argc, const char **argv)
3632 struct ctdb_node_map *nodemap = NULL;
3633 struct ctdb_node_map *file_nodemap;
3634 struct ctdb_node_map *remote_nodemap;
3635 struct ctdb_req_control request;
3636 struct ctdb_reply_control **reply;
3642 nodemap = get_nodemap(ctdb, false);
3643 if (nodemap == NULL) {
3647 file_nodemap = read_nodes_file(mem_ctx, ctdb->pnn);
3648 if (file_nodemap == NULL) {
3652 for (i=0; i<nodemap->num; i++) {
3653 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3657 ret = ctdb_ctrl_get_nodes_file(mem_ctx, ctdb->ev, ctdb->client,
3658 nodemap->node[i].pnn, TIMEOUT(),
3662 "ERROR: Failed to get nodes file from node %u\n",
3663 nodemap->node[i].pnn);
3667 if (! nodemap_identical(file_nodemap, remote_nodemap)) {
3669 "ERROR: Nodes file on node %u differs"
3670 " from current node (%u)\n",
3671 nodemap->node[i].pnn, ctdb->pnn);
3676 ret = check_node_file_changes(mem_ctx, nodemap, file_nodemap, &reload);
3682 fprintf(stderr, "No change in nodes file,"
3683 " skipping unnecessary reload\n");
3687 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
3688 mem_ctx, &pnn_list);
3690 fprintf(stderr, "Memory allocation error\n");
3694 ret = disable_recoveries(mem_ctx, ctdb, 2*options.timelimit,
3697 fprintf(stderr, "Failed to disable recoveries\n");
3701 ctdb_req_control_reload_nodes_file(&request);
3702 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3703 pnn_list, count, TIMEOUT(),
3704 &request, NULL, &reply);
3706 bool failed = false;
3708 for (i=0; i<count; i++) {
3709 ret = ctdb_reply_control_reload_nodes_file(reply[i]);
3712 "Node %u failed to reload nodes\n",
3719 "You MUST fix failed nodes manually!\n");
3723 ret = disable_recoveries(mem_ctx, ctdb, 0, pnn_list, count);
3725 fprintf(stderr, "Failed to enable recoveries\n");
3732 static int moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3733 ctdb_sock_addr *addr, uint32_t pnn)
3735 struct ctdb_public_ip_list *pubip_list;
3736 struct ctdb_public_ip pubip;
3737 struct ctdb_node_map *nodemap;
3738 struct ctdb_req_control request;
3742 ret = ctdb_message_disable_ip_check(mem_ctx, ctdb->ev, ctdb->client,
3743 CTDB_BROADCAST_CONNECTED,
3744 2*options.timelimit);
3746 fprintf(stderr, "Failed to disable IP check\n");
3750 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3751 pnn, TIMEOUT(), &pubip_list);
3753 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3758 for (i=0; i<pubip_list->num; i++) {
3759 if (ctdb_same_ip(addr, &pubip_list->ip[i].addr)) {
3764 if (i == pubip_list->num) {
3765 fprintf(stderr, "Node %u CANNOT host IP address %s\n",
3766 pnn, ctdb_sock_addr_to_string(mem_ctx, addr));
3770 nodemap = get_nodemap(ctdb, false);
3771 if (nodemap == NULL) {
3775 count = list_of_active_nodes(nodemap, pnn, mem_ctx, &pnn_list);
3777 fprintf(stderr, "Memory allocation error\n");
3783 ctdb_req_control_release_ip(&request, &pubip);
3785 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3786 pnn_list, count, TIMEOUT(),
3787 &request, NULL, NULL);
3789 fprintf(stderr, "Failed to release IP on nodes\n");
3793 ret = ctdb_ctrl_takeover_ip(mem_ctx, ctdb->ev, ctdb->client,
3794 pnn, TIMEOUT(), &pubip);
3796 fprintf(stderr, "Failed to takeover IP on node %u\n", pnn);
3803 static int control_moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3804 int argc, const char **argv)
3806 ctdb_sock_addr addr;
3808 int ret, retries = 0;
3814 if (! parse_ip(argv[0], NULL, 0, &addr)) {
3815 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3819 pnn = strtoul(argv[1], NULL, 10);
3820 if (pnn == CTDB_UNKNOWN_PNN) {
3821 fprintf(stderr, "Invalid PNN %s\n", argv[1]);
3825 while (retries < 5) {
3826 ret = moveip(mem_ctx, ctdb, &addr, pnn);
3836 fprintf(stderr, "Failed to move IP %s to node %u\n",
3844 static int rebalancenode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3849 ret = ctdb_message_rebalance_node(mem_ctx, ctdb->ev, ctdb->client,
3850 CTDB_BROADCAST_CONNECTED, pnn);
3853 "Failed to ask recovery master to distribute IPs\n");
3860 static int control_addip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3861 int argc, const char **argv)
3863 ctdb_sock_addr addr;
3864 struct ctdb_public_ip_list *pubip_list;
3865 struct ctdb_addr_info addr_info;
3867 int ret, i, retries = 0;
3873 if (! parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
3874 fprintf(stderr, "Invalid IP/Mask %s\n", argv[0]);
3878 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3879 ctdb->cmd_pnn, TIMEOUT(), &pubip_list);
3881 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3886 for (i=0; i<pubip_list->num; i++) {
3887 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3888 fprintf(stderr, "Node already knows about IP %s\n",
3889 ctdb_sock_addr_to_string(mem_ctx, &addr));
3894 addr_info.addr = addr;
3895 addr_info.mask = mask;
3896 addr_info.iface = argv[1];
3898 while (retries < 5) {
3899 ret = ctdb_ctrl_add_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3900 ctdb->cmd_pnn, TIMEOUT(),
3911 fprintf(stderr, "Failed to add public IP to node %u."
3912 " Giving up\n", ctdb->cmd_pnn);
3916 ret = rebalancenode(mem_ctx, ctdb, ctdb->cmd_pnn);
3924 static int control_delip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3925 int argc, const char **argv)
3927 ctdb_sock_addr addr;
3928 struct ctdb_public_ip_list *pubip_list;
3929 struct ctdb_addr_info addr_info;
3936 if (! parse_ip(argv[0], NULL, 0, &addr)) {
3937 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3941 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3942 ctdb->cmd_pnn, TIMEOUT(), &pubip_list);
3944 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3949 for (i=0; i<pubip_list->num; i++) {
3950 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3955 if (i == pubip_list->num) {
3956 fprintf(stderr, "Node does not know about IP address %s\n",
3957 ctdb_sock_addr_to_string(mem_ctx, &addr));
3961 addr_info.addr = addr;
3963 addr_info.iface = NULL;
3965 ret = ctdb_ctrl_del_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3966 ctdb->cmd_pnn, TIMEOUT(), &addr_info);
3968 fprintf(stderr, "Failed to delete public IP from node %u\n",
3976 static int control_eventscript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3977 int argc, const char **argv)
3982 usage("eventscript");
3985 if (strcmp(argv[0], "monitor") != 0) {
3986 fprintf(stderr, "Only monitor event can be run\n");
3990 ret = ctdb_ctrl_run_eventscripts(mem_ctx, ctdb->ev, ctdb->client,
3991 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
3993 fprintf(stderr, "Failed to run monitor event on node %u\n",
4001 #define DB_VERSION 3
4002 #define MAX_DB_NAME 64
4003 #define MAX_REC_BUFFER_SIZE (100*1000)
4006 unsigned long version;
4008 unsigned long flags;
4011 char name[MAX_DB_NAME];
4014 struct backup_state {
4015 TALLOC_CTX *mem_ctx;
4016 struct ctdb_rec_buffer *recbuf;
4019 unsigned int nbuf, nrec;
4022 static int backup_handler(uint32_t reqid, struct ctdb_ltdb_header *header,
4023 TDB_DATA key, TDB_DATA data, void *private_data)
4025 struct backup_state *state = (struct backup_state *)private_data;
4029 if (state->recbuf == NULL) {
4030 state->recbuf = ctdb_rec_buffer_init(state->mem_ctx,
4032 if (state->recbuf == NULL) {
4037 ret = ctdb_rec_buffer_add(state->recbuf, state->recbuf, reqid,
4043 len = ctdb_rec_buffer_len(state->recbuf);
4044 if (len < MAX_REC_BUFFER_SIZE) {
4048 ret = ctdb_rec_buffer_write(state->recbuf, state->fd);
4050 fprintf(stderr, "Failed to write records to backup file\n");
4055 state->nrec += state->recbuf->count;
4056 TALLOC_FREE(state->recbuf);
4061 static int control_backupdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4062 int argc, const char **argv)
4064 const char *db_name;
4065 struct ctdb_db_context *db;
4068 struct backup_state state;
4069 struct db_header db_hdr;
4076 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4080 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4083 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4087 fd = open(argv[1], O_RDWR|O_CREAT, 0600);
4090 fprintf(stderr, "Failed to open file %s for writing\n",
4095 /* Write empty header first */
4096 ZERO_STRUCT(db_hdr);
4097 ret = write(fd, &db_hdr, sizeof(struct db_header));
4101 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4105 state.mem_ctx = mem_ctx;
4106 state.recbuf = NULL;
4111 ret = ctdb_db_traverse(db, true, false, backup_handler, &state);
4113 fprintf(stderr, "Failed to collect records from DB %s\n",
4119 if (state.recbuf != NULL) {
4120 ret = ctdb_rec_buffer_write(state.recbuf, state.fd);
4123 "Failed to write records to backup file\n");
4129 state.nrec += state.recbuf->count;
4130 TALLOC_FREE(state.recbuf);
4133 db_hdr.version = DB_VERSION;
4134 db_hdr.timestamp = time(NULL);
4135 db_hdr.flags = db_flags;
4136 db_hdr.nbuf = state.nbuf;
4137 db_hdr.nrec = state.nrec;
4138 strncpy(db_hdr.name, db_name, MAX_DB_NAME-1);
4140 lseek(fd, 0, SEEK_SET);
4141 ret = write(fd, &db_hdr, sizeof(struct db_header));
4145 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4150 printf("Database backed up to %s\n", argv[1]);
4154 static int control_restoredb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4155 int argc, const char **argv)
4157 const char *db_name = NULL;
4158 struct ctdb_db_context *db;
4159 struct db_header db_hdr;
4160 struct ctdb_node_map *nodemap;
4161 struct ctdb_req_control request;
4162 struct ctdb_reply_control **reply;
4163 struct ctdb_transdb wipedb;
4164 struct ctdb_pulldb_ext pulldb;
4165 struct ctdb_rec_buffer *recbuf;
4166 uint32_t generation;
4172 if (argc < 1 || argc > 2) {
4176 fd = open(argv[0], O_RDONLY, 0600);
4179 fprintf(stderr, "Failed to open file %s for reading\n",
4188 ret = read(fd, &db_hdr, sizeof(struct db_header));
4192 fprintf(stderr, "Failed to read db header from file %s\n",
4196 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4198 if (db_hdr.version != DB_VERSION) {
4200 "Wrong version of backup file, expected %u, got %lu\n",
4201 DB_VERSION, db_hdr.version);
4206 if (db_name == NULL) {
4207 db_name = db_hdr.name;
4210 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4211 localtime(&db_hdr.timestamp));
4212 printf("Restoring database %s from backup @ %s\n", db_name, timebuf);
4214 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4217 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4222 nodemap = get_nodemap(ctdb, false);
4223 if (nodemap == NULL) {
4224 fprintf(stderr, "Failed to get nodemap\n");
4229 ret = get_generation(mem_ctx, ctdb, &generation);
4231 fprintf(stderr, "Failed to get current generation\n");
4236 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4243 wipedb.db_id = ctdb_db_id(db);
4244 wipedb.tid = generation;
4246 ctdb_req_control_db_freeze(&request, wipedb.db_id);
4247 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4248 ctdb->client, pnn_list, count,
4249 TIMEOUT(), &request, NULL, NULL);
4255 ctdb_req_control_db_transaction_start(&request, &wipedb);
4256 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4257 pnn_list, count, TIMEOUT(),
4258 &request, NULL, NULL);
4263 ctdb_req_control_wipe_database(&request, &wipedb);
4264 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4265 pnn_list, count, TIMEOUT(),
4266 &request, NULL, NULL);
4271 pulldb.db_id = ctdb_db_id(db);
4273 pulldb.srvid = SRVID_CTDB_PUSHDB;
4275 ctdb_req_control_db_push_start(&request, &pulldb);
4276 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4277 pnn_list, count, TIMEOUT(),
4278 &request, NULL, NULL);
4283 for (i=0; i<db_hdr.nbuf; i++) {
4284 struct ctdb_req_message message;
4287 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4292 data.dsize = ctdb_rec_buffer_len(recbuf);
4293 data.dptr = talloc_size(mem_ctx, data.dsize);
4294 if (data.dptr == NULL) {
4298 ctdb_rec_buffer_push(recbuf, data.dptr);
4300 message.srvid = pulldb.srvid;
4301 message.data.data = data;
4303 ret = ctdb_client_message_multi(mem_ctx, ctdb->ev,
4311 talloc_free(recbuf);
4312 talloc_free(data.dptr);
4315 ctdb_req_control_db_push_confirm(&request, pulldb.db_id);
4316 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4317 pnn_list, count, TIMEOUT(),
4318 &request, NULL, &reply);
4323 for (i=0; i<count; i++) {
4324 uint32_t num_records;
4326 ret = ctdb_reply_control_db_push_confirm(reply[i],
4329 fprintf(stderr, "Invalid response from node %u\n",
4334 if (num_records != db_hdr.nrec) {
4335 fprintf(stderr, "Node %u received %u of %lu records\n",
4336 pnn_list[i], num_records, db_hdr.nrec);
4341 ctdb_req_control_db_set_healthy(&request, wipedb.db_id);
4342 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4343 pnn_list, count, TIMEOUT(),
4344 &request, NULL, NULL);
4349 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4350 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4351 pnn_list, count, TIMEOUT(),
4352 &request, NULL, NULL);
4357 ctdb_req_control_db_thaw(&request, wipedb.db_id);
4358 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4359 ctdb->client, pnn_list, count,
4360 TIMEOUT(), &request, NULL, NULL);
4365 printf("Database %s restored\n", db_name);
4371 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4372 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4376 struct dumpdbbackup_state {
4377 ctdb_rec_parser_func_t parser;
4378 struct dump_record_state sub_state;
4381 static int dumpdbbackup_handler(uint32_t reqid,
4382 struct ctdb_ltdb_header *header,
4383 TDB_DATA key, TDB_DATA data,
4386 struct dumpdbbackup_state *state =
4387 (struct dumpdbbackup_state *)private_data;
4388 struct ctdb_ltdb_header hdr;
4391 ret = ctdb_ltdb_header_extract(&data, &hdr);
4396 return state->parser(reqid, &hdr, key, data, &state->sub_state);
4399 static int control_dumpdbbackup(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4400 int argc, const char **argv)
4402 struct db_header db_hdr;
4404 struct dumpdbbackup_state state;
4408 usage("dumpbackup");
4411 fd = open(argv[0], O_RDONLY, 0600);
4414 fprintf(stderr, "Failed to open file %s for reading\n",
4419 ret = read(fd, &db_hdr, sizeof(struct db_header));
4423 fprintf(stderr, "Failed to read db header from file %s\n",
4427 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4429 if (db_hdr.version != DB_VERSION) {
4431 "Wrong version of backup file, expected %u, got %lu\n",
4432 DB_VERSION, db_hdr.version);
4437 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4438 localtime(&db_hdr.timestamp));
4439 printf("Dumping database %s from backup @ %s\n",
4440 db_hdr.name, timebuf);
4442 state.parser = dump_record;
4443 state.sub_state.count = 0;
4445 for (i=0; i<db_hdr.nbuf; i++) {
4446 struct ctdb_rec_buffer *recbuf;
4448 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4450 fprintf(stderr, "Failed to read records\n");
4455 ret = ctdb_rec_buffer_traverse(recbuf, dumpdbbackup_handler,
4458 fprintf(stderr, "Failed to dump records\n");
4465 printf("Dumped %u record(s)\n", state.sub_state.count);
4469 static int control_wipedb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4470 int argc, const char **argv)
4472 const char *db_name;
4473 struct ctdb_db_context *db;
4476 struct ctdb_node_map *nodemap;
4477 struct ctdb_req_control request;
4478 struct ctdb_transdb wipedb;
4479 uint32_t generation;
4487 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4491 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4494 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4498 nodemap = get_nodemap(ctdb, false);
4499 if (nodemap == NULL) {
4500 fprintf(stderr, "Failed to get nodemap\n");
4504 ret = get_generation(mem_ctx, ctdb, &generation);
4506 fprintf(stderr, "Failed to get current generation\n");
4510 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4516 ctdb_req_control_db_freeze(&request, db_id);
4517 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4518 ctdb->client, pnn_list, count,
4519 TIMEOUT(), &request, NULL, NULL);
4524 wipedb.db_id = db_id;
4525 wipedb.tid = generation;
4527 ctdb_req_control_db_transaction_start(&request, &wipedb);
4528 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4529 pnn_list, count, TIMEOUT(),
4530 &request, NULL, NULL);
4535 ctdb_req_control_wipe_database(&request, &wipedb);
4536 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4537 pnn_list, count, TIMEOUT(),
4538 &request, NULL, NULL);
4543 ctdb_req_control_db_set_healthy(&request, db_id);
4544 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4545 pnn_list, count, TIMEOUT(),
4546 &request, NULL, NULL);
4551 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4552 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4553 pnn_list, count, TIMEOUT(),
4554 &request, NULL, NULL);
4559 ctdb_req_control_db_thaw(&request, db_id);
4560 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4561 ctdb->client, pnn_list, count,
4562 TIMEOUT(), &request, NULL, NULL);
4567 printf("Database %s wiped\n", db_name);
4572 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4573 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4577 static int control_recmaster(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4578 int argc, const char **argv)
4583 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
4584 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
4589 printf("%u\n", recmaster);
4593 static void print_scriptstatus_one(struct ctdb_script_list *slist,
4594 const char *event_str)
4599 if (slist == NULL) {
4600 if (! options.machinereadable) {
4601 printf("%s cycle never run\n", event_str);
4606 for (i=0; i<slist->num_scripts; i++) {
4607 if (slist->script[i].status != -ENOEXEC) {
4612 if (! options.machinereadable) {
4613 printf("%d scripts were executed last %s cycle\n",
4614 num_run, event_str);
4617 for (i=0; i<slist->num_scripts; i++) {
4618 const char *status = NULL;
4620 switch (slist->script[i].status) {
4622 status = "TIMEDOUT";
4625 status = "DISABLED";
4631 if (slist->script[i].status > 0) {
4637 if (options.machinereadable) {
4638 printf("%s%s%s%s%s%i%s%s%s%lu.%06lu%s%lu.%06lu%s%s%s\n",
4640 event_str, options.sep,
4641 slist->script[i].name, options.sep,
4642 slist->script[i].status, options.sep,
4643 status, options.sep,
4644 (unsigned long)slist->script[i].start.tv_sec,
4645 slist->script[i].start.tv_usec, options.sep,
4646 (unsigned long)slist->script[i].finished.tv_sec,
4647 slist->script[i].finished.tv_usec, options.sep,
4648 slist->script[i].output, options.sep);
4653 printf("%-20s Status:%s ",
4654 slist->script[i].name, status);
4656 /* Some other error, eg from stat. */
4657 printf("%-20s Status:CANNOT RUN (%s)",
4658 slist->script[i].name,
4659 strerror(-slist->script[i].status));
4662 if (slist->script[i].status >= 0) {
4663 printf("Duration:%.3lf ",
4664 timeval_delta(&slist->script[i].finished,
4665 &slist->script[i].start));
4667 if (slist->script[i].status != -ENOEXEC) {
4668 printf("%s", ctime(&slist->script[i].start.tv_sec));
4669 if (slist->script[i].status != 0) {
4670 printf(" OUTPUT:%s\n",
4671 slist->script[i].output);
4679 static void print_scriptstatus(struct ctdb_script_list **slist,
4680 int count, const char **event_str)
4684 if (options.machinereadable) {
4685 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
4687 "Type", options.sep,
4688 "Name", options.sep,
4689 "Code", options.sep,
4690 "Status", options.sep,
4691 "Start", options.sep,
4693 "Error Output", options.sep);
4696 for (i=0; i<count; i++) {
4697 print_scriptstatus_one(slist[i], event_str[i]);
4701 static int control_scriptstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4702 int argc, const char **argv)
4704 struct ctdb_script_list **slist;
4705 const char *event_str;
4706 enum ctdb_event event;
4707 const char *all_events[] = {
4708 "init", "setup", "startup", "monitor",
4709 "takeip", "releaseip", "updateip", "ipreallocated" };
4712 int count, start, end, num;
4715 usage("scriptstatus");
4719 event_str = "monitor";
4721 event_str = argv[0];
4727 for (i=0; i<ARRAY_SIZE(all_events); i++) {
4728 if (strcmp(event_str, all_events[i]) == 0) {
4737 if (strcmp(event_str, "all") == 0) {
4739 count = ARRAY_SIZE(all_events);
4745 fprintf(stderr, "Unknown event name %s\n", argv[0]);
4746 usage("scriptstatus");
4749 slist = talloc_array(mem_ctx, struct ctdb_script_list *, count);
4750 if (slist == NULL) {
4751 fprintf(stderr, "Memory allocation error\n");
4756 for (i=start; i<end; i++) {
4757 event = ctdb_event_from_string(all_events[i]);
4759 ret = ctdb_ctrl_get_event_script_status(mem_ctx, ctdb->ev,
4766 "failed to get script status for %s event\n",
4771 if (slist[num] == NULL) {
4776 /* The ETIME status is ignored for certain events.
4777 * In that case the status is 0, but endtime is not set.
4779 for (j=0; j<slist[num]->num_scripts; j++) {
4780 if (slist[num]->script[j].status == 0 &&
4781 timeval_is_zero(&slist[num]->script[j].finished)) {
4782 slist[num]->script[j].status = -ETIME;
4789 print_scriptstatus(slist, count, &all_events[start]);
4793 static int control_enablescript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4794 int argc, const char **argv)
4799 usage("enablescript");
4802 ret = ctdb_ctrl_enable_script(mem_ctx, ctdb->ev, ctdb->client,
4803 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4805 fprintf(stderr, "Failed to enable script %s\n", argv[0]);
4812 static int control_disablescript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4813 int argc, const char **argv)
4818 usage("disablescript");
4821 ret = ctdb_ctrl_disable_script(mem_ctx, ctdb->ev, ctdb->client,
4822 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4824 fprintf(stderr, "Failed to disable script %s\n", argv[0]);
4831 static int control_natgw(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4832 int argc, const char **argv)
4834 char *t, *natgw_helper = NULL;
4840 t = getenv("CTDB_NATGW_HELPER");
4842 natgw_helper = talloc_strdup(mem_ctx, t);
4844 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4845 CTDB_HELPER_BINDIR);
4848 if (natgw_helper == NULL) {
4849 fprintf(stderr, "Unable to set NAT gateway helper\n");
4853 return run_helper("NAT gateway helper", natgw_helper, argv[0]);
4856 static int control_natgwlist(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4857 int argc, const char **argv)
4859 char *t, *natgw_helper = NULL;
4865 t = getenv("CTDB_NATGW_HELPER");
4867 natgw_helper = talloc_strdup(mem_ctx, t);
4869 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4870 CTDB_HELPER_BINDIR);
4873 if (natgw_helper == NULL) {
4874 fprintf(stderr, "Unable to set NAT gateway helper\n");
4878 return run_helper("NAT gateway helper", natgw_helper, "natgwlist");
4882 * Find the PNN of the current node
4883 * discover the pnn by loading the nodes file and try to bind
4884 * to all addresses one at a time until the ip address is found.
4886 static bool find_node_xpnn(TALLOC_CTX *mem_ctx, uint32_t *pnn)
4888 struct ctdb_node_map *nodemap;
4891 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
4892 if (nodemap == NULL) {
4896 for (i=0; i<nodemap->num; i++) {
4897 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
4900 if (ctdb_sys_have_ip(&nodemap->node[i].addr)) {
4902 *pnn = nodemap->node[i].pnn;
4904 talloc_free(nodemap);
4909 fprintf(stderr, "Failed to detect PNN of the current node.\n");
4910 talloc_free(nodemap);
4914 static int control_getreclock(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4915 int argc, const char **argv)
4917 const char *reclock;
4921 usage("getreclock");
4924 ret = ctdb_ctrl_get_reclock_file(mem_ctx, ctdb->ev, ctdb->client,
4925 ctdb->cmd_pnn, TIMEOUT(), &reclock);
4930 if (reclock != NULL) {
4931 printf("%s\n", reclock);
4937 static int control_setlmasterrole(TALLOC_CTX *mem_ctx,
4938 struct ctdb_context *ctdb,
4939 int argc, const char **argv)
4941 uint32_t lmasterrole = 0;
4945 usage("setlmasterrole");
4948 if (strcmp(argv[0], "on") == 0) {
4950 } else if (strcmp(argv[0], "off") == 0) {
4953 usage("setlmasterrole");
4956 ret = ctdb_ctrl_set_lmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4957 ctdb->cmd_pnn, TIMEOUT(), lmasterrole);
4965 static int control_setrecmasterrole(TALLOC_CTX *mem_ctx,
4966 struct ctdb_context *ctdb,
4967 int argc, const char **argv)
4969 uint32_t recmasterrole = 0;
4973 usage("setrecmasterrole");
4976 if (strcmp(argv[0], "on") == 0) {
4978 } else if (strcmp(argv[0], "off") == 0) {
4981 usage("setrecmasterrole");
4984 ret = ctdb_ctrl_set_recmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4985 ctdb->cmd_pnn, TIMEOUT(),
4994 static int control_setdbreadonly(TALLOC_CTX *mem_ctx,
4995 struct ctdb_context *ctdb,
4996 int argc, const char **argv)
5003 usage("setdbreadonly");
5006 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
5010 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5011 fprintf(stderr, "Cannot set READONLY on persistent DB\n");
5015 ret = ctdb_ctrl_set_db_readonly(mem_ctx, ctdb->ev, ctdb->client,
5016 ctdb->cmd_pnn, TIMEOUT(), db_id);
5024 static int control_setdbsticky(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5025 int argc, const char **argv)
5032 usage("setdbsticky");
5035 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
5039 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5040 fprintf(stderr, "Cannot set STICKY on persistent DB\n");
5044 ret = ctdb_ctrl_set_db_sticky(mem_ctx, ctdb->ev, ctdb->client,
5045 ctdb->cmd_pnn, TIMEOUT(), db_id);
5053 static int control_pfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5054 int argc, const char **argv)
5056 const char *db_name;
5057 struct ctdb_db_context *db;
5058 struct ctdb_transaction_handle *h;
5063 if (argc < 2 || argc > 3) {
5067 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5071 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5072 fprintf(stderr, "DB %s is not a persistent database\n",
5077 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5080 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5084 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5086 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5090 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5091 TIMEOUT(), db, true, &h);
5093 fprintf(stderr, "Failed to start transaction on db %s\n",
5098 ret = ctdb_transaction_fetch_record(h, key, mem_ctx, &data);
5100 fprintf(stderr, "Failed to read record for key %s\n",
5102 ctdb_transaction_cancel(h);
5106 printf("%.*s\n", (int)data.dsize, data.dptr);
5108 ctdb_transaction_cancel(h);
5112 static int control_pstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5113 int argc, const char **argv)
5115 const char *db_name;
5116 struct ctdb_db_context *db;
5117 struct ctdb_transaction_handle *h;
5126 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5130 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5131 fprintf(stderr, "DB %s is not a persistent database\n",
5136 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5139 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5143 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5145 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5149 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5151 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5155 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5156 TIMEOUT(), db, false, &h);
5158 fprintf(stderr, "Failed to start transaction on db %s\n",
5163 ret = ctdb_transaction_store_record(h, key, data);
5165 fprintf(stderr, "Failed to store record for key %s\n",
5167 ctdb_transaction_cancel(h);
5171 ret = ctdb_transaction_commit(h);
5173 fprintf(stderr, "Failed to commit transaction on db %s\n",
5175 ctdb_transaction_cancel(h);
5182 static int control_pdelete(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5183 int argc, const char **argv)
5185 const char *db_name;
5186 struct ctdb_db_context *db;
5187 struct ctdb_transaction_handle *h;
5196 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5200 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5201 fprintf(stderr, "DB %s is not a persistent database\n",
5206 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5209 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5213 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5215 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5219 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5220 TIMEOUT(), db, false, &h);
5222 fprintf(stderr, "Failed to start transaction on db %s\n",
5227 ret = ctdb_transaction_delete_record(h, key);
5229 fprintf(stderr, "Failed to delete record for key %s\n",
5231 ctdb_transaction_cancel(h);
5235 ret = ctdb_transaction_commit(h);
5237 fprintf(stderr, "Failed to commit transaction on db %s\n",
5239 ctdb_transaction_cancel(h);
5246 static int ptrans_parse_string(TALLOC_CTX *mem_ctx, const char **ptr, TDB_DATA *data)
5254 /* Skip whitespace */
5255 n = strspn(*ptr, " \t");
5259 /* Quoted ASCII string - no wide characters! */
5261 n = strcspn(t, "\"");
5264 ret = str_to_data(t, n, mem_ctx, data);
5271 fprintf(stderr, "Unmatched \" in input %s\n", *ptr);
5275 fprintf(stderr, "Unsupported input format in %s\n", *ptr);
5282 #define MAX_LINE_SIZE 1024
5284 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
5285 TDB_DATA *key, TDB_DATA *value)
5287 char line [MAX_LINE_SIZE]; /* FIXME: make this more flexible? */
5291 ptr = fgets(line, MAX_LINE_SIZE, file);
5297 ret = ptrans_parse_string(mem_ctx, &ptr, key);
5298 if (ret != 0 || ptr == NULL || key->dptr == NULL) {
5299 /* Line Ignored but not EOF */
5305 ret = ptrans_parse_string(mem_ctx, &ptr, value);
5307 /* Line Ignored but not EOF */
5308 talloc_free(key->dptr);
5316 static int control_ptrans(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5317 int argc, const char **argv)
5319 const char *db_name;
5320 struct ctdb_db_context *db;
5321 struct ctdb_transaction_handle *h;
5324 TDB_DATA key, value;
5327 if (argc < 1 || argc > 2) {
5331 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5335 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5336 fprintf(stderr, "DB %s is not a persistent database\n",
5342 file = fopen(argv[1], "r");
5344 fprintf(stderr, "Failed to open file %s\n", argv[1]);
5351 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5354 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5358 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5359 TIMEOUT(), db, false, &h);
5361 fprintf(stderr, "Failed to start transaction on db %s\n",
5366 while (ptrans_get_key_value(mem_ctx, file, &key, &value)) {
5367 if (key.dsize != 0) {
5368 ret = ctdb_transaction_store_record(h, key, value);
5370 fprintf(stderr, "Failed to store record\n");
5371 ctdb_transaction_cancel(h);
5374 talloc_free(key.dptr);
5375 talloc_free(value.dptr);
5379 ret = ctdb_transaction_commit(h);
5381 fprintf(stderr, "Failed to commit transaction on db %s\n",
5383 ctdb_transaction_cancel(h);
5387 if (file != stdin) {
5393 static int control_tfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5394 int argc, const char **argv)
5396 struct tdb_context *tdb;
5398 struct ctdb_ltdb_header header;
5401 if (argc < 2 || argc > 3) {
5405 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5407 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5411 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5413 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5418 data = tdb_fetch(tdb, key);
5419 if (data.dptr == NULL) {
5420 fprintf(stderr, "No record for key %s\n", argv[1]);
5425 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
5426 fprintf(stderr, "Invalid record for key %s\n", argv[1]);
5437 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
5439 fprintf(stderr, "Failed to open output file %s\n",
5444 nwritten = sys_write(fd, data.dptr, data.dsize);
5445 if (nwritten != data.dsize) {
5446 fprintf(stderr, "Failed to write record to file\n");
5455 ret = ctdb_ltdb_header_extract(&data, &header);
5457 fprintf(stderr, "Failed to parse header from data\n");
5461 dump_ltdb_header(&header);
5462 dump_tdb_data("data", data);
5467 static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5468 int argc, const char **argv)
5470 struct tdb_context *tdb;
5471 TDB_DATA key, data[2], value;
5472 struct ctdb_ltdb_header header;
5473 uint8_t header_buf[sizeof(struct ctdb_ltdb_header)];
5476 if (argc < 3 || argc > 5) {
5480 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5482 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5486 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5488 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5493 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &value);
5495 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5500 ZERO_STRUCT(header);
5503 header.rsn = (uint64_t)strtoull(argv[3], NULL, 0);
5506 header.dmaster = (uint32_t)atol(argv[4]);
5509 header.flags = (uint32_t)atol(argv[5]);
5512 ctdb_ltdb_header_push(&header, header_buf);
5514 data[0].dsize = ctdb_ltdb_header_len(&header);
5515 data[0].dptr = header_buf;
5517 data[1].dsize = value.dsize;
5518 data[1].dptr = value.dptr;
5520 ret = tdb_storev(tdb, key, data, 2, TDB_REPLACE);
5522 fprintf(stderr, "Failed to write record %s to file %s\n",
5531 static int control_readkey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5532 int argc, const char **argv)
5534 const char *db_name;
5535 struct ctdb_db_context *db;
5536 struct ctdb_record_handle *h;
5539 bool readonly = false;
5542 if (argc < 2 || argc > 3) {
5547 if (strcmp(argv[2], "readonly") == 0) {
5554 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5558 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5559 fprintf(stderr, "DB %s is not a volatile database\n",
5564 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5567 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5571 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5573 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5577 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5578 db, key, readonly, &h, NULL, &data);
5580 fprintf(stderr, "Failed to read record for key %s\n",
5583 printf("Data: size:%zu ptr:[%.*s]\n", data.dsize,
5584 (int)data.dsize, data.dptr);
5591 static int control_writekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5592 int argc, const char **argv)
5594 const char *db_name;
5595 struct ctdb_db_context *db;
5596 struct ctdb_record_handle *h;
5605 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5609 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5610 fprintf(stderr, "DB %s is not a volatile database\n",
5615 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5618 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5622 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5624 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5628 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5630 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5634 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5635 db, key, false, &h, NULL, NULL);
5637 fprintf(stderr, "Failed to lock record for key %s\n", argv[0]);
5641 ret = ctdb_store_record(h, data);
5643 fprintf(stderr, "Failed to store record for key %s\n",
5651 static int control_deletekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5652 int argc, const char **argv)
5654 const char *db_name;
5655 struct ctdb_db_context *db;
5656 struct ctdb_record_handle *h;
5665 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5669 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5670 fprintf(stderr, "DB %s is not a volatile database\n",
5675 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5678 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5682 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5684 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5688 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5689 db, key, false, &h, NULL, &data);
5691 fprintf(stderr, "Failed to fetch record for key %s\n",
5696 ret = ctdb_delete_record(h);
5698 fprintf(stderr, "Failed to delete record for key %s\n",
5706 static int control_checktcpport(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5707 int argc, const char **argv)
5709 struct sockaddr_in sin;
5715 usage("chktcpport");
5718 port = atoi(argv[0]);
5720 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
5722 fprintf(stderr, "Failed to open local socket\n");
5726 v = fcntl(s, F_GETFL, 0);
5727 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK)) {
5728 fprintf(stderr, "Unable to set socket non-blocking\n");
5733 bzero(&sin, sizeof(sin));
5734 sin.sin_family = AF_INET;
5735 sin.sin_port = htons(port);
5736 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
5739 fprintf(stderr, "Failed to bind to TCP port %u\n", port);
5746 static int control_getdbseqnum(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5747 int argc, const char **argv)
5750 const char *db_name;
5755 usage("getdbseqnum");
5758 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5762 ret = ctdb_ctrl_get_db_seqnum(mem_ctx, ctdb->ev, ctdb->client,
5763 ctdb->cmd_pnn, TIMEOUT(), db_id,
5766 fprintf(stderr, "Failed to get sequence number for DB %s\n",
5771 printf("0x%"PRIx64"\n", seqnum);
5775 static int control_nodestatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5776 int argc, const char **argv)
5778 const char *nodestring = NULL;
5779 struct ctdb_node_map *nodemap;
5783 usage("nodestatus");
5787 nodestring = argv[0];
5790 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap)) {
5794 nodemap = get_nodemap(ctdb, false);
5795 if (nodemap == NULL) {
5799 if (options.machinereadable) {
5800 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5802 print_nodemap(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5806 for (i=0; i<nodemap->num; i++) {
5807 ret |= nodemap->node[i].flags;
5816 } db_stats_fields[] = {
5817 #define DBSTATISTICS_FIELD(n) { #n, offsetof(struct ctdb_db_statistics, n) }
5818 DBSTATISTICS_FIELD(db_ro_delegations),
5819 DBSTATISTICS_FIELD(db_ro_revokes),
5820 DBSTATISTICS_FIELD(locks.num_calls),
5821 DBSTATISTICS_FIELD(locks.num_current),
5822 DBSTATISTICS_FIELD(locks.num_pending),
5823 DBSTATISTICS_FIELD(locks.num_failed),
5824 DBSTATISTICS_FIELD(db_ro_delegations),
5827 static void print_dbstatistics(const char *db_name,
5828 struct ctdb_db_statistics *s)
5831 const char *prefix = NULL;
5834 printf("DB Statistics %s\n", db_name);
5836 for (i=0; i<ARRAY_SIZE(db_stats_fields); i++) {
5837 if (strchr(db_stats_fields[i].name, '.') != NULL) {
5838 preflen = strcspn(db_stats_fields[i].name, ".") + 1;
5840 strncmp(prefix, db_stats_fields[i].name, preflen) != 0) {
5841 prefix = db_stats_fields[i].name;
5842 printf(" %*.*s\n", preflen-1, preflen-1,
5843 db_stats_fields[i].name);
5848 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
5849 db_stats_fields[i].name+preflen, preflen ? 0 : 4, "",
5850 *(uint32_t *)(db_stats_fields[i].offset+(uint8_t *)s));
5853 printf(" hop_count_buckets:");
5854 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5855 printf(" %d", s->hop_count_bucket[i]);
5859 printf(" lock_buckets:");
5860 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5861 printf(" %d", s->locks.buckets[i]);
5865 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5866 "locks_latency MIN/AVG/MAX",
5867 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
5868 s->locks.latency.max, s->locks.latency.num);
5870 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5871 "vacuum_latency MIN/AVG/MAX",
5872 s->vacuum.latency.min, LATENCY_AVG(s->vacuum.latency),
5873 s->vacuum.latency.max, s->vacuum.latency.num);
5875 printf(" Num Hot Keys: %d\n", s->num_hot_keys);
5876 for (i=0; i<s->num_hot_keys; i++) {
5878 printf(" Count:%d Key:", s->hot_keys[i].count);
5879 for (j=0; j<s->hot_keys[i].key.dsize; j++) {
5880 printf("%02x", s->hot_keys[i].key.dptr[j] & 0xff);
5886 static int control_dbstatistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5887 int argc, const char **argv)
5890 const char *db_name;
5891 struct ctdb_db_statistics *dbstats;
5895 usage("dbstatistics");
5898 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5902 ret = ctdb_ctrl_get_db_statistics(mem_ctx, ctdb->ev, ctdb->client,
5903 ctdb->cmd_pnn, TIMEOUT(), db_id,
5906 fprintf(stderr, "Failed to get statistics for DB %s\n",
5911 print_dbstatistics(db_name, dbstats);
5915 struct disable_takeover_runs_state {
5923 static void disable_takeover_run_handler(uint64_t srvid, TDB_DATA data,
5926 struct disable_takeover_runs_state *state =
5927 (struct disable_takeover_runs_state *)private_data;
5930 if (data.dsize != sizeof(int)) {
5935 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
5936 ret = *(int *)data.dptr;
5938 state->status = ret;
5942 for (i=0; i<state->node_count; i++) {
5943 if (state->pnn_list[i] == ret) {
5944 state->reply[i] = true;
5950 for (i=0; i<state->node_count; i++) {
5951 if (! state->reply[i]) {
5952 state->done = false;
5958 static int disable_takeover_runs(TALLOC_CTX *mem_ctx,
5959 struct ctdb_context *ctdb, uint32_t timeout,
5960 uint32_t *pnn_list, int count)
5962 struct ctdb_disable_message disable = { 0 };
5963 struct disable_takeover_runs_state state;
5966 disable.pnn = ctdb->pnn;
5967 disable.srvid = next_srvid(ctdb);
5968 disable.timeout = timeout;
5970 state.pnn_list = pnn_list;
5971 state.node_count = count;
5974 state.reply = talloc_zero_array(mem_ctx, bool, count);
5975 if (state.reply == NULL) {
5979 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
5981 disable_takeover_run_handler,
5987 for (i=0; i<count; i++) {
5988 ret = ctdb_message_disable_takeover_runs(mem_ctx, ctdb->ev,
5997 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
5999 fprintf(stderr, "Timed out waiting to disable takeover runs\n");
6001 ret = (state.status >= 0 ? 0 : 1);
6005 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
6006 disable.srvid, &state);
6010 static int control_reloadips(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
6011 int argc, const char **argv)
6013 const char *nodestring = NULL;
6014 struct ctdb_node_map *nodemap, *nodemap2;
6015 struct ctdb_req_control request;
6016 uint32_t *pnn_list, *pnn_list2;
6017 int ret, count, count2;
6024 nodestring = argv[0];
6027 nodemap = get_nodemap(ctdb, false);
6028 if (nodemap == NULL) {
6032 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap2)) {
6036 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
6037 mem_ctx, &pnn_list);
6039 fprintf(stderr, "Memory allocation error\n");
6043 count2 = list_of_active_nodes(nodemap2, CTDB_UNKNOWN_PNN,
6044 mem_ctx, &pnn_list2);
6046 fprintf(stderr, "Memory allocation error\n");
6050 /* Disable takeover runs on all connected nodes. A reply
6051 * indicating success is needed from each node so all nodes
6052 * will need to be active.
6054 * A check could be added to not allow reloading of IPs when
6055 * there are disconnected nodes. However, this should
6056 * probably be left up to the administrator.
6058 ret = disable_takeover_runs(mem_ctx, ctdb, 2*options.timelimit,
6061 fprintf(stderr, "Failed to disable takeover runs\n");
6065 /* Now tell all the desired nodes to reload their public IPs.
6066 * Keep trying this until it succeeds. This assumes all
6067 * failures are transient, which might not be true...
6069 ctdb_req_control_reload_public_ips(&request);
6070 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
6071 pnn_list2, count2, TIMEOUT(),
6072 &request, NULL, NULL);
6074 fprintf(stderr, "Failed to reload IPs on some nodes.\n");
6077 /* It isn't strictly necessary to wait until takeover runs are
6078 * re-enabled but doing so can't hurt.
6080 ret = disable_takeover_runs(mem_ctx, ctdb, 0, pnn_list, count);
6082 fprintf(stderr, "Failed to enable takeover runs\n");
6086 return ipreallocate(mem_ctx, ctdb);
6089 static int control_ipiface(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
6090 int argc, const char **argv)
6092 ctdb_sock_addr addr;
6099 if (! parse_ip(argv[0], NULL, 0, &addr)) {
6100 fprintf(stderr, "Failed to Parse IP %s\n", argv[0]);
6104 iface = ctdb_sys_find_ifname(&addr);
6105 if (iface == NULL) {
6106 fprintf(stderr, "Failed to find interface for IP %s\n",
6116 static const struct ctdb_cmd {
6118 int (*fn)(TALLOC_CTX *, struct ctdb_context *, int, const char **);
6119 bool without_daemon; /* can be run without daemon running ? */
6120 bool remote; /* can be run on remote nodes */
6123 } ctdb_commands[] = {
6124 { "version", control_version, true, false,
6125 "show version of ctdb", NULL },
6126 { "status", control_status, false, true,
6127 "show node status", NULL },
6128 { "uptime", control_uptime, false, true,
6129 "show node uptime", NULL },
6130 { "ping", control_ping, false, true,
6131 "ping all nodes", NULL },
6132 { "runstate", control_runstate, false, true,
6133 "get/check runstate of a node",
6134 "[setup|first_recovery|startup|running]" },
6135 { "getvar", control_getvar, false, true,
6136 "get a tunable variable", "<name>" },
6137 { "setvar", control_setvar, false, true,
6138 "set a tunable variable", "<name> <value>" },
6139 { "listvars", control_listvars, false, true,
6140 "list tunable variables", NULL },
6141 { "statistics", control_statistics, false, true,
6142 "show ctdb statistics", NULL },
6143 { "statisticsreset", control_statistics_reset, false, true,
6144 "reset ctdb statistics", NULL },
6145 { "stats", control_stats, false, true,
6146 "show rolling statistics", "[count]" },
6147 { "ip", control_ip, false, true,
6148 "show public ips", "[all]" },
6149 { "ipinfo", control_ipinfo, false, true,
6150 "show public ip details", "<ip>" },
6151 { "ifaces", control_ifaces, false, true,
6152 "show interfaces", NULL },
6153 { "setifacelink", control_setifacelink, false, true,
6154 "set interface link status", "<iface> up|down" },
6155 { "process-exists", control_process_exists, false, true,
6156 "check if a process exists on a node", "<pid>" },
6157 { "getdbmap", control_getdbmap, false, true,
6158 "show attached databases", NULL },
6159 { "getdbstatus", control_getdbstatus, false, true,
6160 "show database status", "<dbname|dbid>" },
6161 { "catdb", control_catdb, false, false,
6162 "dump cluster-wide ctdb database", "<dbname|dbid>" },
6163 { "cattdb", control_cattdb, false, false,
6164 "dump local ctdb database", "<dbname|dbid>" },
6165 { "getmonmode", control_getmonmode, false, true,
6166 "show monitoring mode", NULL },
6167 { "getcapabilities", control_getcapabilities, false, true,
6168 "show node capabilities", NULL },
6169 { "pnn", control_pnn, false, false,
6170 "show the pnn of the currnet node", NULL },
6171 { "lvs", control_lvs, false, false,
6172 "show lvs configuration", "master|list|status" },
6173 { "disablemonitor", control_disable_monitor, false, true,
6174 "disable monitoring", NULL },
6175 { "enablemonitor", control_enable_monitor, false, true,
6176 "enable monitoring", NULL },
6177 { "setdebug", control_setdebug, false, true,
6178 "set debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
6179 { "getdebug", control_getdebug, false, true,
6180 "get debug level", NULL },
6181 { "attach", control_attach, false, false,
6182 "attach a database", "<dbname> [persistent]" },
6183 { "detach", control_detach, false, false,
6184 "detach database(s)", "<dbname|dbid> ..." },
6185 { "dumpmemory", control_dumpmemory, false, true,
6186 "dump ctdbd memory map", NULL },
6187 { "rddumpmemory", control_rddumpmemory, false, true,
6188 "dump recoverd memory map", NULL },
6189 { "getpid", control_getpid, false, true,
6190 "get ctdbd process ID", NULL },
6191 { "disable", control_disable, false, true,
6192 "disable a node", NULL },
6193 { "enable", control_enable, false, true,
6194 "enable a node", NULL },
6195 { "stop", control_stop, false, true,
6196 "stop a node", NULL },
6197 { "continue", control_continue, false, true,
6198 "continue a stopped node", NULL },
6199 { "ban", control_ban, false, true,
6200 "ban a node", "<bantime>"},
6201 { "unban", control_unban, false, true,
6202 "unban a node", NULL },
6203 { "shutdown", control_shutdown, false, true,
6204 "shutdown ctdb daemon", NULL },
6205 { "recover", control_recover, false, true,
6206 "force recovery", NULL },
6207 { "sync", control_ipreallocate, false, true,
6208 "run ip reallocation (deprecated)", NULL },
6209 { "ipreallocate", control_ipreallocate, false, true,
6210 "run ip reallocation", NULL },
6211 { "isnotrecmaster", control_isnotrecmaster, false, false,
6212 "check if local node is the recmaster", NULL },
6213 { "gratarp", control_gratarp, false, true,
6214 "send a gratuitous arp", "<ip> <interface>" },
6215 { "tickle", control_tickle, true, false,
6216 "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6217 { "gettickles", control_gettickles, false, true,
6218 "get the list of tickles", "<ip> [<port>]" },
6219 { "addtickle", control_addtickle, false, true,
6220 "add a tickle", "<ip>:<port> <ip>:<port>" },
6221 { "deltickle", control_deltickle, false, true,
6222 "delete a tickle", "<ip>:<port> <ip>:<port>" },
6223 { "check_srvids", control_check_srvids, false, true,
6224 "check if srvid is registered", "<id> [<id> ...]" },
6225 { "listnodes", control_listnodes, true, true,
6226 "list nodes in the cluster", NULL },
6227 { "reloadnodes", control_reloadnodes, false, false,
6228 "reload the nodes file all nodes", NULL },
6229 { "moveip", control_moveip, false, false,
6230 "move an ip address to another node", "<ip> <node>" },
6231 { "addip", control_addip, false, true,
6232 "add an ip address to a node", "<ip/mask> <iface>" },
6233 { "delip", control_delip, false, true,
6234 "delete an ip address from a node", "<ip>" },
6235 { "eventscript", control_eventscript, false, true,
6236 "run an event", "monitor" },
6237 { "backupdb", control_backupdb, false, false,
6238 "backup a database into a file", "<dbname|dbid> <file>" },
6239 { "restoredb", control_restoredb, false, false,
6240 "restore a database from a file", "<file> [dbname]" },
6241 { "dumpdbbackup", control_dumpdbbackup, true, false,
6242 "dump database from a backup file", "<file>" },
6243 { "wipedb", control_wipedb, false, false,
6244 "wipe the contents of a database.", "<dbname|dbid>"},
6245 { "recmaster", control_recmaster, false, true,
6246 "show the pnn for the recovery master", NULL },
6247 { "scriptstatus", control_scriptstatus, false, true,
6248 "show event script status",
6249 "[init|setup|startup|monitor|takeip|releaseip|ipreallocated]" },
6250 { "enablescript", control_enablescript, false, true,
6251 "enable an eventscript", "<script>"},
6252 { "disablescript", control_disablescript, false, true,
6253 "disable an eventscript", "<script>"},
6254 { "natgw", control_natgw, false, false,
6255 "show natgw configuration", "master|list|status" },
6256 { "natgwlist", control_natgwlist, false, false,
6257 "show the nodes belonging to this natgw configuration", NULL },
6258 { "getreclock", control_getreclock, false, true,
6259 "get recovery lock file", NULL },
6260 { "setlmasterrole", control_setlmasterrole, false, true,
6261 "set LMASTER role", "on|off" },
6262 { "setrecmasterrole", control_setrecmasterrole, false, true,
6263 "set RECMASTER role", "on|off"},
6264 { "setdbreadonly", control_setdbreadonly, false, true,
6265 "enable readonly records", "<dbname|dbid>" },
6266 { "setdbsticky", control_setdbsticky, false, true,
6267 "enable sticky records", "<dbname|dbid>"},
6268 { "pfetch", control_pfetch, false, false,
6269 "fetch record from persistent database", "<dbname|dbid> <key> [<file>]" },
6270 { "pstore", control_pstore, false, false,
6271 "write record to persistent database", "<dbname|dbid> <key> <value>" },
6272 { "pdelete", control_pdelete, false, false,
6273 "delete record from persistent database", "<dbname|dbid> <key>" },
6274 { "ptrans", control_ptrans, false, false,
6275 "update a persistent database (from file or stdin)", "<dbname|dbid> [<file>]" },
6276 { "tfetch", control_tfetch, false, true,
6277 "fetch a record", "<tdb-file> <key> [<file>]" },
6278 { "tstore", control_tstore, false, true,
6279 "store a record", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6280 { "readkey", control_readkey, false, false,
6281 "read value of a database key", "<dbname|dbid> <key> [readonly]" },
6282 { "writekey", control_writekey, false, false,
6283 "write value for a database key", "<dbname|dbid> <key> <value>" },
6284 { "deletekey", control_deletekey, false, false,
6285 "delete a database key", "<dbname|dbid> <key>" },
6286 { "checktcpport", control_checktcpport, true, false,
6287 "check if a service is bound to a specific tcp port or not", "<port>" },
6288 { "getdbseqnum", control_getdbseqnum, false, false,
6289 "get database sequence number", "<dbname|dbid>" },
6290 { "nodestatus", control_nodestatus, false, true,
6291 "show and return node status", "[all|<pnn-list>]" },
6292 { "dbstatistics", control_dbstatistics, false, true,
6293 "show database statistics", "<dbname|dbid>" },
6294 { "reloadips", control_reloadips, false, false,
6295 "reload the public addresses file", "[all|<pnn-list>]" },
6296 { "ipiface", control_ipiface, true, false,
6297 "Find the interface an ip address is hosted on", "<ip>" },
6300 static const struct ctdb_cmd *match_command(const char *command)
6302 const struct ctdb_cmd *cmd;
6305 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6306 cmd = &ctdb_commands[i];
6307 if (strlen(command) == strlen(cmd->name) &&
6308 strncmp(command, cmd->name, strlen(command)) == 0) {
6318 * Show usage message
6320 static void usage_full(void)
6324 poptPrintHelp(pc, stdout, 0);
6325 printf("\nCommands:\n");
6326 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6327 printf(" %-15s %-27s %s\n",
6328 ctdb_commands[i].name,
6329 ctdb_commands[i].args ? ctdb_commands[i].args : "",
6330 ctdb_commands[i].msg);
6334 static void usage(const char *command)
6336 const struct ctdb_cmd *cmd;
6338 if (command == NULL) {
6343 cmd = match_command(command);
6347 poptPrintUsage(pc, stdout, 0);
6348 printf("\nCommands:\n");
6349 printf(" %-15s %-27s %s\n",
6350 cmd->name, cmd->args ? cmd->args : "", cmd->msg);
6356 struct poptOption cmdline_options[] = {
6358 { "socket", 's', POPT_ARG_STRING, &options.socket, 0,
6359 "CTDB socket path", "filename" },
6360 { "debug", 'd', POPT_ARG_STRING, &options.debuglevelstr, 0,
6362 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0,
6363 "timelimit (in seconds)" },
6364 { "node", 'n', POPT_ARG_INT, &options.pnn, 0,
6365 "node specification - integer" },
6366 { NULL, 'Y', POPT_ARG_NONE, &options.machinereadable, 0,
6367 "enable machine readable output", NULL },
6368 { "separator", 'x', POPT_ARG_STRING, &options.sep, 0,
6369 "specify separator for machine readable output", "CHAR" },
6370 { NULL, 'X', POPT_ARG_NONE, &options.machineparsable, 0,
6371 "enable machine parsable output with separator |", NULL },
6372 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0,
6373 "enable verbose output", NULL },
6374 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0,
6375 "die if runtime exceeds this limit (in seconds)" },
6379 static int process_command(const struct ctdb_cmd *cmd, int argc,
6382 TALLOC_CTX *tmp_ctx;
6383 struct ctdb_context *ctdb;
6386 uint64_t srvid_offset;
6388 tmp_ctx = talloc_new(NULL);
6389 if (tmp_ctx == NULL) {
6390 fprintf(stderr, "Memory allocation error\n");
6394 if (cmd->without_daemon) {
6395 if (options.pnn != -1) {
6397 "Cannot specify node for command %s\n",
6402 ret = cmd->fn(tmp_ctx, NULL, argc-1, argv+1);
6403 talloc_free(tmp_ctx);
6407 ctdb = talloc_zero(tmp_ctx, struct ctdb_context);
6409 fprintf(stderr, "Memory allocation error\n");
6413 ctdb->ev = tevent_context_init(ctdb);
6414 if (ctdb->ev == NULL) {
6415 fprintf(stderr, "Failed to initialize tevent\n");
6419 ret = ctdb_client_init(ctdb, ctdb->ev, options.socket, &ctdb->client);
6421 fprintf(stderr, "Failed to connect to CTDB daemon (%s)\n",
6424 if (!find_node_xpnn(ctdb, NULL)) {
6425 fprintf(stderr, "Is this node part of CTDB cluster?\n");
6430 ctdb->pnn = ctdb_client_pnn(ctdb->client);
6431 srvid_offset = getpid() & 0xFFFF;
6432 ctdb->srvid = SRVID_CTDB_TOOL | (srvid_offset << 16);
6434 if (options.pnn != -1) {
6435 status = verify_pnn(ctdb, options.pnn);
6440 ctdb->cmd_pnn = options.pnn;
6442 ctdb->cmd_pnn = ctdb->pnn;
6445 if (! cmd->remote && ctdb->pnn != ctdb->cmd_pnn) {
6446 fprintf(stderr, "Node cannot be specified for command %s\n",
6451 ret = cmd->fn(tmp_ctx, ctdb, argc-1, argv+1);
6452 talloc_free(tmp_ctx);
6456 talloc_free(tmp_ctx);
6460 static void signal_handler(int sig)
6462 fprintf(stderr, "Maximum runtime exceeded - exiting\n");
6465 static void alarm_handler(int sig)
6467 /* Kill any child processes */
6468 signal(SIGTERM, signal_handler);
6474 int main(int argc, const char *argv[])
6477 const char **extra_argv;
6479 const struct ctdb_cmd *cmd;
6480 const char *ctdb_socket;
6486 /* Set default options */
6487 options.socket = CTDB_SOCKET;
6488 options.debuglevelstr = NULL;
6489 options.timelimit = 10;
6491 options.maxruntime = 0;
6494 ctdb_socket = getenv("CTDB_SOCKET");
6495 if (ctdb_socket != NULL) {
6496 options.socket = ctdb_socket;
6499 pc = poptGetContext(argv[0], argc, argv, cmdline_options,
6500 POPT_CONTEXT_KEEP_FIRST);
6501 while ((opt = poptGetNextOpt(pc)) != -1) {
6502 fprintf(stderr, "Invalid option %s: %s\n",
6503 poptBadOption(pc, 0), poptStrerror(opt));
6507 if (options.maxruntime == 0) {
6508 const char *ctdb_timeout;
6510 ctdb_timeout = getenv("CTDB_TIMEOUT");
6511 if (ctdb_timeout != NULL) {
6512 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6514 options.maxruntime = 120;
6517 if (options.maxruntime <= 120) {
6518 /* default timeout is 120 seconds */
6519 options.maxruntime = 120;
6522 if (options.machineparsable) {
6523 options.machinereadable = 1;
6526 /* setup the remaining options for the commands */
6528 extra_argv = poptGetArgs(pc);
6531 while (extra_argv[extra_argc]) extra_argc++;
6534 if (extra_argc < 1) {
6538 cmd = match_command(extra_argv[0]);
6540 fprintf(stderr, "Unknown command '%s'\n", extra_argv[0]);
6544 /* Enable logging */
6545 setup_logging("ctdb", DEBUG_STDERR);
6546 if (debug_level_parse(options.debuglevelstr, &loglevel)) {
6547 DEBUGLEVEL = loglevel;
6549 DEBUGLEVEL = DEBUG_ERR;
6552 signal(SIGALRM, alarm_handler);
6553 alarm(options.maxruntime);
6555 ret = process_command(cmd, extra_argc, extra_argv);
6560 (void)poptFreeContext(pc);