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)
434 ret = ctdb_ctrl_process_exists(ctdb, options.pnn, pid);
436 printf("PID %u exists\n", pid);
438 printf("PID %u does not exist\n", pid);
444 display statistics structure
446 static void show_statistics(struct ctdb_statistics *s, int show_header)
448 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
450 const char *prefix=NULL;
452 int tmp, days, hours, minutes, seconds;
457 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
458 STATISTICS_FIELD(num_clients),
459 STATISTICS_FIELD(frozen),
460 STATISTICS_FIELD(recovering),
461 STATISTICS_FIELD(num_recoveries),
462 STATISTICS_FIELD(client_packets_sent),
463 STATISTICS_FIELD(client_packets_recv),
464 STATISTICS_FIELD(node_packets_sent),
465 STATISTICS_FIELD(node_packets_recv),
466 STATISTICS_FIELD(keepalive_packets_sent),
467 STATISTICS_FIELD(keepalive_packets_recv),
468 STATISTICS_FIELD(node.req_call),
469 STATISTICS_FIELD(node.reply_call),
470 STATISTICS_FIELD(node.req_dmaster),
471 STATISTICS_FIELD(node.reply_dmaster),
472 STATISTICS_FIELD(node.reply_error),
473 STATISTICS_FIELD(node.req_message),
474 STATISTICS_FIELD(node.req_control),
475 STATISTICS_FIELD(node.reply_control),
476 STATISTICS_FIELD(client.req_call),
477 STATISTICS_FIELD(client.req_message),
478 STATISTICS_FIELD(client.req_control),
479 STATISTICS_FIELD(timeouts.call),
480 STATISTICS_FIELD(timeouts.control),
481 STATISTICS_FIELD(timeouts.traverse),
482 STATISTICS_FIELD(locks.num_calls),
483 STATISTICS_FIELD(locks.num_current),
484 STATISTICS_FIELD(locks.num_pending),
485 STATISTICS_FIELD(locks.num_failed),
486 STATISTICS_FIELD(total_calls),
487 STATISTICS_FIELD(pending_calls),
488 STATISTICS_FIELD(childwrite_calls),
489 STATISTICS_FIELD(pending_childwrite_calls),
490 STATISTICS_FIELD(memory_used),
491 STATISTICS_FIELD(max_hop_count),
492 STATISTICS_FIELD(total_ro_delegations),
493 STATISTICS_FIELD(total_ro_revokes),
496 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
505 if (options.machinereadable){
507 printm("CTDB version:");
508 printm("Current time of statistics:");
509 printm("Statistics collected since:");
510 for (i=0;i<ARRAY_SIZE(fields);i++) {
511 printm("%s:", fields[i].name);
513 printm("num_reclock_ctdbd_latency:");
514 printm("min_reclock_ctdbd_latency:");
515 printm("avg_reclock_ctdbd_latency:");
516 printm("max_reclock_ctdbd_latency:");
518 printm("num_reclock_recd_latency:");
519 printm("min_reclock_recd_latency:");
520 printm("avg_reclock_recd_latency:");
521 printm("max_reclock_recd_latency:");
523 printm("num_call_latency:");
524 printm("min_call_latency:");
525 printm("avg_call_latency:");
526 printm("max_call_latency:");
528 printm("num_lockwait_latency:");
529 printm("min_lockwait_latency:");
530 printm("avg_lockwait_latency:");
531 printm("max_lockwait_latency:");
533 printm("num_childwrite_latency:");
534 printm("min_childwrite_latency:");
535 printm("avg_childwrite_latency:");
536 printm("max_childwrite_latency:");
539 printm("%d:", CTDB_PROTOCOL);
540 printm("%d:", (int)s->statistics_current_time.tv_sec);
541 printm("%d:", (int)s->statistics_start_time.tv_sec);
542 for (i=0;i<ARRAY_SIZE(fields);i++) {
543 printm("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
545 printm("%d:", s->reclock.ctdbd.num);
546 printm("%.6f:", s->reclock.ctdbd.min);
547 printm("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
548 printm("%.6f:", s->reclock.ctdbd.max);
550 printm("%d:", s->reclock.recd.num);
551 printm("%.6f:", s->reclock.recd.min);
552 printm("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
553 printm("%.6f:", s->reclock.recd.max);
555 printm("%d:", s->call_latency.num);
556 printm("%.6f:", s->call_latency.min);
557 printm("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
558 printm("%.6f:", s->call_latency.max);
560 printm("%d:", s->childwrite_latency.num);
561 printm("%.6f:", s->childwrite_latency.min);
562 printm("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
563 printm("%.6f:", s->childwrite_latency.max);
566 printf("CTDB version %u\n", CTDB_PROTOCOL);
567 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
568 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
570 for (i=0;i<ARRAY_SIZE(fields);i++) {
571 if (strchr(fields[i].name, '.')) {
572 preflen = strcspn(fields[i].name, ".")+1;
573 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
574 prefix = fields[i].name;
575 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
580 printf(" %*s%-22s%*s%10u\n",
582 fields[i].name+preflen,
584 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
586 printf(" hop_count_buckets:");
587 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
588 printf(" %d", s->hop_count_bucket[i]);
591 printf(" lock_buckets:");
592 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
593 printf(" %d", s->locks.buckets[i]);
596 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);
598 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);
600 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);
602 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);
603 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);
606 talloc_free(tmp_ctx);
610 display remote ctdb statistics combined from all nodes
612 static int control_statistics_all(struct ctdb_context *ctdb)
615 struct ctdb_statistics statistics;
619 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
620 CTDB_NO_MEMORY(ctdb, nodes);
622 ZERO_STRUCT(statistics);
624 for (i=0;i<num_nodes;i++) {
625 struct ctdb_statistics s1;
627 uint32_t *v1 = (uint32_t *)&s1;
628 uint32_t *v2 = (uint32_t *)&statistics;
630 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
631 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
633 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
636 for (j=0;j<num_ints;j++) {
639 statistics.max_hop_count =
640 MAX(statistics.max_hop_count, s1.max_hop_count);
641 statistics.call_latency.max =
642 MAX(statistics.call_latency.max, s1.call_latency.max);
645 printf("Gathered statistics for %u nodes\n", num_nodes);
646 show_statistics(&statistics, 1);
651 display remote ctdb statistics
653 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
656 struct ctdb_statistics statistics;
658 if (options.pnn == CTDB_BROADCAST_ALL) {
659 return control_statistics_all(ctdb);
662 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
664 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
667 show_statistics(&statistics, 1);
673 reset remote ctdb statistics
675 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
679 ret = ctdb_statistics_reset(ctdb, options.pnn);
681 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
689 display remote ctdb rolling statistics
691 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
694 struct ctdb_statistics_list_old *stats;
695 int i, num_records = -1;
697 assert_single_node_only();
700 num_records = atoi(argv[0]) - 1;
703 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
705 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
708 for (i=0;i<stats->num;i++) {
709 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
712 show_statistics(&stats->stats[i], i==0);
713 if (i == num_records) {
722 display remote ctdb db statistics
724 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
726 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
727 struct ctdb_db_statistics_old *dbstat;
737 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
741 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
743 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
744 talloc_free(tmp_ctx);
748 printf("DB Statistics: %s\n", argv[0]);
749 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
750 dbstat->db_ro_delegations);
751 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
752 dbstat->db_ro_delegations);
753 printf(" %s\n", "locks");
754 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
755 dbstat->locks.num_calls);
756 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
757 dbstat->locks.num_failed);
758 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
759 dbstat->locks.num_current);
760 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
761 dbstat->locks.num_pending);
762 printf(" %s", "hop_count_buckets:");
763 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
764 printf(" %d", dbstat->hop_count_bucket[i]);
767 printf(" %s", "lock_buckets:");
768 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
769 printf(" %d", dbstat->locks.buckets[i]);
772 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
773 "locks_latency MIN/AVG/MAX",
774 dbstat->locks.latency.min,
775 (dbstat->locks.latency.num ?
776 dbstat->locks.latency.total /dbstat->locks.latency.num :
778 dbstat->locks.latency.max,
779 dbstat->locks.latency.num);
780 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
781 "vacuum_latency MIN/AVG/MAX",
782 dbstat->vacuum.latency.min,
783 (dbstat->vacuum.latency.num ?
784 dbstat->vacuum.latency.total /dbstat->vacuum.latency.num :
786 dbstat->vacuum.latency.max,
787 dbstat->vacuum.latency.num);
789 for (i=0; i<dbstat->num_hot_keys; i++) {
790 if (dbstat->hot_keys[i].count > 0) {
794 dbstat->num_hot_keys = num_hot_keys;
796 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
797 for (i = 0; i < dbstat->num_hot_keys; i++) {
799 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
800 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
801 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
806 talloc_free(tmp_ctx);
811 display uptime of remote node
813 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
816 struct ctdb_uptime *uptime = NULL;
817 int tmp, days, hours, minutes, seconds;
819 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
821 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
825 if (options.machinereadable){
826 printm(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
827 printm(":%u:%u:%u:%lf\n",
828 (unsigned int)uptime->current_time.tv_sec,
829 (unsigned int)uptime->ctdbd_start_time.tv_sec,
830 (unsigned int)uptime->last_recovery_finished.tv_sec,
831 timeval_delta(&uptime->last_recovery_finished,
832 &uptime->last_recovery_started)
837 printf("Current time of node %-4u : %s",
838 options.pnn, ctime(&uptime->current_time.tv_sec));
840 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
848 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
850 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
858 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
860 printf("Duration of last recovery/failover: %lf seconds\n",
861 timeval_delta(&uptime->last_recovery_finished,
862 &uptime->last_recovery_started));
868 show the PNN of the current node
870 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
874 mypnn = getpnn(ctdb);
876 printf("%u\n", mypnn);
881 static struct ctdb_node_map_old *read_nodes_file(TALLOC_CTX *mem_ctx,
884 const char *nodes_list = NULL;
886 /* read the nodes file */
887 if (pnn != CTDB_UNKNOWN_PNN) {
890 t = talloc_asprintf(mem_ctx, "CTDB_NODES_%u", pnn);
892 nodes_list = getenv(t);
896 if (nodes_list == NULL) {
897 nodes_list = getenv("CTDB_NODES");
899 if (nodes_list == NULL) {
900 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
901 getenv("CTDB_BASE"));
902 if (nodes_list == NULL) {
903 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
908 return ctdb_read_nodes_file(mem_ctx, nodes_list);
912 show the PNN of the current node
913 discover the pnn by loading the nodes file and try to bind to all
914 addresses one at a time until the ip address is found.
916 static int find_node_xpnn(void)
918 TALLOC_CTX *mem_ctx = talloc_new(NULL);
919 struct ctdb_node_map_old *node_map;
922 node_map = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
923 if (node_map == NULL) {
924 talloc_free(mem_ctx);
928 for (i = 0; i < node_map->num; i++) {
929 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
932 if (ctdb_sys_have_ip(&node_map->nodes[i].addr)) {
933 pnn = node_map->nodes[i].pnn;
934 talloc_free(mem_ctx);
939 DEBUG(DEBUG_ERR, ("Failed to detect which PNN this node is\n"));
940 talloc_free(mem_ctx);
944 /* Helpers for ctdb status
946 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
948 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
952 if (node->flags == 0) {
953 struct ctdb_iface_list_old *ifaces;
955 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
956 tmp_ctx, &ifaces) == 0) {
957 for (j=0; j < ifaces->num; j++) {
958 if (ifaces->ifaces[j].link_state != 0) {
966 talloc_free(tmp_ctx);
971 static void control_status_header_machine(void)
973 printm(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
974 ":Inactive:PartiallyOnline:ThisNode:\n");
977 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
978 struct ctdb_node_and_flags *node)
980 printm(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
981 ctdb_addr_to_str(&node->addr),
982 !!(node->flags&NODE_FLAGS_DISCONNECTED),
983 !!(node->flags&NODE_FLAGS_BANNED),
984 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
985 !!(node->flags&NODE_FLAGS_UNHEALTHY),
986 !!(node->flags&NODE_FLAGS_STOPPED),
987 !!(node->flags&NODE_FLAGS_INACTIVE),
988 is_partially_online(ctdb, node) ? 1 : 0,
989 (node->pnn == mypnn)?'Y':'N');
994 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
995 struct ctdb_node_and_flags *node)
997 printf("pnn:%d %-16s %s%s\n", node->pnn,
998 ctdb_addr_to_str(&node->addr),
999 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
1000 node->pnn == mypnn?" (THIS NODE)":"");
1006 display remote ctdb status
1008 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
1010 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1012 struct ctdb_vnn_map *vnnmap=NULL;
1013 struct ctdb_node_map_old *nodemap=NULL;
1014 uint32_t recmode, recmaster, mypnn;
1015 int num_deleted_nodes = 0;
1018 mypnn = getpnn(ctdb);
1020 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1022 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1023 talloc_free(tmp_ctx);
1027 if (options.machinereadable) {
1028 control_status_header_machine();
1029 for (i=0;i<nodemap->num;i++) {
1030 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1033 (void) control_status_1_machine(ctdb, mypnn,
1034 &nodemap->nodes[i]);
1036 talloc_free(tmp_ctx);
1040 for (i=0; i<nodemap->num; i++) {
1041 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1042 num_deleted_nodes++;
1045 if (num_deleted_nodes == 0) {
1046 printf("Number of nodes:%d\n", nodemap->num);
1048 printf("Number of nodes:%d (including %d deleted nodes)\n",
1049 nodemap->num, num_deleted_nodes);
1051 for(i=0;i<nodemap->num;i++){
1052 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1055 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1058 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1060 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1061 talloc_free(tmp_ctx);
1064 if (vnnmap->generation == INVALID_GENERATION) {
1065 printf("Generation:INVALID\n");
1067 printf("Generation:%d\n",vnnmap->generation);
1069 printf("Size:%d\n",vnnmap->size);
1070 for(i=0;i<vnnmap->size;i++){
1071 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1074 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1076 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1077 talloc_free(tmp_ctx);
1080 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1082 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1084 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1085 talloc_free(tmp_ctx);
1088 printf("Recovery master:%d\n",recmaster);
1090 talloc_free(tmp_ctx);
1094 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1096 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1098 struct ctdb_node_map_old *nodemap=NULL;
1100 uint32_t pnn_mode, mypnn;
1106 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1107 options.pnn, true, &nodes, &pnn_mode)) {
1111 if (options.machinereadable) {
1112 control_status_header_machine();
1113 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1114 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1117 mypnn = getpnn(ctdb);
1119 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1121 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1122 talloc_free(tmp_ctx);
1128 for (i = 0; i < talloc_array_length(nodes); i++) {
1129 if (options.machinereadable) {
1130 ret |= control_status_1_machine(ctdb, mypnn,
1131 &nodemap->nodes[nodes[i]]);
1133 ret |= control_status_1_human(ctdb, mypnn,
1134 &nodemap->nodes[nodes[i]]);
1138 talloc_free(tmp_ctx);
1142 /* Display NAT gateway status */
1143 static int control_natgw(struct ctdb_context *ctdb, int argc, const char **argv)
1145 static char prog[PATH_MAX+1] = "";
1151 if (!ctdb_set_helper("NAT gateway helper", prog, sizeof(prog),
1152 "CTDB_NATGW_HELPER", CTDB_HELPER_BINDIR,
1154 DEBUG(DEBUG_ERR, ("Unable to set NAT gateway helper\n"));
1158 execl(prog, prog, argv[0], NULL);
1161 ("Unable to run NAT gateway helper %s\n", strerror(errno)));
1166 display the status of the scripts for monitoring (or other events)
1168 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1169 enum ctdb_event type)
1171 struct ctdb_script_list_old *script_status;
1174 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1176 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1180 if (script_status == NULL) {
1181 if (!options.machinereadable) {
1182 printf("%s cycle never run\n",
1183 ctdb_eventscript_call_names[type]);
1188 if (!options.machinereadable) {
1190 for (i=0; i<script_status->num_scripts; i++) {
1191 if (script_status->scripts[i].status != -ENOEXEC) {
1195 printf("%d scripts were executed last %s cycle\n",
1197 ctdb_eventscript_call_names[type]);
1199 for (i=0; i<script_status->num_scripts; i++) {
1200 const char *status = NULL;
1202 /* The ETIME status is ignored for certain events.
1203 * In that case the status is 0, but endtime is not set.
1205 if (script_status->scripts[i].status == 0 &&
1206 timeval_is_zero(&script_status->scripts[i].finished)) {
1207 script_status->scripts[i].status = -ETIME;
1210 switch (script_status->scripts[i].status) {
1212 status = "TIMEDOUT";
1215 status = "DISABLED";
1221 if (script_status->scripts[i].status > 0)
1225 if (options.machinereadable) {
1226 printm(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1227 ctdb_eventscript_call_names[type],
1228 script_status->scripts[i].name,
1229 script_status->scripts[i].status,
1231 (long)script_status->scripts[i].start.tv_sec,
1232 (long)script_status->scripts[i].start.tv_usec,
1233 (long)script_status->scripts[i].finished.tv_sec,
1234 (long)script_status->scripts[i].finished.tv_usec,
1235 script_status->scripts[i].output);
1239 printf("%-20s Status:%s ",
1240 script_status->scripts[i].name, status);
1242 /* Some other error, eg from stat. */
1243 printf("%-20s Status:CANNOT RUN (%s)",
1244 script_status->scripts[i].name,
1245 strerror(-script_status->scripts[i].status));
1247 if (script_status->scripts[i].status >= 0) {
1248 printf("Duration:%.3lf ",
1249 timeval_delta(&script_status->scripts[i].finished,
1250 &script_status->scripts[i].start));
1252 if (script_status->scripts[i].status != -ENOEXEC) {
1254 ctime(&script_status->scripts[i].start.tv_sec));
1255 if (script_status->scripts[i].status != 0) {
1256 printf(" OUTPUT:%s\n",
1257 script_status->scripts[i].output);
1267 static int control_scriptstatus(struct ctdb_context *ctdb,
1268 int argc, const char **argv)
1271 enum ctdb_event type, min, max;
1275 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1280 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1284 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1285 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1291 if (type == CTDB_EVENT_MAX) {
1292 if (strcmp(arg, "all") == 0) {
1294 max = CTDB_EVENT_MAX;
1296 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1301 if (options.machinereadable) {
1302 printm(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1305 for (type = min; type < max; type++) {
1306 ret = control_one_scriptstatus(ctdb, type);
1316 enable an eventscript
1318 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1326 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1328 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1336 disable an eventscript
1338 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1346 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1348 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1356 display the pnn of the recovery master
1358 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1363 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1365 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1368 printf("%d\n",recmaster);
1374 add a tickle to a public address
1376 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1378 struct ctdb_connection t;
1382 assert_single_node_only();
1388 if (parse_ip_port(argv[0], &t.src) == 0) {
1389 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1392 if (parse_ip_port(argv[1], &t.dst) == 0) {
1393 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1397 data.dptr = (uint8_t *)&t;
1398 data.dsize = sizeof(t);
1400 /* tell all nodes about this tcp connection */
1401 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1402 0, data, ctdb, NULL, NULL, NULL, NULL);
1404 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1413 delete a tickle from a node
1415 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1417 struct ctdb_connection t;
1421 assert_single_node_only();
1427 if (parse_ip_port(argv[0], &t.src) == 0) {
1428 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1431 if (parse_ip_port(argv[1], &t.dst) == 0) {
1432 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1436 data.dptr = (uint8_t *)&t;
1437 data.dsize = sizeof(t);
1439 /* tell all nodes about this tcp connection */
1440 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1441 0, data, ctdb, NULL, NULL, NULL, NULL);
1443 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1452 get a list of all tickles for this pnn
1454 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1456 struct ctdb_tickle_list_old *list;
1457 ctdb_sock_addr addr;
1461 assert_single_node_only();
1468 port = atoi(argv[1]);
1471 if (parse_ip(argv[0], NULL, port, &addr) == 0) {
1472 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1476 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1478 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1482 if (options.machinereadable){
1483 printm(":source ip:port:destination ip:port:\n");
1484 for (i=0;i<list->num;i++) {
1485 printm(":%s:%u", ctdb_addr_to_str(&list->connections[i].src), ntohs(list->connections[i].src.ip.sin_port));
1486 printm(":%s:%u:\n", ctdb_addr_to_str(&list->connections[i].dst), ntohs(list->connections[i].dst.ip.sin_port));
1489 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1490 printf("Num tickles:%u\n", list->num);
1491 for (i=0;i<list->num;i++) {
1492 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->connections[i].src), ntohs(list->connections[i].src.ip.sin_port));
1493 printf("DST: %s:%u\n", ctdb_addr_to_str(&list->connections[i].dst), ntohs(list->connections[i].dst.ip.sin_port));
1503 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1505 struct ctdb_public_ip_list_old *ips;
1506 struct ctdb_public_ip ip;
1509 uint32_t disable_time;
1511 struct ctdb_node_map_old *nodemap=NULL;
1512 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1515 data.dptr = (uint8_t*)&disable_time;
1516 data.dsize = sizeof(disable_time);
1517 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1519 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1525 /* read the public ip list from the node */
1526 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1528 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1529 talloc_free(tmp_ctx);
1533 for (i=0;i<ips->num;i++) {
1534 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1539 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1540 pnn, ctdb_addr_to_str(addr)));
1541 talloc_free(tmp_ctx);
1548 data.dptr = (uint8_t *)&ip;
1549 data.dsize = sizeof(ip);
1551 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1553 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1554 talloc_free(tmp_ctx);
1558 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1559 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1566 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1567 talloc_free(tmp_ctx);
1571 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1573 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1574 talloc_free(tmp_ctx);
1578 talloc_free(tmp_ctx);
1584 * scans all other nodes and returns a pnn for another node that can host this
1588 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1590 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1591 struct ctdb_public_ip_list_old *ips;
1592 struct ctdb_node_map_old *nodemap=NULL;
1596 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1598 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1599 talloc_free(tmp_ctx);
1603 for(i=0;i<nodemap->num;i++){
1604 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1607 if (nodemap->nodes[i].pnn == options.pnn) {
1611 /* read the public ip list from this node */
1612 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1614 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1618 for (j=0;j<ips->num;j++) {
1619 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1620 pnn = nodemap->nodes[i].pnn;
1621 talloc_free(tmp_ctx);
1628 talloc_free(tmp_ctx);
1632 /* If pnn is -1 then try to find a node to move IP to... */
1633 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1635 bool pnn_specified = (pnn == -1 ? false : true);
1638 while (retries < 5) {
1639 if (!pnn_specified) {
1640 pnn = find_other_host_for_public_ip(ctdb, addr);
1645 ("Trying to move public IP to node %u\n", pnn));
1648 if (move_ip(ctdb, addr, pnn) == 0) {
1661 move/failover an ip address to a specific node
1663 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1666 ctdb_sock_addr addr;
1668 assert_single_node_only();
1675 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1676 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1681 if (sscanf(argv[1], "%u", &pnn) != 1) {
1682 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1686 if (!try_moveip(ctdb, &addr, pnn)) {
1687 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1694 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1698 data.dptr = (uint8_t *)&pnn;
1699 data.dsize = sizeof(uint32_t);
1700 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1702 ("Failed to send message to force node %u to be a rebalancing target\n",
1711 static int getips_store_callback(void *param, void *data)
1713 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1714 struct ctdb_public_ip_list_old *ips = param;
1718 ips->ips[i].pnn = node_ip->pnn;
1719 ips->ips[i].addr = node_ip->addr;
1723 static int getips_count_callback(void *param, void *data)
1725 uint32_t *count = param;
1732 static uint32_t *ip_key(ctdb_sock_addr *ip)
1734 static uint32_t key[IP_KEYLEN];
1736 bzero(key, sizeof(key));
1738 switch (ip->sa.sa_family) {
1740 key[0] = ip->ip.sin_addr.s_addr;
1743 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1751 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1758 static void *add_ip_callback(void *parm, void *data)
1764 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_public_ip_list_old **ips)
1766 struct ctdb_public_ip_list_old *tmp_ips;
1767 struct ctdb_node_map_old *nodemap=NULL;
1768 trbt_tree_t *ip_tree;
1772 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1774 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1778 ip_tree = trbt_create(tmp_ctx, 0);
1780 for(i=0;i<nodemap->num;i++){
1781 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1784 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1788 /* read the public ip list from this node */
1789 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1791 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1795 for (j=0; j<tmp_ips->num;j++) {
1796 struct ctdb_public_ip *node_ip;
1798 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1799 node_ip->pnn = tmp_ips->ips[j].pnn;
1800 node_ip->addr = tmp_ips->ips[j].addr;
1802 trbt_insertarray32_callback(ip_tree,
1803 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1807 talloc_free(tmp_ips);
1812 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1814 len = offsetof(struct ctdb_public_ip_list_old, ips) +
1815 count*sizeof(struct ctdb_public_ip);
1816 tmp_ips = talloc_zero_size(tmp_ctx, len);
1817 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1825 static void ctdb_every_second(struct tevent_context *ev,
1826 struct tevent_timer *te,
1827 struct timeval t, void *p)
1829 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1831 tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
1832 ctdb_every_second, ctdb);
1835 struct srvid_reply_handler_data {
1839 const char *srvid_str;
1842 static void srvid_broadcast_reply_handler(uint64_t srvid, TDB_DATA data,
1845 struct srvid_reply_handler_data *d =
1846 (struct srvid_reply_handler_data *)private_data;
1850 if (data.dsize != sizeof(ret)) {
1851 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
1855 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
1856 ret = *(int32_t *)data.dptr;
1859 ("%s failed with result %d\n", d->srvid_str, ret));
1863 if (!d->wait_for_all) {
1868 /* Wait for all replies */
1870 for (i = 0; i < talloc_array_length(d->nodes); i++) {
1871 if (d->nodes[i] == ret) {
1873 ("%s reply received from node %u\n",
1874 d->srvid_str, ret));
1877 if (d->nodes[i] != -1) {
1878 /* Found a node that hasn't yet replied */
1884 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
1885 * or replies from all connected nodes. arg is the data argument to
1886 * pass in the srvid_request structure - pass 0 if this isn't needed.
1888 static int srvid_broadcast(struct ctdb_context *ctdb,
1889 uint64_t srvid, uint32_t *arg,
1890 const char *srvid_str, bool wait_for_all)
1895 uint64_t reply_srvid;
1896 struct ctdb_srvid_message request;
1897 struct ctdb_disable_message request_data;
1898 struct srvid_reply_handler_data reply_data;
1901 /* Time ticks to enable timeouts to be processed */
1902 tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
1903 ctdb_every_second, ctdb);
1905 pnn = ctdb_get_pnn(ctdb);
1906 reply_srvid = getpid();
1909 ZERO_STRUCT(request);
1912 request.srvid = reply_srvid;
1914 data.dptr = (uint8_t *)&request;
1915 data.dsize = sizeof(request);
1917 ZERO_STRUCT(request_data);
1919 request_data.pnn = pnn;
1920 request_data.srvid = reply_srvid;
1921 request_data.timeout = *arg;
1923 data.dptr = (uint8_t *)&request_data;
1924 data.dsize = sizeof(request_data);
1927 /* Register message port for reply from recovery master */
1928 ctdb_client_set_message_handler(ctdb, reply_srvid,
1929 srvid_broadcast_reply_handler,
1932 reply_data.wait_for_all = wait_for_all;
1933 reply_data.nodes = NULL;
1934 reply_data.srvid_str = srvid_str;
1937 reply_data.done = false;
1940 struct ctdb_node_map_old *nodemap;
1942 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
1943 CTDB_CURRENT_NODE, ctdb, &nodemap);
1946 ("Unable to get nodemap from current node, try again\n"));
1951 if (reply_data.nodes != NULL) {
1952 talloc_free(reply_data.nodes);
1954 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
1957 talloc_free(nodemap);
1960 /* Send to all connected nodes. Only recmaster replies */
1961 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
1964 /* This can only happen if the socket is closed and
1965 * there's no way to recover from that, so don't try
1969 ("Failed to send %s request to connected nodes\n",
1974 tv = timeval_current();
1975 /* This loop terminates the reply is received */
1976 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
1977 tevent_loop_once(ctdb->ev);
1980 if (!reply_data.done) {
1982 ("Still waiting for confirmation of %s\n", srvid_str));
1987 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
1989 talloc_free(reply_data.nodes);
1994 static int ipreallocate(struct ctdb_context *ctdb)
1996 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
1997 "IP reallocation", false);
2001 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2003 return ipreallocate(ctdb);
2007 add a public ip address to a node
2009 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2012 int len, retries = 0;
2014 ctdb_sock_addr addr;
2015 struct ctdb_addr_info_old *pub;
2016 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2017 struct ctdb_public_ip_list_old *ips;
2021 talloc_free(tmp_ctx);
2025 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2026 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2027 talloc_free(tmp_ctx);
2031 /* read the public ip list from the node */
2032 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2034 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2035 talloc_free(tmp_ctx);
2038 for (i=0;i<ips->num;i++) {
2039 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2040 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2047 /* Dont timeout. This command waits for an ip reallocation
2048 which sometimes can take wuite a while if there has
2049 been a recent recovery
2053 len = offsetof(struct ctdb_addr_info_old, iface) + strlen(argv[1]) + 1;
2054 pub = talloc_size(tmp_ctx, len);
2055 CTDB_NO_MEMORY(ctdb, pub);
2059 pub->len = strlen(argv[1])+1;
2060 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2063 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2065 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2069 } while (retries < 5 && ret != 0);
2071 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2072 talloc_free(tmp_ctx);
2076 if (rebalance_node(ctdb, options.pnn) != 0) {
2077 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2081 talloc_free(tmp_ctx);
2086 add a public ip address to a node
2088 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2090 ctdb_sock_addr addr;
2097 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2098 printf("Badly formed ip : %s\n", argv[0]);
2102 iface = ctdb_sys_find_ifname(&addr);
2103 if (iface == NULL) {
2104 printf("Failed to get interface name for ip: %s", argv[0]);
2108 printf("IP on interface %s\n", iface);
2115 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2117 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2119 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2120 struct ctdb_node_map_old *nodemap=NULL;
2121 struct ctdb_public_ip_list_old *ips;
2124 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2126 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2130 /* remove it from the nodes that are not hosting the ip currently */
2131 for(i=0;i<nodemap->num;i++){
2132 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2135 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2137 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2141 for (j=0;j<ips->num;j++) {
2142 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2150 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2154 options.pnn = nodemap->nodes[i].pnn;
2155 control_delip(ctdb, argc, argv);
2159 /* remove it from every node (also the one hosting it) */
2160 for(i=0;i<nodemap->num;i++){
2161 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2164 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2166 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2170 for (j=0;j<ips->num;j++) {
2171 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2179 options.pnn = nodemap->nodes[i].pnn;
2180 control_delip(ctdb, argc, argv);
2183 talloc_free(tmp_ctx);
2188 delete a public ip address from a node
2190 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2193 ctdb_sock_addr addr;
2194 struct ctdb_addr_info_old pub;
2195 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2196 struct ctdb_public_ip_list_old *ips;
2199 talloc_free(tmp_ctx);
2203 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2204 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2208 if (options.pnn == CTDB_BROADCAST_ALL) {
2209 return control_delip_all(ctdb, argc, argv, &addr);
2216 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2218 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2219 talloc_free(tmp_ctx);
2223 for (i=0;i<ips->num;i++) {
2224 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2230 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2231 ctdb_addr_to_str(&addr)));
2232 talloc_free(tmp_ctx);
2236 /* This is an optimisation. If this node is hosting the IP
2237 * then try to move it somewhere else without invoking a full
2238 * takeover run. We don't care if this doesn't work!
2240 if (ips->ips[i].pnn == options.pnn) {
2241 (void) try_moveip(ctdb, &addr, -1);
2244 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2246 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2247 talloc_free(tmp_ctx);
2251 talloc_free(tmp_ctx);
2258 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2261 ctdb_sock_addr addr;
2263 assert_single_node_only();
2269 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2270 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2274 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2276 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2284 check if a server id exists
2286 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2288 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2294 talloc_free(tmp_ctx);
2298 ids = talloc_array(tmp_ctx, uint64_t, argc);
2299 result = talloc_array(tmp_ctx, uint8_t, argc);
2301 for (i = 0; i < argc; i++) {
2302 ids[i] = strtoull(argv[i], NULL, 0);
2305 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2306 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2308 talloc_free(tmp_ctx);
2312 for (i=0; i < argc; i++) {
2313 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2314 result[i] ? "exists" : "does not exist");
2317 talloc_free(tmp_ctx);
2322 send a tcp tickle ack
2324 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2327 ctdb_sock_addr src, dst;
2333 if (!parse_ip_port(argv[0], &src)) {
2334 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2338 if (!parse_ip_port(argv[1], &dst)) {
2339 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2343 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2347 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2354 display public ip status
2356 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2359 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2360 struct ctdb_public_ip_list_old *ips;
2362 if (argc == 1 && strcmp(argv[0], "all") == 0) {
2363 options.pnn = CTDB_BROADCAST_ALL;
2366 if (options.pnn == CTDB_BROADCAST_ALL) {
2367 /* read the list of public ips from all nodes */
2368 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2370 /* read the public ip list from this node */
2371 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2374 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2375 talloc_free(tmp_ctx);
2379 if (options.machinereadable){
2380 printm(":Public IP:Node:");
2381 if (options.verbose){
2382 printm("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2386 if (options.pnn == CTDB_BROADCAST_ALL) {
2387 printf("Public IPs on ALL nodes\n");
2389 printf("Public IPs on node %u\n", options.pnn);
2393 for (i=1;i<=ips->num;i++) {
2394 struct ctdb_public_ip_info_old *info = NULL;
2396 char *aciface = NULL;
2397 char *avifaces = NULL;
2398 char *cifaces = NULL;
2400 if (options.pnn == CTDB_BROADCAST_ALL) {
2401 pnn = ips->ips[ips->num-i].pnn;
2407 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2408 &ips->ips[ips->num-i].addr, &info);
2415 for (j=0; j < info->num; j++) {
2416 if (cifaces == NULL) {
2417 cifaces = talloc_strdup(info,
2418 info->ifaces[j].name);
2420 cifaces = talloc_asprintf_append(cifaces,
2422 info->ifaces[j].name);
2425 if (info->active_idx == j) {
2426 aciface = info->ifaces[j].name;
2429 if (info->ifaces[j].link_state == 0) {
2433 if (avifaces == NULL) {
2434 avifaces = talloc_strdup(info, info->ifaces[j].name);
2436 avifaces = talloc_asprintf_append(avifaces,
2438 info->ifaces[j].name);
2443 if (options.machinereadable){
2445 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2446 ips->ips[ips->num-i].pnn);
2447 if (options.verbose){
2450 avifaces?avifaces:"",
2451 cifaces?cifaces:"");
2455 if (options.verbose) {
2456 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2457 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2458 ips->ips[ips->num-i].pnn,
2460 avifaces?avifaces:"",
2461 cifaces?cifaces:"");
2464 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2465 ips->ips[ips->num-i].pnn);
2471 talloc_free(tmp_ctx);
2478 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2481 ctdb_sock_addr addr;
2482 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2483 struct ctdb_public_ip_info_old *info;
2486 talloc_free(tmp_ctx);
2490 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2491 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2495 /* read the public ip info from this node */
2496 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2497 tmp_ctx, &addr, &info);
2499 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2500 argv[0], options.pnn));
2501 talloc_free(tmp_ctx);
2505 printf("Public IP[%s] info on node %u\n",
2506 ctdb_addr_to_str(&info->ip.addr),
2509 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2510 ctdb_addr_to_str(&info->ip.addr),
2511 info->ip.pnn, info->num);
2513 for (i=0; i<info->num; i++) {
2514 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2516 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2517 i+1, info->ifaces[i].name,
2518 info->ifaces[i].link_state?"up":"down",
2519 (unsigned int)info->ifaces[i].references,
2520 (i==info->active_idx)?" (active)":"");
2523 talloc_free(tmp_ctx);
2528 display interfaces status
2530 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2532 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2534 struct ctdb_iface_list_old *ifaces;
2537 /* read the public ip list from this node */
2538 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
2540 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2542 talloc_free(tmp_ctx);
2546 if (options.machinereadable){
2547 printm(":Name:LinkStatus:References:\n");
2549 printf("Interfaces on node %u\n", options.pnn);
2552 for (i=0; i<ifaces->num; i++) {
2553 if (options.machinereadable){
2554 printm(":%s:%s:%u:\n",
2555 ifaces->ifaces[i].name,
2556 ifaces->ifaces[i].link_state?"1":"0",
2557 (unsigned int)ifaces->ifaces[i].references);
2559 printf("name:%s link:%s references:%u\n",
2560 ifaces->ifaces[i].name,
2561 ifaces->ifaces[i].link_state?"up":"down",
2562 (unsigned int)ifaces->ifaces[i].references);
2566 talloc_free(tmp_ctx);
2572 set link status of an interface
2574 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2577 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2578 struct ctdb_iface info;
2586 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2587 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2589 talloc_free(tmp_ctx);
2592 strncpy(info.name, argv[0], sizeof(info.name)-1);
2593 info.name[sizeof(info.name)-1] = '\0';
2595 if (strcmp(argv[1], "up") == 0) {
2596 info.link_state = 1;
2597 } else if (strcmp(argv[1], "down") == 0) {
2598 info.link_state = 0;
2600 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2602 talloc_free(tmp_ctx);
2606 /* read the public ip list from this node */
2607 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2610 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2611 argv[0], options.pnn));
2612 talloc_free(tmp_ctx);
2616 talloc_free(tmp_ctx);
2621 display pid of a ctdb daemon
2623 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2628 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2630 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2633 printf("%u\n", pid);
2638 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
2640 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
2642 update_flags_handler_t handler,
2647 struct ctdb_node_map_old *nodemap = NULL;
2651 /* Check if the node is already in the desired state */
2652 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2654 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2657 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
2658 if (set_flag == flag_is_set) {
2659 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
2660 (set_flag ? "already" : "not"), desc));
2665 if (!handler(ctdb, data)) {
2666 DEBUG(DEBUG_WARNING,
2667 ("Failed to send control to set state %s on node %u, try again\n",
2668 desc, options.pnn));
2673 /* Read the nodemap and verify the change took effect.
2674 * Even if the above control/hanlder timed out then it
2675 * could still have worked!
2677 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
2680 DEBUG(DEBUG_WARNING,
2681 ("Unable to get nodemap from local node, try again\n"));
2683 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
2684 } while (nodemap == NULL || (set_flag != flag_is_set));
2686 return ipreallocate(ctdb);
2689 /* Administratively disable a node */
2690 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
2694 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
2695 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2699 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2701 return update_flags_and_ipreallocate(ctdb, NULL,
2702 update_flags_disabled,
2703 NODE_FLAGS_PERMANENTLY_DISABLED,
2705 true /* set_flag*/);
2708 /* Administratively re-enable a node */
2709 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
2713 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
2714 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2718 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2720 return update_flags_and_ipreallocate(ctdb, NULL,
2721 update_flags_not_disabled,
2722 NODE_FLAGS_PERMANENTLY_DISABLED,
2724 false /* set_flag*/);
2728 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
2732 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2737 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2739 return update_flags_and_ipreallocate(ctdb, NULL,
2740 update_flags_stopped,
2743 true /* set_flag*/);
2746 /* Continue a stopped node */
2747 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
2751 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2756 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2758 return update_flags_and_ipreallocate(ctdb, NULL,
2759 update_flags_not_stopped,
2762 false /* set_flag */);
2765 static uint32_t get_generation(struct ctdb_context *ctdb)
2767 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2768 struct ctdb_vnn_map *vnnmap=NULL;
2770 uint32_t generation;
2772 /* wait until the recmaster is not in recovery mode */
2774 uint32_t recmode, recmaster;
2776 if (vnnmap != NULL) {
2777 talloc_free(vnnmap);
2781 /* get the recmaster */
2782 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
2784 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2785 talloc_free(tmp_ctx);
2789 /* get recovery mode */
2790 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
2792 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2793 talloc_free(tmp_ctx);
2797 /* get the current generation number */
2798 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
2800 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2801 talloc_free(tmp_ctx);
2805 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
2806 generation = vnnmap->generation;
2807 talloc_free(tmp_ctx);
2815 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
2817 struct ctdb_ban_state *bantime = (struct ctdb_ban_state *)data;
2820 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
2825 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2827 struct ctdb_ban_state bantime;
2833 bantime.pnn = options.pnn;
2834 bantime.time = strtoul(argv[0], NULL, 0);
2836 if (bantime.time == 0) {
2837 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
2841 return update_flags_and_ipreallocate(ctdb, &bantime,
2842 update_state_banned,
2845 true /* set_flag*/);
2850 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2852 struct ctdb_ban_state bantime;
2854 bantime.pnn = options.pnn;
2857 return update_flags_and_ipreallocate(ctdb, &bantime,
2858 update_state_banned,
2861 false /* set_flag*/);
2865 show ban information for a node
2867 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2870 struct ctdb_node_map_old *nodemap=NULL;
2871 struct ctdb_ban_state *bantime;
2873 /* verify the node exists */
2874 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2876 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2880 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2882 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2886 if (bantime->time == 0) {
2887 printf("Node %u is not banned\n", bantime->pnn);
2889 printf("Node %u is banned, %d seconds remaining\n",
2890 bantime->pnn, bantime->time);
2899 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2903 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2905 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2915 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2918 uint32_t generation, next_generation;
2920 /* record the current generation number */
2921 generation = get_generation(ctdb);
2923 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2925 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2929 /* wait until we are in a new generation */
2931 next_generation = get_generation(ctdb);
2932 if (next_generation != generation) {
2943 display monitoring mode of a remote node
2945 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2950 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2952 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2955 if (!options.machinereadable){
2956 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2959 printm(":%d:\n",monmode);
2966 display capabilities of a remote node
2968 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2970 uint32_t capabilities;
2973 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2975 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2979 if (!options.machinereadable){
2980 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2981 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2983 printm(":RECMASTER:LMASTER:\n");
2985 !!(capabilities&CTDB_CAP_RECMASTER),
2986 !!(capabilities&CTDB_CAP_LMASTER));
2991 /* Display LVS status */
2992 static int control_lvs(struct ctdb_context *ctdb,
2993 int argc, const char **argv)
2995 static char prog[PATH_MAX+1] = "";
3001 if (!ctdb_set_helper("LVS helper", prog, sizeof(prog),
3002 "CTDB_LVS_HELPER", CTDB_HELPER_BINDIR,
3004 DEBUG(DEBUG_ERR, ("Unable to set LVS helper\n"));
3008 execl(prog, prog, argv[0], NULL);
3011 ("Unable to run LVS helper %s\n", strerror(errno)));
3016 disable monitoring on a node
3018 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3023 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3025 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3028 printf("Monitoring mode:%s\n","DISABLED");
3034 enable monitoring on a node
3036 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3041 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3043 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3046 printf("Monitoring mode:%s\n","ACTIVE");
3052 display remote list of keys/data for a db
3054 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3056 const char *db_name;
3057 struct ctdb_db_context *ctdb_db;
3059 struct ctdb_dump_db_context c;
3066 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3070 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3071 if (ctdb_db == NULL) {
3072 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3076 if (options.printlmaster) {
3077 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3078 ctdb, &ctdb->vnn_map);
3080 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3089 c.printemptyrecords = (bool)options.printemptyrecords;
3090 c.printdatasize = (bool)options.printdatasize;
3091 c.printlmaster = (bool)options.printlmaster;
3092 c.printhash = (bool)options.printhash;
3093 c.printrecordflags = (bool)options.printrecordflags;
3095 /* traverse and dump the cluster tdb */
3096 ret = ctdb_dump_db(ctdb_db, &c);
3098 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3099 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3100 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3104 talloc_free(ctdb_db);
3106 printf("Dumped %d records\n", ret);
3110 struct cattdb_data {
3111 struct ctdb_context *ctdb;
3115 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3117 struct cattdb_data *d = private_data;
3118 struct ctdb_dump_db_context c;
3125 c.printemptyrecords = (bool)options.printemptyrecords;
3126 c.printdatasize = (bool)options.printdatasize;
3127 c.printlmaster = false;
3128 c.printhash = (bool)options.printhash;
3129 c.printrecordflags = true;
3131 return ctdb_dumpdb_record(key, data, &c);
3135 cat the local tdb database using same format as catdb
3137 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3139 const char *db_name;
3140 struct ctdb_db_context *ctdb_db;
3141 struct cattdb_data d;
3148 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3152 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3153 if (ctdb_db == NULL) {
3154 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3158 /* traverse the local tdb */
3161 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3162 printf("Failed to cattdb data\n");
3165 talloc_free(ctdb_db);
3167 printf("Dumped %d records\n", d.count);
3172 display the content of a database key
3174 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3176 const char *db_name;
3177 struct ctdb_db_context *ctdb_db;
3178 struct ctdb_record_handle *h;
3179 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3187 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3191 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3192 if (ctdb_db == NULL) {
3193 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3197 key.dptr = discard_const(argv[1]);
3198 key.dsize = strlen((char *)key.dptr);
3200 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3202 printf("Failed to fetch record '%s' on node %d\n",
3203 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3204 talloc_free(tmp_ctx);
3208 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3210 talloc_free(tmp_ctx);
3211 talloc_free(ctdb_db);
3216 display the content of a database key
3218 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3220 const char *db_name;
3221 struct ctdb_db_context *ctdb_db;
3222 struct ctdb_record_handle *h;
3223 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3231 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3235 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3236 if (ctdb_db == NULL) {
3237 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3241 key.dptr = discard_const(argv[1]);
3242 key.dsize = strlen((char *)key.dptr);
3244 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3246 printf("Failed to fetch record '%s' on node %d\n",
3247 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3248 talloc_free(tmp_ctx);
3252 data.dptr = discard_const(argv[2]);
3253 data.dsize = strlen((char *)data.dptr);
3255 if (ctdb_record_store(h, data) != 0) {
3256 printf("Failed to store record\n");
3260 talloc_free(tmp_ctx);
3261 talloc_free(ctdb_db);
3266 fetch a record from a persistent database
3268 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3270 const char *db_name;
3271 struct ctdb_db_context *ctdb_db;
3272 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3273 struct ctdb_transaction_handle *h;
3280 talloc_free(tmp_ctx);
3284 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3285 talloc_free(tmp_ctx);
3289 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3291 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3292 talloc_free(tmp_ctx);
3296 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3297 if (ctdb_db == NULL) {
3298 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3299 talloc_free(tmp_ctx);
3303 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3305 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3306 talloc_free(tmp_ctx);
3310 key.dptr = discard_const(argv[1]);
3311 key.dsize = strlen(argv[1]);
3312 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3314 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3315 talloc_free(tmp_ctx);
3319 if (data.dsize == 0 || data.dptr == NULL) {
3320 DEBUG(DEBUG_ERR,("Record is empty\n"));
3321 talloc_free(tmp_ctx);
3326 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3328 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3329 talloc_free(tmp_ctx);
3332 sys_write(fd, data.dptr, data.dsize);
3335 sys_write(1, data.dptr, data.dsize);
3338 /* abort the transaction */
3342 talloc_free(tmp_ctx);
3347 fetch a record from a tdb-file
3349 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3351 const char *tdb_file;
3354 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3363 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3365 printf("Failed to open TDB file %s\n", tdb_file);
3369 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
3370 if (key.dptr == NULL) {
3371 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3375 data = tdb_fetch(tdb, key);
3376 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3377 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3385 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3387 printf("Failed to open output file %s\n", argv[2]);
3390 if (options.verbose){
3391 sys_write(fd, data.dptr, data.dsize);
3393 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3397 if (options.verbose){
3398 sys_write(1, data.dptr, data.dsize);
3400 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3404 talloc_free(tmp_ctx);
3409 store a record and header to a tdb-file
3411 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
3413 const char *tdb_file;
3415 TDB_DATA key, value, data;
3416 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3417 struct ctdb_ltdb_header header;
3425 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
3427 printf("Failed to open TDB file %s\n", tdb_file);
3431 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
3432 if (key.dptr == NULL) {
3433 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3437 value = strtodata(tmp_ctx, argv[2], strlen(argv[2]));
3438 if (value.dptr == NULL) {
3439 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
3443 ZERO_STRUCT(header);
3445 header.rsn = atoll(argv[3]);
3448 header.dmaster = atoi(argv[4]);
3451 header.flags = atoi(argv[5]);
3454 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
3455 data.dptr = talloc_size(tmp_ctx, data.dsize);
3456 if (data.dptr == NULL) {
3457 printf("Failed to allocate header+value\n");
3461 *(struct ctdb_ltdb_header *)data.dptr = header;
3462 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
3464 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
3465 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
3472 talloc_free(tmp_ctx);
3477 write a record to a persistent database
3479 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3481 const char *db_name;
3482 struct ctdb_db_context *ctdb_db;
3483 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3484 struct ctdb_transaction_handle *h;
3490 talloc_free(tmp_ctx);
3494 fd = open(argv[2], O_RDONLY);
3496 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3497 talloc_free(tmp_ctx);
3501 ret = fstat(fd, &st);
3503 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3505 talloc_free(tmp_ctx);
3509 if (!S_ISREG(st.st_mode)) {
3510 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3512 talloc_free(tmp_ctx);
3516 data.dsize = st.st_size;
3517 if (data.dsize == 0) {
3520 data.dptr = talloc_size(tmp_ctx, data.dsize);
3521 if (data.dptr == NULL) {
3522 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3524 talloc_free(tmp_ctx);
3527 ret = sys_read(fd, data.dptr, data.dsize);
3528 if (ret != data.dsize) {
3529 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3531 talloc_free(tmp_ctx);
3540 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3541 if (ctdb_db == NULL) {
3542 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3543 talloc_free(tmp_ctx);
3547 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3549 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3550 talloc_free(tmp_ctx);
3554 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
3555 if (key.dptr == NULL) {
3556 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3560 ret = ctdb_transaction_store(h, key, data);
3562 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3563 talloc_free(tmp_ctx);
3567 ret = ctdb_transaction_commit(h);
3569 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3570 talloc_free(tmp_ctx);
3575 talloc_free(tmp_ctx);
3580 * delete a record from a persistent database
3582 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
3584 const char *db_name;
3585 struct ctdb_db_context *ctdb_db;
3586 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3587 struct ctdb_transaction_handle *h;
3594 talloc_free(tmp_ctx);
3598 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3599 talloc_free(tmp_ctx);
3603 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3605 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
3606 talloc_free(tmp_ctx);
3610 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3611 if (ctdb_db == NULL) {
3612 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
3613 talloc_free(tmp_ctx);
3617 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3619 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
3620 talloc_free(tmp_ctx);
3624 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
3625 if (key.dptr == NULL) {
3626 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3630 ret = ctdb_transaction_store(h, key, tdb_null);
3632 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
3633 talloc_free(tmp_ctx);
3637 ret = ctdb_transaction_commit(h);
3639 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
3640 talloc_free(tmp_ctx);
3644 talloc_free(tmp_ctx);
3648 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
3653 const char *ret; /* Next byte after successfully parsed value */
3655 /* Error, unless someone says otherwise */
3657 /* Indicates no value to parse */
3660 /* Skip whitespace */
3661 n = strspn(s, " \t");
3665 /* Quoted ASCII string - no wide characters! */
3667 n = strcspn(t, "\"");
3670 *data = strtodata(mem_ctx, t, n);
3671 CTDB_NOMEM_ABORT(data->dptr);
3675 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
3678 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
3684 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
3685 TDB_DATA *key, TDB_DATA *value)
3687 char line [1024]; /* FIXME: make this more flexible? */
3691 ptr = fgets(line, sizeof(line), file);
3698 t = ptrans_parse_string(mem_ctx, line, key);
3699 if (t == NULL || key->dptr == NULL) {
3700 /* Line Ignored but not EOF */
3705 t = ptrans_parse_string(mem_ctx, t, value);
3707 /* Line Ignored but not EOF */
3708 talloc_free(key->dptr);
3717 * Update a persistent database as per file/stdin
3719 static int control_ptrans(struct ctdb_context *ctdb,
3720 int argc, const char **argv)
3722 const char *db_name;
3723 struct ctdb_db_context *ctdb_db;
3724 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3725 struct ctdb_transaction_handle *h;
3726 TDB_DATA key, value;
3731 talloc_free(tmp_ctx);
3737 file = fopen(argv[1], "r");
3739 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
3740 talloc_free(tmp_ctx);
3747 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3748 if (ctdb_db == NULL) {
3749 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3753 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3755 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3759 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
3760 if (key.dsize != 0) {
3761 ret = ctdb_transaction_store(h, key, value);
3762 /* Minimise memory use */
3763 talloc_free(key.dptr);
3764 if (value.dptr != NULL) {
3765 talloc_free(value.dptr);
3768 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3769 ctdb_transaction_cancel(h);
3775 ret = ctdb_transaction_commit(h);
3777 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3781 if (file != stdin) {
3784 talloc_free(tmp_ctx);
3788 if (file != stdin) {
3792 talloc_free(tmp_ctx);
3797 check if a service is bound to a port or not
3799 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
3804 struct sockaddr_in sin;
3807 printf("Use: ctdb chktcport <port>\n");
3811 port = atoi(argv[0]);
3813 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
3815 printf("Failed to open local socket\n");
3819 v = fcntl(s, F_GETFL, 0);
3820 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
3821 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
3824 bzero(&sin, sizeof(sin));
3825 sin.sin_family = AF_INET;
3826 sin.sin_port = htons(port);
3827 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
3830 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
3838 /* Reload public IPs on a specified nodes */
3839 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
3841 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3847 assert_single_node_only();
3853 /* Determine the nodes where IPs need to be reloaded */
3854 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
3855 options.pnn, true, &nodes, &pnn_mode)) {
3861 /* Disable takeover runs on all connected nodes. A reply
3862 * indicating success is needed from each node so all nodes
3863 * will need to be active. This will retry until maxruntime
3864 * is exceeded, hence no error handling.
3866 * A check could be added to not allow reloading of IPs when
3867 * there are disconnected nodes. However, this should
3868 * probably be left up to the administrator.
3870 timeout = LONGTIMEOUT;
3871 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
3872 "Disable takeover runs", true);
3874 /* Now tell all the desired nodes to reload their public IPs.
3875 * Keep trying this until it succeeds. This assumes all
3876 * failures are transient, which might not be true...
3878 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
3879 nodes, 0, LONGTIMELIMIT(),
3881 NULL, NULL, NULL) != 0) {
3883 ("Unable to reload IPs on some nodes, try again.\n"));
3887 /* It isn't strictly necessary to wait until takeover runs are
3888 * re-enabled but doing so can't hurt.
3891 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
3892 "Enable takeover runs", true);
3898 talloc_free(tmp_ctx);
3903 display a list of the databases on a remote ctdb
3905 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3908 struct ctdb_dbid_map_old *dbmap=NULL;
3910 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3912 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3916 if(options.machinereadable){
3917 printm(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
3918 for(i=0;i<dbmap->num;i++){
3919 const char *path = NULL;
3920 const char *name = NULL;
3921 const char *health = NULL;
3926 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3927 dbmap->dbs[i].db_id, ctdb, &path);
3928 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3929 dbmap->dbs[i].db_id, ctdb, &name);
3930 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3931 dbmap->dbs[i].db_id, ctdb, &health);
3932 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3933 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3934 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
3935 printm(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
3936 dbmap->dbs[i].db_id, name, path,
3937 !!(persistent), !!(sticky),
3938 !!(health), !!(readonly));
3943 printf("Number of databases:%d\n", dbmap->num);
3944 for(i=0;i<dbmap->num;i++){
3945 const char *path = NULL;
3946 const char *name = NULL;
3947 const char *health = NULL;
3952 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &path);
3953 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &name);
3954 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &health);
3955 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3956 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3957 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
3958 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
3959 dbmap->dbs[i].db_id, name, path,
3960 persistent?" PERSISTENT":"",
3961 sticky?" STICKY":"",
3962 readonly?" READONLY":"",
3963 health?" UNHEALTHY":"");
3970 display the status of a database on a remote ctdb
3972 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3974 const char *db_name;
3977 const char *path = NULL;
3978 const char *health = NULL;
3984 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
3988 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
3989 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
3990 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
3991 db_id, db_name, path,
3992 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
3993 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
3994 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
3995 (health ? health : "OK"));
4001 check if the local node is recmaster or not
4002 it will return 1 if this node is the recmaster and 0 if it is not
4003 or if the local ctdb daemon could not be contacted
4005 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4007 uint32_t mypnn, recmaster;
4010 assert_single_node_only();
4012 mypnn = getpnn(ctdb);
4014 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4016 printf("Failed to get the recmaster\n");
4020 if (recmaster != mypnn) {
4021 printf("this node is not the recmaster\n");
4025 printf("this node is the recmaster\n");
4032 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4035 struct timeval tv = timeval_current();
4036 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4038 printf("Unable to get ping response from node %u\n", options.pnn);
4041 printf("response from %u time=%.6f sec (%d clients)\n",
4042 options.pnn, timeval_elapsed(&tv), ret);
4049 get a node's runstate
4051 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4054 enum ctdb_runstate runstate;
4056 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4058 printf("Unable to get runstate response from node %u\n",
4063 enum ctdb_runstate t;
4065 for (i=0; i<argc; i++) {
4067 t = runstate_from_string(argv[i]);
4068 if (t == CTDB_RUNSTATE_UNKNOWN) {
4069 printf("Invalid run state (%s)\n", argv[i]);
4073 if (t == runstate) {
4080 printf("CTDB not in required run state (got %s)\n",
4081 runstate_to_string((enum ctdb_runstate)runstate));
4086 printf("%s\n", runstate_to_string(runstate));
4094 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4105 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4107 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4111 printf("%-23s = %u\n", name, value);
4118 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4129 value = strtoul(argv[1], NULL, 0);
4131 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4133 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4137 DEBUG(DEBUG_WARNING,
4138 ("Setting obsolete tunable variable '%s'\n",
4147 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4153 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4155 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4159 for (i=0;i<count;i++) {
4160 control_getvar(ctdb, 1, &list[i]);
4169 display debug level on a node
4171 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4176 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4178 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4181 enum debug_level log_level = debug_level_from_int(level);
4182 const char *desc = debug_level_to_string(log_level);
4184 /* This should never happen */
4187 if (options.machinereadable){
4188 printm(":Name:Level:\n");
4189 printm(":%s:%d:\n", desc, level);
4191 printf("Node %u is at debug level %s (%d)\n",
4192 options.pnn, desc, level);
4199 display reclock file of a node
4201 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4204 const char *reclock;
4206 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4208 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4211 if (reclock != NULL) {
4212 printf("%s\n", reclock);
4219 set the lmaster role on/off
4221 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4224 uint32_t lmasterrole = 0;
4230 if (!strcmp(argv[0], "on")) {
4232 } else if (!strcmp(argv[0], "off")) {
4238 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4240 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4248 set the recmaster role on/off
4250 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4253 uint32_t recmasterrole = 0;
4259 if (!strcmp(argv[0], "on")) {
4261 } else if (!strcmp(argv[0], "off")) {
4267 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4269 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4277 set debug level on a node or all nodes
4279 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4283 enum debug_level log_level;
4286 printf("You must specify the debug level. Valid levels are:\n");
4287 printf("\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
4291 if (!debug_level_parse(argv[0], &log_level)) {
4292 printf("Invalid debug level, must be one of\n");
4293 printf("\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
4297 level = debug_level_to_int(log_level);
4299 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4301 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4307 attach to a database
4309 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4311 const char *db_name;
4312 struct ctdb_db_context *ctdb_db;
4313 bool persistent = false;
4323 if (strcmp(argv[1], "persistent") != 0) {
4329 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4330 if (ctdb_db == NULL) {
4331 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4339 * detach from a database
4341 static int control_detach(struct ctdb_context *ctdb, int argc,
4346 int ret, i, status = 0;
4347 struct ctdb_node_map_old *nodemap = NULL;
4348 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4355 assert_single_node_only();
4357 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
4360 DEBUG(DEBUG_ERR, ("Database cannot be detached "
4361 "when recovery is active\n"));
4362 talloc_free(tmp_ctx);
4366 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4369 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4371 talloc_free(tmp_ctx);
4375 for (i=0; i<nodemap->num; i++) {
4378 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
4382 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
4386 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
4387 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
4388 "inactive (stopped or banned) node "
4389 "%u\n", nodemap->nodes[i].pnn));
4390 talloc_free(tmp_ctx);
4394 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
4395 nodemap->nodes[i].pnn,
4396 "AllowClientDBAttach",
4399 DEBUG(DEBUG_ERR, ("Unable to get tunable "
4400 "AllowClientDBAttach from node %u\n",
4401 nodemap->nodes[i].pnn));
4402 talloc_free(tmp_ctx);
4407 DEBUG(DEBUG_ERR, ("Database access is still active on "
4408 "node %u. Set AllowClientDBAttach=0 "
4410 nodemap->nodes[i].pnn));
4411 talloc_free(tmp_ctx);
4416 talloc_free(tmp_ctx);
4418 for (i=0; i<argc; i++) {
4419 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
4423 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
4424 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
4425 "cannot be detached\n", argv[i]));
4430 ret = ctdb_detach(ctdb, db_id);
4432 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
4442 set the sticky records capability for a database
4444 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
4446 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4454 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
4458 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
4460 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
4461 talloc_free(tmp_ctx);
4465 talloc_free(tmp_ctx);
4470 set the readonly capability for a database
4472 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4474 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4482 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
4486 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4488 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4489 talloc_free(tmp_ctx);
4493 talloc_free(tmp_ctx);
4500 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4510 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
4514 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
4516 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
4520 printf("Sequence number:%lld\n", (long long)seqnum);
4526 run an eventscript on a node
4528 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
4534 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4537 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4541 data.dptr = (unsigned char *)discard_const(argv[0]);
4542 data.dsize = strlen((char *)data.dptr) + 1;
4544 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
4546 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
4547 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4548 if (ret != 0 || res != 0) {
4549 if (errmsg != NULL) {
4551 ("Failed to run eventscripts - %s\n", errmsg));
4553 DEBUG(DEBUG_ERR, ("Failed to run eventscripts\n"));
4555 talloc_free(tmp_ctx);
4558 talloc_free(tmp_ctx);
4562 #define DB_VERSION 1
4563 #define MAX_DB_NAME 64
4564 struct db_file_header {
4565 unsigned long version;
4567 unsigned long persistent;
4569 const char name[MAX_DB_NAME];
4572 struct backup_data {
4573 struct ctdb_marshall_buffer *records;
4576 bool traverse_error;
4579 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
4581 struct backup_data *bd = talloc_get_type(private, struct backup_data);
4582 struct ctdb_rec_data_old *rec;
4584 /* add the record */
4585 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
4587 bd->traverse_error = true;
4588 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
4591 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
4592 if (bd->records == NULL) {
4593 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
4594 bd->traverse_error = true;
4597 bd->records->count++;
4598 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
4599 bd->len += rec->length;
4607 * backup a database to a file
4609 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
4611 const char *db_name;
4613 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4614 struct db_file_header dbhdr;
4615 struct ctdb_db_context *ctdb_db;
4616 struct backup_data *bd;
4619 const char *reason = NULL;
4623 assert_single_node_only();
4626 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4630 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4634 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4635 db_id, tmp_ctx, &reason);
4637 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4639 talloc_free(tmp_ctx);
4643 uint32_t allow_unhealthy = 0;
4645 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4646 "AllowUnhealthyDBRead",
4649 if (allow_unhealthy != 1) {
4650 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4653 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
4655 talloc_free(tmp_ctx);
4659 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4661 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4662 "tunnable AllowUnhealthyDBRead = %u\n",
4666 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4667 if (ctdb_db == NULL) {
4668 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4669 talloc_free(tmp_ctx);
4674 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4676 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4677 talloc_free(tmp_ctx);
4682 bd = talloc_zero(tmp_ctx, struct backup_data);
4684 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4685 talloc_free(tmp_ctx);
4689 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4690 if (bd->records == NULL) {
4691 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4692 talloc_free(tmp_ctx);
4696 bd->len = offsetof(struct ctdb_marshall_buffer, data);
4697 bd->records->db_id = ctdb_db->db_id;
4698 /* traverse the database collecting all records */
4699 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4700 bd->traverse_error) {
4701 DEBUG(DEBUG_ERR,("Traverse error\n"));
4702 talloc_free(tmp_ctx);
4706 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4709 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4711 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4712 talloc_free(tmp_ctx);
4717 dbhdr.version = DB_VERSION;
4718 dbhdr.timestamp = time(NULL);
4719 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4720 dbhdr.size = bd->len;
4721 if (strlen(argv[0]) >= MAX_DB_NAME) {
4722 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4725 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
4726 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
4728 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4731 ret = sys_write(fh, bd->records, bd->len);
4733 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4742 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4746 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
4748 talloc_free(tmp_ctx);
4753 * restore a database from a file
4755 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4758 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4761 struct db_file_header dbhdr;
4762 struct ctdb_db_context *ctdb_db;
4763 struct ctdb_node_map_old *nodemap=NULL;
4764 struct ctdb_vnn_map *vnnmap=NULL;
4766 struct ctdb_transdb w;
4768 uint32_t generation;
4773 assert_single_node_only();
4775 if (argc < 1 || argc > 2) {
4776 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4780 fh = open(argv[0], O_RDONLY);
4782 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4783 talloc_free(tmp_ctx);
4787 sys_read(fh, &dbhdr, sizeof(dbhdr));
4788 if (dbhdr.version != DB_VERSION) {
4789 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4791 talloc_free(tmp_ctx);
4795 dbname = discard_const(dbhdr.name);
4797 dbname = discard_const(argv[1]);
4800 outdata.dsize = dbhdr.size;
4801 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4802 if (outdata.dptr == NULL) {
4803 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4805 talloc_free(tmp_ctx);
4808 sys_read(fh, outdata.dptr, outdata.dsize);
4811 tm = localtime(&dbhdr.timestamp);
4812 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4813 printf("Restoring database '%s' from backup @ %s\n",
4817 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
4818 if (ctdb_db == NULL) {
4819 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4820 talloc_free(tmp_ctx);
4824 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4826 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4827 talloc_free(tmp_ctx);
4832 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4834 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4835 talloc_free(tmp_ctx);
4839 /* freeze all nodes */
4840 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4841 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4842 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4848 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4849 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4850 talloc_free(tmp_ctx);
4855 generation = vnnmap->generation;
4856 data.dptr = (void *)&generation;
4857 data.dsize = sizeof(generation);
4859 /* start a cluster wide transaction */
4860 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4861 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4863 TIMELIMIT(), false, data,
4866 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4871 w.db_id = ctdb_db->db_id;
4874 data.dptr = (void *)&w;
4875 data.dsize = sizeof(w);
4877 /* wipe all the remote databases. */
4878 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4879 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4881 TIMELIMIT(), false, data,
4884 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4885 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4886 talloc_free(tmp_ctx);
4890 /* push the database */
4891 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4892 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4894 TIMELIMIT(), false, outdata,
4897 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4898 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4899 talloc_free(tmp_ctx);
4903 data.dptr = (void *)&ctdb_db->db_id;
4904 data.dsize = sizeof(ctdb_db->db_id);
4906 /* mark the database as healthy */
4907 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4908 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4910 TIMELIMIT(), false, data,
4913 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4914 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4915 talloc_free(tmp_ctx);
4919 data.dptr = (void *)&generation;
4920 data.dsize = sizeof(generation);
4922 /* commit all the changes */
4923 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4925 TIMELIMIT(), false, data,
4928 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4929 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4930 talloc_free(tmp_ctx);
4935 /* thaw all nodes */
4936 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4937 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4943 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4944 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4945 talloc_free(tmp_ctx);
4950 talloc_free(tmp_ctx);
4955 * dump a database backup from a file
4957 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4959 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4961 struct db_file_header dbhdr;
4965 struct ctdb_rec_data_old *rec = NULL;
4966 struct ctdb_marshall_buffer *m;
4967 struct ctdb_dump_db_context c;
4969 assert_single_node_only();
4972 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4976 fh = open(argv[0], O_RDONLY);
4978 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4979 talloc_free(tmp_ctx);
4983 sys_read(fh, &dbhdr, sizeof(dbhdr));
4984 if (dbhdr.version != DB_VERSION) {
4985 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4987 talloc_free(tmp_ctx);
4991 outdata.dsize = dbhdr.size;
4992 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4993 if (outdata.dptr == NULL) {
4994 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4996 talloc_free(tmp_ctx);
4999 sys_read(fh, outdata.dptr, outdata.dsize);
5001 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5003 tm = localtime(&dbhdr.timestamp);
5004 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5005 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5006 dbhdr.name, m->db_id, tbuf);
5011 c.printemptyrecords = (bool)options.printemptyrecords;
5012 c.printdatasize = (bool)options.printdatasize;
5013 c.printlmaster = false;
5014 c.printhash = (bool)options.printhash;
5015 c.printrecordflags = (bool)options.printrecordflags;
5017 for (i=0; i < m->count; i++) {
5021 /* we do not want the header splitted, so we pass NULL*/
5022 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5025 ctdb_dumpdb_record(key, data, &c);
5028 printf("Dumped %d records\n", i);
5029 talloc_free(tmp_ctx);
5034 * wipe a database from a file
5036 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5039 const char *db_name;
5041 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5043 struct ctdb_db_context *ctdb_db;
5044 struct ctdb_node_map_old *nodemap = NULL;
5045 struct ctdb_vnn_map *vnnmap = NULL;
5047 struct ctdb_transdb w;
5049 uint32_t generation;
5052 assert_single_node_only();
5055 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5059 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5063 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5064 if (ctdb_db == NULL) {
5065 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5067 talloc_free(tmp_ctx);
5071 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5074 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5076 talloc_free(tmp_ctx);
5080 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5083 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5085 talloc_free(tmp_ctx);
5089 /* freeze all nodes */
5090 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5091 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5092 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5099 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5100 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5101 CTDB_RECOVERY_ACTIVE);
5102 talloc_free(tmp_ctx);
5107 generation = vnnmap->generation;
5108 data.dptr = (void *)&generation;
5109 data.dsize = sizeof(generation);
5111 /* start a cluster wide transaction */
5112 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5113 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5115 TIMELIMIT(), false, data,
5119 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5120 "transactions.\n"));
5124 w.db_id = ctdb_db->db_id;
5127 data.dptr = (void *)&w;
5128 data.dsize = sizeof(w);
5130 /* wipe all the remote databases. */
5131 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5132 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5134 TIMELIMIT(), false, data,
5137 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5138 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5139 talloc_free(tmp_ctx);
5143 data.dptr = (void *)&ctdb_db->db_id;
5144 data.dsize = sizeof(ctdb_db->db_id);
5146 /* mark the database as healthy */
5147 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5148 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5150 TIMELIMIT(), false, data,
5153 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5154 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5155 talloc_free(tmp_ctx);
5159 data.dptr = (void *)&generation;
5160 data.dsize = sizeof(generation);
5162 /* commit all the changes */
5163 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5165 TIMELIMIT(), false, data,
5168 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5169 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5170 talloc_free(tmp_ctx);
5174 /* thaw all nodes */
5175 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5176 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5182 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5183 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5184 talloc_free(tmp_ctx);
5188 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5190 talloc_free(tmp_ctx);
5197 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5203 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5204 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5205 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5206 if (ret != 0 || res != 0) {
5207 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5208 talloc_free(tmp_ctx);
5211 sys_write(1, data.dptr, data.dsize);
5212 talloc_free(tmp_ctx);
5217 handler for memory dumps
5219 static void mem_dump_handler(uint64_t srvid, TDB_DATA data, void *private_data)
5221 sys_write(1, data.dptr, data.dsize);
5226 dump memory usage on the recovery daemon
5228 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5232 struct ctdb_srvid_message rd;
5234 rd.pnn = ctdb_get_pnn(ctdb);
5235 rd.srvid = getpid();
5237 /* register a message port for receiveing the reply so that we
5238 can receive the reply
5240 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5243 data.dptr = (uint8_t *)&rd;
5244 data.dsize = sizeof(rd);
5246 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5248 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5252 /* this loop will terminate when we have received the reply */
5254 tevent_loop_once(ctdb->ev);
5261 send a message to a srvid
5263 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5265 unsigned long srvid;
5273 srvid = strtoul(argv[0], NULL, 0);
5275 data.dptr = (uint8_t *)discard_const(argv[1]);
5276 data.dsize= strlen(argv[1]);
5278 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5280 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5288 handler for msglisten
5290 static void msglisten_handler(uint64_t srvid, TDB_DATA data,
5295 printf("Message received: ");
5296 for (i=0;i<data.dsize;i++) {
5297 printf("%c", data.dptr[i]);
5303 listen for messages on a messageport
5305 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5311 /* register a message port and listen for messages
5313 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5314 printf("Listening for messages on srvid:%d\n", (int)srvid);
5317 tevent_loop_once(ctdb->ev);
5324 list all nodes in the cluster
5325 we parse the nodes file directly
5327 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5329 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5330 struct ctdb_node_map_old *node_map;
5333 assert_single_node_only();
5335 node_map = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
5336 if (node_map == NULL) {
5337 talloc_free(mem_ctx);
5341 for (i = 0; i < node_map->num; i++) {
5344 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
5347 addr = ctdb_addr_to_str(&node_map->nodes[i].addr);
5348 if (options.machinereadable){
5349 printm(":%d:%s:\n", node_map->nodes[i].pnn, addr);
5351 printf("%s\n", addr);
5354 talloc_free(mem_ctx);
5359 /**********************************************************************/
5360 /* reload the nodes file on all nodes */
5362 static void get_nodes_files_callback(struct ctdb_context *ctdb,
5363 uint32_t node_pnn, int32_t res,
5364 TDB_DATA outdata, void *callback_data)
5366 struct ctdb_node_map_old **maps =
5367 talloc_get_type(callback_data, struct ctdb_node_map_old *);
5369 if (outdata.dsize < offsetof(struct ctdb_node_map_old, nodes) ||
5370 outdata.dptr == NULL) {
5372 (__location__ " Invalid return data: %u %p\n",
5373 (unsigned)outdata.dsize, outdata.dptr));
5377 if (node_pnn >= talloc_array_length(maps)) {
5379 (__location__ " unexpected PNN %u\n", node_pnn));
5383 maps[node_pnn] = talloc_memdup(maps, outdata.dptr, outdata.dsize);
5386 static void get_nodes_files_fail_callback(struct ctdb_context *ctdb,
5387 uint32_t node_pnn, int32_t res,
5388 TDB_DATA outdata, void *callback_data)
5391 ("ERROR: Failed to get nodes file from node %u\n", node_pnn));
5394 static struct ctdb_node_map_old **
5395 ctdb_get_nodes_files(struct ctdb_context *ctdb,
5396 TALLOC_CTX *mem_ctx,
5397 struct timeval timeout,
5398 struct ctdb_node_map_old *nodemap)
5402 struct ctdb_node_map_old **maps;
5404 maps = talloc_zero_array(mem_ctx, struct ctdb_node_map_old *, nodemap->num);
5405 CTDB_NO_MEMORY_NULL(ctdb, maps);
5407 nodes = list_of_connected_nodes(ctdb, nodemap, mem_ctx, true);
5409 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_NODES_FILE,
5410 nodes, 0, TIMELIMIT(),
5412 get_nodes_files_callback,
5413 get_nodes_files_fail_callback,
5423 static bool node_files_are_identical(struct ctdb_node_map_old *nm1,
5424 struct ctdb_node_map_old *nm2)
5428 if (nm1->num != nm2->num) {
5431 for (i = 0; i < nm1->num; i++) {
5432 if (memcmp(&nm1->nodes[i], &nm2->nodes[i],
5433 sizeof(struct ctdb_node_and_flags)) != 0) {
5441 static bool check_all_node_files_are_identical(struct ctdb_context *ctdb,
5442 TALLOC_CTX *mem_ctx,
5443 struct timeval timeout,
5444 struct ctdb_node_map_old *nodemap,
5445 struct ctdb_node_map_old *file_nodemap)
5447 static struct ctdb_node_map_old **maps;
5451 maps = ctdb_get_nodes_files(ctdb, mem_ctx, timeout, nodemap);
5456 for (i = 0; i < talloc_array_length(maps); i++) {
5457 if (maps[i] == NULL) {
5460 if (!node_files_are_identical(file_nodemap, maps[i])) {
5462 ("ERROR: Nodes file on node %u differs from current node (%u)\n",
5463 i, ctdb_get_pnn(ctdb)));
5472 reload the nodes file on the local node
5474 static bool sanity_check_nodes_file_changes(TALLOC_CTX *mem_ctx,
5475 struct ctdb_node_map_old *nodemap,
5476 struct ctdb_node_map_old *file_nodemap)
5479 bool should_abort = false;
5480 bool have_changes = false;
5482 for (i=0; i<nodemap->num; i++) {
5483 if (i >= file_nodemap->num) {
5485 ("ERROR: Node %u (%s) missing from nodes file\n",
5486 nodemap->nodes[i].pnn,
5487 ctdb_addr_to_str(&nodemap->nodes[i].addr)));
5488 should_abort = true;
5491 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED &&
5492 file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5493 /* Node remains deleted */
5495 ("Node %u is unchanged (DELETED)\n",
5496 nodemap->nodes[i].pnn));
5497 } else if (!(nodemap->nodes[i].flags & NODE_FLAGS_DELETED) &&
5498 !(file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED)) {
5499 /* Node not newly nor previously deleted */
5500 if (!ctdb_same_ip(&nodemap->nodes[i].addr,
5501 &file_nodemap->nodes[i].addr)) {
5503 ("ERROR: Node %u has changed IP address (was %s, now %s)\n",
5504 nodemap->nodes[i].pnn,
5505 /* ctdb_addr_to_str() returns a static */
5506 talloc_strdup(mem_ctx,
5507 ctdb_addr_to_str(&nodemap->nodes[i].addr)),
5508 ctdb_addr_to_str(&file_nodemap->nodes[i].addr)));
5509 should_abort = true;
5512 ("Node %u is unchanged\n",
5513 nodemap->nodes[i].pnn));
5514 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5515 DEBUG(DEBUG_WARNING,
5516 ("WARNING: Node %u is disconnected. You MUST fix this node manually!\n",
5517 nodemap->nodes[i].pnn));
5520 } else if (file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5521 /* Node is being deleted */
5523 ("Node %u is DELETED\n",
5524 nodemap->nodes[i].pnn));
5525 have_changes = true;
5526 if (!(nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED)) {
5528 ("ERROR: Node %u is still connected\n",
5529 nodemap->nodes[i].pnn));
5530 should_abort = true;
5532 } else if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5533 /* Node was previously deleted */
5535 ("Node %u is UNDELETED\n", nodemap->nodes[i].pnn));
5536 have_changes = true;
5542 ("ERROR: Nodes will not be reloaded due to previous error\n"));
5543 talloc_free(mem_ctx);
5547 /* Leftover nodes in file are NEW */
5548 for (; i < file_nodemap->num; i++) {
5549 DEBUG(DEBUG_NOTICE, ("Node %u is NEW\n",
5550 file_nodemap->nodes[i].pnn));
5551 have_changes = true;
5554 return have_changes;
5557 static void reload_nodes_fail_callback(struct ctdb_context *ctdb,
5558 uint32_t node_pnn, int32_t res,
5559 TDB_DATA outdata, void *callback_data)
5561 DEBUG(DEBUG_WARNING,
5562 ("WARNING: Node %u failed to reload nodes. You MUST fix this node manually!\n",
5566 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5569 struct ctdb_node_map_old *nodemap=NULL;
5570 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
5571 struct ctdb_node_map_old *file_nodemap;
5575 assert_current_node_only(ctdb);
5577 /* Load both the current nodemap and the contents of the local
5578 * nodes file. Compare and sanity check them before doing
5581 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5583 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5587 file_nodemap = read_nodes_file(tmp_ctx, ctdb->pnn);
5588 if (file_nodemap == NULL) {
5589 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5590 talloc_free(tmp_ctx);
5594 if (!check_all_node_files_are_identical(ctdb, tmp_ctx, TIMELIMIT(),
5595 nodemap, file_nodemap)) {
5599 if (!sanity_check_nodes_file_changes(tmp_ctx, nodemap, file_nodemap)) {
5601 ("No change in nodes file, skipping unnecessary reload\n"));
5602 talloc_free(tmp_ctx);
5606 /* Now make the changes */
5607 conn = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
5608 for (i = 0; i < talloc_array_length(conn); i++) {
5609 DEBUG(DEBUG_INFO, ("Reloading nodes file on node %u\n",
5613 /* Another timeout could be used, such as ReRecoveryTimeout or
5614 * a new one for this purpose. However, this is the simplest
5616 timeout = options.timelimit;
5617 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
5618 "Disable recoveries", true);
5621 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_NODES_FILE,
5622 conn, 0, TIMELIMIT(),
5624 NULL, reload_nodes_fail_callback,
5628 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
5629 "Enable recoveries", true);
5631 talloc_free(tmp_ctx);
5637 static const struct {
5639 int (*fn)(struct ctdb_context *, int, const char **);
5641 bool without_daemon; /* can be run without daemon running ? */
5644 } ctdb_commands[] = {
5645 { "version", control_version, true, true, "show version of ctdb" },
5646 { "status", control_status, true, false, "show node status" },
5647 { "uptime", control_uptime, true, false, "show node uptime" },
5648 { "ping", control_ping, true, false, "ping all nodes" },
5649 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
5650 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5651 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5652 { "listvars", control_listvars, true, false, "list tunable variables"},
5653 { "statistics", control_statistics, false, false, "show statistics" },
5654 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5655 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5656 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5657 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5658 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5659 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5660 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5661 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5662 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
5663 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
5664 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
5665 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5666 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5667 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5668 { "lvs", control_lvs, false, true, "show lvs configuration", "[master|list|status]" },
5669 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5670 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
5671 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
5672 { "getdebug", control_getdebug, true, false, "get debug level" },
5673 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
5674 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
5675 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
5676 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
5677 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
5678 { "disable", control_disable, true, false, "disable a nodes public IP" },
5679 { "enable", control_enable, true, false, "enable a nodes public IP" },
5680 { "stop", control_stop, true, false, "stop a node" },
5681 { "continue", control_continue, true, false, "re-start a stopped node" },
5682 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
5683 { "unban", control_unban, true, false, "unban a node" },
5684 { "showban", control_showban, true, false, "show ban information"},
5685 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
5686 { "recover", control_recover, true, false, "force recovery" },
5687 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
5688 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
5689 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
5690 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
5691 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5692 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
5693 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
5695 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
5697 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
5698 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
5699 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
5700 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
5701 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
5702 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
5703 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
5704 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
5705 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
5706 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
5707 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
5708 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
5709 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
5710 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
5711 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
5712 { "natgw", control_natgw, false, true, "show NAT gateway configuration ", "[master|list|status]"},
5713 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
5714 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
5715 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
5716 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
5717 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
5718 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
5719 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
5720 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
5721 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
5722 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
5723 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
5724 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
5725 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
5726 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
5727 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
5728 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
5729 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
5730 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
5731 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
5732 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
5733 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
5739 static void usage(void)
5743 "Usage: ctdb [options] <control>\n" \
5745 " -n <node> choose node number, or 'all' (defaults to local node)\n"
5746 " -Y generate machine readable output\n"
5747 " -x <char> specify delimiter for machine readable output\n"
5748 " -v generate verbose output\n"
5749 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
5750 printf("Controls:\n");
5751 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5752 printf(" %-15s %-27s %s\n",
5753 ctdb_commands[i].name,
5754 ctdb_commands[i].args?ctdb_commands[i].args:"",
5755 ctdb_commands[i].msg);
5761 static void ctdb_alarm(int sig)
5763 printf("Maximum runtime exceeded - exiting\n");
5770 int main(int argc, const char *argv[])
5772 struct ctdb_context *ctdb;
5773 char *nodestring = NULL;
5774 int machineparsable = 0;
5775 struct poptOption popt_options[] = {
5778 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
5779 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
5780 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machine readable output", NULL },
5781 { NULL, 'x', POPT_ARG_STRING, &options.machineseparator, 0, "specify separator for machine readable output", "char" },
5782 { NULL, 'X', POPT_ARG_NONE, &machineparsable, 0, "enable machine parsable output with separator |", NULL },
5783 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
5784 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
5785 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
5786 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
5787 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
5788 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
5789 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
5793 const char **extra_argv;
5797 struct tevent_context *ev;
5798 const char *control;
5802 /* set some defaults */
5803 options.maxruntime = 0;
5804 options.timelimit = 10;
5805 options.pnn = CTDB_CURRENT_NODE;
5807 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
5809 while ((opt = poptGetNextOpt(pc)) != -1) {
5812 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
5813 poptBadOption(pc, 0), poptStrerror(opt)));
5818 /* setup the remaining options for the main program to use */
5819 extra_argv = poptGetArgs(pc);
5822 while (extra_argv[extra_argc]) extra_argc++;
5825 if (extra_argc < 1) {
5829 if (options.maxruntime == 0) {
5830 const char *ctdb_timeout;
5831 ctdb_timeout = getenv("CTDB_TIMEOUT");
5832 if (ctdb_timeout != NULL) {
5833 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5835 /* default timeout is 120 seconds */
5836 options.maxruntime = 120;
5840 if (machineparsable) {
5841 options.machineseparator = "|";
5843 if (options.machineseparator != NULL) {
5844 if (strlen(options.machineseparator) != 1) {
5845 printf("Invalid separator \"%s\" - "
5846 "must be single character\n",
5847 options.machineseparator);
5852 options.machinereadable = true;
5853 } else if (options.machinereadable) {
5854 options.machineseparator = ":";
5857 signal(SIGALRM, ctdb_alarm);
5858 alarm(options.maxruntime);
5860 control = extra_argv[0];
5862 /* Default value for CTDB_BASE - don't override */
5863 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
5865 ev = tevent_context_init(NULL);
5867 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5871 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5872 if (strcmp(control, ctdb_commands[i].name) == 0) {
5877 if (i == ARRAY_SIZE(ctdb_commands)) {
5878 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
5882 if (ctdb_commands[i].without_daemon == true) {
5883 if (nodestring != NULL) {
5884 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
5887 ret = ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
5894 /* initialise ctdb */
5895 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
5899 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5901 pnn = find_node_xpnn();
5904 ("Is this node part of a CTDB cluster?\n"));
5909 /* setup the node number(s) to contact */
5910 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
5911 &options.nodes, &options.pnn)) {
5915 if (options.pnn == CTDB_CURRENT_NODE) {
5916 options.pnn = options.nodes[0];
5919 if (ctdb_commands[i].auto_all &&
5920 ((options.pnn == CTDB_BROADCAST_ALL) ||
5921 (options.pnn == CTDB_MULTICAST))) {
5925 for (j = 0; j < talloc_array_length(options.nodes); j++) {
5926 options.pnn = options.nodes[j];
5927 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5930 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5935 (void)poptFreeContext(pc);