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 static void usage(void);
43 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
46 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
49 #define XSTR(x) STR(x)
50 printf("CTDB version: %s\n", XSTR(CTDB_VERS));
57 check if a database exists
59 static int db_exists(struct ctdb_context *ctdb, const char *db_name)
62 struct ctdb_dbid_map *dbmap=NULL;
64 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
66 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
70 for(i=0;i<dbmap->num;i++){
73 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
74 if (!strcmp(name, db_name)) {
83 see if a process exists
85 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
93 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
94 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
98 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
100 printf("%u:%u exists\n", pnn, pid);
102 printf("%u:%u does not exist\n", pnn, pid);
108 display statistics structure
110 static void show_statistics(struct ctdb_statistics *s)
112 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
114 const char *prefix=NULL;
120 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
121 STATISTICS_FIELD(num_clients),
122 STATISTICS_FIELD(frozen),
123 STATISTICS_FIELD(recovering),
124 STATISTICS_FIELD(client_packets_sent),
125 STATISTICS_FIELD(client_packets_recv),
126 STATISTICS_FIELD(node_packets_sent),
127 STATISTICS_FIELD(node_packets_recv),
128 STATISTICS_FIELD(keepalive_packets_sent),
129 STATISTICS_FIELD(keepalive_packets_recv),
130 STATISTICS_FIELD(node.req_call),
131 STATISTICS_FIELD(node.reply_call),
132 STATISTICS_FIELD(node.req_dmaster),
133 STATISTICS_FIELD(node.reply_dmaster),
134 STATISTICS_FIELD(node.reply_error),
135 STATISTICS_FIELD(node.req_message),
136 STATISTICS_FIELD(node.req_control),
137 STATISTICS_FIELD(node.reply_control),
138 STATISTICS_FIELD(client.req_call),
139 STATISTICS_FIELD(client.req_message),
140 STATISTICS_FIELD(client.req_control),
141 STATISTICS_FIELD(timeouts.call),
142 STATISTICS_FIELD(timeouts.control),
143 STATISTICS_FIELD(timeouts.traverse),
144 STATISTICS_FIELD(total_calls),
145 STATISTICS_FIELD(pending_calls),
146 STATISTICS_FIELD(lockwait_calls),
147 STATISTICS_FIELD(pending_lockwait_calls),
148 STATISTICS_FIELD(childwrite_calls),
149 STATISTICS_FIELD(pending_childwrite_calls),
150 STATISTICS_FIELD(memory_used),
151 STATISTICS_FIELD(max_hop_count),
153 printf("CTDB version %u\n", CTDB_VERSION);
154 for (i=0;i<ARRAY_SIZE(fields);i++) {
155 if (strchr(fields[i].name, '.')) {
156 preflen = strcspn(fields[i].name, ".")+1;
157 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
158 prefix = fields[i].name;
159 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
164 printf(" %*s%-22s%*s%10u\n",
166 fields[i].name+preflen,
168 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
170 printf(" %-30s %.6f sec\n", "max_call_latency", s->max_call_latency);
171 printf(" %-30s %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
172 printf(" %-30s %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
173 talloc_free(tmp_ctx);
177 display remote ctdb statistics combined from all nodes
179 static int control_statistics_all(struct ctdb_context *ctdb)
182 struct ctdb_statistics statistics;
186 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
187 CTDB_NO_MEMORY(ctdb, nodes);
189 ZERO_STRUCT(statistics);
191 for (i=0;i<num_nodes;i++) {
192 struct ctdb_statistics s1;
194 uint32_t *v1 = (uint32_t *)&s1;
195 uint32_t *v2 = (uint32_t *)&statistics;
197 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
198 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
200 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
203 for (j=0;j<num_ints;j++) {
206 statistics.max_hop_count =
207 MAX(statistics.max_hop_count, s1.max_hop_count);
208 statistics.max_call_latency =
209 MAX(statistics.max_call_latency, s1.max_call_latency);
210 statistics.max_lockwait_latency =
211 MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
214 printf("Gathered statistics for %u nodes\n", num_nodes);
215 show_statistics(&statistics);
220 display remote ctdb statistics
222 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
225 struct ctdb_statistics statistics;
227 if (options.pnn == CTDB_BROADCAST_ALL) {
228 return control_statistics_all(ctdb);
231 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
233 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
236 show_statistics(&statistics);
242 reset remote ctdb statistics
244 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
248 ret = ctdb_statistics_reset(ctdb, options.pnn);
250 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
258 display uptime of remote node
260 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
264 struct ctdb_uptime *uptime = NULL;
265 int tmp, days, hours, minutes, seconds;
267 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
272 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
274 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
278 if (options.machinereadable){
279 printf(":Current Node Time:Ctdb Start Time:Last Recovery Time:Last Recovery Duration:\n");
280 printf(":%u:%u:%u:%lf\n",
281 (unsigned int)uptime->current_time.tv_sec,
282 (unsigned int)uptime->ctdbd_start_time.tv_sec,
283 (unsigned int)uptime->last_recovery_finished.tv_sec,
284 timeval_delta(&uptime->last_recovery_finished,
285 &uptime->last_recovery_started)
290 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
292 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
300 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
302 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
310 printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
312 printf("Duration of last recovery : %lf seconds\n",
313 timeval_delta(&uptime->last_recovery_finished,
314 &uptime->last_recovery_started));
320 show the PNN of the current node
322 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
326 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
328 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node."));
332 printf("PNN:%d\n", mypnn);
337 display remote ctdb status
339 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
342 struct ctdb_vnn_map *vnnmap=NULL;
343 struct ctdb_node_map *nodemap=NULL;
344 uint32_t recmode, recmaster;
347 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
352 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
354 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
358 if(options.machinereadable){
359 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
360 for(i=0;i<nodemap->num;i++){
361 printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
362 ctdb_addr_to_str(&nodemap->nodes[i].addr),
363 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
364 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
365 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
366 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
371 printf("Number of nodes:%d\n", nodemap->num);
372 for(i=0;i<nodemap->num;i++){
373 static const struct {
377 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
378 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
379 { NODE_FLAGS_BANNED, "BANNED" },
380 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
382 char *flags_str = NULL;
384 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
385 if (nodemap->nodes[i].flags & flag_names[j].flag) {
386 if (flags_str == NULL) {
387 flags_str = talloc_strdup(ctdb, flag_names[j].name);
389 flags_str = talloc_asprintf_append(flags_str, "|%s",
392 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
395 if (flags_str == NULL) {
396 flags_str = talloc_strdup(ctdb, "OK");
397 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
399 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
400 ctdb_addr_to_str(&nodemap->nodes[i].addr),
402 nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
403 talloc_free(flags_str);
406 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
408 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
411 if (vnnmap->generation == INVALID_GENERATION) {
412 printf("Generation:INVALID\n");
414 printf("Generation:%d\n",vnnmap->generation);
416 printf("Size:%d\n",vnnmap->size);
417 for(i=0;i<vnnmap->size;i++){
418 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
421 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
423 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
426 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
428 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
430 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
433 printf("Recovery master:%d\n",recmaster);
439 display the pnn of the recovery master
441 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
446 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
448 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
451 printf("%d\n",recmaster);
457 get a list of all tickles for this pnn
459 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
461 struct ctdb_control_tcp_tickle_list *list;
469 if (parse_ip(argv[0], &addr) == 0) {
470 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
474 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
476 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
480 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
481 printf("Num tickles:%u\n", list->tickles.num);
482 for (i=0;i<list->tickles.num;i++) {
483 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
484 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));
492 /* send a release ip to all nodes */
493 static int control_send_release(struct ctdb_context *ctdb, uint32_t pnn,
494 ctdb_sock_addr *addr)
497 struct ctdb_public_ip pip;
499 struct ctdb_node_map *nodemap=NULL;
501 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
503 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
507 /* send a moveip message to the recovery master */
510 data.dsize = sizeof(pip);
511 data.dptr = (unsigned char *)&pip;
514 /* send release ip to all nodes */
515 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
516 list_of_active_nodes(ctdb, nodemap, ctdb, true),
517 TIMELIMIT(), false, data,
518 NULL, NULL, NULL) != 0) {
519 DEBUG(DEBUG_ERR, (__location__ " Unable to send 'ReleaseIP' to all nodes.\n"));
527 move/failover an ip address to a specific node
529 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
534 struct ctdb_all_public_ips *ips;
541 if (parse_ip(argv[0], &addr) == 0) {
542 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
547 if (sscanf(argv[1], "%u", &pnn) != 1) {
548 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
552 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "DeterministicIPs", &value);
554 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'DeterministicIPs' from local node\n"));
558 DEBUG(DEBUG_ERR, ("The tunable 'DeterministicIPs' is set. You can only move ip addresses when this feature is disabled\n"));
562 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "NoIPFailback", &value);
564 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'NoIPFailback' from local node\n"));
568 DEBUG(DEBUG_ERR, ("The tunable 'NoIPFailback' is NOT set. You can only move ip addresses when this feature is enabled\n"));
572 /* read the public ip list from the node */
573 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
575 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
579 for (i=0;i<ips->num;i++) {
580 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
585 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
586 pnn, ctdb_addr_to_str(&addr)));
589 if (ips->ips[i].pnn == pnn) {
590 DEBUG(DEBUG_ERR, ("Host %u is already hosting '%s'\n",
591 pnn, ctdb_addr_to_str(&ips->ips[i].addr)));
595 ret = control_send_release(ctdb, pnn, &ips->ips[i].addr);
597 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));;
604 void getips_store_callback(void *param, void *data)
606 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
607 struct ctdb_all_public_ips *ips = param;
611 ips->ips[i].pnn = node_ip->pnn;
612 ips->ips[i].addr = node_ip->addr;
615 void getips_count_callback(void *param, void *data)
617 uint32_t *count = param;
623 static uint32_t *ip_key(ctdb_sock_addr *ip)
625 static uint32_t key[IP_KEYLEN];
627 bzero(key, sizeof(key));
629 switch (ip->sa.sa_family) {
631 key[0] = ip->ip.sin_addr.s_addr;
634 key[0] = ip->ip6.sin6_addr.s6_addr32[3];
635 key[1] = ip->ip6.sin6_addr.s6_addr32[2];
636 key[2] = ip->ip6.sin6_addr.s6_addr32[1];
637 key[3] = ip->ip6.sin6_addr.s6_addr32[0];
640 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
647 static void *add_ip_callback(void *parm, void *data)
653 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
655 struct ctdb_all_public_ips *tmp_ips;
656 struct ctdb_node_map *nodemap=NULL;
657 trbt_tree_t *ip_tree;
661 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
663 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
667 ip_tree = trbt_create(tmp_ctx, 0);
669 for(i=0;i<nodemap->num;i++){
670 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
674 /* read the public ip list from this node */
675 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
677 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
681 for (j=0; j<tmp_ips->num;j++) {
682 struct ctdb_public_ip *node_ip;
684 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
685 node_ip->pnn = tmp_ips->ips[j].pnn;
686 node_ip->addr = tmp_ips->ips[j].addr;
688 trbt_insertarray32_callback(ip_tree,
689 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
693 talloc_free(tmp_ips);
698 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
700 len = offsetof(struct ctdb_all_public_ips, ips) +
701 count*sizeof(struct ctdb_public_ip);
702 tmp_ips = talloc_zero_size(tmp_ctx, len);
703 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
712 * scans all other nodes and returns a pnn for another node that can host this
716 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
718 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
719 struct ctdb_all_public_ips *ips;
720 struct ctdb_node_map *nodemap=NULL;
723 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
725 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
726 talloc_free(tmp_ctx);
730 for(i=0;i<nodemap->num;i++){
731 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
734 if (nodemap->nodes[i].pnn == options.pnn) {
738 /* read the public ip list from this node */
739 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
741 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
745 for (j=0;j<ips->num;j++) {
746 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
747 talloc_free(tmp_ctx);
748 return nodemap->nodes[i].pnn;
754 talloc_free(tmp_ctx);
759 add a public ip address to a node
761 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
767 struct ctdb_control_ip_iface *pub;
768 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
769 struct ctdb_all_public_ips *ips;
772 talloc_free(tmp_ctx);
776 if (!parse_ip_mask(argv[0], &addr, &mask)) {
777 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
778 talloc_free(tmp_ctx);
782 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
784 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
785 talloc_free(tmp_ctx);
790 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
791 pub = talloc_size(tmp_ctx, len);
792 CTDB_NO_MEMORY(ctdb, pub);
796 pub->len = strlen(argv[1])+1;
797 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
799 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
801 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
802 talloc_free(tmp_ctx);
807 /* check if some other node is already serving this ip, if not,
810 for (i=0;i<ips->num;i++) {
811 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
815 /* no one has this ip so we claim it */
817 ret = control_send_release(ctdb, options.pnn, &addr);
819 ret = control_send_release(ctdb, ips->ips[i].pnn, &addr);
823 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));
827 talloc_free(tmp_ctx);
831 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
833 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
835 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
836 struct ctdb_node_map *nodemap=NULL;
837 struct ctdb_all_public_ips *ips;
840 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
842 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
846 /* remove it from the nodes that are not hosting the ip currently */
847 for(i=0;i<nodemap->num;i++){
848 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
851 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
852 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
856 for (j=0;j<ips->num;j++) {
857 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
865 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
869 options.pnn = nodemap->nodes[i].pnn;
870 control_delip(ctdb, argc, argv);
874 /* remove it from every node (also the one hosting it) */
875 for(i=0;i<nodemap->num;i++){
876 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
879 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
880 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
884 for (j=0;j<ips->num;j++) {
885 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
893 options.pnn = nodemap->nodes[i].pnn;
894 control_delip(ctdb, argc, argv);
897 talloc_free(tmp_ctx);
902 delete a public ip address from a node
904 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
908 struct ctdb_control_ip_iface pub;
909 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
910 struct ctdb_all_public_ips *ips;
913 talloc_free(tmp_ctx);
917 if (parse_ip(argv[0], &addr) == 0) {
918 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
922 if (options.pnn == CTDB_BROADCAST_ALL) {
923 return control_delip_all(ctdb, argc, argv, &addr);
930 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
932 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
933 talloc_free(tmp_ctx);
937 for (i=0;i<ips->num;i++) {
938 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
944 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
945 ctdb_addr_to_str(&addr)));
946 talloc_free(tmp_ctx);
950 if (ips->ips[i].pnn == options.pnn) {
951 ret = find_other_host_for_public_ip(ctdb, &addr);
953 ret = control_send_release(ctdb, ret, &addr);
955 DEBUG(DEBUG_ERR, ("Failed to migrate this ip to another node. Use moveip of recover to reassign this address to a node\n"));
960 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
962 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
963 talloc_free(tmp_ctx);
967 talloc_free(tmp_ctx);
972 kill a tcp connection
974 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
977 struct ctdb_control_killtcp killtcp;
983 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
984 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
988 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
989 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
993 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
995 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1006 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1009 ctdb_sock_addr addr;
1015 if (!parse_ip(argv[0], &addr)) {
1016 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1020 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1022 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1030 register a server id
1032 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1035 struct ctdb_server_id server_id;
1041 server_id.pnn = strtoul(argv[0], NULL, 0);
1042 server_id.type = strtoul(argv[1], NULL, 0);
1043 server_id.server_id = strtoul(argv[2], NULL, 0);
1045 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1047 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1054 unregister a server id
1056 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1059 struct ctdb_server_id server_id;
1065 server_id.pnn = strtoul(argv[0], NULL, 0);
1066 server_id.type = strtoul(argv[1], NULL, 0);
1067 server_id.server_id = strtoul(argv[2], NULL, 0);
1069 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
1071 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
1078 check if a server id exists
1080 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1084 struct ctdb_server_id server_id;
1090 server_id.pnn = strtoul(argv[0], NULL, 0);
1091 server_id.type = strtoul(argv[1], NULL, 0);
1092 server_id.server_id = strtoul(argv[2], NULL, 0);
1094 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
1096 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
1101 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
1103 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
1109 get a list of all server ids that are registered on a node
1111 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
1114 struct ctdb_server_id_list *server_ids;
1116 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
1118 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
1122 for (i=0; i<server_ids->num; i++) {
1123 printf("Server id %d:%d:%d\n",
1124 server_ids->server_ids[i].pnn,
1125 server_ids->server_ids[i].type,
1126 server_ids->server_ids[i].server_id);
1133 send a tcp tickle ack
1135 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1138 ctdb_sock_addr src, dst;
1144 if (!parse_ip_port(argv[0], &src)) {
1145 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1149 if (!parse_ip_port(argv[1], &dst)) {
1150 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1154 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
1158 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
1165 display public ip status
1167 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
1170 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1171 struct ctdb_all_public_ips *ips;
1173 if (options.pnn == CTDB_BROADCAST_ALL) {
1174 /* read the list of public ips from all nodes */
1175 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1177 /* read the public ip list from this node */
1178 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1181 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
1182 talloc_free(tmp_ctx);
1186 if (options.machinereadable){
1187 printf(":Public IP:Node:\n");
1189 if (options.pnn == CTDB_BROADCAST_ALL) {
1190 printf("Public IPs on ALL nodes\n");
1192 printf("Public IPs on node %u\n", options.pnn);
1196 for (i=1;i<=ips->num;i++) {
1197 if (options.machinereadable){
1198 printf(":%s:%d:\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1200 printf("%s %d\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1204 talloc_free(tmp_ctx);
1209 display pid of a ctdb daemon
1211 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
1216 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
1218 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
1221 printf("Pid:%d\n", pid);
1227 disable a remote node
1229 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
1233 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
1235 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
1243 enable a disabled remote node
1245 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
1249 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
1251 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
1259 ban a node from the cluster
1261 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
1264 struct ctdb_ban_info b;
1272 /* verify we can access the node */
1273 ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1275 DEBUG(DEBUG_ERR,("Can not ban node. Node is not operational.\n"));
1279 ban_time = strtoul(argv[0], NULL, 0);
1281 b.pnn = options.pnn;
1282 b.ban_time = ban_time;
1284 data.dptr = (uint8_t *)&b;
1285 data.dsize = sizeof(b);
1287 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
1289 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
1298 unban a node from the cluster
1300 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
1305 /* verify we can access the node */
1306 ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1308 DEBUG(DEBUG_ERR,("Can not unban node. Node is not operational.\n"));
1312 data.dptr = (uint8_t *)&options.pnn;
1313 data.dsize = sizeof(uint32_t);
1315 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
1317 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
1328 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
1332 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
1334 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
1344 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
1348 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1350 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
1354 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
1356 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
1365 display monitoring mode of a remote node
1367 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
1372 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
1374 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
1377 if (!options.machinereadable){
1378 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
1381 printf(":%d:\n",monmode);
1388 display capabilities of a remote node
1390 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
1392 uint32_t capabilities;
1395 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
1397 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
1401 if (!options.machinereadable){
1402 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
1403 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
1404 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
1406 printf(":RECMASTER:LMASTER:LVS:\n");
1407 printf(":%d:%d:%d:\n",
1408 !!(capabilities&CTDB_CAP_RECMASTER),
1409 !!(capabilities&CTDB_CAP_LMASTER),
1410 !!(capabilities&CTDB_CAP_LVS));
1416 display lvs configuration
1418 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
1420 uint32_t *capabilities;
1421 struct ctdb_node_map *nodemap=NULL;
1423 int healthy_count = 0;
1425 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1427 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1431 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1432 CTDB_NO_MEMORY(ctdb, capabilities);
1434 /* collect capabilities for all connected nodes */
1435 for (i=0; i<nodemap->num; i++) {
1436 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1439 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1443 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1445 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1449 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1453 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1458 /* Print all LVS nodes */
1459 for (i=0; i<nodemap->num; i++) {
1460 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1463 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1466 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1470 if (healthy_count != 0) {
1471 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1476 printf("%d:%s\n", i,
1477 ctdb_addr_to_str(&nodemap->nodes[i].addr));
1484 display who is the lvs master
1486 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1488 uint32_t *capabilities;
1489 struct ctdb_node_map *nodemap=NULL;
1491 int healthy_count = 0;
1493 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1495 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1499 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1500 CTDB_NO_MEMORY(ctdb, capabilities);
1502 /* collect capabilities for all connected nodes */
1503 for (i=0; i<nodemap->num; i++) {
1504 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1507 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1511 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1513 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1517 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1521 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1526 /* find and show the lvsmaster */
1527 for (i=0; i<nodemap->num; i++) {
1528 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1531 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1534 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1538 if (healthy_count != 0) {
1539 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1544 printf("Node %d is LVS master\n", i);
1548 printf("There is no LVS master\n");
1553 disable monitoring on a node
1555 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1560 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
1562 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
1565 printf("Monitoring mode:%s\n","DISABLED");
1571 enable monitoring on a node
1573 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1578 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
1580 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
1583 printf("Monitoring mode:%s\n","ACTIVE");
1589 display remote list of keys/data for a db
1591 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
1593 const char *db_name;
1594 struct ctdb_db_context *ctdb_db;
1604 if (db_exists(ctdb, db_name)) {
1605 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
1609 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
1611 if (ctdb_db == NULL) {
1612 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1616 /* traverse and dump the cluster tdb */
1617 ret = ctdb_dump_db(ctdb_db, stdout);
1619 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
1622 talloc_free(ctdb_db);
1624 printf("Dumped %d records\n", ret);
1630 display a list of the databases on a remote ctdb
1632 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
1635 struct ctdb_dbid_map *dbmap=NULL;
1637 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
1639 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
1643 printf("Number of databases:%d\n", dbmap->num);
1644 for(i=0;i<dbmap->num;i++){
1649 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
1650 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
1651 persistent = dbmap->dbs[i].persistent;
1652 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name,
1653 path, persistent?"PERSISTENT":"");
1660 check if the local node is recmaster or not
1661 it will return 1 if this node is the recmaster and 0 if it is not
1662 or if the local ctdb daemon could not be contacted
1664 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1666 uint32_t mypnn, recmaster;
1669 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1671 printf("Failed to get pnn of node\n");
1675 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1677 printf("Failed to get the recmaster\n");
1681 if (recmaster != mypnn) {
1682 printf("this node is not the recmaster\n");
1686 printf("this node is the recmaster\n");
1693 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
1696 struct timeval tv = timeval_current();
1697 ret = ctdb_ctrl_ping(ctdb, options.pnn);
1699 printf("Unable to get ping response from node %u\n", options.pnn);
1701 printf("response from %u time=%.6f sec (%d clients)\n",
1702 options.pnn, timeval_elapsed(&tv), ret);
1711 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
1722 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
1724 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
1728 printf("%-19s = %u\n", name, value);
1735 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
1746 value = strtoul(argv[1], NULL, 0);
1748 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
1750 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
1759 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
1765 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
1767 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
1771 for (i=0;i<count;i++) {
1772 control_getvar(ctdb, 1, &list[i]);
1782 const char *description;
1783 } debug_levels[] = {
1784 {DEBUG_EMERG, "EMERG"},
1785 {DEBUG_ALERT, "ALERT"},
1786 {DEBUG_CRIT, "CRIT"},
1788 {DEBUG_WARNING, "WARNING"},
1789 {DEBUG_NOTICE, "NOTICE"},
1790 {DEBUG_INFO, "INFO"},
1791 {DEBUG_DEBUG, "DEBUG"}
1794 static const char *get_debug_by_level(int32_t level)
1798 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1799 if (debug_levels[i].level == level) {
1800 return debug_levels[i].description;
1806 static int32_t get_debug_by_desc(const char *desc)
1810 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1811 if (!strcmp(debug_levels[i].description, desc)) {
1812 return debug_levels[i].level;
1816 fprintf(stderr, "Invalid debug level '%s'\nMust be one of\n", desc);
1817 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1818 fprintf(stderr, " %s\n", debug_levels[i].description);
1825 display debug level on a node
1827 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1832 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1834 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
1837 if (options.machinereadable){
1838 printf(":Name:Level:\n");
1839 printf(":%s:%d:\n",get_debug_by_level(level),level);
1841 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
1849 set debug level on a node or all nodes
1851 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1860 if (isalpha(argv[0][0])) {
1861 level = get_debug_by_desc(argv[0]);
1863 level = strtol(argv[0], NULL, 0);
1866 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1868 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1877 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1881 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1883 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
1891 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
1895 ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
1897 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
1904 attach to a database
1906 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
1908 const char *db_name;
1909 struct ctdb_db_context *ctdb_db;
1916 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
1917 if (ctdb_db == NULL) {
1918 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1926 run an eventscript on a node
1928 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
1934 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1937 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
1941 data.dptr = (unsigned char *)discard_const(argv[0]);
1942 data.dsize = strlen((char *)data.dptr) + 1;
1944 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
1946 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
1947 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
1948 if (ret != 0 || res != 0) {
1949 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
1950 talloc_free(tmp_ctx);
1953 talloc_free(tmp_ctx);
1957 #define DB_VERSION 1
1958 #define MAX_DB_NAME 64
1959 struct db_file_header {
1960 unsigned long version;
1962 unsigned long persistent;
1964 const char name[MAX_DB_NAME];
1967 struct backup_data {
1968 struct ctdb_marshall_buffer *records;
1971 bool traverse_error;
1974 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
1976 struct backup_data *bd = talloc_get_type(private, struct backup_data);
1977 struct ctdb_rec_data *rec;
1979 /* add the record */
1980 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
1982 bd->traverse_error = true;
1983 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
1986 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
1987 if (bd->records == NULL) {
1988 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
1989 bd->traverse_error = true;
1992 bd->records->count++;
1993 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
1994 bd->len += rec->length;
2002 * backup a database to a file
2004 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
2007 struct ctdb_dbid_map *dbmap=NULL;
2008 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2009 struct db_file_header dbhdr;
2010 struct ctdb_db_context *ctdb_db;
2011 struct backup_data *bd;
2015 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2019 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
2021 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
2025 for(i=0;i<dbmap->num;i++){
2028 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
2029 if(!strcmp(argv[0], name)){
2030 talloc_free(discard_const(name));
2033 talloc_free(discard_const(name));
2035 if (i == dbmap->num) {
2036 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
2037 talloc_free(tmp_ctx);
2042 ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
2043 if (ctdb_db == NULL) {
2044 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
2049 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
2051 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
2052 talloc_free(tmp_ctx);
2057 bd = talloc_zero(tmp_ctx, struct backup_data);
2059 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
2060 talloc_free(tmp_ctx);
2064 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
2065 if (bd->records == NULL) {
2066 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
2067 talloc_free(tmp_ctx);
2071 bd->len = offsetof(struct ctdb_marshall_buffer, data);
2072 bd->records->db_id = ctdb_db->db_id;
2073 /* traverse the database collecting all records */
2074 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
2075 bd->traverse_error) {
2076 DEBUG(DEBUG_ERR,("Traverse error\n"));
2077 talloc_free(tmp_ctx);
2081 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
2084 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
2086 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
2087 talloc_free(tmp_ctx);
2091 dbhdr.version = DB_VERSION;
2092 dbhdr.timestamp = time(NULL);
2093 dbhdr.persistent = dbmap->dbs[i].persistent;
2094 dbhdr.size = bd->len;
2095 if (strlen(argv[0]) >= MAX_DB_NAME) {
2096 DEBUG(DEBUG_ERR,("Too long dbname\n"));
2097 talloc_free(tmp_ctx);
2100 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
2101 write(fh, &dbhdr, sizeof(dbhdr));
2102 write(fh, bd->records, bd->len);
2105 talloc_free(tmp_ctx);
2110 * restore a database from a file
2112 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
2115 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2118 struct db_file_header dbhdr;
2119 struct ctdb_db_context *ctdb_db;
2120 struct ctdb_node_map *nodemap=NULL;
2121 struct ctdb_vnn_map *vnnmap=NULL;
2123 struct ctdb_control_wipe_database w;
2125 uint32_t generation;
2130 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2134 fh = open(argv[0], O_RDONLY);
2136 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
2137 talloc_free(tmp_ctx);
2141 read(fh, &dbhdr, sizeof(dbhdr));
2142 if (dbhdr.version != DB_VERSION) {
2143 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
2144 talloc_free(tmp_ctx);
2148 outdata.dsize = dbhdr.size;
2149 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
2150 if (outdata.dptr == NULL) {
2151 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
2153 talloc_free(tmp_ctx);
2156 read(fh, outdata.dptr, outdata.dsize);
2159 tm = localtime(&dbhdr.timestamp);
2160 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
2161 printf("Restoring database '%s' from backup @ %s\n",
2165 ctdb_db = ctdb_attach(ctdb, dbhdr.name, dbhdr.persistent, 0);
2166 if (ctdb_db == NULL) {
2167 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbhdr.name));
2168 talloc_free(tmp_ctx);
2172 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2174 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2175 talloc_free(tmp_ctx);
2180 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
2182 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
2183 talloc_free(tmp_ctx);
2187 /* freeze all nodes */
2188 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2189 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
2194 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
2195 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2196 talloc_free(tmp_ctx);
2200 generation = vnnmap->generation;
2201 data.dptr = (void *)&generation;
2202 data.dsize = sizeof(generation);
2204 /* start a cluster wide transaction */
2205 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2206 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
2208 TIMELIMIT(), false, data,
2211 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
2216 w.db_id = ctdb_db->db_id;
2217 w.transaction_id = generation;
2219 data.dptr = (void *)&w;
2220 data.dsize = sizeof(w);
2222 /* wipe all the remote databases. */
2223 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2224 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
2226 TIMELIMIT(), false, data,
2229 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
2230 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2231 talloc_free(tmp_ctx);
2235 /* push the database */
2236 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2237 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
2239 TIMELIMIT(), false, outdata,
2242 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
2243 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2244 talloc_free(tmp_ctx);
2248 data.dptr = (void *)&generation;
2249 data.dsize = sizeof(generation);
2251 /* commit all the changes */
2252 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
2254 TIMELIMIT(), false, data,
2257 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
2258 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2259 talloc_free(tmp_ctx);
2264 /* thaw all nodes */
2265 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2266 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
2271 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
2272 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2273 talloc_free(tmp_ctx);
2278 talloc_free(tmp_ctx);
2285 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2291 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2292 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
2293 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
2294 if (ret != 0 || res != 0) {
2295 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
2296 talloc_free(tmp_ctx);
2299 write(1, data.dptr, data.dsize);
2300 talloc_free(tmp_ctx);
2305 handler for memory dumps
2307 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
2308 TDB_DATA data, void *private_data)
2310 write(1, data.dptr, data.dsize);
2315 dump memory usage on the recovery daemon
2317 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2321 struct rd_memdump_reply rd;
2323 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2325 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
2328 rd.srvid = getpid();
2330 /* register a message port for receiveing the reply so that we
2331 can receive the reply
2333 ctdb_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
2336 data.dptr = (uint8_t *)&rd;
2337 data.dsize = sizeof(rd);
2339 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
2341 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
2345 /* this loop will terminate when we have received the reply */
2347 event_loop_once(ctdb->ev);
2354 list all nodes in the cluster
2356 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
2359 struct ctdb_node_map *nodemap=NULL;
2361 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2363 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2367 for(i=0;i<nodemap->num;i++){
2368 printf("%s\n", ctdb_addr_to_str(&nodemap->nodes[i].addr));
2375 reload the nodes file on the local node
2377 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
2381 struct ctdb_node_map *nodemap=NULL;
2383 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2385 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
2389 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2391 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2395 /* reload the nodes file on all remote nodes */
2396 for (i=0;i<nodemap->num;i++) {
2397 if (nodemap->nodes[i].pnn == mypnn) {
2400 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
2401 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
2402 nodemap->nodes[i].pnn);
2404 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
2408 /* reload the nodes file on the local node */
2409 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
2410 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
2412 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
2415 /* initiate a recovery */
2416 control_recover(ctdb, argc, argv);
2422 static const struct {
2424 int (*fn)(struct ctdb_context *, int, const char **);
2428 } ctdb_commands[] = {
2430 { "version", control_version, true, "show version of ctdb" },
2432 { "status", control_status, true, "show node status" },
2433 { "uptime", control_uptime, true, "show node uptime" },
2434 { "ping", control_ping, true, "ping all nodes" },
2435 { "getvar", control_getvar, true, "get a tunable variable", "<name>"},
2436 { "setvar", control_setvar, true, "set a tunable variable", "<name> <value>"},
2437 { "listvars", control_listvars, true, "list tunable variables"},
2438 { "statistics", control_statistics, false, "show statistics" },
2439 { "statisticsreset", control_statistics_reset, true, "reset statistics"},
2440 { "ip", control_ip, false, "show which public ip's that ctdb manages" },
2441 { "process-exists", control_process_exists, true, "check if a process exists on a node", "<pid>"},
2442 { "getdbmap", control_getdbmap, true, "show the database map" },
2443 { "catdb", control_catdb, true, "dump a database" , "<dbname>"},
2444 { "getmonmode", control_getmonmode, true, "show monitoring mode" },
2445 { "getcapabilities", control_getcapabilities, true, "show node capabilities" },
2446 { "pnn", control_pnn, true, "show the pnn of the currnet node" },
2447 { "lvs", control_lvs, true, "show lvs configuration" },
2448 { "lvsmaster", control_lvsmaster, true, "show which node is the lvs master" },
2449 { "disablemonitor", control_disable_monmode, true, "set monitoring mode to DISABLE" },
2450 { "enablemonitor", control_enable_monmode, true, "set monitoring mode to ACTIVE" },
2451 { "setdebug", control_setdebug, true, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
2452 { "getdebug", control_getdebug, true, "get debug level" },
2453 { "attach", control_attach, true, "attach to a database", "<dbname>" },
2454 { "dumpmemory", control_dumpmemory, true, "dump memory map to stdout" },
2455 { "rddumpmemory", control_rddumpmemory, true, "dump memory map from the recovery daemon to stdout" },
2456 { "getpid", control_getpid, true, "get ctdbd process ID" },
2457 { "disable", control_disable, true, "disable a nodes public IP" },
2458 { "enable", control_enable, true, "enable a nodes public IP" },
2459 { "ban", control_ban, true, "ban a node from the cluster", "<bantime|0>"},
2460 { "unban", control_unban, true, "unban a node from the cluster" },
2461 { "shutdown", control_shutdown, true, "shutdown ctdbd" },
2462 { "recover", control_recover, true, "force recovery" },
2463 { "freeze", control_freeze, true, "freeze all databases" },
2464 { "thaw", control_thaw, true, "thaw all databases" },
2465 { "isnotrecmaster", control_isnotrecmaster, false, "check if the local node is recmaster or not" },
2466 { "killtcp", kill_tcp, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
2467 { "gratiousarp", control_gratious_arp, false, "send a gratious arp", "<ip> <interface>" },
2468 { "tickle", tickle_tcp, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
2469 { "gettickles", control_get_tickles, false, "get the list of tickles registered for this ip", "<ip>" },
2471 { "regsrvid", regsrvid, false, "register a server id", "<pnn> <type> <id>" },
2472 { "unregsrvid", unregsrvid, false, "unregister a server id", "<pnn> <type> <id>" },
2473 { "chksrvid", chksrvid, false, "check if a server id exists", "<pnn> <type> <id>" },
2474 { "getsrvids", getsrvids, false, "get a list of all server ids"},
2475 { "vacuum", ctdb_vacuum, false, "vacuum the databases of empty records", "[max_records]"},
2476 { "repack", ctdb_repack, false, "repack all databases", "[max_freelist]"},
2477 { "listnodes", control_listnodes, false, "list all nodes in the cluster"},
2478 { "reloadnodes", control_reload_nodes_file, false, "reload the nodes file and restart the transport on all nodes"},
2479 { "moveip", control_moveip, false, "move/failover an ip address to another node", "<ip> <node>"},
2480 { "addip", control_addip, true, "add a ip address to a node", "<ip/mask> <iface>"},
2481 { "delip", control_delip, false, "delete an ip address from a node", "<ip>"},
2482 { "eventscript", control_eventscript, true, "run the eventscript with the given parameters on a node", "<arguments>"},
2483 { "backupdb", control_backupdb, false, "backup the database into a file.", "<database> <file>"},
2484 { "restoredb", control_restoredb, false, "restore the database from a file.", "<file>"},
2485 { "recmaster", control_recmaster, false, "show the pnn for the recovery master."},
2491 static void usage(void)
2495 "Usage: ctdb [options] <control>\n" \
2497 " -n <node> choose node number, or 'all' (defaults to local node)\n"
2498 " -Y generate machinereadable output\n"
2499 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
2500 printf("Controls:\n");
2501 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2502 printf(" %-15s %-27s %s\n",
2503 ctdb_commands[i].name,
2504 ctdb_commands[i].args?ctdb_commands[i].args:"",
2505 ctdb_commands[i].msg);
2511 static void ctdb_alarm(int sig)
2513 printf("Maximum runtime exceeded - exiting\n");
2520 int main(int argc, const char *argv[])
2522 struct ctdb_context *ctdb;
2523 char *nodestring = NULL;
2524 struct poptOption popt_options[] = {
2527 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
2528 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
2529 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
2530 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
2534 const char **extra_argv;
2538 struct event_context *ev;
2539 const char *control;
2543 /* set some defaults */
2544 options.maxruntime = 0;
2545 options.timelimit = 3;
2546 options.pnn = CTDB_CURRENT_NODE;
2548 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
2550 while ((opt = poptGetNextOpt(pc)) != -1) {
2553 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
2554 poptBadOption(pc, 0), poptStrerror(opt)));
2559 /* setup the remaining options for the main program to use */
2560 extra_argv = poptGetArgs(pc);
2563 while (extra_argv[extra_argc]) extra_argc++;
2566 if (extra_argc < 1) {
2570 if (options.maxruntime != 0) {
2571 signal(SIGALRM, ctdb_alarm);
2572 alarm(options.maxruntime);
2575 /* setup the node number to contact */
2576 if (nodestring != NULL) {
2577 if (strcmp(nodestring, "all") == 0) {
2578 options.pnn = CTDB_BROADCAST_ALL;
2580 options.pnn = strtoul(nodestring, NULL, 0);
2584 control = extra_argv[0];
2586 ev = event_context_init(NULL);
2588 /* initialise ctdb */
2589 ctdb = ctdb_cmdline_client(ev);
2591 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
2595 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2596 if (strcmp(control, ctdb_commands[i].name) == 0) {
2599 if (options.pnn == CTDB_CURRENT_NODE) {
2601 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
2608 if (ctdb_commands[i].auto_all &&
2609 options.pnn == CTDB_BROADCAST_ALL) {
2614 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
2615 CTDB_NO_MEMORY(ctdb, nodes);
2617 for (j=0;j<num_nodes;j++) {
2618 options.pnn = nodes[j];
2619 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2623 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2629 if (i == ARRAY_SIZE(ctdb_commands)) {
2630 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));