4 Copyright (C) Andrew Tridgell 2007
5 Copyright (C) Ronnie Sahlberg 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "lib/events/events.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "system/network.h"
26 #include "system/locale.h"
29 #include "../include/ctdb.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
34 #define ERR_TIMEOUT 20
36 static void usage(void);
45 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
48 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
51 #define XSTR(x) STR(x)
52 printf("CTDB version: %s\n", XSTR(CTDB_VERS));
59 check if a database exists
61 static int db_exists(struct ctdb_context *ctdb, const char *db_name)
64 struct ctdb_dbid_map *dbmap=NULL;
66 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
68 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
72 for(i=0;i<dbmap->num;i++){
75 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
76 if (!strcmp(name, db_name)) {
85 see if a process exists
87 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
95 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
96 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
100 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
102 printf("%u:%u exists\n", pnn, pid);
104 printf("%u:%u does not exist\n", pnn, pid);
110 display statistics structure
112 static void show_statistics(struct ctdb_statistics *s)
114 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
116 const char *prefix=NULL;
122 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
123 STATISTICS_FIELD(num_clients),
124 STATISTICS_FIELD(frozen),
125 STATISTICS_FIELD(recovering),
126 STATISTICS_FIELD(client_packets_sent),
127 STATISTICS_FIELD(client_packets_recv),
128 STATISTICS_FIELD(node_packets_sent),
129 STATISTICS_FIELD(node_packets_recv),
130 STATISTICS_FIELD(keepalive_packets_sent),
131 STATISTICS_FIELD(keepalive_packets_recv),
132 STATISTICS_FIELD(node.req_call),
133 STATISTICS_FIELD(node.reply_call),
134 STATISTICS_FIELD(node.req_dmaster),
135 STATISTICS_FIELD(node.reply_dmaster),
136 STATISTICS_FIELD(node.reply_error),
137 STATISTICS_FIELD(node.req_message),
138 STATISTICS_FIELD(node.req_control),
139 STATISTICS_FIELD(node.reply_control),
140 STATISTICS_FIELD(client.req_call),
141 STATISTICS_FIELD(client.req_message),
142 STATISTICS_FIELD(client.req_control),
143 STATISTICS_FIELD(timeouts.call),
144 STATISTICS_FIELD(timeouts.control),
145 STATISTICS_FIELD(timeouts.traverse),
146 STATISTICS_FIELD(total_calls),
147 STATISTICS_FIELD(pending_calls),
148 STATISTICS_FIELD(lockwait_calls),
149 STATISTICS_FIELD(pending_lockwait_calls),
150 STATISTICS_FIELD(childwrite_calls),
151 STATISTICS_FIELD(pending_childwrite_calls),
152 STATISTICS_FIELD(memory_used),
153 STATISTICS_FIELD(max_hop_count),
155 printf("CTDB version %u\n", CTDB_VERSION);
156 for (i=0;i<ARRAY_SIZE(fields);i++) {
157 if (strchr(fields[i].name, '.')) {
158 preflen = strcspn(fields[i].name, ".")+1;
159 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
160 prefix = fields[i].name;
161 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
166 printf(" %*s%-22s%*s%10u\n",
168 fields[i].name+preflen,
170 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
172 printf(" %-30s %.6f sec\n", "max_call_latency", s->max_call_latency);
173 printf(" %-30s %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
174 printf(" %-30s %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
175 talloc_free(tmp_ctx);
179 display remote ctdb statistics combined from all nodes
181 static int control_statistics_all(struct ctdb_context *ctdb)
184 struct ctdb_statistics statistics;
188 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
189 CTDB_NO_MEMORY(ctdb, nodes);
191 ZERO_STRUCT(statistics);
193 for (i=0;i<num_nodes;i++) {
194 struct ctdb_statistics s1;
196 uint32_t *v1 = (uint32_t *)&s1;
197 uint32_t *v2 = (uint32_t *)&statistics;
199 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
200 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
202 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
205 for (j=0;j<num_ints;j++) {
208 statistics.max_hop_count =
209 MAX(statistics.max_hop_count, s1.max_hop_count);
210 statistics.max_call_latency =
211 MAX(statistics.max_call_latency, s1.max_call_latency);
212 statistics.max_lockwait_latency =
213 MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
216 printf("Gathered statistics for %u nodes\n", num_nodes);
217 show_statistics(&statistics);
222 display remote ctdb statistics
224 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
227 struct ctdb_statistics statistics;
229 if (options.pnn == CTDB_BROADCAST_ALL) {
230 return control_statistics_all(ctdb);
233 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
235 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
238 show_statistics(&statistics);
244 reset remote ctdb statistics
246 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
250 ret = ctdb_statistics_reset(ctdb, options.pnn);
252 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
260 display uptime of remote node
262 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
266 struct ctdb_uptime *uptime = NULL;
267 int tmp, days, hours, minutes, seconds;
269 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
274 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
276 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
280 if (options.machinereadable){
281 printf(":Current Node Time:Ctdb Start Time:Last Recovery Time:Last Recovery Duration:\n");
282 printf(":%u:%u:%u:%lf\n",
283 (unsigned int)uptime->current_time.tv_sec,
284 (unsigned int)uptime->ctdbd_start_time.tv_sec,
285 (unsigned int)uptime->last_recovery_finished.tv_sec,
286 timeval_delta(&uptime->last_recovery_finished,
287 &uptime->last_recovery_started)
292 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
294 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
302 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
304 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
312 printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
314 printf("Duration of last recovery : %lf seconds\n",
315 timeval_delta(&uptime->last_recovery_finished,
316 &uptime->last_recovery_started));
322 show the PNN of the current node
324 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
328 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
330 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node."));
334 printf("PNN:%d\n", mypnn);
339 display remote ctdb status
341 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
344 struct ctdb_vnn_map *vnnmap=NULL;
345 struct ctdb_node_map *nodemap=NULL;
346 uint32_t recmode, recmaster;
349 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
354 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
356 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
360 if(options.machinereadable){
361 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
362 for(i=0;i<nodemap->num;i++){
363 printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
364 ctdb_addr_to_str(&nodemap->nodes[i].addr),
365 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
366 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
367 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
368 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
373 printf("Number of nodes:%d\n", nodemap->num);
374 for(i=0;i<nodemap->num;i++){
375 static const struct {
379 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
380 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
381 { NODE_FLAGS_BANNED, "BANNED" },
382 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
384 char *flags_str = NULL;
386 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
387 if (nodemap->nodes[i].flags & flag_names[j].flag) {
388 if (flags_str == NULL) {
389 flags_str = talloc_strdup(ctdb, flag_names[j].name);
391 flags_str = talloc_asprintf_append(flags_str, "|%s",
394 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
397 if (flags_str == NULL) {
398 flags_str = talloc_strdup(ctdb, "OK");
399 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
401 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
402 ctdb_addr_to_str(&nodemap->nodes[i].addr),
404 nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
405 talloc_free(flags_str);
408 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
410 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
413 if (vnnmap->generation == INVALID_GENERATION) {
414 printf("Generation:INVALID\n");
416 printf("Generation:%d\n",vnnmap->generation);
418 printf("Size:%d\n",vnnmap->size);
419 for(i=0;i<vnnmap->size;i++){
420 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
423 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
425 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
428 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
430 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
432 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
435 printf("Recovery master:%d\n",recmaster);
441 display the pnn of the recovery master
443 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
448 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
450 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
453 printf("%d\n",recmaster);
459 get a list of all tickles for this pnn
461 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
463 struct ctdb_control_tcp_tickle_list *list;
471 if (parse_ip(argv[0], &addr) == 0) {
472 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
476 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
478 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
482 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
483 printf("Num tickles:%u\n", list->tickles.num);
484 for (i=0;i<list->tickles.num;i++) {
485 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
486 printf("DST: %s:%u\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
494 /* send a release ip to all nodes */
495 static int control_send_release(struct ctdb_context *ctdb, uint32_t pnn,
496 ctdb_sock_addr *addr)
499 struct ctdb_public_ip pip;
501 struct ctdb_node_map *nodemap=NULL;
503 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
505 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
509 /* send a moveip message to the recovery master */
512 data.dsize = sizeof(pip);
513 data.dptr = (unsigned char *)&pip;
516 /* send release ip to all nodes */
517 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
518 list_of_active_nodes(ctdb, nodemap, ctdb, true),
519 TIMELIMIT(), false, data,
520 NULL, NULL, NULL) != 0) {
521 DEBUG(DEBUG_ERR, (__location__ " Unable to send 'ReleaseIP' to all nodes.\n"));
529 move/failover an ip address to a specific node
531 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
536 struct ctdb_all_public_ips *ips;
543 if (parse_ip(argv[0], &addr) == 0) {
544 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
549 if (sscanf(argv[1], "%u", &pnn) != 1) {
550 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
554 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "DeterministicIPs", &value);
556 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'DeterministicIPs' from local node\n"));
560 DEBUG(DEBUG_ERR, ("The tunable 'DeterministicIPs' is set. You can only move ip addresses when this feature is disabled\n"));
564 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "NoIPFailback", &value);
566 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'NoIPFailback' from local node\n"));
570 DEBUG(DEBUG_ERR, ("The tunable 'NoIPFailback' is NOT set. You can only move ip addresses when this feature is enabled\n"));
574 /* read the public ip list from the node */
575 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
577 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
581 for (i=0;i<ips->num;i++) {
582 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
587 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
588 pnn, ctdb_addr_to_str(&addr)));
591 if (ips->ips[i].pnn == pnn) {
592 DEBUG(DEBUG_ERR, ("Host %u is already hosting '%s'\n",
593 pnn, ctdb_addr_to_str(&ips->ips[i].addr)));
597 ret = control_send_release(ctdb, pnn, &ips->ips[i].addr);
599 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));;
606 void getips_store_callback(void *param, void *data)
608 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
609 struct ctdb_all_public_ips *ips = param;
613 ips->ips[i].pnn = node_ip->pnn;
614 ips->ips[i].addr = node_ip->addr;
617 void getips_count_callback(void *param, void *data)
619 uint32_t *count = param;
625 static uint32_t *ip_key(ctdb_sock_addr *ip)
627 static uint32_t key[IP_KEYLEN];
629 bzero(key, sizeof(key));
631 switch (ip->sa.sa_family) {
633 key[0] = ip->ip.sin_addr.s_addr;
636 key[0] = ip->ip6.sin6_addr.s6_addr32[3];
637 key[1] = ip->ip6.sin6_addr.s6_addr32[2];
638 key[2] = ip->ip6.sin6_addr.s6_addr32[1];
639 key[3] = ip->ip6.sin6_addr.s6_addr32[0];
642 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
649 static void *add_ip_callback(void *parm, void *data)
655 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
657 struct ctdb_all_public_ips *tmp_ips;
658 struct ctdb_node_map *nodemap=NULL;
659 trbt_tree_t *ip_tree;
663 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
665 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
669 ip_tree = trbt_create(tmp_ctx, 0);
671 for(i=0;i<nodemap->num;i++){
672 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
676 /* read the public ip list from this node */
677 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
679 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
683 for (j=0; j<tmp_ips->num;j++) {
684 struct ctdb_public_ip *node_ip;
686 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
687 node_ip->pnn = tmp_ips->ips[j].pnn;
688 node_ip->addr = tmp_ips->ips[j].addr;
690 trbt_insertarray32_callback(ip_tree,
691 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
695 talloc_free(tmp_ips);
700 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
702 len = offsetof(struct ctdb_all_public_ips, ips) +
703 count*sizeof(struct ctdb_public_ip);
704 tmp_ips = talloc_zero_size(tmp_ctx, len);
705 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
714 * scans all other nodes and returns a pnn for another node that can host this
718 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
720 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
721 struct ctdb_all_public_ips *ips;
722 struct ctdb_node_map *nodemap=NULL;
725 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
727 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
728 talloc_free(tmp_ctx);
732 for(i=0;i<nodemap->num;i++){
733 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
736 if (nodemap->nodes[i].pnn == options.pnn) {
740 /* read the public ip list from this node */
741 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
743 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
747 for (j=0;j<ips->num;j++) {
748 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
749 talloc_free(tmp_ctx);
750 return nodemap->nodes[i].pnn;
756 talloc_free(tmp_ctx);
761 add a public ip address to a node
763 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
769 struct ctdb_control_ip_iface *pub;
770 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
771 struct ctdb_all_public_ips *ips;
774 talloc_free(tmp_ctx);
778 if (!parse_ip_mask(argv[0], &addr, &mask)) {
779 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
780 talloc_free(tmp_ctx);
784 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
786 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
787 talloc_free(tmp_ctx);
792 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
793 pub = talloc_size(tmp_ctx, len);
794 CTDB_NO_MEMORY(ctdb, pub);
798 pub->len = strlen(argv[1])+1;
799 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
801 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
803 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
804 talloc_free(tmp_ctx);
809 /* check if some other node is already serving this ip, if not,
812 for (i=0;i<ips->num;i++) {
813 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
817 /* no one has this ip so we claim it */
819 ret = control_send_release(ctdb, options.pnn, &addr);
821 ret = control_send_release(ctdb, ips->ips[i].pnn, &addr);
825 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));
829 talloc_free(tmp_ctx);
833 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
835 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
837 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
838 struct ctdb_node_map *nodemap=NULL;
839 struct ctdb_all_public_ips *ips;
842 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
844 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
848 /* remove it from the nodes that are not hosting the ip currently */
849 for(i=0;i<nodemap->num;i++){
850 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
853 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
854 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
858 for (j=0;j<ips->num;j++) {
859 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
867 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
871 options.pnn = nodemap->nodes[i].pnn;
872 control_delip(ctdb, argc, argv);
876 /* remove it from every node (also the one hosting it) */
877 for(i=0;i<nodemap->num;i++){
878 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
881 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
882 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
886 for (j=0;j<ips->num;j++) {
887 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
895 options.pnn = nodemap->nodes[i].pnn;
896 control_delip(ctdb, argc, argv);
899 talloc_free(tmp_ctx);
904 delete a public ip address from a node
906 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
910 struct ctdb_control_ip_iface pub;
911 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
912 struct ctdb_all_public_ips *ips;
915 talloc_free(tmp_ctx);
919 if (parse_ip(argv[0], &addr) == 0) {
920 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
924 if (options.pnn == CTDB_BROADCAST_ALL) {
925 return control_delip_all(ctdb, argc, argv, &addr);
932 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
934 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
935 talloc_free(tmp_ctx);
939 for (i=0;i<ips->num;i++) {
940 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
946 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
947 ctdb_addr_to_str(&addr)));
948 talloc_free(tmp_ctx);
952 if (ips->ips[i].pnn == options.pnn) {
953 ret = find_other_host_for_public_ip(ctdb, &addr);
955 ret = control_send_release(ctdb, ret, &addr);
957 DEBUG(DEBUG_ERR, ("Failed to migrate this ip to another node. Use moveip of recover to reassign this address to a node\n"));
962 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
964 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
965 talloc_free(tmp_ctx);
969 talloc_free(tmp_ctx);
974 kill a tcp connection
976 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
979 struct ctdb_control_killtcp killtcp;
985 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
986 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
990 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
991 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
995 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
997 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1008 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1011 ctdb_sock_addr addr;
1017 if (!parse_ip(argv[0], &addr)) {
1018 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1022 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1024 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1032 register a server id
1034 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1037 struct ctdb_server_id server_id;
1043 server_id.pnn = strtoul(argv[0], NULL, 0);
1044 server_id.type = strtoul(argv[1], NULL, 0);
1045 server_id.server_id = strtoul(argv[2], NULL, 0);
1047 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1049 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1056 unregister a server id
1058 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1061 struct ctdb_server_id server_id;
1067 server_id.pnn = strtoul(argv[0], NULL, 0);
1068 server_id.type = strtoul(argv[1], NULL, 0);
1069 server_id.server_id = strtoul(argv[2], NULL, 0);
1071 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
1073 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
1080 check if a server id exists
1082 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1086 struct ctdb_server_id server_id;
1092 server_id.pnn = strtoul(argv[0], NULL, 0);
1093 server_id.type = strtoul(argv[1], NULL, 0);
1094 server_id.server_id = strtoul(argv[2], NULL, 0);
1096 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
1098 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
1103 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
1105 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
1111 get a list of all server ids that are registered on a node
1113 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
1116 struct ctdb_server_id_list *server_ids;
1118 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
1120 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
1124 for (i=0; i<server_ids->num; i++) {
1125 printf("Server id %d:%d:%d\n",
1126 server_ids->server_ids[i].pnn,
1127 server_ids->server_ids[i].type,
1128 server_ids->server_ids[i].server_id);
1135 send a tcp tickle ack
1137 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1140 ctdb_sock_addr src, dst;
1146 if (!parse_ip_port(argv[0], &src)) {
1147 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1151 if (!parse_ip_port(argv[1], &dst)) {
1152 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1156 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
1160 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
1167 display public ip status
1169 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
1172 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1173 struct ctdb_all_public_ips *ips;
1175 if (options.pnn == CTDB_BROADCAST_ALL) {
1176 /* read the list of public ips from all nodes */
1177 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1179 /* read the public ip list from this node */
1180 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1183 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
1184 talloc_free(tmp_ctx);
1188 if (options.machinereadable){
1189 printf(":Public IP:Node:\n");
1191 if (options.pnn == CTDB_BROADCAST_ALL) {
1192 printf("Public IPs on ALL nodes\n");
1194 printf("Public IPs on node %u\n", options.pnn);
1198 for (i=1;i<=ips->num;i++) {
1199 if (options.machinereadable){
1200 printf(":%s:%d:\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1202 printf("%s %d\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1206 talloc_free(tmp_ctx);
1211 display pid of a ctdb daemon
1213 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
1218 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
1220 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
1223 printf("Pid:%d\n", pid);
1229 disable a remote node
1231 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
1235 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
1237 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
1245 enable a disabled remote node
1247 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
1251 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
1253 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
1260 static uint32_t get_generation(struct ctdb_context *ctdb)
1262 struct ctdb_vnn_map *vnnmap=NULL;
1265 /* wait until the recmaster is not in recovery mode */
1267 uint32_t recmode, recmaster;
1269 if (vnnmap != NULL) {
1270 talloc_free(vnnmap);
1274 /* get the recmaster */
1275 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
1277 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1281 /* get recovery mode */
1282 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
1284 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1288 /* get the current generation number */
1289 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
1291 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
1295 if ((recmode == CTDB_RECOVERY_NORMAL)
1296 && (vnnmap->generation != 1)){
1297 return vnnmap->generation;
1304 ban a node from the cluster
1306 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
1309 struct ctdb_ban_info b;
1312 struct ctdb_node_map *nodemap=NULL;
1313 uint32_t generation, next_generation;
1319 /* record the current generation number */
1320 generation = get_generation(ctdb);
1323 /* verify the node exists */
1324 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
1326 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1329 if (options.pnn >= nodemap->num) {
1330 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", options.pnn));
1334 /* verify we can access the node */
1335 ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1337 DEBUG(DEBUG_ERR,("Can not ban node. Node is not operational.\n"));
1341 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
1342 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
1346 ban_time = strtoul(argv[0], NULL, 0);
1348 b.pnn = options.pnn;
1349 b.ban_time = ban_time;
1351 data.dptr = (uint8_t *)&b;
1352 data.dsize = sizeof(b);
1354 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
1356 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
1360 /* wait until we are in a new generation */
1362 next_generation = get_generation(ctdb);
1363 if (next_generation != generation) {
1374 unban a node from the cluster
1376 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
1380 uint32_t generation, next_generation;
1382 /* record the current generation number */
1383 generation = get_generation(ctdb);
1385 /* verify we can access the node */
1386 ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1388 DEBUG(DEBUG_ERR,("Can not unban node. Node is not operational.\n"));
1392 data.dptr = (uint8_t *)&options.pnn;
1393 data.dsize = sizeof(uint32_t);
1395 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
1397 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
1401 /* wait until we are in a new generation */
1403 next_generation = get_generation(ctdb);
1404 if (next_generation != generation) {
1417 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
1421 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
1423 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
1433 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
1436 uint32_t generation, next_generation;
1438 /* record the current generation number */
1439 generation = get_generation(ctdb);
1441 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1443 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
1447 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
1449 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
1453 /* wait until we are in a new generation */
1455 next_generation = get_generation(ctdb);
1456 if (next_generation != generation) {
1467 display monitoring mode of a remote node
1469 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
1474 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
1476 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
1479 if (!options.machinereadable){
1480 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
1483 printf(":%d:\n",monmode);
1490 display capabilities of a remote node
1492 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
1494 uint32_t capabilities;
1497 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
1499 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
1503 if (!options.machinereadable){
1504 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
1505 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
1506 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
1508 printf(":RECMASTER:LMASTER:LVS:\n");
1509 printf(":%d:%d:%d:\n",
1510 !!(capabilities&CTDB_CAP_RECMASTER),
1511 !!(capabilities&CTDB_CAP_LMASTER),
1512 !!(capabilities&CTDB_CAP_LVS));
1518 display lvs configuration
1520 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
1522 uint32_t *capabilities;
1523 struct ctdb_node_map *nodemap=NULL;
1525 int healthy_count = 0;
1527 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1529 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1533 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1534 CTDB_NO_MEMORY(ctdb, capabilities);
1536 /* collect capabilities for all connected nodes */
1537 for (i=0; i<nodemap->num; i++) {
1538 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1541 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1545 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1547 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1551 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1555 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1560 /* Print all LVS nodes */
1561 for (i=0; i<nodemap->num; i++) {
1562 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1565 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1568 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1572 if (healthy_count != 0) {
1573 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1578 printf("%d:%s\n", i,
1579 ctdb_addr_to_str(&nodemap->nodes[i].addr));
1586 display who is the lvs master
1588 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1590 uint32_t *capabilities;
1591 struct ctdb_node_map *nodemap=NULL;
1593 int healthy_count = 0;
1595 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1597 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1601 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1602 CTDB_NO_MEMORY(ctdb, capabilities);
1604 /* collect capabilities for all connected nodes */
1605 for (i=0; i<nodemap->num; i++) {
1606 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1609 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1613 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1615 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1619 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1623 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1628 /* find and show the lvsmaster */
1629 for (i=0; i<nodemap->num; i++) {
1630 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1633 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1636 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1640 if (healthy_count != 0) {
1641 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1646 printf("Node %d is LVS master\n", i);
1650 printf("There is no LVS master\n");
1655 disable monitoring on a node
1657 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1662 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
1664 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
1667 printf("Monitoring mode:%s\n","DISABLED");
1673 enable monitoring on a node
1675 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1680 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
1682 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
1685 printf("Monitoring mode:%s\n","ACTIVE");
1691 display remote list of keys/data for a db
1693 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
1695 const char *db_name;
1696 struct ctdb_db_context *ctdb_db;
1706 if (db_exists(ctdb, db_name)) {
1707 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
1711 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
1713 if (ctdb_db == NULL) {
1714 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1718 /* traverse and dump the cluster tdb */
1719 ret = ctdb_dump_db(ctdb_db, stdout);
1721 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
1724 talloc_free(ctdb_db);
1726 printf("Dumped %d records\n", ret);
1732 display a list of the databases on a remote ctdb
1734 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
1737 struct ctdb_dbid_map *dbmap=NULL;
1739 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
1741 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
1745 printf("Number of databases:%d\n", dbmap->num);
1746 for(i=0;i<dbmap->num;i++){
1751 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
1752 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
1753 persistent = dbmap->dbs[i].persistent;
1754 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name,
1755 path, persistent?"PERSISTENT":"");
1762 check if the local node is recmaster or not
1763 it will return 1 if this node is the recmaster and 0 if it is not
1764 or if the local ctdb daemon could not be contacted
1766 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1768 uint32_t mypnn, recmaster;
1771 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1773 printf("Failed to get pnn of node\n");
1777 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1779 printf("Failed to get the recmaster\n");
1783 if (recmaster != mypnn) {
1784 printf("this node is not the recmaster\n");
1788 printf("this node is the recmaster\n");
1795 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
1798 struct timeval tv = timeval_current();
1799 ret = ctdb_ctrl_ping(ctdb, options.pnn);
1801 printf("Unable to get ping response from node %u\n", options.pnn);
1804 printf("response from %u time=%.6f sec (%d clients)\n",
1805 options.pnn, timeval_elapsed(&tv), ret);
1814 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
1825 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
1827 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
1831 printf("%-19s = %u\n", name, value);
1838 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
1849 value = strtoul(argv[1], NULL, 0);
1851 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
1853 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
1862 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
1868 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
1870 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
1874 for (i=0;i<count;i++) {
1875 control_getvar(ctdb, 1, &list[i]);
1885 const char *description;
1886 } debug_levels[] = {
1887 {DEBUG_EMERG, "EMERG"},
1888 {DEBUG_ALERT, "ALERT"},
1889 {DEBUG_CRIT, "CRIT"},
1891 {DEBUG_WARNING, "WARNING"},
1892 {DEBUG_NOTICE, "NOTICE"},
1893 {DEBUG_INFO, "INFO"},
1894 {DEBUG_DEBUG, "DEBUG"}
1897 static const char *get_debug_by_level(int32_t level)
1901 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1902 if (debug_levels[i].level == level) {
1903 return debug_levels[i].description;
1909 static int32_t get_debug_by_desc(const char *desc)
1913 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1914 if (!strcmp(debug_levels[i].description, desc)) {
1915 return debug_levels[i].level;
1919 fprintf(stderr, "Invalid debug level '%s'\nMust be one of\n", desc);
1920 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1921 fprintf(stderr, " %s\n", debug_levels[i].description);
1928 display debug level on a node
1930 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1935 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1937 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
1940 if (options.machinereadable){
1941 printf(":Name:Level:\n");
1942 printf(":%s:%d:\n",get_debug_by_level(level),level);
1944 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
1952 set debug level on a node or all nodes
1954 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1963 if (isalpha(argv[0][0])) {
1964 level = get_debug_by_desc(argv[0]);
1966 level = strtol(argv[0], NULL, 0);
1969 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1971 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1980 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1984 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1986 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
1994 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
1998 ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
2000 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
2007 attach to a database
2009 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
2011 const char *db_name;
2012 struct ctdb_db_context *ctdb_db;
2019 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
2020 if (ctdb_db == NULL) {
2021 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2029 run an eventscript on a node
2031 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
2037 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2040 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2044 data.dptr = (unsigned char *)discard_const(argv[0]);
2045 data.dsize = strlen((char *)data.dptr) + 1;
2047 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
2049 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
2050 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
2051 if (ret != 0 || res != 0) {
2052 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
2053 talloc_free(tmp_ctx);
2056 talloc_free(tmp_ctx);
2060 #define DB_VERSION 1
2061 #define MAX_DB_NAME 64
2062 struct db_file_header {
2063 unsigned long version;
2065 unsigned long persistent;
2067 const char name[MAX_DB_NAME];
2070 struct backup_data {
2071 struct ctdb_marshall_buffer *records;
2074 bool traverse_error;
2077 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
2079 struct backup_data *bd = talloc_get_type(private, struct backup_data);
2080 struct ctdb_rec_data *rec;
2082 /* add the record */
2083 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
2085 bd->traverse_error = true;
2086 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
2089 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
2090 if (bd->records == NULL) {
2091 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
2092 bd->traverse_error = true;
2095 bd->records->count++;
2096 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
2097 bd->len += rec->length;
2105 * backup a database to a file
2107 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
2110 struct ctdb_dbid_map *dbmap=NULL;
2111 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2112 struct db_file_header dbhdr;
2113 struct ctdb_db_context *ctdb_db;
2114 struct backup_data *bd;
2118 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2122 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
2124 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
2128 for(i=0;i<dbmap->num;i++){
2131 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
2132 if(!strcmp(argv[0], name)){
2133 talloc_free(discard_const(name));
2136 talloc_free(discard_const(name));
2138 if (i == dbmap->num) {
2139 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
2140 talloc_free(tmp_ctx);
2145 ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
2146 if (ctdb_db == NULL) {
2147 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
2152 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
2154 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
2155 talloc_free(tmp_ctx);
2160 bd = talloc_zero(tmp_ctx, struct backup_data);
2162 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
2163 talloc_free(tmp_ctx);
2167 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
2168 if (bd->records == NULL) {
2169 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
2170 talloc_free(tmp_ctx);
2174 bd->len = offsetof(struct ctdb_marshall_buffer, data);
2175 bd->records->db_id = ctdb_db->db_id;
2176 /* traverse the database collecting all records */
2177 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
2178 bd->traverse_error) {
2179 DEBUG(DEBUG_ERR,("Traverse error\n"));
2180 talloc_free(tmp_ctx);
2184 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
2187 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
2189 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
2190 talloc_free(tmp_ctx);
2194 dbhdr.version = DB_VERSION;
2195 dbhdr.timestamp = time(NULL);
2196 dbhdr.persistent = dbmap->dbs[i].persistent;
2197 dbhdr.size = bd->len;
2198 if (strlen(argv[0]) >= MAX_DB_NAME) {
2199 DEBUG(DEBUG_ERR,("Too long dbname\n"));
2200 talloc_free(tmp_ctx);
2203 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
2204 write(fh, &dbhdr, sizeof(dbhdr));
2205 write(fh, bd->records, bd->len);
2208 talloc_free(tmp_ctx);
2213 * restore a database from a file
2215 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
2218 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2221 struct db_file_header dbhdr;
2222 struct ctdb_db_context *ctdb_db;
2223 struct ctdb_node_map *nodemap=NULL;
2224 struct ctdb_vnn_map *vnnmap=NULL;
2226 struct ctdb_control_wipe_database w;
2228 uint32_t generation;
2233 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2237 fh = open(argv[0], O_RDONLY);
2239 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
2240 talloc_free(tmp_ctx);
2244 read(fh, &dbhdr, sizeof(dbhdr));
2245 if (dbhdr.version != DB_VERSION) {
2246 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
2247 talloc_free(tmp_ctx);
2251 outdata.dsize = dbhdr.size;
2252 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
2253 if (outdata.dptr == NULL) {
2254 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
2256 talloc_free(tmp_ctx);
2259 read(fh, outdata.dptr, outdata.dsize);
2262 tm = localtime(&dbhdr.timestamp);
2263 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
2264 printf("Restoring database '%s' from backup @ %s\n",
2268 ctdb_db = ctdb_attach(ctdb, dbhdr.name, dbhdr.persistent, 0);
2269 if (ctdb_db == NULL) {
2270 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbhdr.name));
2271 talloc_free(tmp_ctx);
2275 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2277 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2278 talloc_free(tmp_ctx);
2283 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
2285 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
2286 talloc_free(tmp_ctx);
2290 /* freeze all nodes */
2291 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2292 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
2297 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
2298 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2299 talloc_free(tmp_ctx);
2303 generation = vnnmap->generation;
2304 data.dptr = (void *)&generation;
2305 data.dsize = sizeof(generation);
2307 /* start a cluster wide transaction */
2308 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2309 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
2311 TIMELIMIT(), false, data,
2314 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
2319 w.db_id = ctdb_db->db_id;
2320 w.transaction_id = generation;
2322 data.dptr = (void *)&w;
2323 data.dsize = sizeof(w);
2325 /* wipe all the remote databases. */
2326 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2327 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
2329 TIMELIMIT(), false, data,
2332 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
2333 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2334 talloc_free(tmp_ctx);
2338 /* push the database */
2339 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2340 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
2342 TIMELIMIT(), false, outdata,
2345 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
2346 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2347 talloc_free(tmp_ctx);
2351 data.dptr = (void *)&generation;
2352 data.dsize = sizeof(generation);
2354 /* commit all the changes */
2355 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
2357 TIMELIMIT(), false, data,
2360 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
2361 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2362 talloc_free(tmp_ctx);
2367 /* thaw all nodes */
2368 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2369 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
2374 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
2375 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2376 talloc_free(tmp_ctx);
2381 talloc_free(tmp_ctx);
2386 * set flags of a node in the nodemap
2388 static int control_setflags(struct ctdb_context *ctdb, int argc, const char **argv)
2395 struct ctdb_node_flag_change c;
2402 if (sscanf(argv[0], "%d", &node) != 1) {
2403 DEBUG(DEBUG_ERR, ("Badly formed node\n"));
2407 if (sscanf(argv[1], "0x%x", &flags) != 1) {
2408 DEBUG(DEBUG_ERR, ("Badly formed flags\n"));
2415 c.new_flags = flags;
2417 data.dsize = sizeof(c);
2418 data.dptr = (unsigned char *)&c;
2420 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_MODIFY_FLAGS, 0,
2421 data, NULL, NULL, &status, NULL, NULL);
2422 if (ret != 0 || status != 0) {
2423 DEBUG(DEBUG_ERR,("Failed to modify flags\n"));
2432 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2438 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2439 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
2440 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
2441 if (ret != 0 || res != 0) {
2442 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
2443 talloc_free(tmp_ctx);
2446 write(1, data.dptr, data.dsize);
2447 talloc_free(tmp_ctx);
2452 handler for memory dumps
2454 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
2455 TDB_DATA data, void *private_data)
2457 write(1, data.dptr, data.dsize);
2462 dump memory usage on the recovery daemon
2464 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2468 struct rd_memdump_reply rd;
2470 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2472 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
2475 rd.srvid = getpid();
2477 /* register a message port for receiveing the reply so that we
2478 can receive the reply
2480 ctdb_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
2483 data.dptr = (uint8_t *)&rd;
2484 data.dsize = sizeof(rd);
2486 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
2488 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
2492 /* this loop will terminate when we have received the reply */
2494 event_loop_once(ctdb->ev);
2501 list all nodes in the cluster
2503 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
2506 struct ctdb_node_map *nodemap=NULL;
2508 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2510 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2514 for(i=0;i<nodemap->num;i++){
2515 printf("%s\n", ctdb_addr_to_str(&nodemap->nodes[i].addr));
2522 reload the nodes file on the local node
2524 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
2528 struct ctdb_node_map *nodemap=NULL;
2530 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2532 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
2536 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2538 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2542 /* reload the nodes file on all remote nodes */
2543 for (i=0;i<nodemap->num;i++) {
2544 if (nodemap->nodes[i].pnn == mypnn) {
2547 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
2548 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
2549 nodemap->nodes[i].pnn);
2551 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
2555 /* reload the nodes file on the local node */
2556 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
2557 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
2559 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
2562 /* initiate a recovery */
2563 control_recover(ctdb, argc, argv);
2569 static const struct {
2571 int (*fn)(struct ctdb_context *, int, const char **);
2575 } ctdb_commands[] = {
2577 { "version", control_version, true, "show version of ctdb" },
2579 { "status", control_status, true, "show node status" },
2580 { "uptime", control_uptime, true, "show node uptime" },
2581 { "ping", control_ping, true, "ping all nodes" },
2582 { "getvar", control_getvar, true, "get a tunable variable", "<name>"},
2583 { "setvar", control_setvar, true, "set a tunable variable", "<name> <value>"},
2584 { "listvars", control_listvars, true, "list tunable variables"},
2585 { "statistics", control_statistics, false, "show statistics" },
2586 { "statisticsreset", control_statistics_reset, true, "reset statistics"},
2587 { "ip", control_ip, false, "show which public ip's that ctdb manages" },
2588 { "process-exists", control_process_exists, true, "check if a process exists on a node", "<pid>"},
2589 { "getdbmap", control_getdbmap, true, "show the database map" },
2590 { "catdb", control_catdb, true, "dump a database" , "<dbname>"},
2591 { "getmonmode", control_getmonmode, true, "show monitoring mode" },
2592 { "getcapabilities", control_getcapabilities, true, "show node capabilities" },
2593 { "pnn", control_pnn, true, "show the pnn of the currnet node" },
2594 { "lvs", control_lvs, true, "show lvs configuration" },
2595 { "lvsmaster", control_lvsmaster, true, "show which node is the lvs master" },
2596 { "disablemonitor", control_disable_monmode, true, "set monitoring mode to DISABLE" },
2597 { "enablemonitor", control_enable_monmode, true, "set monitoring mode to ACTIVE" },
2598 { "setdebug", control_setdebug, true, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
2599 { "getdebug", control_getdebug, true, "get debug level" },
2600 { "attach", control_attach, true, "attach to a database", "<dbname>" },
2601 { "dumpmemory", control_dumpmemory, true, "dump memory map to stdout" },
2602 { "rddumpmemory", control_rddumpmemory, true, "dump memory map from the recovery daemon to stdout" },
2603 { "getpid", control_getpid, true, "get ctdbd process ID" },
2604 { "disable", control_disable, true, "disable a nodes public IP" },
2605 { "enable", control_enable, true, "enable a nodes public IP" },
2606 { "ban", control_ban, true, "ban a node from the cluster", "<bantime|0>"},
2607 { "unban", control_unban, true, "unban a node from the cluster" },
2608 { "shutdown", control_shutdown, true, "shutdown ctdbd" },
2609 { "recover", control_recover, true, "force recovery" },
2610 { "freeze", control_freeze, true, "freeze all databases" },
2611 { "thaw", control_thaw, true, "thaw all databases" },
2612 { "isnotrecmaster", control_isnotrecmaster, false, "check if the local node is recmaster or not" },
2613 { "killtcp", kill_tcp, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
2614 { "gratiousarp", control_gratious_arp, false, "send a gratious arp", "<ip> <interface>" },
2615 { "tickle", tickle_tcp, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
2616 { "gettickles", control_get_tickles, false, "get the list of tickles registered for this ip", "<ip>" },
2618 { "regsrvid", regsrvid, false, "register a server id", "<pnn> <type> <id>" },
2619 { "unregsrvid", unregsrvid, false, "unregister a server id", "<pnn> <type> <id>" },
2620 { "chksrvid", chksrvid, false, "check if a server id exists", "<pnn> <type> <id>" },
2621 { "getsrvids", getsrvids, false, "get a list of all server ids"},
2622 { "vacuum", ctdb_vacuum, false, "vacuum the databases of empty records", "[max_records]"},
2623 { "repack", ctdb_repack, false, "repack all databases", "[max_freelist]"},
2624 { "listnodes", control_listnodes, false, "list all nodes in the cluster"},
2625 { "reloadnodes", control_reload_nodes_file, false, "reload the nodes file and restart the transport on all nodes"},
2626 { "moveip", control_moveip, false, "move/failover an ip address to another node", "<ip> <node>"},
2627 { "addip", control_addip, true, "add a ip address to a node", "<ip/mask> <iface>"},
2628 { "delip", control_delip, false, "delete an ip address from a node", "<ip>"},
2629 { "eventscript", control_eventscript, true, "run the eventscript with the given parameters on a node", "<arguments>"},
2630 { "backupdb", control_backupdb, false, "backup the database into a file.", "<database> <file>"},
2631 { "restoredb", control_restoredb, false, "restore the database from a file.", "<file>"},
2632 { "recmaster", control_recmaster, false, "show the pnn for the recovery master."},
2633 { "setflags", control_setflags, false, "set flags for a node in the nodemap.", "<node> <flags>"},
2639 static void usage(void)
2643 "Usage: ctdb [options] <control>\n" \
2645 " -n <node> choose node number, or 'all' (defaults to local node)\n"
2646 " -Y generate machinereadable output\n"
2647 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
2648 printf("Controls:\n");
2649 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2650 printf(" %-15s %-27s %s\n",
2651 ctdb_commands[i].name,
2652 ctdb_commands[i].args?ctdb_commands[i].args:"",
2653 ctdb_commands[i].msg);
2659 static void ctdb_alarm(int sig)
2661 printf("Maximum runtime exceeded - exiting\n");
2668 int main(int argc, const char *argv[])
2670 struct ctdb_context *ctdb;
2671 char *nodestring = NULL;
2672 struct poptOption popt_options[] = {
2675 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
2676 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
2677 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
2678 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
2682 const char **extra_argv;
2686 struct event_context *ev;
2687 const char *control;
2691 /* set some defaults */
2692 options.maxruntime = 0;
2693 options.timelimit = 3;
2694 options.pnn = CTDB_CURRENT_NODE;
2696 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
2698 while ((opt = poptGetNextOpt(pc)) != -1) {
2701 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
2702 poptBadOption(pc, 0), poptStrerror(opt)));
2707 /* setup the remaining options for the main program to use */
2708 extra_argv = poptGetArgs(pc);
2711 while (extra_argv[extra_argc]) extra_argc++;
2714 if (extra_argc < 1) {
2718 if (options.maxruntime == 0) {
2719 const char *ctdb_timeout;
2720 ctdb_timeout = getenv("CTDB_TIMEOUT");
2721 if (ctdb_timeout != NULL) {
2722 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
2725 if (options.maxruntime != 0) {
2726 signal(SIGALRM, ctdb_alarm);
2727 alarm(options.maxruntime);
2730 /* setup the node number to contact */
2731 if (nodestring != NULL) {
2732 if (strcmp(nodestring, "all") == 0) {
2733 options.pnn = CTDB_BROADCAST_ALL;
2735 options.pnn = strtoul(nodestring, NULL, 0);
2739 control = extra_argv[0];
2741 ev = event_context_init(NULL);
2743 /* initialise ctdb */
2744 ctdb = ctdb_cmdline_client(ev);
2746 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
2750 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2751 if (strcmp(control, ctdb_commands[i].name) == 0) {
2754 if (options.pnn == CTDB_CURRENT_NODE) {
2756 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
2763 if (ctdb_commands[i].auto_all &&
2764 options.pnn == CTDB_BROADCAST_ALL) {
2769 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
2770 CTDB_NO_MEMORY(ctdb, nodes);
2772 for (j=0;j<num_nodes;j++) {
2773 options.pnn = nodes[j];
2774 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2778 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2784 if (i == ARRAY_SIZE(ctdb_commands)) {
2785 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));