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"
35 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
36 #define ERR_NONODE 21 /* node does not exist */
37 #define ERR_DISNODE 22 /* node is disconnected */
39 static void usage(void);
48 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
51 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
54 #define XSTR(x) STR(x)
55 printf("CTDB version: %s\n", XSTR(CTDB_VERS));
62 verify that a node exists and is reachable
64 static void verify_node(struct ctdb_context *ctdb)
67 struct ctdb_node_map *nodemap=NULL;
69 if (options.pnn == CTDB_CURRENT_NODE) {
72 if (options.pnn == CTDB_BROADCAST_ALL) {
76 /* verify the node exists */
77 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
78 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
81 if (options.pnn >= nodemap->num) {
82 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", options.pnn));
85 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DISCONNECTED) {
86 DEBUG(DEBUG_ERR, ("Node %u is DISCONNECTED\n", options.pnn));
90 /* verify we can access the node */
91 ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
93 DEBUG(DEBUG_ERR,("Can not ban node. Node is not operational.\n"));
99 check if a database exists
101 static int db_exists(struct ctdb_context *ctdb, const char *db_name)
104 struct ctdb_dbid_map *dbmap=NULL;
106 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
108 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
112 for(i=0;i<dbmap->num;i++){
115 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
116 if (!strcmp(name, db_name)) {
125 see if a process exists
127 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
135 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
136 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
140 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
142 printf("%u:%u exists\n", pnn, pid);
144 printf("%u:%u does not exist\n", pnn, pid);
150 display statistics structure
152 static void show_statistics(struct ctdb_statistics *s)
154 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
156 const char *prefix=NULL;
162 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
163 STATISTICS_FIELD(num_clients),
164 STATISTICS_FIELD(frozen),
165 STATISTICS_FIELD(recovering),
166 STATISTICS_FIELD(client_packets_sent),
167 STATISTICS_FIELD(client_packets_recv),
168 STATISTICS_FIELD(node_packets_sent),
169 STATISTICS_FIELD(node_packets_recv),
170 STATISTICS_FIELD(keepalive_packets_sent),
171 STATISTICS_FIELD(keepalive_packets_recv),
172 STATISTICS_FIELD(node.req_call),
173 STATISTICS_FIELD(node.reply_call),
174 STATISTICS_FIELD(node.req_dmaster),
175 STATISTICS_FIELD(node.reply_dmaster),
176 STATISTICS_FIELD(node.reply_error),
177 STATISTICS_FIELD(node.req_message),
178 STATISTICS_FIELD(node.req_control),
179 STATISTICS_FIELD(node.reply_control),
180 STATISTICS_FIELD(client.req_call),
181 STATISTICS_FIELD(client.req_message),
182 STATISTICS_FIELD(client.req_control),
183 STATISTICS_FIELD(timeouts.call),
184 STATISTICS_FIELD(timeouts.control),
185 STATISTICS_FIELD(timeouts.traverse),
186 STATISTICS_FIELD(total_calls),
187 STATISTICS_FIELD(pending_calls),
188 STATISTICS_FIELD(lockwait_calls),
189 STATISTICS_FIELD(pending_lockwait_calls),
190 STATISTICS_FIELD(childwrite_calls),
191 STATISTICS_FIELD(pending_childwrite_calls),
192 STATISTICS_FIELD(memory_used),
193 STATISTICS_FIELD(max_hop_count),
195 printf("CTDB version %u\n", CTDB_VERSION);
196 for (i=0;i<ARRAY_SIZE(fields);i++) {
197 if (strchr(fields[i].name, '.')) {
198 preflen = strcspn(fields[i].name, ".")+1;
199 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
200 prefix = fields[i].name;
201 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
206 printf(" %*s%-22s%*s%10u\n",
208 fields[i].name+preflen,
210 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
212 printf(" %-30s %.6f sec\n", "max_call_latency", s->max_call_latency);
213 printf(" %-30s %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
214 printf(" %-30s %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
215 talloc_free(tmp_ctx);
219 display remote ctdb statistics combined from all nodes
221 static int control_statistics_all(struct ctdb_context *ctdb)
224 struct ctdb_statistics statistics;
228 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
229 CTDB_NO_MEMORY(ctdb, nodes);
231 ZERO_STRUCT(statistics);
233 for (i=0;i<num_nodes;i++) {
234 struct ctdb_statistics s1;
236 uint32_t *v1 = (uint32_t *)&s1;
237 uint32_t *v2 = (uint32_t *)&statistics;
239 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
240 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
242 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
245 for (j=0;j<num_ints;j++) {
248 statistics.max_hop_count =
249 MAX(statistics.max_hop_count, s1.max_hop_count);
250 statistics.max_call_latency =
251 MAX(statistics.max_call_latency, s1.max_call_latency);
252 statistics.max_lockwait_latency =
253 MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
256 printf("Gathered statistics for %u nodes\n", num_nodes);
257 show_statistics(&statistics);
262 display remote ctdb statistics
264 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
267 struct ctdb_statistics statistics;
269 if (options.pnn == CTDB_BROADCAST_ALL) {
270 return control_statistics_all(ctdb);
273 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
275 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
278 show_statistics(&statistics);
284 reset remote ctdb statistics
286 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
290 ret = ctdb_statistics_reset(ctdb, options.pnn);
292 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
300 display uptime of remote node
302 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
305 struct ctdb_uptime *uptime = NULL;
306 int tmp, days, hours, minutes, seconds;
308 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
310 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
314 if (options.machinereadable){
315 printf(":Current Node Time:Ctdb Start Time:Last Recovery Time:Last Recovery Duration:\n");
316 printf(":%u:%u:%u:%lf\n",
317 (unsigned int)uptime->current_time.tv_sec,
318 (unsigned int)uptime->ctdbd_start_time.tv_sec,
319 (unsigned int)uptime->last_recovery_finished.tv_sec,
320 timeval_delta(&uptime->last_recovery_finished,
321 &uptime->last_recovery_started)
326 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
328 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
336 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
338 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
346 printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
348 printf("Duration of last recovery : %lf seconds\n",
349 timeval_delta(&uptime->last_recovery_finished,
350 &uptime->last_recovery_started));
356 show the PNN of the current node
358 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
362 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
364 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node."));
368 printf("PNN:%d\n", mypnn);
373 display remote ctdb status
375 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
378 struct ctdb_vnn_map *vnnmap=NULL;
379 struct ctdb_node_map *nodemap=NULL;
380 uint32_t recmode, recmaster;
383 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
388 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
390 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
394 if(options.machinereadable){
395 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
396 for(i=0;i<nodemap->num;i++){
397 printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
398 ctdb_addr_to_str(&nodemap->nodes[i].addr),
399 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
400 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
401 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
402 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
407 printf("Number of nodes:%d\n", nodemap->num);
408 for(i=0;i<nodemap->num;i++){
409 static const struct {
413 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
414 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
415 { NODE_FLAGS_BANNED, "BANNED" },
416 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
418 char *flags_str = NULL;
420 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
421 if (nodemap->nodes[i].flags & flag_names[j].flag) {
422 if (flags_str == NULL) {
423 flags_str = talloc_strdup(ctdb, flag_names[j].name);
425 flags_str = talloc_asprintf_append(flags_str, "|%s",
428 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
431 if (flags_str == NULL) {
432 flags_str = talloc_strdup(ctdb, "OK");
433 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
435 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
436 ctdb_addr_to_str(&nodemap->nodes[i].addr),
438 nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
439 talloc_free(flags_str);
442 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
444 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
447 if (vnnmap->generation == INVALID_GENERATION) {
448 printf("Generation:INVALID\n");
450 printf("Generation:%d\n",vnnmap->generation);
452 printf("Size:%d\n",vnnmap->size);
453 for(i=0;i<vnnmap->size;i++){
454 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
457 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
459 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
462 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
464 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
466 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
469 printf("Recovery master:%d\n",recmaster);
475 display the pnn of the recovery master
477 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
482 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
484 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
487 printf("%d\n",recmaster);
493 get a list of all tickles for this pnn
495 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
497 struct ctdb_control_tcp_tickle_list *list;
505 if (parse_ip(argv[0], &addr) == 0) {
506 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
510 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
512 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
516 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
517 printf("Num tickles:%u\n", list->tickles.num);
518 for (i=0;i<list->tickles.num;i++) {
519 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
520 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));
528 /* send a release ip to all nodes */
529 static int control_send_release(struct ctdb_context *ctdb, uint32_t pnn,
530 ctdb_sock_addr *addr)
533 struct ctdb_public_ip pip;
535 struct ctdb_node_map *nodemap=NULL;
537 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
539 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
543 /* send a moveip message to the recovery master */
546 data.dsize = sizeof(pip);
547 data.dptr = (unsigned char *)&pip;
550 /* send release ip to all nodes */
551 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
552 list_of_active_nodes(ctdb, nodemap, ctdb, true),
553 TIMELIMIT(), false, data,
554 NULL, NULL, NULL) != 0) {
555 DEBUG(DEBUG_ERR, (__location__ " Unable to send 'ReleaseIP' to all nodes.\n"));
563 move/failover an ip address to a specific node
565 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
570 struct ctdb_all_public_ips *ips;
577 if (parse_ip(argv[0], &addr) == 0) {
578 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
583 if (sscanf(argv[1], "%u", &pnn) != 1) {
584 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
588 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "DeterministicIPs", &value);
590 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'DeterministicIPs' from local node\n"));
594 DEBUG(DEBUG_ERR, ("The tunable 'DeterministicIPs' is set. You can only move ip addresses when this feature is disabled\n"));
598 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "NoIPFailback", &value);
600 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'NoIPFailback' from local node\n"));
604 DEBUG(DEBUG_ERR, ("The tunable 'NoIPFailback' is NOT set. You can only move ip addresses when this feature is enabled\n"));
608 /* read the public ip list from the node */
609 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
611 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
615 for (i=0;i<ips->num;i++) {
616 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
621 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
622 pnn, ctdb_addr_to_str(&addr)));
625 if (ips->ips[i].pnn == pnn) {
626 DEBUG(DEBUG_ERR, ("Host %u is already hosting '%s'\n",
627 pnn, ctdb_addr_to_str(&ips->ips[i].addr)));
631 ret = control_send_release(ctdb, pnn, &ips->ips[i].addr);
633 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));;
640 void getips_store_callback(void *param, void *data)
642 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
643 struct ctdb_all_public_ips *ips = param;
647 ips->ips[i].pnn = node_ip->pnn;
648 ips->ips[i].addr = node_ip->addr;
651 void getips_count_callback(void *param, void *data)
653 uint32_t *count = param;
659 static uint32_t *ip_key(ctdb_sock_addr *ip)
661 static uint32_t key[IP_KEYLEN];
663 bzero(key, sizeof(key));
665 switch (ip->sa.sa_family) {
667 key[0] = ip->ip.sin_addr.s_addr;
670 key[0] = ip->ip6.sin6_addr.s6_addr32[3];
671 key[1] = ip->ip6.sin6_addr.s6_addr32[2];
672 key[2] = ip->ip6.sin6_addr.s6_addr32[1];
673 key[3] = ip->ip6.sin6_addr.s6_addr32[0];
676 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
683 static void *add_ip_callback(void *parm, void *data)
689 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
691 struct ctdb_all_public_ips *tmp_ips;
692 struct ctdb_node_map *nodemap=NULL;
693 trbt_tree_t *ip_tree;
697 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
699 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
703 ip_tree = trbt_create(tmp_ctx, 0);
705 for(i=0;i<nodemap->num;i++){
706 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
710 /* read the public ip list from this node */
711 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
713 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
717 for (j=0; j<tmp_ips->num;j++) {
718 struct ctdb_public_ip *node_ip;
720 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
721 node_ip->pnn = tmp_ips->ips[j].pnn;
722 node_ip->addr = tmp_ips->ips[j].addr;
724 trbt_insertarray32_callback(ip_tree,
725 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
729 talloc_free(tmp_ips);
734 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
736 len = offsetof(struct ctdb_all_public_ips, ips) +
737 count*sizeof(struct ctdb_public_ip);
738 tmp_ips = talloc_zero_size(tmp_ctx, len);
739 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
748 * scans all other nodes and returns a pnn for another node that can host this
752 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
754 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
755 struct ctdb_all_public_ips *ips;
756 struct ctdb_node_map *nodemap=NULL;
759 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
761 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
762 talloc_free(tmp_ctx);
766 for(i=0;i<nodemap->num;i++){
767 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
770 if (nodemap->nodes[i].pnn == options.pnn) {
774 /* read the public ip list from this node */
775 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
777 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
781 for (j=0;j<ips->num;j++) {
782 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
783 talloc_free(tmp_ctx);
784 return nodemap->nodes[i].pnn;
790 talloc_free(tmp_ctx);
795 add a public ip address to a node
797 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
803 struct ctdb_control_ip_iface *pub;
804 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
805 struct ctdb_all_public_ips *ips;
808 talloc_free(tmp_ctx);
812 if (!parse_ip_mask(argv[0], &addr, &mask)) {
813 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
814 talloc_free(tmp_ctx);
818 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
820 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
821 talloc_free(tmp_ctx);
826 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
827 pub = talloc_size(tmp_ctx, len);
828 CTDB_NO_MEMORY(ctdb, pub);
832 pub->len = strlen(argv[1])+1;
833 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
835 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
837 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
838 talloc_free(tmp_ctx);
843 /* check if some other node is already serving this ip, if not,
846 for (i=0;i<ips->num;i++) {
847 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
851 /* no one has this ip so we claim it */
853 ret = control_send_release(ctdb, options.pnn, &addr);
855 ret = control_send_release(ctdb, ips->ips[i].pnn, &addr);
859 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));
863 talloc_free(tmp_ctx);
867 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
869 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
871 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
872 struct ctdb_node_map *nodemap=NULL;
873 struct ctdb_all_public_ips *ips;
876 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
878 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
882 /* remove it from the nodes that are not hosting the ip currently */
883 for(i=0;i<nodemap->num;i++){
884 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
887 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
888 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
892 for (j=0;j<ips->num;j++) {
893 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
901 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
905 options.pnn = nodemap->nodes[i].pnn;
906 control_delip(ctdb, argc, argv);
910 /* remove it from every node (also the one hosting it) */
911 for(i=0;i<nodemap->num;i++){
912 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
915 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
916 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
920 for (j=0;j<ips->num;j++) {
921 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
929 options.pnn = nodemap->nodes[i].pnn;
930 control_delip(ctdb, argc, argv);
933 talloc_free(tmp_ctx);
938 delete a public ip address from a node
940 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
944 struct ctdb_control_ip_iface pub;
945 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
946 struct ctdb_all_public_ips *ips;
949 talloc_free(tmp_ctx);
953 if (parse_ip(argv[0], &addr) == 0) {
954 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
958 if (options.pnn == CTDB_BROADCAST_ALL) {
959 return control_delip_all(ctdb, argc, argv, &addr);
966 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
968 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
969 talloc_free(tmp_ctx);
973 for (i=0;i<ips->num;i++) {
974 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
980 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
981 ctdb_addr_to_str(&addr)));
982 talloc_free(tmp_ctx);
986 if (ips->ips[i].pnn == options.pnn) {
987 ret = find_other_host_for_public_ip(ctdb, &addr);
989 ret = control_send_release(ctdb, ret, &addr);
991 DEBUG(DEBUG_ERR, ("Failed to migrate this ip to another node. Use moveip of recover to reassign this address to a node\n"));
996 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
998 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
999 talloc_free(tmp_ctx);
1003 talloc_free(tmp_ctx);
1008 kill a tcp connection
1010 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1013 struct ctdb_control_killtcp killtcp;
1019 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
1020 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1024 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
1025 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1029 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
1031 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1042 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1045 ctdb_sock_addr addr;
1051 if (!parse_ip(argv[0], &addr)) {
1052 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1056 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1058 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1066 register a server id
1068 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1071 struct ctdb_server_id server_id;
1077 server_id.pnn = strtoul(argv[0], NULL, 0);
1078 server_id.type = strtoul(argv[1], NULL, 0);
1079 server_id.server_id = strtoul(argv[2], NULL, 0);
1081 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1083 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1090 unregister a server id
1092 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1095 struct ctdb_server_id server_id;
1101 server_id.pnn = strtoul(argv[0], NULL, 0);
1102 server_id.type = strtoul(argv[1], NULL, 0);
1103 server_id.server_id = strtoul(argv[2], NULL, 0);
1105 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
1107 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
1114 check if a server id exists
1116 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1120 struct ctdb_server_id server_id;
1126 server_id.pnn = strtoul(argv[0], NULL, 0);
1127 server_id.type = strtoul(argv[1], NULL, 0);
1128 server_id.server_id = strtoul(argv[2], NULL, 0);
1130 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
1132 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
1137 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
1139 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
1145 get a list of all server ids that are registered on a node
1147 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
1150 struct ctdb_server_id_list *server_ids;
1152 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
1154 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
1158 for (i=0; i<server_ids->num; i++) {
1159 printf("Server id %d:%d:%d\n",
1160 server_ids->server_ids[i].pnn,
1161 server_ids->server_ids[i].type,
1162 server_ids->server_ids[i].server_id);
1169 send a tcp tickle ack
1171 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1174 ctdb_sock_addr src, dst;
1180 if (!parse_ip_port(argv[0], &src)) {
1181 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1185 if (!parse_ip_port(argv[1], &dst)) {
1186 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1190 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
1194 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
1201 display public ip status
1203 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
1206 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1207 struct ctdb_all_public_ips *ips;
1209 if (options.pnn == CTDB_BROADCAST_ALL) {
1210 /* read the list of public ips from all nodes */
1211 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1213 /* read the public ip list from this node */
1214 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1217 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
1218 talloc_free(tmp_ctx);
1222 if (options.machinereadable){
1223 printf(":Public IP:Node:\n");
1225 if (options.pnn == CTDB_BROADCAST_ALL) {
1226 printf("Public IPs on ALL nodes\n");
1228 printf("Public IPs on node %u\n", options.pnn);
1232 for (i=1;i<=ips->num;i++) {
1233 if (options.machinereadable){
1234 printf(":%s:%d:\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1236 printf("%s %d\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1240 talloc_free(tmp_ctx);
1245 display pid of a ctdb daemon
1247 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
1252 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
1254 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
1257 printf("Pid:%d\n", pid);
1263 disable a remote node
1265 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
1269 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
1271 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
1279 enable a disabled remote node
1281 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
1285 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
1287 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
1294 static uint32_t get_generation(struct ctdb_context *ctdb)
1296 struct ctdb_vnn_map *vnnmap=NULL;
1299 /* wait until the recmaster is not in recovery mode */
1301 uint32_t recmode, recmaster;
1303 if (vnnmap != NULL) {
1304 talloc_free(vnnmap);
1308 /* get the recmaster */
1309 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
1311 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1315 /* get recovery mode */
1316 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
1318 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1322 /* get the current generation number */
1323 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
1325 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
1329 if ((recmode == CTDB_RECOVERY_NORMAL)
1330 && (vnnmap->generation != 1)){
1331 return vnnmap->generation;
1338 ban a node from the cluster
1340 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
1343 struct ctdb_ban_info b;
1346 struct ctdb_node_map *nodemap=NULL;
1347 uint32_t generation, next_generation;
1353 /* record the current generation number */
1354 generation = get_generation(ctdb);
1357 /* verify the node exists */
1358 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
1360 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1364 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
1365 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
1369 ban_time = strtoul(argv[0], NULL, 0);
1371 b.pnn = options.pnn;
1372 b.ban_time = ban_time;
1374 data.dptr = (uint8_t *)&b;
1375 data.dsize = sizeof(b);
1377 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
1379 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
1383 /* wait until we are in a new generation */
1385 next_generation = get_generation(ctdb);
1386 if (next_generation != generation) {
1397 unban a node from the cluster
1399 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
1403 uint32_t generation, next_generation;
1405 /* record the current generation number */
1406 generation = get_generation(ctdb);
1408 data.dptr = (uint8_t *)&options.pnn;
1409 data.dsize = sizeof(uint32_t);
1411 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
1413 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
1417 /* wait until we are in a new generation */
1419 next_generation = get_generation(ctdb);
1420 if (next_generation != generation) {
1433 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
1437 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
1439 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
1449 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
1452 uint32_t generation, next_generation;
1454 /* record the current generation number */
1455 generation = get_generation(ctdb);
1457 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1459 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
1463 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
1465 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
1469 /* wait until we are in a new generation */
1471 next_generation = get_generation(ctdb);
1472 if (next_generation != generation) {
1483 display monitoring mode of a remote node
1485 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
1490 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
1492 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
1495 if (!options.machinereadable){
1496 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
1499 printf(":%d:\n",monmode);
1506 display capabilities of a remote node
1508 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
1510 uint32_t capabilities;
1513 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
1515 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
1519 if (!options.machinereadable){
1520 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
1521 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
1522 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
1524 printf(":RECMASTER:LMASTER:LVS:\n");
1525 printf(":%d:%d:%d:\n",
1526 !!(capabilities&CTDB_CAP_RECMASTER),
1527 !!(capabilities&CTDB_CAP_LMASTER),
1528 !!(capabilities&CTDB_CAP_LVS));
1534 display lvs configuration
1536 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
1538 uint32_t *capabilities;
1539 struct ctdb_node_map *nodemap=NULL;
1541 int healthy_count = 0;
1543 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1545 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1549 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1550 CTDB_NO_MEMORY(ctdb, capabilities);
1552 /* collect capabilities for all connected nodes */
1553 for (i=0; i<nodemap->num; i++) {
1554 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1557 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1561 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1563 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1567 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1571 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1576 /* Print all LVS nodes */
1577 for (i=0; i<nodemap->num; i++) {
1578 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1581 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1584 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1588 if (healthy_count != 0) {
1589 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1594 printf("%d:%s\n", i,
1595 ctdb_addr_to_str(&nodemap->nodes[i].addr));
1602 display who is the lvs master
1604 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1606 uint32_t *capabilities;
1607 struct ctdb_node_map *nodemap=NULL;
1609 int healthy_count = 0;
1611 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1613 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1617 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1618 CTDB_NO_MEMORY(ctdb, capabilities);
1620 /* collect capabilities for all connected nodes */
1621 for (i=0; i<nodemap->num; i++) {
1622 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1625 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1629 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1631 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1635 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1639 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1644 /* find and show the lvsmaster */
1645 for (i=0; i<nodemap->num; i++) {
1646 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1649 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1652 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1656 if (healthy_count != 0) {
1657 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1662 printf("Node %d is LVS master\n", i);
1666 printf("There is no LVS master\n");
1671 disable monitoring on a node
1673 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1678 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
1680 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
1683 printf("Monitoring mode:%s\n","DISABLED");
1689 enable monitoring on a node
1691 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1696 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
1698 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
1701 printf("Monitoring mode:%s\n","ACTIVE");
1707 display remote list of keys/data for a db
1709 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
1711 const char *db_name;
1712 struct ctdb_db_context *ctdb_db;
1722 if (db_exists(ctdb, db_name)) {
1723 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
1727 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
1729 if (ctdb_db == NULL) {
1730 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1734 /* traverse and dump the cluster tdb */
1735 ret = ctdb_dump_db(ctdb_db, stdout);
1737 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
1740 talloc_free(ctdb_db);
1742 printf("Dumped %d records\n", ret);
1748 display a list of the databases on a remote ctdb
1750 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
1753 struct ctdb_dbid_map *dbmap=NULL;
1755 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
1757 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
1761 printf("Number of databases:%d\n", dbmap->num);
1762 for(i=0;i<dbmap->num;i++){
1767 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
1768 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
1769 persistent = dbmap->dbs[i].persistent;
1770 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name,
1771 path, persistent?"PERSISTENT":"");
1778 check if the local node is recmaster or not
1779 it will return 1 if this node is the recmaster and 0 if it is not
1780 or if the local ctdb daemon could not be contacted
1782 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1784 uint32_t mypnn, recmaster;
1787 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1789 printf("Failed to get pnn of node\n");
1793 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1795 printf("Failed to get the recmaster\n");
1799 if (recmaster != mypnn) {
1800 printf("this node is not the recmaster\n");
1804 printf("this node is the recmaster\n");
1811 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
1814 struct timeval tv = timeval_current();
1815 ret = ctdb_ctrl_ping(ctdb, options.pnn);
1817 printf("Unable to get ping response from node %u\n", options.pnn);
1820 printf("response from %u time=%.6f sec (%d clients)\n",
1821 options.pnn, timeval_elapsed(&tv), ret);
1830 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
1841 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
1843 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
1847 printf("%-19s = %u\n", name, value);
1854 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
1865 value = strtoul(argv[1], NULL, 0);
1867 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
1869 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
1878 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
1884 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
1886 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
1890 for (i=0;i<count;i++) {
1891 control_getvar(ctdb, 1, &list[i]);
1901 const char *description;
1902 } debug_levels[] = {
1903 {DEBUG_EMERG, "EMERG"},
1904 {DEBUG_ALERT, "ALERT"},
1905 {DEBUG_CRIT, "CRIT"},
1907 {DEBUG_WARNING, "WARNING"},
1908 {DEBUG_NOTICE, "NOTICE"},
1909 {DEBUG_INFO, "INFO"},
1910 {DEBUG_DEBUG, "DEBUG"}
1913 static const char *get_debug_by_level(int32_t level)
1917 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1918 if (debug_levels[i].level == level) {
1919 return debug_levels[i].description;
1925 static int32_t get_debug_by_desc(const char *desc)
1929 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1930 if (!strcmp(debug_levels[i].description, desc)) {
1931 return debug_levels[i].level;
1935 fprintf(stderr, "Invalid debug level '%s'\nMust be one of\n", desc);
1936 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1937 fprintf(stderr, " %s\n", debug_levels[i].description);
1944 display debug level on a node
1946 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1951 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1953 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
1956 if (options.machinereadable){
1957 printf(":Name:Level:\n");
1958 printf(":%s:%d:\n",get_debug_by_level(level),level);
1960 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
1968 set debug level on a node or all nodes
1970 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1979 if (isalpha(argv[0][0])) {
1980 level = get_debug_by_desc(argv[0]);
1982 level = strtol(argv[0], NULL, 0);
1985 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1987 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1996 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
2000 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
2002 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
2010 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
2014 ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
2016 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
2023 attach to a database
2025 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
2027 const char *db_name;
2028 struct ctdb_db_context *ctdb_db;
2035 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
2036 if (ctdb_db == NULL) {
2037 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2045 run an eventscript on a node
2047 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
2053 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2056 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2060 data.dptr = (unsigned char *)discard_const(argv[0]);
2061 data.dsize = strlen((char *)data.dptr) + 1;
2063 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
2065 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
2066 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
2067 if (ret != 0 || res != 0) {
2068 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
2069 talloc_free(tmp_ctx);
2072 talloc_free(tmp_ctx);
2076 #define DB_VERSION 1
2077 #define MAX_DB_NAME 64
2078 struct db_file_header {
2079 unsigned long version;
2081 unsigned long persistent;
2083 const char name[MAX_DB_NAME];
2086 struct backup_data {
2087 struct ctdb_marshall_buffer *records;
2090 bool traverse_error;
2093 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
2095 struct backup_data *bd = talloc_get_type(private, struct backup_data);
2096 struct ctdb_rec_data *rec;
2098 /* add the record */
2099 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
2101 bd->traverse_error = true;
2102 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
2105 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
2106 if (bd->records == NULL) {
2107 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
2108 bd->traverse_error = true;
2111 bd->records->count++;
2112 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
2113 bd->len += rec->length;
2121 * backup a database to a file
2123 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
2126 struct ctdb_dbid_map *dbmap=NULL;
2127 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2128 struct db_file_header dbhdr;
2129 struct ctdb_db_context *ctdb_db;
2130 struct backup_data *bd;
2134 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2138 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
2140 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
2144 for(i=0;i<dbmap->num;i++){
2147 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
2148 if(!strcmp(argv[0], name)){
2149 talloc_free(discard_const(name));
2152 talloc_free(discard_const(name));
2154 if (i == dbmap->num) {
2155 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
2156 talloc_free(tmp_ctx);
2161 ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
2162 if (ctdb_db == NULL) {
2163 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
2168 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
2170 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
2171 talloc_free(tmp_ctx);
2176 bd = talloc_zero(tmp_ctx, struct backup_data);
2178 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
2179 talloc_free(tmp_ctx);
2183 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
2184 if (bd->records == NULL) {
2185 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
2186 talloc_free(tmp_ctx);
2190 bd->len = offsetof(struct ctdb_marshall_buffer, data);
2191 bd->records->db_id = ctdb_db->db_id;
2192 /* traverse the database collecting all records */
2193 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
2194 bd->traverse_error) {
2195 DEBUG(DEBUG_ERR,("Traverse error\n"));
2196 talloc_free(tmp_ctx);
2200 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
2203 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
2205 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
2206 talloc_free(tmp_ctx);
2210 dbhdr.version = DB_VERSION;
2211 dbhdr.timestamp = time(NULL);
2212 dbhdr.persistent = dbmap->dbs[i].persistent;
2213 dbhdr.size = bd->len;
2214 if (strlen(argv[0]) >= MAX_DB_NAME) {
2215 DEBUG(DEBUG_ERR,("Too long dbname\n"));
2216 talloc_free(tmp_ctx);
2219 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
2220 write(fh, &dbhdr, sizeof(dbhdr));
2221 write(fh, bd->records, bd->len);
2224 talloc_free(tmp_ctx);
2229 * restore a database from a file
2231 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
2234 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2237 struct db_file_header dbhdr;
2238 struct ctdb_db_context *ctdb_db;
2239 struct ctdb_node_map *nodemap=NULL;
2240 struct ctdb_vnn_map *vnnmap=NULL;
2242 struct ctdb_control_wipe_database w;
2244 uint32_t generation;
2249 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2253 fh = open(argv[0], O_RDONLY);
2255 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
2256 talloc_free(tmp_ctx);
2260 read(fh, &dbhdr, sizeof(dbhdr));
2261 if (dbhdr.version != DB_VERSION) {
2262 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
2263 talloc_free(tmp_ctx);
2267 outdata.dsize = dbhdr.size;
2268 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
2269 if (outdata.dptr == NULL) {
2270 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
2272 talloc_free(tmp_ctx);
2275 read(fh, outdata.dptr, outdata.dsize);
2278 tm = localtime(&dbhdr.timestamp);
2279 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
2280 printf("Restoring database '%s' from backup @ %s\n",
2284 ctdb_db = ctdb_attach(ctdb, dbhdr.name, dbhdr.persistent, 0);
2285 if (ctdb_db == NULL) {
2286 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbhdr.name));
2287 talloc_free(tmp_ctx);
2291 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2293 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2294 talloc_free(tmp_ctx);
2299 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
2301 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
2302 talloc_free(tmp_ctx);
2306 /* freeze all nodes */
2307 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2308 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
2313 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
2314 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2315 talloc_free(tmp_ctx);
2319 generation = vnnmap->generation;
2320 data.dptr = (void *)&generation;
2321 data.dsize = sizeof(generation);
2323 /* start a cluster wide transaction */
2324 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2325 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
2327 TIMELIMIT(), false, data,
2330 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
2335 w.db_id = ctdb_db->db_id;
2336 w.transaction_id = generation;
2338 data.dptr = (void *)&w;
2339 data.dsize = sizeof(w);
2341 /* wipe all the remote databases. */
2342 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2343 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
2345 TIMELIMIT(), false, data,
2348 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
2349 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2350 talloc_free(tmp_ctx);
2354 /* push the database */
2355 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2356 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
2358 TIMELIMIT(), false, outdata,
2361 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
2362 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2363 talloc_free(tmp_ctx);
2367 data.dptr = (void *)&generation;
2368 data.dsize = sizeof(generation);
2370 /* commit all the changes */
2371 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
2373 TIMELIMIT(), false, data,
2376 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
2377 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2378 talloc_free(tmp_ctx);
2383 /* thaw all nodes */
2384 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2385 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
2390 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
2391 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2392 talloc_free(tmp_ctx);
2397 talloc_free(tmp_ctx);
2402 * set flags of a node in the nodemap
2404 static int control_setflags(struct ctdb_context *ctdb, int argc, const char **argv)
2411 struct ctdb_node_flag_change c;
2418 if (sscanf(argv[0], "%d", &node) != 1) {
2419 DEBUG(DEBUG_ERR, ("Badly formed node\n"));
2423 if (sscanf(argv[1], "0x%x", &flags) != 1) {
2424 DEBUG(DEBUG_ERR, ("Badly formed flags\n"));
2431 c.new_flags = flags;
2433 data.dsize = sizeof(c);
2434 data.dptr = (unsigned char *)&c;
2436 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_MODIFY_FLAGS, 0,
2437 data, NULL, NULL, &status, NULL, NULL);
2438 if (ret != 0 || status != 0) {
2439 DEBUG(DEBUG_ERR,("Failed to modify flags\n"));
2448 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2454 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2455 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
2456 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
2457 if (ret != 0 || res != 0) {
2458 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
2459 talloc_free(tmp_ctx);
2462 write(1, data.dptr, data.dsize);
2463 talloc_free(tmp_ctx);
2468 handler for memory dumps
2470 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
2471 TDB_DATA data, void *private_data)
2473 write(1, data.dptr, data.dsize);
2478 dump memory usage on the recovery daemon
2480 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2484 struct rd_memdump_reply rd;
2486 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2488 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
2491 rd.srvid = getpid();
2493 /* register a message port for receiveing the reply so that we
2494 can receive the reply
2496 ctdb_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
2499 data.dptr = (uint8_t *)&rd;
2500 data.dsize = sizeof(rd);
2502 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
2504 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
2508 /* this loop will terminate when we have received the reply */
2510 event_loop_once(ctdb->ev);
2517 list all nodes in the cluster
2519 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
2522 struct ctdb_node_map *nodemap=NULL;
2524 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2526 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2530 for(i=0;i<nodemap->num;i++){
2531 printf("%s\n", ctdb_addr_to_str(&nodemap->nodes[i].addr));
2538 reload the nodes file on the local node
2540 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
2544 struct ctdb_node_map *nodemap=NULL;
2546 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2548 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
2552 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2554 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2558 /* reload the nodes file on all remote nodes */
2559 for (i=0;i<nodemap->num;i++) {
2560 if (nodemap->nodes[i].pnn == mypnn) {
2563 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
2564 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
2565 nodemap->nodes[i].pnn);
2567 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
2571 /* reload the nodes file on the local node */
2572 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
2573 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
2575 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
2578 /* initiate a recovery */
2579 control_recover(ctdb, argc, argv);
2585 static const struct {
2587 int (*fn)(struct ctdb_context *, int, const char **);
2591 } ctdb_commands[] = {
2593 { "version", control_version, true, "show version of ctdb" },
2595 { "status", control_status, true, "show node status" },
2596 { "uptime", control_uptime, true, "show node uptime" },
2597 { "ping", control_ping, true, "ping all nodes" },
2598 { "getvar", control_getvar, true, "get a tunable variable", "<name>"},
2599 { "setvar", control_setvar, true, "set a tunable variable", "<name> <value>"},
2600 { "listvars", control_listvars, true, "list tunable variables"},
2601 { "statistics", control_statistics, false, "show statistics" },
2602 { "statisticsreset", control_statistics_reset, true, "reset statistics"},
2603 { "ip", control_ip, false, "show which public ip's that ctdb manages" },
2604 { "process-exists", control_process_exists, true, "check if a process exists on a node", "<pid>"},
2605 { "getdbmap", control_getdbmap, true, "show the database map" },
2606 { "catdb", control_catdb, true, "dump a database" , "<dbname>"},
2607 { "getmonmode", control_getmonmode, true, "show monitoring mode" },
2608 { "getcapabilities", control_getcapabilities, true, "show node capabilities" },
2609 { "pnn", control_pnn, true, "show the pnn of the currnet node" },
2610 { "lvs", control_lvs, true, "show lvs configuration" },
2611 { "lvsmaster", control_lvsmaster, true, "show which node is the lvs master" },
2612 { "disablemonitor", control_disable_monmode, true, "set monitoring mode to DISABLE" },
2613 { "enablemonitor", control_enable_monmode, true, "set monitoring mode to ACTIVE" },
2614 { "setdebug", control_setdebug, true, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
2615 { "getdebug", control_getdebug, true, "get debug level" },
2616 { "attach", control_attach, true, "attach to a database", "<dbname>" },
2617 { "dumpmemory", control_dumpmemory, true, "dump memory map to stdout" },
2618 { "rddumpmemory", control_rddumpmemory, true, "dump memory map from the recovery daemon to stdout" },
2619 { "getpid", control_getpid, true, "get ctdbd process ID" },
2620 { "disable", control_disable, true, "disable a nodes public IP" },
2621 { "enable", control_enable, true, "enable a nodes public IP" },
2622 { "ban", control_ban, true, "ban a node from the cluster", "<bantime|0>"},
2623 { "unban", control_unban, true, "unban a node from the cluster" },
2624 { "shutdown", control_shutdown, true, "shutdown ctdbd" },
2625 { "recover", control_recover, true, "force recovery" },
2626 { "freeze", control_freeze, true, "freeze all databases" },
2627 { "thaw", control_thaw, true, "thaw all databases" },
2628 { "isnotrecmaster", control_isnotrecmaster, false, "check if the local node is recmaster or not" },
2629 { "killtcp", kill_tcp, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
2630 { "gratiousarp", control_gratious_arp, false, "send a gratious arp", "<ip> <interface>" },
2631 { "tickle", tickle_tcp, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
2632 { "gettickles", control_get_tickles, false, "get the list of tickles registered for this ip", "<ip>" },
2634 { "regsrvid", regsrvid, false, "register a server id", "<pnn> <type> <id>" },
2635 { "unregsrvid", unregsrvid, false, "unregister a server id", "<pnn> <type> <id>" },
2636 { "chksrvid", chksrvid, false, "check if a server id exists", "<pnn> <type> <id>" },
2637 { "getsrvids", getsrvids, false, "get a list of all server ids"},
2638 { "vacuum", ctdb_vacuum, false, "vacuum the databases of empty records", "[max_records]"},
2639 { "repack", ctdb_repack, false, "repack all databases", "[max_freelist]"},
2640 { "listnodes", control_listnodes, false, "list all nodes in the cluster"},
2641 { "reloadnodes", control_reload_nodes_file, false, "reload the nodes file and restart the transport on all nodes"},
2642 { "moveip", control_moveip, false, "move/failover an ip address to another node", "<ip> <node>"},
2643 { "addip", control_addip, true, "add a ip address to a node", "<ip/mask> <iface>"},
2644 { "delip", control_delip, false, "delete an ip address from a node", "<ip>"},
2645 { "eventscript", control_eventscript, true, "run the eventscript with the given parameters on a node", "<arguments>"},
2646 { "backupdb", control_backupdb, false, "backup the database into a file.", "<database> <file>"},
2647 { "restoredb", control_restoredb, false, "restore the database from a file.", "<file>"},
2648 { "recmaster", control_recmaster, false, "show the pnn for the recovery master."},
2649 { "setflags", control_setflags, false, "set flags for a node in the nodemap.", "<node> <flags>"},
2655 static void usage(void)
2659 "Usage: ctdb [options] <control>\n" \
2661 " -n <node> choose node number, or 'all' (defaults to local node)\n"
2662 " -Y generate machinereadable output\n"
2663 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
2664 printf("Controls:\n");
2665 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2666 printf(" %-15s %-27s %s\n",
2667 ctdb_commands[i].name,
2668 ctdb_commands[i].args?ctdb_commands[i].args:"",
2669 ctdb_commands[i].msg);
2675 static void ctdb_alarm(int sig)
2677 printf("Maximum runtime exceeded - exiting\n");
2684 int main(int argc, const char *argv[])
2686 struct ctdb_context *ctdb;
2687 char *nodestring = NULL;
2688 struct poptOption popt_options[] = {
2691 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
2692 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
2693 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
2694 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
2698 const char **extra_argv;
2702 struct event_context *ev;
2703 const char *control;
2707 /* set some defaults */
2708 options.maxruntime = 0;
2709 options.timelimit = 3;
2710 options.pnn = CTDB_CURRENT_NODE;
2712 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
2714 while ((opt = poptGetNextOpt(pc)) != -1) {
2717 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
2718 poptBadOption(pc, 0), poptStrerror(opt)));
2723 /* setup the remaining options for the main program to use */
2724 extra_argv = poptGetArgs(pc);
2727 while (extra_argv[extra_argc]) extra_argc++;
2730 if (extra_argc < 1) {
2734 if (options.maxruntime == 0) {
2735 const char *ctdb_timeout;
2736 ctdb_timeout = getenv("CTDB_TIMEOUT");
2737 if (ctdb_timeout != NULL) {
2738 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
2741 if (options.maxruntime != 0) {
2742 signal(SIGALRM, ctdb_alarm);
2743 alarm(options.maxruntime);
2746 /* setup the node number to contact */
2747 if (nodestring != NULL) {
2748 if (strcmp(nodestring, "all") == 0) {
2749 options.pnn = CTDB_BROADCAST_ALL;
2751 options.pnn = strtoul(nodestring, NULL, 0);
2755 control = extra_argv[0];
2757 ev = event_context_init(NULL);
2759 /* initialise ctdb */
2760 ctdb = ctdb_cmdline_client(ev);
2762 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
2766 /* verify the node exists */
2769 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2770 if (strcmp(control, ctdb_commands[i].name) == 0) {
2773 if (options.pnn == CTDB_CURRENT_NODE) {
2775 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
2782 if (ctdb_commands[i].auto_all &&
2783 options.pnn == CTDB_BROADCAST_ALL) {
2788 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
2789 CTDB_NO_MEMORY(ctdb, nodes);
2791 for (j=0;j<num_nodes;j++) {
2792 options.pnn = nodes[j];
2793 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2797 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2803 if (i == ARRAY_SIZE(ctdb_commands)) {
2804 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));