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/tevent/tevent.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_client.h"
31 #include "../include/ctdb_private.h"
32 #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 struct ctdb_connection *ctdb_connection;
41 static void usage(void);
49 int printemptyrecords;
56 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
57 #define LONGTIMELIMIT() timeval_current_ofs(options.timelimit*10, 0)
60 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
63 #define XSTR(x) STR(x)
64 printf("CTDB version: %s\n", XSTR(CTDB_VERS));
71 verify that a node exists and is reachable
73 static void verify_node(struct ctdb_context *ctdb)
76 struct ctdb_node_map *nodemap=NULL;
78 if (options.pnn == CTDB_CURRENT_NODE) {
81 if (options.pnn == CTDB_BROADCAST_ALL) {
85 /* verify the node exists */
86 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
87 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
90 if (options.pnn >= nodemap->num) {
91 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", options.pnn));
94 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DELETED) {
95 DEBUG(DEBUG_ERR, ("Node %u is DELETED\n", options.pnn));
98 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DISCONNECTED) {
99 DEBUG(DEBUG_ERR, ("Node %u is DISCONNECTED\n", options.pnn));
103 /* verify we can access the node */
104 ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
106 DEBUG(DEBUG_ERR,("Can not access node. Node is not operational.\n"));
112 check if a database exists
114 static int db_exists(struct ctdb_context *ctdb, const char *db_name, bool *persistent)
117 struct ctdb_dbid_map *dbmap=NULL;
119 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
121 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
125 for(i=0;i<dbmap->num;i++){
128 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
129 if (!strcmp(name, db_name)) {
131 *persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
141 see if a process exists
143 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
151 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
152 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
156 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
158 printf("%u:%u exists\n", pnn, pid);
160 printf("%u:%u does not exist\n", pnn, pid);
166 display statistics structure
168 static void show_statistics(struct ctdb_statistics *s, int show_header)
170 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
172 const char *prefix=NULL;
174 int tmp, days, hours, minutes, seconds;
179 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
180 STATISTICS_FIELD(num_clients),
181 STATISTICS_FIELD(frozen),
182 STATISTICS_FIELD(recovering),
183 STATISTICS_FIELD(num_recoveries),
184 STATISTICS_FIELD(client_packets_sent),
185 STATISTICS_FIELD(client_packets_recv),
186 STATISTICS_FIELD(node_packets_sent),
187 STATISTICS_FIELD(node_packets_recv),
188 STATISTICS_FIELD(keepalive_packets_sent),
189 STATISTICS_FIELD(keepalive_packets_recv),
190 STATISTICS_FIELD(node.req_call),
191 STATISTICS_FIELD(node.reply_call),
192 STATISTICS_FIELD(node.req_dmaster),
193 STATISTICS_FIELD(node.reply_dmaster),
194 STATISTICS_FIELD(node.reply_error),
195 STATISTICS_FIELD(node.req_message),
196 STATISTICS_FIELD(node.req_control),
197 STATISTICS_FIELD(node.reply_control),
198 STATISTICS_FIELD(client.req_call),
199 STATISTICS_FIELD(client.req_message),
200 STATISTICS_FIELD(client.req_control),
201 STATISTICS_FIELD(timeouts.call),
202 STATISTICS_FIELD(timeouts.control),
203 STATISTICS_FIELD(timeouts.traverse),
204 STATISTICS_FIELD(total_calls),
205 STATISTICS_FIELD(pending_calls),
206 STATISTICS_FIELD(lockwait_calls),
207 STATISTICS_FIELD(pending_lockwait_calls),
208 STATISTICS_FIELD(childwrite_calls),
209 STATISTICS_FIELD(pending_childwrite_calls),
210 STATISTICS_FIELD(memory_used),
211 STATISTICS_FIELD(max_hop_count),
213 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
222 if (options.machinereadable){
224 printf("CTDB version:");
225 printf("Current time of statistics:");
226 printf("Statistics collected since:");
227 for (i=0;i<ARRAY_SIZE(fields);i++) {
228 printf("%s:", fields[i].name);
230 printf("num_reclock_ctdbd_latency:");
231 printf("min_reclock_ctdbd_latency:");
232 printf("avg_reclock_ctdbd_latency:");
233 printf("max_reclock_ctdbd_latency:");
235 printf("num_reclock_recd_latency:");
236 printf("min_reclock_recd_latency:");
237 printf("avg_reclock_recd_latency:");
238 printf("max_reclock_recd_latency:");
240 printf("num_call_latency:");
241 printf("min_call_latency:");
242 printf("avg_call_latency:");
243 printf("max_call_latency:");
245 printf("num_lockwait_latency:");
246 printf("min_lockwait_latency:");
247 printf("avg_lockwait_latency:");
248 printf("max_lockwait_latency:");
250 printf("num_childwrite_latency:");
251 printf("min_childwrite_latency:");
252 printf("avg_childwrite_latency:");
253 printf("max_childwrite_latency:");
256 printf("%d:", CTDB_VERSION);
257 printf("%d:", (int)s->statistics_current_time.tv_sec);
258 printf("%d:", (int)s->statistics_start_time.tv_sec);
259 for (i=0;i<ARRAY_SIZE(fields);i++) {
260 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
262 printf("%d:", s->reclock.ctdbd.num);
263 printf("%.6f:", s->reclock.ctdbd.min);
264 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
265 printf("%.6f:", s->reclock.ctdbd.max);
267 printf("%d:", s->reclock.recd.num);
268 printf("%.6f:", s->reclock.recd.min);
269 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
270 printf("%.6f:", s->reclock.recd.max);
272 printf("%d:", s->call_latency.num);
273 printf("%.6f:", s->call_latency.min);
274 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
275 printf("%.6f:", s->call_latency.max);
277 printf("%d:", s->lockwait_latency.num);
278 printf("%.6f:", s->lockwait_latency.min);
279 printf("%.6f:", s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0);
280 printf("%.6f:", s->lockwait_latency.max);
282 printf("%d:", s->childwrite_latency.num);
283 printf("%.6f:", s->childwrite_latency.min);
284 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
285 printf("%.6f:", s->childwrite_latency.max);
288 printf("CTDB version %u\n", CTDB_VERSION);
289 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
290 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
292 for (i=0;i<ARRAY_SIZE(fields);i++) {
293 if (strchr(fields[i].name, '.')) {
294 preflen = strcspn(fields[i].name, ".")+1;
295 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
296 prefix = fields[i].name;
297 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
302 printf(" %*s%-22s%*s%10u\n",
304 fields[i].name+preflen,
306 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
308 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "reclock_ctdbd MIN/AVG/MAX", s->reclock.ctdbd.min, s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0, s->reclock.ctdbd.max, s->reclock.ctdbd.num);
310 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "reclock_recd MIN/AVG/MAX", s->reclock.recd.min, s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0, s->reclock.recd.max, s->reclock.recd.num);
312 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "call_latency MIN/AVG/MAX", s->call_latency.min, s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0, s->call_latency.max, s->call_latency.num);
313 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "lockwait_latency MIN/AVG/MAX", s->lockwait_latency.min, s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0, s->lockwait_latency.max, s->lockwait_latency.num);
314 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "childwrite_latency MIN/AVG/MAX", s->childwrite_latency.min, s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0, s->childwrite_latency.max, s->childwrite_latency.num);
317 talloc_free(tmp_ctx);
321 display remote ctdb statistics combined from all nodes
323 static int control_statistics_all(struct ctdb_context *ctdb)
326 struct ctdb_statistics statistics;
330 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
331 CTDB_NO_MEMORY(ctdb, nodes);
333 ZERO_STRUCT(statistics);
335 for (i=0;i<num_nodes;i++) {
336 struct ctdb_statistics s1;
338 uint32_t *v1 = (uint32_t *)&s1;
339 uint32_t *v2 = (uint32_t *)&statistics;
341 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
342 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
344 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
347 for (j=0;j<num_ints;j++) {
350 statistics.max_hop_count =
351 MAX(statistics.max_hop_count, s1.max_hop_count);
352 statistics.call_latency.max =
353 MAX(statistics.call_latency.max, s1.call_latency.max);
354 statistics.lockwait_latency.max =
355 MAX(statistics.lockwait_latency.max, s1.lockwait_latency.max);
358 printf("Gathered statistics for %u nodes\n", num_nodes);
359 show_statistics(&statistics, 1);
364 display remote ctdb statistics
366 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
369 struct ctdb_statistics statistics;
371 if (options.pnn == CTDB_BROADCAST_ALL) {
372 return control_statistics_all(ctdb);
375 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
377 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
380 show_statistics(&statistics, 1);
386 reset remote ctdb statistics
388 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
392 ret = ctdb_statistics_reset(ctdb, options.pnn);
394 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
402 display remote ctdb rolling statistics
404 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
407 struct ctdb_statistics_wire *stats;
408 int i, num_records = -1;
411 num_records = atoi(argv[0]) - 1;
414 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
416 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
419 for (i=0;i<stats->num;i++) {
420 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
423 show_statistics(&stats->stats[i], i==0);
424 if (i == num_records) {
433 display uptime of remote node
435 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
438 struct ctdb_uptime *uptime = NULL;
439 int tmp, days, hours, minutes, seconds;
441 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
443 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
447 if (options.machinereadable){
448 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
449 printf(":%u:%u:%u:%lf\n",
450 (unsigned int)uptime->current_time.tv_sec,
451 (unsigned int)uptime->ctdbd_start_time.tv_sec,
452 (unsigned int)uptime->last_recovery_finished.tv_sec,
453 timeval_delta(&uptime->last_recovery_finished,
454 &uptime->last_recovery_started)
459 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
461 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
469 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
471 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
479 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
481 printf("Duration of last recovery/failover: %lf seconds\n",
482 timeval_delta(&uptime->last_recovery_finished,
483 &uptime->last_recovery_started));
489 show the PNN of the current node
491 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
496 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
498 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
502 printf("PNN:%d\n", mypnn);
508 struct pnn_node *next;
513 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
515 const char *nodes_list;
519 struct pnn_node *pnn_nodes = NULL;
520 struct pnn_node *pnn_node;
521 struct pnn_node *tmp_node;
523 /* read the nodes file */
524 nodes_list = getenv("CTDB_NODES");
525 if (nodes_list == NULL) {
526 nodes_list = "/etc/ctdb/nodes";
528 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
532 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
535 for (i=0, pnn=0; i<nlines; i++) {
539 /* strip leading spaces */
540 while((*node == ' ') || (*node == '\t')) {
547 if (strcmp(node, "") == 0) {
550 pnn_node = talloc(mem_ctx, struct pnn_node);
551 pnn_node->pnn = pnn++;
552 pnn_node->addr = talloc_strdup(pnn_node, node);
553 pnn_node->next = pnn_nodes;
554 pnn_nodes = pnn_node;
557 /* swap them around so we return them in incrementing order */
558 pnn_node = pnn_nodes;
562 pnn_node = pnn_node->next;
564 tmp_node->next = pnn_nodes;
565 pnn_nodes = tmp_node;
572 show the PNN of the current node
573 discover the pnn by loading the nodes file and try to bind to all
574 addresses one at a time until the ip address is found.
576 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
578 TALLOC_CTX *mem_ctx = talloc_new(NULL);
579 struct pnn_node *pnn_nodes;
580 struct pnn_node *pnn_node;
582 pnn_nodes = read_nodes_file(mem_ctx);
583 if (pnn_nodes == NULL) {
584 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
585 talloc_free(mem_ctx);
589 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
592 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
593 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
594 talloc_free(mem_ctx);
598 if (ctdb_sys_have_ip(&addr)) {
599 printf("PNN:%d\n", pnn_node->pnn);
600 talloc_free(mem_ctx);
605 printf("Failed to detect which PNN this node is\n");
606 talloc_free(mem_ctx);
611 display remote ctdb status
613 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
616 struct ctdb_vnn_map *vnnmap=NULL;
617 struct ctdb_node_map *nodemap=NULL;
618 uint32_t recmode, recmaster;
621 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
626 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
628 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
632 if (options.machinereadable) {
633 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
634 ":Inactive:PartiallyOnline:ThisNode:\n");
635 for (i=0;i<nodemap->num;i++) {
636 int partially_online = 0;
639 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
642 if (nodemap->nodes[i].flags == 0) {
643 struct ctdb_ifaces_list *ifaces;
645 ret = ctdb_getifaces(ctdb_connection,
646 nodemap->nodes[i].pnn,
649 for (j=0; j < ifaces->num; j++) {
650 if (ifaces->ifaces[j].link_state != 0) {
653 partially_online = 1;
656 ctdb_free_ifaces(ifaces);
659 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", nodemap->nodes[i].pnn,
660 ctdb_addr_to_str(&nodemap->nodes[i].addr),
661 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
662 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
663 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
664 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
665 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED),
666 !!(nodemap->nodes[i].flags&NODE_FLAGS_INACTIVE),
668 (nodemap->nodes[i].pnn == mypnn)?'Y':'N');
673 printf("Number of nodes:%d\n", nodemap->num);
674 for(i=0;i<nodemap->num;i++){
675 static const struct {
679 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
680 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
681 { NODE_FLAGS_BANNED, "BANNED" },
682 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
683 { NODE_FLAGS_DELETED, "DELETED" },
684 { NODE_FLAGS_STOPPED, "STOPPED" },
685 { NODE_FLAGS_INACTIVE, "INACTIVE" },
687 char *flags_str = NULL;
690 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
693 if (nodemap->nodes[i].flags == 0) {
694 struct ctdb_control_get_ifaces *ifaces;
696 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(),
697 nodemap->nodes[i].pnn,
700 for (j=0; j < ifaces->num; j++) {
701 if (ifaces->ifaces[j].link_state != 0) {
704 flags_str = talloc_strdup(ctdb, "PARTIALLYONLINE");
710 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
711 if (nodemap->nodes[i].flags & flag_names[j].flag) {
712 if (flags_str == NULL) {
713 flags_str = talloc_strdup(ctdb, flag_names[j].name);
715 flags_str = talloc_asprintf_append(flags_str, "|%s",
718 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
721 if (flags_str == NULL) {
722 flags_str = talloc_strdup(ctdb, "OK");
723 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
725 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
726 ctdb_addr_to_str(&nodemap->nodes[i].addr),
728 nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
729 talloc_free(flags_str);
732 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
734 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
737 if (vnnmap->generation == INVALID_GENERATION) {
738 printf("Generation:INVALID\n");
740 printf("Generation:%d\n",vnnmap->generation);
742 printf("Size:%d\n",vnnmap->size);
743 for(i=0;i<vnnmap->size;i++){
744 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
747 if (!ctdb_getrecmode(ctdb_connection, options.pnn, &recmode)) {
748 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
751 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
753 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
754 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
757 printf("Recovery master:%d\n",recmaster);
764 struct natgw_node *next;
769 display the list of nodes belonging to this natgw configuration
771 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
774 uint32_t capabilities;
775 const char *natgw_list;
778 struct natgw_node *natgw_nodes = NULL;
779 struct natgw_node *natgw_node;
780 struct ctdb_node_map *nodemap=NULL;
783 /* read the natgw nodes file into a linked list */
784 natgw_list = getenv("NATGW_NODES");
785 if (natgw_list == NULL) {
786 natgw_list = "/etc/ctdb/natgw_nodes";
788 lines = file_lines_load(natgw_list, &nlines, ctdb);
790 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
793 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
796 for (i=0;i<nlines;i++) {
800 /* strip leading spaces */
801 while((*node == ' ') || (*node == '\t')) {
807 if (strcmp(node, "") == 0) {
810 natgw_node = talloc(ctdb, struct natgw_node);
811 natgw_node->addr = talloc_strdup(natgw_node, node);
812 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
813 natgw_node->next = natgw_nodes;
814 natgw_nodes = natgw_node;
817 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
819 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
824 while(i<nodemap->num) {
825 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
826 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
831 /* this node was not in the natgw so we just remove it from
834 if ((natgw_node == NULL)
835 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
838 for (j=i+1; j<nodemap->num; j++) {
839 nodemap->nodes[j-1] = nodemap->nodes[j];
848 /* pick a node to be natgwmaster
849 * we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
851 for(i=0;i<nodemap->num;i++){
852 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
853 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
855 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
858 if (!(capabilities&CTDB_CAP_NATGW)) {
861 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
865 /* we couldnt find any healthy node, try unhealthy ones */
866 if (i == nodemap->num) {
867 for(i=0;i<nodemap->num;i++){
868 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
869 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
871 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
874 if (!(capabilities&CTDB_CAP_NATGW)) {
877 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
882 /* unless all nodes are STOPPED, when we pick one anyway */
883 if (i == nodemap->num) {
884 for(i=0;i<nodemap->num;i++){
885 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
886 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
888 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
891 if (!(capabilities&CTDB_CAP_NATGW)) {
894 printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
898 /* or if we still can not find any */
899 if (i == nodemap->num) {
900 printf("-1 0.0.0.0\n");
901 ret = 2; /* matches ENOENT */
905 /* print the pruned list of nodes belonging to this natgw list */
906 for(i=0;i<nodemap->num;i++){
907 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
910 printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
911 ctdb_addr_to_str(&nodemap->nodes[i].addr),
912 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
913 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
914 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
915 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
916 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
923 display the status of the scripts for monitoring (or other events)
925 static int control_one_scriptstatus(struct ctdb_context *ctdb,
926 enum ctdb_eventscript_call type)
928 struct ctdb_scripts_wire *script_status;
931 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
933 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
937 if (script_status == NULL) {
938 if (!options.machinereadable) {
939 printf("%s cycle never run\n",
940 ctdb_eventscript_call_names[type]);
945 if (!options.machinereadable) {
946 printf("%d scripts were executed last %s cycle\n",
947 script_status->num_scripts,
948 ctdb_eventscript_call_names[type]);
950 for (i=0; i<script_status->num_scripts; i++) {
951 const char *status = NULL;
953 switch (script_status->scripts[i].status) {
964 if (script_status->scripts[i].status > 0)
968 if (options.machinereadable) {
969 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
970 ctdb_eventscript_call_names[type],
971 script_status->scripts[i].name,
972 script_status->scripts[i].status,
974 (long)script_status->scripts[i].start.tv_sec,
975 (long)script_status->scripts[i].start.tv_usec,
976 (long)script_status->scripts[i].finished.tv_sec,
977 (long)script_status->scripts[i].finished.tv_usec,
978 script_status->scripts[i].output);
982 printf("%-20s Status:%s ",
983 script_status->scripts[i].name, status);
985 /* Some other error, eg from stat. */
986 printf("%-20s Status:CANNOT RUN (%s)",
987 script_status->scripts[i].name,
988 strerror(-script_status->scripts[i].status));
990 if (script_status->scripts[i].status >= 0) {
991 printf("Duration:%.3lf ",
992 timeval_delta(&script_status->scripts[i].finished,
993 &script_status->scripts[i].start));
995 if (script_status->scripts[i].status != -ENOEXEC) {
997 ctime(&script_status->scripts[i].start.tv_sec));
998 if (script_status->scripts[i].status != 0) {
999 printf(" OUTPUT:%s\n",
1000 script_status->scripts[i].output);
1010 static int control_scriptstatus(struct ctdb_context *ctdb,
1011 int argc, const char **argv)
1014 enum ctdb_eventscript_call type, min, max;
1018 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1023 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1027 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1028 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1034 if (type == CTDB_EVENT_MAX) {
1035 if (strcmp(arg, "all") == 0) {
1037 max = CTDB_EVENT_MAX;
1039 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1044 if (options.machinereadable) {
1045 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1048 for (type = min; type < max; type++) {
1049 ret = control_one_scriptstatus(ctdb, type);
1059 enable an eventscript
1061 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1069 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1071 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1079 disable an eventscript
1081 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1089 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1091 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1099 display the pnn of the recovery master
1101 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1105 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1106 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1109 printf("%d\n",recmaster);
1115 add a tickle to a public address
1117 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1119 struct ctdb_tcp_connection t;
1127 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1128 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1131 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1132 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1136 data.dptr = (uint8_t *)&t;
1137 data.dsize = sizeof(t);
1139 /* tell all nodes about this tcp connection */
1140 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1141 0, data, ctdb, NULL, NULL, NULL, NULL);
1143 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1152 delete a tickle from a node
1154 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1156 struct ctdb_tcp_connection t;
1164 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1165 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1168 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1169 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1173 data.dptr = (uint8_t *)&t;
1174 data.dsize = sizeof(t);
1176 /* tell all nodes about this tcp connection */
1177 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1178 0, data, ctdb, NULL, NULL, NULL, NULL);
1180 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1189 get a list of all tickles for this pnn
1191 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1193 struct ctdb_control_tcp_tickle_list *list;
1194 ctdb_sock_addr addr;
1203 port = atoi(argv[1]);
1206 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1207 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1211 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1213 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1217 if (options.machinereadable){
1218 printf(":source ip:port:destination ip:port:\n");
1219 for (i=0;i<list->tickles.num;i++) {
1220 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1223 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1224 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1227 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1228 printf("Num tickles:%u\n", list->tickles.num);
1229 for (i=0;i<list->tickles.num;i++) {
1230 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1233 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1234 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));
1244 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1246 struct ctdb_all_public_ips *ips;
1247 struct ctdb_public_ip ip;
1250 uint32_t disable_time;
1252 struct ctdb_node_map *nodemap=NULL;
1253 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1256 data.dptr = (uint8_t*)&disable_time;
1257 data.dsize = sizeof(disable_time);
1258 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1260 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1266 /* read the public ip list from the node */
1267 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1269 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1270 talloc_free(tmp_ctx);
1274 for (i=0;i<ips->num;i++) {
1275 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1280 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1281 pnn, ctdb_addr_to_str(addr)));
1282 talloc_free(tmp_ctx);
1289 data.dptr = (uint8_t *)&ip;
1290 data.dsize = sizeof(ip);
1292 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1294 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1295 talloc_free(tmp_ctx);
1299 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1300 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1307 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1308 talloc_free(tmp_ctx);
1312 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1314 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1315 talloc_free(tmp_ctx);
1319 /* update the recovery daemon so it now knows to expect the new
1320 node assignment for this ip.
1322 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1324 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1328 talloc_free(tmp_ctx);
1333 move/failover an ip address to a specific node
1335 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1338 int ret, retries = 0;
1339 ctdb_sock_addr addr;
1346 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1347 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1352 if (sscanf(argv[1], "%u", &pnn) != 1) {
1353 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1358 ret = move_ip(ctdb, &addr, pnn);
1360 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 second and try again.\n", pnn));
1364 } while (retries < 5 && ret != 0);
1366 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1373 static int getips_store_callback(void *param, void *data)
1375 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1376 struct ctdb_all_public_ips *ips = param;
1380 ips->ips[i].pnn = node_ip->pnn;
1381 ips->ips[i].addr = node_ip->addr;
1385 static int getips_count_callback(void *param, void *data)
1387 uint32_t *count = param;
1394 static uint32_t *ip_key(ctdb_sock_addr *ip)
1396 static uint32_t key[IP_KEYLEN];
1398 bzero(key, sizeof(key));
1400 switch (ip->sa.sa_family) {
1402 key[0] = ip->ip.sin_addr.s_addr;
1405 key[0] = ip->ip6.sin6_addr.s6_addr32[3];
1406 key[1] = ip->ip6.sin6_addr.s6_addr32[2];
1407 key[2] = ip->ip6.sin6_addr.s6_addr32[1];
1408 key[3] = ip->ip6.sin6_addr.s6_addr32[0];
1411 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1418 static void *add_ip_callback(void *parm, void *data)
1424 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1426 struct ctdb_all_public_ips *tmp_ips;
1427 struct ctdb_node_map *nodemap=NULL;
1428 trbt_tree_t *ip_tree;
1432 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1434 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1438 ip_tree = trbt_create(tmp_ctx, 0);
1440 for(i=0;i<nodemap->num;i++){
1441 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1444 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1448 /* read the public ip list from this node */
1449 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1451 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1455 for (j=0; j<tmp_ips->num;j++) {
1456 struct ctdb_public_ip *node_ip;
1458 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1459 node_ip->pnn = tmp_ips->ips[j].pnn;
1460 node_ip->addr = tmp_ips->ips[j].addr;
1462 trbt_insertarray32_callback(ip_tree,
1463 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1467 talloc_free(tmp_ips);
1472 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1474 len = offsetof(struct ctdb_all_public_ips, ips) +
1475 count*sizeof(struct ctdb_public_ip);
1476 tmp_ips = talloc_zero_size(tmp_ctx, len);
1477 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1486 * scans all other nodes and returns a pnn for another node that can host this
1490 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1492 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1493 struct ctdb_all_public_ips *ips;
1494 struct ctdb_node_map *nodemap=NULL;
1497 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1499 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1500 talloc_free(tmp_ctx);
1504 for(i=0;i<nodemap->num;i++){
1505 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1508 if (nodemap->nodes[i].pnn == options.pnn) {
1512 /* read the public ip list from this node */
1513 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1515 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1519 for (j=0;j<ips->num;j++) {
1520 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1521 talloc_free(tmp_ctx);
1522 return nodemap->nodes[i].pnn;
1528 talloc_free(tmp_ctx);
1532 static uint32_t ipreallocate_finished;
1535 handler for receiving the response to ipreallocate
1537 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
1538 TDB_DATA data, void *private_data)
1540 ipreallocate_finished = 1;
1543 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1545 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1547 event_add_timed(ctdb->ev, ctdb,
1548 timeval_current_ofs(1, 0),
1549 ctdb_every_second, ctdb);
1553 ask the recovery daemon on the recovery master to perform a ip reallocation
1555 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
1559 struct takeover_run_reply rd;
1561 struct ctdb_node_map *nodemap=NULL;
1563 struct timeval tv = timeval_current();
1565 /* we need some events to trigger so we can timeout and restart
1568 event_add_timed(ctdb->ev, ctdb,
1569 timeval_current_ofs(1, 0),
1570 ctdb_every_second, ctdb);
1572 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1574 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1577 rd.srvid = getpid();
1579 /* register a message port for receiveing the reply so that we
1580 can receive the reply
1582 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1584 data.dptr = (uint8_t *)&rd;
1585 data.dsize = sizeof(rd);
1588 /* check that there are valid nodes available */
1589 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
1590 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1593 for (i=0; i<nodemap->num;i++) {
1594 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
1598 if (i==nodemap->num) {
1599 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
1604 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1605 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1609 /* verify the node exists */
1610 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
1611 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1616 /* check tha there are nodes available that can act as a recmaster */
1617 for (i=0; i<nodemap->num; i++) {
1618 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1623 if (i == nodemap->num) {
1624 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
1628 /* verify the recovery master is not STOPPED, nor BANNED */
1629 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1630 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1636 /* verify the recovery master is not STOPPED, nor BANNED */
1637 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1638 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1644 ipreallocate_finished = 0;
1645 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
1647 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
1651 tv = timeval_current();
1652 /* this loop will terminate when we have received the reply */
1653 while (timeval_elapsed(&tv) < 5.0 && ipreallocate_finished == 0) {
1654 event_loop_once(ctdb->ev);
1656 if (ipreallocate_finished == 1) {
1669 add a public ip address to a node
1671 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
1674 int len, retries = 0;
1676 ctdb_sock_addr addr;
1677 struct ctdb_control_ip_iface *pub;
1678 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1679 struct ctdb_all_public_ips *ips;
1683 talloc_free(tmp_ctx);
1687 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1688 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1689 talloc_free(tmp_ctx);
1693 /* read the public ip list from the node */
1694 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1696 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
1697 talloc_free(tmp_ctx);
1700 for (i=0;i<ips->num;i++) {
1701 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1702 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
1709 /* Dont timeout. This command waits for an ip reallocation
1710 which sometimes can take wuite a while if there has
1711 been a recent recovery
1715 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1716 pub = talloc_size(tmp_ctx, len);
1717 CTDB_NO_MEMORY(ctdb, pub);
1721 pub->len = strlen(argv[1])+1;
1722 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1725 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1727 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
1731 } while (retries < 5 && ret != 0);
1733 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
1734 talloc_free(tmp_ctx);
1739 ret = control_ipreallocate(ctdb, argc, argv);
1741 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Wait 3 seconds and try again.\n", options.pnn));
1745 } while (retries < 5 && ret != 0);
1747 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Giving up.\n", options.pnn));
1748 talloc_free(tmp_ctx);
1752 talloc_free(tmp_ctx);
1756 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
1758 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
1760 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1761 struct ctdb_node_map *nodemap=NULL;
1762 struct ctdb_all_public_ips *ips;
1765 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1767 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
1771 /* remove it from the nodes that are not hosting the ip currently */
1772 for(i=0;i<nodemap->num;i++){
1773 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1776 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1777 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1781 for (j=0;j<ips->num;j++) {
1782 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1790 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
1794 options.pnn = nodemap->nodes[i].pnn;
1795 control_delip(ctdb, argc, argv);
1799 /* remove it from every node (also the one hosting it) */
1800 for(i=0;i<nodemap->num;i++){
1801 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1804 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1805 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1809 for (j=0;j<ips->num;j++) {
1810 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1818 options.pnn = nodemap->nodes[i].pnn;
1819 control_delip(ctdb, argc, argv);
1822 talloc_free(tmp_ctx);
1827 delete a public ip address from a node
1829 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
1833 ctdb_sock_addr addr;
1834 struct ctdb_control_ip_iface pub;
1835 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1836 struct ctdb_all_public_ips *ips;
1839 talloc_free(tmp_ctx);
1843 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1844 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1848 if (options.pnn == CTDB_BROADCAST_ALL) {
1849 return control_delip_all(ctdb, argc, argv, &addr);
1856 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1858 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1859 talloc_free(tmp_ctx);
1863 for (i=0;i<ips->num;i++) {
1864 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1870 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
1871 ctdb_addr_to_str(&addr)));
1872 talloc_free(tmp_ctx);
1876 if (ips->ips[i].pnn == options.pnn) {
1877 ret = find_other_host_for_public_ip(ctdb, &addr);
1880 ret = move_ip(ctdb, &addr, ret);
1882 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 seconds and try again.\n", options.pnn));
1886 } while (retries < 5 && ret != 0);
1888 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", options.pnn));
1894 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
1896 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
1897 talloc_free(tmp_ctx);
1901 talloc_free(tmp_ctx);
1906 kill a tcp connection
1908 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1911 struct ctdb_control_killtcp killtcp;
1917 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
1918 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1922 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
1923 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1927 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
1929 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1940 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1943 ctdb_sock_addr addr;
1949 if (!parse_ip(argv[0], NULL, 0, &addr)) {
1950 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1954 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1956 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1964 register a server id
1966 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1969 struct ctdb_server_id server_id;
1975 server_id.pnn = strtoul(argv[0], NULL, 0);
1976 server_id.type = strtoul(argv[1], NULL, 0);
1977 server_id.server_id = strtoul(argv[2], NULL, 0);
1979 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1981 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1984 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
1990 unregister a server id
1992 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1995 struct ctdb_server_id server_id;
2001 server_id.pnn = strtoul(argv[0], NULL, 0);
2002 server_id.type = strtoul(argv[1], NULL, 0);
2003 server_id.server_id = strtoul(argv[2], NULL, 0);
2005 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2007 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2014 check if a server id exists
2016 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2020 struct ctdb_server_id server_id;
2026 server_id.pnn = strtoul(argv[0], NULL, 0);
2027 server_id.type = strtoul(argv[1], NULL, 0);
2028 server_id.server_id = strtoul(argv[2], NULL, 0);
2030 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2032 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2037 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2039 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2045 get a list of all server ids that are registered on a node
2047 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2050 struct ctdb_server_id_list *server_ids;
2052 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2054 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2058 for (i=0; i<server_ids->num; i++) {
2059 printf("Server id %d:%d:%d\n",
2060 server_ids->server_ids[i].pnn,
2061 server_ids->server_ids[i].type,
2062 server_ids->server_ids[i].server_id);
2069 check if a server id exists
2071 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2073 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2079 talloc_free(tmp_ctx);
2083 ids = talloc_array(tmp_ctx, uint64_t, argc);
2084 result = talloc_array(tmp_ctx, uint8_t, argc);
2086 for (i = 0; i < argc; i++) {
2087 ids[i] = strtoull(argv[i], NULL, 0);
2090 if (!ctdb_check_message_handlers(ctdb_connection,
2091 options.pnn, argc, ids, result)) {
2092 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2094 talloc_free(tmp_ctx);
2098 for (i=0; i < argc; i++) {
2099 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2100 result[i] ? "exists" : "does not exist");
2103 talloc_free(tmp_ctx);
2108 send a tcp tickle ack
2110 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2113 ctdb_sock_addr src, dst;
2119 if (!parse_ip_port(argv[0], &src)) {
2120 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2124 if (!parse_ip_port(argv[1], &dst)) {
2125 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2129 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2133 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2140 display public ip status
2142 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2145 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2146 struct ctdb_all_public_ips *ips;
2148 if (options.pnn == CTDB_BROADCAST_ALL) {
2149 /* read the list of public ips from all nodes */
2150 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2152 /* read the public ip list from this node */
2153 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2156 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2157 talloc_free(tmp_ctx);
2161 if (options.machinereadable){
2162 printf(":Public IP:Node:");
2163 if (options.verbose){
2164 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2168 if (options.pnn == CTDB_BROADCAST_ALL) {
2169 printf("Public IPs on ALL nodes\n");
2171 printf("Public IPs on node %u\n", options.pnn);
2175 for (i=1;i<=ips->num;i++) {
2176 struct ctdb_control_public_ip_info *info = NULL;
2178 char *aciface = NULL;
2179 char *avifaces = NULL;
2180 char *cifaces = NULL;
2182 if (options.pnn == CTDB_BROADCAST_ALL) {
2183 pnn = ips->ips[ips->num-i].pnn;
2189 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2190 &ips->ips[ips->num-i].addr, &info);
2197 for (j=0; j < info->num; j++) {
2198 if (cifaces == NULL) {
2199 cifaces = talloc_strdup(info,
2200 info->ifaces[j].name);
2202 cifaces = talloc_asprintf_append(cifaces,
2204 info->ifaces[j].name);
2207 if (info->active_idx == j) {
2208 aciface = info->ifaces[j].name;
2211 if (info->ifaces[j].link_state == 0) {
2215 if (avifaces == NULL) {
2216 avifaces = talloc_strdup(info, info->ifaces[j].name);
2218 avifaces = talloc_asprintf_append(avifaces,
2220 info->ifaces[j].name);
2225 if (options.machinereadable){
2227 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2228 ips->ips[ips->num-i].pnn);
2229 if (options.verbose){
2232 avifaces?avifaces:"",
2233 cifaces?cifaces:"");
2237 if (options.verbose) {
2238 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2239 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2240 ips->ips[ips->num-i].pnn,
2242 avifaces?avifaces:"",
2243 cifaces?cifaces:"");
2246 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2247 ips->ips[ips->num-i].pnn);
2253 talloc_free(tmp_ctx);
2260 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2263 ctdb_sock_addr addr;
2264 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2265 struct ctdb_control_public_ip_info *info;
2268 talloc_free(tmp_ctx);
2272 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2273 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2277 /* read the public ip info from this node */
2278 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2279 tmp_ctx, &addr, &info);
2281 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2282 argv[0], options.pnn));
2283 talloc_free(tmp_ctx);
2287 printf("Public IP[%s] info on node %u\n",
2288 ctdb_addr_to_str(&info->ip.addr),
2291 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2292 ctdb_addr_to_str(&info->ip.addr),
2293 info->ip.pnn, info->num);
2295 for (i=0; i<info->num; i++) {
2296 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2298 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2299 i+1, info->ifaces[i].name,
2300 info->ifaces[i].link_state?"up":"down",
2301 (unsigned int)info->ifaces[i].references,
2302 (i==info->active_idx)?" (active)":"");
2305 talloc_free(tmp_ctx);
2310 display interfaces status
2312 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2315 struct ctdb_ifaces_list *ifaces;
2317 /* read the public ip list from this node */
2318 if (!ctdb_getifaces(ctdb_connection, options.pnn, &ifaces)) {
2319 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2324 if (options.machinereadable){
2325 printf(":Name:LinkStatus:References:\n");
2327 printf("Interfaces on node %u\n", options.pnn);
2330 for (i=0; i<ifaces->num; i++) {
2331 if (options.machinereadable){
2332 printf(":%s:%s:%u\n",
2333 ifaces->ifaces[i].name,
2334 ifaces->ifaces[i].link_state?"1":"0",
2335 (unsigned int)ifaces->ifaces[i].references);
2337 printf("name:%s link:%s references:%u\n",
2338 ifaces->ifaces[i].name,
2339 ifaces->ifaces[i].link_state?"up":"down",
2340 (unsigned int)ifaces->ifaces[i].references);
2344 ctdb_free_ifaces(ifaces);
2350 set link status of an interface
2352 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2355 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2356 struct ctdb_control_iface_info info;
2364 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2365 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2367 talloc_free(tmp_ctx);
2370 strcpy(info.name, argv[0]);
2372 if (strcmp(argv[1], "up") == 0) {
2373 info.link_state = 1;
2374 } else if (strcmp(argv[1], "down") == 0) {
2375 info.link_state = 0;
2377 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2379 talloc_free(tmp_ctx);
2383 /* read the public ip list from this node */
2384 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2387 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2388 argv[0], options.pnn));
2389 talloc_free(tmp_ctx);
2393 talloc_free(tmp_ctx);
2398 display pid of a ctdb daemon
2400 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2405 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2407 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2410 printf("Pid:%d\n", pid);
2416 disable a remote node
2418 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2421 struct ctdb_node_map *nodemap=NULL;
2423 /* check if the node is already disabled */
2424 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2425 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2428 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2429 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2434 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2436 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2442 /* read the nodemap and verify the change took effect */
2443 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2444 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2448 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2449 ret = control_ipreallocate(ctdb, argc, argv);
2451 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2459 enable a disabled remote node
2461 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2465 struct ctdb_node_map *nodemap=NULL;
2468 /* check if the node is already enabled */
2469 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2470 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2473 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2474 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2479 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2481 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2487 /* read the nodemap and verify the change took effect */
2488 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2489 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2493 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2495 ret = control_ipreallocate(ctdb, argc, argv);
2497 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2507 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2510 struct ctdb_node_map *nodemap=NULL;
2513 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2515 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2520 /* read the nodemap and verify the change took effect */
2521 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2522 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2526 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2527 ret = control_ipreallocate(ctdb, argc, argv);
2529 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2537 restart a stopped remote node
2539 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2543 struct ctdb_node_map *nodemap=NULL;
2546 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2548 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2554 /* read the nodemap and verify the change took effect */
2555 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2556 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2560 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2561 ret = control_ipreallocate(ctdb, argc, argv);
2563 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2570 static uint32_t get_generation(struct ctdb_context *ctdb)
2572 struct ctdb_vnn_map *vnnmap=NULL;
2575 /* wait until the recmaster is not in recovery mode */
2577 uint32_t recmode, recmaster;
2579 if (vnnmap != NULL) {
2580 talloc_free(vnnmap);
2584 /* get the recmaster */
2585 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
2586 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2590 /* get recovery mode */
2591 if (!ctdb_getrecmode(ctdb_connection, recmaster, &recmode)) {
2592 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2596 /* get the current generation number */
2597 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2599 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2603 if ((recmode == CTDB_RECOVERY_NORMAL)
2604 && (vnnmap->generation != 1)){
2605 return vnnmap->generation;
2612 ban a node from the cluster
2614 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2617 struct ctdb_node_map *nodemap=NULL;
2618 struct ctdb_ban_time bantime;
2624 /* verify the node exists */
2625 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2627 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2631 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2632 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2636 bantime.pnn = options.pnn;
2637 bantime.time = strtoul(argv[0], NULL, 0);
2639 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2641 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2645 ret = control_ipreallocate(ctdb, argc, argv);
2647 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2656 unban a node from the cluster
2658 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2661 struct ctdb_node_map *nodemap=NULL;
2662 struct ctdb_ban_time bantime;
2664 /* verify the node exists */
2665 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2667 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2671 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2672 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2676 bantime.pnn = options.pnn;
2679 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2681 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2685 ret = control_ipreallocate(ctdb, argc, argv);
2687 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2696 show ban information for a node
2698 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2701 struct ctdb_node_map *nodemap=NULL;
2702 struct ctdb_ban_time *bantime;
2704 /* verify the node exists */
2705 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2707 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2711 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2713 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2717 if (bantime->time == 0) {
2718 printf("Node %u is not banned\n", bantime->pnn);
2720 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2729 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2733 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2735 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2745 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2748 uint32_t generation, next_generation;
2750 /* record the current generation number */
2751 generation = get_generation(ctdb);
2753 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2755 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
2759 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2761 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2765 /* wait until we are in a new generation */
2767 next_generation = get_generation(ctdb);
2768 if (next_generation != generation) {
2779 display monitoring mode of a remote node
2781 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2786 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2788 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2791 if (!options.machinereadable){
2792 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2795 printf(":%d:\n",monmode);
2802 display capabilities of a remote node
2804 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2806 uint32_t capabilities;
2809 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2811 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2815 if (!options.machinereadable){
2816 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2817 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2818 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
2819 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
2821 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
2822 printf(":%d:%d:%d:%d:\n",
2823 !!(capabilities&CTDB_CAP_RECMASTER),
2824 !!(capabilities&CTDB_CAP_LMASTER),
2825 !!(capabilities&CTDB_CAP_LVS),
2826 !!(capabilities&CTDB_CAP_NATGW));
2832 display lvs configuration
2834 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
2836 uint32_t *capabilities;
2837 struct ctdb_node_map *nodemap=NULL;
2839 int healthy_count = 0;
2841 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2843 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2847 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2848 CTDB_NO_MEMORY(ctdb, capabilities);
2850 /* collect capabilities for all connected nodes */
2851 for (i=0; i<nodemap->num; i++) {
2852 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2855 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2859 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2861 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2865 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2869 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2874 /* Print all LVS nodes */
2875 for (i=0; i<nodemap->num; i++) {
2876 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2879 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2882 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2886 if (healthy_count != 0) {
2887 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2892 printf("%d:%s\n", i,
2893 ctdb_addr_to_str(&nodemap->nodes[i].addr));
2900 display who is the lvs master
2902 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
2904 uint32_t *capabilities;
2905 struct ctdb_node_map *nodemap=NULL;
2907 int healthy_count = 0;
2909 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2911 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2915 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2916 CTDB_NO_MEMORY(ctdb, capabilities);
2918 /* collect capabilities for all connected nodes */
2919 for (i=0; i<nodemap->num; i++) {
2920 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2923 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2927 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2929 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2933 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2937 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2942 /* find and show the lvsmaster */
2943 for (i=0; i<nodemap->num; i++) {
2944 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2947 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2950 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2954 if (healthy_count != 0) {
2955 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2960 if (options.machinereadable){
2963 printf("Node %d is LVS master\n", i);
2968 printf("There is no LVS master\n");
2973 disable monitoring on a node
2975 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2980 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
2982 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
2985 printf("Monitoring mode:%s\n","DISABLED");
2991 enable monitoring on a node
2993 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2998 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3000 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3003 printf("Monitoring mode:%s\n","ACTIVE");
3009 display remote list of keys/data for a db
3011 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3013 const char *db_name;
3014 struct ctdb_db_context *ctdb_db;
3017 struct ctdb_dump_db_context c;
3026 if (db_exists(ctdb, db_name, &persistent)) {
3027 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3031 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3033 if (ctdb_db == NULL) {
3034 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3038 if (options.printlmaster) {
3039 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3040 ctdb, &ctdb->vnn_map);
3042 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3050 c.printemptyrecords = (bool)options.printemptyrecords;
3051 c.printdatasize = (bool)options.printdatasize;
3052 c.printlmaster = (bool)options.printlmaster;
3053 c.printhash = (bool)options.printhash;
3054 c.printrecordflags = (bool)options.printrecordflags;
3056 /* traverse and dump the cluster tdb */
3057 ret = ctdb_dump_db(ctdb_db, &c);
3059 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3060 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3061 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3065 talloc_free(ctdb_db);
3067 printf("Dumped %d records\n", ret);
3071 struct cattdb_data {
3072 struct ctdb_context *ctdb;
3076 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3078 struct cattdb_data *d = private_data;
3079 struct ctdb_dump_db_context c;
3085 c.printemptyrecords = (bool)options.printemptyrecords;
3086 c.printdatasize = (bool)options.printdatasize;
3087 c.printlmaster = false;
3088 c.printhash = (bool)options.printhash;
3089 c.printrecordflags = true;
3091 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3095 cat the local tdb database using same format as catdb
3097 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3099 const char *db_name;
3100 struct ctdb_db_context *ctdb_db;
3101 struct cattdb_data d;
3111 if (db_exists(ctdb, db_name, &persistent)) {
3112 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3116 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, false, 0);
3118 if (ctdb_db == NULL) {
3119 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3123 /* traverse the local tdb */
3126 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3127 printf("Failed to cattdb data\n");
3130 talloc_free(ctdb_db);
3132 printf("Dumped %d records\n", d.count);
3137 display the content of a database key
3139 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3141 const char *db_name;
3142 struct ctdb_db_context *ctdb_db;
3143 struct ctdb_record_handle *h;
3144 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3155 if (db_exists(ctdb, db_name, &persistent)) {
3156 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3160 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3162 if (ctdb_db == NULL) {
3163 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3167 key.dptr = discard_const(argv[1]);
3168 key.dsize = strlen((char *)key.dptr);
3170 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3172 printf("Failed to fetch record '%s' on node %d\n",
3173 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3174 talloc_free(tmp_ctx);
3178 printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3180 talloc_free(ctdb_db);
3181 talloc_free(tmp_ctx);
3186 display the content of a database key
3188 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3190 const char *db_name;
3191 struct ctdb_db_context *ctdb_db;
3192 struct ctdb_record_handle *h;
3193 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3204 if (db_exists(ctdb, db_name, &persistent)) {
3205 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3209 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3211 if (ctdb_db == NULL) {
3212 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3216 key.dptr = discard_const(argv[1]);
3217 key.dsize = strlen((char *)key.dptr);
3219 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3221 printf("Failed to fetch record '%s' on node %d\n",
3222 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3223 talloc_free(tmp_ctx);
3227 data.dptr = discard_const(argv[2]);
3228 data.dsize = strlen((char *)data.dptr);
3230 if (ctdb_record_store(h, data) != 0) {
3231 printf("Failed to store record\n");
3235 talloc_free(ctdb_db);
3236 talloc_free(tmp_ctx);
3241 fetch a record from a persistent database
3243 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3245 const char *db_name;
3246 struct ctdb_db_context *ctdb_db;
3247 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3248 struct ctdb_transaction_handle *h;
3254 talloc_free(tmp_ctx);
3261 if (db_exists(ctdb, db_name, &persistent)) {
3262 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3263 talloc_free(tmp_ctx);
3268 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3269 talloc_free(tmp_ctx);
3273 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3275 if (ctdb_db == NULL) {
3276 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3277 talloc_free(tmp_ctx);
3281 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3283 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3284 talloc_free(tmp_ctx);
3288 key.dptr = discard_const(argv[1]);
3289 key.dsize = strlen(argv[1]);
3290 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3292 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3293 talloc_free(tmp_ctx);
3297 if (data.dsize == 0 || data.dptr == NULL) {
3298 DEBUG(DEBUG_ERR,("Record is empty\n"));
3299 talloc_free(tmp_ctx);
3304 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3306 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3307 talloc_free(tmp_ctx);
3310 write(fd, data.dptr, data.dsize);
3313 write(1, data.dptr, data.dsize);
3316 /* abort the transaction */
3320 talloc_free(tmp_ctx);
3325 fetch a record from a tdb-file
3327 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3329 const char *tdb_file;
3340 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3342 DEBUG(DEBUG_ERR,("Failed to open TDB file %s\n", tdb_file));
3346 key.dptr = discard_const(argv[1]);
3347 key.dsize = strlen(argv[1]);
3348 data = tdb_fetch(tdb, key);
3349 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3350 DEBUG(DEBUG_ERR,("Failed to read record %s from tdb %s\n", argv[1], tdb_file));
3358 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3360 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3363 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3366 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3373 write a record to a persistent database
3375 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3377 const char *db_name;
3378 struct ctdb_db_context *ctdb_db;
3379 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3380 struct ctdb_transaction_handle *h;
3386 talloc_free(tmp_ctx);
3390 fd = open(argv[2], O_RDONLY);
3392 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3393 talloc_free(tmp_ctx);
3397 ret = fstat(fd, &st);
3399 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3401 talloc_free(tmp_ctx);
3405 if (!S_ISREG(st.st_mode)) {
3406 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3408 talloc_free(tmp_ctx);
3412 data.dsize = st.st_size;
3413 if (data.dsize == 0) {
3416 data.dptr = talloc_size(tmp_ctx, data.dsize);
3417 if (data.dptr == NULL) {
3418 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3420 talloc_free(tmp_ctx);
3423 ret = read(fd, data.dptr, data.dsize);
3424 if (ret != data.dsize) {
3425 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3427 talloc_free(tmp_ctx);
3436 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3437 if (ctdb_db == NULL) {
3438 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3439 talloc_free(tmp_ctx);
3443 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3445 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3446 talloc_free(tmp_ctx);
3450 key.dptr = discard_const(argv[1]);
3451 key.dsize = strlen(argv[1]);
3452 ret = ctdb_transaction_store(h, key, data);
3454 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3455 talloc_free(tmp_ctx);
3459 ret = ctdb_transaction_commit(h);
3461 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3462 talloc_free(tmp_ctx);
3467 talloc_free(tmp_ctx);
3472 check if a service is bound to a port or not
3474 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
3479 struct sockaddr_in sin;
3482 printf("Use: ctdb chktcport <port>\n");
3486 port = atoi(argv[0]);
3488 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
3490 printf("Failed to open local socket\n");
3494 v = fcntl(s, F_GETFL, 0);
3495 fcntl(s, F_SETFL, v | O_NONBLOCK);
3497 bzero(&sin, sizeof(sin));
3498 sin.sin_family = PF_INET;
3499 sin.sin_port = htons(port);
3500 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
3503 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
3512 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
3513 TDB_DATA data, void *private_data)
3515 DEBUG(DEBUG_ERR,("Log data received\n"));
3516 if (data.dsize > 0) {
3517 printf("%s", data.dptr);
3524 display a list of log messages from the in memory ringbuffer
3526 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3530 struct ctdb_get_log_addr log_addr;
3532 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3537 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3538 talloc_free(tmp_ctx);
3542 log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3543 log_addr.srvid = getpid();
3544 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3545 log_addr.level = get_debug_by_desc(argv[0]);
3547 log_addr.level = strtol(argv[0], NULL, 0);
3551 data.dptr = (unsigned char *)&log_addr;
3552 data.dsize = sizeof(log_addr);
3554 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3556 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3559 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3561 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3562 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3563 if (ret != 0 || res != 0) {
3564 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3565 talloc_free(tmp_ctx);
3570 tv = timeval_current();
3571 /* this loop will terminate when we have received the reply */
3572 while (timeval_elapsed(&tv) < 3.0) {
3573 event_loop_once(ctdb->ev);
3576 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3578 talloc_free(tmp_ctx);
3583 clear the in memory log area
3585 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3590 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3592 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3593 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3594 if (ret != 0 || res != 0) {
3595 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3596 talloc_free(tmp_ctx);
3600 talloc_free(tmp_ctx);
3607 display a list of the databases on a remote ctdb
3609 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3612 struct ctdb_dbid_map *dbmap=NULL;
3614 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3616 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3620 if(options.machinereadable){
3621 printf(":ID:Name:Path:Persistent:Unhealthy:ReadOnly:\n");
3622 for(i=0;i<dbmap->num;i++){
3629 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3630 dbmap->dbs[i].dbid, ctdb, &path);
3631 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3632 dbmap->dbs[i].dbid, ctdb, &name);
3633 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3634 dbmap->dbs[i].dbid, ctdb, &health);
3635 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3636 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3637 printf(":0x%08X:%s:%s:%d:%d:%d:\n",
3638 dbmap->dbs[i].dbid, name, path,
3639 !!(persistent), !!(health), !!(readonly));
3644 printf("Number of databases:%d\n", dbmap->num);
3645 for(i=0;i<dbmap->num;i++){
3652 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3653 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3654 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3655 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3656 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3657 printf("dbid:0x%08x name:%s path:%s%s%s%s\n",
3658 dbmap->dbs[i].dbid, name, path,
3659 persistent?" PERSISTENT":"",
3660 readonly?" READONLY":"",
3661 health?" UNHEALTHY":"");
3668 display the status of a database on a remote ctdb
3670 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3673 struct ctdb_dbid_map *dbmap=NULL;
3674 const char *db_name;
3682 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3684 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3688 for(i=0;i<dbmap->num;i++){
3695 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3696 if (strcmp(name, db_name) != 0) {
3700 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3701 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3702 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3703 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3704 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nREADONLY: %s\nHEALTH: %s\n",
3705 dbmap->dbs[i].dbid, name, path,
3706 persistent?"yes":"no",
3707 readonly?"yes":"no",
3708 health?health:"OK");
3712 DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
3717 check if the local node is recmaster or not
3718 it will return 1 if this node is the recmaster and 0 if it is not
3719 or if the local ctdb daemon could not be contacted
3721 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3723 uint32_t mypnn, recmaster;
3725 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
3727 printf("Failed to get pnn of node\n");
3731 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
3732 printf("Failed to get the recmaster\n");
3736 if (recmaster != mypnn) {
3737 printf("this node is not the recmaster\n");
3741 printf("this node is the recmaster\n");
3748 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
3751 struct timeval tv = timeval_current();
3752 ret = ctdb_ctrl_ping(ctdb, options.pnn);
3754 printf("Unable to get ping response from node %u\n", options.pnn);
3757 printf("response from %u time=%.6f sec (%d clients)\n",
3758 options.pnn, timeval_elapsed(&tv), ret);
3767 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
3778 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
3780 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
3784 printf("%-23s = %u\n", name, value);
3791 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
3802 value = strtoul(argv[1], NULL, 0);
3804 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
3806 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
3815 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
3821 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
3823 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
3827 for (i=0;i<count;i++) {
3828 control_getvar(ctdb, 1, &list[i]);
3837 display debug level on a node
3839 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3844 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
3846 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
3849 if (options.machinereadable){
3850 printf(":Name:Level:\n");
3851 printf(":%s:%d:\n",get_debug_by_level(level),level);
3853 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
3860 display reclock file of a node
3862 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3865 const char *reclock;
3867 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
3869 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3872 if (options.machinereadable){
3873 if (reclock != NULL) {
3874 printf("%s", reclock);
3877 if (reclock == NULL) {
3878 printf("No reclock file used.\n");
3880 printf("Reclock file:%s\n", reclock);
3888 set the reclock file of a node
3890 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3893 const char *reclock;
3897 } else if (argc == 1) {
3903 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
3905 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3912 set the natgw state on/off
3914 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
3917 uint32_t natgwstate;
3923 if (!strcmp(argv[0], "on")) {
3925 } else if (!strcmp(argv[0], "off")) {
3931 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
3933 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
3941 set the lmaster role on/off
3943 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3946 uint32_t lmasterrole;
3952 if (!strcmp(argv[0], "on")) {
3954 } else if (!strcmp(argv[0], "off")) {
3960 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
3962 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
3970 set the recmaster role on/off
3972 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3975 uint32_t recmasterrole;
3981 if (!strcmp(argv[0], "on")) {
3983 } else if (!strcmp(argv[0], "off")) {
3989 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
3991 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
3999 set debug level on a node or all nodes
4001 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4007 printf("You must specify the debug level. Valid levels are:\n");
4008 for (i=0; debug_levels[i].description != NULL; i++) {
4009 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4015 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4016 level = get_debug_by_desc(argv[0]);
4018 level = strtol(argv[0], NULL, 0);
4021 for (i=0; debug_levels[i].description != NULL; i++) {
4022 if (level == debug_levels[i].level) {
4026 if (debug_levels[i].description == NULL) {
4027 printf("Invalid debug level, must be one of\n");
4028 for (i=0; debug_levels[i].description != NULL; i++) {
4029 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4034 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4036 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4045 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4051 priority = strtol(argv[0], NULL, 0);
4055 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4057 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4059 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4066 attach to a database
4068 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4070 const char *db_name;
4071 struct ctdb_db_context *ctdb_db;
4072 bool persistent = false;
4082 if (strcmp(argv[1], "persistent") != 0) {
4088 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4089 if (ctdb_db == NULL) {
4090 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4100 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4102 struct ctdb_db_priority db_prio;
4109 db_prio.db_id = strtoul(argv[0], NULL, 0);
4110 db_prio.priority = strtoul(argv[1], NULL, 0);
4112 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
4114 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
4124 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4126 uint32_t db_id, priority;
4133 db_id = strtoul(argv[0], NULL, 0);
4135 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4137 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4141 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
4147 set the readonly capability for a database
4149 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4158 db_id = strtoul(argv[0], NULL, 0);
4160 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4162 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4172 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4182 db_id = strtoul(argv[0], NULL, 0);
4184 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &seqnum);
4186 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
4190 printf("Sequence number:%lld\n", (long long)seqnum);
4196 run an eventscript on a node
4198 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
4204 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4207 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4211 data.dptr = (unsigned char *)discard_const(argv[0]);
4212 data.dsize = strlen((char *)data.dptr) + 1;
4214 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
4216 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
4217 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4218 if (ret != 0 || res != 0) {
4219 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
4220 talloc_free(tmp_ctx);
4223 talloc_free(tmp_ctx);
4227 #define DB_VERSION 1
4228 #define MAX_DB_NAME 64
4229 struct db_file_header {
4230 unsigned long version;
4232 unsigned long persistent;
4234 const char name[MAX_DB_NAME];
4237 struct backup_data {
4238 struct ctdb_marshall_buffer *records;
4241 bool traverse_error;
4244 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
4246 struct backup_data *bd = talloc_get_type(private, struct backup_data);
4247 struct ctdb_rec_data *rec;
4249 /* add the record */
4250 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
4252 bd->traverse_error = true;
4253 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
4256 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
4257 if (bd->records == NULL) {
4258 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
4259 bd->traverse_error = true;
4262 bd->records->count++;
4263 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
4264 bd->len += rec->length;
4272 * backup a database to a file
4274 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
4277 struct ctdb_dbid_map *dbmap=NULL;
4278 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4279 struct db_file_header dbhdr;
4280 struct ctdb_db_context *ctdb_db;
4281 struct backup_data *bd;
4284 const char *reason = NULL;
4287 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4291 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4293 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4297 for(i=0;i<dbmap->num;i++){
4300 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4301 if(!strcmp(argv[0], name)){
4302 talloc_free(discard_const(name));
4305 talloc_free(discard_const(name));
4307 if (i == dbmap->num) {
4308 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4309 talloc_free(tmp_ctx);
4313 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4314 dbmap->dbs[i].dbid, tmp_ctx, &reason);
4316 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4318 talloc_free(tmp_ctx);
4322 uint32_t allow_unhealthy = 0;
4324 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4325 "AllowUnhealthyDBRead",
4328 if (allow_unhealthy != 1) {
4329 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4332 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
4334 talloc_free(tmp_ctx);
4338 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4340 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4341 "tunnable AllowUnhealthyDBRead = %u\n",
4345 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4346 if (ctdb_db == NULL) {
4347 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4348 talloc_free(tmp_ctx);
4353 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4355 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4356 talloc_free(tmp_ctx);
4361 bd = talloc_zero(tmp_ctx, struct backup_data);
4363 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4364 talloc_free(tmp_ctx);
4368 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4369 if (bd->records == NULL) {
4370 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4371 talloc_free(tmp_ctx);
4375 bd->len = offsetof(struct ctdb_marshall_buffer, data);
4376 bd->records->db_id = ctdb_db->db_id;
4377 /* traverse the database collecting all records */
4378 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4379 bd->traverse_error) {
4380 DEBUG(DEBUG_ERR,("Traverse error\n"));
4381 talloc_free(tmp_ctx);
4385 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4388 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4390 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4391 talloc_free(tmp_ctx);
4395 dbhdr.version = DB_VERSION;
4396 dbhdr.timestamp = time(NULL);
4397 dbhdr.persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4398 dbhdr.size = bd->len;
4399 if (strlen(argv[0]) >= MAX_DB_NAME) {
4400 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4403 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
4404 ret = write(fh, &dbhdr, sizeof(dbhdr));
4406 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4409 ret = write(fh, bd->records, bd->len);
4411 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4420 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4424 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
4426 talloc_free(tmp_ctx);
4431 * restore a database from a file
4433 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4436 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4439 struct db_file_header dbhdr;
4440 struct ctdb_db_context *ctdb_db;
4441 struct ctdb_node_map *nodemap=NULL;
4442 struct ctdb_vnn_map *vnnmap=NULL;
4444 struct ctdb_control_wipe_database w;
4446 uint32_t generation;
4451 if (argc < 1 || argc > 2) {
4452 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4456 fh = open(argv[0], O_RDONLY);
4458 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4459 talloc_free(tmp_ctx);
4463 read(fh, &dbhdr, sizeof(dbhdr));
4464 if (dbhdr.version != DB_VERSION) {
4465 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4466 talloc_free(tmp_ctx);
4470 dbname = discard_const(dbhdr.name);
4472 dbname = discard_const(argv[1]);
4475 outdata.dsize = dbhdr.size;
4476 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4477 if (outdata.dptr == NULL) {
4478 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4480 talloc_free(tmp_ctx);
4483 read(fh, outdata.dptr, outdata.dsize);
4486 tm = localtime(&dbhdr.timestamp);
4487 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4488 printf("Restoring database '%s' from backup @ %s\n",
4492 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
4493 if (ctdb_db == NULL) {
4494 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4495 talloc_free(tmp_ctx);
4499 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4501 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4502 talloc_free(tmp_ctx);
4507 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4509 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4510 talloc_free(tmp_ctx);
4514 /* freeze all nodes */
4515 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4516 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4517 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4523 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4524 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4525 talloc_free(tmp_ctx);
4530 generation = vnnmap->generation;
4531 data.dptr = (void *)&generation;
4532 data.dsize = sizeof(generation);
4534 /* start a cluster wide transaction */
4535 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4536 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4538 TIMELIMIT(), false, data,
4541 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4546 w.db_id = ctdb_db->db_id;
4547 w.transaction_id = generation;
4549 data.dptr = (void *)&w;
4550 data.dsize = sizeof(w);
4552 /* wipe all the remote databases. */
4553 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4554 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4556 TIMELIMIT(), false, data,
4559 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4560 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4561 talloc_free(tmp_ctx);
4565 /* push the database */
4566 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4567 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4569 TIMELIMIT(), false, outdata,
4572 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4573 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4574 talloc_free(tmp_ctx);
4578 data.dptr = (void *)&ctdb_db->db_id;
4579 data.dsize = sizeof(ctdb_db->db_id);
4581 /* mark the database as healthy */
4582 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4583 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4585 TIMELIMIT(), false, data,
4588 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4589 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4590 talloc_free(tmp_ctx);
4594 data.dptr = (void *)&generation;
4595 data.dsize = sizeof(generation);
4597 /* commit all the changes */
4598 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4600 TIMELIMIT(), false, data,
4603 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4604 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4605 talloc_free(tmp_ctx);
4610 /* thaw all nodes */
4611 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4612 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4618 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4619 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4620 talloc_free(tmp_ctx);
4625 talloc_free(tmp_ctx);
4630 * dump a database backup from a file
4632 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4634 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4636 struct db_file_header dbhdr;
4640 struct ctdb_rec_data *rec = NULL;
4641 struct ctdb_marshall_buffer *m;
4642 struct ctdb_dump_db_context c;
4645 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4649 fh = open(argv[0], O_RDONLY);
4651 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4652 talloc_free(tmp_ctx);
4656 read(fh, &dbhdr, sizeof(dbhdr));
4657 if (dbhdr.version != DB_VERSION) {
4658 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4659 talloc_free(tmp_ctx);
4663 outdata.dsize = dbhdr.size;
4664 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4665 if (outdata.dptr == NULL) {
4666 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4668 talloc_free(tmp_ctx);
4671 read(fh, outdata.dptr, outdata.dsize);
4673 m = (struct ctdb_marshall_buffer *)outdata.dptr;
4675 tm = localtime(&dbhdr.timestamp);
4676 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4677 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
4678 dbhdr.name, m->db_id, tbuf);
4682 c.printemptyrecords = (bool)options.printemptyrecords;
4683 c.printdatasize = (bool)options.printdatasize;
4684 c.printlmaster = false;
4685 c.printhash = (bool)options.printhash;
4686 c.printrecordflags = (bool)options.printrecordflags;
4688 for (i=0; i < m->count; i++) {
4692 /* we do not want the header splitted, so we pass NULL*/
4693 rec = ctdb_marshall_loop_next(m, rec, &reqid,
4696 ctdb_dumpdb_record(ctdb, key, data, &c);
4699 printf("Dumped %d records\n", i);
4700 talloc_free(tmp_ctx);
4705 * wipe a database from a file
4707 static int control_wipedb(struct ctdb_context *ctdb, int argc,
4711 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4713 struct ctdb_db_context *ctdb_db;
4714 struct ctdb_node_map *nodemap = NULL;
4715 struct ctdb_vnn_map *vnnmap = NULL;
4717 struct ctdb_control_wipe_database w;
4719 uint32_t generation;
4720 struct ctdb_dbid_map *dbmap = NULL;
4723 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4727 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4730 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
4735 for(i=0;i<dbmap->num;i++){
4738 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4739 dbmap->dbs[i].dbid, tmp_ctx, &name);
4740 if(!strcmp(argv[0], name)){
4741 talloc_free(discard_const(name));
4744 talloc_free(discard_const(name));
4746 if (i == dbmap->num) {
4747 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
4749 talloc_free(tmp_ctx);
4753 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4754 if (ctdb_db == NULL) {
4755 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
4757 talloc_free(tmp_ctx);
4761 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
4764 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4766 talloc_free(tmp_ctx);
4770 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4773 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
4775 talloc_free(tmp_ctx);
4779 /* freeze all nodes */
4780 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4781 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4782 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4789 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4790 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
4791 CTDB_RECOVERY_ACTIVE);
4792 talloc_free(tmp_ctx);
4797 generation = vnnmap->generation;
4798 data.dptr = (void *)&generation;
4799 data.dsize = sizeof(generation);
4801 /* start a cluster wide transaction */
4802 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4803 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4805 TIMELIMIT(), false, data,
4809 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
4810 "transactions.\n"));
4814 w.db_id = ctdb_db->db_id;
4815 w.transaction_id = generation;
4817 data.dptr = (void *)&w;
4818 data.dsize = sizeof(w);
4820 /* wipe all the remote databases. */
4821 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4822 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4824 TIMELIMIT(), false, data,
4827 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4828 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4829 talloc_free(tmp_ctx);
4833 data.dptr = (void *)&ctdb_db->db_id;
4834 data.dsize = sizeof(ctdb_db->db_id);
4836 /* mark the database as healthy */
4837 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4838 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4840 TIMELIMIT(), false, data,
4843 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4844 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4845 talloc_free(tmp_ctx);
4849 data.dptr = (void *)&generation;
4850 data.dsize = sizeof(generation);
4852 /* commit all the changes */
4853 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4855 TIMELIMIT(), false, data,
4858 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4859 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4860 talloc_free(tmp_ctx);
4864 /* thaw all nodes */
4865 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4866 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4872 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4873 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4874 talloc_free(tmp_ctx);
4878 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
4880 talloc_free(tmp_ctx);
4887 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4893 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4894 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
4895 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
4896 if (ret != 0 || res != 0) {
4897 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
4898 talloc_free(tmp_ctx);
4901 write(1, data.dptr, data.dsize);
4902 talloc_free(tmp_ctx);
4907 handler for memory dumps
4909 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
4910 TDB_DATA data, void *private_data)
4912 write(1, data.dptr, data.dsize);
4917 dump memory usage on the recovery daemon
4919 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4923 struct rd_memdump_reply rd;
4925 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4927 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4930 rd.srvid = getpid();
4932 /* register a message port for receiveing the reply so that we
4933 can receive the reply
4935 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
4938 data.dptr = (uint8_t *)&rd;
4939 data.dsize = sizeof(rd);
4941 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
4943 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4947 /* this loop will terminate when we have received the reply */
4949 event_loop_once(ctdb->ev);
4956 send a message to a srvid
4958 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
4960 unsigned long srvid;
4968 srvid = strtoul(argv[0], NULL, 0);
4970 data.dptr = (uint8_t *)discard_const(argv[1]);
4971 data.dsize= strlen(argv[1]);
4973 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
4975 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4983 handler for msglisten
4985 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
4986 TDB_DATA data, void *private_data)
4990 printf("Message received: ");
4991 for (i=0;i<data.dsize;i++) {
4992 printf("%c", data.dptr[i]);
4998 listen for messages on a messageport
5000 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5006 /* register a message port and listen for messages
5008 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5009 printf("Listening for messages on srvid:%d\n", (int)srvid);
5012 event_loop_once(ctdb->ev);
5019 list all nodes in the cluster
5020 we parse the nodes file directly
5022 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5024 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5025 struct pnn_node *pnn_nodes;
5026 struct pnn_node *pnn_node;
5028 pnn_nodes = read_nodes_file(mem_ctx);
5029 if (pnn_nodes == NULL) {
5030 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5031 talloc_free(mem_ctx);
5035 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
5036 ctdb_sock_addr addr;
5037 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
5038 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
5039 talloc_free(mem_ctx);
5042 if (options.machinereadable){
5043 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5045 printf("%s\n", pnn_node->addr);
5048 talloc_free(mem_ctx);
5054 reload the nodes file on the local node
5056 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5060 struct ctdb_node_map *nodemap=NULL;
5062 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5064 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
5068 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5070 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5074 /* reload the nodes file on all remote nodes */
5075 for (i=0;i<nodemap->num;i++) {
5076 if (nodemap->nodes[i].pnn == mypnn) {
5079 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
5080 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
5081 nodemap->nodes[i].pnn);
5083 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
5087 /* reload the nodes file on the local node */
5088 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
5089 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
5091 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
5094 /* initiate a recovery */
5095 control_recover(ctdb, argc, argv);
5101 static const struct {
5103 int (*fn)(struct ctdb_context *, int, const char **);
5105 bool without_daemon; /* can be run without daemon running ? */
5108 } ctdb_commands[] = {
5110 { "version", control_version, true, false, "show version of ctdb" },
5112 { "status", control_status, true, false, "show node status" },
5113 { "uptime", control_uptime, true, false, "show node uptime" },
5114 { "ping", control_ping, true, false, "ping all nodes" },
5115 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5116 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5117 { "listvars", control_listvars, true, false, "list tunable variables"},
5118 { "statistics", control_statistics, false, false, "show statistics" },
5119 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5120 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5121 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5122 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5123 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5124 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5125 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5126 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5127 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
5128 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname>"},
5129 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname>"},
5130 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5131 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5132 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5133 { "lvs", control_lvs, true, false, "show lvs configuration" },
5134 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
5135 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5136 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
5137 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
5138 { "getdebug", control_getdebug, true, false, "get debug level" },
5139 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "<level>" },
5140 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer" },
5141 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
5142 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
5143 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
5144 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
5145 { "disable", control_disable, true, false, "disable a nodes public IP" },
5146 { "enable", control_enable, true, false, "enable a nodes public IP" },
5147 { "stop", control_stop, true, false, "stop a node" },
5148 { "continue", control_continue, true, false, "re-start a stopped node" },
5149 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
5150 { "unban", control_unban, true, false, "unban a node" },
5151 { "showban", control_showban, true, false, "show ban information"},
5152 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
5153 { "recover", control_recover, true, false, "force recovery" },
5154 { "sync", control_ipreallocate, true, false, "wait until ctdbd has synced all state changes" },
5155 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
5156 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
5157 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
5158 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
5159 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
5160 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5161 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
5162 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
5164 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
5166 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
5167 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
5168 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
5169 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
5170 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
5171 { "vacuum", ctdb_vacuum, false, true, "vacuum the databases of empty records", "[max_records]"},
5172 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
5173 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
5174 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
5175 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
5176 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
5177 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
5178 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
5179 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
5180 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
5181 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
5182 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
5183 { "recmaster", control_recmaster, false, false, "show the pnn for the recovery master."},
5184 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
5185 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
5186 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
5187 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
5188 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
5189 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
5190 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
5191 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
5192 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
5193 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
5194 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbid> <prio:1-3>"},
5195 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbid>"},
5196 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbid>"},
5197 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
5198 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
5199 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
5200 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<db> <key> [<file>]" },
5201 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<db> <key> <file containing record>" },
5202 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file", "<tdb-file> <key> [<file>]" },
5203 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
5204 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
5205 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
5206 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbid>" },
5212 static void usage(void)
5216 "Usage: ctdb [options] <control>\n" \
5218 " -n <node> choose node number, or 'all' (defaults to local node)\n"
5219 " -Y generate machinereadable output\n"
5220 " -v generate verbose output\n"
5221 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
5222 printf("Controls:\n");
5223 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5224 printf(" %-15s %-27s %s\n",
5225 ctdb_commands[i].name,
5226 ctdb_commands[i].args?ctdb_commands[i].args:"",
5227 ctdb_commands[i].msg);
5233 static void ctdb_alarm(int sig)
5235 printf("Maximum runtime exceeded - exiting\n");
5242 int main(int argc, const char *argv[])
5244 struct ctdb_context *ctdb;
5245 char *nodestring = NULL;
5246 struct poptOption popt_options[] = {
5249 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
5250 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
5251 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
5252 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
5253 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
5254 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
5255 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
5256 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
5257 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
5258 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
5262 const char **extra_argv;
5266 struct event_context *ev;
5267 const char *control;
5268 const char *socket_name;
5272 /* set some defaults */
5273 options.maxruntime = 0;
5274 options.timelimit = 3;
5275 options.pnn = CTDB_CURRENT_NODE;
5277 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
5279 while ((opt = poptGetNextOpt(pc)) != -1) {
5282 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
5283 poptBadOption(pc, 0), poptStrerror(opt)));
5288 /* setup the remaining options for the main program to use */
5289 extra_argv = poptGetArgs(pc);
5292 while (extra_argv[extra_argc]) extra_argc++;
5295 if (extra_argc < 1) {
5299 if (options.maxruntime == 0) {
5300 const char *ctdb_timeout;
5301 ctdb_timeout = getenv("CTDB_TIMEOUT");
5302 if (ctdb_timeout != NULL) {
5303 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5305 /* default timeout is 120 seconds */
5306 options.maxruntime = 120;
5310 signal(SIGALRM, ctdb_alarm);
5311 alarm(options.maxruntime);
5313 control = extra_argv[0];
5315 ev = event_context_init(NULL);
5317 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5320 tevent_loop_allow_nesting(ev);
5322 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5323 if (strcmp(control, ctdb_commands[i].name) == 0) {
5328 if (i == ARRAY_SIZE(ctdb_commands)) {
5329 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
5333 if (ctdb_commands[i].without_daemon == true) {
5334 if (nodestring != NULL) {
5335 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
5339 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
5342 /* initialise ctdb */
5343 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
5346 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5350 /* initialize a libctdb connection as well */
5351 socket_name = ctdb_get_socketname(ctdb);
5352 ctdb_connection = ctdb_connect(socket_name,
5353 ctdb_log_file, stderr);
5354 if (ctdb_connection == NULL) {
5355 DEBUG(DEBUG_ERR, ("Failed to connect to daemon from libctdb\n"));
5359 /* setup the node number to contact */
5360 if (nodestring != NULL) {
5361 if (strcmp(nodestring, "all") == 0) {
5362 options.pnn = CTDB_BROADCAST_ALL;
5364 options.pnn = strtoul(nodestring, NULL, 0);
5368 /* verify the node exists */
5371 if (options.pnn == CTDB_CURRENT_NODE) {
5373 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
5380 if (ctdb_commands[i].auto_all &&
5381 options.pnn == CTDB_BROADCAST_ALL) {
5387 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
5388 CTDB_NO_MEMORY(ctdb, nodes);
5390 for (j=0;j<num_nodes;j++) {
5391 options.pnn = nodes[j];
5392 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5396 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);