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 "system/time.h"
23 #include "system/filesys.h"
24 #include "system/network.h"
25 #include "system/locale.h"
28 #include "../include/ctdb_version.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);
50 int printemptyrecords;
57 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
58 #define LONGTIMELIMIT() timeval_current_ofs(options.timelimit*10, 0)
60 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
62 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
66 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
67 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
68 "Out of memory in " __location__ )); \
72 /* Pretty print the flags to a static buffer in human-readable format.
73 * This never returns NULL!
75 static const char *pretty_print_flags(uint32_t flags)
82 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
83 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
84 { NODE_FLAGS_BANNED, "BANNED" },
85 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
86 { NODE_FLAGS_DELETED, "DELETED" },
87 { NODE_FLAGS_STOPPED, "STOPPED" },
88 { NODE_FLAGS_INACTIVE, "INACTIVE" },
90 static char flags_str[512]; /* Big enough to contain all flag names */
93 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
94 if (flags & flag_names[j].flag) {
95 if (flags_str[0] == '\0') {
96 (void) strcpy(flags_str, flag_names[j].name);
98 (void) strcat(flags_str, "|");
99 (void) strcat(flags_str, flag_names[j].name);
103 if (flags_str[0] == '\0') {
104 (void) strcpy(flags_str, "OK");
110 static int h2i(char h)
112 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
113 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
117 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
120 TDB_DATA key = {NULL, 0};
124 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
129 key.dptr = talloc_size(mem_ctx, key.dsize);
131 for (i=0; i < len/2; i++) {
132 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
137 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
138 * that are disconnected or deleted. If dd_ok is true those nodes are
139 * included in the output list of nodes. If dd_ok is false, those
140 * nodes are filtered from the "all" case and cause an error if
141 * explicitly specified.
143 static bool parse_nodestring(struct ctdb_context *ctdb,
144 const char * nodestring,
145 uint32_t current_pnn,
152 struct ctdb_node_map *nodemap;
156 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
157 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
161 if (nodestring != NULL) {
162 *nodes = talloc_array(ctdb, uint32_t, 0);
163 CTDB_NOMEM_ABORT(*nodes);
167 if (strcmp(nodestring, "all") == 0) {
168 *pnn_mode = CTDB_BROADCAST_ALL;
171 for (i = 0; i < nodemap->num; i++) {
172 if ((nodemap->nodes[i].flags &
173 (NODE_FLAGS_DISCONNECTED |
174 NODE_FLAGS_DELETED)) && !dd_ok) {
177 *nodes = talloc_realloc(ctdb, *nodes,
179 CTDB_NOMEM_ABORT(*nodes);
187 ns = talloc_strdup(ctdb, nodestring);
188 tok = strtok(ns, ",");
189 while (tok != NULL) {
191 i = (uint32_t)strtoul(tok, NULL, 0);
192 if (i >= nodemap->num) {
193 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
196 if ((nodemap->nodes[i].flags &
197 (NODE_FLAGS_DISCONNECTED |
198 NODE_FLAGS_DELETED)) && !dd_ok) {
199 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
202 if (!ctdb_getpnn(ctdb_connection, i, &pnn)) {
203 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
207 *nodes = talloc_realloc(ctdb, *nodes,
209 CTDB_NOMEM_ABORT(*nodes);
214 tok = strtok(NULL, ",");
219 *pnn_mode = (*nodes)[0];
221 *pnn_mode = CTDB_MULTICAST;
225 /* default - no nodes specified */
226 *nodes = talloc_array(ctdb, uint32_t, 1);
227 CTDB_NOMEM_ABORT(*nodes);
228 *pnn_mode = CTDB_CURRENT_NODE;
230 if (!ctdb_getpnn(ctdb_connection, current_pnn,
236 ctdb_free_nodemap(nodemap);
242 check if a database exists
244 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg, uint32_t *dbid, uint8_t *flags)
247 struct ctdb_dbid_map *dbmap=NULL;
248 bool dbid_given = false, found = false;
250 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
252 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
254 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
258 if (strncmp(dbarg, "0x", 2) == 0) {
259 id = strtoul(dbarg, NULL, 0);
263 for(i=0; i<dbmap->num; i++) {
265 if (id == dbmap->dbs[i].dbid) {
271 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
273 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
277 if (strcmp(name, dbarg) == 0) {
278 id = dbmap->dbs[i].dbid;
286 if (dbid) *dbid = id;
287 if (flags) *flags = dbmap->dbs[i].flags;
289 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
293 talloc_free(tmp_ctx);
298 see if a process exists
300 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
308 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
309 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
313 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
315 printf("%u:%u exists\n", pnn, pid);
317 printf("%u:%u does not exist\n", pnn, pid);
323 display statistics structure
325 static void show_statistics(struct ctdb_statistics *s, int show_header)
327 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
329 const char *prefix=NULL;
331 int tmp, days, hours, minutes, seconds;
336 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
337 STATISTICS_FIELD(num_clients),
338 STATISTICS_FIELD(frozen),
339 STATISTICS_FIELD(recovering),
340 STATISTICS_FIELD(num_recoveries),
341 STATISTICS_FIELD(client_packets_sent),
342 STATISTICS_FIELD(client_packets_recv),
343 STATISTICS_FIELD(node_packets_sent),
344 STATISTICS_FIELD(node_packets_recv),
345 STATISTICS_FIELD(keepalive_packets_sent),
346 STATISTICS_FIELD(keepalive_packets_recv),
347 STATISTICS_FIELD(node.req_call),
348 STATISTICS_FIELD(node.reply_call),
349 STATISTICS_FIELD(node.req_dmaster),
350 STATISTICS_FIELD(node.reply_dmaster),
351 STATISTICS_FIELD(node.reply_error),
352 STATISTICS_FIELD(node.req_message),
353 STATISTICS_FIELD(node.req_control),
354 STATISTICS_FIELD(node.reply_control),
355 STATISTICS_FIELD(client.req_call),
356 STATISTICS_FIELD(client.req_message),
357 STATISTICS_FIELD(client.req_control),
358 STATISTICS_FIELD(timeouts.call),
359 STATISTICS_FIELD(timeouts.control),
360 STATISTICS_FIELD(timeouts.traverse),
361 STATISTICS_FIELD(locks.num_calls),
362 STATISTICS_FIELD(locks.num_current),
363 STATISTICS_FIELD(locks.num_pending),
364 STATISTICS_FIELD(locks.num_failed),
365 STATISTICS_FIELD(total_calls),
366 STATISTICS_FIELD(pending_calls),
367 STATISTICS_FIELD(childwrite_calls),
368 STATISTICS_FIELD(pending_childwrite_calls),
369 STATISTICS_FIELD(memory_used),
370 STATISTICS_FIELD(max_hop_count),
371 STATISTICS_FIELD(total_ro_delegations),
372 STATISTICS_FIELD(total_ro_revokes),
375 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
384 if (options.machinereadable){
386 printf("CTDB version:");
387 printf("Current time of statistics:");
388 printf("Statistics collected since:");
389 for (i=0;i<ARRAY_SIZE(fields);i++) {
390 printf("%s:", fields[i].name);
392 printf("num_reclock_ctdbd_latency:");
393 printf("min_reclock_ctdbd_latency:");
394 printf("avg_reclock_ctdbd_latency:");
395 printf("max_reclock_ctdbd_latency:");
397 printf("num_reclock_recd_latency:");
398 printf("min_reclock_recd_latency:");
399 printf("avg_reclock_recd_latency:");
400 printf("max_reclock_recd_latency:");
402 printf("num_call_latency:");
403 printf("min_call_latency:");
404 printf("avg_call_latency:");
405 printf("max_call_latency:");
407 printf("num_lockwait_latency:");
408 printf("min_lockwait_latency:");
409 printf("avg_lockwait_latency:");
410 printf("max_lockwait_latency:");
412 printf("num_childwrite_latency:");
413 printf("min_childwrite_latency:");
414 printf("avg_childwrite_latency:");
415 printf("max_childwrite_latency:");
418 printf("%d:", CTDB_VERSION);
419 printf("%d:", (int)s->statistics_current_time.tv_sec);
420 printf("%d:", (int)s->statistics_start_time.tv_sec);
421 for (i=0;i<ARRAY_SIZE(fields);i++) {
422 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
424 printf("%d:", s->reclock.ctdbd.num);
425 printf("%.6f:", s->reclock.ctdbd.min);
426 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
427 printf("%.6f:", s->reclock.ctdbd.max);
429 printf("%d:", s->reclock.recd.num);
430 printf("%.6f:", s->reclock.recd.min);
431 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
432 printf("%.6f:", s->reclock.recd.max);
434 printf("%d:", s->call_latency.num);
435 printf("%.6f:", s->call_latency.min);
436 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
437 printf("%.6f:", s->call_latency.max);
439 printf("%d:", s->childwrite_latency.num);
440 printf("%.6f:", s->childwrite_latency.min);
441 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
442 printf("%.6f:", s->childwrite_latency.max);
445 printf("CTDB version %u\n", CTDB_VERSION);
446 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
447 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
449 for (i=0;i<ARRAY_SIZE(fields);i++) {
450 if (strchr(fields[i].name, '.')) {
451 preflen = strcspn(fields[i].name, ".")+1;
452 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
453 prefix = fields[i].name;
454 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
459 printf(" %*s%-22s%*s%10u\n",
461 fields[i].name+preflen,
463 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
465 printf(" hop_count_buckets:");
466 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
467 printf(" %d", s->hop_count_bucket[i]);
470 printf(" lock_buckets:");
471 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
472 printf(" %d", s->locks.buckets[i]);
475 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "locks_latency MIN/AVG/MAX", s->locks.latency.min, s->locks.latency.num?s->locks.latency.total/s->locks.latency.num:0.0, s->locks.latency.max, s->locks.latency.num);
477 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);
479 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);
481 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);
482 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);
485 talloc_free(tmp_ctx);
489 display remote ctdb statistics combined from all nodes
491 static int control_statistics_all(struct ctdb_context *ctdb)
494 struct ctdb_statistics statistics;
498 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
499 CTDB_NO_MEMORY(ctdb, nodes);
501 ZERO_STRUCT(statistics);
503 for (i=0;i<num_nodes;i++) {
504 struct ctdb_statistics s1;
506 uint32_t *v1 = (uint32_t *)&s1;
507 uint32_t *v2 = (uint32_t *)&statistics;
509 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
510 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
512 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
515 for (j=0;j<num_ints;j++) {
518 statistics.max_hop_count =
519 MAX(statistics.max_hop_count, s1.max_hop_count);
520 statistics.call_latency.max =
521 MAX(statistics.call_latency.max, s1.call_latency.max);
524 printf("Gathered statistics for %u nodes\n", num_nodes);
525 show_statistics(&statistics, 1);
530 display remote ctdb statistics
532 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
535 struct ctdb_statistics statistics;
537 if (options.pnn == CTDB_BROADCAST_ALL) {
538 return control_statistics_all(ctdb);
541 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
543 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
546 show_statistics(&statistics, 1);
552 reset remote ctdb statistics
554 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
558 ret = ctdb_statistics_reset(ctdb, options.pnn);
560 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
568 display remote ctdb rolling statistics
570 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
573 struct ctdb_statistics_wire *stats;
574 int i, num_records = -1;
577 num_records = atoi(argv[0]) - 1;
580 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
582 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
585 for (i=0;i<stats->num;i++) {
586 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
589 show_statistics(&stats->stats[i], i==0);
590 if (i == num_records) {
599 display remote ctdb db statistics
601 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
603 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
604 struct ctdb_db_statistics *dbstat;
612 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
616 if (!ctdb_getdbstat(ctdb_connection, options.pnn, db_id, &dbstat)) {
617 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
618 talloc_free(tmp_ctx);
622 printf("DB Statistics:\n");
623 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
624 dbstat->db_ro_delegations);
625 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
626 dbstat->db_ro_delegations);
627 printf(" %s\n", "locks");
628 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
629 dbstat->locks.num_calls);
630 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
631 dbstat->locks.num_failed);
632 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
633 dbstat->locks.num_current);
634 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
635 dbstat->locks.num_pending);
636 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
637 " latency_ctdbd MIN/AVG/MAX",
638 dbstat->locks.latency.min,
639 (dbstat->locks.latency.num ?
640 dbstat->locks.latency.total /dbstat->locks.latency.num :
642 dbstat->locks.latency.max,
643 dbstat->locks.latency.num);
644 printf(" %s", " buckets:");
645 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
646 printf(" %d", dbstat->hop_count_bucket[i]);
649 printf("Num Hot Keys: %d\n", dbstat->num_hot_keys);
650 for (i = 0; i < dbstat->num_hot_keys; i++) {
652 printf("Count:%d Key:", dbstat->hot_keys[i].count);
653 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
654 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
659 ctdb_free_dbstat(dbstat);
664 display uptime of remote node
666 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
669 struct ctdb_uptime *uptime = NULL;
670 int tmp, days, hours, minutes, seconds;
672 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
674 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
678 if (options.machinereadable){
679 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
680 printf(":%u:%u:%u:%lf\n",
681 (unsigned int)uptime->current_time.tv_sec,
682 (unsigned int)uptime->ctdbd_start_time.tv_sec,
683 (unsigned int)uptime->last_recovery_finished.tv_sec,
684 timeval_delta(&uptime->last_recovery_finished,
685 &uptime->last_recovery_started)
690 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
692 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
700 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
702 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
710 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
712 printf("Duration of last recovery/failover: %lf seconds\n",
713 timeval_delta(&uptime->last_recovery_finished,
714 &uptime->last_recovery_started));
720 show the PNN of the current node
722 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
727 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
729 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
733 printf("PNN:%d\n", mypnn);
739 struct pnn_node *next;
744 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
746 const char *nodes_list;
750 struct pnn_node *pnn_nodes = NULL;
751 struct pnn_node *pnn_node;
752 struct pnn_node *tmp_node;
754 /* read the nodes file */
755 nodes_list = getenv("CTDB_NODES");
756 if (nodes_list == NULL) {
757 nodes_list = "/etc/ctdb/nodes";
759 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
763 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
766 for (i=0, pnn=0; i<nlines; i++) {
770 /* strip leading spaces */
771 while((*node == ' ') || (*node == '\t')) {
778 if (strcmp(node, "") == 0) {
781 pnn_node = talloc(mem_ctx, struct pnn_node);
782 pnn_node->pnn = pnn++;
783 pnn_node->addr = talloc_strdup(pnn_node, node);
784 pnn_node->next = pnn_nodes;
785 pnn_nodes = pnn_node;
788 /* swap them around so we return them in incrementing order */
789 pnn_node = pnn_nodes;
793 pnn_node = pnn_node->next;
795 tmp_node->next = pnn_nodes;
796 pnn_nodes = tmp_node;
803 show the PNN of the current node
804 discover the pnn by loading the nodes file and try to bind to all
805 addresses one at a time until the ip address is found.
807 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
809 TALLOC_CTX *mem_ctx = talloc_new(NULL);
810 struct pnn_node *pnn_nodes;
811 struct pnn_node *pnn_node;
813 pnn_nodes = read_nodes_file(mem_ctx);
814 if (pnn_nodes == NULL) {
815 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
816 talloc_free(mem_ctx);
820 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
823 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
824 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
825 talloc_free(mem_ctx);
829 if (ctdb_sys_have_ip(&addr)) {
830 printf("PNN:%d\n", pnn_node->pnn);
831 talloc_free(mem_ctx);
836 printf("Failed to detect which PNN this node is\n");
837 talloc_free(mem_ctx);
841 /* Helpers for ctdb status
843 static bool is_partially_online(struct ctdb_node_and_flags *node)
848 if (node->flags == 0) {
849 struct ctdb_ifaces_list *ifaces;
851 if (ctdb_getifaces(ctdb_connection, node->pnn, &ifaces)) {
852 for (j=0; j < ifaces->num; j++) {
853 if (ifaces->ifaces[j].link_state != 0) {
859 ctdb_free_ifaces(ifaces);
866 static void control_status_header_machine(void)
868 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
869 ":Inactive:PartiallyOnline:ThisNode:\n");
872 static int control_status_1_machine(int mypnn, struct ctdb_node_and_flags *node)
874 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
875 ctdb_addr_to_str(&node->addr),
876 !!(node->flags&NODE_FLAGS_DISCONNECTED),
877 !!(node->flags&NODE_FLAGS_BANNED),
878 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
879 !!(node->flags&NODE_FLAGS_UNHEALTHY),
880 !!(node->flags&NODE_FLAGS_STOPPED),
881 !!(node->flags&NODE_FLAGS_INACTIVE),
882 is_partially_online(node) ? 1 : 0,
883 (node->pnn == mypnn)?'Y':'N');
888 static int control_status_1_human(int mypnn, struct ctdb_node_and_flags *node)
890 printf("pnn:%d %-16s %s%s\n", node->pnn,
891 ctdb_addr_to_str(&node->addr),
892 is_partially_online(node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
893 node->pnn == mypnn?" (THIS NODE)":"");
899 display remote ctdb status
901 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
904 struct ctdb_vnn_map *vnnmap=NULL;
905 struct ctdb_node_map *nodemap=NULL;
906 uint32_t recmode, recmaster, mypnn;
908 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
912 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
913 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
917 if (options.machinereadable) {
918 control_status_header_machine();
919 for (i=0;i<nodemap->num;i++) {
920 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
923 (void) control_status_1_machine(mypnn,
929 printf("Number of nodes:%d\n", nodemap->num);
930 for(i=0;i<nodemap->num;i++){
931 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
934 (void) control_status_1_human(mypnn, &nodemap->nodes[i]);
937 if (!ctdb_getvnnmap(ctdb_connection, options.pnn, &vnnmap)) {
938 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
941 if (vnnmap->generation == INVALID_GENERATION) {
942 printf("Generation:INVALID\n");
944 printf("Generation:%d\n",vnnmap->generation);
946 printf("Size:%d\n",vnnmap->size);
947 for(i=0;i<vnnmap->size;i++){
948 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
950 ctdb_free_vnnmap(vnnmap);
952 if (!ctdb_getrecmode(ctdb_connection, options.pnn, &recmode)) {
953 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
956 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
958 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
959 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
962 printf("Recovery master:%d\n",recmaster);
967 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
970 struct ctdb_node_map *nodemap=NULL;
972 uint32_t pnn_mode, mypnn;
978 if (!parse_nodestring(ctdb, argc == 1 ? argv[0] : NULL,
979 options.pnn, true, &nodes, &pnn_mode)) {
983 if (options.machinereadable) {
984 control_status_header_machine();
985 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
986 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
989 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
990 DEBUG(DEBUG_ERR, ("Unable to get PNN from local node\n"));
994 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
995 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1001 for (i = 0; i < talloc_array_length(nodes); i++) {
1002 if (options.machinereadable) {
1003 ret |= control_status_1_machine(mypnn,
1004 &nodemap->nodes[nodes[i]]);
1006 ret |= control_status_1_human(mypnn,
1007 &nodemap->nodes[nodes[i]]);
1014 struct natgw_node *next;
1018 static int find_natgw(struct ctdb_context *ctdb,
1019 struct ctdb_node_map *nodemap, uint32_t flags,
1020 uint32_t *pnn, const char **ip)
1023 uint32_t capabilities;
1025 for (i=0;i<nodemap->num;i++) {
1026 if (!(nodemap->nodes[i].flags & flags)) {
1027 if (!ctdb_getcapabilities(ctdb_connection, nodemap->nodes[i].pnn, &capabilities)) {
1028 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1031 if (!(capabilities&CTDB_CAP_NATGW)) {
1034 *pnn = nodemap->nodes[i].pnn;
1035 *ip = ctdb_addr_to_str(&nodemap->nodes[i].addr);
1040 return 2; /* matches ENOENT */
1044 display the list of nodes belonging to this natgw configuration
1046 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1049 const char *natgw_list;
1052 struct natgw_node *natgw_nodes = NULL;
1053 struct natgw_node *natgw_node;
1054 struct ctdb_node_map *nodemap=NULL;
1055 uint32_t mypnn, pnn;
1058 /* When we have some nodes that could be the NATGW, make a
1059 * series of attempts to find the first node that doesn't have
1060 * certain status flags set.
1062 uint32_t exclude_flags[] = {
1063 /* Look for a nice healthy node */
1064 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1065 /* If not found, an UNHEALTHY/BANNED node will do */
1066 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1067 /* If not found, a STOPPED node will do */
1068 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1072 /* read the natgw nodes file into a linked list */
1073 natgw_list = getenv("CTDB_NATGW_NODES");
1074 if (natgw_list == NULL) {
1075 natgw_list = "/etc/ctdb/natgw_nodes";
1077 lines = file_lines_load(natgw_list, &nlines, ctdb);
1078 if (lines == NULL) {
1079 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
1082 for (i=0;i<nlines;i++) {
1086 /* strip leading spaces */
1087 while((*node == ' ') || (*node == '\t')) {
1093 if (strcmp(node, "") == 0) {
1096 natgw_node = talloc(ctdb, struct natgw_node);
1097 natgw_node->addr = talloc_strdup(natgw_node, node);
1098 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
1099 natgw_node->next = natgw_nodes;
1100 natgw_nodes = natgw_node;
1103 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
1104 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1108 /* Trim the nodemap so it only includes connected nodes in the
1109 * current natgw group.
1112 while(i<nodemap->num) {
1113 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
1114 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
1119 /* this node was not in the natgw so we just remove it from
1122 if ((natgw_node == NULL)
1123 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
1126 for (j=i+1; j<nodemap->num; j++) {
1127 nodemap->nodes[j-1] = nodemap->nodes[j];
1136 ret = 2; /* matches ENOENT */
1139 for (i = 0; exclude_flags[i] != 0; i++) {
1140 ret = find_natgw(ctdb, nodemap,
1151 if (options.machinereadable) {
1152 printf(":Node:IP:\n");
1153 printf(":%d:%s:\n", pnn, ip);
1155 printf("%d %s\n", pnn, ip);
1158 /* print the pruned list of nodes belonging to this natgw list */
1159 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
1160 DEBUG(DEBUG_NOTICE, ("Unable to get PNN from node %u\n", options.pnn));
1161 /* This is actually harmless and will only result in
1162 * the "this node" indication being missing
1166 if (options.machinereadable) {
1167 control_status_header_machine();
1169 printf("Number of nodes:%d\n", nodemap->num);
1171 for(i=0;i<nodemap->num;i++){
1172 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1175 if (options.machinereadable) {
1176 control_status_1_machine(mypnn, &(nodemap->nodes[i]));
1178 control_status_1_human(mypnn, &(nodemap->nodes[i]));
1183 ctdb_free_nodemap(nodemap);
1188 display the status of the scripts for monitoring (or other events)
1190 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1191 enum ctdb_eventscript_call type)
1193 struct ctdb_scripts_wire *script_status;
1196 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1198 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1202 if (script_status == NULL) {
1203 if (!options.machinereadable) {
1204 printf("%s cycle never run\n",
1205 ctdb_eventscript_call_names[type]);
1210 if (!options.machinereadable) {
1211 printf("%d scripts were executed last %s cycle\n",
1212 script_status->num_scripts,
1213 ctdb_eventscript_call_names[type]);
1215 for (i=0; i<script_status->num_scripts; i++) {
1216 const char *status = NULL;
1218 switch (script_status->scripts[i].status) {
1220 status = "TIMEDOUT";
1223 status = "DISABLED";
1229 if (script_status->scripts[i].status > 0)
1233 if (options.machinereadable) {
1234 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1235 ctdb_eventscript_call_names[type],
1236 script_status->scripts[i].name,
1237 script_status->scripts[i].status,
1239 (long)script_status->scripts[i].start.tv_sec,
1240 (long)script_status->scripts[i].start.tv_usec,
1241 (long)script_status->scripts[i].finished.tv_sec,
1242 (long)script_status->scripts[i].finished.tv_usec,
1243 script_status->scripts[i].output);
1247 printf("%-20s Status:%s ",
1248 script_status->scripts[i].name, status);
1250 /* Some other error, eg from stat. */
1251 printf("%-20s Status:CANNOT RUN (%s)",
1252 script_status->scripts[i].name,
1253 strerror(-script_status->scripts[i].status));
1255 if (script_status->scripts[i].status >= 0) {
1256 printf("Duration:%.3lf ",
1257 timeval_delta(&script_status->scripts[i].finished,
1258 &script_status->scripts[i].start));
1260 if (script_status->scripts[i].status != -ENOEXEC) {
1262 ctime(&script_status->scripts[i].start.tv_sec));
1263 if (script_status->scripts[i].status != 0) {
1264 printf(" OUTPUT:%s\n",
1265 script_status->scripts[i].output);
1275 static int control_scriptstatus(struct ctdb_context *ctdb,
1276 int argc, const char **argv)
1279 enum ctdb_eventscript_call type, min, max;
1283 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1288 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1292 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1293 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1299 if (type == CTDB_EVENT_MAX) {
1300 if (strcmp(arg, "all") == 0) {
1302 max = CTDB_EVENT_MAX;
1304 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1309 if (options.machinereadable) {
1310 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1313 for (type = min; type < max; type++) {
1314 ret = control_one_scriptstatus(ctdb, type);
1324 enable an eventscript
1326 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1334 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1336 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1344 disable an eventscript
1346 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1354 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1356 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1364 display the pnn of the recovery master
1366 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1370 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1371 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1374 printf("%d\n",recmaster);
1380 add a tickle to a public address
1382 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1384 struct ctdb_tcp_connection t;
1392 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1393 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1396 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1397 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1401 data.dptr = (uint8_t *)&t;
1402 data.dsize = sizeof(t);
1404 /* tell all nodes about this tcp connection */
1405 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1406 0, data, ctdb, NULL, NULL, NULL, NULL);
1408 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1417 delete a tickle from a node
1419 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1421 struct ctdb_tcp_connection t;
1429 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1430 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1433 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1434 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1438 data.dptr = (uint8_t *)&t;
1439 data.dsize = sizeof(t);
1441 /* tell all nodes about this tcp connection */
1442 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1443 0, data, ctdb, NULL, NULL, NULL, NULL);
1445 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1454 get a list of all tickles for this pnn
1456 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1458 struct ctdb_control_tcp_tickle_list *list;
1459 ctdb_sock_addr addr;
1468 port = atoi(argv[1]);
1471 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1472 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1476 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1478 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1482 if (options.machinereadable){
1483 printf(":source ip:port:destination ip:port:\n");
1484 for (i=0;i<list->tickles.num;i++) {
1485 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1488 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1489 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1492 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1493 printf("Num tickles:%u\n", list->tickles.num);
1494 for (i=0;i<list->tickles.num;i++) {
1495 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1498 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1499 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));
1509 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1511 struct ctdb_all_public_ips *ips;
1512 struct ctdb_public_ip ip;
1515 uint32_t disable_time;
1517 struct ctdb_node_map *nodemap=NULL;
1518 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1521 data.dptr = (uint8_t*)&disable_time;
1522 data.dsize = sizeof(disable_time);
1523 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1525 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1531 /* read the public ip list from the node */
1532 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1534 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1535 talloc_free(tmp_ctx);
1539 for (i=0;i<ips->num;i++) {
1540 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1545 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1546 pnn, ctdb_addr_to_str(addr)));
1547 talloc_free(tmp_ctx);
1554 data.dptr = (uint8_t *)&ip;
1555 data.dsize = sizeof(ip);
1557 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1559 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1560 talloc_free(tmp_ctx);
1564 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1565 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1572 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1573 talloc_free(tmp_ctx);
1577 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1579 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1580 talloc_free(tmp_ctx);
1584 /* update the recovery daemon so it now knows to expect the new
1585 node assignment for this ip.
1587 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1589 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1593 talloc_free(tmp_ctx);
1599 * scans all other nodes and returns a pnn for another node that can host this
1603 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1605 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1606 struct ctdb_all_public_ips *ips;
1607 struct ctdb_node_map *nodemap=NULL;
1610 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1612 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1613 talloc_free(tmp_ctx);
1617 for(i=0;i<nodemap->num;i++){
1618 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1621 if (nodemap->nodes[i].pnn == options.pnn) {
1625 /* read the public ip list from this node */
1626 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1628 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1632 for (j=0;j<ips->num;j++) {
1633 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1634 talloc_free(tmp_ctx);
1635 return nodemap->nodes[i].pnn;
1641 talloc_free(tmp_ctx);
1645 /* If pnn is -1 then try to find a node to move IP to... */
1646 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1648 bool pnn_specified = (pnn == -1 ? false : true);
1651 while (retries < 5) {
1652 if (!pnn_specified) {
1653 pnn = find_other_host_for_public_ip(ctdb, addr);
1658 ("Trying to move public IP to node %u\n", pnn));
1661 if (move_ip(ctdb, addr, pnn) == 0) {
1674 move/failover an ip address to a specific node
1676 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1679 ctdb_sock_addr addr;
1686 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1687 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1692 if (sscanf(argv[1], "%u", &pnn) != 1) {
1693 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1697 if (!try_moveip(ctdb, &addr, pnn)) {
1698 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1705 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1710 if (ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), pnn, &recmaster) != 0) {
1711 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", pnn));
1715 data.dptr = (uint8_t *)&pnn;
1716 data.dsize = sizeof(uint32_t);
1717 if (ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1718 DEBUG(DEBUG_ERR,("Failed to send message to force node reallocation\n"));
1727 rebalance a node by setting it to allow failback and triggering a
1730 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1732 switch (options.pnn) {
1733 case CTDB_BROADCAST_ALL:
1734 case CTDB_CURRENT_NODE:
1735 DEBUG(DEBUG_ERR,("You must specify a node number with -n <pnn> for the node to rebalance\n"));
1739 return rebalance_node(ctdb, options.pnn);
1743 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1745 struct ctdb_public_ip ip;
1748 uint32_t disable_time;
1750 struct ctdb_node_map *nodemap=NULL;
1751 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1754 data.dptr = (uint8_t*)&disable_time;
1755 data.dsize = sizeof(disable_time);
1756 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1758 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1765 data.dptr = (uint8_t *)&ip;
1766 data.dsize = sizeof(ip);
1768 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1770 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1771 talloc_free(tmp_ctx);
1775 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1776 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1783 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1784 talloc_free(tmp_ctx);
1788 talloc_free(tmp_ctx);
1793 release an ip form all nodes and have it re-assigned by recd
1795 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1797 ctdb_sock_addr addr;
1804 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1805 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1809 if (rebalance_ip(ctdb, &addr) != 0) {
1810 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
1817 static int getips_store_callback(void *param, void *data)
1819 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1820 struct ctdb_all_public_ips *ips = param;
1824 ips->ips[i].pnn = node_ip->pnn;
1825 ips->ips[i].addr = node_ip->addr;
1829 static int getips_count_callback(void *param, void *data)
1831 uint32_t *count = param;
1838 static uint32_t *ip_key(ctdb_sock_addr *ip)
1840 static uint32_t key[IP_KEYLEN];
1842 bzero(key, sizeof(key));
1844 switch (ip->sa.sa_family) {
1846 key[0] = ip->ip.sin_addr.s_addr;
1849 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1857 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1864 static void *add_ip_callback(void *parm, void *data)
1870 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1872 struct ctdb_all_public_ips *tmp_ips;
1873 struct ctdb_node_map *nodemap=NULL;
1874 trbt_tree_t *ip_tree;
1878 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1880 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1884 ip_tree = trbt_create(tmp_ctx, 0);
1886 for(i=0;i<nodemap->num;i++){
1887 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1890 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1894 /* read the public ip list from this node */
1895 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1897 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1901 for (j=0; j<tmp_ips->num;j++) {
1902 struct ctdb_public_ip *node_ip;
1904 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1905 node_ip->pnn = tmp_ips->ips[j].pnn;
1906 node_ip->addr = tmp_ips->ips[j].addr;
1908 trbt_insertarray32_callback(ip_tree,
1909 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1913 talloc_free(tmp_ips);
1918 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1920 len = offsetof(struct ctdb_all_public_ips, ips) +
1921 count*sizeof(struct ctdb_public_ip);
1922 tmp_ips = talloc_zero_size(tmp_ctx, len);
1923 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1931 static bool ipreallocate_finished;
1934 handler for receiving the response to ipreallocate
1936 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
1937 TDB_DATA data, void *private_data)
1939 ipreallocate_finished = true;
1942 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1944 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1946 event_add_timed(ctdb->ev, ctdb,
1947 timeval_current_ofs(1, 0),
1948 ctdb_every_second, ctdb);
1952 ask the recovery daemon on the recovery master to perform a ip reallocation
1954 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
1958 struct takeover_run_reply rd;
1959 struct ctdb_node_map *nodemap=NULL;
1961 struct timeval tv = timeval_current();
1963 /* we need some events to trigger so we can timeout and restart
1966 event_add_timed(ctdb->ev, ctdb,
1967 timeval_current_ofs(1, 0),
1968 ctdb_every_second, ctdb);
1970 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1972 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1975 rd.srvid = getpid();
1977 /* register a message port for receiveing the reply so that we
1978 can receive the reply
1980 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1982 data.dptr = (uint8_t *)&rd;
1983 data.dsize = sizeof(rd);
1986 /* get the number of nodes and node flags */
1987 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
1988 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1993 ipreallocate_finished = false;
1995 for (i=0; i<nodemap->num;i++) {
1996 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1999 /* Send to all active nodes. Only recmaster will reply. */
2000 ret = ctdb_client_send_message(ctdb, i, CTDB_SRVID_TAKEOVER_RUN, data);
2002 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
2009 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
2013 tv = timeval_current();
2014 /* this loop will terminate when we have received the reply */
2015 while (timeval_elapsed(&tv) < 5.0 && !ipreallocate_finished) {
2016 event_loop_once(ctdb->ev);
2019 if (!ipreallocate_finished) {
2028 add a public ip address to a node
2030 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2033 int len, retries = 0;
2035 ctdb_sock_addr addr;
2036 struct ctdb_control_ip_iface *pub;
2037 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2038 struct ctdb_all_public_ips *ips;
2042 talloc_free(tmp_ctx);
2046 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2047 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2048 talloc_free(tmp_ctx);
2052 /* read the public ip list from the node */
2053 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2055 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2056 talloc_free(tmp_ctx);
2059 for (i=0;i<ips->num;i++) {
2060 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2061 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2068 /* Dont timeout. This command waits for an ip reallocation
2069 which sometimes can take wuite a while if there has
2070 been a recent recovery
2074 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2075 pub = talloc_size(tmp_ctx, len);
2076 CTDB_NO_MEMORY(ctdb, pub);
2080 pub->len = strlen(argv[1])+1;
2081 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2084 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2086 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2090 } while (retries < 5 && ret != 0);
2092 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2093 talloc_free(tmp_ctx);
2097 if (rebalance_node(ctdb, options.pnn) != 0) {
2098 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2102 talloc_free(tmp_ctx);
2107 add a public ip address to a node
2109 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2111 ctdb_sock_addr addr;
2117 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2118 printf("Badly formed ip : %s\n", argv[0]);
2122 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2127 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2129 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2131 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2132 struct ctdb_node_map *nodemap=NULL;
2133 struct ctdb_all_public_ips *ips;
2136 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2138 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2142 /* remove it from the nodes that are not hosting the ip currently */
2143 for(i=0;i<nodemap->num;i++){
2144 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2147 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2148 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2152 for (j=0;j<ips->num;j++) {
2153 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2161 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2165 options.pnn = nodemap->nodes[i].pnn;
2166 control_delip(ctdb, argc, argv);
2170 /* remove it from every node (also the one hosting it) */
2171 for(i=0;i<nodemap->num;i++){
2172 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2175 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2176 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2180 for (j=0;j<ips->num;j++) {
2181 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2189 options.pnn = nodemap->nodes[i].pnn;
2190 control_delip(ctdb, argc, argv);
2193 talloc_free(tmp_ctx);
2198 delete a public ip address from a node
2200 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2203 ctdb_sock_addr addr;
2204 struct ctdb_control_ip_iface pub;
2205 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2206 struct ctdb_all_public_ips *ips;
2209 talloc_free(tmp_ctx);
2213 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2214 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2218 if (options.pnn == CTDB_BROADCAST_ALL) {
2219 return control_delip_all(ctdb, argc, argv, &addr);
2226 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2228 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2229 talloc_free(tmp_ctx);
2233 for (i=0;i<ips->num;i++) {
2234 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2240 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2241 ctdb_addr_to_str(&addr)));
2242 talloc_free(tmp_ctx);
2246 /* This is an optimisation. If this node is hosting the IP
2247 * then try to move it somewhere else without invoking a full
2248 * takeover run. We don't care if this doesn't work!
2250 if (ips->ips[i].pnn == options.pnn) {
2251 (void) try_moveip(ctdb, &addr, -1);
2254 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2256 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2257 talloc_free(tmp_ctx);
2261 talloc_free(tmp_ctx);
2266 kill a tcp connection
2268 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2271 struct ctdb_control_killtcp killtcp;
2277 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2278 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2282 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2283 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2287 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2289 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2300 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2303 ctdb_sock_addr addr;
2309 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2310 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2314 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2316 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2324 register a server id
2326 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2329 struct ctdb_server_id server_id;
2335 server_id.pnn = strtoul(argv[0], NULL, 0);
2336 server_id.type = strtoul(argv[1], NULL, 0);
2337 server_id.server_id = strtoul(argv[2], NULL, 0);
2339 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2341 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2344 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2350 unregister a server id
2352 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2355 struct ctdb_server_id server_id;
2361 server_id.pnn = strtoul(argv[0], NULL, 0);
2362 server_id.type = strtoul(argv[1], NULL, 0);
2363 server_id.server_id = strtoul(argv[2], NULL, 0);
2365 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2367 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2374 check if a server id exists
2376 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2380 struct ctdb_server_id server_id;
2386 server_id.pnn = strtoul(argv[0], NULL, 0);
2387 server_id.type = strtoul(argv[1], NULL, 0);
2388 server_id.server_id = strtoul(argv[2], NULL, 0);
2390 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2392 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2397 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2399 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2405 get a list of all server ids that are registered on a node
2407 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2410 struct ctdb_server_id_list *server_ids;
2412 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2414 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2418 for (i=0; i<server_ids->num; i++) {
2419 printf("Server id %d:%d:%d\n",
2420 server_ids->server_ids[i].pnn,
2421 server_ids->server_ids[i].type,
2422 server_ids->server_ids[i].server_id);
2429 check if a server id exists
2431 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2433 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2439 talloc_free(tmp_ctx);
2443 ids = talloc_array(tmp_ctx, uint64_t, argc);
2444 result = talloc_array(tmp_ctx, uint8_t, argc);
2446 for (i = 0; i < argc; i++) {
2447 ids[i] = strtoull(argv[i], NULL, 0);
2450 if (!ctdb_check_message_handlers(ctdb_connection,
2451 options.pnn, argc, ids, result)) {
2452 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2454 talloc_free(tmp_ctx);
2458 for (i=0; i < argc; i++) {
2459 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2460 result[i] ? "exists" : "does not exist");
2463 talloc_free(tmp_ctx);
2468 send a tcp tickle ack
2470 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2473 ctdb_sock_addr src, dst;
2479 if (!parse_ip_port(argv[0], &src)) {
2480 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2484 if (!parse_ip_port(argv[1], &dst)) {
2485 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2489 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2493 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2500 display public ip status
2502 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2505 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2506 struct ctdb_all_public_ips *ips;
2508 if (options.pnn == CTDB_BROADCAST_ALL) {
2509 /* read the list of public ips from all nodes */
2510 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2512 /* read the public ip list from this node */
2513 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2516 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2517 talloc_free(tmp_ctx);
2521 if (options.machinereadable){
2522 printf(":Public IP:Node:");
2523 if (options.verbose){
2524 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2528 if (options.pnn == CTDB_BROADCAST_ALL) {
2529 printf("Public IPs on ALL nodes\n");
2531 printf("Public IPs on node %u\n", options.pnn);
2535 for (i=1;i<=ips->num;i++) {
2536 struct ctdb_control_public_ip_info *info = NULL;
2538 char *aciface = NULL;
2539 char *avifaces = NULL;
2540 char *cifaces = NULL;
2542 if (options.pnn == CTDB_BROADCAST_ALL) {
2543 pnn = ips->ips[ips->num-i].pnn;
2549 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2550 &ips->ips[ips->num-i].addr, &info);
2557 for (j=0; j < info->num; j++) {
2558 if (cifaces == NULL) {
2559 cifaces = talloc_strdup(info,
2560 info->ifaces[j].name);
2562 cifaces = talloc_asprintf_append(cifaces,
2564 info->ifaces[j].name);
2567 if (info->active_idx == j) {
2568 aciface = info->ifaces[j].name;
2571 if (info->ifaces[j].link_state == 0) {
2575 if (avifaces == NULL) {
2576 avifaces = talloc_strdup(info, info->ifaces[j].name);
2578 avifaces = talloc_asprintf_append(avifaces,
2580 info->ifaces[j].name);
2585 if (options.machinereadable){
2587 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2588 ips->ips[ips->num-i].pnn);
2589 if (options.verbose){
2592 avifaces?avifaces:"",
2593 cifaces?cifaces:"");
2597 if (options.verbose) {
2598 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2599 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2600 ips->ips[ips->num-i].pnn,
2602 avifaces?avifaces:"",
2603 cifaces?cifaces:"");
2606 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2607 ips->ips[ips->num-i].pnn);
2613 talloc_free(tmp_ctx);
2620 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2623 ctdb_sock_addr addr;
2624 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2625 struct ctdb_control_public_ip_info *info;
2628 talloc_free(tmp_ctx);
2632 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2633 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2637 /* read the public ip info from this node */
2638 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2639 tmp_ctx, &addr, &info);
2641 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2642 argv[0], options.pnn));
2643 talloc_free(tmp_ctx);
2647 printf("Public IP[%s] info on node %u\n",
2648 ctdb_addr_to_str(&info->ip.addr),
2651 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2652 ctdb_addr_to_str(&info->ip.addr),
2653 info->ip.pnn, info->num);
2655 for (i=0; i<info->num; i++) {
2656 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2658 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2659 i+1, info->ifaces[i].name,
2660 info->ifaces[i].link_state?"up":"down",
2661 (unsigned int)info->ifaces[i].references,
2662 (i==info->active_idx)?" (active)":"");
2665 talloc_free(tmp_ctx);
2670 display interfaces status
2672 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2675 struct ctdb_ifaces_list *ifaces;
2677 /* read the public ip list from this node */
2678 if (!ctdb_getifaces(ctdb_connection, options.pnn, &ifaces)) {
2679 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2684 if (options.machinereadable){
2685 printf(":Name:LinkStatus:References:\n");
2687 printf("Interfaces on node %u\n", options.pnn);
2690 for (i=0; i<ifaces->num; i++) {
2691 if (options.machinereadable){
2692 printf(":%s:%s:%u\n",
2693 ifaces->ifaces[i].name,
2694 ifaces->ifaces[i].link_state?"1":"0",
2695 (unsigned int)ifaces->ifaces[i].references);
2697 printf("name:%s link:%s references:%u\n",
2698 ifaces->ifaces[i].name,
2699 ifaces->ifaces[i].link_state?"up":"down",
2700 (unsigned int)ifaces->ifaces[i].references);
2704 ctdb_free_ifaces(ifaces);
2710 set link status of an interface
2712 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2715 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2716 struct ctdb_control_iface_info info;
2724 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2725 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2727 talloc_free(tmp_ctx);
2730 strcpy(info.name, argv[0]);
2732 if (strcmp(argv[1], "up") == 0) {
2733 info.link_state = 1;
2734 } else if (strcmp(argv[1], "down") == 0) {
2735 info.link_state = 0;
2737 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2739 talloc_free(tmp_ctx);
2743 /* read the public ip list from this node */
2744 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2747 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2748 argv[0], options.pnn));
2749 talloc_free(tmp_ctx);
2753 talloc_free(tmp_ctx);
2758 display pid of a ctdb daemon
2760 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2765 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2767 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2770 printf("Pid:%d\n", pid);
2776 disable a remote node
2778 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2781 struct ctdb_node_map *nodemap=NULL;
2783 /* check if the node is already disabled */
2784 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2785 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2788 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2789 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2794 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2796 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2802 /* read the nodemap and verify the change took effect */
2803 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2804 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2808 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2809 ret = control_ipreallocate(ctdb, argc, argv);
2811 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2819 enable a disabled remote node
2821 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2825 struct ctdb_node_map *nodemap=NULL;
2828 /* check if the node is already enabled */
2829 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2830 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2833 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2834 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2839 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2841 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2847 /* read the nodemap and verify the change took effect */
2848 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2849 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2853 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2855 ret = control_ipreallocate(ctdb, argc, argv);
2857 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2867 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2870 struct ctdb_node_map *nodemap=NULL;
2873 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2875 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2880 /* read the nodemap and verify the change took effect */
2881 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2882 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2885 } while (nodemap == NULL || !(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2886 ret = control_ipreallocate(ctdb, argc, argv);
2888 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2896 restart a stopped remote node
2898 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2902 struct ctdb_node_map *nodemap=NULL;
2905 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2907 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2913 /* read the nodemap and verify the change took effect */
2914 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2915 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2918 } while (nodemap == NULL || nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2919 ret = control_ipreallocate(ctdb, argc, argv);
2921 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2928 static uint32_t get_generation(struct ctdb_context *ctdb)
2930 struct ctdb_vnn_map *vnnmap=NULL;
2933 /* wait until the recmaster is not in recovery mode */
2935 uint32_t recmode, recmaster;
2937 if (vnnmap != NULL) {
2938 talloc_free(vnnmap);
2942 /* get the recmaster */
2943 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
2944 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2948 /* get recovery mode */
2949 if (!ctdb_getrecmode(ctdb_connection, recmaster, &recmode)) {
2950 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2954 /* get the current generation number */
2955 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2957 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2961 if ((recmode == CTDB_RECOVERY_NORMAL)
2962 && (vnnmap->generation != 1)){
2963 return vnnmap->generation;
2970 ban a node from the cluster
2972 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2975 struct ctdb_node_map *nodemap=NULL;
2976 struct ctdb_ban_time bantime;
2982 /* verify the node exists */
2983 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2985 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2989 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2990 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2994 bantime.pnn = options.pnn;
2995 bantime.time = strtoul(argv[0], NULL, 0);
2997 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2999 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
3003 ret = control_ipreallocate(ctdb, argc, argv);
3005 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
3014 unban a node from the cluster
3016 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3019 struct ctdb_node_map *nodemap=NULL;
3020 struct ctdb_ban_time bantime;
3022 /* verify the node exists */
3023 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3025 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3029 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
3030 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
3034 bantime.pnn = options.pnn;
3037 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
3039 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
3043 ret = control_ipreallocate(ctdb, argc, argv);
3045 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
3054 show ban information for a node
3056 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3059 struct ctdb_node_map *nodemap=NULL;
3060 struct ctdb_ban_time *bantime;
3062 /* verify the node exists */
3063 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3065 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3069 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3071 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3075 if (bantime->time == 0) {
3076 printf("Node %u is not banned\n", bantime->pnn);
3078 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
3087 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3091 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3093 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3103 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3106 uint32_t generation, next_generation;
3109 /* "force" option ignores freeze failure and forces recovery */
3110 force = (argc == 1) && (strcasecmp(argv[0], "force") == 0);
3112 /* record the current generation number */
3113 generation = get_generation(ctdb);
3115 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3118 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3121 DEBUG(DEBUG_WARNING, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3124 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3126 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3130 /* wait until we are in a new generation */
3132 next_generation = get_generation(ctdb);
3133 if (next_generation != generation) {
3144 display monitoring mode of a remote node
3146 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3151 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3153 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3156 if (!options.machinereadable){
3157 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3160 printf(":%d:\n",monmode);
3167 display capabilities of a remote node
3169 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3171 uint32_t capabilities;
3173 if (!ctdb_getcapabilities(ctdb_connection, options.pnn, &capabilities)) {
3174 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3178 if (!options.machinereadable){
3179 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3180 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3181 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3182 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3184 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3185 printf(":%d:%d:%d:%d:\n",
3186 !!(capabilities&CTDB_CAP_RECMASTER),
3187 !!(capabilities&CTDB_CAP_LMASTER),
3188 !!(capabilities&CTDB_CAP_LVS),
3189 !!(capabilities&CTDB_CAP_NATGW));
3195 display lvs configuration
3197 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3199 uint32_t *capabilities;
3200 struct ctdb_node_map *nodemap=NULL;
3202 int healthy_count = 0;
3204 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
3205 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3209 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3210 CTDB_NO_MEMORY(ctdb, capabilities);
3214 /* collect capabilities for all connected nodes */
3215 for (i=0; i<nodemap->num; i++) {
3216 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3219 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3223 if (!ctdb_getcapabilities(ctdb_connection, i, &capabilities[i])) {
3224 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3229 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3233 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3238 /* Print all LVS nodes */
3239 for (i=0; i<nodemap->num; i++) {
3240 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3243 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3246 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3250 if (healthy_count != 0) {
3251 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3256 printf("%d:%s\n", i,
3257 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3261 ctdb_free_nodemap(nodemap);
3266 display who is the lvs master
3268 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3270 uint32_t *capabilities;
3271 struct ctdb_node_map *nodemap=NULL;
3273 int healthy_count = 0;
3275 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
3276 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3280 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3281 CTDB_NO_MEMORY(ctdb, capabilities);
3285 /* collect capabilities for all connected nodes */
3286 for (i=0; i<nodemap->num; i++) {
3287 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3290 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3294 if (!ctdb_getcapabilities(ctdb_connection, i, &capabilities[i])) {
3295 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3300 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3304 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3309 /* find and show the lvsmaster */
3310 for (i=0; i<nodemap->num; i++) {
3311 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3314 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3317 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3321 if (healthy_count != 0) {
3322 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3327 if (options.machinereadable){
3330 printf("Node %d is LVS master\n", i);
3336 printf("There is no LVS master\n");
3338 ctdb_free_nodemap(nodemap);
3343 disable monitoring on a node
3345 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3350 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3352 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3355 printf("Monitoring mode:%s\n","DISABLED");
3361 enable monitoring on a node
3363 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3368 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3370 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3373 printf("Monitoring mode:%s\n","ACTIVE");
3379 display remote list of keys/data for a db
3381 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3383 const char *db_name;
3384 struct ctdb_db_context *ctdb_db;
3386 struct ctdb_dump_db_context c;
3395 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3399 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3400 if (ctdb_db == NULL) {
3401 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3405 if (options.printlmaster) {
3406 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3407 ctdb, &ctdb->vnn_map);
3409 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3417 c.printemptyrecords = (bool)options.printemptyrecords;
3418 c.printdatasize = (bool)options.printdatasize;
3419 c.printlmaster = (bool)options.printlmaster;
3420 c.printhash = (bool)options.printhash;
3421 c.printrecordflags = (bool)options.printrecordflags;
3423 /* traverse and dump the cluster tdb */
3424 ret = ctdb_dump_db(ctdb_db, &c);
3426 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3427 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3428 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3432 talloc_free(ctdb_db);
3434 printf("Dumped %d records\n", ret);
3438 struct cattdb_data {
3439 struct ctdb_context *ctdb;
3443 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3445 struct cattdb_data *d = private_data;
3446 struct ctdb_dump_db_context c;
3452 c.printemptyrecords = (bool)options.printemptyrecords;
3453 c.printdatasize = (bool)options.printdatasize;
3454 c.printlmaster = false;
3455 c.printhash = (bool)options.printhash;
3456 c.printrecordflags = true;
3458 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3462 cat the local tdb database using same format as catdb
3464 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3466 const char *db_name;
3467 struct ctdb_db_context *ctdb_db;
3468 struct cattdb_data d;
3477 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3481 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3482 if (ctdb_db == NULL) {
3483 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3487 /* traverse the local tdb */
3490 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3491 printf("Failed to cattdb data\n");
3494 talloc_free(ctdb_db);
3496 printf("Dumped %d records\n", d.count);
3501 display the content of a database key
3503 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3505 const char *db_name;
3506 struct ctdb_db_context *ctdb_db;
3507 struct ctdb_record_handle *h;
3508 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3518 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3522 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3523 if (ctdb_db == NULL) {
3524 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3528 key.dptr = discard_const(argv[1]);
3529 key.dsize = strlen((char *)key.dptr);
3531 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3533 printf("Failed to fetch record '%s' on node %d\n",
3534 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3535 talloc_free(tmp_ctx);
3539 printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3541 talloc_free(ctdb_db);
3542 talloc_free(tmp_ctx);
3547 display the content of a database key
3549 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3551 const char *db_name;
3552 struct ctdb_db_context *ctdb_db;
3553 struct ctdb_record_handle *h;
3554 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3564 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3568 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3569 if (ctdb_db == NULL) {
3570 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3574 key.dptr = discard_const(argv[1]);
3575 key.dsize = strlen((char *)key.dptr);
3577 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3579 printf("Failed to fetch record '%s' on node %d\n",
3580 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3581 talloc_free(tmp_ctx);
3585 data.dptr = discard_const(argv[2]);
3586 data.dsize = strlen((char *)data.dptr);
3588 if (ctdb_record_store(h, data) != 0) {
3589 printf("Failed to store record\n");
3593 talloc_free(ctdb_db);
3594 talloc_free(tmp_ctx);
3599 fetch a record from a persistent database
3601 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3603 const char *db_name;
3604 struct ctdb_db_context *ctdb_db;
3605 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3606 struct ctdb_transaction_handle *h;
3613 talloc_free(tmp_ctx);
3619 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3620 talloc_free(tmp_ctx);
3624 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3626 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3627 talloc_free(tmp_ctx);
3631 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3632 if (ctdb_db == NULL) {
3633 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3634 talloc_free(tmp_ctx);
3638 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3640 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3641 talloc_free(tmp_ctx);
3645 key.dptr = discard_const(argv[1]);
3646 key.dsize = strlen(argv[1]);
3647 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3649 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3650 talloc_free(tmp_ctx);
3654 if (data.dsize == 0 || data.dptr == NULL) {
3655 DEBUG(DEBUG_ERR,("Record is empty\n"));
3656 talloc_free(tmp_ctx);
3661 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3663 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3664 talloc_free(tmp_ctx);
3667 write(fd, data.dptr, data.dsize);
3670 write(1, data.dptr, data.dsize);
3673 /* abort the transaction */
3677 talloc_free(tmp_ctx);
3682 fetch a record from a tdb-file
3684 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3686 const char *tdb_file;
3689 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3698 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3700 printf("Failed to open TDB file %s\n", tdb_file);
3704 if (!strncmp(argv[1], "0x", 2)) {
3705 key = hextodata(tmp_ctx, argv[1] + 2);
3706 if (key.dsize == 0) {
3707 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3711 key.dptr = discard_const(argv[1]);
3712 key.dsize = strlen(argv[1]);
3715 data = tdb_fetch(tdb, key);
3716 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3717 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3725 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3727 printf("Failed to open output file %s\n", argv[2]);
3730 if (options.verbose){
3731 write(fd, data.dptr, data.dsize);
3733 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3737 if (options.verbose){
3738 write(1, data.dptr, data.dsize);
3740 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3744 talloc_free(tmp_ctx);
3749 store a record and header to a tdb-file
3751 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
3753 const char *tdb_file;
3756 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3764 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
3766 printf("Failed to open TDB file %s\n", tdb_file);
3770 if (!strncmp(argv[1], "0x", 2)) {
3771 key = hextodata(tmp_ctx, argv[1] + 2);
3772 if (key.dsize == 0) {
3773 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3777 key.dptr = discard_const(argv[1]);
3778 key.dsize = strlen(argv[1]);
3781 if (!strncmp(argv[2], "0x", 2)) {
3782 data = hextodata(tmp_ctx, argv[2] + 2);
3783 if (data.dsize == 0) {
3784 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
3788 data.dptr = discard_const(argv[2]);
3789 data.dsize = strlen(argv[2]);
3792 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
3793 printf("Not enough data. You must specify the full ctdb_ltdb_header too when storing\n");
3796 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
3797 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
3804 talloc_free(tmp_ctx);
3809 write a record to a persistent database
3811 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3813 const char *db_name;
3814 struct ctdb_db_context *ctdb_db;
3815 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3816 struct ctdb_transaction_handle *h;
3822 talloc_free(tmp_ctx);
3826 fd = open(argv[2], O_RDONLY);
3828 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3829 talloc_free(tmp_ctx);
3833 ret = fstat(fd, &st);
3835 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3837 talloc_free(tmp_ctx);
3841 if (!S_ISREG(st.st_mode)) {
3842 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3844 talloc_free(tmp_ctx);
3848 data.dsize = st.st_size;
3849 if (data.dsize == 0) {
3852 data.dptr = talloc_size(tmp_ctx, data.dsize);
3853 if (data.dptr == NULL) {
3854 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3856 talloc_free(tmp_ctx);
3859 ret = read(fd, data.dptr, data.dsize);
3860 if (ret != data.dsize) {
3861 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3863 talloc_free(tmp_ctx);
3872 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3873 if (ctdb_db == NULL) {
3874 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3875 talloc_free(tmp_ctx);
3879 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3881 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3882 talloc_free(tmp_ctx);
3886 key.dptr = discard_const(argv[1]);
3887 key.dsize = strlen(argv[1]);
3888 ret = ctdb_transaction_store(h, key, data);
3890 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3891 talloc_free(tmp_ctx);
3895 ret = ctdb_transaction_commit(h);
3897 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3898 talloc_free(tmp_ctx);
3903 talloc_free(tmp_ctx);
3908 * delete a record from a persistent database
3910 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
3912 const char *db_name;
3913 struct ctdb_db_context *ctdb_db;
3914 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3915 struct ctdb_transaction_handle *h;
3922 talloc_free(tmp_ctx);
3928 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3929 talloc_free(tmp_ctx);
3933 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3935 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
3936 talloc_free(tmp_ctx);
3940 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3941 if (ctdb_db == NULL) {
3942 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
3943 talloc_free(tmp_ctx);
3947 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3949 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
3950 talloc_free(tmp_ctx);
3954 key.dptr = discard_const(argv[1]);
3955 key.dsize = strlen(argv[1]);
3956 ret = ctdb_transaction_store(h, key, tdb_null);
3958 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
3959 talloc_free(tmp_ctx);
3963 ret = ctdb_transaction_commit(h);
3965 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
3966 talloc_free(tmp_ctx);
3970 talloc_free(tmp_ctx);
3975 check if a service is bound to a port or not
3977 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
3982 struct sockaddr_in sin;
3985 printf("Use: ctdb chktcport <port>\n");
3989 port = atoi(argv[0]);
3991 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
3993 printf("Failed to open local socket\n");
3997 v = fcntl(s, F_GETFL, 0);
3998 fcntl(s, F_SETFL, v | O_NONBLOCK);
4000 bzero(&sin, sizeof(sin));
4001 sin.sin_family = PF_INET;
4002 sin.sin_port = htons(port);
4003 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4006 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4015 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4016 TDB_DATA data, void *private_data)
4018 DEBUG(DEBUG_ERR,("Log data received\n"));
4019 if (data.dsize > 0) {
4020 printf("%s", data.dptr);
4027 display a list of log messages from the in memory ringbuffer
4029 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4033 struct ctdb_get_log_addr log_addr;
4037 /* Since this can fail, do it first */
4038 log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4039 if (log_addr.pnn == -1) {
4040 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4044 /* Process options */
4046 log_addr.level = DEBUG_NOTICE;
4047 for (i = 0; i < argc; i++) {
4048 if (strcmp(argv[i], "recoverd") == 0) {
4049 main_daemon = false;
4051 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4052 log_addr.level = get_debug_by_desc(argv[i]);
4054 log_addr.level = strtol(argv[i], NULL, 0);
4059 /* Our message port is our PID */
4060 log_addr.srvid = getpid();
4062 data.dptr = (unsigned char *)&log_addr;
4063 data.dsize = sizeof(log_addr);
4065 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4067 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4070 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4075 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4077 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4078 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4079 if (ret != 0 || res != 0) {
4080 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4081 talloc_free(tmp_ctx);
4084 talloc_free(tmp_ctx);
4086 ret = ctdb_client_send_message(ctdb, options.pnn,
4087 CTDB_SRVID_GETLOG, data);
4089 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4094 tv = timeval_current();
4095 /* this loop will terminate when we have received the reply */
4096 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4097 event_loop_once(ctdb->ev);
4100 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4106 clear the in memory log area
4108 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4112 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4113 /* "recoverd" not given - get logs from main daemon */
4116 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4118 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4119 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4120 if (ret != 0 || res != 0) {
4121 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4122 talloc_free(tmp_ctx);
4126 talloc_free(tmp_ctx);
4128 TDB_DATA data; /* unused in recoverd... */
4131 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4133 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4142 static uint32_t reloadips_finished;
4144 static void reloadips_handler(struct ctdb_context *ctdb, uint64_t srvid,
4145 TDB_DATA data, void *private_data)
4147 reloadips_finished = 1;
4150 static int reloadips_all(struct ctdb_context *ctdb)
4152 struct reloadips_all_reply rips;
4153 struct ctdb_node_map *nodemap=NULL;
4158 /* check that there are valid nodes available */
4159 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
4160 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
4163 for (i=0; i<nodemap->num;i++) {
4164 if (nodemap->nodes[i].flags != 0) {
4165 DEBUG(DEBUG_ERR,("reloadips -n all can only be used when all nodes are up and healthy. Aborting due to problem with node %d\n", i));
4171 rips.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4172 if (rips.pnn == -1) {
4173 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4176 rips.srvid = getpid();
4179 /* register a message port for receiveing the reply so that we
4180 can receive the reply
4182 ctdb_client_set_message_handler(ctdb, rips.srvid, reloadips_handler, NULL);
4184 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
4185 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node\n"));
4190 data.dptr = (uint8_t *)&rips;
4191 data.dsize = sizeof(rips);
4193 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_RELOAD_ALL_IPS, data);
4195 DEBUG(DEBUG_ERR,("Failed to send reload all ips request message to %u\n", options.pnn));
4199 reloadips_finished = 0;
4200 while (reloadips_finished == 0) {
4201 event_loop_once(ctdb->ev);
4208 reload public ips on a specific node
4210 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4215 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4217 if (options.pnn == CTDB_BROADCAST_ALL) {
4218 return reloadips_all(ctdb);
4221 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4222 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4223 if (ret != 0 || res != 0) {
4224 DEBUG(DEBUG_ERR,("Failed to reload ips\n"));
4225 talloc_free(tmp_ctx);
4229 talloc_free(tmp_ctx);
4234 display a list of the databases on a remote ctdb
4236 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4239 struct ctdb_dbid_map *dbmap=NULL;
4241 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4243 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4247 if(options.machinereadable){
4248 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4249 for(i=0;i<dbmap->num;i++){
4257 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4258 dbmap->dbs[i].dbid, ctdb, &path);
4259 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4260 dbmap->dbs[i].dbid, ctdb, &name);
4261 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4262 dbmap->dbs[i].dbid, ctdb, &health);
4263 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4264 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4265 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4266 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4267 dbmap->dbs[i].dbid, name, path,
4268 !!(persistent), !!(sticky),
4269 !!(health), !!(readonly));
4274 printf("Number of databases:%d\n", dbmap->num);
4275 for(i=0;i<dbmap->num;i++){
4283 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4284 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4285 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4286 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4287 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4288 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4289 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4290 dbmap->dbs[i].dbid, name, path,
4291 persistent?" PERSISTENT":"",
4292 sticky?" STICKY":"",
4293 readonly?" READONLY":"",
4294 health?" UNHEALTHY":"");
4301 display the status of a database on a remote ctdb
4303 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4305 const char *db_name;
4317 if (!db_exists(ctdb, db_name, &db_id, &flags)) {
4321 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4322 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4323 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4324 db_id, db_name, path,
4325 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4326 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4327 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4328 (health ? health : "OK"));
4334 check if the local node is recmaster or not
4335 it will return 1 if this node is the recmaster and 0 if it is not
4336 or if the local ctdb daemon could not be contacted
4338 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4340 uint32_t mypnn, recmaster;
4342 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
4344 printf("Failed to get pnn of node\n");
4348 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
4349 printf("Failed to get the recmaster\n");
4353 if (recmaster != mypnn) {
4354 printf("this node is not the recmaster\n");
4358 printf("this node is the recmaster\n");
4365 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4368 struct timeval tv = timeval_current();
4369 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4371 printf("Unable to get ping response from node %u\n", options.pnn);
4374 printf("response from %u time=%.6f sec (%d clients)\n",
4375 options.pnn, timeval_elapsed(&tv), ret);
4382 get a node's runstate
4384 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4387 enum ctdb_runstate runstate;
4389 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4391 printf("Unable to get runstate response from node %u\n",
4396 enum ctdb_runstate t;
4398 for (i=0; i<argc; i++) {
4400 t = runstate_from_string(argv[i]);
4401 if (t == CTDB_RUNSTATE_UNKNOWN) {
4402 printf("Invalid run state (%s)\n", argv[i]);
4406 if (t == runstate) {
4413 printf("CTDB not in required run state (got %s)\n",
4414 runstate_to_string((enum ctdb_runstate)runstate));
4419 printf("%s\n", runstate_to_string(runstate));
4427 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4438 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4440 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4444 printf("%-23s = %u\n", name, value);
4451 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4462 value = strtoul(argv[1], NULL, 0);
4464 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4466 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4475 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4481 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4483 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4487 for (i=0;i<count;i++) {
4488 control_getvar(ctdb, 1, &list[i]);
4497 display debug level on a node
4499 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4504 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4506 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4509 if (options.machinereadable){
4510 printf(":Name:Level:\n");
4511 printf(":%s:%d:\n",get_debug_by_level(level),level);
4513 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4520 display reclock file of a node
4522 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4525 const char *reclock;
4527 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4529 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4532 if (options.machinereadable){
4533 if (reclock != NULL) {
4534 printf("%s", reclock);
4537 if (reclock == NULL) {
4538 printf("No reclock file used.\n");
4540 printf("Reclock file:%s\n", reclock);
4548 set the reclock file of a node
4550 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4553 const char *reclock;
4557 } else if (argc == 1) {
4563 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4565 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4572 set the natgw state on/off
4574 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4577 uint32_t natgwstate;
4583 if (!strcmp(argv[0], "on")) {
4585 } else if (!strcmp(argv[0], "off")) {
4591 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4593 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4601 set the lmaster role on/off
4603 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4606 uint32_t lmasterrole;
4612 if (!strcmp(argv[0], "on")) {
4614 } else if (!strcmp(argv[0], "off")) {
4620 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4622 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4630 set the recmaster role on/off
4632 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4635 uint32_t recmasterrole;
4641 if (!strcmp(argv[0], "on")) {
4643 } else if (!strcmp(argv[0], "off")) {
4649 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4651 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4659 set debug level on a node or all nodes
4661 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4667 printf("You must specify the debug level. Valid levels are:\n");
4668 for (i=0; debug_levels[i].description != NULL; i++) {
4669 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4675 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4676 level = get_debug_by_desc(argv[0]);
4678 level = strtol(argv[0], NULL, 0);
4681 for (i=0; debug_levels[i].description != NULL; i++) {
4682 if (level == debug_levels[i].level) {
4686 if (debug_levels[i].description == NULL) {
4687 printf("Invalid debug level, must be one of\n");
4688 for (i=0; debug_levels[i].description != NULL; i++) {
4689 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4694 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4696 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4705 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4711 priority = strtol(argv[0], NULL, 0);
4715 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4717 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4719 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4726 attach to a database
4728 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4730 const char *db_name;
4731 struct ctdb_db_context *ctdb_db;
4732 bool persistent = false;
4742 if (strcmp(argv[1], "persistent") != 0) {
4748 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4749 if (ctdb_db == NULL) {
4750 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4760 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4762 struct ctdb_db_priority db_prio;
4769 db_prio.db_id = strtoul(argv[0], NULL, 0);
4770 db_prio.priority = strtoul(argv[1], NULL, 0);
4772 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
4774 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
4784 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4786 uint32_t db_id, priority;
4793 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4797 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4799 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4803 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
4809 set the sticky records capability for a database
4811 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
4813 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4821 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4825 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
4827 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
4828 talloc_free(tmp_ctx);
4832 talloc_free(tmp_ctx);
4837 set the readonly capability for a database
4839 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4841 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4849 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4853 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4855 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4856 talloc_free(tmp_ctx);
4860 talloc_free(tmp_ctx);
4867 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4877 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4881 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &seqnum);
4883 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
4887 printf("Sequence number:%lld\n", (long long)seqnum);
4895 static int control_setdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4898 struct ctdb_db_context *ctdb_db;
4901 uint64_t old_seqnum, new_seqnum;
4902 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4903 struct ctdb_transaction_handle *h;
4908 talloc_free(tmp_ctx);
4912 if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
4913 talloc_free(tmp_ctx);
4917 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4919 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", argv[0]));
4920 talloc_free(tmp_ctx);
4924 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &old_seqnum);
4926 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
4927 talloc_free(tmp_ctx);
4931 new_seqnum = strtoull(argv[1], NULL, 0);
4932 if (new_seqnum <= old_seqnum) {
4933 DEBUG(DEBUG_ERR, ("New sequence number is less than current sequence number\n"));
4934 talloc_free(tmp_ctx);
4938 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], persistent, 0);
4939 if (ctdb_db == NULL) {
4940 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4941 talloc_free(tmp_ctx);
4945 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4947 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", argv[0]));
4948 talloc_free(tmp_ctx);
4952 key.dptr = (uint8_t *)discard_const(CTDB_DB_SEQNUM_KEY);
4953 key.dsize = strlen(CTDB_DB_SEQNUM_KEY) + 1;
4955 data.dsize = sizeof(new_seqnum);
4956 data.dptr = talloc_size(tmp_ctx, data.dsize);
4957 *data.dptr = new_seqnum;
4959 ret = ctdb_transaction_store(h, key, data);
4961 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4962 talloc_free(tmp_ctx);
4966 ret = ctdb_transaction_commit(h);
4968 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4969 talloc_free(tmp_ctx);
4973 talloc_free(tmp_ctx);
4978 run an eventscript on a node
4980 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
4986 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4989 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4993 data.dptr = (unsigned char *)discard_const(argv[0]);
4994 data.dsize = strlen((char *)data.dptr) + 1;
4996 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
4998 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
4999 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5000 if (ret != 0 || res != 0) {
5001 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5002 talloc_free(tmp_ctx);
5005 talloc_free(tmp_ctx);
5009 #define DB_VERSION 1
5010 #define MAX_DB_NAME 64
5011 struct db_file_header {
5012 unsigned long version;
5014 unsigned long persistent;
5016 const char name[MAX_DB_NAME];
5019 struct backup_data {
5020 struct ctdb_marshall_buffer *records;
5023 bool traverse_error;
5026 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5028 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5029 struct ctdb_rec_data *rec;
5031 /* add the record */
5032 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5034 bd->traverse_error = true;
5035 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5038 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5039 if (bd->records == NULL) {
5040 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5041 bd->traverse_error = true;
5044 bd->records->count++;
5045 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5046 bd->len += rec->length;
5054 * backup a database to a file
5056 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5059 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5060 struct db_file_header dbhdr;
5061 struct ctdb_db_context *ctdb_db;
5062 struct backup_data *bd;
5065 const char *reason = NULL;
5070 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5074 if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
5078 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5079 db_id, tmp_ctx, &reason);
5081 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5083 talloc_free(tmp_ctx);
5087 uint32_t allow_unhealthy = 0;
5089 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5090 "AllowUnhealthyDBRead",
5093 if (allow_unhealthy != 1) {
5094 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5097 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5099 talloc_free(tmp_ctx);
5103 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5105 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5106 "tunnable AllowUnhealthyDBRead = %u\n",
5110 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5111 if (ctdb_db == NULL) {
5112 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5113 talloc_free(tmp_ctx);
5118 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5120 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5121 talloc_free(tmp_ctx);
5126 bd = talloc_zero(tmp_ctx, struct backup_data);
5128 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5129 talloc_free(tmp_ctx);
5133 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5134 if (bd->records == NULL) {
5135 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5136 talloc_free(tmp_ctx);
5140 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5141 bd->records->db_id = ctdb_db->db_id;
5142 /* traverse the database collecting all records */
5143 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5144 bd->traverse_error) {
5145 DEBUG(DEBUG_ERR,("Traverse error\n"));
5146 talloc_free(tmp_ctx);
5150 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5153 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5155 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5156 talloc_free(tmp_ctx);
5160 dbhdr.version = DB_VERSION;
5161 dbhdr.timestamp = time(NULL);
5162 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5163 dbhdr.size = bd->len;
5164 if (strlen(argv[0]) >= MAX_DB_NAME) {
5165 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5168 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
5169 ret = write(fh, &dbhdr, sizeof(dbhdr));
5171 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5174 ret = write(fh, bd->records, bd->len);
5176 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5185 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5189 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5191 talloc_free(tmp_ctx);
5196 * restore a database from a file
5198 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5201 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5204 struct db_file_header dbhdr;
5205 struct ctdb_db_context *ctdb_db;
5206 struct ctdb_node_map *nodemap=NULL;
5207 struct ctdb_vnn_map *vnnmap=NULL;
5209 struct ctdb_control_wipe_database w;
5211 uint32_t generation;
5216 if (argc < 1 || argc > 2) {
5217 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5221 fh = open(argv[0], O_RDONLY);
5223 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5224 talloc_free(tmp_ctx);
5228 read(fh, &dbhdr, sizeof(dbhdr));
5229 if (dbhdr.version != DB_VERSION) {
5230 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5231 talloc_free(tmp_ctx);
5235 dbname = discard_const(dbhdr.name);
5237 dbname = discard_const(argv[1]);
5240 outdata.dsize = dbhdr.size;
5241 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5242 if (outdata.dptr == NULL) {
5243 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5245 talloc_free(tmp_ctx);
5248 read(fh, outdata.dptr, outdata.dsize);
5251 tm = localtime(&dbhdr.timestamp);
5252 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5253 printf("Restoring database '%s' from backup @ %s\n",
5257 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5258 if (ctdb_db == NULL) {
5259 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5260 talloc_free(tmp_ctx);
5264 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5266 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5267 talloc_free(tmp_ctx);
5272 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5274 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5275 talloc_free(tmp_ctx);
5279 /* freeze all nodes */
5280 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5281 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5282 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5288 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5289 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5290 talloc_free(tmp_ctx);
5295 generation = vnnmap->generation;
5296 data.dptr = (void *)&generation;
5297 data.dsize = sizeof(generation);
5299 /* start a cluster wide transaction */
5300 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5301 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5303 TIMELIMIT(), false, data,
5306 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5311 w.db_id = ctdb_db->db_id;
5312 w.transaction_id = generation;
5314 data.dptr = (void *)&w;
5315 data.dsize = sizeof(w);
5317 /* wipe all the remote databases. */
5318 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5319 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5321 TIMELIMIT(), false, data,
5324 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5325 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5326 talloc_free(tmp_ctx);
5330 /* push the database */
5331 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5332 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5334 TIMELIMIT(), false, outdata,
5337 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5338 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5339 talloc_free(tmp_ctx);
5343 data.dptr = (void *)&ctdb_db->db_id;
5344 data.dsize = sizeof(ctdb_db->db_id);
5346 /* mark the database as healthy */
5347 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5348 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5350 TIMELIMIT(), false, data,
5353 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5354 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5355 talloc_free(tmp_ctx);
5359 data.dptr = (void *)&generation;
5360 data.dsize = sizeof(generation);
5362 /* commit all the changes */
5363 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5365 TIMELIMIT(), false, data,
5368 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5369 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5370 talloc_free(tmp_ctx);
5375 /* thaw all nodes */
5376 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5377 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5383 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5384 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5385 talloc_free(tmp_ctx);
5390 talloc_free(tmp_ctx);
5395 * dump a database backup from a file
5397 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5399 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5401 struct db_file_header dbhdr;
5405 struct ctdb_rec_data *rec = NULL;
5406 struct ctdb_marshall_buffer *m;
5407 struct ctdb_dump_db_context c;
5410 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5414 fh = open(argv[0], O_RDONLY);
5416 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5417 talloc_free(tmp_ctx);
5421 read(fh, &dbhdr, sizeof(dbhdr));
5422 if (dbhdr.version != DB_VERSION) {
5423 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5424 talloc_free(tmp_ctx);
5428 outdata.dsize = dbhdr.size;
5429 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5430 if (outdata.dptr == NULL) {
5431 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5433 talloc_free(tmp_ctx);
5436 read(fh, outdata.dptr, outdata.dsize);
5438 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5440 tm = localtime(&dbhdr.timestamp);
5441 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5442 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5443 dbhdr.name, m->db_id, tbuf);
5447 c.printemptyrecords = (bool)options.printemptyrecords;
5448 c.printdatasize = (bool)options.printdatasize;
5449 c.printlmaster = false;
5450 c.printhash = (bool)options.printhash;
5451 c.printrecordflags = (bool)options.printrecordflags;
5453 for (i=0; i < m->count; i++) {
5457 /* we do not want the header splitted, so we pass NULL*/
5458 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5461 ctdb_dumpdb_record(ctdb, key, data, &c);
5464 printf("Dumped %d records\n", i);
5465 talloc_free(tmp_ctx);
5470 * wipe a database from a file
5472 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5476 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5478 struct ctdb_db_context *ctdb_db;
5479 struct ctdb_node_map *nodemap = NULL;
5480 struct ctdb_vnn_map *vnnmap = NULL;
5482 struct ctdb_control_wipe_database w;
5484 uint32_t generation;
5488 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5492 if (!db_exists(ctdb, argv[0], NULL, &flags)) {
5496 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5497 if (ctdb_db == NULL) {
5498 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5500 talloc_free(tmp_ctx);
5504 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5507 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5509 talloc_free(tmp_ctx);
5513 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5516 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5518 talloc_free(tmp_ctx);
5522 /* freeze all nodes */
5523 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5524 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5525 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5532 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5533 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5534 CTDB_RECOVERY_ACTIVE);
5535 talloc_free(tmp_ctx);
5540 generation = vnnmap->generation;
5541 data.dptr = (void *)&generation;
5542 data.dsize = sizeof(generation);
5544 /* start a cluster wide transaction */
5545 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5546 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5548 TIMELIMIT(), false, data,
5552 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5553 "transactions.\n"));
5557 w.db_id = ctdb_db->db_id;
5558 w.transaction_id = generation;
5560 data.dptr = (void *)&w;
5561 data.dsize = sizeof(w);
5563 /* wipe all the remote databases. */
5564 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5565 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5567 TIMELIMIT(), false, data,
5570 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5571 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5572 talloc_free(tmp_ctx);
5576 data.dptr = (void *)&ctdb_db->db_id;
5577 data.dsize = sizeof(ctdb_db->db_id);
5579 /* mark the database as healthy */
5580 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5581 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5583 TIMELIMIT(), false, data,
5586 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5587 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5588 talloc_free(tmp_ctx);
5592 data.dptr = (void *)&generation;
5593 data.dsize = sizeof(generation);
5595 /* commit all the changes */
5596 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5598 TIMELIMIT(), false, data,
5601 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5602 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5603 talloc_free(tmp_ctx);
5607 /* thaw all nodes */
5608 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5609 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5615 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5616 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5617 talloc_free(tmp_ctx);
5621 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5623 talloc_free(tmp_ctx);
5630 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5636 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5637 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5638 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5639 if (ret != 0 || res != 0) {
5640 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5641 talloc_free(tmp_ctx);
5644 write(1, data.dptr, data.dsize);
5645 talloc_free(tmp_ctx);
5650 handler for memory dumps
5652 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
5653 TDB_DATA data, void *private_data)
5655 write(1, data.dptr, data.dsize);
5660 dump memory usage on the recovery daemon
5662 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5666 struct rd_memdump_reply rd;
5668 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5670 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
5673 rd.srvid = getpid();
5675 /* register a message port for receiveing the reply so that we
5676 can receive the reply
5678 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5681 data.dptr = (uint8_t *)&rd;
5682 data.dsize = sizeof(rd);
5684 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5686 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5690 /* this loop will terminate when we have received the reply */
5692 event_loop_once(ctdb->ev);
5699 send a message to a srvid
5701 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5703 unsigned long srvid;
5711 srvid = strtoul(argv[0], NULL, 0);
5713 data.dptr = (uint8_t *)discard_const(argv[1]);
5714 data.dsize= strlen(argv[1]);
5716 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5718 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5726 handler for msglisten
5728 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
5729 TDB_DATA data, void *private_data)
5733 printf("Message received: ");
5734 for (i=0;i<data.dsize;i++) {
5735 printf("%c", data.dptr[i]);
5741 listen for messages on a messageport
5743 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5749 /* register a message port and listen for messages
5751 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5752 printf("Listening for messages on srvid:%d\n", (int)srvid);
5755 event_loop_once(ctdb->ev);
5762 list all nodes in the cluster
5763 we parse the nodes file directly
5765 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5767 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5768 struct pnn_node *pnn_nodes;
5769 struct pnn_node *pnn_node;
5771 pnn_nodes = read_nodes_file(mem_ctx);
5772 if (pnn_nodes == NULL) {
5773 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5774 talloc_free(mem_ctx);
5778 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
5779 ctdb_sock_addr addr;
5780 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
5781 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
5782 talloc_free(mem_ctx);
5785 if (options.machinereadable){
5786 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5788 printf("%s\n", pnn_node->addr);
5791 talloc_free(mem_ctx);
5797 reload the nodes file on the local node
5799 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5803 struct ctdb_node_map *nodemap=NULL;
5805 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5807 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
5811 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5813 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5817 /* reload the nodes file on all remote nodes */
5818 for (i=0;i<nodemap->num;i++) {
5819 if (nodemap->nodes[i].pnn == mypnn) {
5822 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
5823 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
5824 nodemap->nodes[i].pnn);
5826 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
5830 /* reload the nodes file on the local node */
5831 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
5832 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
5834 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
5837 /* initiate a recovery */
5838 control_recover(ctdb, argc, argv);
5844 static const struct {
5846 int (*fn)(struct ctdb_context *, int, const char **);
5848 bool without_daemon; /* can be run without daemon running ? */
5851 } ctdb_commands[] = {
5852 { "version", control_version, true, true, "show version of ctdb" },
5853 { "status", control_status, true, false, "show node status" },
5854 { "uptime", control_uptime, true, false, "show node uptime" },
5855 { "ping", control_ping, true, false, "ping all nodes" },
5856 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
5857 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5858 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5859 { "listvars", control_listvars, true, false, "list tunable variables"},
5860 { "statistics", control_statistics, false, false, "show statistics" },
5861 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5862 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5863 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5864 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5865 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5866 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5867 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5868 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5869 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
5870 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
5871 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
5872 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5873 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5874 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5875 { "lvs", control_lvs, true, false, "show lvs configuration" },
5876 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
5877 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5878 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
5879 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
5880 { "getdebug", control_getdebug, true, false, "get debug level" },
5881 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
5882 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
5883 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
5884 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
5885 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
5886 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
5887 { "disable", control_disable, true, false, "disable a nodes public IP" },
5888 { "enable", control_enable, true, false, "enable a nodes public IP" },
5889 { "stop", control_stop, true, false, "stop a node" },
5890 { "continue", control_continue, true, false, "re-start a stopped node" },
5891 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
5892 { "unban", control_unban, true, false, "unban a node" },
5893 { "showban", control_showban, true, false, "show ban information"},
5894 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
5895 { "recover", control_recover, true, false, "force recovery" },
5896 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
5897 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
5898 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
5899 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
5900 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
5901 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
5902 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5903 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
5904 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
5906 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
5908 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
5909 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
5910 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
5911 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
5912 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
5913 { "vacuum", ctdb_vacuum, false, true, "vacuum the databases of empty records", "[max_records]"},
5914 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
5915 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
5916 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
5917 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
5918 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
5919 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
5920 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
5921 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
5922 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
5923 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
5924 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
5925 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
5926 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
5927 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
5928 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
5929 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
5930 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
5931 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
5932 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
5933 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
5934 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
5935 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
5936 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
5937 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
5938 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
5939 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
5940 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
5941 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
5942 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
5943 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
5944 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
5945 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
5946 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
5947 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
5948 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
5949 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
5950 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
5951 { "rebalancenode", control_rebalancenode, false, false, "release a node by allowing it to takeover ips", "<pnn>"},
5952 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
5953 { "setdbseqnum", control_setdbseqnum, false, false, "set the sequence number for a database", "<dbname|dbid> <seqnum>" },
5954 { "nodestatus", control_nodestatus, true, false, "show and return node status" },
5955 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
5956 { "reloadips", control_reloadips, false, false, "reload the public addresses file on a node" },
5957 { "ipiface", control_ipiface, true, true, "Find which interface an ip address is hsoted on", "<ip>" },
5963 static void usage(void)
5967 "Usage: ctdb [options] <control>\n" \
5969 " -n <node> choose node number, or 'all' (defaults to local node)\n"
5970 " -Y generate machinereadable output\n"
5971 " -v generate verbose output\n"
5972 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
5973 printf("Controls:\n");
5974 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5975 printf(" %-15s %-27s %s\n",
5976 ctdb_commands[i].name,
5977 ctdb_commands[i].args?ctdb_commands[i].args:"",
5978 ctdb_commands[i].msg);
5984 static void ctdb_alarm(int sig)
5986 printf("Maximum runtime exceeded - exiting\n");
5993 int main(int argc, const char *argv[])
5995 struct ctdb_context *ctdb;
5996 char *nodestring = NULL;
5997 struct poptOption popt_options[] = {
6000 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6001 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6002 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6003 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6004 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6005 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6006 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6007 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6008 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6009 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6013 const char **extra_argv;
6017 struct event_context *ev;
6018 const char *control;
6019 const char *socket_name;
6023 /* set some defaults */
6024 options.maxruntime = 0;
6025 options.timelimit = 3;
6026 options.pnn = CTDB_CURRENT_NODE;
6028 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6030 while ((opt = poptGetNextOpt(pc)) != -1) {
6033 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6034 poptBadOption(pc, 0), poptStrerror(opt)));
6039 /* setup the remaining options for the main program to use */
6040 extra_argv = poptGetArgs(pc);
6043 while (extra_argv[extra_argc]) extra_argc++;
6046 if (extra_argc < 1) {
6050 if (options.maxruntime == 0) {
6051 const char *ctdb_timeout;
6052 ctdb_timeout = getenv("CTDB_TIMEOUT");
6053 if (ctdb_timeout != NULL) {
6054 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6056 /* default timeout is 120 seconds */
6057 options.maxruntime = 120;
6061 signal(SIGALRM, ctdb_alarm);
6062 alarm(options.maxruntime);
6064 control = extra_argv[0];
6066 ev = event_context_init(NULL);
6068 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6072 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6073 if (strcmp(control, ctdb_commands[i].name) == 0) {
6078 if (i == ARRAY_SIZE(ctdb_commands)) {
6079 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6083 if (ctdb_commands[i].without_daemon == true) {
6084 if (nodestring != NULL) {
6085 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6089 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6092 /* initialise ctdb */
6093 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6096 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6100 /* initialize a libctdb connection as well */
6101 socket_name = ctdb_get_socketname(ctdb);
6102 ctdb_connection = ctdb_connect(socket_name,
6103 ctdb_log_file, stderr);
6104 if (ctdb_connection == NULL) {
6105 DEBUG(DEBUG_ERR, ("Failed to connect to daemon from libctdb\n"));
6109 /* setup the node number(s) to contact */
6110 if (!parse_nodestring(ctdb, nodestring, CTDB_CURRENT_NODE, false,
6111 &options.nodes, &options.pnn)) {
6115 if (options.pnn == CTDB_CURRENT_NODE) {
6116 options.pnn = options.nodes[0];
6119 if (ctdb_commands[i].auto_all &&
6120 ((options.pnn == CTDB_BROADCAST_ALL) ||
6121 (options.pnn == CTDB_MULTICAST))) {
6125 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6126 options.pnn = options.nodes[j];
6127 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6130 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6133 ctdb_disconnect(ctdb_connection);
6136 (void)poptFreeContext(pc);