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"
32 #include "lib/tdb_wrap/tdb_wrap.h"
33 #include "lib/util/dlinklist.h"
34 #include "lib/util/debug.h"
35 #include "lib/util/substitute.h"
36 #include "lib/util/time.h"
38 #include "ctdb_version.h"
39 #include "ctdb_private.h"
40 #include "ctdb_client.h"
42 #include "common/cmdline.h"
43 #include "common/rb_tree.h"
44 #include "common/system.h"
45 #include "common/common.h"
46 #include "common/logging.h"
48 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
49 #define ERR_NONODE 21 /* node does not exist */
50 #define ERR_DISNODE 22 /* node is disconnected */
52 static void usage(void);
59 const char *machineseparator;
62 int printemptyrecords;
69 #define LONGTIMEOUT options.timelimit*10
71 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
72 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
74 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
76 return (tv2->tv_sec - tv->tv_sec) +
77 (tv2->tv_usec - tv->tv_usec)*1.0e-6;
80 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
82 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
86 /* Like printf(3) but substitute for separator in format */
87 static int printm(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
88 static int printm(const char *format, ...)
92 size_t len = strlen(format);
93 char new_format[len+1];
95 strcpy(new_format, format);
97 if (options.machineseparator[0] != ':') {
98 all_string_sub(new_format,
99 ":", options.machineseparator, len + 1);
102 va_start(ap, format);
103 ret = vprintf(new_format, ap);
109 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
110 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
111 "Out of memory in " __location__ )); \
115 static uint32_t getpnn(struct ctdb_context *ctdb)
117 if ((options.pnn == CTDB_BROADCAST_ALL) ||
118 (options.pnn == CTDB_MULTICAST)) {
120 ("Cannot get PNN for node %u\n", options.pnn));
124 if (options.pnn == CTDB_CURRENT_NODE) {
125 return ctdb_get_pnn(ctdb);
131 static void assert_single_node_only(void)
133 if ((options.pnn == CTDB_BROADCAST_ALL) ||
134 (options.pnn == CTDB_MULTICAST)) {
136 ("This control can not be applied to multiple PNNs\n"));
141 static void assert_current_node_only(struct ctdb_context *ctdb)
143 if (options.pnn != ctdb_get_pnn(ctdb)) {
145 ("This control can only be applied to the current node\n"));
150 /* Pretty print the flags to a static buffer in human-readable format.
151 * This never returns NULL!
153 static const char *pretty_print_flags(uint32_t flags)
156 static const struct {
160 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
161 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
162 { NODE_FLAGS_BANNED, "BANNED" },
163 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
164 { NODE_FLAGS_DELETED, "DELETED" },
165 { NODE_FLAGS_STOPPED, "STOPPED" },
166 { NODE_FLAGS_INACTIVE, "INACTIVE" },
168 static char flags_str[512]; /* Big enough to contain all flag names */
171 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
172 if (flags & flag_names[j].flag) {
173 if (flags_str[0] == '\0') {
174 (void) strcpy(flags_str, flag_names[j].name);
176 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
177 (void) strncat(flags_str, flag_names[j].name,
178 sizeof(flags_str)-1);
182 if (flags_str[0] == '\0') {
183 (void) strcpy(flags_str, "OK");
189 static int h2i(char h)
191 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
192 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
196 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
199 TDB_DATA key = {NULL, 0};
202 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
207 key.dptr = talloc_size(mem_ctx, key.dsize);
209 for (i=0; i < len/2; i++) {
210 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
215 static TDB_DATA strtodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
219 if (!strncmp(str, "0x", 2)) {
220 key = hextodata(mem_ctx, str + 2, len - 2);
222 key.dptr = talloc_memdup(mem_ctx, str, len);
229 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
230 * that are disconnected or deleted. If dd_ok is true those nodes are
231 * included in the output list of nodes. If dd_ok is false, those
232 * nodes are filtered from the "all" case and cause an error if
233 * explicitly specified.
235 static bool parse_nodestring(struct ctdb_context *ctdb,
237 const char * nodestring,
238 uint32_t current_pnn,
243 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
246 struct ctdb_node_map_old *nodemap;
251 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
253 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
254 talloc_free(tmp_ctx);
258 if (nodestring != NULL) {
259 *nodes = talloc_array(mem_ctx, uint32_t, 0);
260 if (*nodes == NULL) {
266 if (strcmp(nodestring, "all") == 0) {
267 *pnn_mode = CTDB_BROADCAST_ALL;
270 for (i = 0; i < nodemap->num; i++) {
271 if ((nodemap->nodes[i].flags &
272 (NODE_FLAGS_DISCONNECTED |
273 NODE_FLAGS_DELETED)) && !dd_ok) {
276 *nodes = talloc_realloc(mem_ctx, *nodes,
278 if (*nodes == NULL) {
288 ns = talloc_strdup(tmp_ctx, nodestring);
289 tok = strtok(ns, ",");
290 while (tok != NULL) {
293 i = (uint32_t)strtoul(tok, &endptr, 0);
294 if (i == 0 && tok == endptr) {
296 ("Invalid node %s\n", tok));
297 talloc_free(tmp_ctx);
300 if (i >= nodemap->num) {
301 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
302 talloc_free(tmp_ctx);
305 if ((nodemap->nodes[i].flags &
306 (NODE_FLAGS_DISCONNECTED |
307 NODE_FLAGS_DELETED)) && !dd_ok) {
308 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
309 talloc_free(tmp_ctx);
312 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
313 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
314 talloc_free(tmp_ctx);
318 *nodes = talloc_realloc(mem_ctx, *nodes,
320 if (*nodes == NULL) {
327 tok = strtok(NULL, ",");
332 *pnn_mode = (*nodes)[0];
334 *pnn_mode = CTDB_MULTICAST;
338 /* default - no nodes specified */
339 *nodes = talloc_array(mem_ctx, uint32_t, 1);
340 if (*nodes == NULL) {
343 *pnn_mode = CTDB_CURRENT_NODE;
345 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
350 talloc_free(tmp_ctx);
354 talloc_free(tmp_ctx);
359 check if a database exists
361 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
362 uint32_t *dbid, const char **dbname, uint8_t *flags)
365 struct ctdb_dbid_map_old *dbmap=NULL;
366 bool dbid_given = false, found = false;
368 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
369 const char *name = NULL;
371 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
373 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
377 if (strncmp(dbarg, "0x", 2) == 0) {
378 id = strtoul(dbarg, NULL, 0);
382 for(i=0; i<dbmap->num; i++) {
384 if (id == dbmap->dbs[i].db_id) {
389 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, tmp_ctx, &name);
391 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].db_id));
395 if (strcmp(name, dbarg) == 0) {
396 id = dbmap->dbs[i].db_id;
403 if (found && dbid_given && dbname != NULL) {
404 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, tmp_ctx, &name);
406 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].db_id));
413 if (dbid) *dbid = id;
414 if (dbname) *dbname = talloc_strdup(ctdb, name);
415 if (flags) *flags = dbmap->dbs[i].flags;
417 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
421 talloc_free(tmp_ctx);
426 see if a process exists
428 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
436 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
437 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
441 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
443 printf("%u:%u exists\n", pnn, pid);
445 printf("%u:%u does not exist\n", pnn, pid);
451 display statistics structure
453 static void show_statistics(struct ctdb_statistics *s, int show_header)
455 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
457 const char *prefix=NULL;
459 int tmp, days, hours, minutes, seconds;
464 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
465 STATISTICS_FIELD(num_clients),
466 STATISTICS_FIELD(frozen),
467 STATISTICS_FIELD(recovering),
468 STATISTICS_FIELD(num_recoveries),
469 STATISTICS_FIELD(client_packets_sent),
470 STATISTICS_FIELD(client_packets_recv),
471 STATISTICS_FIELD(node_packets_sent),
472 STATISTICS_FIELD(node_packets_recv),
473 STATISTICS_FIELD(keepalive_packets_sent),
474 STATISTICS_FIELD(keepalive_packets_recv),
475 STATISTICS_FIELD(node.req_call),
476 STATISTICS_FIELD(node.reply_call),
477 STATISTICS_FIELD(node.req_dmaster),
478 STATISTICS_FIELD(node.reply_dmaster),
479 STATISTICS_FIELD(node.reply_error),
480 STATISTICS_FIELD(node.req_message),
481 STATISTICS_FIELD(node.req_control),
482 STATISTICS_FIELD(node.reply_control),
483 STATISTICS_FIELD(client.req_call),
484 STATISTICS_FIELD(client.req_message),
485 STATISTICS_FIELD(client.req_control),
486 STATISTICS_FIELD(timeouts.call),
487 STATISTICS_FIELD(timeouts.control),
488 STATISTICS_FIELD(timeouts.traverse),
489 STATISTICS_FIELD(locks.num_calls),
490 STATISTICS_FIELD(locks.num_current),
491 STATISTICS_FIELD(locks.num_pending),
492 STATISTICS_FIELD(locks.num_failed),
493 STATISTICS_FIELD(total_calls),
494 STATISTICS_FIELD(pending_calls),
495 STATISTICS_FIELD(childwrite_calls),
496 STATISTICS_FIELD(pending_childwrite_calls),
497 STATISTICS_FIELD(memory_used),
498 STATISTICS_FIELD(max_hop_count),
499 STATISTICS_FIELD(total_ro_delegations),
500 STATISTICS_FIELD(total_ro_revokes),
503 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
512 if (options.machinereadable){
514 printm("CTDB version:");
515 printm("Current time of statistics:");
516 printm("Statistics collected since:");
517 for (i=0;i<ARRAY_SIZE(fields);i++) {
518 printm("%s:", fields[i].name);
520 printm("num_reclock_ctdbd_latency:");
521 printm("min_reclock_ctdbd_latency:");
522 printm("avg_reclock_ctdbd_latency:");
523 printm("max_reclock_ctdbd_latency:");
525 printm("num_reclock_recd_latency:");
526 printm("min_reclock_recd_latency:");
527 printm("avg_reclock_recd_latency:");
528 printm("max_reclock_recd_latency:");
530 printm("num_call_latency:");
531 printm("min_call_latency:");
532 printm("avg_call_latency:");
533 printm("max_call_latency:");
535 printm("num_lockwait_latency:");
536 printm("min_lockwait_latency:");
537 printm("avg_lockwait_latency:");
538 printm("max_lockwait_latency:");
540 printm("num_childwrite_latency:");
541 printm("min_childwrite_latency:");
542 printm("avg_childwrite_latency:");
543 printm("max_childwrite_latency:");
546 printm("%d:", CTDB_PROTOCOL);
547 printm("%d:", (int)s->statistics_current_time.tv_sec);
548 printm("%d:", (int)s->statistics_start_time.tv_sec);
549 for (i=0;i<ARRAY_SIZE(fields);i++) {
550 printm("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
552 printm("%d:", s->reclock.ctdbd.num);
553 printm("%.6f:", s->reclock.ctdbd.min);
554 printm("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
555 printm("%.6f:", s->reclock.ctdbd.max);
557 printm("%d:", s->reclock.recd.num);
558 printm("%.6f:", s->reclock.recd.min);
559 printm("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
560 printm("%.6f:", s->reclock.recd.max);
562 printm("%d:", s->call_latency.num);
563 printm("%.6f:", s->call_latency.min);
564 printm("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
565 printm("%.6f:", s->call_latency.max);
567 printm("%d:", s->childwrite_latency.num);
568 printm("%.6f:", s->childwrite_latency.min);
569 printm("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
570 printm("%.6f:", s->childwrite_latency.max);
573 printf("CTDB version %u\n", CTDB_PROTOCOL);
574 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
575 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
577 for (i=0;i<ARRAY_SIZE(fields);i++) {
578 if (strchr(fields[i].name, '.')) {
579 preflen = strcspn(fields[i].name, ".")+1;
580 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
581 prefix = fields[i].name;
582 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
587 printf(" %*s%-22s%*s%10u\n",
589 fields[i].name+preflen,
591 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
593 printf(" hop_count_buckets:");
594 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
595 printf(" %d", s->hop_count_bucket[i]);
598 printf(" lock_buckets:");
599 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
600 printf(" %d", s->locks.buckets[i]);
603 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);
605 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);
607 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);
609 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);
610 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);
613 talloc_free(tmp_ctx);
617 display remote ctdb statistics combined from all nodes
619 static int control_statistics_all(struct ctdb_context *ctdb)
622 struct ctdb_statistics statistics;
626 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
627 CTDB_NO_MEMORY(ctdb, nodes);
629 ZERO_STRUCT(statistics);
631 for (i=0;i<num_nodes;i++) {
632 struct ctdb_statistics s1;
634 uint32_t *v1 = (uint32_t *)&s1;
635 uint32_t *v2 = (uint32_t *)&statistics;
637 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
638 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
640 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
643 for (j=0;j<num_ints;j++) {
646 statistics.max_hop_count =
647 MAX(statistics.max_hop_count, s1.max_hop_count);
648 statistics.call_latency.max =
649 MAX(statistics.call_latency.max, s1.call_latency.max);
652 printf("Gathered statistics for %u nodes\n", num_nodes);
653 show_statistics(&statistics, 1);
658 display remote ctdb statistics
660 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
663 struct ctdb_statistics statistics;
665 if (options.pnn == CTDB_BROADCAST_ALL) {
666 return control_statistics_all(ctdb);
669 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
671 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
674 show_statistics(&statistics, 1);
680 reset remote ctdb statistics
682 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
686 ret = ctdb_statistics_reset(ctdb, options.pnn);
688 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
696 display remote ctdb rolling statistics
698 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
701 struct ctdb_statistics_list_old *stats;
702 int i, num_records = -1;
704 assert_single_node_only();
707 num_records = atoi(argv[0]) - 1;
710 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
712 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
715 for (i=0;i<stats->num;i++) {
716 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
719 show_statistics(&stats->stats[i], i==0);
720 if (i == num_records) {
729 display remote ctdb db statistics
731 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
733 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
734 struct ctdb_db_statistics_old *dbstat;
744 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
748 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
750 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
751 talloc_free(tmp_ctx);
755 printf("DB Statistics: %s\n", argv[0]);
756 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
757 dbstat->db_ro_delegations);
758 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
759 dbstat->db_ro_delegations);
760 printf(" %s\n", "locks");
761 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
762 dbstat->locks.num_calls);
763 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
764 dbstat->locks.num_failed);
765 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
766 dbstat->locks.num_current);
767 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
768 dbstat->locks.num_pending);
769 printf(" %s", "hop_count_buckets:");
770 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
771 printf(" %d", dbstat->hop_count_bucket[i]);
774 printf(" %s", "lock_buckets:");
775 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
776 printf(" %d", dbstat->locks.buckets[i]);
779 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
780 "locks_latency MIN/AVG/MAX",
781 dbstat->locks.latency.min,
782 (dbstat->locks.latency.num ?
783 dbstat->locks.latency.total /dbstat->locks.latency.num :
785 dbstat->locks.latency.max,
786 dbstat->locks.latency.num);
787 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
788 "vacuum_latency MIN/AVG/MAX",
789 dbstat->vacuum.latency.min,
790 (dbstat->vacuum.latency.num ?
791 dbstat->vacuum.latency.total /dbstat->vacuum.latency.num :
793 dbstat->vacuum.latency.max,
794 dbstat->vacuum.latency.num);
796 for (i=0; i<dbstat->num_hot_keys; i++) {
797 if (dbstat->hot_keys[i].count > 0) {
801 dbstat->num_hot_keys = num_hot_keys;
803 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
804 for (i = 0; i < dbstat->num_hot_keys; i++) {
806 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
807 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
808 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
813 talloc_free(tmp_ctx);
818 display uptime of remote node
820 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
823 struct ctdb_uptime *uptime = NULL;
824 int tmp, days, hours, minutes, seconds;
826 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
828 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
832 if (options.machinereadable){
833 printm(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
834 printm(":%u:%u:%u:%lf\n",
835 (unsigned int)uptime->current_time.tv_sec,
836 (unsigned int)uptime->ctdbd_start_time.tv_sec,
837 (unsigned int)uptime->last_recovery_finished.tv_sec,
838 timeval_delta(&uptime->last_recovery_finished,
839 &uptime->last_recovery_started)
844 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
846 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
854 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
856 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
864 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
866 printf("Duration of last recovery/failover: %lf seconds\n",
867 timeval_delta(&uptime->last_recovery_finished,
868 &uptime->last_recovery_started));
874 show the PNN of the current node
876 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
880 mypnn = getpnn(ctdb);
882 printf("PNN:%d\n", mypnn);
887 static struct ctdb_node_map_old *read_nodes_file(TALLOC_CTX *mem_ctx)
889 const char *nodes_list;
891 /* read the nodes file */
892 nodes_list = getenv("CTDB_NODES");
893 if (nodes_list == NULL) {
894 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
895 getenv("CTDB_BASE"));
896 if (nodes_list == NULL) {
897 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
902 return ctdb_read_nodes_file(mem_ctx, nodes_list);
906 show the PNN of the current node
907 discover the pnn by loading the nodes file and try to bind to all
908 addresses one at a time until the ip address is found.
910 static int find_node_xpnn(void)
912 TALLOC_CTX *mem_ctx = talloc_new(NULL);
913 struct ctdb_node_map_old *node_map;
916 node_map = read_nodes_file(mem_ctx);
917 if (node_map == NULL) {
918 talloc_free(mem_ctx);
922 for (i = 0; i < node_map->num; i++) {
923 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
926 if (ctdb_sys_have_ip(&node_map->nodes[i].addr)) {
927 pnn = node_map->nodes[i].pnn;
928 talloc_free(mem_ctx);
933 printf("Failed to detect which PNN this node is\n");
934 talloc_free(mem_ctx);
938 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
942 assert_single_node_only();
944 pnn = find_node_xpnn();
949 printf("PNN:%d\n", pnn);
953 /* Helpers for ctdb status
955 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
957 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
961 if (node->flags == 0) {
962 struct ctdb_iface_list_old *ifaces;
964 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
965 tmp_ctx, &ifaces) == 0) {
966 for (j=0; j < ifaces->num; j++) {
967 if (ifaces->ifaces[j].link_state != 0) {
975 talloc_free(tmp_ctx);
980 static void control_status_header_machine(void)
982 printm(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
983 ":Inactive:PartiallyOnline:ThisNode:\n");
986 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
987 struct ctdb_node_and_flags *node)
989 printm(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
990 ctdb_addr_to_str(&node->addr),
991 !!(node->flags&NODE_FLAGS_DISCONNECTED),
992 !!(node->flags&NODE_FLAGS_BANNED),
993 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
994 !!(node->flags&NODE_FLAGS_UNHEALTHY),
995 !!(node->flags&NODE_FLAGS_STOPPED),
996 !!(node->flags&NODE_FLAGS_INACTIVE),
997 is_partially_online(ctdb, node) ? 1 : 0,
998 (node->pnn == mypnn)?'Y':'N');
1003 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
1004 struct ctdb_node_and_flags *node)
1006 printf("pnn:%d %-16s %s%s\n", node->pnn,
1007 ctdb_addr_to_str(&node->addr),
1008 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
1009 node->pnn == mypnn?" (THIS NODE)":"");
1015 display remote ctdb status
1017 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
1019 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1021 struct ctdb_vnn_map *vnnmap=NULL;
1022 struct ctdb_node_map_old *nodemap=NULL;
1023 uint32_t recmode, recmaster, mypnn;
1024 int num_deleted_nodes = 0;
1027 mypnn = getpnn(ctdb);
1029 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1031 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1032 talloc_free(tmp_ctx);
1036 if (options.machinereadable) {
1037 control_status_header_machine();
1038 for (i=0;i<nodemap->num;i++) {
1039 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1042 (void) control_status_1_machine(ctdb, mypnn,
1043 &nodemap->nodes[i]);
1045 talloc_free(tmp_ctx);
1049 for (i=0; i<nodemap->num; i++) {
1050 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1051 num_deleted_nodes++;
1054 if (num_deleted_nodes == 0) {
1055 printf("Number of nodes:%d\n", nodemap->num);
1057 printf("Number of nodes:%d (including %d deleted nodes)\n",
1058 nodemap->num, num_deleted_nodes);
1060 for(i=0;i<nodemap->num;i++){
1061 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1064 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1067 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1069 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1070 talloc_free(tmp_ctx);
1073 if (vnnmap->generation == INVALID_GENERATION) {
1074 printf("Generation:INVALID\n");
1076 printf("Generation:%d\n",vnnmap->generation);
1078 printf("Size:%d\n",vnnmap->size);
1079 for(i=0;i<vnnmap->size;i++){
1080 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1083 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1085 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1086 talloc_free(tmp_ctx);
1089 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1091 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1093 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1094 talloc_free(tmp_ctx);
1097 printf("Recovery master:%d\n",recmaster);
1099 talloc_free(tmp_ctx);
1103 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1105 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1107 struct ctdb_node_map_old *nodemap=NULL;
1109 uint32_t pnn_mode, mypnn;
1115 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1116 options.pnn, true, &nodes, &pnn_mode)) {
1120 if (options.machinereadable) {
1121 control_status_header_machine();
1122 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1123 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1126 mypnn = getpnn(ctdb);
1128 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1130 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1131 talloc_free(tmp_ctx);
1137 for (i = 0; i < talloc_array_length(nodes); i++) {
1138 if (options.machinereadable) {
1139 ret |= control_status_1_machine(ctdb, mypnn,
1140 &nodemap->nodes[nodes[i]]);
1142 ret |= control_status_1_human(ctdb, mypnn,
1143 &nodemap->nodes[nodes[i]]);
1147 talloc_free(tmp_ctx);
1151 /* talloc off the existing nodemap... */
1152 static struct ctdb_node_map_old *talloc_nodemap(struct ctdb_node_map_old *nodemap)
1154 return talloc_zero_size(nodemap,
1155 offsetof(struct ctdb_node_map_old, nodes) +
1156 nodemap->num * sizeof(struct ctdb_node_and_flags));
1159 static struct ctdb_node_map_old *
1160 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1161 struct ctdb_node_map_old *nodemap,
1162 uint32_t required_capabilities,
1166 uint32_t capabilities;
1167 struct ctdb_node_map_old *ret;
1169 ret = talloc_nodemap(nodemap);
1170 CTDB_NO_MEMORY_NULL(ctdb, ret);
1174 for (i = 0; i < nodemap->num; i++) {
1177 /* Disconnected nodes have no capabilities! */
1178 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1182 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1183 nodemap->nodes[i].pnn,
1186 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1187 nodemap->nodes[i].pnn));
1191 if (!(capabilities & required_capabilities)) {
1195 ret->nodes[ret->num] = nodemap->nodes[i];
1205 static struct ctdb_node_map_old *
1206 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1207 struct ctdb_node_map_old *nodemap,
1208 uint32_t flags_mask)
1211 struct ctdb_node_map_old *ret;
1213 ret = talloc_nodemap(nodemap);
1214 CTDB_NO_MEMORY_NULL(ctdb, ret);
1218 for (i = 0; i < nodemap->num; i++) {
1219 if (nodemap->nodes[i].flags & flags_mask) {
1223 ret->nodes[ret->num] = nodemap->nodes[i];
1231 display the list of nodes belonging to this natgw configuration
1233 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1235 static char prog[PATH_MAX+1] = "";
1241 if (!ctdb_set_helper("NAT gateway helper", prog, sizeof(prog),
1242 "CTDB_NATGW_HELPER", CTDB_HELPER_BINDIR,
1244 DEBUG(DEBUG_ERR, ("Unable to set NAT gateway helper\n"));
1248 execl(prog, prog, "natgwlist", NULL);
1251 ("Unable to run NAT gateway helper %s\n", strerror(errno)));
1256 display the status of the scripts for monitoring (or other events)
1258 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1259 enum ctdb_event type)
1261 struct ctdb_script_list_old *script_status;
1264 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1266 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1270 if (script_status == NULL) {
1271 if (!options.machinereadable) {
1272 printf("%s cycle never run\n",
1273 ctdb_eventscript_call_names[type]);
1278 if (!options.machinereadable) {
1280 for (i=0; i<script_status->num_scripts; i++) {
1281 if (script_status->scripts[i].status != -ENOEXEC) {
1285 printf("%d scripts were executed last %s cycle\n",
1287 ctdb_eventscript_call_names[type]);
1289 for (i=0; i<script_status->num_scripts; i++) {
1290 const char *status = NULL;
1292 /* The ETIME status is ignored for certain events.
1293 * In that case the status is 0, but endtime is not set.
1295 if (script_status->scripts[i].status == 0 &&
1296 timeval_is_zero(&script_status->scripts[i].finished)) {
1297 script_status->scripts[i].status = -ETIME;
1300 switch (script_status->scripts[i].status) {
1302 status = "TIMEDOUT";
1305 status = "DISABLED";
1311 if (script_status->scripts[i].status > 0)
1315 if (options.machinereadable) {
1316 printm(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1317 ctdb_eventscript_call_names[type],
1318 script_status->scripts[i].name,
1319 script_status->scripts[i].status,
1321 (long)script_status->scripts[i].start.tv_sec,
1322 (long)script_status->scripts[i].start.tv_usec,
1323 (long)script_status->scripts[i].finished.tv_sec,
1324 (long)script_status->scripts[i].finished.tv_usec,
1325 script_status->scripts[i].output);
1329 printf("%-20s Status:%s ",
1330 script_status->scripts[i].name, status);
1332 /* Some other error, eg from stat. */
1333 printf("%-20s Status:CANNOT RUN (%s)",
1334 script_status->scripts[i].name,
1335 strerror(-script_status->scripts[i].status));
1337 if (script_status->scripts[i].status >= 0) {
1338 printf("Duration:%.3lf ",
1339 timeval_delta(&script_status->scripts[i].finished,
1340 &script_status->scripts[i].start));
1342 if (script_status->scripts[i].status != -ENOEXEC) {
1344 ctime(&script_status->scripts[i].start.tv_sec));
1345 if (script_status->scripts[i].status != 0) {
1346 printf(" OUTPUT:%s\n",
1347 script_status->scripts[i].output);
1357 static int control_scriptstatus(struct ctdb_context *ctdb,
1358 int argc, const char **argv)
1361 enum ctdb_event type, min, max;
1365 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1370 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1374 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1375 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1381 if (type == CTDB_EVENT_MAX) {
1382 if (strcmp(arg, "all") == 0) {
1384 max = CTDB_EVENT_MAX;
1386 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1391 if (options.machinereadable) {
1392 printm(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1395 for (type = min; type < max; type++) {
1396 ret = control_one_scriptstatus(ctdb, type);
1406 enable an eventscript
1408 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1416 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1418 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1426 disable an eventscript
1428 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1436 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1438 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1446 display the pnn of the recovery master
1448 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1453 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1455 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1458 printf("%d\n",recmaster);
1464 add a tickle to a public address
1466 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1468 struct ctdb_connection t;
1472 assert_single_node_only();
1478 if (parse_ip_port(argv[0], &t.src) == 0) {
1479 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1482 if (parse_ip_port(argv[1], &t.dst) == 0) {
1483 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1487 data.dptr = (uint8_t *)&t;
1488 data.dsize = sizeof(t);
1490 /* tell all nodes about this tcp connection */
1491 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1492 0, data, ctdb, NULL, NULL, NULL, NULL);
1494 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1503 delete a tickle from a node
1505 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1507 struct ctdb_connection t;
1511 assert_single_node_only();
1517 if (parse_ip_port(argv[0], &t.src) == 0) {
1518 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1521 if (parse_ip_port(argv[1], &t.dst) == 0) {
1522 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1526 data.dptr = (uint8_t *)&t;
1527 data.dsize = sizeof(t);
1529 /* tell all nodes about this tcp connection */
1530 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1531 0, data, ctdb, NULL, NULL, NULL, NULL);
1533 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1542 get a list of all tickles for this pnn
1544 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1546 struct ctdb_tickle_list_old *list;
1547 ctdb_sock_addr addr;
1551 assert_single_node_only();
1558 port = atoi(argv[1]);
1561 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1562 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1566 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1568 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1572 if (options.machinereadable){
1573 printm(":source ip:port:destination ip:port:\n");
1574 for (i=0;i<list->num;i++) {
1575 if (port && port != ntohs(list->connections[i].dst.ip.sin_port)) {
1578 printm(":%s:%u", ctdb_addr_to_str(&list->connections[i].src), ntohs(list->connections[i].src.ip.sin_port));
1579 printm(":%s:%u:\n", ctdb_addr_to_str(&list->connections[i].dst), ntohs(list->connections[i].dst.ip.sin_port));
1582 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1583 printf("Num tickles:%u\n", list->num);
1584 for (i=0;i<list->num;i++) {
1585 if (port && port != ntohs(list->connections[i].dst.ip.sin_port)) {
1588 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->connections[i].src), ntohs(list->connections[i].src.ip.sin_port));
1589 printf("DST: %s:%u\n", ctdb_addr_to_str(&list->connections[i].dst), ntohs(list->connections[i].dst.ip.sin_port));
1599 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1601 struct ctdb_public_ip_list_old *ips;
1602 struct ctdb_public_ip ip;
1605 uint32_t disable_time;
1607 struct ctdb_node_map_old *nodemap=NULL;
1608 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1611 data.dptr = (uint8_t*)&disable_time;
1612 data.dsize = sizeof(disable_time);
1613 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1615 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1621 /* read the public ip list from the node */
1622 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1624 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1625 talloc_free(tmp_ctx);
1629 for (i=0;i<ips->num;i++) {
1630 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1635 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1636 pnn, ctdb_addr_to_str(addr)));
1637 talloc_free(tmp_ctx);
1644 data.dptr = (uint8_t *)&ip;
1645 data.dsize = sizeof(ip);
1647 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1649 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1650 talloc_free(tmp_ctx);
1654 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1655 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1662 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1663 talloc_free(tmp_ctx);
1667 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1669 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1670 talloc_free(tmp_ctx);
1674 /* update the recovery daemon so it now knows to expect the new
1675 node assignment for this ip.
1677 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1679 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1683 talloc_free(tmp_ctx);
1689 * scans all other nodes and returns a pnn for another node that can host this
1693 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1695 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1696 struct ctdb_public_ip_list_old *ips;
1697 struct ctdb_node_map_old *nodemap=NULL;
1701 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1703 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1704 talloc_free(tmp_ctx);
1708 for(i=0;i<nodemap->num;i++){
1709 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1712 if (nodemap->nodes[i].pnn == options.pnn) {
1716 /* read the public ip list from this node */
1717 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1719 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1723 for (j=0;j<ips->num;j++) {
1724 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1725 pnn = nodemap->nodes[i].pnn;
1726 talloc_free(tmp_ctx);
1733 talloc_free(tmp_ctx);
1737 /* If pnn is -1 then try to find a node to move IP to... */
1738 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1740 bool pnn_specified = (pnn == -1 ? false : true);
1743 while (retries < 5) {
1744 if (!pnn_specified) {
1745 pnn = find_other_host_for_public_ip(ctdb, addr);
1750 ("Trying to move public IP to node %u\n", pnn));
1753 if (move_ip(ctdb, addr, pnn) == 0) {
1766 move/failover an ip address to a specific node
1768 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1771 ctdb_sock_addr addr;
1773 assert_single_node_only();
1780 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1781 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1786 if (sscanf(argv[1], "%u", &pnn) != 1) {
1787 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1791 if (!try_moveip(ctdb, &addr, pnn)) {
1792 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1799 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1803 data.dptr = (uint8_t *)&pnn;
1804 data.dsize = sizeof(uint32_t);
1805 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1807 ("Failed to send message to force node %u to be a rebalancing target\n",
1817 rebalance a node by setting it to allow failback and triggering a
1820 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1822 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1827 assert_single_node_only();
1833 /* Determine the nodes where IPs need to be reloaded */
1834 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1835 options.pnn, true, &nodes, &pnn_mode)) {
1840 for (i = 0; i < talloc_array_length(nodes); i++) {
1841 if (!rebalance_node(ctdb, nodes[i])) {
1847 talloc_free(tmp_ctx);
1851 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1853 struct ctdb_public_ip ip;
1856 uint32_t disable_time;
1858 struct ctdb_node_map_old *nodemap=NULL;
1859 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1862 data.dptr = (uint8_t*)&disable_time;
1863 data.dsize = sizeof(disable_time);
1864 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1866 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1873 data.dptr = (uint8_t *)&ip;
1874 data.dsize = sizeof(ip);
1876 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1878 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1879 talloc_free(tmp_ctx);
1883 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1884 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1891 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1892 talloc_free(tmp_ctx);
1896 talloc_free(tmp_ctx);
1901 release an ip form all nodes and have it re-assigned by recd
1903 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1905 ctdb_sock_addr addr;
1907 assert_single_node_only();
1914 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1915 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1919 if (rebalance_ip(ctdb, &addr) != 0) {
1920 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
1927 static int getips_store_callback(void *param, void *data)
1929 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1930 struct ctdb_public_ip_list_old *ips = param;
1934 ips->ips[i].pnn = node_ip->pnn;
1935 ips->ips[i].addr = node_ip->addr;
1939 static int getips_count_callback(void *param, void *data)
1941 uint32_t *count = param;
1948 static uint32_t *ip_key(ctdb_sock_addr *ip)
1950 static uint32_t key[IP_KEYLEN];
1952 bzero(key, sizeof(key));
1954 switch (ip->sa.sa_family) {
1956 key[0] = ip->ip.sin_addr.s_addr;
1959 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1967 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1974 static void *add_ip_callback(void *parm, void *data)
1980 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_public_ip_list_old **ips)
1982 struct ctdb_public_ip_list_old *tmp_ips;
1983 struct ctdb_node_map_old *nodemap=NULL;
1984 trbt_tree_t *ip_tree;
1988 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1990 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1994 ip_tree = trbt_create(tmp_ctx, 0);
1996 for(i=0;i<nodemap->num;i++){
1997 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2000 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2004 /* read the public ip list from this node */
2005 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2007 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2011 for (j=0; j<tmp_ips->num;j++) {
2012 struct ctdb_public_ip *node_ip;
2014 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2015 node_ip->pnn = tmp_ips->ips[j].pnn;
2016 node_ip->addr = tmp_ips->ips[j].addr;
2018 trbt_insertarray32_callback(ip_tree,
2019 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2023 talloc_free(tmp_ips);
2028 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2030 len = offsetof(struct ctdb_public_ip_list_old, ips) +
2031 count*sizeof(struct ctdb_public_ip);
2032 tmp_ips = talloc_zero_size(tmp_ctx, len);
2033 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2041 static void ctdb_every_second(struct tevent_context *ev,
2042 struct tevent_timer *te,
2043 struct timeval t, void *p)
2045 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2047 tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
2048 ctdb_every_second, ctdb);
2051 struct srvid_reply_handler_data {
2055 const char *srvid_str;
2058 static void srvid_broadcast_reply_handler(uint64_t srvid, TDB_DATA data,
2061 struct srvid_reply_handler_data *d =
2062 (struct srvid_reply_handler_data *)private_data;
2066 if (data.dsize != sizeof(ret)) {
2067 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2071 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2072 ret = *(int32_t *)data.dptr;
2075 ("%s failed with result %d\n", d->srvid_str, ret));
2079 if (!d->wait_for_all) {
2084 /* Wait for all replies */
2086 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2087 if (d->nodes[i] == ret) {
2089 ("%s reply received from node %u\n",
2090 d->srvid_str, ret));
2093 if (d->nodes[i] != -1) {
2094 /* Found a node that hasn't yet replied */
2100 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2101 * or replies from all connected nodes. arg is the data argument to
2102 * pass in the srvid_request structure - pass 0 if this isn't needed.
2104 static int srvid_broadcast(struct ctdb_context *ctdb,
2105 uint64_t srvid, uint32_t *arg,
2106 const char *srvid_str, bool wait_for_all)
2111 uint64_t reply_srvid;
2112 struct ctdb_srvid_message request;
2113 struct ctdb_disable_message request_data;
2114 struct srvid_reply_handler_data reply_data;
2117 ZERO_STRUCT(request);
2119 /* Time ticks to enable timeouts to be processed */
2120 tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
2121 ctdb_every_second, ctdb);
2123 pnn = ctdb_get_pnn(ctdb);
2124 reply_srvid = getpid();
2128 request.srvid = reply_srvid;
2130 data.dptr = (uint8_t *)&request;
2131 data.dsize = sizeof(request);
2133 request_data.pnn = pnn;
2134 request_data.srvid = reply_srvid;
2135 request_data.timeout = *arg;
2137 data.dptr = (uint8_t *)&request_data;
2138 data.dsize = sizeof(request_data);
2141 /* Register message port for reply from recovery master */
2142 ctdb_client_set_message_handler(ctdb, reply_srvid,
2143 srvid_broadcast_reply_handler,
2146 reply_data.wait_for_all = wait_for_all;
2147 reply_data.nodes = NULL;
2148 reply_data.srvid_str = srvid_str;
2151 reply_data.done = false;
2154 struct ctdb_node_map_old *nodemap;
2156 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2157 CTDB_CURRENT_NODE, ctdb, &nodemap);
2160 ("Unable to get nodemap from current node, try again\n"));
2165 if (reply_data.nodes != NULL) {
2166 talloc_free(reply_data.nodes);
2168 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2171 talloc_free(nodemap);
2174 /* Send to all connected nodes. Only recmaster replies */
2175 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2178 /* This can only happen if the socket is closed and
2179 * there's no way to recover from that, so don't try
2183 ("Failed to send %s request to connected nodes\n",
2188 tv = timeval_current();
2189 /* This loop terminates the reply is received */
2190 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2191 tevent_loop_once(ctdb->ev);
2194 if (!reply_data.done) {
2196 ("Still waiting for confirmation of %s\n", srvid_str));
2201 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2203 talloc_free(reply_data.nodes);
2208 static int ipreallocate(struct ctdb_context *ctdb)
2210 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2211 "IP reallocation", false);
2215 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2217 return ipreallocate(ctdb);
2221 add a public ip address to a node
2223 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2226 int len, retries = 0;
2228 ctdb_sock_addr addr;
2229 struct ctdb_addr_info_old *pub;
2230 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2231 struct ctdb_public_ip_list_old *ips;
2235 talloc_free(tmp_ctx);
2239 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2240 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2241 talloc_free(tmp_ctx);
2245 /* read the public ip list from the node */
2246 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2248 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2249 talloc_free(tmp_ctx);
2252 for (i=0;i<ips->num;i++) {
2253 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2254 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2261 /* Dont timeout. This command waits for an ip reallocation
2262 which sometimes can take wuite a while if there has
2263 been a recent recovery
2267 len = offsetof(struct ctdb_addr_info_old, iface) + strlen(argv[1]) + 1;
2268 pub = talloc_size(tmp_ctx, len);
2269 CTDB_NO_MEMORY(ctdb, pub);
2273 pub->len = strlen(argv[1])+1;
2274 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2277 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2279 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2283 } while (retries < 5 && ret != 0);
2285 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2286 talloc_free(tmp_ctx);
2290 if (rebalance_node(ctdb, options.pnn) != 0) {
2291 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2295 talloc_free(tmp_ctx);
2300 add a public ip address to a node
2302 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2304 ctdb_sock_addr addr;
2311 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2312 printf("Badly formed ip : %s\n", argv[0]);
2316 iface = ctdb_sys_find_ifname(&addr);
2317 if (iface == NULL) {
2318 printf("Failed to get interface name for ip: %s", argv[0]);
2322 printf("IP on interface %s\n", iface);
2329 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2331 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2333 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2334 struct ctdb_node_map_old *nodemap=NULL;
2335 struct ctdb_public_ip_list_old *ips;
2338 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2340 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2344 /* remove it from the nodes that are not hosting the ip currently */
2345 for(i=0;i<nodemap->num;i++){
2346 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2349 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2351 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2355 for (j=0;j<ips->num;j++) {
2356 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2364 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2368 options.pnn = nodemap->nodes[i].pnn;
2369 control_delip(ctdb, argc, argv);
2373 /* remove it from every node (also the one hosting it) */
2374 for(i=0;i<nodemap->num;i++){
2375 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2378 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2380 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2384 for (j=0;j<ips->num;j++) {
2385 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2393 options.pnn = nodemap->nodes[i].pnn;
2394 control_delip(ctdb, argc, argv);
2397 talloc_free(tmp_ctx);
2402 delete a public ip address from a node
2404 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2407 ctdb_sock_addr addr;
2408 struct ctdb_addr_info_old pub;
2409 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2410 struct ctdb_public_ip_list_old *ips;
2413 talloc_free(tmp_ctx);
2417 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2418 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2422 if (options.pnn == CTDB_BROADCAST_ALL) {
2423 return control_delip_all(ctdb, argc, argv, &addr);
2430 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2432 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2433 talloc_free(tmp_ctx);
2437 for (i=0;i<ips->num;i++) {
2438 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2444 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2445 ctdb_addr_to_str(&addr)));
2446 talloc_free(tmp_ctx);
2450 /* This is an optimisation. If this node is hosting the IP
2451 * then try to move it somewhere else without invoking a full
2452 * takeover run. We don't care if this doesn't work!
2454 if (ips->ips[i].pnn == options.pnn) {
2455 (void) try_moveip(ctdb, &addr, -1);
2458 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2460 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2461 talloc_free(tmp_ctx);
2465 talloc_free(tmp_ctx);
2469 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2470 int argc, const char **argv)
2472 struct ctdb_connection *killtcp;
2473 int max_entries, current, i;
2474 struct timeval timeout;
2475 char line[128], src[128], dst[128];
2478 struct client_async_data *async_data;
2479 struct ctdb_client_control_state *state;
2489 while (!feof(stdin)) {
2490 if (fgets(line, sizeof(line), stdin) == NULL) {
2494 /* Silently skip empty lines */
2495 if (line[0] == '\n') {
2499 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2500 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2502 talloc_free(killtcp);
2506 if (current >= max_entries) {
2507 max_entries += 1024;
2508 killtcp = talloc_realloc(ctdb, killtcp,
2509 struct ctdb_connection,
2511 CTDB_NO_MEMORY(ctdb, killtcp);
2514 if (!parse_ip_port(src, &killtcp[current].src)) {
2515 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2517 talloc_free(killtcp);
2521 if (!parse_ip_port(dst, &killtcp[current].dst)) {
2522 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2524 talloc_free(killtcp);
2531 async_data = talloc_zero(ctdb, struct client_async_data);
2532 if (async_data == NULL) {
2533 talloc_free(killtcp);
2537 for (i = 0; i < current; i++) {
2539 data.dsize = sizeof(struct ctdb_connection);
2540 data.dptr = (unsigned char *)&killtcp[i];
2542 timeout = TIMELIMIT();
2543 state = ctdb_control_send(ctdb, options.pnn, 0,
2544 CTDB_CONTROL_KILL_TCP, 0, data,
2545 async_data, &timeout, NULL);
2547 if (state == NULL) {
2549 ("Failed to call async killtcp control to node %u\n",
2551 talloc_free(killtcp);
2555 ctdb_client_async_add(async_data, state);
2558 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2559 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2560 talloc_free(killtcp);
2564 talloc_free(killtcp);
2570 kill a tcp connection
2572 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2575 struct ctdb_connection killtcp;
2577 assert_single_node_only();
2580 return kill_tcp_from_file(ctdb, argc, argv);
2587 if (!parse_ip_port(argv[0], &killtcp.src)) {
2588 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2592 if (!parse_ip_port(argv[1], &killtcp.dst)) {
2593 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2597 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2599 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2610 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2613 ctdb_sock_addr addr;
2615 assert_single_node_only();
2621 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2622 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2626 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2628 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2636 register a server id
2638 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2641 struct ctdb_client_id server_id;
2647 server_id.pnn = strtoul(argv[0], NULL, 0);
2648 server_id.type = strtoul(argv[1], NULL, 0);
2649 server_id.server_id = strtoul(argv[2], NULL, 0);
2651 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2653 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2656 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2662 unregister a server id
2664 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2667 struct ctdb_client_id server_id;
2673 server_id.pnn = strtoul(argv[0], NULL, 0);
2674 server_id.type = strtoul(argv[1], NULL, 0);
2675 server_id.server_id = strtoul(argv[2], NULL, 0);
2677 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2679 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2686 check if a server id exists
2688 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2690 uint32_t status = 0;
2692 struct ctdb_client_id server_id;
2698 server_id.pnn = strtoul(argv[0], NULL, 0);
2699 server_id.type = strtoul(argv[1], NULL, 0);
2700 server_id.server_id = strtoul(argv[2], NULL, 0);
2702 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2704 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2709 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2711 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2717 get a list of all server ids that are registered on a node
2719 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2722 struct ctdb_client_id_list_old *server_ids;
2724 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2726 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2730 for (i=0; i<server_ids->num; i++) {
2731 printf("Server id %d:%d:%d\n",
2732 server_ids->server_ids[i].pnn,
2733 server_ids->server_ids[i].type,
2734 server_ids->server_ids[i].server_id);
2741 check if a server id exists
2743 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2745 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2751 talloc_free(tmp_ctx);
2755 ids = talloc_array(tmp_ctx, uint64_t, argc);
2756 result = talloc_array(tmp_ctx, uint8_t, argc);
2758 for (i = 0; i < argc; i++) {
2759 ids[i] = strtoull(argv[i], NULL, 0);
2762 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2763 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2765 talloc_free(tmp_ctx);
2769 for (i=0; i < argc; i++) {
2770 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2771 result[i] ? "exists" : "does not exist");
2774 talloc_free(tmp_ctx);
2779 send a tcp tickle ack
2781 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2784 ctdb_sock_addr src, dst;
2790 if (!parse_ip_port(argv[0], &src)) {
2791 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2795 if (!parse_ip_port(argv[1], &dst)) {
2796 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2800 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2804 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2811 display public ip status
2813 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2816 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2817 struct ctdb_public_ip_list_old *ips;
2819 if (argc == 1 && strcmp(argv[0], "all") == 0) {
2820 options.pnn = CTDB_BROADCAST_ALL;
2823 if (options.pnn == CTDB_BROADCAST_ALL) {
2824 /* read the list of public ips from all nodes */
2825 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2827 /* read the public ip list from this node */
2828 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2831 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2832 talloc_free(tmp_ctx);
2836 if (options.machinereadable){
2837 printm(":Public IP:Node:");
2838 if (options.verbose){
2839 printm("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2843 if (options.pnn == CTDB_BROADCAST_ALL) {
2844 printf("Public IPs on ALL nodes\n");
2846 printf("Public IPs on node %u\n", options.pnn);
2850 for (i=1;i<=ips->num;i++) {
2851 struct ctdb_public_ip_info_old *info = NULL;
2853 char *aciface = NULL;
2854 char *avifaces = NULL;
2855 char *cifaces = NULL;
2857 if (options.pnn == CTDB_BROADCAST_ALL) {
2858 pnn = ips->ips[ips->num-i].pnn;
2864 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2865 &ips->ips[ips->num-i].addr, &info);
2872 for (j=0; j < info->num; j++) {
2873 if (cifaces == NULL) {
2874 cifaces = talloc_strdup(info,
2875 info->ifaces[j].name);
2877 cifaces = talloc_asprintf_append(cifaces,
2879 info->ifaces[j].name);
2882 if (info->active_idx == j) {
2883 aciface = info->ifaces[j].name;
2886 if (info->ifaces[j].link_state == 0) {
2890 if (avifaces == NULL) {
2891 avifaces = talloc_strdup(info, info->ifaces[j].name);
2893 avifaces = talloc_asprintf_append(avifaces,
2895 info->ifaces[j].name);
2900 if (options.machinereadable){
2902 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2903 ips->ips[ips->num-i].pnn);
2904 if (options.verbose){
2907 avifaces?avifaces:"",
2908 cifaces?cifaces:"");
2912 if (options.verbose) {
2913 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2914 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2915 ips->ips[ips->num-i].pnn,
2917 avifaces?avifaces:"",
2918 cifaces?cifaces:"");
2921 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2922 ips->ips[ips->num-i].pnn);
2928 talloc_free(tmp_ctx);
2935 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2938 ctdb_sock_addr addr;
2939 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2940 struct ctdb_public_ip_info_old *info;
2943 talloc_free(tmp_ctx);
2947 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2948 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2952 /* read the public ip info from this node */
2953 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2954 tmp_ctx, &addr, &info);
2956 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2957 argv[0], options.pnn));
2958 talloc_free(tmp_ctx);
2962 printf("Public IP[%s] info on node %u\n",
2963 ctdb_addr_to_str(&info->ip.addr),
2966 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2967 ctdb_addr_to_str(&info->ip.addr),
2968 info->ip.pnn, info->num);
2970 for (i=0; i<info->num; i++) {
2971 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2973 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2974 i+1, info->ifaces[i].name,
2975 info->ifaces[i].link_state?"up":"down",
2976 (unsigned int)info->ifaces[i].references,
2977 (i==info->active_idx)?" (active)":"");
2980 talloc_free(tmp_ctx);
2985 display interfaces status
2987 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2989 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2991 struct ctdb_iface_list_old *ifaces;
2994 /* read the public ip list from this node */
2995 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
2997 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2999 talloc_free(tmp_ctx);
3003 if (options.machinereadable){
3004 printm(":Name:LinkStatus:References:\n");
3006 printf("Interfaces on node %u\n", options.pnn);
3009 for (i=0; i<ifaces->num; i++) {
3010 if (options.machinereadable){
3011 printm(":%s:%s:%u:\n",
3012 ifaces->ifaces[i].name,
3013 ifaces->ifaces[i].link_state?"1":"0",
3014 (unsigned int)ifaces->ifaces[i].references);
3016 printf("name:%s link:%s references:%u\n",
3017 ifaces->ifaces[i].name,
3018 ifaces->ifaces[i].link_state?"up":"down",
3019 (unsigned int)ifaces->ifaces[i].references);
3023 talloc_free(tmp_ctx);
3029 set link status of an interface
3031 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3034 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3035 struct ctdb_iface info;
3043 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3044 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3046 talloc_free(tmp_ctx);
3049 strcpy(info.name, argv[0]);
3051 if (strcmp(argv[1], "up") == 0) {
3052 info.link_state = 1;
3053 } else if (strcmp(argv[1], "down") == 0) {
3054 info.link_state = 0;
3056 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3058 talloc_free(tmp_ctx);
3062 /* read the public ip list from this node */
3063 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3066 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3067 argv[0], options.pnn));
3068 talloc_free(tmp_ctx);
3072 talloc_free(tmp_ctx);
3077 display pid of a ctdb daemon
3079 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3084 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3086 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3089 printf("Pid:%d\n", pid);
3094 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3096 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3098 update_flags_handler_t handler,
3103 struct ctdb_node_map_old *nodemap = NULL;
3107 /* Check if the node is already in the desired state */
3108 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3110 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3113 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3114 if (set_flag == flag_is_set) {
3115 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3116 (set_flag ? "already" : "not"), desc));
3121 if (!handler(ctdb, data)) {
3122 DEBUG(DEBUG_WARNING,
3123 ("Failed to send control to set state %s on node %u, try again\n",
3124 desc, options.pnn));
3129 /* Read the nodemap and verify the change took effect.
3130 * Even if the above control/hanlder timed out then it
3131 * could still have worked!
3133 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3136 DEBUG(DEBUG_WARNING,
3137 ("Unable to get nodemap from local node, try again\n"));
3139 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3140 } while (nodemap == NULL || (set_flag != flag_is_set));
3142 return ipreallocate(ctdb);
3145 /* Administratively disable a node */
3146 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3150 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3151 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3155 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3157 return update_flags_and_ipreallocate(ctdb, NULL,
3158 update_flags_disabled,
3159 NODE_FLAGS_PERMANENTLY_DISABLED,
3161 true /* set_flag*/);
3164 /* Administratively re-enable a node */
3165 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3169 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3170 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3174 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3176 return update_flags_and_ipreallocate(ctdb, NULL,
3177 update_flags_not_disabled,
3178 NODE_FLAGS_PERMANENTLY_DISABLED,
3180 false /* set_flag*/);
3184 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3188 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3193 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3195 return update_flags_and_ipreallocate(ctdb, NULL,
3196 update_flags_stopped,
3199 true /* set_flag*/);
3202 /* Continue a stopped node */
3203 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3207 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3212 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3214 return update_flags_and_ipreallocate(ctdb, NULL,
3215 update_flags_not_stopped,
3218 false /* set_flag */);
3221 static uint32_t get_generation(struct ctdb_context *ctdb)
3223 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3224 struct ctdb_vnn_map *vnnmap=NULL;
3226 uint32_t generation;
3228 /* wait until the recmaster is not in recovery mode */
3230 uint32_t recmode, recmaster;
3232 if (vnnmap != NULL) {
3233 talloc_free(vnnmap);
3237 /* get the recmaster */
3238 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3240 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3241 talloc_free(tmp_ctx);
3245 /* get recovery mode */
3246 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3248 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3249 talloc_free(tmp_ctx);
3253 /* get the current generation number */
3254 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3256 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3257 talloc_free(tmp_ctx);
3261 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3262 generation = vnnmap->generation;
3263 talloc_free(tmp_ctx);
3271 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3273 struct ctdb_ban_state *bantime = (struct ctdb_ban_state *)data;
3276 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3281 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3283 struct ctdb_ban_state bantime;
3289 bantime.pnn = options.pnn;
3290 bantime.time = strtoul(argv[0], NULL, 0);
3292 if (bantime.time == 0) {
3293 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3297 return update_flags_and_ipreallocate(ctdb, &bantime,
3298 update_state_banned,
3301 true /* set_flag*/);
3306 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3308 struct ctdb_ban_state bantime;
3310 bantime.pnn = options.pnn;
3313 return update_flags_and_ipreallocate(ctdb, &bantime,
3314 update_state_banned,
3317 false /* set_flag*/);
3321 show ban information for a node
3323 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3326 struct ctdb_node_map_old *nodemap=NULL;
3327 struct ctdb_ban_state *bantime;
3329 /* verify the node exists */
3330 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3332 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3336 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3338 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3342 if (bantime->time == 0) {
3343 printf("Node %u is not banned\n", bantime->pnn);
3345 printf("Node %u is banned, %d seconds remaining\n",
3346 bantime->pnn, bantime->time);
3355 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3359 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3361 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3371 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3374 uint32_t generation, next_generation;
3376 /* record the current generation number */
3377 generation = get_generation(ctdb);
3379 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3381 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3385 /* wait until we are in a new generation */
3387 next_generation = get_generation(ctdb);
3388 if (next_generation != generation) {
3399 display monitoring mode of a remote node
3401 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3406 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3408 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3411 if (!options.machinereadable){
3412 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3415 printm(":%d:\n",monmode);
3422 display capabilities of a remote node
3424 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3426 uint32_t capabilities;
3429 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3431 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3435 if (!options.machinereadable){
3436 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3437 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3438 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3440 printm(":RECMASTER:LMASTER:LVS:\n");
3441 printm(":%d:%d:%d:\n",
3442 !!(capabilities&CTDB_CAP_RECMASTER),
3443 !!(capabilities&CTDB_CAP_LMASTER),
3444 !!(capabilities&CTDB_CAP_LVS));
3450 display lvs configuration
3453 static uint32_t lvs_exclude_flags[] = {
3454 /* Look for a nice healthy node */
3455 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3456 /* If not found, an UNHEALTHY node will do */
3457 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3461 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3463 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3464 struct ctdb_node_map_old *orig_nodemap=NULL;
3465 struct ctdb_node_map_old *nodemap;
3468 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3469 tmp_ctx, &orig_nodemap);
3471 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3472 talloc_free(tmp_ctx);
3476 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3477 CTDB_CAP_LVS, false);
3478 if (nodemap == NULL) {
3486 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3487 struct ctdb_node_map_old *t =
3488 filter_nodemap_by_flags(ctdb, nodemap,
3489 lvs_exclude_flags[i]);
3496 /* At least 1 node without excluded flags */
3498 for (j = 0; j < t->num; j++) {
3499 printf("%d:%s\n", t->nodes[j].pnn,
3500 ctdb_addr_to_str(&t->nodes[j].addr));
3507 talloc_free(tmp_ctx);
3512 display who is the lvs master
3514 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3516 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3517 struct ctdb_node_map_old *nodemap=NULL;
3520 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3523 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3524 talloc_free(tmp_ctx);
3528 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3529 struct ctdb_node_map_old *t =
3530 filter_nodemap_by_flags(ctdb, nodemap,
3531 lvs_exclude_flags[i]);
3538 struct ctdb_node_map_old *n;
3539 n = filter_nodemap_by_capabilities(ctdb,
3550 if (options.machinereadable) {
3551 printm("%d\n", n->nodes[0].pnn);
3553 printf("Node %d is LVS master\n", n->nodes[0].pnn);
3561 printf("There is no LVS master\n");
3564 talloc_free(tmp_ctx);
3569 disable monitoring on a node
3571 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3576 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3578 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3581 printf("Monitoring mode:%s\n","DISABLED");
3587 enable monitoring on a node
3589 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3594 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3596 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3599 printf("Monitoring mode:%s\n","ACTIVE");
3605 display remote list of keys/data for a db
3607 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3609 const char *db_name;
3610 struct ctdb_db_context *ctdb_db;
3612 struct ctdb_dump_db_context c;
3619 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3623 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3624 if (ctdb_db == NULL) {
3625 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3629 if (options.printlmaster) {
3630 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3631 ctdb, &ctdb->vnn_map);
3633 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3642 c.printemptyrecords = (bool)options.printemptyrecords;
3643 c.printdatasize = (bool)options.printdatasize;
3644 c.printlmaster = (bool)options.printlmaster;
3645 c.printhash = (bool)options.printhash;
3646 c.printrecordflags = (bool)options.printrecordflags;
3648 /* traverse and dump the cluster tdb */
3649 ret = ctdb_dump_db(ctdb_db, &c);
3651 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3652 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3653 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3657 talloc_free(ctdb_db);
3659 printf("Dumped %d records\n", ret);
3663 struct cattdb_data {
3664 struct ctdb_context *ctdb;
3668 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3670 struct cattdb_data *d = private_data;
3671 struct ctdb_dump_db_context c;
3678 c.printemptyrecords = (bool)options.printemptyrecords;
3679 c.printdatasize = (bool)options.printdatasize;
3680 c.printlmaster = false;
3681 c.printhash = (bool)options.printhash;
3682 c.printrecordflags = true;
3684 return ctdb_dumpdb_record(key, data, &c);
3688 cat the local tdb database using same format as catdb
3690 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3692 const char *db_name;
3693 struct ctdb_db_context *ctdb_db;
3694 struct cattdb_data d;
3701 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3705 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3706 if (ctdb_db == NULL) {
3707 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3711 /* traverse the local tdb */
3714 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3715 printf("Failed to cattdb data\n");
3718 talloc_free(ctdb_db);
3720 printf("Dumped %d records\n", d.count);
3725 display the content of a database key
3727 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3729 const char *db_name;
3730 struct ctdb_db_context *ctdb_db;
3731 struct ctdb_record_handle *h;
3732 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3740 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3744 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3745 if (ctdb_db == NULL) {
3746 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3750 key.dptr = discard_const(argv[1]);
3751 key.dsize = strlen((char *)key.dptr);
3753 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3755 printf("Failed to fetch record '%s' on node %d\n",
3756 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3757 talloc_free(tmp_ctx);
3761 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3763 talloc_free(tmp_ctx);
3764 talloc_free(ctdb_db);
3769 display the content of a database key
3771 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3773 const char *db_name;
3774 struct ctdb_db_context *ctdb_db;
3775 struct ctdb_record_handle *h;
3776 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3784 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3788 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3789 if (ctdb_db == NULL) {
3790 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3794 key.dptr = discard_const(argv[1]);
3795 key.dsize = strlen((char *)key.dptr);
3797 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3799 printf("Failed to fetch record '%s' on node %d\n",
3800 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3801 talloc_free(tmp_ctx);
3805 data.dptr = discard_const(argv[2]);
3806 data.dsize = strlen((char *)data.dptr);
3808 if (ctdb_record_store(h, data) != 0) {
3809 printf("Failed to store record\n");
3813 talloc_free(tmp_ctx);
3814 talloc_free(ctdb_db);
3819 fetch a record from a persistent database
3821 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3823 const char *db_name;
3824 struct ctdb_db_context *ctdb_db;
3825 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3826 struct ctdb_transaction_handle *h;
3833 talloc_free(tmp_ctx);
3837 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3838 talloc_free(tmp_ctx);
3842 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3844 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3845 talloc_free(tmp_ctx);
3849 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3850 if (ctdb_db == NULL) {
3851 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3852 talloc_free(tmp_ctx);
3856 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3858 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3859 talloc_free(tmp_ctx);
3863 key.dptr = discard_const(argv[1]);
3864 key.dsize = strlen(argv[1]);
3865 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3867 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3868 talloc_free(tmp_ctx);
3872 if (data.dsize == 0 || data.dptr == NULL) {
3873 DEBUG(DEBUG_ERR,("Record is empty\n"));
3874 talloc_free(tmp_ctx);
3879 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3881 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3882 talloc_free(tmp_ctx);
3885 sys_write(fd, data.dptr, data.dsize);
3888 sys_write(1, data.dptr, data.dsize);
3891 /* abort the transaction */
3895 talloc_free(tmp_ctx);
3900 fetch a record from a tdb-file
3902 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3904 const char *tdb_file;
3907 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3916 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3918 printf("Failed to open TDB file %s\n", tdb_file);
3922 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
3923 if (key.dptr == NULL) {
3924 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3928 data = tdb_fetch(tdb, key);
3929 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3930 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3938 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3940 printf("Failed to open output file %s\n", argv[2]);
3943 if (options.verbose){
3944 sys_write(fd, data.dptr, data.dsize);
3946 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3950 if (options.verbose){
3951 sys_write(1, data.dptr, data.dsize);
3953 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3957 talloc_free(tmp_ctx);
3962 store a record and header to a tdb-file
3964 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
3966 const char *tdb_file;
3968 TDB_DATA key, value, data;
3969 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3970 struct ctdb_ltdb_header header;
3978 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
3980 printf("Failed to open TDB file %s\n", tdb_file);
3984 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
3985 if (key.dptr == NULL) {
3986 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3990 value = strtodata(tmp_ctx, argv[2], strlen(argv[2]));
3991 if (value.dptr == NULL) {
3992 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
3996 ZERO_STRUCT(header);
3998 header.rsn = atoll(argv[3]);
4001 header.dmaster = atoi(argv[4]);
4004 header.flags = atoi(argv[5]);
4007 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4008 data.dptr = talloc_size(tmp_ctx, data.dsize);
4009 if (data.dptr == NULL) {
4010 printf("Failed to allocate header+value\n");
4014 *(struct ctdb_ltdb_header *)data.dptr = header;
4015 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4017 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4018 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4025 talloc_free(tmp_ctx);
4030 write a record to a persistent database
4032 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4034 const char *db_name;
4035 struct ctdb_db_context *ctdb_db;
4036 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4037 struct ctdb_transaction_handle *h;
4043 talloc_free(tmp_ctx);
4047 fd = open(argv[2], O_RDONLY);
4049 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4050 talloc_free(tmp_ctx);
4054 ret = fstat(fd, &st);
4056 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4058 talloc_free(tmp_ctx);
4062 if (!S_ISREG(st.st_mode)) {
4063 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4065 talloc_free(tmp_ctx);
4069 data.dsize = st.st_size;
4070 if (data.dsize == 0) {
4073 data.dptr = talloc_size(tmp_ctx, data.dsize);
4074 if (data.dptr == NULL) {
4075 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4077 talloc_free(tmp_ctx);
4080 ret = sys_read(fd, data.dptr, data.dsize);
4081 if (ret != data.dsize) {
4082 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4084 talloc_free(tmp_ctx);
4093 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4094 if (ctdb_db == NULL) {
4095 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4096 talloc_free(tmp_ctx);
4100 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4102 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4103 talloc_free(tmp_ctx);
4107 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
4108 if (key.dptr == NULL) {
4109 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4113 ret = ctdb_transaction_store(h, key, data);
4115 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4116 talloc_free(tmp_ctx);
4120 ret = ctdb_transaction_commit(h);
4122 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4123 talloc_free(tmp_ctx);
4128 talloc_free(tmp_ctx);
4133 * delete a record from a persistent database
4135 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4137 const char *db_name;
4138 struct ctdb_db_context *ctdb_db;
4139 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4140 struct ctdb_transaction_handle *h;
4147 talloc_free(tmp_ctx);
4151 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4152 talloc_free(tmp_ctx);
4156 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4158 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4159 talloc_free(tmp_ctx);
4163 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4164 if (ctdb_db == NULL) {
4165 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4166 talloc_free(tmp_ctx);
4170 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4172 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4173 talloc_free(tmp_ctx);
4177 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
4178 if (key.dptr == NULL) {
4179 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4183 ret = ctdb_transaction_store(h, key, tdb_null);
4185 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4186 talloc_free(tmp_ctx);
4190 ret = ctdb_transaction_commit(h);
4192 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4193 talloc_free(tmp_ctx);
4197 talloc_free(tmp_ctx);
4201 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4206 const char *ret; /* Next byte after successfully parsed value */
4208 /* Error, unless someone says otherwise */
4210 /* Indicates no value to parse */
4213 /* Skip whitespace */
4214 n = strspn(s, " \t");
4218 /* Quoted ASCII string - no wide characters! */
4220 n = strcspn(t, "\"");
4223 *data = strtodata(mem_ctx, t, n);
4224 CTDB_NOMEM_ABORT(data->dptr);
4228 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4231 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4237 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4238 TDB_DATA *key, TDB_DATA *value)
4240 char line [1024]; /* FIXME: make this more flexible? */
4244 ptr = fgets(line, sizeof(line), file);
4251 t = ptrans_parse_string(mem_ctx, line, key);
4252 if (t == NULL || key->dptr == NULL) {
4253 /* Line Ignored but not EOF */
4258 t = ptrans_parse_string(mem_ctx, t, value);
4260 /* Line Ignored but not EOF */
4261 talloc_free(key->dptr);
4270 * Update a persistent database as per file/stdin
4272 static int control_ptrans(struct ctdb_context *ctdb,
4273 int argc, const char **argv)
4275 const char *db_name;
4276 struct ctdb_db_context *ctdb_db;
4277 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4278 struct ctdb_transaction_handle *h;
4279 TDB_DATA key, value;
4284 talloc_free(tmp_ctx);
4290 file = fopen(argv[1], "r");
4292 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4293 talloc_free(tmp_ctx);
4300 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4301 if (ctdb_db == NULL) {
4302 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4306 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4308 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4312 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4313 if (key.dsize != 0) {
4314 ret = ctdb_transaction_store(h, key, value);
4315 /* Minimise memory use */
4316 talloc_free(key.dptr);
4317 if (value.dptr != NULL) {
4318 talloc_free(value.dptr);
4321 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4322 ctdb_transaction_cancel(h);
4328 ret = ctdb_transaction_commit(h);
4330 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4334 if (file != stdin) {
4337 talloc_free(tmp_ctx);
4341 if (file != stdin) {
4345 talloc_free(tmp_ctx);
4350 check if a service is bound to a port or not
4352 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4357 struct sockaddr_in sin;
4360 printf("Use: ctdb chktcport <port>\n");
4364 port = atoi(argv[0]);
4366 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
4368 printf("Failed to open local socket\n");
4372 v = fcntl(s, F_GETFL, 0);
4373 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4374 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4377 bzero(&sin, sizeof(sin));
4378 sin.sin_family = AF_INET;
4379 sin.sin_port = htons(port);
4380 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4383 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4391 /* Reload public IPs on a specified nodes */
4392 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4394 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4400 assert_single_node_only();
4406 /* Determine the nodes where IPs need to be reloaded */
4407 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4408 options.pnn, true, &nodes, &pnn_mode)) {
4414 /* Disable takeover runs on all connected nodes. A reply
4415 * indicating success is needed from each node so all nodes
4416 * will need to be active. This will retry until maxruntime
4417 * is exceeded, hence no error handling.
4419 * A check could be added to not allow reloading of IPs when
4420 * there are disconnected nodes. However, this should
4421 * probably be left up to the administrator.
4423 timeout = LONGTIMEOUT;
4424 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4425 "Disable takeover runs", true);
4427 /* Now tell all the desired nodes to reload their public IPs.
4428 * Keep trying this until it succeeds. This assumes all
4429 * failures are transient, which might not be true...
4431 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4432 nodes, 0, LONGTIMELIMIT(),
4434 NULL, NULL, NULL) != 0) {
4436 ("Unable to reload IPs on some nodes, try again.\n"));
4440 /* It isn't strictly necessary to wait until takeover runs are
4441 * re-enabled but doing so can't hurt.
4444 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4445 "Enable takeover runs", true);
4451 talloc_free(tmp_ctx);
4456 display a list of the databases on a remote ctdb
4458 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4461 struct ctdb_dbid_map_old *dbmap=NULL;
4463 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4465 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4469 if(options.machinereadable){
4470 printm(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4471 for(i=0;i<dbmap->num;i++){
4472 const char *path = NULL;
4473 const char *name = NULL;
4474 const char *health = NULL;
4479 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4480 dbmap->dbs[i].db_id, ctdb, &path);
4481 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4482 dbmap->dbs[i].db_id, ctdb, &name);
4483 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4484 dbmap->dbs[i].db_id, ctdb, &health);
4485 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4486 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4487 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4488 printm(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4489 dbmap->dbs[i].db_id, name, path,
4490 !!(persistent), !!(sticky),
4491 !!(health), !!(readonly));
4496 printf("Number of databases:%d\n", dbmap->num);
4497 for(i=0;i<dbmap->num;i++){
4498 const char *path = NULL;
4499 const char *name = NULL;
4500 const char *health = NULL;
4505 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &path);
4506 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &name);
4507 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &health);
4508 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4509 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4510 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4511 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4512 dbmap->dbs[i].db_id, name, path,
4513 persistent?" PERSISTENT":"",
4514 sticky?" STICKY":"",
4515 readonly?" READONLY":"",
4516 health?" UNHEALTHY":"");
4523 display the status of a database on a remote ctdb
4525 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4527 const char *db_name;
4530 const char *path = NULL;
4531 const char *health = NULL;
4537 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4541 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4542 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4543 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4544 db_id, db_name, path,
4545 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4546 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4547 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4548 (health ? health : "OK"));
4554 check if the local node is recmaster or not
4555 it will return 1 if this node is the recmaster and 0 if it is not
4556 or if the local ctdb daemon could not be contacted
4558 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4560 uint32_t mypnn, recmaster;
4563 assert_single_node_only();
4565 mypnn = getpnn(ctdb);
4567 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4569 printf("Failed to get the recmaster\n");
4573 if (recmaster != mypnn) {
4574 printf("this node is not the recmaster\n");
4578 printf("this node is the recmaster\n");
4585 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4588 struct timeval tv = timeval_current();
4589 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4591 printf("Unable to get ping response from node %u\n", options.pnn);
4594 printf("response from %u time=%.6f sec (%d clients)\n",
4595 options.pnn, timeval_elapsed(&tv), ret);
4602 get a node's runstate
4604 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4607 enum ctdb_runstate runstate;
4609 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4611 printf("Unable to get runstate response from node %u\n",
4616 enum ctdb_runstate t;
4618 for (i=0; i<argc; i++) {
4620 t = runstate_from_string(argv[i]);
4621 if (t == CTDB_RUNSTATE_UNKNOWN) {
4622 printf("Invalid run state (%s)\n", argv[i]);
4626 if (t == runstate) {
4633 printf("CTDB not in required run state (got %s)\n",
4634 runstate_to_string((enum ctdb_runstate)runstate));
4639 printf("%s\n", runstate_to_string(runstate));
4647 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4658 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4660 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4664 printf("%-23s = %u\n", name, value);
4671 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4682 value = strtoul(argv[1], NULL, 0);
4684 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4686 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4690 DEBUG(DEBUG_WARNING,
4691 ("Setting obsolete tunable variable '%s'\n",
4700 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4706 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4708 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4712 for (i=0;i<count;i++) {
4713 control_getvar(ctdb, 1, &list[i]);
4722 display debug level on a node
4724 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4729 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4731 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4734 enum debug_level log_level = debug_level_from_int(level);
4735 const char *desc = debug_level_to_string(log_level);
4737 /* This should never happen */
4740 if (options.machinereadable){
4741 printm(":Name:Level:\n");
4742 printm(":%s:%d:\n", desc, level);
4744 printf("Node %u is at debug level %s (%d)\n",
4745 options.pnn, desc, level);
4752 display reclock file of a node
4754 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4757 const char *reclock;
4759 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4761 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4764 if (options.machinereadable){
4765 if (reclock != NULL) {
4766 printm("%s", reclock);
4769 if (reclock == NULL) {
4770 printf("No reclock file used.\n");
4772 printf("Reclock file:%s\n", reclock);
4780 set the reclock file of a node
4782 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4785 const char *reclock = NULL;
4789 } else if (argc == 1) {
4795 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4797 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4804 set the lmaster role on/off
4806 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4809 uint32_t lmasterrole = 0;
4815 if (!strcmp(argv[0], "on")) {
4817 } else if (!strcmp(argv[0], "off")) {
4823 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4825 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4833 set the recmaster role on/off
4835 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4838 uint32_t recmasterrole = 0;
4844 if (!strcmp(argv[0], "on")) {
4846 } else if (!strcmp(argv[0], "off")) {
4852 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4854 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4862 set debug level on a node or all nodes
4864 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4868 enum debug_level log_level;
4871 printf("You must specify the debug level. Valid levels are:\n");
4872 printf("\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
4876 if (!debug_level_parse(argv[0], &log_level)) {
4877 printf("Invalid debug level, must be one of\n");
4878 printf("\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
4882 level = debug_level_to_int(log_level);
4884 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4886 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4895 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4901 priority = strtol(argv[0], NULL, 0);
4905 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4907 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4909 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4916 attach to a database
4918 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4920 const char *db_name;
4921 struct ctdb_db_context *ctdb_db;
4922 bool persistent = false;
4932 if (strcmp(argv[1], "persistent") != 0) {
4938 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4939 if (ctdb_db == NULL) {
4940 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4948 * detach from a database
4950 static int control_detach(struct ctdb_context *ctdb, int argc,
4955 int ret, i, status = 0;
4956 struct ctdb_node_map_old *nodemap = NULL;
4957 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4964 assert_single_node_only();
4966 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
4969 DEBUG(DEBUG_ERR, ("Database cannot be detached "
4970 "when recovery is active\n"));
4971 talloc_free(tmp_ctx);
4975 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4978 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4980 talloc_free(tmp_ctx);
4984 for (i=0; i<nodemap->num; i++) {
4987 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
4991 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
4995 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
4996 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
4997 "inactive (stopped or banned) node "
4998 "%u\n", nodemap->nodes[i].pnn));
4999 talloc_free(tmp_ctx);
5003 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5004 nodemap->nodes[i].pnn,
5005 "AllowClientDBAttach",
5008 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5009 "AllowClientDBAttach from node %u\n",
5010 nodemap->nodes[i].pnn));
5011 talloc_free(tmp_ctx);
5016 DEBUG(DEBUG_ERR, ("Database access is still active on "
5017 "node %u. Set AllowClientDBAttach=0 "
5019 nodemap->nodes[i].pnn));
5020 talloc_free(tmp_ctx);
5025 talloc_free(tmp_ctx);
5027 for (i=0; i<argc; i++) {
5028 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5032 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5033 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5034 "cannot be detached\n", argv[i]));
5039 ret = ctdb_detach(ctdb, db_id);
5041 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5053 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5055 struct ctdb_db_priority db_prio;
5062 db_prio.db_id = strtoul(argv[0], NULL, 0);
5063 db_prio.priority = strtoul(argv[1], NULL, 0);
5065 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5067 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5077 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5079 uint32_t db_id, priority;
5086 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5090 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5092 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5096 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5102 set the sticky records capability for a database
5104 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5106 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5114 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5118 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5120 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5121 talloc_free(tmp_ctx);
5125 talloc_free(tmp_ctx);
5130 set the readonly capability for a database
5132 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5134 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5142 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5146 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5148 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5149 talloc_free(tmp_ctx);
5153 talloc_free(tmp_ctx);
5160 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5170 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5174 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5176 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5180 printf("Sequence number:%lld\n", (long long)seqnum);
5186 run an eventscript on a node
5188 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5194 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5197 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5201 data.dptr = (unsigned char *)discard_const(argv[0]);
5202 data.dsize = strlen((char *)data.dptr) + 1;
5204 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5206 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5207 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5208 if (ret != 0 || res != 0) {
5209 if (errmsg != NULL) {
5211 ("Failed to run eventscripts - %s\n", errmsg));
5213 DEBUG(DEBUG_ERR, ("Failed to run eventscripts\n"));
5215 talloc_free(tmp_ctx);
5218 talloc_free(tmp_ctx);
5222 #define DB_VERSION 1
5223 #define MAX_DB_NAME 64
5224 struct db_file_header {
5225 unsigned long version;
5227 unsigned long persistent;
5229 const char name[MAX_DB_NAME];
5232 struct backup_data {
5233 struct ctdb_marshall_buffer *records;
5236 bool traverse_error;
5239 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5241 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5242 struct ctdb_rec_data_old *rec;
5244 /* add the record */
5245 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5247 bd->traverse_error = true;
5248 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5251 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5252 if (bd->records == NULL) {
5253 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5254 bd->traverse_error = true;
5257 bd->records->count++;
5258 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5259 bd->len += rec->length;
5267 * backup a database to a file
5269 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5271 const char *db_name;
5273 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5274 struct db_file_header dbhdr;
5275 struct ctdb_db_context *ctdb_db;
5276 struct backup_data *bd;
5279 const char *reason = NULL;
5283 assert_single_node_only();
5286 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5290 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5294 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5295 db_id, tmp_ctx, &reason);
5297 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5299 talloc_free(tmp_ctx);
5303 uint32_t allow_unhealthy = 0;
5305 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5306 "AllowUnhealthyDBRead",
5309 if (allow_unhealthy != 1) {
5310 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5313 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5315 talloc_free(tmp_ctx);
5319 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5321 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5322 "tunnable AllowUnhealthyDBRead = %u\n",
5326 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5327 if (ctdb_db == NULL) {
5328 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5329 talloc_free(tmp_ctx);
5334 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5336 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5337 talloc_free(tmp_ctx);
5342 bd = talloc_zero(tmp_ctx, struct backup_data);
5344 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5345 talloc_free(tmp_ctx);
5349 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5350 if (bd->records == NULL) {
5351 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5352 talloc_free(tmp_ctx);
5356 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5357 bd->records->db_id = ctdb_db->db_id;
5358 /* traverse the database collecting all records */
5359 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5360 bd->traverse_error) {
5361 DEBUG(DEBUG_ERR,("Traverse error\n"));
5362 talloc_free(tmp_ctx);
5366 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5369 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5371 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5372 talloc_free(tmp_ctx);
5377 dbhdr.version = DB_VERSION;
5378 dbhdr.timestamp = time(NULL);
5379 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5380 dbhdr.size = bd->len;
5381 if (strlen(argv[0]) >= MAX_DB_NAME) {
5382 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5385 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5386 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
5388 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5391 ret = sys_write(fh, bd->records, bd->len);
5393 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5402 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5406 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5408 talloc_free(tmp_ctx);
5413 * restore a database from a file
5415 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5418 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5421 struct db_file_header dbhdr;
5422 struct ctdb_db_context *ctdb_db;
5423 struct ctdb_node_map_old *nodemap=NULL;
5424 struct ctdb_vnn_map *vnnmap=NULL;
5426 struct ctdb_transdb w;
5428 uint32_t generation;
5433 assert_single_node_only();
5435 if (argc < 1 || argc > 2) {
5436 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5440 fh = open(argv[0], O_RDONLY);
5442 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5443 talloc_free(tmp_ctx);
5447 sys_read(fh, &dbhdr, sizeof(dbhdr));
5448 if (dbhdr.version != DB_VERSION) {
5449 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5451 talloc_free(tmp_ctx);
5455 dbname = discard_const(dbhdr.name);
5457 dbname = discard_const(argv[1]);
5460 outdata.dsize = dbhdr.size;
5461 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5462 if (outdata.dptr == NULL) {
5463 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5465 talloc_free(tmp_ctx);
5468 sys_read(fh, outdata.dptr, outdata.dsize);
5471 tm = localtime(&dbhdr.timestamp);
5472 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5473 printf("Restoring database '%s' from backup @ %s\n",
5477 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5478 if (ctdb_db == NULL) {
5479 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5480 talloc_free(tmp_ctx);
5484 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5486 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5487 talloc_free(tmp_ctx);
5492 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5494 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5495 talloc_free(tmp_ctx);
5499 /* freeze all nodes */
5500 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5501 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5502 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5508 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5509 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5510 talloc_free(tmp_ctx);
5515 generation = vnnmap->generation;
5516 data.dptr = (void *)&generation;
5517 data.dsize = sizeof(generation);
5519 /* start a cluster wide transaction */
5520 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5521 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5523 TIMELIMIT(), false, data,
5526 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5531 w.db_id = ctdb_db->db_id;
5534 data.dptr = (void *)&w;
5535 data.dsize = sizeof(w);
5537 /* wipe all the remote databases. */
5538 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5539 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5541 TIMELIMIT(), false, data,
5544 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5545 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5546 talloc_free(tmp_ctx);
5550 /* push the database */
5551 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5552 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5554 TIMELIMIT(), false, outdata,
5557 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5558 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5559 talloc_free(tmp_ctx);
5563 data.dptr = (void *)&ctdb_db->db_id;
5564 data.dsize = sizeof(ctdb_db->db_id);
5566 /* mark the database as healthy */
5567 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5568 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5570 TIMELIMIT(), false, data,
5573 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5574 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5575 talloc_free(tmp_ctx);
5579 data.dptr = (void *)&generation;
5580 data.dsize = sizeof(generation);
5582 /* commit all the changes */
5583 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5585 TIMELIMIT(), false, data,
5588 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5589 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5590 talloc_free(tmp_ctx);
5595 /* thaw all nodes */
5596 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5597 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5603 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5604 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5605 talloc_free(tmp_ctx);
5610 talloc_free(tmp_ctx);
5615 * dump a database backup from a file
5617 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5619 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5621 struct db_file_header dbhdr;
5625 struct ctdb_rec_data_old *rec = NULL;
5626 struct ctdb_marshall_buffer *m;
5627 struct ctdb_dump_db_context c;
5629 assert_single_node_only();
5632 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5636 fh = open(argv[0], O_RDONLY);
5638 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5639 talloc_free(tmp_ctx);
5643 sys_read(fh, &dbhdr, sizeof(dbhdr));
5644 if (dbhdr.version != DB_VERSION) {
5645 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5647 talloc_free(tmp_ctx);
5651 outdata.dsize = dbhdr.size;
5652 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5653 if (outdata.dptr == NULL) {
5654 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5656 talloc_free(tmp_ctx);
5659 sys_read(fh, outdata.dptr, outdata.dsize);
5661 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5663 tm = localtime(&dbhdr.timestamp);
5664 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5665 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5666 dbhdr.name, m->db_id, tbuf);
5671 c.printemptyrecords = (bool)options.printemptyrecords;
5672 c.printdatasize = (bool)options.printdatasize;
5673 c.printlmaster = false;
5674 c.printhash = (bool)options.printhash;
5675 c.printrecordflags = (bool)options.printrecordflags;
5677 for (i=0; i < m->count; i++) {
5681 /* we do not want the header splitted, so we pass NULL*/
5682 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5685 ctdb_dumpdb_record(key, data, &c);
5688 printf("Dumped %d records\n", i);
5689 talloc_free(tmp_ctx);
5694 * wipe a database from a file
5696 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5699 const char *db_name;
5701 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5703 struct ctdb_db_context *ctdb_db;
5704 struct ctdb_node_map_old *nodemap = NULL;
5705 struct ctdb_vnn_map *vnnmap = NULL;
5707 struct ctdb_transdb w;
5709 uint32_t generation;
5712 assert_single_node_only();
5715 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5719 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5723 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5724 if (ctdb_db == NULL) {
5725 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5727 talloc_free(tmp_ctx);
5731 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5734 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5736 talloc_free(tmp_ctx);
5740 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5743 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5745 talloc_free(tmp_ctx);
5749 /* freeze all nodes */
5750 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5751 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5752 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5759 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5760 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5761 CTDB_RECOVERY_ACTIVE);
5762 talloc_free(tmp_ctx);
5767 generation = vnnmap->generation;
5768 data.dptr = (void *)&generation;
5769 data.dsize = sizeof(generation);
5771 /* start a cluster wide transaction */
5772 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5773 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5775 TIMELIMIT(), false, data,
5779 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5780 "transactions.\n"));
5784 w.db_id = ctdb_db->db_id;
5787 data.dptr = (void *)&w;
5788 data.dsize = sizeof(w);
5790 /* wipe all the remote databases. */
5791 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5792 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5794 TIMELIMIT(), false, data,
5797 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5798 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5799 talloc_free(tmp_ctx);
5803 data.dptr = (void *)&ctdb_db->db_id;
5804 data.dsize = sizeof(ctdb_db->db_id);
5806 /* mark the database as healthy */
5807 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5808 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5810 TIMELIMIT(), false, data,
5813 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5814 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5815 talloc_free(tmp_ctx);
5819 data.dptr = (void *)&generation;
5820 data.dsize = sizeof(generation);
5822 /* commit all the changes */
5823 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5825 TIMELIMIT(), false, data,
5828 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5829 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5830 talloc_free(tmp_ctx);
5834 /* thaw all nodes */
5835 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5836 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5842 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5843 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5844 talloc_free(tmp_ctx);
5848 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5850 talloc_free(tmp_ctx);
5857 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5863 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5864 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5865 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5866 if (ret != 0 || res != 0) {
5867 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5868 talloc_free(tmp_ctx);
5871 sys_write(1, data.dptr, data.dsize);
5872 talloc_free(tmp_ctx);
5877 handler for memory dumps
5879 static void mem_dump_handler(uint64_t srvid, TDB_DATA data, void *private_data)
5881 sys_write(1, data.dptr, data.dsize);
5886 dump memory usage on the recovery daemon
5888 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5892 struct ctdb_srvid_message rd;
5894 rd.pnn = ctdb_get_pnn(ctdb);
5895 rd.srvid = getpid();
5897 /* register a message port for receiveing the reply so that we
5898 can receive the reply
5900 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5903 data.dptr = (uint8_t *)&rd;
5904 data.dsize = sizeof(rd);
5906 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5908 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5912 /* this loop will terminate when we have received the reply */
5914 tevent_loop_once(ctdb->ev);
5921 send a message to a srvid
5923 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5925 unsigned long srvid;
5933 srvid = strtoul(argv[0], NULL, 0);
5935 data.dptr = (uint8_t *)discard_const(argv[1]);
5936 data.dsize= strlen(argv[1]);
5938 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5940 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5948 handler for msglisten
5950 static void msglisten_handler(uint64_t srvid, TDB_DATA data,
5955 printf("Message received: ");
5956 for (i=0;i<data.dsize;i++) {
5957 printf("%c", data.dptr[i]);
5963 listen for messages on a messageport
5965 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5971 /* register a message port and listen for messages
5973 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5974 printf("Listening for messages on srvid:%d\n", (int)srvid);
5977 tevent_loop_once(ctdb->ev);
5984 list all nodes in the cluster
5985 we parse the nodes file directly
5987 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5989 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5990 struct ctdb_node_map_old *node_map;
5993 assert_single_node_only();
5995 node_map = read_nodes_file(mem_ctx);
5996 if (node_map == NULL) {
5997 talloc_free(mem_ctx);
6001 for (i = 0; i < node_map->num; i++) {
6004 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
6007 addr = ctdb_addr_to_str(&node_map->nodes[i].addr);
6008 if (options.machinereadable){
6009 printm(":%d:%s:\n", node_map->nodes[i].pnn, addr);
6011 printf("%s\n", addr);
6014 talloc_free(mem_ctx);
6019 /**********************************************************************/
6020 /* reload the nodes file on all nodes */
6022 static void get_nodes_files_callback(struct ctdb_context *ctdb,
6023 uint32_t node_pnn, int32_t res,
6024 TDB_DATA outdata, void *callback_data)
6026 struct ctdb_node_map_old **maps =
6027 talloc_get_type(callback_data, struct ctdb_node_map_old *);
6029 if (outdata.dsize < offsetof(struct ctdb_node_map_old, nodes) ||
6030 outdata.dptr == NULL) {
6032 (__location__ " Invalid return data: %u %p\n",
6033 (unsigned)outdata.dsize, outdata.dptr));
6037 if (node_pnn >= talloc_array_length(maps)) {
6039 (__location__ " unexpected PNN %u\n", node_pnn));
6043 maps[node_pnn] = talloc_memdup(maps, outdata.dptr, outdata.dsize);
6046 static void get_nodes_files_fail_callback(struct ctdb_context *ctdb,
6047 uint32_t node_pnn, int32_t res,
6048 TDB_DATA outdata, void *callback_data)
6051 ("ERROR: Failed to get nodes file from node %u\n", node_pnn));
6054 static struct ctdb_node_map_old **
6055 ctdb_get_nodes_files(struct ctdb_context *ctdb,
6056 TALLOC_CTX *mem_ctx,
6057 struct timeval timeout,
6058 struct ctdb_node_map_old *nodemap)
6062 struct ctdb_node_map_old **maps;
6064 maps = talloc_zero_array(mem_ctx, struct ctdb_node_map_old *, nodemap->num);
6065 CTDB_NO_MEMORY_NULL(ctdb, maps);
6067 nodes = list_of_connected_nodes(ctdb, nodemap, mem_ctx, true);
6069 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_NODES_FILE,
6070 nodes, 0, TIMELIMIT(),
6072 get_nodes_files_callback,
6073 get_nodes_files_fail_callback,
6083 static bool node_files_are_identical(struct ctdb_node_map_old *nm1,
6084 struct ctdb_node_map_old *nm2)
6088 if (nm1->num != nm2->num) {
6091 for (i = 0; i < nm1->num; i++) {
6092 if (memcmp(&nm1->nodes[i], &nm2->nodes[i],
6093 sizeof(struct ctdb_node_and_flags)) != 0) {
6101 static bool check_all_node_files_are_identical(struct ctdb_context *ctdb,
6102 TALLOC_CTX *mem_ctx,
6103 struct timeval timeout,
6104 struct ctdb_node_map_old *nodemap,
6105 struct ctdb_node_map_old *file_nodemap)
6107 static struct ctdb_node_map_old **maps;
6111 maps = ctdb_get_nodes_files(ctdb, mem_ctx, timeout, nodemap);
6116 for (i = 0; i < talloc_array_length(maps); i++) {
6117 if (maps[i] == NULL) {
6120 if (!node_files_are_identical(file_nodemap, maps[i])) {
6122 ("ERROR: Node file on node %u differs from current node (%u)\n",
6123 i, ctdb_get_pnn(ctdb)));
6132 reload the nodes file on the local node
6134 static bool sanity_check_nodes_file_changes(TALLOC_CTX *mem_ctx,
6135 struct ctdb_node_map_old *nodemap,
6136 struct ctdb_node_map_old *file_nodemap)
6139 bool should_abort = false;
6140 bool have_changes = false;
6142 for (i=0; i<nodemap->num; i++) {
6143 if (i >= file_nodemap->num) {
6145 ("ERROR: Node %u (%s) missing from nodes file\n",
6146 nodemap->nodes[i].pnn,
6147 ctdb_addr_to_str(&nodemap->nodes[i].addr)));
6148 should_abort = true;
6151 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED &&
6152 file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6153 /* Node remains deleted */
6155 ("Node %u is unchanged (DELETED)\n",
6156 nodemap->nodes[i].pnn));
6157 } else if (!(nodemap->nodes[i].flags & NODE_FLAGS_DELETED) &&
6158 !(file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED)) {
6159 /* Node not newly nor previously deleted */
6160 if (!ctdb_same_ip(&nodemap->nodes[i].addr,
6161 &file_nodemap->nodes[i].addr)) {
6163 ("ERROR: Node %u has changed IP address (was %s, now %s)\n",
6164 nodemap->nodes[i].pnn,
6165 /* ctdb_addr_to_str() returns a static */
6166 talloc_strdup(mem_ctx,
6167 ctdb_addr_to_str(&nodemap->nodes[i].addr)),
6168 ctdb_addr_to_str(&file_nodemap->nodes[i].addr)));
6169 should_abort = true;
6172 ("Node %u is unchanged\n",
6173 nodemap->nodes[i].pnn));
6174 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
6175 DEBUG(DEBUG_WARNING,
6176 ("WARNING: Node %u is disconnected. You MUST fix this node manually!\n",
6177 nodemap->nodes[i].pnn));
6180 } else if (file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6181 /* Node is being deleted */
6183 ("Node %u is DELETED\n",
6184 nodemap->nodes[i].pnn));
6185 have_changes = true;
6186 if (!(nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED)) {
6188 ("ERROR: Node %u is still connected\n",
6189 nodemap->nodes[i].pnn));
6190 should_abort = true;
6192 } else if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6193 /* Node was previously deleted */
6195 ("Node %u is UNDELETED\n", nodemap->nodes[i].pnn));
6196 have_changes = true;
6202 ("ERROR: Nodes will not be reloaded due to previous error\n"));
6203 talloc_free(mem_ctx);
6207 /* Leftover nodes in file are NEW */
6208 for (; i < file_nodemap->num; i++) {
6209 DEBUG(DEBUG_NOTICE, ("Node %u is NEW\n",
6210 file_nodemap->nodes[i].pnn));
6211 have_changes = true;
6214 return have_changes;
6217 static void reload_nodes_fail_callback(struct ctdb_context *ctdb,
6218 uint32_t node_pnn, int32_t res,
6219 TDB_DATA outdata, void *callback_data)
6221 DEBUG(DEBUG_WARNING,
6222 ("WARNING: Node %u failed to reload nodes. You MUST fix this node manually!\n",
6226 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6229 struct ctdb_node_map_old *nodemap=NULL;
6230 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
6231 struct ctdb_node_map_old *file_nodemap;
6235 assert_current_node_only(ctdb);
6237 /* Load both the current nodemap and the contents of the local
6238 * nodes file. Compare and sanity check them before doing
6241 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6243 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6247 file_nodemap = read_nodes_file(tmp_ctx);
6248 if (file_nodemap == NULL) {
6249 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6250 talloc_free(tmp_ctx);
6254 if (!check_all_node_files_are_identical(ctdb, tmp_ctx, TIMELIMIT(),
6255 nodemap, file_nodemap)) {
6259 if (!sanity_check_nodes_file_changes(tmp_ctx, nodemap, file_nodemap)) {
6261 ("No change in nodes file, skipping unnecessary reload\n"));
6262 talloc_free(tmp_ctx);
6266 /* Now make the changes */
6267 conn = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
6268 for (i = 0; i < talloc_array_length(conn); i++) {
6269 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n",
6273 /* Another timeout could be used, such as ReRecoveryTimeout or
6274 * a new one for this purpose. However, this is the simplest
6276 timeout = options.timelimit;
6277 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
6278 "Disable recoveries", true);
6281 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_NODES_FILE,
6282 conn, 0, TIMELIMIT(),
6284 NULL, reload_nodes_fail_callback,
6288 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
6289 "Enable recoveries", true);
6291 talloc_free(tmp_ctx);
6297 static const struct {
6299 int (*fn)(struct ctdb_context *, int, const char **);
6301 bool without_daemon; /* can be run without daemon running ? */
6304 } ctdb_commands[] = {
6305 { "version", control_version, true, true, "show version of ctdb" },
6306 { "status", control_status, true, false, "show node status" },
6307 { "uptime", control_uptime, true, false, "show node uptime" },
6308 { "ping", control_ping, true, false, "ping all nodes" },
6309 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6310 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6311 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6312 { "listvars", control_listvars, true, false, "list tunable variables"},
6313 { "statistics", control_statistics, false, false, "show statistics" },
6314 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6315 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6316 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6317 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6318 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6319 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6320 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6321 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6322 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6323 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6324 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6325 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6326 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6327 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6328 { "lvs", control_lvs, true, false, "show lvs configuration" },
6329 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6330 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6331 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6332 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6333 { "getdebug", control_getdebug, true, false, "get debug level" },
6334 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6335 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6336 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6337 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6338 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6339 { "disable", control_disable, true, false, "disable a nodes public IP" },
6340 { "enable", control_enable, true, false, "enable a nodes public IP" },
6341 { "stop", control_stop, true, false, "stop a node" },
6342 { "continue", control_continue, true, false, "re-start a stopped node" },
6343 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6344 { "unban", control_unban, true, false, "unban a node" },
6345 { "showban", control_showban, true, false, "show ban information"},
6346 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6347 { "recover", control_recover, true, false, "force recovery" },
6348 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6349 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6350 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6351 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6352 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6353 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6354 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6355 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6356 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6358 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6360 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6361 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6362 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6363 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6364 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6365 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6366 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6367 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6368 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6369 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6370 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6371 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6372 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6373 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6374 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6375 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6376 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6377 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6378 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6379 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6380 { "natgwlist", control_natgwlist, false, true, "show the nodes belonging to this natgw configuration"},
6381 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6382 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6383 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6384 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6385 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6386 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6387 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6388 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6389 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6390 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6391 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6392 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6393 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6394 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6395 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6396 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6397 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6398 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6399 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6400 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6401 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6402 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6403 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6404 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6405 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6406 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6412 static void usage(void)
6416 "Usage: ctdb [options] <control>\n" \
6418 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6419 " -Y generate machine readable output\n"
6420 " -x <char> specify delimiter for machine readable output\n"
6421 " -v generate verbose output\n"
6422 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6423 printf("Controls:\n");
6424 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6425 printf(" %-15s %-27s %s\n",
6426 ctdb_commands[i].name,
6427 ctdb_commands[i].args?ctdb_commands[i].args:"",
6428 ctdb_commands[i].msg);
6434 static void ctdb_alarm(int sig)
6436 printf("Maximum runtime exceeded - exiting\n");
6443 int main(int argc, const char *argv[])
6445 struct ctdb_context *ctdb;
6446 char *nodestring = NULL;
6447 int machineparsable = 0;
6448 struct poptOption popt_options[] = {
6451 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6452 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6453 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machine readable output", NULL },
6454 { NULL, 'x', POPT_ARG_STRING, &options.machineseparator, 0, "specify separator for machine readable output", "char" },
6455 { NULL, 'X', POPT_ARG_NONE, &machineparsable, 0, "enable machine parsable output with separator |", NULL },
6456 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6457 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6458 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6459 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6460 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6461 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6462 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6466 const char **extra_argv;
6470 struct tevent_context *ev;
6471 const char *control;
6475 /* set some defaults */
6476 options.maxruntime = 0;
6477 options.timelimit = 10;
6478 options.pnn = CTDB_CURRENT_NODE;
6480 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6482 while ((opt = poptGetNextOpt(pc)) != -1) {
6485 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6486 poptBadOption(pc, 0), poptStrerror(opt)));
6491 /* setup the remaining options for the main program to use */
6492 extra_argv = poptGetArgs(pc);
6495 while (extra_argv[extra_argc]) extra_argc++;
6498 if (extra_argc < 1) {
6502 if (options.maxruntime == 0) {
6503 const char *ctdb_timeout;
6504 ctdb_timeout = getenv("CTDB_TIMEOUT");
6505 if (ctdb_timeout != NULL) {
6506 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6508 /* default timeout is 120 seconds */
6509 options.maxruntime = 120;
6513 if (machineparsable) {
6514 options.machineseparator = "|";
6516 if (options.machineseparator != NULL) {
6517 if (strlen(options.machineseparator) != 1) {
6518 printf("Invalid separator \"%s\" - "
6519 "must be single character\n",
6520 options.machineseparator);
6525 options.machinereadable = true;
6526 } else if (options.machinereadable) {
6527 options.machineseparator = ":";
6530 signal(SIGALRM, ctdb_alarm);
6531 alarm(options.maxruntime);
6533 control = extra_argv[0];
6535 /* Default value for CTDB_BASE - don't override */
6536 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
6538 ev = tevent_context_init(NULL);
6540 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6544 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6545 if (strcmp(control, ctdb_commands[i].name) == 0) {
6550 if (i == ARRAY_SIZE(ctdb_commands)) {
6551 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6555 if (ctdb_commands[i].without_daemon == true) {
6556 if (nodestring != NULL) {
6557 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6560 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6563 /* initialise ctdb */
6564 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6568 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6570 pnn = find_node_xpnn();
6573 ("Is this node part of a CTDB cluster?\n"));
6578 /* setup the node number(s) to contact */
6579 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6580 &options.nodes, &options.pnn)) {
6584 if (options.pnn == CTDB_CURRENT_NODE) {
6585 options.pnn = options.nodes[0];
6588 if (ctdb_commands[i].auto_all &&
6589 ((options.pnn == CTDB_BROADCAST_ALL) ||
6590 (options.pnn == CTDB_MULTICAST))) {
6594 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6595 options.pnn = options.nodes[j];
6596 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6599 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6604 (void)poptFreeContext(pc);