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);
476 display the status of the monitoring scripts
478 static int control_scriptstatus(struct ctdb_context *ctdb, int argc, const char **argv)
481 struct ctdb_monitoring_wire *script_status;
483 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, &script_status);
485 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
489 printf("%d scripts were executed last monitoring cycle\n", script_status->num_scripts);
490 for (i=0; i<script_status->num_scripts; i++) {
491 printf("%-20s Status:%s ",
492 script_status->scripts[i].name,
493 script_status->scripts[i].timedout?"TIMEDOUT":script_status->scripts[i].status==0?"OK":"ERROR");
494 if (script_status->scripts[i].timedout == 0) {
495 printf("Duration:%.3lf ",
496 timeval_delta(&script_status->scripts[i].finished,
497 &script_status->scripts[i].start));
500 ctime(&script_status->scripts[i].start.tv_sec));
501 if ((script_status->scripts[i].timedout != 0)
502 || (script_status->scripts[i].status != 0) ) {
503 printf(" OUTPUT:%s\n",
504 script_status->scripts[i].output);
513 display the pnn of the recovery master
515 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
520 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
522 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
525 printf("%d\n",recmaster);
531 get a list of all tickles for this pnn
533 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
535 struct ctdb_control_tcp_tickle_list *list;
543 if (parse_ip(argv[0], NULL, &addr) == 0) {
544 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
548 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
550 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
554 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
555 printf("Num tickles:%u\n", list->tickles.num);
556 for (i=0;i<list->tickles.num;i++) {
557 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
558 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));
566 /* send a release ip to all nodes */
567 static int control_send_release(struct ctdb_context *ctdb, uint32_t pnn,
568 ctdb_sock_addr *addr)
571 struct ctdb_public_ip pip;
573 struct ctdb_node_map *nodemap=NULL;
575 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
577 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
581 /* send a moveip message to the recovery master */
584 data.dsize = sizeof(pip);
585 data.dptr = (unsigned char *)&pip;
588 /* send release ip to all nodes */
589 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
590 list_of_active_nodes(ctdb, nodemap, ctdb, true),
591 TIMELIMIT(), false, data,
592 NULL, NULL, NULL) != 0) {
593 DEBUG(DEBUG_ERR, (__location__ " Unable to send 'ReleaseIP' to all nodes.\n"));
601 move/failover an ip address to a specific node
603 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
608 struct ctdb_all_public_ips *ips;
615 if (parse_ip(argv[0], NULL, &addr) == 0) {
616 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
621 if (sscanf(argv[1], "%u", &pnn) != 1) {
622 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
626 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "DeterministicIPs", &value);
628 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'DeterministicIPs' from local node\n"));
632 DEBUG(DEBUG_ERR, ("The tunable 'DeterministicIPs' is set. You can only move ip addresses when this feature is disabled\n"));
636 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "NoIPFailback", &value);
638 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'NoIPFailback' from local node\n"));
642 DEBUG(DEBUG_ERR, ("The tunable 'NoIPFailback' is NOT set. You can only move ip addresses when this feature is enabled\n"));
646 /* read the public ip list from the node */
647 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
649 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
653 for (i=0;i<ips->num;i++) {
654 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
659 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
660 pnn, ctdb_addr_to_str(&addr)));
663 if (ips->ips[i].pnn == pnn) {
664 DEBUG(DEBUG_ERR, ("Host %u is already hosting '%s'\n",
665 pnn, ctdb_addr_to_str(&ips->ips[i].addr)));
669 ret = control_send_release(ctdb, pnn, &ips->ips[i].addr);
671 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));;
678 void getips_store_callback(void *param, void *data)
680 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
681 struct ctdb_all_public_ips *ips = param;
685 ips->ips[i].pnn = node_ip->pnn;
686 ips->ips[i].addr = node_ip->addr;
689 void getips_count_callback(void *param, void *data)
691 uint32_t *count = param;
697 static uint32_t *ip_key(ctdb_sock_addr *ip)
699 static uint32_t key[IP_KEYLEN];
701 bzero(key, sizeof(key));
703 switch (ip->sa.sa_family) {
705 key[0] = ip->ip.sin_addr.s_addr;
708 key[0] = ip->ip6.sin6_addr.s6_addr32[3];
709 key[1] = ip->ip6.sin6_addr.s6_addr32[2];
710 key[2] = ip->ip6.sin6_addr.s6_addr32[1];
711 key[3] = ip->ip6.sin6_addr.s6_addr32[0];
714 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
721 static void *add_ip_callback(void *parm, void *data)
727 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
729 struct ctdb_all_public_ips *tmp_ips;
730 struct ctdb_node_map *nodemap=NULL;
731 trbt_tree_t *ip_tree;
735 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
737 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
741 ip_tree = trbt_create(tmp_ctx, 0);
743 for(i=0;i<nodemap->num;i++){
744 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
748 /* read the public ip list from this node */
749 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
751 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
755 for (j=0; j<tmp_ips->num;j++) {
756 struct ctdb_public_ip *node_ip;
758 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
759 node_ip->pnn = tmp_ips->ips[j].pnn;
760 node_ip->addr = tmp_ips->ips[j].addr;
762 trbt_insertarray32_callback(ip_tree,
763 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
767 talloc_free(tmp_ips);
772 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
774 len = offsetof(struct ctdb_all_public_ips, ips) +
775 count*sizeof(struct ctdb_public_ip);
776 tmp_ips = talloc_zero_size(tmp_ctx, len);
777 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
786 * scans all other nodes and returns a pnn for another node that can host this
790 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
792 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
793 struct ctdb_all_public_ips *ips;
794 struct ctdb_node_map *nodemap=NULL;
797 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
799 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
800 talloc_free(tmp_ctx);
804 for(i=0;i<nodemap->num;i++){
805 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
808 if (nodemap->nodes[i].pnn == options.pnn) {
812 /* read the public ip list from this node */
813 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
815 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
819 for (j=0;j<ips->num;j++) {
820 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
821 talloc_free(tmp_ctx);
822 return nodemap->nodes[i].pnn;
828 talloc_free(tmp_ctx);
833 add a public ip address to a node
835 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
841 struct ctdb_control_ip_iface *pub;
842 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
843 struct ctdb_all_public_ips *ips;
846 talloc_free(tmp_ctx);
850 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
851 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
852 talloc_free(tmp_ctx);
856 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
858 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
859 talloc_free(tmp_ctx);
864 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
865 pub = talloc_size(tmp_ctx, len);
866 CTDB_NO_MEMORY(ctdb, pub);
870 pub->len = strlen(argv[1])+1;
871 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
873 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
875 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
876 talloc_free(tmp_ctx);
881 /* check if some other node is already serving this ip, if not,
884 for (i=0;i<ips->num;i++) {
885 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
889 /* no one has this ip so we claim it */
891 ret = control_send_release(ctdb, options.pnn, &addr);
893 ret = control_send_release(ctdb, ips->ips[i].pnn, &addr);
897 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));
901 talloc_free(tmp_ctx);
905 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
907 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
909 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
910 struct ctdb_node_map *nodemap=NULL;
911 struct ctdb_all_public_ips *ips;
914 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
916 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
920 /* remove it from the nodes that are not hosting the ip currently */
921 for(i=0;i<nodemap->num;i++){
922 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
925 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
926 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
930 for (j=0;j<ips->num;j++) {
931 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
939 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
943 options.pnn = nodemap->nodes[i].pnn;
944 control_delip(ctdb, argc, argv);
948 /* remove it from every node (also the one hosting it) */
949 for(i=0;i<nodemap->num;i++){
950 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
953 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
954 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
958 for (j=0;j<ips->num;j++) {
959 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
967 options.pnn = nodemap->nodes[i].pnn;
968 control_delip(ctdb, argc, argv);
971 talloc_free(tmp_ctx);
976 delete a public ip address from a node
978 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
982 struct ctdb_control_ip_iface pub;
983 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
984 struct ctdb_all_public_ips *ips;
987 talloc_free(tmp_ctx);
991 if (parse_ip(argv[0], NULL, &addr) == 0) {
992 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
996 if (options.pnn == CTDB_BROADCAST_ALL) {
997 return control_delip_all(ctdb, argc, argv, &addr);
1004 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1006 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1007 talloc_free(tmp_ctx);
1011 for (i=0;i<ips->num;i++) {
1012 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1018 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
1019 ctdb_addr_to_str(&addr)));
1020 talloc_free(tmp_ctx);
1024 if (ips->ips[i].pnn == options.pnn) {
1025 ret = find_other_host_for_public_ip(ctdb, &addr);
1027 ret = control_send_release(ctdb, ret, &addr);
1029 DEBUG(DEBUG_ERR, ("Failed to migrate this ip to another node. Use moveip of recover to reassign this address to a node\n"));
1034 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
1036 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
1037 talloc_free(tmp_ctx);
1041 talloc_free(tmp_ctx);
1046 kill a tcp connection
1048 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1051 struct ctdb_control_killtcp killtcp;
1057 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
1058 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1062 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
1063 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1067 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
1069 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1080 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1083 ctdb_sock_addr addr;
1089 if (!parse_ip(argv[0], NULL, &addr)) {
1090 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1094 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1096 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1104 register a server id
1106 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1109 struct ctdb_server_id server_id;
1115 server_id.pnn = strtoul(argv[0], NULL, 0);
1116 server_id.type = strtoul(argv[1], NULL, 0);
1117 server_id.server_id = strtoul(argv[2], NULL, 0);
1119 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1121 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1128 unregister a server id
1130 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1133 struct ctdb_server_id server_id;
1139 server_id.pnn = strtoul(argv[0], NULL, 0);
1140 server_id.type = strtoul(argv[1], NULL, 0);
1141 server_id.server_id = strtoul(argv[2], NULL, 0);
1143 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
1145 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
1152 check if a server id exists
1154 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1158 struct ctdb_server_id server_id;
1164 server_id.pnn = strtoul(argv[0], NULL, 0);
1165 server_id.type = strtoul(argv[1], NULL, 0);
1166 server_id.server_id = strtoul(argv[2], NULL, 0);
1168 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
1170 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
1175 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
1177 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
1183 get a list of all server ids that are registered on a node
1185 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
1188 struct ctdb_server_id_list *server_ids;
1190 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
1192 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
1196 for (i=0; i<server_ids->num; i++) {
1197 printf("Server id %d:%d:%d\n",
1198 server_ids->server_ids[i].pnn,
1199 server_ids->server_ids[i].type,
1200 server_ids->server_ids[i].server_id);
1207 send a tcp tickle ack
1209 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1212 ctdb_sock_addr src, dst;
1218 if (!parse_ip_port(argv[0], &src)) {
1219 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1223 if (!parse_ip_port(argv[1], &dst)) {
1224 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1228 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
1232 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
1239 display public ip status
1241 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
1244 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1245 struct ctdb_all_public_ips *ips;
1247 if (options.pnn == CTDB_BROADCAST_ALL) {
1248 /* read the list of public ips from all nodes */
1249 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1251 /* read the public ip list from this node */
1252 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1255 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
1256 talloc_free(tmp_ctx);
1260 if (options.machinereadable){
1261 printf(":Public IP:Node:\n");
1263 if (options.pnn == CTDB_BROADCAST_ALL) {
1264 printf("Public IPs on ALL nodes\n");
1266 printf("Public IPs on node %u\n", options.pnn);
1270 for (i=1;i<=ips->num;i++) {
1271 if (options.machinereadable){
1272 printf(":%s:%d:\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1274 printf("%s %d\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1278 talloc_free(tmp_ctx);
1283 display pid of a ctdb daemon
1285 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
1290 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
1292 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
1295 printf("Pid:%d\n", pid);
1301 disable a remote node
1303 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
1307 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
1309 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
1317 enable a disabled remote node
1319 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
1323 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
1325 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
1332 static uint32_t get_generation(struct ctdb_context *ctdb)
1334 struct ctdb_vnn_map *vnnmap=NULL;
1337 /* wait until the recmaster is not in recovery mode */
1339 uint32_t recmode, recmaster;
1341 if (vnnmap != NULL) {
1342 talloc_free(vnnmap);
1346 /* get the recmaster */
1347 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
1349 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1353 /* get recovery mode */
1354 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
1356 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1360 /* get the current generation number */
1361 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
1363 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
1367 if ((recmode == CTDB_RECOVERY_NORMAL)
1368 && (vnnmap->generation != 1)){
1369 return vnnmap->generation;
1376 ban a node from the cluster
1378 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
1381 struct ctdb_ban_info b;
1384 struct ctdb_node_map *nodemap=NULL;
1385 uint32_t generation, next_generation;
1391 /* record the current generation number */
1392 generation = get_generation(ctdb);
1395 /* verify the node exists */
1396 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
1398 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1402 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
1403 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
1407 ban_time = strtoul(argv[0], NULL, 0);
1409 b.pnn = options.pnn;
1410 b.ban_time = ban_time;
1412 data.dptr = (uint8_t *)&b;
1413 data.dsize = sizeof(b);
1415 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
1417 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
1421 /* wait until we are in a new generation */
1423 next_generation = get_generation(ctdb);
1424 if (next_generation != generation) {
1435 unban a node from the cluster
1437 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
1441 uint32_t generation, next_generation;
1443 /* record the current generation number */
1444 generation = get_generation(ctdb);
1446 data.dptr = (uint8_t *)&options.pnn;
1447 data.dsize = sizeof(uint32_t);
1449 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
1451 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
1455 /* wait until we are in a new generation */
1457 next_generation = get_generation(ctdb);
1458 if (next_generation != generation) {
1471 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
1475 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
1477 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
1487 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
1490 uint32_t generation, next_generation;
1492 /* record the current generation number */
1493 generation = get_generation(ctdb);
1495 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1497 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
1501 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
1503 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
1507 /* wait until we are in a new generation */
1509 next_generation = get_generation(ctdb);
1510 if (next_generation != generation) {
1521 display monitoring mode of a remote node
1523 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
1528 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
1530 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
1533 if (!options.machinereadable){
1534 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
1537 printf(":%d:\n",monmode);
1544 display capabilities of a remote node
1546 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
1548 uint32_t capabilities;
1551 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
1553 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
1557 if (!options.machinereadable){
1558 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
1559 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
1560 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
1562 printf(":RECMASTER:LMASTER:LVS:\n");
1563 printf(":%d:%d:%d:\n",
1564 !!(capabilities&CTDB_CAP_RECMASTER),
1565 !!(capabilities&CTDB_CAP_LMASTER),
1566 !!(capabilities&CTDB_CAP_LVS));
1572 display lvs configuration
1574 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
1576 uint32_t *capabilities;
1577 struct ctdb_node_map *nodemap=NULL;
1579 int healthy_count = 0;
1581 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1583 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1587 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1588 CTDB_NO_MEMORY(ctdb, capabilities);
1590 /* collect capabilities for all connected nodes */
1591 for (i=0; i<nodemap->num; i++) {
1592 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1595 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1599 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1601 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1605 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1609 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1614 /* Print all LVS nodes */
1615 for (i=0; i<nodemap->num; i++) {
1616 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1619 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1622 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1626 if (healthy_count != 0) {
1627 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1632 printf("%d:%s\n", i,
1633 ctdb_addr_to_str(&nodemap->nodes[i].addr));
1640 display who is the lvs master
1642 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1644 uint32_t *capabilities;
1645 struct ctdb_node_map *nodemap=NULL;
1647 int healthy_count = 0;
1649 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1651 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1655 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1656 CTDB_NO_MEMORY(ctdb, capabilities);
1658 /* collect capabilities for all connected nodes */
1659 for (i=0; i<nodemap->num; i++) {
1660 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1663 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1667 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1669 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1673 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1677 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1682 /* find and show the lvsmaster */
1683 for (i=0; i<nodemap->num; i++) {
1684 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1687 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1690 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1694 if (healthy_count != 0) {
1695 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1700 printf("Node %d is LVS master\n", i);
1704 printf("There is no LVS master\n");
1709 disable monitoring on a node
1711 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1716 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
1718 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
1721 printf("Monitoring mode:%s\n","DISABLED");
1727 enable monitoring on a node
1729 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1734 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
1736 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
1739 printf("Monitoring mode:%s\n","ACTIVE");
1745 display remote list of keys/data for a db
1747 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
1749 const char *db_name;
1750 struct ctdb_db_context *ctdb_db;
1760 if (db_exists(ctdb, db_name)) {
1761 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
1765 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
1767 if (ctdb_db == NULL) {
1768 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1772 /* traverse and dump the cluster tdb */
1773 ret = ctdb_dump_db(ctdb_db, stdout);
1775 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
1778 talloc_free(ctdb_db);
1780 printf("Dumped %d records\n", ret);
1786 display a list of the databases on a remote ctdb
1788 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
1791 struct ctdb_dbid_map *dbmap=NULL;
1793 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
1795 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
1799 printf("Number of databases:%d\n", dbmap->num);
1800 for(i=0;i<dbmap->num;i++){
1805 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
1806 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
1807 persistent = dbmap->dbs[i].persistent;
1808 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name,
1809 path, persistent?"PERSISTENT":"");
1816 check if the local node is recmaster or not
1817 it will return 1 if this node is the recmaster and 0 if it is not
1818 or if the local ctdb daemon could not be contacted
1820 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1822 uint32_t mypnn, recmaster;
1825 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1827 printf("Failed to get pnn of node\n");
1831 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1833 printf("Failed to get the recmaster\n");
1837 if (recmaster != mypnn) {
1838 printf("this node is not the recmaster\n");
1842 printf("this node is the recmaster\n");
1849 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
1852 struct timeval tv = timeval_current();
1853 ret = ctdb_ctrl_ping(ctdb, options.pnn);
1855 printf("Unable to get ping response from node %u\n", options.pnn);
1858 printf("response from %u time=%.6f sec (%d clients)\n",
1859 options.pnn, timeval_elapsed(&tv), ret);
1868 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
1879 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
1881 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
1885 printf("%-19s = %u\n", name, value);
1892 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
1903 value = strtoul(argv[1], NULL, 0);
1905 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
1907 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
1916 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
1922 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
1924 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
1928 for (i=0;i<count;i++) {
1929 control_getvar(ctdb, 1, &list[i]);
1939 const char *description;
1940 } debug_levels[] = {
1941 {DEBUG_EMERG, "EMERG"},
1942 {DEBUG_ALERT, "ALERT"},
1943 {DEBUG_CRIT, "CRIT"},
1945 {DEBUG_WARNING, "WARNING"},
1946 {DEBUG_NOTICE, "NOTICE"},
1947 {DEBUG_INFO, "INFO"},
1948 {DEBUG_DEBUG, "DEBUG"}
1951 static const char *get_debug_by_level(int32_t level)
1955 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1956 if (debug_levels[i].level == level) {
1957 return debug_levels[i].description;
1963 static int32_t get_debug_by_desc(const char *desc)
1967 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1968 if (!strcmp(debug_levels[i].description, desc)) {
1969 return debug_levels[i].level;
1973 fprintf(stderr, "Invalid debug level '%s'\nMust be one of\n", desc);
1974 for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1975 fprintf(stderr, " %s\n", debug_levels[i].description);
1982 display debug level on a node
1984 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1989 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1991 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
1994 if (options.machinereadable){
1995 printf(":Name:Level:\n");
1996 printf(":%s:%d:\n",get_debug_by_level(level),level);
1998 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
2006 set debug level on a node or all nodes
2008 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
2017 if (isalpha(argv[0][0])) {
2018 level = get_debug_by_desc(argv[0]);
2020 level = strtol(argv[0], NULL, 0);
2023 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
2025 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
2034 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
2038 ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
2040 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
2048 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
2052 ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
2054 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
2061 attach to a database
2063 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
2065 const char *db_name;
2066 struct ctdb_db_context *ctdb_db;
2073 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
2074 if (ctdb_db == NULL) {
2075 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2083 run an eventscript on a node
2085 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
2091 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2094 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2098 data.dptr = (unsigned char *)discard_const(argv[0]);
2099 data.dsize = strlen((char *)data.dptr) + 1;
2101 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
2103 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
2104 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
2105 if (ret != 0 || res != 0) {
2106 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
2107 talloc_free(tmp_ctx);
2110 talloc_free(tmp_ctx);
2114 #define DB_VERSION 1
2115 #define MAX_DB_NAME 64
2116 struct db_file_header {
2117 unsigned long version;
2119 unsigned long persistent;
2121 const char name[MAX_DB_NAME];
2124 struct backup_data {
2125 struct ctdb_marshall_buffer *records;
2128 bool traverse_error;
2131 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
2133 struct backup_data *bd = talloc_get_type(private, struct backup_data);
2134 struct ctdb_rec_data *rec;
2136 /* add the record */
2137 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
2139 bd->traverse_error = true;
2140 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
2143 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
2144 if (bd->records == NULL) {
2145 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
2146 bd->traverse_error = true;
2149 bd->records->count++;
2150 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
2151 bd->len += rec->length;
2159 * backup a database to a file
2161 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
2164 struct ctdb_dbid_map *dbmap=NULL;
2165 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2166 struct db_file_header dbhdr;
2167 struct ctdb_db_context *ctdb_db;
2168 struct backup_data *bd;
2172 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2176 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
2178 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
2182 for(i=0;i<dbmap->num;i++){
2185 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
2186 if(!strcmp(argv[0], name)){
2187 talloc_free(discard_const(name));
2190 talloc_free(discard_const(name));
2192 if (i == dbmap->num) {
2193 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
2194 talloc_free(tmp_ctx);
2199 ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
2200 if (ctdb_db == NULL) {
2201 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
2206 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
2208 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
2209 talloc_free(tmp_ctx);
2214 bd = talloc_zero(tmp_ctx, struct backup_data);
2216 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
2217 talloc_free(tmp_ctx);
2221 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
2222 if (bd->records == NULL) {
2223 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
2224 talloc_free(tmp_ctx);
2228 bd->len = offsetof(struct ctdb_marshall_buffer, data);
2229 bd->records->db_id = ctdb_db->db_id;
2230 /* traverse the database collecting all records */
2231 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
2232 bd->traverse_error) {
2233 DEBUG(DEBUG_ERR,("Traverse error\n"));
2234 talloc_free(tmp_ctx);
2238 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
2241 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
2243 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
2244 talloc_free(tmp_ctx);
2248 dbhdr.version = DB_VERSION;
2249 dbhdr.timestamp = time(NULL);
2250 dbhdr.persistent = dbmap->dbs[i].persistent;
2251 dbhdr.size = bd->len;
2252 if (strlen(argv[0]) >= MAX_DB_NAME) {
2253 DEBUG(DEBUG_ERR,("Too long dbname\n"));
2254 talloc_free(tmp_ctx);
2257 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
2258 write(fh, &dbhdr, sizeof(dbhdr));
2259 write(fh, bd->records, bd->len);
2262 talloc_free(tmp_ctx);
2267 * restore a database from a file
2269 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
2272 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2275 struct db_file_header dbhdr;
2276 struct ctdb_db_context *ctdb_db;
2277 struct ctdb_node_map *nodemap=NULL;
2278 struct ctdb_vnn_map *vnnmap=NULL;
2280 struct ctdb_control_wipe_database w;
2282 uint32_t generation;
2287 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2291 fh = open(argv[0], O_RDONLY);
2293 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
2294 talloc_free(tmp_ctx);
2298 read(fh, &dbhdr, sizeof(dbhdr));
2299 if (dbhdr.version != DB_VERSION) {
2300 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
2301 talloc_free(tmp_ctx);
2305 outdata.dsize = dbhdr.size;
2306 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
2307 if (outdata.dptr == NULL) {
2308 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
2310 talloc_free(tmp_ctx);
2313 read(fh, outdata.dptr, outdata.dsize);
2316 tm = localtime(&dbhdr.timestamp);
2317 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
2318 printf("Restoring database '%s' from backup @ %s\n",
2322 ctdb_db = ctdb_attach(ctdb, dbhdr.name, dbhdr.persistent, 0);
2323 if (ctdb_db == NULL) {
2324 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbhdr.name));
2325 talloc_free(tmp_ctx);
2329 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2331 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2332 talloc_free(tmp_ctx);
2337 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
2339 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
2340 talloc_free(tmp_ctx);
2344 /* freeze all nodes */
2345 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2346 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
2351 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
2352 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2353 talloc_free(tmp_ctx);
2357 generation = vnnmap->generation;
2358 data.dptr = (void *)&generation;
2359 data.dsize = sizeof(generation);
2361 /* start a cluster wide transaction */
2362 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2363 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
2365 TIMELIMIT(), false, data,
2368 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
2373 w.db_id = ctdb_db->db_id;
2374 w.transaction_id = generation;
2376 data.dptr = (void *)&w;
2377 data.dsize = sizeof(w);
2379 /* wipe all the remote databases. */
2380 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2381 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
2383 TIMELIMIT(), false, data,
2386 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
2387 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2388 talloc_free(tmp_ctx);
2392 /* push the database */
2393 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2394 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
2396 TIMELIMIT(), false, outdata,
2399 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
2400 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2401 talloc_free(tmp_ctx);
2405 data.dptr = (void *)&generation;
2406 data.dsize = sizeof(generation);
2408 /* commit all the changes */
2409 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
2411 TIMELIMIT(), false, data,
2414 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
2415 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2416 talloc_free(tmp_ctx);
2421 /* thaw all nodes */
2422 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2423 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
2428 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
2429 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2430 talloc_free(tmp_ctx);
2435 talloc_free(tmp_ctx);
2440 * set flags of a node in the nodemap
2442 static int control_setflags(struct ctdb_context *ctdb, int argc, const char **argv)
2449 struct ctdb_node_flag_change c;
2456 if (sscanf(argv[0], "%d", &node) != 1) {
2457 DEBUG(DEBUG_ERR, ("Badly formed node\n"));
2461 if (sscanf(argv[1], "0x%x", &flags) != 1) {
2462 DEBUG(DEBUG_ERR, ("Badly formed flags\n"));
2469 c.new_flags = flags;
2471 data.dsize = sizeof(c);
2472 data.dptr = (unsigned char *)&c;
2474 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_MODIFY_FLAGS, 0,
2475 data, NULL, NULL, &status, NULL, NULL);
2476 if (ret != 0 || status != 0) {
2477 DEBUG(DEBUG_ERR,("Failed to modify flags\n"));
2486 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2492 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2493 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
2494 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
2495 if (ret != 0 || res != 0) {
2496 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
2497 talloc_free(tmp_ctx);
2500 write(1, data.dptr, data.dsize);
2501 talloc_free(tmp_ctx);
2506 handler for memory dumps
2508 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
2509 TDB_DATA data, void *private_data)
2511 write(1, data.dptr, data.dsize);
2516 dump memory usage on the recovery daemon
2518 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2522 struct rd_memdump_reply rd;
2524 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2526 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
2529 rd.srvid = getpid();
2531 /* register a message port for receiveing the reply so that we
2532 can receive the reply
2534 ctdb_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
2537 data.dptr = (uint8_t *)&rd;
2538 data.dsize = sizeof(rd);
2540 ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
2542 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
2546 /* this loop will terminate when we have received the reply */
2548 event_loop_once(ctdb->ev);
2555 list all nodes in the cluster
2557 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
2560 struct ctdb_node_map *nodemap=NULL;
2562 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2564 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2568 for(i=0;i<nodemap->num;i++){
2569 printf("%s\n", ctdb_addr_to_str(&nodemap->nodes[i].addr));
2576 reload the nodes file on the local node
2578 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
2582 struct ctdb_node_map *nodemap=NULL;
2584 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2586 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
2590 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2592 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2596 /* reload the nodes file on all remote nodes */
2597 for (i=0;i<nodemap->num;i++) {
2598 if (nodemap->nodes[i].pnn == mypnn) {
2601 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
2602 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
2603 nodemap->nodes[i].pnn);
2605 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
2609 /* reload the nodes file on the local node */
2610 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
2611 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
2613 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
2616 /* initiate a recovery */
2617 control_recover(ctdb, argc, argv);
2623 static const struct {
2625 int (*fn)(struct ctdb_context *, int, const char **);
2629 } ctdb_commands[] = {
2631 { "version", control_version, true, "show version of ctdb" },
2633 { "status", control_status, true, "show node status" },
2634 { "uptime", control_uptime, true, "show node uptime" },
2635 { "ping", control_ping, true, "ping all nodes" },
2636 { "getvar", control_getvar, true, "get a tunable variable", "<name>"},
2637 { "setvar", control_setvar, true, "set a tunable variable", "<name> <value>"},
2638 { "listvars", control_listvars, true, "list tunable variables"},
2639 { "statistics", control_statistics, false, "show statistics" },
2640 { "statisticsreset", control_statistics_reset, true, "reset statistics"},
2641 { "ip", control_ip, false, "show which public ip's that ctdb manages" },
2642 { "process-exists", control_process_exists, true, "check if a process exists on a node", "<pid>"},
2643 { "getdbmap", control_getdbmap, true, "show the database map" },
2644 { "catdb", control_catdb, true, "dump a database" , "<dbname>"},
2645 { "getmonmode", control_getmonmode, true, "show monitoring mode" },
2646 { "getcapabilities", control_getcapabilities, true, "show node capabilities" },
2647 { "pnn", control_pnn, true, "show the pnn of the currnet node" },
2648 { "lvs", control_lvs, true, "show lvs configuration" },
2649 { "lvsmaster", control_lvsmaster, true, "show which node is the lvs master" },
2650 { "disablemonitor", control_disable_monmode, true, "set monitoring mode to DISABLE" },
2651 { "enablemonitor", control_enable_monmode, true, "set monitoring mode to ACTIVE" },
2652 { "setdebug", control_setdebug, true, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
2653 { "getdebug", control_getdebug, true, "get debug level" },
2654 { "attach", control_attach, true, "attach to a database", "<dbname>" },
2655 { "dumpmemory", control_dumpmemory, true, "dump memory map to stdout" },
2656 { "rddumpmemory", control_rddumpmemory, true, "dump memory map from the recovery daemon to stdout" },
2657 { "getpid", control_getpid, true, "get ctdbd process ID" },
2658 { "disable", control_disable, true, "disable a nodes public IP" },
2659 { "enable", control_enable, true, "enable a nodes public IP" },
2660 { "ban", control_ban, true, "ban a node from the cluster", "<bantime|0>"},
2661 { "unban", control_unban, true, "unban a node from the cluster" },
2662 { "shutdown", control_shutdown, true, "shutdown ctdbd" },
2663 { "recover", control_recover, true, "force recovery" },
2664 { "freeze", control_freeze, true, "freeze all databases" },
2665 { "thaw", control_thaw, true, "thaw all databases" },
2666 { "isnotrecmaster", control_isnotrecmaster, false, "check if the local node is recmaster or not" },
2667 { "killtcp", kill_tcp, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
2668 { "gratiousarp", control_gratious_arp, false, "send a gratious arp", "<ip> <interface>" },
2669 { "tickle", tickle_tcp, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
2670 { "gettickles", control_get_tickles, false, "get the list of tickles registered for this ip", "<ip>" },
2672 { "regsrvid", regsrvid, false, "register a server id", "<pnn> <type> <id>" },
2673 { "unregsrvid", unregsrvid, false, "unregister a server id", "<pnn> <type> <id>" },
2674 { "chksrvid", chksrvid, false, "check if a server id exists", "<pnn> <type> <id>" },
2675 { "getsrvids", getsrvids, false, "get a list of all server ids"},
2676 { "vacuum", ctdb_vacuum, false, "vacuum the databases of empty records", "[max_records]"},
2677 { "repack", ctdb_repack, false, "repack all databases", "[max_freelist]"},
2678 { "listnodes", control_listnodes, false, "list all nodes in the cluster"},
2679 { "reloadnodes", control_reload_nodes_file, false, "reload the nodes file and restart the transport on all nodes"},
2680 { "moveip", control_moveip, false, "move/failover an ip address to another node", "<ip> <node>"},
2681 { "addip", control_addip, true, "add a ip address to a node", "<ip/mask> <iface>"},
2682 { "delip", control_delip, false, "delete an ip address from a node", "<ip>"},
2683 { "eventscript", control_eventscript, true, "run the eventscript with the given parameters on a node", "<arguments>"},
2684 { "backupdb", control_backupdb, false, "backup the database into a file.", "<database> <file>"},
2685 { "restoredb", control_restoredb, false, "restore the database from a file.", "<file>"},
2686 { "recmaster", control_recmaster, false, "show the pnn for the recovery master."},
2687 { "setflags", control_setflags, false, "set flags for a node in the nodemap.", "<node> <flags>"},
2688 { "scriptstatus", control_scriptstatus, false, "show the status of the monitoring scripts"},
2694 static void usage(void)
2698 "Usage: ctdb [options] <control>\n" \
2700 " -n <node> choose node number, or 'all' (defaults to local node)\n"
2701 " -Y generate machinereadable output\n"
2702 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
2703 printf("Controls:\n");
2704 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2705 printf(" %-15s %-27s %s\n",
2706 ctdb_commands[i].name,
2707 ctdb_commands[i].args?ctdb_commands[i].args:"",
2708 ctdb_commands[i].msg);
2714 static void ctdb_alarm(int sig)
2716 printf("Maximum runtime exceeded - exiting\n");
2723 int main(int argc, const char *argv[])
2725 struct ctdb_context *ctdb;
2726 char *nodestring = NULL;
2727 struct poptOption popt_options[] = {
2730 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
2731 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
2732 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
2733 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
2737 const char **extra_argv;
2741 struct event_context *ev;
2742 const char *control;
2746 /* set some defaults */
2747 options.maxruntime = 0;
2748 options.timelimit = 3;
2749 options.pnn = CTDB_CURRENT_NODE;
2751 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
2753 while ((opt = poptGetNextOpt(pc)) != -1) {
2756 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
2757 poptBadOption(pc, 0), poptStrerror(opt)));
2762 /* setup the remaining options for the main program to use */
2763 extra_argv = poptGetArgs(pc);
2766 while (extra_argv[extra_argc]) extra_argc++;
2769 if (extra_argc < 1) {
2773 if (options.maxruntime == 0) {
2774 const char *ctdb_timeout;
2775 ctdb_timeout = getenv("CTDB_TIMEOUT");
2776 if (ctdb_timeout != NULL) {
2777 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
2780 if (options.maxruntime != 0) {
2781 signal(SIGALRM, ctdb_alarm);
2782 alarm(options.maxruntime);
2785 /* setup the node number to contact */
2786 if (nodestring != NULL) {
2787 if (strcmp(nodestring, "all") == 0) {
2788 options.pnn = CTDB_BROADCAST_ALL;
2790 options.pnn = strtoul(nodestring, NULL, 0);
2794 control = extra_argv[0];
2796 ev = event_context_init(NULL);
2798 /* initialise ctdb */
2799 ctdb = ctdb_cmdline_client(ev);
2801 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
2805 /* verify the node exists */
2808 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2809 if (strcmp(control, ctdb_commands[i].name) == 0) {
2812 if (options.pnn == CTDB_CURRENT_NODE) {
2814 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
2821 if (ctdb_commands[i].auto_all &&
2822 options.pnn == CTDB_BROADCAST_ALL) {
2827 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
2828 CTDB_NO_MEMORY(ctdb, nodes);
2830 for (j=0;j<num_nodes;j++) {
2831 options.pnn = nodes[j];
2832 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2836 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2842 if (i == ARRAY_SIZE(ctdb_commands)) {
2843 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));