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 void print_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1379 struct ctdb_public_ip_list *ips,
1380 struct ctdb_public_ip_info **ipinfo,
1384 char *conf, *avail, *active;
1386 if (options.machinereadable == 1) {
1387 printf("%s%s%s%s%s", options.sep,
1388 "Public IP", options.sep,
1389 "Node", options.sep);
1390 if (options.verbose == 1) {
1391 printf("%s%s%s%s%s%s\n",
1392 "ActiveInterfaces", options.sep,
1393 "AvailableInterfaces", options.sep,
1394 "ConfiguredInterfaces", options.sep);
1400 printf("Public IPs on ALL nodes\n");
1402 printf("Public IPs on node %u\n", ctdb->cmd_pnn);
1406 /* IPs are reverse sorted */
1407 for (i=ips->num-1; i>=0; i--) {
1409 if (options.machinereadable == 1) {
1410 printf("%s%s%s%d%s", options.sep,
1411 ctdb_sock_addr_to_string(
1412 mem_ctx, &ips->ip[i].addr),
1414 (int)ips->ip[i].pnn, options.sep);
1416 printf("%s", ctdb_sock_addr_to_string(
1417 mem_ctx, &ips->ip[i].addr));
1420 if (options.verbose == 0) {
1421 if (options.machinereadable == 1) {
1424 printf(" %d\n", (int)ips->ip[i].pnn);
1433 for (j=0; j<ipinfo[i]->ifaces->num; j++) {
1434 struct ctdb_iface *iface;
1436 iface = &ipinfo[i]->ifaces->iface[j];
1438 conf = talloc_strdup(mem_ctx, iface->name);
1440 conf = talloc_asprintf_append(
1441 mem_ctx, ",%s", iface->name);
1444 if (ipinfo[i]->active_idx == j) {
1445 active = iface->name;
1448 if (iface->link_state == 0) {
1452 if (avail == NULL) {
1453 avail = talloc_strdup(mem_ctx, iface->name);
1455 avail = talloc_asprintf_append(
1456 mem_ctx, ",%s", iface->name);
1460 if (options.machinereadable == 1) {
1461 printf("%s%s%s%s%s%s\n",
1462 active ? active : "", options.sep,
1463 avail ? avail : "", options.sep,
1464 conf ? conf : "", options.sep);
1466 printf(" node[%u] active[%s] available[%s] configured[%s]\n",
1467 ips->ip[i].pnn, active ? active : "",
1468 avail ? avail : "", conf ? conf : "");
1473 static int collect_ips(uint8_t *keybuf, size_t keylen, uint8_t *databuf,
1474 size_t datalen, void *private_data)
1476 struct ctdb_public_ip_list *ips = talloc_get_type_abort(
1477 private_data, struct ctdb_public_ip_list);
1478 struct ctdb_public_ip *ip;
1480 ip = (struct ctdb_public_ip *)databuf;
1481 ips->ip[ips->num] = *ip;
1487 static int get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
1488 struct ctdb_public_ip_list **out)
1490 struct ctdb_node_map *nodemap;
1491 struct ctdb_public_ip_list *ips;
1492 struct db_hash_context *ipdb;
1494 int ret, count, i, j;
1496 nodemap = get_nodemap(ctdb, false);
1497 if (nodemap == NULL) {
1501 ret = db_hash_init(mem_ctx, "ips", 101, DB_HASH_COMPLEX, &ipdb);
1506 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
1512 for (i=0; i<count; i++) {
1513 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1514 pnn_list[i], TIMEOUT(), &ips);
1519 for (j=0; j<ips->num; j++) {
1520 struct ctdb_public_ip ip;
1522 ip.pnn = ips->ip[j].pnn;
1523 ip.addr = ips->ip[j].addr;
1525 ret = db_hash_add(ipdb, (uint8_t *)&ip.addr,
1527 (uint8_t *)&ip, sizeof(ip));
1536 talloc_free(pnn_list);
1538 ret = db_hash_traverse(ipdb, NULL, NULL, &count);
1543 ips = talloc_zero(mem_ctx, struct ctdb_public_ip_list);
1548 ips->ip = talloc_array(ips, struct ctdb_public_ip, count);
1549 if (ips->ip == NULL) {
1553 ret = db_hash_traverse(ipdb, collect_ips, ips, &count);
1558 if (count != ips->num) {
1572 static int control_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1573 int argc, const char **argv)
1575 struct ctdb_public_ip_list *ips;
1576 struct ctdb_public_ip_info **ipinfo;
1578 bool do_all = false;
1585 if (strcmp(argv[0], "all") == 0) {
1593 ret = get_all_public_ips(ctdb, mem_ctx, &ips);
1595 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1596 ctdb->cmd_pnn, TIMEOUT(), &ips);
1602 ipinfo = talloc_array(mem_ctx, struct ctdb_public_ip_info *, ips->num);
1603 if (ipinfo == NULL) {
1607 for (i=0; i<ips->num; i++) {
1610 pnn = ips->ip[i].pnn;
1612 pnn = ctdb->cmd_pnn;
1614 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev,
1616 TIMEOUT(), &ips->ip[i].addr,
1623 print_ip(mem_ctx, ctdb, ips, ipinfo, do_all);
1627 static int control_ipinfo(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1628 int argc, const char **argv)
1630 struct ctdb_public_ip_info *ipinfo;
1631 ctdb_sock_addr addr;
1638 if (! parse_ip(argv[0], NULL, 0, &addr)) {
1639 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
1643 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev, ctdb->client,
1644 ctdb->cmd_pnn, TIMEOUT(), &addr,
1648 printf("Node %u does not know about IP %s\n",
1649 ctdb->cmd_pnn, argv[0]);
1654 printf("Public IP[%s] info on node %u\n",
1655 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1658 printf("IP:%s\nCurrentNode:%u\nNumInterfaces:%u\n",
1659 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1660 ipinfo->ip.pnn, ipinfo->ifaces->num);
1662 for (i=0; i<ipinfo->ifaces->num; i++) {
1663 struct ctdb_iface *iface;
1665 iface = &ipinfo->ifaces->iface[i];
1666 iface->name[CTDB_IFACE_SIZE] = '\0';
1667 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
1669 iface->link_state == 0 ? "down" : "up",
1671 (i == ipinfo->active_idx) ? " (active)" : "");
1677 static int control_ifaces(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1678 int argc, const char **argv)
1680 struct ctdb_iface_list *ifaces;
1687 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1688 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1693 if (ifaces->num == 0) {
1694 printf("No interfaces configured on node %u\n",
1699 if (options.machinereadable) {
1700 printf("%s%s%s%s%s%s%s\n", options.sep,
1701 "Name", options.sep,
1702 "LinkStatus", options.sep,
1703 "References", options.sep);
1705 printf("Interfaces on node %u\n", ctdb->cmd_pnn);
1708 for (i=0; i<ifaces->num; i++) {
1709 if (options.machinereadable) {
1710 printf("%s%s%s%u%s%u%s\n", options.sep,
1711 ifaces->iface[i].name, options.sep,
1712 ifaces->iface[i].link_state, options.sep,
1713 ifaces->iface[i].references, options.sep);
1715 printf("name:%s link:%s references:%u\n",
1716 ifaces->iface[i].name,
1717 ifaces->iface[i].link_state ? "up" : "down",
1718 ifaces->iface[i].references);
1725 static int control_setifacelink(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1726 int argc, const char **argv)
1728 struct ctdb_iface_list *ifaces;
1729 struct ctdb_iface *iface;
1733 usage("setifacelink");
1736 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
1737 fprintf(stderr, "Interface name '%s' too long\n", argv[0]);
1741 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1742 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1745 "Failed to get interface information from node %u\n",
1751 for (i=0; i<ifaces->num; i++) {
1752 if (strcmp(ifaces->iface[i].name, argv[0]) == 0) {
1753 iface = &ifaces->iface[i];
1758 if (iface == NULL) {
1759 printf("Interface %s not configured on node %u\n",
1760 argv[0], ctdb->cmd_pnn);
1764 if (strcmp(argv[1], "up") == 0) {
1765 iface->link_state = 1;
1766 } else if (strcmp(argv[1], "down") == 0) {
1767 iface->link_state = 0;
1769 usage("setifacelink");
1773 iface->references = 0;
1775 ret = ctdb_ctrl_set_iface_link_state(mem_ctx, ctdb->ev, ctdb->client,
1776 ctdb->cmd_pnn, TIMEOUT(), iface);
1784 static int control_process_exists(TALLOC_CTX *mem_ctx,
1785 struct ctdb_context *ctdb,
1786 int argc, const char **argv)
1792 usage("process-exists");
1795 pid = atoi(argv[0]);
1796 ret = ctdb_ctrl_process_exists(mem_ctx, ctdb->ev, ctdb->client,
1797 ctdb->cmd_pnn, TIMEOUT(), pid, &status);
1803 printf("PID %u exists\n", pid);
1805 printf("PID %u does not exist\n", pid);
1810 static int control_getdbmap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1811 int argc, const char **argv)
1813 struct ctdb_dbid_map *dbmap;
1820 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
1821 ctdb->cmd_pnn, TIMEOUT(), &dbmap);
1826 if (options.machinereadable == 1) {
1827 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1830 "Name", options.sep,
1831 "Path", options.sep,
1832 "Persistent", options.sep,
1833 "Sticky", options.sep,
1834 "Unhealthy", options.sep,
1835 "Readonly", options.sep);
1837 printf("Number of databases:%d\n", dbmap->num);
1840 for (i=0; i<dbmap->num; i++) {
1849 db_id = dbmap->dbs[i].db_id;
1851 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
1852 ctdb->cmd_pnn, TIMEOUT(), db_id,
1858 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1859 ctdb->cmd_pnn, TIMEOUT(), db_id,
1865 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1866 ctdb->cmd_pnn, TIMEOUT(), db_id,
1872 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
1873 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
1874 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
1876 if (options.machinereadable == 1) {
1877 printf("%s0x%08X%s%s%s%s%s%d%s%d%s%d%s%d%s\n",
1882 !! (persistent), options.sep,
1883 !! (sticky), options.sep,
1884 !! (health), options.sep,
1885 !! (readonly), options.sep);
1887 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
1889 persistent ? " PERSISTENT" : "",
1890 sticky ? " STICKY" : "",
1891 readonly ? " READONLY" : "",
1892 health ? " UNHEALTHY" : "");
1895 talloc_free(discard_const(name));
1896 talloc_free(discard_const(path));
1897 talloc_free(discard_const(health));
1903 static int control_getdbstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1904 int argc, const char **argv)
1907 const char *db_name, *db_path, *db_health;
1912 usage("getdbstatus");
1915 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
1919 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1920 ctdb->cmd_pnn, TIMEOUT(), db_id,
1926 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1927 ctdb->cmd_pnn, TIMEOUT(), db_id,
1933 printf("dbid: 0x%08x\nname: %s\npath: %s\n", db_id, db_name, db_path);
1934 printf("PERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
1935 (db_flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
1936 (db_flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
1937 (db_flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
1938 (db_health ? db_health : "OK"));
1942 struct dump_record_state {
1946 #define ISASCII(x) (isprint(x) && ! strchr("\"\\", (x)))
1948 static void dump_tdb_data(const char *name, TDB_DATA val)
1952 fprintf(stdout, "%s(%zu) = \"", name, val.dsize);
1953 for (i=0; i<val.dsize; i++) {
1954 if (ISASCII(val.dptr[i])) {
1955 fprintf(stdout, "%c", val.dptr[i]);
1957 fprintf(stdout, "\\%02X", val.dptr[i]);
1960 fprintf(stdout, "\"\n");
1963 static void dump_ltdb_header(struct ctdb_ltdb_header *header)
1965 fprintf(stdout, "dmaster: %u\n", header->dmaster);
1966 fprintf(stdout, "rsn: %" PRIu64 "\n", header->rsn);
1967 fprintf(stdout, "flags: 0x%08x", header->flags);
1968 if (header->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
1969 fprintf(stdout, " MIGRATED_WITH_DATA");
1971 if (header->flags & CTDB_REC_FLAG_VACUUM_MIGRATED) {
1972 fprintf(stdout, " VACUUM_MIGRATED");
1974 if (header->flags & CTDB_REC_FLAG_AUTOMATIC) {
1975 fprintf(stdout, " AUTOMATIC");
1977 if (header->flags & CTDB_REC_RO_HAVE_DELEGATIONS) {
1978 fprintf(stdout, " RO_HAVE_DELEGATIONS");
1980 if (header->flags & CTDB_REC_RO_HAVE_READONLY) {
1981 fprintf(stdout, " RO_HAVE_READONLY");
1983 if (header->flags & CTDB_REC_RO_REVOKING_READONLY) {
1984 fprintf(stdout, " RO_REVOKING_READONLY");
1986 if (header->flags & CTDB_REC_RO_REVOKE_COMPLETE) {
1987 fprintf(stdout, " RO_REVOKE_COMPLETE");
1989 fprintf(stdout, "\n");
1993 static int dump_record(uint32_t reqid, struct ctdb_ltdb_header *header,
1994 TDB_DATA key, TDB_DATA data, void *private_data)
1996 struct dump_record_state *state =
1997 (struct dump_record_state *)private_data;
2001 dump_tdb_data("key", key);
2002 dump_ltdb_header(header);
2003 dump_tdb_data("data", data);
2004 fprintf(stdout, "\n");
2009 struct traverse_state {
2010 TALLOC_CTX *mem_ctx;
2012 ctdb_rec_parser_func_t func;
2013 struct dump_record_state sub_state;
2016 static void traverse_handler(uint64_t srvid, TDB_DATA data, void *private_data)
2018 struct traverse_state *state = (struct traverse_state *)private_data;
2019 struct ctdb_rec_data *rec;
2020 struct ctdb_ltdb_header header;
2023 ret = ctdb_rec_data_pull(data.dptr, data.dsize, state->mem_ctx, &rec);
2028 if (rec->key.dsize == 0 && rec->data.dsize == 0) {
2030 /* end of traverse */
2035 ret = ctdb_ltdb_header_extract(&rec->data, &header);
2041 if (rec->data.dsize == 0) {
2046 ret = state->func(rec->reqid, &header, rec->key, rec->data,
2054 static int control_catdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2055 int argc, const char **argv)
2057 struct ctdb_db_context *db;
2058 const char *db_name;
2061 struct ctdb_traverse_start_ext traverse;
2062 struct traverse_state state;
2069 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2073 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2076 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2081 ZERO_STRUCT(traverse);
2083 traverse.db_id = db_id;
2085 traverse.srvid = next_srvid(ctdb);
2086 traverse.withemptyrecords = false;
2088 state.mem_ctx = mem_ctx;
2090 state.func = dump_record;
2091 state.sub_state.count = 0;
2093 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2095 traverse_handler, &state);
2100 ret = ctdb_ctrl_traverse_start_ext(mem_ctx, ctdb->ev, ctdb->client,
2101 ctdb->cmd_pnn, TIMEOUT(),
2107 ctdb_client_wait(ctdb->ev, &state.done);
2109 printf("Dumped %u records\n", state.sub_state.count);
2111 ret = ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2112 traverse.srvid, &state);
2120 static int control_cattdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2121 int argc, const char **argv)
2123 struct ctdb_db_context *db;
2124 const char *db_name;
2127 struct dump_record_state state;
2134 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2138 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2141 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2146 ret = ctdb_db_traverse(db, true, true, dump_record, &state);
2148 printf("Dumped %u record(s)\n", state.count);
2153 static int control_getmonmode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2154 int argc, const char **argv)
2159 usage("getmonmode");
2162 ret = ctdb_ctrl_get_monmode(mem_ctx, ctdb->ev, ctdb->client,
2163 ctdb->cmd_pnn, TIMEOUT(), &mode);
2169 (mode == CTDB_MONITORING_ENABLED) ? "ENABLED" : "DISABLED");
2173 static int control_getcapabilities(TALLOC_CTX *mem_ctx,
2174 struct ctdb_context *ctdb,
2175 int argc, const char **argv)
2181 usage("getcapabilities");
2184 ret = ctdb_ctrl_get_capabilities(mem_ctx, ctdb->ev, ctdb->client,
2185 ctdb->cmd_pnn, TIMEOUT(), &caps);
2190 if (options.machinereadable == 1) {
2191 printf("%s%s%s%s%s\n",
2193 "RECMASTER", options.sep,
2194 "LMASTER", options.sep);
2195 printf("%s%d%s%d%s\n", options.sep,
2196 !! (caps & CTDB_CAP_RECMASTER), options.sep,
2197 !! (caps & CTDB_CAP_LMASTER), options.sep);
2199 printf("RECMASTER: %s\n",
2200 (caps & CTDB_CAP_RECMASTER) ? "YES" : "NO");
2201 printf("LMASTER: %s\n",
2202 (caps & CTDB_CAP_LMASTER) ? "YES" : "NO");
2208 static int control_pnn(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2209 int argc, const char **argv)
2211 printf("%u\n", ctdb_client_pnn(ctdb->client));
2215 static int control_lvs(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2216 int argc, const char **argv)
2218 char *t, *lvs_helper = NULL;
2224 t = getenv("CTDB_LVS_HELPER");
2226 lvs_helper = talloc_strdup(mem_ctx, t);
2228 lvs_helper = talloc_asprintf(mem_ctx, "%s/ctdb_lvs",
2229 CTDB_HELPER_BINDIR);
2232 if (lvs_helper == NULL) {
2233 fprintf(stderr, "Unable to set LVS helper\n");
2237 return run_helper("LVS helper", lvs_helper, argv[0]);
2240 static int control_disable_monitor(TALLOC_CTX *mem_ctx,
2241 struct ctdb_context *ctdb,
2242 int argc, const char **argv)
2247 usage("disablemonitor");
2250 ret = ctdb_ctrl_disable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2251 ctdb->cmd_pnn, TIMEOUT());
2259 static int control_enable_monitor(TALLOC_CTX *mem_ctx,
2260 struct ctdb_context *ctdb,
2261 int argc, const char **argv)
2266 usage("enablemonitor");
2269 ret = ctdb_ctrl_enable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2270 ctdb->cmd_pnn, TIMEOUT());
2278 static int control_setdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2279 int argc, const char **argv)
2289 found = debug_level_parse(argv[0], &log_level);
2292 "Invalid debug level '%s'. Valid levels are:\n",
2294 fprintf(stderr, "\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
2298 ret = ctdb_ctrl_setdebug(mem_ctx, ctdb->ev, ctdb->client,
2299 ctdb->cmd_pnn, TIMEOUT(), log_level);
2307 static int control_getdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2308 int argc, const char **argv)
2311 const char *log_str;
2318 ret = ctdb_ctrl_getdebug(mem_ctx, ctdb->ev, ctdb->client,
2319 ctdb->cmd_pnn, TIMEOUT(), &loglevel);
2324 log_str = debug_level_to_string(loglevel);
2325 printf("%s\n", log_str);
2330 static int control_attach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2331 int argc, const char **argv)
2333 const char *db_name;
2334 uint8_t db_flags = 0;
2337 if (argc < 1 || argc > 2) {
2343 if (strcmp(argv[1], "persistent") == 0) {
2344 db_flags = CTDB_DB_FLAGS_PERSISTENT;
2345 } else if (strcmp(argv[1], "readonly") == 0) {
2346 db_flags = CTDB_DB_FLAGS_READONLY;
2347 } else if (strcmp(argv[1], "sticky") == 0) {
2348 db_flags = CTDB_DB_FLAGS_STICKY;
2354 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2363 static int control_detach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2364 int argc, const char **argv)
2366 const char *db_name;
2369 struct ctdb_node_map *nodemap;
2377 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2378 ctdb->cmd_pnn, TIMEOUT(), &recmode);
2383 if (recmode == CTDB_RECOVERY_ACTIVE) {
2384 fprintf(stderr, "Database cannot be detached"
2385 " when recovery is active\n");
2389 nodemap = get_nodemap(ctdb, false);
2390 if (nodemap == NULL) {
2394 for (i=0; i<nodemap->num; i++) {
2397 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
2400 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
2403 if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
2404 fprintf(stderr, "Database cannot be detached on"
2405 " inactive (stopped or banned) node %u\n",
2406 nodemap->node[i].pnn);
2410 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
2411 nodemap->node[i].pnn, TIMEOUT(),
2412 "AllowClientDBAttach", &value);
2415 "Unable to get tunable AllowClientDBAttach"
2416 " from node %u\n", nodemap->node[i].pnn);
2422 "Database access is still active on node %u."
2423 " Set AllowclientDBAttach=0 on all nodes.\n",
2424 nodemap->node[i].pnn);
2430 for (i=0; i<argc; i++) {
2431 if (! db_exists(mem_ctx, ctdb, argv[i], &db_id, &db_name,
2436 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
2438 "Persistent database %s cannot be detached\n",
2443 ret = ctdb_detach(mem_ctx, ctdb->ev, ctdb->client,
2446 fprintf(stderr, "Database %s detach failed\n", db_name);
2454 static int control_dumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2455 int argc, const char **argv)
2457 const char *mem_str;
2461 ret = ctdb_ctrl_dump_memory(mem_ctx, ctdb->ev, ctdb->client,
2462 ctdb->cmd_pnn, TIMEOUT(), &mem_str);
2467 n = write(1, mem_str, strlen(mem_str)+1);
2468 if (n < 0 || n != strlen(mem_str)+1) {
2469 fprintf(stderr, "Failed to write talloc summary\n");
2476 static void dump_memory(uint64_t srvid, TDB_DATA data, void *private_data)
2478 bool *done = (bool *)private_data;
2481 n = write(1, data.dptr, data.dsize);
2482 if (n < 0 || n != data.dsize) {
2483 fprintf(stderr, "Failed to write talloc summary\n");
2489 static int control_rddumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2490 int argc, const char **argv)
2492 struct ctdb_srvid_message msg = { 0 };
2496 msg.pnn = ctdb->pnn;
2497 msg.srvid = next_srvid(ctdb);
2499 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2500 msg.srvid, dump_memory, &done);
2505 ret = ctdb_message_mem_dump(mem_ctx, ctdb->ev, ctdb->client,
2506 ctdb->cmd_pnn, &msg);
2511 ctdb_client_wait(ctdb->ev, &done);
2515 static int control_getpid(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2516 int argc, const char **argv)
2521 ret = ctdb_ctrl_get_pid(mem_ctx, ctdb->ev, ctdb->client,
2522 ctdb->cmd_pnn, TIMEOUT(), &pid);
2527 printf("%u\n", pid);
2531 static int check_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2532 const char *desc, uint32_t flag, bool set_flag)
2534 struct ctdb_node_map *nodemap;
2537 nodemap = get_nodemap(ctdb, false);
2538 if (nodemap == NULL) {
2542 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2543 if (set_flag == flag_is_set) {
2545 fprintf(stderr, "Node %u is already %s\n",
2546 ctdb->cmd_pnn, desc);
2548 fprintf(stderr, "Node %u is not %s\n",
2549 ctdb->cmd_pnn, desc);
2557 static void wait_for_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2558 uint32_t flag, bool set_flag)
2560 struct ctdb_node_map *nodemap;
2564 nodemap = get_nodemap(ctdb, true);
2565 if (nodemap == NULL) {
2567 "Failed to get nodemap, trying again\n");
2572 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2573 if (flag_is_set == set_flag) {
2581 struct ipreallocate_state {
2586 static void ipreallocate_handler(uint64_t srvid, TDB_DATA data,
2589 struct ipreallocate_state *state =
2590 (struct ipreallocate_state *)private_data;
2592 if (data.dsize != sizeof(int)) {
2597 state->status = *(int *)data.dptr;
2601 static int ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb)
2603 struct ctdb_srvid_message msg = { 0 };
2604 struct ipreallocate_state state;
2607 msg.pnn = ctdb->pnn;
2608 msg.srvid = next_srvid(ctdb);
2611 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2613 ipreallocate_handler, &state);
2619 ret = ctdb_message_takeover_run(mem_ctx, ctdb->ev,
2621 CTDB_BROADCAST_CONNECTED,
2627 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done,
2633 if (state.status >= 0) {
2642 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2647 static int control_disable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2648 int argc, const char **argv)
2656 ret = check_flags(mem_ctx, ctdb, "disabled",
2657 NODE_FLAGS_PERMANENTLY_DISABLED, true);
2662 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2663 ctdb->cmd_pnn, TIMEOUT(),
2664 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2667 "Failed to set DISABLED flag on node %u\n",
2672 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, true);
2673 return ipreallocate(mem_ctx, ctdb);
2676 static int control_enable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2677 int argc, const char **argv)
2685 ret = check_flags(mem_ctx, ctdb, "disabled",
2686 NODE_FLAGS_PERMANENTLY_DISABLED, false);
2691 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2692 ctdb->cmd_pnn, TIMEOUT(),
2693 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2695 fprintf(stderr, "Failed to reset DISABLED flag on node %u\n",
2700 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, false);
2701 return ipreallocate(mem_ctx, ctdb);
2704 static int control_stop(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2705 int argc, const char **argv)
2713 ret = check_flags(mem_ctx, ctdb, "stopped",
2714 NODE_FLAGS_STOPPED, true);
2719 ret = ctdb_ctrl_stop_node(mem_ctx, ctdb->ev, ctdb->client,
2720 ctdb->cmd_pnn, TIMEOUT());
2722 fprintf(stderr, "Failed to stop node %u\n", ctdb->cmd_pnn);
2726 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, true);
2727 return ipreallocate(mem_ctx, ctdb);
2730 static int control_continue(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2731 int argc, const char **argv)
2739 ret = check_flags(mem_ctx, ctdb, "stopped",
2740 NODE_FLAGS_STOPPED, false);
2745 ret = ctdb_ctrl_continue_node(mem_ctx, ctdb->ev, ctdb->client,
2746 ctdb->cmd_pnn, TIMEOUT());
2748 fprintf(stderr, "Failed to continue stopped node %u\n",
2753 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, false);
2754 return ipreallocate(mem_ctx, ctdb);
2757 static int control_ban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2758 int argc, const char **argv)
2760 struct ctdb_ban_state ban_state;
2767 ret = check_flags(mem_ctx, ctdb, "banned",
2768 NODE_FLAGS_BANNED, true);
2773 ban_state.pnn = ctdb->cmd_pnn;
2774 ban_state.time = strtoul(argv[0], NULL, 0);
2776 if (ban_state.time == 0) {
2777 fprintf(stderr, "Ban time cannot be zero\n");
2781 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2782 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2784 fprintf(stderr, "Failed to ban node %u\n", ctdb->cmd_pnn);
2788 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, true);
2789 return ipreallocate(mem_ctx, ctdb);
2793 static int control_unban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2794 int argc, const char **argv)
2796 struct ctdb_ban_state ban_state;
2803 ret = check_flags(mem_ctx, ctdb, "banned",
2804 NODE_FLAGS_BANNED, false);
2809 ban_state.pnn = ctdb->cmd_pnn;
2812 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2813 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2815 fprintf(stderr, "Failed to unban node %u\n", ctdb->cmd_pnn);
2819 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, false);
2820 return ipreallocate(mem_ctx, ctdb);
2824 static int control_shutdown(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2825 int argc, const char **argv)
2833 ret = ctdb_ctrl_shutdown(mem_ctx, ctdb->ev, ctdb->client,
2834 ctdb->cmd_pnn, TIMEOUT());
2836 fprintf(stderr, "Unable to shutdown node %u\n", ctdb->cmd_pnn);
2843 static int get_generation(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2844 uint32_t *generation)
2848 struct ctdb_vnn_map *vnnmap;
2852 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2853 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
2855 fprintf(stderr, "Failed to find recovery master\n");
2859 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2860 recmaster, TIMEOUT(), &recmode);
2862 fprintf(stderr, "Failed to get recovery mode from node %u\n",
2867 if (recmode == CTDB_RECOVERY_ACTIVE) {
2872 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
2873 recmaster, TIMEOUT(), &vnnmap);
2875 fprintf(stderr, "Failed to get generation from node %u\n",
2880 if (vnnmap->generation == INVALID_GENERATION) {
2881 talloc_free(vnnmap);
2886 *generation = vnnmap->generation;
2887 talloc_free(vnnmap);
2892 static int control_recover(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2893 int argc, const char **argv)
2895 uint32_t generation, next_generation;
2902 ret = get_generation(mem_ctx, ctdb, &generation);
2907 ret = ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
2908 ctdb->cmd_pnn, TIMEOUT(),
2909 CTDB_RECOVERY_ACTIVE);
2911 fprintf(stderr, "Failed to set recovery mode active\n");
2916 ret = get_generation(mem_ctx, ctdb, &next_generation);
2919 "Failed to confirm end of recovery\n");
2923 if (next_generation != generation) {
2933 static int control_ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2934 int argc, const char **argv)
2937 usage("ipreallocate");
2940 return ipreallocate(mem_ctx, ctdb);
2943 static int control_isnotrecmaster(TALLOC_CTX *mem_ctx,
2944 struct ctdb_context *ctdb,
2945 int argc, const char **argv)
2951 usage("isnotrecmaster");
2954 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2955 ctdb->pnn, TIMEOUT(), &recmaster);
2957 fprintf(stderr, "Failed to get recmaster\n");
2961 if (recmaster != ctdb->pnn) {
2962 printf("this node is not the recmaster\n");
2966 printf("this node is the recmaster\n");
2970 static int control_gratarp(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2971 int argc, const char **argv)
2973 struct ctdb_addr_info addr_info;
2980 if (! parse_ip(argv[0], NULL, 0, &addr_info.addr)) {
2981 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
2984 addr_info.iface = argv[1];
2986 ret = ctdb_ctrl_send_gratuitous_arp(mem_ctx, ctdb->ev, ctdb->client,
2987 ctdb->cmd_pnn, TIMEOUT(),
2990 fprintf(stderr, "Unable to send gratuitous arp from node %u\n",
2998 static int control_tickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2999 int argc, const char **argv)
3001 ctdb_sock_addr src, dst;
3004 if (argc != 0 && argc != 2) {
3009 struct ctdb_connection *clist;
3013 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3019 for (i=0; i<count; i++) {
3020 ret = ctdb_sys_send_tcp(&clist[i].src,
3030 if (num_failed > 0) {
3031 fprintf(stderr, "Failed to send %d tickles\n",
3040 if (! parse_ip_port(argv[0], &src)) {
3041 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3045 if (! parse_ip_port(argv[1], &dst)) {
3046 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3050 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
3052 fprintf(stderr, "Failed to send tickle ack\n");
3059 static int control_gettickles(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3060 int argc, const char **argv)
3062 ctdb_sock_addr addr;
3063 struct ctdb_tickle_list *tickles;
3067 if (argc < 1 || argc > 2) {
3068 usage("gettickles");
3072 port = strtoul(argv[1], NULL, 10);
3075 if (! parse_ip(argv[0], NULL, port, &addr)) {
3076 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3080 ret = ctdb_ctrl_get_tcp_tickle_list(mem_ctx, ctdb->ev, ctdb->client,
3081 ctdb->cmd_pnn, TIMEOUT(), &addr,
3084 fprintf(stderr, "Failed to get list of connections\n");
3088 if (options.machinereadable) {
3089 printf("%s%s%s%s%s%s%s%s%s\n",
3091 "Source IP", options.sep,
3092 "Port", options.sep,
3093 "Destiation IP", options.sep,
3094 "Port", options.sep);
3095 for (i=0; i<tickles->num; i++) {
3096 printf("%s%s%s%u%s%s%s%u%s\n", options.sep,
3097 ctdb_sock_addr_to_string(
3098 mem_ctx, &tickles->conn[i].src),
3100 ntohs(tickles->conn[i].src.ip.sin_port),
3102 ctdb_sock_addr_to_string(
3103 mem_ctx, &tickles->conn[i].dst),
3105 ntohs(tickles->conn[i].dst.ip.sin_port),
3109 printf("Connections for IP: %s\n",
3110 ctdb_sock_addr_to_string(mem_ctx, &tickles->addr));
3111 printf("Num connections: %u\n", tickles->num);
3112 for (i=0; i<tickles->num; i++) {
3113 printf("SRC: %s:%u DST: %s:%u\n",
3114 ctdb_sock_addr_to_string(
3115 mem_ctx, &tickles->conn[i].src),
3116 ntohs(tickles->conn[i].src.ip.sin_port),
3117 ctdb_sock_addr_to_string(
3118 mem_ctx, &tickles->conn[i].dst),
3119 ntohs(tickles->conn[i].dst.ip.sin_port));
3123 talloc_free(tickles);
3127 typedef void (*clist_request_func)(struct ctdb_req_control *request,
3128 struct ctdb_connection *conn);
3130 typedef int (*clist_reply_func)(struct ctdb_reply_control *reply);
3132 struct process_clist_state {
3133 struct ctdb_connection *clist;
3135 int num_failed, num_total;
3136 clist_reply_func reply_func;
3139 static void process_clist_done(struct tevent_req *subreq);
3141 static struct tevent_req *process_clist_send(
3142 TALLOC_CTX *mem_ctx,
3143 struct ctdb_context *ctdb,
3144 struct ctdb_connection *clist,
3146 clist_request_func request_func,
3147 clist_reply_func reply_func)
3149 struct tevent_req *req, *subreq;
3150 struct process_clist_state *state;
3151 struct ctdb_req_control request;
3154 req = tevent_req_create(mem_ctx, &state, struct process_clist_state);
3159 state->clist = clist;
3160 state->count = count;
3161 state->reply_func = reply_func;
3163 for (i=0; i<count; i++) {
3164 request_func(&request, &clist[i]);
3165 subreq = ctdb_client_control_send(state, ctdb->ev,
3166 ctdb->client, ctdb->cmd_pnn,
3167 TIMEOUT(), &request);
3168 if (tevent_req_nomem(subreq, req)) {
3169 return tevent_req_post(req, ctdb->ev);
3171 tevent_req_set_callback(subreq, process_clist_done, req);
3177 static void process_clist_done(struct tevent_req *subreq)
3179 struct tevent_req *req = tevent_req_callback_data(
3180 subreq, struct tevent_req);
3181 struct process_clist_state *state = tevent_req_data(
3182 req, struct process_clist_state);
3183 struct ctdb_reply_control *reply;
3187 status = ctdb_client_control_recv(subreq, NULL, state, &reply);
3188 TALLOC_FREE(subreq);
3190 state->num_failed += 1;
3194 ret = state->reply_func(reply);
3196 state->num_failed += 1;
3201 state->num_total += 1;
3202 if (state->num_total == state->count) {
3203 tevent_req_done(req);
3207 static int process_clist_recv(struct tevent_req *req)
3209 struct process_clist_state *state = tevent_req_data(
3210 req, struct process_clist_state);
3212 return state->num_failed;
3215 static int control_addtickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3216 int argc, const char **argv)
3218 struct ctdb_connection conn;
3221 if (argc != 0 && argc != 2) {
3226 struct ctdb_connection *clist;
3227 struct tevent_req *req;
3230 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3238 req = process_clist_send(mem_ctx, ctdb, clist, count,
3239 ctdb_req_control_tcp_add_delayed_update,
3240 ctdb_reply_control_tcp_add_delayed_update);
3246 tevent_req_poll(req, ctdb->ev);
3249 ret = process_clist_recv(req);
3251 fprintf(stderr, "Failed to add %d tickles\n", ret);
3258 if (! parse_ip_port(argv[0], &conn.src)) {
3259 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3262 if (! parse_ip_port(argv[1], &conn.dst)) {
3263 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3267 ret = ctdb_ctrl_tcp_add_delayed_update(mem_ctx, ctdb->ev,
3268 ctdb->client, ctdb->cmd_pnn,
3271 fprintf(stderr, "Failed to register connection\n");
3278 static int control_deltickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3279 int argc, const char **argv)
3281 struct ctdb_connection conn;
3284 if (argc != 0 && argc != 2) {
3289 struct ctdb_connection *clist;
3290 struct tevent_req *req;
3293 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3301 req = process_clist_send(mem_ctx, ctdb, clist, count,
3302 ctdb_req_control_tcp_remove,
3303 ctdb_reply_control_tcp_remove);
3309 tevent_req_poll(req, ctdb->ev);
3312 ret = process_clist_recv(req);
3314 fprintf(stderr, "Failed to remove %d tickles\n", ret);
3321 if (! parse_ip_port(argv[0], &conn.src)) {
3322 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3325 if (! parse_ip_port(argv[1], &conn.dst)) {
3326 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3330 ret = ctdb_ctrl_tcp_remove(mem_ctx, ctdb->ev, ctdb->client,
3331 ctdb->cmd_pnn, TIMEOUT(), &conn);
3333 fprintf(stderr, "Failed to unregister connection\n");
3340 static int control_check_srvids(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3341 int argc, const char **argv)
3348 usage("check_srvids");
3351 srvid = talloc_array(mem_ctx, uint64_t, argc);
3352 if (srvid == NULL) {
3353 fprintf(stderr, "Memory allocation error\n");
3357 for (i=0; i<argc; i++) {
3358 srvid[i] = strtoull(argv[i], NULL, 0);
3361 ret = ctdb_ctrl_check_srvids(mem_ctx, ctdb->ev, ctdb->client,
3362 ctdb->cmd_pnn, TIMEOUT(), srvid, argc,
3365 fprintf(stderr, "Failed to check srvids on node %u\n",
3370 for (i=0; i<argc; i++) {
3371 printf("SRVID 0x%" PRIx64 " %s\n", srvid[i],
3372 (result[i] ? "exists" : "does not exist"));
3378 static int control_listnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3379 int argc, const char **argv)
3381 struct ctdb_node_map *nodemap;
3388 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
3389 if (nodemap == NULL) {
3393 for (i=0; i<nodemap->num; i++) {
3394 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
3398 if (options.machinereadable) {
3399 printf("%s%u%s%s%s\n", options.sep,
3400 nodemap->node[i].pnn, options.sep,
3401 ctdb_sock_addr_to_string(
3402 mem_ctx, &nodemap->node[i].addr),
3406 ctdb_sock_addr_to_string(
3407 mem_ctx, &nodemap->node[i].addr));
3414 static bool nodemap_identical(struct ctdb_node_map *nodemap1,
3415 struct ctdb_node_map *nodemap2)
3419 if (nodemap1->num != nodemap2->num) {
3423 for (i=0; i<nodemap1->num; i++) {
3424 struct ctdb_node_and_flags *n1, *n2;
3426 n1 = &nodemap1->node[i];
3427 n2 = &nodemap2->node[i];
3429 if ((n1->pnn != n2->pnn) ||
3430 (n1->flags != n2->flags) ||
3431 ! ctdb_sock_addr_same_ip(&n1->addr, &n2->addr)) {
3439 static int check_node_file_changes(TALLOC_CTX *mem_ctx,
3440 struct ctdb_node_map *nm,
3441 struct ctdb_node_map *fnm,
3445 bool check_failed = false;
3449 for (i=0; i<nm->num; i++) {
3450 if (i >= fnm->num) {
3452 "Node %u (%s) missing from nodes file\n",
3454 ctdb_sock_addr_to_string(
3455 mem_ctx, &nm->node[i].addr));
3456 check_failed = true;
3459 if (nm->node[i].flags & NODE_FLAGS_DELETED &&
3460 fnm->node[i].flags & NODE_FLAGS_DELETED) {
3461 /* Node remains deleted */
3465 if (! (nm->node[i].flags & NODE_FLAGS_DELETED) &&
3466 ! (fnm->node[i].flags & NODE_FLAGS_DELETED)) {
3467 /* Node not newly nor previously deleted */
3468 if (! ctdb_same_ip(&nm->node[i].addr,
3469 &fnm->node[i].addr)) {
3471 "Node %u has changed IP address"
3472 " (was %s, now %s)\n",
3474 ctdb_sock_addr_to_string(
3475 mem_ctx, &nm->node[i].addr),
3476 ctdb_sock_addr_to_string(
3477 mem_ctx, &fnm->node[i].addr));
3478 check_failed = true;
3480 if (nm->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3482 "WARNING: Node %u is disconnected."
3483 " You MUST fix this node manually!\n",
3490 if (fnm->node[i].flags & NODE_FLAGS_DELETED) {
3491 /* Node is being deleted */
3492 printf("Node %u is DELETED\n", nm->node[i].pnn);
3494 if (! (nm->node[i].flags & NODE_FLAGS_DISCONNECTED)) {
3496 "ERROR: Node %u is still connected\n",
3498 check_failed = true;
3503 if (nm->node[i].flags & NODE_FLAGS_DELETED) {
3504 /* Node was previously deleted */
3505 printf("Node %u is UNDELETED\n", nm->node[i].pnn);
3512 "ERROR: Nodes will not be reloaded due to previous error\n");
3516 /* Leftover nodes in file are NEW */
3517 for (; i < fnm->num; i++) {
3518 printf("Node %u is NEW\n", fnm->node[i].pnn);
3525 struct disable_recoveries_state {
3533 static void disable_recoveries_handler(uint64_t srvid, TDB_DATA data,
3536 struct disable_recoveries_state *state =
3537 (struct disable_recoveries_state *)private_data;
3540 if (data.dsize != sizeof(int)) {
3545 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
3546 ret = *(int *)data.dptr;
3548 state->status = ret;
3552 for (i=0; i<state->node_count; i++) {
3553 if (state->pnn_list[i] == ret) {
3554 state->reply[i] = true;
3560 for (i=0; i<state->node_count; i++) {
3561 if (! state->reply[i]) {
3562 state->done = false;
3568 static int disable_recoveries(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3569 uint32_t timeout, uint32_t *pnn_list, int count)
3571 struct ctdb_disable_message disable = { 0 };
3572 struct disable_recoveries_state state;
3575 disable.pnn = ctdb->pnn;
3576 disable.srvid = next_srvid(ctdb);
3577 disable.timeout = timeout;
3579 state.pnn_list = pnn_list;
3580 state.node_count = count;
3583 state.reply = talloc_zero_array(mem_ctx, bool, count);
3584 if (state.reply == NULL) {
3588 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
3590 disable_recoveries_handler,
3596 for (i=0; i<count; i++) {
3597 ret = ctdb_message_disable_recoveries(mem_ctx, ctdb->ev,
3606 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
3608 fprintf(stderr, "Timed out waiting to disable recoveries\n");
3610 ret = (state.status >= 0 ? 0 : 1);
3614 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
3615 disable.srvid, &state);
3619 static int control_reloadnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3620 int argc, const char **argv)
3622 struct ctdb_node_map *nodemap = NULL;
3623 struct ctdb_node_map *file_nodemap;
3624 struct ctdb_node_map *remote_nodemap;
3625 struct ctdb_req_control request;
3626 struct ctdb_reply_control **reply;
3632 nodemap = get_nodemap(ctdb, false);
3633 if (nodemap == NULL) {
3637 file_nodemap = read_nodes_file(mem_ctx, ctdb->pnn);
3638 if (file_nodemap == NULL) {
3642 for (i=0; i<nodemap->num; i++) {
3643 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3647 ret = ctdb_ctrl_get_nodes_file(mem_ctx, ctdb->ev, ctdb->client,
3648 nodemap->node[i].pnn, TIMEOUT(),
3652 "ERROR: Failed to get nodes file from node %u\n",
3653 nodemap->node[i].pnn);
3657 if (! nodemap_identical(file_nodemap, remote_nodemap)) {
3659 "ERROR: Nodes file on node %u differs"
3660 " from current node (%u)\n",
3661 nodemap->node[i].pnn, ctdb->pnn);
3666 ret = check_node_file_changes(mem_ctx, nodemap, file_nodemap, &reload);
3672 fprintf(stderr, "No change in nodes file,"
3673 " skipping unnecessary reload\n");
3677 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
3678 mem_ctx, &pnn_list);
3680 fprintf(stderr, "Memory allocation error\n");
3684 ret = disable_recoveries(mem_ctx, ctdb, 2*options.timelimit,
3687 fprintf(stderr, "Failed to disable recoveries\n");
3691 ctdb_req_control_reload_nodes_file(&request);
3692 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3693 pnn_list, count, TIMEOUT(),
3694 &request, NULL, &reply);
3696 bool failed = false;
3698 for (i=0; i<count; i++) {
3699 ret = ctdb_reply_control_reload_nodes_file(reply[i]);
3702 "Node %u failed to reload nodes\n",
3709 "You MUST fix failed nodes manually!\n");
3713 ret = disable_recoveries(mem_ctx, ctdb, 0, pnn_list, count);
3715 fprintf(stderr, "Failed to enable recoveries\n");
3722 static int moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3723 ctdb_sock_addr *addr, uint32_t pnn)
3725 struct ctdb_public_ip_list *pubip_list;
3726 struct ctdb_public_ip pubip;
3727 struct ctdb_node_map *nodemap;
3728 struct ctdb_req_control request;
3732 ret = ctdb_message_disable_ip_check(mem_ctx, ctdb->ev, ctdb->client,
3733 CTDB_BROADCAST_CONNECTED,
3734 2*options.timelimit);
3736 fprintf(stderr, "Failed to disable IP check\n");
3740 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3741 pnn, TIMEOUT(), &pubip_list);
3743 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3748 for (i=0; i<pubip_list->num; i++) {
3749 if (ctdb_same_ip(addr, &pubip_list->ip[i].addr)) {
3754 if (i == pubip_list->num) {
3755 fprintf(stderr, "Node %u CANNOT host IP address %s\n",
3756 pnn, ctdb_sock_addr_to_string(mem_ctx, addr));
3760 nodemap = get_nodemap(ctdb, false);
3761 if (nodemap == NULL) {
3765 count = list_of_active_nodes(nodemap, pnn, mem_ctx, &pnn_list);
3767 fprintf(stderr, "Memory allocation error\n");
3773 ctdb_req_control_release_ip(&request, &pubip);
3775 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3776 pnn_list, count, TIMEOUT(),
3777 &request, NULL, NULL);
3779 fprintf(stderr, "Failed to release IP on nodes\n");
3783 ret = ctdb_ctrl_takeover_ip(mem_ctx, ctdb->ev, ctdb->client,
3784 pnn, TIMEOUT(), &pubip);
3786 fprintf(stderr, "Failed to takeover IP on node %u\n", pnn);
3793 static int control_moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3794 int argc, const char **argv)
3796 ctdb_sock_addr addr;
3798 int ret, retries = 0;
3804 if (! parse_ip(argv[0], NULL, 0, &addr)) {
3805 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3809 pnn = strtoul(argv[1], NULL, 10);
3810 if (pnn == CTDB_UNKNOWN_PNN) {
3811 fprintf(stderr, "Invalid PNN %s\n", argv[1]);
3815 while (retries < 5) {
3816 ret = moveip(mem_ctx, ctdb, &addr, pnn);
3826 fprintf(stderr, "Failed to move IP %s to node %u\n",
3834 static int rebalancenode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3839 ret = ctdb_message_rebalance_node(mem_ctx, ctdb->ev, ctdb->client,
3840 CTDB_BROADCAST_CONNECTED, pnn);
3843 "Failed to ask recovery master to distribute IPs\n");
3850 static int control_addip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3851 int argc, const char **argv)
3853 ctdb_sock_addr addr;
3854 struct ctdb_public_ip_list *pubip_list;
3855 struct ctdb_addr_info addr_info;
3857 int ret, i, retries = 0;
3863 if (! parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
3864 fprintf(stderr, "Invalid IP/Mask %s\n", argv[0]);
3868 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3869 ctdb->cmd_pnn, TIMEOUT(), &pubip_list);
3871 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3876 for (i=0; i<pubip_list->num; i++) {
3877 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3878 fprintf(stderr, "Node already knows about IP %s\n",
3879 ctdb_sock_addr_to_string(mem_ctx, &addr));
3884 addr_info.addr = addr;
3885 addr_info.mask = mask;
3886 addr_info.iface = argv[1];
3888 while (retries < 5) {
3889 ret = ctdb_ctrl_add_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3890 ctdb->cmd_pnn, TIMEOUT(),
3901 fprintf(stderr, "Failed to add public IP to node %u."
3902 " Giving up\n", ctdb->cmd_pnn);
3906 ret = rebalancenode(mem_ctx, ctdb, ctdb->cmd_pnn);
3914 static int control_delip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3915 int argc, const char **argv)
3917 ctdb_sock_addr addr;
3918 struct ctdb_public_ip_list *pubip_list;
3919 struct ctdb_addr_info addr_info;
3926 if (! parse_ip(argv[0], NULL, 0, &addr)) {
3927 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3931 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3932 ctdb->cmd_pnn, TIMEOUT(), &pubip_list);
3934 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3939 for (i=0; i<pubip_list->num; i++) {
3940 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3945 if (i == pubip_list->num) {
3946 fprintf(stderr, "Node does not know about IP address %s\n",
3947 ctdb_sock_addr_to_string(mem_ctx, &addr));
3951 addr_info.addr = addr;
3953 addr_info.iface = NULL;
3955 ret = ctdb_ctrl_del_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3956 ctdb->cmd_pnn, TIMEOUT(), &addr_info);
3958 fprintf(stderr, "Failed to delete public IP from node %u\n",
3966 static int control_eventscript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3967 int argc, const char **argv)
3972 usage("eventscript");
3975 if (strcmp(argv[0], "monitor") != 0) {
3976 fprintf(stderr, "Only monitor event can be run\n");
3980 ret = ctdb_ctrl_run_eventscripts(mem_ctx, ctdb->ev, ctdb->client,
3981 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
3983 fprintf(stderr, "Failed to run monitor event on node %u\n",
3991 #define DB_VERSION 3
3992 #define MAX_DB_NAME 64
3993 #define MAX_REC_BUFFER_SIZE (100*1000)
3996 unsigned long version;
3998 unsigned long flags;
4001 char name[MAX_DB_NAME];
4004 struct backup_state {
4005 TALLOC_CTX *mem_ctx;
4006 struct ctdb_rec_buffer *recbuf;
4009 unsigned int nbuf, nrec;
4012 static int backup_handler(uint32_t reqid, struct ctdb_ltdb_header *header,
4013 TDB_DATA key, TDB_DATA data, void *private_data)
4015 struct backup_state *state = (struct backup_state *)private_data;
4019 if (state->recbuf == NULL) {
4020 state->recbuf = ctdb_rec_buffer_init(state->mem_ctx,
4022 if (state->recbuf == NULL) {
4027 ret = ctdb_rec_buffer_add(state->recbuf, state->recbuf, reqid,
4033 len = ctdb_rec_buffer_len(state->recbuf);
4034 if (len < MAX_REC_BUFFER_SIZE) {
4038 ret = ctdb_rec_buffer_write(state->recbuf, state->fd);
4040 fprintf(stderr, "Failed to write records to backup file\n");
4045 state->nrec += state->recbuf->count;
4046 TALLOC_FREE(state->recbuf);
4051 static int control_backupdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4052 int argc, const char **argv)
4054 const char *db_name;
4055 struct ctdb_db_context *db;
4058 struct backup_state state;
4059 struct db_header db_hdr;
4066 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4070 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4073 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4077 fd = open(argv[1], O_RDWR|O_CREAT, 0600);
4080 fprintf(stderr, "Failed to open file %s for writing\n",
4085 /* Write empty header first */
4086 ZERO_STRUCT(db_hdr);
4087 ret = write(fd, &db_hdr, sizeof(struct db_header));
4091 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4095 state.mem_ctx = mem_ctx;
4096 state.recbuf = NULL;
4101 ret = ctdb_db_traverse(db, true, false, backup_handler, &state);
4103 fprintf(stderr, "Failed to collect records from DB %s\n",
4109 if (state.recbuf != NULL) {
4110 ret = ctdb_rec_buffer_write(state.recbuf, state.fd);
4113 "Failed to write records to backup file\n");
4119 state.nrec += state.recbuf->count;
4120 TALLOC_FREE(state.recbuf);
4123 db_hdr.version = DB_VERSION;
4124 db_hdr.timestamp = time(NULL);
4125 db_hdr.flags = db_flags;
4126 db_hdr.nbuf = state.nbuf;
4127 db_hdr.nrec = state.nrec;
4128 strncpy(db_hdr.name, db_name, MAX_DB_NAME-1);
4130 lseek(fd, 0, SEEK_SET);
4131 ret = write(fd, &db_hdr, sizeof(struct db_header));
4135 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4140 printf("Database backed up to %s\n", argv[1]);
4144 static int control_restoredb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4145 int argc, const char **argv)
4147 const char *db_name = NULL;
4148 struct ctdb_db_context *db;
4149 struct db_header db_hdr;
4150 struct ctdb_node_map *nodemap;
4151 struct ctdb_req_control request;
4152 struct ctdb_reply_control **reply;
4153 struct ctdb_transdb wipedb;
4154 struct ctdb_pulldb_ext pulldb;
4155 struct ctdb_rec_buffer *recbuf;
4156 uint32_t generation;
4162 if (argc < 1 || argc > 2) {
4166 fd = open(argv[0], O_RDONLY, 0600);
4169 fprintf(stderr, "Failed to open file %s for reading\n",
4178 ret = read(fd, &db_hdr, sizeof(struct db_header));
4182 fprintf(stderr, "Failed to read db header from file %s\n",
4186 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4188 if (db_hdr.version != DB_VERSION) {
4190 "Wrong version of backup file, expected %u, got %lu\n",
4191 DB_VERSION, db_hdr.version);
4196 if (db_name == NULL) {
4197 db_name = db_hdr.name;
4200 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4201 localtime(&db_hdr.timestamp));
4202 printf("Restoring database %s from backup @ %s\n", db_name, timebuf);
4204 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4207 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4212 nodemap = get_nodemap(ctdb, false);
4213 if (nodemap == NULL) {
4214 fprintf(stderr, "Failed to get nodemap\n");
4219 ret = get_generation(mem_ctx, ctdb, &generation);
4221 fprintf(stderr, "Failed to get current generation\n");
4226 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4233 wipedb.db_id = ctdb_db_id(db);
4234 wipedb.tid = generation;
4236 ctdb_req_control_db_freeze(&request, wipedb.db_id);
4237 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4238 ctdb->client, pnn_list, count,
4239 TIMEOUT(), &request, NULL, NULL);
4245 ctdb_req_control_db_transaction_start(&request, &wipedb);
4246 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4247 pnn_list, count, TIMEOUT(),
4248 &request, NULL, NULL);
4253 ctdb_req_control_wipe_database(&request, &wipedb);
4254 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4255 pnn_list, count, TIMEOUT(),
4256 &request, NULL, NULL);
4261 pulldb.db_id = ctdb_db_id(db);
4263 pulldb.srvid = SRVID_CTDB_PUSHDB;
4265 ctdb_req_control_db_push_start(&request, &pulldb);
4266 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4267 pnn_list, count, TIMEOUT(),
4268 &request, NULL, NULL);
4273 for (i=0; i<db_hdr.nbuf; i++) {
4274 struct ctdb_req_message message;
4277 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4282 data.dsize = ctdb_rec_buffer_len(recbuf);
4283 data.dptr = talloc_size(mem_ctx, data.dsize);
4284 if (data.dptr == NULL) {
4288 ctdb_rec_buffer_push(recbuf, data.dptr);
4290 message.srvid = pulldb.srvid;
4291 message.data.data = data;
4293 ret = ctdb_client_message_multi(mem_ctx, ctdb->ev,
4301 talloc_free(recbuf);
4302 talloc_free(data.dptr);
4305 ctdb_req_control_db_push_confirm(&request, pulldb.db_id);
4306 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4307 pnn_list, count, TIMEOUT(),
4308 &request, NULL, &reply);
4313 for (i=0; i<count; i++) {
4314 uint32_t num_records;
4316 ret = ctdb_reply_control_db_push_confirm(reply[i],
4319 fprintf(stderr, "Invalid response from node %u\n",
4324 if (num_records != db_hdr.nrec) {
4325 fprintf(stderr, "Node %u received %u of %lu records\n",
4326 pnn_list[i], num_records, db_hdr.nrec);
4331 ctdb_req_control_db_set_healthy(&request, wipedb.db_id);
4332 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4333 pnn_list, count, TIMEOUT(),
4334 &request, NULL, NULL);
4339 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4340 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4341 pnn_list, count, TIMEOUT(),
4342 &request, NULL, NULL);
4347 ctdb_req_control_db_thaw(&request, wipedb.db_id);
4348 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4349 ctdb->client, pnn_list, count,
4350 TIMEOUT(), &request, NULL, NULL);
4355 printf("Database %s restored\n", db_name);
4361 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4362 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4366 struct dumpdbbackup_state {
4367 ctdb_rec_parser_func_t parser;
4368 struct dump_record_state sub_state;
4371 static int dumpdbbackup_handler(uint32_t reqid,
4372 struct ctdb_ltdb_header *header,
4373 TDB_DATA key, TDB_DATA data,
4376 struct dumpdbbackup_state *state =
4377 (struct dumpdbbackup_state *)private_data;
4378 struct ctdb_ltdb_header hdr;
4381 ret = ctdb_ltdb_header_extract(&data, &hdr);
4386 return state->parser(reqid, &hdr, key, data, &state->sub_state);
4389 static int control_dumpdbbackup(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4390 int argc, const char **argv)
4392 struct db_header db_hdr;
4394 struct dumpdbbackup_state state;
4398 usage("dumpbackup");
4401 fd = open(argv[0], O_RDONLY, 0600);
4404 fprintf(stderr, "Failed to open file %s for reading\n",
4409 ret = read(fd, &db_hdr, sizeof(struct db_header));
4413 fprintf(stderr, "Failed to read db header from file %s\n",
4417 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4419 if (db_hdr.version != DB_VERSION) {
4421 "Wrong version of backup file, expected %u, got %lu\n",
4422 DB_VERSION, db_hdr.version);
4427 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4428 localtime(&db_hdr.timestamp));
4429 printf("Dumping database %s from backup @ %s\n",
4430 db_hdr.name, timebuf);
4432 state.parser = dump_record;
4433 state.sub_state.count = 0;
4435 for (i=0; i<db_hdr.nbuf; i++) {
4436 struct ctdb_rec_buffer *recbuf;
4438 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4440 fprintf(stderr, "Failed to read records\n");
4445 ret = ctdb_rec_buffer_traverse(recbuf, dumpdbbackup_handler,
4448 fprintf(stderr, "Failed to dump records\n");
4455 printf("Dumped %u record(s)\n", state.sub_state.count);
4459 static int control_wipedb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4460 int argc, const char **argv)
4462 const char *db_name;
4463 struct ctdb_db_context *db;
4466 struct ctdb_node_map *nodemap;
4467 struct ctdb_req_control request;
4468 struct ctdb_transdb wipedb;
4469 uint32_t generation;
4477 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4481 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4484 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4488 nodemap = get_nodemap(ctdb, false);
4489 if (nodemap == NULL) {
4490 fprintf(stderr, "Failed to get nodemap\n");
4494 ret = get_generation(mem_ctx, ctdb, &generation);
4496 fprintf(stderr, "Failed to get current generation\n");
4500 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4506 ctdb_req_control_db_freeze(&request, db_id);
4507 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4508 ctdb->client, pnn_list, count,
4509 TIMEOUT(), &request, NULL, NULL);
4514 wipedb.db_id = db_id;
4515 wipedb.tid = generation;
4517 ctdb_req_control_db_transaction_start(&request, &wipedb);
4518 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4519 pnn_list, count, TIMEOUT(),
4520 &request, NULL, NULL);
4525 ctdb_req_control_wipe_database(&request, &wipedb);
4526 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4527 pnn_list, count, TIMEOUT(),
4528 &request, NULL, NULL);
4533 ctdb_req_control_db_set_healthy(&request, db_id);
4534 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4535 pnn_list, count, TIMEOUT(),
4536 &request, NULL, NULL);
4541 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4542 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4543 pnn_list, count, TIMEOUT(),
4544 &request, NULL, NULL);
4549 ctdb_req_control_db_thaw(&request, db_id);
4550 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4551 ctdb->client, pnn_list, count,
4552 TIMEOUT(), &request, NULL, NULL);
4557 printf("Database %s wiped\n", db_name);
4562 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4563 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4567 static int control_recmaster(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4568 int argc, const char **argv)
4573 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
4574 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
4579 printf("%u\n", recmaster);
4583 static void print_scriptstatus_one(struct ctdb_script_list *slist,
4584 const char *event_str)
4589 if (slist == NULL) {
4590 if (! options.machinereadable) {
4591 printf("%s cycle never run\n", event_str);
4596 for (i=0; i<slist->num_scripts; i++) {
4597 if (slist->script[i].status != -ENOEXEC) {
4602 if (! options.machinereadable) {
4603 printf("%d scripts were executed last %s cycle\n",
4604 num_run, event_str);
4607 for (i=0; i<slist->num_scripts; i++) {
4608 const char *status = NULL;
4610 switch (slist->script[i].status) {
4612 status = "TIMEDOUT";
4615 status = "DISABLED";
4621 if (slist->script[i].status > 0) {
4627 if (options.machinereadable) {
4628 printf("%s%s%s%s%s%i%s%s%s%lu.%06lu%s%lu.%06lu%s%s%s\n",
4630 event_str, options.sep,
4631 slist->script[i].name, options.sep,
4632 slist->script[i].status, options.sep,
4633 status, options.sep,
4634 (unsigned long)slist->script[i].start.tv_sec,
4635 slist->script[i].start.tv_usec, options.sep,
4636 (unsigned long)slist->script[i].finished.tv_sec,
4637 slist->script[i].finished.tv_usec, options.sep,
4638 slist->script[i].output, options.sep);
4643 printf("%-20s Status:%s ",
4644 slist->script[i].name, status);
4646 /* Some other error, eg from stat. */
4647 printf("%-20s Status:CANNOT RUN (%s)",
4648 slist->script[i].name,
4649 strerror(-slist->script[i].status));
4652 if (slist->script[i].status >= 0) {
4653 printf("Duration:%.3lf ",
4654 timeval_delta(&slist->script[i].finished,
4655 &slist->script[i].start));
4657 if (slist->script[i].status != -ENOEXEC) {
4658 printf("%s", ctime(&slist->script[i].start.tv_sec));
4659 if (slist->script[i].status != 0) {
4660 printf(" OUTPUT:%s\n",
4661 slist->script[i].output);
4669 static void print_scriptstatus(struct ctdb_script_list **slist,
4670 int count, const char **event_str)
4674 if (options.machinereadable) {
4675 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
4677 "Type", options.sep,
4678 "Name", options.sep,
4679 "Code", options.sep,
4680 "Status", options.sep,
4681 "Start", options.sep,
4683 "Error Output", options.sep);
4686 for (i=0; i<count; i++) {
4687 print_scriptstatus_one(slist[i], event_str[i]);
4691 static int control_scriptstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4692 int argc, const char **argv)
4694 struct ctdb_script_list **slist;
4695 const char *event_str;
4696 enum ctdb_event event;
4697 const char *all_events[] = {
4698 "init", "setup", "startup", "monitor",
4699 "takeip", "releaseip", "updateip", "ipreallocated" };
4702 int count, start, end, num;
4705 usage("scriptstatus");
4709 event_str = "monitor";
4711 event_str = argv[0];
4717 for (i=0; i<ARRAY_SIZE(all_events); i++) {
4718 if (strcmp(event_str, all_events[i]) == 0) {
4727 if (strcmp(event_str, "all") == 0) {
4729 count = ARRAY_SIZE(all_events);
4735 fprintf(stderr, "Unknown event name %s\n", argv[0]);
4736 usage("scriptstatus");
4739 slist = talloc_array(mem_ctx, struct ctdb_script_list *, count);
4740 if (slist == NULL) {
4741 fprintf(stderr, "Memory allocation error\n");
4746 for (i=start; i<end; i++) {
4747 event = ctdb_event_from_string(all_events[i]);
4749 ret = ctdb_ctrl_get_event_script_status(mem_ctx, ctdb->ev,
4756 "failed to get script status for %s event\n",
4761 if (slist[num] == NULL) {
4766 /* The ETIME status is ignored for certain events.
4767 * In that case the status is 0, but endtime is not set.
4769 for (j=0; j<slist[num]->num_scripts; j++) {
4770 if (slist[num]->script[j].status == 0 &&
4771 timeval_is_zero(&slist[num]->script[j].finished)) {
4772 slist[num]->script[j].status = -ETIME;
4779 print_scriptstatus(slist, count, &all_events[start]);
4783 static int control_enablescript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4784 int argc, const char **argv)
4789 usage("enablescript");
4792 ret = ctdb_ctrl_enable_script(mem_ctx, ctdb->ev, ctdb->client,
4793 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4795 fprintf(stderr, "Failed to enable script %s\n", argv[0]);
4802 static int control_disablescript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4803 int argc, const char **argv)
4808 usage("disablescript");
4811 ret = ctdb_ctrl_disable_script(mem_ctx, ctdb->ev, ctdb->client,
4812 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4814 fprintf(stderr, "Failed to disable script %s\n", argv[0]);
4821 static int control_natgw(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4822 int argc, const char **argv)
4824 char *t, *natgw_helper = NULL;
4830 t = getenv("CTDB_NATGW_HELPER");
4832 natgw_helper = talloc_strdup(mem_ctx, t);
4834 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4835 CTDB_HELPER_BINDIR);
4838 if (natgw_helper == NULL) {
4839 fprintf(stderr, "Unable to set NAT gateway helper\n");
4843 return run_helper("NAT gateway helper", natgw_helper, argv[0]);
4846 static int control_natgwlist(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4847 int argc, const char **argv)
4849 char *t, *natgw_helper = NULL;
4855 t = getenv("CTDB_NATGW_HELPER");
4857 natgw_helper = talloc_strdup(mem_ctx, t);
4859 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4860 CTDB_HELPER_BINDIR);
4863 if (natgw_helper == NULL) {
4864 fprintf(stderr, "Unable to set NAT gateway helper\n");
4868 return run_helper("NAT gateway helper", natgw_helper, "natgwlist");
4872 * Find the PNN of the current node
4873 * discover the pnn by loading the nodes file and try to bind
4874 * to all addresses one at a time until the ip address is found.
4876 static bool find_node_xpnn(TALLOC_CTX *mem_ctx, uint32_t *pnn)
4878 struct ctdb_node_map *nodemap;
4881 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
4882 if (nodemap == NULL) {
4886 for (i=0; i<nodemap->num; i++) {
4887 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
4890 if (ctdb_sys_have_ip(&nodemap->node[i].addr)) {
4892 *pnn = nodemap->node[i].pnn;
4894 talloc_free(nodemap);
4899 fprintf(stderr, "Failed to detect PNN of the current node.\n");
4900 talloc_free(nodemap);
4904 static int control_getreclock(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4905 int argc, const char **argv)
4907 const char *reclock;
4911 usage("getreclock");
4914 ret = ctdb_ctrl_get_reclock_file(mem_ctx, ctdb->ev, ctdb->client,
4915 ctdb->cmd_pnn, TIMEOUT(), &reclock);
4920 if (reclock != NULL) {
4921 printf("%s\n", reclock);
4927 static int control_setlmasterrole(TALLOC_CTX *mem_ctx,
4928 struct ctdb_context *ctdb,
4929 int argc, const char **argv)
4931 uint32_t lmasterrole = 0;
4935 usage("setlmasterrole");
4938 if (strcmp(argv[0], "on") == 0) {
4940 } else if (strcmp(argv[0], "off") == 0) {
4943 usage("setlmasterrole");
4946 ret = ctdb_ctrl_set_lmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4947 ctdb->cmd_pnn, TIMEOUT(), lmasterrole);
4955 static int control_setrecmasterrole(TALLOC_CTX *mem_ctx,
4956 struct ctdb_context *ctdb,
4957 int argc, const char **argv)
4959 uint32_t recmasterrole = 0;
4963 usage("setrecmasterrole");
4966 if (strcmp(argv[0], "on") == 0) {
4968 } else if (strcmp(argv[0], "off") == 0) {
4971 usage("setrecmasterrole");
4974 ret = ctdb_ctrl_set_recmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4975 ctdb->cmd_pnn, TIMEOUT(),
4984 static int control_setdbreadonly(TALLOC_CTX *mem_ctx,
4985 struct ctdb_context *ctdb,
4986 int argc, const char **argv)
4993 usage("setdbreadonly");
4996 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
5000 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5001 fprintf(stderr, "Cannot set READONLY on persistent DB\n");
5005 ret = ctdb_ctrl_set_db_readonly(mem_ctx, ctdb->ev, ctdb->client,
5006 ctdb->cmd_pnn, TIMEOUT(), db_id);
5014 static int control_setdbsticky(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5015 int argc, const char **argv)
5022 usage("setdbsticky");
5025 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
5029 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5030 fprintf(stderr, "Cannot set STICKY on persistent DB\n");
5034 ret = ctdb_ctrl_set_db_sticky(mem_ctx, ctdb->ev, ctdb->client,
5035 ctdb->cmd_pnn, TIMEOUT(), db_id);
5043 static int control_pfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5044 int argc, const char **argv)
5046 const char *db_name;
5047 struct ctdb_db_context *db;
5048 struct ctdb_transaction_handle *h;
5053 if (argc < 2 || argc > 3) {
5057 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5061 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5062 fprintf(stderr, "DB %s is not a persistent database\n",
5067 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5070 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5074 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5076 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5080 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5081 TIMEOUT(), db, true, &h);
5083 fprintf(stderr, "Failed to start transaction on db %s\n",
5088 ret = ctdb_transaction_fetch_record(h, key, mem_ctx, &data);
5090 fprintf(stderr, "Failed to read record for key %s\n",
5092 ctdb_transaction_cancel(h);
5096 printf("%.*s\n", (int)data.dsize, data.dptr);
5098 ctdb_transaction_cancel(h);
5102 static int control_pstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5103 int argc, const char **argv)
5105 const char *db_name;
5106 struct ctdb_db_context *db;
5107 struct ctdb_transaction_handle *h;
5116 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5120 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5121 fprintf(stderr, "DB %s is not a persistent database\n",
5126 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5129 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5133 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5135 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5139 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5141 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5145 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5146 TIMEOUT(), db, false, &h);
5148 fprintf(stderr, "Failed to start transaction on db %s\n",
5153 ret = ctdb_transaction_store_record(h, key, data);
5155 fprintf(stderr, "Failed to store record for key %s\n",
5157 ctdb_transaction_cancel(h);
5161 ret = ctdb_transaction_commit(h);
5163 fprintf(stderr, "Failed to commit transaction on db %s\n",
5165 ctdb_transaction_cancel(h);
5172 static int control_pdelete(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5173 int argc, const char **argv)
5175 const char *db_name;
5176 struct ctdb_db_context *db;
5177 struct ctdb_transaction_handle *h;
5186 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5190 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5191 fprintf(stderr, "DB %s is not a persistent database\n",
5196 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5199 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5203 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5205 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5209 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5210 TIMEOUT(), db, false, &h);
5212 fprintf(stderr, "Failed to start transaction on db %s\n",
5217 ret = ctdb_transaction_delete_record(h, key);
5219 fprintf(stderr, "Failed to delete record for key %s\n",
5221 ctdb_transaction_cancel(h);
5225 ret = ctdb_transaction_commit(h);
5227 fprintf(stderr, "Failed to commit transaction on db %s\n",
5229 ctdb_transaction_cancel(h);
5236 static int ptrans_parse_string(TALLOC_CTX *mem_ctx, const char **ptr, TDB_DATA *data)
5244 /* Skip whitespace */
5245 n = strspn(*ptr, " \t");
5249 /* Quoted ASCII string - no wide characters! */
5251 n = strcspn(t, "\"");
5254 ret = str_to_data(t, n, mem_ctx, data);
5261 fprintf(stderr, "Unmatched \" in input %s\n", *ptr);
5265 fprintf(stderr, "Unsupported input format in %s\n", *ptr);
5272 #define MAX_LINE_SIZE 1024
5274 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
5275 TDB_DATA *key, TDB_DATA *value)
5277 char line [MAX_LINE_SIZE]; /* FIXME: make this more flexible? */
5281 ptr = fgets(line, MAX_LINE_SIZE, file);
5287 ret = ptrans_parse_string(mem_ctx, &ptr, key);
5288 if (ret != 0 || ptr == NULL || key->dptr == NULL) {
5289 /* Line Ignored but not EOF */
5295 ret = ptrans_parse_string(mem_ctx, &ptr, value);
5297 /* Line Ignored but not EOF */
5298 talloc_free(key->dptr);
5306 static int control_ptrans(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5307 int argc, const char **argv)
5309 const char *db_name;
5310 struct ctdb_db_context *db;
5311 struct ctdb_transaction_handle *h;
5314 TDB_DATA key, value;
5317 if (argc < 1 || argc > 2) {
5321 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5325 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5326 fprintf(stderr, "DB %s is not a persistent database\n",
5332 file = fopen(argv[1], "r");
5334 fprintf(stderr, "Failed to open file %s\n", argv[1]);
5341 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5344 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5348 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5349 TIMEOUT(), db, false, &h);
5351 fprintf(stderr, "Failed to start transaction on db %s\n",
5356 while (ptrans_get_key_value(mem_ctx, file, &key, &value)) {
5357 if (key.dsize != 0) {
5358 ret = ctdb_transaction_store_record(h, key, value);
5360 fprintf(stderr, "Failed to store record\n");
5361 ctdb_transaction_cancel(h);
5364 talloc_free(key.dptr);
5365 talloc_free(value.dptr);
5369 ret = ctdb_transaction_commit(h);
5371 fprintf(stderr, "Failed to commit transaction on db %s\n",
5373 ctdb_transaction_cancel(h);
5377 if (file != stdin) {
5383 static int control_tfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5384 int argc, const char **argv)
5386 struct tdb_context *tdb;
5388 struct ctdb_ltdb_header header;
5391 if (argc < 2 || argc > 3) {
5395 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5397 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5401 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5403 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5408 data = tdb_fetch(tdb, key);
5409 if (data.dptr == NULL) {
5410 fprintf(stderr, "No record for key %s\n", argv[1]);
5415 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
5416 fprintf(stderr, "Invalid record for key %s\n", argv[1]);
5427 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
5429 fprintf(stderr, "Failed to open output file %s\n",
5434 nwritten = sys_write(fd, data.dptr, data.dsize);
5435 if (nwritten != data.dsize) {
5436 fprintf(stderr, "Failed to write record to file\n");
5445 ret = ctdb_ltdb_header_extract(&data, &header);
5447 fprintf(stderr, "Failed to parse header from data\n");
5451 dump_ltdb_header(&header);
5452 dump_tdb_data("data", data);
5457 static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5458 int argc, const char **argv)
5460 struct tdb_context *tdb;
5461 TDB_DATA key, data[2], value;
5462 struct ctdb_ltdb_header header;
5463 uint8_t header_buf[sizeof(struct ctdb_ltdb_header)];
5466 if (argc < 3 || argc > 5) {
5470 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5472 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5476 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5478 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5483 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &value);
5485 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5490 ZERO_STRUCT(header);
5493 header.rsn = (uint64_t)strtoull(argv[3], NULL, 0);
5496 header.dmaster = (uint32_t)atol(argv[4]);
5499 header.flags = (uint32_t)atol(argv[5]);
5502 ctdb_ltdb_header_push(&header, header_buf);
5504 data[0].dsize = ctdb_ltdb_header_len(&header);
5505 data[0].dptr = header_buf;
5507 data[1].dsize = value.dsize;
5508 data[1].dptr = value.dptr;
5510 ret = tdb_storev(tdb, key, data, 2, TDB_REPLACE);
5512 fprintf(stderr, "Failed to write record %s to file %s\n",
5521 static int control_readkey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5522 int argc, const char **argv)
5524 const char *db_name;
5525 struct ctdb_db_context *db;
5526 struct ctdb_record_handle *h;
5529 bool readonly = false;
5532 if (argc < 2 || argc > 3) {
5537 if (strcmp(argv[2], "readonly") == 0) {
5544 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5548 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5549 fprintf(stderr, "DB %s is not a volatile database\n",
5554 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5557 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5561 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5563 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5567 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5568 db, key, readonly, &h, NULL, &data);
5570 fprintf(stderr, "Failed to read record for key %s\n",
5573 printf("Data: size:%zu ptr:[%.*s]\n", data.dsize,
5574 (int)data.dsize, data.dptr);
5581 static int control_writekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5582 int argc, const char **argv)
5584 const char *db_name;
5585 struct ctdb_db_context *db;
5586 struct ctdb_record_handle *h;
5595 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5599 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5600 fprintf(stderr, "DB %s is not a volatile database\n",
5605 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5608 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5612 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5614 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5618 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5620 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5624 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5625 db, key, false, &h, NULL, NULL);
5627 fprintf(stderr, "Failed to lock record for key %s\n", argv[0]);
5631 ret = ctdb_store_record(h, data);
5633 fprintf(stderr, "Failed to store record for key %s\n",
5641 static int control_deletekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5642 int argc, const char **argv)
5644 const char *db_name;
5645 struct ctdb_db_context *db;
5646 struct ctdb_record_handle *h;
5655 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5659 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5660 fprintf(stderr, "DB %s is not a volatile database\n",
5665 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5668 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5672 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5674 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5678 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5679 db, key, false, &h, NULL, &data);
5681 fprintf(stderr, "Failed to fetch record for key %s\n",
5686 ret = ctdb_delete_record(h);
5688 fprintf(stderr, "Failed to delete record for key %s\n",
5696 static int control_checktcpport(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5697 int argc, const char **argv)
5699 struct sockaddr_in sin;
5705 usage("chktcpport");
5708 port = atoi(argv[0]);
5710 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
5712 fprintf(stderr, "Failed to open local socket\n");
5716 v = fcntl(s, F_GETFL, 0);
5717 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK)) {
5718 fprintf(stderr, "Unable to set socket non-blocking\n");
5723 bzero(&sin, sizeof(sin));
5724 sin.sin_family = AF_INET;
5725 sin.sin_port = htons(port);
5726 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
5729 fprintf(stderr, "Failed to bind to TCP port %u\n", port);
5736 static int control_getdbseqnum(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5737 int argc, const char **argv)
5740 const char *db_name;
5745 usage("getdbseqnum");
5748 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5752 ret = ctdb_ctrl_get_db_seqnum(mem_ctx, ctdb->ev, ctdb->client,
5753 ctdb->cmd_pnn, TIMEOUT(), db_id,
5756 fprintf(stderr, "Failed to get sequence number for DB %s\n",
5761 printf("0x%"PRIx64"\n", seqnum);
5765 static int control_nodestatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5766 int argc, const char **argv)
5768 const char *nodestring = NULL;
5769 struct ctdb_node_map *nodemap;
5773 usage("nodestatus");
5777 nodestring = argv[0];
5780 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap)) {
5784 nodemap = get_nodemap(ctdb, false);
5785 if (nodemap == NULL) {
5789 if (options.machinereadable) {
5790 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5792 print_nodemap(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5796 for (i=0; i<nodemap->num; i++) {
5797 ret |= nodemap->node[i].flags;
5806 } db_stats_fields[] = {
5807 #define DBSTATISTICS_FIELD(n) { #n, offsetof(struct ctdb_db_statistics, n) }
5808 DBSTATISTICS_FIELD(db_ro_delegations),
5809 DBSTATISTICS_FIELD(db_ro_revokes),
5810 DBSTATISTICS_FIELD(locks.num_calls),
5811 DBSTATISTICS_FIELD(locks.num_current),
5812 DBSTATISTICS_FIELD(locks.num_pending),
5813 DBSTATISTICS_FIELD(locks.num_failed),
5814 DBSTATISTICS_FIELD(db_ro_delegations),
5817 static void print_dbstatistics(const char *db_name,
5818 struct ctdb_db_statistics *s)
5821 const char *prefix = NULL;
5824 printf("DB Statistics %s\n", db_name);
5826 for (i=0; i<ARRAY_SIZE(db_stats_fields); i++) {
5827 if (strchr(db_stats_fields[i].name, '.') != NULL) {
5828 preflen = strcspn(db_stats_fields[i].name, ".") + 1;
5830 strncmp(prefix, db_stats_fields[i].name, preflen) != 0) {
5831 prefix = db_stats_fields[i].name;
5832 printf(" %*.*s\n", preflen-1, preflen-1,
5833 db_stats_fields[i].name);
5838 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
5839 db_stats_fields[i].name+preflen, preflen ? 0 : 4, "",
5840 *(uint32_t *)(db_stats_fields[i].offset+(uint8_t *)s));
5843 printf(" hop_count_buckets:");
5844 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5845 printf(" %d", s->hop_count_bucket[i]);
5849 printf(" lock_buckets:");
5850 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5851 printf(" %d", s->locks.buckets[i]);
5855 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5856 "locks_latency MIN/AVG/MAX",
5857 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
5858 s->locks.latency.max, s->locks.latency.num);
5860 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5861 "vacuum_latency MIN/AVG/MAX",
5862 s->vacuum.latency.min, LATENCY_AVG(s->vacuum.latency),
5863 s->vacuum.latency.max, s->vacuum.latency.num);
5865 printf(" Num Hot Keys: %d\n", s->num_hot_keys);
5866 for (i=0; i<s->num_hot_keys; i++) {
5868 printf(" Count:%d Key:", s->hot_keys[i].count);
5869 for (j=0; j<s->hot_keys[i].key.dsize; j++) {
5870 printf("%02x", s->hot_keys[i].key.dptr[j] & 0xff);
5876 static int control_dbstatistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5877 int argc, const char **argv)
5880 const char *db_name;
5881 struct ctdb_db_statistics *dbstats;
5885 usage("dbstatistics");
5888 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5892 ret = ctdb_ctrl_get_db_statistics(mem_ctx, ctdb->ev, ctdb->client,
5893 ctdb->cmd_pnn, TIMEOUT(), db_id,
5896 fprintf(stderr, "Failed to get statistics for DB %s\n",
5901 print_dbstatistics(db_name, dbstats);
5905 struct disable_takeover_runs_state {
5913 static void disable_takeover_run_handler(uint64_t srvid, TDB_DATA data,
5916 struct disable_takeover_runs_state *state =
5917 (struct disable_takeover_runs_state *)private_data;
5920 if (data.dsize != sizeof(int)) {
5925 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
5926 ret = *(int *)data.dptr;
5928 state->status = ret;
5932 for (i=0; i<state->node_count; i++) {
5933 if (state->pnn_list[i] == ret) {
5934 state->reply[i] = true;
5940 for (i=0; i<state->node_count; i++) {
5941 if (! state->reply[i]) {
5942 state->done = false;
5948 static int disable_takeover_runs(TALLOC_CTX *mem_ctx,
5949 struct ctdb_context *ctdb, uint32_t timeout,
5950 uint32_t *pnn_list, int count)
5952 struct ctdb_disable_message disable = { 0 };
5953 struct disable_takeover_runs_state state;
5956 disable.pnn = ctdb->pnn;
5957 disable.srvid = next_srvid(ctdb);
5958 disable.timeout = timeout;
5960 state.pnn_list = pnn_list;
5961 state.node_count = count;
5964 state.reply = talloc_zero_array(mem_ctx, bool, count);
5965 if (state.reply == NULL) {
5969 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
5971 disable_takeover_run_handler,
5977 for (i=0; i<count; i++) {
5978 ret = ctdb_message_disable_takeover_runs(mem_ctx, ctdb->ev,
5987 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
5989 fprintf(stderr, "Timed out waiting to disable takeover runs\n");
5991 ret = (state.status >= 0 ? 0 : 1);
5995 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
5996 disable.srvid, &state);
6000 static int control_reloadips(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
6001 int argc, const char **argv)
6003 const char *nodestring = NULL;
6004 struct ctdb_node_map *nodemap, *nodemap2;
6005 struct ctdb_req_control request;
6006 uint32_t *pnn_list, *pnn_list2;
6007 int ret, count, count2;
6014 nodestring = argv[0];
6017 nodemap = get_nodemap(ctdb, false);
6018 if (nodemap == NULL) {
6022 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap2)) {
6026 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
6027 mem_ctx, &pnn_list);
6029 fprintf(stderr, "Memory allocation error\n");
6033 count2 = list_of_active_nodes(nodemap2, CTDB_UNKNOWN_PNN,
6034 mem_ctx, &pnn_list2);
6036 fprintf(stderr, "Memory allocation error\n");
6040 /* Disable takeover runs on all connected nodes. A reply
6041 * indicating success is needed from each node so all nodes
6042 * will need to be active.
6044 * A check could be added to not allow reloading of IPs when
6045 * there are disconnected nodes. However, this should
6046 * probably be left up to the administrator.
6048 ret = disable_takeover_runs(mem_ctx, ctdb, 2*options.timelimit,
6051 fprintf(stderr, "Failed to disable takeover runs\n");
6055 /* Now tell all the desired nodes to reload their public IPs.
6056 * Keep trying this until it succeeds. This assumes all
6057 * failures are transient, which might not be true...
6059 ctdb_req_control_reload_public_ips(&request);
6060 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
6061 pnn_list2, count2, TIMEOUT(),
6062 &request, NULL, NULL);
6064 fprintf(stderr, "Failed to reload IPs on some nodes.\n");
6067 /* It isn't strictly necessary to wait until takeover runs are
6068 * re-enabled but doing so can't hurt.
6070 ret = disable_takeover_runs(mem_ctx, ctdb, 0, pnn_list, count);
6072 fprintf(stderr, "Failed to enable takeover runs\n");
6076 return ipreallocate(mem_ctx, ctdb);
6079 static int control_ipiface(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
6080 int argc, const char **argv)
6082 ctdb_sock_addr addr;
6089 if (! parse_ip(argv[0], NULL, 0, &addr)) {
6090 fprintf(stderr, "Failed to Parse IP %s\n", argv[0]);
6094 iface = ctdb_sys_find_ifname(&addr);
6095 if (iface == NULL) {
6096 fprintf(stderr, "Failed to find interface for IP %s\n",
6106 static const struct ctdb_cmd {
6108 int (*fn)(TALLOC_CTX *, struct ctdb_context *, int, const char **);
6109 bool without_daemon; /* can be run without daemon running ? */
6110 bool remote; /* can be run on remote nodes */
6113 } ctdb_commands[] = {
6114 { "version", control_version, true, false,
6115 "show version of ctdb", NULL },
6116 { "status", control_status, false, true,
6117 "show node status", NULL },
6118 { "uptime", control_uptime, false, true,
6119 "show node uptime", NULL },
6120 { "ping", control_ping, false, true,
6121 "ping all nodes", NULL },
6122 { "runstate", control_runstate, false, true,
6123 "get/check runstate of a node",
6124 "[setup|first_recovery|startup|running]" },
6125 { "getvar", control_getvar, false, true,
6126 "get a tunable variable", "<name>" },
6127 { "setvar", control_setvar, false, true,
6128 "set a tunable variable", "<name> <value>" },
6129 { "listvars", control_listvars, false, true,
6130 "list tunable variables", NULL },
6131 { "statistics", control_statistics, false, true,
6132 "show ctdb statistics", NULL },
6133 { "statisticsreset", control_statistics_reset, false, true,
6134 "reset ctdb statistics", NULL },
6135 { "stats", control_stats, false, true,
6136 "show rolling statistics", "[count]" },
6137 { "ip", control_ip, false, true,
6138 "show public ips", "[all]" },
6139 { "ipinfo", control_ipinfo, false, true,
6140 "show public ip details", "<ip>" },
6141 { "ifaces", control_ifaces, false, true,
6142 "show interfaces", NULL },
6143 { "setifacelink", control_setifacelink, false, true,
6144 "set interface link status", "<iface> up|down" },
6145 { "process-exists", control_process_exists, false, true,
6146 "check if a process exists on a node", "<pid>" },
6147 { "getdbmap", control_getdbmap, false, true,
6148 "show attached databases", NULL },
6149 { "getdbstatus", control_getdbstatus, false, true,
6150 "show database status", "<dbname|dbid>" },
6151 { "catdb", control_catdb, false, false,
6152 "dump cluster-wide ctdb database", "<dbname|dbid>" },
6153 { "cattdb", control_cattdb, false, false,
6154 "dump local ctdb database", "<dbname|dbid>" },
6155 { "getmonmode", control_getmonmode, false, true,
6156 "show monitoring mode", NULL },
6157 { "getcapabilities", control_getcapabilities, false, true,
6158 "show node capabilities", NULL },
6159 { "pnn", control_pnn, false, false,
6160 "show the pnn of the currnet node", NULL },
6161 { "lvs", control_lvs, false, false,
6162 "show lvs configuration", "master|list|status" },
6163 { "disablemonitor", control_disable_monitor, false, true,
6164 "disable monitoring", NULL },
6165 { "enablemonitor", control_enable_monitor, false, true,
6166 "enable monitoring", NULL },
6167 { "setdebug", control_setdebug, false, true,
6168 "set debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
6169 { "getdebug", control_getdebug, false, true,
6170 "get debug level", NULL },
6171 { "attach", control_attach, false, false,
6172 "attach a database", "<dbname> [persistent]" },
6173 { "detach", control_detach, false, false,
6174 "detach database(s)", "<dbname|dbid> ..." },
6175 { "dumpmemory", control_dumpmemory, false, true,
6176 "dump ctdbd memory map", NULL },
6177 { "rddumpmemory", control_rddumpmemory, false, true,
6178 "dump recoverd memory map", NULL },
6179 { "getpid", control_getpid, false, true,
6180 "get ctdbd process ID", NULL },
6181 { "disable", control_disable, false, true,
6182 "disable a node", NULL },
6183 { "enable", control_enable, false, true,
6184 "enable a node", NULL },
6185 { "stop", control_stop, false, true,
6186 "stop a node", NULL },
6187 { "continue", control_continue, false, true,
6188 "continue a stopped node", NULL },
6189 { "ban", control_ban, false, true,
6190 "ban a node", "<bantime>"},
6191 { "unban", control_unban, false, true,
6192 "unban a node", NULL },
6193 { "shutdown", control_shutdown, false, true,
6194 "shutdown ctdb daemon", NULL },
6195 { "recover", control_recover, false, true,
6196 "force recovery", NULL },
6197 { "sync", control_ipreallocate, false, true,
6198 "run ip reallocation (deprecated)", NULL },
6199 { "ipreallocate", control_ipreallocate, false, true,
6200 "run ip reallocation", NULL },
6201 { "isnotrecmaster", control_isnotrecmaster, false, false,
6202 "check if local node is the recmaster", NULL },
6203 { "gratarp", control_gratarp, false, true,
6204 "send a gratuitous arp", "<ip> <interface>" },
6205 { "tickle", control_tickle, true, false,
6206 "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6207 { "gettickles", control_gettickles, false, true,
6208 "get the list of tickles", "<ip> [<port>]" },
6209 { "addtickle", control_addtickle, false, true,
6210 "add a tickle", "<ip>:<port> <ip>:<port>" },
6211 { "deltickle", control_deltickle, false, true,
6212 "delete a tickle", "<ip>:<port> <ip>:<port>" },
6213 { "check_srvids", control_check_srvids, false, true,
6214 "check if srvid is registered", "<id> [<id> ...]" },
6215 { "listnodes", control_listnodes, true, true,
6216 "list nodes in the cluster", NULL },
6217 { "reloadnodes", control_reloadnodes, false, false,
6218 "reload the nodes file all nodes", NULL },
6219 { "moveip", control_moveip, false, false,
6220 "move an ip address to another node", "<ip> <node>" },
6221 { "addip", control_addip, false, true,
6222 "add an ip address to a node", "<ip/mask> <iface>" },
6223 { "delip", control_delip, false, true,
6224 "delete an ip address from a node", "<ip>" },
6225 { "eventscript", control_eventscript, false, true,
6226 "run an event", "monitor" },
6227 { "backupdb", control_backupdb, false, false,
6228 "backup a database into a file", "<dbname|dbid> <file>" },
6229 { "restoredb", control_restoredb, false, false,
6230 "restore a database from a file", "<file> [dbname]" },
6231 { "dumpdbbackup", control_dumpdbbackup, true, false,
6232 "dump database from a backup file", "<file>" },
6233 { "wipedb", control_wipedb, false, false,
6234 "wipe the contents of a database.", "<dbname|dbid>"},
6235 { "recmaster", control_recmaster, false, true,
6236 "show the pnn for the recovery master", NULL },
6237 { "scriptstatus", control_scriptstatus, false, true,
6238 "show event script status",
6239 "[init|setup|startup|monitor|takeip|releaseip|ipreallocated]" },
6240 { "enablescript", control_enablescript, false, true,
6241 "enable an eventscript", "<script>"},
6242 { "disablescript", control_disablescript, false, true,
6243 "disable an eventscript", "<script>"},
6244 { "natgw", control_natgw, false, false,
6245 "show natgw configuration", "master|list|status" },
6246 { "natgwlist", control_natgwlist, false, false,
6247 "show the nodes belonging to this natgw configuration", NULL },
6248 { "getreclock", control_getreclock, false, true,
6249 "get recovery lock file", NULL },
6250 { "setlmasterrole", control_setlmasterrole, false, true,
6251 "set LMASTER role", "on|off" },
6252 { "setrecmasterrole", control_setrecmasterrole, false, true,
6253 "set RECMASTER role", "on|off"},
6254 { "setdbreadonly", control_setdbreadonly, false, true,
6255 "enable readonly records", "<dbname|dbid>" },
6256 { "setdbsticky", control_setdbsticky, false, true,
6257 "enable sticky records", "<dbname|dbid>"},
6258 { "pfetch", control_pfetch, false, false,
6259 "fetch record from persistent database", "<dbname|dbid> <key> [<file>]" },
6260 { "pstore", control_pstore, false, false,
6261 "write record to persistent database", "<dbname|dbid> <key> <value>" },
6262 { "pdelete", control_pdelete, false, false,
6263 "delete record from persistent database", "<dbname|dbid> <key>" },
6264 { "ptrans", control_ptrans, false, false,
6265 "update a persistent database (from file or stdin)", "<dbname|dbid> [<file>]" },
6266 { "tfetch", control_tfetch, false, true,
6267 "fetch a record", "<tdb-file> <key> [<file>]" },
6268 { "tstore", control_tstore, false, true,
6269 "store a record", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6270 { "readkey", control_readkey, false, false,
6271 "read value of a database key", "<dbname|dbid> <key> [readonly]" },
6272 { "writekey", control_writekey, false, false,
6273 "write value for a database key", "<dbname|dbid> <key> <value>" },
6274 { "deletekey", control_deletekey, false, false,
6275 "delete a database key", "<dbname|dbid> <key>" },
6276 { "checktcpport", control_checktcpport, true, false,
6277 "check if a service is bound to a specific tcp port or not", "<port>" },
6278 { "getdbseqnum", control_getdbseqnum, false, false,
6279 "get database sequence number", "<dbname|dbid>" },
6280 { "nodestatus", control_nodestatus, false, true,
6281 "show and return node status", "[all|<pnn-list>]" },
6282 { "dbstatistics", control_dbstatistics, false, true,
6283 "show database statistics", "<dbname|dbid>" },
6284 { "reloadips", control_reloadips, false, false,
6285 "reload the public addresses file", "[all|<pnn-list>]" },
6286 { "ipiface", control_ipiface, true, false,
6287 "Find the interface an ip address is hosted on", "<ip>" },
6290 static const struct ctdb_cmd *match_command(const char *command)
6292 const struct ctdb_cmd *cmd;
6295 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6296 cmd = &ctdb_commands[i];
6297 if (strlen(command) == strlen(cmd->name) &&
6298 strncmp(command, cmd->name, strlen(command)) == 0) {
6308 * Show usage message
6310 static void usage_full(void)
6314 poptPrintHelp(pc, stdout, 0);
6315 printf("\nCommands:\n");
6316 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6317 printf(" %-15s %-27s %s\n",
6318 ctdb_commands[i].name,
6319 ctdb_commands[i].args ? ctdb_commands[i].args : "",
6320 ctdb_commands[i].msg);
6324 static void usage(const char *command)
6326 const struct ctdb_cmd *cmd;
6328 if (command == NULL) {
6333 cmd = match_command(command);
6337 poptPrintUsage(pc, stdout, 0);
6338 printf("\nCommands:\n");
6339 printf(" %-15s %-27s %s\n",
6340 cmd->name, cmd->args ? cmd->args : "", cmd->msg);
6346 struct poptOption cmdline_options[] = {
6348 { "socket", 's', POPT_ARG_STRING, &options.socket, 0,
6349 "CTDB socket path", "filename" },
6350 { "debug", 'd', POPT_ARG_STRING, &options.debuglevelstr, 0,
6352 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0,
6353 "timelimit (in seconds)" },
6354 { "node", 'n', POPT_ARG_INT, &options.pnn, 0,
6355 "node specification - integer" },
6356 { NULL, 'Y', POPT_ARG_NONE, &options.machinereadable, 0,
6357 "enable machine readable output", NULL },
6358 { "separator", 'x', POPT_ARG_STRING, &options.sep, 0,
6359 "specify separator for machine readable output", "CHAR" },
6360 { NULL, 'X', POPT_ARG_NONE, &options.machineparsable, 0,
6361 "enable machine parsable output with separator |", NULL },
6362 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0,
6363 "enable verbose output", NULL },
6364 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0,
6365 "die if runtime exceeds this limit (in seconds)" },
6369 static int process_command(const struct ctdb_cmd *cmd, int argc,
6372 TALLOC_CTX *tmp_ctx;
6373 struct ctdb_context *ctdb;
6376 uint64_t srvid_offset;
6378 tmp_ctx = talloc_new(NULL);
6379 if (tmp_ctx == NULL) {
6380 fprintf(stderr, "Memory allocation error\n");
6384 if (cmd->without_daemon) {
6385 if (options.pnn != -1) {
6387 "Cannot specify node for command %s\n",
6392 ret = cmd->fn(tmp_ctx, NULL, argc-1, argv+1);
6393 talloc_free(tmp_ctx);
6397 ctdb = talloc_zero(tmp_ctx, struct ctdb_context);
6399 fprintf(stderr, "Memory allocation error\n");
6403 ctdb->ev = tevent_context_init(ctdb);
6404 if (ctdb->ev == NULL) {
6405 fprintf(stderr, "Failed to initialize tevent\n");
6409 ret = ctdb_client_init(ctdb, ctdb->ev, options.socket, &ctdb->client);
6411 fprintf(stderr, "Failed to connect to CTDB daemon (%s)\n",
6414 if (!find_node_xpnn(ctdb, NULL)) {
6415 fprintf(stderr, "Is this node part of CTDB cluster?\n");
6420 ctdb->pnn = ctdb_client_pnn(ctdb->client);
6421 srvid_offset = getpid() & 0xFFFF;
6422 ctdb->srvid = SRVID_CTDB_TOOL | (srvid_offset << 16);
6424 if (options.pnn != -1) {
6425 status = verify_pnn(ctdb, options.pnn);
6430 ctdb->cmd_pnn = options.pnn;
6432 ctdb->cmd_pnn = ctdb->pnn;
6435 if (! cmd->remote && ctdb->pnn != ctdb->cmd_pnn) {
6436 fprintf(stderr, "Node cannot be specified for command %s\n",
6441 ret = cmd->fn(tmp_ctx, ctdb, argc-1, argv+1);
6442 talloc_free(tmp_ctx);
6446 talloc_free(tmp_ctx);
6450 static void signal_handler(int sig)
6452 fprintf(stderr, "Maximum runtime exceeded - exiting\n");
6455 static void alarm_handler(int sig)
6457 /* Kill any child processes */
6458 signal(SIGTERM, signal_handler);
6464 int main(int argc, const char *argv[])
6467 const char **extra_argv;
6469 const struct ctdb_cmd *cmd;
6470 const char *ctdb_socket;
6476 /* Set default options */
6477 options.socket = CTDB_SOCKET;
6478 options.debuglevelstr = NULL;
6479 options.timelimit = 10;
6481 options.maxruntime = 0;
6484 ctdb_socket = getenv("CTDB_SOCKET");
6485 if (ctdb_socket != NULL) {
6486 options.socket = ctdb_socket;
6489 pc = poptGetContext(argv[0], argc, argv, cmdline_options,
6490 POPT_CONTEXT_KEEP_FIRST);
6491 while ((opt = poptGetNextOpt(pc)) != -1) {
6492 fprintf(stderr, "Invalid option %s: %s\n",
6493 poptBadOption(pc, 0), poptStrerror(opt));
6497 if (options.maxruntime == 0) {
6498 const char *ctdb_timeout;
6500 ctdb_timeout = getenv("CTDB_TIMEOUT");
6501 if (ctdb_timeout != NULL) {
6502 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6504 options.maxruntime = 120;
6507 if (options.maxruntime <= 120) {
6508 /* default timeout is 120 seconds */
6509 options.maxruntime = 120;
6512 if (options.machineparsable) {
6513 options.machinereadable = 1;
6516 /* setup the remaining options for the commands */
6518 extra_argv = poptGetArgs(pc);
6521 while (extra_argv[extra_argc]) extra_argc++;
6524 if (extra_argc < 1) {
6528 cmd = match_command(extra_argv[0]);
6530 fprintf(stderr, "Unknown command '%s'\n", extra_argv[0]);
6534 /* Enable logging */
6535 setup_logging("ctdb", DEBUG_STDERR);
6536 if (debug_level_parse(options.debuglevelstr, &loglevel)) {
6537 DEBUGLEVEL = loglevel;
6539 DEBUGLEVEL = DEBUG_ERR;
6542 signal(SIGALRM, alarm_handler);
6543 alarm(options.maxruntime);
6545 ret = process_command(cmd, extra_argc, extra_argv);
6550 (void)poptFreeContext(pc);