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 static void usage(void);
55 const char *machineseparator;
58 int printemptyrecords;
65 #define LONGTIMEOUT options.timelimit*10
67 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
68 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
70 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
72 return (tv2->tv_sec - tv->tv_sec) +
73 (tv2->tv_usec - tv->tv_usec)*1.0e-6;
76 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
78 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
82 /* Like printf(3) but substitute for separator in format */
83 static int printm(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
84 static int printm(const char *format, ...)
88 size_t len = strlen(format);
89 char new_format[len+1];
91 strncpy(new_format, format, len+1);
93 if (options.machineseparator[0] != ':') {
94 all_string_sub(new_format,
95 ":", options.machineseparator, len + 1);
99 ret = vprintf(new_format, ap);
105 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
106 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
107 "Out of memory in " __location__ )); \
111 static uint32_t getpnn(struct ctdb_context *ctdb)
113 if ((options.pnn == CTDB_BROADCAST_ALL) ||
114 (options.pnn == CTDB_MULTICAST)) {
116 ("Cannot get PNN for node %u\n", options.pnn));
120 if (options.pnn == CTDB_CURRENT_NODE) {
121 return ctdb_get_pnn(ctdb);
127 static void assert_single_node_only(void)
129 if ((options.pnn == CTDB_BROADCAST_ALL) ||
130 (options.pnn == CTDB_MULTICAST)) {
132 ("This control can not be applied to multiple PNNs\n"));
137 static void assert_current_node_only(struct ctdb_context *ctdb)
139 if (options.pnn != ctdb_get_pnn(ctdb)) {
141 ("This control can only be applied to the current node\n"));
146 /* Pretty print the flags to a static buffer in human-readable format.
147 * This never returns NULL!
149 static const char *pretty_print_flags(uint32_t flags)
152 static const struct {
156 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
157 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
158 { NODE_FLAGS_BANNED, "BANNED" },
159 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
160 { NODE_FLAGS_DELETED, "DELETED" },
161 { NODE_FLAGS_STOPPED, "STOPPED" },
162 { NODE_FLAGS_INACTIVE, "INACTIVE" },
164 static char flags_str[512]; /* Big enough to contain all flag names */
167 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
168 if (flags & flag_names[j].flag) {
169 if (flags_str[0] == '\0') {
170 (void) strcpy(flags_str, flag_names[j].name);
172 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
173 (void) strncat(flags_str, flag_names[j].name,
174 sizeof(flags_str)-1);
178 if (flags_str[0] == '\0') {
179 (void) strcpy(flags_str, "OK");
185 static int h2i(char h)
187 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
188 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
192 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
195 TDB_DATA key = {NULL, 0};
198 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
203 key.dptr = talloc_size(mem_ctx, key.dsize);
205 for (i=0; i < len/2; i++) {
206 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
211 static TDB_DATA strtodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
215 if (!strncmp(str, "0x", 2)) {
216 key = hextodata(mem_ctx, str + 2, len - 2);
218 key.dptr = talloc_memdup(mem_ctx, str, len);
225 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
226 * that are disconnected or deleted. If dd_ok is true those nodes are
227 * included in the output list of nodes. If dd_ok is false, those
228 * nodes are filtered from the "all" case and cause an error if
229 * explicitly specified.
231 static bool parse_nodestring(struct ctdb_context *ctdb,
233 const char * nodestring,
234 uint32_t current_pnn,
239 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
242 struct ctdb_node_map_old *nodemap;
247 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
249 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
250 talloc_free(tmp_ctx);
254 if (nodestring != NULL) {
255 *nodes = talloc_array(mem_ctx, uint32_t, 0);
256 if (*nodes == NULL) {
262 if (strcmp(nodestring, "all") == 0) {
263 *pnn_mode = CTDB_BROADCAST_ALL;
266 for (i = 0; i < nodemap->num; i++) {
267 if ((nodemap->nodes[i].flags &
268 (NODE_FLAGS_DISCONNECTED |
269 NODE_FLAGS_DELETED)) && !dd_ok) {
272 *nodes = talloc_realloc(mem_ctx, *nodes,
274 if (*nodes == NULL) {
284 ns = talloc_strdup(tmp_ctx, nodestring);
285 tok = strtok(ns, ",");
286 while (tok != NULL) {
289 i = (uint32_t)strtoul(tok, &endptr, 0);
290 if (i == 0 && tok == endptr) {
292 ("Invalid node %s\n", tok));
293 talloc_free(tmp_ctx);
296 if (i >= nodemap->num) {
297 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
298 talloc_free(tmp_ctx);
301 if ((nodemap->nodes[i].flags &
302 (NODE_FLAGS_DISCONNECTED |
303 NODE_FLAGS_DELETED)) && !dd_ok) {
304 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
305 talloc_free(tmp_ctx);
308 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
309 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
310 talloc_free(tmp_ctx);
314 *nodes = talloc_realloc(mem_ctx, *nodes,
316 if (*nodes == NULL) {
323 tok = strtok(NULL, ",");
328 *pnn_mode = (*nodes)[0];
330 *pnn_mode = CTDB_MULTICAST;
334 /* default - no nodes specified */
335 *nodes = talloc_array(mem_ctx, uint32_t, 1);
336 if (*nodes == NULL) {
339 *pnn_mode = CTDB_CURRENT_NODE;
341 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
346 talloc_free(tmp_ctx);
350 talloc_free(tmp_ctx);
355 check if a database exists
357 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
358 uint32_t *dbid, const char **dbname, uint8_t *flags)
361 struct ctdb_dbid_map_old *dbmap=NULL;
362 bool dbid_given = false, found = false;
364 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
365 const char *name = NULL;
367 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
369 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
373 if (strncmp(dbarg, "0x", 2) == 0) {
374 id = strtoul(dbarg, NULL, 0);
378 for(i=0; i<dbmap->num; i++) {
380 if (id == dbmap->dbs[i].db_id) {
385 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, tmp_ctx, &name);
387 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].db_id));
391 if (strcmp(name, dbarg) == 0) {
392 id = dbmap->dbs[i].db_id;
399 if (found && dbid_given && dbname != NULL) {
400 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, tmp_ctx, &name);
402 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].db_id));
409 if (dbid) *dbid = id;
410 if (dbname) *dbname = talloc_strdup(ctdb, name);
411 if (flags) *flags = dbmap->dbs[i].flags;
413 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
417 talloc_free(tmp_ctx);
422 see if a process exists
424 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
432 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
433 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
437 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
439 printf("%u:%u exists\n", pnn, pid);
441 printf("%u:%u does not exist\n", pnn, pid);
447 display statistics structure
449 static void show_statistics(struct ctdb_statistics *s, int show_header)
451 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
453 const char *prefix=NULL;
455 int tmp, days, hours, minutes, seconds;
460 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
461 STATISTICS_FIELD(num_clients),
462 STATISTICS_FIELD(frozen),
463 STATISTICS_FIELD(recovering),
464 STATISTICS_FIELD(num_recoveries),
465 STATISTICS_FIELD(client_packets_sent),
466 STATISTICS_FIELD(client_packets_recv),
467 STATISTICS_FIELD(node_packets_sent),
468 STATISTICS_FIELD(node_packets_recv),
469 STATISTICS_FIELD(keepalive_packets_sent),
470 STATISTICS_FIELD(keepalive_packets_recv),
471 STATISTICS_FIELD(node.req_call),
472 STATISTICS_FIELD(node.reply_call),
473 STATISTICS_FIELD(node.req_dmaster),
474 STATISTICS_FIELD(node.reply_dmaster),
475 STATISTICS_FIELD(node.reply_error),
476 STATISTICS_FIELD(node.req_message),
477 STATISTICS_FIELD(node.req_control),
478 STATISTICS_FIELD(node.reply_control),
479 STATISTICS_FIELD(client.req_call),
480 STATISTICS_FIELD(client.req_message),
481 STATISTICS_FIELD(client.req_control),
482 STATISTICS_FIELD(timeouts.call),
483 STATISTICS_FIELD(timeouts.control),
484 STATISTICS_FIELD(timeouts.traverse),
485 STATISTICS_FIELD(locks.num_calls),
486 STATISTICS_FIELD(locks.num_current),
487 STATISTICS_FIELD(locks.num_pending),
488 STATISTICS_FIELD(locks.num_failed),
489 STATISTICS_FIELD(total_calls),
490 STATISTICS_FIELD(pending_calls),
491 STATISTICS_FIELD(childwrite_calls),
492 STATISTICS_FIELD(pending_childwrite_calls),
493 STATISTICS_FIELD(memory_used),
494 STATISTICS_FIELD(max_hop_count),
495 STATISTICS_FIELD(total_ro_delegations),
496 STATISTICS_FIELD(total_ro_revokes),
499 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
508 if (options.machinereadable){
510 printm("CTDB version:");
511 printm("Current time of statistics:");
512 printm("Statistics collected since:");
513 for (i=0;i<ARRAY_SIZE(fields);i++) {
514 printm("%s:", fields[i].name);
516 printm("num_reclock_ctdbd_latency:");
517 printm("min_reclock_ctdbd_latency:");
518 printm("avg_reclock_ctdbd_latency:");
519 printm("max_reclock_ctdbd_latency:");
521 printm("num_reclock_recd_latency:");
522 printm("min_reclock_recd_latency:");
523 printm("avg_reclock_recd_latency:");
524 printm("max_reclock_recd_latency:");
526 printm("num_call_latency:");
527 printm("min_call_latency:");
528 printm("avg_call_latency:");
529 printm("max_call_latency:");
531 printm("num_lockwait_latency:");
532 printm("min_lockwait_latency:");
533 printm("avg_lockwait_latency:");
534 printm("max_lockwait_latency:");
536 printm("num_childwrite_latency:");
537 printm("min_childwrite_latency:");
538 printm("avg_childwrite_latency:");
539 printm("max_childwrite_latency:");
542 printm("%d:", CTDB_PROTOCOL);
543 printm("%d:", (int)s->statistics_current_time.tv_sec);
544 printm("%d:", (int)s->statistics_start_time.tv_sec);
545 for (i=0;i<ARRAY_SIZE(fields);i++) {
546 printm("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
548 printm("%d:", s->reclock.ctdbd.num);
549 printm("%.6f:", s->reclock.ctdbd.min);
550 printm("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
551 printm("%.6f:", s->reclock.ctdbd.max);
553 printm("%d:", s->reclock.recd.num);
554 printm("%.6f:", s->reclock.recd.min);
555 printm("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
556 printm("%.6f:", s->reclock.recd.max);
558 printm("%d:", s->call_latency.num);
559 printm("%.6f:", s->call_latency.min);
560 printm("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
561 printm("%.6f:", s->call_latency.max);
563 printm("%d:", s->childwrite_latency.num);
564 printm("%.6f:", s->childwrite_latency.min);
565 printm("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
566 printm("%.6f:", s->childwrite_latency.max);
569 printf("CTDB version %u\n", CTDB_PROTOCOL);
570 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
571 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
573 for (i=0;i<ARRAY_SIZE(fields);i++) {
574 if (strchr(fields[i].name, '.')) {
575 preflen = strcspn(fields[i].name, ".")+1;
576 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
577 prefix = fields[i].name;
578 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
583 printf(" %*s%-22s%*s%10u\n",
585 fields[i].name+preflen,
587 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
589 printf(" hop_count_buckets:");
590 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
591 printf(" %d", s->hop_count_bucket[i]);
594 printf(" lock_buckets:");
595 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
596 printf(" %d", s->locks.buckets[i]);
599 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);
601 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);
603 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);
605 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);
606 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);
609 talloc_free(tmp_ctx);
613 display remote ctdb statistics combined from all nodes
615 static int control_statistics_all(struct ctdb_context *ctdb)
618 struct ctdb_statistics statistics;
622 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
623 CTDB_NO_MEMORY(ctdb, nodes);
625 ZERO_STRUCT(statistics);
627 for (i=0;i<num_nodes;i++) {
628 struct ctdb_statistics s1;
630 uint32_t *v1 = (uint32_t *)&s1;
631 uint32_t *v2 = (uint32_t *)&statistics;
633 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
634 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
636 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
639 for (j=0;j<num_ints;j++) {
642 statistics.max_hop_count =
643 MAX(statistics.max_hop_count, s1.max_hop_count);
644 statistics.call_latency.max =
645 MAX(statistics.call_latency.max, s1.call_latency.max);
648 printf("Gathered statistics for %u nodes\n", num_nodes);
649 show_statistics(&statistics, 1);
654 display remote ctdb statistics
656 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
659 struct ctdb_statistics statistics;
661 if (options.pnn == CTDB_BROADCAST_ALL) {
662 return control_statistics_all(ctdb);
665 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
667 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
670 show_statistics(&statistics, 1);
676 reset remote ctdb statistics
678 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
682 ret = ctdb_statistics_reset(ctdb, options.pnn);
684 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
692 display remote ctdb rolling statistics
694 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
697 struct ctdb_statistics_list_old *stats;
698 int i, num_records = -1;
700 assert_single_node_only();
703 num_records = atoi(argv[0]) - 1;
706 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
708 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
711 for (i=0;i<stats->num;i++) {
712 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
715 show_statistics(&stats->stats[i], i==0);
716 if (i == num_records) {
725 display remote ctdb db statistics
727 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
729 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
730 struct ctdb_db_statistics_old *dbstat;
740 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
744 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
746 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
747 talloc_free(tmp_ctx);
751 printf("DB Statistics: %s\n", argv[0]);
752 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
753 dbstat->db_ro_delegations);
754 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
755 dbstat->db_ro_delegations);
756 printf(" %s\n", "locks");
757 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
758 dbstat->locks.num_calls);
759 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
760 dbstat->locks.num_failed);
761 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
762 dbstat->locks.num_current);
763 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
764 dbstat->locks.num_pending);
765 printf(" %s", "hop_count_buckets:");
766 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
767 printf(" %d", dbstat->hop_count_bucket[i]);
770 printf(" %s", "lock_buckets:");
771 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
772 printf(" %d", dbstat->locks.buckets[i]);
775 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
776 "locks_latency MIN/AVG/MAX",
777 dbstat->locks.latency.min,
778 (dbstat->locks.latency.num ?
779 dbstat->locks.latency.total /dbstat->locks.latency.num :
781 dbstat->locks.latency.max,
782 dbstat->locks.latency.num);
783 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
784 "vacuum_latency MIN/AVG/MAX",
785 dbstat->vacuum.latency.min,
786 (dbstat->vacuum.latency.num ?
787 dbstat->vacuum.latency.total /dbstat->vacuum.latency.num :
789 dbstat->vacuum.latency.max,
790 dbstat->vacuum.latency.num);
792 for (i=0; i<dbstat->num_hot_keys; i++) {
793 if (dbstat->hot_keys[i].count > 0) {
797 dbstat->num_hot_keys = num_hot_keys;
799 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
800 for (i = 0; i < dbstat->num_hot_keys; i++) {
802 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
803 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
804 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
809 talloc_free(tmp_ctx);
814 display uptime of remote node
816 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
819 struct ctdb_uptime *uptime = NULL;
820 int tmp, days, hours, minutes, seconds;
822 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
824 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
828 if (options.machinereadable){
829 printm(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
830 printm(":%u:%u:%u:%lf\n",
831 (unsigned int)uptime->current_time.tv_sec,
832 (unsigned int)uptime->ctdbd_start_time.tv_sec,
833 (unsigned int)uptime->last_recovery_finished.tv_sec,
834 timeval_delta(&uptime->last_recovery_finished,
835 &uptime->last_recovery_started)
840 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
842 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
850 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
852 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
860 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
862 printf("Duration of last recovery/failover: %lf seconds\n",
863 timeval_delta(&uptime->last_recovery_finished,
864 &uptime->last_recovery_started));
870 show the PNN of the current node
872 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
876 mypnn = getpnn(ctdb);
878 printf("PNN:%d\n", mypnn);
883 static struct ctdb_node_map_old *read_nodes_file(TALLOC_CTX *mem_ctx,
886 const char *nodes_list = NULL;
888 /* read the nodes file */
889 if (pnn != CTDB_UNKNOWN_PNN) {
892 t = talloc_asprintf(mem_ctx, "CTDB_NODES_%u", pnn);
894 nodes_list = getenv(t);
898 if (nodes_list == NULL) {
899 nodes_list = getenv("CTDB_NODES");
901 if (nodes_list == NULL) {
902 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
903 getenv("CTDB_BASE"));
904 if (nodes_list == NULL) {
905 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
910 return ctdb_read_nodes_file(mem_ctx, nodes_list);
914 show the PNN of the current node
915 discover the pnn by loading the nodes file and try to bind to all
916 addresses one at a time until the ip address is found.
918 static int find_node_xpnn(void)
920 TALLOC_CTX *mem_ctx = talloc_new(NULL);
921 struct ctdb_node_map_old *node_map;
924 node_map = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
925 if (node_map == NULL) {
926 talloc_free(mem_ctx);
930 for (i = 0; i < node_map->num; i++) {
931 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
934 if (ctdb_sys_have_ip(&node_map->nodes[i].addr)) {
935 pnn = node_map->nodes[i].pnn;
936 talloc_free(mem_ctx);
941 DEBUG(DEBUG_ERR, ("Failed to detect which PNN this node is\n"));
942 talloc_free(mem_ctx);
946 /* Helpers for ctdb status
948 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
950 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
954 if (node->flags == 0) {
955 struct ctdb_iface_list_old *ifaces;
957 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
958 tmp_ctx, &ifaces) == 0) {
959 for (j=0; j < ifaces->num; j++) {
960 if (ifaces->ifaces[j].link_state != 0) {
968 talloc_free(tmp_ctx);
973 static void control_status_header_machine(void)
975 printm(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
976 ":Inactive:PartiallyOnline:ThisNode:\n");
979 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
980 struct ctdb_node_and_flags *node)
982 printm(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
983 ctdb_addr_to_str(&node->addr),
984 !!(node->flags&NODE_FLAGS_DISCONNECTED),
985 !!(node->flags&NODE_FLAGS_BANNED),
986 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
987 !!(node->flags&NODE_FLAGS_UNHEALTHY),
988 !!(node->flags&NODE_FLAGS_STOPPED),
989 !!(node->flags&NODE_FLAGS_INACTIVE),
990 is_partially_online(ctdb, node) ? 1 : 0,
991 (node->pnn == mypnn)?'Y':'N');
996 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
997 struct ctdb_node_and_flags *node)
999 printf("pnn:%d %-16s %s%s\n", node->pnn,
1000 ctdb_addr_to_str(&node->addr),
1001 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
1002 node->pnn == mypnn?" (THIS NODE)":"");
1008 display remote ctdb status
1010 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
1012 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1014 struct ctdb_vnn_map *vnnmap=NULL;
1015 struct ctdb_node_map_old *nodemap=NULL;
1016 uint32_t recmode, recmaster, mypnn;
1017 int num_deleted_nodes = 0;
1020 mypnn = getpnn(ctdb);
1022 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1024 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1025 talloc_free(tmp_ctx);
1029 if (options.machinereadable) {
1030 control_status_header_machine();
1031 for (i=0;i<nodemap->num;i++) {
1032 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1035 (void) control_status_1_machine(ctdb, mypnn,
1036 &nodemap->nodes[i]);
1038 talloc_free(tmp_ctx);
1042 for (i=0; i<nodemap->num; i++) {
1043 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1044 num_deleted_nodes++;
1047 if (num_deleted_nodes == 0) {
1048 printf("Number of nodes:%d\n", nodemap->num);
1050 printf("Number of nodes:%d (including %d deleted nodes)\n",
1051 nodemap->num, num_deleted_nodes);
1053 for(i=0;i<nodemap->num;i++){
1054 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1057 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1060 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1062 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1063 talloc_free(tmp_ctx);
1066 if (vnnmap->generation == INVALID_GENERATION) {
1067 printf("Generation:INVALID\n");
1069 printf("Generation:%d\n",vnnmap->generation);
1071 printf("Size:%d\n",vnnmap->size);
1072 for(i=0;i<vnnmap->size;i++){
1073 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1076 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1078 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1079 talloc_free(tmp_ctx);
1082 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1084 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1086 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1087 talloc_free(tmp_ctx);
1090 printf("Recovery master:%d\n",recmaster);
1092 talloc_free(tmp_ctx);
1096 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1098 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1100 struct ctdb_node_map_old *nodemap=NULL;
1102 uint32_t pnn_mode, mypnn;
1108 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1109 options.pnn, true, &nodes, &pnn_mode)) {
1113 if (options.machinereadable) {
1114 control_status_header_machine();
1115 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1116 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1119 mypnn = getpnn(ctdb);
1121 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1123 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1124 talloc_free(tmp_ctx);
1130 for (i = 0; i < talloc_array_length(nodes); i++) {
1131 if (options.machinereadable) {
1132 ret |= control_status_1_machine(ctdb, mypnn,
1133 &nodemap->nodes[nodes[i]]);
1135 ret |= control_status_1_human(ctdb, mypnn,
1136 &nodemap->nodes[nodes[i]]);
1140 talloc_free(tmp_ctx);
1144 /* Display NAT gateway status */
1145 static int control_natgw(struct ctdb_context *ctdb, int argc, const char **argv)
1147 static char prog[PATH_MAX+1] = "";
1153 if (!ctdb_set_helper("NAT gateway helper", prog, sizeof(prog),
1154 "CTDB_NATGW_HELPER", CTDB_HELPER_BINDIR,
1156 DEBUG(DEBUG_ERR, ("Unable to set NAT gateway helper\n"));
1160 execl(prog, prog, argv[0], NULL);
1163 ("Unable to run NAT gateway helper %s\n", strerror(errno)));
1168 display the status of the scripts for monitoring (or other events)
1170 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1171 enum ctdb_event type)
1173 struct ctdb_script_list_old *script_status;
1176 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1178 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1182 if (script_status == NULL) {
1183 if (!options.machinereadable) {
1184 printf("%s cycle never run\n",
1185 ctdb_eventscript_call_names[type]);
1190 if (!options.machinereadable) {
1192 for (i=0; i<script_status->num_scripts; i++) {
1193 if (script_status->scripts[i].status != -ENOEXEC) {
1197 printf("%d scripts were executed last %s cycle\n",
1199 ctdb_eventscript_call_names[type]);
1201 for (i=0; i<script_status->num_scripts; i++) {
1202 const char *status = NULL;
1204 /* The ETIME status is ignored for certain events.
1205 * In that case the status is 0, but endtime is not set.
1207 if (script_status->scripts[i].status == 0 &&
1208 timeval_is_zero(&script_status->scripts[i].finished)) {
1209 script_status->scripts[i].status = -ETIME;
1212 switch (script_status->scripts[i].status) {
1214 status = "TIMEDOUT";
1217 status = "DISABLED";
1223 if (script_status->scripts[i].status > 0)
1227 if (options.machinereadable) {
1228 printm(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1229 ctdb_eventscript_call_names[type],
1230 script_status->scripts[i].name,
1231 script_status->scripts[i].status,
1233 (long)script_status->scripts[i].start.tv_sec,
1234 (long)script_status->scripts[i].start.tv_usec,
1235 (long)script_status->scripts[i].finished.tv_sec,
1236 (long)script_status->scripts[i].finished.tv_usec,
1237 script_status->scripts[i].output);
1241 printf("%-20s Status:%s ",
1242 script_status->scripts[i].name, status);
1244 /* Some other error, eg from stat. */
1245 printf("%-20s Status:CANNOT RUN (%s)",
1246 script_status->scripts[i].name,
1247 strerror(-script_status->scripts[i].status));
1249 if (script_status->scripts[i].status >= 0) {
1250 printf("Duration:%.3lf ",
1251 timeval_delta(&script_status->scripts[i].finished,
1252 &script_status->scripts[i].start));
1254 if (script_status->scripts[i].status != -ENOEXEC) {
1256 ctime(&script_status->scripts[i].start.tv_sec));
1257 if (script_status->scripts[i].status != 0) {
1258 printf(" OUTPUT:%s\n",
1259 script_status->scripts[i].output);
1269 static int control_scriptstatus(struct ctdb_context *ctdb,
1270 int argc, const char **argv)
1273 enum ctdb_event type, min, max;
1277 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1282 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1286 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1287 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1293 if (type == CTDB_EVENT_MAX) {
1294 if (strcmp(arg, "all") == 0) {
1296 max = CTDB_EVENT_MAX;
1298 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1303 if (options.machinereadable) {
1304 printm(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1307 for (type = min; type < max; type++) {
1308 ret = control_one_scriptstatus(ctdb, type);
1318 enable an eventscript
1320 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1328 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1330 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1338 disable an eventscript
1340 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1348 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1350 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1358 display the pnn of the recovery master
1360 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1365 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1367 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1370 printf("%d\n",recmaster);
1376 add a tickle to a public address
1378 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1380 struct ctdb_connection t;
1384 assert_single_node_only();
1390 if (parse_ip_port(argv[0], &t.src) == 0) {
1391 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1394 if (parse_ip_port(argv[1], &t.dst) == 0) {
1395 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1399 data.dptr = (uint8_t *)&t;
1400 data.dsize = sizeof(t);
1402 /* tell all nodes about this tcp connection */
1403 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1404 0, data, ctdb, NULL, NULL, NULL, NULL);
1406 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1415 delete a tickle from a node
1417 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1419 struct ctdb_connection t;
1423 assert_single_node_only();
1429 if (parse_ip_port(argv[0], &t.src) == 0) {
1430 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1433 if (parse_ip_port(argv[1], &t.dst) == 0) {
1434 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1438 data.dptr = (uint8_t *)&t;
1439 data.dsize = sizeof(t);
1441 /* tell all nodes about this tcp connection */
1442 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1443 0, data, ctdb, NULL, NULL, NULL, NULL);
1445 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1454 get a list of all tickles for this pnn
1456 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1458 struct ctdb_tickle_list_old *list;
1459 ctdb_sock_addr addr;
1463 assert_single_node_only();
1470 port = atoi(argv[1]);
1473 if (parse_ip(argv[0], NULL, port, &addr) == 0) {
1474 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1478 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1480 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1484 if (options.machinereadable){
1485 printm(":source ip:port:destination ip:port:\n");
1486 for (i=0;i<list->num;i++) {
1487 printm(":%s:%u", ctdb_addr_to_str(&list->connections[i].src), ntohs(list->connections[i].src.ip.sin_port));
1488 printm(":%s:%u:\n", ctdb_addr_to_str(&list->connections[i].dst), ntohs(list->connections[i].dst.ip.sin_port));
1491 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1492 printf("Num tickles:%u\n", list->num);
1493 for (i=0;i<list->num;i++) {
1494 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->connections[i].src), ntohs(list->connections[i].src.ip.sin_port));
1495 printf("DST: %s:%u\n", ctdb_addr_to_str(&list->connections[i].dst), ntohs(list->connections[i].dst.ip.sin_port));
1505 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1507 struct ctdb_public_ip_list_old *ips;
1508 struct ctdb_public_ip ip;
1511 uint32_t disable_time;
1513 struct ctdb_node_map_old *nodemap=NULL;
1514 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1517 data.dptr = (uint8_t*)&disable_time;
1518 data.dsize = sizeof(disable_time);
1519 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1521 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1527 /* read the public ip list from the node */
1528 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1530 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1531 talloc_free(tmp_ctx);
1535 for (i=0;i<ips->num;i++) {
1536 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1541 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1542 pnn, ctdb_addr_to_str(addr)));
1543 talloc_free(tmp_ctx);
1550 data.dptr = (uint8_t *)&ip;
1551 data.dsize = sizeof(ip);
1553 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1555 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1556 talloc_free(tmp_ctx);
1560 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1561 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1568 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1569 talloc_free(tmp_ctx);
1573 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1575 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1576 talloc_free(tmp_ctx);
1580 talloc_free(tmp_ctx);
1586 * scans all other nodes and returns a pnn for another node that can host this
1590 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1592 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1593 struct ctdb_public_ip_list_old *ips;
1594 struct ctdb_node_map_old *nodemap=NULL;
1598 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1600 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1601 talloc_free(tmp_ctx);
1605 for(i=0;i<nodemap->num;i++){
1606 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1609 if (nodemap->nodes[i].pnn == options.pnn) {
1613 /* read the public ip list from this node */
1614 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1616 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1620 for (j=0;j<ips->num;j++) {
1621 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1622 pnn = nodemap->nodes[i].pnn;
1623 talloc_free(tmp_ctx);
1630 talloc_free(tmp_ctx);
1634 /* If pnn is -1 then try to find a node to move IP to... */
1635 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1637 bool pnn_specified = (pnn == -1 ? false : true);
1640 while (retries < 5) {
1641 if (!pnn_specified) {
1642 pnn = find_other_host_for_public_ip(ctdb, addr);
1647 ("Trying to move public IP to node %u\n", pnn));
1650 if (move_ip(ctdb, addr, pnn) == 0) {
1663 move/failover an ip address to a specific node
1665 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1668 ctdb_sock_addr addr;
1670 assert_single_node_only();
1677 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1678 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1683 if (sscanf(argv[1], "%u", &pnn) != 1) {
1684 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1688 if (!try_moveip(ctdb, &addr, pnn)) {
1689 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1696 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1700 data.dptr = (uint8_t *)&pnn;
1701 data.dsize = sizeof(uint32_t);
1702 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1704 ("Failed to send message to force node %u to be a rebalancing target\n",
1713 static int getips_store_callback(void *param, void *data)
1715 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1716 struct ctdb_public_ip_list_old *ips = param;
1720 ips->ips[i].pnn = node_ip->pnn;
1721 ips->ips[i].addr = node_ip->addr;
1725 static int getips_count_callback(void *param, void *data)
1727 uint32_t *count = param;
1734 static uint32_t *ip_key(ctdb_sock_addr *ip)
1736 static uint32_t key[IP_KEYLEN];
1738 bzero(key, sizeof(key));
1740 switch (ip->sa.sa_family) {
1742 key[0] = ip->ip.sin_addr.s_addr;
1745 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1753 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1760 static void *add_ip_callback(void *parm, void *data)
1766 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_public_ip_list_old **ips)
1768 struct ctdb_public_ip_list_old *tmp_ips;
1769 struct ctdb_node_map_old *nodemap=NULL;
1770 trbt_tree_t *ip_tree;
1774 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1776 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1780 ip_tree = trbt_create(tmp_ctx, 0);
1782 for(i=0;i<nodemap->num;i++){
1783 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1786 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1790 /* read the public ip list from this node */
1791 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1793 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1797 for (j=0; j<tmp_ips->num;j++) {
1798 struct ctdb_public_ip *node_ip;
1800 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1801 node_ip->pnn = tmp_ips->ips[j].pnn;
1802 node_ip->addr = tmp_ips->ips[j].addr;
1804 trbt_insertarray32_callback(ip_tree,
1805 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1809 talloc_free(tmp_ips);
1814 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1816 len = offsetof(struct ctdb_public_ip_list_old, ips) +
1817 count*sizeof(struct ctdb_public_ip);
1818 tmp_ips = talloc_zero_size(tmp_ctx, len);
1819 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1827 static void ctdb_every_second(struct tevent_context *ev,
1828 struct tevent_timer *te,
1829 struct timeval t, void *p)
1831 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1833 tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
1834 ctdb_every_second, ctdb);
1837 struct srvid_reply_handler_data {
1841 const char *srvid_str;
1844 static void srvid_broadcast_reply_handler(uint64_t srvid, TDB_DATA data,
1847 struct srvid_reply_handler_data *d =
1848 (struct srvid_reply_handler_data *)private_data;
1852 if (data.dsize != sizeof(ret)) {
1853 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
1857 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
1858 ret = *(int32_t *)data.dptr;
1861 ("%s failed with result %d\n", d->srvid_str, ret));
1865 if (!d->wait_for_all) {
1870 /* Wait for all replies */
1872 for (i = 0; i < talloc_array_length(d->nodes); i++) {
1873 if (d->nodes[i] == ret) {
1875 ("%s reply received from node %u\n",
1876 d->srvid_str, ret));
1879 if (d->nodes[i] != -1) {
1880 /* Found a node that hasn't yet replied */
1886 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
1887 * or replies from all connected nodes. arg is the data argument to
1888 * pass in the srvid_request structure - pass 0 if this isn't needed.
1890 static int srvid_broadcast(struct ctdb_context *ctdb,
1891 uint64_t srvid, uint32_t *arg,
1892 const char *srvid_str, bool wait_for_all)
1897 uint64_t reply_srvid;
1898 struct ctdb_srvid_message request;
1899 struct ctdb_disable_message request_data;
1900 struct srvid_reply_handler_data reply_data;
1903 /* Time ticks to enable timeouts to be processed */
1904 tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
1905 ctdb_every_second, ctdb);
1907 pnn = ctdb_get_pnn(ctdb);
1908 reply_srvid = getpid();
1911 ZERO_STRUCT(request);
1914 request.srvid = reply_srvid;
1916 data.dptr = (uint8_t *)&request;
1917 data.dsize = sizeof(request);
1919 ZERO_STRUCT(request_data);
1921 request_data.pnn = pnn;
1922 request_data.srvid = reply_srvid;
1923 request_data.timeout = *arg;
1925 data.dptr = (uint8_t *)&request_data;
1926 data.dsize = sizeof(request_data);
1929 /* Register message port for reply from recovery master */
1930 ctdb_client_set_message_handler(ctdb, reply_srvid,
1931 srvid_broadcast_reply_handler,
1934 reply_data.wait_for_all = wait_for_all;
1935 reply_data.nodes = NULL;
1936 reply_data.srvid_str = srvid_str;
1939 reply_data.done = false;
1942 struct ctdb_node_map_old *nodemap;
1944 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
1945 CTDB_CURRENT_NODE, ctdb, &nodemap);
1948 ("Unable to get nodemap from current node, try again\n"));
1953 if (reply_data.nodes != NULL) {
1954 talloc_free(reply_data.nodes);
1956 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
1959 talloc_free(nodemap);
1962 /* Send to all connected nodes. Only recmaster replies */
1963 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
1966 /* This can only happen if the socket is closed and
1967 * there's no way to recover from that, so don't try
1971 ("Failed to send %s request to connected nodes\n",
1976 tv = timeval_current();
1977 /* This loop terminates the reply is received */
1978 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
1979 tevent_loop_once(ctdb->ev);
1982 if (!reply_data.done) {
1984 ("Still waiting for confirmation of %s\n", srvid_str));
1989 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
1991 talloc_free(reply_data.nodes);
1996 static int ipreallocate(struct ctdb_context *ctdb)
1998 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
1999 "IP reallocation", false);
2003 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2005 return ipreallocate(ctdb);
2009 add a public ip address to a node
2011 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2014 int len, retries = 0;
2016 ctdb_sock_addr addr;
2017 struct ctdb_addr_info_old *pub;
2018 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2019 struct ctdb_public_ip_list_old *ips;
2023 talloc_free(tmp_ctx);
2027 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2028 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2029 talloc_free(tmp_ctx);
2033 /* read the public ip list from the node */
2034 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2036 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2037 talloc_free(tmp_ctx);
2040 for (i=0;i<ips->num;i++) {
2041 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2042 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2049 /* Dont timeout. This command waits for an ip reallocation
2050 which sometimes can take wuite a while if there has
2051 been a recent recovery
2055 len = offsetof(struct ctdb_addr_info_old, iface) + strlen(argv[1]) + 1;
2056 pub = talloc_size(tmp_ctx, len);
2057 CTDB_NO_MEMORY(ctdb, pub);
2061 pub->len = strlen(argv[1])+1;
2062 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2065 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2067 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2071 } while (retries < 5 && ret != 0);
2073 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2074 talloc_free(tmp_ctx);
2078 if (rebalance_node(ctdb, options.pnn) != 0) {
2079 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2083 talloc_free(tmp_ctx);
2088 add a public ip address to a node
2090 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2092 ctdb_sock_addr addr;
2099 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2100 printf("Badly formed ip : %s\n", argv[0]);
2104 iface = ctdb_sys_find_ifname(&addr);
2105 if (iface == NULL) {
2106 printf("Failed to get interface name for ip: %s", argv[0]);
2110 printf("IP on interface %s\n", iface);
2117 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2119 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2121 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2122 struct ctdb_node_map_old *nodemap=NULL;
2123 struct ctdb_public_ip_list_old *ips;
2126 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2128 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2132 /* remove it from the nodes that are not hosting the ip currently */
2133 for(i=0;i<nodemap->num;i++){
2134 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2137 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2139 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2143 for (j=0;j<ips->num;j++) {
2144 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2152 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2156 options.pnn = nodemap->nodes[i].pnn;
2157 control_delip(ctdb, argc, argv);
2161 /* remove it from every node (also the one hosting it) */
2162 for(i=0;i<nodemap->num;i++){
2163 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2166 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2168 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2172 for (j=0;j<ips->num;j++) {
2173 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2181 options.pnn = nodemap->nodes[i].pnn;
2182 control_delip(ctdb, argc, argv);
2185 talloc_free(tmp_ctx);
2190 delete a public ip address from a node
2192 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2195 ctdb_sock_addr addr;
2196 struct ctdb_addr_info_old pub;
2197 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2198 struct ctdb_public_ip_list_old *ips;
2201 talloc_free(tmp_ctx);
2205 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2206 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2210 if (options.pnn == CTDB_BROADCAST_ALL) {
2211 return control_delip_all(ctdb, argc, argv, &addr);
2218 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2220 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2221 talloc_free(tmp_ctx);
2225 for (i=0;i<ips->num;i++) {
2226 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2232 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2233 ctdb_addr_to_str(&addr)));
2234 talloc_free(tmp_ctx);
2238 /* This is an optimisation. If this node is hosting the IP
2239 * then try to move it somewhere else without invoking a full
2240 * takeover run. We don't care if this doesn't work!
2242 if (ips->ips[i].pnn == options.pnn) {
2243 (void) try_moveip(ctdb, &addr, -1);
2246 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2248 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2249 talloc_free(tmp_ctx);
2253 talloc_free(tmp_ctx);
2260 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2263 ctdb_sock_addr addr;
2265 assert_single_node_only();
2271 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2272 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2276 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2278 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2286 check if a server id exists
2288 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2290 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2296 talloc_free(tmp_ctx);
2300 ids = talloc_array(tmp_ctx, uint64_t, argc);
2301 result = talloc_array(tmp_ctx, uint8_t, argc);
2303 for (i = 0; i < argc; i++) {
2304 ids[i] = strtoull(argv[i], NULL, 0);
2307 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2308 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2310 talloc_free(tmp_ctx);
2314 for (i=0; i < argc; i++) {
2315 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2316 result[i] ? "exists" : "does not exist");
2319 talloc_free(tmp_ctx);
2324 send a tcp tickle ack
2326 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2329 ctdb_sock_addr src, dst;
2335 if (!parse_ip_port(argv[0], &src)) {
2336 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2340 if (!parse_ip_port(argv[1], &dst)) {
2341 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2345 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2349 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2356 display public ip status
2358 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2361 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2362 struct ctdb_public_ip_list_old *ips;
2364 if (argc == 1 && strcmp(argv[0], "all") == 0) {
2365 options.pnn = CTDB_BROADCAST_ALL;
2368 if (options.pnn == CTDB_BROADCAST_ALL) {
2369 /* read the list of public ips from all nodes */
2370 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2372 /* read the public ip list from this node */
2373 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2376 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2377 talloc_free(tmp_ctx);
2381 if (options.machinereadable){
2382 printm(":Public IP:Node:");
2383 if (options.verbose){
2384 printm("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2388 if (options.pnn == CTDB_BROADCAST_ALL) {
2389 printf("Public IPs on ALL nodes\n");
2391 printf("Public IPs on node %u\n", options.pnn);
2395 for (i=1;i<=ips->num;i++) {
2396 struct ctdb_public_ip_info_old *info = NULL;
2398 char *aciface = NULL;
2399 char *avifaces = NULL;
2400 char *cifaces = NULL;
2402 if (options.pnn == CTDB_BROADCAST_ALL) {
2403 pnn = ips->ips[ips->num-i].pnn;
2409 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2410 &ips->ips[ips->num-i].addr, &info);
2417 for (j=0; j < info->num; j++) {
2418 if (cifaces == NULL) {
2419 cifaces = talloc_strdup(info,
2420 info->ifaces[j].name);
2422 cifaces = talloc_asprintf_append(cifaces,
2424 info->ifaces[j].name);
2427 if (info->active_idx == j) {
2428 aciface = info->ifaces[j].name;
2431 if (info->ifaces[j].link_state == 0) {
2435 if (avifaces == NULL) {
2436 avifaces = talloc_strdup(info, info->ifaces[j].name);
2438 avifaces = talloc_asprintf_append(avifaces,
2440 info->ifaces[j].name);
2445 if (options.machinereadable){
2447 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2448 ips->ips[ips->num-i].pnn);
2449 if (options.verbose){
2452 avifaces?avifaces:"",
2453 cifaces?cifaces:"");
2457 if (options.verbose) {
2458 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2459 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2460 ips->ips[ips->num-i].pnn,
2462 avifaces?avifaces:"",
2463 cifaces?cifaces:"");
2466 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2467 ips->ips[ips->num-i].pnn);
2473 talloc_free(tmp_ctx);
2480 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2483 ctdb_sock_addr addr;
2484 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2485 struct ctdb_public_ip_info_old *info;
2488 talloc_free(tmp_ctx);
2492 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2493 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2497 /* read the public ip info from this node */
2498 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2499 tmp_ctx, &addr, &info);
2501 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2502 argv[0], options.pnn));
2503 talloc_free(tmp_ctx);
2507 printf("Public IP[%s] info on node %u\n",
2508 ctdb_addr_to_str(&info->ip.addr),
2511 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2512 ctdb_addr_to_str(&info->ip.addr),
2513 info->ip.pnn, info->num);
2515 for (i=0; i<info->num; i++) {
2516 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2518 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2519 i+1, info->ifaces[i].name,
2520 info->ifaces[i].link_state?"up":"down",
2521 (unsigned int)info->ifaces[i].references,
2522 (i==info->active_idx)?" (active)":"");
2525 talloc_free(tmp_ctx);
2530 display interfaces status
2532 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2534 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2536 struct ctdb_iface_list_old *ifaces;
2539 /* read the public ip list from this node */
2540 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
2542 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2544 talloc_free(tmp_ctx);
2548 if (options.machinereadable){
2549 printm(":Name:LinkStatus:References:\n");
2551 printf("Interfaces on node %u\n", options.pnn);
2554 for (i=0; i<ifaces->num; i++) {
2555 if (options.machinereadable){
2556 printm(":%s:%s:%u:\n",
2557 ifaces->ifaces[i].name,
2558 ifaces->ifaces[i].link_state?"1":"0",
2559 (unsigned int)ifaces->ifaces[i].references);
2561 printf("name:%s link:%s references:%u\n",
2562 ifaces->ifaces[i].name,
2563 ifaces->ifaces[i].link_state?"up":"down",
2564 (unsigned int)ifaces->ifaces[i].references);
2568 talloc_free(tmp_ctx);
2574 set link status of an interface
2576 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2579 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2580 struct ctdb_iface info;
2588 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2589 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2591 talloc_free(tmp_ctx);
2594 strncpy(info.name, argv[0], sizeof(info.name)-1);
2595 info.name[sizeof(info.name)-1] = '\0';
2597 if (strcmp(argv[1], "up") == 0) {
2598 info.link_state = 1;
2599 } else if (strcmp(argv[1], "down") == 0) {
2600 info.link_state = 0;
2602 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2604 talloc_free(tmp_ctx);
2608 /* read the public ip list from this node */
2609 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2612 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2613 argv[0], options.pnn));
2614 talloc_free(tmp_ctx);
2618 talloc_free(tmp_ctx);
2623 display pid of a ctdb daemon
2625 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2630 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2632 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2635 printf("Pid:%d\n", pid);
2640 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
2642 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
2644 update_flags_handler_t handler,
2649 struct ctdb_node_map_old *nodemap = NULL;
2653 /* Check if the node is already in the desired state */
2654 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2656 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2659 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
2660 if (set_flag == flag_is_set) {
2661 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
2662 (set_flag ? "already" : "not"), desc));
2667 if (!handler(ctdb, data)) {
2668 DEBUG(DEBUG_WARNING,
2669 ("Failed to send control to set state %s on node %u, try again\n",
2670 desc, options.pnn));
2675 /* Read the nodemap and verify the change took effect.
2676 * Even if the above control/hanlder timed out then it
2677 * could still have worked!
2679 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
2682 DEBUG(DEBUG_WARNING,
2683 ("Unable to get nodemap from local node, try again\n"));
2685 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
2686 } while (nodemap == NULL || (set_flag != flag_is_set));
2688 return ipreallocate(ctdb);
2691 /* Administratively disable a node */
2692 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
2696 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
2697 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2701 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2703 return update_flags_and_ipreallocate(ctdb, NULL,
2704 update_flags_disabled,
2705 NODE_FLAGS_PERMANENTLY_DISABLED,
2707 true /* set_flag*/);
2710 /* Administratively re-enable a node */
2711 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
2715 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
2716 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2720 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2722 return update_flags_and_ipreallocate(ctdb, NULL,
2723 update_flags_not_disabled,
2724 NODE_FLAGS_PERMANENTLY_DISABLED,
2726 false /* set_flag*/);
2730 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
2734 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2739 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2741 return update_flags_and_ipreallocate(ctdb, NULL,
2742 update_flags_stopped,
2745 true /* set_flag*/);
2748 /* Continue a stopped node */
2749 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
2753 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2758 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2760 return update_flags_and_ipreallocate(ctdb, NULL,
2761 update_flags_not_stopped,
2764 false /* set_flag */);
2767 static uint32_t get_generation(struct ctdb_context *ctdb)
2769 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2770 struct ctdb_vnn_map *vnnmap=NULL;
2772 uint32_t generation;
2774 /* wait until the recmaster is not in recovery mode */
2776 uint32_t recmode, recmaster;
2778 if (vnnmap != NULL) {
2779 talloc_free(vnnmap);
2783 /* get the recmaster */
2784 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
2786 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2787 talloc_free(tmp_ctx);
2791 /* get recovery mode */
2792 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
2794 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2795 talloc_free(tmp_ctx);
2799 /* get the current generation number */
2800 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
2802 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2803 talloc_free(tmp_ctx);
2807 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
2808 generation = vnnmap->generation;
2809 talloc_free(tmp_ctx);
2817 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
2819 struct ctdb_ban_state *bantime = (struct ctdb_ban_state *)data;
2822 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
2827 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2829 struct ctdb_ban_state bantime;
2835 bantime.pnn = options.pnn;
2836 bantime.time = strtoul(argv[0], NULL, 0);
2838 if (bantime.time == 0) {
2839 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
2843 return update_flags_and_ipreallocate(ctdb, &bantime,
2844 update_state_banned,
2847 true /* set_flag*/);
2852 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2854 struct ctdb_ban_state bantime;
2856 bantime.pnn = options.pnn;
2859 return update_flags_and_ipreallocate(ctdb, &bantime,
2860 update_state_banned,
2863 false /* set_flag*/);
2867 show ban information for a node
2869 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2872 struct ctdb_node_map_old *nodemap=NULL;
2873 struct ctdb_ban_state *bantime;
2875 /* verify the node exists */
2876 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2878 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2882 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2884 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2888 if (bantime->time == 0) {
2889 printf("Node %u is not banned\n", bantime->pnn);
2891 printf("Node %u is banned, %d seconds remaining\n",
2892 bantime->pnn, bantime->time);
2901 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2905 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2907 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2917 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2920 uint32_t generation, next_generation;
2922 /* record the current generation number */
2923 generation = get_generation(ctdb);
2925 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2927 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2931 /* wait until we are in a new generation */
2933 next_generation = get_generation(ctdb);
2934 if (next_generation != generation) {
2945 display monitoring mode of a remote node
2947 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2952 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2954 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2957 if (!options.machinereadable){
2958 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2961 printm(":%d:\n",monmode);
2968 display capabilities of a remote node
2970 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2972 uint32_t capabilities;
2975 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2977 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2981 if (!options.machinereadable){
2982 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2983 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2985 printm(":RECMASTER:LMASTER:\n");
2987 !!(capabilities&CTDB_CAP_RECMASTER),
2988 !!(capabilities&CTDB_CAP_LMASTER));
2993 /* Display LVS status */
2994 static int control_lvs(struct ctdb_context *ctdb,
2995 int argc, const char **argv)
2997 static char prog[PATH_MAX+1] = "";
3003 if (!ctdb_set_helper("LVS helper", prog, sizeof(prog),
3004 "CTDB_LVS_HELPER", CTDB_HELPER_BINDIR,
3006 DEBUG(DEBUG_ERR, ("Unable to set LVS helper\n"));
3010 execl(prog, prog, argv[0], NULL);
3013 ("Unable to run LVS helper %s\n", strerror(errno)));
3018 disable monitoring on a node
3020 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3025 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3027 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3030 printf("Monitoring mode:%s\n","DISABLED");
3036 enable monitoring on a node
3038 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3043 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3045 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3048 printf("Monitoring mode:%s\n","ACTIVE");
3054 display remote list of keys/data for a db
3056 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3058 const char *db_name;
3059 struct ctdb_db_context *ctdb_db;
3061 struct ctdb_dump_db_context c;
3068 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3072 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3073 if (ctdb_db == NULL) {
3074 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3078 if (options.printlmaster) {
3079 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3080 ctdb, &ctdb->vnn_map);
3082 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3091 c.printemptyrecords = (bool)options.printemptyrecords;
3092 c.printdatasize = (bool)options.printdatasize;
3093 c.printlmaster = (bool)options.printlmaster;
3094 c.printhash = (bool)options.printhash;
3095 c.printrecordflags = (bool)options.printrecordflags;
3097 /* traverse and dump the cluster tdb */
3098 ret = ctdb_dump_db(ctdb_db, &c);
3100 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3101 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3102 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3106 talloc_free(ctdb_db);
3108 printf("Dumped %d records\n", ret);
3112 struct cattdb_data {
3113 struct ctdb_context *ctdb;
3117 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3119 struct cattdb_data *d = private_data;
3120 struct ctdb_dump_db_context c;
3127 c.printemptyrecords = (bool)options.printemptyrecords;
3128 c.printdatasize = (bool)options.printdatasize;
3129 c.printlmaster = false;
3130 c.printhash = (bool)options.printhash;
3131 c.printrecordflags = true;
3133 return ctdb_dumpdb_record(key, data, &c);
3137 cat the local tdb database using same format as catdb
3139 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3141 const char *db_name;
3142 struct ctdb_db_context *ctdb_db;
3143 struct cattdb_data d;
3150 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3154 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3155 if (ctdb_db == NULL) {
3156 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3160 /* traverse the local tdb */
3163 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3164 printf("Failed to cattdb data\n");
3167 talloc_free(ctdb_db);
3169 printf("Dumped %d records\n", d.count);
3174 display the content of a database key
3176 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3178 const char *db_name;
3179 struct ctdb_db_context *ctdb_db;
3180 struct ctdb_record_handle *h;
3181 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3189 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3193 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3194 if (ctdb_db == NULL) {
3195 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3199 key.dptr = discard_const(argv[1]);
3200 key.dsize = strlen((char *)key.dptr);
3202 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3204 printf("Failed to fetch record '%s' on node %d\n",
3205 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3206 talloc_free(tmp_ctx);
3210 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3212 talloc_free(tmp_ctx);
3213 talloc_free(ctdb_db);
3218 display the content of a database key
3220 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3222 const char *db_name;
3223 struct ctdb_db_context *ctdb_db;
3224 struct ctdb_record_handle *h;
3225 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3233 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3237 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3238 if (ctdb_db == NULL) {
3239 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3243 key.dptr = discard_const(argv[1]);
3244 key.dsize = strlen((char *)key.dptr);
3246 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3248 printf("Failed to fetch record '%s' on node %d\n",
3249 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3250 talloc_free(tmp_ctx);
3254 data.dptr = discard_const(argv[2]);
3255 data.dsize = strlen((char *)data.dptr);
3257 if (ctdb_record_store(h, data) != 0) {
3258 printf("Failed to store record\n");
3262 talloc_free(tmp_ctx);
3263 talloc_free(ctdb_db);
3268 fetch a record from a persistent database
3270 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3272 const char *db_name;
3273 struct ctdb_db_context *ctdb_db;
3274 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3275 struct ctdb_transaction_handle *h;
3282 talloc_free(tmp_ctx);
3286 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3287 talloc_free(tmp_ctx);
3291 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3293 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3294 talloc_free(tmp_ctx);
3298 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3299 if (ctdb_db == NULL) {
3300 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3301 talloc_free(tmp_ctx);
3305 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3307 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3308 talloc_free(tmp_ctx);
3312 key.dptr = discard_const(argv[1]);
3313 key.dsize = strlen(argv[1]);
3314 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3316 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3317 talloc_free(tmp_ctx);
3321 if (data.dsize == 0 || data.dptr == NULL) {
3322 DEBUG(DEBUG_ERR,("Record is empty\n"));
3323 talloc_free(tmp_ctx);
3328 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3330 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3331 talloc_free(tmp_ctx);
3334 sys_write(fd, data.dptr, data.dsize);
3337 sys_write(1, data.dptr, data.dsize);
3340 /* abort the transaction */
3344 talloc_free(tmp_ctx);
3349 fetch a record from a tdb-file
3351 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3353 const char *tdb_file;
3356 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3365 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3367 printf("Failed to open TDB file %s\n", tdb_file);
3371 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
3372 if (key.dptr == NULL) {
3373 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3377 data = tdb_fetch(tdb, key);
3378 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3379 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3387 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3389 printf("Failed to open output file %s\n", argv[2]);
3392 if (options.verbose){
3393 sys_write(fd, data.dptr, data.dsize);
3395 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3399 if (options.verbose){
3400 sys_write(1, data.dptr, data.dsize);
3402 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3406 talloc_free(tmp_ctx);
3411 store a record and header to a tdb-file
3413 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
3415 const char *tdb_file;
3417 TDB_DATA key, value, data;
3418 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3419 struct ctdb_ltdb_header header;
3427 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
3429 printf("Failed to open TDB file %s\n", tdb_file);
3433 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
3434 if (key.dptr == NULL) {
3435 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3439 value = strtodata(tmp_ctx, argv[2], strlen(argv[2]));
3440 if (value.dptr == NULL) {
3441 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
3445 ZERO_STRUCT(header);
3447 header.rsn = atoll(argv[3]);
3450 header.dmaster = atoi(argv[4]);
3453 header.flags = atoi(argv[5]);
3456 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
3457 data.dptr = talloc_size(tmp_ctx, data.dsize);
3458 if (data.dptr == NULL) {
3459 printf("Failed to allocate header+value\n");
3463 *(struct ctdb_ltdb_header *)data.dptr = header;
3464 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
3466 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
3467 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
3474 talloc_free(tmp_ctx);
3479 write a record to a persistent database
3481 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3483 const char *db_name;
3484 struct ctdb_db_context *ctdb_db;
3485 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3486 struct ctdb_transaction_handle *h;
3492 talloc_free(tmp_ctx);
3496 fd = open(argv[2], O_RDONLY);
3498 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3499 talloc_free(tmp_ctx);
3503 ret = fstat(fd, &st);
3505 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3507 talloc_free(tmp_ctx);
3511 if (!S_ISREG(st.st_mode)) {
3512 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3514 talloc_free(tmp_ctx);
3518 data.dsize = st.st_size;
3519 if (data.dsize == 0) {
3522 data.dptr = talloc_size(tmp_ctx, data.dsize);
3523 if (data.dptr == NULL) {
3524 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3526 talloc_free(tmp_ctx);
3529 ret = sys_read(fd, data.dptr, data.dsize);
3530 if (ret != data.dsize) {
3531 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3533 talloc_free(tmp_ctx);
3542 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3543 if (ctdb_db == NULL) {
3544 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3545 talloc_free(tmp_ctx);
3549 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3551 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3552 talloc_free(tmp_ctx);
3556 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
3557 if (key.dptr == NULL) {
3558 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3562 ret = ctdb_transaction_store(h, key, data);
3564 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3565 talloc_free(tmp_ctx);
3569 ret = ctdb_transaction_commit(h);
3571 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3572 talloc_free(tmp_ctx);
3577 talloc_free(tmp_ctx);
3582 * delete a record from a persistent database
3584 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
3586 const char *db_name;
3587 struct ctdb_db_context *ctdb_db;
3588 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3589 struct ctdb_transaction_handle *h;
3596 talloc_free(tmp_ctx);
3600 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3601 talloc_free(tmp_ctx);
3605 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3607 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
3608 talloc_free(tmp_ctx);
3612 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3613 if (ctdb_db == NULL) {
3614 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
3615 talloc_free(tmp_ctx);
3619 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3621 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
3622 talloc_free(tmp_ctx);
3626 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
3627 if (key.dptr == NULL) {
3628 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3632 ret = ctdb_transaction_store(h, key, tdb_null);
3634 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
3635 talloc_free(tmp_ctx);
3639 ret = ctdb_transaction_commit(h);
3641 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
3642 talloc_free(tmp_ctx);
3646 talloc_free(tmp_ctx);
3650 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
3655 const char *ret; /* Next byte after successfully parsed value */
3657 /* Error, unless someone says otherwise */
3659 /* Indicates no value to parse */
3662 /* Skip whitespace */
3663 n = strspn(s, " \t");
3667 /* Quoted ASCII string - no wide characters! */
3669 n = strcspn(t, "\"");
3672 *data = strtodata(mem_ctx, t, n);
3673 CTDB_NOMEM_ABORT(data->dptr);
3677 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
3680 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
3686 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
3687 TDB_DATA *key, TDB_DATA *value)
3689 char line [1024]; /* FIXME: make this more flexible? */
3693 ptr = fgets(line, sizeof(line), file);
3700 t = ptrans_parse_string(mem_ctx, line, key);
3701 if (t == NULL || key->dptr == NULL) {
3702 /* Line Ignored but not EOF */
3707 t = ptrans_parse_string(mem_ctx, t, value);
3709 /* Line Ignored but not EOF */
3710 talloc_free(key->dptr);
3719 * Update a persistent database as per file/stdin
3721 static int control_ptrans(struct ctdb_context *ctdb,
3722 int argc, const char **argv)
3724 const char *db_name;
3725 struct ctdb_db_context *ctdb_db;
3726 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3727 struct ctdb_transaction_handle *h;
3728 TDB_DATA key, value;
3733 talloc_free(tmp_ctx);
3739 file = fopen(argv[1], "r");
3741 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
3742 talloc_free(tmp_ctx);
3749 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3750 if (ctdb_db == NULL) {
3751 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3755 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3757 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3761 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
3762 if (key.dsize != 0) {
3763 ret = ctdb_transaction_store(h, key, value);
3764 /* Minimise memory use */
3765 talloc_free(key.dptr);
3766 if (value.dptr != NULL) {
3767 talloc_free(value.dptr);
3770 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3771 ctdb_transaction_cancel(h);
3777 ret = ctdb_transaction_commit(h);
3779 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3783 if (file != stdin) {
3786 talloc_free(tmp_ctx);
3790 if (file != stdin) {
3794 talloc_free(tmp_ctx);
3799 check if a service is bound to a port or not
3801 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
3806 struct sockaddr_in sin;
3809 printf("Use: ctdb chktcport <port>\n");
3813 port = atoi(argv[0]);
3815 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
3817 printf("Failed to open local socket\n");
3821 v = fcntl(s, F_GETFL, 0);
3822 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
3823 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
3826 bzero(&sin, sizeof(sin));
3827 sin.sin_family = AF_INET;
3828 sin.sin_port = htons(port);
3829 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
3832 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
3840 /* Reload public IPs on a specified nodes */
3841 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
3843 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3849 assert_single_node_only();
3855 /* Determine the nodes where IPs need to be reloaded */
3856 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
3857 options.pnn, true, &nodes, &pnn_mode)) {
3863 /* Disable takeover runs on all connected nodes. A reply
3864 * indicating success is needed from each node so all nodes
3865 * will need to be active. This will retry until maxruntime
3866 * is exceeded, hence no error handling.
3868 * A check could be added to not allow reloading of IPs when
3869 * there are disconnected nodes. However, this should
3870 * probably be left up to the administrator.
3872 timeout = LONGTIMEOUT;
3873 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
3874 "Disable takeover runs", true);
3876 /* Now tell all the desired nodes to reload their public IPs.
3877 * Keep trying this until it succeeds. This assumes all
3878 * failures are transient, which might not be true...
3880 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
3881 nodes, 0, LONGTIMELIMIT(),
3883 NULL, NULL, NULL) != 0) {
3885 ("Unable to reload IPs on some nodes, try again.\n"));
3889 /* It isn't strictly necessary to wait until takeover runs are
3890 * re-enabled but doing so can't hurt.
3893 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
3894 "Enable takeover runs", true);
3900 talloc_free(tmp_ctx);
3905 display a list of the databases on a remote ctdb
3907 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3910 struct ctdb_dbid_map_old *dbmap=NULL;
3912 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3914 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3918 if(options.machinereadable){
3919 printm(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
3920 for(i=0;i<dbmap->num;i++){
3921 const char *path = NULL;
3922 const char *name = NULL;
3923 const char *health = NULL;
3928 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3929 dbmap->dbs[i].db_id, ctdb, &path);
3930 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3931 dbmap->dbs[i].db_id, ctdb, &name);
3932 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3933 dbmap->dbs[i].db_id, ctdb, &health);
3934 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3935 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3936 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
3937 printm(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
3938 dbmap->dbs[i].db_id, name, path,
3939 !!(persistent), !!(sticky),
3940 !!(health), !!(readonly));
3945 printf("Number of databases:%d\n", dbmap->num);
3946 for(i=0;i<dbmap->num;i++){
3947 const char *path = NULL;
3948 const char *name = NULL;
3949 const char *health = NULL;
3954 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &path);
3955 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &name);
3956 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &health);
3957 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3958 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3959 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
3960 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
3961 dbmap->dbs[i].db_id, name, path,
3962 persistent?" PERSISTENT":"",
3963 sticky?" STICKY":"",
3964 readonly?" READONLY":"",
3965 health?" UNHEALTHY":"");
3972 display the status of a database on a remote ctdb
3974 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3976 const char *db_name;
3979 const char *path = NULL;
3980 const char *health = NULL;
3986 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
3990 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
3991 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
3992 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
3993 db_id, db_name, path,
3994 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
3995 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
3996 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
3997 (health ? health : "OK"));
4003 check if the local node is recmaster or not
4004 it will return 1 if this node is the recmaster and 0 if it is not
4005 or if the local ctdb daemon could not be contacted
4007 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4009 uint32_t mypnn, recmaster;
4012 assert_single_node_only();
4014 mypnn = getpnn(ctdb);
4016 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4018 printf("Failed to get the recmaster\n");
4022 if (recmaster != mypnn) {
4023 printf("this node is not the recmaster\n");
4027 printf("this node is the recmaster\n");
4034 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4037 struct timeval tv = timeval_current();
4038 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4040 printf("Unable to get ping response from node %u\n", options.pnn);
4043 printf("response from %u time=%.6f sec (%d clients)\n",
4044 options.pnn, timeval_elapsed(&tv), ret);
4051 get a node's runstate
4053 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4056 enum ctdb_runstate runstate;
4058 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4060 printf("Unable to get runstate response from node %u\n",
4065 enum ctdb_runstate t;
4067 for (i=0; i<argc; i++) {
4069 t = runstate_from_string(argv[i]);
4070 if (t == CTDB_RUNSTATE_UNKNOWN) {
4071 printf("Invalid run state (%s)\n", argv[i]);
4075 if (t == runstate) {
4082 printf("CTDB not in required run state (got %s)\n",
4083 runstate_to_string((enum ctdb_runstate)runstate));
4088 printf("%s\n", runstate_to_string(runstate));
4096 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4107 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4109 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4113 printf("%-23s = %u\n", name, value);
4120 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4131 value = strtoul(argv[1], NULL, 0);
4133 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4135 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4139 DEBUG(DEBUG_WARNING,
4140 ("Setting obsolete tunable variable '%s'\n",
4149 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4155 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4157 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4161 for (i=0;i<count;i++) {
4162 control_getvar(ctdb, 1, &list[i]);
4171 display debug level on a node
4173 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4178 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4180 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4183 enum debug_level log_level = debug_level_from_int(level);
4184 const char *desc = debug_level_to_string(log_level);
4186 /* This should never happen */
4189 if (options.machinereadable){
4190 printm(":Name:Level:\n");
4191 printm(":%s:%d:\n", desc, level);
4193 printf("Node %u is at debug level %s (%d)\n",
4194 options.pnn, desc, level);
4201 display reclock file of a node
4203 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4206 const char *reclock;
4208 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4210 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4213 if (reclock != NULL) {
4214 printf("%s\n", reclock);
4221 set the lmaster role on/off
4223 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4226 uint32_t lmasterrole = 0;
4232 if (!strcmp(argv[0], "on")) {
4234 } else if (!strcmp(argv[0], "off")) {
4240 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4242 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4250 set the recmaster role on/off
4252 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4255 uint32_t recmasterrole = 0;
4261 if (!strcmp(argv[0], "on")) {
4263 } else if (!strcmp(argv[0], "off")) {
4269 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4271 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4279 set debug level on a node or all nodes
4281 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4285 enum debug_level log_level;
4288 printf("You must specify the debug level. Valid levels are:\n");
4289 printf("\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
4293 if (!debug_level_parse(argv[0], &log_level)) {
4294 printf("Invalid debug level, must be one of\n");
4295 printf("\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
4299 level = debug_level_to_int(log_level);
4301 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4303 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4309 attach to a database
4311 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4313 const char *db_name;
4314 struct ctdb_db_context *ctdb_db;
4315 bool persistent = false;
4325 if (strcmp(argv[1], "persistent") != 0) {
4331 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4332 if (ctdb_db == NULL) {
4333 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4341 * detach from a database
4343 static int control_detach(struct ctdb_context *ctdb, int argc,
4348 int ret, i, status = 0;
4349 struct ctdb_node_map_old *nodemap = NULL;
4350 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4357 assert_single_node_only();
4359 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
4362 DEBUG(DEBUG_ERR, ("Database cannot be detached "
4363 "when recovery is active\n"));
4364 talloc_free(tmp_ctx);
4368 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4371 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4373 talloc_free(tmp_ctx);
4377 for (i=0; i<nodemap->num; i++) {
4380 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
4384 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
4388 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
4389 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
4390 "inactive (stopped or banned) node "
4391 "%u\n", nodemap->nodes[i].pnn));
4392 talloc_free(tmp_ctx);
4396 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
4397 nodemap->nodes[i].pnn,
4398 "AllowClientDBAttach",
4401 DEBUG(DEBUG_ERR, ("Unable to get tunable "
4402 "AllowClientDBAttach from node %u\n",
4403 nodemap->nodes[i].pnn));
4404 talloc_free(tmp_ctx);
4409 DEBUG(DEBUG_ERR, ("Database access is still active on "
4410 "node %u. Set AllowClientDBAttach=0 "
4412 nodemap->nodes[i].pnn));
4413 talloc_free(tmp_ctx);
4418 talloc_free(tmp_ctx);
4420 for (i=0; i<argc; i++) {
4421 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
4425 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
4426 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
4427 "cannot be detached\n", argv[i]));
4432 ret = ctdb_detach(ctdb, db_id);
4434 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
4444 set the sticky records capability for a database
4446 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
4448 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4456 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
4460 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
4462 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
4463 talloc_free(tmp_ctx);
4467 talloc_free(tmp_ctx);
4472 set the readonly capability for a database
4474 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4476 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4484 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
4488 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4490 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4491 talloc_free(tmp_ctx);
4495 talloc_free(tmp_ctx);
4502 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4512 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
4516 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
4518 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
4522 printf("Sequence number:%lld\n", (long long)seqnum);
4528 run an eventscript on a node
4530 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
4536 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4539 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4543 data.dptr = (unsigned char *)discard_const(argv[0]);
4544 data.dsize = strlen((char *)data.dptr) + 1;
4546 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
4548 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
4549 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4550 if (ret != 0 || res != 0) {
4551 if (errmsg != NULL) {
4553 ("Failed to run eventscripts - %s\n", errmsg));
4555 DEBUG(DEBUG_ERR, ("Failed to run eventscripts\n"));
4557 talloc_free(tmp_ctx);
4560 talloc_free(tmp_ctx);
4564 #define DB_VERSION 1
4565 #define MAX_DB_NAME 64
4566 struct db_file_header {
4567 unsigned long version;
4569 unsigned long persistent;
4571 const char name[MAX_DB_NAME];
4574 struct backup_data {
4575 struct ctdb_marshall_buffer *records;
4578 bool traverse_error;
4581 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
4583 struct backup_data *bd = talloc_get_type(private, struct backup_data);
4584 struct ctdb_rec_data_old *rec;
4586 /* add the record */
4587 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
4589 bd->traverse_error = true;
4590 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
4593 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
4594 if (bd->records == NULL) {
4595 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
4596 bd->traverse_error = true;
4599 bd->records->count++;
4600 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
4601 bd->len += rec->length;
4609 * backup a database to a file
4611 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
4613 const char *db_name;
4615 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4616 struct db_file_header dbhdr;
4617 struct ctdb_db_context *ctdb_db;
4618 struct backup_data *bd;
4621 const char *reason = NULL;
4625 assert_single_node_only();
4628 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4632 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4636 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4637 db_id, tmp_ctx, &reason);
4639 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4641 talloc_free(tmp_ctx);
4645 uint32_t allow_unhealthy = 0;
4647 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4648 "AllowUnhealthyDBRead",
4651 if (allow_unhealthy != 1) {
4652 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4655 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
4657 talloc_free(tmp_ctx);
4661 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4663 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4664 "tunnable AllowUnhealthyDBRead = %u\n",
4668 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4669 if (ctdb_db == NULL) {
4670 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4671 talloc_free(tmp_ctx);
4676 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4678 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4679 talloc_free(tmp_ctx);
4684 bd = talloc_zero(tmp_ctx, struct backup_data);
4686 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4687 talloc_free(tmp_ctx);
4691 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4692 if (bd->records == NULL) {
4693 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4694 talloc_free(tmp_ctx);
4698 bd->len = offsetof(struct ctdb_marshall_buffer, data);
4699 bd->records->db_id = ctdb_db->db_id;
4700 /* traverse the database collecting all records */
4701 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4702 bd->traverse_error) {
4703 DEBUG(DEBUG_ERR,("Traverse error\n"));
4704 talloc_free(tmp_ctx);
4708 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4711 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4713 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4714 talloc_free(tmp_ctx);
4719 dbhdr.version = DB_VERSION;
4720 dbhdr.timestamp = time(NULL);
4721 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4722 dbhdr.size = bd->len;
4723 if (strlen(argv[0]) >= MAX_DB_NAME) {
4724 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4727 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
4728 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
4730 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4733 ret = sys_write(fh, bd->records, bd->len);
4735 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4744 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4748 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
4750 talloc_free(tmp_ctx);
4755 * restore a database from a file
4757 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4760 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4763 struct db_file_header dbhdr;
4764 struct ctdb_db_context *ctdb_db;
4765 struct ctdb_node_map_old *nodemap=NULL;
4766 struct ctdb_vnn_map *vnnmap=NULL;
4768 struct ctdb_transdb w;
4770 uint32_t generation;
4775 assert_single_node_only();
4777 if (argc < 1 || argc > 2) {
4778 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4782 fh = open(argv[0], O_RDONLY);
4784 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4785 talloc_free(tmp_ctx);
4789 sys_read(fh, &dbhdr, sizeof(dbhdr));
4790 if (dbhdr.version != DB_VERSION) {
4791 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4793 talloc_free(tmp_ctx);
4797 dbname = discard_const(dbhdr.name);
4799 dbname = discard_const(argv[1]);
4802 outdata.dsize = dbhdr.size;
4803 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4804 if (outdata.dptr == NULL) {
4805 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4807 talloc_free(tmp_ctx);
4810 sys_read(fh, outdata.dptr, outdata.dsize);
4813 tm = localtime(&dbhdr.timestamp);
4814 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4815 printf("Restoring database '%s' from backup @ %s\n",
4819 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
4820 if (ctdb_db == NULL) {
4821 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4822 talloc_free(tmp_ctx);
4826 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4828 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4829 talloc_free(tmp_ctx);
4834 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4836 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4837 talloc_free(tmp_ctx);
4841 /* freeze all nodes */
4842 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4843 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4844 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4850 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4851 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4852 talloc_free(tmp_ctx);
4857 generation = vnnmap->generation;
4858 data.dptr = (void *)&generation;
4859 data.dsize = sizeof(generation);
4861 /* start a cluster wide transaction */
4862 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4863 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4865 TIMELIMIT(), false, data,
4868 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4873 w.db_id = ctdb_db->db_id;
4876 data.dptr = (void *)&w;
4877 data.dsize = sizeof(w);
4879 /* wipe all the remote databases. */
4880 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4881 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4883 TIMELIMIT(), false, data,
4886 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4887 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4888 talloc_free(tmp_ctx);
4892 /* push the database */
4893 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4894 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4896 TIMELIMIT(), false, outdata,
4899 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4900 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4901 talloc_free(tmp_ctx);
4905 data.dptr = (void *)&ctdb_db->db_id;
4906 data.dsize = sizeof(ctdb_db->db_id);
4908 /* mark the database as healthy */
4909 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4910 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4912 TIMELIMIT(), false, data,
4915 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4916 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4917 talloc_free(tmp_ctx);
4921 data.dptr = (void *)&generation;
4922 data.dsize = sizeof(generation);
4924 /* commit all the changes */
4925 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4927 TIMELIMIT(), false, data,
4930 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4931 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4932 talloc_free(tmp_ctx);
4937 /* thaw all nodes */
4938 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4939 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4945 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4946 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4947 talloc_free(tmp_ctx);
4952 talloc_free(tmp_ctx);
4957 * dump a database backup from a file
4959 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4961 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4963 struct db_file_header dbhdr;
4967 struct ctdb_rec_data_old *rec = NULL;
4968 struct ctdb_marshall_buffer *m;
4969 struct ctdb_dump_db_context c;
4971 assert_single_node_only();
4974 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4978 fh = open(argv[0], O_RDONLY);
4980 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4981 talloc_free(tmp_ctx);
4985 sys_read(fh, &dbhdr, sizeof(dbhdr));
4986 if (dbhdr.version != DB_VERSION) {
4987 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4989 talloc_free(tmp_ctx);
4993 outdata.dsize = dbhdr.size;
4994 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4995 if (outdata.dptr == NULL) {
4996 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4998 talloc_free(tmp_ctx);
5001 sys_read(fh, outdata.dptr, outdata.dsize);
5003 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5005 tm = localtime(&dbhdr.timestamp);
5006 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5007 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5008 dbhdr.name, m->db_id, tbuf);
5013 c.printemptyrecords = (bool)options.printemptyrecords;
5014 c.printdatasize = (bool)options.printdatasize;
5015 c.printlmaster = false;
5016 c.printhash = (bool)options.printhash;
5017 c.printrecordflags = (bool)options.printrecordflags;
5019 for (i=0; i < m->count; i++) {
5023 /* we do not want the header splitted, so we pass NULL*/
5024 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5027 ctdb_dumpdb_record(key, data, &c);
5030 printf("Dumped %d records\n", i);
5031 talloc_free(tmp_ctx);
5036 * wipe a database from a file
5038 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5041 const char *db_name;
5043 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5045 struct ctdb_db_context *ctdb_db;
5046 struct ctdb_node_map_old *nodemap = NULL;
5047 struct ctdb_vnn_map *vnnmap = NULL;
5049 struct ctdb_transdb w;
5051 uint32_t generation;
5054 assert_single_node_only();
5057 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5061 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5065 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5066 if (ctdb_db == NULL) {
5067 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5069 talloc_free(tmp_ctx);
5073 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5076 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5078 talloc_free(tmp_ctx);
5082 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5085 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5087 talloc_free(tmp_ctx);
5091 /* freeze all nodes */
5092 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5093 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5094 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5101 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5102 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5103 CTDB_RECOVERY_ACTIVE);
5104 talloc_free(tmp_ctx);
5109 generation = vnnmap->generation;
5110 data.dptr = (void *)&generation;
5111 data.dsize = sizeof(generation);
5113 /* start a cluster wide transaction */
5114 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5115 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5117 TIMELIMIT(), false, data,
5121 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5122 "transactions.\n"));
5126 w.db_id = ctdb_db->db_id;
5129 data.dptr = (void *)&w;
5130 data.dsize = sizeof(w);
5132 /* wipe all the remote databases. */
5133 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5134 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5136 TIMELIMIT(), false, data,
5139 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5140 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5141 talloc_free(tmp_ctx);
5145 data.dptr = (void *)&ctdb_db->db_id;
5146 data.dsize = sizeof(ctdb_db->db_id);
5148 /* mark the database as healthy */
5149 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5150 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5152 TIMELIMIT(), false, data,
5155 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5156 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5157 talloc_free(tmp_ctx);
5161 data.dptr = (void *)&generation;
5162 data.dsize = sizeof(generation);
5164 /* commit all the changes */
5165 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5167 TIMELIMIT(), false, data,
5170 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5171 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5172 talloc_free(tmp_ctx);
5176 /* thaw all nodes */
5177 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5178 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5184 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5185 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5186 talloc_free(tmp_ctx);
5190 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5192 talloc_free(tmp_ctx);
5199 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5205 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5206 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5207 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5208 if (ret != 0 || res != 0) {
5209 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5210 talloc_free(tmp_ctx);
5213 sys_write(1, data.dptr, data.dsize);
5214 talloc_free(tmp_ctx);
5219 handler for memory dumps
5221 static void mem_dump_handler(uint64_t srvid, TDB_DATA data, void *private_data)
5223 sys_write(1, data.dptr, data.dsize);
5228 dump memory usage on the recovery daemon
5230 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5234 struct ctdb_srvid_message rd;
5236 rd.pnn = ctdb_get_pnn(ctdb);
5237 rd.srvid = getpid();
5239 /* register a message port for receiveing the reply so that we
5240 can receive the reply
5242 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5245 data.dptr = (uint8_t *)&rd;
5246 data.dsize = sizeof(rd);
5248 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5250 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5254 /* this loop will terminate when we have received the reply */
5256 tevent_loop_once(ctdb->ev);
5263 send a message to a srvid
5265 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5267 unsigned long srvid;
5275 srvid = strtoul(argv[0], NULL, 0);
5277 data.dptr = (uint8_t *)discard_const(argv[1]);
5278 data.dsize= strlen(argv[1]);
5280 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5282 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5290 handler for msglisten
5292 static void msglisten_handler(uint64_t srvid, TDB_DATA data,
5297 printf("Message received: ");
5298 for (i=0;i<data.dsize;i++) {
5299 printf("%c", data.dptr[i]);
5305 listen for messages on a messageport
5307 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5313 /* register a message port and listen for messages
5315 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5316 printf("Listening for messages on srvid:%d\n", (int)srvid);
5319 tevent_loop_once(ctdb->ev);
5326 list all nodes in the cluster
5327 we parse the nodes file directly
5329 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5331 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5332 struct ctdb_node_map_old *node_map;
5335 assert_single_node_only();
5337 node_map = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
5338 if (node_map == NULL) {
5339 talloc_free(mem_ctx);
5343 for (i = 0; i < node_map->num; i++) {
5346 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
5349 addr = ctdb_addr_to_str(&node_map->nodes[i].addr);
5350 if (options.machinereadable){
5351 printm(":%d:%s:\n", node_map->nodes[i].pnn, addr);
5353 printf("%s\n", addr);
5356 talloc_free(mem_ctx);
5361 /**********************************************************************/
5362 /* reload the nodes file on all nodes */
5364 static void get_nodes_files_callback(struct ctdb_context *ctdb,
5365 uint32_t node_pnn, int32_t res,
5366 TDB_DATA outdata, void *callback_data)
5368 struct ctdb_node_map_old **maps =
5369 talloc_get_type(callback_data, struct ctdb_node_map_old *);
5371 if (outdata.dsize < offsetof(struct ctdb_node_map_old, nodes) ||
5372 outdata.dptr == NULL) {
5374 (__location__ " Invalid return data: %u %p\n",
5375 (unsigned)outdata.dsize, outdata.dptr));
5379 if (node_pnn >= talloc_array_length(maps)) {
5381 (__location__ " unexpected PNN %u\n", node_pnn));
5385 maps[node_pnn] = talloc_memdup(maps, outdata.dptr, outdata.dsize);
5388 static void get_nodes_files_fail_callback(struct ctdb_context *ctdb,
5389 uint32_t node_pnn, int32_t res,
5390 TDB_DATA outdata, void *callback_data)
5393 ("ERROR: Failed to get nodes file from node %u\n", node_pnn));
5396 static struct ctdb_node_map_old **
5397 ctdb_get_nodes_files(struct ctdb_context *ctdb,
5398 TALLOC_CTX *mem_ctx,
5399 struct timeval timeout,
5400 struct ctdb_node_map_old *nodemap)
5404 struct ctdb_node_map_old **maps;
5406 maps = talloc_zero_array(mem_ctx, struct ctdb_node_map_old *, nodemap->num);
5407 CTDB_NO_MEMORY_NULL(ctdb, maps);
5409 nodes = list_of_connected_nodes(ctdb, nodemap, mem_ctx, true);
5411 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_NODES_FILE,
5412 nodes, 0, TIMELIMIT(),
5414 get_nodes_files_callback,
5415 get_nodes_files_fail_callback,
5425 static bool node_files_are_identical(struct ctdb_node_map_old *nm1,
5426 struct ctdb_node_map_old *nm2)
5430 if (nm1->num != nm2->num) {
5433 for (i = 0; i < nm1->num; i++) {
5434 if (memcmp(&nm1->nodes[i], &nm2->nodes[i],
5435 sizeof(struct ctdb_node_and_flags)) != 0) {
5443 static bool check_all_node_files_are_identical(struct ctdb_context *ctdb,
5444 TALLOC_CTX *mem_ctx,
5445 struct timeval timeout,
5446 struct ctdb_node_map_old *nodemap,
5447 struct ctdb_node_map_old *file_nodemap)
5449 static struct ctdb_node_map_old **maps;
5453 maps = ctdb_get_nodes_files(ctdb, mem_ctx, timeout, nodemap);
5458 for (i = 0; i < talloc_array_length(maps); i++) {
5459 if (maps[i] == NULL) {
5462 if (!node_files_are_identical(file_nodemap, maps[i])) {
5464 ("ERROR: Node file on node %u differs from current node (%u)\n",
5465 i, ctdb_get_pnn(ctdb)));
5474 reload the nodes file on the local node
5476 static bool sanity_check_nodes_file_changes(TALLOC_CTX *mem_ctx,
5477 struct ctdb_node_map_old *nodemap,
5478 struct ctdb_node_map_old *file_nodemap)
5481 bool should_abort = false;
5482 bool have_changes = false;
5484 for (i=0; i<nodemap->num; i++) {
5485 if (i >= file_nodemap->num) {
5487 ("ERROR: Node %u (%s) missing from nodes file\n",
5488 nodemap->nodes[i].pnn,
5489 ctdb_addr_to_str(&nodemap->nodes[i].addr)));
5490 should_abort = true;
5493 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED &&
5494 file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5495 /* Node remains deleted */
5497 ("Node %u is unchanged (DELETED)\n",
5498 nodemap->nodes[i].pnn));
5499 } else if (!(nodemap->nodes[i].flags & NODE_FLAGS_DELETED) &&
5500 !(file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED)) {
5501 /* Node not newly nor previously deleted */
5502 if (!ctdb_same_ip(&nodemap->nodes[i].addr,
5503 &file_nodemap->nodes[i].addr)) {
5505 ("ERROR: Node %u has changed IP address (was %s, now %s)\n",
5506 nodemap->nodes[i].pnn,
5507 /* ctdb_addr_to_str() returns a static */
5508 talloc_strdup(mem_ctx,
5509 ctdb_addr_to_str(&nodemap->nodes[i].addr)),
5510 ctdb_addr_to_str(&file_nodemap->nodes[i].addr)));
5511 should_abort = true;
5514 ("Node %u is unchanged\n",
5515 nodemap->nodes[i].pnn));
5516 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5517 DEBUG(DEBUG_WARNING,
5518 ("WARNING: Node %u is disconnected. You MUST fix this node manually!\n",
5519 nodemap->nodes[i].pnn));
5522 } else if (file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5523 /* Node is being deleted */
5525 ("Node %u is DELETED\n",
5526 nodemap->nodes[i].pnn));
5527 have_changes = true;
5528 if (!(nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED)) {
5530 ("ERROR: Node %u is still connected\n",
5531 nodemap->nodes[i].pnn));
5532 should_abort = true;
5534 } else if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5535 /* Node was previously deleted */
5537 ("Node %u is UNDELETED\n", nodemap->nodes[i].pnn));
5538 have_changes = true;
5544 ("ERROR: Nodes will not be reloaded due to previous error\n"));
5545 talloc_free(mem_ctx);
5549 /* Leftover nodes in file are NEW */
5550 for (; i < file_nodemap->num; i++) {
5551 DEBUG(DEBUG_NOTICE, ("Node %u is NEW\n",
5552 file_nodemap->nodes[i].pnn));
5553 have_changes = true;
5556 return have_changes;
5559 static void reload_nodes_fail_callback(struct ctdb_context *ctdb,
5560 uint32_t node_pnn, int32_t res,
5561 TDB_DATA outdata, void *callback_data)
5563 DEBUG(DEBUG_WARNING,
5564 ("WARNING: Node %u failed to reload nodes. You MUST fix this node manually!\n",
5568 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5571 struct ctdb_node_map_old *nodemap=NULL;
5572 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
5573 struct ctdb_node_map_old *file_nodemap;
5577 assert_current_node_only(ctdb);
5579 /* Load both the current nodemap and the contents of the local
5580 * nodes file. Compare and sanity check them before doing
5583 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5585 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5589 file_nodemap = read_nodes_file(tmp_ctx, ctdb->pnn);
5590 if (file_nodemap == NULL) {
5591 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5592 talloc_free(tmp_ctx);
5596 if (!check_all_node_files_are_identical(ctdb, tmp_ctx, TIMELIMIT(),
5597 nodemap, file_nodemap)) {
5601 if (!sanity_check_nodes_file_changes(tmp_ctx, nodemap, file_nodemap)) {
5603 ("No change in nodes file, skipping unnecessary reload\n"));
5604 talloc_free(tmp_ctx);
5608 /* Now make the changes */
5609 conn = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
5610 for (i = 0; i < talloc_array_length(conn); i++) {
5611 DEBUG(DEBUG_INFO, ("Reloading nodes file on node %u\n",
5615 /* Another timeout could be used, such as ReRecoveryTimeout or
5616 * a new one for this purpose. However, this is the simplest
5618 timeout = options.timelimit;
5619 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
5620 "Disable recoveries", true);
5623 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_NODES_FILE,
5624 conn, 0, TIMELIMIT(),
5626 NULL, reload_nodes_fail_callback,
5630 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
5631 "Enable recoveries", true);
5633 talloc_free(tmp_ctx);
5639 static const struct {
5641 int (*fn)(struct ctdb_context *, int, const char **);
5643 bool without_daemon; /* can be run without daemon running ? */
5646 } ctdb_commands[] = {
5647 { "version", control_version, true, true, "show version of ctdb" },
5648 { "status", control_status, true, false, "show node status" },
5649 { "uptime", control_uptime, true, false, "show node uptime" },
5650 { "ping", control_ping, true, false, "ping all nodes" },
5651 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
5652 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5653 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5654 { "listvars", control_listvars, true, false, "list tunable variables"},
5655 { "statistics", control_statistics, false, false, "show statistics" },
5656 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5657 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5658 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5659 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5660 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5661 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5662 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5663 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5664 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
5665 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
5666 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
5667 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5668 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5669 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5670 { "lvs", control_lvs, false, true, "show lvs configuration", "[master|list|status]" },
5671 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5672 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
5673 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
5674 { "getdebug", control_getdebug, true, false, "get debug level" },
5675 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
5676 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
5677 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
5678 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
5679 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
5680 { "disable", control_disable, true, false, "disable a nodes public IP" },
5681 { "enable", control_enable, true, false, "enable a nodes public IP" },
5682 { "stop", control_stop, true, false, "stop a node" },
5683 { "continue", control_continue, true, false, "re-start a stopped node" },
5684 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
5685 { "unban", control_unban, true, false, "unban a node" },
5686 { "showban", control_showban, true, false, "show ban information"},
5687 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
5688 { "recover", control_recover, true, false, "force recovery" },
5689 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
5690 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
5691 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
5692 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
5693 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5694 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
5695 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
5697 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
5699 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
5700 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
5701 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
5702 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
5703 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
5704 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
5705 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
5706 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
5707 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
5708 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
5709 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
5710 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
5711 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
5712 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
5713 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
5714 { "natgw", control_natgw, false, true, "show NAT gateway configuration ", "[master|list|status]"},
5715 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
5716 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
5717 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
5718 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
5719 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
5720 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
5721 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
5722 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
5723 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
5724 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
5725 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
5726 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
5727 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
5728 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
5729 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
5730 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
5731 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
5732 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
5733 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
5734 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
5735 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
5741 static void usage(void)
5745 "Usage: ctdb [options] <control>\n" \
5747 " -n <node> choose node number, or 'all' (defaults to local node)\n"
5748 " -Y generate machine readable output\n"
5749 " -x <char> specify delimiter for machine readable output\n"
5750 " -v generate verbose output\n"
5751 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
5752 printf("Controls:\n");
5753 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5754 printf(" %-15s %-27s %s\n",
5755 ctdb_commands[i].name,
5756 ctdb_commands[i].args?ctdb_commands[i].args:"",
5757 ctdb_commands[i].msg);
5763 static void ctdb_alarm(int sig)
5765 printf("Maximum runtime exceeded - exiting\n");
5772 int main(int argc, const char *argv[])
5774 struct ctdb_context *ctdb;
5775 char *nodestring = NULL;
5776 int machineparsable = 0;
5777 struct poptOption popt_options[] = {
5780 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
5781 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
5782 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machine readable output", NULL },
5783 { NULL, 'x', POPT_ARG_STRING, &options.machineseparator, 0, "specify separator for machine readable output", "char" },
5784 { NULL, 'X', POPT_ARG_NONE, &machineparsable, 0, "enable machine parsable output with separator |", NULL },
5785 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
5786 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
5787 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
5788 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
5789 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
5790 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
5791 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
5795 const char **extra_argv;
5799 struct tevent_context *ev;
5800 const char *control;
5804 /* set some defaults */
5805 options.maxruntime = 0;
5806 options.timelimit = 10;
5807 options.pnn = CTDB_CURRENT_NODE;
5809 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
5811 while ((opt = poptGetNextOpt(pc)) != -1) {
5814 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
5815 poptBadOption(pc, 0), poptStrerror(opt)));
5820 /* setup the remaining options for the main program to use */
5821 extra_argv = poptGetArgs(pc);
5824 while (extra_argv[extra_argc]) extra_argc++;
5827 if (extra_argc < 1) {
5831 if (options.maxruntime == 0) {
5832 const char *ctdb_timeout;
5833 ctdb_timeout = getenv("CTDB_TIMEOUT");
5834 if (ctdb_timeout != NULL) {
5835 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5837 /* default timeout is 120 seconds */
5838 options.maxruntime = 120;
5842 if (machineparsable) {
5843 options.machineseparator = "|";
5845 if (options.machineseparator != NULL) {
5846 if (strlen(options.machineseparator) != 1) {
5847 printf("Invalid separator \"%s\" - "
5848 "must be single character\n",
5849 options.machineseparator);
5854 options.machinereadable = true;
5855 } else if (options.machinereadable) {
5856 options.machineseparator = ":";
5859 signal(SIGALRM, ctdb_alarm);
5860 alarm(options.maxruntime);
5862 control = extra_argv[0];
5864 /* Default value for CTDB_BASE - don't override */
5865 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
5867 ev = tevent_context_init(NULL);
5869 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5873 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5874 if (strcmp(control, ctdb_commands[i].name) == 0) {
5879 if (i == ARRAY_SIZE(ctdb_commands)) {
5880 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
5884 if (ctdb_commands[i].without_daemon == true) {
5885 if (nodestring != NULL) {
5886 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
5889 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
5892 /* initialise ctdb */
5893 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
5897 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5899 pnn = find_node_xpnn();
5902 ("Is this node part of a CTDB cluster?\n"));
5907 /* setup the node number(s) to contact */
5908 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
5909 &options.nodes, &options.pnn)) {
5913 if (options.pnn == CTDB_CURRENT_NODE) {
5914 options.pnn = options.nodes[0];
5917 if (ctdb_commands[i].auto_all &&
5918 ((options.pnn == CTDB_BROADCAST_ALL) ||
5919 (options.pnn == CTDB_MULTICAST))) {
5923 for (j = 0; j < talloc_array_length(options.nodes); j++) {
5924 options.pnn = options.nodes[j];
5925 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5928 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5933 (void)poptFreeContext(pc);