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"
36 #include "common/db_hash.h"
37 #include "common/logging.h"
38 #include "protocol/protocol.h"
39 #include "protocol/protocol_api.h"
40 #include "common/system.h"
41 #include "client/client.h"
43 #define TIMEOUT() timeval_current_ofs(options.timelimit, 0)
45 #define SRVID_CTDB_TOOL (CTDB_SRVID_TOOL_RANGE | 0x0001000000000000LL)
46 #define SRVID_CTDB_PUSHDB (CTDB_SRVID_TOOL_RANGE | 0x0002000000000000LL)
50 const char *debuglevelstr;
58 int printemptyrecords;
65 static poptContext pc;
68 struct tevent_context *ev;
69 struct ctdb_client_context *client;
70 struct ctdb_node_map *nodemap;
71 uint32_t pnn, cmd_pnn;
75 static void usage(const char *command);
81 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
83 return (tv2->tv_sec - tv->tv_sec) +
84 (tv2->tv_usec - tv->tv_usec) * 1.0e-6;
87 static struct ctdb_node_and_flags *get_node_by_pnn(
88 struct ctdb_node_map *nodemap,
93 for (i=0; i<nodemap->num; i++) {
94 if (nodemap->node[i].pnn == pnn) {
95 return &nodemap->node[i];
101 static const char *pretty_print_flags(TALLOC_CTX *mem_ctx, uint32_t flags)
103 static const struct {
107 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
108 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
109 { NODE_FLAGS_BANNED, "BANNED" },
110 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
111 { NODE_FLAGS_DELETED, "DELETED" },
112 { NODE_FLAGS_STOPPED, "STOPPED" },
113 { NODE_FLAGS_INACTIVE, "INACTIVE" },
115 char *flags_str = NULL;
118 for (i=0; i<ARRAY_SIZE(flag_names); i++) {
119 if (flags & flag_names[i].flag) {
120 if (flags_str == NULL) {
121 flags_str = talloc_asprintf(mem_ctx,
122 "%s", flag_names[i].name);
124 flags_str = talloc_asprintf_append(flags_str,
125 "|%s", flag_names[i].name);
127 if (flags_str == NULL) {
128 return "OUT-OF-MEMORY";
132 if (flags_str == NULL) {
139 static uint64_t next_srvid(struct ctdb_context *ctdb)
146 * Get consistent nodemap information.
148 * If nodemap is already cached, use that. If not get it.
149 * If the current node is BANNED, then get nodemap from "better" node.
151 static struct ctdb_node_map *get_nodemap(struct ctdb_context *ctdb, bool force)
154 struct ctdb_node_map *nodemap;
155 struct ctdb_node_and_flags *node;
156 uint32_t current_node;
160 TALLOC_FREE(ctdb->nodemap);
163 if (ctdb->nodemap != NULL) {
164 return ctdb->nodemap;
167 tmp_ctx = talloc_new(ctdb);
168 if (tmp_ctx == NULL) {
172 current_node = ctdb->pnn;
174 ret = ctdb_ctrl_get_nodemap(tmp_ctx, ctdb->ev, ctdb->client,
175 current_node, TIMEOUT(), &nodemap);
177 fprintf(stderr, "Failed to get nodemap from node %u\n",
182 node = get_node_by_pnn(nodemap, current_node);
183 if (node->flags & NODE_FLAGS_BANNED) {
186 current_node = (current_node + 1) % nodemap->num;
187 node = get_node_by_pnn(nodemap, current_node);
189 (NODE_FLAGS_DELETED|NODE_FLAGS_DISCONNECTED))) {
192 } while (current_node != ctdb->pnn);
194 if (current_node == ctdb->pnn) {
195 /* Tried all nodes in the cluster */
196 fprintf(stderr, "Warning: All nodes are banned.\n");
203 ctdb->nodemap = talloc_steal(ctdb, nodemap);
207 talloc_free(tmp_ctx);
211 static bool verify_pnn(struct ctdb_context *ctdb, int pnn)
213 struct ctdb_node_map *nodemap;
221 nodemap = get_nodemap(ctdb, false);
222 if (nodemap == NULL) {
227 for (i=0; i<nodemap->num; i++) {
228 if (nodemap->node[i].pnn == pnn) {
234 fprintf(stderr, "Node %u does not exist\n", pnn);
238 if (nodemap->node[i].flags &
239 (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED)) {
240 fprintf(stderr, "Node %u has status %s\n", pnn,
241 pretty_print_flags(ctdb, nodemap->node[i].flags));
248 static struct ctdb_node_map *talloc_nodemap(TALLOC_CTX *mem_ctx,
249 struct ctdb_node_map *nodemap)
251 struct ctdb_node_map *nodemap2;
253 nodemap2 = talloc_zero(mem_ctx, struct ctdb_node_map);
254 if (nodemap2 == NULL) {
258 nodemap2->node = talloc_array(nodemap2, struct ctdb_node_and_flags,
260 if (nodemap2->node == NULL) {
261 talloc_free(nodemap2);
269 * Get the number and the list of matching nodes
271 * nodestring := NULL | all | pnn,[pnn,...]
273 * If nodestring is NULL, use the current node.
275 static bool parse_nodestring(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
276 const char *nodestring,
277 struct ctdb_node_map **out)
279 struct ctdb_node_map *nodemap, *nodemap2;
280 struct ctdb_node_and_flags *node;
283 nodemap = get_nodemap(ctdb, false);
284 if (nodemap == NULL) {
288 nodemap2 = talloc_nodemap(mem_ctx, nodemap);
289 if (nodemap2 == NULL) {
293 if (nodestring == NULL) {
294 for (i=0; i<nodemap->num; i++) {
295 if (nodemap->node[i].pnn == ctdb->cmd_pnn) {
296 nodemap2->node[0] = nodemap->node[i];
305 if (strcmp(nodestring, "all") == 0) {
306 for (i=0; i<nodemap->num; i++) {
307 nodemap2->node[i] = nodemap->node[i];
309 nodemap2->num = nodemap->num;
315 ns = talloc_strdup(mem_ctx, nodestring);
320 tok = strtok(ns, ",");
321 while (tok != NULL) {
325 pnn = (uint32_t)strtoul(tok, &endptr, 0);
326 if (pnn == 0 && tok == endptr) {
327 fprintf(stderr, "Invalid node %s\n", tok);
331 node = get_node_by_pnn(nodemap, pnn);
333 fprintf(stderr, "Node %u does not exist\n",
338 nodemap2->node[nodemap2->num] = *node;
341 tok = strtok(NULL, ",");
350 /* Compare IP address */
351 static bool ctdb_same_ip(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
355 if (ip1->sa.sa_family != ip2->sa.sa_family) {
359 switch (ip1->sa.sa_family) {
361 ret = (memcmp(&ip1->ip.sin_addr, &ip2->ip.sin_addr,
362 sizeof(struct in_addr)) == 0);
366 ret = (memcmp(&ip1->ip6.sin6_addr, &ip2->ip6.sin6_addr,
367 sizeof(struct in6_addr)) == 0);
374 /* Append a node to a node map with given address and flags */
375 static bool node_map_add(struct ctdb_node_map *nodemap,
376 const char *nstr, uint32_t flags)
380 struct ctdb_node_and_flags *n;
382 if (! parse_ip(nstr, NULL, 0, &addr)) {
383 fprintf(stderr, "Invalid IP address %s\n", nstr);
388 nodemap->node = talloc_realloc(nodemap, nodemap->node,
389 struct ctdb_node_and_flags, num+1);
390 if (nodemap->node == NULL) {
394 n = &nodemap->node[num];
399 nodemap->num = num+1;
403 /* Read a nodes file into a node map */
404 static struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
410 struct ctdb_node_map *nodemap;
412 nodemap = talloc_zero(mem_ctx, struct ctdb_node_map);
413 if (nodemap == NULL) {
417 lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
422 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
426 for (i=0; i<nlines; i++) {
432 /* strip leading spaces */
433 while((*node == ' ') || (*node == '\t')) {
439 /* strip trailing spaces */
441 ((node[len-1] == ' ') || (node[len-1] == '\t')))
451 /* A "deleted" node is a node that is
452 commented out in the nodes file. This is
453 used instead of removing a line, which
454 would cause subsequent nodes to change
456 flags = NODE_FLAGS_DELETED;
457 node = discard_const("0.0.0.0");
461 if (! node_map_add(nodemap, node, flags)) {
463 TALLOC_FREE(nodemap);
472 static struct ctdb_node_map *read_nodes_file(TALLOC_CTX *mem_ctx, uint32_t pnn)
474 struct ctdb_node_map *nodemap;
476 const char *nodes_list = NULL;
478 if (pnn != CTDB_UNKNOWN_PNN) {
479 nodepath = talloc_asprintf(mem_ctx, "CTDB_NODES_%u", pnn);
480 if (nodepath != NULL) {
481 nodes_list = getenv(nodepath);
484 if (nodes_list == NULL) {
485 nodes_list = getenv("CTDB_NODES");
487 if (nodes_list == NULL) {
488 const char *basedir = getenv("CTDB_BASE");
489 if (basedir == NULL) {
490 basedir = CTDB_ETCDIR;
492 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes", basedir);
493 if (nodes_list == NULL) {
494 fprintf(stderr, "Memory allocation error\n");
499 nodemap = ctdb_read_nodes_file(mem_ctx, nodes_list);
500 if (nodemap == NULL) {
501 fprintf(stderr, "Failed to read nodes file \"%s\"\n",
509 static struct ctdb_dbid *db_find(TALLOC_CTX *mem_ctx,
510 struct ctdb_context *ctdb,
511 struct ctdb_dbid_map *dbmap,
514 struct ctdb_dbid *db = NULL;
518 for (i=0; i<dbmap->num; i++) {
519 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
520 ctdb->pnn, TIMEOUT(),
521 dbmap->dbs[i].db_id, &name);
526 if (strcmp(db_name, name) == 0) {
527 talloc_free(discard_const(name));
536 static bool db_exists(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
537 const char *db_arg, uint32_t *db_id,
538 const char **db_name, uint8_t *db_flags)
540 struct ctdb_dbid_map *dbmap;
541 struct ctdb_dbid *db = NULL;
543 const char *name = NULL;
546 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
547 ctdb->pnn, TIMEOUT(), &dbmap);
552 if (strncmp(db_arg, "0x", 2) == 0) {
553 id = strtoul(db_arg, NULL, 0);
554 for (i=0; i<dbmap->num; i++) {
555 if (id == dbmap->dbs[i].db_id) {
562 db = db_find(mem_ctx, ctdb, dbmap, name);
566 fprintf(stderr, "No database matching '%s' found\n", db_arg);
571 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
572 ctdb->pnn, TIMEOUT(), id, &name);
581 if (db_name != NULL) {
582 *db_name = talloc_strdup(mem_ctx, name);
584 if (db_flags != NULL) {
585 *db_flags = db->flags;
590 static int h2i(char h)
592 if (h >= 'a' && h <= 'f') {
595 if (h >= 'A' && h <= 'F') {
601 static int hex_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
608 fprintf(stderr, "Key (%s) contains odd number of hex digits\n",
613 data.dsize = len / 2;
614 data.dptr = talloc_size(mem_ctx, data.dsize);
615 if (data.dptr == NULL) {
619 for (i=0; i<data.dsize; i++) {
620 data.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
627 static int str_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
633 if (strncmp(str, "0x", 2) == 0) {
634 ret = hex_to_data(str+2, len-2, mem_ctx, &data);
636 data.dptr = talloc_memdup(mem_ctx, str, len);
637 if (data.dptr == NULL) {
647 static int run_helper(const char *command, const char *path, const char *arg1)
650 int save_errno, status, ret;
655 fprintf(stderr, "Failed to fork %s (%s) - %s\n",
656 command, path, strerror(save_errno));
661 ret = execl(path, path, arg1, NULL);
665 /* Should not happen */
669 ret = waitpid(pid, &status, 0);
672 fprintf(stderr, "waitpid() failed for %s - %s\n",
673 command, strerror(save_errno));
677 if (WIFEXITED(status)) {
678 ret = WEXITSTATUS(status);
682 if (WIFSIGNALED(status)) {
683 fprintf(stderr, "%s terminated with signal %d\n",
684 command, WTERMSIG(status));
695 static int control_version(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
696 int argc, const char **argv)
698 printf("%s\n", CTDB_VERSION_STRING);
702 static bool partially_online(TALLOC_CTX *mem_ctx,
703 struct ctdb_context *ctdb,
704 struct ctdb_node_and_flags *node)
706 struct ctdb_iface_list *iface_list;
710 if (node->flags != 0) {
714 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
715 node->pnn, TIMEOUT(), &iface_list);
721 for (i=0; i < iface_list->num; i++) {
722 if (iface_list->iface[i].link_state == 0) {
731 static void print_nodemap_machine(TALLOC_CTX *mem_ctx,
732 struct ctdb_context *ctdb,
733 struct ctdb_node_map *nodemap,
736 struct ctdb_node_and_flags *node;
739 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
743 "Disconnected", options.sep,
744 "Banned", options.sep,
745 "Disabled", options.sep,
746 "Unhealthy", options.sep,
747 "Stopped", options.sep,
748 "Inactive", options.sep,
749 "PartiallyOnline", options.sep,
750 "ThisNode", options.sep);
752 for (i=0; i<nodemap->num; i++) {
753 node = &nodemap->node[i];
754 if (node->flags & NODE_FLAGS_DELETED) {
758 printf("%s%u%s%s%s%d%s%d%s%d%s%d%s%d%s%d%s%d%s%c%s\n",
760 node->pnn, options.sep,
761 ctdb_sock_addr_to_string(mem_ctx, &node->addr),
763 !! (node->flags & NODE_FLAGS_DISCONNECTED), options.sep,
764 !! (node->flags & NODE_FLAGS_BANNED), options.sep,
765 !! (node->flags & NODE_FLAGS_PERMANENTLY_DISABLED),
767 !! (node->flags & NODE_FLAGS_UNHEALTHY), options.sep,
768 !! (node->flags & NODE_FLAGS_STOPPED), options.sep,
769 !! (node->flags & NODE_FLAGS_INACTIVE), options.sep,
770 partially_online(mem_ctx, ctdb, node), options.sep,
771 (node->pnn == mypnn)?'Y':'N', options.sep);
776 static void print_nodemap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
777 struct ctdb_node_map *nodemap, uint32_t mypnn)
779 struct ctdb_node_and_flags *node;
780 int num_deleted_nodes = 0;
783 for (i=0; i<nodemap->num; i++) {
784 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
789 if (num_deleted_nodes == 0) {
790 printf("Number of nodes:%d\n", nodemap->num);
792 printf("Number of nodes:%d (including %d deleted nodes)\n",
793 nodemap->num, num_deleted_nodes);
796 for (i=0; i<nodemap->num; i++) {
797 node = &nodemap->node[i];
798 if (node->flags & NODE_FLAGS_DELETED) {
802 printf("pnn:%u %-16s %s%s\n",
804 ctdb_sock_addr_to_string(mem_ctx, &node->addr),
805 partially_online(mem_ctx, ctdb, node) ?
807 pretty_print_flags(mem_ctx, node->flags),
808 node->pnn == mypnn ? " (THIS NODE)" : "");
812 static void print_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
813 struct ctdb_node_map *nodemap, uint32_t mypnn,
814 struct ctdb_vnn_map *vnnmap, int recmode,
819 print_nodemap(mem_ctx, ctdb, nodemap, mypnn);
821 if (vnnmap->generation == INVALID_GENERATION) {
822 printf("Generation:INVALID\n");
824 printf("Generation:%u\n", vnnmap->generation);
826 printf("Size:%d\n", vnnmap->size);
827 for (i=0; i<vnnmap->size; i++) {
828 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
831 printf("Recovery mode:%s (%d)\n",
832 recmode == CTDB_RECOVERY_NORMAL ? "NORMAL" : "RECOVERY",
834 printf("Recovery master:%d\n", recmaster);
837 static int control_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
838 int argc, const char **argv)
840 struct ctdb_node_map *nodemap;
841 struct ctdb_vnn_map *vnnmap;
850 nodemap = get_nodemap(ctdb, false);
851 if (nodemap == NULL) {
855 if (options.machinereadable == 1) {
856 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
860 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
861 ctdb->cmd_pnn, TIMEOUT(), &vnnmap);
866 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
867 ctdb->cmd_pnn, TIMEOUT(), &recmode);
872 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
873 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
878 print_status(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn, vnnmap,
883 static int control_uptime(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
884 int argc, const char **argv)
886 struct ctdb_uptime *uptime;
887 int ret, tmp, days, hours, minutes, seconds;
889 ret = ctdb_ctrl_uptime(mem_ctx, ctdb->ev, ctdb->client,
890 ctdb->cmd_pnn, TIMEOUT(), &uptime);
895 printf("Current time of node %-4u : %s",
896 ctdb->cmd_pnn, ctime(&uptime->current_time.tv_sec));
898 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
899 seconds = tmp % 60; tmp /= 60;
900 minutes = tmp % 60; tmp /= 60;
901 hours = tmp % 24; tmp /= 24;
904 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s",
905 days, hours, minutes, seconds,
906 ctime(&uptime->ctdbd_start_time.tv_sec));
908 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
909 seconds = tmp % 60; tmp /= 60;
910 minutes = tmp % 60; tmp /= 60;
911 hours = tmp % 24; tmp /= 24;
914 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s",
915 days, hours, minutes, seconds,
916 ctime(&uptime->last_recovery_finished.tv_sec));
918 printf("Duration of last recovery/failover: %lf seconds\n",
919 timeval_delta(&uptime->last_recovery_finished,
920 &uptime->last_recovery_started));
925 static int control_ping(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
926 int argc, const char **argv)
929 int ret, num_clients;
931 tv = timeval_current();
932 ret = ctdb_ctrl_ping(mem_ctx, ctdb->ev, ctdb->client,
933 ctdb->cmd_pnn, TIMEOUT(), &num_clients);
938 printf("response from %u time=%.6f sec (%d clients)\n",
939 ctdb->cmd_pnn, timeval_elapsed(&tv), num_clients);
943 const char *runstate_to_string(enum ctdb_runstate runstate);
944 enum ctdb_runstate runstate_from_string(const char *runstate_str);
946 static int control_runstate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
947 int argc, const char **argv)
949 enum ctdb_runstate runstate;
953 ret = ctdb_ctrl_get_runstate(mem_ctx, ctdb->ev, ctdb->client,
954 ctdb->cmd_pnn, TIMEOUT(), &runstate);
960 for (i=0; i<argc; i++) {
961 enum ctdb_runstate t;
964 t = ctdb_runstate_from_string(argv[i]);
965 if (t == CTDB_RUNSTATE_UNKNOWN) {
966 printf("Invalid run state (%s)\n", argv[i]);
977 printf("CTDB not in required run state (got %s)\n",
978 ctdb_runstate_to_string(runstate));
982 printf("%s\n", ctdb_runstate_to_string(runstate));
986 static int control_getvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
987 int argc, const char **argv)
989 struct ctdb_var_list *tun_var_list;
998 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
999 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1002 "Failed to get list of variables from node %u\n",
1008 for (i=0; i<tun_var_list->count; i++) {
1009 if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
1016 printf("No such tunable %s\n", argv[0]);
1020 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
1021 ctdb->cmd_pnn, TIMEOUT(), argv[0], &value);
1026 printf("%-26s = %u\n", argv[0], value);
1030 static int control_setvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1031 int argc, const char **argv)
1033 struct ctdb_var_list *tun_var_list;
1034 struct ctdb_tunable tunable;
1042 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1043 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1046 "Failed to get list of variables from node %u\n",
1052 for (i=0; i<tun_var_list->count; i++) {
1053 if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
1060 printf("No such tunable %s\n", argv[0]);
1064 tunable.name = argv[0];
1065 tunable.value = strtoul(argv[1], NULL, 0);
1067 ret = ctdb_ctrl_set_tunable(mem_ctx, ctdb->ev, ctdb->client,
1068 ctdb->cmd_pnn, TIMEOUT(), &tunable);
1072 "Setting obsolete tunable variable '%s'\n",
1081 static int control_listvars(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1082 int argc, const char **argv)
1084 struct ctdb_var_list *tun_var_list;
1091 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1092 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1097 for (i=0; i<tun_var_list->count; i++) {
1098 control_getvar(mem_ctx, ctdb, 1, &tun_var_list->var[i]);
1107 } stats_fields[] = {
1108 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
1109 STATISTICS_FIELD(num_clients),
1110 STATISTICS_FIELD(frozen),
1111 STATISTICS_FIELD(recovering),
1112 STATISTICS_FIELD(num_recoveries),
1113 STATISTICS_FIELD(client_packets_sent),
1114 STATISTICS_FIELD(client_packets_recv),
1115 STATISTICS_FIELD(node_packets_sent),
1116 STATISTICS_FIELD(node_packets_recv),
1117 STATISTICS_FIELD(keepalive_packets_sent),
1118 STATISTICS_FIELD(keepalive_packets_recv),
1119 STATISTICS_FIELD(node.req_call),
1120 STATISTICS_FIELD(node.reply_call),
1121 STATISTICS_FIELD(node.req_dmaster),
1122 STATISTICS_FIELD(node.reply_dmaster),
1123 STATISTICS_FIELD(node.reply_error),
1124 STATISTICS_FIELD(node.req_message),
1125 STATISTICS_FIELD(node.req_control),
1126 STATISTICS_FIELD(node.reply_control),
1127 STATISTICS_FIELD(client.req_call),
1128 STATISTICS_FIELD(client.req_message),
1129 STATISTICS_FIELD(client.req_control),
1130 STATISTICS_FIELD(timeouts.call),
1131 STATISTICS_FIELD(timeouts.control),
1132 STATISTICS_FIELD(timeouts.traverse),
1133 STATISTICS_FIELD(locks.num_calls),
1134 STATISTICS_FIELD(locks.num_current),
1135 STATISTICS_FIELD(locks.num_pending),
1136 STATISTICS_FIELD(locks.num_failed),
1137 STATISTICS_FIELD(total_calls),
1138 STATISTICS_FIELD(pending_calls),
1139 STATISTICS_FIELD(childwrite_calls),
1140 STATISTICS_FIELD(pending_childwrite_calls),
1141 STATISTICS_FIELD(memory_used),
1142 STATISTICS_FIELD(max_hop_count),
1143 STATISTICS_FIELD(total_ro_delegations),
1144 STATISTICS_FIELD(total_ro_revokes),
1147 #define LATENCY_AVG(v) ((v).num ? (v).total / (v).num : 0.0 )
1149 static void print_statistics_machine(struct ctdb_statistics *s,
1155 printf("CTDB version%s", options.sep);
1156 printf("Current time of statistics%s", options.sep);
1157 printf("Statistics collected since%s", options.sep);
1158 for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
1159 printf("%s%s", stats_fields[i].name, options.sep);
1161 printf("num_reclock_ctdbd_latency%s", options.sep);
1162 printf("min_reclock_ctdbd_latency%s", options.sep);
1163 printf("avg_reclock_ctdbd_latency%s", options.sep);
1164 printf("max_reclock_ctdbd_latency%s", options.sep);
1166 printf("num_reclock_recd_latency%s", options.sep);
1167 printf("min_reclock_recd_latency%s", options.sep);
1168 printf("avg_reclock_recd_latency%s", options.sep);
1169 printf("max_reclock_recd_latency%s", options.sep);
1171 printf("num_call_latency%s", options.sep);
1172 printf("min_call_latency%s", options.sep);
1173 printf("avg_call_latency%s", options.sep);
1174 printf("max_call_latency%s", options.sep);
1176 printf("num_lockwait_latency%s", options.sep);
1177 printf("min_lockwait_latency%s", options.sep);
1178 printf("avg_lockwait_latency%s", options.sep);
1179 printf("max_lockwait_latency%s", options.sep);
1181 printf("num_childwrite_latency%s", options.sep);
1182 printf("min_childwrite_latency%s", options.sep);
1183 printf("avg_childwrite_latency%s", options.sep);
1184 printf("max_childwrite_latency%s", options.sep);
1188 printf("%u%s", CTDB_PROTOCOL, options.sep);
1189 printf("%u%s", (uint32_t)s->statistics_current_time.tv_sec, options.sep);
1190 printf("%u%s", (uint32_t)s->statistics_start_time.tv_sec, options.sep);
1191 for (i=0;i<ARRAY_SIZE(stats_fields);i++) {
1193 *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s),
1196 printf("%u%s", s->reclock.ctdbd.num, options.sep);
1197 printf("%.6f%s", s->reclock.ctdbd.min, options.sep);
1198 printf("%.6f%s", LATENCY_AVG(s->reclock.ctdbd), options.sep);
1199 printf("%.6f%s", s->reclock.ctdbd.max, options.sep);
1201 printf("%u%s", s->reclock.recd.num, options.sep);
1202 printf("%.6f%s", s->reclock.recd.min, options.sep);
1203 printf("%.6f%s", LATENCY_AVG(s->reclock.recd), options.sep);
1204 printf("%.6f%s", s->reclock.recd.max, options.sep);
1206 printf("%d%s", s->call_latency.num, options.sep);
1207 printf("%.6f%s", s->call_latency.min, options.sep);
1208 printf("%.6f%s", LATENCY_AVG(s->call_latency), options.sep);
1209 printf("%.6f%s", s->call_latency.max, options.sep);
1211 printf("%d%s", s->childwrite_latency.num, options.sep);
1212 printf("%.6f%s", s->childwrite_latency.min, options.sep);
1213 printf("%.6f%s", LATENCY_AVG(s->childwrite_latency), options.sep);
1214 printf("%.6f%s", s->childwrite_latency.max, options.sep);
1218 static void print_statistics(struct ctdb_statistics *s)
1220 int tmp, days, hours, minutes, seconds;
1222 const char *prefix = NULL;
1225 tmp = s->statistics_current_time.tv_sec -
1226 s->statistics_start_time.tv_sec;
1227 seconds = tmp % 60; tmp /= 60;
1228 minutes = tmp % 60; tmp /= 60;
1229 hours = tmp % 24; tmp /= 24;
1232 printf("CTDB version %u\n", CTDB_PROTOCOL);
1233 printf("Current time of statistics : %s",
1234 ctime(&s->statistics_current_time.tv_sec));
1235 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s",
1236 days, hours, minutes, seconds,
1237 ctime(&s->statistics_start_time.tv_sec));
1239 for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
1240 if (strchr(stats_fields[i].name, '.') != NULL) {
1241 preflen = strcspn(stats_fields[i].name, ".") + 1;
1243 strncmp(prefix, stats_fields[i].name, preflen) != 0) {
1244 prefix = stats_fields[i].name;
1245 printf(" %*.*s\n", preflen-1, preflen-1,
1246 stats_fields[i].name);
1251 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
1252 stats_fields[i].name+preflen, preflen ? 0 : 4, "",
1253 *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s));
1256 printf(" hop_count_buckets:");
1257 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
1258 printf(" %d", s->hop_count_bucket[i]);
1261 printf(" lock_buckets:");
1262 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
1263 printf(" %d", s->locks.buckets[i]);
1266 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1267 "locks_latency MIN/AVG/MAX",
1268 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
1269 s->locks.latency.max, s->locks.latency.num);
1271 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1272 "reclock_ctdbd MIN/AVG/MAX",
1273 s->reclock.ctdbd.min, LATENCY_AVG(s->reclock.ctdbd),
1274 s->reclock.ctdbd.max, s->reclock.ctdbd.num);
1276 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1277 "reclock_recd MIN/AVG/MAX",
1278 s->reclock.recd.min, LATENCY_AVG(s->reclock.recd),
1279 s->reclock.recd.max, s->reclock.recd.num);
1281 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1282 "call_latency MIN/AVG/MAX",
1283 s->call_latency.min, LATENCY_AVG(s->call_latency),
1284 s->call_latency.max, s->call_latency.num);
1286 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1287 "childwrite_latency MIN/AVG/MAX",
1288 s->childwrite_latency.min,
1289 LATENCY_AVG(s->childwrite_latency),
1290 s->childwrite_latency.max, s->childwrite_latency.num);
1293 static int control_statistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1294 int argc, const char **argv)
1296 struct ctdb_statistics *stats;
1300 usage("statistics");
1303 ret = ctdb_ctrl_statistics(mem_ctx, ctdb->ev, ctdb->client,
1304 ctdb->cmd_pnn, TIMEOUT(), &stats);
1309 if (options.machinereadable) {
1310 print_statistics_machine(stats, true);
1312 print_statistics(stats);
1318 static int control_statistics_reset(TALLOC_CTX *mem_ctx,
1319 struct ctdb_context *ctdb,
1320 int argc, const char **argv)
1325 usage("statisticsreset");
1328 ret = ctdb_ctrl_statistics_reset(mem_ctx, ctdb->ev, ctdb->client,
1329 ctdb->cmd_pnn, TIMEOUT());
1337 static int control_stats(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1338 int argc, const char **argv)
1340 struct ctdb_statistics_list *slist;
1341 int ret, count = 0, i;
1342 bool show_header = true;
1349 count = atoi(argv[0]);
1352 ret = ctdb_ctrl_get_stat_history(mem_ctx, ctdb->ev, ctdb->client,
1353 ctdb->cmd_pnn, TIMEOUT(), &slist);
1358 for (i=0; i<slist->num; i++) {
1359 if (slist->stats[i].statistics_start_time.tv_sec == 0) {
1362 if (options.machinereadable == 1) {
1363 print_statistics_machine(&slist->stats[i],
1365 show_header = false;
1367 print_statistics(&slist->stats[i]);
1369 if (count > 0 && i == count) {
1377 static void print_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1378 struct ctdb_public_ip_list *ips,
1379 struct ctdb_public_ip_info **ipinfo,
1383 char *conf, *avail, *active;
1385 if (options.machinereadable == 1) {
1386 printf("%s%s%s%s%s", options.sep,
1387 "Public IP", options.sep,
1388 "Node", options.sep);
1389 if (options.verbose == 1) {
1390 printf("%s%s%s%s%s%s\n",
1391 "ActiveInterfaces", options.sep,
1392 "AvailableInterfaces", options.sep,
1393 "ConfiguredInterfaces", options.sep);
1399 printf("Public IPs on ALL nodes\n");
1401 printf("Public IPs on node %u\n", ctdb->cmd_pnn);
1405 /* IPs are reverse sorted */
1406 for (i=ips->num-1; i>=0; i--) {
1408 if (options.machinereadable == 1) {
1409 printf("%s%s%s%d%s", options.sep,
1410 ctdb_sock_addr_to_string(
1411 mem_ctx, &ips->ip[i].addr),
1413 (int)ips->ip[i].pnn, options.sep);
1415 printf("%s", ctdb_sock_addr_to_string(
1416 mem_ctx, &ips->ip[i].addr));
1419 if (options.verbose == 0) {
1420 if (options.machinereadable == 1) {
1423 printf(" %d\n", (int)ips->ip[i].pnn);
1432 for (j=0; j<ipinfo[i]->ifaces->num; j++) {
1433 struct ctdb_iface *iface;
1435 iface = &ipinfo[i]->ifaces->iface[j];
1437 conf = talloc_strdup(mem_ctx, iface->name);
1439 conf = talloc_asprintf_append(
1440 mem_ctx, ",%s", iface->name);
1443 if (ipinfo[i]->active_idx == j) {
1444 active = iface->name;
1447 if (iface->link_state == 0) {
1451 if (avail == NULL) {
1452 avail = talloc_strdup(mem_ctx, iface->name);
1454 avail = talloc_asprintf_append(
1455 mem_ctx, ",%s", iface->name);
1459 if (options.machinereadable == 1) {
1460 printf("%s%s%s%s%s%s\n",
1461 active ? active : "", options.sep,
1462 avail ? avail : "", options.sep,
1463 conf ? conf : "", options.sep);
1465 printf(" node[%u] active[%s] available[%s] configured[%s]\n",
1466 ips->ip[i].pnn, active ? active : "",
1467 avail ? avail : "", conf ? conf : "");
1472 static int collect_ips(uint8_t *keybuf, size_t keylen, uint8_t *databuf,
1473 size_t datalen, void *private_data)
1475 struct ctdb_public_ip_list *ips = talloc_get_type_abort(
1476 private_data, struct ctdb_public_ip_list);
1477 struct ctdb_public_ip *ip;
1479 ip = (struct ctdb_public_ip *)databuf;
1480 ips->ip[ips->num] = *ip;
1486 static int get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
1487 struct ctdb_public_ip_list **out)
1489 struct ctdb_node_map *nodemap;
1490 struct ctdb_public_ip_list *ips;
1491 struct db_hash_context *ipdb;
1493 int ret, count, i, j;
1495 nodemap = get_nodemap(ctdb, false);
1496 if (nodemap == NULL) {
1500 ret = db_hash_init(mem_ctx, "ips", 101, DB_HASH_COMPLEX, &ipdb);
1505 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
1511 for (i=0; i<count; i++) {
1512 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1513 pnn_list[i], TIMEOUT(), &ips);
1518 for (j=0; j<ips->num; j++) {
1519 struct ctdb_public_ip ip;
1521 ip.pnn = ips->ip[j].pnn;
1522 ip.addr = ips->ip[j].addr;
1524 ret = db_hash_add(ipdb, (uint8_t *)&ip.addr,
1526 (uint8_t *)&ip, sizeof(ip));
1535 talloc_free(pnn_list);
1537 ret = db_hash_traverse(ipdb, NULL, NULL, &count);
1542 ips = talloc_zero(mem_ctx, struct ctdb_public_ip_list);
1547 ips->ip = talloc_array(ips, struct ctdb_public_ip, count);
1548 if (ips->ip == NULL) {
1552 ret = db_hash_traverse(ipdb, collect_ips, ips, &count);
1557 if (count != ips->num) {
1571 static int control_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1572 int argc, const char **argv)
1574 struct ctdb_public_ip_list *ips;
1575 struct ctdb_public_ip_info **ipinfo;
1577 bool do_all = false;
1584 if (strcmp(argv[0], "all") == 0) {
1592 ret = get_all_public_ips(ctdb, mem_ctx, &ips);
1594 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1595 ctdb->cmd_pnn, TIMEOUT(), &ips);
1601 ipinfo = talloc_array(mem_ctx, struct ctdb_public_ip_info *, ips->num);
1602 if (ipinfo == NULL) {
1606 for (i=0; i<ips->num; i++) {
1609 pnn = ips->ip[i].pnn;
1611 pnn = ctdb->cmd_pnn;
1613 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev,
1615 TIMEOUT(), &ips->ip[i].addr,
1622 print_ip(mem_ctx, ctdb, ips, ipinfo, do_all);
1626 static int control_ipinfo(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1627 int argc, const char **argv)
1629 struct ctdb_public_ip_info *ipinfo;
1630 ctdb_sock_addr addr;
1637 if (! parse_ip(argv[0], NULL, 0, &addr)) {
1638 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
1642 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev, ctdb->client,
1643 ctdb->cmd_pnn, TIMEOUT(), &addr,
1647 printf("Node %u does not know about IP %s\n",
1648 ctdb->cmd_pnn, argv[0]);
1653 printf("Public IP[%s] info on node %u\n",
1654 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1657 printf("IP:%s\nCurrentNode:%u\nNumInterfaces:%u\n",
1658 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
1659 ipinfo->ip.pnn, ipinfo->ifaces->num);
1661 for (i=0; i<ipinfo->ifaces->num; i++) {
1662 struct ctdb_iface *iface;
1664 iface = &ipinfo->ifaces->iface[i];
1665 iface->name[CTDB_IFACE_SIZE] = '\0';
1666 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
1668 iface->link_state == 0 ? "down" : "up",
1670 (i == ipinfo->active_idx) ? " (active)" : "");
1676 static int control_ifaces(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1677 int argc, const char **argv)
1679 struct ctdb_iface_list *ifaces;
1686 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1687 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1692 if (ifaces->num == 0) {
1693 printf("No interfaces configured on node %u\n",
1698 if (options.machinereadable) {
1699 printf("%s%s%s%s%s%s%s\n", options.sep,
1700 "Name", options.sep,
1701 "LinkStatus", options.sep,
1702 "References", options.sep);
1704 printf("Interfaces on node %u\n", ctdb->cmd_pnn);
1707 for (i=0; i<ifaces->num; i++) {
1708 if (options.machinereadable) {
1709 printf("%s%s%s%u%s%u%s\n", options.sep,
1710 ifaces->iface[i].name, options.sep,
1711 ifaces->iface[i].link_state, options.sep,
1712 ifaces->iface[i].references, options.sep);
1714 printf("name:%s link:%s references:%u\n",
1715 ifaces->iface[i].name,
1716 ifaces->iface[i].link_state ? "up" : "down",
1717 ifaces->iface[i].references);
1724 static int control_setifacelink(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1725 int argc, const char **argv)
1727 struct ctdb_iface_list *ifaces;
1728 struct ctdb_iface *iface;
1732 usage("setifacelink");
1735 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
1736 fprintf(stderr, "Interface name '%s' too long\n", argv[0]);
1740 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1741 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1744 "Failed to get interface information from node %u\n",
1750 for (i=0; i<ifaces->num; i++) {
1751 if (strcmp(ifaces->iface[i].name, argv[0]) == 0) {
1752 iface = &ifaces->iface[i];
1757 if (iface == NULL) {
1758 printf("Interface %s not configured on node %u\n",
1759 argv[0], ctdb->cmd_pnn);
1763 if (strcmp(argv[1], "up") == 0) {
1764 iface->link_state = 1;
1765 } else if (strcmp(argv[1], "down") == 0) {
1766 iface->link_state = 0;
1768 usage("setifacelink");
1772 iface->references = 0;
1774 ret = ctdb_ctrl_set_iface_link_state(mem_ctx, ctdb->ev, ctdb->client,
1775 ctdb->cmd_pnn, TIMEOUT(), iface);
1783 static int control_process_exists(TALLOC_CTX *mem_ctx,
1784 struct ctdb_context *ctdb,
1785 int argc, const char **argv)
1791 usage("process-exists");
1794 pid = atoi(argv[0]);
1795 ret = ctdb_ctrl_process_exists(mem_ctx, ctdb->ev, ctdb->client,
1796 ctdb->cmd_pnn, TIMEOUT(), pid, &status);
1802 printf("PID %u exists\n", pid);
1804 printf("PID %u does not exist\n", pid);
1809 static int control_getdbmap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1810 int argc, const char **argv)
1812 struct ctdb_dbid_map *dbmap;
1819 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
1820 ctdb->cmd_pnn, TIMEOUT(), &dbmap);
1825 if (options.machinereadable == 1) {
1826 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1829 "Name", options.sep,
1830 "Path", options.sep,
1831 "Persistent", options.sep,
1832 "Sticky", options.sep,
1833 "Unhealthy", options.sep,
1834 "Readonly", options.sep);
1836 printf("Number of databases:%d\n", dbmap->num);
1839 for (i=0; i<dbmap->num; i++) {
1848 db_id = dbmap->dbs[i].db_id;
1850 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
1851 ctdb->cmd_pnn, TIMEOUT(), db_id,
1857 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1858 ctdb->cmd_pnn, TIMEOUT(), db_id,
1864 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1865 ctdb->cmd_pnn, TIMEOUT(), db_id,
1871 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
1872 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
1873 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
1875 if (options.machinereadable == 1) {
1876 printf("%s0x%08X%s%s%s%s%s%d%s%d%s%d%s%d%s\n",
1881 !! (persistent), options.sep,
1882 !! (sticky), options.sep,
1883 !! (health), options.sep,
1884 !! (readonly), options.sep);
1886 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
1888 persistent ? " PERSISTENT" : "",
1889 sticky ? " STICKY" : "",
1890 readonly ? " READONLY" : "",
1891 health ? " UNHEALTHY" : "");
1894 talloc_free(discard_const(name));
1895 talloc_free(discard_const(path));
1896 talloc_free(discard_const(health));
1902 static int control_getdbstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1903 int argc, const char **argv)
1906 const char *db_name, *db_path, *db_health;
1911 usage("getdbstatus");
1914 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
1918 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1919 ctdb->cmd_pnn, TIMEOUT(), db_id,
1925 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1926 ctdb->cmd_pnn, TIMEOUT(), db_id,
1932 printf("dbid: 0x%08x\nname: %s\npath: %s\n", db_id, db_name, db_path);
1933 printf("PERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
1934 (db_flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
1935 (db_flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
1936 (db_flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
1937 (db_health ? db_health : "OK"));
1941 struct dump_record_state {
1945 #define ISASCII(x) (isprint(x) && ! strchr("\"\\", (x)))
1947 static void dump_tdb_data(const char *name, TDB_DATA val)
1951 fprintf(stdout, "%s(%zu) = \"", name, val.dsize);
1952 for (i=0; i<val.dsize; i++) {
1953 if (ISASCII(val.dptr[i])) {
1954 fprintf(stdout, "%c", val.dptr[i]);
1956 fprintf(stdout, "\\%02X", val.dptr[i]);
1959 fprintf(stdout, "\"\n");
1962 static void dump_ltdb_header(struct ctdb_ltdb_header *header)
1964 fprintf(stdout, "dmaster: %u\n", header->dmaster);
1965 fprintf(stdout, "rsn: %" PRIu64 "\n", header->rsn);
1966 fprintf(stdout, "flags: 0x%08x", header->flags);
1967 if (header->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
1968 fprintf(stdout, " MIGRATED_WITH_DATA");
1970 if (header->flags & CTDB_REC_FLAG_VACUUM_MIGRATED) {
1971 fprintf(stdout, " VACUUM_MIGRATED");
1973 if (header->flags & CTDB_REC_FLAG_AUTOMATIC) {
1974 fprintf(stdout, " AUTOMATIC");
1976 if (header->flags & CTDB_REC_RO_HAVE_DELEGATIONS) {
1977 fprintf(stdout, " RO_HAVE_DELEGATIONS");
1979 if (header->flags & CTDB_REC_RO_HAVE_READONLY) {
1980 fprintf(stdout, " RO_HAVE_READONLY");
1982 if (header->flags & CTDB_REC_RO_REVOKING_READONLY) {
1983 fprintf(stdout, " RO_REVOKING_READONLY");
1985 if (header->flags & CTDB_REC_RO_REVOKE_COMPLETE) {
1986 fprintf(stdout, " RO_REVOKE_COMPLETE");
1988 fprintf(stdout, "\n");
1992 static int dump_record(uint32_t reqid, struct ctdb_ltdb_header *header,
1993 TDB_DATA key, TDB_DATA data, void *private_data)
1995 struct dump_record_state *state =
1996 (struct dump_record_state *)private_data;
2000 dump_tdb_data("key", key);
2001 dump_ltdb_header(header);
2002 dump_tdb_data("data", data);
2003 fprintf(stdout, "\n");
2008 struct traverse_state {
2009 TALLOC_CTX *mem_ctx;
2011 ctdb_rec_parser_func_t func;
2012 struct dump_record_state sub_state;
2015 static void traverse_handler(uint64_t srvid, TDB_DATA data, void *private_data)
2017 struct traverse_state *state = (struct traverse_state *)private_data;
2018 struct ctdb_rec_data *rec;
2019 struct ctdb_ltdb_header header;
2022 ret = ctdb_rec_data_pull(data.dptr, data.dsize, state->mem_ctx, &rec);
2027 if (rec->key.dsize == 0 && rec->data.dsize == 0) {
2029 /* end of traverse */
2034 ret = ctdb_ltdb_header_extract(&rec->data, &header);
2040 if (rec->data.dsize == 0) {
2044 ret = state->func(rec->reqid, &header, rec->key, rec->data,
2052 static int control_catdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2053 int argc, const char **argv)
2055 struct ctdb_db_context *db;
2056 const char *db_name;
2059 struct ctdb_traverse_start_ext traverse;
2060 struct traverse_state state;
2067 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2071 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2074 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2079 ZERO_STRUCT(traverse);
2081 traverse.db_id = db_id;
2083 traverse.srvid = next_srvid(ctdb);
2084 traverse.withemptyrecords = false;
2086 state.mem_ctx = mem_ctx;
2088 state.func = dump_record;
2089 state.sub_state.count = 0;
2091 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2093 traverse_handler, &state);
2098 ret = ctdb_ctrl_traverse_start_ext(mem_ctx, ctdb->ev, ctdb->client,
2099 ctdb->cmd_pnn, TIMEOUT(),
2105 ctdb_client_wait(ctdb->ev, &state.done);
2107 printf("Dumped %u records\n", state.sub_state.count);
2109 ret = ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2110 traverse.srvid, &state);
2118 static int control_cattdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2119 int argc, const char **argv)
2121 struct ctdb_db_context *db;
2122 const char *db_name;
2125 struct dump_record_state state;
2132 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2136 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2139 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2144 ret = ctdb_db_traverse(db, true, true, dump_record, &state);
2146 printf("Dumped %u record(s)\n", state.count);
2151 static int control_getmonmode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2152 int argc, const char **argv)
2157 usage("getmonmode");
2160 ret = ctdb_ctrl_get_monmode(mem_ctx, ctdb->ev, ctdb->client,
2161 ctdb->cmd_pnn, TIMEOUT(), &mode);
2167 (mode == CTDB_MONITORING_ENABLED) ? "ENABLED" : "DISABLED");
2171 static int control_getcapabilities(TALLOC_CTX *mem_ctx,
2172 struct ctdb_context *ctdb,
2173 int argc, const char **argv)
2179 usage("getcapabilities");
2182 ret = ctdb_ctrl_get_capabilities(mem_ctx, ctdb->ev, ctdb->client,
2183 ctdb->cmd_pnn, TIMEOUT(), &caps);
2188 if (options.machinereadable == 1) {
2189 printf("%s%s%s%s%s\n",
2191 "RECMASTER", options.sep,
2192 "LMASTER", options.sep);
2193 printf("%s%d%s%d%s\n", options.sep,
2194 !! (caps & CTDB_CAP_RECMASTER), options.sep,
2195 !! (caps & CTDB_CAP_LMASTER), options.sep);
2197 printf("RECMASTER: %s\n",
2198 (caps & CTDB_CAP_RECMASTER) ? "YES" : "NO");
2199 printf("LMASTER: %s\n",
2200 (caps & CTDB_CAP_LMASTER) ? "YES" : "NO");
2206 static int control_pnn(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2207 int argc, const char **argv)
2209 printf("%u\n", ctdb_client_pnn(ctdb->client));
2213 static int control_lvs(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2214 int argc, const char **argv)
2216 char *t, *lvs_helper = NULL;
2222 t = getenv("CTDB_LVS_HELPER");
2224 lvs_helper = talloc_strdup(mem_ctx, t);
2226 lvs_helper = talloc_asprintf(mem_ctx, "%s/ctdb_lvs",
2227 CTDB_HELPER_BINDIR);
2230 if (lvs_helper == NULL) {
2231 fprintf(stderr, "Unable to set LVS helper\n");
2235 return run_helper("LVS helper", lvs_helper, argv[0]);
2238 static int control_disable_monitor(TALLOC_CTX *mem_ctx,
2239 struct ctdb_context *ctdb,
2240 int argc, const char **argv)
2245 usage("disablemonitor");
2248 ret = ctdb_ctrl_disable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2249 ctdb->cmd_pnn, TIMEOUT());
2257 static int control_enable_monitor(TALLOC_CTX *mem_ctx,
2258 struct ctdb_context *ctdb,
2259 int argc, const char **argv)
2264 usage("enablemonitor");
2267 ret = ctdb_ctrl_enable_monitor(mem_ctx, ctdb->ev, ctdb->client,
2268 ctdb->cmd_pnn, TIMEOUT());
2276 static int control_setdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2277 int argc, const char **argv)
2279 enum debug_level log_level;
2287 found = debug_level_parse(argv[0], &log_level);
2290 "Invalid debug level '%s'. Valid levels are:\n",
2292 fprintf(stderr, "\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
2296 ret = ctdb_ctrl_setdebug(mem_ctx, ctdb->ev, ctdb->client,
2297 ctdb->cmd_pnn, TIMEOUT(), log_level);
2305 static int control_getdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2306 int argc, const char **argv)
2308 enum debug_level loglevel;
2309 const char *log_str;
2316 ret = ctdb_ctrl_getdebug(mem_ctx, ctdb->ev, ctdb->client,
2317 ctdb->cmd_pnn, TIMEOUT(), &loglevel);
2322 log_str = debug_level_to_string(loglevel);
2323 printf("%s\n", log_str);
2328 static int control_attach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2329 int argc, const char **argv)
2331 const char *db_name;
2332 uint8_t db_flags = 0;
2335 if (argc < 1 || argc > 2) {
2341 if (strcmp(argv[1], "persistent") == 0) {
2342 db_flags = CTDB_DB_FLAGS_PERSISTENT;
2343 } else if (strcmp(argv[1], "readonly") == 0) {
2344 db_flags = CTDB_DB_FLAGS_READONLY;
2345 } else if (strcmp(argv[1], "sticky") == 0) {
2346 db_flags = CTDB_DB_FLAGS_STICKY;
2352 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2361 static int control_detach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2362 int argc, const char **argv)
2364 const char *db_name;
2367 struct ctdb_node_map *nodemap;
2375 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2376 ctdb->cmd_pnn, TIMEOUT(), &recmode);
2381 if (recmode == CTDB_RECOVERY_ACTIVE) {
2382 fprintf(stderr, "Database cannot be detached"
2383 " when recovery is active\n");
2387 nodemap = get_nodemap(ctdb, false);
2388 if (nodemap == NULL) {
2392 for (i=0; i<nodemap->num; i++) {
2395 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
2398 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
2401 if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
2402 fprintf(stderr, "Database cannot be detached on"
2403 " inactive (stopped or banned) node %u\n",
2404 nodemap->node[i].pnn);
2408 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
2409 nodemap->node[i].pnn, TIMEOUT(),
2410 "AllowClientDBAttach", &value);
2413 "Unable to get tunable AllowClientDBAttach"
2414 " from node %u\n", nodemap->node[i].pnn);
2420 "Database access is still active on node %u."
2421 " Set AllowclientDBAttach=0 on all nodes.\n",
2422 nodemap->node[i].pnn);
2428 for (i=0; i<argc; i++) {
2429 if (! db_exists(mem_ctx, ctdb, argv[i], &db_id, &db_name,
2434 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
2436 "Persistent database %s cannot be detached\n",
2441 ret = ctdb_detach(mem_ctx, ctdb->ev, ctdb->client,
2444 fprintf(stderr, "Database %s detach failed\n", db_name);
2452 static int control_dumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2453 int argc, const char **argv)
2455 const char *mem_str;
2459 ret = ctdb_ctrl_dump_memory(mem_ctx, ctdb->ev, ctdb->client,
2460 ctdb->cmd_pnn, TIMEOUT(), &mem_str);
2465 n = write(1, mem_str, strlen(mem_str)+1);
2466 if (n < 0 || n != strlen(mem_str)+1) {
2467 fprintf(stderr, "Failed to write talloc summary\n");
2474 static void dump_memory(uint64_t srvid, TDB_DATA data, void *private_data)
2476 bool *done = (bool *)private_data;
2479 n = write(1, data.dptr, data.dsize);
2480 if (n < 0 || n != data.dsize) {
2481 fprintf(stderr, "Failed to write talloc summary\n");
2487 static int control_rddumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2488 int argc, const char **argv)
2490 struct ctdb_srvid_message msg = { 0 };
2494 msg.pnn = ctdb->pnn;
2495 msg.srvid = next_srvid(ctdb);
2497 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2498 msg.srvid, dump_memory, &done);
2503 ret = ctdb_message_mem_dump(mem_ctx, ctdb->ev, ctdb->client,
2504 ctdb->cmd_pnn, &msg);
2509 ctdb_client_wait(ctdb->ev, &done);
2513 static int control_getpid(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2514 int argc, const char **argv)
2519 ret = ctdb_ctrl_get_pid(mem_ctx, ctdb->ev, ctdb->client,
2520 ctdb->cmd_pnn, TIMEOUT(), &pid);
2525 printf("%u\n", pid);
2529 static int check_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2530 const char *desc, uint32_t flag, bool set_flag)
2532 struct ctdb_node_map *nodemap;
2535 nodemap = get_nodemap(ctdb, false);
2536 if (nodemap == NULL) {
2540 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2541 if (set_flag == flag_is_set) {
2543 fprintf(stderr, "Node %u is already %s\n",
2544 ctdb->cmd_pnn, desc);
2546 fprintf(stderr, "Node %u is not %s\n",
2547 ctdb->cmd_pnn, desc);
2555 static void wait_for_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2556 uint32_t flag, bool set_flag)
2558 struct ctdb_node_map *nodemap;
2562 nodemap = get_nodemap(ctdb, true);
2563 if (nodemap == NULL) {
2565 "Failed to get nodemap, trying again\n");
2570 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2571 if (flag_is_set == set_flag) {
2579 struct ipreallocate_state {
2584 static void ipreallocate_handler(uint64_t srvid, TDB_DATA data,
2587 struct ipreallocate_state *state =
2588 (struct ipreallocate_state *)private_data;
2590 if (data.dsize != sizeof(int)) {
2595 state->status = *(int *)data.dptr;
2599 static int ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb)
2601 struct ctdb_srvid_message msg = { 0 };
2602 struct ipreallocate_state state;
2605 msg.pnn = ctdb->pnn;
2606 msg.srvid = next_srvid(ctdb);
2609 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2611 ipreallocate_handler, &state);
2617 ret = ctdb_message_takeover_run(mem_ctx, ctdb->ev,
2619 CTDB_BROADCAST_CONNECTED,
2625 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done,
2631 if (state.status >= 0) {
2640 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2645 static int control_disable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2646 int argc, const char **argv)
2654 ret = check_flags(mem_ctx, ctdb, "disabled",
2655 NODE_FLAGS_PERMANENTLY_DISABLED, true);
2660 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2661 ctdb->cmd_pnn, TIMEOUT(),
2662 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2665 "Failed to set DISABLED flag on node %u\n",
2670 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, true);
2671 return ipreallocate(mem_ctx, ctdb);
2674 static int control_enable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2675 int argc, const char **argv)
2683 ret = check_flags(mem_ctx, ctdb, "disabled",
2684 NODE_FLAGS_PERMANENTLY_DISABLED, false);
2689 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2690 ctdb->cmd_pnn, TIMEOUT(),
2691 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2693 fprintf(stderr, "Failed to reset DISABLED flag on node %u\n",
2698 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, false);
2699 return ipreallocate(mem_ctx, ctdb);
2702 static int control_stop(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2703 int argc, const char **argv)
2711 ret = check_flags(mem_ctx, ctdb, "stopped",
2712 NODE_FLAGS_STOPPED, true);
2717 ret = ctdb_ctrl_stop_node(mem_ctx, ctdb->ev, ctdb->client,
2718 ctdb->cmd_pnn, TIMEOUT());
2720 fprintf(stderr, "Failed to stop node %u\n", ctdb->cmd_pnn);
2724 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, true);
2725 return ipreallocate(mem_ctx, ctdb);
2728 static int control_continue(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2729 int argc, const char **argv)
2737 ret = check_flags(mem_ctx, ctdb, "stopped",
2738 NODE_FLAGS_STOPPED, false);
2743 ret = ctdb_ctrl_continue_node(mem_ctx, ctdb->ev, ctdb->client,
2744 ctdb->cmd_pnn, TIMEOUT());
2746 fprintf(stderr, "Failed to continue stopped node %u\n",
2751 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, false);
2752 return ipreallocate(mem_ctx, ctdb);
2755 static int control_ban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2756 int argc, const char **argv)
2758 struct ctdb_ban_state ban_state;
2765 ret = check_flags(mem_ctx, ctdb, "banned",
2766 NODE_FLAGS_BANNED, true);
2771 ban_state.pnn = ctdb->cmd_pnn;
2772 ban_state.time = strtoul(argv[0], NULL, 0);
2774 if (ban_state.time == 0) {
2775 fprintf(stderr, "Ban time cannot be zero\n");
2779 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2780 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2782 fprintf(stderr, "Failed to ban node %u\n", ctdb->cmd_pnn);
2786 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, true);
2787 return ipreallocate(mem_ctx, ctdb);
2791 static int control_unban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2792 int argc, const char **argv)
2794 struct ctdb_ban_state ban_state;
2801 ret = check_flags(mem_ctx, ctdb, "banned",
2802 NODE_FLAGS_BANNED, false);
2807 ban_state.pnn = ctdb->cmd_pnn;
2810 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2811 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2813 fprintf(stderr, "Failed to unban node %u\n", ctdb->cmd_pnn);
2817 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, false);
2818 return ipreallocate(mem_ctx, ctdb);
2822 static int control_shutdown(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2823 int argc, const char **argv)
2831 ret = ctdb_ctrl_shutdown(mem_ctx, ctdb->ev, ctdb->client,
2832 ctdb->cmd_pnn, TIMEOUT());
2834 fprintf(stderr, "Unable to shutdown node %u\n", ctdb->cmd_pnn);
2841 static int get_generation(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2842 uint32_t *generation)
2846 struct ctdb_vnn_map *vnnmap;
2850 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2851 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
2853 fprintf(stderr, "Failed to find recovery master\n");
2857 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2858 recmaster, TIMEOUT(), &recmode);
2860 fprintf(stderr, "Failed to get recovery mode from node %u\n",
2865 if (recmode == CTDB_RECOVERY_ACTIVE) {
2870 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
2871 recmaster, TIMEOUT(), &vnnmap);
2873 fprintf(stderr, "Failed to get generation from node %u\n",
2878 if (vnnmap->generation == 1) {
2879 talloc_free(vnnmap);
2884 *generation = vnnmap->generation;
2885 talloc_free(vnnmap);
2890 static int control_recover(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2891 int argc, const char **argv)
2893 uint32_t generation, next_generation;
2900 ret = get_generation(mem_ctx, ctdb, &generation);
2905 ret = ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
2906 ctdb->cmd_pnn, TIMEOUT(),
2907 CTDB_RECOVERY_ACTIVE);
2909 fprintf(stderr, "Failed to set recovery mode active\n");
2914 ret = get_generation(mem_ctx, ctdb, &next_generation);
2917 "Failed to confirm end of recovery\n");
2921 if (next_generation != generation) {
2931 static int control_ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2932 int argc, const char **argv)
2935 usage("ipreallocate");
2938 return ipreallocate(mem_ctx, ctdb);
2941 static int control_isnotrecmaster(TALLOC_CTX *mem_ctx,
2942 struct ctdb_context *ctdb,
2943 int argc, const char **argv)
2949 usage("isnotrecmaster");
2952 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2953 ctdb->pnn, TIMEOUT(), &recmaster);
2955 fprintf(stderr, "Failed to get recmaster\n");
2959 if (recmaster != ctdb->pnn) {
2960 printf("this node is not the recmaster\n");
2964 printf("this node is the recmaster\n");
2968 static int control_gratarp(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2969 int argc, const char **argv)
2971 struct ctdb_addr_info addr_info;
2978 if (! parse_ip(argv[0], NULL, 0, &addr_info.addr)) {
2979 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
2982 addr_info.iface = argv[1];
2984 ret = ctdb_ctrl_send_gratuitous_arp(mem_ctx, ctdb->ev, ctdb->client,
2985 ctdb->cmd_pnn, TIMEOUT(),
2988 fprintf(stderr, "Unable to send gratuitous arp from node %u\n",
2996 static int control_tickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2997 int argc, const char **argv)
2999 ctdb_sock_addr src, dst;
3002 if (argc != 0 && argc != 2) {
3007 struct ctdb_connection *clist;
3011 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3017 for (i=0; i<count; i++) {
3018 ret = ctdb_sys_send_tcp(&clist[i].src,
3026 if (num_failed > 0) {
3027 fprintf(stderr, "Failed to send %d tickles\n",
3036 if (! parse_ip_port(argv[0], &src)) {
3037 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3041 if (! parse_ip_port(argv[1], &dst)) {
3042 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3046 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
3048 fprintf(stderr, "Failed to send tickle ack\n");
3055 static int control_gettickles(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3056 int argc, const char **argv)
3058 ctdb_sock_addr addr;
3059 struct ctdb_tickle_list *tickles;
3063 if (argc < 1 || argc > 2) {
3064 usage("gettickles");
3068 port = strtoul(argv[1], NULL, 10);
3071 if (! parse_ip(argv[0], NULL, port, &addr)) {
3072 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3076 ret = ctdb_ctrl_get_tcp_tickle_list(mem_ctx, ctdb->ev, ctdb->client,
3077 ctdb->cmd_pnn, TIMEOUT(), &addr,
3080 fprintf(stderr, "Failed to get list of connections\n");
3084 if (options.machinereadable) {
3085 printf("%s%s%s%s%s%s%s%s%s\n",
3087 "Source IP", options.sep,
3088 "Port", options.sep,
3089 "Destiation IP", options.sep,
3090 "Port", options.sep);
3091 for (i=0; i<tickles->num; i++) {
3092 printf("%s%s%s%u%s%s%s%u%s\n", options.sep,
3093 ctdb_sock_addr_to_string(
3094 mem_ctx, &tickles->conn[i].src),
3096 ntohs(tickles->conn[i].src.ip.sin_port),
3098 ctdb_sock_addr_to_string(
3099 mem_ctx, &tickles->conn[i].dst),
3101 ntohs(tickles->conn[i].dst.ip.sin_port),
3105 printf("Connections for IP: %s\n",
3106 ctdb_sock_addr_to_string(mem_ctx, &tickles->addr));
3107 printf("Num connections: %u\n", tickles->num);
3108 for (i=0; i<tickles->num; i++) {
3109 printf("SRC: %s:%u DST: %s:%u\n",
3110 ctdb_sock_addr_to_string(
3111 mem_ctx, &tickles->conn[i].src),
3112 ntohs(tickles->conn[i].src.ip.sin_port),
3113 ctdb_sock_addr_to_string(
3114 mem_ctx, &tickles->conn[i].dst),
3115 ntohs(tickles->conn[i].dst.ip.sin_port));
3122 typedef void (*clist_request_func)(struct ctdb_req_control *request,
3123 struct ctdb_connection *conn);
3125 typedef int (*clist_reply_func)(struct ctdb_reply_control *reply);
3127 struct process_clist_state {
3128 struct ctdb_connection *clist;
3130 int num_failed, num_total;
3131 clist_reply_func reply_func;
3134 static void process_clist_done(struct tevent_req *subreq);
3136 static struct tevent_req *process_clist_send(
3137 TALLOC_CTX *mem_ctx,
3138 struct ctdb_context *ctdb,
3139 struct ctdb_connection *clist,
3141 clist_request_func request_func,
3142 clist_reply_func reply_func)
3144 struct tevent_req *req, *subreq;
3145 struct process_clist_state *state;
3146 struct ctdb_req_control request;
3149 req = tevent_req_create(mem_ctx, &state, struct process_clist_state);
3154 state->clist = clist;
3155 state->count = count;
3156 state->reply_func = reply_func;
3158 for (i=0; i<count; i++) {
3159 request_func(&request, &clist[i]);
3160 subreq = ctdb_client_control_send(state, ctdb->ev,
3161 ctdb->client, ctdb->cmd_pnn,
3162 TIMEOUT(), &request);
3163 if (tevent_req_nomem(subreq, req)) {
3164 return tevent_req_post(req, ctdb->ev);
3166 tevent_req_set_callback(subreq, process_clist_done, req);
3172 static void process_clist_done(struct tevent_req *subreq)
3174 struct tevent_req *req = tevent_req_callback_data(
3175 subreq, struct tevent_req);
3176 struct process_clist_state *state = tevent_req_data(
3177 req, struct process_clist_state);
3178 struct ctdb_reply_control *reply;
3182 status = ctdb_client_control_recv(subreq, NULL, state, &reply);
3183 TALLOC_FREE(subreq);
3185 state->num_failed += 1;
3189 ret = state->reply_func(reply);
3191 state->num_failed += 1;
3196 state->num_total += 1;
3197 if (state->num_total == state->count) {
3198 tevent_req_done(req);
3202 static int process_clist_recv(struct tevent_req *req)
3204 struct process_clist_state *state = tevent_req_data(
3205 req, struct process_clist_state);
3207 return state->num_failed;
3210 static int control_addtickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3211 int argc, const char **argv)
3213 struct ctdb_connection conn;
3216 if (argc != 0 && argc != 2) {
3221 struct ctdb_connection *clist;
3222 struct tevent_req *req;
3225 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3230 req = process_clist_send(mem_ctx, ctdb, clist, count,
3231 ctdb_req_control_tcp_add_delayed_update,
3232 ctdb_reply_control_tcp_add_delayed_update);
3237 tevent_req_poll(req, ctdb->ev);
3239 ret = process_clist_recv(req);
3241 fprintf(stderr, "Failed to add %d tickles\n", ret);
3248 if (! parse_ip_port(argv[0], &conn.src)) {
3249 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3252 if (! parse_ip_port(argv[1], &conn.dst)) {
3253 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3257 ret = ctdb_ctrl_tcp_add_delayed_update(mem_ctx, ctdb->ev,
3258 ctdb->client, ctdb->cmd_pnn,
3261 fprintf(stderr, "Failed to register connection\n");
3268 static int control_deltickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3269 int argc, const char **argv)
3271 struct ctdb_connection conn;
3274 if (argc != 0 && argc != 2) {
3279 struct ctdb_connection *clist;
3280 struct tevent_req *req;
3283 ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
3288 req = process_clist_send(mem_ctx, ctdb, clist, count,
3289 ctdb_req_control_tcp_remove,
3290 ctdb_reply_control_tcp_remove);
3295 tevent_req_poll(req, ctdb->ev);
3297 ret = process_clist_recv(req);
3299 fprintf(stderr, "Failed to remove %d tickles\n", ret);
3306 if (! parse_ip_port(argv[0], &conn.src)) {
3307 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3310 if (! parse_ip_port(argv[1], &conn.dst)) {
3311 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3315 ret = ctdb_ctrl_tcp_remove(mem_ctx, ctdb->ev, ctdb->client,
3316 ctdb->cmd_pnn, TIMEOUT(), &conn);
3318 fprintf(stderr, "Failed to unregister connection\n");
3325 static int control_check_srvids(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3326 int argc, const char **argv)
3333 usage("check_srvids");
3336 srvid = talloc_array(mem_ctx, uint64_t, argc);
3337 if (srvid == NULL) {
3338 fprintf(stderr, "Memory allocation error\n");
3342 for (i=0; i<argc; i++) {
3343 srvid[i] = strtoull(argv[i], NULL, 0);
3346 ret = ctdb_ctrl_check_srvids(mem_ctx, ctdb->ev, ctdb->client,
3347 ctdb->cmd_pnn, TIMEOUT(), srvid, argc,
3350 fprintf(stderr, "Failed to check srvids on node %u\n",
3355 for (i=0; i<argc; i++) {
3356 printf("SRVID 0x%" PRIx64 " %s\n", srvid[i],
3357 (result[i] ? "exists" : "does not exist"));
3363 static int control_listnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3364 int argc, const char **argv)
3366 struct ctdb_node_map *nodemap;
3373 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
3374 if (nodemap == NULL) {
3378 for (i=0; i<nodemap->num; i++) {
3379 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
3383 if (options.machinereadable) {
3384 printf("%s%u%s%s%s\n", options.sep,
3385 nodemap->node[i].pnn, options.sep,
3386 ctdb_sock_addr_to_string(
3387 mem_ctx, &nodemap->node[i].addr),
3391 ctdb_sock_addr_to_string(
3392 mem_ctx, &nodemap->node[i].addr));
3399 static bool nodemap_identical(struct ctdb_node_map *nodemap1,
3400 struct ctdb_node_map *nodemap2)
3404 if (nodemap1->num != nodemap2->num) {
3408 for (i=0; i<nodemap1->num; i++) {
3409 struct ctdb_node_and_flags *n1, *n2;
3411 n1 = &nodemap1->node[i];
3412 n2 = &nodemap2->node[i];
3414 if ((n1->pnn != n2->pnn) ||
3415 (n1->flags != n2->flags) ||
3416 ! ctdb_sock_addr_same_ip(&n1->addr, &n2->addr)) {
3424 static int check_node_file_changes(TALLOC_CTX *mem_ctx,
3425 struct ctdb_node_map *nm,
3426 struct ctdb_node_map *fnm,
3430 bool check_failed = false;
3434 for (i=0; i<nm->num; i++) {
3435 if (i >= fnm->num) {
3437 "Node %u (%s) missing from nodes file\n",
3439 ctdb_sock_addr_to_string(
3440 mem_ctx, &nm->node[i].addr));
3441 check_failed = true;
3444 if (nm->node[i].flags & NODE_FLAGS_DELETED &&
3445 fnm->node[i].flags & NODE_FLAGS_DELETED) {
3446 /* Node remains deleted */
3450 if (! (nm->node[i].flags & NODE_FLAGS_DELETED) &&
3451 ! (fnm->node[i].flags & NODE_FLAGS_DELETED)) {
3452 /* Node not newly nor previously deleted */
3453 if (! ctdb_same_ip(&nm->node[i].addr,
3454 &fnm->node[i].addr)) {
3456 "Node %u has changed IP address"
3457 " (was %s, now %s)\n",
3459 ctdb_sock_addr_to_string(
3460 mem_ctx, &nm->node[i].addr),
3461 ctdb_sock_addr_to_string(
3462 mem_ctx, &fnm->node[i].addr));
3463 check_failed = true;
3465 if (nm->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3467 "WARNING: Node %u is disconnected."
3468 " You MUST fix this node manually!\n",
3475 if (fnm->node[i].flags & NODE_FLAGS_DELETED) {
3476 /* Node is being deleted */
3477 printf("Node %u is DELETED\n", nm->node[i].pnn);
3479 if (! (nm->node[i].flags & NODE_FLAGS_DISCONNECTED)) {
3481 "ERROR: Node %u is still connected\n",
3483 check_failed = true;
3488 if (nm->node[i].flags & NODE_FLAGS_DELETED) {
3489 /* Node was previously deleted */
3490 printf("Node %u is UNDELETED\n", nm->node[i].pnn);
3497 "ERROR: Nodes will not be reloaded due to previous error\n");
3501 /* Leftover nodes in file are NEW */
3502 for (; i < fnm->num; i++) {
3503 printf("Node %u is NEW\n", fnm->node[i].pnn);
3510 struct disable_recoveries_state {
3518 static void disable_recoveries_handler(uint64_t srvid, TDB_DATA data,
3521 struct disable_recoveries_state *state =
3522 (struct disable_recoveries_state *)private_data;
3525 if (data.dsize != sizeof(int)) {
3530 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
3531 ret = *(int *)data.dptr;
3533 state->status = ret;
3537 for (i=0; i<state->node_count; i++) {
3538 if (state->pnn_list[i] == ret) {
3539 state->reply[i] = true;
3545 for (i=0; i<state->node_count; i++) {
3546 if (! state->reply[i]) {
3547 state->done = false;
3553 static int disable_recoveries(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3554 uint32_t timeout, uint32_t *pnn_list, int count)
3556 struct ctdb_disable_message disable = { 0 };
3557 struct disable_recoveries_state state;
3560 disable.pnn = ctdb->pnn;
3561 disable.srvid = next_srvid(ctdb);
3562 disable.timeout = timeout;
3564 state.pnn_list = pnn_list;
3565 state.node_count = count;
3568 state.reply = talloc_zero_array(mem_ctx, bool, count);
3569 if (state.reply == NULL) {
3573 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
3575 disable_recoveries_handler,
3581 for (i=0; i<count; i++) {
3582 ret = ctdb_message_disable_recoveries(mem_ctx, ctdb->ev,
3591 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
3593 fprintf(stderr, "Timed out waiting to disable recoveries\n");
3595 ret = (state.status >= 0 ? 0 : 1);
3599 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
3600 disable.srvid, &state);
3604 static int control_reloadnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3605 int argc, const char **argv)
3607 struct ctdb_node_map *nodemap = NULL;
3608 struct ctdb_node_map *file_nodemap;
3609 struct ctdb_node_map *remote_nodemap;
3610 struct ctdb_req_control request;
3611 struct ctdb_reply_control **reply;
3617 nodemap = get_nodemap(ctdb, false);
3618 if (nodemap == NULL) {
3622 file_nodemap = read_nodes_file(mem_ctx, ctdb->pnn);
3623 if (file_nodemap == NULL) {
3627 for (i=0; i<nodemap->num; i++) {
3628 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3632 ret = ctdb_ctrl_get_nodes_file(mem_ctx, ctdb->ev, ctdb->client,
3633 nodemap->node[i].pnn, TIMEOUT(),
3637 "ERROR: Failed to get nodes file from node %u\n",
3638 nodemap->node[i].pnn);
3642 if (! nodemap_identical(file_nodemap, remote_nodemap)) {
3644 "ERROR: Nodes file on node %u differs"
3645 " from current node (%u)\n",
3646 nodemap->node[i].pnn, ctdb->pnn);
3651 ret = check_node_file_changes(mem_ctx, nodemap, file_nodemap, &reload);
3657 fprintf(stderr, "No change in nodes file,"
3658 " skipping unnecessary reload\n");
3662 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
3663 mem_ctx, &pnn_list);
3665 fprintf(stderr, "Memory allocation error\n");
3669 ret = disable_recoveries(mem_ctx, ctdb, 2*options.timelimit,
3672 fprintf(stderr, "Failed to disable recoveries\n");
3676 ctdb_req_control_reload_nodes_file(&request);
3677 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3678 pnn_list, count, TIMEOUT(),
3679 &request, NULL, &reply);
3681 bool failed = false;
3683 for (i=0; i<count; i++) {
3684 ret = ctdb_reply_control_reload_nodes_file(reply[i]);
3687 "Node %u failed to reload nodes\n",
3694 "You MUST fix failed nodes manually!\n");
3698 ret = disable_recoveries(mem_ctx, ctdb, 0, pnn_list, count);
3700 fprintf(stderr, "Failed to enable recoveries\n");
3707 static int moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3708 ctdb_sock_addr *addr, uint32_t pnn)
3710 struct ctdb_public_ip_list *pubip_list;
3711 struct ctdb_public_ip pubip;
3712 struct ctdb_node_map *nodemap;
3713 struct ctdb_req_control request;
3717 ret = ctdb_message_disable_ip_check(mem_ctx, ctdb->ev, ctdb->client,
3718 CTDB_BROADCAST_CONNECTED,
3719 2*options.timelimit);
3721 fprintf(stderr, "Failed to disable IP check\n");
3725 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3726 pnn, TIMEOUT(), &pubip_list);
3728 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3733 for (i=0; i<pubip_list->num; i++) {
3734 if (ctdb_same_ip(addr, &pubip_list->ip[i].addr)) {
3739 if (i == pubip_list->num) {
3740 fprintf(stderr, "Node %u CANNOT host IP address %s\n",
3741 pnn, ctdb_sock_addr_to_string(mem_ctx, addr));
3745 nodemap = get_nodemap(ctdb, false);
3746 if (nodemap == NULL) {
3750 count = list_of_active_nodes(nodemap, pnn, mem_ctx, &pnn_list);
3752 fprintf(stderr, "Memory allocation error\n");
3758 ctdb_req_control_release_ip(&request, &pubip);
3760 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3761 pnn_list, count, TIMEOUT(),
3762 &request, NULL, NULL);
3764 fprintf(stderr, "Failed to release IP on nodes\n");
3768 ret = ctdb_ctrl_takeover_ip(mem_ctx, ctdb->ev, ctdb->client,
3769 pnn, TIMEOUT(), &pubip);
3771 fprintf(stderr, "Failed to takeover IP on node %u\n", pnn);
3778 static int control_moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3779 int argc, const char **argv)
3781 ctdb_sock_addr addr;
3783 int ret, retries = 0;
3789 if (! parse_ip(argv[0], NULL, 0, &addr)) {
3790 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3794 pnn = strtoul(argv[1], NULL, 10);
3795 if (pnn == CTDB_UNKNOWN_PNN) {
3796 fprintf(stderr, "Invalid PNN %s\n", argv[1]);
3800 while (retries < 5) {
3801 ret = moveip(mem_ctx, ctdb, &addr, pnn);
3811 fprintf(stderr, "Failed to move IP %s to node %u\n",
3819 static int control_rebalanceip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3820 int argc, const char **argv)
3822 ctdb_sock_addr addr;
3823 struct ctdb_node_map *nodemap;
3824 struct ctdb_public_ip pubip;
3825 struct ctdb_req_control request;
3830 usage("rebalanceip");
3833 if (parse_ip(argv[0], NULL, 0, &addr)) {
3834 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3838 ret = ctdb_message_disable_ip_check(mem_ctx, ctdb->ev, ctdb->client,
3839 CTDB_BROADCAST_CONNECTED,
3840 2*options.timelimit);
3842 fprintf(stderr, "Failed to disable IP check\n");
3846 nodemap = get_nodemap(ctdb, false);
3847 if (nodemap == NULL) {
3851 count = list_of_active_nodes(nodemap, -1, mem_ctx, &pnn_list);
3853 fprintf(stderr, "Memory allocation error\n");
3857 pubip.pnn = CTDB_UNKNOWN_PNN;
3860 ctdb_req_control_release_ip(&request, &pubip);
3861 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3862 pnn_list, count, TIMEOUT(),
3863 &request, NULL, NULL);
3865 fprintf(stderr, "Failed to release IP from nodes\n");
3869 return ipreallocate(mem_ctx, ctdb);
3872 static int rebalancenode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3877 ret = ctdb_message_rebalance_node(mem_ctx, ctdb->ev, ctdb->client,
3878 CTDB_BROADCAST_CONNECTED, pnn);
3881 "Failed to ask recovery master to distribute IPs\n");
3888 static int control_addip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3889 int argc, const char **argv)
3891 ctdb_sock_addr addr;
3892 struct ctdb_public_ip_list *pubip_list;
3893 struct ctdb_addr_info addr_info;
3895 int ret, i, retries = 0;
3901 if (! parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
3902 fprintf(stderr, "Invalid IP/Mask %s\n", argv[0]);
3906 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3907 ctdb->cmd_pnn, TIMEOUT(), &pubip_list);
3909 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3914 for (i=0; i<pubip_list->num; i++) {
3915 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3916 fprintf(stderr, "Node already knows about IP %s\n",
3917 ctdb_sock_addr_to_string(mem_ctx, &addr));
3922 addr_info.addr = addr;
3923 addr_info.mask = mask;
3924 addr_info.iface = argv[1];
3926 while (retries < 5) {
3927 ret = ctdb_ctrl_add_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3928 ctdb->cmd_pnn, TIMEOUT(),
3939 fprintf(stderr, "Failed to add public IP to node %u."
3940 " Giving up\n", ctdb->cmd_pnn);
3944 ret = rebalancenode(mem_ctx, ctdb, ctdb->cmd_pnn);
3949 return ipreallocate(mem_ctx, ctdb);
3952 static int control_delip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3953 int argc, const char **argv)
3955 ctdb_sock_addr addr;
3956 struct ctdb_public_ip_list *pubip_list;
3957 struct ctdb_addr_info addr_info;
3964 if (! parse_ip(argv[0], NULL, 0, &addr)) {
3965 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3969 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3970 ctdb->cmd_pnn, TIMEOUT(), &pubip_list);
3972 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3977 for (i=0; i<pubip_list->num; i++) {
3978 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3983 if (i == pubip_list->num) {
3984 fprintf(stderr, "Node does not know about IP address %s\n",
3985 ctdb_sock_addr_to_string(mem_ctx, &addr));
3989 addr_info.addr = addr;
3991 addr_info.iface = NULL;
3993 ret = ctdb_ctrl_del_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3994 ctdb->cmd_pnn, TIMEOUT(), &addr_info);
3996 fprintf(stderr, "Failed to delete public IP from node %u\n",
4004 static int control_eventscript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4005 int argc, const char **argv)
4010 usage("eventscript");
4013 if (strcmp(argv[0], "monitor") != 0) {
4014 fprintf(stderr, "Only monitor event can be run\n");
4018 ret = ctdb_ctrl_run_eventscripts(mem_ctx, ctdb->ev, ctdb->client,
4019 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4021 fprintf(stderr, "Failed to run monitor event on node %u\n",
4029 #define DB_VERSION 3
4030 #define MAX_DB_NAME 64
4031 #define MAX_REC_BUFFER_SIZE (100*1000)
4034 unsigned long version;
4036 unsigned long flags;
4039 char name[MAX_DB_NAME];
4042 struct backup_state {
4043 TALLOC_CTX *mem_ctx;
4044 struct ctdb_rec_buffer *recbuf;
4047 unsigned int nbuf, nrec;
4050 static int backup_handler(uint32_t reqid, struct ctdb_ltdb_header *header,
4051 TDB_DATA key, TDB_DATA data, void *private_data)
4053 struct backup_state *state = (struct backup_state *)private_data;
4057 if (state->recbuf == NULL) {
4058 state->recbuf = ctdb_rec_buffer_init(state->mem_ctx,
4060 if (state->recbuf == NULL) {
4065 ret = ctdb_rec_buffer_add(state->recbuf, state->recbuf, reqid,
4071 len = ctdb_rec_buffer_len(state->recbuf);
4072 if (len < MAX_REC_BUFFER_SIZE) {
4076 ret = ctdb_rec_buffer_write(state->recbuf, state->fd);
4078 fprintf(stderr, "Failed to write records to backup file\n");
4083 state->nrec += state->recbuf->count;
4084 TALLOC_FREE(state->recbuf);
4089 static int control_backupdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4090 int argc, const char **argv)
4092 const char *db_name;
4093 struct ctdb_db_context *db;
4096 struct backup_state state;
4097 struct db_header db_hdr;
4104 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4108 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4111 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4115 fd = open(argv[1], O_RDWR|O_CREAT, 0600);
4118 fprintf(stderr, "Failed to open file %s for writing\n",
4123 /* Write empty header first */
4124 ZERO_STRUCT(db_hdr);
4125 ret = write(fd, &db_hdr, sizeof(struct db_header));
4129 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4133 state.mem_ctx = mem_ctx;
4134 state.recbuf = NULL;
4139 ret = ctdb_db_traverse(db, true, false, backup_handler, &state);
4141 fprintf(stderr, "Failed to collect records from DB %s\n",
4147 if (state.recbuf != NULL) {
4148 ret = ctdb_rec_buffer_write(state.recbuf, state.fd);
4151 "Failed to write records to backup file\n");
4157 state.nrec += state.recbuf->count;
4158 TALLOC_FREE(state.recbuf);
4161 db_hdr.version = DB_VERSION;
4162 db_hdr.timestamp = time(NULL);
4163 db_hdr.flags = db_flags;
4164 db_hdr.nbuf = state.nbuf;
4165 db_hdr.nrec = state.nrec;
4166 strncpy(db_hdr.name, db_name, MAX_DB_NAME-1);
4168 lseek(fd, 0, SEEK_SET);
4169 ret = write(fd, &db_hdr, sizeof(struct db_header));
4173 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4178 printf("Database backed up to %s\n", argv[1]);
4182 static int control_restoredb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4183 int argc, const char **argv)
4185 const char *db_name = NULL;
4186 struct ctdb_db_context *db;
4187 struct db_header db_hdr;
4188 struct ctdb_node_map *nodemap;
4189 struct ctdb_req_control request;
4190 struct ctdb_reply_control **reply;
4191 struct ctdb_transdb wipedb;
4192 struct ctdb_pulldb_ext pulldb;
4193 struct ctdb_rec_buffer *recbuf;
4194 uint32_t generation;
4200 if (argc < 1 || argc > 2) {
4204 fd = open(argv[0], O_RDONLY, 0600);
4207 fprintf(stderr, "Failed to open file %s for reading\n",
4216 ret = read(fd, &db_hdr, sizeof(struct db_header));
4220 fprintf(stderr, "Failed to read db header from file %s\n",
4225 if (db_hdr.version != DB_VERSION) {
4227 "Wrong version of backup file, expected %u, got %lu\n",
4228 DB_VERSION, db_hdr.version);
4233 if (db_name == NULL) {
4234 db_name = db_hdr.name;
4237 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4238 localtime(&db_hdr.timestamp));
4239 printf("Restoring database %s from backup @ %s\n", db_name, timebuf);
4241 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4244 fprintf(stderr, "Failed to attach to DB %s\n", db_hdr.name);
4248 nodemap = get_nodemap(ctdb, false);
4249 if (nodemap == NULL) {
4250 fprintf(stderr, "Failed to get nodemap\n");
4254 ret = get_generation(mem_ctx, ctdb, &generation);
4256 fprintf(stderr, "Failed to get current generation\n");
4260 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4266 wipedb.db_id = ctdb_db_id(db);
4267 wipedb.tid = generation;
4269 ctdb_req_control_db_freeze(&request, wipedb.db_id);
4270 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4271 ctdb->client, pnn_list, count,
4272 TIMEOUT(), &request, NULL, NULL);
4278 ctdb_req_control_db_transaction_start(&request, &wipedb);
4279 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4280 pnn_list, count, TIMEOUT(),
4281 &request, NULL, NULL);
4286 ctdb_req_control_wipe_database(&request, &wipedb);
4287 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4288 pnn_list, count, TIMEOUT(),
4289 &request, NULL, NULL);
4294 pulldb.db_id = ctdb_db_id(db);
4296 pulldb.srvid = SRVID_CTDB_PUSHDB;
4298 ctdb_req_control_db_push_start(&request, &pulldb);
4299 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4300 pnn_list, count, TIMEOUT(),
4301 &request, NULL, NULL);
4306 for (i=0; i<db_hdr.nbuf; i++) {
4307 struct ctdb_req_message message;
4310 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4315 data.dsize = ctdb_rec_buffer_len(recbuf);
4316 data.dptr = talloc_size(mem_ctx, data.dsize);
4317 if (data.dptr == NULL) {
4321 ctdb_rec_buffer_push(recbuf, data.dptr);
4323 message.srvid = pulldb.srvid;
4324 message.data.data = data;
4326 ret = ctdb_client_message_multi(mem_ctx, ctdb->ev,
4334 talloc_free(recbuf);
4335 talloc_free(data.dptr);
4338 ctdb_req_control_db_push_confirm(&request, pulldb.db_id);
4339 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4340 pnn_list, count, TIMEOUT(),
4341 &request, NULL, &reply);
4346 for (i=0; i<count; i++) {
4347 uint32_t num_records;
4349 ret = ctdb_reply_control_db_push_confirm(reply[i],
4352 fprintf(stderr, "Invalid response from node %u\n",
4357 if (num_records != db_hdr.nrec) {
4358 fprintf(stderr, "Node %u received %u of %lu records\n",
4359 pnn_list[i], num_records, db_hdr.nrec);
4364 ctdb_req_control_db_set_healthy(&request, wipedb.db_id);
4365 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4366 pnn_list, count, TIMEOUT(),
4367 &request, NULL, NULL);
4372 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4373 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4374 pnn_list, count, TIMEOUT(),
4375 &request, NULL, NULL);
4380 ctdb_req_control_db_thaw(&request, wipedb.db_id);
4381 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4382 ctdb->client, pnn_list, count,
4383 TIMEOUT(), &request, NULL, NULL);
4388 printf("Database %s restored\n", db_name);
4393 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4394 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4398 struct dumpdbbackup_state {
4399 ctdb_rec_parser_func_t parser;
4400 struct dump_record_state sub_state;
4403 static int dumpdbbackup_handler(uint32_t reqid,
4404 struct ctdb_ltdb_header *header,
4405 TDB_DATA key, TDB_DATA data,
4408 struct dumpdbbackup_state *state =
4409 (struct dumpdbbackup_state *)private_data;
4410 struct ctdb_ltdb_header hdr;
4413 ret = ctdb_ltdb_header_extract(&data, &hdr);
4418 return state->parser(reqid, &hdr, key, data, &state->sub_state);
4421 static int control_dumpdbbackup(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4422 int argc, const char **argv)
4424 struct db_header db_hdr;
4426 struct dumpdbbackup_state state;
4430 usage("dumpbackup");
4433 fd = open(argv[0], O_RDONLY, 0600);
4436 fprintf(stderr, "Failed to open file %s for reading\n",
4441 ret = read(fd, &db_hdr, sizeof(struct db_header));
4445 fprintf(stderr, "Failed to read db header from file %s\n",
4450 if (db_hdr.version != DB_VERSION) {
4452 "Wrong version of backup file, expected %u, got %lu\n",
4453 DB_VERSION, db_hdr.version);
4457 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4458 localtime(&db_hdr.timestamp));
4459 printf("Dumping database %s from backup @ %s\n",
4460 db_hdr.name, timebuf);
4462 state.parser = dump_record;
4463 state.sub_state.count = 0;
4465 for (i=0; i<db_hdr.nbuf; i++) {
4466 struct ctdb_rec_buffer *recbuf;
4468 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4470 fprintf(stderr, "Failed to read records\n");
4474 ret = ctdb_rec_buffer_traverse(recbuf, dumpdbbackup_handler,
4477 fprintf(stderr, "Failed to dump records\n");
4482 printf("Dumped %u record(s)\n", state.sub_state.count);
4486 static int control_wipedb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4487 int argc, const char **argv)
4489 const char *db_name;
4490 struct ctdb_db_context *db;
4493 struct ctdb_node_map *nodemap;
4494 struct ctdb_req_control request;
4495 struct ctdb_transdb wipedb;
4496 uint32_t generation;
4504 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4508 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4511 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4515 nodemap = get_nodemap(ctdb, false);
4516 if (nodemap == NULL) {
4517 fprintf(stderr, "Failed to get nodemap\n");
4521 ret = get_generation(mem_ctx, ctdb, &generation);
4523 fprintf(stderr, "Failed to get current generation\n");
4527 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4533 ctdb_req_control_db_freeze(&request, db_id);
4534 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4535 ctdb->client, pnn_list, count,
4536 TIMEOUT(), &request, NULL, NULL);
4541 wipedb.db_id = db_id;
4542 wipedb.tid = generation;
4544 ctdb_req_control_db_transaction_start(&request, &wipedb);
4545 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4546 pnn_list, count, TIMEOUT(),
4547 &request, NULL, NULL);
4552 ctdb_req_control_wipe_database(&request, &wipedb);
4553 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4554 pnn_list, count, TIMEOUT(),
4555 &request, NULL, NULL);
4560 ctdb_req_control_db_set_healthy(&request, db_id);
4561 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4562 pnn_list, count, TIMEOUT(),
4563 &request, NULL, NULL);
4568 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4569 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4570 pnn_list, count, TIMEOUT(),
4571 &request, NULL, NULL);
4576 ctdb_req_control_db_thaw(&request, db_id);
4577 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4578 ctdb->client, pnn_list, count,
4579 TIMEOUT(), &request, NULL, NULL);
4584 printf("Database %s wiped\n", db_name);
4589 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4590 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4594 static int control_recmaster(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4595 int argc, const char **argv)
4600 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
4601 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
4606 printf("%u\n", recmaster);
4610 static void print_scriptstatus_one(struct ctdb_script_list *slist,
4611 const char *event_str)
4616 if (slist == NULL) {
4617 if (! options.machinereadable) {
4618 printf("%s cycle never run\n", event_str);
4623 for (i=0; i<slist->num_scripts; i++) {
4624 if (slist->script[i].status != -ENOEXEC) {
4629 if (! options.machinereadable) {
4630 printf("%d scripts were executed last %s cycle\n",
4631 num_run, event_str);
4634 for (i=0; i<slist->num_scripts; i++) {
4635 const char *status = NULL;
4637 switch (slist->script[i].status) {
4639 status = "TIMEDOUT";
4642 status = "DISABLED";
4648 if (slist->script[i].status > 0) {
4654 if (options.machinereadable) {
4655 printf("%s%s%s%s%s%i%s%s%s%lu.%06lu%s%lu.%06lu%s%s%s\n",
4657 event_str, options.sep,
4658 slist->script[i].name, options.sep,
4659 slist->script[i].status, options.sep,
4660 status, options.sep,
4661 slist->script[i].start.tv_sec,
4662 slist->script[i].start.tv_usec, options.sep,
4663 slist->script[i].finished.tv_sec,
4664 slist->script[i].finished.tv_usec, options.sep,
4665 slist->script[i].output, options.sep);
4670 printf("%-20s Status:%s ",
4671 slist->script[i].name, status);
4673 /* Some other error, eg from stat. */
4674 printf("%-20s Status:CANNOT RUN (%s)",
4675 slist->script[i].name,
4676 strerror(-slist->script[i].status));
4679 if (slist->script[i].status >= 0) {
4680 printf("Duration:%.3lf ",
4681 timeval_delta(&slist->script[i].finished,
4682 &slist->script[i].start));
4684 if (slist->script[i].status != -ENOEXEC) {
4685 printf("%s", ctime(&slist->script[i].start.tv_sec));
4686 if (slist->script[i].status != 0) {
4687 printf(" OUTPUT:%s\n",
4688 slist->script[i].output);
4696 static void print_scriptstatus(struct ctdb_script_list **slist,
4697 int count, const char **event_str)
4701 if (options.machinereadable) {
4702 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
4704 "Type", options.sep,
4705 "Name", options.sep,
4706 "Code", options.sep,
4707 "Status", options.sep,
4708 "Start", options.sep,
4710 "Error Output", options.sep);
4713 for (i=0; i<count; i++) {
4714 print_scriptstatus_one(slist[i], event_str[i]);
4718 static int control_scriptstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4719 int argc, const char **argv)
4721 struct ctdb_script_list **slist;
4722 const char *event_str;
4723 enum ctdb_event event;
4724 const char *all_events[] = {
4725 "init", "setup", "startup", "monitor",
4726 "takeip", "releaseip", "updateip", "ipreallocated" };
4729 int count, start, end, num;
4732 usage("scriptstatus");
4736 event_str = "monitor";
4738 event_str = argv[0];
4742 for (i=0; i<ARRAY_SIZE(all_events); i++) {
4743 if (strcmp(event_str, all_events[i]) == 0) {
4752 if (strcmp(event_str, "all") == 0) {
4754 count = ARRAY_SIZE(all_events);
4760 fprintf(stderr, "Unknown event name %s\n", argv[0]);
4761 usage("scriptstatus");
4764 slist = talloc_array(mem_ctx, struct ctdb_script_list *, count);
4765 if (slist == NULL) {
4766 fprintf(stderr, "Memory allocation error\n");
4771 for (i=start; i<end; i++) {
4772 event = ctdb_event_from_string(all_events[i]);
4774 ret = ctdb_ctrl_get_event_script_status(mem_ctx, ctdb->ev,
4781 "failed to get script status for %s event\n",
4786 if (slist[num] == NULL) {
4791 /* The ETIME status is ignored for certain events.
4792 * In that case the status is 0, but endtime is not set.
4794 for (j=0; j<slist[num]->num_scripts; j++) {
4795 if (slist[num]->script[j].status == 0 &&
4796 timeval_is_zero(&slist[num]->script[j].finished)) {
4797 slist[num]->script[j].status = -ETIME;
4804 print_scriptstatus(slist, count, &all_events[start]);
4808 static int control_enablescript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4809 int argc, const char **argv)
4814 usage("enablescript");
4817 ret = ctdb_ctrl_enable_script(mem_ctx, ctdb->ev, ctdb->client,
4818 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4820 fprintf(stderr, "Failed to enable script %s\n", argv[0]);
4827 static int control_disablescript(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4828 int argc, const char **argv)
4833 usage("disablescript");
4836 ret = ctdb_ctrl_disable_script(mem_ctx, ctdb->ev, ctdb->client,
4837 ctdb->cmd_pnn, TIMEOUT(), argv[0]);
4839 fprintf(stderr, "Failed to disable script %s\n", argv[0]);
4846 static int control_natgw(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, argv[0]);
4871 static int control_natgwlist(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4872 int argc, const char **argv)
4874 char *t, *natgw_helper = NULL;
4880 t = getenv("CTDB_NATGW_HELPER");
4882 natgw_helper = talloc_strdup(mem_ctx, t);
4884 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4885 CTDB_HELPER_BINDIR);
4888 if (natgw_helper == NULL) {
4889 fprintf(stderr, "Unable to set NAT gateway helper\n");
4893 return run_helper("NAT gateway helper", natgw_helper, "natgwlist");
4897 * Find the PNN of the current node
4898 * discover the pnn by loading the nodes file and try to bind
4899 * to all addresses one at a time until the ip address is found.
4901 static bool find_node_xpnn(TALLOC_CTX *mem_ctx, uint32_t *pnn)
4903 struct ctdb_node_map *nodemap;
4906 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
4907 if (nodemap == NULL) {
4911 for (i=0; i<nodemap->num; i++) {
4912 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
4915 if (ctdb_sys_have_ip(&nodemap->node[i].addr)) {
4917 *pnn = nodemap->node[i].pnn;
4919 talloc_free(nodemap);
4924 fprintf(stderr, "Failed to detect PNN of the current node.\n");
4925 talloc_free(nodemap);
4929 static int control_getreclock(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4930 int argc, const char **argv)
4932 const char *reclock;
4936 usage("getreclock");
4939 ret = ctdb_ctrl_get_reclock_file(mem_ctx, ctdb->ev, ctdb->client,
4940 ctdb->cmd_pnn, TIMEOUT(), &reclock);
4945 if (reclock != NULL) {
4946 printf("%s\n", reclock);
4952 static int control_setlmasterrole(TALLOC_CTX *mem_ctx,
4953 struct ctdb_context *ctdb,
4954 int argc, const char **argv)
4956 uint32_t lmasterrole = 0;
4960 usage("setlmasterrole");
4963 if (strcmp(argv[0], "on") == 0) {
4965 } else if (strcmp(argv[0], "off") == 0) {
4968 usage("setlmasterrole");
4971 ret = ctdb_ctrl_set_lmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4972 ctdb->cmd_pnn, TIMEOUT(), lmasterrole);
4980 static int control_setrecmasterrole(TALLOC_CTX *mem_ctx,
4981 struct ctdb_context *ctdb,
4982 int argc, const char **argv)
4984 uint32_t recmasterrole = 0;
4988 usage("setrecmasterrole");
4991 if (strcmp(argv[0], "on") == 0) {
4993 } else if (strcmp(argv[0], "off") == 0) {
4996 usage("setrecmasterrole");
4999 ret = ctdb_ctrl_set_recmasterrole(mem_ctx, ctdb->ev, ctdb->client,
5000 ctdb->cmd_pnn, TIMEOUT(),
5009 static int control_setdbreadonly(TALLOC_CTX *mem_ctx,
5010 struct ctdb_context *ctdb,
5011 int argc, const char **argv)
5018 usage("setdbreadonly");
5021 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
5025 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5026 fprintf(stderr, "Cannot set READONLY on persistent DB\n");
5030 ret = ctdb_ctrl_set_db_readonly(mem_ctx, ctdb->ev, ctdb->client,
5031 ctdb->cmd_pnn, TIMEOUT(), db_id);
5039 static int control_setdbsticky(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5040 int argc, const char **argv)
5047 usage("setdbsticky");
5050 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
5054 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5055 fprintf(stderr, "Cannot set STICKY on persistent DB\n");
5059 ret = ctdb_ctrl_set_db_sticky(mem_ctx, ctdb->ev, ctdb->client,
5060 ctdb->cmd_pnn, TIMEOUT(), db_id);
5068 static int control_pfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5069 int argc, const char **argv)
5071 const char *db_name;
5072 struct ctdb_db_context *db;
5073 struct ctdb_transaction_handle *h;
5078 if (argc < 2 || argc > 3) {
5082 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5086 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5087 fprintf(stderr, "DB %s is not a persistent database\n",
5092 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5095 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5099 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5101 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5105 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5106 TIMEOUT(), db, true, &h);
5108 fprintf(stderr, "Failed to start transaction on db %s\n",
5113 ret = ctdb_transaction_fetch_record(h, key, mem_ctx, &data);
5115 fprintf(stderr, "Failed to read record for key %s\n",
5120 printf("%.*s\n", (int)data.dsize, data.dptr);
5122 ctdb_transaction_cancel(h);
5126 static int control_pstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5127 int argc, const char **argv)
5129 const char *db_name;
5130 struct ctdb_db_context *db;
5131 struct ctdb_transaction_handle *h;
5140 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5144 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5145 fprintf(stderr, "DB %s is not a persistent database\n",
5150 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5153 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5157 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5159 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5163 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5165 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5169 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5170 TIMEOUT(), db, false, &h);
5172 fprintf(stderr, "Failed to start transaction on db %s\n",
5177 ret = ctdb_transaction_store_record(h, key, data);
5179 fprintf(stderr, "Failed to store record for key %s\n",
5184 ret = ctdb_transaction_commit(h);
5186 fprintf(stderr, "Failed to commit transaction on db %s\n",
5194 static int control_pdelete(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5195 int argc, const char **argv)
5197 const char *db_name;
5198 struct ctdb_db_context *db;
5199 struct ctdb_transaction_handle *h;
5208 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5212 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5213 fprintf(stderr, "DB %s is not a persistent database\n",
5218 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5221 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5225 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5227 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5231 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5232 TIMEOUT(), db, false, &h);
5234 fprintf(stderr, "Failed to start transaction on db %s\n",
5239 ret = ctdb_transaction_delete_record(h, key);
5241 fprintf(stderr, "Failed to delete record for key %s\n",
5246 ret = ctdb_transaction_commit(h);
5248 fprintf(stderr, "Failed to commit transaction on db %s\n",
5256 static int ptrans_parse_string(TALLOC_CTX *mem_ctx, const char **ptr, TDB_DATA *data)
5264 /* Skip whitespace */
5265 n = strspn(*ptr, " \t");
5269 /* Quoted ASCII string - no wide characters! */
5271 n = strcspn(t, "\"");
5274 ret = str_to_data(t, n, mem_ctx, data);
5281 fprintf(stderr, "Unmatched \" in input %s\n", *ptr);
5285 fprintf(stderr, "Unsupported input format in %s\n", *ptr);
5292 #define MAX_LINE_SIZE 1024
5294 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
5295 TDB_DATA *key, TDB_DATA *value)
5297 char line [MAX_LINE_SIZE]; /* FIXME: make this more flexible? */
5301 ptr = fgets(line, MAX_LINE_SIZE, file);
5307 ret = ptrans_parse_string(mem_ctx, &ptr, key);
5308 if (ret != 0 || ptr == NULL || key->dptr == NULL) {
5309 /* Line Ignored but not EOF */
5315 ret = ptrans_parse_string(mem_ctx, &ptr, value);
5317 /* Line Ignored but not EOF */
5318 talloc_free(key->dptr);
5326 static int control_ptrans(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5327 int argc, const char **argv)
5329 const char *db_name;
5330 struct ctdb_db_context *db;
5331 struct ctdb_transaction_handle *h;
5334 TDB_DATA key, value;
5337 if (argc < 1 || argc > 2) {
5341 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5345 if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
5346 fprintf(stderr, "DB %s is not a persistent database\n",
5352 file = fopen(argv[1], "r");
5354 fprintf(stderr, "Failed to open file %s\n", argv[1]);
5361 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5364 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5368 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5369 TIMEOUT(), db, false, &h);
5371 fprintf(stderr, "Failed to start transaction on db %s\n",
5376 while (ptrans_get_key_value(mem_ctx, file, &key, &value)) {
5377 if (key.dsize != 0) {
5378 ret = ctdb_transaction_store_record(h, key, value);
5380 fprintf(stderr, "Failed to store record\n");
5381 ctdb_transaction_cancel(h);
5384 talloc_free(key.dptr);
5385 talloc_free(value.dptr);
5389 ret = ctdb_transaction_commit(h);
5391 fprintf(stderr, "Failed to commit transaction on db %s\n",
5396 if (file != stdin) {
5402 static int control_tfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5403 int argc, const char **argv)
5405 struct tdb_context *tdb;
5407 struct ctdb_ltdb_header header;
5410 if (argc < 2 || argc > 3) {
5414 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5416 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5420 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5422 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5426 data = tdb_fetch(tdb, key);
5427 if (data.dptr == NULL) {
5428 fprintf(stderr, "No record for key %s\n", argv[1]);
5432 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
5433 fprintf(stderr, "Invalid record for key %s\n", argv[1]);
5443 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
5445 fprintf(stderr, "Failed to open output file %s\n",
5450 nwritten = sys_write(fd, data.dptr, data.dsize);
5451 if (nwritten != data.dsize) {
5452 fprintf(stderr, "Failed to write record to file\n");
5459 ret = ctdb_ltdb_header_extract(&data, &header);
5461 fprintf(stderr, "Failed to parse header from data\n");
5465 dump_ltdb_header(&header);
5466 dump_tdb_data("data", data);
5471 static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5472 int argc, const char **argv)
5474 struct tdb_context *tdb;
5475 TDB_DATA key, data, value;
5476 struct ctdb_ltdb_header header;
5480 if (argc < 3 || argc > 5) {
5484 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5486 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5490 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5492 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5496 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &value);
5498 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5502 ZERO_STRUCT(header);
5505 header.rsn = (uint64_t)strtoull(argv[3], NULL, 0);
5508 header.dmaster = (uint32_t)atol(argv[4]);
5511 header.flags = (uint32_t)atol(argv[5]);
5514 offset = ctdb_ltdb_header_len(&header);
5515 data.dsize = offset + value.dsize;
5516 data.dptr = talloc_size(mem_ctx, data.dsize);
5517 if (data.dptr == NULL) {
5518 fprintf(stderr, "Memory allocation error\n");
5522 ctdb_ltdb_header_push(&header, data.dptr);
5523 memcpy(data.dptr + offset, value.dptr, value.dsize);
5525 ret = tdb_store(tdb, key, data, TDB_REPLACE);
5527 fprintf(stderr, "Failed to write record %s to file %s\n",
5536 static int control_readkey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5537 int argc, const char **argv)
5539 const char *db_name;
5540 struct ctdb_db_context *db;
5541 struct ctdb_record_handle *h;
5544 bool readonly = false;
5547 if (argc < 2 || argc > 3) {
5552 if (strcmp(argv[2], "readonly") == 0) {
5559 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5563 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5564 fprintf(stderr, "DB %s is not a volatile database\n",
5569 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5572 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5576 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5578 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5582 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5583 db, key, readonly, &h, NULL, &data);
5585 fprintf(stderr, "Failed to read record for key %s\n",
5588 printf("Data: size:%zu ptr:[%.*s]\n", data.dsize,
5589 (int)data.dsize, data.dptr);
5596 static int control_writekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5597 int argc, const char **argv)
5599 const char *db_name;
5600 struct ctdb_db_context *db;
5601 struct ctdb_record_handle *h;
5610 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5614 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5615 fprintf(stderr, "DB %s is not a volatile database\n",
5620 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5623 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5627 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5629 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5633 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5635 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5639 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5640 db, key, false, &h, NULL, NULL);
5642 fprintf(stderr, "Failed to lock record for key %s\n", argv[0]);
5646 ret = ctdb_store_record(h, data);
5648 fprintf(stderr, "Failed to store record for key %s\n",
5656 static int control_deletekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5657 int argc, const char **argv)
5659 const char *db_name;
5660 struct ctdb_db_context *db;
5661 struct ctdb_record_handle *h;
5670 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5674 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
5675 fprintf(stderr, "DB %s is not a volatile database\n",
5680 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5683 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5687 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5689 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5693 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5694 db, key, false, &h, NULL, &data);
5696 fprintf(stderr, "Failed to fetch record for key %s\n",
5701 ret = ctdb_delete_record(h);
5703 fprintf(stderr, "Failed to delete record for key %s\n",
5711 static int control_checktcpport(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5712 int argc, const char **argv)
5714 struct sockaddr_in sin;
5720 usage("chktcpport");
5723 port = atoi(argv[0]);
5725 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
5727 fprintf(stderr, "Failed to open local socket\n");
5731 v = fcntl(s, F_GETFL, 0);
5732 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK)) {
5733 fprintf(stderr, "Unable to set socket non-blocking\n");
5737 bzero(&sin, sizeof(sin));
5738 sin.sin_family = AF_INET;
5739 sin.sin_port = htons(port);
5740 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
5743 fprintf(stderr, "Failed to bind to TCP port %u\n", port);
5750 static int control_rebalancenode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5751 int argc, const char **argv)
5754 usage("rebalancenode");
5757 if (! rebalancenode(mem_ctx, ctdb, ctdb->cmd_pnn)) {
5758 fprintf(stderr, "Failed to rebalance IPs on node %u\n",
5766 static int control_getdbseqnum(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5767 int argc, const char **argv)
5770 const char *db_name;
5775 usage("getdbseqnum");
5778 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5782 ret = ctdb_ctrl_get_db_seqnum(mem_ctx, ctdb->ev, ctdb->client,
5783 ctdb->cmd_pnn, TIMEOUT(), db_id,
5786 fprintf(stderr, "Failed to get sequence number for DB %s\n",
5791 printf("0x%"PRIx64"\n", seqnum);
5795 static int control_nodestatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5796 int argc, const char **argv)
5798 const char *nodestring = NULL;
5799 struct ctdb_node_map *nodemap;
5803 usage("nodestatus");
5807 nodestring = argv[0];
5810 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap)) {
5814 nodemap = get_nodemap(ctdb, false);
5815 if (nodemap == NULL) {
5819 if (options.machinereadable) {
5820 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5822 print_nodemap(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5826 for (i=0; i<nodemap->num; i++) {
5827 ret |= nodemap->node[i].flags;
5836 } db_stats_fields[] = {
5837 #define DBSTATISTICS_FIELD(n) { #n, offsetof(struct ctdb_db_statistics, n) }
5838 DBSTATISTICS_FIELD(db_ro_delegations),
5839 DBSTATISTICS_FIELD(db_ro_revokes),
5840 DBSTATISTICS_FIELD(locks.num_calls),
5841 DBSTATISTICS_FIELD(locks.num_current),
5842 DBSTATISTICS_FIELD(locks.num_pending),
5843 DBSTATISTICS_FIELD(locks.num_failed),
5844 DBSTATISTICS_FIELD(db_ro_delegations),
5847 static void print_dbstatistics(const char *db_name,
5848 struct ctdb_db_statistics *s)
5851 const char *prefix = NULL;
5854 printf("DB Statistics %s\n", db_name);
5856 for (i=0; i<ARRAY_SIZE(db_stats_fields); i++) {
5857 if (strchr(db_stats_fields[i].name, '.') != NULL) {
5858 preflen = strcspn(db_stats_fields[i].name, ".") + 1;
5860 strncmp(prefix, db_stats_fields[i].name, preflen) != 0) {
5861 prefix = db_stats_fields[i].name;
5862 printf(" %*.*s\n", preflen-1, preflen-1,
5863 db_stats_fields[i].name);
5868 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
5869 db_stats_fields[i].name+preflen, preflen ? 0 : 4, "",
5870 *(uint32_t *)(db_stats_fields[i].offset+(uint8_t *)s));
5873 printf(" hop_count_buckets:");
5874 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5875 printf(" %d", s->hop_count_bucket[i]);
5879 printf(" lock_buckets:");
5880 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5881 printf(" %d", s->locks.buckets[i]);
5885 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5886 "locks_latency MIN/AVG/MAX",
5887 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
5888 s->locks.latency.max, s->locks.latency.num);
5890 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5891 "vacuum_latency MIN/AVG/MAX",
5892 s->vacuum.latency.min, LATENCY_AVG(s->vacuum.latency),
5893 s->vacuum.latency.max, s->vacuum.latency.num);
5895 printf(" Num Hot Keys: %d\n", s->num_hot_keys);
5896 for (i=0; i<s->num_hot_keys; i++) {
5898 printf(" Count:%d Key:", s->hot_keys[i].count);
5899 for (j=0; j<s->hot_keys[i].key.dsize; j++) {
5900 printf("%02x", s->hot_keys[i].key.dptr[j] & 0xff);
5906 static int control_dbstatistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5907 int argc, const char **argv)
5910 const char *db_name;
5911 struct ctdb_db_statistics *dbstats;
5915 usage("dbstatistics");
5918 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5922 ret = ctdb_ctrl_get_db_statistics(mem_ctx, ctdb->ev, ctdb->client,
5923 ctdb->cmd_pnn, TIMEOUT(), db_id,
5926 fprintf(stderr, "Failed to get statistics for DB %s\n",
5931 print_dbstatistics(db_name, dbstats);
5935 struct disable_takeover_runs_state {
5943 static void disable_takeover_run_handler(uint64_t srvid, TDB_DATA data,
5946 struct disable_takeover_runs_state *state =
5947 (struct disable_takeover_runs_state *)private_data;
5950 if (data.dsize != sizeof(int)) {
5955 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
5956 ret = *(int *)data.dptr;
5958 state->status = ret;
5962 for (i=0; i<state->node_count; i++) {
5963 if (state->pnn_list[i] == ret) {
5964 state->reply[i] = true;
5970 for (i=0; i<state->node_count; i++) {
5971 if (! state->reply[i]) {
5972 state->done = false;
5978 static int disable_takeover_runs(TALLOC_CTX *mem_ctx,
5979 struct ctdb_context *ctdb, uint32_t timeout,
5980 uint32_t *pnn_list, int count)
5982 struct ctdb_disable_message disable = { 0 };
5983 struct disable_takeover_runs_state state;
5986 disable.pnn = ctdb->pnn;
5987 disable.srvid = next_srvid(ctdb);
5988 disable.timeout = timeout;
5990 state.pnn_list = pnn_list;
5991 state.node_count = count;
5994 state.reply = talloc_zero_array(mem_ctx, bool, count);
5995 if (state.reply == NULL) {
5999 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
6001 disable_takeover_run_handler,
6007 for (i=0; i<count; i++) {
6008 ret = ctdb_message_disable_takeover_runs(mem_ctx, ctdb->ev,
6017 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
6019 fprintf(stderr, "Timed out waiting to disable takeover runs\n");
6021 ret = (state.status >= 0 ? 0 : 1);
6025 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
6026 disable.srvid, &state);
6030 static int control_reloadips(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
6031 int argc, const char **argv)
6033 const char *nodestring = NULL;
6034 struct ctdb_node_map *nodemap, *nodemap2;
6035 struct ctdb_req_control request;
6036 uint32_t *pnn_list, *pnn_list2;
6037 int ret, count, count2;
6044 nodestring = argv[0];
6047 nodemap = get_nodemap(ctdb, false);
6048 if (nodemap == NULL) {
6052 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap2)) {
6056 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
6057 mem_ctx, &pnn_list);
6059 fprintf(stderr, "Memory allocation error\n");
6063 count2 = list_of_active_nodes(nodemap2, CTDB_UNKNOWN_PNN,
6064 mem_ctx, &pnn_list2);
6066 fprintf(stderr, "Memory allocation error\n");
6070 /* Disable takeover runs on all connected nodes. A reply
6071 * indicating success is needed from each node so all nodes
6072 * will need to be active.
6074 * A check could be added to not allow reloading of IPs when
6075 * there are disconnected nodes. However, this should
6076 * probably be left up to the administrator.
6078 ret = disable_takeover_runs(mem_ctx, ctdb, 2*options.timelimit,
6081 fprintf(stderr, "Failed to disable takeover runs\n");
6085 /* Now tell all the desired nodes to reload their public IPs.
6086 * Keep trying this until it succeeds. This assumes all
6087 * failures are transient, which might not be true...
6089 ctdb_req_control_reload_public_ips(&request);
6090 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
6091 pnn_list2, count2, TIMEOUT(),
6092 &request, NULL, NULL);
6094 fprintf(stderr, "Failed to reload IPs on some nodes.\n");
6097 /* It isn't strictly necessary to wait until takeover runs are
6098 * re-enabled but doing so can't hurt.
6100 ret = disable_takeover_runs(mem_ctx, ctdb, 0, pnn_list, count);
6102 fprintf(stderr, "Failed to enable takeover runs\n");
6106 return ipreallocate(mem_ctx, ctdb);
6109 static int control_ipiface(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
6110 int argc, const char **argv)
6112 ctdb_sock_addr addr;
6119 if (! parse_ip(argv[0], NULL, 0, &addr)) {
6120 fprintf(stderr, "Failed to Parse IP %s\n", argv[0]);
6124 iface = ctdb_sys_find_ifname(&addr);
6125 if (iface == NULL) {
6126 fprintf(stderr, "Failed to find interface for IP %s\n",
6136 static const struct ctdb_cmd {
6138 int (*fn)(TALLOC_CTX *, struct ctdb_context *, int, const char **);
6139 bool without_daemon; /* can be run without daemon running ? */
6140 bool remote; /* can be run on remote nodes */
6143 } ctdb_commands[] = {
6144 { "version", control_version, true, false,
6145 "show version of ctdb", NULL },
6146 { "status", control_status, false, true,
6147 "show node status", NULL },
6148 { "uptime", control_uptime, false, true,
6149 "show node uptime", NULL },
6150 { "ping", control_ping, false, true,
6151 "ping all nodes", NULL },
6152 { "runstate", control_runstate, false, true,
6153 "get/check runstate of a node",
6154 "[setup|first_recovery|startup|running]" },
6155 { "getvar", control_getvar, false, true,
6156 "get a tunable variable", "<name>" },
6157 { "setvar", control_setvar, false, true,
6158 "set a tunable variable", "<name> <value>" },
6159 { "listvars", control_listvars, false, true,
6160 "list tunable variables", NULL },
6161 { "statistics", control_statistics, false, true,
6162 "show ctdb statistics", NULL },
6163 { "statisticsreset", control_statistics_reset, false, true,
6164 "reset ctdb statistics", NULL },
6165 { "stats", control_stats, false, true,
6166 "show rolling statistics", "[count]" },
6167 { "ip", control_ip, false, true,
6168 "show public ips", "[all]" },
6169 { "ipinfo", control_ipinfo, false, true,
6170 "show public ip details", "<ip>" },
6171 { "ifaces", control_ifaces, false, true,
6172 "show interfaces", NULL },
6173 { "setifacelink", control_setifacelink, false, true,
6174 "set interface link status", "<iface> up|down" },
6175 { "process-exists", control_process_exists, false, true,
6176 "check if a process exists on a node", "<pid>" },
6177 { "getdbmap", control_getdbmap, false, true,
6178 "show attached databases", NULL },
6179 { "getdbstatus", control_getdbstatus, false, true,
6180 "show database status", "<dbname|dbid>" },
6181 { "catdb", control_catdb, false, false,
6182 "dump cluster-wide ctdb database", "<dbname|dbid>" },
6183 { "cattdb", control_cattdb, false, false,
6184 "dump local ctdb database", "<dbname|dbid>" },
6185 { "getmonmode", control_getmonmode, false, true,
6186 "show monitoring mode", NULL },
6187 { "getcapabilities", control_getcapabilities, false, true,
6188 "show node capabilities", NULL },
6189 { "pnn", control_pnn, false, false,
6190 "show the pnn of the currnet node", NULL },
6191 { "lvs", control_lvs, false, false,
6192 "show lvs configuration", "master|list|status" },
6193 { "disablemonitor", control_disable_monitor, false, true,
6194 "disable monitoring", NULL },
6195 { "enablemonitor", control_enable_monitor, false, true,
6196 "enable monitoring", NULL },
6197 { "setdebug", control_setdebug, false, true,
6198 "set debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
6199 { "getdebug", control_getdebug, false, true,
6200 "get debug level", NULL },
6201 { "attach", control_attach, false, false,
6202 "attach a database", "<dbname> [persistent]" },
6203 { "detach", control_detach, false, false,
6204 "detach database(s)", "<dbname|dbid> ..." },
6205 { "dumpmemory", control_dumpmemory, false, true,
6206 "dump ctdbd memory map", NULL },
6207 { "rddumpmemory", control_rddumpmemory, false, true,
6208 "dump recoverd memory map", NULL },
6209 { "getpid", control_getpid, false, true,
6210 "get ctdbd process ID", NULL },
6211 { "disable", control_disable, false, true,
6212 "disable a node", NULL },
6213 { "enable", control_enable, false, true,
6214 "enable a node", NULL },
6215 { "stop", control_stop, false, true,
6216 "stop a node", NULL },
6217 { "continue", control_continue, false, true,
6218 "continue a stopped node", NULL },
6219 { "ban", control_ban, false, true,
6220 "ban a node", "<bantime>"},
6221 { "unban", control_unban, false, true,
6222 "unban a node", NULL },
6223 { "shutdown", control_shutdown, false, true,
6224 "shutdown ctdb daemon", NULL },
6225 { "recover", control_recover, false, true,
6226 "force recovery", NULL },
6227 { "sync", control_ipreallocate, false, true,
6228 "run ip reallocation (deprecated)", NULL },
6229 { "ipreallocate", control_ipreallocate, false, true,
6230 "run ip reallocation", NULL },
6231 { "isnotrecmaster", control_isnotrecmaster, false, false,
6232 "check if local node is the recmaster", NULL },
6233 { "gratarp", control_gratarp, false, true,
6234 "send a gratuitous arp", "<ip> <interface>" },
6235 { "tickle", control_tickle, false, false,
6236 "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6237 { "gettickles", control_gettickles, false, true,
6238 "get the list of tickles", "<ip> [<port>]" },
6239 { "addtickle", control_addtickle, false, true,
6240 "add a tickle", "<ip>:<port> <ip>:<port>" },
6241 { "deltickle", control_deltickle, false, true,
6242 "delete a tickle", "<ip>:<port> <ip>:<port>" },
6243 { "check_srvids", control_check_srvids, false, true,
6244 "check if srvid is registered", "<id> [<id> ...]" },
6245 { "listnodes", control_listnodes, true, true,
6246 "list nodes in the cluster", NULL },
6247 { "reloadnodes", control_reloadnodes, false, false,
6248 "reload the nodes file all nodes", NULL },
6249 { "moveip", control_moveip, false, false,
6250 "move an ip address to another node", "<ip> <node>" },
6251 { "rebalanceip", control_rebalanceip, false, false,
6252 "move an ip address optimally to another node", "<ip>" },
6253 { "addip", control_addip, false, true,
6254 "add an ip address to a node", "<ip/mask> <iface>" },
6255 { "delip", control_delip, false, true,
6256 "delete an ip address from a node", "<ip>" },
6257 { "eventscript", control_eventscript, false, true,
6258 "run an event", "monitor" },
6259 { "backupdb", control_backupdb, false, false,
6260 "backup a database into a file", "<dbname|dbid> <file>" },
6261 { "restoredb", control_restoredb, false, false,
6262 "restore a database from a file", "<file> [dbname]" },
6263 { "dumpdbbackup", control_dumpdbbackup, true, false,
6264 "dump database from a backup file", "<file>" },
6265 { "wipedb", control_wipedb, false, false,
6266 "wipe the contents of a database.", "<dbname|dbid>"},
6267 { "recmaster", control_recmaster, false, true,
6268 "show the pnn for the recovery master", NULL },
6269 { "scriptstatus", control_scriptstatus, false, true,
6270 "show event script status",
6271 "[init|setup|startup|monitor|takeip|releaseip|ipreallocated]" },
6272 { "enablescript", control_enablescript, false, true,
6273 "enable an eventscript", "<script>"},
6274 { "disablescript", control_disablescript, false, true,
6275 "disable an eventscript", "<script>"},
6276 { "natgw", control_natgw, false, false,
6277 "show natgw configuration", "master|list|status" },
6278 { "natgwlist", control_natgwlist, false, false,
6279 "show the nodes belonging to this natgw configuration", NULL },
6280 { "getreclock", control_getreclock, false, true,
6281 "get recovery lock file", NULL },
6282 { "setlmasterrole", control_setlmasterrole, false, true,
6283 "set LMASTER role", "on|off" },
6284 { "setrecmasterrole", control_setrecmasterrole, false, true,
6285 "set RECMASTER role", "on|off"},
6286 { "setdbreadonly", control_setdbreadonly, false, true,
6287 "enable readonly records", "<dbname|dbid>" },
6288 { "setdbsticky", control_setdbsticky, false, true,
6289 "enable sticky records", "<dbname|dbid>"},
6290 { "pfetch", control_pfetch, false, false,
6291 "fetch record from persistent database", "<dbname|dbid> <key> [<file>]" },
6292 { "pstore", control_pstore, false, false,
6293 "write record to persistent database", "<dbname|dbid> <key> <value>" },
6294 { "pdelete", control_pdelete, false, false,
6295 "delete record from persistent database", "<dbname|dbid> <key>" },
6296 { "ptrans", control_ptrans, false, false,
6297 "update a persistent database (from file or stdin)", "<dbname|dbid> [<file>]" },
6298 { "tfetch", control_tfetch, false, true,
6299 "fetch a record", "<tdb-file> <key> [<file>]" },
6300 { "tstore", control_tstore, false, true,
6301 "store a record", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6302 { "readkey", control_readkey, false, false,
6303 "read value of a database key", "<dbname|dbid> <key> [readonly]" },
6304 { "writekey", control_writekey, false, false,
6305 "write value for a database key", "<dbname|dbid> <key> <value>" },
6306 { "deletekey", control_deletekey, false, false,
6307 "delete a database key", "<dbname|dbid> <key>" },
6308 { "checktcpport", control_checktcpport, true, false,
6309 "check if a service is bound to a specific tcp port or not", "<port>" },
6310 { "rebalancenode", control_rebalancenode, false, true,
6311 "mark nodes as forced IP rebalancing targets", NULL },
6312 { "getdbseqnum", control_getdbseqnum, false, false,
6313 "get database sequence number", "<dbname|dbid>" },
6314 { "nodestatus", control_nodestatus, false, true,
6315 "show and return node status", "[all|<pnn-list>]" },
6316 { "dbstatistics", control_dbstatistics, false, true,
6317 "show database statistics", "<dbname|dbid>" },
6318 { "reloadips", control_reloadips, false, false,
6319 "reload the public addresses file", "[all|<pnn-list>]" },
6320 { "ipiface", control_ipiface, true, false,
6321 "Find the interface an ip address is hosted on", "<ip>" },
6324 static const struct ctdb_cmd *match_command(const char *command)
6326 const struct ctdb_cmd *cmd;
6329 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6330 cmd = &ctdb_commands[i];
6331 if (strlen(command) == strlen(cmd->name) &&
6332 strncmp(command, cmd->name, strlen(command)) == 0) {
6342 * Show usage message
6344 static void usage_full(void)
6348 poptPrintHelp(pc, stdout, 0);
6349 printf("\nCommands:\n");
6350 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6351 printf(" %-15s %-27s %s\n",
6352 ctdb_commands[i].name,
6353 ctdb_commands[i].args ? ctdb_commands[i].args : "",
6354 ctdb_commands[i].msg);
6358 static void usage(const char *command)
6360 const struct ctdb_cmd *cmd;
6362 if (command == NULL) {
6367 cmd = match_command(command);
6371 poptPrintUsage(pc, stdout, 0);
6372 printf("\nCommands:\n");
6373 printf(" %-15s %-27s %s\n",
6374 cmd->name, cmd->args ? cmd->args : "", cmd->msg);
6380 struct poptOption cmdline_options[] = {
6382 { "socket", 's', POPT_ARG_STRING, &options.socket, 0,
6383 "CTDB socket path", "filename" },
6384 { "debug", 'd', POPT_ARG_STRING, &options.debuglevelstr, 0,
6386 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0,
6387 "timelimit (in seconds)" },
6388 { "node", 'n', POPT_ARG_INT, &options.pnn, 0,
6389 "node specification - integer" },
6390 { NULL, 'Y', POPT_ARG_NONE, &options.machinereadable, 0,
6391 "enable machine readable output", NULL },
6392 { "separator", 'x', POPT_ARG_STRING, &options.sep, 0,
6393 "specify separator for machine readable output", "CHAR" },
6394 { NULL, 'X', POPT_ARG_NONE, &options.machineparsable, 0,
6395 "enable machine parsable output with separator |", NULL },
6396 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0,
6397 "enable verbose output", NULL },
6398 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0,
6399 "die if runtime exceeds this limit (in seconds)" },
6403 static int process_command(const struct ctdb_cmd *cmd, int argc,
6406 TALLOC_CTX *tmp_ctx;
6407 struct ctdb_context *ctdb;
6410 uint64_t srvid_offset;
6412 tmp_ctx = talloc_new(NULL);
6413 if (tmp_ctx == NULL) {
6414 fprintf(stderr, "Memory allocation error\n");
6418 if (cmd->without_daemon) {
6419 if (options.pnn != -1) {
6421 "Cannot specify node for command %s\n",
6426 return cmd->fn(tmp_ctx, NULL, argc-1, argv+1);
6429 ctdb = talloc_zero(tmp_ctx, struct ctdb_context);
6431 fprintf(stderr, "Memory allocation error\n");
6435 ctdb->ev = tevent_context_init(ctdb);
6436 if (ctdb->ev == NULL) {
6437 fprintf(stderr, "Failed to initialize tevent\n");
6441 ret = ctdb_client_init(ctdb, ctdb->ev, options.socket, &ctdb->client);
6443 fprintf(stderr, "Failed to connect to CTDB daemon (%s)\n",
6446 if (!find_node_xpnn(ctdb, NULL)) {
6447 fprintf(stderr, "Is this node part of CTDB cluster?\n");
6452 ctdb->pnn = ctdb_client_pnn(ctdb->client);
6453 srvid_offset = getpid() & 0xFFFF;
6454 ctdb->srvid = SRVID_CTDB_TOOL | (srvid_offset << 16);
6456 if (options.pnn != -1) {
6457 status = verify_pnn(ctdb, options.pnn);
6462 ctdb->cmd_pnn = options.pnn;
6464 ctdb->cmd_pnn = ctdb->pnn;
6467 if (! cmd->remote && ctdb->pnn != ctdb->cmd_pnn) {
6468 fprintf(stderr, "Node cannot be specified for command %s\n",
6473 ret = cmd->fn(tmp_ctx, ctdb, argc-1, argv+1);
6474 talloc_free(tmp_ctx);
6478 talloc_free(tmp_ctx);
6482 static void signal_handler(int sig)
6484 fprintf(stderr, "Maximum runtime exceeded - exiting\n");
6487 static void alarm_handler(int sig)
6489 /* Kill any child processes */
6490 signal(SIGTERM, signal_handler);
6496 int main(int argc, const char *argv[])
6499 const char **extra_argv;
6501 const struct ctdb_cmd *cmd;
6502 const char *ctdb_socket;
6503 enum debug_level loglevel;
6508 /* Set default options */
6509 options.socket = CTDB_SOCKET;
6510 options.debuglevelstr = NULL;
6511 options.timelimit = 10;
6513 options.maxruntime = 0;
6516 ctdb_socket = getenv("CTDB_SOCKET");
6517 if (ctdb_socket != NULL) {
6518 options.socket = ctdb_socket;
6521 pc = poptGetContext(argv[0], argc, argv, cmdline_options,
6522 POPT_CONTEXT_KEEP_FIRST);
6523 while ((opt = poptGetNextOpt(pc)) != -1) {
6524 fprintf(stderr, "Invalid option %s: %s\n",
6525 poptBadOption(pc, 0), poptStrerror(opt));
6529 if (options.maxruntime == 0) {
6530 const char *ctdb_timeout;
6532 ctdb_timeout = getenv("CTDB_TIMEOUT");
6533 if (ctdb_timeout != NULL) {
6534 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6536 options.maxruntime = 120;
6539 if (options.maxruntime <= 120) {
6540 /* default timeout is 120 seconds */
6541 options.maxruntime = 120;
6544 if (options.machineparsable) {
6545 options.machinereadable = 1;
6548 /* setup the remaining options for the commands */
6550 extra_argv = poptGetArgs(pc);
6553 while (extra_argv[extra_argc]) extra_argc++;
6556 if (extra_argc < 1) {
6560 cmd = match_command(extra_argv[0]);
6562 fprintf(stderr, "Unknown command '%s'\n", extra_argv[0]);
6566 /* Enable logging */
6567 setup_logging("ctdb", DEBUG_STDERR);
6568 if (debug_level_parse(options.debuglevelstr, &loglevel)) {
6569 DEBUGLEVEL = loglevel;
6571 DEBUGLEVEL = DEBUG_ERR;
6574 signal(SIGALRM, alarm_handler);
6575 alarm(options.maxruntime);
6577 ret = process_command(cmd, extra_argc, extra_argv);
6582 (void)poptFreeContext(pc);